blob: 031752cfb6f753648d06d9dd3312ba9507116891 [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0
Alex Tomasa86c6182006-10-11 01:21:03 -07002/*
3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4 * Written by Alex Tomas <alex@clusterfs.com>
5 *
6 * Architecture independence:
7 * Copyright (c) 2005, Bull S.A.
8 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
Alex Tomasa86c6182006-10-11 01:21:03 -07009 */
10
11/*
12 * Extents support for EXT4
13 *
14 * TODO:
15 * - ext4*_error() should be used in some situations
16 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
17 * - smart tree reduction
18 */
19
Alex Tomasa86c6182006-10-11 01:21:03 -070020#include <linux/fs.h>
21#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040022#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070023#include <linux/highuid.h>
24#include <linux/pagemap.h>
25#include <linux/quotaops.h>
26#include <linux/string.h>
27#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040029#include <linux/fiemap.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040030#include <linux/backing-dev.h>
Ritesh Harjanid3b6f232020-02-28 14:56:58 +053031#include <linux/iomap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040032#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050033#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050034#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070035
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040036#include <trace/events/ext4.h>
37
Lukas Czerner5f95d212012-03-19 23:03:19 -040038/*
39 * used by extent splitting.
40 */
41#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
42 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040043#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
44#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040045
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040046#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
47#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
48
Darrick J. Wong7ac59902012-04-29 18:37:10 -040049static __le32 ext4_extent_block_csum(struct inode *inode,
50 struct ext4_extent_header *eh)
51{
52 struct ext4_inode_info *ei = EXT4_I(inode);
53 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
54 __u32 csum;
55
56 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
57 EXT4_EXTENT_TAIL_OFFSET(eh));
58 return cpu_to_le32(csum);
59}
60
61static int ext4_extent_block_csum_verify(struct inode *inode,
62 struct ext4_extent_header *eh)
63{
64 struct ext4_extent_tail *et;
65
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040066 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040067 return 1;
68
69 et = find_ext4_extent_tail(eh);
70 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
71 return 0;
72 return 1;
73}
74
75static void ext4_extent_block_csum_set(struct inode *inode,
76 struct ext4_extent_header *eh)
77{
78 struct ext4_extent_tail *et;
79
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040080 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040081 return;
82
83 et = find_ext4_extent_tail(eh);
84 et->et_checksum = ext4_extent_block_csum(inode, eh);
85}
86
Lukas Czerner5f95d212012-03-19 23:03:19 -040087static int ext4_split_extent_at(handle_t *handle,
88 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040089 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -040090 ext4_lblk_t split,
91 int split_flag,
92 int flags);
93
Jan Karaa4130362019-11-05 17:44:16 +010094static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
Alex Tomasa86c6182006-10-11 01:21:03 -070095{
Theodore Ts'o7b808192016-04-25 23:13:17 -040096 /*
Jan Karaa4130362019-11-05 17:44:16 +010097 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
98 * moment, get_block can be called only for blocks inside i_size since
99 * page cache has been already dropped and writes are blocked by
100 * i_mutex. So we can safely drop the i_data_sem here.
Theodore Ts'o7b808192016-04-25 23:13:17 -0400101 */
Jan Karaa4130362019-11-05 17:44:16 +0100102 BUG_ON(EXT4_JOURNAL(inode) == NULL);
103 ext4_discard_preallocations(inode);
104 up_write(&EXT4_I(inode)->i_data_sem);
105 *dropped = 1;
106 return 0;
107}
Jan Kara487caee2009-08-17 22:17:20 -0400108
Jan Karaa4130362019-11-05 17:44:16 +0100109/*
110 * Make sure 'handle' has at least 'check_cred' credits. If not, restart
111 * transaction with 'restart_cred' credits. The function drops i_data_sem
112 * when restarting transaction and gets it after transaction is restarted.
113 *
114 * The function returns 0 on success, 1 if transaction had to be restarted,
115 * and < 0 in case of fatal error.
116 */
117int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
Jan Kara83448bd2019-11-05 17:44:29 +0100118 int check_cred, int restart_cred,
119 int revoke_cred)
Jan Karaa4130362019-11-05 17:44:16 +0100120{
121 int ret;
122 int dropped = 0;
123
124 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
Jan Kara83448bd2019-11-05 17:44:29 +0100125 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
Jan Karaa4130362019-11-05 17:44:16 +0100126 if (dropped)
127 down_write(&EXT4_I(inode)->i_data_sem);
128 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700129}
130
131/*
132 * could return:
133 * - EROFS
134 * - ENOMEM
135 */
136static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
137 struct ext4_ext_path *path)
138{
139 if (path->p_bh) {
140 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400141 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700142 return ext4_journal_get_write_access(handle, path->p_bh);
143 }
144 /* path points to leaf/index in inode body */
145 /* we use in-core data, no need to protect them */
146 return 0;
147}
148
149/*
150 * could return:
151 * - EROFS
152 * - ENOMEM
153 * - EIO
154 */
Eric Biggers43f81672019-12-31 12:04:40 -0600155static int __ext4_ext_dirty(const char *where, unsigned int line,
156 handle_t *handle, struct inode *inode,
157 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700158{
159 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400160
161 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700162 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400163 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700164 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400165 err = __ext4_handle_dirty_metadata(where, line, handle,
166 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700167 } else {
168 /* path points to leaf/index in inode body */
169 err = ext4_mark_inode_dirty(handle, inode);
170 }
171 return err;
172}
173
Eric Biggers43f81672019-12-31 12:04:40 -0600174#define ext4_ext_dirty(handle, inode, path) \
175 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
176
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700177static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700178 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500179 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700180{
Alex Tomasa86c6182006-10-11 01:21:03 -0700181 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400182 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700184
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500185 /*
186 * Try to predict block placement assuming that we are
187 * filling in a file which will eventually be
188 * non-sparse --- i.e., in the case of libbfd writing
189 * an ELF object sections out-of-order but in a way
190 * the eventually results in a contiguous object or
191 * executable file, or some database extending a table
192 * space file. However, this is actually somewhat
193 * non-ideal if we are writing a sparse file such as
194 * qemu or KVM writing a raw image file that is going
195 * to stay fairly sparse, since it will end up
196 * fragmenting the file system's free space. Maybe we
197 * should have some hueristics or some way to allow
198 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800199 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500200 * common.
201 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800202 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500203 if (ex) {
204 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
205 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
206
207 if (block > ext_block)
208 return ext_pblk + (block - ext_block);
209 else
210 return ext_pblk - (ext_block - block);
211 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700212
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700213 /* it looks like index is empty;
214 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700215 if (path[depth].p_bh)
216 return path[depth].p_bh->b_blocknr;
217 }
218
219 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400220 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700221}
222
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400223/*
224 * Allocation for a meta data block
225 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700226static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400227ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700228 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400229 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700230{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700231 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700232
233 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400234 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
235 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700236 return newblock;
237}
238
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700240{
241 int size;
242
243 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
244 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100245#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400246 if (!check && size > 6)
247 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700248#endif
249 return size;
250}
251
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400252static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700253{
254 int size;
255
256 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
257 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100258#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400259 if (!check && size > 5)
260 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700261#endif
262 return size;
263}
264
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700266{
267 int size;
268
269 size = sizeof(EXT4_I(inode)->i_data);
270 size -= sizeof(struct ext4_extent_header);
271 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100272#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400273 if (!check && size > 3)
274 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700275#endif
276 return size;
277}
278
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400279static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700280{
281 int size;
282
283 size = sizeof(EXT4_I(inode)->i_data);
284 size -= sizeof(struct ext4_extent_header);
285 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100286#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400287 if (!check && size > 4)
288 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700289#endif
290 return size;
291}
292
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400293static inline int
294ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400295 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400296 int nofail)
297{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400298 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400299 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
300
Theodore Ts'odfe50802014-09-01 14:37:09 -0400301 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400302 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
303 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
304 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
305}
306
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400307static int
308ext4_ext_max_entries(struct inode *inode, int depth)
309{
310 int max;
311
312 if (depth == ext_depth(inode)) {
313 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400314 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400315 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400316 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400317 } else {
318 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400319 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400320 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400321 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400322 }
323
324 return max;
325}
326
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400327static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
328{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400329 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400330 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500331 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400332
Vegard Nossumf70749c2016-06-30 11:53:46 -0400333 /*
334 * We allow neither:
335 * - zero length
336 * - overflow/wrap-around
337 */
338 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400339 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400340 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400341}
342
343static int ext4_valid_extent_idx(struct inode *inode,
344 struct ext4_extent_idx *ext_idx)
345{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400346 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400347
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400348 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400349}
350
351static int ext4_valid_extent_entries(struct inode *inode,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400352 struct ext4_extent_header *eh,
353 ext4_fsblk_t *pblk, int depth)
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400354{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400355 unsigned short entries;
356 if (eh->eh_entries == 0)
357 return 1;
358
359 entries = le16_to_cpu(eh->eh_entries);
360
361 if (depth == 0) {
362 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400363 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500364 ext4_lblk_t lblock = 0;
365 ext4_lblk_t prev = 0;
366 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400367 while (entries) {
368 if (!ext4_valid_extent(inode, ext))
369 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500370
371 /* Check for overlapping extents */
372 lblock = le32_to_cpu(ext->ee_block);
373 len = ext4_ext_get_actual_len(ext);
374 if ((lblock <= prev) && prev) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400375 *pblk = ext4_ext_pblock(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500376 return 0;
377 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400378 ext++;
379 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500380 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400381 }
382 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400383 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400384 while (entries) {
385 if (!ext4_valid_extent_idx(inode, ext_idx))
386 return 0;
387 ext_idx++;
388 entries--;
389 }
390 }
391 return 1;
392}
393
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400394static int __ext4_ext_check(const char *function, unsigned int line,
395 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400396 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400397{
398 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400399 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400400
401 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
402 error_msg = "invalid magic";
403 goto corrupted;
404 }
405 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
406 error_msg = "unexpected eh_depth";
407 goto corrupted;
408 }
409 if (unlikely(eh->eh_max == 0)) {
410 error_msg = "invalid eh_max";
411 goto corrupted;
412 }
413 max = ext4_ext_max_entries(inode, depth);
414 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
415 error_msg = "too large eh_max";
416 goto corrupted;
417 }
418 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
419 error_msg = "invalid eh_entries";
420 goto corrupted;
421 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400422 if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) {
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400423 error_msg = "invalid extent entries";
424 goto corrupted;
425 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400426 if (unlikely(depth > 32)) {
427 error_msg = "too large eh_depth";
428 goto corrupted;
429 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400430 /* Verify checksum on non-root extent tree nodes */
431 if (ext_depth(inode) != depth &&
432 !ext4_extent_block_csum_verify(inode, eh)) {
433 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400434 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400435 goto corrupted;
436 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400437 return 0;
438
439corrupted:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400440 ext4_error_inode_err(inode, function, line, 0, -err,
441 "pblk %llu bad header/extent: %s - magic %x, "
442 "entries %u, max %u(%u), depth %u(%u)",
443 (unsigned long long) pblk, error_msg,
444 le16_to_cpu(eh->eh_magic),
445 le16_to_cpu(eh->eh_entries),
446 le16_to_cpu(eh->eh_max),
447 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400448 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400449}
450
Theodore Ts'oc3491792013-08-16 21:21:41 -0400451#define ext4_ext_check(inode, eh, depth, pblk) \
452 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400453
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400454int ext4_ext_check_inode(struct inode *inode)
455{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400456 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400457}
458
Dmitry Monakhov40686642019-11-06 12:25:02 +0000459static void ext4_cache_extents(struct inode *inode,
460 struct ext4_extent_header *eh)
461{
462 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
463 ext4_lblk_t prev = 0;
464 int i;
465
466 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
467 unsigned int status = EXTENT_STATUS_WRITTEN;
468 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
469 int len = ext4_ext_get_actual_len(ex);
470
471 if (prev && (prev != lblk))
472 ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
473 EXTENT_STATUS_HOLE);
474
475 if (ext4_ext_is_unwritten(ex))
476 status = EXTENT_STATUS_UNWRITTEN;
477 ext4_es_cache_extent(inode, lblk, len,
478 ext4_ext_pblock(ex), status);
479 prev = lblk + len;
480 }
481}
482
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400483static struct buffer_head *
484__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400485 struct inode *inode, ext4_fsblk_t pblk, int depth,
486 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400487{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400488 struct buffer_head *bh;
489 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400490
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400491 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400492 if (unlikely(!bh))
493 return ERR_PTR(-ENOMEM);
494
495 if (!bh_uptodate_or_lock(bh)) {
496 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
497 err = bh_submit_read(bh);
498 if (err < 0)
499 goto errout;
500 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400501 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400502 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400503 if (!ext4_has_feature_journal(inode->i_sb) ||
504 (inode->i_ino !=
505 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
506 err = __ext4_ext_check(function, line, inode,
507 ext_block_hdr(bh), depth, pblk);
508 if (err)
509 goto errout;
510 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400511 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400512 /*
513 * If this is a leaf block, cache all of its entries
514 */
515 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
516 struct ext4_extent_header *eh = ext_block_hdr(bh);
Dmitry Monakhov40686642019-11-06 12:25:02 +0000517 ext4_cache_extents(inode, eh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400518 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400519 return bh;
520errout:
521 put_bh(bh);
522 return ERR_PTR(err);
523
Darrick J. Wongf8489122012-04-29 18:21:10 -0400524}
525
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400526#define read_extent_tree_block(inode, pblk, depth, flags) \
527 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
528 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400529
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400530/*
531 * This function is called to cache a file's extent information in the
532 * extent status tree
533 */
534int ext4_ext_precache(struct inode *inode)
535{
536 struct ext4_inode_info *ei = EXT4_I(inode);
537 struct ext4_ext_path *path = NULL;
538 struct buffer_head *bh;
539 int i = 0, depth, ret = 0;
540
541 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
542 return 0; /* not an extent-mapped inode */
543
544 down_read(&ei->i_data_sem);
545 depth = ext_depth(inode);
546
Ritesh Harjani2f424a52020-02-28 14:56:55 +0530547 /* Don't cache anything if there are no external extent blocks */
548 if (!depth) {
549 up_read(&ei->i_data_sem);
550 return ret;
551 }
552
Kees Cook6396bb22018-06-12 14:03:40 -0700553 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400554 GFP_NOFS);
555 if (path == NULL) {
556 up_read(&ei->i_data_sem);
557 return -ENOMEM;
558 }
559
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400560 path[0].p_hdr = ext_inode_hdr(inode);
561 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
562 if (ret)
563 goto out;
564 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
565 while (i >= 0) {
566 /*
567 * If this is a leaf block or we've reached the end of
568 * the index block, go up
569 */
570 if ((i == depth) ||
571 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
572 brelse(path[i].p_bh);
573 path[i].p_bh = NULL;
574 i--;
575 continue;
576 }
577 bh = read_extent_tree_block(inode,
578 ext4_idx_pblock(path[i].p_idx++),
579 depth - i - 1,
580 EXT4_EX_FORCE_CACHE);
581 if (IS_ERR(bh)) {
582 ret = PTR_ERR(bh);
583 break;
584 }
585 i++;
586 path[i].p_bh = bh;
587 path[i].p_hdr = ext_block_hdr(bh);
588 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
589 }
590 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
591out:
592 up_read(&ei->i_data_sem);
593 ext4_ext_drop_refs(path);
594 kfree(path);
595 return ret;
596}
597
Alex Tomasa86c6182006-10-11 01:21:03 -0700598#ifdef EXT_DEBUG
599static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
600{
601 int k, l = path->p_depth;
602
603 ext_debug("path:");
604 for (k = 0; k <= l; k++, path++) {
605 if (path->p_idx) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600606 ext_debug(" %d->%llu",
607 le32_to_cpu(path->p_idx->ei_block),
608 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700609 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400610 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700611 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400612 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400613 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400614 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700615 } else
616 ext_debug(" []");
617 }
618 ext_debug("\n");
619}
620
621static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
622{
623 int depth = ext_depth(inode);
624 struct ext4_extent_header *eh;
625 struct ext4_extent *ex;
626 int i;
627
628 if (!path)
629 return;
630
631 eh = path[depth].p_hdr;
632 ex = EXT_FIRST_EXTENT(eh);
633
Mingming553f9002009-09-18 13:34:55 -0400634 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
635
Alex Tomasa86c6182006-10-11 01:21:03 -0700636 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400637 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400638 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400639 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700640 }
641 ext_debug("\n");
642}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400643
644static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
645 ext4_fsblk_t newblock, int level)
646{
647 int depth = ext_depth(inode);
648 struct ext4_extent *ex;
649
650 if (depth != level) {
651 struct ext4_extent_idx *idx;
652 idx = path[level].p_idx;
653 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
654 ext_debug("%d: move %d:%llu in new index %llu\n", level,
655 le32_to_cpu(idx->ei_block),
656 ext4_idx_pblock(idx),
657 newblock);
658 idx++;
659 }
660
661 return;
662 }
663
664 ex = path[depth].p_ext;
665 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
666 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
667 le32_to_cpu(ex->ee_block),
668 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400669 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400670 ext4_ext_get_actual_len(ex),
671 newblock);
672 ex++;
673 }
674}
675
Alex Tomasa86c6182006-10-11 01:21:03 -0700676#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400677#define ext4_ext_show_path(inode, path)
678#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400679#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700680#endif
681
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500682void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700683{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400684 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700685
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400686 if (!path)
687 return;
688 depth = path->p_depth;
Eric Biggersde745482019-12-31 12:04:44 -0600689 for (i = 0; i <= depth; i++, path++) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700690 if (path->p_bh) {
691 brelse(path->p_bh);
692 path->p_bh = NULL;
693 }
Eric Biggersde745482019-12-31 12:04:44 -0600694 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700695}
696
697/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700698 * ext4_ext_binsearch_idx:
699 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400700 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700701 */
702static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500703ext4_ext_binsearch_idx(struct inode *inode,
704 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700705{
706 struct ext4_extent_header *eh = path->p_hdr;
707 struct ext4_extent_idx *r, *l, *m;
708
Alex Tomasa86c6182006-10-11 01:21:03 -0700709
Eric Sandeenbba90742008-01-28 23:58:27 -0500710 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700711
712 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400713 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700714 while (l <= r) {
715 m = l + (r - l) / 2;
716 if (block < le32_to_cpu(m->ei_block))
717 r = m - 1;
718 else
719 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400720 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
721 m, le32_to_cpu(m->ei_block),
722 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700723 }
724
725 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400726 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400727 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700728
729#ifdef CHECK_BINSEARCH
730 {
731 struct ext4_extent_idx *chix, *ix;
732 int k;
733
734 chix = ix = EXT_FIRST_INDEX(eh);
735 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -0600736 if (k != 0 && le32_to_cpu(ix->ei_block) <=
737 le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400738 printk(KERN_DEBUG "k=%d, ix=0x%p, "
739 "first=0x%p\n", k,
740 ix, EXT_FIRST_INDEX(eh));
741 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700742 le32_to_cpu(ix->ei_block),
743 le32_to_cpu(ix[-1].ei_block));
744 }
745 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400746 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700747 if (block < le32_to_cpu(ix->ei_block))
748 break;
749 chix = ix;
750 }
751 BUG_ON(chix != path->p_idx);
752 }
753#endif
754
755}
756
757/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700758 * ext4_ext_binsearch:
759 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400760 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700761 */
762static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500763ext4_ext_binsearch(struct inode *inode,
764 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700765{
766 struct ext4_extent_header *eh = path->p_hdr;
767 struct ext4_extent *r, *l, *m;
768
Alex Tomasa86c6182006-10-11 01:21:03 -0700769 if (eh->eh_entries == 0) {
770 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700771 * this leaf is empty:
772 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700773 */
774 return;
775 }
776
Eric Sandeenbba90742008-01-28 23:58:27 -0500777 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700778
779 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400780 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700781
782 while (l <= r) {
783 m = l + (r - l) / 2;
784 if (block < le32_to_cpu(m->ee_block))
785 r = m - 1;
786 else
787 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400788 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
789 m, le32_to_cpu(m->ee_block),
790 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700791 }
792
793 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400794 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400795 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400796 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400797 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400798 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700799
800#ifdef CHECK_BINSEARCH
801 {
802 struct ext4_extent *chex, *ex;
803 int k;
804
805 chex = ex = EXT_FIRST_EXTENT(eh);
806 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
807 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400808 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700809 if (block < le32_to_cpu(ex->ee_block))
810 break;
811 chex = ex;
812 }
813 BUG_ON(chex != path->p_ext);
814 }
815#endif
816
817}
818
819int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
820{
821 struct ext4_extent_header *eh;
822
823 eh = ext_inode_hdr(inode);
824 eh->eh_depth = 0;
825 eh->eh_entries = 0;
826 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400827 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700828 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700829 return 0;
830}
831
832struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400833ext4_find_extent(struct inode *inode, ext4_lblk_t block,
834 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700835{
836 struct ext4_extent_header *eh;
837 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400838 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
839 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500840 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700841
842 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400843 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400844 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
845 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
846 depth);
847 ret = -EFSCORRUPTED;
848 goto err;
849 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700850
Theodore Ts'o10809df82014-09-01 14:40:09 -0400851 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400852 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400853 if (depth > path[0].p_maxdepth) {
854 kfree(path);
855 *orig_path = path = NULL;
856 }
857 }
858 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400859 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700860 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400862 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700863 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400864 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700866 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400867 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700868
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400869 i = depth;
Dmitry Monakhov40686642019-11-06 12:25:02 +0000870 if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
871 ext4_cache_extents(inode, eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 /* walk through the tree */
873 while (i) {
874 ext_debug("depth %d: num %d, max %d\n",
875 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400876
Alex Tomasa86c6182006-10-11 01:21:03 -0700877 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400878 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700879 path[ppos].p_depth = i;
880 path[ppos].p_ext = NULL;
881
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400882 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
883 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530884 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400885 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700886 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500887 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400888
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 eh = ext_block_hdr(bh);
890 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 path[ppos].p_bh = bh;
892 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700893 }
894
895 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700896 path[ppos].p_ext = NULL;
897 path[ppos].p_idx = NULL;
898
Alex Tomasa86c6182006-10-11 01:21:03 -0700899 /* find extent */
900 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400901 /* if not an empty leaf */
902 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400903 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700904
905 ext4_ext_show_path(inode, path);
906
907 return path;
908
909err:
910 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400911 kfree(path);
912 if (orig_path)
913 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500914 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700915}
916
917/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700918 * ext4_ext_insert_index:
919 * insert new index [@logical;@ptr] into the block at @curp;
920 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700921 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400922static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
923 struct ext4_ext_path *curp,
924 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700925{
926 struct ext4_extent_idx *ix;
927 int len, err;
928
Avantika Mathur7e028972006-12-06 20:41:33 -0800929 err = ext4_ext_get_access(handle, inode, curp);
930 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700931 return err;
932
Frank Mayhar273df552010-03-02 11:46:09 -0500933 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
934 EXT4_ERROR_INODE(inode,
935 "logical %d == ei_block %d!",
936 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400937 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500938 }
Robin Dongd4620312011-07-17 23:43:42 -0400939
940 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
941 >= le16_to_cpu(curp->p_hdr->eh_max))) {
942 EXT4_ERROR_INODE(inode,
943 "eh_entries %d >= eh_max %d!",
944 le16_to_cpu(curp->p_hdr->eh_entries),
945 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400946 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400947 }
948
Alex Tomasa86c6182006-10-11 01:21:03 -0700949 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
950 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400951 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700952 ix = curp->p_idx + 1;
953 } else {
954 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400955 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700956 ix = curp->p_idx;
957 }
958
Eric Gouriou80e675f2011-10-27 11:52:18 -0400959 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
960 BUG_ON(len < 0);
961 if (len > 0) {
962 ext_debug("insert new index %d: "
963 "move %d indices from 0x%p to 0x%p\n",
964 logical, len, ix, ix + 1);
965 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
966 }
967
Tao Maf472e022011-10-17 10:13:46 -0400968 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
969 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400970 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -0400971 }
972
Alex Tomasa86c6182006-10-11 01:21:03 -0700973 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700974 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400975 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700976
Frank Mayhar273df552010-03-02 11:46:09 -0500977 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
978 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400979 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500980 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700981
982 err = ext4_ext_dirty(handle, inode, curp);
983 ext4_std_error(inode->i_sb, err);
984
985 return err;
986}
987
988/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700989 * ext4_ext_split:
990 * inserts new subtree into the path, using free index entry
991 * at depth @at:
992 * - allocates all needed blocks (new leaf and all intermediate index blocks)
993 * - makes decision where to split
994 * - moves remaining extents and index entries (right to the split point)
995 * into the newly allocated blocks
996 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700997 */
998static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400999 unsigned int flags,
1000 struct ext4_ext_path *path,
1001 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001002{
1003 struct buffer_head *bh = NULL;
1004 int depth = ext_depth(inode);
1005 struct ext4_extent_header *neh;
1006 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001007 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001008 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001009 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001010 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001011 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001012 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001013
1014 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001015 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001016
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001017 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001018 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001019 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1020 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001021 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001022 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001023 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1024 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001025 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001026 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001027 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001028 } else {
1029 border = newext->ee_block;
1030 ext_debug("leaf will be added."
1031 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001032 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001033 }
1034
1035 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001036 * If error occurs, then we break processing
1037 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001038 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001039 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 */
1041
1042 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001043 * Get array to track all allocated blocks.
1044 * We need this to handle errors and free blocks
1045 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001046 */
Kees Cook6396bb22018-06-12 14:03:40 -07001047 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 if (!ablocks)
1049 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001050
1051 /* allocate all needed blocks */
1052 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1053 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001054 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001055 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 if (newblock == 0)
1057 goto cleanup;
1058 ablocks[a] = newblock;
1059 }
1060
1061 /* initialize new leaf */
1062 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001063 if (unlikely(newblock == 0)) {
1064 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001065 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001066 goto cleanup;
1067 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001068 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001069 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001070 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001071 goto cleanup;
1072 }
1073 lock_buffer(bh);
1074
Avantika Mathur7e028972006-12-06 20:41:33 -08001075 err = ext4_journal_get_create_access(handle, bh);
1076 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001077 goto cleanup;
1078
1079 neh = ext_block_hdr(bh);
1080 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001081 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 neh->eh_magic = EXT4_EXT_MAGIC;
1083 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001084
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001085 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001086 if (unlikely(path[depth].p_hdr->eh_entries !=
1087 path[depth].p_hdr->eh_max)) {
1088 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1089 path[depth].p_hdr->eh_entries,
1090 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001091 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001092 goto cleanup;
1093 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001095 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1096 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001097 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001098 struct ext4_extent *ex;
1099 ex = EXT_FIRST_EXTENT(neh);
1100 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001101 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001102 }
1103
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001104 /* zero out unused area in the extent block */
1105 ext_size = sizeof(struct ext4_extent_header) +
1106 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1107 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001108 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001109 set_buffer_uptodate(bh);
1110 unlock_buffer(bh);
1111
Frank Mayhar03901312009-01-07 00:06:22 -05001112 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001113 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001114 goto cleanup;
1115 brelse(bh);
1116 bh = NULL;
1117
1118 /* correct old leaf */
1119 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001120 err = ext4_ext_get_access(handle, inode, path + depth);
1121 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001122 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001123 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001124 err = ext4_ext_dirty(handle, inode, path + depth);
1125 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001126 goto cleanup;
1127
1128 }
1129
1130 /* create intermediate indexes */
1131 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001132 if (unlikely(k < 0)) {
1133 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001134 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001135 goto cleanup;
1136 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001137 if (k)
1138 ext_debug("create %d intermediate indices\n", k);
1139 /* insert new index into current index block */
1140 /* current depth stored in i var */
1141 i = depth - 1;
1142 while (k--) {
1143 oldblock = newblock;
1144 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001145 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001146 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001147 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001148 goto cleanup;
1149 }
1150 lock_buffer(bh);
1151
Avantika Mathur7e028972006-12-06 20:41:33 -08001152 err = ext4_journal_get_create_access(handle, bh);
1153 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001154 goto cleanup;
1155
1156 neh = ext_block_hdr(bh);
1157 neh->eh_entries = cpu_to_le16(1);
1158 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001159 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001160 neh->eh_depth = cpu_to_le16(depth - i);
1161 fidx = EXT_FIRST_INDEX(neh);
1162 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001163 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001164
Eric Sandeenbba90742008-01-28 23:58:27 -05001165 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1166 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001167
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001168 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001169 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1170 EXT_LAST_INDEX(path[i].p_hdr))) {
1171 EXT4_ERROR_INODE(inode,
1172 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1173 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001174 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001175 goto cleanup;
1176 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001177 /* start copy indexes */
1178 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1179 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1180 EXT_MAX_INDEX(path[i].p_hdr));
1181 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001182 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001183 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001184 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001185 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001186 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001187 /* zero out unused area in the extent block */
1188 ext_size = sizeof(struct ext4_extent_header) +
1189 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1190 memset(bh->b_data + ext_size, 0,
1191 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001192 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001193 set_buffer_uptodate(bh);
1194 unlock_buffer(bh);
1195
Frank Mayhar03901312009-01-07 00:06:22 -05001196 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001197 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001198 goto cleanup;
1199 brelse(bh);
1200 bh = NULL;
1201
1202 /* correct old index */
1203 if (m) {
1204 err = ext4_ext_get_access(handle, inode, path + i);
1205 if (err)
1206 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001207 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001208 err = ext4_ext_dirty(handle, inode, path + i);
1209 if (err)
1210 goto cleanup;
1211 }
1212
1213 i--;
1214 }
1215
1216 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001217 err = ext4_ext_insert_index(handle, inode, path + at,
1218 le32_to_cpu(border), newblock);
1219
1220cleanup:
1221 if (bh) {
1222 if (buffer_locked(bh))
1223 unlock_buffer(bh);
1224 brelse(bh);
1225 }
1226
1227 if (err) {
1228 /* free all allocated blocks in error case */
1229 for (i = 0; i < depth; i++) {
1230 if (!ablocks[i])
1231 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001232 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001233 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001234 }
1235 }
1236 kfree(ablocks);
1237
1238 return err;
1239}
1240
1241/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001242 * ext4_ext_grow_indepth:
1243 * implements tree growing procedure:
1244 * - allocates new block
1245 * - moves top-level data (index block or leaf) into the new block
1246 * - initializes new top-level, creating index that points to the
1247 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001248 */
1249static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001250 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001251{
Alex Tomasa86c6182006-10-11 01:21:03 -07001252 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001253 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001254 ext4_fsblk_t newblock, goal = 0;
1255 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001256 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001257 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001258
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001259 /* Try to prepend new index to old one */
1260 if (ext_depth(inode))
1261 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1262 if (goal > le32_to_cpu(es->s_first_data_block)) {
1263 flags |= EXT4_MB_HINT_TRY_GOAL;
1264 goal--;
1265 } else
1266 goal = ext4_inode_to_goal_block(inode);
1267 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1268 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001269 if (newblock == 0)
1270 return err;
1271
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001272 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001273 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001274 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001275 lock_buffer(bh);
1276
Avantika Mathur7e028972006-12-06 20:41:33 -08001277 err = ext4_journal_get_create_access(handle, bh);
1278 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001279 unlock_buffer(bh);
1280 goto out;
1281 }
1282
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001283 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001284 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001285 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1286 /* zero out unused area in the extent block */
1287 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001288
1289 /* set size of new block */
1290 neh = ext_block_hdr(bh);
1291 /* old root could have indexes or leaves
1292 * so calculate e_max right way */
1293 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001294 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001295 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001296 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001297 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001298 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001299 set_buffer_uptodate(bh);
1300 unlock_buffer(bh);
1301
Frank Mayhar03901312009-01-07 00:06:22 -05001302 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001303 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001304 goto out;
1305
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001306 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001307 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001308 neh->eh_entries = cpu_to_le16(1);
1309 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1310 if (neh->eh_depth == 0) {
1311 /* Root extent block becomes index block */
1312 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1313 EXT_FIRST_INDEX(neh)->ei_block =
1314 EXT_FIRST_EXTENT(neh)->ee_block;
1315 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001316 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001317 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001318 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001319 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001320
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001321 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001322 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001323out:
1324 brelse(bh);
1325
1326 return err;
1327}
1328
1329/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001330 * ext4_ext_create_new_leaf:
1331 * finds empty index and adds new leaf.
1332 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001333 */
1334static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001335 unsigned int mb_flags,
1336 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001337 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001338 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001339{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001340 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001341 struct ext4_ext_path *curp;
1342 int depth, i, err = 0;
1343
1344repeat:
1345 i = depth = ext_depth(inode);
1346
1347 /* walk up to the tree and look for free index entry */
1348 curp = path + depth;
1349 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1350 i--;
1351 curp--;
1352 }
1353
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001354 /* we use already allocated block for index block,
1355 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001356 if (EXT_HAS_FREE_INDEX(curp)) {
1357 /* if we found index with free entry, then use that
1358 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001359 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001360 if (err)
1361 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001362
1363 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001364 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001365 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001366 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001367 if (IS_ERR(path))
1368 err = PTR_ERR(path);
1369 } else {
1370 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001371 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001372 if (err)
1373 goto out;
1374
1375 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001376 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001377 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001378 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001379 if (IS_ERR(path)) {
1380 err = PTR_ERR(path);
1381 goto out;
1382 }
1383
1384 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001385 * only first (depth 0 -> 1) produces free space;
1386 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001387 */
1388 depth = ext_depth(inode);
1389 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001390 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001391 goto repeat;
1392 }
1393 }
1394
1395out:
1396 return err;
1397}
1398
1399/*
Alex Tomas1988b512008-01-28 23:58:27 -05001400 * search the closest allocated block to the left for *logical
1401 * and returns it at @logical + it's physical address at @phys
1402 * if *logical is the smallest allocated block, the function
1403 * returns 0 at @phys
1404 * return value contains 0 (success) or error code
1405 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001406static int ext4_ext_search_left(struct inode *inode,
1407 struct ext4_ext_path *path,
1408 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001409{
1410 struct ext4_extent_idx *ix;
1411 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001412 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001413
Frank Mayhar273df552010-03-02 11:46:09 -05001414 if (unlikely(path == NULL)) {
1415 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001416 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001417 }
Alex Tomas1988b512008-01-28 23:58:27 -05001418 depth = path->p_depth;
1419 *phys = 0;
1420
1421 if (depth == 0 && path->p_ext == NULL)
1422 return 0;
1423
1424 /* usually extent in the path covers blocks smaller
1425 * then *logical, but it can be that extent is the
1426 * first one in the file */
1427
1428 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001429 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001430 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001431 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1432 EXT4_ERROR_INODE(inode,
1433 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1434 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001435 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001436 }
Alex Tomas1988b512008-01-28 23:58:27 -05001437 while (--depth >= 0) {
1438 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001439 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1440 EXT4_ERROR_INODE(inode,
1441 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001442 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001443 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001444 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001445 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001446 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001447 }
Alex Tomas1988b512008-01-28 23:58:27 -05001448 }
1449 return 0;
1450 }
1451
Frank Mayhar273df552010-03-02 11:46:09 -05001452 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1453 EXT4_ERROR_INODE(inode,
1454 "logical %d < ee_block %d + ee_len %d!",
1455 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001456 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001457 }
Alex Tomas1988b512008-01-28 23:58:27 -05001458
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001459 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001460 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001461 return 0;
1462}
1463
1464/*
1465 * search the closest allocated block to the right for *logical
1466 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001467 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001468 * returns 0 at @phys
1469 * return value contains 0 (success) or error code
1470 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001471static int ext4_ext_search_right(struct inode *inode,
1472 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001473 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1474 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001475{
1476 struct buffer_head *bh = NULL;
1477 struct ext4_extent_header *eh;
1478 struct ext4_extent_idx *ix;
1479 struct ext4_extent *ex;
1480 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001481 int depth; /* Note, NOT eh_depth; depth from top of tree */
1482 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001483
Frank Mayhar273df552010-03-02 11:46:09 -05001484 if (unlikely(path == NULL)) {
1485 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001486 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001487 }
Alex Tomas1988b512008-01-28 23:58:27 -05001488 depth = path->p_depth;
1489 *phys = 0;
1490
1491 if (depth == 0 && path->p_ext == NULL)
1492 return 0;
1493
1494 /* usually extent in the path covers blocks smaller
1495 * then *logical, but it can be that extent is the
1496 * first one in the file */
1497
1498 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001499 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001500 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001501 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1502 EXT4_ERROR_INODE(inode,
1503 "first_extent(path[%d].p_hdr) != ex",
1504 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001505 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001506 }
Alex Tomas1988b512008-01-28 23:58:27 -05001507 while (--depth >= 0) {
1508 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001509 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1510 EXT4_ERROR_INODE(inode,
1511 "ix != EXT_FIRST_INDEX *logical %d!",
1512 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001513 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001514 }
Alex Tomas1988b512008-01-28 23:58:27 -05001515 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001516 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001517 }
1518
Frank Mayhar273df552010-03-02 11:46:09 -05001519 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1520 EXT4_ERROR_INODE(inode,
1521 "logical %d < ee_block %d + ee_len %d!",
1522 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001523 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001524 }
Alex Tomas1988b512008-01-28 23:58:27 -05001525
1526 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1527 /* next allocated block in this leaf */
1528 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001529 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001530 }
1531
1532 /* go up and search for index to the right */
1533 while (--depth >= 0) {
1534 ix = path[depth].p_idx;
1535 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001536 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001537 }
1538
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001539 /* we've gone up to the root and found no index to the right */
1540 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001541
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001542got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001543 /* we've found index to the right, let's
1544 * follow it and find the closest allocated
1545 * block to the right */
1546 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001547 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001548 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001549 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001550 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001551 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001552 if (IS_ERR(bh))
1553 return PTR_ERR(bh);
1554 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001555 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001556 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001557 put_bh(bh);
1558 }
1559
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001560 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001561 if (IS_ERR(bh))
1562 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001563 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001564 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001565found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001566 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001567 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001568 *ret_ex = ex;
1569 if (bh)
1570 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001571 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001572}
1573
1574/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001575 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001576 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001577 * NOTE: it considers block number from index entry as
1578 * allocated block. Thus, index entries have to be consistent
1579 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001580 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001581ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001582ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1583{
1584 int depth;
1585
1586 BUG_ON(path == NULL);
1587 depth = path->p_depth;
1588
1589 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001590 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001591
1592 while (depth >= 0) {
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001593 struct ext4_ext_path *p = &path[depth];
1594
Alex Tomasa86c6182006-10-11 01:21:03 -07001595 if (depth == path->p_depth) {
1596 /* leaf */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001597 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
1598 return le32_to_cpu(p->p_ext[1].ee_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001599 } else {
1600 /* index */
Eric Biggers6e89bbb2019-12-31 12:04:43 -06001601 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
1602 return le32_to_cpu(p->p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001603 }
1604 depth--;
1605 }
1606
Lukas Czernerf17722f2011-06-06 00:05:17 -04001607 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001608}
1609
1610/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001611 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001612 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001613 */
Robin Dong57187892011-07-23 21:49:07 -04001614static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001615{
1616 int depth;
1617
1618 BUG_ON(path == NULL);
1619 depth = path->p_depth;
1620
1621 /* zero-tree has no leaf blocks at all */
1622 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001623 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001624
1625 /* go to index block */
1626 depth--;
1627
1628 while (depth >= 0) {
1629 if (path[depth].p_idx !=
1630 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001631 return (ext4_lblk_t)
1632 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001633 depth--;
1634 }
1635
Lukas Czernerf17722f2011-06-06 00:05:17 -04001636 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001637}
1638
1639/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001640 * ext4_ext_correct_indexes:
1641 * if leaf gets modified and modified extent is first in the leaf,
1642 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001643 * TODO: do we need to correct tree in all cases?
1644 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001645static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001646 struct ext4_ext_path *path)
1647{
1648 struct ext4_extent_header *eh;
1649 int depth = ext_depth(inode);
1650 struct ext4_extent *ex;
1651 __le32 border;
1652 int k, err = 0;
1653
1654 eh = path[depth].p_hdr;
1655 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001656
1657 if (unlikely(ex == NULL || eh == NULL)) {
1658 EXT4_ERROR_INODE(inode,
1659 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001660 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001661 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001662
1663 if (depth == 0) {
1664 /* there is no tree at all */
1665 return 0;
1666 }
1667
1668 if (ex != EXT_FIRST_EXTENT(eh)) {
1669 /* we correct tree if first leaf got modified only */
1670 return 0;
1671 }
1672
1673 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001674 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001675 */
1676 k = depth - 1;
1677 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001678 err = ext4_ext_get_access(handle, inode, path + k);
1679 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001680 return err;
1681 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001682 err = ext4_ext_dirty(handle, inode, path + k);
1683 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001684 return err;
1685
1686 while (k--) {
1687 /* change all left-side indexes */
1688 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1689 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001690 err = ext4_ext_get_access(handle, inode, path + k);
1691 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001692 break;
1693 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001694 err = ext4_ext_dirty(handle, inode, path + k);
1695 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001696 break;
1697 }
1698
1699 return err;
1700}
1701
Eric Biggers43f81672019-12-31 12:04:40 -06001702static int ext4_can_extents_be_merged(struct inode *inode,
1703 struct ext4_extent *ex1,
1704 struct ext4_extent *ex2)
Alex Tomasa86c6182006-10-11 01:21:03 -07001705{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001706 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001707
Lukas Czerner556615d2014-04-20 23:45:47 -04001708 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001709 return 0;
1710
1711 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1712 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1713
1714 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001715 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001716 return 0;
1717
Eric Sandeenda0169b2013-11-04 09:58:26 -05001718 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001719 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001720
Lukas Czerner556615d2014-04-20 23:45:47 -04001721 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001722 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001723 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001724#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001725 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001726 return 0;
1727#endif
1728
Theodore Ts'obf89d162010-10-27 21:30:14 -04001729 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001730 return 1;
1731 return 0;
1732}
1733
1734/*
Amit Arora56055d32007-07-17 21:42:38 -04001735 * This function tries to merge the "ex" extent to the next extent in the tree.
1736 * It always tries to merge towards right. If you want to merge towards
1737 * left, pass "ex - 1" as argument instead of "ex".
1738 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1739 * 1 if they got merged.
1740 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001741static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001742 struct ext4_ext_path *path,
1743 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001744{
1745 struct ext4_extent_header *eh;
1746 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001747 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001748
1749 depth = ext_depth(inode);
1750 BUG_ON(path[depth].p_hdr == NULL);
1751 eh = path[depth].p_hdr;
1752
1753 while (ex < EXT_LAST_EXTENT(eh)) {
1754 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1755 break;
1756 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001757 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001758 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1759 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001760 if (unwritten)
1761 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001762
1763 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1764 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1765 * sizeof(struct ext4_extent);
1766 memmove(ex + 1, ex + 2, len);
1767 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001768 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001769 merge_done = 1;
1770 WARN_ON(eh->eh_entries == 0);
1771 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001772 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001773 }
1774
1775 return merge_done;
1776}
1777
1778/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001779 * This function does a very simple check to see if we can collapse
1780 * an extent tree with a single extent tree leaf block into the inode.
1781 */
1782static void ext4_ext_try_to_merge_up(handle_t *handle,
1783 struct inode *inode,
1784 struct ext4_ext_path *path)
1785{
1786 size_t s;
1787 unsigned max_root = ext4_ext_space_root(inode, 0);
1788 ext4_fsblk_t blk;
1789
1790 if ((path[0].p_depth != 1) ||
1791 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1792 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1793 return;
1794
1795 /*
1796 * We need to modify the block allocation bitmap and the block
1797 * group descriptor to release the extent tree block. If we
1798 * can't get the journal credits, give up.
1799 */
Jan Kara83448bd2019-11-05 17:44:29 +01001800 if (ext4_journal_extend(handle, 2,
1801 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001802 return;
1803
1804 /*
1805 * Copy the extent data up to the inode
1806 */
1807 blk = ext4_idx_pblock(path[0].p_idx);
1808 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1809 sizeof(struct ext4_extent_idx);
1810 s += sizeof(struct ext4_extent_header);
1811
Theodore Ts'o10809df82014-09-01 14:40:09 -04001812 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001813 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1814 path[0].p_depth = 0;
1815 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1816 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1817 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1818
1819 brelse(path[1].p_bh);
1820 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001821 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001822}
1823
1824/*
Eric Biggersadde81c2019-12-31 12:04:41 -06001825 * This function tries to merge the @ex extent to neighbours in the tree, then
1826 * tries to collapse the extent tree into the inode.
Yongqiang Yang197217a2011-05-03 11:45:29 -04001827 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001828static void ext4_ext_try_to_merge(handle_t *handle,
1829 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001830 struct ext4_ext_path *path,
Eric Biggersadde81c2019-12-31 12:04:41 -06001831 struct ext4_extent *ex)
1832{
Yongqiang Yang197217a2011-05-03 11:45:29 -04001833 struct ext4_extent_header *eh;
1834 unsigned int depth;
1835 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001836
1837 depth = ext_depth(inode);
1838 BUG_ON(path[depth].p_hdr == NULL);
1839 eh = path[depth].p_hdr;
1840
1841 if (ex > EXT_FIRST_EXTENT(eh))
1842 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1843
1844 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001845 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001846
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001847 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001848}
1849
1850/*
Amit Arora25d14f92007-05-24 13:04:13 -04001851 * check if a portion of the "newext" extent overlaps with an
1852 * existing extent.
1853 *
1854 * If there is an overlap discovered, it updates the length of the newext
1855 * such that there will be no overlap, and then returns 1.
1856 * If there is no overlap found, it returns 0.
1857 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001858static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1859 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001860 struct ext4_extent *newext,
1861 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001862{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001863 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001864 unsigned int depth, len1;
1865 unsigned int ret = 0;
1866
1867 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001868 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001869 depth = ext_depth(inode);
1870 if (!path[depth].p_ext)
1871 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001872 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001873
1874 /*
1875 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001876 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001877 */
1878 if (b2 < b1) {
1879 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001880 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001881 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001882 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001883 }
1884
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001885 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001886 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001887 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001888 newext->ee_len = cpu_to_le16(len1);
1889 ret = 1;
1890 }
1891
1892 /* check for overlap */
1893 if (b1 + len1 > b2) {
1894 newext->ee_len = cpu_to_le16(b2 - b1);
1895 ret = 1;
1896 }
1897out:
1898 return ret;
1899}
1900
1901/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001902 * ext4_ext_insert_extent:
1903 * tries to merge requsted extent into the existing extent or
1904 * inserts requested extent as new one into the tree,
1905 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001906 */
1907int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001908 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001909 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001910{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001911 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001912 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001913 struct ext4_extent *ex, *fex;
1914 struct ext4_extent *nearex; /* nearest extent */
1915 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001916 int depth, len, err;
1917 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001918 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001919
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001920 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1921 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001922 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1923 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001924 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001925 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001926 depth = ext_depth(inode);
1927 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001928 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001929 if (unlikely(path[depth].p_hdr == NULL)) {
1930 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001931 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001932 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001933
1934 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001935 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001936
1937 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001938 * Try to see whether we should rather test the extent on
1939 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001940 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001941 * left, or on the right from the searched position. This
1942 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001943 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001944 if (ex < EXT_LAST_EXTENT(eh) &&
1945 (le32_to_cpu(ex->ee_block) +
1946 ext4_ext_get_actual_len(ex) <
1947 le32_to_cpu(newext->ee_block))) {
1948 ex += 1;
1949 goto prepend;
1950 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1951 (le32_to_cpu(newext->ee_block) +
1952 ext4_ext_get_actual_len(newext) <
1953 le32_to_cpu(ex->ee_block)))
1954 ex -= 1;
1955
1956 /* Try to append newex to the ex */
1957 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1958 ext_debug("append [%d]%d block to %u:[%d]%d"
1959 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001960 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001961 ext4_ext_get_actual_len(newext),
1962 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001963 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001964 ext4_ext_get_actual_len(ex),
1965 ext4_ext_pblock(ex));
1966 err = ext4_ext_get_access(handle, inode,
1967 path + depth);
1968 if (err)
1969 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001970 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001971 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001972 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001973 if (unwritten)
1974 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001975 eh = path[depth].p_hdr;
1976 nearex = ex;
1977 goto merge;
1978 }
1979
1980prepend:
1981 /* Try to prepend newex to the ex */
1982 if (ext4_can_extents_be_merged(inode, newext, ex)) {
1983 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
1984 "(from %llu)\n",
1985 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001986 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001987 ext4_ext_get_actual_len(newext),
1988 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001989 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001990 ext4_ext_get_actual_len(ex),
1991 ext4_ext_pblock(ex));
1992 err = ext4_ext_get_access(handle, inode,
1993 path + depth);
1994 if (err)
1995 return err;
1996
Lukas Czerner556615d2014-04-20 23:45:47 -04001997 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001998 ex->ee_block = newext->ee_block;
1999 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2000 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2001 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002002 if (unwritten)
2003 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002004 eh = path[depth].p_hdr;
2005 nearex = ex;
2006 goto merge;
2007 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002008 }
2009
Alex Tomasa86c6182006-10-11 01:21:03 -07002010 depth = ext_depth(inode);
2011 eh = path[depth].p_hdr;
2012 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2013 goto has_space;
2014
2015 /* probably next leaf has space for us? */
2016 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002017 next = EXT_MAX_BLOCKS;
2018 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002019 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002020 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002021 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002022 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002023 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 if (IS_ERR(npath))
2025 return PTR_ERR(npath);
2026 BUG_ON(npath->p_depth != path->p_depth);
2027 eh = npath[depth].p_hdr;
2028 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002029 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002030 le16_to_cpu(eh->eh_entries));
2031 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002032 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002033 }
2034 ext_debug("next leaf has no free space(%d,%d)\n",
2035 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2036 }
2037
2038 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002039 * There is no free space in the found leaf.
2040 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002041 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002042 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002043 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002044 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002045 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002046 if (err)
2047 goto cleanup;
2048 depth = ext_depth(inode);
2049 eh = path[depth].p_hdr;
2050
2051has_space:
2052 nearex = path[depth].p_ext;
2053
Avantika Mathur7e028972006-12-06 20:41:33 -08002054 err = ext4_ext_get_access(handle, inode, path + depth);
2055 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002056 goto cleanup;
2057
2058 if (!nearex) {
2059 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002060 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002061 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002062 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002063 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002064 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002065 nearex = EXT_FIRST_EXTENT(eh);
2066 } else {
2067 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002068 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002069 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002070 ext_debug("insert %u:%llu:[%d]%d before: "
2071 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002072 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002073 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002074 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002075 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002076 nearex);
2077 nearex++;
2078 } else {
2079 /* Insert before */
2080 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002081 ext_debug("insert %u:%llu:[%d]%d after: "
2082 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002083 le32_to_cpu(newext->ee_block),
2084 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002085 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002086 ext4_ext_get_actual_len(newext),
2087 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002088 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002089 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2090 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002091 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002092 "move %d extents from 0x%p to 0x%p\n",
2093 le32_to_cpu(newext->ee_block),
2094 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002095 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002096 ext4_ext_get_actual_len(newext),
2097 len, nearex, nearex + 1);
2098 memmove(nearex + 1, nearex,
2099 len * sizeof(struct ext4_extent));
2100 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002101 }
2102
Marcin Slusarze8546d02008-04-17 10:38:59 -04002103 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002104 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002105 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002106 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002107 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002108
2109merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002110 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002111 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002112 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002113
Alex Tomasa86c6182006-10-11 01:21:03 -07002114
2115 /* time to correct all indexes above */
2116 err = ext4_ext_correct_indexes(handle, inode, path);
2117 if (err)
2118 goto cleanup;
2119
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002120 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002121
2122cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002123 ext4_ext_drop_refs(npath);
2124 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002125 return err;
2126}
2127
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002128static int ext4_fill_es_cache_info(struct inode *inode,
2129 ext4_lblk_t block, ext4_lblk_t num,
2130 struct fiemap_extent_info *fieinfo)
2131{
2132 ext4_lblk_t next, end = block + num - 1;
2133 struct extent_status es;
2134 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2135 unsigned int flags;
2136 int err;
2137
2138 while (block <= end) {
2139 next = 0;
2140 flags = 0;
2141 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2142 break;
2143 if (ext4_es_is_unwritten(&es))
2144 flags |= FIEMAP_EXTENT_UNWRITTEN;
2145 if (ext4_es_is_delayed(&es))
2146 flags |= (FIEMAP_EXTENT_DELALLOC |
2147 FIEMAP_EXTENT_UNKNOWN);
2148 if (ext4_es_is_hole(&es))
2149 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2150 if (next == 0)
2151 flags |= FIEMAP_EXTENT_LAST;
2152 if (flags & (FIEMAP_EXTENT_DELALLOC|
2153 EXT4_FIEMAP_EXTENT_HOLE))
2154 es.es_pblk = 0;
2155 else
2156 es.es_pblk = ext4_es_pblock(&es);
2157 err = fiemap_fill_next_extent(fieinfo,
2158 (__u64)es.es_lblk << blksize_bits,
2159 (__u64)es.es_pblk << blksize_bits,
2160 (__u64)es.es_len << blksize_bits,
2161 flags);
2162 if (next == 0)
2163 break;
2164 block = next;
2165 if (err < 0)
2166 return err;
2167 if (err == 1)
2168 return 0;
2169 }
2170 return 0;
2171}
2172
2173
Alex Tomasa86c6182006-10-11 01:21:03 -07002174/*
Jan Kara140a5252016-03-09 22:46:57 -05002175 * ext4_ext_determine_hole - determine hole around given block
2176 * @inode: inode we lookup in
2177 * @path: path in extent tree to @lblk
2178 * @lblk: pointer to logical block around which we want to determine hole
2179 *
2180 * Determine hole length (and start if easily possible) around given logical
2181 * block. We don't try too hard to find the beginning of the hole but @path
2182 * actually points to extent before @lblk, we provide it.
2183 *
2184 * The function returns the length of a hole starting at @lblk. We update @lblk
2185 * to the beginning of the hole if we managed to find it.
2186 */
2187static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2188 struct ext4_ext_path *path,
2189 ext4_lblk_t *lblk)
2190{
2191 int depth = ext_depth(inode);
2192 struct ext4_extent *ex;
2193 ext4_lblk_t len;
2194
2195 ex = path[depth].p_ext;
2196 if (ex == NULL) {
2197 /* there is no extent yet, so gap is [0;-] */
2198 *lblk = 0;
2199 len = EXT_MAX_BLOCKS;
2200 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2201 len = le32_to_cpu(ex->ee_block) - *lblk;
2202 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2203 + ext4_ext_get_actual_len(ex)) {
2204 ext4_lblk_t next;
2205
2206 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2207 next = ext4_ext_next_allocated_block(path);
2208 BUG_ON(next == *lblk);
2209 len = next - *lblk;
2210 } else {
2211 BUG();
2212 }
2213 return len;
2214}
2215
2216/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002217 * ext4_ext_put_gap_in_cache:
2218 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002219 * and cache this gap
2220 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002221static void
Jan Kara140a5252016-03-09 22:46:57 -05002222ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2223 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002224{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002225 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002226
Eric Whitneyad431022018-10-01 14:10:39 -04002227 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2228 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002229 if (es.es_len) {
2230 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002231 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002232 return;
Jan Kara140a5252016-03-09 22:46:57 -05002233 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002234 }
Jan Kara140a5252016-03-09 22:46:57 -05002235 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2236 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2237 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002238}
2239
2240/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002241 * ext4_ext_rm_idx:
2242 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002243 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002244static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002245 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002246{
Alex Tomasa86c6182006-10-11 01:21:03 -07002247 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002248 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002249
2250 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002251 depth--;
2252 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002253 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002254 if (unlikely(path->p_hdr->eh_entries == 0)) {
2255 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002256 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002257 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002258 err = ext4_ext_get_access(handle, inode, path);
2259 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002260 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002261
2262 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2263 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2264 len *= sizeof(struct ext4_extent_idx);
2265 memmove(path->p_idx, path->p_idx + 1, len);
2266 }
2267
Marcin Slusarze8546d02008-04-17 10:38:59 -04002268 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002269 err = ext4_ext_dirty(handle, inode, path);
2270 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002271 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002272 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002273 trace_ext4_ext_rm_idx(inode, leaf);
2274
Peter Huewe7dc57612011-02-21 21:01:42 -05002275 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002276 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002277
2278 while (--depth >= 0) {
2279 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2280 break;
2281 path--;
2282 err = ext4_ext_get_access(handle, inode, path);
2283 if (err)
2284 break;
2285 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2286 err = ext4_ext_dirty(handle, inode, path);
2287 if (err)
2288 break;
2289 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002290 return err;
2291}
2292
2293/*
Mingming Caoee12b632008-08-19 22:16:05 -04002294 * ext4_ext_calc_credits_for_single_extent:
2295 * This routine returns max. credits that needed to insert an extent
2296 * to the extent tree.
2297 * When pass the actual path, the caller should calculate credits
2298 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002299 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002300int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002301 struct ext4_ext_path *path)
2302{
Alex Tomasa86c6182006-10-11 01:21:03 -07002303 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002304 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002305 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002306
Alex Tomasa86c6182006-10-11 01:21:03 -07002307 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002308 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002309 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2310
2311 /*
2312 * There are some space in the leaf tree, no
2313 * need to account for leaf block credit
2314 *
2315 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002316 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002317 * accounted.
2318 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002319 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002320 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002321 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002322 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 }
2324
Mingming Cao525f4ed2008-08-19 22:15:58 -04002325 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002326}
Alex Tomasa86c6182006-10-11 01:21:03 -07002327
Mingming Caoee12b632008-08-19 22:16:05 -04002328/*
Jan Karafffb2732013-06-04 13:01:11 -04002329 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002330 *
Jan Karafffb2732013-06-04 13:01:11 -04002331 * If we add a single extent, then in the worse case, each tree level
2332 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002333 *
Jan Karafffb2732013-06-04 13:01:11 -04002334 * If more extents are inserted, they could cause the whole tree split more
2335 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002336 */
Jan Karafffb2732013-06-04 13:01:11 -04002337int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002338{
2339 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002340 int depth;
2341
2342 /* If we are converting the inline data, only one is needed here. */
2343 if (ext4_has_inline_data(inode))
2344 return 1;
2345
2346 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002347
Jan Karafffb2732013-06-04 13:01:11 -04002348 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002349 index = depth * 2;
2350 else
2351 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002352
Mingming Caoee12b632008-08-19 22:16:05 -04002353 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002354}
2355
Theodore Ts'o981250c2013-06-12 11:48:29 -04002356static inline int get_default_free_blocks_flags(struct inode *inode)
2357{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002358 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2359 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002360 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2361 else if (ext4_should_journal_data(inode))
2362 return EXT4_FREE_BLOCKS_FORGET;
2363 return 0;
2364}
2365
Eric Whitney9fe67142018-10-01 14:25:08 -04002366/*
2367 * ext4_rereserve_cluster - increment the reserved cluster count when
2368 * freeing a cluster with a pending reservation
2369 *
2370 * @inode - file containing the cluster
2371 * @lblk - logical block in cluster to be reserved
2372 *
2373 * Increments the reserved cluster count and adjusts quota in a bigalloc
2374 * file system when freeing a partial cluster containing at least one
2375 * delayed and unwritten block. A partial cluster meeting that
2376 * requirement will have a pending reservation. If so, the
2377 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2378 * defer reserved and allocated space accounting to a subsequent call
2379 * to this function.
2380 */
2381static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2382{
2383 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2384 struct ext4_inode_info *ei = EXT4_I(inode);
2385
2386 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2387
2388 spin_lock(&ei->i_block_reservation_lock);
2389 ei->i_reserved_data_blocks++;
2390 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2391 spin_unlock(&ei->i_block_reservation_lock);
2392
2393 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2394 ext4_remove_pending(inode, lblk);
2395}
2396
Alex Tomasa86c6182006-10-11 01:21:03 -07002397static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002398 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002399 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002400 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002401{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002402 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002403 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002404 ext4_fsblk_t last_pblk, pblk;
2405 ext4_lblk_t num;
2406 int flags;
2407
2408 /* only extent tail removal is allowed */
2409 if (from < le32_to_cpu(ex->ee_block) ||
2410 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2411 ext4_error(sbi->s_sb,
2412 "strange request: removal(2) %u-%u from %u:%u",
2413 from, to, le32_to_cpu(ex->ee_block), ee_len);
2414 return 0;
2415 }
2416
2417#ifdef EXTENTS_STATS
2418 spin_lock(&sbi->s_ext_stats_lock);
2419 sbi->s_ext_blocks += ee_len;
2420 sbi->s_ext_extents++;
2421 if (ee_len < sbi->s_ext_min)
2422 sbi->s_ext_min = ee_len;
2423 if (ee_len > sbi->s_ext_max)
2424 sbi->s_ext_max = ee_len;
2425 if (ext_depth(inode) > sbi->s_depth_max)
2426 sbi->s_depth_max = ext_depth(inode);
2427 spin_unlock(&sbi->s_ext_stats_lock);
2428#endif
2429
2430 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2431
2432 /*
2433 * if we have a partial cluster, and it's different from the
2434 * cluster of the last block in the extent, we free it
2435 */
2436 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2437
2438 if (partial->state != initial &&
2439 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2440 if (partial->state == tofree) {
2441 flags = get_default_free_blocks_flags(inode);
2442 if (ext4_is_pending(inode, partial->lblk))
2443 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2444 ext4_free_blocks(handle, inode, NULL,
2445 EXT4_C2B(sbi, partial->pclu),
2446 sbi->s_cluster_ratio, flags);
2447 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2448 ext4_rereserve_cluster(inode, partial->lblk);
2449 }
2450 partial->state = initial;
2451 }
2452
2453 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2454 pblk = ext4_ext_pblock(ex) + ee_len - num;
2455
2456 /*
2457 * We free the partial cluster at the end of the extent (if any),
2458 * unless the cluster is used by another extent (partial_cluster
2459 * state is nofree). If a partial cluster exists here, it must be
2460 * shared with the last block in the extent.
2461 */
2462 flags = get_default_free_blocks_flags(inode);
2463
2464 /* partial, left end cluster aligned, right end unaligned */
2465 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2466 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2467 (partial->state != nofree)) {
2468 if (ext4_is_pending(inode, to))
2469 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2470 ext4_free_blocks(handle, inode, NULL,
2471 EXT4_PBLK_CMASK(sbi, last_pblk),
2472 sbi->s_cluster_ratio, flags);
2473 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2474 ext4_rereserve_cluster(inode, to);
2475 partial->state = initial;
2476 flags = get_default_free_blocks_flags(inode);
2477 }
2478
2479 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002480
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002481 /*
2482 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002483 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002484 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002485 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002486 */
2487 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002488 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002489
Eric Whitney9fe67142018-10-01 14:25:08 -04002490 /* reset the partial cluster if we've freed past it */
2491 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2492 partial->state = initial;
2493
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002494 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002495 * If we've freed the entire extent but the beginning is not left
2496 * cluster aligned and is not marked as ineligible for freeing we
2497 * record the partial cluster at the beginning of the extent. It
2498 * wasn't freed by the preceding ext4_free_blocks() call, and we
2499 * need to look farther to the left to determine if it's to be freed
2500 * (not shared with another extent). Else, reset the partial
2501 * cluster - we're either done freeing or the beginning of the
2502 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002503 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002504 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2505 if (partial->state == initial) {
2506 partial->pclu = EXT4_B2C(sbi, pblk);
2507 partial->lblk = from;
2508 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002509 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002510 } else {
2511 partial->state = initial;
2512 }
2513
Alex Tomasa86c6182006-10-11 01:21:03 -07002514 return 0;
2515}
2516
Allison Hendersond583fb82011-05-25 07:41:43 -04002517/*
2518 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002519 * blocks appearing between "start" and "end". Both "start"
2520 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002521 *
2522 * @handle: The journal handle
2523 * @inode: The files inode
2524 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002525 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002526 * has been released from it. However, if this value is
2527 * negative, it's a cluster just to the right of the
2528 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002529 * @start: The first block to remove
2530 * @end: The last block to remove
2531 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002532static int
2533ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002534 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002535 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002536 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002537{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002538 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002539 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002540 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002541 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002542 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002543 unsigned num;
2544 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002545 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002546 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002547 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002548 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002549
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002550 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002551 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002552 if (!path[depth].p_hdr)
2553 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2554 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002555 if (unlikely(path[depth].p_hdr == NULL)) {
2556 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002557 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002558 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002559 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002560 ex = path[depth].p_ext;
2561 if (!ex)
2562 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002563
2564 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002565 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002566
Eric Whitney9fe67142018-10-01 14:25:08 -04002567 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002568
Alex Tomasa86c6182006-10-11 01:21:03 -07002569 while (ex >= EXT_FIRST_EXTENT(eh) &&
2570 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002571
Lukas Czerner556615d2014-04-20 23:45:47 -04002572 if (ext4_ext_is_unwritten(ex))
2573 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002574 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002575 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002576
Mingming553f9002009-09-18 13:34:55 -04002577 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002578 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002579 path[depth].p_ext = ex;
2580
2581 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002582 b = ex_ee_block+ex_ee_len - 1 < end ?
2583 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002584
2585 ext_debug(" border %u:%u\n", a, b);
2586
Allison Hendersond583fb82011-05-25 07:41:43 -04002587 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002588 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002589 /*
2590 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002591 * so note that its first cluster is in use to avoid
2592 * freeing it when removing blocks. Eventually, the
2593 * right edge of the truncated/punched region will
2594 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002595 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002596 if (sbi->s_cluster_ratio > 1) {
2597 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002598 partial->pclu = EXT4_B2C(sbi, pblk);
2599 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002600 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002601 ex--;
2602 ex_ee_block = le32_to_cpu(ex->ee_block);
2603 ex_ee_len = ext4_ext_get_actual_len(ex);
2604 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002605 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002606 EXT4_ERROR_INODE(inode,
2607 "can not handle truncate %u:%u "
2608 "on extent %u:%u",
2609 start, end, ex_ee_block,
2610 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002611 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002612 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 } else if (a != ex_ee_block) {
2614 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002615 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002616 } else {
2617 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002618 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002619 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002620 /*
2621 * 3 for leaf, sb, and inode plus 2 (bmap and group
2622 * descriptor) for each block group; assume two block
2623 * groups plus ex_ee_len/blocks_per_block_group for
2624 * the worst case
2625 */
2626 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002627 if (ex == EXT_FIRST_EXTENT(eh)) {
2628 correct_index = 1;
2629 credits += (ext_depth(inode)) + 1;
2630 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002631 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002632 /*
2633 * We may end up freeing some index blocks and data from the
2634 * punched range. Note that partial clusters are accounted for
2635 * by ext4_free_data_revoke_credits().
2636 */
2637 revoke_credits =
2638 ext4_free_metadata_revoke_credits(inode->i_sb,
2639 ext_depth(inode)) +
2640 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002641
Jan Karaa4130362019-11-05 17:44:16 +01002642 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002643 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002644 if (err) {
2645 if (err > 0)
2646 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002647 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002648 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002649
2650 err = ext4_ext_get_access(handle, inode, path + depth);
2651 if (err)
2652 goto out;
2653
Eric Whitney9fe67142018-10-01 14:25:08 -04002654 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002655 if (err)
2656 goto out;
2657
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002658 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002659 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002660 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002661
Alex Tomasa86c6182006-10-11 01:21:03 -07002662 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002663 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002664 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002665 * extent have been removed.
2666 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002667 if (unwritten && num)
2668 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002669 /*
2670 * If the extent was completely released,
2671 * we need to remove it from the leaf
2672 */
2673 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002674 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002675 /*
2676 * For hole punching, we need to scoot all the
2677 * extents up when an extent is removed so that
2678 * we dont have blank extents in the middle
2679 */
2680 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2681 sizeof(struct ext4_extent));
2682
2683 /* Now get rid of the one at the end */
2684 memset(EXT_LAST_EXTENT(eh), 0,
2685 sizeof(struct ext4_extent));
2686 }
2687 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002688 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002689
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002690 err = ext4_ext_dirty(handle, inode, path + depth);
2691 if (err)
2692 goto out;
2693
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002694 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002695 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002696 ex--;
2697 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002698 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002699 }
2700
2701 if (correct_index && eh->eh_entries)
2702 err = ext4_ext_correct_indexes(handle, inode, path);
2703
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002704 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002705 * If there's a partial cluster and at least one extent remains in
2706 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002707 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002708 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002709 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002710 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002711 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002712 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002713 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2714 int flags = get_default_free_blocks_flags(inode);
2715
2716 if (ext4_is_pending(inode, partial->lblk))
2717 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002718 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002719 EXT4_C2B(sbi, partial->pclu),
2720 sbi->s_cluster_ratio, flags);
2721 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2722 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002723 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002724 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002725 }
2726
Alex Tomasa86c6182006-10-11 01:21:03 -07002727 /* if this leaf is free, then we should
2728 * remove it from index block above */
2729 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002730 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002731
2732out:
2733 return err;
2734}
2735
2736/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002737 * ext4_ext_more_to_rm:
2738 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002739 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002740static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002741ext4_ext_more_to_rm(struct ext4_ext_path *path)
2742{
2743 BUG_ON(path->p_idx == NULL);
2744
2745 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2746 return 0;
2747
2748 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002749 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002750 * so we have to consider current index for truncation
2751 */
2752 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2753 return 0;
2754 return 1;
2755}
2756
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002757int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2758 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002759{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002760 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002761 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002762 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002763 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002764 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002765 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002766
Eric Whitney9fe67142018-10-01 14:25:08 -04002767 partial.pclu = 0;
2768 partial.lblk = 0;
2769 partial.state = initial;
2770
Lukas Czerner5f95d212012-03-19 23:03:19 -04002771 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002772
2773 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002774 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2775 depth + 1,
2776 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002777 if (IS_ERR(handle))
2778 return PTR_ERR(handle);
2779
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002780again:
Lukas Czerner61801322013-05-27 23:32:35 -04002781 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002782
Alex Tomasa86c6182006-10-11 01:21:03 -07002783 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002784 * Check if we are removing extents inside the extent tree. If that
2785 * is the case, we are going to punch a hole inside the extent tree
2786 * so we have to check whether we need to split the extent covering
2787 * the last block to remove so we can easily remove the part of it
2788 * in ext4_ext_rm_leaf().
2789 */
2790 if (end < EXT_MAX_BLOCKS - 1) {
2791 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002792 ext4_lblk_t ee_block, ex_end, lblk;
2793 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002794
Eric Whitneyf4226d92014-11-23 00:55:42 -05002795 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002796 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002797 if (IS_ERR(path)) {
2798 ext4_journal_stop(handle);
2799 return PTR_ERR(path);
2800 }
2801 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002802 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002803 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002804 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002805 if (depth) {
2806 EXT4_ERROR_INODE(inode,
2807 "path[%d].p_hdr == NULL",
2808 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002809 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002810 }
2811 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002812 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002813
2814 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002815 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002816
2817 /*
2818 * See if the last block is inside the extent, if so split
2819 * the extent at 'end' block so we can easily remove the
2820 * tail of the first part of the split extent in
2821 * ext4_ext_rm_leaf().
2822 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002823 if (end >= ee_block && end < ex_end) {
2824
2825 /*
2826 * If we're going to split the extent, note that
2827 * the cluster containing the block after 'end' is
2828 * in use to avoid freeing it when removing blocks.
2829 */
2830 if (sbi->s_cluster_ratio > 1) {
2831 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04002832 partial.pclu = EXT4_B2C(sbi, pblk);
2833 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002834 }
2835
Lukas Czerner5f95d212012-03-19 23:03:19 -04002836 /*
2837 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002838 * block in the first new extent. Also we should not
2839 * fail removing space due to ENOSPC so try to use
2840 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002841 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002842 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002843 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002844 if (err < 0)
2845 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002846
Eric Whitney7bd75232019-02-28 23:34:11 -05002847 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
2848 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05002849 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05002850 * If we're punching, there's an extent to the right.
2851 * If the partial cluster hasn't been set, set it to
2852 * that extent's first cluster and its state to nofree
2853 * so it won't be freed should it contain blocks to be
2854 * removed. If it's already set (tofree/nofree), we're
2855 * retrying and keep the original partial cluster info
2856 * so a cluster marked tofree as a result of earlier
2857 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05002858 */
2859 lblk = ex_end + 1;
2860 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2861 &ex);
2862 if (err)
2863 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04002864 if (pblk) {
2865 partial.pclu = EXT4_B2C(sbi, pblk);
2866 partial.state = nofree;
2867 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002868 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002869 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002870 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002871 * We start scanning from right side, freeing all the blocks
2872 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002873 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002874 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002875 if (path) {
2876 int k = i = depth;
2877 while (--k > 0)
2878 path[k].p_block =
2879 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2880 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002881 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Ashish Sangwan968dee72012-07-22 22:49:08 -04002882 GFP_NOFS);
2883 if (path == NULL) {
2884 ext4_journal_stop(handle);
2885 return -ENOMEM;
2886 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002887 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002888 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002889 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002890
Theodore Ts'oc3491792013-08-16 21:21:41 -04002891 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002892 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002893 goto out;
2894 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002895 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002896 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002897
2898 while (i >= 0 && err == 0) {
2899 if (i == depth) {
2900 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002901 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002902 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002903 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002904 brelse(path[i].p_bh);
2905 path[i].p_bh = NULL;
2906 i--;
2907 continue;
2908 }
2909
2910 /* this is index block */
2911 if (!path[i].p_hdr) {
2912 ext_debug("initialize header\n");
2913 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002914 }
2915
Alex Tomasa86c6182006-10-11 01:21:03 -07002916 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002917 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002918 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2919 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2920 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2921 path[i].p_hdr,
2922 le16_to_cpu(path[i].p_hdr->eh_entries));
2923 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002924 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002925 path[i].p_idx--;
2926 }
2927
2928 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2929 i, EXT_FIRST_INDEX(path[i].p_hdr),
2930 path[i].p_idx);
2931 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002932 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002933 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002934 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002935 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002936 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002937 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002938 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2939 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002940 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002941 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002942 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002943 break;
2944 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04002945 /* Yield here to deal with large extent trees.
2946 * Should be a no-op if we did IO above. */
2947 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002948 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002949 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002950 break;
2951 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002952 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002953
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002954 /* save actual number of indexes since this
2955 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002956 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2957 i++;
2958 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002959 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002960 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002961 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002962 * handle must be already prepared by the
2963 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05002964 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002965 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002966 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002967 brelse(path[i].p_bh);
2968 path[i].p_bh = NULL;
2969 i--;
2970 ext_debug("return to level %d\n", i);
2971 }
2972 }
2973
Eric Whitney9fe67142018-10-01 14:25:08 -04002974 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
2975 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04002976
Eric Whitney0756b902014-11-23 00:59:39 -05002977 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002978 * if there's a partial cluster and we have removed the first extent
2979 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05002980 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002981 if (partial.state == tofree && err == 0) {
2982 int flags = get_default_free_blocks_flags(inode);
2983
2984 if (ext4_is_pending(inode, partial.lblk))
2985 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04002986 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002987 EXT4_C2B(sbi, partial.pclu),
2988 sbi->s_cluster_ratio, flags);
2989 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2990 ext4_rereserve_cluster(inode, partial.lblk);
2991 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04002992 }
2993
Alex Tomasa86c6182006-10-11 01:21:03 -07002994 /* TODO: flexible tree reduction should be here */
2995 if (path->p_hdr->eh_entries == 0) {
2996 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002997 * truncate to zero freed all the tree,
2998 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002999 */
3000 err = ext4_ext_get_access(handle, inode, path);
3001 if (err == 0) {
3002 ext_inode_hdr(inode)->eh_depth = 0;
3003 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003004 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003005 err = ext4_ext_dirty(handle, inode, path);
3006 }
3007 }
3008out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003009 ext4_ext_drop_refs(path);
3010 kfree(path);
3011 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003012 if (err == -EAGAIN)
3013 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003014 ext4_journal_stop(handle);
3015
3016 return err;
3017}
3018
3019/*
3020 * called at mount time
3021 */
3022void ext4_ext_init(struct super_block *sb)
3023{
3024 /*
3025 * possible initialization would be here
3026 */
3027
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003028 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003029#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003030 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003031#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003032 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003033#endif
3034#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003035 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003036#endif
3037#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003038 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003039#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003040 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003041#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003042#ifdef EXTENTS_STATS
3043 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3044 EXT4_SB(sb)->s_ext_min = 1 << 30;
3045 EXT4_SB(sb)->s_ext_max = 0;
3046#endif
3047 }
3048}
3049
3050/*
3051 * called at umount time
3052 */
3053void ext4_ext_release(struct super_block *sb)
3054{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003055 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003056 return;
3057
3058#ifdef EXTENTS_STATS
3059 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3060 struct ext4_sb_info *sbi = EXT4_SB(sb);
3061 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3062 sbi->s_ext_blocks, sbi->s_ext_extents,
3063 sbi->s_ext_blocks / sbi->s_ext_extents);
3064 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3065 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3066 }
3067#endif
3068}
3069
Zheng Liud7b2a002013-08-28 14:47:06 -04003070static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3071{
3072 ext4_lblk_t ee_block;
3073 ext4_fsblk_t ee_pblock;
3074 unsigned int ee_len;
3075
3076 ee_block = le32_to_cpu(ex->ee_block);
3077 ee_len = ext4_ext_get_actual_len(ex);
3078 ee_pblock = ext4_ext_pblock(ex);
3079
3080 if (ee_len == 0)
3081 return 0;
3082
3083 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3084 EXTENT_STATUS_WRITTEN);
3085}
3086
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003087/* FIXME!! we need to try to merge to left or right after zero-out */
3088static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3089{
Lukas Czerner24075182010-10-27 21:30:06 -04003090 ext4_fsblk_t ee_pblock;
3091 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003092
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003093 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003094 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003095 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3096 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003097}
3098
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003099/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003100 * ext4_split_extent_at() splits an extent at given block.
3101 *
3102 * @handle: the journal handle
3103 * @inode: the file inode
3104 * @path: the path to the extent
3105 * @split: the logical block where the extent is splitted.
3106 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003107 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003108 * @flags: flags used to insert new extent to extent tree.
3109 *
3110 *
3111 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3112 * of which are deterimined by split_flag.
3113 *
3114 * There are two cases:
3115 * a> the extent are splitted into two extent.
3116 * b> split is not needed, and just mark the extent.
3117 *
3118 * return 0 on success.
3119 */
3120static int ext4_split_extent_at(handle_t *handle,
3121 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003122 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003123 ext4_lblk_t split,
3124 int split_flag,
3125 int flags)
3126{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003127 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003128 ext4_fsblk_t newblock;
3129 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003130 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003131 struct ext4_extent *ex2 = NULL;
3132 unsigned int ee_len, depth;
3133 int err = 0;
3134
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003135 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3136 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3137
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003138 ext_debug("ext4_split_extents_at: inode %lu, logical"
3139 "block %llu\n", inode->i_ino, (unsigned long long)split);
3140
3141 ext4_ext_show_leaf(inode, path);
3142
3143 depth = ext_depth(inode);
3144 ex = path[depth].p_ext;
3145 ee_block = le32_to_cpu(ex->ee_block);
3146 ee_len = ext4_ext_get_actual_len(ex);
3147 newblock = split - ee_block + ext4_ext_pblock(ex);
3148
3149 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003150 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003151 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003152 EXT4_EXT_MARK_UNWRIT1 |
3153 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003154
3155 err = ext4_ext_get_access(handle, inode, path + depth);
3156 if (err)
3157 goto out;
3158
3159 if (split == ee_block) {
3160 /*
3161 * case b: block @split is the block that the extent begins with
3162 * then we just change the state of the extent, and splitting
3163 * is not needed.
3164 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003165 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3166 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003167 else
3168 ext4_ext_mark_initialized(ex);
3169
3170 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003171 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003172
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003173 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003174 goto out;
3175 }
3176
3177 /* case a */
3178 memcpy(&orig_ex, ex, sizeof(orig_ex));
3179 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003180 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3181 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003182
3183 /*
3184 * path may lead to new leaf, not to original leaf any more
3185 * after ext4_ext_insert_extent() returns,
3186 */
3187 err = ext4_ext_dirty(handle, inode, path + depth);
3188 if (err)
3189 goto fix_extent_len;
3190
3191 ex2 = &newex;
3192 ex2->ee_block = cpu_to_le32(split);
3193 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3194 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003195 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3196 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003197
Theodore Ts'odfe50802014-09-01 14:37:09 -04003198 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003199 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003200 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003201 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003202 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003203 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003204 zero_ex.ee_len = cpu_to_le16(
3205 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003206 ext4_ext_store_pblock(&zero_ex,
3207 ext4_ext_pblock(ex2));
3208 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003209 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003210 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003211 zero_ex.ee_len = cpu_to_le16(
3212 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003213 ext4_ext_store_pblock(&zero_ex,
3214 ext4_ext_pblock(ex));
3215 }
3216 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003217 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003218 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003219 zero_ex.ee_len = cpu_to_le16(
3220 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003221 ext4_ext_store_pblock(&zero_ex,
3222 ext4_ext_pblock(&orig_ex));
3223 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003224
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003225 if (err)
3226 goto fix_extent_len;
3227 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003228 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003229 ext4_ext_try_to_merge(handle, inode, path, ex);
3230 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003231 if (err)
3232 goto fix_extent_len;
3233
3234 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003235 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003236
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003237 goto out;
3238 } else if (err)
3239 goto fix_extent_len;
3240
3241out:
3242 ext4_ext_show_leaf(inode, path);
3243 return err;
3244
3245fix_extent_len:
3246 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003247 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003248 return err;
3249}
3250
3251/*
3252 * ext4_split_extents() splits an extent and mark extent which is covered
3253 * by @map as split_flags indicates
3254 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003255 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003256 * There are three possibilities:
3257 * a> There is no split required
3258 * b> Splits in two extents: Split is happening at either end of the extent
3259 * c> Splits in three extents: Somone is splitting in middle of the extent
3260 *
3261 */
3262static int ext4_split_extent(handle_t *handle,
3263 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003264 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003265 struct ext4_map_blocks *map,
3266 int split_flag,
3267 int flags)
3268{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003269 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003270 ext4_lblk_t ee_block;
3271 struct ext4_extent *ex;
3272 unsigned int ee_len, depth;
3273 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003274 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003275 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003276 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003277
3278 depth = ext_depth(inode);
3279 ex = path[depth].p_ext;
3280 ee_block = le32_to_cpu(ex->ee_block);
3281 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003282 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003283
3284 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003285 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003286 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003287 if (unwritten)
3288 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3289 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003290 if (split_flag & EXT4_EXT_DATA_VALID2)
3291 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003292 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003293 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003294 if (err)
3295 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003296 } else {
3297 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003298 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003299 /*
3300 * Update path is required because previous ext4_split_extent_at() may
3301 * result in split of original leaf or extent zeroout.
3302 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003303 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003304 if (IS_ERR(path))
3305 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003306 depth = ext_depth(inode);
3307 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003308 if (!ex) {
3309 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3310 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003311 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003312 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003313 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003314 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003315
3316 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003317 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003318 if (unwritten) {
3319 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003320 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003321 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003322 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003323 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003324 map->m_lblk, split_flag1, flags);
3325 if (err)
3326 goto out;
3327 }
3328
3329 ext4_ext_show_leaf(inode, path);
3330out:
Zheng Liu3a225672013-03-10 21:20:23 -04003331 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003332}
3333
Amit Arora56055d32007-07-17 21:42:38 -04003334/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003335 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003336 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003337 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003338 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003339 * There are three possibilities:
3340 * a> There is no split required: Entire extent should be initialized
3341 * b> Splits in two extents: Write is happening at either end of the extent
3342 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003343 *
3344 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003345 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003346 * - The extent pointed to by 'path' contains a superset
3347 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3348 *
3349 * Post-conditions on success:
3350 * - the returned value is the number of blocks beyond map->l_lblk
3351 * that are allocated and initialized.
3352 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003353 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003354static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003355 struct inode *inode,
3356 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003357 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003358 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003359{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003360 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003361 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003362 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003363 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003364 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003365 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003366 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003367 unsigned int ee_len, depth, map_len = map->m_len;
3368 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003369 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003370 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003371
3372 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3373 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003374 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003375
Zheng Liu67a5da52012-08-17 09:54:17 -04003376 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003377 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3378 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003379 if (eof_block < map->m_lblk + map_len)
3380 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003381
3382 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003383 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003384 ex = path[depth].p_ext;
3385 ee_block = le32_to_cpu(ex->ee_block);
3386 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003387 zero_ex1.ee_len = 0;
3388 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003389
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003390 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3391
3392 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003393 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003394 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003395
3396 /*
3397 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003398 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003399 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003400 * memmove() calls. Transferring to the left is the common case in
3401 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3402 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003403 *
3404 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003405 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003406 * This would require removing the extent if the transfer
3407 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003408 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003409 * same extent tree node.
3410 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003411 if ((map->m_lblk == ee_block) &&
3412 /* See if we can merge left */
3413 (map_len < ee_len) && /*L1*/
3414 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003415 ext4_lblk_t prev_lblk;
3416 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003417 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003418
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003419 abut_ex = ex - 1;
3420 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3421 prev_len = ext4_ext_get_actual_len(abut_ex);
3422 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003423 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003424
3425 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003426 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003427 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003428 * - C1: abut_ex is initialized,
3429 * - C2: abut_ex is logically abutting ex,
3430 * - C3: abut_ex is physically abutting ex,
3431 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003432 * overflowing the (initialized) length limit.
3433 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003434 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003435 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3436 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003437 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003438 err = ext4_ext_get_access(handle, inode, path + depth);
3439 if (err)
3440 goto out;
3441
3442 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003443 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003444
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003445 /* Shift the start of ex by 'map_len' blocks */
3446 ex->ee_block = cpu_to_le32(ee_block + map_len);
3447 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3448 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003449 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003450
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003451 /* Extend abut_ex by 'map_len' blocks */
3452 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003453
3454 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003455 allocated = map_len;
3456 }
3457 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3458 (map_len < ee_len) && /*L1*/
3459 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3460 /* See if we can merge right */
3461 ext4_lblk_t next_lblk;
3462 ext4_fsblk_t next_pblk, ee_pblk;
3463 unsigned int next_len;
3464
3465 abut_ex = ex + 1;
3466 next_lblk = le32_to_cpu(abut_ex->ee_block);
3467 next_len = ext4_ext_get_actual_len(abut_ex);
3468 next_pblk = ext4_ext_pblock(abut_ex);
3469 ee_pblk = ext4_ext_pblock(ex);
3470
3471 /*
3472 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3473 * upon those conditions:
3474 * - C1: abut_ex is initialized,
3475 * - C2: abut_ex is logically abutting ex,
3476 * - C3: abut_ex is physically abutting ex,
3477 * - C4: abut_ex can receive the additional blocks without
3478 * overflowing the (initialized) length limit.
3479 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003480 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003481 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3482 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3483 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3484 err = ext4_ext_get_access(handle, inode, path + depth);
3485 if (err)
3486 goto out;
3487
3488 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3489 map, ex, abut_ex);
3490
3491 /* Shift the start of abut_ex by 'map_len' blocks */
3492 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3493 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3494 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003495 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003496
3497 /* Extend abut_ex by 'map_len' blocks */
3498 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3499
3500 /* Result: number of initialized blocks past m_lblk */
3501 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003502 }
3503 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003504 if (allocated) {
3505 /* Mark the block containing both extents as dirty */
3506 ext4_ext_dirty(handle, inode, path + depth);
3507
3508 /* Update path to point to the right extent */
3509 path[depth].p_ext = abut_ex;
3510 goto out;
3511 } else
3512 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003513
Yongqiang Yang667eff32011-05-03 12:25:07 -04003514 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003515 /*
3516 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003517 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003518 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003519 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003520
Zheng Liu67a5da52012-08-17 09:54:17 -04003521 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3522 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003523 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003524
Amit Arora56055d32007-07-17 21:42:38 -04003525 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003526 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003527 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003528 * 2. split the extent into two extents, zeroout the head of the first
3529 * extent.
3530 * 3. split the extent into two extents, zeroout the tail of the second
3531 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003532 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003533 * 5. no splitting needed, just possibly zeroout the head and / or the
3534 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003535 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003536 split_map.m_lblk = map->m_lblk;
3537 split_map.m_len = map->m_len;
3538
Jan Kara4f8caa62017-05-26 17:40:52 -04003539 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003540 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003541 /* case 3 or 5 */
3542 zero_ex1.ee_block =
3543 cpu_to_le32(split_map.m_lblk +
3544 split_map.m_len);
3545 zero_ex1.ee_len =
3546 cpu_to_le16(allocated - split_map.m_len);
3547 ext4_ext_store_pblock(&zero_ex1,
3548 ext4_ext_pblock(ex) + split_map.m_lblk +
3549 split_map.m_len - ee_block);
3550 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003551 if (err)
3552 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003553 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003554 }
3555 if (split_map.m_lblk - ee_block + split_map.m_len <
3556 max_zeroout) {
3557 /* case 2 or 5 */
3558 if (split_map.m_lblk != ee_block) {
3559 zero_ex2.ee_block = ex->ee_block;
3560 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003561 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003562 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003563 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003564 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003565 if (err)
3566 goto out;
3567 }
3568
Jan Kara4f8caa62017-05-26 17:40:52 -04003569 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003570 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003571 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003572 }
3573 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003574
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003575 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3576 flags);
3577 if (err > 0)
3578 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003579out:
Zheng Liuadb23552013-03-10 21:13:05 -04003580 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003581 if (!err) {
3582 err = ext4_zeroout_es(inode, &zero_ex1);
3583 if (!err)
3584 err = ext4_zeroout_es(inode, &zero_ex2);
3585 }
Amit Arora56055d32007-07-17 21:42:38 -04003586 return err ? err : allocated;
3587}
3588
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003589/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003590 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003591 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003592 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003593 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003594 * Writing to an unwritten extent may result in splitting the unwritten
3595 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003596 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003597 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003598 * b> Splits in two extents: Write is happening at either end of the extent
3599 * c> Splits in three extents: Somone is writing in middle of the extent
3600 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003601 * This works the same way in the case of initialized -> unwritten conversion.
3602 *
Mingming Cao00314622009-09-28 15:49:08 -04003603 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003604 * the unwritten extent split. To prevent ENOSPC occur at the IO
3605 * complete, we need to split the unwritten extent before DIO submit
3606 * the IO. The unwritten extent called at this time will be split
3607 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003608 * being filled will be convert to initialized by the end_io callback function
3609 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003610 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003611 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003612 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003613static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003614 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003615 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003616 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003617 int flags)
3618{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003619 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003620 ext4_lblk_t eof_block;
3621 ext4_lblk_t ee_block;
3622 struct ext4_extent *ex;
3623 unsigned int ee_len;
3624 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003625
Lukas Czernerb8a86842014-03-18 18:05:35 -04003626 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3627 __func__, inode->i_ino,
3628 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003629
3630 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3631 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003632 if (eof_block < map->m_lblk + map->m_len)
3633 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003634 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003635 * It is safe to convert extent to initialized via explicit
3636 * zeroout only if extent is fully insde i_size or new_size.
3637 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003638 depth = ext_depth(inode);
3639 ex = path[depth].p_ext;
3640 ee_block = le32_to_cpu(ex->ee_block);
3641 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003642
Lukas Czernerb8a86842014-03-18 18:05:35 -04003643 /* Convert to unwritten */
3644 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3645 split_flag |= EXT4_EXT_DATA_VALID1;
3646 /* Convert to initialized */
3647 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3648 split_flag |= ee_block + ee_len <= eof_block ?
3649 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003650 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003651 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003652 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003653 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003654}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003655
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003656static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003657 struct inode *inode,
3658 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003659 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003660{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003661 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003662 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003663 ext4_lblk_t ee_block;
3664 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003665 int depth;
3666 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003667
3668 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003669 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003670 ee_block = le32_to_cpu(ex->ee_block);
3671 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003672
Yongqiang Yang197217a2011-05-03 11:45:29 -04003673 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3674 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003675 (unsigned long long)ee_block, ee_len);
3676
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003677 /* If extent is larger than requested it is a clear sign that we still
3678 * have some extent state machine issues left. So extent_split is still
3679 * required.
3680 * TODO: Once all related issues will be fixed this situation should be
3681 * illegal.
3682 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003683 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003684#ifdef CONFIG_EXT4_DEBUG
3685 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003686 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003687 inode->i_ino, (unsigned long long)ee_block, ee_len,
3688 (unsigned long long)map->m_lblk, map->m_len);
3689#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003690 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003691 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003692 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003693 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003694 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003695 if (IS_ERR(path))
3696 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003697 depth = ext_depth(inode);
3698 ex = path[depth].p_ext;
3699 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003700
Mingming Cao00314622009-09-28 15:49:08 -04003701 err = ext4_ext_get_access(handle, inode, path + depth);
3702 if (err)
3703 goto out;
3704 /* first mark the extent as initialized */
3705 ext4_ext_mark_initialized(ex);
3706
Yongqiang Yang197217a2011-05-03 11:45:29 -04003707 /* note: ext4_ext_correct_indexes() isn't needed here because
3708 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003709 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003710 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003711
Mingming Cao00314622009-09-28 15:49:08 -04003712 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003713 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003714out:
3715 ext4_ext_show_leaf(inode, path);
3716 return err;
3717}
3718
3719static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003720convert_initialized_extent(handle_t *handle, struct inode *inode,
3721 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003722 struct ext4_ext_path **ppath,
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003723 unsigned int *allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003724{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003725 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003726 struct ext4_extent *ex;
3727 ext4_lblk_t ee_block;
3728 unsigned int ee_len;
3729 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003730 int err = 0;
3731
3732 /*
3733 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003734 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003735 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003736 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3737 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003738
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003739 depth = ext_depth(inode);
3740 ex = path[depth].p_ext;
3741 ee_block = le32_to_cpu(ex->ee_block);
3742 ee_len = ext4_ext_get_actual_len(ex);
3743
3744 ext_debug("%s: inode %lu, logical"
3745 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3746 (unsigned long long)ee_block, ee_len);
3747
3748 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003749 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003750 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3751 if (err < 0)
3752 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003753 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003754 if (IS_ERR(path))
3755 return PTR_ERR(path);
3756 depth = ext_depth(inode);
3757 ex = path[depth].p_ext;
3758 if (!ex) {
3759 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3760 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003761 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003762 }
3763 }
3764
3765 err = ext4_ext_get_access(handle, inode, path + depth);
3766 if (err)
3767 return err;
3768 /* first mark the extent as unwritten */
3769 ext4_ext_mark_unwritten(ex);
3770
3771 /* note: ext4_ext_correct_indexes() isn't needed here because
3772 * borders are not changed
3773 */
3774 ext4_ext_try_to_merge(handle, inode, path, ex);
3775
3776 /* Mark modified extent as dirty */
3777 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3778 if (err)
3779 return err;
3780 ext4_ext_show_leaf(inode, path);
3781
3782 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003783
Lukas Czernerb8a86842014-03-18 18:05:35 -04003784 map->m_flags |= EXT4_MAP_UNWRITTEN;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05003785 if (*allocated > map->m_len)
3786 *allocated = map->m_len;
3787 map->m_len = *allocated;
3788 return 0;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003789}
3790
3791static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003792ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003793 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003794 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003795 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003796{
Eric Whitney4337ecd2020-02-11 16:02:16 -05003797#ifdef EXT_DEBUG
Theodore Ts'odfe50802014-09-01 14:37:09 -04003798 struct ext4_ext_path *path = *ppath;
Eric Whitney4337ecd2020-02-11 16:02:16 -05003799#endif
Mingming Cao00314622009-09-28 15:49:08 -04003800 int ret = 0;
3801 int err = 0;
3802
Lukas Czerner556615d2014-04-20 23:45:47 -04003803 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05003804 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003805 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003806 flags, allocated);
3807 ext4_ext_show_leaf(inode, path);
3808
Lukas Czerner27dd4382013-04-09 22:11:22 -04003809 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04003810 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04003811 * allocate metadata blocks for the new extent block if needed.
3812 */
3813 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
3814
Lukas Czerner556615d2014-04-20 23:45:47 -04003815 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05003816 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003817
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003818 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003819 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003820 ret = ext4_split_convert_extents(handle, inode, map, ppath,
3821 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04003822 if (ret <= 0)
3823 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003824 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003825 goto out;
3826 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003827 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04003828 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05003829 if (flags & EXT4_GET_BLOCKS_ZERO) {
3830 if (allocated > map->m_len)
3831 allocated = map->m_len;
3832 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
3833 allocated);
3834 if (err < 0)
3835 goto out2;
3836 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003837 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003838 ppath);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003839 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003840 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Whitney4337ecd2020-02-11 16:02:16 -05003841 else
Theodore Ts'o58590b02010-10-27 21:23:12 -04003842 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04003843 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05003844 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04003845 if (allocated > map->m_len)
3846 allocated = map->m_len;
3847 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003848 goto out2;
3849 }
3850 /* buffered IO case */
3851 /*
3852 * repeat fallocate creation request
3853 * we already have an unwritten extent
3854 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003855 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05003856 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003857 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05003858 }
Mingming Cao00314622009-09-28 15:49:08 -04003859
3860 /* buffered READ or buffered write_begin() lookup */
3861 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3862 /*
3863 * We have blocks reserved already. We
3864 * return allocated blocks so that delalloc
3865 * won't do block reservation for us. But
3866 * the buffer head will be unmapped so that
3867 * a read from the block returns 0s.
3868 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003869 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003870 goto out1;
3871 }
3872
3873 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04003874 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003875 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003876 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003877out:
3878 if (ret <= 0) {
3879 err = ret;
3880 goto out2;
3881 } else
3882 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003883 map->m_flags |= EXT4_MAP_NEW;
zhangyi (F)16e08b12019-02-10 23:32:07 -05003884 if (allocated > map->m_len)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003885 allocated = map->m_len;
Zheng Liu3a225672013-03-10 21:20:23 -04003886 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003887
Mingming Cao00314622009-09-28 15:49:08 -04003888map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003889 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003890out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003891 if (allocated > map->m_len)
3892 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003893 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003894 map->m_pblk = newblock;
3895 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003896out2:
Mingming Cao00314622009-09-28 15:49:08 -04003897 return err ? err : allocated;
3898}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003899
Mingming Cao00314622009-09-28 15:49:08 -04003900/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003901 * get_implied_cluster_alloc - check to see if the requested
3902 * allocation (in the map structure) overlaps with a cluster already
3903 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003904 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003905 * @map The requested lblk->pblk mapping
3906 * @ex The extent structure which might contain an implied
3907 * cluster allocation
3908 *
3909 * This function is called by ext4_ext_map_blocks() after we failed to
3910 * find blocks that were already in the inode's extent tree. Hence,
3911 * we know that the beginning of the requested region cannot overlap
3912 * the extent from the inode's extent tree. There are three cases we
3913 * want to catch. The first is this case:
3914 *
3915 * |--- cluster # N--|
3916 * |--- extent ---| |---- requested region ---|
3917 * |==========|
3918 *
3919 * The second case that we need to test for is this one:
3920 *
3921 * |--------- cluster # N ----------------|
3922 * |--- requested region --| |------- extent ----|
3923 * |=======================|
3924 *
3925 * The third case is when the requested region lies between two extents
3926 * within the same cluster:
3927 * |------------- cluster # N-------------|
3928 * |----- ex -----| |---- ex_right ----|
3929 * |------ requested region ------|
3930 * |================|
3931 *
3932 * In each of the above cases, we need to set the map->m_pblk and
3933 * map->m_len so it corresponds to the return the extent labelled as
3934 * "|====|" from cluster #N, since it is already in use for data in
3935 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3936 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3937 * as a new "allocated" block region. Otherwise, we will return 0 and
3938 * ext4_ext_map_blocks() will then allocate one or more new clusters
3939 * by calling ext4_mb_new_blocks().
3940 */
Aditya Kalid8990242011-09-09 19:18:51 -04003941static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003942 struct ext4_map_blocks *map,
3943 struct ext4_extent *ex,
3944 struct ext4_ext_path *path)
3945{
Aditya Kalid8990242011-09-09 19:18:51 -04003946 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003947 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003948 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003949 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003950 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3951 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3952 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3953
3954 /* The extent passed in that we are trying to match */
3955 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3956 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3957
3958 /* The requested region passed into ext4_map_blocks() */
3959 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003960
3961 if ((rr_cluster_start == ex_cluster_end) ||
3962 (rr_cluster_start == ex_cluster_start)) {
3963 if (rr_cluster_start == ex_cluster_end)
3964 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003965 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003966 map->m_len = min(map->m_len,
3967 (unsigned) sbi->s_cluster_ratio - c_offset);
3968 /*
3969 * Check for and handle this case:
3970 *
3971 * |--------- cluster # N-------------|
3972 * |------- extent ----|
3973 * |--- requested region ---|
3974 * |===========|
3975 */
3976
3977 if (map->m_lblk < ee_block)
3978 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3979
3980 /*
3981 * Check for the case where there is already another allocated
3982 * block to the right of 'ex' but before the end of the cluster.
3983 *
3984 * |------------- cluster # N-------------|
3985 * |----- ex -----| |---- ex_right ----|
3986 * |------ requested region ------|
3987 * |================|
3988 */
3989 if (map->m_lblk > ee_block) {
3990 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3991 map->m_len = min(map->m_len, next - map->m_lblk);
3992 }
Aditya Kalid8990242011-09-09 19:18:51 -04003993
3994 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003995 return 1;
3996 }
Aditya Kalid8990242011-09-09 19:18:51 -04003997
3998 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003999 return 0;
4000}
4001
4002
4003/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004004 * Block allocation/map/preallocation routine for extents based files
4005 *
4006 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004007 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004008 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4009 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004010 *
4011 * return > 0, number of of blocks already mapped/allocated
4012 * if create == 0 and these are pre-allocated blocks
4013 * buffer head is unmapped
4014 * otherwise blocks are mapped
4015 *
4016 * return = 0, if plain look up failed (blocks have not been allocated)
4017 * buffer head is unmapped
4018 *
4019 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004020 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004021int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4022 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004023{
4024 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004025 struct ext4_extent newex, *ex, *ex2;
4026 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004027 ext4_fsblk_t newblock = 0;
Eric Whitney34990462020-03-11 16:50:33 -04004028 int err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004029 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004030 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004031 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004032 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07004033
Mingming84fe3be2009-09-01 08:44:37 -04004034 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004035 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004036 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004037
Alex Tomasa86c6182006-10-11 01:21:03 -07004038 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004039 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004040 if (IS_ERR(path)) {
4041 err = PTR_ERR(path);
4042 path = NULL;
4043 goto out2;
4044 }
4045
4046 depth = ext_depth(inode);
4047
4048 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004049 * consistent leaf must not be empty;
4050 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004051 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004052 */
Frank Mayhar273df552010-03-02 11:46:09 -05004053 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4054 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004055 "lblock: %lu, depth: %d pblock %lld",
4056 (unsigned long) map->m_lblk, depth,
4057 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004058 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004059 goto out2;
4060 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004061
Avantika Mathur7e028972006-12-06 20:41:33 -08004062 ex = path[depth].p_ext;
4063 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004064 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004065 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004066 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004067
Lukas Czernerb8a86842014-03-18 18:05:35 -04004068
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004069 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004070 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004071 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004072 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004073 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004074
4075 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4076
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004077 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004078 if (in_range(map->m_lblk, ee_block, ee_len)) {
4079 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004080 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004081 allocated = ee_len - (map->m_lblk - ee_block);
4082 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4083 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004084
Lukas Czernerb8a86842014-03-18 18:05:35 -04004085 /*
4086 * If the extent is initialized check whether the
4087 * caller wants to convert it to unwritten.
4088 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004089 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004090 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004091 err = convert_initialized_extent(handle,
4092 inode, map, &path, &allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004093 goto out2;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004094 } else if (!ext4_ext_is_unwritten(ex)) {
Lukas Czerner78771912012-03-19 23:05:43 -04004095 goto out;
Eric Whitneyf064a9d2020-02-18 15:26:56 -05004096 }
Zheng Liu69eb33d2013-02-18 00:31:07 -05004097
Lukas Czerner556615d2014-04-20 23:45:47 -04004098 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004099 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004100 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004101 if (ret < 0)
4102 err = ret;
4103 else
4104 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004105 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004106 }
4107 }
4108
4109 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004110 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004111 * we couldn't try to create block if create flag is zero
4112 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004113 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004114 ext4_lblk_t hole_start, hole_len;
4115
Jan Karafacab4d2016-03-09 22:54:00 -05004116 hole_start = map->m_lblk;
4117 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004118 /*
4119 * put just found gap into cache to speed up
4120 * subsequent requests
4121 */
Jan Kara140a5252016-03-09 22:46:57 -05004122 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004123
4124 /* Update hole_len to reflect hole size after map->m_lblk */
4125 if (hole_start != map->m_lblk)
4126 hole_len -= map->m_lblk - hole_start;
4127 map->m_pblk = 0;
4128 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4129
Alex Tomasa86c6182006-10-11 01:21:03 -07004130 goto out2;
4131 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004132
Alex Tomasa86c6182006-10-11 01:21:03 -07004133 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004134 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004135 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004136 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004137 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004138
4139 /*
4140 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004141 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004142 */
4143 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004144 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004145 ar.len = allocated = map->m_len;
4146 newblock = map->m_pblk;
4147 goto got_allocated_blocks;
4148 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004149
Alex Tomasc9de5602008-01-29 00:19:52 -05004150 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004151 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004152 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4153 if (err)
4154 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004155 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004156 ex2 = NULL;
4157 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004158 if (err)
4159 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004160
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004161 /* Check if the extent after searching to the right implies a
4162 * cluster we can use. */
4163 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004164 get_implied_cluster_alloc(inode->i_sb, map, ex2, 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 }
4169
Amit Arora749269f2007-07-18 09:02:56 -04004170 /*
4171 * See if request is beyond maximum number of blocks we can have in
4172 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004173 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4174 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004175 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004176 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004177 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004178 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004179 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4180 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4181 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004182
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004183 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004184 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004185 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004186 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004187 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004188 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004189 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004190
4191 /* allocate new block */
4192 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004193 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4194 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004195 /*
4196 * We calculate the offset from the beginning of the cluster
4197 * for the logical block number, since when we allocate a
4198 * physical cluster, the physical block should start at the
4199 * same offset from the beginning of the cluster. This is
4200 * needed so that future calls to get_implied_cluster_alloc()
4201 * work correctly.
4202 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004203 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004204 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4205 ar.goal -= offset;
4206 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004207 if (S_ISREG(inode->i_mode))
4208 ar.flags = EXT4_MB_HINT_DATA;
4209 else
4210 /* disable in-core preallocation for non-regular files */
4211 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004212 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4213 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004214 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4215 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004216 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4217 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004218 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004219 if (!newblock)
4220 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004221 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004222 ar.goal, newblock, allocated);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004223 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004224 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4225 if (ar.len > allocated)
4226 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004227
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004228got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004229 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004230 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004231 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004232 /* Mark unwritten */
Eric Whitney34990462020-03-11 16:50:33 -04004233 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Lukas Czerner556615d2014-04-20 23:45:47 -04004234 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004235 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004236 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004237
Eric Whitney4337ecd2020-02-11 16:02:16 -05004238 err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
Eric Whitney34990462020-03-11 16:50:33 -04004239 if (err) {
4240 if (allocated_clusters) {
4241 int fb_flags = 0;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004242
Eric Whitney34990462020-03-11 16:50:33 -04004243 /*
4244 * free data blocks we just allocated.
4245 * not a good idea to call discard here directly,
4246 * but otherwise we'd need to call it every free().
4247 */
4248 ext4_discard_preallocations(inode);
4249 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4250 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
4251 ext4_free_blocks(handle, inode, NULL, newblock,
4252 EXT4_C2B(sbi, allocated_clusters),
4253 fb_flags);
4254 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004255 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004256 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004257
Alex Tomasa86c6182006-10-11 01:21:03 -07004258 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004259 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004260 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004261 if (allocated > map->m_len)
4262 allocated = map->m_len;
4263 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004264
Jan Karab436b9b2009-12-08 23:51:10 -05004265 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004266 * Reduce the reserved cluster count to reflect successful deferred
4267 * allocation of delayed allocated clusters or direct allocation of
4268 * clusters discovered to be delayed allocated. Once allocated, a
4269 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004270 */
Eric Whitney29711482020-03-11 16:51:25 -04004271 if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) {
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004272 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004273 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004274 * When allocating delayed allocated clusters, simply
4275 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004276 */
4277 ext4_da_update_reserve_space(inode, allocated_clusters,
4278 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004279 } else {
4280 ext4_lblk_t lblk, len;
4281 unsigned int n;
4282
4283 /*
4284 * When allocating non-delayed allocated clusters
4285 * (from fallocate, filemap, DIO, or clusters
4286 * allocated when delalloc has been disabled by
4287 * ext4_nonda_switch), reduce the reserved cluster
4288 * count by the number of allocated clusters that
4289 * have previously been delayed allocated. Quota
4290 * has been claimed by ext4_mb_new_blocks() above,
4291 * so release the quota reservations made for any
4292 * previously delayed allocated clusters.
4293 */
4294 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4295 len = allocated_clusters << sbi->s_cluster_bits;
4296 n = ext4_es_delayed_clu(inode, lblk, len);
4297 if (n > 0)
4298 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004299 }
4300 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004301
4302 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004303 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004304 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004305 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004306 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004307 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004308 else
Jan Karab436b9b2009-12-08 23:51:10 -05004309 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004310out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004311 if (allocated > map->m_len)
4312 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004313 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004314 map->m_flags |= EXT4_MAP_MAPPED;
4315 map->m_pblk = newblock;
4316 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004317out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004318 ext4_ext_drop_refs(path);
4319 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004320
Theodore Ts'o63b99962013-07-16 10:28:47 -04004321 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4322 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004323 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004324}
4325
Theodore Ts'od0abb362016-11-13 22:02:28 -05004326int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004327{
Alex Tomasa86c6182006-10-11 01:21:03 -07004328 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004329 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004330 int err = 0;
4331
4332 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004333 * TODO: optimization is possible here.
4334 * Probably we need not scan at all,
4335 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004336 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004337
4338 /* we have to know where to truncate from in crash case */
4339 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004340 err = ext4_mark_inode_dirty(handle, inode);
4341 if (err)
4342 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004343
4344 last_block = (inode->i_size + sb->s_blocksize - 1)
4345 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004346retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004347 err = ext4_es_remove_extent(inode, last_block,
4348 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004349 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004350 cond_resched();
4351 congestion_wait(BLK_RW_ASYNC, HZ/50);
4352 goto retry;
4353 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004354 if (err)
4355 return err;
4356 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004357}
4358
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004359static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004360 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004361 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004362{
Al Viro496ad9a2013-01-23 17:07:38 -05004363 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004364 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004365 int ret = 0;
4366 int ret2 = 0;
4367 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004368 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004369 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004370 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004371 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004372
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004373 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004374 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004375 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004376 /*
4377 * Don't normalize the request if it can fit in one extent so
4378 * that it doesn't get unnecessarily split into multiple
4379 * extents.
4380 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004381 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004382 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004383
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004384 /*
4385 * credits to insert 1 extent into extent tree
4386 */
4387 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004388 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004389
Amit Aroraa2df2a62007-07-17 21:42:41 -04004390retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004391 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004392 /*
4393 * Recalculate credits when extent tree depth changes.
4394 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004395 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004396 credits = ext4_chunk_trans_blocks(inode, len);
4397 depth = ext_depth(inode);
4398 }
4399
Theodore Ts'o9924a922013-02-08 21:59:22 -05004400 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4401 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004402 if (IS_ERR(handle)) {
4403 ret = PTR_ERR(handle);
4404 break;
4405 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004406 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004407 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004408 ext4_debug("inode #%lu: block %u: len %u: "
4409 "ext4_ext_map_blocks returned %d",
4410 inode->i_ino, map.m_lblk,
4411 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004412 ext4_mark_inode_dirty(handle, inode);
4413 ret2 = ext4_journal_stop(handle);
4414 break;
4415 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004416 map.m_lblk += ret;
4417 map.m_len = len = len - ret;
4418 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004419 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004420 if (new_size) {
4421 if (epos > new_size)
4422 epos = new_size;
4423 if (ext4_update_inode_size(inode, epos) & 0x1)
4424 inode->i_mtime = inode->i_ctime;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004425 }
4426 ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004427 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004428 ret2 = ext4_journal_stop(handle);
4429 if (ret2)
4430 break;
4431 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004432 if (ret == -ENOSPC &&
4433 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4434 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004435 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004436 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004437
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004438 return ret > 0 ? ret2 : ret;
4439}
4440
Eric Biggers43f81672019-12-31 12:04:40 -06004441static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
4442
4443static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len);
4444
Lukas Czernerb8a86842014-03-18 18:05:35 -04004445static long ext4_zero_range(struct file *file, loff_t offset,
4446 loff_t len, int mode)
4447{
4448 struct inode *inode = file_inode(file);
4449 handle_t *handle = NULL;
4450 unsigned int max_blocks;
4451 loff_t new_size = 0;
4452 int ret = 0;
4453 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004454 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004455 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004456 loff_t start, end;
4457 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004458 unsigned int blkbits = inode->i_blkbits;
4459
4460 trace_ext4_zero_range(inode, offset, len, mode);
4461
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004462 /* Call ext4_force_commit to flush all data in case of data=journal. */
4463 if (ext4_should_journal_data(inode)) {
4464 ret = ext4_force_commit(inode->i_sb);
4465 if (ret)
4466 return ret;
4467 }
4468
Lukas Czernerb8a86842014-03-18 18:05:35 -04004469 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004470 * Round up offset. This is not fallocate, we neet to zero out
4471 * blocks, so convert interior block aligned part of the range to
4472 * unwritten and possibly manually zero out unaligned parts of the
4473 * range.
4474 */
4475 start = round_up(offset, 1 << blkbits);
4476 end = round_down((offset + len), 1 << blkbits);
4477
4478 if (start < offset || end > offset + len)
4479 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004480 partial_begin = offset & ((1 << blkbits) - 1);
4481 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004482
4483 lblk = start >> blkbits;
4484 max_blocks = (end >> blkbits);
4485 if (max_blocks < lblk)
4486 max_blocks = 0;
4487 else
4488 max_blocks -= lblk;
4489
Al Viro59551022016-01-22 15:40:57 -05004490 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004491
4492 /*
4493 * Indirect files do not support unwritten extnets
4494 */
4495 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4496 ret = -EOPNOTSUPP;
4497 goto out_mutex;
4498 }
4499
4500 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004501 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004502 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004503 new_size = offset + len;
4504 ret = inode_newsize_ok(inode, new_size);
4505 if (ret)
4506 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004507 }
4508
Lukas Czerner0f2af212015-04-03 00:09:13 -04004509 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4510 if (mode & FALLOC_FL_KEEP_SIZE)
4511 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4512
Jan Kara17048e82015-12-07 14:29:17 -05004513 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004514 inode_dio_wait(inode);
4515
Lukas Czerner0f2af212015-04-03 00:09:13 -04004516 /* Preallocate the range including the unaligned edges */
4517 if (partial_begin || partial_end) {
4518 ret = ext4_alloc_file_blocks(file,
4519 round_down(offset, 1 << blkbits) >> blkbits,
4520 (round_up((offset + len), 1 << blkbits) -
4521 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004522 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004523 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004524 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004525
4526 }
4527
4528 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004529 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004530 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4531 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004532
Jan Karaea3d7202015-12-07 14:28:03 -05004533 /*
4534 * Prevent page faults from reinstantiating pages we have
4535 * released from page cache.
4536 */
4537 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004538
4539 ret = ext4_break_layouts(inode);
4540 if (ret) {
4541 up_write(&EXT4_I(inode)->i_mmap_sem);
4542 goto out_mutex;
4543 }
4544
Jan Kara01127842015-12-07 14:34:49 -05004545 ret = ext4_update_disksize_before_punch(inode, offset, len);
4546 if (ret) {
4547 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004548 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004549 }
Jan Karaea3d7202015-12-07 14:28:03 -05004550 /* Now release the pages and zero block aligned part of pages */
4551 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004552 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004553
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004554 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004555 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004556 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004557 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004558 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004559 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004560 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004561 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004562
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004563 /*
4564 * In worst case we have to writeout two nonadjacent unwritten
4565 * blocks and update the inode
4566 */
4567 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4568 if (ext4_should_journal_data(inode))
4569 credits += 2;
4570 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004571 if (IS_ERR(handle)) {
4572 ret = PTR_ERR(handle);
4573 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004574 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004575 }
4576
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004577 inode->i_mtime = inode->i_ctime = current_time(inode);
Eric Whitney4337ecd2020-02-11 16:02:16 -05004578 if (new_size)
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004579 ext4_update_inode_size(inode, new_size);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004580 ext4_mark_inode_dirty(handle, inode);
4581
4582 /* Zero out partial block at the edges of the range */
4583 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004584 if (ret >= 0)
4585 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004586
4587 if (file->f_flags & O_SYNC)
4588 ext4_handle_sync(handle);
4589
4590 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004591out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004592 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004593 return ret;
4594}
4595
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004596/*
4597 * preallocate space for a file. This implements ext4's fallocate file
4598 * operation, which gets called from sys_fallocate system call.
4599 * For block-mapped files, posix_fallocate should fall back to the method
4600 * of writing zeroes to the required new blocks (the same behavior which is
4601 * expected for file systems which do not support fallocate() system call).
4602 */
4603long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4604{
4605 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004606 loff_t new_size = 0;
4607 unsigned int max_blocks;
4608 int ret = 0;
4609 int flags;
4610 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004611 unsigned int blkbits = inode->i_blkbits;
4612
Michael Halcrow2058f832015-04-12 00:55:10 -04004613 /*
4614 * Encrypted inodes can't handle collapse range or insert
4615 * range since we would need to re-encrypt blocks with a
4616 * different IV or XTS tweak (which are based on the logical
4617 * block number).
Michael Halcrow2058f832015-04-12 00:55:10 -04004618 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304619 if (IS_ENCRYPTED(inode) &&
Eric Biggers457b1e32019-12-26 09:42:16 -06004620 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004621 return -EOPNOTSUPP;
4622
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004623 /* Return error if mode is not supported */
4624 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004625 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4626 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004627 return -EOPNOTSUPP;
4628
4629 if (mode & FALLOC_FL_PUNCH_HOLE)
4630 return ext4_punch_hole(inode, offset, len);
4631
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004632 ret = ext4_convert_inline_data(inode);
4633 if (ret)
4634 return ret;
4635
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004636 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4637 return ext4_collapse_range(inode, offset, len);
4638
Namjae Jeon331573f2015-06-09 01:55:03 -04004639 if (mode & FALLOC_FL_INSERT_RANGE)
4640 return ext4_insert_range(inode, offset, len);
4641
Lukas Czernerb8a86842014-03-18 18:05:35 -04004642 if (mode & FALLOC_FL_ZERO_RANGE)
4643 return ext4_zero_range(file, offset, len, mode);
4644
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004645 trace_ext4_fallocate_enter(inode, offset, len, mode);
4646 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004647
Fabian Frederick518eaa62016-09-15 11:55:01 -04004648 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004649 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004650 if (mode & FALLOC_FL_KEEP_SIZE)
4651 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4652
Al Viro59551022016-01-22 15:40:57 -05004653 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004654
Davide Italiano280227a2015-05-02 23:21:15 -04004655 /*
4656 * We only support preallocation for extent-based files only
4657 */
4658 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4659 ret = -EOPNOTSUPP;
4660 goto out;
4661 }
4662
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004663 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Eric Biggers9b02e492019-12-31 12:04:38 -06004664 (offset + len > inode->i_size ||
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004665 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004666 new_size = offset + len;
4667 ret = inode_newsize_ok(inode, new_size);
4668 if (ret)
4669 goto out;
4670 }
4671
Jan Kara17048e82015-12-07 14:29:17 -05004672 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004673 inode_dio_wait(inode);
4674
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004675 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004676 if (ret)
4677 goto out;
4678
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004679 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4680 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4681 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004682 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004683out:
Al Viro59551022016-01-22 15:40:57 -05004684 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004685 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4686 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004687}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004688
4689/*
Mingming Cao00314622009-09-28 15:49:08 -04004690 * This function convert a range of blocks to written extents
4691 * The caller of this function will pass the start offset and the size.
4692 * all unwritten extents within this range will be converted to
4693 * written extents.
4694 *
4695 * This function is called from the direct IO end io call back
4696 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004697 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004698 */
Jan Kara6b523df2013-06-04 13:21:11 -04004699int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4700 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004701{
Mingming Cao00314622009-09-28 15:49:08 -04004702 unsigned int max_blocks;
4703 int ret = 0;
4704 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004705 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304706 unsigned int blkbits = inode->i_blkbits;
4707 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004708
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004709 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004710 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4711
Ritesh Harjania00713e2019-10-16 13:07:08 +05304712 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004713 /*
4714 * credits to insert 1 extent into extent tree
4715 */
4716 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4717 }
Mingming Cao00314622009-09-28 15:49:08 -04004718 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004719 map.m_lblk += ret;
4720 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04004721 if (credits) {
4722 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4723 credits);
4724 if (IS_ERR(handle)) {
4725 ret = PTR_ERR(handle);
4726 break;
4727 }
Mingming Cao00314622009-09-28 15:49:08 -04004728 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004729 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004730 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05004731 if (ret <= 0)
4732 ext4_warning(inode->i_sb,
4733 "inode #%lu: block %u: len %u: "
4734 "ext4_ext_map_blocks returned %d",
4735 inode->i_ino, map.m_lblk,
4736 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004737 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04004738 if (credits)
4739 ret2 = ext4_journal_stop(handle);
4740 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04004741 break;
4742 }
4743 return ret > 0 ? ret2 : ret;
4744}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004745
Ritesh Harjania00713e2019-10-16 13:07:08 +05304746int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
4747{
4748 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304749 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304750
4751 /*
4752 * This is somewhat ugly but the idea is clear: When transaction is
4753 * reserved, everything goes into it. Otherwise we rather start several
4754 * smaller transactions for conversion of each extent separately.
4755 */
4756 if (handle) {
4757 handle = ext4_journal_start_reserved(handle,
4758 EXT4_HT_EXT_CONVERT);
4759 if (IS_ERR(handle))
4760 return PTR_ERR(handle);
4761 }
4762
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05304763 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
4764 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
4765 io_end_vec->offset,
4766 io_end_vec->size);
4767 if (ret)
4768 break;
4769 }
4770
Ritesh Harjania00713e2019-10-16 13:07:08 +05304771 if (handle)
4772 err = ext4_journal_stop(handle);
4773
4774 return ret < 0 ? ret : err;
4775}
4776
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304777static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004778{
4779 __u64 physical = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304780 __u64 length = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004781 int blockbits = inode->i_sb->s_blocksize_bits;
4782 int error = 0;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304783 u16 iomap_type;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004784
4785 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004786 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004787 struct ext4_iloc iloc;
4788 int offset; /* offset of xattr in inode */
4789
4790 error = ext4_get_inode_loc(inode, &iloc);
4791 if (error)
4792 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04004793 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004794 offset = EXT4_GOOD_OLD_INODE_SIZE +
4795 EXT4_I(inode)->i_extra_isize;
4796 physical += offset;
4797 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004798 brelse(iloc.bh);
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304799 iomap_type = IOMAP_INLINE;
4800 } else if (EXT4_I(inode)->i_file_acl) { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04004801 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004802 length = inode->i_sb->s_blocksize;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304803 iomap_type = IOMAP_MAPPED;
4804 } else {
4805 /* no in-inode or external block for xattr, so return -ENOENT */
4806 error = -ENOENT;
4807 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004808 }
4809
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304810 iomap->addr = physical;
4811 iomap->offset = 0;
4812 iomap->length = length;
4813 iomap->type = iomap_type;
4814 iomap->flags = 0;
4815out:
4816 return error;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004817}
4818
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304819static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset,
4820 loff_t length, unsigned flags,
4821 struct iomap *iomap, struct iomap *srcmap)
4822{
4823 int error;
4824
4825 error = ext4_iomap_xattr_fiemap(inode, iomap);
4826 if (error == 0 && (offset >= iomap->length))
4827 error = -ENOENT;
4828 return error;
4829}
4830
4831static const struct iomap_ops ext4_iomap_xattr_ops = {
4832 .iomap_begin = ext4_iomap_xattr_begin,
4833};
4834
4835static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4836 __u64 start, __u64 len, bool from_es_cache)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004837{
4838 ext4_lblk_t start_blk;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304839 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004840 int error = 0;
4841
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004842 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
4843 error = ext4_ext_precache(inode);
4844 if (error)
4845 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004846 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04004847 }
4848
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304849 if (from_es_cache)
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004850 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304851
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004852 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004853 return -EBADR;
4854
4855 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304856 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
4857 error = iomap_fiemap(inode, fieinfo, start, len,
4858 &ext4_iomap_xattr_ops);
4859 } else if (!from_es_cache) {
4860 error = iomap_fiemap(inode, fieinfo, start, len,
4861 &ext4_iomap_report_ops);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004862 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004863 ext4_lblk_t len_blks;
4864 __u64 last_blk;
4865
Eric Sandeen6873fa02008-10-07 00:46:36 -04004866 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004867 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004868 if (last_blk >= EXT_MAX_BLOCKS)
4869 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004870 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004871
4872 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004873 * Walk the extent tree gathering extent information
4874 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004875 */
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304876 error = ext4_fill_es_cache_info(inode, start_blk, len_blks,
4877 fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004878 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04004879 return error;
4880}
Namjae Jeon9eb79482014-02-23 15:18:59 -05004881
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004882int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4883 __u64 start, __u64 len)
4884{
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304885 return _ext4_fiemap(inode, fieinfo, start, len, false);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004886}
4887
4888int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
4889 __u64 start, __u64 len)
4890{
4891 if (ext4_has_inline_data(inode)) {
4892 int has_inline;
4893
4894 down_read(&EXT4_I(inode)->xattr_sem);
4895 has_inline = ext4_has_inline_data(inode);
4896 up_read(&EXT4_I(inode)->xattr_sem);
4897 if (has_inline)
4898 return 0;
4899 }
4900
Ritesh Harjanid3b6f232020-02-28 14:56:58 +05304901 return _ext4_fiemap(inode, fieinfo, start, len, true);
Theodore Ts'obb5835e2019-08-11 16:32:41 -04004902}
4903
4904
Namjae Jeon9eb79482014-02-23 15:18:59 -05004905/*
4906 * ext4_access_path:
4907 * Function to access the path buffer for marking it dirty.
4908 * It also checks if there are sufficient credits left in the journal handle
4909 * to update path.
4910 */
4911static int
4912ext4_access_path(handle_t *handle, struct inode *inode,
4913 struct ext4_ext_path *path)
4914{
4915 int credits, err;
4916
4917 if (!ext4_handle_valid(handle))
4918 return 0;
4919
4920 /*
4921 * Check if need to extend journal credits
4922 * 3 for leaf, sb, and inode plus 2 (bmap and group
4923 * descriptor) for each block group; assume two block
4924 * groups
4925 */
Jan Karaa4130362019-11-05 17:44:16 +01004926 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01004927 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01004928 if (err < 0)
4929 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004930
4931 err = ext4_ext_get_access(handle, inode, path);
4932 return err;
4933}
4934
4935/*
4936 * ext4_ext_shift_path_extents:
4937 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04004938 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
4939 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05004940 */
4941static int
4942ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
4943 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04004944 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05004945{
4946 int depth, err = 0;
4947 struct ext4_extent *ex_start, *ex_last;
zhengbin4756ee12019-12-25 10:45:59 +08004948 bool update = false;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004949 depth = path->p_depth;
4950
4951 while (depth >= 0) {
4952 if (depth == path->p_depth) {
4953 ex_start = path[depth].p_ext;
4954 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004955 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004956
4957 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05004958
4959 err = ext4_access_path(handle, inode, path + depth);
4960 if (err)
4961 goto out;
4962
4963 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
zhengbin4756ee12019-12-25 10:45:59 +08004964 update = true;
Namjae Jeon9eb79482014-02-23 15:18:59 -05004965
Namjae Jeon9eb79482014-02-23 15:18:59 -05004966 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04004967 if (SHIFT == SHIFT_LEFT) {
4968 le32_add_cpu(&ex_start->ee_block,
4969 -shift);
4970 /* Try to merge to the left. */
4971 if ((ex_start >
4972 EXT_FIRST_EXTENT(path[depth].p_hdr))
4973 &&
4974 ext4_ext_try_to_merge_right(inode,
4975 path, ex_start - 1))
4976 ex_last--;
4977 else
4978 ex_start++;
4979 } else {
4980 le32_add_cpu(&ex_last->ee_block, shift);
4981 ext4_ext_try_to_merge_right(inode, path,
4982 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04004983 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04004984 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05004985 }
4986 err = ext4_ext_dirty(handle, inode, path + depth);
4987 if (err)
4988 goto out;
4989
4990 if (--depth < 0 || !update)
4991 break;
4992 }
4993
4994 /* Update index too */
4995 err = ext4_access_path(handle, inode, path + depth);
4996 if (err)
4997 goto out;
4998
Namjae Jeon331573f2015-06-09 01:55:03 -04004999 if (SHIFT == SHIFT_LEFT)
5000 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5001 else
5002 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005003 err = ext4_ext_dirty(handle, inode, path + depth);
5004 if (err)
5005 goto out;
5006
5007 /* we are done if current index is not a starting index */
5008 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5009 break;
5010
5011 depth--;
5012 }
5013
5014out:
5015 return err;
5016}
5017
5018/*
5019 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005020 * All the extents which lies in the range from @start to the last allocated
5021 * block for the @inode are shifted either towards left or right (depending
5022 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005023 * On success, 0 is returned, error otherwise.
5024 */
5025static int
5026ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005027 ext4_lblk_t start, ext4_lblk_t shift,
5028 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005029{
5030 struct ext4_ext_path *path;
5031 int ret = 0, depth;
5032 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005033 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005034
5035 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005036 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5037 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005038 if (IS_ERR(path))
5039 return PTR_ERR(path);
5040
5041 depth = path->p_depth;
5042 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005043 if (!extent)
5044 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005045
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005046 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005047
Namjae Jeon331573f2015-06-09 01:55:03 -04005048 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005049 * For left shifts, make sure the hole on the left is big enough to
5050 * accommodate the shift. For right shifts, make sure the last extent
5051 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005052 */
5053 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005054 path = ext4_find_extent(inode, start - 1, &path,
5055 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005056 if (IS_ERR(path))
5057 return PTR_ERR(path);
5058 depth = path->p_depth;
5059 extent = path[depth].p_ext;
5060 if (extent) {
5061 ex_start = le32_to_cpu(extent->ee_block);
5062 ex_end = le32_to_cpu(extent->ee_block) +
5063 ext4_ext_get_actual_len(extent);
5064 } else {
5065 ex_start = 0;
5066 ex_end = 0;
5067 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005068
Namjae Jeon331573f2015-06-09 01:55:03 -04005069 if ((start == ex_start && shift > ex_start) ||
5070 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005071 ret = -EINVAL;
5072 goto out;
5073 }
5074 } else {
5075 if (shift > EXT_MAX_BLOCKS -
5076 (stop + ext4_ext_get_actual_len(extent))) {
5077 ret = -EINVAL;
5078 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005079 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005080 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005081
Namjae Jeon331573f2015-06-09 01:55:03 -04005082 /*
5083 * In case of left shift, iterator points to start and it is increased
5084 * till we reach stop. In case of right shift, iterator points to stop
5085 * and it is decreased till we reach start.
5086 */
5087 if (SHIFT == SHIFT_LEFT)
5088 iterator = &start;
5089 else
5090 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005091
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005092 /*
5093 * Its safe to start updating extents. Start and stop are unsigned, so
5094 * in case of right shift if extent with 0 block is reached, iterator
5095 * becomes NULL to indicate the end of the loop.
5096 */
5097 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005098 path = ext4_find_extent(inode, *iterator, &path,
5099 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005100 if (IS_ERR(path))
5101 return PTR_ERR(path);
5102 depth = path->p_depth;
5103 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005104 if (!extent) {
5105 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005106 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005107 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005108 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005109 if (SHIFT == SHIFT_LEFT && *iterator >
5110 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005111 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005112 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5113 path[depth].p_ext++;
5114 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005115 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005116 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005117 }
5118 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005119
5120 if (SHIFT == SHIFT_LEFT) {
5121 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5122 *iterator = le32_to_cpu(extent->ee_block) +
5123 ext4_ext_get_actual_len(extent);
5124 } else {
5125 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005126 if (le32_to_cpu(extent->ee_block) > 0)
5127 *iterator = le32_to_cpu(extent->ee_block) - 1;
5128 else
5129 /* Beginning is reached, end of the loop */
5130 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005131 /* Update path extent in case we need to stop */
5132 while (le32_to_cpu(extent->ee_block) < start)
5133 extent++;
5134 path[depth].p_ext = extent;
5135 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005136 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005137 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005138 if (ret)
5139 break;
5140 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005141out:
5142 ext4_ext_drop_refs(path);
5143 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005144 return ret;
5145}
5146
5147/*
5148 * ext4_collapse_range:
5149 * This implements the fallocate's collapse range functionality for ext4
5150 * Returns: 0 and non-zero on error.
5151 */
Eric Biggers43f81672019-12-31 12:04:40 -06005152static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005153{
5154 struct super_block *sb = inode->i_sb;
5155 ext4_lblk_t punch_start, punch_stop;
5156 handle_t *handle;
5157 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005158 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005159 int ret;
5160
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005161 /*
5162 * We need to test this early because xfstests assumes that a
5163 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5164 * system does not support collapse range.
5165 */
5166 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5167 return -EOPNOTSUPP;
5168
Eric Biggers9b02e492019-12-31 12:04:38 -06005169 /* Collapse range works only on fs cluster size aligned regions. */
5170 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005171 return -EINVAL;
5172
Namjae Jeon9eb79482014-02-23 15:18:59 -05005173 trace_ext4_collapse_range(inode, offset, len);
5174
5175 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5176 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5177
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005178 /* Call ext4_force_commit to flush all data in case of data=journal. */
5179 if (ext4_should_journal_data(inode)) {
5180 ret = ext4_force_commit(inode->i_sb);
5181 if (ret)
5182 return ret;
5183 }
5184
Al Viro59551022016-01-22 15:40:57 -05005185 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005186 /*
5187 * There is no need to overlap collapse range with EOF, in which case
5188 * it is effectively a truncate operation
5189 */
Eric Biggers9b02e492019-12-31 12:04:38 -06005190 if (offset + len >= inode->i_size) {
Lukas Czerner23fffa92014-04-12 09:56:41 -04005191 ret = -EINVAL;
5192 goto out_mutex;
5193 }
5194
Namjae Jeon9eb79482014-02-23 15:18:59 -05005195 /* Currently just for extent based files */
5196 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5197 ret = -EOPNOTSUPP;
5198 goto out_mutex;
5199 }
5200
Namjae Jeon9eb79482014-02-23 15:18:59 -05005201 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005202 inode_dio_wait(inode);
5203
Jan Karaea3d7202015-12-07 14:28:03 -05005204 /*
5205 * Prevent page faults from reinstantiating pages we have released from
5206 * page cache.
5207 */
5208 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005209
5210 ret = ext4_break_layouts(inode);
5211 if (ret)
5212 goto out_mmap;
5213
Jan Kara32ebffd2015-12-07 14:31:11 -05005214 /*
5215 * Need to round down offset to be aligned with page size boundary
5216 * for page size > block size.
5217 */
5218 ioffset = round_down(offset, PAGE_SIZE);
5219 /*
5220 * Write tail of the last page before removed range since it will get
5221 * removed from the page cache below.
5222 */
5223 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5224 if (ret)
5225 goto out_mmap;
5226 /*
5227 * Write data that will be shifted to preserve them when discarding
5228 * page cache below. We are also protected from pages becoming dirty
5229 * by i_mmap_sem.
5230 */
5231 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5232 LLONG_MAX);
5233 if (ret)
5234 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005235 truncate_pagecache(inode, ioffset);
5236
Namjae Jeon9eb79482014-02-23 15:18:59 -05005237 credits = ext4_writepage_trans_blocks(inode);
5238 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5239 if (IS_ERR(handle)) {
5240 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005241 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005242 }
5243
5244 down_write(&EXT4_I(inode)->i_data_sem);
5245 ext4_discard_preallocations(inode);
5246
5247 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005248 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005249 if (ret) {
5250 up_write(&EXT4_I(inode)->i_data_sem);
5251 goto out_stop;
5252 }
5253
5254 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5255 if (ret) {
5256 up_write(&EXT4_I(inode)->i_data_sem);
5257 goto out_stop;
5258 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005259 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005260
5261 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005262 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005263 if (ret) {
5264 up_write(&EXT4_I(inode)->i_data_sem);
5265 goto out_stop;
5266 }
5267
Eric Biggers9b02e492019-12-31 12:04:38 -06005268 new_size = inode->i_size - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005269 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005270 EXT4_I(inode)->i_disksize = new_size;
5271
Namjae Jeon9eb79482014-02-23 15:18:59 -05005272 up_write(&EXT4_I(inode)->i_data_sem);
5273 if (IS_SYNC(inode))
5274 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005275 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005276 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005277 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005278
5279out_stop:
5280 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005281out_mmap:
5282 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005283out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005284 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005285 return ret;
5286}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005287
Namjae Jeon331573f2015-06-09 01:55:03 -04005288/*
5289 * ext4_insert_range:
5290 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5291 * The data blocks starting from @offset to the EOF are shifted by @len
5292 * towards right to create a hole in the @inode. Inode size is increased
5293 * by len bytes.
5294 * Returns 0 on success, error otherwise.
5295 */
Eric Biggers43f81672019-12-31 12:04:40 -06005296static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
Namjae Jeon331573f2015-06-09 01:55:03 -04005297{
5298 struct super_block *sb = inode->i_sb;
5299 handle_t *handle;
5300 struct ext4_ext_path *path;
5301 struct ext4_extent *extent;
5302 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5303 unsigned int credits, ee_len;
5304 int ret = 0, depth, split_flag = 0;
5305 loff_t ioffset;
5306
5307 /*
5308 * We need to test this early because xfstests assumes that an
5309 * insert range of (0, 1) will return EOPNOTSUPP if the file
5310 * system does not support insert range.
5311 */
5312 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5313 return -EOPNOTSUPP;
5314
Eric Biggers9b02e492019-12-31 12:04:38 -06005315 /* Insert range works only on fs cluster size aligned regions. */
5316 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
Namjae Jeon331573f2015-06-09 01:55:03 -04005317 return -EINVAL;
5318
Namjae Jeon331573f2015-06-09 01:55:03 -04005319 trace_ext4_insert_range(inode, offset, len);
5320
5321 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5322 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5323
5324 /* Call ext4_force_commit to flush all data in case of data=journal */
5325 if (ext4_should_journal_data(inode)) {
5326 ret = ext4_force_commit(inode->i_sb);
5327 if (ret)
5328 return ret;
5329 }
5330
Al Viro59551022016-01-22 15:40:57 -05005331 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005332 /* Currently just for extent based files */
5333 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5334 ret = -EOPNOTSUPP;
5335 goto out_mutex;
5336 }
5337
Eric Biggers9b02e492019-12-31 12:04:38 -06005338 /* Check whether the maximum file size would be exceeded */
5339 if (len > inode->i_sb->s_maxbytes - inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005340 ret = -EFBIG;
5341 goto out_mutex;
5342 }
5343
Eric Biggers9b02e492019-12-31 12:04:38 -06005344 /* Offset must be less than i_size */
5345 if (offset >= inode->i_size) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005346 ret = -EINVAL;
5347 goto out_mutex;
5348 }
5349
Namjae Jeon331573f2015-06-09 01:55:03 -04005350 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005351 inode_dio_wait(inode);
5352
Jan Karaea3d7202015-12-07 14:28:03 -05005353 /*
5354 * Prevent page faults from reinstantiating pages we have released from
5355 * page cache.
5356 */
5357 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005358
5359 ret = ext4_break_layouts(inode);
5360 if (ret)
5361 goto out_mmap;
5362
Jan Kara32ebffd2015-12-07 14:31:11 -05005363 /*
5364 * Need to round down to align start offset to page size boundary
5365 * for page size > block size.
5366 */
5367 ioffset = round_down(offset, PAGE_SIZE);
5368 /* Write out all dirty pages */
5369 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5370 LLONG_MAX);
5371 if (ret)
5372 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005373 truncate_pagecache(inode, ioffset);
5374
Namjae Jeon331573f2015-06-09 01:55:03 -04005375 credits = ext4_writepage_trans_blocks(inode);
5376 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5377 if (IS_ERR(handle)) {
5378 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005379 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005380 }
5381
5382 /* Expand file to avoid data loss if there is error while shifting */
5383 inode->i_size += len;
5384 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005385 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005386 ret = ext4_mark_inode_dirty(handle, inode);
5387 if (ret)
5388 goto out_stop;
5389
5390 down_write(&EXT4_I(inode)->i_data_sem);
5391 ext4_discard_preallocations(inode);
5392
5393 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5394 if (IS_ERR(path)) {
5395 up_write(&EXT4_I(inode)->i_data_sem);
5396 goto out_stop;
5397 }
5398
5399 depth = ext_depth(inode);
5400 extent = path[depth].p_ext;
5401 if (extent) {
5402 ee_start_lblk = le32_to_cpu(extent->ee_block);
5403 ee_len = ext4_ext_get_actual_len(extent);
5404
5405 /*
5406 * If offset_lblk is not the starting block of extent, split
5407 * the extent @offset_lblk
5408 */
5409 if ((offset_lblk > ee_start_lblk) &&
5410 (offset_lblk < (ee_start_lblk + ee_len))) {
5411 if (ext4_ext_is_unwritten(extent))
5412 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5413 EXT4_EXT_MARK_UNWRIT2;
5414 ret = ext4_split_extent_at(handle, inode, &path,
5415 offset_lblk, split_flag,
5416 EXT4_EX_NOCACHE |
5417 EXT4_GET_BLOCKS_PRE_IO |
5418 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5419 }
5420
5421 ext4_ext_drop_refs(path);
5422 kfree(path);
5423 if (ret < 0) {
5424 up_write(&EXT4_I(inode)->i_data_sem);
5425 goto out_stop;
5426 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005427 } else {
5428 ext4_ext_drop_refs(path);
5429 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005430 }
5431
5432 ret = ext4_es_remove_extent(inode, offset_lblk,
5433 EXT_MAX_BLOCKS - offset_lblk);
5434 if (ret) {
5435 up_write(&EXT4_I(inode)->i_data_sem);
5436 goto out_stop;
5437 }
5438
5439 /*
5440 * if offset_lblk lies in a hole which is at start of file, use
5441 * ee_start_lblk to shift extents
5442 */
5443 ret = ext4_ext_shift_extents(inode, handle,
5444 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5445 len_lblk, SHIFT_RIGHT);
5446
5447 up_write(&EXT4_I(inode)->i_data_sem);
5448 if (IS_SYNC(inode))
5449 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005450 if (ret >= 0)
5451 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005452
5453out_stop:
5454 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005455out_mmap:
5456 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005457out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005458 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005459 return ret;
5460}
5461
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005462/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005463 * ext4_swap_extents() - Swap extents between two inodes
5464 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005465 * @inode1: First inode
5466 * @inode2: Second inode
5467 * @lblk1: Start block for first inode
5468 * @lblk2: Start block for second inode
5469 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005470 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005471 * @erp: Pointer to save error value
5472 *
5473 * This helper routine does exactly what is promise "swap extents". All other
5474 * stuff such as page-cache locking consistency, bh mapping consistency or
5475 * extent's data copying must be performed by caller.
5476 * Locking:
5477 * i_mutex is held for both inodes
5478 * i_data_sem is locked for write for both inodes
5479 * Assumptions:
5480 * All pages from requested range are locked for both inodes
5481 */
5482int
5483ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005484 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005485 ext4_lblk_t count, int unwritten, int *erp)
5486{
5487 struct ext4_ext_path *path1 = NULL;
5488 struct ext4_ext_path *path2 = NULL;
5489 int replaced_count = 0;
5490
5491 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5492 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005493 BUG_ON(!inode_is_locked(inode1));
5494 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005495
5496 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005497 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005498 return 0;
5499 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005500 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005501 return 0;
5502
5503 while (count) {
5504 struct ext4_extent *ex1, *ex2, tmp_ex;
5505 ext4_lblk_t e1_blk, e2_blk;
5506 int e1_len, e2_len, len;
5507 int split = 0;
5508
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005509 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305510 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005511 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005512 path1 = NULL;
5513 finish:
5514 count = 0;
5515 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005516 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005517 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305518 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005519 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005520 path2 = NULL;
5521 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005522 }
5523 ex1 = path1[path1->p_depth].p_ext;
5524 ex2 = path2[path2->p_depth].p_ext;
5525 /* Do we have somthing to swap ? */
5526 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005527 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005528
5529 e1_blk = le32_to_cpu(ex1->ee_block);
5530 e2_blk = le32_to_cpu(ex2->ee_block);
5531 e1_len = ext4_ext_get_actual_len(ex1);
5532 e2_len = ext4_ext_get_actual_len(ex2);
5533
5534 /* Hole handling */
5535 if (!in_range(lblk1, e1_blk, e1_len) ||
5536 !in_range(lblk2, e2_blk, e2_len)) {
5537 ext4_lblk_t next1, next2;
5538
5539 /* if hole after extent, then go to next extent */
5540 next1 = ext4_ext_next_allocated_block(path1);
5541 next2 = ext4_ext_next_allocated_block(path2);
5542 /* If hole before extent, then shift to that extent */
5543 if (e1_blk > lblk1)
5544 next1 = e1_blk;
5545 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005546 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005547 /* Do we have something to swap */
5548 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005549 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005550 /* Move to the rightest boundary */
5551 len = next1 - lblk1;
5552 if (len < next2 - lblk2)
5553 len = next2 - lblk2;
5554 if (len > count)
5555 len = count;
5556 lblk1 += len;
5557 lblk2 += len;
5558 count -= len;
5559 goto repeat;
5560 }
5561
5562 /* Prepare left boundary */
5563 if (e1_blk < lblk1) {
5564 split = 1;
5565 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005566 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005567 if (unlikely(*erp))
5568 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005569 }
5570 if (e2_blk < lblk2) {
5571 split = 1;
5572 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005573 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005574 if (unlikely(*erp))
5575 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005576 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005577 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005578 * path must to be revalidated. */
5579 if (split)
5580 goto repeat;
5581
5582 /* Prepare right boundary */
5583 len = count;
5584 if (len > e1_blk + e1_len - lblk1)
5585 len = e1_blk + e1_len - lblk1;
5586 if (len > e2_blk + e2_len - lblk2)
5587 len = e2_blk + e2_len - lblk2;
5588
5589 if (len != e1_len) {
5590 split = 1;
5591 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005592 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005593 if (unlikely(*erp))
5594 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005595 }
5596 if (len != e2_len) {
5597 split = 1;
5598 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005599 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005600 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005601 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005602 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005603 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005604 * path must to be revalidated. */
5605 if (split)
5606 goto repeat;
5607
5608 BUG_ON(e2_len != e1_len);
5609 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005610 if (unlikely(*erp))
5611 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005612 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005613 if (unlikely(*erp))
5614 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005615
5616 /* Both extents are fully inside boundaries. Swap it now */
5617 tmp_ex = *ex1;
5618 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5619 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5620 ex1->ee_len = cpu_to_le16(e2_len);
5621 ex2->ee_len = cpu_to_le16(e1_len);
5622 if (unwritten)
5623 ext4_ext_mark_unwritten(ex2);
5624 if (ext4_ext_is_unwritten(&tmp_ex))
5625 ext4_ext_mark_unwritten(ex1);
5626
5627 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5628 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5629 *erp = ext4_ext_dirty(handle, inode2, path2 +
5630 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005631 if (unlikely(*erp))
5632 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005633 *erp = ext4_ext_dirty(handle, inode1, path1 +
5634 path1->p_depth);
5635 /*
5636 * Looks scarry ah..? second inode already points to new blocks,
5637 * and it was successfully dirtied. But luckily error may happen
5638 * only due to journal error, so full transaction will be
5639 * aborted anyway.
5640 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005641 if (unlikely(*erp))
5642 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005643 lblk1 += len;
5644 lblk2 += len;
5645 replaced_count += len;
5646 count -= len;
5647
5648 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005649 ext4_ext_drop_refs(path1);
5650 kfree(path1);
5651 ext4_ext_drop_refs(path2);
5652 kfree(path2);
5653 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005654 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005655 return replaced_count;
5656}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005657
5658/*
5659 * ext4_clu_mapped - determine whether any block in a logical cluster has
5660 * been mapped to a physical cluster
5661 *
5662 * @inode - file containing the logical cluster
5663 * @lclu - logical cluster of interest
5664 *
5665 * Returns 1 if any block in the logical cluster is mapped, signifying
5666 * that a physical cluster has been allocated for it. Otherwise,
5667 * returns 0. Can also return negative error codes. Derived from
5668 * ext4_ext_map_blocks().
5669 */
5670int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5671{
5672 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5673 struct ext4_ext_path *path;
5674 int depth, mapped = 0, err = 0;
5675 struct ext4_extent *extent;
5676 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5677
5678 /* search for the extent closest to the first block in the cluster */
5679 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5680 if (IS_ERR(path)) {
5681 err = PTR_ERR(path);
5682 path = NULL;
5683 goto out;
5684 }
5685
5686 depth = ext_depth(inode);
5687
5688 /*
5689 * A consistent leaf must not be empty. This situation is possible,
5690 * though, _during_ tree modification, and it's why an assert can't
5691 * be put in ext4_find_extent().
5692 */
5693 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
5694 EXT4_ERROR_INODE(inode,
5695 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
5696 (unsigned long) EXT4_C2B(sbi, lclu),
5697 depth, path[depth].p_block);
5698 err = -EFSCORRUPTED;
5699 goto out;
5700 }
5701
5702 extent = path[depth].p_ext;
5703
5704 /* can't be mapped if the extent tree is empty */
5705 if (extent == NULL)
5706 goto out;
5707
5708 first_lblk = le32_to_cpu(extent->ee_block);
5709 first_lclu = EXT4_B2C(sbi, first_lblk);
5710
5711 /*
5712 * Three possible outcomes at this point - found extent spanning
5713 * the target cluster, to the left of the target cluster, or to the
5714 * right of the target cluster. The first two cases are handled here.
5715 * The last case indicates the target cluster is not mapped.
5716 */
5717 if (lclu >= first_lclu) {
5718 last_lclu = EXT4_B2C(sbi, first_lblk +
5719 ext4_ext_get_actual_len(extent) - 1);
5720 if (lclu <= last_lclu) {
5721 mapped = 1;
5722 } else {
5723 first_lblk = ext4_ext_next_allocated_block(path);
5724 first_lclu = EXT4_B2C(sbi, first_lblk);
5725 if (lclu == first_lclu)
5726 mapped = 1;
5727 }
5728 }
5729
5730out:
5731 ext4_ext_drop_refs(path);
5732 kfree(path);
5733
5734 return err ? err : mapped;
5735}