blob: 2f711cc3cdcecfb657b1306898c911dd96a6d1ca [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 Harjanid3b6f232020-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);
103 ext4_discard_preallocations(inode);
104 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");
Alex Tomasa86c6182006-10-11 01:21:03 -0700142 return ext4_journal_get_write_access(handle, path->p_bh);
143 }
144 /* path points to leaf/index in inode body */
145 /* we use in-core data, no need to protect them */
146 return 0;
147}
148
149/*
150 * could return:
151 * - EROFS
152 * - ENOMEM
153 * - EIO
154 */
Eric Biggers43f81672019-12-31 12:04:40 -0600155static int __ext4_ext_dirty(const char *where, unsigned int line,
156 handle_t *handle, struct inode *inode,
157 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700158{
159 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400160
161 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700162 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400163 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700164 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400165 err = __ext4_handle_dirty_metadata(where, line, handle,
166 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700167 } else {
168 /* path points to leaf/index in inode body */
169 err = ext4_mark_inode_dirty(handle, inode);
170 }
171 return err;
172}
173
Eric Biggers43f81672019-12-31 12:04:40 -0600174#define ext4_ext_dirty(handle, inode, path) \
175 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
176
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700177static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700178 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500179 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700180{
Alex Tomasa86c6182006-10-11 01:21:03 -0700181 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400182 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700184
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500185 /*
186 * Try to predict block placement assuming that we are
187 * filling in a file which will eventually be
188 * non-sparse --- i.e., in the case of libbfd writing
189 * an ELF object sections out-of-order but in a way
190 * the eventually results in a contiguous object or
191 * executable file, or some database extending a table
192 * space file. However, this is actually somewhat
193 * non-ideal if we are writing a sparse file such as
194 * qemu or KVM writing a raw image file that is going
195 * to stay fairly sparse, since it will end up
196 * fragmenting the file system's free space. Maybe we
197 * should have some hueristics or some way to allow
198 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800199 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500200 * common.
201 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800202 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500203 if (ex) {
204 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
205 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
206
207 if (block > ext_block)
208 return ext_pblk + (block - ext_block);
209 else
210 return ext_pblk - (ext_block - block);
211 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700212
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700213 /* it looks like index is empty;
214 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700215 if (path[depth].p_bh)
216 return path[depth].p_bh->b_blocknr;
217 }
218
219 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400220 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700221}
222
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400223/*
224 * Allocation for a meta data block
225 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700226static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400227ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700228 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400229 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700230{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700231 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700232
233 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400234 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
235 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700236 return newblock;
237}
238
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700240{
241 int size;
242
243 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
244 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100245#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400246 if (!check && size > 6)
247 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700248#endif
249 return size;
250}
251
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400252static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700253{
254 int size;
255
256 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
257 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100258#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400259 if (!check && size > 5)
260 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700261#endif
262 return size;
263}
264
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700266{
267 int size;
268
269 size = sizeof(EXT4_I(inode)->i_data);
270 size -= sizeof(struct ext4_extent_header);
271 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100272#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400273 if (!check && size > 3)
274 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700275#endif
276 return size;
277}
278
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400279static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700280{
281 int size;
282
283 size = sizeof(EXT4_I(inode)->i_data);
284 size -= sizeof(struct ext4_extent_header);
285 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100286#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400287 if (!check && size > 4)
288 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700289#endif
290 return size;
291}
292
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400293static inline int
294ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400295 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400296 int nofail)
297{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400298 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400299 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700300 int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO;
301
302 if (nofail)
303 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400304
Theodore Ts'odfe50802014-09-01 14:37:09 -0400305 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400306 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700307 flags);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400308}
309
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400310static int
311ext4_ext_max_entries(struct inode *inode, int depth)
312{
313 int max;
314
315 if (depth == ext_depth(inode)) {
316 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400317 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400318 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400319 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400320 } else {
321 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400322 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400323 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400324 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400325 }
326
327 return max;
328}
329
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400330static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
331{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400332 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400333 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500334 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400335
Vegard Nossumf70749c2016-06-30 11:53:46 -0400336 /*
337 * We allow neither:
338 * - zero length
339 * - overflow/wrap-around
340 */
341 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400342 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400343 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400344}
345
346static int ext4_valid_extent_idx(struct inode *inode,
347 struct ext4_extent_idx *ext_idx)
348{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400349 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400350
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400351 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400352}
353
354static int ext4_valid_extent_entries(struct inode *inode,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400355 struct ext4_extent_header *eh,
356 ext4_fsblk_t *pblk, int depth)
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400357{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400358 unsigned short entries;
359 if (eh->eh_entries == 0)
360 return 1;
361
362 entries = le16_to_cpu(eh->eh_entries);
363
364 if (depth == 0) {
365 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400366 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500367 ext4_lblk_t lblock = 0;
368 ext4_lblk_t prev = 0;
369 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400370 while (entries) {
371 if (!ext4_valid_extent(inode, ext))
372 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500373
374 /* Check for overlapping extents */
375 lblock = le32_to_cpu(ext->ee_block);
376 len = ext4_ext_get_actual_len(ext);
377 if ((lblock <= prev) && prev) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400378 *pblk = ext4_ext_pblock(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500379 return 0;
380 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400381 ext++;
382 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500383 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400384 }
385 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400386 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400387 while (entries) {
388 if (!ext4_valid_extent_idx(inode, ext_idx))
389 return 0;
390 ext_idx++;
391 entries--;
392 }
393 }
394 return 1;
395}
396
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400397static int __ext4_ext_check(const char *function, unsigned int line,
398 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400399 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400400{
401 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400402 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400403
404 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
405 error_msg = "invalid magic";
406 goto corrupted;
407 }
408 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
409 error_msg = "unexpected eh_depth";
410 goto corrupted;
411 }
412 if (unlikely(eh->eh_max == 0)) {
413 error_msg = "invalid eh_max";
414 goto corrupted;
415 }
416 max = ext4_ext_max_entries(inode, depth);
417 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
418 error_msg = "too large eh_max";
419 goto corrupted;
420 }
421 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
422 error_msg = "invalid eh_entries";
423 goto corrupted;
424 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400425 if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) {
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400426 error_msg = "invalid extent entries";
427 goto corrupted;
428 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400429 if (unlikely(depth > 32)) {
430 error_msg = "too large eh_depth";
431 goto corrupted;
432 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400433 /* Verify checksum on non-root extent tree nodes */
434 if (ext_depth(inode) != depth &&
435 !ext4_extent_block_csum_verify(inode, eh)) {
436 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400437 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400438 goto corrupted;
439 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400440 return 0;
441
442corrupted:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400443 ext4_error_inode_err(inode, function, line, 0, -err,
444 "pblk %llu bad header/extent: %s - magic %x, "
445 "entries %u, max %u(%u), depth %u(%u)",
446 (unsigned long long) pblk, error_msg,
447 le16_to_cpu(eh->eh_magic),
448 le16_to_cpu(eh->eh_entries),
449 le16_to_cpu(eh->eh_max),
450 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400451 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400452}
453
Theodore Ts'oc3491792013-08-16 21:21:41 -0400454#define ext4_ext_check(inode, eh, depth, pblk) \
455 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400456
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400457int ext4_ext_check_inode(struct inode *inode)
458{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400459 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400460}
461
Dmitry Monakhov40686642019-11-06 12:25:02 +0000462static void ext4_cache_extents(struct inode *inode,
463 struct ext4_extent_header *eh)
464{
465 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
466 ext4_lblk_t prev = 0;
467 int i;
468
469 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
470 unsigned int status = EXTENT_STATUS_WRITTEN;
471 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
472 int len = ext4_ext_get_actual_len(ex);
473
474 if (prev && (prev != lblk))
475 ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
476 EXTENT_STATUS_HOLE);
477
478 if (ext4_ext_is_unwritten(ex))
479 status = EXTENT_STATUS_UNWRITTEN;
480 ext4_es_cache_extent(inode, lblk, len,
481 ext4_ext_pblock(ex), status);
482 prev = lblk + len;
483 }
484}
485
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400486static struct buffer_head *
487__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400488 struct inode *inode, ext4_fsblk_t pblk, int depth,
489 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400490{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400491 struct buffer_head *bh;
492 int err;
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700493 gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400494
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700495 if (flags & EXT4_EX_NOFAIL)
496 gfp_flags |= __GFP_NOFAIL;
497
498 bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400499 if (unlikely(!bh))
500 return ERR_PTR(-ENOMEM);
501
502 if (!bh_uptodate_or_lock(bh)) {
503 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
504 err = bh_submit_read(bh);
505 if (err < 0)
506 goto errout;
507 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400508 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400509 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400510 if (!ext4_has_feature_journal(inode->i_sb) ||
511 (inode->i_ino !=
512 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
513 err = __ext4_ext_check(function, line, inode,
514 ext_block_hdr(bh), depth, pblk);
515 if (err)
516 goto errout;
517 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400518 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400519 /*
520 * If this is a leaf block, cache all of its entries
521 */
522 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
523 struct ext4_extent_header *eh = ext_block_hdr(bh);
Dmitry Monakhov40686642019-11-06 12:25:02 +0000524 ext4_cache_extents(inode, eh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400525 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400526 return bh;
527errout:
528 put_bh(bh);
529 return ERR_PTR(err);
530
Darrick J. Wongf8489122012-04-29 18:21:10 -0400531}
532
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400533#define read_extent_tree_block(inode, pblk, depth, flags) \
534 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
535 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400536
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400537/*
538 * This function is called to cache a file's extent information in the
539 * extent status tree
540 */
541int ext4_ext_precache(struct inode *inode)
542{
543 struct ext4_inode_info *ei = EXT4_I(inode);
544 struct ext4_ext_path *path = NULL;
545 struct buffer_head *bh;
546 int i = 0, depth, ret = 0;
547
548 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
549 return 0; /* not an extent-mapped inode */
550
551 down_read(&ei->i_data_sem);
552 depth = ext_depth(inode);
553
Ritesh Harjani2f424a52020-02-28 14:56:55 +0530554 /* Don't cache anything if there are no external extent blocks */
555 if (!depth) {
556 up_read(&ei->i_data_sem);
557 return ret;
558 }
559
Kees Cook6396bb22018-06-12 14:03:40 -0700560 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400561 GFP_NOFS);
562 if (path == NULL) {
563 up_read(&ei->i_data_sem);
564 return -ENOMEM;
565 }
566
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400567 path[0].p_hdr = ext_inode_hdr(inode);
568 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
569 if (ret)
570 goto out;
571 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
572 while (i >= 0) {
573 /*
574 * If this is a leaf block or we've reached the end of
575 * the index block, go up
576 */
577 if ((i == depth) ||
578 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
579 brelse(path[i].p_bh);
580 path[i].p_bh = NULL;
581 i--;
582 continue;
583 }
584 bh = read_extent_tree_block(inode,
585 ext4_idx_pblock(path[i].p_idx++),
586 depth - i - 1,
587 EXT4_EX_FORCE_CACHE);
588 if (IS_ERR(bh)) {
589 ret = PTR_ERR(bh);
590 break;
591 }
592 i++;
593 path[i].p_bh = bh;
594 path[i].p_hdr = ext_block_hdr(bh);
595 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
596 }
597 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
598out:
599 up_read(&ei->i_data_sem);
600 ext4_ext_drop_refs(path);
601 kfree(path);
602 return ret;
603}
604
Alex Tomasa86c6182006-10-11 01:21:03 -0700605#ifdef EXT_DEBUG
606static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
607{
608 int k, l = path->p_depth;
609
610 ext_debug("path:");
611 for (k = 0; k <= l; k++, path++) {
612 if (path->p_idx) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600613 ext_debug(" %d->%llu",
614 le32_to_cpu(path->p_idx->ei_block),
615 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700616 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400617 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700618 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400619 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400620 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400621 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700622 } else
623 ext_debug(" []");
624 }
625 ext_debug("\n");
626}
627
628static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
629{
630 int depth = ext_depth(inode);
631 struct ext4_extent_header *eh;
632 struct ext4_extent *ex;
633 int i;
634
635 if (!path)
636 return;
637
638 eh = path[depth].p_hdr;
639 ex = EXT_FIRST_EXTENT(eh);
640
Mingming553f9002009-09-18 13:34:55 -0400641 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
642
Alex Tomasa86c6182006-10-11 01:21:03 -0700643 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400644 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400645 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400646 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700647 }
648 ext_debug("\n");
649}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400650
651static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
652 ext4_fsblk_t newblock, int level)
653{
654 int depth = ext_depth(inode);
655 struct ext4_extent *ex;
656
657 if (depth != level) {
658 struct ext4_extent_idx *idx;
659 idx = path[level].p_idx;
660 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
661 ext_debug("%d: move %d:%llu in new index %llu\n", level,
662 le32_to_cpu(idx->ei_block),
663 ext4_idx_pblock(idx),
664 newblock);
665 idx++;
666 }
667
668 return;
669 }
670
671 ex = path[depth].p_ext;
672 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
673 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
674 le32_to_cpu(ex->ee_block),
675 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400676 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400677 ext4_ext_get_actual_len(ex),
678 newblock);
679 ex++;
680 }
681}
682
Alex Tomasa86c6182006-10-11 01:21:03 -0700683#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400684#define ext4_ext_show_path(inode, path)
685#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400686#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700687#endif
688
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500689void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700690{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400691 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700692
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400693 if (!path)
694 return;
695 depth = path->p_depth;
Eric Biggersde745482019-12-31 12:04:44 -0600696 for (i = 0; i <= depth; i++, path++) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700697 if (path->p_bh) {
698 brelse(path->p_bh);
699 path->p_bh = NULL;
700 }
Eric Biggersde745482019-12-31 12:04:44 -0600701 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700702}
703
704/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700705 * ext4_ext_binsearch_idx:
706 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400707 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700708 */
709static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500710ext4_ext_binsearch_idx(struct inode *inode,
711 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700712{
713 struct ext4_extent_header *eh = path->p_hdr;
714 struct ext4_extent_idx *r, *l, *m;
715
Alex Tomasa86c6182006-10-11 01:21:03 -0700716
Eric Sandeenbba90742008-01-28 23:58:27 -0500717 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700718
719 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400720 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700721 while (l <= r) {
722 m = l + (r - l) / 2;
723 if (block < le32_to_cpu(m->ei_block))
724 r = m - 1;
725 else
726 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400727 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
728 m, le32_to_cpu(m->ei_block),
729 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700730 }
731
732 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400733 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400734 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700735
736#ifdef CHECK_BINSEARCH
737 {
738 struct ext4_extent_idx *chix, *ix;
739 int k;
740
741 chix = ix = EXT_FIRST_INDEX(eh);
742 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600743 if (k != 0 && le32_to_cpu(ix->ei_block) <=
744 le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400745 printk(KERN_DEBUG "k=%d, ix=0x%p, "
746 "first=0x%p\n", k,
747 ix, EXT_FIRST_INDEX(eh));
748 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700749 le32_to_cpu(ix->ei_block),
750 le32_to_cpu(ix[-1].ei_block));
751 }
752 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400753 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 if (block < le32_to_cpu(ix->ei_block))
755 break;
756 chix = ix;
757 }
758 BUG_ON(chix != path->p_idx);
759 }
760#endif
761
762}
763
764/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700765 * ext4_ext_binsearch:
766 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400767 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700768 */
769static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500770ext4_ext_binsearch(struct inode *inode,
771 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700772{
773 struct ext4_extent_header *eh = path->p_hdr;
774 struct ext4_extent *r, *l, *m;
775
Alex Tomasa86c6182006-10-11 01:21:03 -0700776 if (eh->eh_entries == 0) {
777 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700778 * this leaf is empty:
779 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700780 */
781 return;
782 }
783
Eric Sandeenbba90742008-01-28 23:58:27 -0500784 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700785
786 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400787 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700788
789 while (l <= r) {
790 m = l + (r - l) / 2;
791 if (block < le32_to_cpu(m->ee_block))
792 r = m - 1;
793 else
794 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400795 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
796 m, le32_to_cpu(m->ee_block),
797 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700798 }
799
800 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400801 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400802 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400803 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400804 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400805 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700806
807#ifdef CHECK_BINSEARCH
808 {
809 struct ext4_extent *chex, *ex;
810 int k;
811
812 chex = ex = EXT_FIRST_EXTENT(eh);
813 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
814 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400815 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700816 if (block < le32_to_cpu(ex->ee_block))
817 break;
818 chex = ex;
819 }
820 BUG_ON(chex != path->p_ext);
821 }
822#endif
823
824}
825
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -0700826void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700827{
828 struct ext4_extent_header *eh;
829
830 eh = ext_inode_hdr(inode);
831 eh->eh_depth = 0;
832 eh->eh_entries = 0;
833 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400834 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700836}
837
838struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400839ext4_find_extent(struct inode *inode, ext4_lblk_t block,
840 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700841{
842 struct ext4_extent_header *eh;
843 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400844 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
845 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500846 int ret;
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700847 gfp_t gfp_flags = GFP_NOFS;
848
849 if (flags & EXT4_EX_NOFAIL)
850 gfp_flags |= __GFP_NOFAIL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700851
852 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400853 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400854 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
855 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
856 depth);
857 ret = -EFSCORRUPTED;
858 goto err;
859 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700860
Theodore Ts'o10809df82014-09-01 14:40:09 -0400861 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400862 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400863 if (depth > path[0].p_maxdepth) {
864 kfree(path);
865 *orig_path = path = NULL;
866 }
867 }
868 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400869 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700870 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700871 gfp_flags);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400872 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400874 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700875 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700876 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400877 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700878
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400879 i = depth;
Dmitry Monakhov40686642019-11-06 12:25:02 +0000880 if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
881 ext4_cache_extents(inode, eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700882 /* walk through the tree */
883 while (i) {
884 ext_debug("depth %d: num %d, max %d\n",
885 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400886
Alex Tomasa86c6182006-10-11 01:21:03 -0700887 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400888 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 path[ppos].p_depth = i;
890 path[ppos].p_ext = NULL;
891
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400892 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
893 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530894 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400895 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700896 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500897 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400898
Alex Tomasa86c6182006-10-11 01:21:03 -0700899 eh = ext_block_hdr(bh);
900 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 path[ppos].p_bh = bh;
902 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700903 }
904
905 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 path[ppos].p_ext = NULL;
907 path[ppos].p_idx = NULL;
908
Alex Tomasa86c6182006-10-11 01:21:03 -0700909 /* find extent */
910 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400911 /* if not an empty leaf */
912 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400913 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700914
915 ext4_ext_show_path(inode, path);
916
917 return path;
918
919err:
920 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400921 kfree(path);
922 if (orig_path)
923 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500924 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700925}
926
927/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700928 * ext4_ext_insert_index:
929 * insert new index [@logical;@ptr] into the block at @curp;
930 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700931 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400932static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
933 struct ext4_ext_path *curp,
934 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700935{
936 struct ext4_extent_idx *ix;
937 int len, err;
938
Avantika Mathur7e028972006-12-06 20:41:33 -0800939 err = ext4_ext_get_access(handle, inode, curp);
940 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700941 return err;
942
Frank Mayhar273df552010-03-02 11:46:09 -0500943 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
944 EXT4_ERROR_INODE(inode,
945 "logical %d == ei_block %d!",
946 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400947 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500948 }
Robin Dongd4620312011-07-17 23:43:42 -0400949
950 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
951 >= le16_to_cpu(curp->p_hdr->eh_max))) {
952 EXT4_ERROR_INODE(inode,
953 "eh_entries %d >= eh_max %d!",
954 le16_to_cpu(curp->p_hdr->eh_entries),
955 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400956 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400957 }
958
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
960 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400961 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 ix = curp->p_idx + 1;
963 } else {
964 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400965 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700966 ix = curp->p_idx;
967 }
968
Eric Gouriou80e675f2011-10-27 11:52:18 -0400969 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
970 BUG_ON(len < 0);
971 if (len > 0) {
972 ext_debug("insert new index %d: "
973 "move %d indices from 0x%p to 0x%p\n",
974 logical, len, ix, ix + 1);
975 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
976 }
977
Tao Maf472e022011-10-17 10:13:46 -0400978 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
979 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400980 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -0400981 }
982
Alex Tomasa86c6182006-10-11 01:21:03 -0700983 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700984 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400985 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700986
Frank Mayhar273df552010-03-02 11:46:09 -0500987 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
988 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400989 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500990 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700991
992 err = ext4_ext_dirty(handle, inode, curp);
993 ext4_std_error(inode->i_sb, err);
994
995 return err;
996}
997
998/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700999 * ext4_ext_split:
1000 * inserts new subtree into the path, using free index entry
1001 * at depth @at:
1002 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1003 * - makes decision where to split
1004 * - moves remaining extents and index entries (right to the split point)
1005 * into the newly allocated blocks
1006 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001007 */
1008static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001009 unsigned int flags,
1010 struct ext4_ext_path *path,
1011 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001012{
1013 struct buffer_head *bh = NULL;
1014 int depth = ext_depth(inode);
1015 struct ext4_extent_header *neh;
1016 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001017 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001018 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001019 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001020 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001021 gfp_t gfp_flags = GFP_NOFS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001022 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001023 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001024
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001025 if (flags & EXT4_EX_NOFAIL)
1026 gfp_flags |= __GFP_NOFAIL;
1027
Alex Tomasa86c6182006-10-11 01:21:03 -07001028 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001029 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001030
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001031 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001032 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001033 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1034 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001035 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001036 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001037 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1038 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001039 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001041 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 } else {
1043 border = newext->ee_block;
1044 ext_debug("leaf will be added."
1045 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001046 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001047 }
1048
1049 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001050 * If error occurs, then we break processing
1051 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001053 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001054 */
1055
1056 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001057 * Get array to track all allocated blocks.
1058 * We need this to handle errors and free blocks
1059 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001061 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001062 if (!ablocks)
1063 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001064
1065 /* allocate all needed blocks */
1066 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1067 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001068 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001069 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001070 if (newblock == 0)
1071 goto cleanup;
1072 ablocks[a] = newblock;
1073 }
1074
1075 /* initialize new leaf */
1076 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001077 if (unlikely(newblock == 0)) {
1078 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001079 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001080 goto cleanup;
1081 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001082 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001083 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001084 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001085 goto cleanup;
1086 }
1087 lock_buffer(bh);
1088
Avantika Mathur7e028972006-12-06 20:41:33 -08001089 err = ext4_journal_get_create_access(handle, bh);
1090 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001091 goto cleanup;
1092
1093 neh = ext_block_hdr(bh);
1094 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001095 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001096 neh->eh_magic = EXT4_EXT_MAGIC;
1097 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001098
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001099 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001100 if (unlikely(path[depth].p_hdr->eh_entries !=
1101 path[depth].p_hdr->eh_max)) {
1102 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1103 path[depth].p_hdr->eh_entries,
1104 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001105 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001106 goto cleanup;
1107 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001108 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001109 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1110 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001111 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001112 struct ext4_extent *ex;
1113 ex = EXT_FIRST_EXTENT(neh);
1114 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001115 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001116 }
1117
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001118 /* zero out unused area in the extent block */
1119 ext_size = sizeof(struct ext4_extent_header) +
1120 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1121 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001122 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001123 set_buffer_uptodate(bh);
1124 unlock_buffer(bh);
1125
Frank Mayhar03901312009-01-07 00:06:22 -05001126 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001127 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001128 goto cleanup;
1129 brelse(bh);
1130 bh = NULL;
1131
1132 /* correct old leaf */
1133 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001134 err = ext4_ext_get_access(handle, inode, path + depth);
1135 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001136 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001137 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001138 err = ext4_ext_dirty(handle, inode, path + depth);
1139 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001140 goto cleanup;
1141
1142 }
1143
1144 /* create intermediate indexes */
1145 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001146 if (unlikely(k < 0)) {
1147 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001148 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001149 goto cleanup;
1150 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001151 if (k)
1152 ext_debug("create %d intermediate indices\n", k);
1153 /* insert new index into current index block */
1154 /* current depth stored in i var */
1155 i = depth - 1;
1156 while (k--) {
1157 oldblock = newblock;
1158 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001159 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001160 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001161 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001162 goto cleanup;
1163 }
1164 lock_buffer(bh);
1165
Avantika Mathur7e028972006-12-06 20:41:33 -08001166 err = ext4_journal_get_create_access(handle, bh);
1167 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);
1175 fidx = EXT_FIRST_INDEX(neh);
1176 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001177 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001178
Eric Sandeenbba90742008-01-28 23:58:27 -05001179 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1180 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001181
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001182 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001183 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1184 EXT_LAST_INDEX(path[i].p_hdr))) {
1185 EXT4_ERROR_INODE(inode,
1186 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1187 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001188 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001189 goto cleanup;
1190 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001191 /* start copy indexes */
1192 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1193 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1194 EXT_MAX_INDEX(path[i].p_hdr));
1195 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001196 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001197 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001198 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001199 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001200 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001201 /* zero out unused area in the extent block */
1202 ext_size = sizeof(struct ext4_extent_header) +
1203 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1204 memset(bh->b_data + ext_size, 0,
1205 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001206 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001207 set_buffer_uptodate(bh);
1208 unlock_buffer(bh);
1209
Frank Mayhar03901312009-01-07 00:06:22 -05001210 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001211 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001212 goto cleanup;
1213 brelse(bh);
1214 bh = NULL;
1215
1216 /* correct old index */
1217 if (m) {
1218 err = ext4_ext_get_access(handle, inode, path + i);
1219 if (err)
1220 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001221 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001222 err = ext4_ext_dirty(handle, inode, path + i);
1223 if (err)
1224 goto cleanup;
1225 }
1226
1227 i--;
1228 }
1229
1230 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001231 err = ext4_ext_insert_index(handle, inode, path + at,
1232 le32_to_cpu(border), newblock);
1233
1234cleanup:
1235 if (bh) {
1236 if (buffer_locked(bh))
1237 unlock_buffer(bh);
1238 brelse(bh);
1239 }
1240
1241 if (err) {
1242 /* free all allocated blocks in error case */
1243 for (i = 0; i < depth; i++) {
1244 if (!ablocks[i])
1245 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001246 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001247 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001248 }
1249 }
1250 kfree(ablocks);
1251
1252 return err;
1253}
1254
1255/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001256 * ext4_ext_grow_indepth:
1257 * implements tree growing procedure:
1258 * - allocates new block
1259 * - moves top-level data (index block or leaf) into the new block
1260 * - initializes new top-level, creating index that points to the
1261 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001262 */
1263static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001264 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001265{
Alex Tomasa86c6182006-10-11 01:21:03 -07001266 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001267 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001268 ext4_fsblk_t newblock, goal = 0;
1269 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001270 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001271 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001272
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001273 /* Try to prepend new index to old one */
1274 if (ext_depth(inode))
1275 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1276 if (goal > le32_to_cpu(es->s_first_data_block)) {
1277 flags |= EXT4_MB_HINT_TRY_GOAL;
1278 goal--;
1279 } else
1280 goal = ext4_inode_to_goal_block(inode);
1281 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1282 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001283 if (newblock == 0)
1284 return err;
1285
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001286 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001287 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001288 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001289 lock_buffer(bh);
1290
Avantika Mathur7e028972006-12-06 20:41:33 -08001291 err = ext4_journal_get_create_access(handle, bh);
1292 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001293 unlock_buffer(bh);
1294 goto out;
1295 }
1296
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001297 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001298 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001299 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1300 /* zero out unused area in the extent block */
1301 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001302
1303 /* set size of new block */
1304 neh = ext_block_hdr(bh);
1305 /* old root could have indexes or leaves
1306 * so calculate e_max right way */
1307 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001308 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001309 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001310 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001311 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001312 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001313 set_buffer_uptodate(bh);
1314 unlock_buffer(bh);
1315
Frank Mayhar03901312009-01-07 00:06:22 -05001316 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001317 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001318 goto out;
1319
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001320 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001321 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001322 neh->eh_entries = cpu_to_le16(1);
1323 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1324 if (neh->eh_depth == 0) {
1325 /* Root extent block becomes index block */
1326 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1327 EXT_FIRST_INDEX(neh)->ei_block =
1328 EXT_FIRST_EXTENT(neh)->ee_block;
1329 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001330 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001331 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001332 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001333 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001334
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001335 le16_add_cpu(&neh->eh_depth, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07001336 err = ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001337out:
1338 brelse(bh);
1339
1340 return err;
1341}
1342
1343/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001344 * ext4_ext_create_new_leaf:
1345 * finds empty index and adds new leaf.
1346 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001347 */
1348static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001349 unsigned int mb_flags,
1350 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001351 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001352 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001353{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001354 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001355 struct ext4_ext_path *curp;
1356 int depth, i, err = 0;
1357
1358repeat:
1359 i = depth = ext_depth(inode);
1360
1361 /* walk up to the tree and look for free index entry */
1362 curp = path + depth;
1363 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1364 i--;
1365 curp--;
1366 }
1367
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001368 /* we use already allocated block for index block,
1369 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001370 if (EXT_HAS_FREE_INDEX(curp)) {
1371 /* if we found index with free entry, then use that
1372 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001373 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001374 if (err)
1375 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001376
1377 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001378 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001379 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001380 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001381 if (IS_ERR(path))
1382 err = PTR_ERR(path);
1383 } else {
1384 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001385 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001386 if (err)
1387 goto out;
1388
1389 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001390 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001391 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001392 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001393 if (IS_ERR(path)) {
1394 err = PTR_ERR(path);
1395 goto out;
1396 }
1397
1398 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001399 * only first (depth 0 -> 1) produces free space;
1400 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001401 */
1402 depth = ext_depth(inode);
1403 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001404 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001405 goto repeat;
1406 }
1407 }
1408
1409out:
1410 return err;
1411}
1412
1413/*
Alex Tomas1988b512008-01-28 23:58:27 -05001414 * search the closest allocated block to the left for *logical
1415 * and returns it at @logical + it's physical address at @phys
1416 * if *logical is the smallest allocated block, the function
1417 * returns 0 at @phys
1418 * return value contains 0 (success) or error code
1419 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001420static int ext4_ext_search_left(struct inode *inode,
1421 struct ext4_ext_path *path,
1422 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001423{
1424 struct ext4_extent_idx *ix;
1425 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001426 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001427
Frank Mayhar273df552010-03-02 11:46:09 -05001428 if (unlikely(path == NULL)) {
1429 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001430 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001431 }
Alex Tomas1988b512008-01-28 23:58:27 -05001432 depth = path->p_depth;
1433 *phys = 0;
1434
1435 if (depth == 0 && path->p_ext == NULL)
1436 return 0;
1437
1438 /* usually extent in the path covers blocks smaller
1439 * then *logical, but it can be that extent is the
1440 * first one in the file */
1441
1442 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001443 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001444 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001445 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1446 EXT4_ERROR_INODE(inode,
1447 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1448 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001449 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001450 }
Alex Tomas1988b512008-01-28 23:58:27 -05001451 while (--depth >= 0) {
1452 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001453 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1454 EXT4_ERROR_INODE(inode,
1455 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001456 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001457 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001458 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001459 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001460 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001461 }
Alex Tomas1988b512008-01-28 23:58:27 -05001462 }
1463 return 0;
1464 }
1465
Frank Mayhar273df552010-03-02 11:46:09 -05001466 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1467 EXT4_ERROR_INODE(inode,
1468 "logical %d < ee_block %d + ee_len %d!",
1469 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001470 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001471 }
Alex Tomas1988b512008-01-28 23:58:27 -05001472
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001473 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001474 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001475 return 0;
1476}
1477
1478/*
1479 * search the closest allocated block to the right for *logical
1480 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001481 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001482 * returns 0 at @phys
1483 * return value contains 0 (success) or error code
1484 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001485static int ext4_ext_search_right(struct inode *inode,
1486 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001487 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1488 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001489{
1490 struct buffer_head *bh = NULL;
1491 struct ext4_extent_header *eh;
1492 struct ext4_extent_idx *ix;
1493 struct ext4_extent *ex;
1494 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001495 int depth; /* Note, NOT eh_depth; depth from top of tree */
1496 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001497
Frank Mayhar273df552010-03-02 11:46:09 -05001498 if (unlikely(path == NULL)) {
1499 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001500 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001501 }
Alex Tomas1988b512008-01-28 23:58:27 -05001502 depth = path->p_depth;
1503 *phys = 0;
1504
1505 if (depth == 0 && path->p_ext == NULL)
1506 return 0;
1507
1508 /* usually extent in the path covers blocks smaller
1509 * then *logical, but it can be that extent is the
1510 * first one in the file */
1511
1512 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001513 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001514 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001515 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1516 EXT4_ERROR_INODE(inode,
1517 "first_extent(path[%d].p_hdr) != ex",
1518 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001519 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001520 }
Alex Tomas1988b512008-01-28 23:58:27 -05001521 while (--depth >= 0) {
1522 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001523 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1524 EXT4_ERROR_INODE(inode,
1525 "ix != EXT_FIRST_INDEX *logical %d!",
1526 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001527 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001528 }
Alex Tomas1988b512008-01-28 23:58:27 -05001529 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001530 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001531 }
1532
Frank Mayhar273df552010-03-02 11:46:09 -05001533 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1534 EXT4_ERROR_INODE(inode,
1535 "logical %d < ee_block %d + ee_len %d!",
1536 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001537 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001538 }
Alex Tomas1988b512008-01-28 23:58:27 -05001539
1540 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1541 /* next allocated block in this leaf */
1542 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001543 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001544 }
1545
1546 /* go up and search for index to the right */
1547 while (--depth >= 0) {
1548 ix = path[depth].p_idx;
1549 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001550 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001551 }
1552
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001553 /* we've gone up to the root and found no index to the right */
1554 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001555
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001556got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001557 /* we've found index to the right, let's
1558 * follow it and find the closest allocated
1559 * block to the right */
1560 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001561 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001562 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001563 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001564 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001565 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001566 if (IS_ERR(bh))
1567 return PTR_ERR(bh);
1568 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001569 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001570 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001571 put_bh(bh);
1572 }
1573
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001574 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001575 if (IS_ERR(bh))
1576 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001577 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001578 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001579found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001580 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001581 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001582 *ret_ex = ex;
1583 if (bh)
1584 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001585 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001586}
1587
1588/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001589 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001590 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001591 * NOTE: it considers block number from index entry as
1592 * allocated block. Thus, index entries have to be consistent
1593 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001594 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001595ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001596ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1597{
1598 int depth;
1599
1600 BUG_ON(path == NULL);
1601 depth = path->p_depth;
1602
1603 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001604 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001605
1606 while (depth >= 0) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001607 struct ext4_ext_path *p = &path[depth];
1608
Alex Tomasa86c6182006-10-11 01:21:03 -07001609 if (depth == path->p_depth) {
1610 /* leaf */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001611 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
1612 return le32_to_cpu(p->p_ext[1].ee_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001613 } else {
1614 /* index */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001615 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
1616 return le32_to_cpu(p->p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001617 }
1618 depth--;
1619 }
1620
Lukas Czernerf17722f2011-06-06 00:05:17 -04001621 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001622}
1623
1624/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001625 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001626 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001627 */
Robin Dong57187892011-07-23 21:49:07 -04001628static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001629{
1630 int depth;
1631
1632 BUG_ON(path == NULL);
1633 depth = path->p_depth;
1634
1635 /* zero-tree has no leaf blocks at all */
1636 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001637 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001638
1639 /* go to index block */
1640 depth--;
1641
1642 while (depth >= 0) {
1643 if (path[depth].p_idx !=
1644 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001645 return (ext4_lblk_t)
1646 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001647 depth--;
1648 }
1649
Lukas Czernerf17722f2011-06-06 00:05:17 -04001650 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001651}
1652
1653/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001654 * ext4_ext_correct_indexes:
1655 * if leaf gets modified and modified extent is first in the leaf,
1656 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001657 * TODO: do we need to correct tree in all cases?
1658 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001659static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001660 struct ext4_ext_path *path)
1661{
1662 struct ext4_extent_header *eh;
1663 int depth = ext_depth(inode);
1664 struct ext4_extent *ex;
1665 __le32 border;
1666 int k, err = 0;
1667
1668 eh = path[depth].p_hdr;
1669 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001670
1671 if (unlikely(ex == NULL || eh == NULL)) {
1672 EXT4_ERROR_INODE(inode,
1673 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001674 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001675 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001676
1677 if (depth == 0) {
1678 /* there is no tree at all */
1679 return 0;
1680 }
1681
1682 if (ex != EXT_FIRST_EXTENT(eh)) {
1683 /* we correct tree if first leaf got modified only */
1684 return 0;
1685 }
1686
1687 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001688 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001689 */
1690 k = depth - 1;
1691 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001692 err = ext4_ext_get_access(handle, inode, path + k);
1693 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 return err;
1695 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001696 err = ext4_ext_dirty(handle, inode, path + k);
1697 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001698 return err;
1699
1700 while (k--) {
1701 /* change all left-side indexes */
1702 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1703 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001704 err = ext4_ext_get_access(handle, inode, path + k);
1705 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001706 break;
1707 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001708 err = ext4_ext_dirty(handle, inode, path + k);
1709 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001710 break;
1711 }
1712
1713 return err;
1714}
1715
Eric Biggers43f81672019-12-31 12:04:40 -06001716static int ext4_can_extents_be_merged(struct inode *inode,
1717 struct ext4_extent *ex1,
1718 struct ext4_extent *ex2)
Alex Tomasa86c6182006-10-11 01:21:03 -07001719{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001720 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001721
Lukas Czerner556615d2014-04-20 23:45:47 -04001722 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001723 return 0;
1724
1725 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1726 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1727
1728 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001729 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001730 return 0;
1731
Eric Sandeenda0169b2013-11-04 09:58:26 -05001732 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001733 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001734
Lukas Czerner556615d2014-04-20 23:45:47 -04001735 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001736 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001737 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001738#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001739 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001740 return 0;
1741#endif
1742
Theodore Ts'obf89d162010-10-27 21:30:14 -04001743 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001744 return 1;
1745 return 0;
1746}
1747
1748/*
Amit Arora56055d32007-07-17 21:42:38 -04001749 * This function tries to merge the "ex" extent to the next extent in the tree.
1750 * It always tries to merge towards right. If you want to merge towards
1751 * left, pass "ex - 1" as argument instead of "ex".
1752 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1753 * 1 if they got merged.
1754 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001755static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001756 struct ext4_ext_path *path,
1757 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001758{
1759 struct ext4_extent_header *eh;
1760 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001761 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001762
1763 depth = ext_depth(inode);
1764 BUG_ON(path[depth].p_hdr == NULL);
1765 eh = path[depth].p_hdr;
1766
1767 while (ex < EXT_LAST_EXTENT(eh)) {
1768 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1769 break;
1770 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001771 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001772 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1773 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001774 if (unwritten)
1775 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001776
1777 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1778 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1779 * sizeof(struct ext4_extent);
1780 memmove(ex + 1, ex + 2, len);
1781 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001782 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001783 merge_done = 1;
1784 WARN_ON(eh->eh_entries == 0);
1785 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001786 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001787 }
1788
1789 return merge_done;
1790}
1791
1792/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001793 * This function does a very simple check to see if we can collapse
1794 * an extent tree with a single extent tree leaf block into the inode.
1795 */
1796static void ext4_ext_try_to_merge_up(handle_t *handle,
1797 struct inode *inode,
1798 struct ext4_ext_path *path)
1799{
1800 size_t s;
1801 unsigned max_root = ext4_ext_space_root(inode, 0);
1802 ext4_fsblk_t blk;
1803
1804 if ((path[0].p_depth != 1) ||
1805 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1806 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1807 return;
1808
1809 /*
1810 * We need to modify the block allocation bitmap and the block
1811 * group descriptor to release the extent tree block. If we
1812 * can't get the journal credits, give up.
1813 */
Jan Kara83448bd2019-11-05 17:44:29 +01001814 if (ext4_journal_extend(handle, 2,
1815 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001816 return;
1817
1818 /*
1819 * Copy the extent data up to the inode
1820 */
1821 blk = ext4_idx_pblock(path[0].p_idx);
1822 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1823 sizeof(struct ext4_extent_idx);
1824 s += sizeof(struct ext4_extent_header);
1825
Theodore Ts'o10809df82014-09-01 14:40:09 -04001826 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001827 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1828 path[0].p_depth = 0;
1829 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1830 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1831 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1832
1833 brelse(path[1].p_bh);
1834 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001835 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001836}
1837
1838/*
Eric Biggersadde81c2019-12-31 12:04:41 -06001839 * This function tries to merge the @ex extent to neighbours in the tree, then
1840 * tries to collapse the extent tree into the inode.
Yongqiang Yang197217a2011-05-03 11:45:29 -04001841 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001842static void ext4_ext_try_to_merge(handle_t *handle,
1843 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001844 struct ext4_ext_path *path,
Eric Biggersadde81c2019-12-31 12:04:41 -06001845 struct ext4_extent *ex)
1846{
Yongqiang Yang197217a2011-05-03 11:45:29 -04001847 struct ext4_extent_header *eh;
1848 unsigned int depth;
1849 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001850
1851 depth = ext_depth(inode);
1852 BUG_ON(path[depth].p_hdr == NULL);
1853 eh = path[depth].p_hdr;
1854
1855 if (ex > EXT_FIRST_EXTENT(eh))
1856 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1857
1858 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001859 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001860
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001861 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001862}
1863
1864/*
Amit Arora25d14f92007-05-24 13:04:13 -04001865 * check if a portion of the "newext" extent overlaps with an
1866 * existing extent.
1867 *
1868 * If there is an overlap discovered, it updates the length of the newext
1869 * such that there will be no overlap, and then returns 1.
1870 * If there is no overlap found, it returns 0.
1871 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001872static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1873 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001874 struct ext4_extent *newext,
1875 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001876{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001877 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001878 unsigned int depth, len1;
1879 unsigned int ret = 0;
1880
1881 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001882 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001883 depth = ext_depth(inode);
1884 if (!path[depth].p_ext)
1885 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001886 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001887
1888 /*
1889 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001890 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001891 */
1892 if (b2 < b1) {
1893 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001894 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001895 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001896 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001897 }
1898
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001899 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001900 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001901 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001902 newext->ee_len = cpu_to_le16(len1);
1903 ret = 1;
1904 }
1905
1906 /* check for overlap */
1907 if (b1 + len1 > b2) {
1908 newext->ee_len = cpu_to_le16(b2 - b1);
1909 ret = 1;
1910 }
1911out:
1912 return ret;
1913}
1914
1915/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001916 * ext4_ext_insert_extent:
1917 * tries to merge requsted extent into the existing extent or
1918 * inserts requested extent as new one into the tree,
1919 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001920 */
1921int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001922 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001923 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001924{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001925 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001926 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001927 struct ext4_extent *ex, *fex;
1928 struct ext4_extent *nearex; /* nearest extent */
1929 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001930 int depth, len, err;
1931 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001932 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001933
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001934 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1935 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001936 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1937 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001938 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001939 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001940 depth = ext_depth(inode);
1941 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001942 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001943 if (unlikely(path[depth].p_hdr == NULL)) {
1944 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001945 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001946 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001947
1948 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001949 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001950
1951 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001952 * Try to see whether we should rather test the extent on
1953 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001954 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001955 * left, or on the right from the searched position. This
1956 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001957 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001958 if (ex < EXT_LAST_EXTENT(eh) &&
1959 (le32_to_cpu(ex->ee_block) +
1960 ext4_ext_get_actual_len(ex) <
1961 le32_to_cpu(newext->ee_block))) {
1962 ex += 1;
1963 goto prepend;
1964 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1965 (le32_to_cpu(newext->ee_block) +
1966 ext4_ext_get_actual_len(newext) <
1967 le32_to_cpu(ex->ee_block)))
1968 ex -= 1;
1969
1970 /* Try to append newex to the ex */
1971 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1972 ext_debug("append [%d]%d block to %u:[%d]%d"
1973 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001974 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001975 ext4_ext_get_actual_len(newext),
1976 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001977 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001978 ext4_ext_get_actual_len(ex),
1979 ext4_ext_pblock(ex));
1980 err = ext4_ext_get_access(handle, inode,
1981 path + depth);
1982 if (err)
1983 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001984 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001985 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001986 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001987 if (unwritten)
1988 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001989 eh = path[depth].p_hdr;
1990 nearex = ex;
1991 goto merge;
1992 }
1993
1994prepend:
1995 /* Try to prepend newex to the ex */
1996 if (ext4_can_extents_be_merged(inode, newext, ex)) {
1997 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
1998 "(from %llu)\n",
1999 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002000 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002001 ext4_ext_get_actual_len(newext),
2002 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002003 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002004 ext4_ext_get_actual_len(ex),
2005 ext4_ext_pblock(ex));
2006 err = ext4_ext_get_access(handle, inode,
2007 path + depth);
2008 if (err)
2009 return err;
2010
Lukas Czerner556615d2014-04-20 23:45:47 -04002011 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002012 ex->ee_block = newext->ee_block;
2013 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2014 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2015 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002016 if (unwritten)
2017 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002018 eh = path[depth].p_hdr;
2019 nearex = ex;
2020 goto merge;
2021 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002022 }
2023
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 depth = ext_depth(inode);
2025 eh = path[depth].p_hdr;
2026 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2027 goto has_space;
2028
2029 /* probably next leaf has space for us? */
2030 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002031 next = EXT_MAX_BLOCKS;
2032 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002033 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002034 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002035 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002036 BUG_ON(npath != NULL);
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002037 npath = ext4_find_extent(inode, next, NULL, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002038 if (IS_ERR(npath))
2039 return PTR_ERR(npath);
2040 BUG_ON(npath->p_depth != path->p_depth);
2041 eh = npath[depth].p_hdr;
2042 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002043 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002044 le16_to_cpu(eh->eh_entries));
2045 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002046 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002047 }
2048 ext_debug("next leaf has no free space(%d,%d)\n",
2049 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2050 }
2051
2052 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002053 * There is no free space in the found leaf.
2054 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002055 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002056 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002057 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002058 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002059 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002060 if (err)
2061 goto cleanup;
2062 depth = ext_depth(inode);
2063 eh = path[depth].p_hdr;
2064
2065has_space:
2066 nearex = path[depth].p_ext;
2067
Avantika Mathur7e028972006-12-06 20:41:33 -08002068 err = ext4_ext_get_access(handle, inode, path + depth);
2069 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002070 goto cleanup;
2071
2072 if (!nearex) {
2073 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002074 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002075 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002076 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002077 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002078 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002079 nearex = EXT_FIRST_EXTENT(eh);
2080 } else {
2081 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002082 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002083 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002084 ext_debug("insert %u:%llu:[%d]%d before: "
2085 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002086 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002087 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002088 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002089 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002090 nearex);
2091 nearex++;
2092 } else {
2093 /* Insert before */
2094 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002095 ext_debug("insert %u:%llu:[%d]%d after: "
2096 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002097 le32_to_cpu(newext->ee_block),
2098 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002099 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002100 ext4_ext_get_actual_len(newext),
2101 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002102 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002103 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2104 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002105 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002106 "move %d extents from 0x%p to 0x%p\n",
2107 le32_to_cpu(newext->ee_block),
2108 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002109 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002110 ext4_ext_get_actual_len(newext),
2111 len, nearex, nearex + 1);
2112 memmove(nearex + 1, nearex,
2113 len * sizeof(struct ext4_extent));
2114 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002115 }
2116
Marcin Slusarze8546d02008-04-17 10:38:59 -04002117 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002118 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002119 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002120 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002121 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002122
2123merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002124 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002125 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002126 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002127
Alex Tomasa86c6182006-10-11 01:21:03 -07002128
2129 /* time to correct all indexes above */
2130 err = ext4_ext_correct_indexes(handle, inode, path);
2131 if (err)
2132 goto cleanup;
2133
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002134 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002135
2136cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002137 ext4_ext_drop_refs(npath);
2138 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 return err;
2140}
2141
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002142static int ext4_fill_es_cache_info(struct inode *inode,
2143 ext4_lblk_t block, ext4_lblk_t num,
2144 struct fiemap_extent_info *fieinfo)
2145{
2146 ext4_lblk_t next, end = block + num - 1;
2147 struct extent_status es;
2148 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2149 unsigned int flags;
2150 int err;
2151
2152 while (block <= end) {
2153 next = 0;
2154 flags = 0;
2155 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2156 break;
2157 if (ext4_es_is_unwritten(&es))
2158 flags |= FIEMAP_EXTENT_UNWRITTEN;
2159 if (ext4_es_is_delayed(&es))
2160 flags |= (FIEMAP_EXTENT_DELALLOC |
2161 FIEMAP_EXTENT_UNKNOWN);
2162 if (ext4_es_is_hole(&es))
2163 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2164 if (next == 0)
2165 flags |= FIEMAP_EXTENT_LAST;
2166 if (flags & (FIEMAP_EXTENT_DELALLOC|
2167 EXT4_FIEMAP_EXTENT_HOLE))
2168 es.es_pblk = 0;
2169 else
2170 es.es_pblk = ext4_es_pblock(&es);
2171 err = fiemap_fill_next_extent(fieinfo,
2172 (__u64)es.es_lblk << blksize_bits,
2173 (__u64)es.es_pblk << blksize_bits,
2174 (__u64)es.es_len << blksize_bits,
2175 flags);
2176 if (next == 0)
2177 break;
2178 block = next;
2179 if (err < 0)
2180 return err;
2181 if (err == 1)
2182 return 0;
2183 }
2184 return 0;
2185}
2186
2187
Alex Tomasa86c6182006-10-11 01:21:03 -07002188/*
Jan Kara140a5252016-03-09 22:46:57 -05002189 * ext4_ext_determine_hole - determine hole around given block
2190 * @inode: inode we lookup in
2191 * @path: path in extent tree to @lblk
2192 * @lblk: pointer to logical block around which we want to determine hole
2193 *
2194 * Determine hole length (and start if easily possible) around given logical
2195 * block. We don't try too hard to find the beginning of the hole but @path
2196 * actually points to extent before @lblk, we provide it.
2197 *
2198 * The function returns the length of a hole starting at @lblk. We update @lblk
2199 * to the beginning of the hole if we managed to find it.
2200 */
2201static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2202 struct ext4_ext_path *path,
2203 ext4_lblk_t *lblk)
2204{
2205 int depth = ext_depth(inode);
2206 struct ext4_extent *ex;
2207 ext4_lblk_t len;
2208
2209 ex = path[depth].p_ext;
2210 if (ex == NULL) {
2211 /* there is no extent yet, so gap is [0;-] */
2212 *lblk = 0;
2213 len = EXT_MAX_BLOCKS;
2214 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2215 len = le32_to_cpu(ex->ee_block) - *lblk;
2216 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2217 + ext4_ext_get_actual_len(ex)) {
2218 ext4_lblk_t next;
2219
2220 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2221 next = ext4_ext_next_allocated_block(path);
2222 BUG_ON(next == *lblk);
2223 len = next - *lblk;
2224 } else {
2225 BUG();
2226 }
2227 return len;
2228}
2229
2230/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002231 * ext4_ext_put_gap_in_cache:
2232 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002233 * and cache this gap
2234 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002235static void
Jan Kara140a5252016-03-09 22:46:57 -05002236ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2237 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002238{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002239 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002240
Eric Whitneyad431022018-10-01 14:10:39 -04002241 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2242 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002243 if (es.es_len) {
2244 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002245 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002246 return;
Jan Kara140a5252016-03-09 22:46:57 -05002247 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002248 }
Jan Kara140a5252016-03-09 22:46:57 -05002249 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2250 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2251 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002252}
2253
2254/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002255 * ext4_ext_rm_idx:
2256 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002257 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002258static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002259 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002260{
Alex Tomasa86c6182006-10-11 01:21:03 -07002261 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002262 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002263
2264 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002265 depth--;
2266 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002267 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002268 if (unlikely(path->p_hdr->eh_entries == 0)) {
2269 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002270 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002271 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002272 err = ext4_ext_get_access(handle, inode, path);
2273 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002274 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002275
2276 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2277 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2278 len *= sizeof(struct ext4_extent_idx);
2279 memmove(path->p_idx, path->p_idx + 1, len);
2280 }
2281
Marcin Slusarze8546d02008-04-17 10:38:59 -04002282 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002283 err = ext4_ext_dirty(handle, inode, path);
2284 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002285 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002286 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002287 trace_ext4_ext_rm_idx(inode, leaf);
2288
Peter Huewe7dc57612011-02-21 21:01:42 -05002289 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002290 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002291
2292 while (--depth >= 0) {
2293 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2294 break;
2295 path--;
2296 err = ext4_ext_get_access(handle, inode, path);
2297 if (err)
2298 break;
2299 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2300 err = ext4_ext_dirty(handle, inode, path);
2301 if (err)
2302 break;
2303 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002304 return err;
2305}
2306
2307/*
Mingming Caoee12b632008-08-19 22:16:05 -04002308 * ext4_ext_calc_credits_for_single_extent:
2309 * This routine returns max. credits that needed to insert an extent
2310 * to the extent tree.
2311 * When pass the actual path, the caller should calculate credits
2312 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002313 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002314int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002315 struct ext4_ext_path *path)
2316{
Alex Tomasa86c6182006-10-11 01:21:03 -07002317 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002318 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002319 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002320
Alex Tomasa86c6182006-10-11 01:21:03 -07002321 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002322 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002323 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2324
2325 /*
2326 * There are some space in the leaf tree, no
2327 * need to account for leaf block credit
2328 *
2329 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002330 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002331 * accounted.
2332 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002333 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002334 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002335 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002336 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002337 }
2338
Mingming Cao525f4ed2008-08-19 22:15:58 -04002339 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002340}
Alex Tomasa86c6182006-10-11 01:21:03 -07002341
Mingming Caoee12b632008-08-19 22:16:05 -04002342/*
Jan Karafffb2732013-06-04 13:01:11 -04002343 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002344 *
Jan Karafffb2732013-06-04 13:01:11 -04002345 * If we add a single extent, then in the worse case, each tree level
2346 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002347 *
Jan Karafffb2732013-06-04 13:01:11 -04002348 * If more extents are inserted, they could cause the whole tree split more
2349 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002350 */
Jan Karafffb2732013-06-04 13:01:11 -04002351int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002352{
2353 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002354 int depth;
2355
2356 /* If we are converting the inline data, only one is needed here. */
2357 if (ext4_has_inline_data(inode))
2358 return 1;
2359
2360 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002361
Jan Karafffb2732013-06-04 13:01:11 -04002362 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002363 index = depth * 2;
2364 else
2365 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002366
Mingming Caoee12b632008-08-19 22:16:05 -04002367 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002368}
2369
Theodore Ts'o981250c2013-06-12 11:48:29 -04002370static inline int get_default_free_blocks_flags(struct inode *inode)
2371{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002372 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2373 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002374 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2375 else if (ext4_should_journal_data(inode))
2376 return EXT4_FREE_BLOCKS_FORGET;
2377 return 0;
2378}
2379
Eric Whitney9fe67142018-10-01 14:25:08 -04002380/*
2381 * ext4_rereserve_cluster - increment the reserved cluster count when
2382 * freeing a cluster with a pending reservation
2383 *
2384 * @inode - file containing the cluster
2385 * @lblk - logical block in cluster to be reserved
2386 *
2387 * Increments the reserved cluster count and adjusts quota in a bigalloc
2388 * file system when freeing a partial cluster containing at least one
2389 * delayed and unwritten block. A partial cluster meeting that
2390 * requirement will have a pending reservation. If so, the
2391 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2392 * defer reserved and allocated space accounting to a subsequent call
2393 * to this function.
2394 */
2395static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2396{
2397 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2398 struct ext4_inode_info *ei = EXT4_I(inode);
2399
2400 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2401
2402 spin_lock(&ei->i_block_reservation_lock);
2403 ei->i_reserved_data_blocks++;
2404 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2405 spin_unlock(&ei->i_block_reservation_lock);
2406
2407 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2408 ext4_remove_pending(inode, lblk);
2409}
2410
Alex Tomasa86c6182006-10-11 01:21:03 -07002411static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002412 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002413 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002414 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002415{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002416 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002417 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002418 ext4_fsblk_t last_pblk, pblk;
2419 ext4_lblk_t num;
2420 int flags;
2421
2422 /* only extent tail removal is allowed */
2423 if (from < le32_to_cpu(ex->ee_block) ||
2424 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2425 ext4_error(sbi->s_sb,
2426 "strange request: removal(2) %u-%u from %u:%u",
2427 from, to, le32_to_cpu(ex->ee_block), ee_len);
2428 return 0;
2429 }
2430
2431#ifdef EXTENTS_STATS
2432 spin_lock(&sbi->s_ext_stats_lock);
2433 sbi->s_ext_blocks += ee_len;
2434 sbi->s_ext_extents++;
2435 if (ee_len < sbi->s_ext_min)
2436 sbi->s_ext_min = ee_len;
2437 if (ee_len > sbi->s_ext_max)
2438 sbi->s_ext_max = ee_len;
2439 if (ext_depth(inode) > sbi->s_depth_max)
2440 sbi->s_depth_max = ext_depth(inode);
2441 spin_unlock(&sbi->s_ext_stats_lock);
2442#endif
2443
2444 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2445
2446 /*
2447 * if we have a partial cluster, and it's different from the
2448 * cluster of the last block in the extent, we free it
2449 */
2450 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2451
2452 if (partial->state != initial &&
2453 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2454 if (partial->state == tofree) {
2455 flags = get_default_free_blocks_flags(inode);
2456 if (ext4_is_pending(inode, partial->lblk))
2457 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2458 ext4_free_blocks(handle, inode, NULL,
2459 EXT4_C2B(sbi, partial->pclu),
2460 sbi->s_cluster_ratio, flags);
2461 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2462 ext4_rereserve_cluster(inode, partial->lblk);
2463 }
2464 partial->state = initial;
2465 }
2466
2467 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2468 pblk = ext4_ext_pblock(ex) + ee_len - num;
2469
2470 /*
2471 * We free the partial cluster at the end of the extent (if any),
2472 * unless the cluster is used by another extent (partial_cluster
2473 * state is nofree). If a partial cluster exists here, it must be
2474 * shared with the last block in the extent.
2475 */
2476 flags = get_default_free_blocks_flags(inode);
2477
2478 /* partial, left end cluster aligned, right end unaligned */
2479 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2480 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2481 (partial->state != nofree)) {
2482 if (ext4_is_pending(inode, to))
2483 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2484 ext4_free_blocks(handle, inode, NULL,
2485 EXT4_PBLK_CMASK(sbi, last_pblk),
2486 sbi->s_cluster_ratio, flags);
2487 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2488 ext4_rereserve_cluster(inode, to);
2489 partial->state = initial;
2490 flags = get_default_free_blocks_flags(inode);
2491 }
2492
2493 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002494
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002495 /*
2496 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002497 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002498 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002499 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002500 */
2501 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002502 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002503
Eric Whitney9fe67142018-10-01 14:25:08 -04002504 /* reset the partial cluster if we've freed past it */
2505 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2506 partial->state = initial;
2507
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002508 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002509 * If we've freed the entire extent but the beginning is not left
2510 * cluster aligned and is not marked as ineligible for freeing we
2511 * record the partial cluster at the beginning of the extent. It
2512 * wasn't freed by the preceding ext4_free_blocks() call, and we
2513 * need to look farther to the left to determine if it's to be freed
2514 * (not shared with another extent). Else, reset the partial
2515 * cluster - we're either done freeing or the beginning of the
2516 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002517 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002518 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2519 if (partial->state == initial) {
2520 partial->pclu = EXT4_B2C(sbi, pblk);
2521 partial->lblk = from;
2522 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002523 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002524 } else {
2525 partial->state = initial;
2526 }
2527
Alex Tomasa86c6182006-10-11 01:21:03 -07002528 return 0;
2529}
2530
Allison Hendersond583fb82011-05-25 07:41:43 -04002531/*
2532 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002533 * blocks appearing between "start" and "end". Both "start"
2534 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002535 *
2536 * @handle: The journal handle
2537 * @inode: The files inode
2538 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002539 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002540 * has been released from it. However, if this value is
2541 * negative, it's a cluster just to the right of the
2542 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002543 * @start: The first block to remove
2544 * @end: The last block to remove
2545 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002546static int
2547ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002548 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002549 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002550 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002551{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002552 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002553 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002554 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002555 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002556 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002557 unsigned num;
2558 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002559 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002560 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002561 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002562 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002563
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002564 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002565 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002566 if (!path[depth].p_hdr)
2567 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2568 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002569 if (unlikely(path[depth].p_hdr == NULL)) {
2570 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002571 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002572 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002573 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002574 ex = path[depth].p_ext;
2575 if (!ex)
2576 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002577
2578 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002579 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002580
Eric Whitney9fe67142018-10-01 14:25:08 -04002581 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002582
Alex Tomasa86c6182006-10-11 01:21:03 -07002583 while (ex >= EXT_FIRST_EXTENT(eh) &&
2584 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002585
Lukas Czerner556615d2014-04-20 23:45:47 -04002586 if (ext4_ext_is_unwritten(ex))
2587 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002588 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002589 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002590
Mingming553f9002009-09-18 13:34:55 -04002591 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002592 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002593 path[depth].p_ext = ex;
2594
2595 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002596 b = ex_ee_block+ex_ee_len - 1 < end ?
2597 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002598
2599 ext_debug(" border %u:%u\n", a, b);
2600
Allison Hendersond583fb82011-05-25 07:41:43 -04002601 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002602 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002603 /*
2604 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002605 * so note that its first cluster is in use to avoid
2606 * freeing it when removing blocks. Eventually, the
2607 * right edge of the truncated/punched region will
2608 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002609 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002610 if (sbi->s_cluster_ratio > 1) {
2611 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002612 partial->pclu = EXT4_B2C(sbi, pblk);
2613 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002614 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002615 ex--;
2616 ex_ee_block = le32_to_cpu(ex->ee_block);
2617 ex_ee_len = ext4_ext_get_actual_len(ex);
2618 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002619 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002620 EXT4_ERROR_INODE(inode,
2621 "can not handle truncate %u:%u "
2622 "on extent %u:%u",
2623 start, end, ex_ee_block,
2624 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002625 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002626 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002627 } else if (a != ex_ee_block) {
2628 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002629 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002630 } else {
2631 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002633 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002634 /*
2635 * 3 for leaf, sb, and inode plus 2 (bmap and group
2636 * descriptor) for each block group; assume two block
2637 * groups plus ex_ee_len/blocks_per_block_group for
2638 * the worst case
2639 */
2640 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002641 if (ex == EXT_FIRST_EXTENT(eh)) {
2642 correct_index = 1;
2643 credits += (ext_depth(inode)) + 1;
2644 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002645 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002646 /*
2647 * We may end up freeing some index blocks and data from the
2648 * punched range. Note that partial clusters are accounted for
2649 * by ext4_free_data_revoke_credits().
2650 */
2651 revoke_credits =
2652 ext4_free_metadata_revoke_credits(inode->i_sb,
2653 ext_depth(inode)) +
2654 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002655
Jan Karaa4130362019-11-05 17:44:16 +01002656 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002657 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002658 if (err) {
2659 if (err > 0)
2660 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002662 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002663
2664 err = ext4_ext_get_access(handle, inode, path + depth);
2665 if (err)
2666 goto out;
2667
Eric Whitney9fe67142018-10-01 14:25:08 -04002668 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002669 if (err)
2670 goto out;
2671
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002672 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002673 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002674 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002675
Alex Tomasa86c6182006-10-11 01:21:03 -07002676 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002677 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002678 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002679 * extent have been removed.
2680 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002681 if (unwritten && num)
2682 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002683 /*
2684 * If the extent was completely released,
2685 * we need to remove it from the leaf
2686 */
2687 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002688 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002689 /*
2690 * For hole punching, we need to scoot all the
2691 * extents up when an extent is removed so that
2692 * we dont have blank extents in the middle
2693 */
2694 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2695 sizeof(struct ext4_extent));
2696
2697 /* Now get rid of the one at the end */
2698 memset(EXT_LAST_EXTENT(eh), 0,
2699 sizeof(struct ext4_extent));
2700 }
2701 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002702 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002703
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002704 err = ext4_ext_dirty(handle, inode, path + depth);
2705 if (err)
2706 goto out;
2707
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002708 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002709 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002710 ex--;
2711 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002712 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002713 }
2714
2715 if (correct_index && eh->eh_entries)
2716 err = ext4_ext_correct_indexes(handle, inode, path);
2717
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002718 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002719 * If there's a partial cluster and at least one extent remains in
2720 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002721 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002722 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002723 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002724 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002725 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002726 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002727 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2728 int flags = get_default_free_blocks_flags(inode);
2729
2730 if (ext4_is_pending(inode, partial->lblk))
2731 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002732 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002733 EXT4_C2B(sbi, partial->pclu),
2734 sbi->s_cluster_ratio, flags);
2735 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2736 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002737 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002738 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002739 }
2740
Alex Tomasa86c6182006-10-11 01:21:03 -07002741 /* if this leaf is free, then we should
2742 * remove it from index block above */
2743 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002744 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002745
2746out:
2747 return err;
2748}
2749
2750/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002751 * ext4_ext_more_to_rm:
2752 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002753 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002754static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002755ext4_ext_more_to_rm(struct ext4_ext_path *path)
2756{
2757 BUG_ON(path->p_idx == NULL);
2758
2759 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2760 return 0;
2761
2762 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002763 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002764 * so we have to consider current index for truncation
2765 */
2766 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2767 return 0;
2768 return 1;
2769}
2770
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002771int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2772 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002773{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002774 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002775 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002776 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002777 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002778 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002779 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002780
Eric Whitney9fe67142018-10-01 14:25:08 -04002781 partial.pclu = 0;
2782 partial.lblk = 0;
2783 partial.state = initial;
2784
Lukas Czerner5f95d212012-03-19 23:03:19 -04002785 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002786
2787 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002788 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2789 depth + 1,
2790 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002791 if (IS_ERR(handle))
2792 return PTR_ERR(handle);
2793
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002794again:
Lukas Czerner61801322013-05-27 23:32:35 -04002795 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002796
Alex Tomasa86c6182006-10-11 01:21:03 -07002797 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002798 * Check if we are removing extents inside the extent tree. If that
2799 * is the case, we are going to punch a hole inside the extent tree
2800 * so we have to check whether we need to split the extent covering
2801 * the last block to remove so we can easily remove the part of it
2802 * in ext4_ext_rm_leaf().
2803 */
2804 if (end < EXT_MAX_BLOCKS - 1) {
2805 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002806 ext4_lblk_t ee_block, ex_end, lblk;
2807 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002808
Eric Whitneyf4226d92014-11-23 00:55:42 -05002809 /* find extent for or closest extent to this block */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002810 path = ext4_find_extent(inode, end, NULL,
2811 EXT4_EX_NOCACHE | EXT4_EX_NOFAIL);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002812 if (IS_ERR(path)) {
2813 ext4_journal_stop(handle);
2814 return PTR_ERR(path);
2815 }
2816 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002817 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002818 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002819 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002820 if (depth) {
2821 EXT4_ERROR_INODE(inode,
2822 "path[%d].p_hdr == NULL",
2823 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002824 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002825 }
2826 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002827 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002828
2829 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002830 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002831
2832 /*
2833 * See if the last block is inside the extent, if so split
2834 * the extent at 'end' block so we can easily remove the
2835 * tail of the first part of the split extent in
2836 * ext4_ext_rm_leaf().
2837 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002838 if (end >= ee_block && end < ex_end) {
2839
2840 /*
2841 * If we're going to split the extent, note that
2842 * the cluster containing the block after 'end' is
2843 * in use to avoid freeing it when removing blocks.
2844 */
2845 if (sbi->s_cluster_ratio > 1) {
2846 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04002847 partial.pclu = EXT4_B2C(sbi, pblk);
2848 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002849 }
2850
Lukas Czerner5f95d212012-03-19 23:03:19 -04002851 /*
2852 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002853 * block in the first new extent. Also we should not
2854 * fail removing space due to ENOSPC so try to use
2855 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002856 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002857 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002858 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002859 if (err < 0)
2860 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002861
Eric Whitney7bd75232019-02-28 23:34:11 -05002862 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
2863 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05002864 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05002865 * If we're punching, there's an extent to the right.
2866 * If the partial cluster hasn't been set, set it to
2867 * that extent's first cluster and its state to nofree
2868 * so it won't be freed should it contain blocks to be
2869 * removed. If it's already set (tofree/nofree), we're
2870 * retrying and keep the original partial cluster info
2871 * so a cluster marked tofree as a result of earlier
2872 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05002873 */
2874 lblk = ex_end + 1;
2875 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2876 &ex);
2877 if (err)
2878 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04002879 if (pblk) {
2880 partial.pclu = EXT4_B2C(sbi, pblk);
2881 partial.state = nofree;
2882 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002883 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002884 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002885 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002886 * We start scanning from right side, freeing all the blocks
2887 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002888 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002889 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002890 if (path) {
2891 int k = i = depth;
2892 while (--k > 0)
2893 path[k].p_block =
2894 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2895 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002896 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002897 GFP_NOFS | __GFP_NOFAIL);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002898 if (path == NULL) {
2899 ext4_journal_stop(handle);
2900 return -ENOMEM;
2901 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002902 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002903 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002904 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002905
Theodore Ts'oc3491792013-08-16 21:21:41 -04002906 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002907 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002908 goto out;
2909 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002910 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002911 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002912
2913 while (i >= 0 && err == 0) {
2914 if (i == depth) {
2915 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002916 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002917 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002918 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002919 brelse(path[i].p_bh);
2920 path[i].p_bh = NULL;
2921 i--;
2922 continue;
2923 }
2924
2925 /* this is index block */
2926 if (!path[i].p_hdr) {
2927 ext_debug("initialize header\n");
2928 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002929 }
2930
Alex Tomasa86c6182006-10-11 01:21:03 -07002931 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002932 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002933 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2934 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2935 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2936 path[i].p_hdr,
2937 le16_to_cpu(path[i].p_hdr->eh_entries));
2938 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002939 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002940 path[i].p_idx--;
2941 }
2942
2943 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2944 i, EXT_FIRST_INDEX(path[i].p_hdr),
2945 path[i].p_idx);
2946 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002947 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002948 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002949 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002950 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002951 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002952 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002953 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2954 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002955 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002956 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002957 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002958 break;
2959 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04002960 /* Yield here to deal with large extent trees.
2961 * Should be a no-op if we did IO above. */
2962 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002963 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002964 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002965 break;
2966 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002967 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002968
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002969 /* save actual number of indexes since this
2970 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002971 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2972 i++;
2973 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002974 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002975 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002976 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002977 * handle must be already prepared by the
2978 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05002979 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002980 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002981 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002982 brelse(path[i].p_bh);
2983 path[i].p_bh = NULL;
2984 i--;
2985 ext_debug("return to level %d\n", i);
2986 }
2987 }
2988
Eric Whitney9fe67142018-10-01 14:25:08 -04002989 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
2990 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04002991
Eric Whitney0756b902014-11-23 00:59:39 -05002992 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002993 * if there's a partial cluster and we have removed the first extent
2994 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05002995 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002996 if (partial.state == tofree && err == 0) {
2997 int flags = get_default_free_blocks_flags(inode);
2998
2999 if (ext4_is_pending(inode, partial.lblk))
3000 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003001 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003002 EXT4_C2B(sbi, partial.pclu),
3003 sbi->s_cluster_ratio, flags);
3004 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3005 ext4_rereserve_cluster(inode, partial.lblk);
3006 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003007 }
3008
Alex Tomasa86c6182006-10-11 01:21:03 -07003009 /* TODO: flexible tree reduction should be here */
3010 if (path->p_hdr->eh_entries == 0) {
3011 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003012 * truncate to zero freed all the tree,
3013 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003014 */
3015 err = ext4_ext_get_access(handle, inode, path);
3016 if (err == 0) {
3017 ext_inode_hdr(inode)->eh_depth = 0;
3018 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003019 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003020 err = ext4_ext_dirty(handle, inode, path);
3021 }
3022 }
3023out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003024 ext4_ext_drop_refs(path);
3025 kfree(path);
3026 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003027 if (err == -EAGAIN)
3028 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003029 ext4_journal_stop(handle);
3030
3031 return err;
3032}
3033
3034/*
3035 * called at mount time
3036 */
3037void ext4_ext_init(struct super_block *sb)
3038{
3039 /*
3040 * possible initialization would be here
3041 */
3042
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003043 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003044#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003045 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003046#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003047 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003048#endif
3049#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003050 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003051#endif
3052#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003053 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003054#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003055 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003056#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003057#ifdef EXTENTS_STATS
3058 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3059 EXT4_SB(sb)->s_ext_min = 1 << 30;
3060 EXT4_SB(sb)->s_ext_max = 0;
3061#endif
3062 }
3063}
3064
3065/*
3066 * called at umount time
3067 */
3068void ext4_ext_release(struct super_block *sb)
3069{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003070 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003071 return;
3072
3073#ifdef EXTENTS_STATS
3074 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3075 struct ext4_sb_info *sbi = EXT4_SB(sb);
3076 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3077 sbi->s_ext_blocks, sbi->s_ext_extents,
3078 sbi->s_ext_blocks / sbi->s_ext_extents);
3079 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3080 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3081 }
3082#endif
3083}
3084
Zheng Liud7b2a002013-08-28 14:47:06 -04003085static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3086{
3087 ext4_lblk_t ee_block;
3088 ext4_fsblk_t ee_pblock;
3089 unsigned int ee_len;
3090
3091 ee_block = le32_to_cpu(ex->ee_block);
3092 ee_len = ext4_ext_get_actual_len(ex);
3093 ee_pblock = ext4_ext_pblock(ex);
3094
3095 if (ee_len == 0)
3096 return 0;
3097
3098 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3099 EXTENT_STATUS_WRITTEN);
3100}
3101
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003102/* FIXME!! we need to try to merge to left or right after zero-out */
3103static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3104{
Lukas Czerner24075182010-10-27 21:30:06 -04003105 ext4_fsblk_t ee_pblock;
3106 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003107
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003108 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003109 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003110 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3111 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003112}
3113
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003114/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003115 * ext4_split_extent_at() splits an extent at given block.
3116 *
3117 * @handle: the journal handle
3118 * @inode: the file inode
3119 * @path: the path to the extent
3120 * @split: the logical block where the extent is splitted.
3121 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003122 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003123 * @flags: flags used to insert new extent to extent tree.
3124 *
3125 *
3126 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3127 * of which are deterimined by split_flag.
3128 *
3129 * There are two cases:
3130 * a> the extent are splitted into two extent.
3131 * b> split is not needed, and just mark the extent.
3132 *
3133 * return 0 on success.
3134 */
3135static int ext4_split_extent_at(handle_t *handle,
3136 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003137 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003138 ext4_lblk_t split,
3139 int split_flag,
3140 int flags)
3141{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003142 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003143 ext4_fsblk_t newblock;
3144 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003145 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003146 struct ext4_extent *ex2 = NULL;
3147 unsigned int ee_len, depth;
3148 int err = 0;
3149
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003150 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3151 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3152
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003153 ext_debug("ext4_split_extents_at: inode %lu, logical"
3154 "block %llu\n", inode->i_ino, (unsigned long long)split);
3155
3156 ext4_ext_show_leaf(inode, path);
3157
3158 depth = ext_depth(inode);
3159 ex = path[depth].p_ext;
3160 ee_block = le32_to_cpu(ex->ee_block);
3161 ee_len = ext4_ext_get_actual_len(ex);
3162 newblock = split - ee_block + ext4_ext_pblock(ex);
3163
3164 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003165 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003166 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003167 EXT4_EXT_MARK_UNWRIT1 |
3168 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003169
3170 err = ext4_ext_get_access(handle, inode, path + depth);
3171 if (err)
3172 goto out;
3173
3174 if (split == ee_block) {
3175 /*
3176 * case b: block @split is the block that the extent begins with
3177 * then we just change the state of the extent, and splitting
3178 * is not needed.
3179 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003180 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3181 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003182 else
3183 ext4_ext_mark_initialized(ex);
3184
3185 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003186 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003187
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003188 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003189 goto out;
3190 }
3191
3192 /* case a */
3193 memcpy(&orig_ex, ex, sizeof(orig_ex));
3194 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003195 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3196 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003197
3198 /*
3199 * path may lead to new leaf, not to original leaf any more
3200 * after ext4_ext_insert_extent() returns,
3201 */
3202 err = ext4_ext_dirty(handle, inode, path + depth);
3203 if (err)
3204 goto fix_extent_len;
3205
3206 ex2 = &newex;
3207 ex2->ee_block = cpu_to_le32(split);
3208 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3209 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003210 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3211 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003212
Theodore Ts'odfe50802014-09-01 14:37:09 -04003213 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003214 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003215 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003216 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003217 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003218 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003219 zero_ex.ee_len = cpu_to_le16(
3220 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003221 ext4_ext_store_pblock(&zero_ex,
3222 ext4_ext_pblock(ex2));
3223 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003224 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003225 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003226 zero_ex.ee_len = cpu_to_le16(
3227 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003228 ext4_ext_store_pblock(&zero_ex,
3229 ext4_ext_pblock(ex));
3230 }
3231 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003232 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003233 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003234 zero_ex.ee_len = cpu_to_le16(
3235 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003236 ext4_ext_store_pblock(&zero_ex,
3237 ext4_ext_pblock(&orig_ex));
3238 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003239
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003240 if (err)
3241 goto fix_extent_len;
3242 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003243 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003244 ext4_ext_try_to_merge(handle, inode, path, ex);
3245 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003246 if (err)
3247 goto fix_extent_len;
3248
3249 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003250 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003251
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003252 goto out;
3253 } else if (err)
3254 goto fix_extent_len;
3255
3256out:
3257 ext4_ext_show_leaf(inode, path);
3258 return err;
3259
3260fix_extent_len:
3261 ex->ee_len = orig_ex.ee_len;
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003262 /*
3263 * Ignore ext4_ext_dirty return value since we are already in error path
3264 * and err is a non-zero error code.
3265 */
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003266 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003267 return err;
3268}
3269
3270/*
3271 * ext4_split_extents() splits an extent and mark extent which is covered
3272 * by @map as split_flags indicates
3273 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003274 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003275 * There are three possibilities:
3276 * a> There is no split required
3277 * b> Splits in two extents: Split is happening at either end of the extent
3278 * c> Splits in three extents: Somone is splitting in middle of the extent
3279 *
3280 */
3281static int ext4_split_extent(handle_t *handle,
3282 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003283 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003284 struct ext4_map_blocks *map,
3285 int split_flag,
3286 int flags)
3287{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003288 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003289 ext4_lblk_t ee_block;
3290 struct ext4_extent *ex;
3291 unsigned int ee_len, depth;
3292 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003293 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003294 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003295 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296
3297 depth = ext_depth(inode);
3298 ex = path[depth].p_ext;
3299 ee_block = le32_to_cpu(ex->ee_block);
3300 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003301 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003302
3303 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003304 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003305 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003306 if (unwritten)
3307 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3308 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003309 if (split_flag & EXT4_EXT_DATA_VALID2)
3310 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003311 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003312 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003313 if (err)
3314 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003315 } else {
3316 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003317 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003318 /*
3319 * Update path is required because previous ext4_split_extent_at() may
3320 * result in split of original leaf or extent zeroout.
3321 */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07003322 path = ext4_find_extent(inode, map->m_lblk, ppath, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003323 if (IS_ERR(path))
3324 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003325 depth = ext_depth(inode);
3326 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003327 if (!ex) {
3328 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3329 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003330 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003331 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003332 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003333 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003334
3335 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003336 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003337 if (unwritten) {
3338 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003339 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003340 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003341 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003342 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003343 map->m_lblk, split_flag1, flags);
3344 if (err)
3345 goto out;
3346 }
3347
3348 ext4_ext_show_leaf(inode, path);
3349out:
Zheng Liu3a225672013-03-10 21:20:23 -04003350 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003351}
3352
Amit Arora56055d32007-07-17 21:42:38 -04003353/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003354 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003355 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003356 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003357 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003358 * There are three possibilities:
3359 * a> There is no split required: Entire extent should be initialized
3360 * b> Splits in two extents: Write is happening at either end of the extent
3361 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003362 *
3363 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003364 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003365 * - The extent pointed to by 'path' contains a superset
3366 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3367 *
3368 * Post-conditions on success:
3369 * - the returned value is the number of blocks beyond map->l_lblk
3370 * that are allocated and initialized.
3371 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003372 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003373static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003374 struct inode *inode,
3375 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003376 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003377 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003378{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003379 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003380 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003381 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003382 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003383 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003384 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003385 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003386 unsigned int ee_len, depth, map_len = map->m_len;
3387 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003388 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003389 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003390
3391 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3392 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003393 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003394
Zheng Liu67a5da52012-08-17 09:54:17 -04003395 sbi = EXT4_SB(inode->i_sb);
Jan Kara801674f2020-03-31 12:50:16 +02003396 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3397 >> inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003398 if (eof_block < map->m_lblk + map_len)
3399 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003400
3401 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003402 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003403 ex = path[depth].p_ext;
3404 ee_block = le32_to_cpu(ex->ee_block);
3405 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003406 zero_ex1.ee_len = 0;
3407 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003408
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003409 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3410
3411 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003412 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003413 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003414
3415 /*
3416 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003417 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003418 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003419 * memmove() calls. Transferring to the left is the common case in
3420 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3421 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003422 *
3423 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003424 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003425 * This would require removing the extent if the transfer
3426 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003427 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003428 * same extent tree node.
3429 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003430 if ((map->m_lblk == ee_block) &&
3431 /* See if we can merge left */
3432 (map_len < ee_len) && /*L1*/
3433 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003434 ext4_lblk_t prev_lblk;
3435 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003436 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003437
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003438 abut_ex = ex - 1;
3439 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3440 prev_len = ext4_ext_get_actual_len(abut_ex);
3441 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003442 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003443
3444 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003445 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003446 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003447 * - C1: abut_ex is initialized,
3448 * - C2: abut_ex is logically abutting ex,
3449 * - C3: abut_ex is physically abutting ex,
3450 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003451 * overflowing the (initialized) length limit.
3452 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003453 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003454 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3455 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003456 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003457 err = ext4_ext_get_access(handle, inode, path + depth);
3458 if (err)
3459 goto out;
3460
3461 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003462 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003463
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003464 /* Shift the start of ex by 'map_len' blocks */
3465 ex->ee_block = cpu_to_le32(ee_block + map_len);
3466 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3467 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003468 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003469
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003470 /* Extend abut_ex by 'map_len' blocks */
3471 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003472
3473 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003474 allocated = map_len;
3475 }
3476 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3477 (map_len < ee_len) && /*L1*/
3478 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3479 /* See if we can merge right */
3480 ext4_lblk_t next_lblk;
3481 ext4_fsblk_t next_pblk, ee_pblk;
3482 unsigned int next_len;
3483
3484 abut_ex = ex + 1;
3485 next_lblk = le32_to_cpu(abut_ex->ee_block);
3486 next_len = ext4_ext_get_actual_len(abut_ex);
3487 next_pblk = ext4_ext_pblock(abut_ex);
3488 ee_pblk = ext4_ext_pblock(ex);
3489
3490 /*
3491 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3492 * upon those conditions:
3493 * - C1: abut_ex is initialized,
3494 * - C2: abut_ex is logically abutting ex,
3495 * - C3: abut_ex is physically abutting ex,
3496 * - C4: abut_ex can receive the additional blocks without
3497 * overflowing the (initialized) length limit.
3498 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003499 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003500 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3501 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3502 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3503 err = ext4_ext_get_access(handle, inode, path + depth);
3504 if (err)
3505 goto out;
3506
3507 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3508 map, ex, abut_ex);
3509
3510 /* Shift the start of abut_ex by 'map_len' blocks */
3511 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3512 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3513 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003514 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003515
3516 /* Extend abut_ex by 'map_len' blocks */
3517 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3518
3519 /* Result: number of initialized blocks past m_lblk */
3520 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003521 }
3522 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003523 if (allocated) {
3524 /* Mark the block containing both extents as dirty */
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003525 err = ext4_ext_dirty(handle, inode, path + depth);
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003526
3527 /* Update path to point to the right extent */
3528 path[depth].p_ext = abut_ex;
3529 goto out;
3530 } else
3531 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003532
Yongqiang Yang667eff32011-05-03 12:25:07 -04003533 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003534 /*
3535 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003536 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003537 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003538 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003539
Zheng Liu67a5da52012-08-17 09:54:17 -04003540 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3541 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003542 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003543
Amit Arora56055d32007-07-17 21:42:38 -04003544 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003545 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003546 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003547 * 2. split the extent into two extents, zeroout the head of the first
3548 * extent.
3549 * 3. split the extent into two extents, zeroout the tail of the second
3550 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003551 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003552 * 5. no splitting needed, just possibly zeroout the head and / or the
3553 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003554 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003555 split_map.m_lblk = map->m_lblk;
3556 split_map.m_len = map->m_len;
3557
Jan Kara4f8caa62017-05-26 17:40:52 -04003558 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003559 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003560 /* case 3 or 5 */
3561 zero_ex1.ee_block =
3562 cpu_to_le32(split_map.m_lblk +
3563 split_map.m_len);
3564 zero_ex1.ee_len =
3565 cpu_to_le16(allocated - split_map.m_len);
3566 ext4_ext_store_pblock(&zero_ex1,
3567 ext4_ext_pblock(ex) + split_map.m_lblk +
3568 split_map.m_len - ee_block);
3569 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003570 if (err)
3571 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003572 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003573 }
3574 if (split_map.m_lblk - ee_block + split_map.m_len <
3575 max_zeroout) {
3576 /* case 2 or 5 */
3577 if (split_map.m_lblk != ee_block) {
3578 zero_ex2.ee_block = ex->ee_block;
3579 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003580 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003581 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003582 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003583 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003584 if (err)
3585 goto out;
3586 }
3587
Jan Kara4f8caa62017-05-26 17:40:52 -04003588 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003589 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003590 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003591 }
3592 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003593
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003594 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3595 flags);
3596 if (err > 0)
3597 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003598out:
Zheng Liuadb23552013-03-10 21:13:05 -04003599 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003600 if (!err) {
3601 err = ext4_zeroout_es(inode, &zero_ex1);
3602 if (!err)
3603 err = ext4_zeroout_es(inode, &zero_ex2);
3604 }
Amit Arora56055d32007-07-17 21:42:38 -04003605 return err ? err : allocated;
3606}
3607
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003608/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003609 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003610 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003611 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003612 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003613 * Writing to an unwritten extent may result in splitting the unwritten
3614 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003615 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003616 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003617 * b> Splits in two extents: Write is happening at either end of the extent
3618 * c> Splits in three extents: Somone is writing in middle of the extent
3619 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003620 * This works the same way in the case of initialized -> unwritten conversion.
3621 *
Mingming Cao00314622009-09-28 15:49:08 -04003622 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003623 * the unwritten extent split. To prevent ENOSPC occur at the IO
3624 * complete, we need to split the unwritten extent before DIO submit
3625 * the IO. The unwritten extent called at this time will be split
3626 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003627 * being filled will be convert to initialized by the end_io callback function
3628 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003629 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003630 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003631 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003632static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003633 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003634 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003635 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003636 int flags)
3637{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003638 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003639 ext4_lblk_t eof_block;
3640 ext4_lblk_t ee_block;
3641 struct ext4_extent *ex;
3642 unsigned int ee_len;
3643 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003644
Lukas Czernerb8a86842014-03-18 18:05:35 -04003645 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3646 __func__, inode->i_ino,
3647 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003648
Jan Kara801674f2020-03-31 12:50:16 +02003649 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3650 >> inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003651 if (eof_block < map->m_lblk + map->m_len)
3652 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003653 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003654 * It is safe to convert extent to initialized via explicit
3655 * zeroout only if extent is fully insde i_size or new_size.
3656 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003657 depth = ext_depth(inode);
3658 ex = path[depth].p_ext;
3659 ee_block = le32_to_cpu(ex->ee_block);
3660 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003661
Lukas Czernerb8a86842014-03-18 18:05:35 -04003662 /* Convert to unwritten */
3663 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3664 split_flag |= EXT4_EXT_DATA_VALID1;
3665 /* Convert to initialized */
3666 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3667 split_flag |= ee_block + ee_len <= eof_block ?
3668 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003669 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003670 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003671 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003672 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003673}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003674
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003675static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003676 struct inode *inode,
3677 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003678 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003679{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003680 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003681 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003682 ext4_lblk_t ee_block;
3683 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003684 int depth;
3685 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003686
3687 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003688 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003689 ee_block = le32_to_cpu(ex->ee_block);
3690 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003691
Yongqiang Yang197217a2011-05-03 11:45:29 -04003692 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3693 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003694 (unsigned long long)ee_block, ee_len);
3695
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003696 /* If extent is larger than requested it is a clear sign that we still
3697 * have some extent state machine issues left. So extent_split is still
3698 * required.
3699 * TODO: Once all related issues will be fixed this situation should be
3700 * illegal.
3701 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003702 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003703#ifdef CONFIG_EXT4_DEBUG
3704 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003705 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003706 inode->i_ino, (unsigned long long)ee_block, ee_len,
3707 (unsigned long long)map->m_lblk, map->m_len);
3708#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003709 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003710 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003711 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003712 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003713 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003714 if (IS_ERR(path))
3715 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003716 depth = ext_depth(inode);
3717 ex = path[depth].p_ext;
3718 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003719
Mingming Cao00314622009-09-28 15:49:08 -04003720 err = ext4_ext_get_access(handle, inode, path + depth);
3721 if (err)
3722 goto out;
3723 /* first mark the extent as initialized */
3724 ext4_ext_mark_initialized(ex);
3725
Yongqiang Yang197217a2011-05-03 11:45:29 -04003726 /* note: ext4_ext_correct_indexes() isn't needed here because
3727 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003728 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003729 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003730
Mingming Cao00314622009-09-28 15:49:08 -04003731 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003732 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003733out:
3734 ext4_ext_show_leaf(inode, path);
3735 return err;
3736}
3737
3738static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003739convert_initialized_extent(handle_t *handle, struct inode *inode,
3740 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003741 struct ext4_ext_path **ppath,
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003742 unsigned int *allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003743{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003744 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003745 struct ext4_extent *ex;
3746 ext4_lblk_t ee_block;
3747 unsigned int ee_len;
3748 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003749 int err = 0;
3750
3751 /*
3752 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003753 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003754 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003755 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3756 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003757
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003758 depth = ext_depth(inode);
3759 ex = path[depth].p_ext;
3760 ee_block = le32_to_cpu(ex->ee_block);
3761 ee_len = ext4_ext_get_actual_len(ex);
3762
3763 ext_debug("%s: inode %lu, logical"
3764 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3765 (unsigned long long)ee_block, ee_len);
3766
3767 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003768 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003769 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3770 if (err < 0)
3771 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003772 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003773 if (IS_ERR(path))
3774 return PTR_ERR(path);
3775 depth = ext_depth(inode);
3776 ex = path[depth].p_ext;
3777 if (!ex) {
3778 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3779 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003780 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003781 }
3782 }
3783
3784 err = ext4_ext_get_access(handle, inode, path + depth);
3785 if (err)
3786 return err;
3787 /* first mark the extent as unwritten */
3788 ext4_ext_mark_unwritten(ex);
3789
3790 /* note: ext4_ext_correct_indexes() isn't needed here because
3791 * borders are not changed
3792 */
3793 ext4_ext_try_to_merge(handle, inode, path, ex);
3794
3795 /* Mark modified extent as dirty */
3796 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3797 if (err)
3798 return err;
3799 ext4_ext_show_leaf(inode, path);
3800
3801 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003802
Lukas Czernerb8a86842014-03-18 18:05:35 -04003803 map->m_flags |= EXT4_MAP_UNWRITTEN;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003804 if (*allocated > map->m_len)
3805 *allocated = map->m_len;
3806 map->m_len = *allocated;
3807 return 0;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003808}
3809
3810static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003811ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003812 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003813 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003814 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003815{
Ritesh Harjani8ec2d312020-05-10 11:54:53 +05303816 struct ext4_ext_path __maybe_unused *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003817 int ret = 0;
3818 int err = 0;
3819
Lukas Czerner556615d2014-04-20 23:45:47 -04003820 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05003821 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003822 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003823 flags, allocated);
3824 ext4_ext_show_leaf(inode, path);
3825
Lukas Czerner27dd4382013-04-09 22:11:22 -04003826 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04003827 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04003828 * allocate metadata blocks for the new extent block if needed.
3829 */
3830 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
3831
Lukas Czerner556615d2014-04-20 23:45:47 -04003832 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05003833 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003834
Eric Whitney779e2652020-04-30 14:53:19 -04003835 /* get_block() before submitting IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003836 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003837 ret = ext4_split_convert_extents(handle, inode, map, ppath,
3838 flags | EXT4_GET_BLOCKS_CONVERT);
Eric Whitney779e2652020-04-30 14:53:19 -04003839 if (ret < 0) {
3840 err = ret;
3841 goto out2;
3842 }
3843 /*
3844 * shouldn't get a 0 return when splitting an extent unless
3845 * m_len is 0 (bug) or extent has been corrupted
3846 */
3847 if (unlikely(ret == 0)) {
3848 EXT4_ERROR_INODE(inode,
3849 "unexpected ret == 0, m_len = %u",
3850 map->m_len);
3851 err = -EFSCORRUPTED;
3852 goto out2;
3853 }
Zheng Liua25a4e12013-02-18 00:28:04 -05003854 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003855 goto out;
3856 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003857 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003858 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Eric Whitneybee6cf02020-04-30 14:53:18 -04003859 err = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003860 ppath);
Eric Whitneybee6cf02020-04-30 14:53:18 -04003861 if (err < 0)
3862 goto out2;
3863 ext4_update_inode_fsync_trans(handle, inode, 1);
3864 goto map_out;
Mingming Cao00314622009-09-28 15:49:08 -04003865 }
Eric Whitneybee6cf02020-04-30 14:53:18 -04003866 /* buffered IO cases */
Mingming Cao00314622009-09-28 15:49:08 -04003867 /*
3868 * repeat fallocate creation request
3869 * we already have an unwritten extent
3870 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003871 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05003872 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003873 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003874 }
Mingming Cao00314622009-09-28 15:49:08 -04003875
3876 /* buffered READ or buffered write_begin() lookup */
3877 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3878 /*
3879 * We have blocks reserved already. We
3880 * return allocated blocks so that delalloc
3881 * won't do block reservation for us. But
3882 * the buffer head will be unmapped so that
3883 * a read from the block returns 0s.
3884 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003885 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003886 goto out1;
3887 }
3888
Eric Whitneybe809e12020-04-30 14:53:20 -04003889 /*
3890 * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1.
3891 * For buffered writes, at writepage time, etc. Convert a
3892 * discovered unwritten extent to written.
3893 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04003894 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Eric Whitneybe809e12020-04-30 14:53:20 -04003895 if (ret < 0) {
Mingming Cao00314622009-09-28 15:49:08 -04003896 err = ret;
3897 goto out2;
Eric Whitney779e2652020-04-30 14:53:19 -04003898 }
Eric Whitneybe809e12020-04-30 14:53:20 -04003899 ext4_update_inode_fsync_trans(handle, inode, 1);
3900 /*
3901 * shouldn't get a 0 return when converting an unwritten extent
3902 * unless m_len is 0 (bug) or extent has been corrupted
3903 */
3904 if (unlikely(ret == 0)) {
3905 EXT4_ERROR_INODE(inode, "unexpected ret == 0, m_len = %u",
3906 map->m_len);
3907 err = -EFSCORRUPTED;
3908 goto out2;
3909 }
3910
Eric Whitney779e2652020-04-30 14:53:19 -04003911out:
3912 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003913 map->m_flags |= EXT4_MAP_NEW;
Mingming Cao00314622009-09-28 15:49:08 -04003914map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003915 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003916out1:
Eric Whitneybee6cf02020-04-30 14:53:18 -04003917 map->m_pblk = newblock;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003918 if (allocated > map->m_len)
3919 allocated = map->m_len;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003920 map->m_len = allocated;
Eric Whitneybee6cf02020-04-30 14:53:18 -04003921 ext4_ext_show_leaf(inode, path);
Mingming Cao00314622009-09-28 15:49:08 -04003922out2:
Mingming Cao00314622009-09-28 15:49:08 -04003923 return err ? err : allocated;
3924}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003925
Mingming Cao00314622009-09-28 15:49:08 -04003926/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003927 * get_implied_cluster_alloc - check to see if the requested
3928 * allocation (in the map structure) overlaps with a cluster already
3929 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003930 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003931 * @map The requested lblk->pblk mapping
3932 * @ex The extent structure which might contain an implied
3933 * cluster allocation
3934 *
3935 * This function is called by ext4_ext_map_blocks() after we failed to
3936 * find blocks that were already in the inode's extent tree. Hence,
3937 * we know that the beginning of the requested region cannot overlap
3938 * the extent from the inode's extent tree. There are three cases we
3939 * want to catch. The first is this case:
3940 *
3941 * |--- cluster # N--|
3942 * |--- extent ---| |---- requested region ---|
3943 * |==========|
3944 *
3945 * The second case that we need to test for is this one:
3946 *
3947 * |--------- cluster # N ----------------|
3948 * |--- requested region --| |------- extent ----|
3949 * |=======================|
3950 *
3951 * The third case is when the requested region lies between two extents
3952 * within the same cluster:
3953 * |------------- cluster # N-------------|
3954 * |----- ex -----| |---- ex_right ----|
3955 * |------ requested region ------|
3956 * |================|
3957 *
3958 * In each of the above cases, we need to set the map->m_pblk and
3959 * map->m_len so it corresponds to the return the extent labelled as
3960 * "|====|" from cluster #N, since it is already in use for data in
3961 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3962 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3963 * as a new "allocated" block region. Otherwise, we will return 0 and
3964 * ext4_ext_map_blocks() will then allocate one or more new clusters
3965 * by calling ext4_mb_new_blocks().
3966 */
Aditya Kalid8990242011-09-09 19:18:51 -04003967static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003968 struct ext4_map_blocks *map,
3969 struct ext4_extent *ex,
3970 struct ext4_ext_path *path)
3971{
Aditya Kalid8990242011-09-09 19:18:51 -04003972 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003973 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003974 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003975 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003976 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3977 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3978 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3979
3980 /* The extent passed in that we are trying to match */
3981 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3982 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3983
3984 /* The requested region passed into ext4_map_blocks() */
3985 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003986
3987 if ((rr_cluster_start == ex_cluster_end) ||
3988 (rr_cluster_start == ex_cluster_start)) {
3989 if (rr_cluster_start == ex_cluster_end)
3990 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003991 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003992 map->m_len = min(map->m_len,
3993 (unsigned) sbi->s_cluster_ratio - c_offset);
3994 /*
3995 * Check for and handle this case:
3996 *
3997 * |--------- cluster # N-------------|
3998 * |------- extent ----|
3999 * |--- requested region ---|
4000 * |===========|
4001 */
4002
4003 if (map->m_lblk < ee_block)
4004 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4005
4006 /*
4007 * Check for the case where there is already another allocated
4008 * block to the right of 'ex' but before the end of the cluster.
4009 *
4010 * |------------- cluster # N-------------|
4011 * |----- ex -----| |---- ex_right ----|
4012 * |------ requested region ------|
4013 * |================|
4014 */
4015 if (map->m_lblk > ee_block) {
4016 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4017 map->m_len = min(map->m_len, next - map->m_lblk);
4018 }
Aditya Kalid8990242011-09-09 19:18:51 -04004019
4020 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004021 return 1;
4022 }
Aditya Kalid8990242011-09-09 19:18:51 -04004023
4024 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004025 return 0;
4026}
4027
4028
4029/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004030 * Block allocation/map/preallocation routine for extents based files
4031 *
4032 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004033 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004034 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4035 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004036 *
4037 * return > 0, number of of blocks already mapped/allocated
4038 * if create == 0 and these are pre-allocated blocks
4039 * buffer head is unmapped
4040 * otherwise blocks are mapped
4041 *
4042 * return = 0, if plain look up failed (blocks have not been allocated)
4043 * buffer head is unmapped
4044 *
4045 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004046 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004047int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4048 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004049{
4050 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004051 struct ext4_extent newex, *ex, *ex2;
4052 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004053 ext4_fsblk_t newblock = 0;
Eric Whitney34990462020-03-11 16:50:33 -04004054 int err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004055 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004056 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004057 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004058 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07004059
Mingming84fe3be2009-09-01 08:44:37 -04004060 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004061 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004062 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004063
Alex Tomasa86c6182006-10-11 01:21:03 -07004064 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004065 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004066 if (IS_ERR(path)) {
4067 err = PTR_ERR(path);
4068 path = NULL;
4069 goto out2;
4070 }
4071
4072 depth = ext_depth(inode);
4073
4074 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004075 * consistent leaf must not be empty;
4076 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004077 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004078 */
Frank Mayhar273df552010-03-02 11:46:09 -05004079 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4080 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004081 "lblock: %lu, depth: %d pblock %lld",
4082 (unsigned long) map->m_lblk, depth,
4083 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004084 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004085 goto out2;
4086 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004087
Avantika Mathur7e028972006-12-06 20:41:33 -08004088 ex = path[depth].p_ext;
4089 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004090 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004091 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004092 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004093
Lukas Czernerb8a86842014-03-18 18:05:35 -04004094
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004095 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004096 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004097 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004098 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004099 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004100
4101 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4102
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004103 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004104 if (in_range(map->m_lblk, ee_block, ee_len)) {
4105 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004106 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004107 allocated = ee_len - (map->m_lblk - ee_block);
4108 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4109 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004110
Lukas Czernerb8a86842014-03-18 18:05:35 -04004111 /*
4112 * If the extent is initialized check whether the
4113 * caller wants to convert it to unwritten.
4114 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004115 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004116 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004117 err = convert_initialized_extent(handle,
4118 inode, map, &path, &allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004119 goto out2;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004120 } else if (!ext4_ext_is_unwritten(ex)) {
Lukas Czerner78771912012-03-19 23:05:43 -04004121 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004122 }
Zheng Liu69eb33d2013-02-18 00:31:07 -05004123
Lukas Czerner556615d2014-04-20 23:45:47 -04004124 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004125 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004126 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004127 if (ret < 0)
4128 err = ret;
4129 else
4130 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004131 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004132 }
4133 }
4134
4135 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004136 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004137 * we couldn't try to create block if create flag is zero
4138 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004139 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004140 ext4_lblk_t hole_start, hole_len;
4141
Jan Karafacab4d2016-03-09 22:54:00 -05004142 hole_start = map->m_lblk;
4143 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004144 /*
4145 * put just found gap into cache to speed up
4146 * subsequent requests
4147 */
Jan Kara140a5252016-03-09 22:46:57 -05004148 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004149
4150 /* Update hole_len to reflect hole size after map->m_lblk */
4151 if (hole_start != map->m_lblk)
4152 hole_len -= map->m_lblk - hole_start;
4153 map->m_pblk = 0;
4154 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4155
Alex Tomasa86c6182006-10-11 01:21:03 -07004156 goto out2;
4157 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004158
Alex Tomasa86c6182006-10-11 01:21:03 -07004159 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004160 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004161 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004162 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004163 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004164
4165 /*
4166 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004167 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004168 */
4169 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004170 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004171 ar.len = allocated = map->m_len;
4172 newblock = map->m_pblk;
4173 goto got_allocated_blocks;
4174 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004175
Alex Tomasc9de5602008-01-29 00:19:52 -05004176 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004177 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004178 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4179 if (err)
4180 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004181 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004182 ex2 = NULL;
4183 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004184 if (err)
4185 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004186
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004187 /* Check if the extent after searching to the right implies a
4188 * cluster we can use. */
4189 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004190 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004191 ar.len = allocated = map->m_len;
4192 newblock = map->m_pblk;
4193 goto got_allocated_blocks;
4194 }
4195
Amit Arora749269f2007-07-18 09:02:56 -04004196 /*
4197 * See if request is beyond maximum number of blocks we can have in
4198 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004199 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4200 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004201 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004202 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004203 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004204 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004205 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4206 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4207 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004208
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004209 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004210 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004211 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004212 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004213 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004214 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004215 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004216
4217 /* allocate new block */
4218 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004219 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4220 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004221 /*
4222 * We calculate the offset from the beginning of the cluster
4223 * for the logical block number, since when we allocate a
4224 * physical cluster, the physical block should start at the
4225 * same offset from the beginning of the cluster. This is
4226 * needed so that future calls to get_implied_cluster_alloc()
4227 * work correctly.
4228 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004229 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004230 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4231 ar.goal -= offset;
4232 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004233 if (S_ISREG(inode->i_mode))
4234 ar.flags = EXT4_MB_HINT_DATA;
4235 else
4236 /* disable in-core preallocation for non-regular files */
4237 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004238 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4239 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004240 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4241 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004242 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4243 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004244 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004245 if (!newblock)
4246 goto out2;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004247 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004248 ar.len = EXT4_C2B(sbi, ar.len) - offset;
Ritesh Harjaniec8c60b2020-05-10 11:54:52 +05304249 ext_debug("allocate new block: goal %llu, found %llu/%u, requested %u\n",
4250 ar.goal, newblock, ar.len, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004251 if (ar.len > allocated)
4252 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004253
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004254got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004255 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004256 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004257 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004258 /* Mark unwritten */
Eric Whitney34990462020-03-11 16:50:33 -04004259 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Lukas Czerner556615d2014-04-20 23:45:47 -04004260 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004261 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004262 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004263
Eric Whitney4337ecd2020-02-11 16:02:16 -05004264 err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
Eric Whitney34990462020-03-11 16:50:33 -04004265 if (err) {
4266 if (allocated_clusters) {
4267 int fb_flags = 0;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004268
Eric Whitney34990462020-03-11 16:50:33 -04004269 /*
4270 * free data blocks we just allocated.
4271 * not a good idea to call discard here directly,
4272 * but otherwise we'd need to call it every free().
4273 */
4274 ext4_discard_preallocations(inode);
4275 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4276 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
4277 ext4_free_blocks(handle, inode, NULL, newblock,
4278 EXT4_C2B(sbi, allocated_clusters),
4279 fb_flags);
4280 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004281 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004282 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004283
Alex Tomasa86c6182006-10-11 01:21:03 -07004284 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004285 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004286 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004287 if (allocated > map->m_len)
4288 allocated = map->m_len;
4289 map->m_flags |= EXT4_MAP_NEW;
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);
Alex Tomasa86c6182006-10-11 01:21:03 -07004336out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004337 if (allocated > map->m_len)
4338 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004339 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004340 map->m_flags |= EXT4_MAP_MAPPED;
4341 map->m_pblk = newblock;
4342 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004343out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004344 ext4_ext_drop_refs(path);
4345 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004346
Theodore Ts'o63b99962013-07-16 10:28:47 -04004347 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4348 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004349 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004350}
4351
Theodore Ts'od0abb362016-11-13 22:02:28 -05004352int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004353{
Alex Tomasa86c6182006-10-11 01:21:03 -07004354 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004355 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004356 int err = 0;
4357
4358 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004359 * TODO: optimization is possible here.
4360 * Probably we need not scan at all,
4361 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004362 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004363
4364 /* we have to know where to truncate from in crash case */
4365 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004366 err = ext4_mark_inode_dirty(handle, inode);
4367 if (err)
4368 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004369
4370 last_block = (inode->i_size + sb->s_blocksize - 1)
4371 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004372retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004373 err = ext4_es_remove_extent(inode, last_block,
4374 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004375 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004376 cond_resched();
4377 congestion_wait(BLK_RW_ASYNC, HZ/50);
4378 goto retry;
4379 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004380 if (err)
4381 return err;
Theodore Ts'o73c384c02020-05-07 10:50:28 -07004382retry_remove_space:
4383 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
4384 if (err == -ENOMEM) {
4385 cond_resched();
4386 congestion_wait(BLK_RW_ASYNC, HZ/50);
4387 goto retry_remove_space;
4388 }
4389 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004390}
4391
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004392static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004393 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004394 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004395{
Al Viro496ad9a2013-01-23 17:07:38 -05004396 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004397 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004398 int ret = 0;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004399 int ret2 = 0, ret3 = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004400 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004401 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004402 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004403 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004404 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004405
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004406 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004407 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004408 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004409 /*
4410 * Don't normalize the request if it can fit in one extent so
4411 * that it doesn't get unnecessarily split into multiple
4412 * extents.
4413 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004414 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004415 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004416
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004417 /*
4418 * credits to insert 1 extent into extent tree
4419 */
4420 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004421 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004422
Amit Aroraa2df2a62007-07-17 21:42:41 -04004423retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004424 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004425 /*
4426 * Recalculate credits when extent tree depth changes.
4427 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004428 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004429 credits = ext4_chunk_trans_blocks(inode, len);
4430 depth = ext_depth(inode);
4431 }
4432
Theodore Ts'o9924a922013-02-08 21:59:22 -05004433 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4434 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004435 if (IS_ERR(handle)) {
4436 ret = PTR_ERR(handle);
4437 break;
4438 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004439 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004440 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004441 ext4_debug("inode #%lu: block %u: len %u: "
4442 "ext4_ext_map_blocks returned %d",
4443 inode->i_ino, map.m_lblk,
4444 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004445 ext4_mark_inode_dirty(handle, inode);
4446 ret2 = ext4_journal_stop(handle);
4447 break;
4448 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004449 map.m_lblk += ret;
4450 map.m_len = len = len - ret;
4451 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004452 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004453 if (new_size) {
4454 if (epos > new_size)
4455 epos = new_size;
4456 if (ext4_update_inode_size(inode, epos) & 0x1)
4457 inode->i_mtime = inode->i_ctime;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004458 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004459 ret2 = ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004460 ext4_update_inode_fsync_trans(handle, inode, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004461 ret3 = ext4_journal_stop(handle);
4462 ret2 = ret3 ? ret3 : ret2;
4463 if (unlikely(ret2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004464 break;
4465 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004466 if (ret == -ENOSPC &&
4467 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4468 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004469 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004470 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004471
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004472 return ret > 0 ? ret2 : ret;
4473}
4474
Eric Biggers43f81672019-12-31 12:04:40 -06004475static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
4476
4477static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len);
4478
Lukas Czernerb8a86842014-03-18 18:05:35 -04004479static long ext4_zero_range(struct file *file, loff_t offset,
4480 loff_t len, int mode)
4481{
4482 struct inode *inode = file_inode(file);
4483 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 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004504 * Round up offset. This is not fallocate, we neet to zero out
4505 * 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 */
4569 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004570
4571 ret = ext4_break_layouts(inode);
4572 if (ret) {
4573 up_write(&EXT4_I(inode)->i_mmap_sem);
4574 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) {
4579 up_write(&EXT4_I(inode)->i_mmap_sem);
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 Karaea3d7202015-12-07 14:28:03 -05004588 up_write(&EXT4_I(inode)->i_mmap_sem);
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;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004615
4616 /* Zero out partial block at the edges of the range */
4617 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004618 if (ret >= 0)
4619 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004620
4621 if (file->f_flags & O_SYNC)
4622 ext4_handle_sync(handle);
4623
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004624out_handle:
Lukas Czernerb8a86842014-03-18 18:05:35 -04004625 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004626out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004627 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004628 return ret;
4629}
4630
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004631/*
4632 * preallocate space for a file. This implements ext4's fallocate file
4633 * operation, which gets called from sys_fallocate system call.
4634 * For block-mapped files, posix_fallocate should fall back to the method
4635 * of writing zeroes to the required new blocks (the same behavior which is
4636 * expected for file systems which do not support fallocate() system call).
4637 */
4638long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4639{
4640 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004641 loff_t new_size = 0;
4642 unsigned int max_blocks;
4643 int ret = 0;
4644 int flags;
4645 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004646 unsigned int blkbits = inode->i_blkbits;
4647
Michael Halcrow2058f832015-04-12 00:55:10 -04004648 /*
4649 * Encrypted inodes can't handle collapse range or insert
4650 * range since we would need to re-encrypt blocks with a
4651 * different IV or XTS tweak (which are based on the logical
4652 * block number).
Michael Halcrow2058f832015-04-12 00:55:10 -04004653 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304654 if (IS_ENCRYPTED(inode) &&
Eric Biggers457b1e32019-12-26 09:42:16 -06004655 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004656 return -EOPNOTSUPP;
4657
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004658 /* Return error if mode is not supported */
4659 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004660 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4661 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004662 return -EOPNOTSUPP;
4663
4664 if (mode & FALLOC_FL_PUNCH_HOLE)
4665 return ext4_punch_hole(inode, offset, len);
4666
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004667 ret = ext4_convert_inline_data(inode);
4668 if (ret)
4669 return ret;
4670
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004671 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4672 return ext4_collapse_range(inode, offset, len);
4673
Namjae Jeon331573f2015-06-09 01:55:03 -04004674 if (mode & FALLOC_FL_INSERT_RANGE)
4675 return ext4_insert_range(inode, offset, len);
4676
Lukas Czernerb8a86842014-03-18 18:05:35 -04004677 if (mode & FALLOC_FL_ZERO_RANGE)
4678 return ext4_zero_range(file, offset, len, mode);
4679
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004680 trace_ext4_fallocate_enter(inode, offset, len, mode);
4681 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004682
Fabian Frederick518eaa62016-09-15 11:55:01 -04004683 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004684 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004685
Al Viro59551022016-01-22 15:40:57 -05004686 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004687
Davide Italiano280227a2015-05-02 23:21:15 -04004688 /*
4689 * We only support preallocation for extent-based files only
4690 */
4691 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4692 ret = -EOPNOTSUPP;
4693 goto out;
4694 }
4695
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004696 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004697 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004698 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004699 new_size = offset + len;
4700 ret = inode_newsize_ok(inode, new_size);
4701 if (ret)
4702 goto out;
4703 }
4704
Jan Kara17048e82015-12-07 14:29:17 -05004705 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004706 inode_dio_wait(inode);
4707
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004708 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004709 if (ret)
4710 goto out;
4711
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004712 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4713 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4714 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004715 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004716out:
Al Viro59551022016-01-22 15:40:57 -05004717 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004718 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4719 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004720}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004721
4722/*
Mingming Cao00314622009-09-28 15:49:08 -04004723 * This function convert a range of blocks to written extents
4724 * The caller of this function will pass the start offset and the size.
4725 * all unwritten extents within this range will be converted to
4726 * written extents.
4727 *
4728 * This function is called from the direct IO end io call back
4729 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004730 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004731 */
Jan Kara6b523df2013-06-04 13:21:11 -04004732int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4733 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004734{
Mingming Cao00314622009-09-28 15:49:08 -04004735 unsigned int max_blocks;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004736 int ret = 0, ret2 = 0, ret3 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004737 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304738 unsigned int blkbits = inode->i_blkbits;
4739 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004740
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004741 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004742 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4743
Ritesh Harjania00713e2019-10-16 13:07:08 +05304744 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004745 /*
4746 * credits to insert 1 extent into extent tree
4747 */
4748 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4749 }
Mingming Cao00314622009-09-28 15:49:08 -04004750 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004751 map.m_lblk += ret;
4752 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04004753 if (credits) {
4754 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4755 credits);
4756 if (IS_ERR(handle)) {
4757 ret = PTR_ERR(handle);
4758 break;
4759 }
Mingming Cao00314622009-09-28 15:49:08 -04004760 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004761 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004762 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05004763 if (ret <= 0)
4764 ext4_warning(inode->i_sb,
4765 "inode #%lu: block %u: len %u: "
4766 "ext4_ext_map_blocks returned %d",
4767 inode->i_ino, map.m_lblk,
4768 map.m_len, ret);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004769 ret2 = ext4_mark_inode_dirty(handle, inode);
4770 if (credits) {
4771 ret3 = ext4_journal_stop(handle);
4772 if (unlikely(ret3))
4773 ret2 = ret3;
4774 }
4775
Jan Kara6b523df2013-06-04 13:21:11 -04004776 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04004777 break;
4778 }
4779 return ret > 0 ? ret2 : ret;
4780}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004781
Ritesh Harjania00713e2019-10-16 13:07:08 +05304782int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
4783{
4784 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304785 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304786
4787 /*
4788 * This is somewhat ugly but the idea is clear: When transaction is
4789 * reserved, everything goes into it. Otherwise we rather start several
4790 * smaller transactions for conversion of each extent separately.
4791 */
4792 if (handle) {
4793 handle = ext4_journal_start_reserved(handle,
4794 EXT4_HT_EXT_CONVERT);
4795 if (IS_ERR(handle))
4796 return PTR_ERR(handle);
4797 }
4798
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304799 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
4800 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
4801 io_end_vec->offset,
4802 io_end_vec->size);
4803 if (ret)
4804 break;
4805 }
4806
Ritesh Harjania00713e2019-10-16 13:07:08 +05304807 if (handle)
4808 err = ext4_journal_stop(handle);
4809
4810 return ret < 0 ? ret : err;
4811}
4812
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304813static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004814{
4815 __u64 physical = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304816 __u64 length = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004817 int blockbits = inode->i_sb->s_blocksize_bits;
4818 int error = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304819 u16 iomap_type;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004820
4821 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004822 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004823 struct ext4_iloc iloc;
4824 int offset; /* offset of xattr in inode */
4825
4826 error = ext4_get_inode_loc(inode, &iloc);
4827 if (error)
4828 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04004829 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004830 offset = EXT4_GOOD_OLD_INODE_SIZE +
4831 EXT4_I(inode)->i_extra_isize;
4832 physical += offset;
4833 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004834 brelse(iloc.bh);
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304835 iomap_type = IOMAP_INLINE;
4836 } else if (EXT4_I(inode)->i_file_acl) { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04004837 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004838 length = inode->i_sb->s_blocksize;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304839 iomap_type = IOMAP_MAPPED;
4840 } else {
4841 /* no in-inode or external block for xattr, so return -ENOENT */
4842 error = -ENOENT;
4843 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004844 }
4845
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304846 iomap->addr = physical;
4847 iomap->offset = 0;
4848 iomap->length = length;
4849 iomap->type = iomap_type;
4850 iomap->flags = 0;
4851out:
4852 return error;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004853}
4854
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304855static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset,
4856 loff_t length, unsigned flags,
4857 struct iomap *iomap, struct iomap *srcmap)
4858{
4859 int error;
4860
4861 error = ext4_iomap_xattr_fiemap(inode, iomap);
4862 if (error == 0 && (offset >= iomap->length))
4863 error = -ENOENT;
4864 return error;
4865}
4866
4867static const struct iomap_ops ext4_iomap_xattr_ops = {
4868 .iomap_begin = ext4_iomap_xattr_begin,
4869};
4870
4871static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4872 __u64 start, __u64 len, bool from_es_cache)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004873{
4874 ext4_lblk_t start_blk;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304875 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004876 int error = 0;
4877
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004878 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
4879 error = ext4_ext_precache(inode);
4880 if (error)
4881 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004882 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004883 }
4884
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304885 if (from_es_cache)
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004886 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304887
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004888 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004889 return -EBADR;
4890
4891 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304892 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
4893 error = iomap_fiemap(inode, fieinfo, start, len,
4894 &ext4_iomap_xattr_ops);
4895 } else if (!from_es_cache) {
4896 error = iomap_fiemap(inode, fieinfo, start, len,
4897 &ext4_iomap_report_ops);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004898 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004899 ext4_lblk_t len_blks;
4900 __u64 last_blk;
4901
Eric Sandeen6873fa02008-10-07 00:46:36 -04004902 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004903 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004904 if (last_blk >= EXT_MAX_BLOCKS)
4905 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004906 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004907
4908 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004909 * Walk the extent tree gathering extent information
4910 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004911 */
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304912 error = ext4_fill_es_cache_info(inode, start_blk, len_blks,
4913 fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004914 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04004915 return error;
4916}
Namjae Jeon9eb79482014-02-23 15:18:59 -05004917
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004918int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4919 __u64 start, __u64 len)
4920{
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304921 return _ext4_fiemap(inode, fieinfo, start, len, false);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004922}
4923
4924int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
4925 __u64 start, __u64 len)
4926{
4927 if (ext4_has_inline_data(inode)) {
4928 int has_inline;
4929
4930 down_read(&EXT4_I(inode)->xattr_sem);
4931 has_inline = ext4_has_inline_data(inode);
4932 up_read(&EXT4_I(inode)->xattr_sem);
4933 if (has_inline)
4934 return 0;
4935 }
4936
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304937 return _ext4_fiemap(inode, fieinfo, start, len, true);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004938}
4939
4940
Namjae Jeon9eb79482014-02-23 15:18:59 -05004941/*
4942 * ext4_access_path:
4943 * Function to access the path buffer for marking it dirty.
4944 * It also checks if there are sufficient credits left in the journal handle
4945 * to update path.
4946 */
4947static int
4948ext4_access_path(handle_t *handle, struct inode *inode,
4949 struct ext4_ext_path *path)
4950{
4951 int credits, err;
4952
4953 if (!ext4_handle_valid(handle))
4954 return 0;
4955
4956 /*
4957 * Check if need to extend journal credits
4958 * 3 for leaf, sb, and inode plus 2 (bmap and group
4959 * descriptor) for each block group; assume two block
4960 * groups
4961 */
Jan Karaa4130362019-11-05 17:44:16 +01004962 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01004963 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01004964 if (err < 0)
4965 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004966
4967 err = ext4_ext_get_access(handle, inode, path);
4968 return err;
4969}
4970
4971/*
4972 * ext4_ext_shift_path_extents:
4973 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04004974 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
4975 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05004976 */
4977static int
4978ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
4979 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04004980 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05004981{
4982 int depth, err = 0;
4983 struct ext4_extent *ex_start, *ex_last;
zhengbin4756ee12019-12-25 10:45:59 +08004984 bool update = false;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004985 depth = path->p_depth;
4986
4987 while (depth >= 0) {
4988 if (depth == path->p_depth) {
4989 ex_start = path[depth].p_ext;
4990 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004991 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004992
4993 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05004994
4995 err = ext4_access_path(handle, inode, path + depth);
4996 if (err)
4997 goto out;
4998
4999 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
zhengbin4756ee12019-12-25 10:45:59 +08005000 update = true;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005001
Namjae Jeon9eb79482014-02-23 15:18:59 -05005002 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005003 if (SHIFT == SHIFT_LEFT) {
5004 le32_add_cpu(&ex_start->ee_block,
5005 -shift);
5006 /* Try to merge to the left. */
5007 if ((ex_start >
5008 EXT_FIRST_EXTENT(path[depth].p_hdr))
5009 &&
5010 ext4_ext_try_to_merge_right(inode,
5011 path, ex_start - 1))
5012 ex_last--;
5013 else
5014 ex_start++;
5015 } else {
5016 le32_add_cpu(&ex_last->ee_block, shift);
5017 ext4_ext_try_to_merge_right(inode, path,
5018 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005019 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005020 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005021 }
5022 err = ext4_ext_dirty(handle, inode, path + depth);
5023 if (err)
5024 goto out;
5025
5026 if (--depth < 0 || !update)
5027 break;
5028 }
5029
5030 /* Update index too */
5031 err = ext4_access_path(handle, inode, path + depth);
5032 if (err)
5033 goto out;
5034
Namjae Jeon331573f2015-06-09 01:55:03 -04005035 if (SHIFT == SHIFT_LEFT)
5036 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5037 else
5038 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005039 err = ext4_ext_dirty(handle, inode, path + depth);
5040 if (err)
5041 goto out;
5042
5043 /* we are done if current index is not a starting index */
5044 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5045 break;
5046
5047 depth--;
5048 }
5049
5050out:
5051 return err;
5052}
5053
5054/*
5055 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005056 * All the extents which lies in the range from @start to the last allocated
5057 * block for the @inode are shifted either towards left or right (depending
5058 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005059 * On success, 0 is returned, error otherwise.
5060 */
5061static int
5062ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005063 ext4_lblk_t start, ext4_lblk_t shift,
5064 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005065{
5066 struct ext4_ext_path *path;
5067 int ret = 0, depth;
5068 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005069 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005070
5071 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005072 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5073 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005074 if (IS_ERR(path))
5075 return PTR_ERR(path);
5076
5077 depth = path->p_depth;
5078 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005079 if (!extent)
5080 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005081
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005082 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005083
Namjae Jeon331573f2015-06-09 01:55:03 -04005084 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005085 * For left shifts, make sure the hole on the left is big enough to
5086 * accommodate the shift. For right shifts, make sure the last extent
5087 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005088 */
5089 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005090 path = ext4_find_extent(inode, start - 1, &path,
5091 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005092 if (IS_ERR(path))
5093 return PTR_ERR(path);
5094 depth = path->p_depth;
5095 extent = path[depth].p_ext;
5096 if (extent) {
5097 ex_start = le32_to_cpu(extent->ee_block);
5098 ex_end = le32_to_cpu(extent->ee_block) +
5099 ext4_ext_get_actual_len(extent);
5100 } else {
5101 ex_start = 0;
5102 ex_end = 0;
5103 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005104
Namjae Jeon331573f2015-06-09 01:55:03 -04005105 if ((start == ex_start && shift > ex_start) ||
5106 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005107 ret = -EINVAL;
5108 goto out;
5109 }
5110 } else {
5111 if (shift > EXT_MAX_BLOCKS -
5112 (stop + ext4_ext_get_actual_len(extent))) {
5113 ret = -EINVAL;
5114 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005115 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005116 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005117
Namjae Jeon331573f2015-06-09 01:55:03 -04005118 /*
5119 * In case of left shift, iterator points to start and it is increased
5120 * till we reach stop. In case of right shift, iterator points to stop
5121 * and it is decreased till we reach start.
5122 */
5123 if (SHIFT == SHIFT_LEFT)
5124 iterator = &start;
5125 else
5126 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005127
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005128 /*
5129 * Its safe to start updating extents. Start and stop are unsigned, so
5130 * in case of right shift if extent with 0 block is reached, iterator
5131 * becomes NULL to indicate the end of the loop.
5132 */
5133 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005134 path = ext4_find_extent(inode, *iterator, &path,
5135 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005136 if (IS_ERR(path))
5137 return PTR_ERR(path);
5138 depth = path->p_depth;
5139 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005140 if (!extent) {
5141 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005142 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005143 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005144 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005145 if (SHIFT == SHIFT_LEFT && *iterator >
5146 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005147 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005148 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5149 path[depth].p_ext++;
5150 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005151 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005152 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005153 }
5154 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005155
5156 if (SHIFT == SHIFT_LEFT) {
5157 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5158 *iterator = le32_to_cpu(extent->ee_block) +
5159 ext4_ext_get_actual_len(extent);
5160 } else {
5161 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005162 if (le32_to_cpu(extent->ee_block) > 0)
5163 *iterator = le32_to_cpu(extent->ee_block) - 1;
5164 else
5165 /* Beginning is reached, end of the loop */
5166 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005167 /* Update path extent in case we need to stop */
5168 while (le32_to_cpu(extent->ee_block) < start)
5169 extent++;
5170 path[depth].p_ext = extent;
5171 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005172 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005173 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005174 if (ret)
5175 break;
5176 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005177out:
5178 ext4_ext_drop_refs(path);
5179 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005180 return ret;
5181}
5182
5183/*
5184 * ext4_collapse_range:
5185 * This implements the fallocate's collapse range functionality for ext4
5186 * Returns: 0 and non-zero on error.
5187 */
Eric Biggers43f81672019-12-31 12:04:40 -06005188static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005189{
5190 struct super_block *sb = inode->i_sb;
5191 ext4_lblk_t punch_start, punch_stop;
5192 handle_t *handle;
5193 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005194 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005195 int ret;
5196
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005197 /*
5198 * We need to test this early because xfstests assumes that a
5199 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5200 * system does not support collapse range.
5201 */
5202 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5203 return -EOPNOTSUPP;
5204
Eric Biggers9b02e492019-12-31 12:04:38 -06005205 /* Collapse range works only on fs cluster size aligned regions. */
5206 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005207 return -EINVAL;
5208
Namjae Jeon9eb79482014-02-23 15:18:59 -05005209 trace_ext4_collapse_range(inode, offset, len);
5210
5211 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5212 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5213
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005214 /* Call ext4_force_commit to flush all data in case of data=journal. */
5215 if (ext4_should_journal_data(inode)) {
5216 ret = ext4_force_commit(inode->i_sb);
5217 if (ret)
5218 return ret;
5219 }
5220
Al Viro59551022016-01-22 15:40:57 -05005221 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005222 /*
5223 * There is no need to overlap collapse range with EOF, in which case
5224 * it is effectively a truncate operation
5225 */
Eric Biggers9b02e492019-12-31 12:04:38 -06005226 if (offset + len >= inode->i_size) {
Lukas Czerner23fffa92014-04-12 09:56:41 -04005227 ret = -EINVAL;
5228 goto out_mutex;
5229 }
5230
Namjae Jeon9eb79482014-02-23 15:18:59 -05005231 /* Currently just for extent based files */
5232 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5233 ret = -EOPNOTSUPP;
5234 goto out_mutex;
5235 }
5236
Namjae Jeon9eb79482014-02-23 15:18:59 -05005237 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005238 inode_dio_wait(inode);
5239
Jan Karaea3d7202015-12-07 14:28:03 -05005240 /*
5241 * Prevent page faults from reinstantiating pages we have released from
5242 * page cache.
5243 */
5244 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005245
5246 ret = ext4_break_layouts(inode);
5247 if (ret)
5248 goto out_mmap;
5249
Jan Kara32ebffd2015-12-07 14:31:11 -05005250 /*
5251 * Need to round down offset to be aligned with page size boundary
5252 * for page size > block size.
5253 */
5254 ioffset = round_down(offset, PAGE_SIZE);
5255 /*
5256 * Write tail of the last page before removed range since it will get
5257 * removed from the page cache below.
5258 */
5259 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5260 if (ret)
5261 goto out_mmap;
5262 /*
5263 * Write data that will be shifted to preserve them when discarding
5264 * page cache below. We are also protected from pages becoming dirty
5265 * by i_mmap_sem.
5266 */
5267 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5268 LLONG_MAX);
5269 if (ret)
5270 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005271 truncate_pagecache(inode, ioffset);
5272
Namjae Jeon9eb79482014-02-23 15:18:59 -05005273 credits = ext4_writepage_trans_blocks(inode);
5274 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5275 if (IS_ERR(handle)) {
5276 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005277 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005278 }
5279
5280 down_write(&EXT4_I(inode)->i_data_sem);
5281 ext4_discard_preallocations(inode);
5282
5283 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005284 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005285 if (ret) {
5286 up_write(&EXT4_I(inode)->i_data_sem);
5287 goto out_stop;
5288 }
5289
5290 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5291 if (ret) {
5292 up_write(&EXT4_I(inode)->i_data_sem);
5293 goto out_stop;
5294 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005295 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005296
5297 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005298 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005299 if (ret) {
5300 up_write(&EXT4_I(inode)->i_data_sem);
5301 goto out_stop;
5302 }
5303
Eric Biggers9b02e492019-12-31 12:04:38 -06005304 new_size = inode->i_size - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005305 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005306 EXT4_I(inode)->i_disksize = new_size;
5307
Namjae Jeon9eb79482014-02-23 15:18:59 -05005308 up_write(&EXT4_I(inode)->i_data_sem);
5309 if (IS_SYNC(inode))
5310 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005311 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005312 ret = ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005313 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005314
5315out_stop:
5316 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005317out_mmap:
5318 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005319out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005320 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005321 return ret;
5322}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005323
Namjae Jeon331573f2015-06-09 01:55:03 -04005324/*
5325 * ext4_insert_range:
5326 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5327 * The data blocks starting from @offset to the EOF are shifted by @len
5328 * towards right to create a hole in the @inode. Inode size is increased
5329 * by len bytes.
5330 * Returns 0 on success, error otherwise.
5331 */
Eric Biggers43f81672019-12-31 12:04:40 -06005332static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon331573f2015-06-09 01:55:03 -04005333{
5334 struct super_block *sb = inode->i_sb;
5335 handle_t *handle;
5336 struct ext4_ext_path *path;
5337 struct ext4_extent *extent;
5338 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5339 unsigned int credits, ee_len;
5340 int ret = 0, depth, split_flag = 0;
5341 loff_t ioffset;
5342
5343 /*
5344 * We need to test this early because xfstests assumes that an
5345 * insert range of (0, 1) will return EOPNOTSUPP if the file
5346 * system does not support insert range.
5347 */
5348 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5349 return -EOPNOTSUPP;
5350
Eric Biggers9b02e492019-12-31 12:04:38 -06005351 /* Insert range works only on fs cluster size aligned regions. */
5352 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon331573f2015-06-09 01:55:03 -04005353 return -EINVAL;
5354
Namjae Jeon331573f2015-06-09 01:55:03 -04005355 trace_ext4_insert_range(inode, offset, len);
5356
5357 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5358 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5359
5360 /* Call ext4_force_commit to flush all data in case of data=journal */
5361 if (ext4_should_journal_data(inode)) {
5362 ret = ext4_force_commit(inode->i_sb);
5363 if (ret)
5364 return ret;
5365 }
5366
Al Viro59551022016-01-22 15:40:57 -05005367 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005368 /* Currently just for extent based files */
5369 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5370 ret = -EOPNOTSUPP;
5371 goto out_mutex;
5372 }
5373
Eric Biggers9b02e492019-12-31 12:04:38 -06005374 /* Check whether the maximum file size would be exceeded */
5375 if (len > inode->i_sb->s_maxbytes - inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005376 ret = -EFBIG;
5377 goto out_mutex;
5378 }
5379
Eric Biggers9b02e492019-12-31 12:04:38 -06005380 /* Offset must be less than i_size */
5381 if (offset >= inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005382 ret = -EINVAL;
5383 goto out_mutex;
5384 }
5385
Namjae Jeon331573f2015-06-09 01:55:03 -04005386 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005387 inode_dio_wait(inode);
5388
Jan Karaea3d7202015-12-07 14:28:03 -05005389 /*
5390 * Prevent page faults from reinstantiating pages we have released from
5391 * page cache.
5392 */
5393 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005394
5395 ret = ext4_break_layouts(inode);
5396 if (ret)
5397 goto out_mmap;
5398
Jan Kara32ebffd2015-12-07 14:31:11 -05005399 /*
5400 * Need to round down to align start offset to page size boundary
5401 * for page size > block size.
5402 */
5403 ioffset = round_down(offset, PAGE_SIZE);
5404 /* Write out all dirty pages */
5405 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5406 LLONG_MAX);
5407 if (ret)
5408 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005409 truncate_pagecache(inode, ioffset);
5410
Namjae Jeon331573f2015-06-09 01:55:03 -04005411 credits = ext4_writepage_trans_blocks(inode);
5412 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5413 if (IS_ERR(handle)) {
5414 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005415 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005416 }
5417
5418 /* Expand file to avoid data loss if there is error while shifting */
5419 inode->i_size += len;
5420 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005421 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005422 ret = ext4_mark_inode_dirty(handle, inode);
5423 if (ret)
5424 goto out_stop;
5425
5426 down_write(&EXT4_I(inode)->i_data_sem);
5427 ext4_discard_preallocations(inode);
5428
5429 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5430 if (IS_ERR(path)) {
5431 up_write(&EXT4_I(inode)->i_data_sem);
5432 goto out_stop;
5433 }
5434
5435 depth = ext_depth(inode);
5436 extent = path[depth].p_ext;
5437 if (extent) {
5438 ee_start_lblk = le32_to_cpu(extent->ee_block);
5439 ee_len = ext4_ext_get_actual_len(extent);
5440
5441 /*
5442 * If offset_lblk is not the starting block of extent, split
5443 * the extent @offset_lblk
5444 */
5445 if ((offset_lblk > ee_start_lblk) &&
5446 (offset_lblk < (ee_start_lblk + ee_len))) {
5447 if (ext4_ext_is_unwritten(extent))
5448 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5449 EXT4_EXT_MARK_UNWRIT2;
5450 ret = ext4_split_extent_at(handle, inode, &path,
5451 offset_lblk, split_flag,
5452 EXT4_EX_NOCACHE |
5453 EXT4_GET_BLOCKS_PRE_IO |
5454 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5455 }
5456
5457 ext4_ext_drop_refs(path);
5458 kfree(path);
5459 if (ret < 0) {
5460 up_write(&EXT4_I(inode)->i_data_sem);
5461 goto out_stop;
5462 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005463 } else {
5464 ext4_ext_drop_refs(path);
5465 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005466 }
5467
5468 ret = ext4_es_remove_extent(inode, offset_lblk,
5469 EXT_MAX_BLOCKS - offset_lblk);
5470 if (ret) {
5471 up_write(&EXT4_I(inode)->i_data_sem);
5472 goto out_stop;
5473 }
5474
5475 /*
5476 * if offset_lblk lies in a hole which is at start of file, use
5477 * ee_start_lblk to shift extents
5478 */
5479 ret = ext4_ext_shift_extents(inode, handle,
5480 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5481 len_lblk, SHIFT_RIGHT);
5482
5483 up_write(&EXT4_I(inode)->i_data_sem);
5484 if (IS_SYNC(inode))
5485 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005486 if (ret >= 0)
5487 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005488
5489out_stop:
5490 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005491out_mmap:
5492 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005493out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005494 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005495 return ret;
5496}
5497
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005498/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005499 * ext4_swap_extents() - Swap extents between two inodes
5500 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005501 * @inode1: First inode
5502 * @inode2: Second inode
5503 * @lblk1: Start block for first inode
5504 * @lblk2: Start block for second inode
5505 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005506 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005507 * @erp: Pointer to save error value
5508 *
5509 * This helper routine does exactly what is promise "swap extents". All other
5510 * stuff such as page-cache locking consistency, bh mapping consistency or
5511 * extent's data copying must be performed by caller.
5512 * Locking:
5513 * i_mutex is held for both inodes
5514 * i_data_sem is locked for write for both inodes
5515 * Assumptions:
5516 * All pages from requested range are locked for both inodes
5517 */
5518int
5519ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005520 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005521 ext4_lblk_t count, int unwritten, int *erp)
5522{
5523 struct ext4_ext_path *path1 = NULL;
5524 struct ext4_ext_path *path2 = NULL;
5525 int replaced_count = 0;
5526
5527 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5528 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005529 BUG_ON(!inode_is_locked(inode1));
5530 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005531
5532 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005533 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005534 return 0;
5535 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005536 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005537 return 0;
5538
5539 while (count) {
5540 struct ext4_extent *ex1, *ex2, tmp_ex;
5541 ext4_lblk_t e1_blk, e2_blk;
5542 int e1_len, e2_len, len;
5543 int split = 0;
5544
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005545 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305546 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005547 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005548 path1 = NULL;
5549 finish:
5550 count = 0;
5551 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005552 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005553 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305554 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005555 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005556 path2 = NULL;
5557 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005558 }
5559 ex1 = path1[path1->p_depth].p_ext;
5560 ex2 = path2[path2->p_depth].p_ext;
5561 /* Do we have somthing to swap ? */
5562 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005563 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005564
5565 e1_blk = le32_to_cpu(ex1->ee_block);
5566 e2_blk = le32_to_cpu(ex2->ee_block);
5567 e1_len = ext4_ext_get_actual_len(ex1);
5568 e2_len = ext4_ext_get_actual_len(ex2);
5569
5570 /* Hole handling */
5571 if (!in_range(lblk1, e1_blk, e1_len) ||
5572 !in_range(lblk2, e2_blk, e2_len)) {
5573 ext4_lblk_t next1, next2;
5574
5575 /* if hole after extent, then go to next extent */
5576 next1 = ext4_ext_next_allocated_block(path1);
5577 next2 = ext4_ext_next_allocated_block(path2);
5578 /* If hole before extent, then shift to that extent */
5579 if (e1_blk > lblk1)
5580 next1 = e1_blk;
5581 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005582 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005583 /* Do we have something to swap */
5584 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005585 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005586 /* Move to the rightest boundary */
5587 len = next1 - lblk1;
5588 if (len < next2 - lblk2)
5589 len = next2 - lblk2;
5590 if (len > count)
5591 len = count;
5592 lblk1 += len;
5593 lblk2 += len;
5594 count -= len;
5595 goto repeat;
5596 }
5597
5598 /* Prepare left boundary */
5599 if (e1_blk < lblk1) {
5600 split = 1;
5601 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005602 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005603 if (unlikely(*erp))
5604 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005605 }
5606 if (e2_blk < lblk2) {
5607 split = 1;
5608 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005609 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005610 if (unlikely(*erp))
5611 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005612 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005613 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005614 * path must to be revalidated. */
5615 if (split)
5616 goto repeat;
5617
5618 /* Prepare right boundary */
5619 len = count;
5620 if (len > e1_blk + e1_len - lblk1)
5621 len = e1_blk + e1_len - lblk1;
5622 if (len > e2_blk + e2_len - lblk2)
5623 len = e2_blk + e2_len - lblk2;
5624
5625 if (len != e1_len) {
5626 split = 1;
5627 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005628 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005629 if (unlikely(*erp))
5630 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005631 }
5632 if (len != e2_len) {
5633 split = 1;
5634 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005635 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005636 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005637 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005638 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005639 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005640 * path must to be revalidated. */
5641 if (split)
5642 goto repeat;
5643
5644 BUG_ON(e2_len != e1_len);
5645 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005646 if (unlikely(*erp))
5647 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005648 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005649 if (unlikely(*erp))
5650 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005651
5652 /* Both extents are fully inside boundaries. Swap it now */
5653 tmp_ex = *ex1;
5654 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5655 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5656 ex1->ee_len = cpu_to_le16(e2_len);
5657 ex2->ee_len = cpu_to_le16(e1_len);
5658 if (unwritten)
5659 ext4_ext_mark_unwritten(ex2);
5660 if (ext4_ext_is_unwritten(&tmp_ex))
5661 ext4_ext_mark_unwritten(ex1);
5662
5663 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5664 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5665 *erp = ext4_ext_dirty(handle, inode2, path2 +
5666 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005667 if (unlikely(*erp))
5668 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005669 *erp = ext4_ext_dirty(handle, inode1, path1 +
5670 path1->p_depth);
5671 /*
5672 * Looks scarry ah..? second inode already points to new blocks,
5673 * and it was successfully dirtied. But luckily error may happen
5674 * only due to journal error, so full transaction will be
5675 * aborted anyway.
5676 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005677 if (unlikely(*erp))
5678 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005679 lblk1 += len;
5680 lblk2 += len;
5681 replaced_count += len;
5682 count -= len;
5683
5684 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005685 ext4_ext_drop_refs(path1);
5686 kfree(path1);
5687 ext4_ext_drop_refs(path2);
5688 kfree(path2);
5689 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005690 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005691 return replaced_count;
5692}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005693
5694/*
5695 * ext4_clu_mapped - determine whether any block in a logical cluster has
5696 * been mapped to a physical cluster
5697 *
5698 * @inode - file containing the logical cluster
5699 * @lclu - logical cluster of interest
5700 *
5701 * Returns 1 if any block in the logical cluster is mapped, signifying
5702 * that a physical cluster has been allocated for it. Otherwise,
5703 * returns 0. Can also return negative error codes. Derived from
5704 * ext4_ext_map_blocks().
5705 */
5706int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5707{
5708 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5709 struct ext4_ext_path *path;
5710 int depth, mapped = 0, err = 0;
5711 struct ext4_extent *extent;
5712 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5713
5714 /* search for the extent closest to the first block in the cluster */
5715 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5716 if (IS_ERR(path)) {
5717 err = PTR_ERR(path);
5718 path = NULL;
5719 goto out;
5720 }
5721
5722 depth = ext_depth(inode);
5723
5724 /*
5725 * A consistent leaf must not be empty. This situation is possible,
5726 * though, _during_ tree modification, and it's why an assert can't
5727 * be put in ext4_find_extent().
5728 */
5729 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
5730 EXT4_ERROR_INODE(inode,
5731 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
5732 (unsigned long) EXT4_C2B(sbi, lclu),
5733 depth, path[depth].p_block);
5734 err = -EFSCORRUPTED;
5735 goto out;
5736 }
5737
5738 extent = path[depth].p_ext;
5739
5740 /* can't be mapped if the extent tree is empty */
5741 if (extent == NULL)
5742 goto out;
5743
5744 first_lblk = le32_to_cpu(extent->ee_block);
5745 first_lclu = EXT4_B2C(sbi, first_lblk);
5746
5747 /*
5748 * Three possible outcomes at this point - found extent spanning
5749 * the target cluster, to the left of the target cluster, or to the
5750 * right of the target cluster. The first two cases are handled here.
5751 * The last case indicates the target cluster is not mapped.
5752 */
5753 if (lclu >= first_lclu) {
5754 last_lclu = EXT4_B2C(sbi, first_lblk +
5755 ext4_ext_get_actual_len(extent) - 1);
5756 if (lclu <= last_lclu) {
5757 mapped = 1;
5758 } else {
5759 first_lblk = ext4_ext_next_allocated_block(path);
5760 first_lclu = EXT4_B2C(sbi, first_lblk);
5761 if (lclu == first_lclu)
5762 mapped = 1;
5763 }
5764 }
5765
5766out:
5767 ext4_ext_drop_refs(path);
5768 kfree(path);
5769
5770 return err ? err : mapped;
5771}