blob: c0de30f25185060a083aef4199086fea38819375 [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>
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +053031#include <linux/iomap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040032#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050033#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050034#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070035
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040036#include <trace/events/ext4.h>
37
Lukas Czerner5f95d212012-03-19 23:03:19 -040038/*
39 * used by extent splitting.
40 */
41#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
42 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040043#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
44#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040045
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040046#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
47#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
48
Darrick J. Wong7ac59902012-04-29 18:37:10 -040049static __le32 ext4_extent_block_csum(struct inode *inode,
50 struct ext4_extent_header *eh)
51{
52 struct ext4_inode_info *ei = EXT4_I(inode);
53 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
54 __u32 csum;
55
56 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
57 EXT4_EXTENT_TAIL_OFFSET(eh));
58 return cpu_to_le32(csum);
59}
60
61static int ext4_extent_block_csum_verify(struct inode *inode,
62 struct ext4_extent_header *eh)
63{
64 struct ext4_extent_tail *et;
65
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040066 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040067 return 1;
68
69 et = find_ext4_extent_tail(eh);
70 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
71 return 0;
72 return 1;
73}
74
75static void ext4_extent_block_csum_set(struct inode *inode,
76 struct ext4_extent_header *eh)
77{
78 struct ext4_extent_tail *et;
79
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040080 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040081 return;
82
83 et = find_ext4_extent_tail(eh);
84 et->et_checksum = ext4_extent_block_csum(inode, eh);
85}
86
Lukas Czerner5f95d212012-03-19 23:03:19 -040087static int ext4_split_extent_at(handle_t *handle,
88 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040089 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -040090 ext4_lblk_t split,
91 int split_flag,
92 int flags);
93
Jan Karaa4130362019-11-05 17:44:16 +010094static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
Alex Tomasa86c6182006-10-11 01:21:03 -070095{
Theodore Ts'o7b808192016-04-25 23:13:17 -040096 /*
Jan Karaa4130362019-11-05 17:44:16 +010097 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
98 * moment, get_block can be called only for blocks inside i_size since
99 * page cache has been already dropped and writes are blocked by
100 * i_mutex. So we can safely drop the i_data_sem here.
Theodore Ts'o7b808192016-04-25 23:13:17 -0400101 */
Jan Karaa4130362019-11-05 17:44:16 +0100102 BUG_ON(EXT4_JOURNAL(inode) == NULL);
brookxu27bc4462020-08-17 15:36:15 +0800103 ext4_discard_preallocations(inode, 0);
Jan Karaa4130362019-11-05 17:44:16 +0100104 up_write(&EXT4_I(inode)->i_data_sem);
105 *dropped = 1;
106 return 0;
107}
Jan Kara487caee2009-08-17 22:17:20 -0400108
Jan Karaa4130362019-11-05 17:44:16 +0100109/*
110 * Make sure 'handle' has at least 'check_cred' credits. If not, restart
111 * transaction with 'restart_cred' credits. The function drops i_data_sem
112 * when restarting transaction and gets it after transaction is restarted.
113 *
114 * The function returns 0 on success, 1 if transaction had to be restarted,
115 * and < 0 in case of fatal error.
116 */
117int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
Jan Kara83448bd2019-11-05 17:44:29 +0100118 int check_cred, int restart_cred,
119 int revoke_cred)
Jan Karaa4130362019-11-05 17:44:16 +0100120{
121 int ret;
122 int dropped = 0;
123
124 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
Jan Kara83448bd2019-11-05 17:44:29 +0100125 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
Jan Karaa4130362019-11-05 17:44:16 +0100126 if (dropped)
127 down_write(&EXT4_I(inode)->i_data_sem);
128 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700129}
130
131/*
132 * could return:
133 * - EROFS
134 * - ENOMEM
135 */
136static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
137 struct ext4_ext_path *path)
138{
139 if (path->p_bh) {
140 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400141 BUFFER_TRACE(path->p_bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200142 return ext4_journal_get_write_access(handle, inode->i_sb,
143 path->p_bh, EXT4_JTR_NONE);
Alex Tomasa86c6182006-10-11 01:21:03 -0700144 }
145 /* path points to leaf/index in inode body */
146 /* we use in-core data, no need to protect them */
147 return 0;
148}
149
150/*
151 * could return:
152 * - EROFS
153 * - ENOMEM
154 * - EIO
155 */
Eric Biggers43f81672019-12-31 12:04:40 -0600156static int __ext4_ext_dirty(const char *where, unsigned int line,
157 handle_t *handle, struct inode *inode,
158 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700159{
160 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400161
162 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700163 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400164 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700165 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400166 err = __ext4_handle_dirty_metadata(where, line, handle,
167 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700168 } else {
169 /* path points to leaf/index in inode body */
170 err = ext4_mark_inode_dirty(handle, inode);
171 }
172 return err;
173}
174
Eric Biggers43f81672019-12-31 12:04:40 -0600175#define ext4_ext_dirty(handle, inode, path) \
176 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
177
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700178static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700179 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500180 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700181{
Alex Tomasa86c6182006-10-11 01:21:03 -0700182 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400183 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700184 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700185
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500186 /*
187 * Try to predict block placement assuming that we are
188 * filling in a file which will eventually be
189 * non-sparse --- i.e., in the case of libbfd writing
190 * an ELF object sections out-of-order but in a way
191 * the eventually results in a contiguous object or
192 * executable file, or some database extending a table
193 * space file. However, this is actually somewhat
194 * non-ideal if we are writing a sparse file such as
195 * qemu or KVM writing a raw image file that is going
196 * to stay fairly sparse, since it will end up
197 * fragmenting the file system's free space. Maybe we
198 * should have some hueristics or some way to allow
199 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800200 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500201 * common.
202 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800203 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500204 if (ex) {
205 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
206 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
207
208 if (block > ext_block)
209 return ext_pblk + (block - ext_block);
210 else
211 return ext_pblk - (ext_block - block);
212 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700213
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700214 /* it looks like index is empty;
215 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700216 if (path[depth].p_bh)
217 return path[depth].p_bh->b_blocknr;
218 }
219
220 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400221 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700222}
223
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400224/*
225 * Allocation for a meta data block
226 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700227static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400228ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700229 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400230 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700231{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700232 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700233
234 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400235 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
236 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700237 return newblock;
238}
239
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400240static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700241{
242 int size;
243
244 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
245 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100246#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400247 if (!check && size > 6)
248 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700249#endif
250 return size;
251}
252
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400253static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700254{
255 int size;
256
257 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
258 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100259#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400260 if (!check && size > 5)
261 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700262#endif
263 return size;
264}
265
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400266static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700267{
268 int size;
269
270 size = sizeof(EXT4_I(inode)->i_data);
271 size -= sizeof(struct ext4_extent_header);
272 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100273#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400274 if (!check && size > 3)
275 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700276#endif
277 return size;
278}
279
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400280static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700281{
282 int size;
283
284 size = sizeof(EXT4_I(inode)->i_data);
285 size -= sizeof(struct ext4_extent_header);
286 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100287#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400288 if (!check && size > 4)
289 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700290#endif
291 return size;
292}
293
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400294static inline int
295ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400296 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400297 int nofail)
298{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400299 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400300 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700301 int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO;
302
303 if (nofail)
304 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400305
Theodore Ts'odfe50802014-09-01 14:37:09 -0400306 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400307 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700308 flags);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400309}
310
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400311static int
312ext4_ext_max_entries(struct inode *inode, int depth)
313{
314 int max;
315
316 if (depth == ext_depth(inode)) {
317 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400318 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400319 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400320 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400321 } else {
322 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400323 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400324 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400325 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400326 }
327
328 return max;
329}
330
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400331static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
332{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400333 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400334 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500335 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400336
Vegard Nossumf70749c2016-06-30 11:53:46 -0400337 /*
338 * We allow neither:
339 * - zero length
340 * - overflow/wrap-around
341 */
342 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400343 return 0;
Jan Karace9f24c2020-07-28 15:04:34 +0200344 return ext4_inode_block_valid(inode, block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400345}
346
347static int ext4_valid_extent_idx(struct inode *inode,
348 struct ext4_extent_idx *ext_idx)
349{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400350 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400351
Jan Karace9f24c2020-07-28 15:04:34 +0200352 return ext4_inode_block_valid(inode, block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400353}
354
355static int ext4_valid_extent_entries(struct inode *inode,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400356 struct ext4_extent_header *eh,
357 ext4_fsblk_t *pblk, int depth)
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400358{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400359 unsigned short entries;
360 if (eh->eh_entries == 0)
361 return 1;
362
363 entries = le16_to_cpu(eh->eh_entries);
364
365 if (depth == 0) {
366 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400367 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500368 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) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400379 *pblk = ext4_ext_pblock(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500380 return 0;
381 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400382 ext++;
383 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500384 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400385 }
386 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400387 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400388 while (entries) {
389 if (!ext4_valid_extent_idx(inode, ext_idx))
390 return 0;
391 ext_idx++;
392 entries--;
393 }
394 }
395 return 1;
396}
397
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400398static int __ext4_ext_check(const char *function, unsigned int line,
399 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400400 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400401{
402 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400403 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400404
405 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
406 error_msg = "invalid magic";
407 goto corrupted;
408 }
409 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
410 error_msg = "unexpected eh_depth";
411 goto corrupted;
412 }
413 if (unlikely(eh->eh_max == 0)) {
414 error_msg = "invalid eh_max";
415 goto corrupted;
416 }
417 max = ext4_ext_max_entries(inode, depth);
418 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
419 error_msg = "too large eh_max";
420 goto corrupted;
421 }
422 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
423 error_msg = "invalid eh_entries";
424 goto corrupted;
425 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400426 if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) {
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400427 error_msg = "invalid extent entries";
428 goto corrupted;
429 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400430 if (unlikely(depth > 32)) {
431 error_msg = "too large eh_depth";
432 goto corrupted;
433 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400434 /* Verify checksum on non-root extent tree nodes */
435 if (ext_depth(inode) != depth &&
436 !ext4_extent_block_csum_verify(inode, eh)) {
437 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400438 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400439 goto corrupted;
440 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400441 return 0;
442
443corrupted:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400444 ext4_error_inode_err(inode, function, line, 0, -err,
445 "pblk %llu bad header/extent: %s - magic %x, "
446 "entries %u, max %u(%u), depth %u(%u)",
447 (unsigned long long) pblk, error_msg,
448 le16_to_cpu(eh->eh_magic),
449 le16_to_cpu(eh->eh_entries),
450 le16_to_cpu(eh->eh_max),
451 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400452 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400453}
454
Theodore Ts'oc3491792013-08-16 21:21:41 -0400455#define ext4_ext_check(inode, eh, depth, pblk) \
456 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400457
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400458int ext4_ext_check_inode(struct inode *inode)
459{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400460 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400461}
462
Dmitry Monakhov40686642019-11-06 12:25:02 +0000463static void ext4_cache_extents(struct inode *inode,
464 struct ext4_extent_header *eh)
465{
466 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
467 ext4_lblk_t prev = 0;
468 int i;
469
470 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
471 unsigned int status = EXTENT_STATUS_WRITTEN;
472 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
473 int len = ext4_ext_get_actual_len(ex);
474
475 if (prev && (prev != lblk))
476 ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
477 EXTENT_STATUS_HOLE);
478
479 if (ext4_ext_is_unwritten(ex))
480 status = EXTENT_STATUS_UNWRITTEN;
481 ext4_es_cache_extent(inode, lblk, len,
482 ext4_ext_pblock(ex), status);
483 prev = lblk + len;
484 }
485}
486
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400487static struct buffer_head *
488__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400489 struct inode *inode, ext4_fsblk_t pblk, int depth,
490 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400491{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400492 struct buffer_head *bh;
493 int err;
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700494 gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400495
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700496 if (flags & EXT4_EX_NOFAIL)
497 gfp_flags |= __GFP_NOFAIL;
498
499 bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400500 if (unlikely(!bh))
501 return ERR_PTR(-ENOMEM);
502
503 if (!bh_uptodate_or_lock(bh)) {
504 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
zhangyi (F)2d069c02020-09-24 15:33:33 +0800505 err = ext4_read_bh(bh, 0, NULL);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400506 if (err < 0)
507 goto errout;
508 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400509 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400510 return bh;
Jan Karace9f24c2020-07-28 15:04:34 +0200511 err = __ext4_ext_check(function, line, inode,
512 ext_block_hdr(bh), depth, pblk);
513 if (err)
514 goto errout;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400515 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400516 /*
517 * If this is a leaf block, cache all of its entries
518 */
519 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
520 struct ext4_extent_header *eh = ext_block_hdr(bh);
Dmitry Monakhov40686642019-11-06 12:25:02 +0000521 ext4_cache_extents(inode, eh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400522 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400523 return bh;
524errout:
525 put_bh(bh);
526 return ERR_PTR(err);
527
Darrick J. Wongf8489122012-04-29 18:21:10 -0400528}
529
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400530#define read_extent_tree_block(inode, pblk, depth, flags) \
531 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
532 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400533
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400534/*
535 * This function is called to cache a file's extent information in the
536 * extent status tree
537 */
538int ext4_ext_precache(struct inode *inode)
539{
540 struct ext4_inode_info *ei = EXT4_I(inode);
541 struct ext4_ext_path *path = NULL;
542 struct buffer_head *bh;
543 int i = 0, depth, ret = 0;
544
545 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
546 return 0; /* not an extent-mapped inode */
547
548 down_read(&ei->i_data_sem);
549 depth = ext_depth(inode);
550
Ritesh Harjani2f424a52020-02-28 14:56:55 +0530551 /* Don't cache anything if there are no external extent blocks */
552 if (!depth) {
553 up_read(&ei->i_data_sem);
554 return ret;
555 }
556
Kees Cook6396bb22018-06-12 14:03:40 -0700557 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400558 GFP_NOFS);
559 if (path == NULL) {
560 up_read(&ei->i_data_sem);
561 return -ENOMEM;
562 }
563
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400564 path[0].p_hdr = ext_inode_hdr(inode);
565 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
566 if (ret)
567 goto out;
568 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
569 while (i >= 0) {
570 /*
571 * If this is a leaf block or we've reached the end of
572 * the index block, go up
573 */
574 if ((i == depth) ||
575 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
576 brelse(path[i].p_bh);
577 path[i].p_bh = NULL;
578 i--;
579 continue;
580 }
581 bh = read_extent_tree_block(inode,
582 ext4_idx_pblock(path[i].p_idx++),
583 depth - i - 1,
584 EXT4_EX_FORCE_CACHE);
585 if (IS_ERR(bh)) {
586 ret = PTR_ERR(bh);
587 break;
588 }
589 i++;
590 path[i].p_bh = bh;
591 path[i].p_hdr = ext_block_hdr(bh);
592 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
593 }
594 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
595out:
596 up_read(&ei->i_data_sem);
597 ext4_ext_drop_refs(path);
598 kfree(path);
599 return ret;
600}
601
Alex Tomasa86c6182006-10-11 01:21:03 -0700602#ifdef EXT_DEBUG
603static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
604{
605 int k, l = path->p_depth;
606
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530607 ext_debug(inode, "path:");
Alex Tomasa86c6182006-10-11 01:21:03 -0700608 for (k = 0; k <= l; k++, path++) {
609 if (path->p_idx) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530610 ext_debug(inode, " %d->%llu",
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600611 le32_to_cpu(path->p_idx->ei_block),
612 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700613 } else if (path->p_ext) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530614 ext_debug(inode, " %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700615 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400616 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400617 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400618 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700619 } else
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530620 ext_debug(inode, " []");
Alex Tomasa86c6182006-10-11 01:21:03 -0700621 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530622 ext_debug(inode, "\n");
Alex Tomasa86c6182006-10-11 01:21:03 -0700623}
624
625static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
626{
627 int depth = ext_depth(inode);
628 struct ext4_extent_header *eh;
629 struct ext4_extent *ex;
630 int i;
631
632 if (!path)
633 return;
634
635 eh = path[depth].p_hdr;
636 ex = EXT_FIRST_EXTENT(eh);
637
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530638 ext_debug(inode, "Displaying leaf extents\n");
Mingming553f9002009-09-18 13:34:55 -0400639
Alex Tomasa86c6182006-10-11 01:21:03 -0700640 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530641 ext_debug(inode, "%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400642 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400643 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700644 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530645 ext_debug(inode, "\n");
Alex Tomasa86c6182006-10-11 01:21:03 -0700646}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400647
648static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
649 ext4_fsblk_t newblock, int level)
650{
651 int depth = ext_depth(inode);
652 struct ext4_extent *ex;
653
654 if (depth != level) {
655 struct ext4_extent_idx *idx;
656 idx = path[level].p_idx;
657 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530658 ext_debug(inode, "%d: move %d:%llu in new index %llu\n",
659 level, le32_to_cpu(idx->ei_block),
660 ext4_idx_pblock(idx), newblock);
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400661 idx++;
662 }
663
664 return;
665 }
666
667 ex = path[depth].p_ext;
668 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530669 ext_debug(inode, "move %d:%llu:[%d]%d in new leaf %llu\n",
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400670 le32_to_cpu(ex->ee_block),
671 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400672 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400673 ext4_ext_get_actual_len(ex),
674 newblock);
675 ex++;
676 }
677}
678
Alex Tomasa86c6182006-10-11 01:21:03 -0700679#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400680#define ext4_ext_show_path(inode, path)
681#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400682#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700683#endif
684
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500685void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700686{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400687 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700688
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400689 if (!path)
690 return;
691 depth = path->p_depth;
Eric Biggersde745482019-12-31 12:04:44 -0600692 for (i = 0; i <= depth; i++, path++) {
Markus Elfringe0f49d22020-06-13 19:12:24 +0200693 brelse(path->p_bh);
694 path->p_bh = NULL;
Eric Biggersde745482019-12-31 12:04:44 -0600695 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700696}
697
698/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700699 * ext4_ext_binsearch_idx:
700 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400701 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700702 */
703static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500704ext4_ext_binsearch_idx(struct inode *inode,
705 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700706{
707 struct ext4_extent_header *eh = path->p_hdr;
708 struct ext4_extent_idx *r, *l, *m;
709
Alex Tomasa86c6182006-10-11 01:21:03 -0700710
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530711 ext_debug(inode, "binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700712
713 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400714 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700715 while (l <= r) {
716 m = l + (r - l) / 2;
717 if (block < le32_to_cpu(m->ei_block))
718 r = m - 1;
719 else
720 l = m + 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530721 ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l,
722 le32_to_cpu(l->ei_block), m, le32_to_cpu(m->ei_block),
723 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700724 }
725
726 path->p_idx = l - 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530727 ext_debug(inode, " -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400728 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700729
730#ifdef CHECK_BINSEARCH
731 {
732 struct ext4_extent_idx *chix, *ix;
733 int k;
734
735 chix = ix = EXT_FIRST_INDEX(eh);
736 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600737 if (k != 0 && le32_to_cpu(ix->ei_block) <=
738 le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400739 printk(KERN_DEBUG "k=%d, ix=0x%p, "
740 "first=0x%p\n", k,
741 ix, EXT_FIRST_INDEX(eh));
742 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700743 le32_to_cpu(ix->ei_block),
744 le32_to_cpu(ix[-1].ei_block));
745 }
746 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400747 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700748 if (block < le32_to_cpu(ix->ei_block))
749 break;
750 chix = ix;
751 }
752 BUG_ON(chix != path->p_idx);
753 }
754#endif
755
756}
757
758/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700759 * ext4_ext_binsearch:
760 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400761 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700762 */
763static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500764ext4_ext_binsearch(struct inode *inode,
765 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700766{
767 struct ext4_extent_header *eh = path->p_hdr;
768 struct ext4_extent *r, *l, *m;
769
Alex Tomasa86c6182006-10-11 01:21:03 -0700770 if (eh->eh_entries == 0) {
771 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700772 * this leaf is empty:
773 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700774 */
775 return;
776 }
777
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530778 ext_debug(inode, "binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700779
780 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400781 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700782
783 while (l <= r) {
784 m = l + (r - l) / 2;
785 if (block < le32_to_cpu(m->ee_block))
786 r = m - 1;
787 else
788 l = m + 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530789 ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l,
790 le32_to_cpu(l->ee_block), m, le32_to_cpu(m->ee_block),
791 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700792 }
793
794 path->p_ext = l - 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530795 ext_debug(inode, " -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400796 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400797 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400798 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400799 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700800
801#ifdef CHECK_BINSEARCH
802 {
803 struct ext4_extent *chex, *ex;
804 int k;
805
806 chex = ex = EXT_FIRST_EXTENT(eh);
807 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
808 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400809 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700810 if (block < le32_to_cpu(ex->ee_block))
811 break;
812 chex = ex;
813 }
814 BUG_ON(chex != path->p_ext);
815 }
816#endif
817
818}
819
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -0700820void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700821{
822 struct ext4_extent_header *eh;
823
824 eh = ext_inode_hdr(inode);
825 eh->eh_depth = 0;
826 eh->eh_entries = 0;
827 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400828 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Anirudh Rayabharamce3aba42021-05-07 00:26:54 +0530829 eh->eh_generation = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700830 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700831}
832
833struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400834ext4_find_extent(struct inode *inode, ext4_lblk_t block,
835 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700836{
837 struct ext4_extent_header *eh;
838 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400839 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
840 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500841 int ret;
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700842 gfp_t gfp_flags = GFP_NOFS;
843
844 if (flags & EXT4_EX_NOFAIL)
845 gfp_flags |= __GFP_NOFAIL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700846
847 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400848 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400849 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
850 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
851 depth);
852 ret = -EFSCORRUPTED;
853 goto err;
854 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700855
Theodore Ts'o10809df82014-09-01 14:40:09 -0400856 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400857 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400858 if (depth > path[0].p_maxdepth) {
859 kfree(path);
860 *orig_path = path = NULL;
861 }
862 }
863 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400864 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700865 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700866 gfp_flags);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400867 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700868 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400869 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700870 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700871 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400872 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700873
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400874 i = depth;
Dmitry Monakhov40686642019-11-06 12:25:02 +0000875 if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
876 ext4_cache_extents(inode, eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700877 /* walk through the tree */
878 while (i) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530879 ext_debug(inode, "depth %d: num %d, max %d\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700880 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400881
Alex Tomasa86c6182006-10-11 01:21:03 -0700882 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400883 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700884 path[ppos].p_depth = i;
885 path[ppos].p_ext = NULL;
886
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400887 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
888 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530889 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400890 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500892 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400893
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 eh = ext_block_hdr(bh);
895 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700896 path[ppos].p_bh = bh;
897 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 }
899
900 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 path[ppos].p_ext = NULL;
902 path[ppos].p_idx = NULL;
903
Alex Tomasa86c6182006-10-11 01:21:03 -0700904 /* find extent */
905 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400906 /* if not an empty leaf */
907 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400908 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700909
910 ext4_ext_show_path(inode, path);
911
912 return path;
913
914err:
915 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400916 kfree(path);
917 if (orig_path)
918 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500919 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700920}
921
922/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700923 * ext4_ext_insert_index:
924 * insert new index [@logical;@ptr] into the block at @curp;
925 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700926 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400927static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
928 struct ext4_ext_path *curp,
929 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700930{
931 struct ext4_extent_idx *ix;
932 int len, err;
933
Avantika Mathur7e028972006-12-06 20:41:33 -0800934 err = ext4_ext_get_access(handle, inode, curp);
935 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700936 return err;
937
Frank Mayhar273df552010-03-02 11:46:09 -0500938 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
939 EXT4_ERROR_INODE(inode,
940 "logical %d == ei_block %d!",
941 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400942 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500943 }
Robin Dongd4620312011-07-17 23:43:42 -0400944
945 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
946 >= le16_to_cpu(curp->p_hdr->eh_max))) {
947 EXT4_ERROR_INODE(inode,
948 "eh_entries %d >= eh_max %d!",
949 le16_to_cpu(curp->p_hdr->eh_entries),
950 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400951 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400952 }
953
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
955 /* insert after */
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530956 ext_debug(inode, "insert new index %d after: %llu\n",
957 logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700958 ix = curp->p_idx + 1;
959 } else {
960 /* insert before */
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530961 ext_debug(inode, "insert new index %d before: %llu\n",
962 logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700963 ix = curp->p_idx;
964 }
965
Eric Gouriou80e675f2011-10-27 11:52:18 -0400966 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
967 BUG_ON(len < 0);
968 if (len > 0) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530969 ext_debug(inode, "insert new index %d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -0400970 "move %d indices from 0x%p to 0x%p\n",
971 logical, len, ix, ix + 1);
972 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
973 }
974
Tao Maf472e022011-10-17 10:13:46 -0400975 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
976 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400977 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -0400978 }
979
Alex Tomasa86c6182006-10-11 01:21:03 -0700980 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700981 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400982 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700983
Frank Mayhar273df552010-03-02 11:46:09 -0500984 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
985 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400986 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500987 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700988
989 err = ext4_ext_dirty(handle, inode, curp);
990 ext4_std_error(inode->i_sb, err);
991
992 return err;
993}
994
995/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700996 * ext4_ext_split:
997 * inserts new subtree into the path, using free index entry
998 * at depth @at:
999 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1000 * - makes decision where to split
1001 * - moves remaining extents and index entries (right to the split point)
1002 * into the newly allocated blocks
1003 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001004 */
1005static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001006 unsigned int flags,
1007 struct ext4_ext_path *path,
1008 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001009{
1010 struct buffer_head *bh = NULL;
1011 int depth = ext_depth(inode);
1012 struct ext4_extent_header *neh;
1013 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001014 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001015 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001017 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001018 gfp_t gfp_flags = GFP_NOFS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001019 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001020 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001021
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001022 if (flags & EXT4_EX_NOFAIL)
1023 gfp_flags |= __GFP_NOFAIL;
1024
Alex Tomasa86c6182006-10-11 01:21:03 -07001025 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001026 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001027
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001028 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001029 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001030 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1031 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001032 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001033 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001034 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1035 border = path[depth].p_ext[1].ee_block;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301036 ext_debug(inode, "leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001037 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001038 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 } else {
1040 border = newext->ee_block;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301041 ext_debug(inode, "leaf will be added."
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001043 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001044 }
1045
1046 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001047 * If error occurs, then we break processing
1048 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001049 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001050 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001051 */
1052
1053 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001054 * Get array to track all allocated blocks.
1055 * We need this to handle errors and free blocks
1056 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001057 */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001058 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001059 if (!ablocks)
1060 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001061
1062 /* allocate all needed blocks */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301063 ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
Alex Tomasa86c6182006-10-11 01:21:03 -07001064 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001065 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001066 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001067 if (newblock == 0)
1068 goto cleanup;
1069 ablocks[a] = newblock;
1070 }
1071
1072 /* initialize new leaf */
1073 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001074 if (unlikely(newblock == 0)) {
1075 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001076 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001077 goto cleanup;
1078 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001079 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001080 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001081 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 goto cleanup;
1083 }
1084 lock_buffer(bh);
1085
Jan Kara188c2992021-08-16 11:57:04 +02001086 err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
1087 EXT4_JTR_NONE);
Avantika Mathur7e028972006-12-06 20:41:33 -08001088 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001089 goto cleanup;
1090
1091 neh = ext_block_hdr(bh);
1092 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001093 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 neh->eh_magic = EXT4_EXT_MAGIC;
1095 neh->eh_depth = 0;
Anirudh Rayabharamce3aba42021-05-07 00:26:54 +05301096 neh->eh_generation = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001097
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001098 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001099 if (unlikely(path[depth].p_hdr->eh_entries !=
1100 path[depth].p_hdr->eh_max)) {
1101 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1102 path[depth].p_hdr->eh_entries,
1103 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001104 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001105 goto cleanup;
1106 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001107 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001108 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1109 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001110 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001111 struct ext4_extent *ex;
1112 ex = EXT_FIRST_EXTENT(neh);
1113 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001114 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001115 }
1116
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001117 /* zero out unused area in the extent block */
1118 ext_size = sizeof(struct ext4_extent_header) +
1119 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1120 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001121 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001122 set_buffer_uptodate(bh);
1123 unlock_buffer(bh);
1124
Frank Mayhar03901312009-01-07 00:06:22 -05001125 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001126 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001127 goto cleanup;
1128 brelse(bh);
1129 bh = NULL;
1130
1131 /* correct old leaf */
1132 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001133 err = ext4_ext_get_access(handle, inode, path + depth);
1134 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001136 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001137 err = ext4_ext_dirty(handle, inode, path + depth);
1138 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001139 goto cleanup;
1140
1141 }
1142
1143 /* create intermediate indexes */
1144 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001145 if (unlikely(k < 0)) {
1146 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001147 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001148 goto cleanup;
1149 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001150 if (k)
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301151 ext_debug(inode, "create %d intermediate indices\n", k);
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 /* insert new index into current index block */
1153 /* current depth stored in i var */
1154 i = depth - 1;
1155 while (k--) {
1156 oldblock = newblock;
1157 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001158 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001159 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001160 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001161 goto cleanup;
1162 }
1163 lock_buffer(bh);
1164
Jan Kara188c2992021-08-16 11:57:04 +02001165 err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
1166 EXT4_JTR_NONE);
Avantika Mathur7e028972006-12-06 20:41:33 -08001167 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001168 goto cleanup;
1169
1170 neh = ext_block_hdr(bh);
1171 neh->eh_entries = cpu_to_le16(1);
1172 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001173 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001174 neh->eh_depth = cpu_to_le16(depth - i);
Anirudh Rayabharamce3aba42021-05-07 00:26:54 +05301175 neh->eh_generation = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001176 fidx = EXT_FIRST_INDEX(neh);
1177 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001178 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001179
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301180 ext_debug(inode, "int.index at %d (block %llu): %u -> %llu\n",
Eric Sandeenbba90742008-01-28 23:58:27 -05001181 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001182
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001183 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001184 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1185 EXT_LAST_INDEX(path[i].p_hdr))) {
1186 EXT4_ERROR_INODE(inode,
1187 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1188 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001189 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001190 goto cleanup;
1191 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001192 /* start copy indexes */
1193 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301194 ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001195 EXT_MAX_INDEX(path[i].p_hdr));
1196 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001197 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001198 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001199 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001200 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001201 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001202 /* zero out unused area in the extent block */
1203 ext_size = sizeof(struct ext4_extent_header) +
1204 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1205 memset(bh->b_data + ext_size, 0,
1206 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001207 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001208 set_buffer_uptodate(bh);
1209 unlock_buffer(bh);
1210
Frank Mayhar03901312009-01-07 00:06:22 -05001211 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001212 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001213 goto cleanup;
1214 brelse(bh);
1215 bh = NULL;
1216
1217 /* correct old index */
1218 if (m) {
1219 err = ext4_ext_get_access(handle, inode, path + i);
1220 if (err)
1221 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001222 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001223 err = ext4_ext_dirty(handle, inode, path + i);
1224 if (err)
1225 goto cleanup;
1226 }
1227
1228 i--;
1229 }
1230
1231 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001232 err = ext4_ext_insert_index(handle, inode, path + at,
1233 le32_to_cpu(border), newblock);
1234
1235cleanup:
1236 if (bh) {
1237 if (buffer_locked(bh))
1238 unlock_buffer(bh);
1239 brelse(bh);
1240 }
1241
1242 if (err) {
1243 /* free all allocated blocks in error case */
1244 for (i = 0; i < depth; i++) {
1245 if (!ablocks[i])
1246 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001247 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001248 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001249 }
1250 }
1251 kfree(ablocks);
1252
1253 return err;
1254}
1255
1256/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001257 * ext4_ext_grow_indepth:
1258 * implements tree growing procedure:
1259 * - allocates new block
1260 * - moves top-level data (index block or leaf) into the new block
1261 * - initializes new top-level, creating index that points to the
1262 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001263 */
1264static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001265 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001266{
Alex Tomasa86c6182006-10-11 01:21:03 -07001267 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001268 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001269 ext4_fsblk_t newblock, goal = 0;
1270 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001271 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001272 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001273
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001274 /* Try to prepend new index to old one */
1275 if (ext_depth(inode))
1276 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1277 if (goal > le32_to_cpu(es->s_first_data_block)) {
1278 flags |= EXT4_MB_HINT_TRY_GOAL;
1279 goal--;
1280 } else
1281 goal = ext4_inode_to_goal_block(inode);
1282 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1283 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001284 if (newblock == 0)
1285 return err;
1286
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001287 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001288 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001289 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001290 lock_buffer(bh);
1291
Jan Kara188c2992021-08-16 11:57:04 +02001292 err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
1293 EXT4_JTR_NONE);
Avantika Mathur7e028972006-12-06 20:41:33 -08001294 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001295 unlock_buffer(bh);
1296 goto out;
1297 }
1298
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001299 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001300 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001301 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1302 /* zero out unused area in the extent block */
1303 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001304
1305 /* set size of new block */
1306 neh = ext_block_hdr(bh);
1307 /* old root could have indexes or leaves
1308 * so calculate e_max right way */
1309 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001310 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001311 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001312 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001313 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001314 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001315 set_buffer_uptodate(bh);
yangerkun0caaefba2021-06-09 15:55:45 +08001316 set_buffer_verified(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001317 unlock_buffer(bh);
1318
Frank Mayhar03901312009-01-07 00:06:22 -05001319 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001320 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001321 goto out;
1322
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001323 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001324 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001325 neh->eh_entries = cpu_to_le16(1);
1326 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1327 if (neh->eh_depth == 0) {
1328 /* Root extent block becomes index block */
1329 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1330 EXT_FIRST_INDEX(neh)->ei_block =
1331 EXT_FIRST_EXTENT(neh)->ee_block;
1332 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301333 ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001334 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001335 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001336 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001337
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001338 le16_add_cpu(&neh->eh_depth, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07001339 err = ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001340out:
1341 brelse(bh);
1342
1343 return err;
1344}
1345
1346/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001347 * ext4_ext_create_new_leaf:
1348 * finds empty index and adds new leaf.
1349 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001350 */
1351static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001352 unsigned int mb_flags,
1353 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001354 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001355 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001356{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001357 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001358 struct ext4_ext_path *curp;
1359 int depth, i, err = 0;
1360
1361repeat:
1362 i = depth = ext_depth(inode);
1363
1364 /* walk up to the tree and look for free index entry */
1365 curp = path + depth;
1366 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1367 i--;
1368 curp--;
1369 }
1370
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001371 /* we use already allocated block for index block,
1372 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001373 if (EXT_HAS_FREE_INDEX(curp)) {
1374 /* if we found index with free entry, then use that
1375 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001376 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001377 if (err)
1378 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001379
1380 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001381 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001382 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001383 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001384 if (IS_ERR(path))
1385 err = PTR_ERR(path);
1386 } else {
1387 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001388 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001389 if (err)
1390 goto out;
1391
1392 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001393 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001394 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001395 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001396 if (IS_ERR(path)) {
1397 err = PTR_ERR(path);
1398 goto out;
1399 }
1400
1401 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001402 * only first (depth 0 -> 1) produces free space;
1403 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001404 */
1405 depth = ext_depth(inode);
1406 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001407 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001408 goto repeat;
1409 }
1410 }
1411
1412out:
1413 return err;
1414}
1415
1416/*
Alex Tomas1988b512008-01-28 23:58:27 -05001417 * search the closest allocated block to the left for *logical
1418 * and returns it at @logical + it's physical address at @phys
1419 * if *logical is the smallest allocated block, the function
1420 * returns 0 at @phys
1421 * return value contains 0 (success) or error code
1422 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001423static int ext4_ext_search_left(struct inode *inode,
1424 struct ext4_ext_path *path,
1425 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001426{
1427 struct ext4_extent_idx *ix;
1428 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001429 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001430
Frank Mayhar273df552010-03-02 11:46:09 -05001431 if (unlikely(path == NULL)) {
1432 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001433 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001434 }
Alex Tomas1988b512008-01-28 23:58:27 -05001435 depth = path->p_depth;
1436 *phys = 0;
1437
1438 if (depth == 0 && path->p_ext == NULL)
1439 return 0;
1440
1441 /* usually extent in the path covers blocks smaller
1442 * then *logical, but it can be that extent is the
1443 * first one in the file */
1444
1445 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001446 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001447 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001448 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1449 EXT4_ERROR_INODE(inode,
1450 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1451 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001452 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001453 }
Alex Tomas1988b512008-01-28 23:58:27 -05001454 while (--depth >= 0) {
1455 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001456 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1457 EXT4_ERROR_INODE(inode,
1458 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001459 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001460 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001461 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001462 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001463 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001464 }
Alex Tomas1988b512008-01-28 23:58:27 -05001465 }
1466 return 0;
1467 }
1468
Frank Mayhar273df552010-03-02 11:46:09 -05001469 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1470 EXT4_ERROR_INODE(inode,
1471 "logical %d < ee_block %d + ee_len %d!",
1472 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001473 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001474 }
Alex Tomas1988b512008-01-28 23:58:27 -05001475
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001476 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001477 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001478 return 0;
1479}
1480
1481/*
yangerkund7dce9e2020-10-28 13:56:17 +08001482 * Search the closest allocated block to the right for *logical
1483 * and returns it at @logical + it's physical address at @phys.
1484 * If not exists, return 0 and @phys is set to 0. We will return
1485 * 1 which means we found an allocated block and ret_ex is valid.
1486 * Or return a (< 0) error code.
Alex Tomas1988b512008-01-28 23:58:27 -05001487 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001488static int ext4_ext_search_right(struct inode *inode,
1489 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001490 ext4_lblk_t *logical, ext4_fsblk_t *phys,
yangerkund7dce9e2020-10-28 13:56:17 +08001491 struct ext4_extent *ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001492{
1493 struct buffer_head *bh = NULL;
1494 struct ext4_extent_header *eh;
1495 struct ext4_extent_idx *ix;
1496 struct ext4_extent *ex;
1497 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001498 int depth; /* Note, NOT eh_depth; depth from top of tree */
1499 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001500
Frank Mayhar273df552010-03-02 11:46:09 -05001501 if (unlikely(path == NULL)) {
1502 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001503 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001504 }
Alex Tomas1988b512008-01-28 23:58:27 -05001505 depth = path->p_depth;
1506 *phys = 0;
1507
1508 if (depth == 0 && path->p_ext == NULL)
1509 return 0;
1510
1511 /* usually extent in the path covers blocks smaller
1512 * then *logical, but it can be that extent is the
1513 * first one in the file */
1514
1515 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001516 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001517 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001518 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1519 EXT4_ERROR_INODE(inode,
1520 "first_extent(path[%d].p_hdr) != ex",
1521 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001522 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001523 }
Alex Tomas1988b512008-01-28 23:58:27 -05001524 while (--depth >= 0) {
1525 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001526 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1527 EXT4_ERROR_INODE(inode,
1528 "ix != EXT_FIRST_INDEX *logical %d!",
1529 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001530 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001531 }
Alex Tomas1988b512008-01-28 23:58:27 -05001532 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001533 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001534 }
1535
Frank Mayhar273df552010-03-02 11:46:09 -05001536 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1537 EXT4_ERROR_INODE(inode,
1538 "logical %d < ee_block %d + ee_len %d!",
1539 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001540 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001541 }
Alex Tomas1988b512008-01-28 23:58:27 -05001542
1543 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1544 /* next allocated block in this leaf */
1545 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001546 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001547 }
1548
1549 /* go up and search for index to the right */
1550 while (--depth >= 0) {
1551 ix = path[depth].p_idx;
1552 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001553 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001554 }
1555
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001556 /* we've gone up to the root and found no index to the right */
1557 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001558
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001559got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001560 /* we've found index to the right, let's
1561 * follow it and find the closest allocated
1562 * block to the right */
1563 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001564 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001565 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001566 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001567 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001568 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001569 if (IS_ERR(bh))
1570 return PTR_ERR(bh);
1571 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001572 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001573 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001574 put_bh(bh);
1575 }
1576
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001577 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001578 if (IS_ERR(bh))
1579 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001580 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001581 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001582found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001583 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001584 *phys = ext4_ext_pblock(ex);
yangerkund7dce9e2020-10-28 13:56:17 +08001585 if (ret_ex)
1586 *ret_ex = *ex;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001587 if (bh)
1588 put_bh(bh);
yangerkund7dce9e2020-10-28 13:56:17 +08001589 return 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001590}
1591
1592/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001593 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001594 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001595 * NOTE: it considers block number from index entry as
1596 * allocated block. Thus, index entries have to be consistent
1597 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001598 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001599ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001600ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1601{
1602 int depth;
1603
1604 BUG_ON(path == NULL);
1605 depth = path->p_depth;
1606
1607 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001608 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001609
1610 while (depth >= 0) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001611 struct ext4_ext_path *p = &path[depth];
1612
Alex Tomasa86c6182006-10-11 01:21:03 -07001613 if (depth == path->p_depth) {
1614 /* leaf */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001615 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
1616 return le32_to_cpu(p->p_ext[1].ee_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001617 } else {
1618 /* index */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001619 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
1620 return le32_to_cpu(p->p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001621 }
1622 depth--;
1623 }
1624
Lukas Czernerf17722f2011-06-06 00:05:17 -04001625 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001626}
1627
1628/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001629 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001630 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001631 */
Robin Dong57187892011-07-23 21:49:07 -04001632static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001633{
1634 int depth;
1635
1636 BUG_ON(path == NULL);
1637 depth = path->p_depth;
1638
1639 /* zero-tree has no leaf blocks at all */
1640 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001641 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001642
1643 /* go to index block */
1644 depth--;
1645
1646 while (depth >= 0) {
1647 if (path[depth].p_idx !=
1648 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001649 return (ext4_lblk_t)
1650 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001651 depth--;
1652 }
1653
Lukas Czernerf17722f2011-06-06 00:05:17 -04001654 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001655}
1656
1657/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001658 * ext4_ext_correct_indexes:
1659 * if leaf gets modified and modified extent is first in the leaf,
1660 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001661 * TODO: do we need to correct tree in all cases?
1662 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001663static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001664 struct ext4_ext_path *path)
1665{
1666 struct ext4_extent_header *eh;
1667 int depth = ext_depth(inode);
1668 struct ext4_extent *ex;
1669 __le32 border;
1670 int k, err = 0;
1671
1672 eh = path[depth].p_hdr;
1673 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001674
1675 if (unlikely(ex == NULL || eh == NULL)) {
1676 EXT4_ERROR_INODE(inode,
1677 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001678 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001679 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001680
1681 if (depth == 0) {
1682 /* there is no tree at all */
1683 return 0;
1684 }
1685
1686 if (ex != EXT_FIRST_EXTENT(eh)) {
1687 /* we correct tree if first leaf got modified only */
1688 return 0;
1689 }
1690
1691 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001692 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 */
1694 k = depth - 1;
1695 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001696 err = ext4_ext_get_access(handle, inode, path + k);
1697 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001698 return err;
1699 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001700 err = ext4_ext_dirty(handle, inode, path + k);
1701 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001702 return err;
1703
1704 while (k--) {
1705 /* change all left-side indexes */
1706 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1707 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001708 err = ext4_ext_get_access(handle, inode, path + k);
1709 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001710 break;
1711 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001712 err = ext4_ext_dirty(handle, inode, path + k);
1713 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001714 break;
1715 }
1716
1717 return err;
1718}
1719
Eric Biggers43f81672019-12-31 12:04:40 -06001720static int ext4_can_extents_be_merged(struct inode *inode,
1721 struct ext4_extent *ex1,
1722 struct ext4_extent *ex2)
Alex Tomasa86c6182006-10-11 01:21:03 -07001723{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001724 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001725
Lukas Czerner556615d2014-04-20 23:45:47 -04001726 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001727 return 0;
1728
1729 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1730 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1731
1732 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001733 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001734 return 0;
1735
Eric Sandeenda0169b2013-11-04 09:58:26 -05001736 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001737 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001738
Lukas Czerner556615d2014-04-20 23:45:47 -04001739 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001740 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001741 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001742#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001743 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001744 return 0;
1745#endif
1746
Theodore Ts'obf89d162010-10-27 21:30:14 -04001747 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001748 return 1;
1749 return 0;
1750}
1751
1752/*
Amit Arora56055d32007-07-17 21:42:38 -04001753 * This function tries to merge the "ex" extent to the next extent in the tree.
1754 * It always tries to merge towards right. If you want to merge towards
1755 * left, pass "ex - 1" as argument instead of "ex".
1756 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1757 * 1 if they got merged.
1758 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001759static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001760 struct ext4_ext_path *path,
1761 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001762{
1763 struct ext4_extent_header *eh;
1764 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001765 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001766
1767 depth = ext_depth(inode);
1768 BUG_ON(path[depth].p_hdr == NULL);
1769 eh = path[depth].p_hdr;
1770
1771 while (ex < EXT_LAST_EXTENT(eh)) {
1772 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1773 break;
1774 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001775 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001776 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1777 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001778 if (unwritten)
1779 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001780
1781 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1782 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1783 * sizeof(struct ext4_extent);
1784 memmove(ex + 1, ex + 2, len);
1785 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001786 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001787 merge_done = 1;
1788 WARN_ON(eh->eh_entries == 0);
1789 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001790 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001791 }
1792
1793 return merge_done;
1794}
1795
1796/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001797 * This function does a very simple check to see if we can collapse
1798 * an extent tree with a single extent tree leaf block into the inode.
1799 */
1800static void ext4_ext_try_to_merge_up(handle_t *handle,
1801 struct inode *inode,
1802 struct ext4_ext_path *path)
1803{
1804 size_t s;
1805 unsigned max_root = ext4_ext_space_root(inode, 0);
1806 ext4_fsblk_t blk;
1807
1808 if ((path[0].p_depth != 1) ||
1809 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1810 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1811 return;
1812
1813 /*
1814 * We need to modify the block allocation bitmap and the block
1815 * group descriptor to release the extent tree block. If we
1816 * can't get the journal credits, give up.
1817 */
Jan Kara83448bd2019-11-05 17:44:29 +01001818 if (ext4_journal_extend(handle, 2,
1819 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001820 return;
1821
1822 /*
1823 * Copy the extent data up to the inode
1824 */
1825 blk = ext4_idx_pblock(path[0].p_idx);
1826 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1827 sizeof(struct ext4_extent_idx);
1828 s += sizeof(struct ext4_extent_header);
1829
Theodore Ts'o10809df82014-09-01 14:40:09 -04001830 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001831 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1832 path[0].p_depth = 0;
1833 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1834 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1835 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1836
1837 brelse(path[1].p_bh);
1838 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001839 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001840}
1841
1842/*
Eric Biggersadde81c2019-12-31 12:04:41 -06001843 * This function tries to merge the @ex extent to neighbours in the tree, then
1844 * tries to collapse the extent tree into the inode.
Yongqiang Yang197217a2011-05-03 11:45:29 -04001845 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001846static void ext4_ext_try_to_merge(handle_t *handle,
1847 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001848 struct ext4_ext_path *path,
Eric Biggersadde81c2019-12-31 12:04:41 -06001849 struct ext4_extent *ex)
1850{
Yongqiang Yang197217a2011-05-03 11:45:29 -04001851 struct ext4_extent_header *eh;
1852 unsigned int depth;
1853 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001854
1855 depth = ext_depth(inode);
1856 BUG_ON(path[depth].p_hdr == NULL);
1857 eh = path[depth].p_hdr;
1858
1859 if (ex > EXT_FIRST_EXTENT(eh))
1860 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1861
1862 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001863 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001864
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001865 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001866}
1867
1868/*
Amit Arora25d14f92007-05-24 13:04:13 -04001869 * check if a portion of the "newext" extent overlaps with an
1870 * existing extent.
1871 *
1872 * If there is an overlap discovered, it updates the length of the newext
1873 * such that there will be no overlap, and then returns 1.
1874 * If there is no overlap found, it returns 0.
1875 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001876static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1877 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001878 struct ext4_extent *newext,
1879 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001880{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001881 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001882 unsigned int depth, len1;
1883 unsigned int ret = 0;
1884
1885 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001886 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001887 depth = ext_depth(inode);
1888 if (!path[depth].p_ext)
1889 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001890 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001891
1892 /*
1893 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001894 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001895 */
1896 if (b2 < b1) {
1897 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001898 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001899 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001900 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001901 }
1902
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001903 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001904 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001905 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001906 newext->ee_len = cpu_to_le16(len1);
1907 ret = 1;
1908 }
1909
1910 /* check for overlap */
1911 if (b1 + len1 > b2) {
1912 newext->ee_len = cpu_to_le16(b2 - b1);
1913 ret = 1;
1914 }
1915out:
1916 return ret;
1917}
1918
1919/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001920 * ext4_ext_insert_extent:
Keyur Patele4d7f2d2020-06-10 23:19:46 -04001921 * tries to merge requested extent into the existing extent or
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001922 * inserts requested extent as new one into the tree,
1923 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001924 */
1925int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001926 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001927 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001928{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001929 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001930 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001931 struct ext4_extent *ex, *fex;
1932 struct ext4_extent *nearex; /* nearest extent */
1933 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001934 int depth, len, err;
1935 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001936 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001937
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001938 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1939 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001940 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1941 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001942 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001943 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001944 depth = ext_depth(inode);
1945 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001946 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001947 if (unlikely(path[depth].p_hdr == NULL)) {
1948 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001949 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001950 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001951
1952 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001953 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001954
1955 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001956 * Try to see whether we should rather test the extent on
1957 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001958 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001959 * left, or on the right from the searched position. This
1960 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001961 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001962 if (ex < EXT_LAST_EXTENT(eh) &&
1963 (le32_to_cpu(ex->ee_block) +
1964 ext4_ext_get_actual_len(ex) <
1965 le32_to_cpu(newext->ee_block))) {
1966 ex += 1;
1967 goto prepend;
1968 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1969 (le32_to_cpu(newext->ee_block) +
1970 ext4_ext_get_actual_len(newext) <
1971 le32_to_cpu(ex->ee_block)))
1972 ex -= 1;
1973
1974 /* Try to append newex to the ex */
1975 if (ext4_can_extents_be_merged(inode, ex, newext)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301976 ext_debug(inode, "append [%d]%d block to %u:[%d]%d"
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001977 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001978 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001979 ext4_ext_get_actual_len(newext),
1980 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001981 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001982 ext4_ext_get_actual_len(ex),
1983 ext4_ext_pblock(ex));
1984 err = ext4_ext_get_access(handle, inode,
1985 path + depth);
1986 if (err)
1987 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001988 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001989 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001990 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001991 if (unwritten)
1992 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001993 eh = path[depth].p_hdr;
1994 nearex = ex;
1995 goto merge;
1996 }
1997
1998prepend:
1999 /* Try to prepend newex to the ex */
2000 if (ext4_can_extents_be_merged(inode, newext, ex)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302001 ext_debug(inode, "prepend %u[%d]%d block to %u:[%d]%d"
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002002 "(from %llu)\n",
2003 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002004 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002005 ext4_ext_get_actual_len(newext),
2006 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002007 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002008 ext4_ext_get_actual_len(ex),
2009 ext4_ext_pblock(ex));
2010 err = ext4_ext_get_access(handle, inode,
2011 path + depth);
2012 if (err)
2013 return err;
2014
Lukas Czerner556615d2014-04-20 23:45:47 -04002015 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002016 ex->ee_block = newext->ee_block;
2017 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2018 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2019 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002020 if (unwritten)
2021 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002022 eh = path[depth].p_hdr;
2023 nearex = ex;
2024 goto merge;
2025 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002026 }
2027
Alex Tomasa86c6182006-10-11 01:21:03 -07002028 depth = ext_depth(inode);
2029 eh = path[depth].p_hdr;
2030 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2031 goto has_space;
2032
2033 /* probably next leaf has space for us? */
2034 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002035 next = EXT_MAX_BLOCKS;
2036 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002037 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002038 if (next != EXT_MAX_BLOCKS) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302039 ext_debug(inode, "next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002040 BUG_ON(npath != NULL);
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002041 npath = ext4_find_extent(inode, next, NULL, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002042 if (IS_ERR(npath))
2043 return PTR_ERR(npath);
2044 BUG_ON(npath->p_depth != path->p_depth);
2045 eh = npath[depth].p_hdr;
2046 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302047 ext_debug(inode, "next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 le16_to_cpu(eh->eh_entries));
2049 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002050 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002051 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302052 ext_debug(inode, "next leaf has no free space(%d,%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002053 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2054 }
2055
2056 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002057 * There is no free space in the found leaf.
2058 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002059 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002060 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002061 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002062 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002063 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002064 if (err)
2065 goto cleanup;
2066 depth = ext_depth(inode);
2067 eh = path[depth].p_hdr;
2068
2069has_space:
2070 nearex = path[depth].p_ext;
2071
Avantika Mathur7e028972006-12-06 20:41:33 -08002072 err = ext4_ext_get_access(handle, inode, path + depth);
2073 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002074 goto cleanup;
2075
2076 if (!nearex) {
2077 /* there is no extent in this leaf, create first one */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302078 ext_debug(inode, "first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002079 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002080 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002081 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002082 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002083 nearex = EXT_FIRST_EXTENT(eh);
2084 } else {
2085 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002086 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002087 /* Insert after */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302088 ext_debug(inode, "insert %u:%llu:[%d]%d before: "
Yongqiang Yang32de6752011-11-01 18:56:41 -04002089 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002090 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002091 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002092 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002093 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002094 nearex);
2095 nearex++;
2096 } else {
2097 /* Insert before */
2098 BUG_ON(newext->ee_block == nearex->ee_block);
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302099 ext_debug(inode, "insert %u:%llu:[%d]%d after: "
Yongqiang Yang32de6752011-11-01 18:56:41 -04002100 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002101 le32_to_cpu(newext->ee_block),
2102 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002103 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002104 ext4_ext_get_actual_len(newext),
2105 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002106 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002107 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2108 if (len > 0) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302109 ext_debug(inode, "insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002110 "move %d extents from 0x%p to 0x%p\n",
2111 le32_to_cpu(newext->ee_block),
2112 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002113 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002114 ext4_ext_get_actual_len(newext),
2115 len, nearex, nearex + 1);
2116 memmove(nearex + 1, nearex,
2117 len * sizeof(struct ext4_extent));
2118 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002119 }
2120
Marcin Slusarze8546d02008-04-17 10:38:59 -04002121 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002122 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002123 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002124 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002125 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002126
2127merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002128 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002129 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002130 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002131
Alex Tomasa86c6182006-10-11 01:21:03 -07002132
2133 /* time to correct all indexes above */
2134 err = ext4_ext_correct_indexes(handle, inode, path);
2135 if (err)
2136 goto cleanup;
2137
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002138 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002139
2140cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002141 ext4_ext_drop_refs(npath);
2142 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002143 return err;
2144}
2145
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002146static int ext4_fill_es_cache_info(struct inode *inode,
2147 ext4_lblk_t block, ext4_lblk_t num,
2148 struct fiemap_extent_info *fieinfo)
2149{
2150 ext4_lblk_t next, end = block + num - 1;
2151 struct extent_status es;
2152 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2153 unsigned int flags;
2154 int err;
2155
2156 while (block <= end) {
2157 next = 0;
2158 flags = 0;
2159 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2160 break;
2161 if (ext4_es_is_unwritten(&es))
2162 flags |= FIEMAP_EXTENT_UNWRITTEN;
2163 if (ext4_es_is_delayed(&es))
2164 flags |= (FIEMAP_EXTENT_DELALLOC |
2165 FIEMAP_EXTENT_UNKNOWN);
2166 if (ext4_es_is_hole(&es))
2167 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2168 if (next == 0)
2169 flags |= FIEMAP_EXTENT_LAST;
2170 if (flags & (FIEMAP_EXTENT_DELALLOC|
2171 EXT4_FIEMAP_EXTENT_HOLE))
2172 es.es_pblk = 0;
2173 else
2174 es.es_pblk = ext4_es_pblock(&es);
2175 err = fiemap_fill_next_extent(fieinfo,
2176 (__u64)es.es_lblk << blksize_bits,
2177 (__u64)es.es_pblk << blksize_bits,
2178 (__u64)es.es_len << blksize_bits,
2179 flags);
2180 if (next == 0)
2181 break;
2182 block = next;
2183 if (err < 0)
2184 return err;
2185 if (err == 1)
2186 return 0;
2187 }
2188 return 0;
2189}
2190
2191
Alex Tomasa86c6182006-10-11 01:21:03 -07002192/*
Jan Kara140a5252016-03-09 22:46:57 -05002193 * ext4_ext_determine_hole - determine hole around given block
2194 * @inode: inode we lookup in
2195 * @path: path in extent tree to @lblk
2196 * @lblk: pointer to logical block around which we want to determine hole
2197 *
2198 * Determine hole length (and start if easily possible) around given logical
2199 * block. We don't try too hard to find the beginning of the hole but @path
2200 * actually points to extent before @lblk, we provide it.
2201 *
2202 * The function returns the length of a hole starting at @lblk. We update @lblk
2203 * to the beginning of the hole if we managed to find it.
2204 */
2205static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2206 struct ext4_ext_path *path,
2207 ext4_lblk_t *lblk)
2208{
2209 int depth = ext_depth(inode);
2210 struct ext4_extent *ex;
2211 ext4_lblk_t len;
2212
2213 ex = path[depth].p_ext;
2214 if (ex == NULL) {
2215 /* there is no extent yet, so gap is [0;-] */
2216 *lblk = 0;
2217 len = EXT_MAX_BLOCKS;
2218 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2219 len = le32_to_cpu(ex->ee_block) - *lblk;
2220 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2221 + ext4_ext_get_actual_len(ex)) {
2222 ext4_lblk_t next;
2223
2224 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2225 next = ext4_ext_next_allocated_block(path);
2226 BUG_ON(next == *lblk);
2227 len = next - *lblk;
2228 } else {
2229 BUG();
2230 }
2231 return len;
2232}
2233
2234/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002235 * ext4_ext_put_gap_in_cache:
2236 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002237 * and cache this gap
2238 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002239static void
Jan Kara140a5252016-03-09 22:46:57 -05002240ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2241 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002242{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002243 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002244
Eric Whitneyad431022018-10-01 14:10:39 -04002245 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2246 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002247 if (es.es_len) {
2248 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002249 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002250 return;
Jan Kara140a5252016-03-09 22:46:57 -05002251 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002252 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302253 ext_debug(inode, " -> %u:%u\n", hole_start, hole_len);
Jan Kara140a5252016-03-09 22:46:57 -05002254 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2255 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002256}
2257
2258/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002259 * ext4_ext_rm_idx:
2260 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002261 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002262static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002263 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002264{
Alex Tomasa86c6182006-10-11 01:21:03 -07002265 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002266 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002267
2268 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002269 depth--;
2270 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002271 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002272 if (unlikely(path->p_hdr->eh_entries == 0)) {
2273 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002274 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002275 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002276 err = ext4_ext_get_access(handle, inode, path);
2277 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002278 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002279
2280 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2281 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2282 len *= sizeof(struct ext4_extent_idx);
2283 memmove(path->p_idx, path->p_idx + 1, len);
2284 }
2285
Marcin Slusarze8546d02008-04-17 10:38:59 -04002286 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002287 err = ext4_ext_dirty(handle, inode, path);
2288 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002289 return err;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302290 ext_debug(inode, "index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002291 trace_ext4_ext_rm_idx(inode, leaf);
2292
Peter Huewe7dc57612011-02-21 21:01:42 -05002293 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002294 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002295
2296 while (--depth >= 0) {
2297 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2298 break;
2299 path--;
2300 err = ext4_ext_get_access(handle, inode, path);
2301 if (err)
2302 break;
2303 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2304 err = ext4_ext_dirty(handle, inode, path);
2305 if (err)
2306 break;
2307 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002308 return err;
2309}
2310
2311/*
Mingming Caoee12b632008-08-19 22:16:05 -04002312 * ext4_ext_calc_credits_for_single_extent:
2313 * This routine returns max. credits that needed to insert an extent
2314 * to the extent tree.
2315 * When pass the actual path, the caller should calculate credits
2316 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002317 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002318int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002319 struct ext4_ext_path *path)
2320{
Alex Tomasa86c6182006-10-11 01:21:03 -07002321 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002322 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002323 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002324
Alex Tomasa86c6182006-10-11 01:21:03 -07002325 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002326 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002327 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2328
2329 /*
2330 * There are some space in the leaf tree, no
2331 * need to account for leaf block credit
2332 *
2333 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002334 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002335 * accounted.
2336 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002337 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002338 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002339 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002340 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002341 }
2342
Mingming Cao525f4ed2008-08-19 22:15:58 -04002343 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002344}
Alex Tomasa86c6182006-10-11 01:21:03 -07002345
Mingming Caoee12b632008-08-19 22:16:05 -04002346/*
Jan Karafffb2732013-06-04 13:01:11 -04002347 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002348 *
Jan Karafffb2732013-06-04 13:01:11 -04002349 * If we add a single extent, then in the worse case, each tree level
2350 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002351 *
Jan Karafffb2732013-06-04 13:01:11 -04002352 * If more extents are inserted, they could cause the whole tree split more
2353 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002354 */
Jan Karafffb2732013-06-04 13:01:11 -04002355int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002356{
2357 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002358 int depth;
2359
2360 /* If we are converting the inline data, only one is needed here. */
2361 if (ext4_has_inline_data(inode))
2362 return 1;
2363
2364 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002365
Jan Karafffb2732013-06-04 13:01:11 -04002366 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002367 index = depth * 2;
2368 else
2369 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002370
Mingming Caoee12b632008-08-19 22:16:05 -04002371 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002372}
2373
Theodore Ts'o981250c2013-06-12 11:48:29 -04002374static inline int get_default_free_blocks_flags(struct inode *inode)
2375{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002376 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2377 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002378 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2379 else if (ext4_should_journal_data(inode))
2380 return EXT4_FREE_BLOCKS_FORGET;
2381 return 0;
2382}
2383
Eric Whitney9fe67142018-10-01 14:25:08 -04002384/*
2385 * ext4_rereserve_cluster - increment the reserved cluster count when
2386 * freeing a cluster with a pending reservation
2387 *
2388 * @inode - file containing the cluster
2389 * @lblk - logical block in cluster to be reserved
2390 *
2391 * Increments the reserved cluster count and adjusts quota in a bigalloc
2392 * file system when freeing a partial cluster containing at least one
2393 * delayed and unwritten block. A partial cluster meeting that
2394 * requirement will have a pending reservation. If so, the
2395 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2396 * defer reserved and allocated space accounting to a subsequent call
2397 * to this function.
2398 */
2399static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2400{
2401 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2402 struct ext4_inode_info *ei = EXT4_I(inode);
2403
2404 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2405
2406 spin_lock(&ei->i_block_reservation_lock);
2407 ei->i_reserved_data_blocks++;
2408 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2409 spin_unlock(&ei->i_block_reservation_lock);
2410
2411 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2412 ext4_remove_pending(inode, lblk);
2413}
2414
Alex Tomasa86c6182006-10-11 01:21:03 -07002415static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002416 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002417 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002418 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002419{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002420 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002421 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002422 ext4_fsblk_t last_pblk, pblk;
2423 ext4_lblk_t num;
2424 int flags;
2425
2426 /* only extent tail removal is allowed */
2427 if (from < le32_to_cpu(ex->ee_block) ||
2428 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2429 ext4_error(sbi->s_sb,
2430 "strange request: removal(2) %u-%u from %u:%u",
2431 from, to, le32_to_cpu(ex->ee_block), ee_len);
2432 return 0;
2433 }
2434
2435#ifdef EXTENTS_STATS
2436 spin_lock(&sbi->s_ext_stats_lock);
2437 sbi->s_ext_blocks += ee_len;
2438 sbi->s_ext_extents++;
2439 if (ee_len < sbi->s_ext_min)
2440 sbi->s_ext_min = ee_len;
2441 if (ee_len > sbi->s_ext_max)
2442 sbi->s_ext_max = ee_len;
2443 if (ext_depth(inode) > sbi->s_depth_max)
2444 sbi->s_depth_max = ext_depth(inode);
2445 spin_unlock(&sbi->s_ext_stats_lock);
2446#endif
2447
2448 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2449
2450 /*
2451 * if we have a partial cluster, and it's different from the
2452 * cluster of the last block in the extent, we free it
2453 */
2454 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2455
2456 if (partial->state != initial &&
2457 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2458 if (partial->state == tofree) {
2459 flags = get_default_free_blocks_flags(inode);
2460 if (ext4_is_pending(inode, partial->lblk))
2461 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2462 ext4_free_blocks(handle, inode, NULL,
2463 EXT4_C2B(sbi, partial->pclu),
2464 sbi->s_cluster_ratio, flags);
2465 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2466 ext4_rereserve_cluster(inode, partial->lblk);
2467 }
2468 partial->state = initial;
2469 }
2470
2471 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2472 pblk = ext4_ext_pblock(ex) + ee_len - num;
2473
2474 /*
2475 * We free the partial cluster at the end of the extent (if any),
2476 * unless the cluster is used by another extent (partial_cluster
2477 * state is nofree). If a partial cluster exists here, it must be
2478 * shared with the last block in the extent.
2479 */
2480 flags = get_default_free_blocks_flags(inode);
2481
2482 /* partial, left end cluster aligned, right end unaligned */
2483 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2484 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2485 (partial->state != nofree)) {
2486 if (ext4_is_pending(inode, to))
2487 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2488 ext4_free_blocks(handle, inode, NULL,
2489 EXT4_PBLK_CMASK(sbi, last_pblk),
2490 sbi->s_cluster_ratio, flags);
2491 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2492 ext4_rereserve_cluster(inode, to);
2493 partial->state = initial;
2494 flags = get_default_free_blocks_flags(inode);
2495 }
2496
2497 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002498
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002499 /*
2500 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002501 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002502 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002503 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002504 */
2505 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002506 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002507
Eric Whitney9fe67142018-10-01 14:25:08 -04002508 /* reset the partial cluster if we've freed past it */
2509 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2510 partial->state = initial;
2511
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002512 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002513 * If we've freed the entire extent but the beginning is not left
2514 * cluster aligned and is not marked as ineligible for freeing we
2515 * record the partial cluster at the beginning of the extent. It
2516 * wasn't freed by the preceding ext4_free_blocks() call, and we
2517 * need to look farther to the left to determine if it's to be freed
2518 * (not shared with another extent). Else, reset the partial
2519 * cluster - we're either done freeing or the beginning of the
2520 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002521 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002522 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2523 if (partial->state == initial) {
2524 partial->pclu = EXT4_B2C(sbi, pblk);
2525 partial->lblk = from;
2526 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002527 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002528 } else {
2529 partial->state = initial;
2530 }
2531
Alex Tomasa86c6182006-10-11 01:21:03 -07002532 return 0;
2533}
2534
Allison Hendersond583fb82011-05-25 07:41:43 -04002535/*
2536 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf437602014-11-23 00:58:11 -05002537 * blocks appearing between "start" and "end". Both "start"
2538 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002539 *
2540 * @handle: The journal handle
2541 * @inode: The files inode
2542 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002543 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf437602014-11-23 00:58:11 -05002544 * has been released from it. However, if this value is
2545 * negative, it's a cluster just to the right of the
2546 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002547 * @start: The first block to remove
2548 * @end: The last block to remove
2549 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002550static int
2551ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002552 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002553 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002554 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002555{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002556 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002557 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002558 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002559 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002560 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002561 unsigned num;
2562 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002563 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002564 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002565 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002566 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002567
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002568 /* the header must be checked already in ext4_ext_remove_space() */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302569 ext_debug(inode, "truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002570 if (!path[depth].p_hdr)
2571 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2572 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002573 if (unlikely(path[depth].p_hdr == NULL)) {
2574 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002575 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002576 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002577 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002578 ex = path[depth].p_ext;
2579 if (!ex)
2580 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002581
2582 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002583 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002584
Eric Whitney9fe67142018-10-01 14:25:08 -04002585 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002586
Alex Tomasa86c6182006-10-11 01:21:03 -07002587 while (ex >= EXT_FIRST_EXTENT(eh) &&
2588 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002589
Lukas Czerner556615d2014-04-20 23:45:47 -04002590 if (ext4_ext_is_unwritten(ex))
2591 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002592 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002593 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002594
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302595 ext_debug(inode, "remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002596 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002597 path[depth].p_ext = ex;
2598
2599 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002600 b = ex_ee_block+ex_ee_len - 1 < end ?
2601 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002602
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302603 ext_debug(inode, " border %u:%u\n", a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002604
Allison Hendersond583fb82011-05-25 07:41:43 -04002605 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002606 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002607 /*
2608 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002609 * so note that its first cluster is in use to avoid
2610 * freeing it when removing blocks. Eventually, the
2611 * right edge of the truncated/punched region will
2612 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002613 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002614 if (sbi->s_cluster_ratio > 1) {
2615 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002616 partial->pclu = EXT4_B2C(sbi, pblk);
2617 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002618 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002619 ex--;
2620 ex_ee_block = le32_to_cpu(ex->ee_block);
2621 ex_ee_len = ext4_ext_get_actual_len(ex);
2622 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002623 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002624 EXT4_ERROR_INODE(inode,
2625 "can not handle truncate %u:%u "
2626 "on extent %u:%u",
2627 start, end, ex_ee_block,
2628 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002629 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002630 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002631 } else if (a != ex_ee_block) {
2632 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002633 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002634 } else {
2635 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002636 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002637 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002638 /*
2639 * 3 for leaf, sb, and inode plus 2 (bmap and group
2640 * descriptor) for each block group; assume two block
2641 * groups plus ex_ee_len/blocks_per_block_group for
2642 * the worst case
2643 */
2644 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002645 if (ex == EXT_FIRST_EXTENT(eh)) {
2646 correct_index = 1;
2647 credits += (ext_depth(inode)) + 1;
2648 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002649 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002650 /*
2651 * We may end up freeing some index blocks and data from the
2652 * punched range. Note that partial clusters are accounted for
2653 * by ext4_free_data_revoke_credits().
2654 */
2655 revoke_credits =
2656 ext4_free_metadata_revoke_credits(inode->i_sb,
2657 ext_depth(inode)) +
2658 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002659
Jan Karaa4130362019-11-05 17:44:16 +01002660 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002661 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002662 if (err) {
2663 if (err > 0)
2664 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002665 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002666 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002667
2668 err = ext4_ext_get_access(handle, inode, path + depth);
2669 if (err)
2670 goto out;
2671
Eric Whitney9fe67142018-10-01 14:25:08 -04002672 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002673 if (err)
2674 goto out;
2675
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002676 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002677 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002678 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002679
Alex Tomasa86c6182006-10-11 01:21:03 -07002680 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002681 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002682 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002683 * extent have been removed.
2684 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002685 if (unwritten && num)
2686 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002687 /*
2688 * If the extent was completely released,
2689 * we need to remove it from the leaf
2690 */
2691 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002692 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002693 /*
2694 * For hole punching, we need to scoot all the
2695 * extents up when an extent is removed so that
2696 * we dont have blank extents in the middle
2697 */
2698 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2699 sizeof(struct ext4_extent));
2700
2701 /* Now get rid of the one at the end */
2702 memset(EXT_LAST_EXTENT(eh), 0,
2703 sizeof(struct ext4_extent));
2704 }
2705 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf437602014-11-23 00:58:11 -05002706 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002707
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002708 err = ext4_ext_dirty(handle, inode, path + depth);
2709 if (err)
2710 goto out;
2711
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302712 ext_debug(inode, "new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002713 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002714 ex--;
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 }
2718
2719 if (correct_index && eh->eh_entries)
2720 err = ext4_ext_correct_indexes(handle, inode, path);
2721
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002722 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002723 * If there's a partial cluster and at least one extent remains in
2724 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf437602014-11-23 00:58:11 -05002725 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002726 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf437602014-11-23 00:58:11 -05002727 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002728 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002729 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf437602014-11-23 00:58:11 -05002730 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002731 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2732 int flags = get_default_free_blocks_flags(inode);
2733
2734 if (ext4_is_pending(inode, partial->lblk))
2735 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf437602014-11-23 00:58:11 -05002736 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002737 EXT4_C2B(sbi, partial->pclu),
2738 sbi->s_cluster_ratio, flags);
2739 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2740 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf437602014-11-23 00:58:11 -05002741 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002742 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002743 }
2744
Alex Tomasa86c6182006-10-11 01:21:03 -07002745 /* if this leaf is free, then we should
2746 * remove it from index block above */
2747 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002748 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002749
2750out:
2751 return err;
2752}
2753
2754/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002755 * ext4_ext_more_to_rm:
2756 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002757 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002758static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002759ext4_ext_more_to_rm(struct ext4_ext_path *path)
2760{
2761 BUG_ON(path->p_idx == NULL);
2762
2763 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2764 return 0;
2765
2766 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002767 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002768 * so we have to consider current index for truncation
2769 */
2770 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2771 return 0;
2772 return 1;
2773}
2774
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002775int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2776 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002777{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002778 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002779 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002780 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002781 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002782 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002783 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002784
Eric Whitney9fe67142018-10-01 14:25:08 -04002785 partial.pclu = 0;
2786 partial.lblk = 0;
2787 partial.state = initial;
2788
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302789 ext_debug(inode, "truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002790
2791 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002792 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2793 depth + 1,
2794 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002795 if (IS_ERR(handle))
2796 return PTR_ERR(handle);
2797
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002798again:
Lukas Czerner61801322013-05-27 23:32:35 -04002799 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002800
Alex Tomasa86c6182006-10-11 01:21:03 -07002801 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002802 * Check if we are removing extents inside the extent tree. If that
2803 * is the case, we are going to punch a hole inside the extent tree
2804 * so we have to check whether we need to split the extent covering
2805 * the last block to remove so we can easily remove the part of it
2806 * in ext4_ext_rm_leaf().
2807 */
2808 if (end < EXT_MAX_BLOCKS - 1) {
2809 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002810 ext4_lblk_t ee_block, ex_end, lblk;
2811 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002812
Eric Whitneyf4226d92014-11-23 00:55:42 -05002813 /* find extent for or closest extent to this block */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002814 path = ext4_find_extent(inode, end, NULL,
2815 EXT4_EX_NOCACHE | EXT4_EX_NOFAIL);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002816 if (IS_ERR(path)) {
2817 ext4_journal_stop(handle);
2818 return PTR_ERR(path);
2819 }
2820 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002821 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002822 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002823 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002824 if (depth) {
2825 EXT4_ERROR_INODE(inode,
2826 "path[%d].p_hdr == NULL",
2827 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002828 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002829 }
2830 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002831 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002832
2833 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002834 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002835
2836 /*
2837 * See if the last block is inside the extent, if so split
2838 * the extent at 'end' block so we can easily remove the
2839 * tail of the first part of the split extent in
2840 * ext4_ext_rm_leaf().
2841 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002842 if (end >= ee_block && end < ex_end) {
2843
2844 /*
2845 * If we're going to split the extent, note that
2846 * the cluster containing the block after 'end' is
2847 * in use to avoid freeing it when removing blocks.
2848 */
2849 if (sbi->s_cluster_ratio > 1) {
Jeffle Xucfb3c852020-05-22 12:18:44 +08002850 pblk = ext4_ext_pblock(ex) + end - ee_block + 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002851 partial.pclu = EXT4_B2C(sbi, pblk);
2852 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002853 }
2854
Lukas Czerner5f95d212012-03-19 23:03:19 -04002855 /*
2856 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002857 * block in the first new extent. Also we should not
2858 * fail removing space due to ENOSPC so try to use
2859 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002860 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002861 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002862 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002863 if (err < 0)
2864 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002865
Eric Whitney7bd75232019-02-28 23:34:11 -05002866 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
2867 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05002868 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05002869 * If we're punching, there's an extent to the right.
2870 * If the partial cluster hasn't been set, set it to
2871 * that extent's first cluster and its state to nofree
2872 * so it won't be freed should it contain blocks to be
2873 * removed. If it's already set (tofree/nofree), we're
2874 * retrying and keep the original partial cluster info
2875 * so a cluster marked tofree as a result of earlier
2876 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05002877 */
2878 lblk = ex_end + 1;
2879 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
yangerkund7dce9e2020-10-28 13:56:17 +08002880 NULL);
2881 if (err < 0)
Eric Whitneyf4226d92014-11-23 00:55:42 -05002882 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04002883 if (pblk) {
2884 partial.pclu = EXT4_B2C(sbi, pblk);
2885 partial.state = nofree;
2886 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002887 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002888 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002889 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002890 * We start scanning from right side, freeing all the blocks
2891 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002892 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002893 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002894 if (path) {
2895 int k = i = depth;
2896 while (--k > 0)
2897 path[k].p_block =
2898 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2899 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002900 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002901 GFP_NOFS | __GFP_NOFAIL);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002902 if (path == NULL) {
2903 ext4_journal_stop(handle);
2904 return -ENOMEM;
2905 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002906 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002907 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002908 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002909
Theodore Ts'oc3491792013-08-16 21:21:41 -04002910 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002911 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002912 goto out;
2913 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002914 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002915 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002916
2917 while (i >= 0 && err == 0) {
2918 if (i == depth) {
2919 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002920 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002921 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002922 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002923 brelse(path[i].p_bh);
2924 path[i].p_bh = NULL;
2925 i--;
2926 continue;
2927 }
2928
2929 /* this is index block */
2930 if (!path[i].p_hdr) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302931 ext_debug(inode, "initialize header\n");
Alex Tomasa86c6182006-10-11 01:21:03 -07002932 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002933 }
2934
Alex Tomasa86c6182006-10-11 01:21:03 -07002935 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002936 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002937 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2938 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302939 ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002940 path[i].p_hdr,
2941 le16_to_cpu(path[i].p_hdr->eh_entries));
2942 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002943 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002944 path[i].p_idx--;
2945 }
2946
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302947 ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002948 i, EXT_FIRST_INDEX(path[i].p_hdr),
2949 path[i].p_idx);
2950 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002951 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002952 /* go to the next level */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302953 ext_debug(inode, "move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002954 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002955 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002956 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002957 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2958 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002959 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002960 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002961 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002962 break;
2963 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04002964 /* Yield here to deal with large extent trees.
2965 * Should be a no-op if we did IO above. */
2966 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002967 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002968 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002969 break;
2970 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002971 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002972
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002973 /* save actual number of indexes since this
2974 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002975 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2976 i++;
2977 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002978 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002979 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002980 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002981 * handle must be already prepared by the
2982 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05002983 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002984 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002985 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002986 brelse(path[i].p_bh);
2987 path[i].p_bh = NULL;
2988 i--;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302989 ext_debug(inode, "return to level %d\n", i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002990 }
2991 }
2992
Eric Whitney9fe67142018-10-01 14:25:08 -04002993 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
2994 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04002995
Eric Whitney0756b902014-11-23 00:59:39 -05002996 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002997 * if there's a partial cluster and we have removed the first extent
2998 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05002999 */
Eric Whitney9fe67142018-10-01 14:25:08 -04003000 if (partial.state == tofree && err == 0) {
3001 int flags = get_default_free_blocks_flags(inode);
3002
3003 if (ext4_is_pending(inode, partial.lblk))
3004 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003005 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003006 EXT4_C2B(sbi, partial.pclu),
3007 sbi->s_cluster_ratio, flags);
3008 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3009 ext4_rereserve_cluster(inode, partial.lblk);
3010 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003011 }
3012
Alex Tomasa86c6182006-10-11 01:21:03 -07003013 /* TODO: flexible tree reduction should be here */
3014 if (path->p_hdr->eh_entries == 0) {
3015 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003016 * truncate to zero freed all the tree,
3017 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003018 */
3019 err = ext4_ext_get_access(handle, inode, path);
3020 if (err == 0) {
3021 ext_inode_hdr(inode)->eh_depth = 0;
3022 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003023 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003024 err = ext4_ext_dirty(handle, inode, path);
3025 }
3026 }
3027out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003028 ext4_ext_drop_refs(path);
3029 kfree(path);
3030 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003031 if (err == -EAGAIN)
3032 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003033 ext4_journal_stop(handle);
3034
3035 return err;
3036}
3037
3038/*
3039 * called at mount time
3040 */
3041void ext4_ext_init(struct super_block *sb)
3042{
3043 /*
3044 * possible initialization would be here
3045 */
3046
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003047 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003048#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b978162012-03-19 23:41:49 -04003049 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003050#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b978162012-03-19 23:41:49 -04003051 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003052#endif
3053#ifdef CHECK_BINSEARCH
Theodore Ts'o92b978162012-03-19 23:41:49 -04003054 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003055#endif
3056#ifdef EXTENTS_STATS
Theodore Ts'o92b978162012-03-19 23:41:49 -04003057 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003058#endif
Theodore Ts'o92b978162012-03-19 23:41:49 -04003059 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003060#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003061#ifdef EXTENTS_STATS
3062 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3063 EXT4_SB(sb)->s_ext_min = 1 << 30;
3064 EXT4_SB(sb)->s_ext_max = 0;
3065#endif
3066 }
3067}
3068
3069/*
3070 * called at umount time
3071 */
3072void ext4_ext_release(struct super_block *sb)
3073{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003074 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003075 return;
3076
3077#ifdef EXTENTS_STATS
3078 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3079 struct ext4_sb_info *sbi = EXT4_SB(sb);
3080 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3081 sbi->s_ext_blocks, sbi->s_ext_extents,
3082 sbi->s_ext_blocks / sbi->s_ext_extents);
3083 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3084 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3085 }
3086#endif
3087}
3088
Zheng Liud7b2a002013-08-28 14:47:06 -04003089static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3090{
3091 ext4_lblk_t ee_block;
3092 ext4_fsblk_t ee_pblock;
3093 unsigned int ee_len;
3094
3095 ee_block = le32_to_cpu(ex->ee_block);
3096 ee_len = ext4_ext_get_actual_len(ex);
3097 ee_pblock = ext4_ext_pblock(ex);
3098
3099 if (ee_len == 0)
3100 return 0;
3101
3102 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3103 EXTENT_STATUS_WRITTEN);
3104}
3105
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003106/* FIXME!! we need to try to merge to left or right after zero-out */
3107static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3108{
Lukas Czerner24075182010-10-27 21:30:06 -04003109 ext4_fsblk_t ee_pblock;
3110 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003111
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003112 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003113 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003114 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3115 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003116}
3117
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003118/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003119 * ext4_split_extent_at() splits an extent at given block.
3120 *
3121 * @handle: the journal handle
3122 * @inode: the file inode
3123 * @path: the path to the extent
3124 * @split: the logical block where the extent is splitted.
3125 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003126 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003127 * @flags: flags used to insert new extent to extent tree.
3128 *
3129 *
3130 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
Keyur Patele4d7f2d2020-06-10 23:19:46 -04003131 * of which are determined by split_flag.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003132 *
3133 * There are two cases:
3134 * a> the extent are splitted into two extent.
3135 * b> split is not needed, and just mark the extent.
3136 *
3137 * return 0 on success.
3138 */
3139static int ext4_split_extent_at(handle_t *handle,
3140 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003141 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003142 ext4_lblk_t split,
3143 int split_flag,
3144 int flags)
3145{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003146 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003147 ext4_fsblk_t newblock;
3148 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003149 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003150 struct ext4_extent *ex2 = NULL;
3151 unsigned int ee_len, depth;
3152 int err = 0;
3153
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003154 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3155 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3156
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303157 ext_debug(inode, "logical block %llu\n", (unsigned long long)split);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003158
3159 ext4_ext_show_leaf(inode, path);
3160
3161 depth = ext_depth(inode);
3162 ex = path[depth].p_ext;
3163 ee_block = le32_to_cpu(ex->ee_block);
3164 ee_len = ext4_ext_get_actual_len(ex);
3165 newblock = split - ee_block + ext4_ext_pblock(ex);
3166
3167 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003168 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003169 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003170 EXT4_EXT_MARK_UNWRIT1 |
3171 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003172
3173 err = ext4_ext_get_access(handle, inode, path + depth);
3174 if (err)
3175 goto out;
3176
3177 if (split == ee_block) {
3178 /*
3179 * case b: block @split is the block that the extent begins with
3180 * then we just change the state of the extent, and splitting
3181 * is not needed.
3182 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003183 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3184 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003185 else
3186 ext4_ext_mark_initialized(ex);
3187
3188 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003189 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003190
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003191 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003192 goto out;
3193 }
3194
3195 /* case a */
3196 memcpy(&orig_ex, ex, sizeof(orig_ex));
3197 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003198 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3199 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003200
3201 /*
3202 * path may lead to new leaf, not to original leaf any more
3203 * after ext4_ext_insert_extent() returns,
3204 */
3205 err = ext4_ext_dirty(handle, inode, path + depth);
3206 if (err)
3207 goto fix_extent_len;
3208
3209 ex2 = &newex;
3210 ex2->ee_block = cpu_to_le32(split);
3211 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3212 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003213 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3214 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003215
Theodore Ts'odfe50802014-09-01 14:37:09 -04003216 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Ye Bin082cd4e2021-05-06 22:10:42 +08003217 if (err != -ENOSPC && err != -EDQUOT)
3218 goto out;
3219
3220 if (EXT4_EXT_MAY_ZEROOUT & split_flag) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003221 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003222 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003223 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003224 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003225 zero_ex.ee_len = cpu_to_le16(
3226 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003227 ext4_ext_store_pblock(&zero_ex,
3228 ext4_ext_pblock(ex2));
3229 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003230 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003231 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003232 zero_ex.ee_len = cpu_to_le16(
3233 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003234 ext4_ext_store_pblock(&zero_ex,
3235 ext4_ext_pblock(ex));
3236 }
3237 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003238 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003239 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003240 zero_ex.ee_len = cpu_to_le16(
3241 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003242 ext4_ext_store_pblock(&zero_ex,
3243 ext4_ext_pblock(&orig_ex));
3244 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003245
Ye Bin082cd4e2021-05-06 22:10:42 +08003246 if (!err) {
3247 /* update the extent length and mark as initialized */
3248 ex->ee_len = cpu_to_le16(ee_len);
3249 ext4_ext_try_to_merge(handle, inode, path, ex);
3250 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3251 if (!err)
3252 /* update extent status tree */
3253 err = ext4_zeroout_es(inode, &zero_ex);
3254 /* If we failed at this point, we don't know in which
3255 * state the extent tree exactly is so don't try to fix
3256 * length of the original extent as it may do even more
3257 * damage.
3258 */
3259 goto out;
3260 }
3261 }
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003262
3263fix_extent_len:
3264 ex->ee_len = orig_ex.ee_len;
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003265 /*
3266 * Ignore ext4_ext_dirty return value since we are already in error path
3267 * and err is a non-zero error code.
3268 */
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003269 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003270 return err;
Ye Bin082cd4e2021-05-06 22:10:42 +08003271out:
3272 ext4_ext_show_leaf(inode, path);
3273 return err;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003274}
3275
3276/*
3277 * ext4_split_extents() splits an extent and mark extent which is covered
3278 * by @map as split_flags indicates
3279 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003280 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003281 * There are three possibilities:
3282 * a> There is no split required
3283 * b> Splits in two extents: Split is happening at either end of the extent
3284 * c> Splits in three extents: Somone is splitting in middle of the extent
3285 *
3286 */
3287static int ext4_split_extent(handle_t *handle,
3288 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003289 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003290 struct ext4_map_blocks *map,
3291 int split_flag,
3292 int flags)
3293{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003294 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003295 ext4_lblk_t ee_block;
3296 struct ext4_extent *ex;
3297 unsigned int ee_len, depth;
3298 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003299 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003300 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003301 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003302
3303 depth = ext_depth(inode);
3304 ex = path[depth].p_ext;
3305 ee_block = le32_to_cpu(ex->ee_block);
3306 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003307 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003308
3309 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003310 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003311 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003312 if (unwritten)
3313 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3314 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003315 if (split_flag & EXT4_EXT_DATA_VALID2)
3316 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003317 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003318 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003319 if (err)
3320 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003321 } else {
3322 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003323 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003324 /*
3325 * Update path is required because previous ext4_split_extent_at() may
3326 * result in split of original leaf or extent zeroout.
3327 */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07003328 path = ext4_find_extent(inode, map->m_lblk, ppath, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003329 if (IS_ERR(path))
3330 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003331 depth = ext_depth(inode);
3332 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003333 if (!ex) {
3334 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3335 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003336 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003337 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003338 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003339 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003340
3341 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003342 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003343 if (unwritten) {
3344 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003345 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003346 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003347 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003348 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003349 map->m_lblk, split_flag1, flags);
3350 if (err)
3351 goto out;
3352 }
3353
3354 ext4_ext_show_leaf(inode, path);
3355out:
Zheng Liu3a225672013-03-10 21:20:23 -04003356 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003357}
3358
Amit Arora56055d32007-07-17 21:42:38 -04003359/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003360 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003361 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003362 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003363 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003364 * There are three possibilities:
3365 * a> There is no split required: Entire extent should be initialized
3366 * b> Splits in two extents: Write is happening at either end of the extent
3367 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003368 *
3369 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003370 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003371 * - The extent pointed to by 'path' contains a superset
3372 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3373 *
3374 * Post-conditions on success:
3375 * - the returned value is the number of blocks beyond map->l_lblk
3376 * that are allocated and initialized.
3377 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003378 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003379static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003380 struct inode *inode,
3381 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003382 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003383 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003384{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003385 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003386 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003387 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003388 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003389 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003390 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003391 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003392 unsigned int ee_len, depth, map_len = map->m_len;
3393 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003394 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003395 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003396
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303397 ext_debug(inode, "logical block %llu, max_blocks %u\n",
3398 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003399
Zheng Liu67a5da52012-08-17 09:54:17 -04003400 sbi = EXT4_SB(inode->i_sb);
Jan Kara801674f2020-03-31 12:50:16 +02003401 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3402 >> inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003403 if (eof_block < map->m_lblk + map_len)
3404 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003405
3406 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003407 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003408 ex = path[depth].p_ext;
3409 ee_block = le32_to_cpu(ex->ee_block);
3410 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003411 zero_ex1.ee_len = 0;
3412 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003413
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003414 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3415
3416 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003417 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003418 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003419
3420 /*
3421 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003422 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003423 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003424 * memmove() calls. Transferring to the left is the common case in
3425 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3426 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003427 *
3428 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003429 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003430 * This would require removing the extent if the transfer
3431 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003432 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003433 * same extent tree node.
3434 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003435 if ((map->m_lblk == ee_block) &&
3436 /* See if we can merge left */
3437 (map_len < ee_len) && /*L1*/
3438 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003439 ext4_lblk_t prev_lblk;
3440 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003441 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003442
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003443 abut_ex = ex - 1;
3444 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3445 prev_len = ext4_ext_get_actual_len(abut_ex);
3446 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003447 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003448
3449 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003450 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003451 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003452 * - C1: abut_ex is initialized,
3453 * - C2: abut_ex is logically abutting ex,
3454 * - C3: abut_ex is physically abutting ex,
3455 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003456 * overflowing the (initialized) length limit.
3457 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003458 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003459 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3460 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003461 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003462 err = ext4_ext_get_access(handle, inode, path + depth);
3463 if (err)
3464 goto out;
3465
3466 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003467 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003468
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003469 /* Shift the start of ex by 'map_len' blocks */
3470 ex->ee_block = cpu_to_le32(ee_block + map_len);
3471 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3472 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003473 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003474
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003475 /* Extend abut_ex by 'map_len' blocks */
3476 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003477
3478 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003479 allocated = map_len;
3480 }
3481 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3482 (map_len < ee_len) && /*L1*/
3483 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3484 /* See if we can merge right */
3485 ext4_lblk_t next_lblk;
3486 ext4_fsblk_t next_pblk, ee_pblk;
3487 unsigned int next_len;
3488
3489 abut_ex = ex + 1;
3490 next_lblk = le32_to_cpu(abut_ex->ee_block);
3491 next_len = ext4_ext_get_actual_len(abut_ex);
3492 next_pblk = ext4_ext_pblock(abut_ex);
3493 ee_pblk = ext4_ext_pblock(ex);
3494
3495 /*
3496 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3497 * upon those conditions:
3498 * - C1: abut_ex is initialized,
3499 * - C2: abut_ex is logically abutting ex,
3500 * - C3: abut_ex is physically abutting ex,
3501 * - C4: abut_ex can receive the additional blocks without
3502 * overflowing the (initialized) length limit.
3503 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003504 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003505 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3506 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3507 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3508 err = ext4_ext_get_access(handle, inode, path + depth);
3509 if (err)
3510 goto out;
3511
3512 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3513 map, ex, abut_ex);
3514
3515 /* Shift the start of abut_ex by 'map_len' blocks */
3516 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3517 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3518 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003519 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003520
3521 /* Extend abut_ex by 'map_len' blocks */
3522 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3523
3524 /* Result: number of initialized blocks past m_lblk */
3525 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003526 }
3527 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003528 if (allocated) {
3529 /* Mark the block containing both extents as dirty */
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003530 err = ext4_ext_dirty(handle, inode, path + depth);
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003531
3532 /* Update path to point to the right extent */
3533 path[depth].p_ext = abut_ex;
3534 goto out;
3535 } else
3536 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003537
Yongqiang Yang667eff32011-05-03 12:25:07 -04003538 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003539 /*
3540 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003541 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003542 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003543 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003544
Zheng Liu67a5da52012-08-17 09:54:17 -04003545 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3546 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003547 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003548
Amit Arora56055d32007-07-17 21:42:38 -04003549 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003550 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003551 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003552 * 2. split the extent into two extents, zeroout the head of the first
3553 * extent.
3554 * 3. split the extent into two extents, zeroout the tail of the second
3555 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003556 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003557 * 5. no splitting needed, just possibly zeroout the head and / or the
3558 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003559 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003560 split_map.m_lblk = map->m_lblk;
3561 split_map.m_len = map->m_len;
3562
Jan Kara4f8caa62017-05-26 17:40:52 -04003563 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003564 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003565 /* case 3 or 5 */
3566 zero_ex1.ee_block =
3567 cpu_to_le32(split_map.m_lblk +
3568 split_map.m_len);
3569 zero_ex1.ee_len =
3570 cpu_to_le16(allocated - split_map.m_len);
3571 ext4_ext_store_pblock(&zero_ex1,
3572 ext4_ext_pblock(ex) + split_map.m_lblk +
3573 split_map.m_len - ee_block);
3574 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003575 if (err)
Theodore Ts'o308c57c2021-08-13 11:20:48 -04003576 goto fallback;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003577 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003578 }
3579 if (split_map.m_lblk - ee_block + split_map.m_len <
3580 max_zeroout) {
3581 /* case 2 or 5 */
3582 if (split_map.m_lblk != ee_block) {
3583 zero_ex2.ee_block = ex->ee_block;
3584 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003585 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003586 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003587 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003588 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003589 if (err)
Theodore Ts'o308c57c2021-08-13 11:20:48 -04003590 goto fallback;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003591 }
3592
Jan Kara4f8caa62017-05-26 17:40:52 -04003593 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003594 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003595 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003596 }
3597 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003598
Theodore Ts'o308c57c2021-08-13 11:20:48 -04003599fallback:
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003600 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3601 flags);
3602 if (err > 0)
3603 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003604out:
Zheng Liuadb23552013-03-10 21:13:05 -04003605 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003606 if (!err) {
3607 err = ext4_zeroout_es(inode, &zero_ex1);
3608 if (!err)
3609 err = ext4_zeroout_es(inode, &zero_ex2);
3610 }
Amit Arora56055d32007-07-17 21:42:38 -04003611 return err ? err : allocated;
3612}
3613
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003614/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003615 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003616 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003617 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003618 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003619 * Writing to an unwritten extent may result in splitting the unwritten
3620 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003621 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003622 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003623 * b> Splits in two extents: Write is happening at either end of the extent
3624 * c> Splits in three extents: Somone is writing in middle of the extent
3625 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003626 * This works the same way in the case of initialized -> unwritten conversion.
3627 *
Mingming Cao00314622009-09-28 15:49:08 -04003628 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003629 * the unwritten extent split. To prevent ENOSPC occur at the IO
3630 * complete, we need to split the unwritten extent before DIO submit
3631 * the IO. The unwritten extent called at this time will be split
3632 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003633 * being filled will be convert to initialized by the end_io callback function
3634 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003635 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003636 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003637 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003638static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003639 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003640 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003641 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003642 int flags)
3643{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003644 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003645 ext4_lblk_t eof_block;
3646 ext4_lblk_t ee_block;
3647 struct ext4_extent *ex;
3648 unsigned int ee_len;
3649 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003650
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303651 ext_debug(inode, "logical block %llu, max_blocks %u\n",
Lukas Czernerb8a86842014-03-18 18:05:35 -04003652 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003653
Jan Kara801674f2020-03-31 12:50:16 +02003654 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3655 >> inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003656 if (eof_block < map->m_lblk + map->m_len)
3657 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003658 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003659 * It is safe to convert extent to initialized via explicit
Keyur Patele4d7f2d2020-06-10 23:19:46 -04003660 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003661 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003662 depth = ext_depth(inode);
3663 ex = path[depth].p_ext;
3664 ee_block = le32_to_cpu(ex->ee_block);
3665 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003666
Lukas Czernerb8a86842014-03-18 18:05:35 -04003667 /* Convert to unwritten */
3668 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3669 split_flag |= EXT4_EXT_DATA_VALID1;
3670 /* Convert to initialized */
3671 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3672 split_flag |= ee_block + ee_len <= eof_block ?
3673 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003674 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003675 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003676 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003677 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003678}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003679
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003680static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003681 struct inode *inode,
3682 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003683 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003684{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003685 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003686 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003687 ext4_lblk_t ee_block;
3688 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003689 int depth;
3690 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003691
3692 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003693 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003694 ee_block = le32_to_cpu(ex->ee_block);
3695 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003696
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303697 ext_debug(inode, "logical block %llu, max_blocks %u\n",
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003698 (unsigned long long)ee_block, ee_len);
3699
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003700 /* If extent is larger than requested it is a clear sign that we still
3701 * have some extent state machine issues left. So extent_split is still
3702 * required.
3703 * TODO: Once all related issues will be fixed this situation should be
3704 * illegal.
3705 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003706 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003707#ifdef CONFIG_EXT4_DEBUG
3708 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003709 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003710 inode->i_ino, (unsigned long long)ee_block, ee_len,
3711 (unsigned long long)map->m_lblk, map->m_len);
3712#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003713 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003714 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003715 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003716 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003717 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003718 if (IS_ERR(path))
3719 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003720 depth = ext_depth(inode);
3721 ex = path[depth].p_ext;
3722 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003723
Mingming Cao00314622009-09-28 15:49:08 -04003724 err = ext4_ext_get_access(handle, inode, path + depth);
3725 if (err)
3726 goto out;
3727 /* first mark the extent as initialized */
3728 ext4_ext_mark_initialized(ex);
3729
Yongqiang Yang197217a2011-05-03 11:45:29 -04003730 /* note: ext4_ext_correct_indexes() isn't needed here because
3731 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003732 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003733 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003734
Mingming Cao00314622009-09-28 15:49:08 -04003735 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003736 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003737out:
3738 ext4_ext_show_leaf(inode, path);
3739 return err;
3740}
3741
3742static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003743convert_initialized_extent(handle_t *handle, struct inode *inode,
3744 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003745 struct ext4_ext_path **ppath,
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003746 unsigned int *allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003747{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003748 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003749 struct ext4_extent *ex;
3750 ext4_lblk_t ee_block;
3751 unsigned int ee_len;
3752 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003753 int err = 0;
3754
3755 /*
3756 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003757 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003758 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003759 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3760 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003761
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003762 depth = ext_depth(inode);
3763 ex = path[depth].p_ext;
3764 ee_block = le32_to_cpu(ex->ee_block);
3765 ee_len = ext4_ext_get_actual_len(ex);
3766
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303767 ext_debug(inode, "logical block %llu, max_blocks %u\n",
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003768 (unsigned long long)ee_block, ee_len);
3769
3770 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003771 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003772 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3773 if (err < 0)
3774 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003775 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003776 if (IS_ERR(path))
3777 return PTR_ERR(path);
3778 depth = ext_depth(inode);
3779 ex = path[depth].p_ext;
3780 if (!ex) {
3781 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3782 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003783 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003784 }
3785 }
3786
3787 err = ext4_ext_get_access(handle, inode, path + depth);
3788 if (err)
3789 return err;
3790 /* first mark the extent as unwritten */
3791 ext4_ext_mark_unwritten(ex);
3792
3793 /* note: ext4_ext_correct_indexes() isn't needed here because
3794 * borders are not changed
3795 */
3796 ext4_ext_try_to_merge(handle, inode, path, ex);
3797
3798 /* Mark modified extent as dirty */
3799 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3800 if (err)
3801 return err;
3802 ext4_ext_show_leaf(inode, path);
3803
3804 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003805
Lukas Czernerb8a86842014-03-18 18:05:35 -04003806 map->m_flags |= EXT4_MAP_UNWRITTEN;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003807 if (*allocated > map->m_len)
3808 *allocated = map->m_len;
3809 map->m_len = *allocated;
3810 return 0;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003811}
3812
3813static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003814ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003815 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003816 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003817 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003818{
Ritesh Harjani8ec2d312020-05-10 11:54:53 +05303819 struct ext4_ext_path __maybe_unused *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003820 int ret = 0;
3821 int err = 0;
3822
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303823 ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n",
3824 (unsigned long long)map->m_lblk, map->m_len, flags,
3825 allocated);
Mingming Cao00314622009-09-28 15:49:08 -04003826 ext4_ext_show_leaf(inode, path);
3827
Lukas Czerner27dd4382013-04-09 22:11:22 -04003828 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04003829 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04003830 * allocate metadata blocks for the new extent block if needed.
3831 */
3832 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
3833
Lukas Czerner556615d2014-04-20 23:45:47 -04003834 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05003835 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003836
Eric Whitney779e2652020-04-30 14:53:19 -04003837 /* get_block() before submitting IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003838 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003839 ret = ext4_split_convert_extents(handle, inode, map, ppath,
3840 flags | EXT4_GET_BLOCKS_CONVERT);
Eric Whitney779e2652020-04-30 14:53:19 -04003841 if (ret < 0) {
3842 err = ret;
3843 goto out2;
3844 }
3845 /*
3846 * shouldn't get a 0 return when splitting an extent unless
3847 * m_len is 0 (bug) or extent has been corrupted
3848 */
3849 if (unlikely(ret == 0)) {
3850 EXT4_ERROR_INODE(inode,
3851 "unexpected ret == 0, m_len = %u",
3852 map->m_len);
3853 err = -EFSCORRUPTED;
3854 goto out2;
3855 }
Zheng Liua25a4e12013-02-18 00:28:04 -05003856 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003857 goto out;
3858 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003859 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003860 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Eric Whitneybee6cf02020-04-30 14:53:18 -04003861 err = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003862 ppath);
Eric Whitneybee6cf02020-04-30 14:53:18 -04003863 if (err < 0)
3864 goto out2;
3865 ext4_update_inode_fsync_trans(handle, inode, 1);
3866 goto map_out;
Mingming Cao00314622009-09-28 15:49:08 -04003867 }
Eric Whitneybee6cf02020-04-30 14:53:18 -04003868 /* buffered IO cases */
Mingming Cao00314622009-09-28 15:49:08 -04003869 /*
3870 * repeat fallocate creation request
3871 * we already have an unwritten extent
3872 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003873 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05003874 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003875 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003876 }
Mingming Cao00314622009-09-28 15:49:08 -04003877
3878 /* buffered READ or buffered write_begin() lookup */
3879 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3880 /*
3881 * We have blocks reserved already. We
3882 * return allocated blocks so that delalloc
3883 * won't do block reservation for us. But
3884 * the buffer head will be unmapped so that
3885 * a read from the block returns 0s.
3886 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003887 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003888 goto out1;
3889 }
3890
Eric Whitneybe809e12020-04-30 14:53:20 -04003891 /*
3892 * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1.
3893 * For buffered writes, at writepage time, etc. Convert a
3894 * discovered unwritten extent to written.
3895 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04003896 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Eric Whitneybe809e12020-04-30 14:53:20 -04003897 if (ret < 0) {
Mingming Cao00314622009-09-28 15:49:08 -04003898 err = ret;
3899 goto out2;
Eric Whitney779e2652020-04-30 14:53:19 -04003900 }
Eric Whitneybe809e12020-04-30 14:53:20 -04003901 ext4_update_inode_fsync_trans(handle, inode, 1);
3902 /*
3903 * shouldn't get a 0 return when converting an unwritten extent
3904 * unless m_len is 0 (bug) or extent has been corrupted
3905 */
3906 if (unlikely(ret == 0)) {
3907 EXT4_ERROR_INODE(inode, "unexpected ret == 0, m_len = %u",
3908 map->m_len);
3909 err = -EFSCORRUPTED;
3910 goto out2;
3911 }
3912
Eric Whitney779e2652020-04-30 14:53:19 -04003913out:
3914 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003915 map->m_flags |= EXT4_MAP_NEW;
Mingming Cao00314622009-09-28 15:49:08 -04003916map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003917 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003918out1:
Eric Whitneybee6cf02020-04-30 14:53:18 -04003919 map->m_pblk = newblock;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003920 if (allocated > map->m_len)
3921 allocated = map->m_len;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003922 map->m_len = allocated;
Eric Whitneybee6cf02020-04-30 14:53:18 -04003923 ext4_ext_show_leaf(inode, path);
Mingming Cao00314622009-09-28 15:49:08 -04003924out2:
Mingming Cao00314622009-09-28 15:49:08 -04003925 return err ? err : allocated;
3926}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003927
Mingming Cao00314622009-09-28 15:49:08 -04003928/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003929 * get_implied_cluster_alloc - check to see if the requested
3930 * allocation (in the map structure) overlaps with a cluster already
3931 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003932 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003933 * @map The requested lblk->pblk mapping
3934 * @ex The extent structure which might contain an implied
3935 * cluster allocation
3936 *
3937 * This function is called by ext4_ext_map_blocks() after we failed to
3938 * find blocks that were already in the inode's extent tree. Hence,
3939 * we know that the beginning of the requested region cannot overlap
3940 * the extent from the inode's extent tree. There are three cases we
3941 * want to catch. The first is this case:
3942 *
3943 * |--- cluster # N--|
3944 * |--- extent ---| |---- requested region ---|
3945 * |==========|
3946 *
3947 * The second case that we need to test for is this one:
3948 *
3949 * |--------- cluster # N ----------------|
3950 * |--- requested region --| |------- extent ----|
3951 * |=======================|
3952 *
3953 * The third case is when the requested region lies between two extents
3954 * within the same cluster:
3955 * |------------- cluster # N-------------|
3956 * |----- ex -----| |---- ex_right ----|
3957 * |------ requested region ------|
3958 * |================|
3959 *
3960 * In each of the above cases, we need to set the map->m_pblk and
3961 * map->m_len so it corresponds to the return the extent labelled as
3962 * "|====|" from cluster #N, since it is already in use for data in
3963 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3964 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3965 * as a new "allocated" block region. Otherwise, we will return 0 and
3966 * ext4_ext_map_blocks() will then allocate one or more new clusters
3967 * by calling ext4_mb_new_blocks().
3968 */
Aditya Kalid8990242011-09-09 19:18:51 -04003969static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003970 struct ext4_map_blocks *map,
3971 struct ext4_extent *ex,
3972 struct ext4_ext_path *path)
3973{
Aditya Kalid8990242011-09-09 19:18:51 -04003974 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003975 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003976 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003977 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003978 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3979 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3980 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3981
3982 /* The extent passed in that we are trying to match */
3983 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3984 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3985
3986 /* The requested region passed into ext4_map_blocks() */
3987 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003988
3989 if ((rr_cluster_start == ex_cluster_end) ||
3990 (rr_cluster_start == ex_cluster_start)) {
3991 if (rr_cluster_start == ex_cluster_end)
3992 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003993 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003994 map->m_len = min(map->m_len,
3995 (unsigned) sbi->s_cluster_ratio - c_offset);
3996 /*
3997 * Check for and handle this case:
3998 *
3999 * |--------- cluster # N-------------|
4000 * |------- extent ----|
4001 * |--- requested region ---|
4002 * |===========|
4003 */
4004
4005 if (map->m_lblk < ee_block)
4006 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4007
4008 /*
4009 * Check for the case where there is already another allocated
4010 * block to the right of 'ex' but before the end of the cluster.
4011 *
4012 * |------------- cluster # N-------------|
4013 * |----- ex -----| |---- ex_right ----|
4014 * |------ requested region ------|
4015 * |================|
4016 */
4017 if (map->m_lblk > ee_block) {
4018 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4019 map->m_len = min(map->m_len, next - map->m_lblk);
4020 }
Aditya Kalid8990242011-09-09 19:18:51 -04004021
4022 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004023 return 1;
4024 }
Aditya Kalid8990242011-09-09 19:18:51 -04004025
4026 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004027 return 0;
4028}
4029
4030
4031/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004032 * Block allocation/map/preallocation routine for extents based files
4033 *
4034 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004035 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004036 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4037 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004038 *
Randy Dunlapb483bb72020-08-04 19:48:50 -07004039 * return > 0, number of blocks already mapped/allocated
Mingming Caof5ab0d12008-02-25 15:29:55 -05004040 * if create == 0 and these are pre-allocated blocks
4041 * buffer head is unmapped
4042 * otherwise blocks are mapped
4043 *
4044 * return = 0, if plain look up failed (blocks have not been allocated)
4045 * buffer head is unmapped
4046 *
4047 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004048 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004049int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4050 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004051{
4052 struct ext4_ext_path *path = NULL;
yangerkund7dce9e2020-10-28 13:56:17 +08004053 struct ext4_extent newex, *ex, ex2;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004054 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney8ad8d712020-05-10 11:58:05 -04004055 ext4_fsblk_t newblock = 0, pblk;
Eric Whitney34990462020-03-11 16:50:33 -04004056 int err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004057 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004058 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004059 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004060 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07004061
Ritesh Harjani70aa1552020-05-10 11:54:55 +05304062 ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004063 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004064
Alex Tomasa86c6182006-10-11 01:21:03 -07004065 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004066 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004067 if (IS_ERR(path)) {
4068 err = PTR_ERR(path);
4069 path = NULL;
Eric Whitney8ad8d712020-05-10 11:58:05 -04004070 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07004071 }
4072
4073 depth = ext_depth(inode);
4074
4075 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004076 * consistent leaf must not be empty;
4077 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004078 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004079 */
Frank Mayhar273df552010-03-02 11:46:09 -05004080 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4081 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004082 "lblock: %lu, depth: %d pblock %lld",
4083 (unsigned long) map->m_lblk, depth,
4084 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004085 err = -EFSCORRUPTED;
Eric Whitney8ad8d712020-05-10 11:58:05 -04004086 goto out;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004087 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004088
Avantika Mathur7e028972006-12-06 20:41:33 -08004089 ex = path[depth].p_ext;
4090 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004091 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004092 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004093 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004094
Lukas Czernerb8a86842014-03-18 18:05:35 -04004095
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004096 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004097 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004098 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004099 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004100 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004101
4102 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4103
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004104 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004105 if (in_range(map->m_lblk, ee_block, ee_len)) {
4106 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004107 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004108 allocated = ee_len - (map->m_lblk - ee_block);
Ritesh Harjani70aa1552020-05-10 11:54:55 +05304109 ext_debug(inode, "%u fit into %u:%d -> %llu\n",
4110 map->m_lblk, ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004111
Lukas Czernerb8a86842014-03-18 18:05:35 -04004112 /*
4113 * If the extent is initialized check whether the
4114 * caller wants to convert it to unwritten.
4115 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004116 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004117 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004118 err = convert_initialized_extent(handle,
4119 inode, map, &path, &allocated);
Eric Whitney8ad8d712020-05-10 11:58:05 -04004120 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004121 } else if (!ext4_ext_is_unwritten(ex)) {
Eric Whitney8ad8d712020-05-10 11:58:05 -04004122 map->m_flags |= EXT4_MAP_MAPPED;
4123 map->m_pblk = newblock;
4124 if (allocated > map->m_len)
4125 allocated = map->m_len;
4126 map->m_len = allocated;
4127 ext4_ext_show_leaf(inode, path);
Lukas Czerner78771912012-03-19 23:05:43 -04004128 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004129 }
Zheng Liu69eb33d2013-02-18 00:31:07 -05004130
Lukas Czerner556615d2014-04-20 23:45:47 -04004131 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004132 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004133 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004134 if (ret < 0)
4135 err = ret;
4136 else
4137 allocated = ret;
Eric Whitney8ad8d712020-05-10 11:58:05 -04004138 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07004139 }
4140 }
4141
4142 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004143 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004144 * we couldn't try to create block if create flag is zero
4145 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004146 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004147 ext4_lblk_t hole_start, hole_len;
4148
Jan Karafacab4d2016-03-09 22:54:00 -05004149 hole_start = map->m_lblk;
4150 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004151 /*
4152 * put just found gap into cache to speed up
4153 * subsequent requests
4154 */
Jan Kara140a5252016-03-09 22:46:57 -05004155 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004156
4157 /* Update hole_len to reflect hole size after map->m_lblk */
4158 if (hole_start != map->m_lblk)
4159 hole_len -= map->m_lblk - hole_start;
4160 map->m_pblk = 0;
4161 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4162
Eric Whitney8ad8d712020-05-10 11:58:05 -04004163 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07004164 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004165
Alex Tomasa86c6182006-10-11 01:21:03 -07004166 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004167 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004168 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004169 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004170 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004171
4172 /*
4173 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004174 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004175 */
4176 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004177 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004178 ar.len = allocated = map->m_len;
4179 newblock = map->m_pblk;
4180 goto got_allocated_blocks;
4181 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004182
Alex Tomasc9de5602008-01-29 00:19:52 -05004183 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004184 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004185 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4186 if (err)
Eric Whitney8ad8d712020-05-10 11:58:05 -04004187 goto out;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004188 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004189 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
yangerkund7dce9e2020-10-28 13:56:17 +08004190 if (err < 0)
Eric Whitney8ad8d712020-05-10 11:58:05 -04004191 goto out;
Amit Arora25d14f92007-05-24 13:04:13 -04004192
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004193 /* Check if the extent after searching to the right implies a
4194 * cluster we can use. */
yangerkund7dce9e2020-10-28 13:56:17 +08004195 if ((sbi->s_cluster_ratio > 1) && err &&
4196 get_implied_cluster_alloc(inode->i_sb, map, &ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004197 ar.len = allocated = map->m_len;
4198 newblock = map->m_pblk;
4199 goto got_allocated_blocks;
4200 }
4201
Amit Arora749269f2007-07-18 09:02:56 -04004202 /*
4203 * See if request is beyond maximum number of blocks we can have in
4204 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004205 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4206 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004207 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004208 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004209 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004210 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004211 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4212 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4213 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004214
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004215 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004216 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004217 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004218 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004219 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004220 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004221 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004222
4223 /* allocate new block */
4224 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004225 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4226 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004227 /*
4228 * We calculate the offset from the beginning of the cluster
4229 * for the logical block number, since when we allocate a
4230 * physical cluster, the physical block should start at the
4231 * same offset from the beginning of the cluster. This is
4232 * needed so that future calls to get_implied_cluster_alloc()
4233 * work correctly.
4234 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004235 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004236 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4237 ar.goal -= offset;
4238 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004239 if (S_ISREG(inode->i_mode))
4240 ar.flags = EXT4_MB_HINT_DATA;
4241 else
4242 /* disable in-core preallocation for non-regular files */
4243 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004244 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4245 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004246 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4247 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004248 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4249 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004250 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004251 if (!newblock)
Eric Whitney8ad8d712020-05-10 11:58:05 -04004252 goto out;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004253 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004254 ar.len = EXT4_C2B(sbi, ar.len) - offset;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05304255 ext_debug(inode, "allocate new block: goal %llu, found %llu/%u, requested %u\n",
Ritesh Harjaniec8c60b2020-05-10 11:54:52 +05304256 ar.goal, newblock, ar.len, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004257 if (ar.len > allocated)
4258 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004259
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004260got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004261 /* try to insert new extent into found leaf and return */
Eric Whitney8ad8d712020-05-10 11:58:05 -04004262 pblk = newblock + offset;
4263 ext4_ext_store_pblock(&newex, pblk);
Alex Tomasc9de5602008-01-29 00:19:52 -05004264 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004265 /* Mark unwritten */
Eric Whitney34990462020-03-11 16:50:33 -04004266 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Lukas Czerner556615d2014-04-20 23:45:47 -04004267 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004268 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004269 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004270
Eric Whitney4337ecd2020-02-11 16:02:16 -05004271 err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
Eric Whitney34990462020-03-11 16:50:33 -04004272 if (err) {
4273 if (allocated_clusters) {
4274 int fb_flags = 0;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004275
Eric Whitney34990462020-03-11 16:50:33 -04004276 /*
4277 * free data blocks we just allocated.
4278 * not a good idea to call discard here directly,
4279 * but otherwise we'd need to call it every free().
4280 */
brookxu27bc4462020-08-17 15:36:15 +08004281 ext4_discard_preallocations(inode, 0);
Eric Whitney34990462020-03-11 16:50:33 -04004282 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4283 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
4284 ext4_free_blocks(handle, inode, NULL, newblock,
4285 EXT4_C2B(sbi, allocated_clusters),
4286 fb_flags);
4287 }
Eric Whitney8ad8d712020-05-10 11:58:05 -04004288 goto out;
Alex Tomas315054f2007-05-24 13:04:25 -04004289 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004290
Jan Karab436b9b2009-12-08 23:51:10 -05004291 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004292 * Reduce the reserved cluster count to reflect successful deferred
4293 * allocation of delayed allocated clusters or direct allocation of
4294 * clusters discovered to be delayed allocated. Once allocated, a
4295 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004296 */
Eric Whitney29711482020-03-11 16:51:25 -04004297 if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) {
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004298 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004299 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004300 * When allocating delayed allocated clusters, simply
4301 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004302 */
4303 ext4_da_update_reserve_space(inode, allocated_clusters,
4304 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004305 } else {
4306 ext4_lblk_t lblk, len;
4307 unsigned int n;
4308
4309 /*
4310 * When allocating non-delayed allocated clusters
4311 * (from fallocate, filemap, DIO, or clusters
4312 * allocated when delalloc has been disabled by
4313 * ext4_nonda_switch), reduce the reserved cluster
4314 * count by the number of allocated clusters that
4315 * have previously been delayed allocated. Quota
4316 * has been claimed by ext4_mb_new_blocks() above,
4317 * so release the quota reservations made for any
4318 * previously delayed allocated clusters.
4319 */
4320 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4321 len = allocated_clusters << sbi->s_cluster_bits;
4322 n = ext4_es_delayed_clu(inode, lblk, len);
4323 if (n > 0)
4324 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004325 }
4326 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004327
4328 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004329 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004330 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004331 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004332 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004333 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004334 else
Jan Karab436b9b2009-12-08 23:51:10 -05004335 ext4_update_inode_fsync_trans(handle, inode, 0);
Eric Whitney8ad8d712020-05-10 11:58:05 -04004336
4337 map->m_flags |= (EXT4_MAP_NEW | EXT4_MAP_MAPPED);
4338 map->m_pblk = pblk;
4339 map->m_len = ar.len;
4340 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004341 ext4_ext_show_leaf(inode, path);
Eric Whitney8ad8d712020-05-10 11:58:05 -04004342out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004343 ext4_ext_drop_refs(path);
4344 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004345
Theodore Ts'o63b99962013-07-16 10:28:47 -04004346 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4347 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004348 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004349}
4350
Theodore Ts'od0abb362016-11-13 22:02:28 -05004351int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004352{
Alex Tomasa86c6182006-10-11 01:21:03 -07004353 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004354 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004355 int err = 0;
4356
4357 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004358 * TODO: optimization is possible here.
4359 * Probably we need not scan at all,
4360 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004361 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004362
4363 /* we have to know where to truncate from in crash case */
4364 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004365 err = ext4_mark_inode_dirty(handle, inode);
4366 if (err)
4367 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004368
4369 last_block = (inode->i_size + sb->s_blocksize - 1)
4370 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004371retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004372 err = ext4_es_remove_extent(inode, last_block,
4373 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004374 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004375 cond_resched();
4376 congestion_wait(BLK_RW_ASYNC, HZ/50);
4377 goto retry;
4378 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004379 if (err)
4380 return err;
Theodore Ts'o73c384c02020-05-07 10:50:28 -07004381retry_remove_space:
4382 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
4383 if (err == -ENOMEM) {
4384 cond_resched();
4385 congestion_wait(BLK_RW_ASYNC, HZ/50);
4386 goto retry_remove_space;
4387 }
4388 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004389}
4390
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004391static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004392 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004393 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004394{
Al Viro496ad9a2013-01-23 17:07:38 -05004395 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004396 handle_t *handle;
Theodore Ts'o64395d92021-03-21 00:45:37 -04004397 int ret = 0, ret2 = 0, ret3 = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004398 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004399 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004400 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004401 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004402 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004403
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004404 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004405 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004406 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004407 /*
4408 * Don't normalize the request if it can fit in one extent so
4409 * that it doesn't get unnecessarily split into multiple
4410 * extents.
4411 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004412 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004413 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004414
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004415 /*
4416 * credits to insert 1 extent into extent tree
4417 */
4418 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004419 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004420
Amit Aroraa2df2a62007-07-17 21:42:41 -04004421retry:
Eric Whitney32583862021-01-13 17:14:03 -05004422 while (len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004423 /*
4424 * Recalculate credits when extent tree depth changes.
4425 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004426 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004427 credits = ext4_chunk_trans_blocks(inode, len);
4428 depth = ext_depth(inode);
4429 }
4430
Theodore Ts'o9924a922013-02-08 21:59:22 -05004431 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4432 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004433 if (IS_ERR(handle)) {
4434 ret = PTR_ERR(handle);
4435 break;
4436 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004437 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c922008-01-28 23:58:27 -05004438 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004439 ext4_debug("inode #%lu: block %u: len %u: "
4440 "ext4_ext_map_blocks returned %d",
4441 inode->i_ino, map.m_lblk,
4442 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004443 ext4_mark_inode_dirty(handle, inode);
Eric Whitney32583862021-01-13 17:14:03 -05004444 ext4_journal_stop(handle);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004445 break;
4446 }
Eric Whitney32583862021-01-13 17:14:03 -05004447 /*
4448 * allow a full retry cycle for any remaining allocations
4449 */
4450 retries = 0;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004451 map.m_lblk += ret;
4452 map.m_len = len = len - ret;
4453 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004454 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004455 if (new_size) {
4456 if (epos > new_size)
4457 epos = new_size;
4458 if (ext4_update_inode_size(inode, epos) & 0x1)
4459 inode->i_mtime = inode->i_ctime;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004460 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004461 ret2 = ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004462 ext4_update_inode_fsync_trans(handle, inode, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004463 ret3 = ext4_journal_stop(handle);
4464 ret2 = ret3 ? ret3 : ret2;
4465 if (unlikely(ret2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004466 break;
4467 }
Eric Whitney32583862021-01-13 17:14:03 -05004468 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004469 goto retry;
Lukas Czernerf282ac12014-03-18 17:44:35 -04004470
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004471 return ret > 0 ? ret2 : ret;
4472}
4473
Eric Biggers43f81672019-12-31 12:04:40 -06004474static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
4475
4476static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len);
4477
Lukas Czernerb8a86842014-03-18 18:05:35 -04004478static long ext4_zero_range(struct file *file, loff_t offset,
4479 loff_t len, int mode)
4480{
4481 struct inode *inode = file_inode(file);
Jan Karad4f52582021-02-04 18:05:42 +01004482 struct address_space *mapping = file->f_mapping;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004483 handle_t *handle = NULL;
4484 unsigned int max_blocks;
4485 loff_t new_size = 0;
4486 int ret = 0;
4487 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004488 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004489 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004490 loff_t start, end;
4491 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004492 unsigned int blkbits = inode->i_blkbits;
4493
4494 trace_ext4_zero_range(inode, offset, len, mode);
4495
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004496 /* Call ext4_force_commit to flush all data in case of data=journal. */
4497 if (ext4_should_journal_data(inode)) {
4498 ret = ext4_force_commit(inode->i_sb);
4499 if (ret)
4500 return ret;
4501 }
4502
Lukas Czernerb8a86842014-03-18 18:05:35 -04004503 /*
Keyur Patele4d7f2d2020-06-10 23:19:46 -04004504 * Round up offset. This is not fallocate, we need to zero out
Lukas Czernerb8a86842014-03-18 18:05:35 -04004505 * blocks, so convert interior block aligned part of the range to
4506 * unwritten and possibly manually zero out unaligned parts of the
4507 * range.
4508 */
4509 start = round_up(offset, 1 << blkbits);
4510 end = round_down((offset + len), 1 << blkbits);
4511
4512 if (start < offset || end > offset + len)
4513 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004514 partial_begin = offset & ((1 << blkbits) - 1);
4515 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004516
4517 lblk = start >> blkbits;
4518 max_blocks = (end >> blkbits);
4519 if (max_blocks < lblk)
4520 max_blocks = 0;
4521 else
4522 max_blocks -= lblk;
4523
Al Viro59551022016-01-22 15:40:57 -05004524 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004525
4526 /*
Christophe JAILLET80dd4972020-05-03 22:06:47 +02004527 * Indirect files do not support unwritten extents
Lukas Czernerb8a86842014-03-18 18:05:35 -04004528 */
4529 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4530 ret = -EOPNOTSUPP;
4531 goto out_mutex;
4532 }
4533
4534 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004535 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004536 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004537 new_size = offset + len;
4538 ret = inode_newsize_ok(inode, new_size);
4539 if (ret)
4540 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004541 }
4542
Lukas Czerner0f2af212015-04-03 00:09:13 -04004543 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004544
Jan Kara17048e82015-12-07 14:29:17 -05004545 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004546 inode_dio_wait(inode);
4547
Lukas Czerner0f2af212015-04-03 00:09:13 -04004548 /* Preallocate the range including the unaligned edges */
4549 if (partial_begin || partial_end) {
4550 ret = ext4_alloc_file_blocks(file,
4551 round_down(offset, 1 << blkbits) >> blkbits,
4552 (round_up((offset + len), 1 << blkbits) -
4553 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004554 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004555 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004556 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004557
4558 }
4559
4560 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004561 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004562 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4563 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004564
Jan Karaea3d7202015-12-07 14:28:03 -05004565 /*
4566 * Prevent page faults from reinstantiating pages we have
4567 * released from page cache.
4568 */
Jan Karad4f52582021-02-04 18:05:42 +01004569 filemap_invalidate_lock(mapping);
Ross Zwisler430657b2018-07-29 17:00:22 -04004570
4571 ret = ext4_break_layouts(inode);
4572 if (ret) {
Jan Karad4f52582021-02-04 18:05:42 +01004573 filemap_invalidate_unlock(mapping);
Ross Zwisler430657b2018-07-29 17:00:22 -04004574 goto out_mutex;
4575 }
4576
Jan Kara01127842015-12-07 14:34:49 -05004577 ret = ext4_update_disksize_before_punch(inode, offset, len);
4578 if (ret) {
Jan Karad4f52582021-02-04 18:05:42 +01004579 filemap_invalidate_unlock(mapping);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004580 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004581 }
Jan Karaea3d7202015-12-07 14:28:03 -05004582 /* Now release the pages and zero block aligned part of pages */
4583 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004584 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004585
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004586 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004587 flags);
Jan Karad4f52582021-02-04 18:05:42 +01004588 filemap_invalidate_unlock(mapping);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004589 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004590 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004591 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004592 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004593 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004594
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004595 /*
4596 * In worst case we have to writeout two nonadjacent unwritten
4597 * blocks and update the inode
4598 */
4599 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4600 if (ext4_should_journal_data(inode))
4601 credits += 2;
4602 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004603 if (IS_ERR(handle)) {
4604 ret = PTR_ERR(handle);
4605 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004606 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004607 }
4608
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004609 inode->i_mtime = inode->i_ctime = current_time(inode);
Eric Whitney4337ecd2020-02-11 16:02:16 -05004610 if (new_size)
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004611 ext4_update_inode_size(inode, new_size);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004612 ret = ext4_mark_inode_dirty(handle, inode);
4613 if (unlikely(ret))
4614 goto out_handle;
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08004615 ext4_fc_track_range(handle, inode, offset >> inode->i_sb->s_blocksize_bits,
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004616 (offset + len - 1) >> inode->i_sb->s_blocksize_bits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004617 /* Zero out partial block at the edges of the range */
4618 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004619 if (ret >= 0)
4620 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004621
4622 if (file->f_flags & O_SYNC)
4623 ext4_handle_sync(handle);
4624
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004625out_handle:
Lukas Czernerb8a86842014-03-18 18:05:35 -04004626 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004627out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004628 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004629 return ret;
4630}
4631
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004632/*
4633 * preallocate space for a file. This implements ext4's fallocate file
4634 * operation, which gets called from sys_fallocate system call.
4635 * For block-mapped files, posix_fallocate should fall back to the method
4636 * of writing zeroes to the required new blocks (the same behavior which is
4637 * expected for file systems which do not support fallocate() system call).
4638 */
4639long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4640{
4641 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004642 loff_t new_size = 0;
4643 unsigned int max_blocks;
4644 int ret = 0;
4645 int flags;
4646 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004647 unsigned int blkbits = inode->i_blkbits;
4648
Michael Halcrow2058f832015-04-12 00:55:10 -04004649 /*
4650 * Encrypted inodes can't handle collapse range or insert
4651 * range since we would need to re-encrypt blocks with a
4652 * different IV or XTS tweak (which are based on the logical
4653 * block number).
Michael Halcrow2058f832015-04-12 00:55:10 -04004654 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304655 if (IS_ENCRYPTED(inode) &&
Eric Biggers457b1e32019-12-26 09:42:16 -06004656 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004657 return -EOPNOTSUPP;
4658
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004659 /* Return error if mode is not supported */
4660 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004661 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4662 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004663 return -EOPNOTSUPP;
4664
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004665 ext4_fc_start_update(inode);
4666
4667 if (mode & FALLOC_FL_PUNCH_HOLE) {
4668 ret = ext4_punch_hole(inode, offset, len);
4669 goto exit;
4670 }
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004671
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004672 ret = ext4_convert_inline_data(inode);
4673 if (ret)
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004674 goto exit;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004675
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004676 if (mode & FALLOC_FL_COLLAPSE_RANGE) {
4677 ret = ext4_collapse_range(inode, offset, len);
4678 goto exit;
4679 }
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004680
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004681 if (mode & FALLOC_FL_INSERT_RANGE) {
4682 ret = ext4_insert_range(inode, offset, len);
4683 goto exit;
4684 }
Namjae Jeon331573f2015-06-09 01:55:03 -04004685
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004686 if (mode & FALLOC_FL_ZERO_RANGE) {
4687 ret = ext4_zero_range(file, offset, len, mode);
4688 goto exit;
4689 }
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004690 trace_ext4_fallocate_enter(inode, offset, len, mode);
4691 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004692
Fabian Frederick518eaa62016-09-15 11:55:01 -04004693 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004694 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004695
Al Viro59551022016-01-22 15:40:57 -05004696 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004697
Davide Italiano280227a2015-05-02 23:21:15 -04004698 /*
4699 * We only support preallocation for extent-based files only
4700 */
4701 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4702 ret = -EOPNOTSUPP;
4703 goto out;
4704 }
4705
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004706 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004707 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004708 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004709 new_size = offset + len;
4710 ret = inode_newsize_ok(inode, new_size);
4711 if (ret)
4712 goto out;
4713 }
4714
Jan Kara17048e82015-12-07 14:29:17 -05004715 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004716 inode_dio_wait(inode);
4717
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004718 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004719 if (ret)
4720 goto out;
4721
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004722 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004723 ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
4724 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004725 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004726out:
Al Viro59551022016-01-22 15:40:57 -05004727 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004728 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004729exit:
4730 ext4_fc_stop_update(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004731 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004732}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004733
4734/*
Mingming Cao00314622009-09-28 15:49:08 -04004735 * This function convert a range of blocks to written extents
4736 * The caller of this function will pass the start offset and the size.
4737 * all unwritten extents within this range will be converted to
4738 * written extents.
4739 *
4740 * This function is called from the direct IO end io call back
4741 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004742 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004743 */
Jan Kara6b523df2013-06-04 13:21:11 -04004744int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4745 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004746{
Mingming Cao00314622009-09-28 15:49:08 -04004747 unsigned int max_blocks;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004748 int ret = 0, ret2 = 0, ret3 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004749 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304750 unsigned int blkbits = inode->i_blkbits;
4751 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004752
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004753 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004754 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4755
Ritesh Harjania00713e2019-10-16 13:07:08 +05304756 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004757 /*
4758 * credits to insert 1 extent into extent tree
4759 */
4760 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4761 }
Mingming Cao00314622009-09-28 15:49:08 -04004762 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004763 map.m_lblk += ret;
4764 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04004765 if (credits) {
4766 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4767 credits);
4768 if (IS_ERR(handle)) {
4769 ret = PTR_ERR(handle);
4770 break;
4771 }
Mingming Cao00314622009-09-28 15:49:08 -04004772 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004773 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004774 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05004775 if (ret <= 0)
4776 ext4_warning(inode->i_sb,
4777 "inode #%lu: block %u: len %u: "
4778 "ext4_ext_map_blocks returned %d",
4779 inode->i_ino, map.m_lblk,
4780 map.m_len, ret);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004781 ret2 = ext4_mark_inode_dirty(handle, inode);
4782 if (credits) {
4783 ret3 = ext4_journal_stop(handle);
4784 if (unlikely(ret3))
4785 ret2 = ret3;
4786 }
4787
Jan Kara6b523df2013-06-04 13:21:11 -04004788 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04004789 break;
4790 }
4791 return ret > 0 ? ret2 : ret;
4792}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004793
Ritesh Harjania00713e2019-10-16 13:07:08 +05304794int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
4795{
Ritesh Harjanid1e18b82020-10-08 20:32:48 +05304796 int ret = 0, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304797 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304798
4799 /*
4800 * This is somewhat ugly but the idea is clear: When transaction is
4801 * reserved, everything goes into it. Otherwise we rather start several
4802 * smaller transactions for conversion of each extent separately.
4803 */
4804 if (handle) {
4805 handle = ext4_journal_start_reserved(handle,
4806 EXT4_HT_EXT_CONVERT);
4807 if (IS_ERR(handle))
4808 return PTR_ERR(handle);
4809 }
4810
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304811 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
4812 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
4813 io_end_vec->offset,
4814 io_end_vec->size);
4815 if (ret)
4816 break;
4817 }
4818
Ritesh Harjania00713e2019-10-16 13:07:08 +05304819 if (handle)
4820 err = ext4_journal_stop(handle);
4821
4822 return ret < 0 ? ret : err;
4823}
4824
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304825static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004826{
4827 __u64 physical = 0;
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304828 __u64 length = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004829 int blockbits = inode->i_sb->s_blocksize_bits;
4830 int error = 0;
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304831 u16 iomap_type;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004832
4833 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004834 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004835 struct ext4_iloc iloc;
4836 int offset; /* offset of xattr in inode */
4837
4838 error = ext4_get_inode_loc(inode, &iloc);
4839 if (error)
4840 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04004841 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004842 offset = EXT4_GOOD_OLD_INODE_SIZE +
4843 EXT4_I(inode)->i_extra_isize;
4844 physical += offset;
4845 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004846 brelse(iloc.bh);
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304847 iomap_type = IOMAP_INLINE;
4848 } else if (EXT4_I(inode)->i_file_acl) { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04004849 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004850 length = inode->i_sb->s_blocksize;
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304851 iomap_type = IOMAP_MAPPED;
4852 } else {
4853 /* no in-inode or external block for xattr, so return -ENOENT */
4854 error = -ENOENT;
4855 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004856 }
4857
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304858 iomap->addr = physical;
4859 iomap->offset = 0;
4860 iomap->length = length;
4861 iomap->type = iomap_type;
4862 iomap->flags = 0;
4863out:
4864 return error;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004865}
4866
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304867static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset,
4868 loff_t length, unsigned flags,
4869 struct iomap *iomap, struct iomap *srcmap)
4870{
4871 int error;
4872
4873 error = ext4_iomap_xattr_fiemap(inode, iomap);
4874 if (error == 0 && (offset >= iomap->length))
4875 error = -ENOENT;
4876 return error;
4877}
4878
4879static const struct iomap_ops ext4_iomap_xattr_ops = {
4880 .iomap_begin = ext4_iomap_xattr_begin,
4881};
4882
Christoph Hellwig328e24a2020-05-05 17:43:15 +02004883static int ext4_fiemap_check_ranges(struct inode *inode, u64 start, u64 *len)
4884{
4885 u64 maxbytes;
4886
4887 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4888 maxbytes = inode->i_sb->s_maxbytes;
4889 else
4890 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
4891
4892 if (*len == 0)
4893 return -EINVAL;
4894 if (start > maxbytes)
4895 return -EFBIG;
4896
4897 /*
4898 * Shrink request scope to what the fs can actually handle.
4899 */
4900 if (*len > maxbytes || (maxbytes - *len) < start)
4901 *len = maxbytes - start;
4902 return 0;
4903}
4904
Christoph Hellwig03a5ed22020-05-23 09:30:08 +02004905int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4906 u64 start, u64 len)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004907{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004908 int error = 0;
4909
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004910 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
4911 error = ext4_ext_precache(inode);
4912 if (error)
4913 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004914 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004915 }
4916
Christoph Hellwig328e24a2020-05-05 17:43:15 +02004917 /*
4918 * For bitmap files the maximum size limit could be smaller than
4919 * s_maxbytes, so check len here manually instead of just relying on the
4920 * generic check.
4921 */
4922 error = ext4_fiemap_check_ranges(inode, start, &len);
4923 if (error)
4924 return error;
4925
Eric Sandeen6873fa02008-10-07 00:46:36 -04004926 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
Ritesh Harjanid3b6f23f2020-02-28 14:56:58 +05304927 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
Christoph Hellwig03a5ed22020-05-23 09:30:08 +02004928 return iomap_fiemap(inode, fieinfo, start, len,
4929 &ext4_iomap_xattr_ops);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004930 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05004931
Christoph Hellwig03a5ed22020-05-23 09:30:08 +02004932 return iomap_fiemap(inode, fieinfo, start, len, &ext4_iomap_report_ops);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004933}
4934
4935int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
4936 __u64 start, __u64 len)
4937{
Christoph Hellwig03a5ed22020-05-23 09:30:08 +02004938 ext4_lblk_t start_blk, len_blks;
4939 __u64 last_blk;
4940 int error = 0;
4941
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004942 if (ext4_has_inline_data(inode)) {
4943 int has_inline;
4944
4945 down_read(&EXT4_I(inode)->xattr_sem);
4946 has_inline = ext4_has_inline_data(inode);
4947 up_read(&EXT4_I(inode)->xattr_sem);
4948 if (has_inline)
4949 return 0;
4950 }
4951
Christoph Hellwig03a5ed22020-05-23 09:30:08 +02004952 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
4953 error = ext4_ext_precache(inode);
4954 if (error)
4955 return error;
4956 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
4957 }
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004958
Christoph Hellwig45dd0522020-05-23 09:30:14 +02004959 error = fiemap_prep(inode, fieinfo, start, &len, 0);
Christoph Hellwigcddf8a22020-05-23 09:30:13 +02004960 if (error)
4961 return error;
Christoph Hellwig03a5ed22020-05-23 09:30:08 +02004962
4963 error = ext4_fiemap_check_ranges(inode, start, &len);
4964 if (error)
4965 return error;
4966
4967 start_blk = start >> inode->i_sb->s_blocksize_bits;
4968 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
4969 if (last_blk >= EXT_MAX_BLOCKS)
4970 last_blk = EXT_MAX_BLOCKS-1;
4971 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
4972
4973 /*
4974 * Walk the extent tree gathering extent information
4975 * and pushing extents back to the user.
4976 */
4977 return ext4_fill_es_cache_info(inode, start_blk, len_blks, fieinfo);
4978}
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004979
Namjae Jeon9eb79482014-02-23 15:18:59 -05004980/*
4981 * ext4_access_path:
4982 * Function to access the path buffer for marking it dirty.
4983 * It also checks if there are sufficient credits left in the journal handle
4984 * to update path.
4985 */
4986static int
4987ext4_access_path(handle_t *handle, struct inode *inode,
4988 struct ext4_ext_path *path)
4989{
4990 int credits, err;
4991
4992 if (!ext4_handle_valid(handle))
4993 return 0;
4994
4995 /*
4996 * Check if need to extend journal credits
4997 * 3 for leaf, sb, and inode plus 2 (bmap and group
4998 * descriptor) for each block group; assume two block
4999 * groups
5000 */
Jan Karaa4130362019-11-05 17:44:16 +01005001 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01005002 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01005003 if (err < 0)
5004 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005005
5006 err = ext4_ext_get_access(handle, inode, path);
5007 return err;
5008}
5009
5010/*
5011 * ext4_ext_shift_path_extents:
5012 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005013 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5014 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005015 */
5016static int
5017ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5018 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005019 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005020{
5021 int depth, err = 0;
5022 struct ext4_extent *ex_start, *ex_last;
zhengbin4756ee12019-12-25 10:45:59 +08005023 bool update = false;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005024 depth = path->p_depth;
5025
5026 while (depth >= 0) {
5027 if (depth == path->p_depth) {
5028 ex_start = path[depth].p_ext;
5029 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005030 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005031
5032 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005033
5034 err = ext4_access_path(handle, inode, path + depth);
5035 if (err)
5036 goto out;
5037
5038 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
zhengbin4756ee12019-12-25 10:45:59 +08005039 update = true;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005040
Namjae Jeon9eb79482014-02-23 15:18:59 -05005041 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005042 if (SHIFT == SHIFT_LEFT) {
5043 le32_add_cpu(&ex_start->ee_block,
5044 -shift);
5045 /* Try to merge to the left. */
5046 if ((ex_start >
5047 EXT_FIRST_EXTENT(path[depth].p_hdr))
5048 &&
5049 ext4_ext_try_to_merge_right(inode,
5050 path, ex_start - 1))
5051 ex_last--;
5052 else
5053 ex_start++;
5054 } else {
5055 le32_add_cpu(&ex_last->ee_block, shift);
5056 ext4_ext_try_to_merge_right(inode, path,
5057 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005058 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005059 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005060 }
5061 err = ext4_ext_dirty(handle, inode, path + depth);
5062 if (err)
5063 goto out;
5064
5065 if (--depth < 0 || !update)
5066 break;
5067 }
5068
5069 /* Update index too */
5070 err = ext4_access_path(handle, inode, path + depth);
5071 if (err)
5072 goto out;
5073
Namjae Jeon331573f2015-06-09 01:55:03 -04005074 if (SHIFT == SHIFT_LEFT)
5075 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5076 else
5077 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005078 err = ext4_ext_dirty(handle, inode, path + depth);
5079 if (err)
5080 goto out;
5081
5082 /* we are done if current index is not a starting index */
5083 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5084 break;
5085
5086 depth--;
5087 }
5088
5089out:
5090 return err;
5091}
5092
5093/*
5094 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005095 * All the extents which lies in the range from @start to the last allocated
5096 * block for the @inode are shifted either towards left or right (depending
5097 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005098 * On success, 0 is returned, error otherwise.
5099 */
5100static int
5101ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005102 ext4_lblk_t start, ext4_lblk_t shift,
5103 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005104{
5105 struct ext4_ext_path *path;
5106 int ret = 0, depth;
5107 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005108 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005109
5110 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005111 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5112 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005113 if (IS_ERR(path))
5114 return PTR_ERR(path);
5115
5116 depth = path->p_depth;
5117 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005118 if (!extent)
5119 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005120
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005121 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005122
Namjae Jeon331573f2015-06-09 01:55:03 -04005123 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005124 * For left shifts, make sure the hole on the left is big enough to
5125 * accommodate the shift. For right shifts, make sure the last extent
5126 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005127 */
5128 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005129 path = ext4_find_extent(inode, start - 1, &path,
5130 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005131 if (IS_ERR(path))
5132 return PTR_ERR(path);
5133 depth = path->p_depth;
5134 extent = path[depth].p_ext;
5135 if (extent) {
5136 ex_start = le32_to_cpu(extent->ee_block);
5137 ex_end = le32_to_cpu(extent->ee_block) +
5138 ext4_ext_get_actual_len(extent);
5139 } else {
5140 ex_start = 0;
5141 ex_end = 0;
5142 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005143
Namjae Jeon331573f2015-06-09 01:55:03 -04005144 if ((start == ex_start && shift > ex_start) ||
5145 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005146 ret = -EINVAL;
5147 goto out;
5148 }
5149 } else {
5150 if (shift > EXT_MAX_BLOCKS -
5151 (stop + ext4_ext_get_actual_len(extent))) {
5152 ret = -EINVAL;
5153 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005154 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005155 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005156
Namjae Jeon331573f2015-06-09 01:55:03 -04005157 /*
5158 * In case of left shift, iterator points to start and it is increased
5159 * till we reach stop. In case of right shift, iterator points to stop
5160 * and it is decreased till we reach start.
5161 */
5162 if (SHIFT == SHIFT_LEFT)
5163 iterator = &start;
5164 else
5165 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005166
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005167 /*
5168 * Its safe to start updating extents. Start and stop are unsigned, so
5169 * in case of right shift if extent with 0 block is reached, iterator
5170 * becomes NULL to indicate the end of the loop.
5171 */
5172 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005173 path = ext4_find_extent(inode, *iterator, &path,
5174 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005175 if (IS_ERR(path))
5176 return PTR_ERR(path);
5177 depth = path->p_depth;
5178 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005179 if (!extent) {
5180 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005181 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005182 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005183 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005184 if (SHIFT == SHIFT_LEFT && *iterator >
5185 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005186 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005187 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5188 path[depth].p_ext++;
5189 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005190 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005191 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005192 }
5193 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005194
5195 if (SHIFT == SHIFT_LEFT) {
5196 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5197 *iterator = le32_to_cpu(extent->ee_block) +
5198 ext4_ext_get_actual_len(extent);
5199 } else {
5200 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005201 if (le32_to_cpu(extent->ee_block) > 0)
5202 *iterator = le32_to_cpu(extent->ee_block) - 1;
5203 else
5204 /* Beginning is reached, end of the loop */
5205 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005206 /* Update path extent in case we need to stop */
5207 while (le32_to_cpu(extent->ee_block) < start)
5208 extent++;
5209 path[depth].p_ext = extent;
5210 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005211 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005212 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005213 if (ret)
5214 break;
5215 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005216out:
5217 ext4_ext_drop_refs(path);
5218 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005219 return ret;
5220}
5221
5222/*
5223 * ext4_collapse_range:
5224 * This implements the fallocate's collapse range functionality for ext4
5225 * Returns: 0 and non-zero on error.
5226 */
Eric Biggers43f81672019-12-31 12:04:40 -06005227static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005228{
5229 struct super_block *sb = inode->i_sb;
Jan Karad4f52582021-02-04 18:05:42 +01005230 struct address_space *mapping = inode->i_mapping;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005231 ext4_lblk_t punch_start, punch_stop;
5232 handle_t *handle;
5233 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005234 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005235 int ret;
5236
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005237 /*
5238 * We need to test this early because xfstests assumes that a
5239 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5240 * system does not support collapse range.
5241 */
5242 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5243 return -EOPNOTSUPP;
5244
Eric Biggers9b02e492019-12-31 12:04:38 -06005245 /* Collapse range works only on fs cluster size aligned regions. */
5246 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005247 return -EINVAL;
5248
Namjae Jeon9eb79482014-02-23 15:18:59 -05005249 trace_ext4_collapse_range(inode, offset, len);
5250
5251 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5252 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5253
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005254 /* Call ext4_force_commit to flush all data in case of data=journal. */
5255 if (ext4_should_journal_data(inode)) {
5256 ret = ext4_force_commit(inode->i_sb);
5257 if (ret)
5258 return ret;
5259 }
5260
Al Viro59551022016-01-22 15:40:57 -05005261 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005262 /*
5263 * There is no need to overlap collapse range with EOF, in which case
5264 * it is effectively a truncate operation
5265 */
Eric Biggers9b02e492019-12-31 12:04:38 -06005266 if (offset + len >= inode->i_size) {
Lukas Czerner23fffa92014-04-12 09:56:41 -04005267 ret = -EINVAL;
5268 goto out_mutex;
5269 }
5270
Namjae Jeon9eb79482014-02-23 15:18:59 -05005271 /* Currently just for extent based files */
5272 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5273 ret = -EOPNOTSUPP;
5274 goto out_mutex;
5275 }
5276
Namjae Jeon9eb79482014-02-23 15:18:59 -05005277 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005278 inode_dio_wait(inode);
5279
Jan Karaea3d7202015-12-07 14:28:03 -05005280 /*
5281 * Prevent page faults from reinstantiating pages we have released from
5282 * page cache.
5283 */
Jan Karad4f52582021-02-04 18:05:42 +01005284 filemap_invalidate_lock(mapping);
Ross Zwisler430657b2018-07-29 17:00:22 -04005285
5286 ret = ext4_break_layouts(inode);
5287 if (ret)
5288 goto out_mmap;
5289
Jan Kara32ebffd2015-12-07 14:31:11 -05005290 /*
5291 * Need to round down offset to be aligned with page size boundary
5292 * for page size > block size.
5293 */
5294 ioffset = round_down(offset, PAGE_SIZE);
5295 /*
5296 * Write tail of the last page before removed range since it will get
5297 * removed from the page cache below.
5298 */
Jan Karad4f52582021-02-04 18:05:42 +01005299 ret = filemap_write_and_wait_range(mapping, ioffset, offset);
Jan Kara32ebffd2015-12-07 14:31:11 -05005300 if (ret)
5301 goto out_mmap;
5302 /*
5303 * Write data that will be shifted to preserve them when discarding
5304 * page cache below. We are also protected from pages becoming dirty
Jan Karad4f52582021-02-04 18:05:42 +01005305 * by i_rwsem and invalidate_lock.
Jan Kara32ebffd2015-12-07 14:31:11 -05005306 */
Jan Karad4f52582021-02-04 18:05:42 +01005307 ret = filemap_write_and_wait_range(mapping, offset + len,
Jan Kara32ebffd2015-12-07 14:31:11 -05005308 LLONG_MAX);
5309 if (ret)
5310 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005311 truncate_pagecache(inode, ioffset);
5312
Namjae Jeon9eb79482014-02-23 15:18:59 -05005313 credits = ext4_writepage_trans_blocks(inode);
5314 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5315 if (IS_ERR(handle)) {
5316 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005317 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005318 }
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005319 ext4_fc_start_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005320
5321 down_write(&EXT4_I(inode)->i_data_sem);
brookxu27bc4462020-08-17 15:36:15 +08005322 ext4_discard_preallocations(inode, 0);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005323
5324 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005325 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005326 if (ret) {
5327 up_write(&EXT4_I(inode)->i_data_sem);
5328 goto out_stop;
5329 }
5330
5331 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5332 if (ret) {
5333 up_write(&EXT4_I(inode)->i_data_sem);
5334 goto out_stop;
5335 }
brookxu27bc4462020-08-17 15:36:15 +08005336 ext4_discard_preallocations(inode, 0);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005337
5338 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005339 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005340 if (ret) {
5341 up_write(&EXT4_I(inode)->i_data_sem);
5342 goto out_stop;
5343 }
5344
Eric Biggers9b02e492019-12-31 12:04:38 -06005345 new_size = inode->i_size - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005346 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005347 EXT4_I(inode)->i_disksize = new_size;
5348
Namjae Jeon9eb79482014-02-23 15:18:59 -05005349 up_write(&EXT4_I(inode)->i_data_sem);
5350 if (IS_SYNC(inode))
5351 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005352 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005353 ret = ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005354 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005355
5356out_stop:
5357 ext4_journal_stop(handle);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005358 ext4_fc_stop_ineligible(sb);
Jan Karaea3d7202015-12-07 14:28:03 -05005359out_mmap:
Jan Karad4f52582021-02-04 18:05:42 +01005360 filemap_invalidate_unlock(mapping);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005361out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005362 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005363 return ret;
5364}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005365
Namjae Jeon331573f2015-06-09 01:55:03 -04005366/*
5367 * ext4_insert_range:
5368 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5369 * The data blocks starting from @offset to the EOF are shifted by @len
5370 * towards right to create a hole in the @inode. Inode size is increased
5371 * by len bytes.
5372 * Returns 0 on success, error otherwise.
5373 */
Eric Biggers43f81672019-12-31 12:04:40 -06005374static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon331573f2015-06-09 01:55:03 -04005375{
5376 struct super_block *sb = inode->i_sb;
Jan Karad4f52582021-02-04 18:05:42 +01005377 struct address_space *mapping = inode->i_mapping;
Namjae Jeon331573f2015-06-09 01:55:03 -04005378 handle_t *handle;
5379 struct ext4_ext_path *path;
5380 struct ext4_extent *extent;
5381 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5382 unsigned int credits, ee_len;
5383 int ret = 0, depth, split_flag = 0;
5384 loff_t ioffset;
5385
5386 /*
5387 * We need to test this early because xfstests assumes that an
5388 * insert range of (0, 1) will return EOPNOTSUPP if the file
5389 * system does not support insert range.
5390 */
5391 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5392 return -EOPNOTSUPP;
5393
Eric Biggers9b02e492019-12-31 12:04:38 -06005394 /* Insert range works only on fs cluster size aligned regions. */
5395 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon331573f2015-06-09 01:55:03 -04005396 return -EINVAL;
5397
Namjae Jeon331573f2015-06-09 01:55:03 -04005398 trace_ext4_insert_range(inode, offset, len);
5399
5400 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5401 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5402
5403 /* Call ext4_force_commit to flush all data in case of data=journal */
5404 if (ext4_should_journal_data(inode)) {
5405 ret = ext4_force_commit(inode->i_sb);
5406 if (ret)
5407 return ret;
5408 }
5409
Al Viro59551022016-01-22 15:40:57 -05005410 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005411 /* Currently just for extent based files */
5412 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5413 ret = -EOPNOTSUPP;
5414 goto out_mutex;
5415 }
5416
Eric Biggers9b02e492019-12-31 12:04:38 -06005417 /* Check whether the maximum file size would be exceeded */
5418 if (len > inode->i_sb->s_maxbytes - inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005419 ret = -EFBIG;
5420 goto out_mutex;
5421 }
5422
Eric Biggers9b02e492019-12-31 12:04:38 -06005423 /* Offset must be less than i_size */
5424 if (offset >= inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005425 ret = -EINVAL;
5426 goto out_mutex;
5427 }
5428
Namjae Jeon331573f2015-06-09 01:55:03 -04005429 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005430 inode_dio_wait(inode);
5431
Jan Karaea3d7202015-12-07 14:28:03 -05005432 /*
5433 * Prevent page faults from reinstantiating pages we have released from
5434 * page cache.
5435 */
Jan Karad4f52582021-02-04 18:05:42 +01005436 filemap_invalidate_lock(mapping);
Ross Zwisler430657b2018-07-29 17:00:22 -04005437
5438 ret = ext4_break_layouts(inode);
5439 if (ret)
5440 goto out_mmap;
5441
Jan Kara32ebffd2015-12-07 14:31:11 -05005442 /*
5443 * Need to round down to align start offset to page size boundary
5444 * for page size > block size.
5445 */
5446 ioffset = round_down(offset, PAGE_SIZE);
5447 /* Write out all dirty pages */
5448 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5449 LLONG_MAX);
5450 if (ret)
5451 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005452 truncate_pagecache(inode, ioffset);
5453
Namjae Jeon331573f2015-06-09 01:55:03 -04005454 credits = ext4_writepage_trans_blocks(inode);
5455 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5456 if (IS_ERR(handle)) {
5457 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005458 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005459 }
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005460 ext4_fc_start_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005461
5462 /* Expand file to avoid data loss if there is error while shifting */
5463 inode->i_size += len;
5464 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005465 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005466 ret = ext4_mark_inode_dirty(handle, inode);
5467 if (ret)
5468 goto out_stop;
5469
5470 down_write(&EXT4_I(inode)->i_data_sem);
brookxu27bc4462020-08-17 15:36:15 +08005471 ext4_discard_preallocations(inode, 0);
Namjae Jeon331573f2015-06-09 01:55:03 -04005472
5473 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5474 if (IS_ERR(path)) {
5475 up_write(&EXT4_I(inode)->i_data_sem);
5476 goto out_stop;
5477 }
5478
5479 depth = ext_depth(inode);
5480 extent = path[depth].p_ext;
5481 if (extent) {
5482 ee_start_lblk = le32_to_cpu(extent->ee_block);
5483 ee_len = ext4_ext_get_actual_len(extent);
5484
5485 /*
5486 * If offset_lblk is not the starting block of extent, split
5487 * the extent @offset_lblk
5488 */
5489 if ((offset_lblk > ee_start_lblk) &&
5490 (offset_lblk < (ee_start_lblk + ee_len))) {
5491 if (ext4_ext_is_unwritten(extent))
5492 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5493 EXT4_EXT_MARK_UNWRIT2;
5494 ret = ext4_split_extent_at(handle, inode, &path,
5495 offset_lblk, split_flag,
5496 EXT4_EX_NOCACHE |
5497 EXT4_GET_BLOCKS_PRE_IO |
5498 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5499 }
5500
5501 ext4_ext_drop_refs(path);
5502 kfree(path);
5503 if (ret < 0) {
5504 up_write(&EXT4_I(inode)->i_data_sem);
5505 goto out_stop;
5506 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005507 } else {
5508 ext4_ext_drop_refs(path);
5509 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005510 }
5511
5512 ret = ext4_es_remove_extent(inode, offset_lblk,
5513 EXT_MAX_BLOCKS - offset_lblk);
5514 if (ret) {
5515 up_write(&EXT4_I(inode)->i_data_sem);
5516 goto out_stop;
5517 }
5518
5519 /*
5520 * if offset_lblk lies in a hole which is at start of file, use
5521 * ee_start_lblk to shift extents
5522 */
5523 ret = ext4_ext_shift_extents(inode, handle,
5524 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5525 len_lblk, SHIFT_RIGHT);
5526
5527 up_write(&EXT4_I(inode)->i_data_sem);
5528 if (IS_SYNC(inode))
5529 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005530 if (ret >= 0)
5531 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005532
5533out_stop:
5534 ext4_journal_stop(handle);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005535 ext4_fc_stop_ineligible(sb);
Jan Karaea3d7202015-12-07 14:28:03 -05005536out_mmap:
Jan Karad4f52582021-02-04 18:05:42 +01005537 filemap_invalidate_unlock(mapping);
Namjae Jeon331573f2015-06-09 01:55:03 -04005538out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005539 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005540 return ret;
5541}
5542
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005543/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005544 * ext4_swap_extents() - Swap extents between two inodes
5545 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005546 * @inode1: First inode
5547 * @inode2: Second inode
5548 * @lblk1: Start block for first inode
5549 * @lblk2: Start block for second inode
5550 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005551 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005552 * @erp: Pointer to save error value
5553 *
5554 * This helper routine does exactly what is promise "swap extents". All other
5555 * stuff such as page-cache locking consistency, bh mapping consistency or
5556 * extent's data copying must be performed by caller.
5557 * Locking:
5558 * i_mutex is held for both inodes
5559 * i_data_sem is locked for write for both inodes
5560 * Assumptions:
5561 * All pages from requested range are locked for both inodes
5562 */
5563int
5564ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005565 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005566 ext4_lblk_t count, int unwritten, int *erp)
5567{
5568 struct ext4_ext_path *path1 = NULL;
5569 struct ext4_ext_path *path2 = NULL;
5570 int replaced_count = 0;
5571
5572 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5573 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005574 BUG_ON(!inode_is_locked(inode1));
5575 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005576
5577 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005578 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005579 return 0;
5580 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005581 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005582 return 0;
5583
5584 while (count) {
5585 struct ext4_extent *ex1, *ex2, tmp_ex;
5586 ext4_lblk_t e1_blk, e2_blk;
5587 int e1_len, e2_len, len;
5588 int split = 0;
5589
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005590 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305591 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005592 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005593 path1 = NULL;
5594 finish:
5595 count = 0;
5596 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005597 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005598 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305599 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005600 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005601 path2 = NULL;
5602 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005603 }
5604 ex1 = path1[path1->p_depth].p_ext;
5605 ex2 = path2[path2->p_depth].p_ext;
Keyur Patele4d7f2d2020-06-10 23:19:46 -04005606 /* Do we have something to swap ? */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005607 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005608 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005609
5610 e1_blk = le32_to_cpu(ex1->ee_block);
5611 e2_blk = le32_to_cpu(ex2->ee_block);
5612 e1_len = ext4_ext_get_actual_len(ex1);
5613 e2_len = ext4_ext_get_actual_len(ex2);
5614
5615 /* Hole handling */
5616 if (!in_range(lblk1, e1_blk, e1_len) ||
5617 !in_range(lblk2, e2_blk, e2_len)) {
5618 ext4_lblk_t next1, next2;
5619
5620 /* if hole after extent, then go to next extent */
5621 next1 = ext4_ext_next_allocated_block(path1);
5622 next2 = ext4_ext_next_allocated_block(path2);
5623 /* If hole before extent, then shift to that extent */
5624 if (e1_blk > lblk1)
5625 next1 = e1_blk;
5626 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005627 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005628 /* Do we have something to swap */
5629 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005630 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005631 /* Move to the rightest boundary */
5632 len = next1 - lblk1;
5633 if (len < next2 - lblk2)
5634 len = next2 - lblk2;
5635 if (len > count)
5636 len = count;
5637 lblk1 += len;
5638 lblk2 += len;
5639 count -= len;
5640 goto repeat;
5641 }
5642
5643 /* Prepare left boundary */
5644 if (e1_blk < lblk1) {
5645 split = 1;
5646 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005647 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005648 if (unlikely(*erp))
5649 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005650 }
5651 if (e2_blk < lblk2) {
5652 split = 1;
5653 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005654 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005655 if (unlikely(*erp))
5656 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005657 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005658 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005659 * path must to be revalidated. */
5660 if (split)
5661 goto repeat;
5662
5663 /* Prepare right boundary */
5664 len = count;
5665 if (len > e1_blk + e1_len - lblk1)
5666 len = e1_blk + e1_len - lblk1;
5667 if (len > e2_blk + e2_len - lblk2)
5668 len = e2_blk + e2_len - lblk2;
5669
5670 if (len != e1_len) {
5671 split = 1;
5672 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005673 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005674 if (unlikely(*erp))
5675 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005676 }
5677 if (len != e2_len) {
5678 split = 1;
5679 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005680 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005681 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005682 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005683 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005684 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005685 * path must to be revalidated. */
5686 if (split)
5687 goto repeat;
5688
5689 BUG_ON(e2_len != e1_len);
5690 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005691 if (unlikely(*erp))
5692 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005693 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005694 if (unlikely(*erp))
5695 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005696
5697 /* Both extents are fully inside boundaries. Swap it now */
5698 tmp_ex = *ex1;
5699 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5700 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5701 ex1->ee_len = cpu_to_le16(e2_len);
5702 ex2->ee_len = cpu_to_le16(e1_len);
5703 if (unwritten)
5704 ext4_ext_mark_unwritten(ex2);
5705 if (ext4_ext_is_unwritten(&tmp_ex))
5706 ext4_ext_mark_unwritten(ex1);
5707
5708 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5709 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5710 *erp = ext4_ext_dirty(handle, inode2, path2 +
5711 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005712 if (unlikely(*erp))
5713 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005714 *erp = ext4_ext_dirty(handle, inode1, path1 +
5715 path1->p_depth);
5716 /*
5717 * Looks scarry ah..? second inode already points to new blocks,
5718 * and it was successfully dirtied. But luckily error may happen
5719 * only due to journal error, so full transaction will be
5720 * aborted anyway.
5721 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005722 if (unlikely(*erp))
5723 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005724 lblk1 += len;
5725 lblk2 += len;
5726 replaced_count += len;
5727 count -= len;
5728
5729 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005730 ext4_ext_drop_refs(path1);
5731 kfree(path1);
5732 ext4_ext_drop_refs(path2);
5733 kfree(path2);
5734 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005735 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005736 return replaced_count;
5737}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005738
5739/*
5740 * ext4_clu_mapped - determine whether any block in a logical cluster has
5741 * been mapped to a physical cluster
5742 *
5743 * @inode - file containing the logical cluster
5744 * @lclu - logical cluster of interest
5745 *
5746 * Returns 1 if any block in the logical cluster is mapped, signifying
5747 * that a physical cluster has been allocated for it. Otherwise,
5748 * returns 0. Can also return negative error codes. Derived from
5749 * ext4_ext_map_blocks().
5750 */
5751int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5752{
5753 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5754 struct ext4_ext_path *path;
5755 int depth, mapped = 0, err = 0;
5756 struct ext4_extent *extent;
5757 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5758
5759 /* search for the extent closest to the first block in the cluster */
5760 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5761 if (IS_ERR(path)) {
5762 err = PTR_ERR(path);
5763 path = NULL;
5764 goto out;
5765 }
5766
5767 depth = ext_depth(inode);
5768
5769 /*
5770 * A consistent leaf must not be empty. This situation is possible,
5771 * though, _during_ tree modification, and it's why an assert can't
5772 * be put in ext4_find_extent().
5773 */
5774 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
5775 EXT4_ERROR_INODE(inode,
5776 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
5777 (unsigned long) EXT4_C2B(sbi, lclu),
5778 depth, path[depth].p_block);
5779 err = -EFSCORRUPTED;
5780 goto out;
5781 }
5782
5783 extent = path[depth].p_ext;
5784
5785 /* can't be mapped if the extent tree is empty */
5786 if (extent == NULL)
5787 goto out;
5788
5789 first_lblk = le32_to_cpu(extent->ee_block);
5790 first_lclu = EXT4_B2C(sbi, first_lblk);
5791
5792 /*
5793 * Three possible outcomes at this point - found extent spanning
5794 * the target cluster, to the left of the target cluster, or to the
5795 * right of the target cluster. The first two cases are handled here.
5796 * The last case indicates the target cluster is not mapped.
5797 */
5798 if (lclu >= first_lclu) {
5799 last_lclu = EXT4_B2C(sbi, first_lblk +
5800 ext4_ext_get_actual_len(extent) - 1);
5801 if (lclu <= last_lclu) {
5802 mapped = 1;
5803 } else {
5804 first_lblk = ext4_ext_next_allocated_block(path);
5805 first_lclu = EXT4_B2C(sbi, first_lblk);
5806 if (lclu == first_lclu)
5807 mapped = 1;
5808 }
5809 }
5810
5811out:
5812 ext4_ext_drop_refs(path);
5813 kfree(path);
5814
5815 return err ? err : mapped;
5816}
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07005817
5818/*
5819 * Updates physical block address and unwritten status of extent
5820 * starting at lblk start and of len. If such an extent doesn't exist,
5821 * this function splits the extent tree appropriately to create an
5822 * extent like this. This function is called in the fast commit
5823 * replay path. Returns 0 on success and error on failure.
5824 */
5825int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start,
5826 int len, int unwritten, ext4_fsblk_t pblk)
5827{
5828 struct ext4_ext_path *path = NULL, *ppath;
5829 struct ext4_extent *ex;
5830 int ret;
5831
5832 path = ext4_find_extent(inode, start, NULL, 0);
Dan Carpenterbc185462020-10-23 14:22:32 +03005833 if (IS_ERR(path))
5834 return PTR_ERR(path);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07005835 ex = path[path->p_depth].p_ext;
5836 if (!ex) {
5837 ret = -EFSCORRUPTED;
5838 goto out;
5839 }
5840
5841 if (le32_to_cpu(ex->ee_block) != start ||
5842 ext4_ext_get_actual_len(ex) != len) {
5843 /* We need to split this extent to match our extent first */
5844 ppath = path;
5845 down_write(&EXT4_I(inode)->i_data_sem);
5846 ret = ext4_force_split_extent_at(NULL, inode, &ppath, start, 1);
5847 up_write(&EXT4_I(inode)->i_data_sem);
5848 if (ret)
5849 goto out;
5850 kfree(path);
5851 path = ext4_find_extent(inode, start, NULL, 0);
5852 if (IS_ERR(path))
5853 return -1;
5854 ppath = path;
5855 ex = path[path->p_depth].p_ext;
5856 WARN_ON(le32_to_cpu(ex->ee_block) != start);
5857 if (ext4_ext_get_actual_len(ex) != len) {
5858 down_write(&EXT4_I(inode)->i_data_sem);
5859 ret = ext4_force_split_extent_at(NULL, inode, &ppath,
5860 start + len, 1);
5861 up_write(&EXT4_I(inode)->i_data_sem);
5862 if (ret)
5863 goto out;
5864 kfree(path);
5865 path = ext4_find_extent(inode, start, NULL, 0);
5866 if (IS_ERR(path))
5867 return -EINVAL;
5868 ex = path[path->p_depth].p_ext;
5869 }
5870 }
5871 if (unwritten)
5872 ext4_ext_mark_unwritten(ex);
5873 else
5874 ext4_ext_mark_initialized(ex);
5875 ext4_ext_store_pblock(ex, pblk);
5876 down_write(&EXT4_I(inode)->i_data_sem);
5877 ret = ext4_ext_dirty(NULL, inode, &path[path->p_depth]);
5878 up_write(&EXT4_I(inode)->i_data_sem);
5879out:
5880 ext4_ext_drop_refs(path);
5881 kfree(path);
5882 ext4_mark_inode_dirty(NULL, inode);
5883 return ret;
5884}
5885
5886/* Try to shrink the extent tree */
5887void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end)
5888{
5889 struct ext4_ext_path *path = NULL;
5890 struct ext4_extent *ex;
5891 ext4_lblk_t old_cur, cur = 0;
5892
5893 while (cur < end) {
5894 path = ext4_find_extent(inode, cur, NULL, 0);
5895 if (IS_ERR(path))
5896 return;
5897 ex = path[path->p_depth].p_ext;
5898 if (!ex) {
5899 ext4_ext_drop_refs(path);
5900 kfree(path);
5901 ext4_mark_inode_dirty(NULL, inode);
5902 return;
5903 }
5904 old_cur = cur;
5905 cur = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
5906 if (cur <= old_cur)
5907 cur = old_cur + 1;
5908 ext4_ext_try_to_merge(NULL, inode, path, ex);
5909 down_write(&EXT4_I(inode)->i_data_sem);
5910 ext4_ext_dirty(NULL, inode, &path[path->p_depth]);
5911 up_write(&EXT4_I(inode)->i_data_sem);
5912 ext4_mark_inode_dirty(NULL, inode);
5913 ext4_ext_drop_refs(path);
5914 kfree(path);
5915 }
5916}
5917
5918/* Check if *cur is a hole and if it is, skip it */
5919static void skip_hole(struct inode *inode, ext4_lblk_t *cur)
5920{
5921 int ret;
5922 struct ext4_map_blocks map;
5923
5924 map.m_lblk = *cur;
5925 map.m_len = ((inode->i_size) >> inode->i_sb->s_blocksize_bits) - *cur;
5926
5927 ret = ext4_map_blocks(NULL, inode, &map, 0);
5928 if (ret != 0)
5929 return;
5930 *cur = *cur + map.m_len;
5931}
5932
5933/* Count number of blocks used by this inode and update i_blocks */
5934int ext4_ext_replay_set_iblocks(struct inode *inode)
5935{
5936 struct ext4_ext_path *path = NULL, *path2 = NULL;
5937 struct ext4_extent *ex;
5938 ext4_lblk_t cur = 0, end;
5939 int numblks = 0, i, ret = 0;
5940 ext4_fsblk_t cmp1, cmp2;
5941 struct ext4_map_blocks map;
5942
5943 /* Determin the size of the file first */
5944 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5945 EXT4_EX_NOCACHE);
5946 if (IS_ERR(path))
5947 return PTR_ERR(path);
5948 ex = path[path->p_depth].p_ext;
5949 if (!ex) {
5950 ext4_ext_drop_refs(path);
5951 kfree(path);
5952 goto out;
5953 }
5954 end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
5955 ext4_ext_drop_refs(path);
5956 kfree(path);
5957
5958 /* Count the number of data blocks */
5959 cur = 0;
5960 while (cur < end) {
5961 map.m_lblk = cur;
5962 map.m_len = end - cur;
5963 ret = ext4_map_blocks(NULL, inode, &map, 0);
5964 if (ret < 0)
5965 break;
5966 if (ret > 0)
5967 numblks += ret;
5968 cur = cur + map.m_len;
5969 }
5970
5971 /*
5972 * Count the number of extent tree blocks. We do it by looking up
5973 * two successive extents and determining the difference between
5974 * their paths. When path is different for 2 successive extents
5975 * we compare the blocks in the path at each level and increment
5976 * iblocks by total number of differences found.
5977 */
5978 cur = 0;
5979 skip_hole(inode, &cur);
5980 path = ext4_find_extent(inode, cur, NULL, 0);
5981 if (IS_ERR(path))
5982 goto out;
5983 numblks += path->p_depth;
5984 ext4_ext_drop_refs(path);
5985 kfree(path);
5986 while (cur < end) {
5987 path = ext4_find_extent(inode, cur, NULL, 0);
5988 if (IS_ERR(path))
5989 break;
5990 ex = path[path->p_depth].p_ext;
5991 if (!ex) {
5992 ext4_ext_drop_refs(path);
5993 kfree(path);
5994 return 0;
5995 }
5996 cur = max(cur + 1, le32_to_cpu(ex->ee_block) +
5997 ext4_ext_get_actual_len(ex));
5998 skip_hole(inode, &cur);
5999
6000 path2 = ext4_find_extent(inode, cur, NULL, 0);
6001 if (IS_ERR(path2)) {
6002 ext4_ext_drop_refs(path);
6003 kfree(path);
6004 break;
6005 }
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07006006 for (i = 0; i <= max(path->p_depth, path2->p_depth); i++) {
6007 cmp1 = cmp2 = 0;
6008 if (i <= path->p_depth)
6009 cmp1 = path[i].p_bh ?
6010 path[i].p_bh->b_blocknr : 0;
6011 if (i <= path2->p_depth)
6012 cmp2 = path2[i].p_bh ?
6013 path2[i].p_bh->b_blocknr : 0;
6014 if (cmp1 != cmp2 && cmp2 != 0)
6015 numblks++;
6016 }
6017 ext4_ext_drop_refs(path);
6018 ext4_ext_drop_refs(path2);
6019 kfree(path);
6020 kfree(path2);
6021 }
6022
6023out:
6024 inode->i_blocks = numblks << (inode->i_sb->s_blocksize_bits - 9);
6025 ext4_mark_inode_dirty(NULL, inode);
6026 return 0;
6027}
6028
6029int ext4_ext_clear_bb(struct inode *inode)
6030{
6031 struct ext4_ext_path *path = NULL;
6032 struct ext4_extent *ex;
6033 ext4_lblk_t cur = 0, end;
6034 int j, ret = 0;
6035 struct ext4_map_blocks map;
6036
6037 /* Determin the size of the file first */
6038 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
6039 EXT4_EX_NOCACHE);
6040 if (IS_ERR(path))
6041 return PTR_ERR(path);
6042 ex = path[path->p_depth].p_ext;
6043 if (!ex) {
6044 ext4_ext_drop_refs(path);
6045 kfree(path);
6046 return 0;
6047 }
6048 end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
6049 ext4_ext_drop_refs(path);
6050 kfree(path);
6051
6052 cur = 0;
6053 while (cur < end) {
6054 map.m_lblk = cur;
6055 map.m_len = end - cur;
6056 ret = ext4_map_blocks(NULL, inode, &map, 0);
6057 if (ret < 0)
6058 break;
6059 if (ret > 0) {
6060 path = ext4_find_extent(inode, map.m_lblk, NULL, 0);
6061 if (!IS_ERR_OR_NULL(path)) {
6062 for (j = 0; j < path->p_depth; j++) {
6063
6064 ext4_mb_mark_bb(inode->i_sb,
6065 path[j].p_block, 1, 0);
6066 }
6067 ext4_ext_drop_refs(path);
6068 kfree(path);
6069 }
6070 ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0);
6071 }
6072 cur = cur + map.m_len;
6073 }
6074
6075 return 0;
6076}