blob: 6befdecf977a2187d41c6b78a4baa203bf7ae084 [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);
300
Theodore Ts'odfe50802014-09-01 14:37:09 -0400301 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400302 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
303 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
304 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
305}
306
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400307static int
308ext4_ext_max_entries(struct inode *inode, int depth)
309{
310 int max;
311
312 if (depth == ext_depth(inode)) {
313 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400314 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400315 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400316 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400317 } else {
318 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400319 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400320 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400321 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400322 }
323
324 return max;
325}
326
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400327static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
328{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400329 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400330 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500331 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400332
Vegard Nossumf70749c2016-06-30 11:53:46 -0400333 /*
334 * We allow neither:
335 * - zero length
336 * - overflow/wrap-around
337 */
338 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400339 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400340 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400341}
342
343static int ext4_valid_extent_idx(struct inode *inode,
344 struct ext4_extent_idx *ext_idx)
345{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400346 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400347
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400348 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400349}
350
351static int ext4_valid_extent_entries(struct inode *inode,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400352 struct ext4_extent_header *eh,
353 ext4_fsblk_t *pblk, int depth)
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400354{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400355 unsigned short entries;
356 if (eh->eh_entries == 0)
357 return 1;
358
359 entries = le16_to_cpu(eh->eh_entries);
360
361 if (depth == 0) {
362 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400363 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500364 ext4_lblk_t lblock = 0;
365 ext4_lblk_t prev = 0;
366 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400367 while (entries) {
368 if (!ext4_valid_extent(inode, ext))
369 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500370
371 /* Check for overlapping extents */
372 lblock = le32_to_cpu(ext->ee_block);
373 len = ext4_ext_get_actual_len(ext);
374 if ((lblock <= prev) && prev) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400375 *pblk = ext4_ext_pblock(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500376 return 0;
377 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400378 ext++;
379 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500380 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400381 }
382 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400383 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400384 while (entries) {
385 if (!ext4_valid_extent_idx(inode, ext_idx))
386 return 0;
387 ext_idx++;
388 entries--;
389 }
390 }
391 return 1;
392}
393
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400394static int __ext4_ext_check(const char *function, unsigned int line,
395 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400396 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400397{
398 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400399 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400400
401 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
402 error_msg = "invalid magic";
403 goto corrupted;
404 }
405 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
406 error_msg = "unexpected eh_depth";
407 goto corrupted;
408 }
409 if (unlikely(eh->eh_max == 0)) {
410 error_msg = "invalid eh_max";
411 goto corrupted;
412 }
413 max = ext4_ext_max_entries(inode, depth);
414 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
415 error_msg = "too large eh_max";
416 goto corrupted;
417 }
418 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
419 error_msg = "invalid eh_entries";
420 goto corrupted;
421 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400422 if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) {
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400423 error_msg = "invalid extent entries";
424 goto corrupted;
425 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400426 if (unlikely(depth > 32)) {
427 error_msg = "too large eh_depth";
428 goto corrupted;
429 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400430 /* Verify checksum on non-root extent tree nodes */
431 if (ext_depth(inode) != depth &&
432 !ext4_extent_block_csum_verify(inode, eh)) {
433 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400434 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400435 goto corrupted;
436 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400437 return 0;
438
439corrupted:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400440 ext4_error_inode_err(inode, function, line, 0, -err,
441 "pblk %llu bad header/extent: %s - magic %x, "
442 "entries %u, max %u(%u), depth %u(%u)",
443 (unsigned long long) pblk, error_msg,
444 le16_to_cpu(eh->eh_magic),
445 le16_to_cpu(eh->eh_entries),
446 le16_to_cpu(eh->eh_max),
447 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400448 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400449}
450
Theodore Ts'oc3491792013-08-16 21:21:41 -0400451#define ext4_ext_check(inode, eh, depth, pblk) \
452 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400453
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400454int ext4_ext_check_inode(struct inode *inode)
455{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400456 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400457}
458
Dmitry Monakhov40686642019-11-06 12:25:02 +0000459static void ext4_cache_extents(struct inode *inode,
460 struct ext4_extent_header *eh)
461{
462 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
463 ext4_lblk_t prev = 0;
464 int i;
465
466 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
467 unsigned int status = EXTENT_STATUS_WRITTEN;
468 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
469 int len = ext4_ext_get_actual_len(ex);
470
471 if (prev && (prev != lblk))
472 ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
473 EXTENT_STATUS_HOLE);
474
475 if (ext4_ext_is_unwritten(ex))
476 status = EXTENT_STATUS_UNWRITTEN;
477 ext4_es_cache_extent(inode, lblk, len,
478 ext4_ext_pblock(ex), status);
479 prev = lblk + len;
480 }
481}
482
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400483static struct buffer_head *
484__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400485 struct inode *inode, ext4_fsblk_t pblk, int depth,
486 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400487{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400488 struct buffer_head *bh;
489 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400490
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400491 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400492 if (unlikely(!bh))
493 return ERR_PTR(-ENOMEM);
494
495 if (!bh_uptodate_or_lock(bh)) {
496 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
497 err = bh_submit_read(bh);
498 if (err < 0)
499 goto errout;
500 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400501 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400502 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400503 if (!ext4_has_feature_journal(inode->i_sb) ||
504 (inode->i_ino !=
505 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
506 err = __ext4_ext_check(function, line, inode,
507 ext_block_hdr(bh), depth, pblk);
508 if (err)
509 goto errout;
510 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400511 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400512 /*
513 * If this is a leaf block, cache all of its entries
514 */
515 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
516 struct ext4_extent_header *eh = ext_block_hdr(bh);
Dmitry Monakhov40686642019-11-06 12:25:02 +0000517 ext4_cache_extents(inode, eh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400518 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400519 return bh;
520errout:
521 put_bh(bh);
522 return ERR_PTR(err);
523
Darrick J. Wongf8489122012-04-29 18:21:10 -0400524}
525
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400526#define read_extent_tree_block(inode, pblk, depth, flags) \
527 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
528 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400529
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400530/*
531 * This function is called to cache a file's extent information in the
532 * extent status tree
533 */
534int ext4_ext_precache(struct inode *inode)
535{
536 struct ext4_inode_info *ei = EXT4_I(inode);
537 struct ext4_ext_path *path = NULL;
538 struct buffer_head *bh;
539 int i = 0, depth, ret = 0;
540
541 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
542 return 0; /* not an extent-mapped inode */
543
544 down_read(&ei->i_data_sem);
545 depth = ext_depth(inode);
546
Ritesh Harjani2f424a52020-02-28 14:56:55 +0530547 /* Don't cache anything if there are no external extent blocks */
548 if (!depth) {
549 up_read(&ei->i_data_sem);
550 return ret;
551 }
552
Kees Cook6396bb22018-06-12 14:03:40 -0700553 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400554 GFP_NOFS);
555 if (path == NULL) {
556 up_read(&ei->i_data_sem);
557 return -ENOMEM;
558 }
559
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400560 path[0].p_hdr = ext_inode_hdr(inode);
561 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
562 if (ret)
563 goto out;
564 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
565 while (i >= 0) {
566 /*
567 * If this is a leaf block or we've reached the end of
568 * the index block, go up
569 */
570 if ((i == depth) ||
571 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
572 brelse(path[i].p_bh);
573 path[i].p_bh = NULL;
574 i--;
575 continue;
576 }
577 bh = read_extent_tree_block(inode,
578 ext4_idx_pblock(path[i].p_idx++),
579 depth - i - 1,
580 EXT4_EX_FORCE_CACHE);
581 if (IS_ERR(bh)) {
582 ret = PTR_ERR(bh);
583 break;
584 }
585 i++;
586 path[i].p_bh = bh;
587 path[i].p_hdr = ext_block_hdr(bh);
588 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
589 }
590 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
591out:
592 up_read(&ei->i_data_sem);
593 ext4_ext_drop_refs(path);
594 kfree(path);
595 return ret;
596}
597
Alex Tomasa86c6182006-10-11 01:21:03 -0700598#ifdef EXT_DEBUG
599static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
600{
601 int k, l = path->p_depth;
602
603 ext_debug("path:");
604 for (k = 0; k <= l; k++, path++) {
605 if (path->p_idx) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600606 ext_debug(" %d->%llu",
607 le32_to_cpu(path->p_idx->ei_block),
608 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700609 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400610 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700611 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400612 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400613 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400614 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700615 } else
616 ext_debug(" []");
617 }
618 ext_debug("\n");
619}
620
621static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
622{
623 int depth = ext_depth(inode);
624 struct ext4_extent_header *eh;
625 struct ext4_extent *ex;
626 int i;
627
628 if (!path)
629 return;
630
631 eh = path[depth].p_hdr;
632 ex = EXT_FIRST_EXTENT(eh);
633
Mingming553f9002009-09-18 13:34:55 -0400634 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
635
Alex Tomasa86c6182006-10-11 01:21:03 -0700636 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400637 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400638 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400639 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700640 }
641 ext_debug("\n");
642}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400643
644static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
645 ext4_fsblk_t newblock, int level)
646{
647 int depth = ext_depth(inode);
648 struct ext4_extent *ex;
649
650 if (depth != level) {
651 struct ext4_extent_idx *idx;
652 idx = path[level].p_idx;
653 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
654 ext_debug("%d: move %d:%llu in new index %llu\n", level,
655 le32_to_cpu(idx->ei_block),
656 ext4_idx_pblock(idx),
657 newblock);
658 idx++;
659 }
660
661 return;
662 }
663
664 ex = path[depth].p_ext;
665 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
666 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
667 le32_to_cpu(ex->ee_block),
668 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400669 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400670 ext4_ext_get_actual_len(ex),
671 newblock);
672 ex++;
673 }
674}
675
Alex Tomasa86c6182006-10-11 01:21:03 -0700676#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400677#define ext4_ext_show_path(inode, path)
678#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400679#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700680#endif
681
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500682void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700683{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400684 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700685
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400686 if (!path)
687 return;
688 depth = path->p_depth;
Eric Biggersde745482019-12-31 12:04:44 -0600689 for (i = 0; i <= depth; i++, path++) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700690 if (path->p_bh) {
691 brelse(path->p_bh);
692 path->p_bh = NULL;
693 }
Eric Biggersde745482019-12-31 12:04:44 -0600694 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700695}
696
697/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700698 * ext4_ext_binsearch_idx:
699 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400700 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700701 */
702static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500703ext4_ext_binsearch_idx(struct inode *inode,
704 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700705{
706 struct ext4_extent_header *eh = path->p_hdr;
707 struct ext4_extent_idx *r, *l, *m;
708
Alex Tomasa86c6182006-10-11 01:21:03 -0700709
Eric Sandeenbba90742008-01-28 23:58:27 -0500710 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700711
712 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400713 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700714 while (l <= r) {
715 m = l + (r - l) / 2;
716 if (block < le32_to_cpu(m->ei_block))
717 r = m - 1;
718 else
719 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400720 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
721 m, le32_to_cpu(m->ei_block),
722 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700723 }
724
725 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400726 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400727 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700728
729#ifdef CHECK_BINSEARCH
730 {
731 struct ext4_extent_idx *chix, *ix;
732 int k;
733
734 chix = ix = EXT_FIRST_INDEX(eh);
735 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600736 if (k != 0 && le32_to_cpu(ix->ei_block) <=
737 le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400738 printk(KERN_DEBUG "k=%d, ix=0x%p, "
739 "first=0x%p\n", k,
740 ix, EXT_FIRST_INDEX(eh));
741 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700742 le32_to_cpu(ix->ei_block),
743 le32_to_cpu(ix[-1].ei_block));
744 }
745 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400746 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700747 if (block < le32_to_cpu(ix->ei_block))
748 break;
749 chix = ix;
750 }
751 BUG_ON(chix != path->p_idx);
752 }
753#endif
754
755}
756
757/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700758 * ext4_ext_binsearch:
759 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400760 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700761 */
762static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500763ext4_ext_binsearch(struct inode *inode,
764 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700765{
766 struct ext4_extent_header *eh = path->p_hdr;
767 struct ext4_extent *r, *l, *m;
768
Alex Tomasa86c6182006-10-11 01:21:03 -0700769 if (eh->eh_entries == 0) {
770 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700771 * this leaf is empty:
772 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700773 */
774 return;
775 }
776
Eric Sandeenbba90742008-01-28 23:58:27 -0500777 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700778
779 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400780 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700781
782 while (l <= r) {
783 m = l + (r - l) / 2;
784 if (block < le32_to_cpu(m->ee_block))
785 r = m - 1;
786 else
787 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400788 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
789 m, le32_to_cpu(m->ee_block),
790 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700791 }
792
793 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400794 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400795 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400796 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400797 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400798 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700799
800#ifdef CHECK_BINSEARCH
801 {
802 struct ext4_extent *chex, *ex;
803 int k;
804
805 chex = ex = EXT_FIRST_EXTENT(eh);
806 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
807 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400808 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700809 if (block < le32_to_cpu(ex->ee_block))
810 break;
811 chex = ex;
812 }
813 BUG_ON(chex != path->p_ext);
814 }
815#endif
816
817}
818
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -0700819void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700820{
821 struct ext4_extent_header *eh;
822
823 eh = ext_inode_hdr(inode);
824 eh->eh_depth = 0;
825 eh->eh_entries = 0;
826 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400827 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700828 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700829}
830
831struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400832ext4_find_extent(struct inode *inode, ext4_lblk_t block,
833 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700834{
835 struct ext4_extent_header *eh;
836 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400837 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
838 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500839 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700840
841 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400842 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400843 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
844 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
845 depth);
846 ret = -EFSCORRUPTED;
847 goto err;
848 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700849
Theodore Ts'o10809df82014-09-01 14:40:09 -0400850 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400851 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400852 if (depth > path[0].p_maxdepth) {
853 kfree(path);
854 *orig_path = path = NULL;
855 }
856 }
857 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400858 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700859 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Alex Tomasa86c6182006-10-11 01:21:03 -0700860 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400861 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700862 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400863 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700864 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400866 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700867
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400868 i = depth;
Dmitry Monakhov40686642019-11-06 12:25:02 +0000869 if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
870 ext4_cache_extents(inode, eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700871 /* walk through the tree */
872 while (i) {
873 ext_debug("depth %d: num %d, max %d\n",
874 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400875
Alex Tomasa86c6182006-10-11 01:21:03 -0700876 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400877 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700878 path[ppos].p_depth = i;
879 path[ppos].p_ext = NULL;
880
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400881 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
882 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530883 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400884 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700885 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500886 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400887
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 eh = ext_block_hdr(bh);
889 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700890 path[ppos].p_bh = bh;
891 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700892 }
893
894 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 path[ppos].p_ext = NULL;
896 path[ppos].p_idx = NULL;
897
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 /* find extent */
899 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400900 /* if not an empty leaf */
901 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400902 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700903
904 ext4_ext_show_path(inode, path);
905
906 return path;
907
908err:
909 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400910 kfree(path);
911 if (orig_path)
912 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500913 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700914}
915
916/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700917 * ext4_ext_insert_index:
918 * insert new index [@logical;@ptr] into the block at @curp;
919 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400921static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
922 struct ext4_ext_path *curp,
923 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700924{
925 struct ext4_extent_idx *ix;
926 int len, err;
927
Avantika Mathur7e028972006-12-06 20:41:33 -0800928 err = ext4_ext_get_access(handle, inode, curp);
929 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 return err;
931
Frank Mayhar273df552010-03-02 11:46:09 -0500932 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
933 EXT4_ERROR_INODE(inode,
934 "logical %d == ei_block %d!",
935 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400936 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500937 }
Robin Dongd4620312011-07-17 23:43:42 -0400938
939 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
940 >= le16_to_cpu(curp->p_hdr->eh_max))) {
941 EXT4_ERROR_INODE(inode,
942 "eh_entries %d >= eh_max %d!",
943 le16_to_cpu(curp->p_hdr->eh_entries),
944 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400945 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400946 }
947
Alex Tomasa86c6182006-10-11 01:21:03 -0700948 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
949 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400950 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700951 ix = curp->p_idx + 1;
952 } else {
953 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400954 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700955 ix = curp->p_idx;
956 }
957
Eric Gouriou80e675f2011-10-27 11:52:18 -0400958 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
959 BUG_ON(len < 0);
960 if (len > 0) {
961 ext_debug("insert new index %d: "
962 "move %d indices from 0x%p to 0x%p\n",
963 logical, len, ix, ix + 1);
964 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
965 }
966
Tao Maf472e022011-10-17 10:13:46 -0400967 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
968 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400969 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -0400970 }
971
Alex Tomasa86c6182006-10-11 01:21:03 -0700972 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700973 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400974 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700975
Frank Mayhar273df552010-03-02 11:46:09 -0500976 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
977 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400978 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500979 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700980
981 err = ext4_ext_dirty(handle, inode, curp);
982 ext4_std_error(inode->i_sb, err);
983
984 return err;
985}
986
987/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700988 * ext4_ext_split:
989 * inserts new subtree into the path, using free index entry
990 * at depth @at:
991 * - allocates all needed blocks (new leaf and all intermediate index blocks)
992 * - makes decision where to split
993 * - moves remaining extents and index entries (right to the split point)
994 * into the newly allocated blocks
995 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700996 */
997static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400998 unsigned int flags,
999 struct ext4_ext_path *path,
1000 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001001{
1002 struct buffer_head *bh = NULL;
1003 int depth = ext_depth(inode);
1004 struct ext4_extent_header *neh;
1005 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001006 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001007 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001008 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001009 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001011 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001012
1013 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001014 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001015
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001016 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001017 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001018 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1019 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001020 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001021 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001022 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1023 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001024 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001025 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001026 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001027 } else {
1028 border = newext->ee_block;
1029 ext_debug("leaf will be added."
1030 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001031 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001032 }
1033
1034 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001035 * If error occurs, then we break processing
1036 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001037 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001038 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 */
1040
1041 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001042 * Get array to track all allocated blocks.
1043 * We need this to handle errors and free blocks
1044 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001045 */
Kees Cook6396bb22018-06-12 14:03:40 -07001046 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001047 if (!ablocks)
1048 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001049
1050 /* allocate all needed blocks */
1051 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1052 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001053 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001054 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001055 if (newblock == 0)
1056 goto cleanup;
1057 ablocks[a] = newblock;
1058 }
1059
1060 /* initialize new leaf */
1061 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001062 if (unlikely(newblock == 0)) {
1063 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001064 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001065 goto cleanup;
1066 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001067 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001068 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001069 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001070 goto cleanup;
1071 }
1072 lock_buffer(bh);
1073
Avantika Mathur7e028972006-12-06 20:41:33 -08001074 err = ext4_journal_get_create_access(handle, bh);
1075 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 goto cleanup;
1077
1078 neh = ext_block_hdr(bh);
1079 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001080 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001081 neh->eh_magic = EXT4_EXT_MAGIC;
1082 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001083
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001084 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001085 if (unlikely(path[depth].p_hdr->eh_entries !=
1086 path[depth].p_hdr->eh_max)) {
1087 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1088 path[depth].p_hdr->eh_entries,
1089 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001090 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001091 goto cleanup;
1092 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001093 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001094 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1095 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001096 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001097 struct ext4_extent *ex;
1098 ex = EXT_FIRST_EXTENT(neh);
1099 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001100 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001101 }
1102
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001103 /* zero out unused area in the extent block */
1104 ext_size = sizeof(struct ext4_extent_header) +
1105 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1106 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001107 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001108 set_buffer_uptodate(bh);
1109 unlock_buffer(bh);
1110
Frank Mayhar03901312009-01-07 00:06:22 -05001111 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001112 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001113 goto cleanup;
1114 brelse(bh);
1115 bh = NULL;
1116
1117 /* correct old leaf */
1118 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001119 err = ext4_ext_get_access(handle, inode, path + depth);
1120 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001121 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001122 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001123 err = ext4_ext_dirty(handle, inode, path + depth);
1124 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001125 goto cleanup;
1126
1127 }
1128
1129 /* create intermediate indexes */
1130 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001131 if (unlikely(k < 0)) {
1132 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001133 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001134 goto cleanup;
1135 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001136 if (k)
1137 ext_debug("create %d intermediate indices\n", k);
1138 /* insert new index into current index block */
1139 /* current depth stored in i var */
1140 i = depth - 1;
1141 while (k--) {
1142 oldblock = newblock;
1143 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001144 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001145 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001146 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001147 goto cleanup;
1148 }
1149 lock_buffer(bh);
1150
Avantika Mathur7e028972006-12-06 20:41:33 -08001151 err = ext4_journal_get_create_access(handle, bh);
1152 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001153 goto cleanup;
1154
1155 neh = ext_block_hdr(bh);
1156 neh->eh_entries = cpu_to_le16(1);
1157 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001158 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001159 neh->eh_depth = cpu_to_le16(depth - i);
1160 fidx = EXT_FIRST_INDEX(neh);
1161 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001162 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001163
Eric Sandeenbba90742008-01-28 23:58:27 -05001164 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1165 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001166
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001167 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001168 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1169 EXT_LAST_INDEX(path[i].p_hdr))) {
1170 EXT4_ERROR_INODE(inode,
1171 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1172 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001173 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001174 goto cleanup;
1175 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001176 /* start copy indexes */
1177 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1178 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1179 EXT_MAX_INDEX(path[i].p_hdr));
1180 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001181 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001182 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001184 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001185 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001186 /* zero out unused area in the extent block */
1187 ext_size = sizeof(struct ext4_extent_header) +
1188 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1189 memset(bh->b_data + ext_size, 0,
1190 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001191 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001192 set_buffer_uptodate(bh);
1193 unlock_buffer(bh);
1194
Frank Mayhar03901312009-01-07 00:06:22 -05001195 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001196 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001197 goto cleanup;
1198 brelse(bh);
1199 bh = NULL;
1200
1201 /* correct old index */
1202 if (m) {
1203 err = ext4_ext_get_access(handle, inode, path + i);
1204 if (err)
1205 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001206 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001207 err = ext4_ext_dirty(handle, inode, path + i);
1208 if (err)
1209 goto cleanup;
1210 }
1211
1212 i--;
1213 }
1214
1215 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001216 err = ext4_ext_insert_index(handle, inode, path + at,
1217 le32_to_cpu(border), newblock);
1218
1219cleanup:
1220 if (bh) {
1221 if (buffer_locked(bh))
1222 unlock_buffer(bh);
1223 brelse(bh);
1224 }
1225
1226 if (err) {
1227 /* free all allocated blocks in error case */
1228 for (i = 0; i < depth; i++) {
1229 if (!ablocks[i])
1230 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001231 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001232 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001233 }
1234 }
1235 kfree(ablocks);
1236
1237 return err;
1238}
1239
1240/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001241 * ext4_ext_grow_indepth:
1242 * implements tree growing procedure:
1243 * - allocates new block
1244 * - moves top-level data (index block or leaf) into the new block
1245 * - initializes new top-level, creating index that points to the
1246 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001247 */
1248static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001249 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001250{
Alex Tomasa86c6182006-10-11 01:21:03 -07001251 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001252 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001253 ext4_fsblk_t newblock, goal = 0;
1254 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001255 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001256 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001257
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001258 /* Try to prepend new index to old one */
1259 if (ext_depth(inode))
1260 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1261 if (goal > le32_to_cpu(es->s_first_data_block)) {
1262 flags |= EXT4_MB_HINT_TRY_GOAL;
1263 goal--;
1264 } else
1265 goal = ext4_inode_to_goal_block(inode);
1266 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1267 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001268 if (newblock == 0)
1269 return err;
1270
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001271 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001272 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001273 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001274 lock_buffer(bh);
1275
Avantika Mathur7e028972006-12-06 20:41:33 -08001276 err = ext4_journal_get_create_access(handle, bh);
1277 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001278 unlock_buffer(bh);
1279 goto out;
1280 }
1281
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001282 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001283 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001284 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1285 /* zero out unused area in the extent block */
1286 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001287
1288 /* set size of new block */
1289 neh = ext_block_hdr(bh);
1290 /* old root could have indexes or leaves
1291 * so calculate e_max right way */
1292 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001293 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001294 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001295 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001296 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001297 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001298 set_buffer_uptodate(bh);
1299 unlock_buffer(bh);
1300
Frank Mayhar03901312009-01-07 00:06:22 -05001301 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001302 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001303 goto out;
1304
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001305 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001306 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001307 neh->eh_entries = cpu_to_le16(1);
1308 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1309 if (neh->eh_depth == 0) {
1310 /* Root extent block becomes index block */
1311 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1312 EXT_FIRST_INDEX(neh)->ei_block =
1313 EXT_FIRST_EXTENT(neh)->ee_block;
1314 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001315 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001316 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001317 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001318 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001319
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001320 le16_add_cpu(&neh->eh_depth, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07001321 err = ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001322out:
1323 brelse(bh);
1324
1325 return err;
1326}
1327
1328/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001329 * ext4_ext_create_new_leaf:
1330 * finds empty index and adds new leaf.
1331 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001332 */
1333static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001334 unsigned int mb_flags,
1335 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001336 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001337 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001338{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001339 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001340 struct ext4_ext_path *curp;
1341 int depth, i, err = 0;
1342
1343repeat:
1344 i = depth = ext_depth(inode);
1345
1346 /* walk up to the tree and look for free index entry */
1347 curp = path + depth;
1348 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1349 i--;
1350 curp--;
1351 }
1352
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001353 /* we use already allocated block for index block,
1354 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001355 if (EXT_HAS_FREE_INDEX(curp)) {
1356 /* if we found index with free entry, then use that
1357 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001358 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001359 if (err)
1360 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001361
1362 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001363 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001364 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001365 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001366 if (IS_ERR(path))
1367 err = PTR_ERR(path);
1368 } else {
1369 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001370 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001371 if (err)
1372 goto out;
1373
1374 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001375 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001376 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001377 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001378 if (IS_ERR(path)) {
1379 err = PTR_ERR(path);
1380 goto out;
1381 }
1382
1383 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001384 * only first (depth 0 -> 1) produces free space;
1385 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001386 */
1387 depth = ext_depth(inode);
1388 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001389 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001390 goto repeat;
1391 }
1392 }
1393
1394out:
1395 return err;
1396}
1397
1398/*
Alex Tomas1988b512008-01-28 23:58:27 -05001399 * search the closest allocated block to the left for *logical
1400 * and returns it at @logical + it's physical address at @phys
1401 * if *logical is the smallest allocated block, the function
1402 * returns 0 at @phys
1403 * return value contains 0 (success) or error code
1404 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001405static int ext4_ext_search_left(struct inode *inode,
1406 struct ext4_ext_path *path,
1407 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001408{
1409 struct ext4_extent_idx *ix;
1410 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001411 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001412
Frank Mayhar273df552010-03-02 11:46:09 -05001413 if (unlikely(path == NULL)) {
1414 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001415 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001416 }
Alex Tomas1988b512008-01-28 23:58:27 -05001417 depth = path->p_depth;
1418 *phys = 0;
1419
1420 if (depth == 0 && path->p_ext == NULL)
1421 return 0;
1422
1423 /* usually extent in the path covers blocks smaller
1424 * then *logical, but it can be that extent is the
1425 * first one in the file */
1426
1427 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001428 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001429 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001430 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1431 EXT4_ERROR_INODE(inode,
1432 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1433 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001434 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001435 }
Alex Tomas1988b512008-01-28 23:58:27 -05001436 while (--depth >= 0) {
1437 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001438 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1439 EXT4_ERROR_INODE(inode,
1440 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001441 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001442 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001443 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001444 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001445 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001446 }
Alex Tomas1988b512008-01-28 23:58:27 -05001447 }
1448 return 0;
1449 }
1450
Frank Mayhar273df552010-03-02 11:46:09 -05001451 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1452 EXT4_ERROR_INODE(inode,
1453 "logical %d < ee_block %d + ee_len %d!",
1454 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001455 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001456 }
Alex Tomas1988b512008-01-28 23:58:27 -05001457
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001458 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001459 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001460 return 0;
1461}
1462
1463/*
1464 * search the closest allocated block to the right for *logical
1465 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001466 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001467 * returns 0 at @phys
1468 * return value contains 0 (success) or error code
1469 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001470static int ext4_ext_search_right(struct inode *inode,
1471 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001472 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1473 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001474{
1475 struct buffer_head *bh = NULL;
1476 struct ext4_extent_header *eh;
1477 struct ext4_extent_idx *ix;
1478 struct ext4_extent *ex;
1479 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001480 int depth; /* Note, NOT eh_depth; depth from top of tree */
1481 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001482
Frank Mayhar273df552010-03-02 11:46:09 -05001483 if (unlikely(path == NULL)) {
1484 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001485 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001486 }
Alex Tomas1988b512008-01-28 23:58:27 -05001487 depth = path->p_depth;
1488 *phys = 0;
1489
1490 if (depth == 0 && path->p_ext == NULL)
1491 return 0;
1492
1493 /* usually extent in the path covers blocks smaller
1494 * then *logical, but it can be that extent is the
1495 * first one in the file */
1496
1497 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001498 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001499 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001500 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1501 EXT4_ERROR_INODE(inode,
1502 "first_extent(path[%d].p_hdr) != ex",
1503 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001504 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001505 }
Alex Tomas1988b512008-01-28 23:58:27 -05001506 while (--depth >= 0) {
1507 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001508 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1509 EXT4_ERROR_INODE(inode,
1510 "ix != EXT_FIRST_INDEX *logical %d!",
1511 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001512 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001513 }
Alex Tomas1988b512008-01-28 23:58:27 -05001514 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001515 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001516 }
1517
Frank Mayhar273df552010-03-02 11:46:09 -05001518 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1519 EXT4_ERROR_INODE(inode,
1520 "logical %d < ee_block %d + ee_len %d!",
1521 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001522 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001523 }
Alex Tomas1988b512008-01-28 23:58:27 -05001524
1525 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1526 /* next allocated block in this leaf */
1527 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001528 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001529 }
1530
1531 /* go up and search for index to the right */
1532 while (--depth >= 0) {
1533 ix = path[depth].p_idx;
1534 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001535 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001536 }
1537
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001538 /* we've gone up to the root and found no index to the right */
1539 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001540
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001541got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001542 /* we've found index to the right, let's
1543 * follow it and find the closest allocated
1544 * block to the right */
1545 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001546 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001547 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001548 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001549 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001550 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001551 if (IS_ERR(bh))
1552 return PTR_ERR(bh);
1553 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001554 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001555 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001556 put_bh(bh);
1557 }
1558
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001559 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001560 if (IS_ERR(bh))
1561 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001562 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001563 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001564found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001565 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001566 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001567 *ret_ex = ex;
1568 if (bh)
1569 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001570 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001571}
1572
1573/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001574 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001575 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001576 * NOTE: it considers block number from index entry as
1577 * allocated block. Thus, index entries have to be consistent
1578 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001579 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001580ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001581ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1582{
1583 int depth;
1584
1585 BUG_ON(path == NULL);
1586 depth = path->p_depth;
1587
1588 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001589 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001590
1591 while (depth >= 0) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001592 struct ext4_ext_path *p = &path[depth];
1593
Alex Tomasa86c6182006-10-11 01:21:03 -07001594 if (depth == path->p_depth) {
1595 /* leaf */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001596 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
1597 return le32_to_cpu(p->p_ext[1].ee_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001598 } else {
1599 /* index */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001600 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
1601 return le32_to_cpu(p->p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001602 }
1603 depth--;
1604 }
1605
Lukas Czernerf17722f2011-06-06 00:05:17 -04001606 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001607}
1608
1609/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001610 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001611 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001612 */
Robin Dong57187892011-07-23 21:49:07 -04001613static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001614{
1615 int depth;
1616
1617 BUG_ON(path == NULL);
1618 depth = path->p_depth;
1619
1620 /* zero-tree has no leaf blocks at all */
1621 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001622 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001623
1624 /* go to index block */
1625 depth--;
1626
1627 while (depth >= 0) {
1628 if (path[depth].p_idx !=
1629 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001630 return (ext4_lblk_t)
1631 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001632 depth--;
1633 }
1634
Lukas Czernerf17722f2011-06-06 00:05:17 -04001635 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001636}
1637
1638/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001639 * ext4_ext_correct_indexes:
1640 * if leaf gets modified and modified extent is first in the leaf,
1641 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001642 * TODO: do we need to correct tree in all cases?
1643 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001644static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001645 struct ext4_ext_path *path)
1646{
1647 struct ext4_extent_header *eh;
1648 int depth = ext_depth(inode);
1649 struct ext4_extent *ex;
1650 __le32 border;
1651 int k, err = 0;
1652
1653 eh = path[depth].p_hdr;
1654 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001655
1656 if (unlikely(ex == NULL || eh == NULL)) {
1657 EXT4_ERROR_INODE(inode,
1658 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001659 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001660 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001661
1662 if (depth == 0) {
1663 /* there is no tree at all */
1664 return 0;
1665 }
1666
1667 if (ex != EXT_FIRST_EXTENT(eh)) {
1668 /* we correct tree if first leaf got modified only */
1669 return 0;
1670 }
1671
1672 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001673 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001674 */
1675 k = depth - 1;
1676 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001677 err = ext4_ext_get_access(handle, inode, path + k);
1678 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001679 return err;
1680 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001681 err = ext4_ext_dirty(handle, inode, path + k);
1682 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001683 return err;
1684
1685 while (k--) {
1686 /* change all left-side indexes */
1687 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1688 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001689 err = ext4_ext_get_access(handle, inode, path + k);
1690 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001691 break;
1692 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001693 err = ext4_ext_dirty(handle, inode, path + k);
1694 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001695 break;
1696 }
1697
1698 return err;
1699}
1700
Eric Biggers43f81672019-12-31 12:04:40 -06001701static int ext4_can_extents_be_merged(struct inode *inode,
1702 struct ext4_extent *ex1,
1703 struct ext4_extent *ex2)
Alex Tomasa86c6182006-10-11 01:21:03 -07001704{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001705 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001706
Lukas Czerner556615d2014-04-20 23:45:47 -04001707 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001708 return 0;
1709
1710 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1711 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1712
1713 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001714 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001715 return 0;
1716
Eric Sandeenda0169b2013-11-04 09:58:26 -05001717 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001718 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001719
Lukas Czerner556615d2014-04-20 23:45:47 -04001720 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001721 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001722 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001723#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001724 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001725 return 0;
1726#endif
1727
Theodore Ts'obf89d162010-10-27 21:30:14 -04001728 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001729 return 1;
1730 return 0;
1731}
1732
1733/*
Amit Arora56055d32007-07-17 21:42:38 -04001734 * This function tries to merge the "ex" extent to the next extent in the tree.
1735 * It always tries to merge towards right. If you want to merge towards
1736 * left, pass "ex - 1" as argument instead of "ex".
1737 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1738 * 1 if they got merged.
1739 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001740static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001741 struct ext4_ext_path *path,
1742 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001743{
1744 struct ext4_extent_header *eh;
1745 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001746 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001747
1748 depth = ext_depth(inode);
1749 BUG_ON(path[depth].p_hdr == NULL);
1750 eh = path[depth].p_hdr;
1751
1752 while (ex < EXT_LAST_EXTENT(eh)) {
1753 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1754 break;
1755 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001756 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001757 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1758 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001759 if (unwritten)
1760 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001761
1762 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1763 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1764 * sizeof(struct ext4_extent);
1765 memmove(ex + 1, ex + 2, len);
1766 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001767 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001768 merge_done = 1;
1769 WARN_ON(eh->eh_entries == 0);
1770 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001771 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001772 }
1773
1774 return merge_done;
1775}
1776
1777/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001778 * This function does a very simple check to see if we can collapse
1779 * an extent tree with a single extent tree leaf block into the inode.
1780 */
1781static void ext4_ext_try_to_merge_up(handle_t *handle,
1782 struct inode *inode,
1783 struct ext4_ext_path *path)
1784{
1785 size_t s;
1786 unsigned max_root = ext4_ext_space_root(inode, 0);
1787 ext4_fsblk_t blk;
1788
1789 if ((path[0].p_depth != 1) ||
1790 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1791 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1792 return;
1793
1794 /*
1795 * We need to modify the block allocation bitmap and the block
1796 * group descriptor to release the extent tree block. If we
1797 * can't get the journal credits, give up.
1798 */
Jan Kara83448bd2019-11-05 17:44:29 +01001799 if (ext4_journal_extend(handle, 2,
1800 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001801 return;
1802
1803 /*
1804 * Copy the extent data up to the inode
1805 */
1806 blk = ext4_idx_pblock(path[0].p_idx);
1807 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1808 sizeof(struct ext4_extent_idx);
1809 s += sizeof(struct ext4_extent_header);
1810
Theodore Ts'o10809df82014-09-01 14:40:09 -04001811 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001812 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1813 path[0].p_depth = 0;
1814 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1815 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1816 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1817
1818 brelse(path[1].p_bh);
1819 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001820 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001821}
1822
1823/*
Eric Biggersadde81c2019-12-31 12:04:41 -06001824 * This function tries to merge the @ex extent to neighbours in the tree, then
1825 * tries to collapse the extent tree into the inode.
Yongqiang Yang197217a2011-05-03 11:45:29 -04001826 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001827static void ext4_ext_try_to_merge(handle_t *handle,
1828 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001829 struct ext4_ext_path *path,
Eric Biggersadde81c2019-12-31 12:04:41 -06001830 struct ext4_extent *ex)
1831{
Yongqiang Yang197217a2011-05-03 11:45:29 -04001832 struct ext4_extent_header *eh;
1833 unsigned int depth;
1834 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001835
1836 depth = ext_depth(inode);
1837 BUG_ON(path[depth].p_hdr == NULL);
1838 eh = path[depth].p_hdr;
1839
1840 if (ex > EXT_FIRST_EXTENT(eh))
1841 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1842
1843 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001844 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001845
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001846 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001847}
1848
1849/*
Amit Arora25d14f92007-05-24 13:04:13 -04001850 * check if a portion of the "newext" extent overlaps with an
1851 * existing extent.
1852 *
1853 * If there is an overlap discovered, it updates the length of the newext
1854 * such that there will be no overlap, and then returns 1.
1855 * If there is no overlap found, it returns 0.
1856 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001857static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1858 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001859 struct ext4_extent *newext,
1860 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001861{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001862 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001863 unsigned int depth, len1;
1864 unsigned int ret = 0;
1865
1866 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001867 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001868 depth = ext_depth(inode);
1869 if (!path[depth].p_ext)
1870 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001871 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001872
1873 /*
1874 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001875 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001876 */
1877 if (b2 < b1) {
1878 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001879 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001880 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001881 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001882 }
1883
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001884 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001885 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001886 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001887 newext->ee_len = cpu_to_le16(len1);
1888 ret = 1;
1889 }
1890
1891 /* check for overlap */
1892 if (b1 + len1 > b2) {
1893 newext->ee_len = cpu_to_le16(b2 - b1);
1894 ret = 1;
1895 }
1896out:
1897 return ret;
1898}
1899
1900/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001901 * ext4_ext_insert_extent:
1902 * tries to merge requsted extent into the existing extent or
1903 * inserts requested extent as new one into the tree,
1904 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001905 */
1906int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001907 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001908 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001909{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001910 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001911 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001912 struct ext4_extent *ex, *fex;
1913 struct ext4_extent *nearex; /* nearest extent */
1914 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001915 int depth, len, err;
1916 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001917 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001918
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001919 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1920 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001921 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1922 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001923 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001924 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001925 depth = ext_depth(inode);
1926 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001927 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001928 if (unlikely(path[depth].p_hdr == NULL)) {
1929 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001930 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001931 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001932
1933 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001934 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001935
1936 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001937 * Try to see whether we should rather test the extent on
1938 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001939 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001940 * left, or on the right from the searched position. This
1941 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001942 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001943 if (ex < EXT_LAST_EXTENT(eh) &&
1944 (le32_to_cpu(ex->ee_block) +
1945 ext4_ext_get_actual_len(ex) <
1946 le32_to_cpu(newext->ee_block))) {
1947 ex += 1;
1948 goto prepend;
1949 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1950 (le32_to_cpu(newext->ee_block) +
1951 ext4_ext_get_actual_len(newext) <
1952 le32_to_cpu(ex->ee_block)))
1953 ex -= 1;
1954
1955 /* Try to append newex to the ex */
1956 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1957 ext_debug("append [%d]%d block to %u:[%d]%d"
1958 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001959 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001960 ext4_ext_get_actual_len(newext),
1961 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001962 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001963 ext4_ext_get_actual_len(ex),
1964 ext4_ext_pblock(ex));
1965 err = ext4_ext_get_access(handle, inode,
1966 path + depth);
1967 if (err)
1968 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001969 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001970 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001971 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001972 if (unwritten)
1973 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001974 eh = path[depth].p_hdr;
1975 nearex = ex;
1976 goto merge;
1977 }
1978
1979prepend:
1980 /* Try to prepend newex to the ex */
1981 if (ext4_can_extents_be_merged(inode, newext, ex)) {
1982 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
1983 "(from %llu)\n",
1984 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001985 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001986 ext4_ext_get_actual_len(newext),
1987 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001988 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001989 ext4_ext_get_actual_len(ex),
1990 ext4_ext_pblock(ex));
1991 err = ext4_ext_get_access(handle, inode,
1992 path + depth);
1993 if (err)
1994 return err;
1995
Lukas Czerner556615d2014-04-20 23:45:47 -04001996 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001997 ex->ee_block = newext->ee_block;
1998 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
1999 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2000 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002001 if (unwritten)
2002 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002003 eh = path[depth].p_hdr;
2004 nearex = ex;
2005 goto merge;
2006 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002007 }
2008
Alex Tomasa86c6182006-10-11 01:21:03 -07002009 depth = ext_depth(inode);
2010 eh = path[depth].p_hdr;
2011 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2012 goto has_space;
2013
2014 /* probably next leaf has space for us? */
2015 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002016 next = EXT_MAX_BLOCKS;
2017 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002018 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002019 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002020 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002021 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002022 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002023 if (IS_ERR(npath))
2024 return PTR_ERR(npath);
2025 BUG_ON(npath->p_depth != path->p_depth);
2026 eh = npath[depth].p_hdr;
2027 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002028 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002029 le16_to_cpu(eh->eh_entries));
2030 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002031 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002032 }
2033 ext_debug("next leaf has no free space(%d,%d)\n",
2034 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2035 }
2036
2037 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002038 * There is no free space in the found leaf.
2039 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002040 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002041 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002042 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002043 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002044 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002045 if (err)
2046 goto cleanup;
2047 depth = ext_depth(inode);
2048 eh = path[depth].p_hdr;
2049
2050has_space:
2051 nearex = path[depth].p_ext;
2052
Avantika Mathur7e028972006-12-06 20:41:33 -08002053 err = ext4_ext_get_access(handle, inode, path + depth);
2054 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002055 goto cleanup;
2056
2057 if (!nearex) {
2058 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002059 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002060 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002061 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002062 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002063 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002064 nearex = EXT_FIRST_EXTENT(eh);
2065 } else {
2066 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002067 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002068 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002069 ext_debug("insert %u:%llu:[%d]%d before: "
2070 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002071 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002072 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002073 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002074 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002075 nearex);
2076 nearex++;
2077 } else {
2078 /* Insert before */
2079 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002080 ext_debug("insert %u:%llu:[%d]%d after: "
2081 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002082 le32_to_cpu(newext->ee_block),
2083 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002084 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002085 ext4_ext_get_actual_len(newext),
2086 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002087 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002088 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2089 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002090 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002091 "move %d extents from 0x%p to 0x%p\n",
2092 le32_to_cpu(newext->ee_block),
2093 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002094 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002095 ext4_ext_get_actual_len(newext),
2096 len, nearex, nearex + 1);
2097 memmove(nearex + 1, nearex,
2098 len * sizeof(struct ext4_extent));
2099 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002100 }
2101
Marcin Slusarze8546d02008-04-17 10:38:59 -04002102 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002103 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002104 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002105 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002106 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002107
2108merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002109 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002110 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002111 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002112
Alex Tomasa86c6182006-10-11 01:21:03 -07002113
2114 /* time to correct all indexes above */
2115 err = ext4_ext_correct_indexes(handle, inode, path);
2116 if (err)
2117 goto cleanup;
2118
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002119 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002120
2121cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002122 ext4_ext_drop_refs(npath);
2123 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002124 return err;
2125}
2126
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002127static int ext4_fill_es_cache_info(struct inode *inode,
2128 ext4_lblk_t block, ext4_lblk_t num,
2129 struct fiemap_extent_info *fieinfo)
2130{
2131 ext4_lblk_t next, end = block + num - 1;
2132 struct extent_status es;
2133 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2134 unsigned int flags;
2135 int err;
2136
2137 while (block <= end) {
2138 next = 0;
2139 flags = 0;
2140 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2141 break;
2142 if (ext4_es_is_unwritten(&es))
2143 flags |= FIEMAP_EXTENT_UNWRITTEN;
2144 if (ext4_es_is_delayed(&es))
2145 flags |= (FIEMAP_EXTENT_DELALLOC |
2146 FIEMAP_EXTENT_UNKNOWN);
2147 if (ext4_es_is_hole(&es))
2148 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2149 if (next == 0)
2150 flags |= FIEMAP_EXTENT_LAST;
2151 if (flags & (FIEMAP_EXTENT_DELALLOC|
2152 EXT4_FIEMAP_EXTENT_HOLE))
2153 es.es_pblk = 0;
2154 else
2155 es.es_pblk = ext4_es_pblock(&es);
2156 err = fiemap_fill_next_extent(fieinfo,
2157 (__u64)es.es_lblk << blksize_bits,
2158 (__u64)es.es_pblk << blksize_bits,
2159 (__u64)es.es_len << blksize_bits,
2160 flags);
2161 if (next == 0)
2162 break;
2163 block = next;
2164 if (err < 0)
2165 return err;
2166 if (err == 1)
2167 return 0;
2168 }
2169 return 0;
2170}
2171
2172
Alex Tomasa86c6182006-10-11 01:21:03 -07002173/*
Jan Kara140a5252016-03-09 22:46:57 -05002174 * ext4_ext_determine_hole - determine hole around given block
2175 * @inode: inode we lookup in
2176 * @path: path in extent tree to @lblk
2177 * @lblk: pointer to logical block around which we want to determine hole
2178 *
2179 * Determine hole length (and start if easily possible) around given logical
2180 * block. We don't try too hard to find the beginning of the hole but @path
2181 * actually points to extent before @lblk, we provide it.
2182 *
2183 * The function returns the length of a hole starting at @lblk. We update @lblk
2184 * to the beginning of the hole if we managed to find it.
2185 */
2186static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2187 struct ext4_ext_path *path,
2188 ext4_lblk_t *lblk)
2189{
2190 int depth = ext_depth(inode);
2191 struct ext4_extent *ex;
2192 ext4_lblk_t len;
2193
2194 ex = path[depth].p_ext;
2195 if (ex == NULL) {
2196 /* there is no extent yet, so gap is [0;-] */
2197 *lblk = 0;
2198 len = EXT_MAX_BLOCKS;
2199 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2200 len = le32_to_cpu(ex->ee_block) - *lblk;
2201 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2202 + ext4_ext_get_actual_len(ex)) {
2203 ext4_lblk_t next;
2204
2205 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2206 next = ext4_ext_next_allocated_block(path);
2207 BUG_ON(next == *lblk);
2208 len = next - *lblk;
2209 } else {
2210 BUG();
2211 }
2212 return len;
2213}
2214
2215/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002216 * ext4_ext_put_gap_in_cache:
2217 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002218 * and cache this gap
2219 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002220static void
Jan Kara140a5252016-03-09 22:46:57 -05002221ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2222 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002223{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002224 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002225
Eric Whitneyad431022018-10-01 14:10:39 -04002226 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2227 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002228 if (es.es_len) {
2229 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002230 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002231 return;
Jan Kara140a5252016-03-09 22:46:57 -05002232 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002233 }
Jan Kara140a5252016-03-09 22:46:57 -05002234 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2235 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2236 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002237}
2238
2239/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002240 * ext4_ext_rm_idx:
2241 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002242 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002243static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002244 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002245{
Alex Tomasa86c6182006-10-11 01:21:03 -07002246 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002247 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002248
2249 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002250 depth--;
2251 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002252 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002253 if (unlikely(path->p_hdr->eh_entries == 0)) {
2254 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002255 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002256 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002257 err = ext4_ext_get_access(handle, inode, path);
2258 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002259 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002260
2261 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2262 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2263 len *= sizeof(struct ext4_extent_idx);
2264 memmove(path->p_idx, path->p_idx + 1, len);
2265 }
2266
Marcin Slusarze8546d02008-04-17 10:38:59 -04002267 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002268 err = ext4_ext_dirty(handle, inode, path);
2269 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002270 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002271 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002272 trace_ext4_ext_rm_idx(inode, leaf);
2273
Peter Huewe7dc57612011-02-21 21:01:42 -05002274 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002275 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002276
2277 while (--depth >= 0) {
2278 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2279 break;
2280 path--;
2281 err = ext4_ext_get_access(handle, inode, path);
2282 if (err)
2283 break;
2284 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2285 err = ext4_ext_dirty(handle, inode, path);
2286 if (err)
2287 break;
2288 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002289 return err;
2290}
2291
2292/*
Mingming Caoee12b632008-08-19 22:16:05 -04002293 * ext4_ext_calc_credits_for_single_extent:
2294 * This routine returns max. credits that needed to insert an extent
2295 * to the extent tree.
2296 * When pass the actual path, the caller should calculate credits
2297 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002298 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002299int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002300 struct ext4_ext_path *path)
2301{
Alex Tomasa86c6182006-10-11 01:21:03 -07002302 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002303 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002304 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002305
Alex Tomasa86c6182006-10-11 01:21:03 -07002306 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002307 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002308 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2309
2310 /*
2311 * There are some space in the leaf tree, no
2312 * need to account for leaf block credit
2313 *
2314 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002315 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002316 * accounted.
2317 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002318 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002319 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002320 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002321 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002322 }
2323
Mingming Cao525f4ed2008-08-19 22:15:58 -04002324 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002325}
Alex Tomasa86c6182006-10-11 01:21:03 -07002326
Mingming Caoee12b632008-08-19 22:16:05 -04002327/*
Jan Karafffb2732013-06-04 13:01:11 -04002328 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002329 *
Jan Karafffb2732013-06-04 13:01:11 -04002330 * If we add a single extent, then in the worse case, each tree level
2331 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002332 *
Jan Karafffb2732013-06-04 13:01:11 -04002333 * If more extents are inserted, they could cause the whole tree split more
2334 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002335 */
Jan Karafffb2732013-06-04 13:01:11 -04002336int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002337{
2338 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002339 int depth;
2340
2341 /* If we are converting the inline data, only one is needed here. */
2342 if (ext4_has_inline_data(inode))
2343 return 1;
2344
2345 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002346
Jan Karafffb2732013-06-04 13:01:11 -04002347 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002348 index = depth * 2;
2349 else
2350 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002351
Mingming Caoee12b632008-08-19 22:16:05 -04002352 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002353}
2354
Theodore Ts'o981250c2013-06-12 11:48:29 -04002355static inline int get_default_free_blocks_flags(struct inode *inode)
2356{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002357 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2358 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002359 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2360 else if (ext4_should_journal_data(inode))
2361 return EXT4_FREE_BLOCKS_FORGET;
2362 return 0;
2363}
2364
Eric Whitney9fe67142018-10-01 14:25:08 -04002365/*
2366 * ext4_rereserve_cluster - increment the reserved cluster count when
2367 * freeing a cluster with a pending reservation
2368 *
2369 * @inode - file containing the cluster
2370 * @lblk - logical block in cluster to be reserved
2371 *
2372 * Increments the reserved cluster count and adjusts quota in a bigalloc
2373 * file system when freeing a partial cluster containing at least one
2374 * delayed and unwritten block. A partial cluster meeting that
2375 * requirement will have a pending reservation. If so, the
2376 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2377 * defer reserved and allocated space accounting to a subsequent call
2378 * to this function.
2379 */
2380static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2381{
2382 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2383 struct ext4_inode_info *ei = EXT4_I(inode);
2384
2385 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2386
2387 spin_lock(&ei->i_block_reservation_lock);
2388 ei->i_reserved_data_blocks++;
2389 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2390 spin_unlock(&ei->i_block_reservation_lock);
2391
2392 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2393 ext4_remove_pending(inode, lblk);
2394}
2395
Alex Tomasa86c6182006-10-11 01:21:03 -07002396static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002397 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002398 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002399 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002400{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002401 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002402 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002403 ext4_fsblk_t last_pblk, pblk;
2404 ext4_lblk_t num;
2405 int flags;
2406
2407 /* only extent tail removal is allowed */
2408 if (from < le32_to_cpu(ex->ee_block) ||
2409 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2410 ext4_error(sbi->s_sb,
2411 "strange request: removal(2) %u-%u from %u:%u",
2412 from, to, le32_to_cpu(ex->ee_block), ee_len);
2413 return 0;
2414 }
2415
2416#ifdef EXTENTS_STATS
2417 spin_lock(&sbi->s_ext_stats_lock);
2418 sbi->s_ext_blocks += ee_len;
2419 sbi->s_ext_extents++;
2420 if (ee_len < sbi->s_ext_min)
2421 sbi->s_ext_min = ee_len;
2422 if (ee_len > sbi->s_ext_max)
2423 sbi->s_ext_max = ee_len;
2424 if (ext_depth(inode) > sbi->s_depth_max)
2425 sbi->s_depth_max = ext_depth(inode);
2426 spin_unlock(&sbi->s_ext_stats_lock);
2427#endif
2428
2429 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2430
2431 /*
2432 * if we have a partial cluster, and it's different from the
2433 * cluster of the last block in the extent, we free it
2434 */
2435 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2436
2437 if (partial->state != initial &&
2438 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2439 if (partial->state == tofree) {
2440 flags = get_default_free_blocks_flags(inode);
2441 if (ext4_is_pending(inode, partial->lblk))
2442 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2443 ext4_free_blocks(handle, inode, NULL,
2444 EXT4_C2B(sbi, partial->pclu),
2445 sbi->s_cluster_ratio, flags);
2446 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2447 ext4_rereserve_cluster(inode, partial->lblk);
2448 }
2449 partial->state = initial;
2450 }
2451
2452 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2453 pblk = ext4_ext_pblock(ex) + ee_len - num;
2454
2455 /*
2456 * We free the partial cluster at the end of the extent (if any),
2457 * unless the cluster is used by another extent (partial_cluster
2458 * state is nofree). If a partial cluster exists here, it must be
2459 * shared with the last block in the extent.
2460 */
2461 flags = get_default_free_blocks_flags(inode);
2462
2463 /* partial, left end cluster aligned, right end unaligned */
2464 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2465 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2466 (partial->state != nofree)) {
2467 if (ext4_is_pending(inode, to))
2468 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2469 ext4_free_blocks(handle, inode, NULL,
2470 EXT4_PBLK_CMASK(sbi, last_pblk),
2471 sbi->s_cluster_ratio, flags);
2472 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2473 ext4_rereserve_cluster(inode, to);
2474 partial->state = initial;
2475 flags = get_default_free_blocks_flags(inode);
2476 }
2477
2478 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002479
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002480 /*
2481 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002482 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002483 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002484 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002485 */
2486 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002487 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002488
Eric Whitney9fe67142018-10-01 14:25:08 -04002489 /* reset the partial cluster if we've freed past it */
2490 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2491 partial->state = initial;
2492
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002493 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002494 * If we've freed the entire extent but the beginning is not left
2495 * cluster aligned and is not marked as ineligible for freeing we
2496 * record the partial cluster at the beginning of the extent. It
2497 * wasn't freed by the preceding ext4_free_blocks() call, and we
2498 * need to look farther to the left to determine if it's to be freed
2499 * (not shared with another extent). Else, reset the partial
2500 * cluster - we're either done freeing or the beginning of the
2501 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002502 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002503 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2504 if (partial->state == initial) {
2505 partial->pclu = EXT4_B2C(sbi, pblk);
2506 partial->lblk = from;
2507 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002508 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002509 } else {
2510 partial->state = initial;
2511 }
2512
Alex Tomasa86c6182006-10-11 01:21:03 -07002513 return 0;
2514}
2515
Allison Hendersond583fb82011-05-25 07:41:43 -04002516/*
2517 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002518 * blocks appearing between "start" and "end". Both "start"
2519 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002520 *
2521 * @handle: The journal handle
2522 * @inode: The files inode
2523 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002524 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002525 * has been released from it. However, if this value is
2526 * negative, it's a cluster just to the right of the
2527 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002528 * @start: The first block to remove
2529 * @end: The last block to remove
2530 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002531static int
2532ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002533 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002534 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002535 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002536{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002537 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002538 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002539 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002540 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002541 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002542 unsigned num;
2543 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002544 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002545 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002546 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002547 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002548
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002549 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002550 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002551 if (!path[depth].p_hdr)
2552 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2553 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002554 if (unlikely(path[depth].p_hdr == NULL)) {
2555 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002556 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002557 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002558 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002559 ex = path[depth].p_ext;
2560 if (!ex)
2561 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002562
2563 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002564 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002565
Eric Whitney9fe67142018-10-01 14:25:08 -04002566 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002567
Alex Tomasa86c6182006-10-11 01:21:03 -07002568 while (ex >= EXT_FIRST_EXTENT(eh) &&
2569 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002570
Lukas Czerner556615d2014-04-20 23:45:47 -04002571 if (ext4_ext_is_unwritten(ex))
2572 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002573 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002574 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002575
Mingming553f9002009-09-18 13:34:55 -04002576 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002577 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002578 path[depth].p_ext = ex;
2579
2580 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002581 b = ex_ee_block+ex_ee_len - 1 < end ?
2582 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002583
2584 ext_debug(" border %u:%u\n", a, b);
2585
Allison Hendersond583fb82011-05-25 07:41:43 -04002586 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002587 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002588 /*
2589 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002590 * so note that its first cluster is in use to avoid
2591 * freeing it when removing blocks. Eventually, the
2592 * right edge of the truncated/punched region will
2593 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002594 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002595 if (sbi->s_cluster_ratio > 1) {
2596 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002597 partial->pclu = EXT4_B2C(sbi, pblk);
2598 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002599 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002600 ex--;
2601 ex_ee_block = le32_to_cpu(ex->ee_block);
2602 ex_ee_len = ext4_ext_get_actual_len(ex);
2603 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002604 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002605 EXT4_ERROR_INODE(inode,
2606 "can not handle truncate %u:%u "
2607 "on extent %u:%u",
2608 start, end, ex_ee_block,
2609 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002610 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002611 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002612 } else if (a != ex_ee_block) {
2613 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002614 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002615 } else {
2616 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002617 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002618 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002619 /*
2620 * 3 for leaf, sb, and inode plus 2 (bmap and group
2621 * descriptor) for each block group; assume two block
2622 * groups plus ex_ee_len/blocks_per_block_group for
2623 * the worst case
2624 */
2625 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002626 if (ex == EXT_FIRST_EXTENT(eh)) {
2627 correct_index = 1;
2628 credits += (ext_depth(inode)) + 1;
2629 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002630 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002631 /*
2632 * We may end up freeing some index blocks and data from the
2633 * punched range. Note that partial clusters are accounted for
2634 * by ext4_free_data_revoke_credits().
2635 */
2636 revoke_credits =
2637 ext4_free_metadata_revoke_credits(inode->i_sb,
2638 ext_depth(inode)) +
2639 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002640
Jan Karaa4130362019-11-05 17:44:16 +01002641 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002642 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002643 if (err) {
2644 if (err > 0)
2645 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002646 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002647 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002648
2649 err = ext4_ext_get_access(handle, inode, path + depth);
2650 if (err)
2651 goto out;
2652
Eric Whitney9fe67142018-10-01 14:25:08 -04002653 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002654 if (err)
2655 goto out;
2656
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002657 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002658 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002659 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002660
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002662 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002663 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002664 * extent have been removed.
2665 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002666 if (unwritten && num)
2667 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002668 /*
2669 * If the extent was completely released,
2670 * we need to remove it from the leaf
2671 */
2672 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002673 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002674 /*
2675 * For hole punching, we need to scoot all the
2676 * extents up when an extent is removed so that
2677 * we dont have blank extents in the middle
2678 */
2679 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2680 sizeof(struct ext4_extent));
2681
2682 /* Now get rid of the one at the end */
2683 memset(EXT_LAST_EXTENT(eh), 0,
2684 sizeof(struct ext4_extent));
2685 }
2686 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002687 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002688
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002689 err = ext4_ext_dirty(handle, inode, path + depth);
2690 if (err)
2691 goto out;
2692
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002693 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002694 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002695 ex--;
2696 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002697 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002698 }
2699
2700 if (correct_index && eh->eh_entries)
2701 err = ext4_ext_correct_indexes(handle, inode, path);
2702
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002703 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002704 * If there's a partial cluster and at least one extent remains in
2705 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002706 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002707 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002708 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002709 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002710 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002711 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002712 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2713 int flags = get_default_free_blocks_flags(inode);
2714
2715 if (ext4_is_pending(inode, partial->lblk))
2716 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002717 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002718 EXT4_C2B(sbi, partial->pclu),
2719 sbi->s_cluster_ratio, flags);
2720 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2721 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002722 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002723 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002724 }
2725
Alex Tomasa86c6182006-10-11 01:21:03 -07002726 /* if this leaf is free, then we should
2727 * remove it from index block above */
2728 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002729 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002730
2731out:
2732 return err;
2733}
2734
2735/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002736 * ext4_ext_more_to_rm:
2737 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002738 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002739static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002740ext4_ext_more_to_rm(struct ext4_ext_path *path)
2741{
2742 BUG_ON(path->p_idx == NULL);
2743
2744 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2745 return 0;
2746
2747 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002748 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002749 * so we have to consider current index for truncation
2750 */
2751 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2752 return 0;
2753 return 1;
2754}
2755
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002756int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2757 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002758{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002759 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002760 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002761 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002762 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002763 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002764 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002765
Eric Whitney9fe67142018-10-01 14:25:08 -04002766 partial.pclu = 0;
2767 partial.lblk = 0;
2768 partial.state = initial;
2769
Lukas Czerner5f95d212012-03-19 23:03:19 -04002770 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002771
2772 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002773 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2774 depth + 1,
2775 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002776 if (IS_ERR(handle))
2777 return PTR_ERR(handle);
2778
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002779again:
Lukas Czerner61801322013-05-27 23:32:35 -04002780 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002781
Alex Tomasa86c6182006-10-11 01:21:03 -07002782 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002783 * Check if we are removing extents inside the extent tree. If that
2784 * is the case, we are going to punch a hole inside the extent tree
2785 * so we have to check whether we need to split the extent covering
2786 * the last block to remove so we can easily remove the part of it
2787 * in ext4_ext_rm_leaf().
2788 */
2789 if (end < EXT_MAX_BLOCKS - 1) {
2790 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002791 ext4_lblk_t ee_block, ex_end, lblk;
2792 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002793
Eric Whitneyf4226d92014-11-23 00:55:42 -05002794 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002795 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002796 if (IS_ERR(path)) {
2797 ext4_journal_stop(handle);
2798 return PTR_ERR(path);
2799 }
2800 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002801 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002802 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002803 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002804 if (depth) {
2805 EXT4_ERROR_INODE(inode,
2806 "path[%d].p_hdr == NULL",
2807 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002808 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002809 }
2810 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002811 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002812
2813 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002814 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002815
2816 /*
2817 * See if the last block is inside the extent, if so split
2818 * the extent at 'end' block so we can easily remove the
2819 * tail of the first part of the split extent in
2820 * ext4_ext_rm_leaf().
2821 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002822 if (end >= ee_block && end < ex_end) {
2823
2824 /*
2825 * If we're going to split the extent, note that
2826 * the cluster containing the block after 'end' is
2827 * in use to avoid freeing it when removing blocks.
2828 */
2829 if (sbi->s_cluster_ratio > 1) {
2830 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04002831 partial.pclu = EXT4_B2C(sbi, pblk);
2832 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002833 }
2834
Lukas Czerner5f95d212012-03-19 23:03:19 -04002835 /*
2836 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002837 * block in the first new extent. Also we should not
2838 * fail removing space due to ENOSPC so try to use
2839 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002840 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002841 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002842 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002843 if (err < 0)
2844 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002845
Eric Whitney7bd75232019-02-28 23:34:11 -05002846 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
2847 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05002848 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05002849 * If we're punching, there's an extent to the right.
2850 * If the partial cluster hasn't been set, set it to
2851 * that extent's first cluster and its state to nofree
2852 * so it won't be freed should it contain blocks to be
2853 * removed. If it's already set (tofree/nofree), we're
2854 * retrying and keep the original partial cluster info
2855 * so a cluster marked tofree as a result of earlier
2856 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05002857 */
2858 lblk = ex_end + 1;
2859 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2860 &ex);
2861 if (err)
2862 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04002863 if (pblk) {
2864 partial.pclu = EXT4_B2C(sbi, pblk);
2865 partial.state = nofree;
2866 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002867 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002868 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002869 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002870 * We start scanning from right side, freeing all the blocks
2871 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002872 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002873 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002874 if (path) {
2875 int k = i = depth;
2876 while (--k > 0)
2877 path[k].p_block =
2878 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2879 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002880 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Ashish Sangwan968dee72012-07-22 22:49:08 -04002881 GFP_NOFS);
2882 if (path == NULL) {
2883 ext4_journal_stop(handle);
2884 return -ENOMEM;
2885 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002886 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002887 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002888 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002889
Theodore Ts'oc3491792013-08-16 21:21:41 -04002890 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002891 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002892 goto out;
2893 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002894 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002895 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002896
2897 while (i >= 0 && err == 0) {
2898 if (i == depth) {
2899 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002900 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002901 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002902 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002903 brelse(path[i].p_bh);
2904 path[i].p_bh = NULL;
2905 i--;
2906 continue;
2907 }
2908
2909 /* this is index block */
2910 if (!path[i].p_hdr) {
2911 ext_debug("initialize header\n");
2912 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002913 }
2914
Alex Tomasa86c6182006-10-11 01:21:03 -07002915 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002916 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002917 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2918 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2919 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2920 path[i].p_hdr,
2921 le16_to_cpu(path[i].p_hdr->eh_entries));
2922 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002923 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002924 path[i].p_idx--;
2925 }
2926
2927 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2928 i, EXT_FIRST_INDEX(path[i].p_hdr),
2929 path[i].p_idx);
2930 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002931 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002932 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002933 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002934 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002935 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002936 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002937 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2938 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002939 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002940 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002941 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002942 break;
2943 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04002944 /* Yield here to deal with large extent trees.
2945 * Should be a no-op if we did IO above. */
2946 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002947 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002948 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002949 break;
2950 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002951 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002952
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002953 /* save actual number of indexes since this
2954 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002955 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2956 i++;
2957 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002958 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002959 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002960 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002961 * handle must be already prepared by the
2962 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05002963 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002964 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002965 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002966 brelse(path[i].p_bh);
2967 path[i].p_bh = NULL;
2968 i--;
2969 ext_debug("return to level %d\n", i);
2970 }
2971 }
2972
Eric Whitney9fe67142018-10-01 14:25:08 -04002973 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
2974 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04002975
Eric Whitney0756b902014-11-23 00:59:39 -05002976 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002977 * if there's a partial cluster and we have removed the first extent
2978 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05002979 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002980 if (partial.state == tofree && err == 0) {
2981 int flags = get_default_free_blocks_flags(inode);
2982
2983 if (ext4_is_pending(inode, partial.lblk))
2984 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04002985 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002986 EXT4_C2B(sbi, partial.pclu),
2987 sbi->s_cluster_ratio, flags);
2988 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2989 ext4_rereserve_cluster(inode, partial.lblk);
2990 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04002991 }
2992
Alex Tomasa86c6182006-10-11 01:21:03 -07002993 /* TODO: flexible tree reduction should be here */
2994 if (path->p_hdr->eh_entries == 0) {
2995 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002996 * truncate to zero freed all the tree,
2997 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002998 */
2999 err = ext4_ext_get_access(handle, inode, path);
3000 if (err == 0) {
3001 ext_inode_hdr(inode)->eh_depth = 0;
3002 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003003 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003004 err = ext4_ext_dirty(handle, inode, path);
3005 }
3006 }
3007out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003008 ext4_ext_drop_refs(path);
3009 kfree(path);
3010 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003011 if (err == -EAGAIN)
3012 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003013 ext4_journal_stop(handle);
3014
3015 return err;
3016}
3017
3018/*
3019 * called at mount time
3020 */
3021void ext4_ext_init(struct super_block *sb)
3022{
3023 /*
3024 * possible initialization would be here
3025 */
3026
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003027 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003028#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003029 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003030#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003031 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003032#endif
3033#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003034 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003035#endif
3036#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003037 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003038#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003039 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003040#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003041#ifdef EXTENTS_STATS
3042 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3043 EXT4_SB(sb)->s_ext_min = 1 << 30;
3044 EXT4_SB(sb)->s_ext_max = 0;
3045#endif
3046 }
3047}
3048
3049/*
3050 * called at umount time
3051 */
3052void ext4_ext_release(struct super_block *sb)
3053{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003054 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003055 return;
3056
3057#ifdef EXTENTS_STATS
3058 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3059 struct ext4_sb_info *sbi = EXT4_SB(sb);
3060 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3061 sbi->s_ext_blocks, sbi->s_ext_extents,
3062 sbi->s_ext_blocks / sbi->s_ext_extents);
3063 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3064 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3065 }
3066#endif
3067}
3068
Zheng Liud7b2a002013-08-28 14:47:06 -04003069static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3070{
3071 ext4_lblk_t ee_block;
3072 ext4_fsblk_t ee_pblock;
3073 unsigned int ee_len;
3074
3075 ee_block = le32_to_cpu(ex->ee_block);
3076 ee_len = ext4_ext_get_actual_len(ex);
3077 ee_pblock = ext4_ext_pblock(ex);
3078
3079 if (ee_len == 0)
3080 return 0;
3081
3082 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3083 EXTENT_STATUS_WRITTEN);
3084}
3085
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003086/* FIXME!! we need to try to merge to left or right after zero-out */
3087static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3088{
Lukas Czerner24075182010-10-27 21:30:06 -04003089 ext4_fsblk_t ee_pblock;
3090 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003091
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003092 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003093 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003094 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3095 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003096}
3097
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003098/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003099 * ext4_split_extent_at() splits an extent at given block.
3100 *
3101 * @handle: the journal handle
3102 * @inode: the file inode
3103 * @path: the path to the extent
3104 * @split: the logical block where the extent is splitted.
3105 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003106 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003107 * @flags: flags used to insert new extent to extent tree.
3108 *
3109 *
3110 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3111 * of which are deterimined by split_flag.
3112 *
3113 * There are two cases:
3114 * a> the extent are splitted into two extent.
3115 * b> split is not needed, and just mark the extent.
3116 *
3117 * return 0 on success.
3118 */
3119static int ext4_split_extent_at(handle_t *handle,
3120 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003121 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003122 ext4_lblk_t split,
3123 int split_flag,
3124 int flags)
3125{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003126 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003127 ext4_fsblk_t newblock;
3128 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003129 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003130 struct ext4_extent *ex2 = NULL;
3131 unsigned int ee_len, depth;
3132 int err = 0;
3133
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003134 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3135 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3136
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003137 ext_debug("ext4_split_extents_at: inode %lu, logical"
3138 "block %llu\n", inode->i_ino, (unsigned long long)split);
3139
3140 ext4_ext_show_leaf(inode, path);
3141
3142 depth = ext_depth(inode);
3143 ex = path[depth].p_ext;
3144 ee_block = le32_to_cpu(ex->ee_block);
3145 ee_len = ext4_ext_get_actual_len(ex);
3146 newblock = split - ee_block + ext4_ext_pblock(ex);
3147
3148 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003149 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003150 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003151 EXT4_EXT_MARK_UNWRIT1 |
3152 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003153
3154 err = ext4_ext_get_access(handle, inode, path + depth);
3155 if (err)
3156 goto out;
3157
3158 if (split == ee_block) {
3159 /*
3160 * case b: block @split is the block that the extent begins with
3161 * then we just change the state of the extent, and splitting
3162 * is not needed.
3163 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003164 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3165 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003166 else
3167 ext4_ext_mark_initialized(ex);
3168
3169 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003170 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003171
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003172 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003173 goto out;
3174 }
3175
3176 /* case a */
3177 memcpy(&orig_ex, ex, sizeof(orig_ex));
3178 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003179 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3180 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003181
3182 /*
3183 * path may lead to new leaf, not to original leaf any more
3184 * after ext4_ext_insert_extent() returns,
3185 */
3186 err = ext4_ext_dirty(handle, inode, path + depth);
3187 if (err)
3188 goto fix_extent_len;
3189
3190 ex2 = &newex;
3191 ex2->ee_block = cpu_to_le32(split);
3192 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3193 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003194 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3195 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003196
Theodore Ts'odfe50802014-09-01 14:37:09 -04003197 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003198 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003199 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003200 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003201 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003202 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003203 zero_ex.ee_len = cpu_to_le16(
3204 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003205 ext4_ext_store_pblock(&zero_ex,
3206 ext4_ext_pblock(ex2));
3207 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003208 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003209 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003210 zero_ex.ee_len = cpu_to_le16(
3211 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003212 ext4_ext_store_pblock(&zero_ex,
3213 ext4_ext_pblock(ex));
3214 }
3215 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003216 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003217 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003218 zero_ex.ee_len = cpu_to_le16(
3219 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003220 ext4_ext_store_pblock(&zero_ex,
3221 ext4_ext_pblock(&orig_ex));
3222 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003223
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003224 if (err)
3225 goto fix_extent_len;
3226 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003227 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003228 ext4_ext_try_to_merge(handle, inode, path, ex);
3229 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003230 if (err)
3231 goto fix_extent_len;
3232
3233 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003234 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003235
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003236 goto out;
3237 } else if (err)
3238 goto fix_extent_len;
3239
3240out:
3241 ext4_ext_show_leaf(inode, path);
3242 return err;
3243
3244fix_extent_len:
3245 ex->ee_len = orig_ex.ee_len;
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003246 /*
3247 * Ignore ext4_ext_dirty return value since we are already in error path
3248 * and err is a non-zero error code.
3249 */
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003250 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003251 return err;
3252}
3253
3254/*
3255 * ext4_split_extents() splits an extent and mark extent which is covered
3256 * by @map as split_flags indicates
3257 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003258 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003259 * There are three possibilities:
3260 * a> There is no split required
3261 * b> Splits in two extents: Split is happening at either end of the extent
3262 * c> Splits in three extents: Somone is splitting in middle of the extent
3263 *
3264 */
3265static int ext4_split_extent(handle_t *handle,
3266 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003267 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003268 struct ext4_map_blocks *map,
3269 int split_flag,
3270 int flags)
3271{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003272 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003273 ext4_lblk_t ee_block;
3274 struct ext4_extent *ex;
3275 unsigned int ee_len, depth;
3276 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003277 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003278 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003279 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003280
3281 depth = ext_depth(inode);
3282 ex = path[depth].p_ext;
3283 ee_block = le32_to_cpu(ex->ee_block);
3284 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003285 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003286
3287 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003288 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003289 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003290 if (unwritten)
3291 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3292 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003293 if (split_flag & EXT4_EXT_DATA_VALID2)
3294 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003295 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003297 if (err)
3298 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003299 } else {
3300 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003301 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003302 /*
3303 * Update path is required because previous ext4_split_extent_at() may
3304 * result in split of original leaf or extent zeroout.
3305 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003306 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003307 if (IS_ERR(path))
3308 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003309 depth = ext_depth(inode);
3310 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003311 if (!ex) {
3312 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3313 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003314 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003315 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003316 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003317 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003318
3319 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003320 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003321 if (unwritten) {
3322 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003323 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003324 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003325 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003326 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003327 map->m_lblk, split_flag1, flags);
3328 if (err)
3329 goto out;
3330 }
3331
3332 ext4_ext_show_leaf(inode, path);
3333out:
Zheng Liu3a225672013-03-10 21:20:23 -04003334 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003335}
3336
Amit Arora56055d32007-07-17 21:42:38 -04003337/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003338 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003339 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003340 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003341 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003342 * There are three possibilities:
3343 * a> There is no split required: Entire extent should be initialized
3344 * b> Splits in two extents: Write is happening at either end of the extent
3345 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003346 *
3347 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003348 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003349 * - The extent pointed to by 'path' contains a superset
3350 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3351 *
3352 * Post-conditions on success:
3353 * - the returned value is the number of blocks beyond map->l_lblk
3354 * that are allocated and initialized.
3355 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003356 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003357static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003358 struct inode *inode,
3359 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003360 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003361 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003362{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003363 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003364 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003365 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003366 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003367 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003368 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003369 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003370 unsigned int ee_len, depth, map_len = map->m_len;
3371 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003372 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003373 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003374
3375 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3376 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003377 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003378
Zheng Liu67a5da52012-08-17 09:54:17 -04003379 sbi = EXT4_SB(inode->i_sb);
Jan Kara801674f2020-03-31 12:50:16 +02003380 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3381 >> inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003382 if (eof_block < map->m_lblk + map_len)
3383 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003384
3385 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003386 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003387 ex = path[depth].p_ext;
3388 ee_block = le32_to_cpu(ex->ee_block);
3389 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003390 zero_ex1.ee_len = 0;
3391 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003392
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003393 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3394
3395 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003396 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003397 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003398
3399 /*
3400 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003401 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003402 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003403 * memmove() calls. Transferring to the left is the common case in
3404 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3405 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003406 *
3407 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003408 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003409 * This would require removing the extent if the transfer
3410 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003411 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003412 * same extent tree node.
3413 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003414 if ((map->m_lblk == ee_block) &&
3415 /* See if we can merge left */
3416 (map_len < ee_len) && /*L1*/
3417 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003418 ext4_lblk_t prev_lblk;
3419 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003420 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003421
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003422 abut_ex = ex - 1;
3423 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3424 prev_len = ext4_ext_get_actual_len(abut_ex);
3425 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003426 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003427
3428 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003429 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003430 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003431 * - C1: abut_ex is initialized,
3432 * - C2: abut_ex is logically abutting ex,
3433 * - C3: abut_ex is physically abutting ex,
3434 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003435 * overflowing the (initialized) length limit.
3436 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003437 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003438 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3439 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003440 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003441 err = ext4_ext_get_access(handle, inode, path + depth);
3442 if (err)
3443 goto out;
3444
3445 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003446 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003447
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003448 /* Shift the start of ex by 'map_len' blocks */
3449 ex->ee_block = cpu_to_le32(ee_block + map_len);
3450 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3451 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003452 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003453
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003454 /* Extend abut_ex by 'map_len' blocks */
3455 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003456
3457 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003458 allocated = map_len;
3459 }
3460 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3461 (map_len < ee_len) && /*L1*/
3462 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3463 /* See if we can merge right */
3464 ext4_lblk_t next_lblk;
3465 ext4_fsblk_t next_pblk, ee_pblk;
3466 unsigned int next_len;
3467
3468 abut_ex = ex + 1;
3469 next_lblk = le32_to_cpu(abut_ex->ee_block);
3470 next_len = ext4_ext_get_actual_len(abut_ex);
3471 next_pblk = ext4_ext_pblock(abut_ex);
3472 ee_pblk = ext4_ext_pblock(ex);
3473
3474 /*
3475 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3476 * upon those conditions:
3477 * - C1: abut_ex is initialized,
3478 * - C2: abut_ex is logically abutting ex,
3479 * - C3: abut_ex is physically abutting ex,
3480 * - C4: abut_ex can receive the additional blocks without
3481 * overflowing the (initialized) length limit.
3482 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003483 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003484 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3485 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3486 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3487 err = ext4_ext_get_access(handle, inode, path + depth);
3488 if (err)
3489 goto out;
3490
3491 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3492 map, ex, abut_ex);
3493
3494 /* Shift the start of abut_ex by 'map_len' blocks */
3495 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3496 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3497 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003498 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003499
3500 /* Extend abut_ex by 'map_len' blocks */
3501 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3502
3503 /* Result: number of initialized blocks past m_lblk */
3504 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003505 }
3506 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003507 if (allocated) {
3508 /* Mark the block containing both extents as dirty */
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003509 err = ext4_ext_dirty(handle, inode, path + depth);
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003510
3511 /* Update path to point to the right extent */
3512 path[depth].p_ext = abut_ex;
3513 goto out;
3514 } else
3515 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003516
Yongqiang Yang667eff32011-05-03 12:25:07 -04003517 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003518 /*
3519 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003520 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003521 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003522 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003523
Zheng Liu67a5da52012-08-17 09:54:17 -04003524 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3525 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003526 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003527
Amit Arora56055d32007-07-17 21:42:38 -04003528 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003529 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003530 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003531 * 2. split the extent into two extents, zeroout the head of the first
3532 * extent.
3533 * 3. split the extent into two extents, zeroout the tail of the second
3534 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003535 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003536 * 5. no splitting needed, just possibly zeroout the head and / or the
3537 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003538 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003539 split_map.m_lblk = map->m_lblk;
3540 split_map.m_len = map->m_len;
3541
Jan Kara4f8caa62017-05-26 17:40:52 -04003542 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003543 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003544 /* case 3 or 5 */
3545 zero_ex1.ee_block =
3546 cpu_to_le32(split_map.m_lblk +
3547 split_map.m_len);
3548 zero_ex1.ee_len =
3549 cpu_to_le16(allocated - split_map.m_len);
3550 ext4_ext_store_pblock(&zero_ex1,
3551 ext4_ext_pblock(ex) + split_map.m_lblk +
3552 split_map.m_len - ee_block);
3553 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003554 if (err)
3555 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003556 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003557 }
3558 if (split_map.m_lblk - ee_block + split_map.m_len <
3559 max_zeroout) {
3560 /* case 2 or 5 */
3561 if (split_map.m_lblk != ee_block) {
3562 zero_ex2.ee_block = ex->ee_block;
3563 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003564 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003565 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003566 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003567 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003568 if (err)
3569 goto out;
3570 }
3571
Jan Kara4f8caa62017-05-26 17:40:52 -04003572 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003573 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003574 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003575 }
3576 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003577
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003578 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3579 flags);
3580 if (err > 0)
3581 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003582out:
Zheng Liuadb23552013-03-10 21:13:05 -04003583 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003584 if (!err) {
3585 err = ext4_zeroout_es(inode, &zero_ex1);
3586 if (!err)
3587 err = ext4_zeroout_es(inode, &zero_ex2);
3588 }
Amit Arora56055d32007-07-17 21:42:38 -04003589 return err ? err : allocated;
3590}
3591
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003592/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003593 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003594 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003595 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003596 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003597 * Writing to an unwritten extent may result in splitting the unwritten
3598 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003599 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003600 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003601 * b> Splits in two extents: Write is happening at either end of the extent
3602 * c> Splits in three extents: Somone is writing in middle of the extent
3603 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003604 * This works the same way in the case of initialized -> unwritten conversion.
3605 *
Mingming Cao00314622009-09-28 15:49:08 -04003606 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003607 * the unwritten extent split. To prevent ENOSPC occur at the IO
3608 * complete, we need to split the unwritten extent before DIO submit
3609 * the IO. The unwritten extent called at this time will be split
3610 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003611 * being filled will be convert to initialized by the end_io callback function
3612 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003613 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003614 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003615 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003616static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003617 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003618 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003619 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003620 int flags)
3621{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003622 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003623 ext4_lblk_t eof_block;
3624 ext4_lblk_t ee_block;
3625 struct ext4_extent *ex;
3626 unsigned int ee_len;
3627 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003628
Lukas Czernerb8a86842014-03-18 18:05:35 -04003629 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3630 __func__, inode->i_ino,
3631 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003632
Jan Kara801674f2020-03-31 12:50:16 +02003633 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3634 >> inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003635 if (eof_block < map->m_lblk + map->m_len)
3636 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003637 /*
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003638 * It is safe to convert extent to initialized via explicit
3639 * zeroout only if extent is fully insde i_size or new_size.
3640 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003641 depth = ext_depth(inode);
3642 ex = path[depth].p_ext;
3643 ee_block = le32_to_cpu(ex->ee_block);
3644 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003645
Lukas Czernerb8a86842014-03-18 18:05:35 -04003646 /* Convert to unwritten */
3647 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3648 split_flag |= EXT4_EXT_DATA_VALID1;
3649 /* Convert to initialized */
3650 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3651 split_flag |= ee_block + ee_len <= eof_block ?
3652 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003653 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003654 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003655 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003656 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003657}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003658
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003659static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003660 struct inode *inode,
3661 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003662 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003663{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003664 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003665 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003666 ext4_lblk_t ee_block;
3667 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003668 int depth;
3669 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003670
3671 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003672 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003673 ee_block = le32_to_cpu(ex->ee_block);
3674 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003675
Yongqiang Yang197217a2011-05-03 11:45:29 -04003676 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3677 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003678 (unsigned long long)ee_block, ee_len);
3679
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003680 /* If extent is larger than requested it is a clear sign that we still
3681 * have some extent state machine issues left. So extent_split is still
3682 * required.
3683 * TODO: Once all related issues will be fixed this situation should be
3684 * illegal.
3685 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003686 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003687#ifdef CONFIG_EXT4_DEBUG
3688 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003689 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003690 inode->i_ino, (unsigned long long)ee_block, ee_len,
3691 (unsigned long long)map->m_lblk, map->m_len);
3692#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003693 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003694 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003695 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003696 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003697 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003698 if (IS_ERR(path))
3699 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003700 depth = ext_depth(inode);
3701 ex = path[depth].p_ext;
3702 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003703
Mingming Cao00314622009-09-28 15:49:08 -04003704 err = ext4_ext_get_access(handle, inode, path + depth);
3705 if (err)
3706 goto out;
3707 /* first mark the extent as initialized */
3708 ext4_ext_mark_initialized(ex);
3709
Yongqiang Yang197217a2011-05-03 11:45:29 -04003710 /* note: ext4_ext_correct_indexes() isn't needed here because
3711 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003712 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003713 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003714
Mingming Cao00314622009-09-28 15:49:08 -04003715 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003716 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003717out:
3718 ext4_ext_show_leaf(inode, path);
3719 return err;
3720}
3721
3722static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003723convert_initialized_extent(handle_t *handle, struct inode *inode,
3724 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003725 struct ext4_ext_path **ppath,
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003726 unsigned int *allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003727{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003728 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003729 struct ext4_extent *ex;
3730 ext4_lblk_t ee_block;
3731 unsigned int ee_len;
3732 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003733 int err = 0;
3734
3735 /*
3736 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003737 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003738 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003739 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3740 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003741
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003742 depth = ext_depth(inode);
3743 ex = path[depth].p_ext;
3744 ee_block = le32_to_cpu(ex->ee_block);
3745 ee_len = ext4_ext_get_actual_len(ex);
3746
3747 ext_debug("%s: inode %lu, logical"
3748 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3749 (unsigned long long)ee_block, ee_len);
3750
3751 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003752 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003753 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3754 if (err < 0)
3755 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003756 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003757 if (IS_ERR(path))
3758 return PTR_ERR(path);
3759 depth = ext_depth(inode);
3760 ex = path[depth].p_ext;
3761 if (!ex) {
3762 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3763 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003764 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003765 }
3766 }
3767
3768 err = ext4_ext_get_access(handle, inode, path + depth);
3769 if (err)
3770 return err;
3771 /* first mark the extent as unwritten */
3772 ext4_ext_mark_unwritten(ex);
3773
3774 /* note: ext4_ext_correct_indexes() isn't needed here because
3775 * borders are not changed
3776 */
3777 ext4_ext_try_to_merge(handle, inode, path, ex);
3778
3779 /* Mark modified extent as dirty */
3780 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3781 if (err)
3782 return err;
3783 ext4_ext_show_leaf(inode, path);
3784
3785 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003786
Lukas Czernerb8a86842014-03-18 18:05:35 -04003787 map->m_flags |= EXT4_MAP_UNWRITTEN;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003788 if (*allocated > map->m_len)
3789 *allocated = map->m_len;
3790 map->m_len = *allocated;
3791 return 0;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003792}
3793
3794static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003795ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003796 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003797 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003798 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003799{
Eric Whitney4337ecd2020-02-11 16:02:16 -05003800#ifdef EXT_DEBUG
Theodore Ts'odfe50802014-09-01 14:37:09 -04003801 struct ext4_ext_path *path = *ppath;
Eric Whitney4337ecd2020-02-11 16:02:16 -05003802#endif
Mingming Cao00314622009-09-28 15:49:08 -04003803 int ret = 0;
3804 int err = 0;
3805
Lukas Czerner556615d2014-04-20 23:45:47 -04003806 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05003807 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003808 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003809 flags, allocated);
3810 ext4_ext_show_leaf(inode, path);
3811
Lukas Czerner27dd4382013-04-09 22:11:22 -04003812 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04003813 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04003814 * allocate metadata blocks for the new extent block if needed.
3815 */
3816 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
3817
Lukas Czerner556615d2014-04-20 23:45:47 -04003818 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05003819 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003820
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003821 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003822 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003823 ret = ext4_split_convert_extents(handle, inode, map, ppath,
3824 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04003825 if (ret <= 0)
3826 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003827 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003828 goto out;
3829 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003830 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003831 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05003832 if (flags & EXT4_GET_BLOCKS_ZERO) {
3833 if (allocated > map->m_len)
3834 allocated = map->m_len;
3835 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
3836 allocated);
3837 if (err < 0)
3838 goto out2;
3839 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003840 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003841 ppath);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003842 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003843 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003844 else
Theodore Ts'o58590b02010-10-27 21:23:12 -04003845 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04003846 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05003847 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04003848 if (allocated > map->m_len)
3849 allocated = map->m_len;
3850 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003851 goto out2;
3852 }
3853 /* buffered IO case */
3854 /*
3855 * repeat fallocate creation request
3856 * we already have an unwritten extent
3857 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003858 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05003859 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003860 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003861 }
Mingming Cao00314622009-09-28 15:49:08 -04003862
3863 /* buffered READ or buffered write_begin() lookup */
3864 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3865 /*
3866 * We have blocks reserved already. We
3867 * return allocated blocks so that delalloc
3868 * won't do block reservation for us. But
3869 * the buffer head will be unmapped so that
3870 * a read from the block returns 0s.
3871 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003872 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003873 goto out1;
3874 }
3875
3876 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04003877 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003878 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003879 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003880out:
3881 if (ret <= 0) {
3882 err = ret;
3883 goto out2;
3884 } else
3885 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003886 map->m_flags |= EXT4_MAP_NEW;
zhangyi (F)16e08b12019-02-10 23:32:07 -05003887 if (allocated > map->m_len)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003888 allocated = map->m_len;
Zheng Liu3a225672013-03-10 21:20:23 -04003889 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003890
Mingming Cao00314622009-09-28 15:49:08 -04003891map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003892 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003893out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003894 if (allocated > map->m_len)
3895 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003896 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003897 map->m_pblk = newblock;
3898 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003899out2:
Mingming Cao00314622009-09-28 15:49:08 -04003900 return err ? err : allocated;
3901}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003902
Mingming Cao00314622009-09-28 15:49:08 -04003903/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003904 * get_implied_cluster_alloc - check to see if the requested
3905 * allocation (in the map structure) overlaps with a cluster already
3906 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003907 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003908 * @map The requested lblk->pblk mapping
3909 * @ex The extent structure which might contain an implied
3910 * cluster allocation
3911 *
3912 * This function is called by ext4_ext_map_blocks() after we failed to
3913 * find blocks that were already in the inode's extent tree. Hence,
3914 * we know that the beginning of the requested region cannot overlap
3915 * the extent from the inode's extent tree. There are three cases we
3916 * want to catch. The first is this case:
3917 *
3918 * |--- cluster # N--|
3919 * |--- extent ---| |---- requested region ---|
3920 * |==========|
3921 *
3922 * The second case that we need to test for is this one:
3923 *
3924 * |--------- cluster # N ----------------|
3925 * |--- requested region --| |------- extent ----|
3926 * |=======================|
3927 *
3928 * The third case is when the requested region lies between two extents
3929 * within the same cluster:
3930 * |------------- cluster # N-------------|
3931 * |----- ex -----| |---- ex_right ----|
3932 * |------ requested region ------|
3933 * |================|
3934 *
3935 * In each of the above cases, we need to set the map->m_pblk and
3936 * map->m_len so it corresponds to the return the extent labelled as
3937 * "|====|" from cluster #N, since it is already in use for data in
3938 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3939 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3940 * as a new "allocated" block region. Otherwise, we will return 0 and
3941 * ext4_ext_map_blocks() will then allocate one or more new clusters
3942 * by calling ext4_mb_new_blocks().
3943 */
Aditya Kalid8990242011-09-09 19:18:51 -04003944static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003945 struct ext4_map_blocks *map,
3946 struct ext4_extent *ex,
3947 struct ext4_ext_path *path)
3948{
Aditya Kalid8990242011-09-09 19:18:51 -04003949 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003950 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003951 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003952 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003953 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3954 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3955 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3956
3957 /* The extent passed in that we are trying to match */
3958 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3959 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3960
3961 /* The requested region passed into ext4_map_blocks() */
3962 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003963
3964 if ((rr_cluster_start == ex_cluster_end) ||
3965 (rr_cluster_start == ex_cluster_start)) {
3966 if (rr_cluster_start == ex_cluster_end)
3967 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003968 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003969 map->m_len = min(map->m_len,
3970 (unsigned) sbi->s_cluster_ratio - c_offset);
3971 /*
3972 * Check for and handle this case:
3973 *
3974 * |--------- cluster # N-------------|
3975 * |------- extent ----|
3976 * |--- requested region ---|
3977 * |===========|
3978 */
3979
3980 if (map->m_lblk < ee_block)
3981 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3982
3983 /*
3984 * Check for the case where there is already another allocated
3985 * block to the right of 'ex' but before the end of the cluster.
3986 *
3987 * |------------- cluster # N-------------|
3988 * |----- ex -----| |---- ex_right ----|
3989 * |------ requested region ------|
3990 * |================|
3991 */
3992 if (map->m_lblk > ee_block) {
3993 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3994 map->m_len = min(map->m_len, next - map->m_lblk);
3995 }
Aditya Kalid8990242011-09-09 19:18:51 -04003996
3997 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003998 return 1;
3999 }
Aditya Kalid8990242011-09-09 19:18:51 -04004000
4001 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004002 return 0;
4003}
4004
4005
4006/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004007 * Block allocation/map/preallocation routine for extents based files
4008 *
4009 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004010 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004011 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4012 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004013 *
4014 * return > 0, number of of blocks already mapped/allocated
4015 * if create == 0 and these are pre-allocated blocks
4016 * buffer head is unmapped
4017 * otherwise blocks are mapped
4018 *
4019 * return = 0, if plain look up failed (blocks have not been allocated)
4020 * buffer head is unmapped
4021 *
4022 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004023 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004024int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4025 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004026{
4027 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004028 struct ext4_extent newex, *ex, *ex2;
4029 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004030 ext4_fsblk_t newblock = 0;
Eric Whitney34990462020-03-11 16:50:33 -04004031 int err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004032 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004033 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004034 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004035 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07004036
Mingming84fe3be2009-09-01 08:44:37 -04004037 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004038 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004039 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004040
Alex Tomasa86c6182006-10-11 01:21:03 -07004041 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004042 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004043 if (IS_ERR(path)) {
4044 err = PTR_ERR(path);
4045 path = NULL;
4046 goto out2;
4047 }
4048
4049 depth = ext_depth(inode);
4050
4051 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004052 * consistent leaf must not be empty;
4053 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004054 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004055 */
Frank Mayhar273df552010-03-02 11:46:09 -05004056 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4057 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004058 "lblock: %lu, depth: %d pblock %lld",
4059 (unsigned long) map->m_lblk, depth,
4060 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004061 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004062 goto out2;
4063 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004064
Avantika Mathur7e028972006-12-06 20:41:33 -08004065 ex = path[depth].p_ext;
4066 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004067 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004068 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004069 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004070
Lukas Czernerb8a86842014-03-18 18:05:35 -04004071
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004072 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004073 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004074 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004075 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004076 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004077
4078 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4079
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004080 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004081 if (in_range(map->m_lblk, ee_block, ee_len)) {
4082 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004083 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004084 allocated = ee_len - (map->m_lblk - ee_block);
4085 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4086 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004087
Lukas Czernerb8a86842014-03-18 18:05:35 -04004088 /*
4089 * If the extent is initialized check whether the
4090 * caller wants to convert it to unwritten.
4091 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004092 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004093 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004094 err = convert_initialized_extent(handle,
4095 inode, map, &path, &allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004096 goto out2;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004097 } else if (!ext4_ext_is_unwritten(ex)) {
Lukas Czerner78771912012-03-19 23:05:43 -04004098 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004099 }
Zheng Liu69eb33d2013-02-18 00:31:07 -05004100
Lukas Czerner556615d2014-04-20 23:45:47 -04004101 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004102 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004103 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004104 if (ret < 0)
4105 err = ret;
4106 else
4107 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004108 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004109 }
4110 }
4111
4112 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004113 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004114 * we couldn't try to create block if create flag is zero
4115 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004116 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004117 ext4_lblk_t hole_start, hole_len;
4118
Jan Karafacab4d2016-03-09 22:54:00 -05004119 hole_start = map->m_lblk;
4120 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004121 /*
4122 * put just found gap into cache to speed up
4123 * subsequent requests
4124 */
Jan Kara140a5252016-03-09 22:46:57 -05004125 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004126
4127 /* Update hole_len to reflect hole size after map->m_lblk */
4128 if (hole_start != map->m_lblk)
4129 hole_len -= map->m_lblk - hole_start;
4130 map->m_pblk = 0;
4131 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4132
Alex Tomasa86c6182006-10-11 01:21:03 -07004133 goto out2;
4134 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004135
Alex Tomasa86c6182006-10-11 01:21:03 -07004136 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004137 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004138 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004139 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004140 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004141
4142 /*
4143 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004144 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004145 */
4146 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004147 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004148 ar.len = allocated = map->m_len;
4149 newblock = map->m_pblk;
4150 goto got_allocated_blocks;
4151 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004152
Alex Tomasc9de5602008-01-29 00:19:52 -05004153 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004154 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004155 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4156 if (err)
4157 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004158 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004159 ex2 = NULL;
4160 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004161 if (err)
4162 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004163
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004164 /* Check if the extent after searching to the right implies a
4165 * cluster we can use. */
4166 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004167 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004168 ar.len = allocated = map->m_len;
4169 newblock = map->m_pblk;
4170 goto got_allocated_blocks;
4171 }
4172
Amit Arora749269f2007-07-18 09:02:56 -04004173 /*
4174 * See if request is beyond maximum number of blocks we can have in
4175 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004176 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4177 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004178 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004179 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004180 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004181 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004182 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4183 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4184 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004185
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004186 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004187 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004188 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004189 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004190 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004191 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004192 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004193
4194 /* allocate new block */
4195 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004196 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4197 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004198 /*
4199 * We calculate the offset from the beginning of the cluster
4200 * for the logical block number, since when we allocate a
4201 * physical cluster, the physical block should start at the
4202 * same offset from the beginning of the cluster. This is
4203 * needed so that future calls to get_implied_cluster_alloc()
4204 * work correctly.
4205 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004206 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004207 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4208 ar.goal -= offset;
4209 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004210 if (S_ISREG(inode->i_mode))
4211 ar.flags = EXT4_MB_HINT_DATA;
4212 else
4213 /* disable in-core preallocation for non-regular files */
4214 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004215 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4216 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004217 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4218 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004219 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4220 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004221 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004222 if (!newblock)
4223 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004224 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004225 ar.goal, newblock, allocated);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004226 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004227 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4228 if (ar.len > allocated)
4229 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004230
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004231got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004232 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004233 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004234 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004235 /* Mark unwritten */
Eric Whitney34990462020-03-11 16:50:33 -04004236 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Lukas Czerner556615d2014-04-20 23:45:47 -04004237 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004238 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004239 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004240
Eric Whitney4337ecd2020-02-11 16:02:16 -05004241 err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
Eric Whitney34990462020-03-11 16:50:33 -04004242 if (err) {
4243 if (allocated_clusters) {
4244 int fb_flags = 0;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004245
Eric Whitney34990462020-03-11 16:50:33 -04004246 /*
4247 * free data blocks we just allocated.
4248 * not a good idea to call discard here directly,
4249 * but otherwise we'd need to call it every free().
4250 */
4251 ext4_discard_preallocations(inode);
4252 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4253 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
4254 ext4_free_blocks(handle, inode, NULL, newblock,
4255 EXT4_C2B(sbi, allocated_clusters),
4256 fb_flags);
4257 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004258 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004259 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004260
Alex Tomasa86c6182006-10-11 01:21:03 -07004261 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004262 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004263 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004264 if (allocated > map->m_len)
4265 allocated = map->m_len;
4266 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004267
Jan Karab436b9b2009-12-08 23:51:10 -05004268 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004269 * Reduce the reserved cluster count to reflect successful deferred
4270 * allocation of delayed allocated clusters or direct allocation of
4271 * clusters discovered to be delayed allocated. Once allocated, a
4272 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004273 */
Eric Whitney29711482020-03-11 16:51:25 -04004274 if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) {
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004275 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004276 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004277 * When allocating delayed allocated clusters, simply
4278 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004279 */
4280 ext4_da_update_reserve_space(inode, allocated_clusters,
4281 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004282 } else {
4283 ext4_lblk_t lblk, len;
4284 unsigned int n;
4285
4286 /*
4287 * When allocating non-delayed allocated clusters
4288 * (from fallocate, filemap, DIO, or clusters
4289 * allocated when delalloc has been disabled by
4290 * ext4_nonda_switch), reduce the reserved cluster
4291 * count by the number of allocated clusters that
4292 * have previously been delayed allocated. Quota
4293 * has been claimed by ext4_mb_new_blocks() above,
4294 * so release the quota reservations made for any
4295 * previously delayed allocated clusters.
4296 */
4297 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4298 len = allocated_clusters << sbi->s_cluster_bits;
4299 n = ext4_es_delayed_clu(inode, lblk, len);
4300 if (n > 0)
4301 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004302 }
4303 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004304
4305 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004306 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004307 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004308 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004309 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004310 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004311 else
Jan Karab436b9b2009-12-08 23:51:10 -05004312 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004313out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004314 if (allocated > map->m_len)
4315 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004316 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004317 map->m_flags |= EXT4_MAP_MAPPED;
4318 map->m_pblk = newblock;
4319 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004320out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004321 ext4_ext_drop_refs(path);
4322 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004323
Theodore Ts'o63b99962013-07-16 10:28:47 -04004324 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4325 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004326 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004327}
4328
Theodore Ts'od0abb362016-11-13 22:02:28 -05004329int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004330{
Alex Tomasa86c6182006-10-11 01:21:03 -07004331 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004332 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004333 int err = 0;
4334
4335 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004336 * TODO: optimization is possible here.
4337 * Probably we need not scan at all,
4338 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004339 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004340
4341 /* we have to know where to truncate from in crash case */
4342 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004343 err = ext4_mark_inode_dirty(handle, inode);
4344 if (err)
4345 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004346
4347 last_block = (inode->i_size + sb->s_blocksize - 1)
4348 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004349retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004350 err = ext4_es_remove_extent(inode, last_block,
4351 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004352 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004353 cond_resched();
4354 congestion_wait(BLK_RW_ASYNC, HZ/50);
4355 goto retry;
4356 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004357 if (err)
4358 return err;
4359 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004360}
4361
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004362static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004363 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004364 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004365{
Al Viro496ad9a2013-01-23 17:07:38 -05004366 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004367 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004368 int ret = 0;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004369 int ret2 = 0, ret3 = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004370 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004371 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004372 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004373 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004374 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004375
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004376 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004377 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004378 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004379 /*
4380 * Don't normalize the request if it can fit in one extent so
4381 * that it doesn't get unnecessarily split into multiple
4382 * extents.
4383 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004384 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004385 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004386
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004387 /*
4388 * credits to insert 1 extent into extent tree
4389 */
4390 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004391 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004392
Amit Aroraa2df2a62007-07-17 21:42:41 -04004393retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004394 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004395 /*
4396 * Recalculate credits when extent tree depth changes.
4397 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004398 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004399 credits = ext4_chunk_trans_blocks(inode, len);
4400 depth = ext_depth(inode);
4401 }
4402
Theodore Ts'o9924a922013-02-08 21:59:22 -05004403 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4404 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004405 if (IS_ERR(handle)) {
4406 ret = PTR_ERR(handle);
4407 break;
4408 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004409 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004410 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004411 ext4_debug("inode #%lu: block %u: len %u: "
4412 "ext4_ext_map_blocks returned %d",
4413 inode->i_ino, map.m_lblk,
4414 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004415 ext4_mark_inode_dirty(handle, inode);
4416 ret2 = ext4_journal_stop(handle);
4417 break;
4418 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004419 map.m_lblk += ret;
4420 map.m_len = len = len - ret;
4421 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004422 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004423 if (new_size) {
4424 if (epos > new_size)
4425 epos = new_size;
4426 if (ext4_update_inode_size(inode, epos) & 0x1)
4427 inode->i_mtime = inode->i_ctime;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004428 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004429 ret2 = ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004430 ext4_update_inode_fsync_trans(handle, inode, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004431 ret3 = ext4_journal_stop(handle);
4432 ret2 = ret3 ? ret3 : ret2;
4433 if (unlikely(ret2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004434 break;
4435 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004436 if (ret == -ENOSPC &&
4437 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4438 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004439 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004440 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004441
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004442 return ret > 0 ? ret2 : ret;
4443}
4444
Eric Biggers43f81672019-12-31 12:04:40 -06004445static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
4446
4447static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len);
4448
Lukas Czernerb8a86842014-03-18 18:05:35 -04004449static long ext4_zero_range(struct file *file, loff_t offset,
4450 loff_t len, int mode)
4451{
4452 struct inode *inode = file_inode(file);
4453 handle_t *handle = NULL;
4454 unsigned int max_blocks;
4455 loff_t new_size = 0;
4456 int ret = 0;
4457 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004458 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004459 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004460 loff_t start, end;
4461 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004462 unsigned int blkbits = inode->i_blkbits;
4463
4464 trace_ext4_zero_range(inode, offset, len, mode);
4465
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004466 /* Call ext4_force_commit to flush all data in case of data=journal. */
4467 if (ext4_should_journal_data(inode)) {
4468 ret = ext4_force_commit(inode->i_sb);
4469 if (ret)
4470 return ret;
4471 }
4472
Lukas Czernerb8a86842014-03-18 18:05:35 -04004473 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004474 * Round up offset. This is not fallocate, we neet to zero out
4475 * blocks, so convert interior block aligned part of the range to
4476 * unwritten and possibly manually zero out unaligned parts of the
4477 * range.
4478 */
4479 start = round_up(offset, 1 << blkbits);
4480 end = round_down((offset + len), 1 << blkbits);
4481
4482 if (start < offset || end > offset + len)
4483 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004484 partial_begin = offset & ((1 << blkbits) - 1);
4485 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004486
4487 lblk = start >> blkbits;
4488 max_blocks = (end >> blkbits);
4489 if (max_blocks < lblk)
4490 max_blocks = 0;
4491 else
4492 max_blocks -= lblk;
4493
Al Viro59551022016-01-22 15:40:57 -05004494 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004495
4496 /*
4497 * Indirect files do not support unwritten extnets
4498 */
4499 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4500 ret = -EOPNOTSUPP;
4501 goto out_mutex;
4502 }
4503
4504 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004505 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004506 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004507 new_size = offset + len;
4508 ret = inode_newsize_ok(inode, new_size);
4509 if (ret)
4510 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004511 }
4512
Lukas Czerner0f2af212015-04-03 00:09:13 -04004513 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004514
Jan Kara17048e82015-12-07 14:29:17 -05004515 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004516 inode_dio_wait(inode);
4517
Lukas Czerner0f2af212015-04-03 00:09:13 -04004518 /* Preallocate the range including the unaligned edges */
4519 if (partial_begin || partial_end) {
4520 ret = ext4_alloc_file_blocks(file,
4521 round_down(offset, 1 << blkbits) >> blkbits,
4522 (round_up((offset + len), 1 << blkbits) -
4523 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004524 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004525 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004526 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004527
4528 }
4529
4530 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004531 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004532 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4533 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004534
Jan Karaea3d7202015-12-07 14:28:03 -05004535 /*
4536 * Prevent page faults from reinstantiating pages we have
4537 * released from page cache.
4538 */
4539 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004540
4541 ret = ext4_break_layouts(inode);
4542 if (ret) {
4543 up_write(&EXT4_I(inode)->i_mmap_sem);
4544 goto out_mutex;
4545 }
4546
Jan Kara01127842015-12-07 14:34:49 -05004547 ret = ext4_update_disksize_before_punch(inode, offset, len);
4548 if (ret) {
4549 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004550 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004551 }
Jan Karaea3d7202015-12-07 14:28:03 -05004552 /* Now release the pages and zero block aligned part of pages */
4553 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004554 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004555
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004556 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004557 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004558 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004559 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004560 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004561 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004562 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004563 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004564
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004565 /*
4566 * In worst case we have to writeout two nonadjacent unwritten
4567 * blocks and update the inode
4568 */
4569 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4570 if (ext4_should_journal_data(inode))
4571 credits += 2;
4572 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004573 if (IS_ERR(handle)) {
4574 ret = PTR_ERR(handle);
4575 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004576 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004577 }
4578
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004579 inode->i_mtime = inode->i_ctime = current_time(inode);
Eric Whitney4337ecd2020-02-11 16:02:16 -05004580 if (new_size)
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004581 ext4_update_inode_size(inode, new_size);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004582 ret = ext4_mark_inode_dirty(handle, inode);
4583 if (unlikely(ret))
4584 goto out_handle;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004585
4586 /* Zero out partial block at the edges of the range */
4587 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004588 if (ret >= 0)
4589 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004590
4591 if (file->f_flags & O_SYNC)
4592 ext4_handle_sync(handle);
4593
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004594out_handle:
Lukas Czernerb8a86842014-03-18 18:05:35 -04004595 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004596out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004597 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004598 return ret;
4599}
4600
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004601/*
4602 * preallocate space for a file. This implements ext4's fallocate file
4603 * operation, which gets called from sys_fallocate system call.
4604 * For block-mapped files, posix_fallocate should fall back to the method
4605 * of writing zeroes to the required new blocks (the same behavior which is
4606 * expected for file systems which do not support fallocate() system call).
4607 */
4608long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4609{
4610 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004611 loff_t new_size = 0;
4612 unsigned int max_blocks;
4613 int ret = 0;
4614 int flags;
4615 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004616 unsigned int blkbits = inode->i_blkbits;
4617
Michael Halcrow2058f832015-04-12 00:55:10 -04004618 /*
4619 * Encrypted inodes can't handle collapse range or insert
4620 * range since we would need to re-encrypt blocks with a
4621 * different IV or XTS tweak (which are based on the logical
4622 * block number).
Michael Halcrow2058f832015-04-12 00:55:10 -04004623 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304624 if (IS_ENCRYPTED(inode) &&
Eric Biggers457b1e32019-12-26 09:42:16 -06004625 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004626 return -EOPNOTSUPP;
4627
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004628 /* Return error if mode is not supported */
4629 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004630 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4631 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004632 return -EOPNOTSUPP;
4633
4634 if (mode & FALLOC_FL_PUNCH_HOLE)
4635 return ext4_punch_hole(inode, offset, len);
4636
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004637 ret = ext4_convert_inline_data(inode);
4638 if (ret)
4639 return ret;
4640
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004641 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4642 return ext4_collapse_range(inode, offset, len);
4643
Namjae Jeon331573f2015-06-09 01:55:03 -04004644 if (mode & FALLOC_FL_INSERT_RANGE)
4645 return ext4_insert_range(inode, offset, len);
4646
Lukas Czernerb8a86842014-03-18 18:05:35 -04004647 if (mode & FALLOC_FL_ZERO_RANGE)
4648 return ext4_zero_range(file, offset, len, mode);
4649
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004650 trace_ext4_fallocate_enter(inode, offset, len, mode);
4651 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004652
Fabian Frederick518eaa62016-09-15 11:55:01 -04004653 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004654 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004655
Al Viro59551022016-01-22 15:40:57 -05004656 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004657
Davide Italiano280227a2015-05-02 23:21:15 -04004658 /*
4659 * We only support preallocation for extent-based files only
4660 */
4661 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4662 ret = -EOPNOTSUPP;
4663 goto out;
4664 }
4665
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004666 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004667 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004668 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004669 new_size = offset + len;
4670 ret = inode_newsize_ok(inode, new_size);
4671 if (ret)
4672 goto out;
4673 }
4674
Jan Kara17048e82015-12-07 14:29:17 -05004675 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004676 inode_dio_wait(inode);
4677
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004678 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004679 if (ret)
4680 goto out;
4681
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004682 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4683 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4684 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004685 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004686out:
Al Viro59551022016-01-22 15:40:57 -05004687 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004688 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4689 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004690}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004691
4692/*
Mingming Cao00314622009-09-28 15:49:08 -04004693 * This function convert a range of blocks to written extents
4694 * The caller of this function will pass the start offset and the size.
4695 * all unwritten extents within this range will be converted to
4696 * written extents.
4697 *
4698 * This function is called from the direct IO end io call back
4699 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004700 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004701 */
Jan Kara6b523df2013-06-04 13:21:11 -04004702int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4703 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004704{
Mingming Cao00314622009-09-28 15:49:08 -04004705 unsigned int max_blocks;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004706 int ret = 0, ret2 = 0, ret3 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004707 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304708 unsigned int blkbits = inode->i_blkbits;
4709 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004710
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004711 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004712 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4713
Ritesh Harjania00713e2019-10-16 13:07:08 +05304714 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004715 /*
4716 * credits to insert 1 extent into extent tree
4717 */
4718 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4719 }
Mingming Cao00314622009-09-28 15:49:08 -04004720 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004721 map.m_lblk += ret;
4722 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04004723 if (credits) {
4724 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4725 credits);
4726 if (IS_ERR(handle)) {
4727 ret = PTR_ERR(handle);
4728 break;
4729 }
Mingming Cao00314622009-09-28 15:49:08 -04004730 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004731 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004732 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05004733 if (ret <= 0)
4734 ext4_warning(inode->i_sb,
4735 "inode #%lu: block %u: len %u: "
4736 "ext4_ext_map_blocks returned %d",
4737 inode->i_ino, map.m_lblk,
4738 map.m_len, ret);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004739 ret2 = ext4_mark_inode_dirty(handle, inode);
4740 if (credits) {
4741 ret3 = ext4_journal_stop(handle);
4742 if (unlikely(ret3))
4743 ret2 = ret3;
4744 }
4745
Jan Kara6b523df2013-06-04 13:21:11 -04004746 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04004747 break;
4748 }
4749 return ret > 0 ? ret2 : ret;
4750}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004751
Ritesh Harjania00713e2019-10-16 13:07:08 +05304752int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
4753{
4754 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304755 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304756
4757 /*
4758 * This is somewhat ugly but the idea is clear: When transaction is
4759 * reserved, everything goes into it. Otherwise we rather start several
4760 * smaller transactions for conversion of each extent separately.
4761 */
4762 if (handle) {
4763 handle = ext4_journal_start_reserved(handle,
4764 EXT4_HT_EXT_CONVERT);
4765 if (IS_ERR(handle))
4766 return PTR_ERR(handle);
4767 }
4768
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304769 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
4770 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
4771 io_end_vec->offset,
4772 io_end_vec->size);
4773 if (ret)
4774 break;
4775 }
4776
Ritesh Harjania00713e2019-10-16 13:07:08 +05304777 if (handle)
4778 err = ext4_journal_stop(handle);
4779
4780 return ret < 0 ? ret : err;
4781}
4782
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304783static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004784{
4785 __u64 physical = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304786 __u64 length = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004787 int blockbits = inode->i_sb->s_blocksize_bits;
4788 int error = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304789 u16 iomap_type;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004790
4791 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004792 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004793 struct ext4_iloc iloc;
4794 int offset; /* offset of xattr in inode */
4795
4796 error = ext4_get_inode_loc(inode, &iloc);
4797 if (error)
4798 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04004799 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004800 offset = EXT4_GOOD_OLD_INODE_SIZE +
4801 EXT4_I(inode)->i_extra_isize;
4802 physical += offset;
4803 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004804 brelse(iloc.bh);
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304805 iomap_type = IOMAP_INLINE;
4806 } else if (EXT4_I(inode)->i_file_acl) { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04004807 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004808 length = inode->i_sb->s_blocksize;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304809 iomap_type = IOMAP_MAPPED;
4810 } else {
4811 /* no in-inode or external block for xattr, so return -ENOENT */
4812 error = -ENOENT;
4813 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004814 }
4815
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304816 iomap->addr = physical;
4817 iomap->offset = 0;
4818 iomap->length = length;
4819 iomap->type = iomap_type;
4820 iomap->flags = 0;
4821out:
4822 return error;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004823}
4824
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304825static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset,
4826 loff_t length, unsigned flags,
4827 struct iomap *iomap, struct iomap *srcmap)
4828{
4829 int error;
4830
4831 error = ext4_iomap_xattr_fiemap(inode, iomap);
4832 if (error == 0 && (offset >= iomap->length))
4833 error = -ENOENT;
4834 return error;
4835}
4836
4837static const struct iomap_ops ext4_iomap_xattr_ops = {
4838 .iomap_begin = ext4_iomap_xattr_begin,
4839};
4840
4841static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4842 __u64 start, __u64 len, bool from_es_cache)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004843{
4844 ext4_lblk_t start_blk;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304845 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004846 int error = 0;
4847
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004848 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
4849 error = ext4_ext_precache(inode);
4850 if (error)
4851 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004852 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004853 }
4854
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304855 if (from_es_cache)
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004856 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304857
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004858 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004859 return -EBADR;
4860
4861 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304862 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
4863 error = iomap_fiemap(inode, fieinfo, start, len,
4864 &ext4_iomap_xattr_ops);
4865 } else if (!from_es_cache) {
4866 error = iomap_fiemap(inode, fieinfo, start, len,
4867 &ext4_iomap_report_ops);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004868 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004869 ext4_lblk_t len_blks;
4870 __u64 last_blk;
4871
Eric Sandeen6873fa02008-10-07 00:46:36 -04004872 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004873 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004874 if (last_blk >= EXT_MAX_BLOCKS)
4875 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004876 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004877
4878 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004879 * Walk the extent tree gathering extent information
4880 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004881 */
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304882 error = ext4_fill_es_cache_info(inode, start_blk, len_blks,
4883 fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004884 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04004885 return error;
4886}
Namjae Jeon9eb79482014-02-23 15:18:59 -05004887
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004888int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4889 __u64 start, __u64 len)
4890{
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304891 return _ext4_fiemap(inode, fieinfo, start, len, false);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004892}
4893
4894int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
4895 __u64 start, __u64 len)
4896{
4897 if (ext4_has_inline_data(inode)) {
4898 int has_inline;
4899
4900 down_read(&EXT4_I(inode)->xattr_sem);
4901 has_inline = ext4_has_inline_data(inode);
4902 up_read(&EXT4_I(inode)->xattr_sem);
4903 if (has_inline)
4904 return 0;
4905 }
4906
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304907 return _ext4_fiemap(inode, fieinfo, start, len, true);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004908}
4909
4910
Namjae Jeon9eb79482014-02-23 15:18:59 -05004911/*
4912 * ext4_access_path:
4913 * Function to access the path buffer for marking it dirty.
4914 * It also checks if there are sufficient credits left in the journal handle
4915 * to update path.
4916 */
4917static int
4918ext4_access_path(handle_t *handle, struct inode *inode,
4919 struct ext4_ext_path *path)
4920{
4921 int credits, err;
4922
4923 if (!ext4_handle_valid(handle))
4924 return 0;
4925
4926 /*
4927 * Check if need to extend journal credits
4928 * 3 for leaf, sb, and inode plus 2 (bmap and group
4929 * descriptor) for each block group; assume two block
4930 * groups
4931 */
Jan Karaa4130362019-11-05 17:44:16 +01004932 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01004933 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01004934 if (err < 0)
4935 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004936
4937 err = ext4_ext_get_access(handle, inode, path);
4938 return err;
4939}
4940
4941/*
4942 * ext4_ext_shift_path_extents:
4943 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04004944 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
4945 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05004946 */
4947static int
4948ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
4949 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04004950 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05004951{
4952 int depth, err = 0;
4953 struct ext4_extent *ex_start, *ex_last;
zhengbin4756ee12019-12-25 10:45:59 +08004954 bool update = false;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004955 depth = path->p_depth;
4956
4957 while (depth >= 0) {
4958 if (depth == path->p_depth) {
4959 ex_start = path[depth].p_ext;
4960 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004961 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004962
4963 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05004964
4965 err = ext4_access_path(handle, inode, path + depth);
4966 if (err)
4967 goto out;
4968
4969 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
zhengbin4756ee12019-12-25 10:45:59 +08004970 update = true;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004971
Namjae Jeon9eb79482014-02-23 15:18:59 -05004972 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04004973 if (SHIFT == SHIFT_LEFT) {
4974 le32_add_cpu(&ex_start->ee_block,
4975 -shift);
4976 /* Try to merge to the left. */
4977 if ((ex_start >
4978 EXT_FIRST_EXTENT(path[depth].p_hdr))
4979 &&
4980 ext4_ext_try_to_merge_right(inode,
4981 path, ex_start - 1))
4982 ex_last--;
4983 else
4984 ex_start++;
4985 } else {
4986 le32_add_cpu(&ex_last->ee_block, shift);
4987 ext4_ext_try_to_merge_right(inode, path,
4988 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04004989 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04004990 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05004991 }
4992 err = ext4_ext_dirty(handle, inode, path + depth);
4993 if (err)
4994 goto out;
4995
4996 if (--depth < 0 || !update)
4997 break;
4998 }
4999
5000 /* Update index too */
5001 err = ext4_access_path(handle, inode, path + depth);
5002 if (err)
5003 goto out;
5004
Namjae Jeon331573f2015-06-09 01:55:03 -04005005 if (SHIFT == SHIFT_LEFT)
5006 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5007 else
5008 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005009 err = ext4_ext_dirty(handle, inode, path + depth);
5010 if (err)
5011 goto out;
5012
5013 /* we are done if current index is not a starting index */
5014 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5015 break;
5016
5017 depth--;
5018 }
5019
5020out:
5021 return err;
5022}
5023
5024/*
5025 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005026 * All the extents which lies in the range from @start to the last allocated
5027 * block for the @inode are shifted either towards left or right (depending
5028 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005029 * On success, 0 is returned, error otherwise.
5030 */
5031static int
5032ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005033 ext4_lblk_t start, ext4_lblk_t shift,
5034 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005035{
5036 struct ext4_ext_path *path;
5037 int ret = 0, depth;
5038 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005039 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005040
5041 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005042 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5043 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005044 if (IS_ERR(path))
5045 return PTR_ERR(path);
5046
5047 depth = path->p_depth;
5048 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005049 if (!extent)
5050 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005051
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005052 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005053
Namjae Jeon331573f2015-06-09 01:55:03 -04005054 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005055 * For left shifts, make sure the hole on the left is big enough to
5056 * accommodate the shift. For right shifts, make sure the last extent
5057 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005058 */
5059 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005060 path = ext4_find_extent(inode, start - 1, &path,
5061 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005062 if (IS_ERR(path))
5063 return PTR_ERR(path);
5064 depth = path->p_depth;
5065 extent = path[depth].p_ext;
5066 if (extent) {
5067 ex_start = le32_to_cpu(extent->ee_block);
5068 ex_end = le32_to_cpu(extent->ee_block) +
5069 ext4_ext_get_actual_len(extent);
5070 } else {
5071 ex_start = 0;
5072 ex_end = 0;
5073 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005074
Namjae Jeon331573f2015-06-09 01:55:03 -04005075 if ((start == ex_start && shift > ex_start) ||
5076 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005077 ret = -EINVAL;
5078 goto out;
5079 }
5080 } else {
5081 if (shift > EXT_MAX_BLOCKS -
5082 (stop + ext4_ext_get_actual_len(extent))) {
5083 ret = -EINVAL;
5084 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005085 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005086 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005087
Namjae Jeon331573f2015-06-09 01:55:03 -04005088 /*
5089 * In case of left shift, iterator points to start and it is increased
5090 * till we reach stop. In case of right shift, iterator points to stop
5091 * and it is decreased till we reach start.
5092 */
5093 if (SHIFT == SHIFT_LEFT)
5094 iterator = &start;
5095 else
5096 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005097
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005098 /*
5099 * Its safe to start updating extents. Start and stop are unsigned, so
5100 * in case of right shift if extent with 0 block is reached, iterator
5101 * becomes NULL to indicate the end of the loop.
5102 */
5103 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005104 path = ext4_find_extent(inode, *iterator, &path,
5105 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005106 if (IS_ERR(path))
5107 return PTR_ERR(path);
5108 depth = path->p_depth;
5109 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005110 if (!extent) {
5111 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005112 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005113 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005114 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005115 if (SHIFT == SHIFT_LEFT && *iterator >
5116 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005117 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005118 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5119 path[depth].p_ext++;
5120 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005121 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005122 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005123 }
5124 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005125
5126 if (SHIFT == SHIFT_LEFT) {
5127 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5128 *iterator = le32_to_cpu(extent->ee_block) +
5129 ext4_ext_get_actual_len(extent);
5130 } else {
5131 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005132 if (le32_to_cpu(extent->ee_block) > 0)
5133 *iterator = le32_to_cpu(extent->ee_block) - 1;
5134 else
5135 /* Beginning is reached, end of the loop */
5136 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005137 /* Update path extent in case we need to stop */
5138 while (le32_to_cpu(extent->ee_block) < start)
5139 extent++;
5140 path[depth].p_ext = extent;
5141 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005142 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005143 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005144 if (ret)
5145 break;
5146 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005147out:
5148 ext4_ext_drop_refs(path);
5149 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005150 return ret;
5151}
5152
5153/*
5154 * ext4_collapse_range:
5155 * This implements the fallocate's collapse range functionality for ext4
5156 * Returns: 0 and non-zero on error.
5157 */
Eric Biggers43f81672019-12-31 12:04:40 -06005158static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005159{
5160 struct super_block *sb = inode->i_sb;
5161 ext4_lblk_t punch_start, punch_stop;
5162 handle_t *handle;
5163 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005164 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005165 int ret;
5166
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005167 /*
5168 * We need to test this early because xfstests assumes that a
5169 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5170 * system does not support collapse range.
5171 */
5172 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5173 return -EOPNOTSUPP;
5174
Eric Biggers9b02e492019-12-31 12:04:38 -06005175 /* Collapse range works only on fs cluster size aligned regions. */
5176 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005177 return -EINVAL;
5178
Namjae Jeon9eb79482014-02-23 15:18:59 -05005179 trace_ext4_collapse_range(inode, offset, len);
5180
5181 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5182 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5183
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005184 /* Call ext4_force_commit to flush all data in case of data=journal. */
5185 if (ext4_should_journal_data(inode)) {
5186 ret = ext4_force_commit(inode->i_sb);
5187 if (ret)
5188 return ret;
5189 }
5190
Al Viro59551022016-01-22 15:40:57 -05005191 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005192 /*
5193 * There is no need to overlap collapse range with EOF, in which case
5194 * it is effectively a truncate operation
5195 */
Eric Biggers9b02e492019-12-31 12:04:38 -06005196 if (offset + len >= inode->i_size) {
Lukas Czerner23fffa92014-04-12 09:56:41 -04005197 ret = -EINVAL;
5198 goto out_mutex;
5199 }
5200
Namjae Jeon9eb79482014-02-23 15:18:59 -05005201 /* Currently just for extent based files */
5202 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5203 ret = -EOPNOTSUPP;
5204 goto out_mutex;
5205 }
5206
Namjae Jeon9eb79482014-02-23 15:18:59 -05005207 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005208 inode_dio_wait(inode);
5209
Jan Karaea3d7202015-12-07 14:28:03 -05005210 /*
5211 * Prevent page faults from reinstantiating pages we have released from
5212 * page cache.
5213 */
5214 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005215
5216 ret = ext4_break_layouts(inode);
5217 if (ret)
5218 goto out_mmap;
5219
Jan Kara32ebffd2015-12-07 14:31:11 -05005220 /*
5221 * Need to round down offset to be aligned with page size boundary
5222 * for page size > block size.
5223 */
5224 ioffset = round_down(offset, PAGE_SIZE);
5225 /*
5226 * Write tail of the last page before removed range since it will get
5227 * removed from the page cache below.
5228 */
5229 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5230 if (ret)
5231 goto out_mmap;
5232 /*
5233 * Write data that will be shifted to preserve them when discarding
5234 * page cache below. We are also protected from pages becoming dirty
5235 * by i_mmap_sem.
5236 */
5237 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5238 LLONG_MAX);
5239 if (ret)
5240 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005241 truncate_pagecache(inode, ioffset);
5242
Namjae Jeon9eb79482014-02-23 15:18:59 -05005243 credits = ext4_writepage_trans_blocks(inode);
5244 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5245 if (IS_ERR(handle)) {
5246 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005247 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005248 }
5249
5250 down_write(&EXT4_I(inode)->i_data_sem);
5251 ext4_discard_preallocations(inode);
5252
5253 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005254 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005255 if (ret) {
5256 up_write(&EXT4_I(inode)->i_data_sem);
5257 goto out_stop;
5258 }
5259
5260 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5261 if (ret) {
5262 up_write(&EXT4_I(inode)->i_data_sem);
5263 goto out_stop;
5264 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005265 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005266
5267 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005268 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005269 if (ret) {
5270 up_write(&EXT4_I(inode)->i_data_sem);
5271 goto out_stop;
5272 }
5273
Eric Biggers9b02e492019-12-31 12:04:38 -06005274 new_size = inode->i_size - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005275 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005276 EXT4_I(inode)->i_disksize = new_size;
5277
Namjae Jeon9eb79482014-02-23 15:18:59 -05005278 up_write(&EXT4_I(inode)->i_data_sem);
5279 if (IS_SYNC(inode))
5280 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005281 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005282 ret = ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005283 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005284
5285out_stop:
5286 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005287out_mmap:
5288 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005289out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005290 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005291 return ret;
5292}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005293
Namjae Jeon331573f2015-06-09 01:55:03 -04005294/*
5295 * ext4_insert_range:
5296 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5297 * The data blocks starting from @offset to the EOF are shifted by @len
5298 * towards right to create a hole in the @inode. Inode size is increased
5299 * by len bytes.
5300 * Returns 0 on success, error otherwise.
5301 */
Eric Biggers43f81672019-12-31 12:04:40 -06005302static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon331573f2015-06-09 01:55:03 -04005303{
5304 struct super_block *sb = inode->i_sb;
5305 handle_t *handle;
5306 struct ext4_ext_path *path;
5307 struct ext4_extent *extent;
5308 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5309 unsigned int credits, ee_len;
5310 int ret = 0, depth, split_flag = 0;
5311 loff_t ioffset;
5312
5313 /*
5314 * We need to test this early because xfstests assumes that an
5315 * insert range of (0, 1) will return EOPNOTSUPP if the file
5316 * system does not support insert range.
5317 */
5318 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5319 return -EOPNOTSUPP;
5320
Eric Biggers9b02e492019-12-31 12:04:38 -06005321 /* Insert range works only on fs cluster size aligned regions. */
5322 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon331573f2015-06-09 01:55:03 -04005323 return -EINVAL;
5324
Namjae Jeon331573f2015-06-09 01:55:03 -04005325 trace_ext4_insert_range(inode, offset, len);
5326
5327 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5328 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5329
5330 /* Call ext4_force_commit to flush all data in case of data=journal */
5331 if (ext4_should_journal_data(inode)) {
5332 ret = ext4_force_commit(inode->i_sb);
5333 if (ret)
5334 return ret;
5335 }
5336
Al Viro59551022016-01-22 15:40:57 -05005337 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005338 /* Currently just for extent based files */
5339 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5340 ret = -EOPNOTSUPP;
5341 goto out_mutex;
5342 }
5343
Eric Biggers9b02e492019-12-31 12:04:38 -06005344 /* Check whether the maximum file size would be exceeded */
5345 if (len > inode->i_sb->s_maxbytes - inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005346 ret = -EFBIG;
5347 goto out_mutex;
5348 }
5349
Eric Biggers9b02e492019-12-31 12:04:38 -06005350 /* Offset must be less than i_size */
5351 if (offset >= inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005352 ret = -EINVAL;
5353 goto out_mutex;
5354 }
5355
Namjae Jeon331573f2015-06-09 01:55:03 -04005356 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005357 inode_dio_wait(inode);
5358
Jan Karaea3d7202015-12-07 14:28:03 -05005359 /*
5360 * Prevent page faults from reinstantiating pages we have released from
5361 * page cache.
5362 */
5363 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005364
5365 ret = ext4_break_layouts(inode);
5366 if (ret)
5367 goto out_mmap;
5368
Jan Kara32ebffd2015-12-07 14:31:11 -05005369 /*
5370 * Need to round down to align start offset to page size boundary
5371 * for page size > block size.
5372 */
5373 ioffset = round_down(offset, PAGE_SIZE);
5374 /* Write out all dirty pages */
5375 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5376 LLONG_MAX);
5377 if (ret)
5378 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005379 truncate_pagecache(inode, ioffset);
5380
Namjae Jeon331573f2015-06-09 01:55:03 -04005381 credits = ext4_writepage_trans_blocks(inode);
5382 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5383 if (IS_ERR(handle)) {
5384 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005385 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005386 }
5387
5388 /* Expand file to avoid data loss if there is error while shifting */
5389 inode->i_size += len;
5390 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005391 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005392 ret = ext4_mark_inode_dirty(handle, inode);
5393 if (ret)
5394 goto out_stop;
5395
5396 down_write(&EXT4_I(inode)->i_data_sem);
5397 ext4_discard_preallocations(inode);
5398
5399 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5400 if (IS_ERR(path)) {
5401 up_write(&EXT4_I(inode)->i_data_sem);
5402 goto out_stop;
5403 }
5404
5405 depth = ext_depth(inode);
5406 extent = path[depth].p_ext;
5407 if (extent) {
5408 ee_start_lblk = le32_to_cpu(extent->ee_block);
5409 ee_len = ext4_ext_get_actual_len(extent);
5410
5411 /*
5412 * If offset_lblk is not the starting block of extent, split
5413 * the extent @offset_lblk
5414 */
5415 if ((offset_lblk > ee_start_lblk) &&
5416 (offset_lblk < (ee_start_lblk + ee_len))) {
5417 if (ext4_ext_is_unwritten(extent))
5418 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5419 EXT4_EXT_MARK_UNWRIT2;
5420 ret = ext4_split_extent_at(handle, inode, &path,
5421 offset_lblk, split_flag,
5422 EXT4_EX_NOCACHE |
5423 EXT4_GET_BLOCKS_PRE_IO |
5424 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5425 }
5426
5427 ext4_ext_drop_refs(path);
5428 kfree(path);
5429 if (ret < 0) {
5430 up_write(&EXT4_I(inode)->i_data_sem);
5431 goto out_stop;
5432 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005433 } else {
5434 ext4_ext_drop_refs(path);
5435 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005436 }
5437
5438 ret = ext4_es_remove_extent(inode, offset_lblk,
5439 EXT_MAX_BLOCKS - offset_lblk);
5440 if (ret) {
5441 up_write(&EXT4_I(inode)->i_data_sem);
5442 goto out_stop;
5443 }
5444
5445 /*
5446 * if offset_lblk lies in a hole which is at start of file, use
5447 * ee_start_lblk to shift extents
5448 */
5449 ret = ext4_ext_shift_extents(inode, handle,
5450 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5451 len_lblk, SHIFT_RIGHT);
5452
5453 up_write(&EXT4_I(inode)->i_data_sem);
5454 if (IS_SYNC(inode))
5455 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005456 if (ret >= 0)
5457 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005458
5459out_stop:
5460 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005461out_mmap:
5462 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005463out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005464 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005465 return ret;
5466}
5467
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005468/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005469 * ext4_swap_extents() - Swap extents between two inodes
5470 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005471 * @inode1: First inode
5472 * @inode2: Second inode
5473 * @lblk1: Start block for first inode
5474 * @lblk2: Start block for second inode
5475 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005476 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005477 * @erp: Pointer to save error value
5478 *
5479 * This helper routine does exactly what is promise "swap extents". All other
5480 * stuff such as page-cache locking consistency, bh mapping consistency or
5481 * extent's data copying must be performed by caller.
5482 * Locking:
5483 * i_mutex is held for both inodes
5484 * i_data_sem is locked for write for both inodes
5485 * Assumptions:
5486 * All pages from requested range are locked for both inodes
5487 */
5488int
5489ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005490 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005491 ext4_lblk_t count, int unwritten, int *erp)
5492{
5493 struct ext4_ext_path *path1 = NULL;
5494 struct ext4_ext_path *path2 = NULL;
5495 int replaced_count = 0;
5496
5497 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5498 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005499 BUG_ON(!inode_is_locked(inode1));
5500 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005501
5502 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005503 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005504 return 0;
5505 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005506 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005507 return 0;
5508
5509 while (count) {
5510 struct ext4_extent *ex1, *ex2, tmp_ex;
5511 ext4_lblk_t e1_blk, e2_blk;
5512 int e1_len, e2_len, len;
5513 int split = 0;
5514
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005515 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305516 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005517 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005518 path1 = NULL;
5519 finish:
5520 count = 0;
5521 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005522 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005523 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305524 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005525 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005526 path2 = NULL;
5527 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005528 }
5529 ex1 = path1[path1->p_depth].p_ext;
5530 ex2 = path2[path2->p_depth].p_ext;
5531 /* Do we have somthing to swap ? */
5532 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005533 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005534
5535 e1_blk = le32_to_cpu(ex1->ee_block);
5536 e2_blk = le32_to_cpu(ex2->ee_block);
5537 e1_len = ext4_ext_get_actual_len(ex1);
5538 e2_len = ext4_ext_get_actual_len(ex2);
5539
5540 /* Hole handling */
5541 if (!in_range(lblk1, e1_blk, e1_len) ||
5542 !in_range(lblk2, e2_blk, e2_len)) {
5543 ext4_lblk_t next1, next2;
5544
5545 /* if hole after extent, then go to next extent */
5546 next1 = ext4_ext_next_allocated_block(path1);
5547 next2 = ext4_ext_next_allocated_block(path2);
5548 /* If hole before extent, then shift to that extent */
5549 if (e1_blk > lblk1)
5550 next1 = e1_blk;
5551 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005552 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005553 /* Do we have something to swap */
5554 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005555 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005556 /* Move to the rightest boundary */
5557 len = next1 - lblk1;
5558 if (len < next2 - lblk2)
5559 len = next2 - lblk2;
5560 if (len > count)
5561 len = count;
5562 lblk1 += len;
5563 lblk2 += len;
5564 count -= len;
5565 goto repeat;
5566 }
5567
5568 /* Prepare left boundary */
5569 if (e1_blk < lblk1) {
5570 split = 1;
5571 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005572 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005573 if (unlikely(*erp))
5574 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005575 }
5576 if (e2_blk < lblk2) {
5577 split = 1;
5578 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005579 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005580 if (unlikely(*erp))
5581 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005582 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005583 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005584 * path must to be revalidated. */
5585 if (split)
5586 goto repeat;
5587
5588 /* Prepare right boundary */
5589 len = count;
5590 if (len > e1_blk + e1_len - lblk1)
5591 len = e1_blk + e1_len - lblk1;
5592 if (len > e2_blk + e2_len - lblk2)
5593 len = e2_blk + e2_len - lblk2;
5594
5595 if (len != e1_len) {
5596 split = 1;
5597 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005598 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005599 if (unlikely(*erp))
5600 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005601 }
5602 if (len != e2_len) {
5603 split = 1;
5604 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005605 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005606 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005607 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005608 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005609 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005610 * path must to be revalidated. */
5611 if (split)
5612 goto repeat;
5613
5614 BUG_ON(e2_len != e1_len);
5615 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005616 if (unlikely(*erp))
5617 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005618 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005619 if (unlikely(*erp))
5620 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005621
5622 /* Both extents are fully inside boundaries. Swap it now */
5623 tmp_ex = *ex1;
5624 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5625 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5626 ex1->ee_len = cpu_to_le16(e2_len);
5627 ex2->ee_len = cpu_to_le16(e1_len);
5628 if (unwritten)
5629 ext4_ext_mark_unwritten(ex2);
5630 if (ext4_ext_is_unwritten(&tmp_ex))
5631 ext4_ext_mark_unwritten(ex1);
5632
5633 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5634 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5635 *erp = ext4_ext_dirty(handle, inode2, path2 +
5636 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005637 if (unlikely(*erp))
5638 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005639 *erp = ext4_ext_dirty(handle, inode1, path1 +
5640 path1->p_depth);
5641 /*
5642 * Looks scarry ah..? second inode already points to new blocks,
5643 * and it was successfully dirtied. But luckily error may happen
5644 * only due to journal error, so full transaction will be
5645 * aborted anyway.
5646 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005647 if (unlikely(*erp))
5648 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005649 lblk1 += len;
5650 lblk2 += len;
5651 replaced_count += len;
5652 count -= len;
5653
5654 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005655 ext4_ext_drop_refs(path1);
5656 kfree(path1);
5657 ext4_ext_drop_refs(path2);
5658 kfree(path2);
5659 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005660 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005661 return replaced_count;
5662}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005663
5664/*
5665 * ext4_clu_mapped - determine whether any block in a logical cluster has
5666 * been mapped to a physical cluster
5667 *
5668 * @inode - file containing the logical cluster
5669 * @lclu - logical cluster of interest
5670 *
5671 * Returns 1 if any block in the logical cluster is mapped, signifying
5672 * that a physical cluster has been allocated for it. Otherwise,
5673 * returns 0. Can also return negative error codes. Derived from
5674 * ext4_ext_map_blocks().
5675 */
5676int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5677{
5678 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5679 struct ext4_ext_path *path;
5680 int depth, mapped = 0, err = 0;
5681 struct ext4_extent *extent;
5682 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5683
5684 /* search for the extent closest to the first block in the cluster */
5685 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5686 if (IS_ERR(path)) {
5687 err = PTR_ERR(path);
5688 path = NULL;
5689 goto out;
5690 }
5691
5692 depth = ext_depth(inode);
5693
5694 /*
5695 * A consistent leaf must not be empty. This situation is possible,
5696 * though, _during_ tree modification, and it's why an assert can't
5697 * be put in ext4_find_extent().
5698 */
5699 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
5700 EXT4_ERROR_INODE(inode,
5701 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
5702 (unsigned long) EXT4_C2B(sbi, lclu),
5703 depth, path[depth].p_block);
5704 err = -EFSCORRUPTED;
5705 goto out;
5706 }
5707
5708 extent = path[depth].p_ext;
5709
5710 /* can't be mapped if the extent tree is empty */
5711 if (extent == NULL)
5712 goto out;
5713
5714 first_lblk = le32_to_cpu(extent->ee_block);
5715 first_lclu = EXT4_B2C(sbi, first_lblk);
5716
5717 /*
5718 * Three possible outcomes at this point - found extent spanning
5719 * the target cluster, to the left of the target cluster, or to the
5720 * right of the target cluster. The first two cases are handled here.
5721 * The last case indicates the target cluster is not mapped.
5722 */
5723 if (lclu >= first_lclu) {
5724 last_lclu = EXT4_B2C(sbi, first_lblk +
5725 ext4_ext_get_actual_len(extent) - 1);
5726 if (lclu <= last_lclu) {
5727 mapped = 1;
5728 } else {
5729 first_lblk = ext4_ext_next_allocated_block(path);
5730 first_lclu = EXT4_B2C(sbi, first_lblk);
5731 if (lclu == first_lclu)
5732 mapped = 1;
5733 }
5734 }
5735
5736out:
5737 ext4_ext_drop_refs(path);
5738 kfree(path);
5739
5740 return err ? err : mapped;
5741}