blob: 969f4c030cf017649f48047d99e3890afa3013d0 [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0
Alex Tomasa86c6182006-10-11 01:21:03 -07002/*
3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4 * Written by Alex Tomas <alex@clusterfs.com>
5 *
6 * Architecture independence:
7 * Copyright (c) 2005, Bull S.A.
8 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
Alex Tomasa86c6182006-10-11 01:21:03 -07009 */
10
11/*
12 * Extents support for EXT4
13 *
14 * TODO:
15 * - ext4*_error() should be used in some situations
16 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
17 * - smart tree reduction
18 */
19
Alex Tomasa86c6182006-10-11 01:21:03 -070020#include <linux/fs.h>
21#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040022#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070023#include <linux/highuid.h>
24#include <linux/pagemap.h>
25#include <linux/quotaops.h>
26#include <linux/string.h>
27#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040029#include <linux/fiemap.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040030#include <linux/backing-dev.h>
Ritesh Harjanid3b6f232020-02-28 14:56:58 +053031#include <linux/iomap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040032#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050033#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050034#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070035
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040036#include <trace/events/ext4.h>
37
Lukas Czerner5f95d212012-03-19 23:03:19 -040038/*
39 * used by extent splitting.
40 */
41#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
42 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040043#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
44#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040045
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040046#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
47#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
48
Darrick J. Wong7ac59902012-04-29 18:37:10 -040049static __le32 ext4_extent_block_csum(struct inode *inode,
50 struct ext4_extent_header *eh)
51{
52 struct ext4_inode_info *ei = EXT4_I(inode);
53 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
54 __u32 csum;
55
56 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
57 EXT4_EXTENT_TAIL_OFFSET(eh));
58 return cpu_to_le32(csum);
59}
60
61static int ext4_extent_block_csum_verify(struct inode *inode,
62 struct ext4_extent_header *eh)
63{
64 struct ext4_extent_tail *et;
65
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040066 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040067 return 1;
68
69 et = find_ext4_extent_tail(eh);
70 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
71 return 0;
72 return 1;
73}
74
75static void ext4_extent_block_csum_set(struct inode *inode,
76 struct ext4_extent_header *eh)
77{
78 struct ext4_extent_tail *et;
79
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040080 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040081 return;
82
83 et = find_ext4_extent_tail(eh);
84 et->et_checksum = ext4_extent_block_csum(inode, eh);
85}
86
Lukas Czerner5f95d212012-03-19 23:03:19 -040087static int ext4_split_extent_at(handle_t *handle,
88 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040089 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -040090 ext4_lblk_t split,
91 int split_flag,
92 int flags);
93
Jan Karaa4130362019-11-05 17:44:16 +010094static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
Alex Tomasa86c6182006-10-11 01:21:03 -070095{
Theodore Ts'o7b808192016-04-25 23:13:17 -040096 /*
Jan Karaa4130362019-11-05 17:44:16 +010097 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
98 * moment, get_block can be called only for blocks inside i_size since
99 * page cache has been already dropped and writes are blocked by
100 * i_mutex. So we can safely drop the i_data_sem here.
Theodore Ts'o7b808192016-04-25 23:13:17 -0400101 */
Jan Karaa4130362019-11-05 17:44:16 +0100102 BUG_ON(EXT4_JOURNAL(inode) == NULL);
103 ext4_discard_preallocations(inode);
104 up_write(&EXT4_I(inode)->i_data_sem);
105 *dropped = 1;
106 return 0;
107}
Jan Kara487caee2009-08-17 22:17:20 -0400108
Jan Karaa4130362019-11-05 17:44:16 +0100109/*
110 * Make sure 'handle' has at least 'check_cred' credits. If not, restart
111 * transaction with 'restart_cred' credits. The function drops i_data_sem
112 * when restarting transaction and gets it after transaction is restarted.
113 *
114 * The function returns 0 on success, 1 if transaction had to be restarted,
115 * and < 0 in case of fatal error.
116 */
117int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
Jan Kara83448bd2019-11-05 17:44:29 +0100118 int check_cred, int restart_cred,
119 int revoke_cred)
Jan Karaa4130362019-11-05 17:44:16 +0100120{
121 int ret;
122 int dropped = 0;
123
124 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
Jan Kara83448bd2019-11-05 17:44:29 +0100125 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
Jan Karaa4130362019-11-05 17:44:16 +0100126 if (dropped)
127 down_write(&EXT4_I(inode)->i_data_sem);
128 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700129}
130
131/*
132 * could return:
133 * - EROFS
134 * - ENOMEM
135 */
136static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
137 struct ext4_ext_path *path)
138{
139 if (path->p_bh) {
140 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400141 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700142 return ext4_journal_get_write_access(handle, path->p_bh);
143 }
144 /* path points to leaf/index in inode body */
145 /* we use in-core data, no need to protect them */
146 return 0;
147}
148
149/*
150 * could return:
151 * - EROFS
152 * - ENOMEM
153 * - EIO
154 */
Eric Biggers43f81672019-12-31 12:04:40 -0600155static int __ext4_ext_dirty(const char *where, unsigned int line,
156 handle_t *handle, struct inode *inode,
157 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700158{
159 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400160
161 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700162 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400163 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700164 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400165 err = __ext4_handle_dirty_metadata(where, line, handle,
166 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700167 } else {
168 /* path points to leaf/index in inode body */
169 err = ext4_mark_inode_dirty(handle, inode);
170 }
171 return err;
172}
173
Eric Biggers43f81672019-12-31 12:04:40 -0600174#define ext4_ext_dirty(handle, inode, path) \
175 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
176
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700177static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700178 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500179 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700180{
Alex Tomasa86c6182006-10-11 01:21:03 -0700181 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400182 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700184
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500185 /*
186 * Try to predict block placement assuming that we are
187 * filling in a file which will eventually be
188 * non-sparse --- i.e., in the case of libbfd writing
189 * an ELF object sections out-of-order but in a way
190 * the eventually results in a contiguous object or
191 * executable file, or some database extending a table
192 * space file. However, this is actually somewhat
193 * non-ideal if we are writing a sparse file such as
194 * qemu or KVM writing a raw image file that is going
195 * to stay fairly sparse, since it will end up
196 * fragmenting the file system's free space. Maybe we
197 * should have some hueristics or some way to allow
198 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800199 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500200 * common.
201 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800202 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500203 if (ex) {
204 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
205 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
206
207 if (block > ext_block)
208 return ext_pblk + (block - ext_block);
209 else
210 return ext_pblk - (ext_block - block);
211 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700212
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700213 /* it looks like index is empty;
214 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700215 if (path[depth].p_bh)
216 return path[depth].p_bh->b_blocknr;
217 }
218
219 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400220 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700221}
222
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400223/*
224 * Allocation for a meta data block
225 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700226static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400227ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700228 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400229 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700230{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700231 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700232
233 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400234 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
235 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700236 return newblock;
237}
238
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700240{
241 int size;
242
243 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
244 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100245#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400246 if (!check && size > 6)
247 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700248#endif
249 return size;
250}
251
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400252static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700253{
254 int size;
255
256 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
257 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100258#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400259 if (!check && size > 5)
260 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700261#endif
262 return size;
263}
264
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700266{
267 int size;
268
269 size = sizeof(EXT4_I(inode)->i_data);
270 size -= sizeof(struct ext4_extent_header);
271 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100272#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400273 if (!check && size > 3)
274 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700275#endif
276 return size;
277}
278
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400279static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700280{
281 int size;
282
283 size = sizeof(EXT4_I(inode)->i_data);
284 size -= sizeof(struct ext4_extent_header);
285 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100286#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400287 if (!check && size > 4)
288 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700289#endif
290 return size;
291}
292
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400293static inline int
294ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400295 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400296 int nofail)
297{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400298 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400299 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700300 int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO;
301
302 if (nofail)
303 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400304
Theodore Ts'odfe50802014-09-01 14:37:09 -0400305 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400306 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700307 flags);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400308}
309
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400310static int
311ext4_ext_max_entries(struct inode *inode, int depth)
312{
313 int max;
314
315 if (depth == ext_depth(inode)) {
316 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400317 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400318 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400319 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400320 } else {
321 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400322 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400323 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400324 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400325 }
326
327 return max;
328}
329
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400330static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
331{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400332 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400333 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500334 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400335
Vegard Nossumf70749c2016-06-30 11:53:46 -0400336 /*
337 * We allow neither:
338 * - zero length
339 * - overflow/wrap-around
340 */
341 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400342 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400343 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400344}
345
346static int ext4_valid_extent_idx(struct inode *inode,
347 struct ext4_extent_idx *ext_idx)
348{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400349 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400350
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400351 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400352}
353
354static int ext4_valid_extent_entries(struct inode *inode,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400355 struct ext4_extent_header *eh,
356 ext4_fsblk_t *pblk, int depth)
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400357{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400358 unsigned short entries;
359 if (eh->eh_entries == 0)
360 return 1;
361
362 entries = le16_to_cpu(eh->eh_entries);
363
364 if (depth == 0) {
365 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400366 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500367 ext4_lblk_t lblock = 0;
368 ext4_lblk_t prev = 0;
369 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400370 while (entries) {
371 if (!ext4_valid_extent(inode, ext))
372 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500373
374 /* Check for overlapping extents */
375 lblock = le32_to_cpu(ext->ee_block);
376 len = ext4_ext_get_actual_len(ext);
377 if ((lblock <= prev) && prev) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400378 *pblk = ext4_ext_pblock(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500379 return 0;
380 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400381 ext++;
382 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500383 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400384 }
385 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400386 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400387 while (entries) {
388 if (!ext4_valid_extent_idx(inode, ext_idx))
389 return 0;
390 ext_idx++;
391 entries--;
392 }
393 }
394 return 1;
395}
396
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400397static int __ext4_ext_check(const char *function, unsigned int line,
398 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400399 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400400{
401 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400402 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400403
404 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
405 error_msg = "invalid magic";
406 goto corrupted;
407 }
408 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
409 error_msg = "unexpected eh_depth";
410 goto corrupted;
411 }
412 if (unlikely(eh->eh_max == 0)) {
413 error_msg = "invalid eh_max";
414 goto corrupted;
415 }
416 max = ext4_ext_max_entries(inode, depth);
417 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
418 error_msg = "too large eh_max";
419 goto corrupted;
420 }
421 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
422 error_msg = "invalid eh_entries";
423 goto corrupted;
424 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400425 if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) {
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400426 error_msg = "invalid extent entries";
427 goto corrupted;
428 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400429 if (unlikely(depth > 32)) {
430 error_msg = "too large eh_depth";
431 goto corrupted;
432 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400433 /* Verify checksum on non-root extent tree nodes */
434 if (ext_depth(inode) != depth &&
435 !ext4_extent_block_csum_verify(inode, eh)) {
436 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400437 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400438 goto corrupted;
439 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400440 return 0;
441
442corrupted:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400443 ext4_error_inode_err(inode, function, line, 0, -err,
444 "pblk %llu bad header/extent: %s - magic %x, "
445 "entries %u, max %u(%u), depth %u(%u)",
446 (unsigned long long) pblk, error_msg,
447 le16_to_cpu(eh->eh_magic),
448 le16_to_cpu(eh->eh_entries),
449 le16_to_cpu(eh->eh_max),
450 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400451 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400452}
453
Theodore Ts'oc3491792013-08-16 21:21:41 -0400454#define ext4_ext_check(inode, eh, depth, pblk) \
455 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400456
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400457int ext4_ext_check_inode(struct inode *inode)
458{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400459 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400460}
461
Dmitry Monakhov40686642019-11-06 12:25:02 +0000462static void ext4_cache_extents(struct inode *inode,
463 struct ext4_extent_header *eh)
464{
465 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
466 ext4_lblk_t prev = 0;
467 int i;
468
469 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
470 unsigned int status = EXTENT_STATUS_WRITTEN;
471 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
472 int len = ext4_ext_get_actual_len(ex);
473
474 if (prev && (prev != lblk))
475 ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
476 EXTENT_STATUS_HOLE);
477
478 if (ext4_ext_is_unwritten(ex))
479 status = EXTENT_STATUS_UNWRITTEN;
480 ext4_es_cache_extent(inode, lblk, len,
481 ext4_ext_pblock(ex), status);
482 prev = lblk + len;
483 }
484}
485
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400486static struct buffer_head *
487__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400488 struct inode *inode, ext4_fsblk_t pblk, int depth,
489 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400490{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400491 struct buffer_head *bh;
492 int err;
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700493 gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400494
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700495 if (flags & EXT4_EX_NOFAIL)
496 gfp_flags |= __GFP_NOFAIL;
497
498 bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400499 if (unlikely(!bh))
500 return ERR_PTR(-ENOMEM);
501
502 if (!bh_uptodate_or_lock(bh)) {
503 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
504 err = bh_submit_read(bh);
505 if (err < 0)
506 goto errout;
507 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400508 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400509 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400510 if (!ext4_has_feature_journal(inode->i_sb) ||
511 (inode->i_ino !=
512 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
513 err = __ext4_ext_check(function, line, inode,
514 ext_block_hdr(bh), depth, pblk);
515 if (err)
516 goto errout;
517 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400518 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400519 /*
520 * If this is a leaf block, cache all of its entries
521 */
522 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
523 struct ext4_extent_header *eh = ext_block_hdr(bh);
Dmitry Monakhov40686642019-11-06 12:25:02 +0000524 ext4_cache_extents(inode, eh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400525 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400526 return bh;
527errout:
528 put_bh(bh);
529 return ERR_PTR(err);
530
Darrick J. Wongf8489122012-04-29 18:21:10 -0400531}
532
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400533#define read_extent_tree_block(inode, pblk, depth, flags) \
534 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
535 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400536
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400537/*
538 * This function is called to cache a file's extent information in the
539 * extent status tree
540 */
541int ext4_ext_precache(struct inode *inode)
542{
543 struct ext4_inode_info *ei = EXT4_I(inode);
544 struct ext4_ext_path *path = NULL;
545 struct buffer_head *bh;
546 int i = 0, depth, ret = 0;
547
548 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
549 return 0; /* not an extent-mapped inode */
550
551 down_read(&ei->i_data_sem);
552 depth = ext_depth(inode);
553
Ritesh Harjani2f424a52020-02-28 14:56:55 +0530554 /* Don't cache anything if there are no external extent blocks */
555 if (!depth) {
556 up_read(&ei->i_data_sem);
557 return ret;
558 }
559
Kees Cook6396bb22018-06-12 14:03:40 -0700560 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400561 GFP_NOFS);
562 if (path == NULL) {
563 up_read(&ei->i_data_sem);
564 return -ENOMEM;
565 }
566
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400567 path[0].p_hdr = ext_inode_hdr(inode);
568 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
569 if (ret)
570 goto out;
571 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
572 while (i >= 0) {
573 /*
574 * If this is a leaf block or we've reached the end of
575 * the index block, go up
576 */
577 if ((i == depth) ||
578 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
579 brelse(path[i].p_bh);
580 path[i].p_bh = NULL;
581 i--;
582 continue;
583 }
584 bh = read_extent_tree_block(inode,
585 ext4_idx_pblock(path[i].p_idx++),
586 depth - i - 1,
587 EXT4_EX_FORCE_CACHE);
588 if (IS_ERR(bh)) {
589 ret = PTR_ERR(bh);
590 break;
591 }
592 i++;
593 path[i].p_bh = bh;
594 path[i].p_hdr = ext_block_hdr(bh);
595 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
596 }
597 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
598out:
599 up_read(&ei->i_data_sem);
600 ext4_ext_drop_refs(path);
601 kfree(path);
602 return ret;
603}
604
Alex Tomasa86c6182006-10-11 01:21:03 -0700605#ifdef EXT_DEBUG
606static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
607{
608 int k, l = path->p_depth;
609
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530610 ext_debug(inode, "path:");
Alex Tomasa86c6182006-10-11 01:21:03 -0700611 for (k = 0; k <= l; k++, path++) {
612 if (path->p_idx) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530613 ext_debug(inode, " %d->%llu",
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600614 le32_to_cpu(path->p_idx->ei_block),
615 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700616 } else if (path->p_ext) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530617 ext_debug(inode, " %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700618 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400619 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400620 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400621 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700622 } else
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530623 ext_debug(inode, " []");
Alex Tomasa86c6182006-10-11 01:21:03 -0700624 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530625 ext_debug(inode, "\n");
Alex Tomasa86c6182006-10-11 01:21:03 -0700626}
627
628static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
629{
630 int depth = ext_depth(inode);
631 struct ext4_extent_header *eh;
632 struct ext4_extent *ex;
633 int i;
634
635 if (!path)
636 return;
637
638 eh = path[depth].p_hdr;
639 ex = EXT_FIRST_EXTENT(eh);
640
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530641 ext_debug(inode, "Displaying leaf extents\n");
Mingming553f9002009-09-18 13:34:55 -0400642
Alex Tomasa86c6182006-10-11 01:21:03 -0700643 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530644 ext_debug(inode, "%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400645 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400646 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700647 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530648 ext_debug(inode, "\n");
Alex Tomasa86c6182006-10-11 01:21:03 -0700649}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400650
651static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
652 ext4_fsblk_t newblock, int level)
653{
654 int depth = ext_depth(inode);
655 struct ext4_extent *ex;
656
657 if (depth != level) {
658 struct ext4_extent_idx *idx;
659 idx = path[level].p_idx;
660 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530661 ext_debug(inode, "%d: move %d:%llu in new index %llu\n",
662 level, le32_to_cpu(idx->ei_block),
663 ext4_idx_pblock(idx), newblock);
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400664 idx++;
665 }
666
667 return;
668 }
669
670 ex = path[depth].p_ext;
671 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530672 ext_debug(inode, "move %d:%llu:[%d]%d in new leaf %llu\n",
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400673 le32_to_cpu(ex->ee_block),
674 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400675 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400676 ext4_ext_get_actual_len(ex),
677 newblock);
678 ex++;
679 }
680}
681
Alex Tomasa86c6182006-10-11 01:21:03 -0700682#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400683#define ext4_ext_show_path(inode, path)
684#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400685#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700686#endif
687
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500688void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700689{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400690 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700691
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400692 if (!path)
693 return;
694 depth = path->p_depth;
Eric Biggersde745482019-12-31 12:04:44 -0600695 for (i = 0; i <= depth; i++, path++) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700696 if (path->p_bh) {
697 brelse(path->p_bh);
698 path->p_bh = NULL;
699 }
Eric Biggersde745482019-12-31 12:04:44 -0600700 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700701}
702
703/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700704 * ext4_ext_binsearch_idx:
705 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400706 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700707 */
708static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500709ext4_ext_binsearch_idx(struct inode *inode,
710 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700711{
712 struct ext4_extent_header *eh = path->p_hdr;
713 struct ext4_extent_idx *r, *l, *m;
714
Alex Tomasa86c6182006-10-11 01:21:03 -0700715
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530716 ext_debug(inode, "binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700717
718 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400719 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700720 while (l <= r) {
721 m = l + (r - l) / 2;
722 if (block < le32_to_cpu(m->ei_block))
723 r = m - 1;
724 else
725 l = m + 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530726 ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l,
727 le32_to_cpu(l->ei_block), m, le32_to_cpu(m->ei_block),
728 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700729 }
730
731 path->p_idx = l - 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530732 ext_debug(inode, " -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400733 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700734
735#ifdef CHECK_BINSEARCH
736 {
737 struct ext4_extent_idx *chix, *ix;
738 int k;
739
740 chix = ix = EXT_FIRST_INDEX(eh);
741 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600742 if (k != 0 && le32_to_cpu(ix->ei_block) <=
743 le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400744 printk(KERN_DEBUG "k=%d, ix=0x%p, "
745 "first=0x%p\n", k,
746 ix, EXT_FIRST_INDEX(eh));
747 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700748 le32_to_cpu(ix->ei_block),
749 le32_to_cpu(ix[-1].ei_block));
750 }
751 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400752 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700753 if (block < le32_to_cpu(ix->ei_block))
754 break;
755 chix = ix;
756 }
757 BUG_ON(chix != path->p_idx);
758 }
759#endif
760
761}
762
763/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700764 * ext4_ext_binsearch:
765 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400766 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700767 */
768static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500769ext4_ext_binsearch(struct inode *inode,
770 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700771{
772 struct ext4_extent_header *eh = path->p_hdr;
773 struct ext4_extent *r, *l, *m;
774
Alex Tomasa86c6182006-10-11 01:21:03 -0700775 if (eh->eh_entries == 0) {
776 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700777 * this leaf is empty:
778 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700779 */
780 return;
781 }
782
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530783 ext_debug(inode, "binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700784
785 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400786 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700787
788 while (l <= r) {
789 m = l + (r - l) / 2;
790 if (block < le32_to_cpu(m->ee_block))
791 r = m - 1;
792 else
793 l = m + 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530794 ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l,
795 le32_to_cpu(l->ee_block), m, le32_to_cpu(m->ee_block),
796 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700797 }
798
799 path->p_ext = l - 1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530800 ext_debug(inode, " -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400801 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400802 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400803 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400804 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700805
806#ifdef CHECK_BINSEARCH
807 {
808 struct ext4_extent *chex, *ex;
809 int k;
810
811 chex = ex = EXT_FIRST_EXTENT(eh);
812 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
813 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400814 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 if (block < le32_to_cpu(ex->ee_block))
816 break;
817 chex = ex;
818 }
819 BUG_ON(chex != path->p_ext);
820 }
821#endif
822
823}
824
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -0700825void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700826{
827 struct ext4_extent_header *eh;
828
829 eh = ext_inode_hdr(inode);
830 eh->eh_depth = 0;
831 eh->eh_entries = 0;
832 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400833 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700834 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700835}
836
837struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400838ext4_find_extent(struct inode *inode, ext4_lblk_t block,
839 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700840{
841 struct ext4_extent_header *eh;
842 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400843 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
844 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500845 int ret;
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700846 gfp_t gfp_flags = GFP_NOFS;
847
848 if (flags & EXT4_EX_NOFAIL)
849 gfp_flags |= __GFP_NOFAIL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700850
851 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400852 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400853 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
854 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
855 depth);
856 ret = -EFSCORRUPTED;
857 goto err;
858 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700859
Theodore Ts'o10809df82014-09-01 14:40:09 -0400860 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400861 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400862 if (depth > path[0].p_maxdepth) {
863 kfree(path);
864 *orig_path = path = NULL;
865 }
866 }
867 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400868 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700869 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Theodore Ts'o73c384c02020-05-07 10:50:28 -0700870 gfp_flags);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400871 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400873 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700874 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700875 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400876 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700877
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400878 i = depth;
Dmitry Monakhov40686642019-11-06 12:25:02 +0000879 if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
880 ext4_cache_extents(inode, eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 /* walk through the tree */
882 while (i) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530883 ext_debug(inode, "depth %d: num %d, max %d\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700884 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400885
Alex Tomasa86c6182006-10-11 01:21:03 -0700886 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400887 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 path[ppos].p_depth = i;
889 path[ppos].p_ext = NULL;
890
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400891 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
892 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530893 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400894 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500896 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400897
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 eh = ext_block_hdr(bh);
899 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700900 path[ppos].p_bh = bh;
901 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700902 }
903
904 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700905 path[ppos].p_ext = NULL;
906 path[ppos].p_idx = NULL;
907
Alex Tomasa86c6182006-10-11 01:21:03 -0700908 /* find extent */
909 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400910 /* if not an empty leaf */
911 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400912 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700913
914 ext4_ext_show_path(inode, path);
915
916 return path;
917
918err:
919 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400920 kfree(path);
921 if (orig_path)
922 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500923 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700924}
925
926/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700927 * ext4_ext_insert_index:
928 * insert new index [@logical;@ptr] into the block at @curp;
929 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400931static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
932 struct ext4_ext_path *curp,
933 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700934{
935 struct ext4_extent_idx *ix;
936 int len, err;
937
Avantika Mathur7e028972006-12-06 20:41:33 -0800938 err = ext4_ext_get_access(handle, inode, curp);
939 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700940 return err;
941
Frank Mayhar273df552010-03-02 11:46:09 -0500942 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
943 EXT4_ERROR_INODE(inode,
944 "logical %d == ei_block %d!",
945 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400946 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500947 }
Robin Dongd4620312011-07-17 23:43:42 -0400948
949 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
950 >= le16_to_cpu(curp->p_hdr->eh_max))) {
951 EXT4_ERROR_INODE(inode,
952 "eh_entries %d >= eh_max %d!",
953 le16_to_cpu(curp->p_hdr->eh_entries),
954 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400955 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400956 }
957
Alex Tomasa86c6182006-10-11 01:21:03 -0700958 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
959 /* insert after */
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530960 ext_debug(inode, "insert new index %d after: %llu\n",
961 logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 ix = curp->p_idx + 1;
963 } else {
964 /* insert before */
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530965 ext_debug(inode, "insert new index %d before: %llu\n",
966 logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700967 ix = curp->p_idx;
968 }
969
Eric Gouriou80e675f2011-10-27 11:52:18 -0400970 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
971 BUG_ON(len < 0);
972 if (len > 0) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530973 ext_debug(inode, "insert new index %d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -0400974 "move %d indices from 0x%p to 0x%p\n",
975 logical, len, ix, ix + 1);
976 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
977 }
978
Tao Maf472e022011-10-17 10:13:46 -0400979 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
980 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400981 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -0400982 }
983
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700985 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400986 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700987
Frank Mayhar273df552010-03-02 11:46:09 -0500988 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
989 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400990 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500991 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700992
993 err = ext4_ext_dirty(handle, inode, curp);
994 ext4_std_error(inode->i_sb, err);
995
996 return err;
997}
998
999/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001000 * ext4_ext_split:
1001 * inserts new subtree into the path, using free index entry
1002 * at depth @at:
1003 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1004 * - makes decision where to split
1005 * - moves remaining extents and index entries (right to the split point)
1006 * into the newly allocated blocks
1007 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001008 */
1009static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001010 unsigned int flags,
1011 struct ext4_ext_path *path,
1012 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001013{
1014 struct buffer_head *bh = NULL;
1015 int depth = ext_depth(inode);
1016 struct ext4_extent_header *neh;
1017 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001018 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001019 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001020 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001021 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001022 gfp_t gfp_flags = GFP_NOFS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001023 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001024 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001025
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001026 if (flags & EXT4_EX_NOFAIL)
1027 gfp_flags |= __GFP_NOFAIL;
1028
Alex Tomasa86c6182006-10-11 01:21:03 -07001029 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001030 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001031
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001032 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001033 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001034 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1035 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001036 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001037 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001038 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1039 border = path[depth].p_ext[1].ee_block;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301040 ext_debug(inode, "leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001041 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001042 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001043 } else {
1044 border = newext->ee_block;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301045 ext_debug(inode, "leaf will be added."
Alex Tomasa86c6182006-10-11 01:21:03 -07001046 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001047 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 }
1049
1050 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001051 * If error occurs, then we break processing
1052 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001054 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001055 */
1056
1057 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001058 * Get array to track all allocated blocks.
1059 * We need this to handle errors and free blocks
1060 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001061 */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07001062 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001063 if (!ablocks)
1064 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001065
1066 /* allocate all needed blocks */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301067 ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
Alex Tomasa86c6182006-10-11 01:21:03 -07001068 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001069 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001070 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001071 if (newblock == 0)
1072 goto cleanup;
1073 ablocks[a] = newblock;
1074 }
1075
1076 /* initialize new leaf */
1077 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001078 if (unlikely(newblock == 0)) {
1079 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001080 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001081 goto cleanup;
1082 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001083 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001084 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001085 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001086 goto cleanup;
1087 }
1088 lock_buffer(bh);
1089
Avantika Mathur7e028972006-12-06 20:41:33 -08001090 err = ext4_journal_get_create_access(handle, bh);
1091 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001092 goto cleanup;
1093
1094 neh = ext_block_hdr(bh);
1095 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001096 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001097 neh->eh_magic = EXT4_EXT_MAGIC;
1098 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001099
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001100 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001101 if (unlikely(path[depth].p_hdr->eh_entries !=
1102 path[depth].p_hdr->eh_max)) {
1103 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1104 path[depth].p_hdr->eh_entries,
1105 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001106 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001107 goto cleanup;
1108 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001109 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001110 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1111 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001112 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001113 struct ext4_extent *ex;
1114 ex = EXT_FIRST_EXTENT(neh);
1115 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001116 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001117 }
1118
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001119 /* zero out unused area in the extent block */
1120 ext_size = sizeof(struct ext4_extent_header) +
1121 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1122 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001123 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 set_buffer_uptodate(bh);
1125 unlock_buffer(bh);
1126
Frank Mayhar03901312009-01-07 00:06:22 -05001127 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001128 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001129 goto cleanup;
1130 brelse(bh);
1131 bh = NULL;
1132
1133 /* correct old leaf */
1134 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001135 err = ext4_ext_get_access(handle, inode, path + depth);
1136 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001137 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001138 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001139 err = ext4_ext_dirty(handle, inode, path + depth);
1140 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001141 goto cleanup;
1142
1143 }
1144
1145 /* create intermediate indexes */
1146 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001147 if (unlikely(k < 0)) {
1148 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001149 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001150 goto cleanup;
1151 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 if (k)
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301153 ext_debug(inode, "create %d intermediate indices\n", k);
Alex Tomasa86c6182006-10-11 01:21:03 -07001154 /* insert new index into current index block */
1155 /* current depth stored in i var */
1156 i = depth - 1;
1157 while (k--) {
1158 oldblock = newblock;
1159 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001160 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001161 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001162 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001163 goto cleanup;
1164 }
1165 lock_buffer(bh);
1166
Avantika Mathur7e028972006-12-06 20:41:33 -08001167 err = ext4_journal_get_create_access(handle, bh);
1168 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001169 goto cleanup;
1170
1171 neh = ext_block_hdr(bh);
1172 neh->eh_entries = cpu_to_le16(1);
1173 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001174 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001175 neh->eh_depth = cpu_to_le16(depth - i);
1176 fidx = EXT_FIRST_INDEX(neh);
1177 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001178 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001179
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301180 ext_debug(inode, "int.index at %d (block %llu): %u -> %llu\n",
Eric Sandeenbba90742008-01-28 23:58:27 -05001181 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001182
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001183 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001184 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1185 EXT_LAST_INDEX(path[i].p_hdr))) {
1186 EXT4_ERROR_INODE(inode,
1187 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1188 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001189 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001190 goto cleanup;
1191 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001192 /* start copy indexes */
1193 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301194 ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001195 EXT_MAX_INDEX(path[i].p_hdr));
1196 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001197 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001198 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001199 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001200 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001201 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001202 /* zero out unused area in the extent block */
1203 ext_size = sizeof(struct ext4_extent_header) +
1204 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1205 memset(bh->b_data + ext_size, 0,
1206 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001207 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001208 set_buffer_uptodate(bh);
1209 unlock_buffer(bh);
1210
Frank Mayhar03901312009-01-07 00:06:22 -05001211 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001212 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001213 goto cleanup;
1214 brelse(bh);
1215 bh = NULL;
1216
1217 /* correct old index */
1218 if (m) {
1219 err = ext4_ext_get_access(handle, inode, path + i);
1220 if (err)
1221 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001222 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001223 err = ext4_ext_dirty(handle, inode, path + i);
1224 if (err)
1225 goto cleanup;
1226 }
1227
1228 i--;
1229 }
1230
1231 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001232 err = ext4_ext_insert_index(handle, inode, path + at,
1233 le32_to_cpu(border), newblock);
1234
1235cleanup:
1236 if (bh) {
1237 if (buffer_locked(bh))
1238 unlock_buffer(bh);
1239 brelse(bh);
1240 }
1241
1242 if (err) {
1243 /* free all allocated blocks in error case */
1244 for (i = 0; i < depth; i++) {
1245 if (!ablocks[i])
1246 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001247 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001248 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001249 }
1250 }
1251 kfree(ablocks);
1252
1253 return err;
1254}
1255
1256/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001257 * ext4_ext_grow_indepth:
1258 * implements tree growing procedure:
1259 * - allocates new block
1260 * - moves top-level data (index block or leaf) into the new block
1261 * - initializes new top-level, creating index that points to the
1262 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001263 */
1264static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001265 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001266{
Alex Tomasa86c6182006-10-11 01:21:03 -07001267 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001268 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001269 ext4_fsblk_t newblock, goal = 0;
1270 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001271 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001272 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001273
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001274 /* Try to prepend new index to old one */
1275 if (ext_depth(inode))
1276 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1277 if (goal > le32_to_cpu(es->s_first_data_block)) {
1278 flags |= EXT4_MB_HINT_TRY_GOAL;
1279 goal--;
1280 } else
1281 goal = ext4_inode_to_goal_block(inode);
1282 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1283 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001284 if (newblock == 0)
1285 return err;
1286
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001287 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001288 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001289 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001290 lock_buffer(bh);
1291
Avantika Mathur7e028972006-12-06 20:41:33 -08001292 err = ext4_journal_get_create_access(handle, bh);
1293 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001294 unlock_buffer(bh);
1295 goto out;
1296 }
1297
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001298 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001299 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001300 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1301 /* zero out unused area in the extent block */
1302 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001303
1304 /* set size of new block */
1305 neh = ext_block_hdr(bh);
1306 /* old root could have indexes or leaves
1307 * so calculate e_max right way */
1308 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001309 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001310 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001311 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001312 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001313 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001314 set_buffer_uptodate(bh);
1315 unlock_buffer(bh);
1316
Frank Mayhar03901312009-01-07 00:06:22 -05001317 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001318 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001319 goto out;
1320
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001321 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001322 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001323 neh->eh_entries = cpu_to_le16(1);
1324 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1325 if (neh->eh_depth == 0) {
1326 /* Root extent block becomes index block */
1327 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1328 EXT_FIRST_INDEX(neh)->ei_block =
1329 EXT_FIRST_EXTENT(neh)->ee_block;
1330 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301331 ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001332 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001333 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001334 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001335
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001336 le16_add_cpu(&neh->eh_depth, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07001337 err = ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001338out:
1339 brelse(bh);
1340
1341 return err;
1342}
1343
1344/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001345 * ext4_ext_create_new_leaf:
1346 * finds empty index and adds new leaf.
1347 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001348 */
1349static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001350 unsigned int mb_flags,
1351 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001352 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001353 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001354{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001355 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001356 struct ext4_ext_path *curp;
1357 int depth, i, err = 0;
1358
1359repeat:
1360 i = depth = ext_depth(inode);
1361
1362 /* walk up to the tree and look for free index entry */
1363 curp = path + depth;
1364 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1365 i--;
1366 curp--;
1367 }
1368
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001369 /* we use already allocated block for index block,
1370 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001371 if (EXT_HAS_FREE_INDEX(curp)) {
1372 /* if we found index with free entry, then use that
1373 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001374 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001375 if (err)
1376 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001377
1378 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001379 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001380 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001381 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001382 if (IS_ERR(path))
1383 err = PTR_ERR(path);
1384 } else {
1385 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001386 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001387 if (err)
1388 goto out;
1389
1390 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001391 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001392 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001393 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001394 if (IS_ERR(path)) {
1395 err = PTR_ERR(path);
1396 goto out;
1397 }
1398
1399 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001400 * only first (depth 0 -> 1) produces free space;
1401 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001402 */
1403 depth = ext_depth(inode);
1404 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001405 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001406 goto repeat;
1407 }
1408 }
1409
1410out:
1411 return err;
1412}
1413
1414/*
Alex Tomas1988b512008-01-28 23:58:27 -05001415 * search the closest allocated block to the left for *logical
1416 * and returns it at @logical + it's physical address at @phys
1417 * if *logical is the smallest allocated block, the function
1418 * returns 0 at @phys
1419 * return value contains 0 (success) or error code
1420 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001421static int ext4_ext_search_left(struct inode *inode,
1422 struct ext4_ext_path *path,
1423 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001424{
1425 struct ext4_extent_idx *ix;
1426 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001427 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001428
Frank Mayhar273df552010-03-02 11:46:09 -05001429 if (unlikely(path == NULL)) {
1430 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001431 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001432 }
Alex Tomas1988b512008-01-28 23:58:27 -05001433 depth = path->p_depth;
1434 *phys = 0;
1435
1436 if (depth == 0 && path->p_ext == NULL)
1437 return 0;
1438
1439 /* usually extent in the path covers blocks smaller
1440 * then *logical, but it can be that extent is the
1441 * first one in the file */
1442
1443 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001444 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001445 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001446 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1447 EXT4_ERROR_INODE(inode,
1448 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1449 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001450 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001451 }
Alex Tomas1988b512008-01-28 23:58:27 -05001452 while (--depth >= 0) {
1453 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001454 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1455 EXT4_ERROR_INODE(inode,
1456 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001457 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001458 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001459 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001460 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001461 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001462 }
Alex Tomas1988b512008-01-28 23:58:27 -05001463 }
1464 return 0;
1465 }
1466
Frank Mayhar273df552010-03-02 11:46:09 -05001467 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1468 EXT4_ERROR_INODE(inode,
1469 "logical %d < ee_block %d + ee_len %d!",
1470 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001471 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001472 }
Alex Tomas1988b512008-01-28 23:58:27 -05001473
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001474 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001475 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001476 return 0;
1477}
1478
1479/*
1480 * search the closest allocated block to the right for *logical
1481 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001482 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001483 * returns 0 at @phys
1484 * return value contains 0 (success) or error code
1485 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001486static int ext4_ext_search_right(struct inode *inode,
1487 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001488 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1489 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001490{
1491 struct buffer_head *bh = NULL;
1492 struct ext4_extent_header *eh;
1493 struct ext4_extent_idx *ix;
1494 struct ext4_extent *ex;
1495 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001496 int depth; /* Note, NOT eh_depth; depth from top of tree */
1497 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001498
Frank Mayhar273df552010-03-02 11:46:09 -05001499 if (unlikely(path == NULL)) {
1500 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001501 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001502 }
Alex Tomas1988b512008-01-28 23:58:27 -05001503 depth = path->p_depth;
1504 *phys = 0;
1505
1506 if (depth == 0 && path->p_ext == NULL)
1507 return 0;
1508
1509 /* usually extent in the path covers blocks smaller
1510 * then *logical, but it can be that extent is the
1511 * first one in the file */
1512
1513 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001514 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001515 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001516 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1517 EXT4_ERROR_INODE(inode,
1518 "first_extent(path[%d].p_hdr) != ex",
1519 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001520 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001521 }
Alex Tomas1988b512008-01-28 23:58:27 -05001522 while (--depth >= 0) {
1523 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001524 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1525 EXT4_ERROR_INODE(inode,
1526 "ix != EXT_FIRST_INDEX *logical %d!",
1527 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001528 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001529 }
Alex Tomas1988b512008-01-28 23:58:27 -05001530 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001531 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001532 }
1533
Frank Mayhar273df552010-03-02 11:46:09 -05001534 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1535 EXT4_ERROR_INODE(inode,
1536 "logical %d < ee_block %d + ee_len %d!",
1537 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001538 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001539 }
Alex Tomas1988b512008-01-28 23:58:27 -05001540
1541 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1542 /* next allocated block in this leaf */
1543 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001544 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001545 }
1546
1547 /* go up and search for index to the right */
1548 while (--depth >= 0) {
1549 ix = path[depth].p_idx;
1550 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001551 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001552 }
1553
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001554 /* we've gone up to the root and found no index to the right */
1555 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001556
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001557got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001558 /* we've found index to the right, let's
1559 * follow it and find the closest allocated
1560 * block to the right */
1561 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001562 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001563 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001564 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001565 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001566 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001567 if (IS_ERR(bh))
1568 return PTR_ERR(bh);
1569 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001570 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001571 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001572 put_bh(bh);
1573 }
1574
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001575 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001576 if (IS_ERR(bh))
1577 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001578 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001579 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001580found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001581 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001582 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001583 *ret_ex = ex;
1584 if (bh)
1585 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001586 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001587}
1588
1589/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001590 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001591 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001592 * NOTE: it considers block number from index entry as
1593 * allocated block. Thus, index entries have to be consistent
1594 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001595 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001596ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001597ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1598{
1599 int depth;
1600
1601 BUG_ON(path == NULL);
1602 depth = path->p_depth;
1603
1604 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001605 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001606
1607 while (depth >= 0) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001608 struct ext4_ext_path *p = &path[depth];
1609
Alex Tomasa86c6182006-10-11 01:21:03 -07001610 if (depth == path->p_depth) {
1611 /* leaf */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001612 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
1613 return le32_to_cpu(p->p_ext[1].ee_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001614 } else {
1615 /* index */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001616 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
1617 return le32_to_cpu(p->p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001618 }
1619 depth--;
1620 }
1621
Lukas Czernerf17722f2011-06-06 00:05:17 -04001622 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001623}
1624
1625/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001626 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001627 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001628 */
Robin Dong57187892011-07-23 21:49:07 -04001629static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001630{
1631 int depth;
1632
1633 BUG_ON(path == NULL);
1634 depth = path->p_depth;
1635
1636 /* zero-tree has no leaf blocks at all */
1637 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001638 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001639
1640 /* go to index block */
1641 depth--;
1642
1643 while (depth >= 0) {
1644 if (path[depth].p_idx !=
1645 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001646 return (ext4_lblk_t)
1647 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001648 depth--;
1649 }
1650
Lukas Czernerf17722f2011-06-06 00:05:17 -04001651 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001652}
1653
1654/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001655 * ext4_ext_correct_indexes:
1656 * if leaf gets modified and modified extent is first in the leaf,
1657 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001658 * TODO: do we need to correct tree in all cases?
1659 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001660static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001661 struct ext4_ext_path *path)
1662{
1663 struct ext4_extent_header *eh;
1664 int depth = ext_depth(inode);
1665 struct ext4_extent *ex;
1666 __le32 border;
1667 int k, err = 0;
1668
1669 eh = path[depth].p_hdr;
1670 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001671
1672 if (unlikely(ex == NULL || eh == NULL)) {
1673 EXT4_ERROR_INODE(inode,
1674 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001675 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001676 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001677
1678 if (depth == 0) {
1679 /* there is no tree at all */
1680 return 0;
1681 }
1682
1683 if (ex != EXT_FIRST_EXTENT(eh)) {
1684 /* we correct tree if first leaf got modified only */
1685 return 0;
1686 }
1687
1688 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001689 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001690 */
1691 k = depth - 1;
1692 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001693 err = ext4_ext_get_access(handle, inode, path + k);
1694 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001695 return err;
1696 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001697 err = ext4_ext_dirty(handle, inode, path + k);
1698 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001699 return err;
1700
1701 while (k--) {
1702 /* change all left-side indexes */
1703 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1704 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001705 err = ext4_ext_get_access(handle, inode, path + k);
1706 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001707 break;
1708 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001709 err = ext4_ext_dirty(handle, inode, path + k);
1710 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001711 break;
1712 }
1713
1714 return err;
1715}
1716
Eric Biggers43f81672019-12-31 12:04:40 -06001717static int ext4_can_extents_be_merged(struct inode *inode,
1718 struct ext4_extent *ex1,
1719 struct ext4_extent *ex2)
Alex Tomasa86c6182006-10-11 01:21:03 -07001720{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001721 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001722
Lukas Czerner556615d2014-04-20 23:45:47 -04001723 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001724 return 0;
1725
1726 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1727 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1728
1729 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001730 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001731 return 0;
1732
Eric Sandeenda0169b2013-11-04 09:58:26 -05001733 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001734 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001735
Lukas Czerner556615d2014-04-20 23:45:47 -04001736 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001737 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001738 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001739#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001740 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001741 return 0;
1742#endif
1743
Theodore Ts'obf89d162010-10-27 21:30:14 -04001744 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001745 return 1;
1746 return 0;
1747}
1748
1749/*
Amit Arora56055d32007-07-17 21:42:38 -04001750 * This function tries to merge the "ex" extent to the next extent in the tree.
1751 * It always tries to merge towards right. If you want to merge towards
1752 * left, pass "ex - 1" as argument instead of "ex".
1753 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1754 * 1 if they got merged.
1755 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001756static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001757 struct ext4_ext_path *path,
1758 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001759{
1760 struct ext4_extent_header *eh;
1761 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001762 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001763
1764 depth = ext_depth(inode);
1765 BUG_ON(path[depth].p_hdr == NULL);
1766 eh = path[depth].p_hdr;
1767
1768 while (ex < EXT_LAST_EXTENT(eh)) {
1769 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1770 break;
1771 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001772 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001773 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1774 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001775 if (unwritten)
1776 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001777
1778 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1779 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1780 * sizeof(struct ext4_extent);
1781 memmove(ex + 1, ex + 2, len);
1782 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001783 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001784 merge_done = 1;
1785 WARN_ON(eh->eh_entries == 0);
1786 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001787 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001788 }
1789
1790 return merge_done;
1791}
1792
1793/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001794 * This function does a very simple check to see if we can collapse
1795 * an extent tree with a single extent tree leaf block into the inode.
1796 */
1797static void ext4_ext_try_to_merge_up(handle_t *handle,
1798 struct inode *inode,
1799 struct ext4_ext_path *path)
1800{
1801 size_t s;
1802 unsigned max_root = ext4_ext_space_root(inode, 0);
1803 ext4_fsblk_t blk;
1804
1805 if ((path[0].p_depth != 1) ||
1806 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1807 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1808 return;
1809
1810 /*
1811 * We need to modify the block allocation bitmap and the block
1812 * group descriptor to release the extent tree block. If we
1813 * can't get the journal credits, give up.
1814 */
Jan Kara83448bd2019-11-05 17:44:29 +01001815 if (ext4_journal_extend(handle, 2,
1816 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001817 return;
1818
1819 /*
1820 * Copy the extent data up to the inode
1821 */
1822 blk = ext4_idx_pblock(path[0].p_idx);
1823 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1824 sizeof(struct ext4_extent_idx);
1825 s += sizeof(struct ext4_extent_header);
1826
Theodore Ts'o10809df82014-09-01 14:40:09 -04001827 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001828 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1829 path[0].p_depth = 0;
1830 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1831 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1832 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1833
1834 brelse(path[1].p_bh);
1835 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001836 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001837}
1838
1839/*
Eric Biggersadde81c2019-12-31 12:04:41 -06001840 * This function tries to merge the @ex extent to neighbours in the tree, then
1841 * tries to collapse the extent tree into the inode.
Yongqiang Yang197217a2011-05-03 11:45:29 -04001842 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001843static void ext4_ext_try_to_merge(handle_t *handle,
1844 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001845 struct ext4_ext_path *path,
Eric Biggersadde81c2019-12-31 12:04:41 -06001846 struct ext4_extent *ex)
1847{
Yongqiang Yang197217a2011-05-03 11:45:29 -04001848 struct ext4_extent_header *eh;
1849 unsigned int depth;
1850 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001851
1852 depth = ext_depth(inode);
1853 BUG_ON(path[depth].p_hdr == NULL);
1854 eh = path[depth].p_hdr;
1855
1856 if (ex > EXT_FIRST_EXTENT(eh))
1857 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1858
1859 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001860 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001861
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001862 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001863}
1864
1865/*
Amit Arora25d14f92007-05-24 13:04:13 -04001866 * check if a portion of the "newext" extent overlaps with an
1867 * existing extent.
1868 *
1869 * If there is an overlap discovered, it updates the length of the newext
1870 * such that there will be no overlap, and then returns 1.
1871 * If there is no overlap found, it returns 0.
1872 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001873static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1874 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001875 struct ext4_extent *newext,
1876 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001877{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001878 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001879 unsigned int depth, len1;
1880 unsigned int ret = 0;
1881
1882 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001883 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001884 depth = ext_depth(inode);
1885 if (!path[depth].p_ext)
1886 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001887 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001888
1889 /*
1890 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001891 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001892 */
1893 if (b2 < b1) {
1894 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001895 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001896 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001897 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001898 }
1899
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001900 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001901 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001902 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001903 newext->ee_len = cpu_to_le16(len1);
1904 ret = 1;
1905 }
1906
1907 /* check for overlap */
1908 if (b1 + len1 > b2) {
1909 newext->ee_len = cpu_to_le16(b2 - b1);
1910 ret = 1;
1911 }
1912out:
1913 return ret;
1914}
1915
1916/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001917 * ext4_ext_insert_extent:
1918 * tries to merge requsted extent into the existing extent or
1919 * inserts requested extent as new one into the tree,
1920 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001921 */
1922int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001923 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001924 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001925{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001926 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001927 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001928 struct ext4_extent *ex, *fex;
1929 struct ext4_extent *nearex; /* nearest extent */
1930 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001931 int depth, len, err;
1932 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001933 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001934
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001935 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1936 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001937 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1938 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001939 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001940 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001941 depth = ext_depth(inode);
1942 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001943 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001944 if (unlikely(path[depth].p_hdr == NULL)) {
1945 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001946 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001947 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001948
1949 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001950 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001951
1952 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001953 * Try to see whether we should rather test the extent on
1954 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001955 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001956 * left, or on the right from the searched position. This
1957 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001958 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001959 if (ex < EXT_LAST_EXTENT(eh) &&
1960 (le32_to_cpu(ex->ee_block) +
1961 ext4_ext_get_actual_len(ex) <
1962 le32_to_cpu(newext->ee_block))) {
1963 ex += 1;
1964 goto prepend;
1965 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1966 (le32_to_cpu(newext->ee_block) +
1967 ext4_ext_get_actual_len(newext) <
1968 le32_to_cpu(ex->ee_block)))
1969 ex -= 1;
1970
1971 /* Try to append newex to the ex */
1972 if (ext4_can_extents_be_merged(inode, ex, newext)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301973 ext_debug(inode, "append [%d]%d block to %u:[%d]%d"
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001974 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001975 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001976 ext4_ext_get_actual_len(newext),
1977 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001978 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001979 ext4_ext_get_actual_len(ex),
1980 ext4_ext_pblock(ex));
1981 err = ext4_ext_get_access(handle, inode,
1982 path + depth);
1983 if (err)
1984 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001985 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001986 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001987 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001988 if (unwritten)
1989 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001990 eh = path[depth].p_hdr;
1991 nearex = ex;
1992 goto merge;
1993 }
1994
1995prepend:
1996 /* Try to prepend newex to the ex */
1997 if (ext4_can_extents_be_merged(inode, newext, ex)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301998 ext_debug(inode, "prepend %u[%d]%d block to %u:[%d]%d"
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001999 "(from %llu)\n",
2000 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002001 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002002 ext4_ext_get_actual_len(newext),
2003 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002004 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002005 ext4_ext_get_actual_len(ex),
2006 ext4_ext_pblock(ex));
2007 err = ext4_ext_get_access(handle, inode,
2008 path + depth);
2009 if (err)
2010 return err;
2011
Lukas Czerner556615d2014-04-20 23:45:47 -04002012 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002013 ex->ee_block = newext->ee_block;
2014 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2015 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2016 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002017 if (unwritten)
2018 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002019 eh = path[depth].p_hdr;
2020 nearex = ex;
2021 goto merge;
2022 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002023 }
2024
Alex Tomasa86c6182006-10-11 01:21:03 -07002025 depth = ext_depth(inode);
2026 eh = path[depth].p_hdr;
2027 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2028 goto has_space;
2029
2030 /* probably next leaf has space for us? */
2031 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002032 next = EXT_MAX_BLOCKS;
2033 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002034 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002035 if (next != EXT_MAX_BLOCKS) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302036 ext_debug(inode, "next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002037 BUG_ON(npath != NULL);
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002038 npath = ext4_find_extent(inode, next, NULL, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002039 if (IS_ERR(npath))
2040 return PTR_ERR(npath);
2041 BUG_ON(npath->p_depth != path->p_depth);
2042 eh = npath[depth].p_hdr;
2043 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302044 ext_debug(inode, "next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002045 le16_to_cpu(eh->eh_entries));
2046 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002047 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302049 ext_debug(inode, "next leaf has no free space(%d,%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002050 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2051 }
2052
2053 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002054 * There is no free space in the found leaf.
2055 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002056 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002057 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002058 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002059 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002060 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002061 if (err)
2062 goto cleanup;
2063 depth = ext_depth(inode);
2064 eh = path[depth].p_hdr;
2065
2066has_space:
2067 nearex = path[depth].p_ext;
2068
Avantika Mathur7e028972006-12-06 20:41:33 -08002069 err = ext4_ext_get_access(handle, inode, path + depth);
2070 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002071 goto cleanup;
2072
2073 if (!nearex) {
2074 /* there is no extent in this leaf, create first one */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302075 ext_debug(inode, "first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002076 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002077 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002078 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002079 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002080 nearex = EXT_FIRST_EXTENT(eh);
2081 } else {
2082 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002083 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002084 /* Insert after */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302085 ext_debug(inode, "insert %u:%llu:[%d]%d before: "
Yongqiang Yang32de6752011-11-01 18:56:41 -04002086 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002087 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002088 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002089 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002090 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002091 nearex);
2092 nearex++;
2093 } else {
2094 /* Insert before */
2095 BUG_ON(newext->ee_block == nearex->ee_block);
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302096 ext_debug(inode, "insert %u:%llu:[%d]%d after: "
Yongqiang Yang32de6752011-11-01 18:56:41 -04002097 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002098 le32_to_cpu(newext->ee_block),
2099 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002100 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002101 ext4_ext_get_actual_len(newext),
2102 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002103 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002104 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2105 if (len > 0) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302106 ext_debug(inode, "insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002107 "move %d extents from 0x%p to 0x%p\n",
2108 le32_to_cpu(newext->ee_block),
2109 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002110 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002111 ext4_ext_get_actual_len(newext),
2112 len, nearex, nearex + 1);
2113 memmove(nearex + 1, nearex,
2114 len * sizeof(struct ext4_extent));
2115 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002116 }
2117
Marcin Slusarze8546d02008-04-17 10:38:59 -04002118 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002119 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002120 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002121 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002122 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002123
2124merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002125 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002126 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002127 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002128
Alex Tomasa86c6182006-10-11 01:21:03 -07002129
2130 /* time to correct all indexes above */
2131 err = ext4_ext_correct_indexes(handle, inode, path);
2132 if (err)
2133 goto cleanup;
2134
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002135 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002136
2137cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002138 ext4_ext_drop_refs(npath);
2139 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002140 return err;
2141}
2142
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002143static int ext4_fill_es_cache_info(struct inode *inode,
2144 ext4_lblk_t block, ext4_lblk_t num,
2145 struct fiemap_extent_info *fieinfo)
2146{
2147 ext4_lblk_t next, end = block + num - 1;
2148 struct extent_status es;
2149 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2150 unsigned int flags;
2151 int err;
2152
2153 while (block <= end) {
2154 next = 0;
2155 flags = 0;
2156 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2157 break;
2158 if (ext4_es_is_unwritten(&es))
2159 flags |= FIEMAP_EXTENT_UNWRITTEN;
2160 if (ext4_es_is_delayed(&es))
2161 flags |= (FIEMAP_EXTENT_DELALLOC |
2162 FIEMAP_EXTENT_UNKNOWN);
2163 if (ext4_es_is_hole(&es))
2164 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2165 if (next == 0)
2166 flags |= FIEMAP_EXTENT_LAST;
2167 if (flags & (FIEMAP_EXTENT_DELALLOC|
2168 EXT4_FIEMAP_EXTENT_HOLE))
2169 es.es_pblk = 0;
2170 else
2171 es.es_pblk = ext4_es_pblock(&es);
2172 err = fiemap_fill_next_extent(fieinfo,
2173 (__u64)es.es_lblk << blksize_bits,
2174 (__u64)es.es_pblk << blksize_bits,
2175 (__u64)es.es_len << blksize_bits,
2176 flags);
2177 if (next == 0)
2178 break;
2179 block = next;
2180 if (err < 0)
2181 return err;
2182 if (err == 1)
2183 return 0;
2184 }
2185 return 0;
2186}
2187
2188
Alex Tomasa86c6182006-10-11 01:21:03 -07002189/*
Jan Kara140a5252016-03-09 22:46:57 -05002190 * ext4_ext_determine_hole - determine hole around given block
2191 * @inode: inode we lookup in
2192 * @path: path in extent tree to @lblk
2193 * @lblk: pointer to logical block around which we want to determine hole
2194 *
2195 * Determine hole length (and start if easily possible) around given logical
2196 * block. We don't try too hard to find the beginning of the hole but @path
2197 * actually points to extent before @lblk, we provide it.
2198 *
2199 * The function returns the length of a hole starting at @lblk. We update @lblk
2200 * to the beginning of the hole if we managed to find it.
2201 */
2202static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2203 struct ext4_ext_path *path,
2204 ext4_lblk_t *lblk)
2205{
2206 int depth = ext_depth(inode);
2207 struct ext4_extent *ex;
2208 ext4_lblk_t len;
2209
2210 ex = path[depth].p_ext;
2211 if (ex == NULL) {
2212 /* there is no extent yet, so gap is [0;-] */
2213 *lblk = 0;
2214 len = EXT_MAX_BLOCKS;
2215 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2216 len = le32_to_cpu(ex->ee_block) - *lblk;
2217 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2218 + ext4_ext_get_actual_len(ex)) {
2219 ext4_lblk_t next;
2220
2221 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2222 next = ext4_ext_next_allocated_block(path);
2223 BUG_ON(next == *lblk);
2224 len = next - *lblk;
2225 } else {
2226 BUG();
2227 }
2228 return len;
2229}
2230
2231/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002232 * ext4_ext_put_gap_in_cache:
2233 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002234 * and cache this gap
2235 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002236static void
Jan Kara140a5252016-03-09 22:46:57 -05002237ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2238 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002239{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002240 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002241
Eric Whitneyad431022018-10-01 14:10:39 -04002242 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2243 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002244 if (es.es_len) {
2245 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002246 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002247 return;
Jan Kara140a5252016-03-09 22:46:57 -05002248 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002249 }
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302250 ext_debug(inode, " -> %u:%u\n", hole_start, hole_len);
Jan Kara140a5252016-03-09 22:46:57 -05002251 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2252 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002253}
2254
2255/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002256 * ext4_ext_rm_idx:
2257 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002258 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002259static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002260 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002261{
Alex Tomasa86c6182006-10-11 01:21:03 -07002262 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002263 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002264
2265 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002266 depth--;
2267 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002268 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002269 if (unlikely(path->p_hdr->eh_entries == 0)) {
2270 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002271 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002272 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002273 err = ext4_ext_get_access(handle, inode, path);
2274 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002275 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002276
2277 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2278 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2279 len *= sizeof(struct ext4_extent_idx);
2280 memmove(path->p_idx, path->p_idx + 1, len);
2281 }
2282
Marcin Slusarze8546d02008-04-17 10:38:59 -04002283 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002284 err = ext4_ext_dirty(handle, inode, path);
2285 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002286 return err;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302287 ext_debug(inode, "index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002288 trace_ext4_ext_rm_idx(inode, leaf);
2289
Peter Huewe7dc57612011-02-21 21:01:42 -05002290 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002291 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002292
2293 while (--depth >= 0) {
2294 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2295 break;
2296 path--;
2297 err = ext4_ext_get_access(handle, inode, path);
2298 if (err)
2299 break;
2300 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2301 err = ext4_ext_dirty(handle, inode, path);
2302 if (err)
2303 break;
2304 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002305 return err;
2306}
2307
2308/*
Mingming Caoee12b632008-08-19 22:16:05 -04002309 * ext4_ext_calc_credits_for_single_extent:
2310 * This routine returns max. credits that needed to insert an extent
2311 * to the extent tree.
2312 * When pass the actual path, the caller should calculate credits
2313 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002314 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002315int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002316 struct ext4_ext_path *path)
2317{
Alex Tomasa86c6182006-10-11 01:21:03 -07002318 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002319 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002320 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002321
Alex Tomasa86c6182006-10-11 01:21:03 -07002322 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002324 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2325
2326 /*
2327 * There are some space in the leaf tree, no
2328 * need to account for leaf block credit
2329 *
2330 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002331 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002332 * accounted.
2333 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002334 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002335 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002336 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002337 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002338 }
2339
Mingming Cao525f4ed2008-08-19 22:15:58 -04002340 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002341}
Alex Tomasa86c6182006-10-11 01:21:03 -07002342
Mingming Caoee12b632008-08-19 22:16:05 -04002343/*
Jan Karafffb2732013-06-04 13:01:11 -04002344 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002345 *
Jan Karafffb2732013-06-04 13:01:11 -04002346 * If we add a single extent, then in the worse case, each tree level
2347 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002348 *
Jan Karafffb2732013-06-04 13:01:11 -04002349 * If more extents are inserted, they could cause the whole tree split more
2350 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002351 */
Jan Karafffb2732013-06-04 13:01:11 -04002352int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002353{
2354 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002355 int depth;
2356
2357 /* If we are converting the inline data, only one is needed here. */
2358 if (ext4_has_inline_data(inode))
2359 return 1;
2360
2361 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002362
Jan Karafffb2732013-06-04 13:01:11 -04002363 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002364 index = depth * 2;
2365 else
2366 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002367
Mingming Caoee12b632008-08-19 22:16:05 -04002368 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002369}
2370
Theodore Ts'o981250c2013-06-12 11:48:29 -04002371static inline int get_default_free_blocks_flags(struct inode *inode)
2372{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002373 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2374 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002375 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2376 else if (ext4_should_journal_data(inode))
2377 return EXT4_FREE_BLOCKS_FORGET;
2378 return 0;
2379}
2380
Eric Whitney9fe67142018-10-01 14:25:08 -04002381/*
2382 * ext4_rereserve_cluster - increment the reserved cluster count when
2383 * freeing a cluster with a pending reservation
2384 *
2385 * @inode - file containing the cluster
2386 * @lblk - logical block in cluster to be reserved
2387 *
2388 * Increments the reserved cluster count and adjusts quota in a bigalloc
2389 * file system when freeing a partial cluster containing at least one
2390 * delayed and unwritten block. A partial cluster meeting that
2391 * requirement will have a pending reservation. If so, the
2392 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2393 * defer reserved and allocated space accounting to a subsequent call
2394 * to this function.
2395 */
2396static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2397{
2398 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2399 struct ext4_inode_info *ei = EXT4_I(inode);
2400
2401 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2402
2403 spin_lock(&ei->i_block_reservation_lock);
2404 ei->i_reserved_data_blocks++;
2405 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2406 spin_unlock(&ei->i_block_reservation_lock);
2407
2408 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2409 ext4_remove_pending(inode, lblk);
2410}
2411
Alex Tomasa86c6182006-10-11 01:21:03 -07002412static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002413 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002414 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002415 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002416{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002417 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002418 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002419 ext4_fsblk_t last_pblk, pblk;
2420 ext4_lblk_t num;
2421 int flags;
2422
2423 /* only extent tail removal is allowed */
2424 if (from < le32_to_cpu(ex->ee_block) ||
2425 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2426 ext4_error(sbi->s_sb,
2427 "strange request: removal(2) %u-%u from %u:%u",
2428 from, to, le32_to_cpu(ex->ee_block), ee_len);
2429 return 0;
2430 }
2431
2432#ifdef EXTENTS_STATS
2433 spin_lock(&sbi->s_ext_stats_lock);
2434 sbi->s_ext_blocks += ee_len;
2435 sbi->s_ext_extents++;
2436 if (ee_len < sbi->s_ext_min)
2437 sbi->s_ext_min = ee_len;
2438 if (ee_len > sbi->s_ext_max)
2439 sbi->s_ext_max = ee_len;
2440 if (ext_depth(inode) > sbi->s_depth_max)
2441 sbi->s_depth_max = ext_depth(inode);
2442 spin_unlock(&sbi->s_ext_stats_lock);
2443#endif
2444
2445 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2446
2447 /*
2448 * if we have a partial cluster, and it's different from the
2449 * cluster of the last block in the extent, we free it
2450 */
2451 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2452
2453 if (partial->state != initial &&
2454 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2455 if (partial->state == tofree) {
2456 flags = get_default_free_blocks_flags(inode);
2457 if (ext4_is_pending(inode, partial->lblk))
2458 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2459 ext4_free_blocks(handle, inode, NULL,
2460 EXT4_C2B(sbi, partial->pclu),
2461 sbi->s_cluster_ratio, flags);
2462 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2463 ext4_rereserve_cluster(inode, partial->lblk);
2464 }
2465 partial->state = initial;
2466 }
2467
2468 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2469 pblk = ext4_ext_pblock(ex) + ee_len - num;
2470
2471 /*
2472 * We free the partial cluster at the end of the extent (if any),
2473 * unless the cluster is used by another extent (partial_cluster
2474 * state is nofree). If a partial cluster exists here, it must be
2475 * shared with the last block in the extent.
2476 */
2477 flags = get_default_free_blocks_flags(inode);
2478
2479 /* partial, left end cluster aligned, right end unaligned */
2480 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2481 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2482 (partial->state != nofree)) {
2483 if (ext4_is_pending(inode, to))
2484 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2485 ext4_free_blocks(handle, inode, NULL,
2486 EXT4_PBLK_CMASK(sbi, last_pblk),
2487 sbi->s_cluster_ratio, flags);
2488 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2489 ext4_rereserve_cluster(inode, to);
2490 partial->state = initial;
2491 flags = get_default_free_blocks_flags(inode);
2492 }
2493
2494 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002495
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002496 /*
2497 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002498 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002499 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002500 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002501 */
2502 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002503 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002504
Eric Whitney9fe67142018-10-01 14:25:08 -04002505 /* reset the partial cluster if we've freed past it */
2506 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2507 partial->state = initial;
2508
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002509 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002510 * If we've freed the entire extent but the beginning is not left
2511 * cluster aligned and is not marked as ineligible for freeing we
2512 * record the partial cluster at the beginning of the extent. It
2513 * wasn't freed by the preceding ext4_free_blocks() call, and we
2514 * need to look farther to the left to determine if it's to be freed
2515 * (not shared with another extent). Else, reset the partial
2516 * cluster - we're either done freeing or the beginning of the
2517 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002518 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002519 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2520 if (partial->state == initial) {
2521 partial->pclu = EXT4_B2C(sbi, pblk);
2522 partial->lblk = from;
2523 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002524 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002525 } else {
2526 partial->state = initial;
2527 }
2528
Alex Tomasa86c6182006-10-11 01:21:03 -07002529 return 0;
2530}
2531
Allison Hendersond583fb82011-05-25 07:41:43 -04002532/*
2533 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002534 * blocks appearing between "start" and "end". Both "start"
2535 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002536 *
2537 * @handle: The journal handle
2538 * @inode: The files inode
2539 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002540 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002541 * has been released from it. However, if this value is
2542 * negative, it's a cluster just to the right of the
2543 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002544 * @start: The first block to remove
2545 * @end: The last block to remove
2546 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002547static int
2548ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002549 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002550 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002551 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002552{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002553 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002554 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002555 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002556 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002557 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002558 unsigned num;
2559 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002560 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002561 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002562 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002563 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002564
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002565 /* the header must be checked already in ext4_ext_remove_space() */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302566 ext_debug(inode, "truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002567 if (!path[depth].p_hdr)
2568 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2569 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002570 if (unlikely(path[depth].p_hdr == NULL)) {
2571 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002572 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002573 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002574 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002575 ex = path[depth].p_ext;
2576 if (!ex)
2577 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002578
2579 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002580 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002581
Eric Whitney9fe67142018-10-01 14:25:08 -04002582 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002583
Alex Tomasa86c6182006-10-11 01:21:03 -07002584 while (ex >= EXT_FIRST_EXTENT(eh) &&
2585 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002586
Lukas Czerner556615d2014-04-20 23:45:47 -04002587 if (ext4_ext_is_unwritten(ex))
2588 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002589 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002590 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002591
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302592 ext_debug(inode, "remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002593 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002594 path[depth].p_ext = ex;
2595
2596 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002597 b = ex_ee_block+ex_ee_len - 1 < end ?
2598 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002599
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302600 ext_debug(inode, " border %u:%u\n", a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002601
Allison Hendersond583fb82011-05-25 07:41:43 -04002602 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002603 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002604 /*
2605 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002606 * so note that its first cluster is in use to avoid
2607 * freeing it when removing blocks. Eventually, the
2608 * right edge of the truncated/punched region will
2609 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002610 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002611 if (sbi->s_cluster_ratio > 1) {
2612 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002613 partial->pclu = EXT4_B2C(sbi, pblk);
2614 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002615 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002616 ex--;
2617 ex_ee_block = le32_to_cpu(ex->ee_block);
2618 ex_ee_len = ext4_ext_get_actual_len(ex);
2619 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002620 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002621 EXT4_ERROR_INODE(inode,
2622 "can not handle truncate %u:%u "
2623 "on extent %u:%u",
2624 start, end, ex_ee_block,
2625 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002626 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002627 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002628 } else if (a != ex_ee_block) {
2629 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002630 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002631 } else {
2632 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002633 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002634 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002635 /*
2636 * 3 for leaf, sb, and inode plus 2 (bmap and group
2637 * descriptor) for each block group; assume two block
2638 * groups plus ex_ee_len/blocks_per_block_group for
2639 * the worst case
2640 */
2641 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002642 if (ex == EXT_FIRST_EXTENT(eh)) {
2643 correct_index = 1;
2644 credits += (ext_depth(inode)) + 1;
2645 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002646 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002647 /*
2648 * We may end up freeing some index blocks and data from the
2649 * punched range. Note that partial clusters are accounted for
2650 * by ext4_free_data_revoke_credits().
2651 */
2652 revoke_credits =
2653 ext4_free_metadata_revoke_credits(inode->i_sb,
2654 ext_depth(inode)) +
2655 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002656
Jan Karaa4130362019-11-05 17:44:16 +01002657 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002658 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002659 if (err) {
2660 if (err > 0)
2661 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002662 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002663 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002664
2665 err = ext4_ext_get_access(handle, inode, path + depth);
2666 if (err)
2667 goto out;
2668
Eric Whitney9fe67142018-10-01 14:25:08 -04002669 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002670 if (err)
2671 goto out;
2672
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002673 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002674 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002675 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002676
Alex Tomasa86c6182006-10-11 01:21:03 -07002677 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002678 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002679 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002680 * extent have been removed.
2681 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002682 if (unwritten && num)
2683 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002684 /*
2685 * If the extent was completely released,
2686 * we need to remove it from the leaf
2687 */
2688 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002689 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002690 /*
2691 * For hole punching, we need to scoot all the
2692 * extents up when an extent is removed so that
2693 * we dont have blank extents in the middle
2694 */
2695 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2696 sizeof(struct ext4_extent));
2697
2698 /* Now get rid of the one at the end */
2699 memset(EXT_LAST_EXTENT(eh), 0,
2700 sizeof(struct ext4_extent));
2701 }
2702 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002703 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002704
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002705 err = ext4_ext_dirty(handle, inode, path + depth);
2706 if (err)
2707 goto out;
2708
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302709 ext_debug(inode, "new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002710 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002711 ex--;
2712 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002713 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002714 }
2715
2716 if (correct_index && eh->eh_entries)
2717 err = ext4_ext_correct_indexes(handle, inode, path);
2718
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002719 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002720 * If there's a partial cluster and at least one extent remains in
2721 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002722 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002723 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002724 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002725 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002726 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002727 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002728 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2729 int flags = get_default_free_blocks_flags(inode);
2730
2731 if (ext4_is_pending(inode, partial->lblk))
2732 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002733 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002734 EXT4_C2B(sbi, partial->pclu),
2735 sbi->s_cluster_ratio, flags);
2736 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2737 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002738 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002739 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002740 }
2741
Alex Tomasa86c6182006-10-11 01:21:03 -07002742 /* if this leaf is free, then we should
2743 * remove it from index block above */
2744 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002745 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002746
2747out:
2748 return err;
2749}
2750
2751/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002752 * ext4_ext_more_to_rm:
2753 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002754 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002755static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002756ext4_ext_more_to_rm(struct ext4_ext_path *path)
2757{
2758 BUG_ON(path->p_idx == NULL);
2759
2760 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2761 return 0;
2762
2763 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002764 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002765 * so we have to consider current index for truncation
2766 */
2767 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2768 return 0;
2769 return 1;
2770}
2771
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002772int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2773 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002774{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002775 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002776 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002777 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002778 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002779 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002780 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002781
Eric Whitney9fe67142018-10-01 14:25:08 -04002782 partial.pclu = 0;
2783 partial.lblk = 0;
2784 partial.state = initial;
2785
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302786 ext_debug(inode, "truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002787
2788 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002789 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2790 depth + 1,
2791 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002792 if (IS_ERR(handle))
2793 return PTR_ERR(handle);
2794
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002795again:
Lukas Czerner61801322013-05-27 23:32:35 -04002796 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002797
Alex Tomasa86c6182006-10-11 01:21:03 -07002798 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002799 * Check if we are removing extents inside the extent tree. If that
2800 * is the case, we are going to punch a hole inside the extent tree
2801 * so we have to check whether we need to split the extent covering
2802 * the last block to remove so we can easily remove the part of it
2803 * in ext4_ext_rm_leaf().
2804 */
2805 if (end < EXT_MAX_BLOCKS - 1) {
2806 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002807 ext4_lblk_t ee_block, ex_end, lblk;
2808 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002809
Eric Whitneyf4226d92014-11-23 00:55:42 -05002810 /* find extent for or closest extent to this block */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002811 path = ext4_find_extent(inode, end, NULL,
2812 EXT4_EX_NOCACHE | EXT4_EX_NOFAIL);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002813 if (IS_ERR(path)) {
2814 ext4_journal_stop(handle);
2815 return PTR_ERR(path);
2816 }
2817 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002818 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002819 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002820 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002821 if (depth) {
2822 EXT4_ERROR_INODE(inode,
2823 "path[%d].p_hdr == NULL",
2824 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002825 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002826 }
2827 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002828 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002829
2830 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002831 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002832
2833 /*
2834 * See if the last block is inside the extent, if so split
2835 * the extent at 'end' block so we can easily remove the
2836 * tail of the first part of the split extent in
2837 * ext4_ext_rm_leaf().
2838 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002839 if (end >= ee_block && end < ex_end) {
2840
2841 /*
2842 * If we're going to split the extent, note that
2843 * the cluster containing the block after 'end' is
2844 * in use to avoid freeing it when removing blocks.
2845 */
2846 if (sbi->s_cluster_ratio > 1) {
2847 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04002848 partial.pclu = EXT4_B2C(sbi, pblk);
2849 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002850 }
2851
Lukas Czerner5f95d212012-03-19 23:03:19 -04002852 /*
2853 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002854 * block in the first new extent. Also we should not
2855 * fail removing space due to ENOSPC so try to use
2856 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002857 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002858 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002859 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002860 if (err < 0)
2861 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002862
Eric Whitney7bd75232019-02-28 23:34:11 -05002863 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
2864 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05002865 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05002866 * If we're punching, there's an extent to the right.
2867 * If the partial cluster hasn't been set, set it to
2868 * that extent's first cluster and its state to nofree
2869 * so it won't be freed should it contain blocks to be
2870 * removed. If it's already set (tofree/nofree), we're
2871 * retrying and keep the original partial cluster info
2872 * so a cluster marked tofree as a result of earlier
2873 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05002874 */
2875 lblk = ex_end + 1;
2876 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2877 &ex);
2878 if (err)
2879 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04002880 if (pblk) {
2881 partial.pclu = EXT4_B2C(sbi, pblk);
2882 partial.state = nofree;
2883 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002884 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002885 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002886 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002887 * We start scanning from right side, freeing all the blocks
2888 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002889 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002890 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002891 if (path) {
2892 int k = i = depth;
2893 while (--k > 0)
2894 path[k].p_block =
2895 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2896 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002897 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o73c384c02020-05-07 10:50:28 -07002898 GFP_NOFS | __GFP_NOFAIL);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002899 if (path == NULL) {
2900 ext4_journal_stop(handle);
2901 return -ENOMEM;
2902 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002903 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002904 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002905 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002906
Theodore Ts'oc3491792013-08-16 21:21:41 -04002907 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002908 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002909 goto out;
2910 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002911 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002912 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002913
2914 while (i >= 0 && err == 0) {
2915 if (i == depth) {
2916 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002917 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002918 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002919 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002920 brelse(path[i].p_bh);
2921 path[i].p_bh = NULL;
2922 i--;
2923 continue;
2924 }
2925
2926 /* this is index block */
2927 if (!path[i].p_hdr) {
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302928 ext_debug(inode, "initialize header\n");
Alex Tomasa86c6182006-10-11 01:21:03 -07002929 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002930 }
2931
Alex Tomasa86c6182006-10-11 01:21:03 -07002932 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002933 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002934 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2935 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302936 ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002937 path[i].p_hdr,
2938 le16_to_cpu(path[i].p_hdr->eh_entries));
2939 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002940 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002941 path[i].p_idx--;
2942 }
2943
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302944 ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002945 i, EXT_FIRST_INDEX(path[i].p_hdr),
2946 path[i].p_idx);
2947 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002948 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002949 /* go to the next level */
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302950 ext_debug(inode, "move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002951 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002952 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002953 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002954 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2955 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002956 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002957 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002958 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002959 break;
2960 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04002961 /* Yield here to deal with large extent trees.
2962 * Should be a no-op if we did IO above. */
2963 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002964 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002965 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002966 break;
2967 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002968 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002969
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002970 /* save actual number of indexes since this
2971 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002972 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2973 i++;
2974 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002975 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002976 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002977 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002978 * handle must be already prepared by the
2979 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05002980 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002981 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002982 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002983 brelse(path[i].p_bh);
2984 path[i].p_bh = NULL;
2985 i--;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05302986 ext_debug(inode, "return to level %d\n", i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002987 }
2988 }
2989
Eric Whitney9fe67142018-10-01 14:25:08 -04002990 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
2991 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04002992
Eric Whitney0756b902014-11-23 00:59:39 -05002993 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002994 * if there's a partial cluster and we have removed the first extent
2995 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05002996 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002997 if (partial.state == tofree && err == 0) {
2998 int flags = get_default_free_blocks_flags(inode);
2999
3000 if (ext4_is_pending(inode, partial.lblk))
3001 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003002 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003003 EXT4_C2B(sbi, partial.pclu),
3004 sbi->s_cluster_ratio, flags);
3005 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3006 ext4_rereserve_cluster(inode, partial.lblk);
3007 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003008 }
3009
Alex Tomasa86c6182006-10-11 01:21:03 -07003010 /* TODO: flexible tree reduction should be here */
3011 if (path->p_hdr->eh_entries == 0) {
3012 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003013 * truncate to zero freed all the tree,
3014 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003015 */
3016 err = ext4_ext_get_access(handle, inode, path);
3017 if (err == 0) {
3018 ext_inode_hdr(inode)->eh_depth = 0;
3019 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003020 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003021 err = ext4_ext_dirty(handle, inode, path);
3022 }
3023 }
3024out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003025 ext4_ext_drop_refs(path);
3026 kfree(path);
3027 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003028 if (err == -EAGAIN)
3029 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003030 ext4_journal_stop(handle);
3031
3032 return err;
3033}
3034
3035/*
3036 * called at mount time
3037 */
3038void ext4_ext_init(struct super_block *sb)
3039{
3040 /*
3041 * possible initialization would be here
3042 */
3043
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003044 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003045#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003046 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003047#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003048 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003049#endif
3050#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003051 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003052#endif
3053#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003054 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003055#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003056 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003057#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003058#ifdef EXTENTS_STATS
3059 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3060 EXT4_SB(sb)->s_ext_min = 1 << 30;
3061 EXT4_SB(sb)->s_ext_max = 0;
3062#endif
3063 }
3064}
3065
3066/*
3067 * called at umount time
3068 */
3069void ext4_ext_release(struct super_block *sb)
3070{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003071 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003072 return;
3073
3074#ifdef EXTENTS_STATS
3075 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3076 struct ext4_sb_info *sbi = EXT4_SB(sb);
3077 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3078 sbi->s_ext_blocks, sbi->s_ext_extents,
3079 sbi->s_ext_blocks / sbi->s_ext_extents);
3080 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3081 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3082 }
3083#endif
3084}
3085
Zheng Liud7b2a002013-08-28 14:47:06 -04003086static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3087{
3088 ext4_lblk_t ee_block;
3089 ext4_fsblk_t ee_pblock;
3090 unsigned int ee_len;
3091
3092 ee_block = le32_to_cpu(ex->ee_block);
3093 ee_len = ext4_ext_get_actual_len(ex);
3094 ee_pblock = ext4_ext_pblock(ex);
3095
3096 if (ee_len == 0)
3097 return 0;
3098
3099 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3100 EXTENT_STATUS_WRITTEN);
3101}
3102
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003103/* FIXME!! we need to try to merge to left or right after zero-out */
3104static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3105{
Lukas Czerner24075182010-10-27 21:30:06 -04003106 ext4_fsblk_t ee_pblock;
3107 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003108
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003109 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003110 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003111 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3112 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003113}
3114
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003115/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003116 * ext4_split_extent_at() splits an extent at given block.
3117 *
3118 * @handle: the journal handle
3119 * @inode: the file inode
3120 * @path: the path to the extent
3121 * @split: the logical block where the extent is splitted.
3122 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003123 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003124 * @flags: flags used to insert new extent to extent tree.
3125 *
3126 *
3127 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3128 * of which are deterimined by split_flag.
3129 *
3130 * There are two cases:
3131 * a> the extent are splitted into two extent.
3132 * b> split is not needed, and just mark the extent.
3133 *
3134 * return 0 on success.
3135 */
3136static int ext4_split_extent_at(handle_t *handle,
3137 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003138 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003139 ext4_lblk_t split,
3140 int split_flag,
3141 int flags)
3142{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003143 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003144 ext4_fsblk_t newblock;
3145 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003146 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003147 struct ext4_extent *ex2 = NULL;
3148 unsigned int ee_len, depth;
3149 int err = 0;
3150
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003151 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3152 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3153
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303154 ext_debug(inode, "logical block %llu\n", (unsigned long long)split);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003155
3156 ext4_ext_show_leaf(inode, path);
3157
3158 depth = ext_depth(inode);
3159 ex = path[depth].p_ext;
3160 ee_block = le32_to_cpu(ex->ee_block);
3161 ee_len = ext4_ext_get_actual_len(ex);
3162 newblock = split - ee_block + ext4_ext_pblock(ex);
3163
3164 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003165 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003166 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003167 EXT4_EXT_MARK_UNWRIT1 |
3168 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003169
3170 err = ext4_ext_get_access(handle, inode, path + depth);
3171 if (err)
3172 goto out;
3173
3174 if (split == ee_block) {
3175 /*
3176 * case b: block @split is the block that the extent begins with
3177 * then we just change the state of the extent, and splitting
3178 * is not needed.
3179 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003180 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3181 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003182 else
3183 ext4_ext_mark_initialized(ex);
3184
3185 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003186 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003187
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003188 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003189 goto out;
3190 }
3191
3192 /* case a */
3193 memcpy(&orig_ex, ex, sizeof(orig_ex));
3194 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003195 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3196 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003197
3198 /*
3199 * path may lead to new leaf, not to original leaf any more
3200 * after ext4_ext_insert_extent() returns,
3201 */
3202 err = ext4_ext_dirty(handle, inode, path + depth);
3203 if (err)
3204 goto fix_extent_len;
3205
3206 ex2 = &newex;
3207 ex2->ee_block = cpu_to_le32(split);
3208 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3209 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003210 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3211 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003212
Theodore Ts'odfe50802014-09-01 14:37:09 -04003213 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003214 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003215 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003216 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003217 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003218 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003219 zero_ex.ee_len = cpu_to_le16(
3220 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003221 ext4_ext_store_pblock(&zero_ex,
3222 ext4_ext_pblock(ex2));
3223 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003224 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003225 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003226 zero_ex.ee_len = cpu_to_le16(
3227 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003228 ext4_ext_store_pblock(&zero_ex,
3229 ext4_ext_pblock(ex));
3230 }
3231 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003232 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003233 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003234 zero_ex.ee_len = cpu_to_le16(
3235 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003236 ext4_ext_store_pblock(&zero_ex,
3237 ext4_ext_pblock(&orig_ex));
3238 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003239
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003240 if (err)
3241 goto fix_extent_len;
3242 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003243 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003244 ext4_ext_try_to_merge(handle, inode, path, ex);
3245 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003246 if (err)
3247 goto fix_extent_len;
3248
3249 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003250 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003251
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003252 goto out;
3253 } else if (err)
3254 goto fix_extent_len;
3255
3256out:
3257 ext4_ext_show_leaf(inode, path);
3258 return err;
3259
3260fix_extent_len:
3261 ex->ee_len = orig_ex.ee_len;
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003262 /*
3263 * Ignore ext4_ext_dirty return value since we are already in error path
3264 * and err is a non-zero error code.
3265 */
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003266 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003267 return err;
3268}
3269
3270/*
3271 * ext4_split_extents() splits an extent and mark extent which is covered
3272 * by @map as split_flags indicates
3273 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003274 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003275 * There are three possibilities:
3276 * a> There is no split required
3277 * b> Splits in two extents: Split is happening at either end of the extent
3278 * c> Splits in three extents: Somone is splitting in middle of the extent
3279 *
3280 */
3281static int ext4_split_extent(handle_t *handle,
3282 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003283 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003284 struct ext4_map_blocks *map,
3285 int split_flag,
3286 int flags)
3287{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003288 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003289 ext4_lblk_t ee_block;
3290 struct ext4_extent *ex;
3291 unsigned int ee_len, depth;
3292 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003293 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003294 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003295 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296
3297 depth = ext_depth(inode);
3298 ex = path[depth].p_ext;
3299 ee_block = le32_to_cpu(ex->ee_block);
3300 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003301 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003302
3303 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003304 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003305 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003306 if (unwritten)
3307 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3308 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003309 if (split_flag & EXT4_EXT_DATA_VALID2)
3310 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003311 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003312 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003313 if (err)
3314 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003315 } else {
3316 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003317 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003318 /*
3319 * Update path is required because previous ext4_split_extent_at() may
3320 * result in split of original leaf or extent zeroout.
3321 */
Theodore Ts'o73c384c02020-05-07 10:50:28 -07003322 path = ext4_find_extent(inode, map->m_lblk, ppath, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003323 if (IS_ERR(path))
3324 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003325 depth = ext_depth(inode);
3326 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003327 if (!ex) {
3328 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3329 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003330 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003331 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003332 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003333 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003334
3335 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003336 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003337 if (unwritten) {
3338 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003339 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003340 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003341 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003342 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003343 map->m_lblk, split_flag1, flags);
3344 if (err)
3345 goto out;
3346 }
3347
3348 ext4_ext_show_leaf(inode, path);
3349out:
Zheng Liu3a225672013-03-10 21:20:23 -04003350 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003351}
3352
Amit Arora56055d32007-07-17 21:42:38 -04003353/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003354 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003355 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003356 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003357 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003358 * There are three possibilities:
3359 * a> There is no split required: Entire extent should be initialized
3360 * b> Splits in two extents: Write is happening at either end of the extent
3361 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003362 *
3363 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003364 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003365 * - The extent pointed to by 'path' contains a superset
3366 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3367 *
3368 * Post-conditions on success:
3369 * - the returned value is the number of blocks beyond map->l_lblk
3370 * that are allocated and initialized.
3371 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003372 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003373static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003374 struct inode *inode,
3375 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003376 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003377 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003378{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003379 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003380 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003381 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003382 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003383 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003384 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003385 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003386 unsigned int ee_len, depth, map_len = map->m_len;
3387 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003388 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003389 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003390
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303391 ext_debug(inode, "logical block %llu, max_blocks %u\n",
3392 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003393
Zheng Liu67a5da52012-08-17 09:54:17 -04003394 sbi = EXT4_SB(inode->i_sb);
Jan Kara801674f2020-03-31 12:50:16 +02003395 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3396 >> inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003397 if (eof_block < map->m_lblk + map_len)
3398 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003399
3400 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003401 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003402 ex = path[depth].p_ext;
3403 ee_block = le32_to_cpu(ex->ee_block);
3404 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003405 zero_ex1.ee_len = 0;
3406 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003407
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003408 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3409
3410 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003411 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003412 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003413
3414 /*
3415 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003416 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003417 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003418 * memmove() calls. Transferring to the left is the common case in
3419 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3420 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003421 *
3422 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003423 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003424 * This would require removing the extent if the transfer
3425 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003426 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003427 * same extent tree node.
3428 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003429 if ((map->m_lblk == ee_block) &&
3430 /* See if we can merge left */
3431 (map_len < ee_len) && /*L1*/
3432 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003433 ext4_lblk_t prev_lblk;
3434 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003435 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003436
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003437 abut_ex = ex - 1;
3438 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3439 prev_len = ext4_ext_get_actual_len(abut_ex);
3440 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003441 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003442
3443 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003444 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003445 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003446 * - C1: abut_ex is initialized,
3447 * - C2: abut_ex is logically abutting ex,
3448 * - C3: abut_ex is physically abutting ex,
3449 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003450 * overflowing the (initialized) length limit.
3451 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003452 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003453 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3454 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003455 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003456 err = ext4_ext_get_access(handle, inode, path + depth);
3457 if (err)
3458 goto out;
3459
3460 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003461 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003462
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003463 /* Shift the start of ex by 'map_len' blocks */
3464 ex->ee_block = cpu_to_le32(ee_block + map_len);
3465 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3466 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003467 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003468
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003469 /* Extend abut_ex by 'map_len' blocks */
3470 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003471
3472 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003473 allocated = map_len;
3474 }
3475 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3476 (map_len < ee_len) && /*L1*/
3477 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3478 /* See if we can merge right */
3479 ext4_lblk_t next_lblk;
3480 ext4_fsblk_t next_pblk, ee_pblk;
3481 unsigned int next_len;
3482
3483 abut_ex = ex + 1;
3484 next_lblk = le32_to_cpu(abut_ex->ee_block);
3485 next_len = ext4_ext_get_actual_len(abut_ex);
3486 next_pblk = ext4_ext_pblock(abut_ex);
3487 ee_pblk = ext4_ext_pblock(ex);
3488
3489 /*
3490 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3491 * upon those conditions:
3492 * - C1: abut_ex is initialized,
3493 * - C2: abut_ex is logically abutting ex,
3494 * - C3: abut_ex is physically abutting ex,
3495 * - C4: abut_ex can receive the additional blocks without
3496 * overflowing the (initialized) length limit.
3497 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003498 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003499 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3500 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3501 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3502 err = ext4_ext_get_access(handle, inode, path + depth);
3503 if (err)
3504 goto out;
3505
3506 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3507 map, ex, abut_ex);
3508
3509 /* Shift the start of abut_ex by 'map_len' blocks */
3510 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3511 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3512 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003513 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003514
3515 /* Extend abut_ex by 'map_len' blocks */
3516 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3517
3518 /* Result: number of initialized blocks past m_lblk */
3519 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003520 }
3521 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003522 if (allocated) {
3523 /* Mark the block containing both extents as dirty */
Harshad Shirwadkarb60ca332020-04-26 18:34:38 -07003524 err = ext4_ext_dirty(handle, inode, path + depth);
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003525
3526 /* Update path to point to the right extent */
3527 path[depth].p_ext = abut_ex;
3528 goto out;
3529 } else
3530 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003531
Yongqiang Yang667eff32011-05-03 12:25:07 -04003532 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003533 /*
3534 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003535 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003536 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003537 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003538
Zheng Liu67a5da52012-08-17 09:54:17 -04003539 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3540 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003541 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003542
Amit Arora56055d32007-07-17 21:42:38 -04003543 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003544 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003545 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003546 * 2. split the extent into two extents, zeroout the head of the first
3547 * extent.
3548 * 3. split the extent into two extents, zeroout the tail of the second
3549 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003550 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003551 * 5. no splitting needed, just possibly zeroout the head and / or the
3552 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003553 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003554 split_map.m_lblk = map->m_lblk;
3555 split_map.m_len = map->m_len;
3556
Jan Kara4f8caa62017-05-26 17:40:52 -04003557 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003558 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003559 /* case 3 or 5 */
3560 zero_ex1.ee_block =
3561 cpu_to_le32(split_map.m_lblk +
3562 split_map.m_len);
3563 zero_ex1.ee_len =
3564 cpu_to_le16(allocated - split_map.m_len);
3565 ext4_ext_store_pblock(&zero_ex1,
3566 ext4_ext_pblock(ex) + split_map.m_lblk +
3567 split_map.m_len - ee_block);
3568 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003569 if (err)
3570 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003571 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003572 }
3573 if (split_map.m_lblk - ee_block + split_map.m_len <
3574 max_zeroout) {
3575 /* case 2 or 5 */
3576 if (split_map.m_lblk != ee_block) {
3577 zero_ex2.ee_block = ex->ee_block;
3578 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003579 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003580 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003581 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003582 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003583 if (err)
3584 goto out;
3585 }
3586
Jan Kara4f8caa62017-05-26 17:40:52 -04003587 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003588 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003589 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003590 }
3591 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003592
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003593 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3594 flags);
3595 if (err > 0)
3596 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003597out:
Zheng Liuadb23552013-03-10 21:13:05 -04003598 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003599 if (!err) {
3600 err = ext4_zeroout_es(inode, &zero_ex1);
3601 if (!err)
3602 err = ext4_zeroout_es(inode, &zero_ex2);
3603 }
Amit Arora56055d32007-07-17 21:42:38 -04003604 return err ? err : allocated;
3605}
3606
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003607/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003608 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003609 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003610 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003611 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003612 * Writing to an unwritten extent may result in splitting the unwritten
3613 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003614 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003615 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003616 * b> Splits in two extents: Write is happening at either end of the extent
3617 * c> Splits in three extents: Somone is writing in middle of the extent
3618 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003619 * This works the same way in the case of initialized -> unwritten conversion.
3620 *
Mingming Cao00314622009-09-28 15:49:08 -04003621 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003622 * the unwritten extent split. To prevent ENOSPC occur at the IO
3623 * complete, we need to split the unwritten extent before DIO submit
3624 * the IO. The unwritten extent called at this time will be split
3625 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003626 * being filled will be convert to initialized by the end_io callback function
3627 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003628 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003629 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003630 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003631static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003632 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003633 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003634 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003635 int flags)
3636{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003637 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003638 ext4_lblk_t eof_block;
3639 ext4_lblk_t ee_block;
3640 struct ext4_extent *ex;
3641 unsigned int ee_len;
3642 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003643
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303644 ext_debug(inode, "logical block %llu, max_blocks %u\n",
Lukas Czernerb8a86842014-03-18 18:05:35 -04003645 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003646
Jan Kara801674f2020-03-31 12:50:16 +02003647 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
3648 >> inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003649 if (eof_block < map->m_lblk + map->m_len)
3650 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003651 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003652 * It is safe to convert extent to initialized via explicit
3653 * zeroout only if extent is fully insde i_size or new_size.
3654 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003655 depth = ext_depth(inode);
3656 ex = path[depth].p_ext;
3657 ee_block = le32_to_cpu(ex->ee_block);
3658 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003659
Lukas Czernerb8a86842014-03-18 18:05:35 -04003660 /* Convert to unwritten */
3661 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3662 split_flag |= EXT4_EXT_DATA_VALID1;
3663 /* Convert to initialized */
3664 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3665 split_flag |= ee_block + ee_len <= eof_block ?
3666 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003667 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003668 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003669 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003670 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003671}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003672
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003673static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003674 struct inode *inode,
3675 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003676 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003677{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003678 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003679 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003680 ext4_lblk_t ee_block;
3681 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003682 int depth;
3683 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003684
3685 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003686 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003687 ee_block = le32_to_cpu(ex->ee_block);
3688 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003689
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303690 ext_debug(inode, "logical block %llu, max_blocks %u\n",
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003691 (unsigned long long)ee_block, ee_len);
3692
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003693 /* If extent is larger than requested it is a clear sign that we still
3694 * have some extent state machine issues left. So extent_split is still
3695 * required.
3696 * TODO: Once all related issues will be fixed this situation should be
3697 * illegal.
3698 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003699 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003700#ifdef CONFIG_EXT4_DEBUG
3701 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003702 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003703 inode->i_ino, (unsigned long long)ee_block, ee_len,
3704 (unsigned long long)map->m_lblk, map->m_len);
3705#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003706 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003707 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003708 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003709 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003710 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003711 if (IS_ERR(path))
3712 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003713 depth = ext_depth(inode);
3714 ex = path[depth].p_ext;
3715 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003716
Mingming Cao00314622009-09-28 15:49:08 -04003717 err = ext4_ext_get_access(handle, inode, path + depth);
3718 if (err)
3719 goto out;
3720 /* first mark the extent as initialized */
3721 ext4_ext_mark_initialized(ex);
3722
Yongqiang Yang197217a2011-05-03 11:45:29 -04003723 /* note: ext4_ext_correct_indexes() isn't needed here because
3724 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003725 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003726 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003727
Mingming Cao00314622009-09-28 15:49:08 -04003728 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003729 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003730out:
3731 ext4_ext_show_leaf(inode, path);
3732 return err;
3733}
3734
3735static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003736convert_initialized_extent(handle_t *handle, struct inode *inode,
3737 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003738 struct ext4_ext_path **ppath,
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003739 unsigned int *allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003740{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003741 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003742 struct ext4_extent *ex;
3743 ext4_lblk_t ee_block;
3744 unsigned int ee_len;
3745 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003746 int err = 0;
3747
3748 /*
3749 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003750 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003751 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003752 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3753 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003754
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003755 depth = ext_depth(inode);
3756 ex = path[depth].p_ext;
3757 ee_block = le32_to_cpu(ex->ee_block);
3758 ee_len = ext4_ext_get_actual_len(ex);
3759
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303760 ext_debug(inode, "logical block %llu, max_blocks %u\n",
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003761 (unsigned long long)ee_block, ee_len);
3762
3763 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003764 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003765 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3766 if (err < 0)
3767 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003768 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003769 if (IS_ERR(path))
3770 return PTR_ERR(path);
3771 depth = ext_depth(inode);
3772 ex = path[depth].p_ext;
3773 if (!ex) {
3774 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3775 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003776 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003777 }
3778 }
3779
3780 err = ext4_ext_get_access(handle, inode, path + depth);
3781 if (err)
3782 return err;
3783 /* first mark the extent as unwritten */
3784 ext4_ext_mark_unwritten(ex);
3785
3786 /* note: ext4_ext_correct_indexes() isn't needed here because
3787 * borders are not changed
3788 */
3789 ext4_ext_try_to_merge(handle, inode, path, ex);
3790
3791 /* Mark modified extent as dirty */
3792 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3793 if (err)
3794 return err;
3795 ext4_ext_show_leaf(inode, path);
3796
3797 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003798
Lukas Czernerb8a86842014-03-18 18:05:35 -04003799 map->m_flags |= EXT4_MAP_UNWRITTEN;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003800 if (*allocated > map->m_len)
3801 *allocated = map->m_len;
3802 map->m_len = *allocated;
3803 return 0;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003804}
3805
3806static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003807ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003808 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003809 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003810 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003811{
Ritesh Harjani8ec2d312020-05-10 11:54:53 +05303812 struct ext4_ext_path __maybe_unused *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003813 int ret = 0;
3814 int err = 0;
3815
Ritesh Harjani70aa1552020-05-10 11:54:55 +05303816 ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n",
3817 (unsigned long long)map->m_lblk, map->m_len, flags,
3818 allocated);
Mingming Cao00314622009-09-28 15:49:08 -04003819 ext4_ext_show_leaf(inode, path);
3820
Lukas Czerner27dd4382013-04-09 22:11:22 -04003821 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04003822 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04003823 * allocate metadata blocks for the new extent block if needed.
3824 */
3825 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
3826
Lukas Czerner556615d2014-04-20 23:45:47 -04003827 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05003828 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003829
Eric Whitney779e2652020-04-30 14:53:19 -04003830 /* get_block() before submitting IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003831 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003832 ret = ext4_split_convert_extents(handle, inode, map, ppath,
3833 flags | EXT4_GET_BLOCKS_CONVERT);
Eric Whitney779e2652020-04-30 14:53:19 -04003834 if (ret < 0) {
3835 err = ret;
3836 goto out2;
3837 }
3838 /*
3839 * shouldn't get a 0 return when splitting an extent unless
3840 * m_len is 0 (bug) or extent has been corrupted
3841 */
3842 if (unlikely(ret == 0)) {
3843 EXT4_ERROR_INODE(inode,
3844 "unexpected ret == 0, m_len = %u",
3845 map->m_len);
3846 err = -EFSCORRUPTED;
3847 goto out2;
3848 }
Zheng Liua25a4e12013-02-18 00:28:04 -05003849 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003850 goto out;
3851 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003852 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003853 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Eric Whitneybee6cf02020-04-30 14:53:18 -04003854 err = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003855 ppath);
Eric Whitneybee6cf02020-04-30 14:53:18 -04003856 if (err < 0)
3857 goto out2;
3858 ext4_update_inode_fsync_trans(handle, inode, 1);
3859 goto map_out;
Mingming Cao00314622009-09-28 15:49:08 -04003860 }
Eric Whitneybee6cf02020-04-30 14:53:18 -04003861 /* buffered IO cases */
Mingming Cao00314622009-09-28 15:49:08 -04003862 /*
3863 * repeat fallocate creation request
3864 * we already have an unwritten extent
3865 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003866 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05003867 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003868 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003869 }
Mingming Cao00314622009-09-28 15:49:08 -04003870
3871 /* buffered READ or buffered write_begin() lookup */
3872 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3873 /*
3874 * We have blocks reserved already. We
3875 * return allocated blocks so that delalloc
3876 * won't do block reservation for us. But
3877 * the buffer head will be unmapped so that
3878 * a read from the block returns 0s.
3879 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003880 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003881 goto out1;
3882 }
3883
Eric Whitneybe809e12020-04-30 14:53:20 -04003884 /*
3885 * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1.
3886 * For buffered writes, at writepage time, etc. Convert a
3887 * discovered unwritten extent to written.
3888 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04003889 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Eric Whitneybe809e12020-04-30 14:53:20 -04003890 if (ret < 0) {
Mingming Cao00314622009-09-28 15:49:08 -04003891 err = ret;
3892 goto out2;
Eric Whitney779e2652020-04-30 14:53:19 -04003893 }
Eric Whitneybe809e12020-04-30 14:53:20 -04003894 ext4_update_inode_fsync_trans(handle, inode, 1);
3895 /*
3896 * shouldn't get a 0 return when converting an unwritten extent
3897 * unless m_len is 0 (bug) or extent has been corrupted
3898 */
3899 if (unlikely(ret == 0)) {
3900 EXT4_ERROR_INODE(inode, "unexpected ret == 0, m_len = %u",
3901 map->m_len);
3902 err = -EFSCORRUPTED;
3903 goto out2;
3904 }
3905
Eric Whitney779e2652020-04-30 14:53:19 -04003906out:
3907 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003908 map->m_flags |= EXT4_MAP_NEW;
Mingming Cao00314622009-09-28 15:49:08 -04003909map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003910 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003911out1:
Eric Whitneybee6cf02020-04-30 14:53:18 -04003912 map->m_pblk = newblock;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003913 if (allocated > map->m_len)
3914 allocated = map->m_len;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003915 map->m_len = allocated;
Eric Whitneybee6cf02020-04-30 14:53:18 -04003916 ext4_ext_show_leaf(inode, path);
Mingming Cao00314622009-09-28 15:49:08 -04003917out2:
Mingming Cao00314622009-09-28 15:49:08 -04003918 return err ? err : allocated;
3919}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003920
Mingming Cao00314622009-09-28 15:49:08 -04003921/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003922 * get_implied_cluster_alloc - check to see if the requested
3923 * allocation (in the map structure) overlaps with a cluster already
3924 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003925 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003926 * @map The requested lblk->pblk mapping
3927 * @ex The extent structure which might contain an implied
3928 * cluster allocation
3929 *
3930 * This function is called by ext4_ext_map_blocks() after we failed to
3931 * find blocks that were already in the inode's extent tree. Hence,
3932 * we know that the beginning of the requested region cannot overlap
3933 * the extent from the inode's extent tree. There are three cases we
3934 * want to catch. The first is this case:
3935 *
3936 * |--- cluster # N--|
3937 * |--- extent ---| |---- requested region ---|
3938 * |==========|
3939 *
3940 * The second case that we need to test for is this one:
3941 *
3942 * |--------- cluster # N ----------------|
3943 * |--- requested region --| |------- extent ----|
3944 * |=======================|
3945 *
3946 * The third case is when the requested region lies between two extents
3947 * within the same cluster:
3948 * |------------- cluster # N-------------|
3949 * |----- ex -----| |---- ex_right ----|
3950 * |------ requested region ------|
3951 * |================|
3952 *
3953 * In each of the above cases, we need to set the map->m_pblk and
3954 * map->m_len so it corresponds to the return the extent labelled as
3955 * "|====|" from cluster #N, since it is already in use for data in
3956 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3957 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3958 * as a new "allocated" block region. Otherwise, we will return 0 and
3959 * ext4_ext_map_blocks() will then allocate one or more new clusters
3960 * by calling ext4_mb_new_blocks().
3961 */
Aditya Kalid8990242011-09-09 19:18:51 -04003962static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003963 struct ext4_map_blocks *map,
3964 struct ext4_extent *ex,
3965 struct ext4_ext_path *path)
3966{
Aditya Kalid8990242011-09-09 19:18:51 -04003967 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003968 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003969 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003970 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003971 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3972 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3973 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3974
3975 /* The extent passed in that we are trying to match */
3976 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3977 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3978
3979 /* The requested region passed into ext4_map_blocks() */
3980 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003981
3982 if ((rr_cluster_start == ex_cluster_end) ||
3983 (rr_cluster_start == ex_cluster_start)) {
3984 if (rr_cluster_start == ex_cluster_end)
3985 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003986 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003987 map->m_len = min(map->m_len,
3988 (unsigned) sbi->s_cluster_ratio - c_offset);
3989 /*
3990 * Check for and handle this case:
3991 *
3992 * |--------- cluster # N-------------|
3993 * |------- extent ----|
3994 * |--- requested region ---|
3995 * |===========|
3996 */
3997
3998 if (map->m_lblk < ee_block)
3999 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4000
4001 /*
4002 * Check for the case where there is already another allocated
4003 * block to the right of 'ex' but before the end of the cluster.
4004 *
4005 * |------------- cluster # N-------------|
4006 * |----- ex -----| |---- ex_right ----|
4007 * |------ requested region ------|
4008 * |================|
4009 */
4010 if (map->m_lblk > ee_block) {
4011 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4012 map->m_len = min(map->m_len, next - map->m_lblk);
4013 }
Aditya Kalid8990242011-09-09 19:18:51 -04004014
4015 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004016 return 1;
4017 }
Aditya Kalid8990242011-09-09 19:18:51 -04004018
4019 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004020 return 0;
4021}
4022
4023
4024/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004025 * Block allocation/map/preallocation routine for extents based files
4026 *
4027 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004028 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004029 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4030 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004031 *
4032 * return > 0, number of of blocks already mapped/allocated
4033 * if create == 0 and these are pre-allocated blocks
4034 * buffer head is unmapped
4035 * otherwise blocks are mapped
4036 *
4037 * return = 0, if plain look up failed (blocks have not been allocated)
4038 * buffer head is unmapped
4039 *
4040 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004041 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004042int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4043 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004044{
4045 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004046 struct ext4_extent newex, *ex, *ex2;
4047 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004048 ext4_fsblk_t newblock = 0;
Eric Whitney34990462020-03-11 16:50:33 -04004049 int err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004050 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004051 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004052 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004053 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07004054
Ritesh Harjani70aa1552020-05-10 11:54:55 +05304055 ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004056 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004057
Alex Tomasa86c6182006-10-11 01:21:03 -07004058 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004059 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004060 if (IS_ERR(path)) {
4061 err = PTR_ERR(path);
4062 path = NULL;
4063 goto out2;
4064 }
4065
4066 depth = ext_depth(inode);
4067
4068 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004069 * consistent leaf must not be empty;
4070 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004071 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004072 */
Frank Mayhar273df552010-03-02 11:46:09 -05004073 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4074 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004075 "lblock: %lu, depth: %d pblock %lld",
4076 (unsigned long) map->m_lblk, depth,
4077 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004078 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004079 goto out2;
4080 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004081
Avantika Mathur7e028972006-12-06 20:41:33 -08004082 ex = path[depth].p_ext;
4083 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004084 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004085 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004086 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004087
Lukas Czernerb8a86842014-03-18 18:05:35 -04004088
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004089 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004090 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004091 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004092 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004093 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004094
4095 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4096
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004097 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004098 if (in_range(map->m_lblk, ee_block, ee_len)) {
4099 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004100 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004101 allocated = ee_len - (map->m_lblk - ee_block);
Ritesh Harjani70aa1552020-05-10 11:54:55 +05304102 ext_debug(inode, "%u fit into %u:%d -> %llu\n",
4103 map->m_lblk, ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004104
Lukas Czernerb8a86842014-03-18 18:05:35 -04004105 /*
4106 * If the extent is initialized check whether the
4107 * caller wants to convert it to unwritten.
4108 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004109 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004110 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004111 err = convert_initialized_extent(handle,
4112 inode, map, &path, &allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004113 goto out2;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004114 } else if (!ext4_ext_is_unwritten(ex)) {
Lukas Czerner78771912012-03-19 23:05:43 -04004115 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004116 }
Zheng Liu69eb33d2013-02-18 00:31:07 -05004117
Lukas Czerner556615d2014-04-20 23:45:47 -04004118 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004119 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004120 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004121 if (ret < 0)
4122 err = ret;
4123 else
4124 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004125 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004126 }
4127 }
4128
4129 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004130 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004131 * we couldn't try to create block if create flag is zero
4132 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004133 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004134 ext4_lblk_t hole_start, hole_len;
4135
Jan Karafacab4d2016-03-09 22:54:00 -05004136 hole_start = map->m_lblk;
4137 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004138 /*
4139 * put just found gap into cache to speed up
4140 * subsequent requests
4141 */
Jan Kara140a5252016-03-09 22:46:57 -05004142 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004143
4144 /* Update hole_len to reflect hole size after map->m_lblk */
4145 if (hole_start != map->m_lblk)
4146 hole_len -= map->m_lblk - hole_start;
4147 map->m_pblk = 0;
4148 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4149
Alex Tomasa86c6182006-10-11 01:21:03 -07004150 goto out2;
4151 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004152
Alex Tomasa86c6182006-10-11 01:21:03 -07004153 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004154 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004155 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004156 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004157 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004158
4159 /*
4160 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004161 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004162 */
4163 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004164 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004165 ar.len = allocated = map->m_len;
4166 newblock = map->m_pblk;
4167 goto got_allocated_blocks;
4168 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004169
Alex Tomasc9de5602008-01-29 00:19:52 -05004170 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004171 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004172 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4173 if (err)
4174 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004175 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004176 ex2 = NULL;
4177 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004178 if (err)
4179 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004180
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004181 /* Check if the extent after searching to the right implies a
4182 * cluster we can use. */
4183 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004184 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004185 ar.len = allocated = map->m_len;
4186 newblock = map->m_pblk;
4187 goto got_allocated_blocks;
4188 }
4189
Amit Arora749269f2007-07-18 09:02:56 -04004190 /*
4191 * See if request is beyond maximum number of blocks we can have in
4192 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004193 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4194 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004195 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004196 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004197 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004198 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004199 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4200 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4201 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004202
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004203 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004204 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004205 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004206 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004207 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004208 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004209 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004210
4211 /* allocate new block */
4212 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004213 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4214 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004215 /*
4216 * We calculate the offset from the beginning of the cluster
4217 * for the logical block number, since when we allocate a
4218 * physical cluster, the physical block should start at the
4219 * same offset from the beginning of the cluster. This is
4220 * needed so that future calls to get_implied_cluster_alloc()
4221 * work correctly.
4222 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004223 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004224 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4225 ar.goal -= offset;
4226 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004227 if (S_ISREG(inode->i_mode))
4228 ar.flags = EXT4_MB_HINT_DATA;
4229 else
4230 /* disable in-core preallocation for non-regular files */
4231 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004232 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4233 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004234 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4235 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004236 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4237 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004238 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004239 if (!newblock)
4240 goto out2;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004241 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004242 ar.len = EXT4_C2B(sbi, ar.len) - offset;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05304243 ext_debug(inode, "allocate new block: goal %llu, found %llu/%u, requested %u\n",
Ritesh Harjaniec8c60b2020-05-10 11:54:52 +05304244 ar.goal, newblock, ar.len, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004245 if (ar.len > allocated)
4246 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004247
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004248got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004249 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004250 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004251 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004252 /* Mark unwritten */
Eric Whitney34990462020-03-11 16:50:33 -04004253 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Lukas Czerner556615d2014-04-20 23:45:47 -04004254 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004255 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004256 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004257
Eric Whitney4337ecd2020-02-11 16:02:16 -05004258 err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
Eric Whitney34990462020-03-11 16:50:33 -04004259 if (err) {
4260 if (allocated_clusters) {
4261 int fb_flags = 0;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004262
Eric Whitney34990462020-03-11 16:50:33 -04004263 /*
4264 * free data blocks we just allocated.
4265 * not a good idea to call discard here directly,
4266 * but otherwise we'd need to call it every free().
4267 */
4268 ext4_discard_preallocations(inode);
4269 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4270 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
4271 ext4_free_blocks(handle, inode, NULL, newblock,
4272 EXT4_C2B(sbi, allocated_clusters),
4273 fb_flags);
4274 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004275 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004276 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004277
Alex Tomasa86c6182006-10-11 01:21:03 -07004278 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004279 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004280 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004281 if (allocated > map->m_len)
4282 allocated = map->m_len;
4283 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004284
Jan Karab436b9b2009-12-08 23:51:10 -05004285 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004286 * Reduce the reserved cluster count to reflect successful deferred
4287 * allocation of delayed allocated clusters or direct allocation of
4288 * clusters discovered to be delayed allocated. Once allocated, a
4289 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004290 */
Eric Whitney29711482020-03-11 16:51:25 -04004291 if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) {
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004292 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004293 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004294 * When allocating delayed allocated clusters, simply
4295 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004296 */
4297 ext4_da_update_reserve_space(inode, allocated_clusters,
4298 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004299 } else {
4300 ext4_lblk_t lblk, len;
4301 unsigned int n;
4302
4303 /*
4304 * When allocating non-delayed allocated clusters
4305 * (from fallocate, filemap, DIO, or clusters
4306 * allocated when delalloc has been disabled by
4307 * ext4_nonda_switch), reduce the reserved cluster
4308 * count by the number of allocated clusters that
4309 * have previously been delayed allocated. Quota
4310 * has been claimed by ext4_mb_new_blocks() above,
4311 * so release the quota reservations made for any
4312 * previously delayed allocated clusters.
4313 */
4314 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4315 len = allocated_clusters << sbi->s_cluster_bits;
4316 n = ext4_es_delayed_clu(inode, lblk, len);
4317 if (n > 0)
4318 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004319 }
4320 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004321
4322 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004323 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004324 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004325 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004326 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004327 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004328 else
Jan Karab436b9b2009-12-08 23:51:10 -05004329 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004330out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004331 if (allocated > map->m_len)
4332 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004333 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004334 map->m_flags |= EXT4_MAP_MAPPED;
4335 map->m_pblk = newblock;
4336 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004337out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004338 ext4_ext_drop_refs(path);
4339 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004340
Theodore Ts'o63b99962013-07-16 10:28:47 -04004341 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4342 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004343 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004344}
4345
Theodore Ts'od0abb362016-11-13 22:02:28 -05004346int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004347{
Alex Tomasa86c6182006-10-11 01:21:03 -07004348 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004349 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004350 int err = 0;
4351
4352 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004353 * TODO: optimization is possible here.
4354 * Probably we need not scan at all,
4355 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004356 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004357
4358 /* we have to know where to truncate from in crash case */
4359 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004360 err = ext4_mark_inode_dirty(handle, inode);
4361 if (err)
4362 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004363
4364 last_block = (inode->i_size + sb->s_blocksize - 1)
4365 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004366retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004367 err = ext4_es_remove_extent(inode, last_block,
4368 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004369 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004370 cond_resched();
4371 congestion_wait(BLK_RW_ASYNC, HZ/50);
4372 goto retry;
4373 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004374 if (err)
4375 return err;
Theodore Ts'o73c384c02020-05-07 10:50:28 -07004376retry_remove_space:
4377 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
4378 if (err == -ENOMEM) {
4379 cond_resched();
4380 congestion_wait(BLK_RW_ASYNC, HZ/50);
4381 goto retry_remove_space;
4382 }
4383 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004384}
4385
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004386static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004387 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004388 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004389{
Al Viro496ad9a2013-01-23 17:07:38 -05004390 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004391 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004392 int ret = 0;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004393 int ret2 = 0, ret3 = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004394 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004395 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004396 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004397 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004398 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004399
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004400 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004401 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004402 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004403 /*
4404 * Don't normalize the request if it can fit in one extent so
4405 * that it doesn't get unnecessarily split into multiple
4406 * extents.
4407 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004408 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004409 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004410
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004411 /*
4412 * credits to insert 1 extent into extent tree
4413 */
4414 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004415 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004416
Amit Aroraa2df2a62007-07-17 21:42:41 -04004417retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004418 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004419 /*
4420 * Recalculate credits when extent tree depth changes.
4421 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004422 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004423 credits = ext4_chunk_trans_blocks(inode, len);
4424 depth = ext_depth(inode);
4425 }
4426
Theodore Ts'o9924a922013-02-08 21:59:22 -05004427 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4428 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004429 if (IS_ERR(handle)) {
4430 ret = PTR_ERR(handle);
4431 break;
4432 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004433 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004434 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004435 ext4_debug("inode #%lu: block %u: len %u: "
4436 "ext4_ext_map_blocks returned %d",
4437 inode->i_ino, map.m_lblk,
4438 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004439 ext4_mark_inode_dirty(handle, inode);
4440 ret2 = ext4_journal_stop(handle);
4441 break;
4442 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004443 map.m_lblk += ret;
4444 map.m_len = len = len - ret;
4445 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004446 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004447 if (new_size) {
4448 if (epos > new_size)
4449 epos = new_size;
4450 if (ext4_update_inode_size(inode, epos) & 0x1)
4451 inode->i_mtime = inode->i_ctime;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004452 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004453 ret2 = ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004454 ext4_update_inode_fsync_trans(handle, inode, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004455 ret3 = ext4_journal_stop(handle);
4456 ret2 = ret3 ? ret3 : ret2;
4457 if (unlikely(ret2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004458 break;
4459 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004460 if (ret == -ENOSPC &&
4461 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4462 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004463 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004464 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004465
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004466 return ret > 0 ? ret2 : ret;
4467}
4468
Eric Biggers43f81672019-12-31 12:04:40 -06004469static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
4470
4471static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len);
4472
Lukas Czernerb8a86842014-03-18 18:05:35 -04004473static long ext4_zero_range(struct file *file, loff_t offset,
4474 loff_t len, int mode)
4475{
4476 struct inode *inode = file_inode(file);
4477 handle_t *handle = NULL;
4478 unsigned int max_blocks;
4479 loff_t new_size = 0;
4480 int ret = 0;
4481 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004482 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004483 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004484 loff_t start, end;
4485 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004486 unsigned int blkbits = inode->i_blkbits;
4487
4488 trace_ext4_zero_range(inode, offset, len, mode);
4489
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004490 /* Call ext4_force_commit to flush all data in case of data=journal. */
4491 if (ext4_should_journal_data(inode)) {
4492 ret = ext4_force_commit(inode->i_sb);
4493 if (ret)
4494 return ret;
4495 }
4496
Lukas Czernerb8a86842014-03-18 18:05:35 -04004497 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004498 * Round up offset. This is not fallocate, we neet to zero out
4499 * blocks, so convert interior block aligned part of the range to
4500 * unwritten and possibly manually zero out unaligned parts of the
4501 * range.
4502 */
4503 start = round_up(offset, 1 << blkbits);
4504 end = round_down((offset + len), 1 << blkbits);
4505
4506 if (start < offset || end > offset + len)
4507 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004508 partial_begin = offset & ((1 << blkbits) - 1);
4509 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004510
4511 lblk = start >> blkbits;
4512 max_blocks = (end >> blkbits);
4513 if (max_blocks < lblk)
4514 max_blocks = 0;
4515 else
4516 max_blocks -= lblk;
4517
Al Viro59551022016-01-22 15:40:57 -05004518 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004519
4520 /*
Christophe JAILLET80dd4972020-05-03 22:06:47 +02004521 * Indirect files do not support unwritten extents
Lukas Czernerb8a86842014-03-18 18:05:35 -04004522 */
4523 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4524 ret = -EOPNOTSUPP;
4525 goto out_mutex;
4526 }
4527
4528 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004529 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004530 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004531 new_size = offset + len;
4532 ret = inode_newsize_ok(inode, new_size);
4533 if (ret)
4534 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004535 }
4536
Lukas Czerner0f2af212015-04-03 00:09:13 -04004537 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004538
Jan Kara17048e82015-12-07 14:29:17 -05004539 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004540 inode_dio_wait(inode);
4541
Lukas Czerner0f2af212015-04-03 00:09:13 -04004542 /* Preallocate the range including the unaligned edges */
4543 if (partial_begin || partial_end) {
4544 ret = ext4_alloc_file_blocks(file,
4545 round_down(offset, 1 << blkbits) >> blkbits,
4546 (round_up((offset + len), 1 << blkbits) -
4547 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004548 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004549 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004550 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004551
4552 }
4553
4554 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004555 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004556 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4557 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004558
Jan Karaea3d7202015-12-07 14:28:03 -05004559 /*
4560 * Prevent page faults from reinstantiating pages we have
4561 * released from page cache.
4562 */
4563 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004564
4565 ret = ext4_break_layouts(inode);
4566 if (ret) {
4567 up_write(&EXT4_I(inode)->i_mmap_sem);
4568 goto out_mutex;
4569 }
4570
Jan Kara01127842015-12-07 14:34:49 -05004571 ret = ext4_update_disksize_before_punch(inode, offset, len);
4572 if (ret) {
4573 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004574 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004575 }
Jan Karaea3d7202015-12-07 14:28:03 -05004576 /* Now release the pages and zero block aligned part of pages */
4577 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004578 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004579
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004580 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004581 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004582 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004583 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004584 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004585 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004586 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004587 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004588
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004589 /*
4590 * In worst case we have to writeout two nonadjacent unwritten
4591 * blocks and update the inode
4592 */
4593 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4594 if (ext4_should_journal_data(inode))
4595 credits += 2;
4596 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004597 if (IS_ERR(handle)) {
4598 ret = PTR_ERR(handle);
4599 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004600 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004601 }
4602
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004603 inode->i_mtime = inode->i_ctime = current_time(inode);
Eric Whitney4337ecd2020-02-11 16:02:16 -05004604 if (new_size)
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004605 ext4_update_inode_size(inode, new_size);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004606 ret = ext4_mark_inode_dirty(handle, inode);
4607 if (unlikely(ret))
4608 goto out_handle;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004609
4610 /* Zero out partial block at the edges of the range */
4611 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004612 if (ret >= 0)
4613 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004614
4615 if (file->f_flags & O_SYNC)
4616 ext4_handle_sync(handle);
4617
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004618out_handle:
Lukas Czernerb8a86842014-03-18 18:05:35 -04004619 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004620out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004621 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004622 return ret;
4623}
4624
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004625/*
4626 * preallocate space for a file. This implements ext4's fallocate file
4627 * operation, which gets called from sys_fallocate system call.
4628 * For block-mapped files, posix_fallocate should fall back to the method
4629 * of writing zeroes to the required new blocks (the same behavior which is
4630 * expected for file systems which do not support fallocate() system call).
4631 */
4632long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4633{
4634 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004635 loff_t new_size = 0;
4636 unsigned int max_blocks;
4637 int ret = 0;
4638 int flags;
4639 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004640 unsigned int blkbits = inode->i_blkbits;
4641
Michael Halcrow2058f832015-04-12 00:55:10 -04004642 /*
4643 * Encrypted inodes can't handle collapse range or insert
4644 * range since we would need to re-encrypt blocks with a
4645 * different IV or XTS tweak (which are based on the logical
4646 * block number).
Michael Halcrow2058f832015-04-12 00:55:10 -04004647 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304648 if (IS_ENCRYPTED(inode) &&
Eric Biggers457b1e32019-12-26 09:42:16 -06004649 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004650 return -EOPNOTSUPP;
4651
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004652 /* Return error if mode is not supported */
4653 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004654 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4655 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004656 return -EOPNOTSUPP;
4657
4658 if (mode & FALLOC_FL_PUNCH_HOLE)
4659 return ext4_punch_hole(inode, offset, len);
4660
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004661 ret = ext4_convert_inline_data(inode);
4662 if (ret)
4663 return ret;
4664
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004665 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4666 return ext4_collapse_range(inode, offset, len);
4667
Namjae Jeon331573f2015-06-09 01:55:03 -04004668 if (mode & FALLOC_FL_INSERT_RANGE)
4669 return ext4_insert_range(inode, offset, len);
4670
Lukas Czernerb8a86842014-03-18 18:05:35 -04004671 if (mode & FALLOC_FL_ZERO_RANGE)
4672 return ext4_zero_range(file, offset, len, mode);
4673
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004674 trace_ext4_fallocate_enter(inode, offset, len, mode);
4675 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004676
Fabian Frederick518eaa62016-09-15 11:55:01 -04004677 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004678 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004679
Al Viro59551022016-01-22 15:40:57 -05004680 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004681
Davide Italiano280227a2015-05-02 23:21:15 -04004682 /*
4683 * We only support preallocation for extent-based files only
4684 */
4685 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4686 ret = -EOPNOTSUPP;
4687 goto out;
4688 }
4689
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004690 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004691 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004692 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004693 new_size = offset + len;
4694 ret = inode_newsize_ok(inode, new_size);
4695 if (ret)
4696 goto out;
4697 }
4698
Jan Kara17048e82015-12-07 14:29:17 -05004699 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004700 inode_dio_wait(inode);
4701
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004702 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004703 if (ret)
4704 goto out;
4705
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004706 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4707 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4708 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004709 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004710out:
Al Viro59551022016-01-22 15:40:57 -05004711 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004712 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4713 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004714}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004715
4716/*
Mingming Cao00314622009-09-28 15:49:08 -04004717 * This function convert a range of blocks to written extents
4718 * The caller of this function will pass the start offset and the size.
4719 * all unwritten extents within this range will be converted to
4720 * written extents.
4721 *
4722 * This function is called from the direct IO end io call back
4723 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004724 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004725 */
Jan Kara6b523df2013-06-04 13:21:11 -04004726int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4727 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004728{
Mingming Cao00314622009-09-28 15:49:08 -04004729 unsigned int max_blocks;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004730 int ret = 0, ret2 = 0, ret3 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004731 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304732 unsigned int blkbits = inode->i_blkbits;
4733 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004734
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004735 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004736 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4737
Ritesh Harjania00713e2019-10-16 13:07:08 +05304738 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004739 /*
4740 * credits to insert 1 extent into extent tree
4741 */
4742 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4743 }
Mingming Cao00314622009-09-28 15:49:08 -04004744 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004745 map.m_lblk += ret;
4746 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04004747 if (credits) {
4748 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4749 credits);
4750 if (IS_ERR(handle)) {
4751 ret = PTR_ERR(handle);
4752 break;
4753 }
Mingming Cao00314622009-09-28 15:49:08 -04004754 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004755 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004756 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05004757 if (ret <= 0)
4758 ext4_warning(inode->i_sb,
4759 "inode #%lu: block %u: len %u: "
4760 "ext4_ext_map_blocks returned %d",
4761 inode->i_ino, map.m_lblk,
4762 map.m_len, ret);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004763 ret2 = ext4_mark_inode_dirty(handle, inode);
4764 if (credits) {
4765 ret3 = ext4_journal_stop(handle);
4766 if (unlikely(ret3))
4767 ret2 = ret3;
4768 }
4769
Jan Kara6b523df2013-06-04 13:21:11 -04004770 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04004771 break;
4772 }
4773 return ret > 0 ? ret2 : ret;
4774}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004775
Ritesh Harjania00713e2019-10-16 13:07:08 +05304776int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
4777{
4778 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304779 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304780
4781 /*
4782 * This is somewhat ugly but the idea is clear: When transaction is
4783 * reserved, everything goes into it. Otherwise we rather start several
4784 * smaller transactions for conversion of each extent separately.
4785 */
4786 if (handle) {
4787 handle = ext4_journal_start_reserved(handle,
4788 EXT4_HT_EXT_CONVERT);
4789 if (IS_ERR(handle))
4790 return PTR_ERR(handle);
4791 }
4792
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304793 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
4794 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
4795 io_end_vec->offset,
4796 io_end_vec->size);
4797 if (ret)
4798 break;
4799 }
4800
Ritesh Harjania00713e2019-10-16 13:07:08 +05304801 if (handle)
4802 err = ext4_journal_stop(handle);
4803
4804 return ret < 0 ? ret : err;
4805}
4806
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304807static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004808{
4809 __u64 physical = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304810 __u64 length = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004811 int blockbits = inode->i_sb->s_blocksize_bits;
4812 int error = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304813 u16 iomap_type;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004814
4815 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004816 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004817 struct ext4_iloc iloc;
4818 int offset; /* offset of xattr in inode */
4819
4820 error = ext4_get_inode_loc(inode, &iloc);
4821 if (error)
4822 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04004823 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004824 offset = EXT4_GOOD_OLD_INODE_SIZE +
4825 EXT4_I(inode)->i_extra_isize;
4826 physical += offset;
4827 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004828 brelse(iloc.bh);
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304829 iomap_type = IOMAP_INLINE;
4830 } else if (EXT4_I(inode)->i_file_acl) { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04004831 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004832 length = inode->i_sb->s_blocksize;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304833 iomap_type = IOMAP_MAPPED;
4834 } else {
4835 /* no in-inode or external block for xattr, so return -ENOENT */
4836 error = -ENOENT;
4837 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004838 }
4839
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304840 iomap->addr = physical;
4841 iomap->offset = 0;
4842 iomap->length = length;
4843 iomap->type = iomap_type;
4844 iomap->flags = 0;
4845out:
4846 return error;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004847}
4848
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304849static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset,
4850 loff_t length, unsigned flags,
4851 struct iomap *iomap, struct iomap *srcmap)
4852{
4853 int error;
4854
4855 error = ext4_iomap_xattr_fiemap(inode, iomap);
4856 if (error == 0 && (offset >= iomap->length))
4857 error = -ENOENT;
4858 return error;
4859}
4860
4861static const struct iomap_ops ext4_iomap_xattr_ops = {
4862 .iomap_begin = ext4_iomap_xattr_begin,
4863};
4864
4865static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4866 __u64 start, __u64 len, bool from_es_cache)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004867{
4868 ext4_lblk_t start_blk;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304869 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004870 int error = 0;
4871
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004872 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
4873 error = ext4_ext_precache(inode);
4874 if (error)
4875 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004876 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004877 }
4878
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304879 if (from_es_cache)
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004880 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304881
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004882 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004883 return -EBADR;
4884
4885 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304886 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
4887 error = iomap_fiemap(inode, fieinfo, start, len,
4888 &ext4_iomap_xattr_ops);
4889 } else if (!from_es_cache) {
4890 error = iomap_fiemap(inode, fieinfo, start, len,
4891 &ext4_iomap_report_ops);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004892 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004893 ext4_lblk_t len_blks;
4894 __u64 last_blk;
4895
Eric Sandeen6873fa02008-10-07 00:46:36 -04004896 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004897 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004898 if (last_blk >= EXT_MAX_BLOCKS)
4899 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004900 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004901
4902 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004903 * Walk the extent tree gathering extent information
4904 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004905 */
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304906 error = ext4_fill_es_cache_info(inode, start_blk, len_blks,
4907 fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004908 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04004909 return error;
4910}
Namjae Jeon9eb79482014-02-23 15:18:59 -05004911
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004912int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4913 __u64 start, __u64 len)
4914{
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304915 return _ext4_fiemap(inode, fieinfo, start, len, false);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004916}
4917
4918int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
4919 __u64 start, __u64 len)
4920{
4921 if (ext4_has_inline_data(inode)) {
4922 int has_inline;
4923
4924 down_read(&EXT4_I(inode)->xattr_sem);
4925 has_inline = ext4_has_inline_data(inode);
4926 up_read(&EXT4_I(inode)->xattr_sem);
4927 if (has_inline)
4928 return 0;
4929 }
4930
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304931 return _ext4_fiemap(inode, fieinfo, start, len, true);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004932}
4933
4934
Namjae Jeon9eb79482014-02-23 15:18:59 -05004935/*
4936 * ext4_access_path:
4937 * Function to access the path buffer for marking it dirty.
4938 * It also checks if there are sufficient credits left in the journal handle
4939 * to update path.
4940 */
4941static int
4942ext4_access_path(handle_t *handle, struct inode *inode,
4943 struct ext4_ext_path *path)
4944{
4945 int credits, err;
4946
4947 if (!ext4_handle_valid(handle))
4948 return 0;
4949
4950 /*
4951 * Check if need to extend journal credits
4952 * 3 for leaf, sb, and inode plus 2 (bmap and group
4953 * descriptor) for each block group; assume two block
4954 * groups
4955 */
Jan Karaa4130362019-11-05 17:44:16 +01004956 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01004957 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01004958 if (err < 0)
4959 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004960
4961 err = ext4_ext_get_access(handle, inode, path);
4962 return err;
4963}
4964
4965/*
4966 * ext4_ext_shift_path_extents:
4967 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04004968 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
4969 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05004970 */
4971static int
4972ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
4973 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04004974 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05004975{
4976 int depth, err = 0;
4977 struct ext4_extent *ex_start, *ex_last;
zhengbin4756ee12019-12-25 10:45:59 +08004978 bool update = false;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004979 depth = path->p_depth;
4980
4981 while (depth >= 0) {
4982 if (depth == path->p_depth) {
4983 ex_start = path[depth].p_ext;
4984 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004985 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004986
4987 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05004988
4989 err = ext4_access_path(handle, inode, path + depth);
4990 if (err)
4991 goto out;
4992
4993 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
zhengbin4756ee12019-12-25 10:45:59 +08004994 update = true;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004995
Namjae Jeon9eb79482014-02-23 15:18:59 -05004996 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04004997 if (SHIFT == SHIFT_LEFT) {
4998 le32_add_cpu(&ex_start->ee_block,
4999 -shift);
5000 /* Try to merge to the left. */
5001 if ((ex_start >
5002 EXT_FIRST_EXTENT(path[depth].p_hdr))
5003 &&
5004 ext4_ext_try_to_merge_right(inode,
5005 path, ex_start - 1))
5006 ex_last--;
5007 else
5008 ex_start++;
5009 } else {
5010 le32_add_cpu(&ex_last->ee_block, shift);
5011 ext4_ext_try_to_merge_right(inode, path,
5012 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005013 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005014 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005015 }
5016 err = ext4_ext_dirty(handle, inode, path + depth);
5017 if (err)
5018 goto out;
5019
5020 if (--depth < 0 || !update)
5021 break;
5022 }
5023
5024 /* Update index too */
5025 err = ext4_access_path(handle, inode, path + depth);
5026 if (err)
5027 goto out;
5028
Namjae Jeon331573f2015-06-09 01:55:03 -04005029 if (SHIFT == SHIFT_LEFT)
5030 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5031 else
5032 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005033 err = ext4_ext_dirty(handle, inode, path + depth);
5034 if (err)
5035 goto out;
5036
5037 /* we are done if current index is not a starting index */
5038 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5039 break;
5040
5041 depth--;
5042 }
5043
5044out:
5045 return err;
5046}
5047
5048/*
5049 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005050 * All the extents which lies in the range from @start to the last allocated
5051 * block for the @inode are shifted either towards left or right (depending
5052 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005053 * On success, 0 is returned, error otherwise.
5054 */
5055static int
5056ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005057 ext4_lblk_t start, ext4_lblk_t shift,
5058 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005059{
5060 struct ext4_ext_path *path;
5061 int ret = 0, depth;
5062 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005063 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005064
5065 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005066 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5067 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005068 if (IS_ERR(path))
5069 return PTR_ERR(path);
5070
5071 depth = path->p_depth;
5072 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005073 if (!extent)
5074 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005075
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005076 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005077
Namjae Jeon331573f2015-06-09 01:55:03 -04005078 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005079 * For left shifts, make sure the hole on the left is big enough to
5080 * accommodate the shift. For right shifts, make sure the last extent
5081 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005082 */
5083 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005084 path = ext4_find_extent(inode, start - 1, &path,
5085 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005086 if (IS_ERR(path))
5087 return PTR_ERR(path);
5088 depth = path->p_depth;
5089 extent = path[depth].p_ext;
5090 if (extent) {
5091 ex_start = le32_to_cpu(extent->ee_block);
5092 ex_end = le32_to_cpu(extent->ee_block) +
5093 ext4_ext_get_actual_len(extent);
5094 } else {
5095 ex_start = 0;
5096 ex_end = 0;
5097 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005098
Namjae Jeon331573f2015-06-09 01:55:03 -04005099 if ((start == ex_start && shift > ex_start) ||
5100 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005101 ret = -EINVAL;
5102 goto out;
5103 }
5104 } else {
5105 if (shift > EXT_MAX_BLOCKS -
5106 (stop + ext4_ext_get_actual_len(extent))) {
5107 ret = -EINVAL;
5108 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005109 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005110 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005111
Namjae Jeon331573f2015-06-09 01:55:03 -04005112 /*
5113 * In case of left shift, iterator points to start and it is increased
5114 * till we reach stop. In case of right shift, iterator points to stop
5115 * and it is decreased till we reach start.
5116 */
5117 if (SHIFT == SHIFT_LEFT)
5118 iterator = &start;
5119 else
5120 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005121
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005122 /*
5123 * Its safe to start updating extents. Start and stop are unsigned, so
5124 * in case of right shift if extent with 0 block is reached, iterator
5125 * becomes NULL to indicate the end of the loop.
5126 */
5127 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005128 path = ext4_find_extent(inode, *iterator, &path,
5129 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005130 if (IS_ERR(path))
5131 return PTR_ERR(path);
5132 depth = path->p_depth;
5133 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005134 if (!extent) {
5135 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005136 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005137 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005138 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005139 if (SHIFT == SHIFT_LEFT && *iterator >
5140 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005141 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005142 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5143 path[depth].p_ext++;
5144 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005145 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005146 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005147 }
5148 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005149
5150 if (SHIFT == SHIFT_LEFT) {
5151 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5152 *iterator = le32_to_cpu(extent->ee_block) +
5153 ext4_ext_get_actual_len(extent);
5154 } else {
5155 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005156 if (le32_to_cpu(extent->ee_block) > 0)
5157 *iterator = le32_to_cpu(extent->ee_block) - 1;
5158 else
5159 /* Beginning is reached, end of the loop */
5160 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005161 /* Update path extent in case we need to stop */
5162 while (le32_to_cpu(extent->ee_block) < start)
5163 extent++;
5164 path[depth].p_ext = extent;
5165 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005166 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005167 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005168 if (ret)
5169 break;
5170 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005171out:
5172 ext4_ext_drop_refs(path);
5173 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005174 return ret;
5175}
5176
5177/*
5178 * ext4_collapse_range:
5179 * This implements the fallocate's collapse range functionality for ext4
5180 * Returns: 0 and non-zero on error.
5181 */
Eric Biggers43f81672019-12-31 12:04:40 -06005182static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005183{
5184 struct super_block *sb = inode->i_sb;
5185 ext4_lblk_t punch_start, punch_stop;
5186 handle_t *handle;
5187 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005188 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005189 int ret;
5190
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005191 /*
5192 * We need to test this early because xfstests assumes that a
5193 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5194 * system does not support collapse range.
5195 */
5196 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5197 return -EOPNOTSUPP;
5198
Eric Biggers9b02e492019-12-31 12:04:38 -06005199 /* Collapse range works only on fs cluster size aligned regions. */
5200 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005201 return -EINVAL;
5202
Namjae Jeon9eb79482014-02-23 15:18:59 -05005203 trace_ext4_collapse_range(inode, offset, len);
5204
5205 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5206 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5207
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005208 /* Call ext4_force_commit to flush all data in case of data=journal. */
5209 if (ext4_should_journal_data(inode)) {
5210 ret = ext4_force_commit(inode->i_sb);
5211 if (ret)
5212 return ret;
5213 }
5214
Al Viro59551022016-01-22 15:40:57 -05005215 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005216 /*
5217 * There is no need to overlap collapse range with EOF, in which case
5218 * it is effectively a truncate operation
5219 */
Eric Biggers9b02e492019-12-31 12:04:38 -06005220 if (offset + len >= inode->i_size) {
Lukas Czerner23fffa92014-04-12 09:56:41 -04005221 ret = -EINVAL;
5222 goto out_mutex;
5223 }
5224
Namjae Jeon9eb79482014-02-23 15:18:59 -05005225 /* Currently just for extent based files */
5226 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5227 ret = -EOPNOTSUPP;
5228 goto out_mutex;
5229 }
5230
Namjae Jeon9eb79482014-02-23 15:18:59 -05005231 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005232 inode_dio_wait(inode);
5233
Jan Karaea3d7202015-12-07 14:28:03 -05005234 /*
5235 * Prevent page faults from reinstantiating pages we have released from
5236 * page cache.
5237 */
5238 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005239
5240 ret = ext4_break_layouts(inode);
5241 if (ret)
5242 goto out_mmap;
5243
Jan Kara32ebffd2015-12-07 14:31:11 -05005244 /*
5245 * Need to round down offset to be aligned with page size boundary
5246 * for page size > block size.
5247 */
5248 ioffset = round_down(offset, PAGE_SIZE);
5249 /*
5250 * Write tail of the last page before removed range since it will get
5251 * removed from the page cache below.
5252 */
5253 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5254 if (ret)
5255 goto out_mmap;
5256 /*
5257 * Write data that will be shifted to preserve them when discarding
5258 * page cache below. We are also protected from pages becoming dirty
5259 * by i_mmap_sem.
5260 */
5261 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5262 LLONG_MAX);
5263 if (ret)
5264 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005265 truncate_pagecache(inode, ioffset);
5266
Namjae Jeon9eb79482014-02-23 15:18:59 -05005267 credits = ext4_writepage_trans_blocks(inode);
5268 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5269 if (IS_ERR(handle)) {
5270 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005271 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005272 }
5273
5274 down_write(&EXT4_I(inode)->i_data_sem);
5275 ext4_discard_preallocations(inode);
5276
5277 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005278 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005279 if (ret) {
5280 up_write(&EXT4_I(inode)->i_data_sem);
5281 goto out_stop;
5282 }
5283
5284 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5285 if (ret) {
5286 up_write(&EXT4_I(inode)->i_data_sem);
5287 goto out_stop;
5288 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005289 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005290
5291 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005292 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005293 if (ret) {
5294 up_write(&EXT4_I(inode)->i_data_sem);
5295 goto out_stop;
5296 }
5297
Eric Biggers9b02e492019-12-31 12:04:38 -06005298 new_size = inode->i_size - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005299 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005300 EXT4_I(inode)->i_disksize = new_size;
5301
Namjae Jeon9eb79482014-02-23 15:18:59 -05005302 up_write(&EXT4_I(inode)->i_data_sem);
5303 if (IS_SYNC(inode))
5304 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005305 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005306 ret = ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005307 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005308
5309out_stop:
5310 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005311out_mmap:
5312 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005313out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005314 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005315 return ret;
5316}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005317
Namjae Jeon331573f2015-06-09 01:55:03 -04005318/*
5319 * ext4_insert_range:
5320 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5321 * The data blocks starting from @offset to the EOF are shifted by @len
5322 * towards right to create a hole in the @inode. Inode size is increased
5323 * by len bytes.
5324 * Returns 0 on success, error otherwise.
5325 */
Eric Biggers43f81672019-12-31 12:04:40 -06005326static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon331573f2015-06-09 01:55:03 -04005327{
5328 struct super_block *sb = inode->i_sb;
5329 handle_t *handle;
5330 struct ext4_ext_path *path;
5331 struct ext4_extent *extent;
5332 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5333 unsigned int credits, ee_len;
5334 int ret = 0, depth, split_flag = 0;
5335 loff_t ioffset;
5336
5337 /*
5338 * We need to test this early because xfstests assumes that an
5339 * insert range of (0, 1) will return EOPNOTSUPP if the file
5340 * system does not support insert range.
5341 */
5342 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5343 return -EOPNOTSUPP;
5344
Eric Biggers9b02e492019-12-31 12:04:38 -06005345 /* Insert range works only on fs cluster size aligned regions. */
5346 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon331573f2015-06-09 01:55:03 -04005347 return -EINVAL;
5348
Namjae Jeon331573f2015-06-09 01:55:03 -04005349 trace_ext4_insert_range(inode, offset, len);
5350
5351 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5352 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5353
5354 /* Call ext4_force_commit to flush all data in case of data=journal */
5355 if (ext4_should_journal_data(inode)) {
5356 ret = ext4_force_commit(inode->i_sb);
5357 if (ret)
5358 return ret;
5359 }
5360
Al Viro59551022016-01-22 15:40:57 -05005361 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005362 /* Currently just for extent based files */
5363 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5364 ret = -EOPNOTSUPP;
5365 goto out_mutex;
5366 }
5367
Eric Biggers9b02e492019-12-31 12:04:38 -06005368 /* Check whether the maximum file size would be exceeded */
5369 if (len > inode->i_sb->s_maxbytes - inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005370 ret = -EFBIG;
5371 goto out_mutex;
5372 }
5373
Eric Biggers9b02e492019-12-31 12:04:38 -06005374 /* Offset must be less than i_size */
5375 if (offset >= inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005376 ret = -EINVAL;
5377 goto out_mutex;
5378 }
5379
Namjae Jeon331573f2015-06-09 01:55:03 -04005380 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005381 inode_dio_wait(inode);
5382
Jan Karaea3d7202015-12-07 14:28:03 -05005383 /*
5384 * Prevent page faults from reinstantiating pages we have released from
5385 * page cache.
5386 */
5387 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005388
5389 ret = ext4_break_layouts(inode);
5390 if (ret)
5391 goto out_mmap;
5392
Jan Kara32ebffd2015-12-07 14:31:11 -05005393 /*
5394 * Need to round down to align start offset to page size boundary
5395 * for page size > block size.
5396 */
5397 ioffset = round_down(offset, PAGE_SIZE);
5398 /* Write out all dirty pages */
5399 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5400 LLONG_MAX);
5401 if (ret)
5402 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005403 truncate_pagecache(inode, ioffset);
5404
Namjae Jeon331573f2015-06-09 01:55:03 -04005405 credits = ext4_writepage_trans_blocks(inode);
5406 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5407 if (IS_ERR(handle)) {
5408 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005409 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005410 }
5411
5412 /* Expand file to avoid data loss if there is error while shifting */
5413 inode->i_size += len;
5414 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005415 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005416 ret = ext4_mark_inode_dirty(handle, inode);
5417 if (ret)
5418 goto out_stop;
5419
5420 down_write(&EXT4_I(inode)->i_data_sem);
5421 ext4_discard_preallocations(inode);
5422
5423 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5424 if (IS_ERR(path)) {
5425 up_write(&EXT4_I(inode)->i_data_sem);
5426 goto out_stop;
5427 }
5428
5429 depth = ext_depth(inode);
5430 extent = path[depth].p_ext;
5431 if (extent) {
5432 ee_start_lblk = le32_to_cpu(extent->ee_block);
5433 ee_len = ext4_ext_get_actual_len(extent);
5434
5435 /*
5436 * If offset_lblk is not the starting block of extent, split
5437 * the extent @offset_lblk
5438 */
5439 if ((offset_lblk > ee_start_lblk) &&
5440 (offset_lblk < (ee_start_lblk + ee_len))) {
5441 if (ext4_ext_is_unwritten(extent))
5442 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5443 EXT4_EXT_MARK_UNWRIT2;
5444 ret = ext4_split_extent_at(handle, inode, &path,
5445 offset_lblk, split_flag,
5446 EXT4_EX_NOCACHE |
5447 EXT4_GET_BLOCKS_PRE_IO |
5448 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5449 }
5450
5451 ext4_ext_drop_refs(path);
5452 kfree(path);
5453 if (ret < 0) {
5454 up_write(&EXT4_I(inode)->i_data_sem);
5455 goto out_stop;
5456 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005457 } else {
5458 ext4_ext_drop_refs(path);
5459 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005460 }
5461
5462 ret = ext4_es_remove_extent(inode, offset_lblk,
5463 EXT_MAX_BLOCKS - offset_lblk);
5464 if (ret) {
5465 up_write(&EXT4_I(inode)->i_data_sem);
5466 goto out_stop;
5467 }
5468
5469 /*
5470 * if offset_lblk lies in a hole which is at start of file, use
5471 * ee_start_lblk to shift extents
5472 */
5473 ret = ext4_ext_shift_extents(inode, handle,
5474 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5475 len_lblk, SHIFT_RIGHT);
5476
5477 up_write(&EXT4_I(inode)->i_data_sem);
5478 if (IS_SYNC(inode))
5479 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005480 if (ret >= 0)
5481 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005482
5483out_stop:
5484 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005485out_mmap:
5486 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005487out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005488 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005489 return ret;
5490}
5491
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005492/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005493 * ext4_swap_extents() - Swap extents between two inodes
5494 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005495 * @inode1: First inode
5496 * @inode2: Second inode
5497 * @lblk1: Start block for first inode
5498 * @lblk2: Start block for second inode
5499 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005500 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005501 * @erp: Pointer to save error value
5502 *
5503 * This helper routine does exactly what is promise "swap extents". All other
5504 * stuff such as page-cache locking consistency, bh mapping consistency or
5505 * extent's data copying must be performed by caller.
5506 * Locking:
5507 * i_mutex is held for both inodes
5508 * i_data_sem is locked for write for both inodes
5509 * Assumptions:
5510 * All pages from requested range are locked for both inodes
5511 */
5512int
5513ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005514 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005515 ext4_lblk_t count, int unwritten, int *erp)
5516{
5517 struct ext4_ext_path *path1 = NULL;
5518 struct ext4_ext_path *path2 = NULL;
5519 int replaced_count = 0;
5520
5521 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5522 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005523 BUG_ON(!inode_is_locked(inode1));
5524 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005525
5526 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005527 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005528 return 0;
5529 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005530 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005531 return 0;
5532
5533 while (count) {
5534 struct ext4_extent *ex1, *ex2, tmp_ex;
5535 ext4_lblk_t e1_blk, e2_blk;
5536 int e1_len, e2_len, len;
5537 int split = 0;
5538
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005539 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305540 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005541 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005542 path1 = NULL;
5543 finish:
5544 count = 0;
5545 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005546 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005547 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305548 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005549 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005550 path2 = NULL;
5551 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005552 }
5553 ex1 = path1[path1->p_depth].p_ext;
5554 ex2 = path2[path2->p_depth].p_ext;
5555 /* Do we have somthing to swap ? */
5556 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005557 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005558
5559 e1_blk = le32_to_cpu(ex1->ee_block);
5560 e2_blk = le32_to_cpu(ex2->ee_block);
5561 e1_len = ext4_ext_get_actual_len(ex1);
5562 e2_len = ext4_ext_get_actual_len(ex2);
5563
5564 /* Hole handling */
5565 if (!in_range(lblk1, e1_blk, e1_len) ||
5566 !in_range(lblk2, e2_blk, e2_len)) {
5567 ext4_lblk_t next1, next2;
5568
5569 /* if hole after extent, then go to next extent */
5570 next1 = ext4_ext_next_allocated_block(path1);
5571 next2 = ext4_ext_next_allocated_block(path2);
5572 /* If hole before extent, then shift to that extent */
5573 if (e1_blk > lblk1)
5574 next1 = e1_blk;
5575 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005576 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005577 /* Do we have something to swap */
5578 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005579 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005580 /* Move to the rightest boundary */
5581 len = next1 - lblk1;
5582 if (len < next2 - lblk2)
5583 len = next2 - lblk2;
5584 if (len > count)
5585 len = count;
5586 lblk1 += len;
5587 lblk2 += len;
5588 count -= len;
5589 goto repeat;
5590 }
5591
5592 /* Prepare left boundary */
5593 if (e1_blk < lblk1) {
5594 split = 1;
5595 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005596 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005597 if (unlikely(*erp))
5598 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005599 }
5600 if (e2_blk < lblk2) {
5601 split = 1;
5602 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005603 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005604 if (unlikely(*erp))
5605 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005606 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005607 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005608 * path must to be revalidated. */
5609 if (split)
5610 goto repeat;
5611
5612 /* Prepare right boundary */
5613 len = count;
5614 if (len > e1_blk + e1_len - lblk1)
5615 len = e1_blk + e1_len - lblk1;
5616 if (len > e2_blk + e2_len - lblk2)
5617 len = e2_blk + e2_len - lblk2;
5618
5619 if (len != e1_len) {
5620 split = 1;
5621 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005622 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005623 if (unlikely(*erp))
5624 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005625 }
5626 if (len != e2_len) {
5627 split = 1;
5628 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005629 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005630 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005631 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005632 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005633 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005634 * path must to be revalidated. */
5635 if (split)
5636 goto repeat;
5637
5638 BUG_ON(e2_len != e1_len);
5639 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005640 if (unlikely(*erp))
5641 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005642 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005643 if (unlikely(*erp))
5644 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005645
5646 /* Both extents are fully inside boundaries. Swap it now */
5647 tmp_ex = *ex1;
5648 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5649 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5650 ex1->ee_len = cpu_to_le16(e2_len);
5651 ex2->ee_len = cpu_to_le16(e1_len);
5652 if (unwritten)
5653 ext4_ext_mark_unwritten(ex2);
5654 if (ext4_ext_is_unwritten(&tmp_ex))
5655 ext4_ext_mark_unwritten(ex1);
5656
5657 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5658 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5659 *erp = ext4_ext_dirty(handle, inode2, path2 +
5660 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005661 if (unlikely(*erp))
5662 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005663 *erp = ext4_ext_dirty(handle, inode1, path1 +
5664 path1->p_depth);
5665 /*
5666 * Looks scarry ah..? second inode already points to new blocks,
5667 * and it was successfully dirtied. But luckily error may happen
5668 * only due to journal error, so full transaction will be
5669 * aborted anyway.
5670 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005671 if (unlikely(*erp))
5672 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005673 lblk1 += len;
5674 lblk2 += len;
5675 replaced_count += len;
5676 count -= len;
5677
5678 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005679 ext4_ext_drop_refs(path1);
5680 kfree(path1);
5681 ext4_ext_drop_refs(path2);
5682 kfree(path2);
5683 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005684 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005685 return replaced_count;
5686}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005687
5688/*
5689 * ext4_clu_mapped - determine whether any block in a logical cluster has
5690 * been mapped to a physical cluster
5691 *
5692 * @inode - file containing the logical cluster
5693 * @lclu - logical cluster of interest
5694 *
5695 * Returns 1 if any block in the logical cluster is mapped, signifying
5696 * that a physical cluster has been allocated for it. Otherwise,
5697 * returns 0. Can also return negative error codes. Derived from
5698 * ext4_ext_map_blocks().
5699 */
5700int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5701{
5702 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5703 struct ext4_ext_path *path;
5704 int depth, mapped = 0, err = 0;
5705 struct ext4_extent *extent;
5706 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5707
5708 /* search for the extent closest to the first block in the cluster */
5709 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5710 if (IS_ERR(path)) {
5711 err = PTR_ERR(path);
5712 path = NULL;
5713 goto out;
5714 }
5715
5716 depth = ext_depth(inode);
5717
5718 /*
5719 * A consistent leaf must not be empty. This situation is possible,
5720 * though, _during_ tree modification, and it's why an assert can't
5721 * be put in ext4_find_extent().
5722 */
5723 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
5724 EXT4_ERROR_INODE(inode,
5725 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
5726 (unsigned long) EXT4_C2B(sbi, lclu),
5727 depth, path[depth].p_block);
5728 err = -EFSCORRUPTED;
5729 goto out;
5730 }
5731
5732 extent = path[depth].p_ext;
5733
5734 /* can't be mapped if the extent tree is empty */
5735 if (extent == NULL)
5736 goto out;
5737
5738 first_lblk = le32_to_cpu(extent->ee_block);
5739 first_lclu = EXT4_B2C(sbi, first_lblk);
5740
5741 /*
5742 * Three possible outcomes at this point - found extent spanning
5743 * the target cluster, to the left of the target cluster, or to the
5744 * right of the target cluster. The first two cases are handled here.
5745 * The last case indicates the target cluster is not mapped.
5746 */
5747 if (lclu >= first_lclu) {
5748 last_lclu = EXT4_B2C(sbi, first_lblk +
5749 ext4_ext_get_actual_len(extent) - 1);
5750 if (lclu <= last_lclu) {
5751 mapped = 1;
5752 } else {
5753 first_lblk = ext4_ext_next_allocated_block(path);
5754 first_lclu = EXT4_B2C(sbi, first_lblk);
5755 if (lclu == first_lclu)
5756 mapped = 1;
5757 }
5758 }
5759
5760out:
5761 ext4_ext_drop_refs(path);
5762 kfree(path);
5763
5764 return err ? err : mapped;
5765}