blob: ee83fe7c98aaf5bbb3267e1d67004a6d0491984f [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>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040031#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050032#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050033#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070034
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040035#include <trace/events/ext4.h>
36
Lukas Czerner5f95d212012-03-19 23:03:19 -040037/*
38 * used by extent splitting.
39 */
40#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
41 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040042#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
43#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040044
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040045#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
46#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
47
Darrick J. Wong7ac59902012-04-29 18:37:10 -040048static __le32 ext4_extent_block_csum(struct inode *inode,
49 struct ext4_extent_header *eh)
50{
51 struct ext4_inode_info *ei = EXT4_I(inode);
52 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
53 __u32 csum;
54
55 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
56 EXT4_EXTENT_TAIL_OFFSET(eh));
57 return cpu_to_le32(csum);
58}
59
60static int ext4_extent_block_csum_verify(struct inode *inode,
61 struct ext4_extent_header *eh)
62{
63 struct ext4_extent_tail *et;
64
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040065 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040066 return 1;
67
68 et = find_ext4_extent_tail(eh);
69 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
70 return 0;
71 return 1;
72}
73
74static void ext4_extent_block_csum_set(struct inode *inode,
75 struct ext4_extent_header *eh)
76{
77 struct ext4_extent_tail *et;
78
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040079 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040080 return;
81
82 et = find_ext4_extent_tail(eh);
83 et->et_checksum = ext4_extent_block_csum(inode, eh);
84}
85
Allison Hendersond583fb82011-05-25 07:41:43 -040086static int ext4_split_extent(handle_t *handle,
87 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040088 struct ext4_ext_path **ppath,
Allison Hendersond583fb82011-05-25 07:41:43 -040089 struct ext4_map_blocks *map,
90 int split_flag,
91 int flags);
92
Lukas Czerner5f95d212012-03-19 23:03:19 -040093static int ext4_split_extent_at(handle_t *handle,
94 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040095 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -040096 ext4_lblk_t split,
97 int split_flag,
98 int flags);
99
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500100static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -0500101 struct extent_status *newes);
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500102
Jan Karaa4130362019-11-05 17:44:16 +0100103static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
Alex Tomasa86c6182006-10-11 01:21:03 -0700104{
Theodore Ts'o7b808192016-04-25 23:13:17 -0400105 /*
Jan Karaa4130362019-11-05 17:44:16 +0100106 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
107 * moment, get_block can be called only for blocks inside i_size since
108 * page cache has been already dropped and writes are blocked by
109 * i_mutex. So we can safely drop the i_data_sem here.
Theodore Ts'o7b808192016-04-25 23:13:17 -0400110 */
Jan Karaa4130362019-11-05 17:44:16 +0100111 BUG_ON(EXT4_JOURNAL(inode) == NULL);
112 ext4_discard_preallocations(inode);
113 up_write(&EXT4_I(inode)->i_data_sem);
114 *dropped = 1;
115 return 0;
116}
Jan Kara487caee2009-08-17 22:17:20 -0400117
Jan Karaa4130362019-11-05 17:44:16 +0100118/*
119 * Make sure 'handle' has at least 'check_cred' credits. If not, restart
120 * transaction with 'restart_cred' credits. The function drops i_data_sem
121 * when restarting transaction and gets it after transaction is restarted.
122 *
123 * The function returns 0 on success, 1 if transaction had to be restarted,
124 * and < 0 in case of fatal error.
125 */
126int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
Jan Kara83448bd2019-11-05 17:44:29 +0100127 int check_cred, int restart_cred,
128 int revoke_cred)
Jan Karaa4130362019-11-05 17:44:16 +0100129{
130 int ret;
131 int dropped = 0;
132
133 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
Jan Kara83448bd2019-11-05 17:44:29 +0100134 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
Jan Karaa4130362019-11-05 17:44:16 +0100135 if (dropped)
136 down_write(&EXT4_I(inode)->i_data_sem);
137 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700138}
139
140/*
141 * could return:
142 * - EROFS
143 * - ENOMEM
144 */
145static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
146 struct ext4_ext_path *path)
147{
148 if (path->p_bh) {
149 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400150 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700151 return ext4_journal_get_write_access(handle, path->p_bh);
152 }
153 /* path points to leaf/index in inode body */
154 /* we use in-core data, no need to protect them */
155 return 0;
156}
157
158/*
159 * could return:
160 * - EROFS
161 * - ENOMEM
162 * - EIO
163 */
Darrick J. Wong26564972013-04-19 14:04:12 -0400164int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
165 struct inode *inode, struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700166{
167 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400168
169 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700170 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400171 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700172 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400173 err = __ext4_handle_dirty_metadata(where, line, handle,
174 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700175 } else {
176 /* path points to leaf/index in inode body */
177 err = ext4_mark_inode_dirty(handle, inode);
178 }
179 return err;
180}
181
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700182static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500184 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700185{
Alex Tomasa86c6182006-10-11 01:21:03 -0700186 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400187 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700188 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700189
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500190 /*
191 * Try to predict block placement assuming that we are
192 * filling in a file which will eventually be
193 * non-sparse --- i.e., in the case of libbfd writing
194 * an ELF object sections out-of-order but in a way
195 * the eventually results in a contiguous object or
196 * executable file, or some database extending a table
197 * space file. However, this is actually somewhat
198 * non-ideal if we are writing a sparse file such as
199 * qemu or KVM writing a raw image file that is going
200 * to stay fairly sparse, since it will end up
201 * fragmenting the file system's free space. Maybe we
202 * should have some hueristics or some way to allow
203 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800204 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500205 * common.
206 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800207 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500208 if (ex) {
209 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
210 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
211
212 if (block > ext_block)
213 return ext_pblk + (block - ext_block);
214 else
215 return ext_pblk - (ext_block - block);
216 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700217
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700218 /* it looks like index is empty;
219 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700220 if (path[depth].p_bh)
221 return path[depth].p_bh->b_blocknr;
222 }
223
224 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400225 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700226}
227
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400228/*
229 * Allocation for a meta data block
230 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700231static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400232ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700233 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400234 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700235{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700236 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700237
238 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400239 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
240 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700241 return newblock;
242}
243
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400244static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700245{
246 int size;
247
248 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
249 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100250#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400251 if (!check && size > 6)
252 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700253#endif
254 return size;
255}
256
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400257static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700258{
259 int size;
260
261 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
262 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100263#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400264 if (!check && size > 5)
265 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700266#endif
267 return size;
268}
269
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400270static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700271{
272 int size;
273
274 size = sizeof(EXT4_I(inode)->i_data);
275 size -= sizeof(struct ext4_extent_header);
276 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100277#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400278 if (!check && size > 3)
279 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700280#endif
281 return size;
282}
283
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400284static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700285{
286 int size;
287
288 size = sizeof(EXT4_I(inode)->i_data);
289 size -= sizeof(struct ext4_extent_header);
290 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100291#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400292 if (!check && size > 4)
293 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700294#endif
295 return size;
296}
297
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400298static inline int
299ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400300 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400301 int nofail)
302{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400303 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400304 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
305
Theodore Ts'odfe50802014-09-01 14:37:09 -0400306 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400307 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
308 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
309 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
310}
311
Mingming Caod2a17632008-07-14 17:52:37 -0400312/*
313 * Calculate the number of metadata blocks needed
314 * to allocate @blocks
315 * Worse case is one block per extent
316 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500317int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400318{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500319 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400320 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400321
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500322 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
323 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400324
325 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500326 * If the new delayed allocation block is contiguous with the
327 * previous da block, it can share index blocks with the
328 * previous block, so we only need to allocate a new index
329 * block every idxs leaf blocks. At ldxs**2 blocks, we need
330 * an additional index block, and at ldxs**3 blocks, yet
331 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400332 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500333 if (ei->i_da_metadata_calc_len &&
334 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400335 int num = 0;
336
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500337 if ((ei->i_da_metadata_calc_len % idxs) == 0)
338 num++;
339 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
340 num++;
341 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
342 num++;
343 ei->i_da_metadata_calc_len = 0;
344 } else
345 ei->i_da_metadata_calc_len++;
346 ei->i_da_metadata_calc_last_lblock++;
347 return num;
348 }
Mingming Caod2a17632008-07-14 17:52:37 -0400349
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500350 /*
351 * In the worst case we need a new set of index blocks at
352 * every level of the inode's extent tree.
353 */
354 ei->i_da_metadata_calc_len = 1;
355 ei->i_da_metadata_calc_last_lblock = lblock;
356 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400357}
358
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400359static int
360ext4_ext_max_entries(struct inode *inode, int depth)
361{
362 int max;
363
364 if (depth == ext_depth(inode)) {
365 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400366 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400367 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400368 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400369 } else {
370 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400371 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400372 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400373 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400374 }
375
376 return max;
377}
378
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400379static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
380{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400381 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400382 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500383 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400384
Vegard Nossumf70749c2016-06-30 11:53:46 -0400385 /*
386 * We allow neither:
387 * - zero length
388 * - overflow/wrap-around
389 */
390 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400391 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400392 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400393}
394
395static int ext4_valid_extent_idx(struct inode *inode,
396 struct ext4_extent_idx *ext_idx)
397{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400398 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400399
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400400 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400401}
402
403static int ext4_valid_extent_entries(struct inode *inode,
404 struct ext4_extent_header *eh,
405 int depth)
406{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400407 unsigned short entries;
408 if (eh->eh_entries == 0)
409 return 1;
410
411 entries = le16_to_cpu(eh->eh_entries);
412
413 if (depth == 0) {
414 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400415 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500416 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
417 ext4_fsblk_t pblock = 0;
418 ext4_lblk_t lblock = 0;
419 ext4_lblk_t prev = 0;
420 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400421 while (entries) {
422 if (!ext4_valid_extent(inode, ext))
423 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500424
425 /* Check for overlapping extents */
426 lblock = le32_to_cpu(ext->ee_block);
427 len = ext4_ext_get_actual_len(ext);
428 if ((lblock <= prev) && prev) {
429 pblock = ext4_ext_pblock(ext);
430 es->s_last_error_block = cpu_to_le64(pblock);
431 return 0;
432 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400433 ext++;
434 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500435 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400436 }
437 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400438 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400439 while (entries) {
440 if (!ext4_valid_extent_idx(inode, ext_idx))
441 return 0;
442 ext_idx++;
443 entries--;
444 }
445 }
446 return 1;
447}
448
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400449static int __ext4_ext_check(const char *function, unsigned int line,
450 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400451 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400452{
453 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400454 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400455
456 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
457 error_msg = "invalid magic";
458 goto corrupted;
459 }
460 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
461 error_msg = "unexpected eh_depth";
462 goto corrupted;
463 }
464 if (unlikely(eh->eh_max == 0)) {
465 error_msg = "invalid eh_max";
466 goto corrupted;
467 }
468 max = ext4_ext_max_entries(inode, depth);
469 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
470 error_msg = "too large eh_max";
471 goto corrupted;
472 }
473 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
474 error_msg = "invalid eh_entries";
475 goto corrupted;
476 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400477 if (!ext4_valid_extent_entries(inode, eh, depth)) {
478 error_msg = "invalid extent entries";
479 goto corrupted;
480 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400481 if (unlikely(depth > 32)) {
482 error_msg = "too large eh_depth";
483 goto corrupted;
484 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400485 /* Verify checksum on non-root extent tree nodes */
486 if (ext_depth(inode) != depth &&
487 !ext4_extent_block_csum_verify(inode, eh)) {
488 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400489 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400490 goto corrupted;
491 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400492 return 0;
493
494corrupted:
Theodore Ts'o878520a2019-11-19 21:54:15 -0500495 ext4_set_errno(inode->i_sb, -err);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400496 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400497 "pblk %llu bad header/extent: %s - magic %x, "
498 "entries %u, max %u(%u), depth %u(%u)",
499 (unsigned long long) pblk, error_msg,
500 le16_to_cpu(eh->eh_magic),
501 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
502 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400503 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400504}
505
Theodore Ts'oc3491792013-08-16 21:21:41 -0400506#define ext4_ext_check(inode, eh, depth, pblk) \
507 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400508
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400509int ext4_ext_check_inode(struct inode *inode)
510{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400511 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400512}
513
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400514static struct buffer_head *
515__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400516 struct inode *inode, ext4_fsblk_t pblk, int depth,
517 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400518{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400519 struct buffer_head *bh;
520 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400521
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400522 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400523 if (unlikely(!bh))
524 return ERR_PTR(-ENOMEM);
525
526 if (!bh_uptodate_or_lock(bh)) {
527 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
528 err = bh_submit_read(bh);
529 if (err < 0)
530 goto errout;
531 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400532 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400533 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400534 if (!ext4_has_feature_journal(inode->i_sb) ||
535 (inode->i_ino !=
536 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
537 err = __ext4_ext_check(function, line, inode,
538 ext_block_hdr(bh), depth, pblk);
539 if (err)
540 goto errout;
541 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400542 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400543 /*
544 * If this is a leaf block, cache all of its entries
545 */
546 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
547 struct ext4_extent_header *eh = ext_block_hdr(bh);
548 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
549 ext4_lblk_t prev = 0;
550 int i;
551
552 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
553 unsigned int status = EXTENT_STATUS_WRITTEN;
554 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
555 int len = ext4_ext_get_actual_len(ex);
556
557 if (prev && (prev != lblk))
558 ext4_es_cache_extent(inode, prev,
559 lblk - prev, ~0,
560 EXTENT_STATUS_HOLE);
561
Lukas Czerner556615d2014-04-20 23:45:47 -0400562 if (ext4_ext_is_unwritten(ex))
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400563 status = EXTENT_STATUS_UNWRITTEN;
564 ext4_es_cache_extent(inode, lblk, len,
565 ext4_ext_pblock(ex), status);
566 prev = lblk + len;
567 }
568 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400569 return bh;
570errout:
571 put_bh(bh);
572 return ERR_PTR(err);
573
Darrick J. Wongf8489122012-04-29 18:21:10 -0400574}
575
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400576#define read_extent_tree_block(inode, pblk, depth, flags) \
577 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
578 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400579
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400580/*
581 * This function is called to cache a file's extent information in the
582 * extent status tree
583 */
584int ext4_ext_precache(struct inode *inode)
585{
586 struct ext4_inode_info *ei = EXT4_I(inode);
587 struct ext4_ext_path *path = NULL;
588 struct buffer_head *bh;
589 int i = 0, depth, ret = 0;
590
591 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
592 return 0; /* not an extent-mapped inode */
593
594 down_read(&ei->i_data_sem);
595 depth = ext_depth(inode);
596
Kees Cook6396bb22018-06-12 14:03:40 -0700597 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400598 GFP_NOFS);
599 if (path == NULL) {
600 up_read(&ei->i_data_sem);
601 return -ENOMEM;
602 }
603
604 /* Don't cache anything if there are no external extent blocks */
605 if (depth == 0)
606 goto out;
607 path[0].p_hdr = ext_inode_hdr(inode);
608 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
609 if (ret)
610 goto out;
611 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
612 while (i >= 0) {
613 /*
614 * If this is a leaf block or we've reached the end of
615 * the index block, go up
616 */
617 if ((i == depth) ||
618 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
619 brelse(path[i].p_bh);
620 path[i].p_bh = NULL;
621 i--;
622 continue;
623 }
624 bh = read_extent_tree_block(inode,
625 ext4_idx_pblock(path[i].p_idx++),
626 depth - i - 1,
627 EXT4_EX_FORCE_CACHE);
628 if (IS_ERR(bh)) {
629 ret = PTR_ERR(bh);
630 break;
631 }
632 i++;
633 path[i].p_bh = bh;
634 path[i].p_hdr = ext_block_hdr(bh);
635 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
636 }
637 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
638out:
639 up_read(&ei->i_data_sem);
640 ext4_ext_drop_refs(path);
641 kfree(path);
642 return ret;
643}
644
Alex Tomasa86c6182006-10-11 01:21:03 -0700645#ifdef EXT_DEBUG
646static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
647{
648 int k, l = path->p_depth;
649
650 ext_debug("path:");
651 for (k = 0; k <= l; k++, path++) {
652 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700653 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400654 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700655 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400656 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700657 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400658 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400659 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400660 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700661 } else
662 ext_debug(" []");
663 }
664 ext_debug("\n");
665}
666
667static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
668{
669 int depth = ext_depth(inode);
670 struct ext4_extent_header *eh;
671 struct ext4_extent *ex;
672 int i;
673
674 if (!path)
675 return;
676
677 eh = path[depth].p_hdr;
678 ex = EXT_FIRST_EXTENT(eh);
679
Mingming553f9002009-09-18 13:34:55 -0400680 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
681
Alex Tomasa86c6182006-10-11 01:21:03 -0700682 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400683 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400684 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400685 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 }
687 ext_debug("\n");
688}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400689
690static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
691 ext4_fsblk_t newblock, int level)
692{
693 int depth = ext_depth(inode);
694 struct ext4_extent *ex;
695
696 if (depth != level) {
697 struct ext4_extent_idx *idx;
698 idx = path[level].p_idx;
699 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
700 ext_debug("%d: move %d:%llu in new index %llu\n", level,
701 le32_to_cpu(idx->ei_block),
702 ext4_idx_pblock(idx),
703 newblock);
704 idx++;
705 }
706
707 return;
708 }
709
710 ex = path[depth].p_ext;
711 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
712 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
713 le32_to_cpu(ex->ee_block),
714 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400715 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400716 ext4_ext_get_actual_len(ex),
717 newblock);
718 ex++;
719 }
720}
721
Alex Tomasa86c6182006-10-11 01:21:03 -0700722#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400723#define ext4_ext_show_path(inode, path)
724#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400725#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700726#endif
727
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500728void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700729{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400730 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700731
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400732 if (!path)
733 return;
734 depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700735 for (i = 0; i <= depth; i++, path++)
736 if (path->p_bh) {
737 brelse(path->p_bh);
738 path->p_bh = NULL;
739 }
740}
741
742/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700743 * ext4_ext_binsearch_idx:
744 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400745 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700746 */
747static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500748ext4_ext_binsearch_idx(struct inode *inode,
749 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700750{
751 struct ext4_extent_header *eh = path->p_hdr;
752 struct ext4_extent_idx *r, *l, *m;
753
Alex Tomasa86c6182006-10-11 01:21:03 -0700754
Eric Sandeenbba90742008-01-28 23:58:27 -0500755 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700756
757 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400758 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700759 while (l <= r) {
760 m = l + (r - l) / 2;
761 if (block < le32_to_cpu(m->ei_block))
762 r = m - 1;
763 else
764 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400765 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
766 m, le32_to_cpu(m->ei_block),
767 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700768 }
769
770 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400771 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400772 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700773
774#ifdef CHECK_BINSEARCH
775 {
776 struct ext4_extent_idx *chix, *ix;
777 int k;
778
779 chix = ix = EXT_FIRST_INDEX(eh);
780 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
781 if (k != 0 &&
782 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400783 printk(KERN_DEBUG "k=%d, ix=0x%p, "
784 "first=0x%p\n", k,
785 ix, EXT_FIRST_INDEX(eh));
786 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700787 le32_to_cpu(ix->ei_block),
788 le32_to_cpu(ix[-1].ei_block));
789 }
790 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400791 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700792 if (block < le32_to_cpu(ix->ei_block))
793 break;
794 chix = ix;
795 }
796 BUG_ON(chix != path->p_idx);
797 }
798#endif
799
800}
801
802/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700803 * ext4_ext_binsearch:
804 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400805 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700806 */
807static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500808ext4_ext_binsearch(struct inode *inode,
809 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700810{
811 struct ext4_extent_header *eh = path->p_hdr;
812 struct ext4_extent *r, *l, *m;
813
Alex Tomasa86c6182006-10-11 01:21:03 -0700814 if (eh->eh_entries == 0) {
815 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700816 * this leaf is empty:
817 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700818 */
819 return;
820 }
821
Eric Sandeenbba90742008-01-28 23:58:27 -0500822 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700823
824 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400825 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700826
827 while (l <= r) {
828 m = l + (r - l) / 2;
829 if (block < le32_to_cpu(m->ee_block))
830 r = m - 1;
831 else
832 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400833 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
834 m, le32_to_cpu(m->ee_block),
835 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700836 }
837
838 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400839 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400840 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400841 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400842 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400843 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700844
845#ifdef CHECK_BINSEARCH
846 {
847 struct ext4_extent *chex, *ex;
848 int k;
849
850 chex = ex = EXT_FIRST_EXTENT(eh);
851 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
852 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400853 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700854 if (block < le32_to_cpu(ex->ee_block))
855 break;
856 chex = ex;
857 }
858 BUG_ON(chex != path->p_ext);
859 }
860#endif
861
862}
863
864int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
865{
866 struct ext4_extent_header *eh;
867
868 eh = ext_inode_hdr(inode);
869 eh->eh_depth = 0;
870 eh->eh_entries = 0;
871 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400872 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700874 return 0;
875}
876
877struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400878ext4_find_extent(struct inode *inode, ext4_lblk_t block,
879 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700880{
881 struct ext4_extent_header *eh;
882 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400883 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
884 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500885 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700886
887 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400888 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400889 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
890 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
891 depth);
892 ret = -EFSCORRUPTED;
893 goto err;
894 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700895
Theodore Ts'o10809df82014-09-01 14:40:09 -0400896 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400897 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400898 if (depth > path[0].p_maxdepth) {
899 kfree(path);
900 *orig_path = path = NULL;
901 }
902 }
903 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400904 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700905 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400907 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700908 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400909 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700911 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400912 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700913
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400914 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 /* walk through the tree */
916 while (i) {
917 ext_debug("depth %d: num %d, max %d\n",
918 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400919
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400921 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 path[ppos].p_depth = i;
923 path[ppos].p_ext = NULL;
924
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400925 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
926 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530927 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400928 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700929 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500930 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400931
Alex Tomasa86c6182006-10-11 01:21:03 -0700932 eh = ext_block_hdr(bh);
933 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700934 path[ppos].p_bh = bh;
935 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700936 }
937
938 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700939 path[ppos].p_ext = NULL;
940 path[ppos].p_idx = NULL;
941
Alex Tomasa86c6182006-10-11 01:21:03 -0700942 /* find extent */
943 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400944 /* if not an empty leaf */
945 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400946 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700947
948 ext4_ext_show_path(inode, path);
949
950 return path;
951
952err:
953 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400954 kfree(path);
955 if (orig_path)
956 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500957 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700958}
959
960/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700961 * ext4_ext_insert_index:
962 * insert new index [@logical;@ptr] into the block at @curp;
963 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700964 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400965static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
966 struct ext4_ext_path *curp,
967 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700968{
969 struct ext4_extent_idx *ix;
970 int len, err;
971
Avantika Mathur7e028972006-12-06 20:41:33 -0800972 err = ext4_ext_get_access(handle, inode, curp);
973 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700974 return err;
975
Frank Mayhar273df552010-03-02 11:46:09 -0500976 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
977 EXT4_ERROR_INODE(inode,
978 "logical %d == ei_block %d!",
979 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400980 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500981 }
Robin Dongd4620312011-07-17 23:43:42 -0400982
983 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
984 >= le16_to_cpu(curp->p_hdr->eh_max))) {
985 EXT4_ERROR_INODE(inode,
986 "eh_entries %d >= eh_max %d!",
987 le16_to_cpu(curp->p_hdr->eh_entries),
988 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400989 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400990 }
991
Alex Tomasa86c6182006-10-11 01:21:03 -0700992 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
993 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400994 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700995 ix = curp->p_idx + 1;
996 } else {
997 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400998 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700999 ix = curp->p_idx;
1000 }
1001
Eric Gouriou80e675f2011-10-27 11:52:18 -04001002 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
1003 BUG_ON(len < 0);
1004 if (len > 0) {
1005 ext_debug("insert new index %d: "
1006 "move %d indices from 0x%p to 0x%p\n",
1007 logical, len, ix, ix + 1);
1008 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
1009 }
1010
Tao Maf472e022011-10-17 10:13:46 -04001011 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1012 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001013 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -04001014 }
1015
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001017 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001018 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001019
Frank Mayhar273df552010-03-02 11:46:09 -05001020 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1021 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001022 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001023 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001024
1025 err = ext4_ext_dirty(handle, inode, curp);
1026 ext4_std_error(inode->i_sb, err);
1027
1028 return err;
1029}
1030
1031/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001032 * ext4_ext_split:
1033 * inserts new subtree into the path, using free index entry
1034 * at depth @at:
1035 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1036 * - makes decision where to split
1037 * - moves remaining extents and index entries (right to the split point)
1038 * into the newly allocated blocks
1039 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 */
1041static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001042 unsigned int flags,
1043 struct ext4_ext_path *path,
1044 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001045{
1046 struct buffer_head *bh = NULL;
1047 int depth = ext_depth(inode);
1048 struct ext4_extent_header *neh;
1049 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001051 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001053 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001054 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001055 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001056
1057 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001058 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001059
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001060 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001061 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001062 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1063 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001064 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001065 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001066 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1067 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001068 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001069 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001070 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001071 } else {
1072 border = newext->ee_block;
1073 ext_debug("leaf will be added."
1074 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001075 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 }
1077
1078 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001079 * If error occurs, then we break processing
1080 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001081 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001082 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001083 */
1084
1085 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001086 * Get array to track all allocated blocks.
1087 * We need this to handle errors and free blocks
1088 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001089 */
Kees Cook6396bb22018-06-12 14:03:40 -07001090 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001091 if (!ablocks)
1092 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001093
1094 /* allocate all needed blocks */
1095 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1096 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001097 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001098 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001099 if (newblock == 0)
1100 goto cleanup;
1101 ablocks[a] = newblock;
1102 }
1103
1104 /* initialize new leaf */
1105 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001106 if (unlikely(newblock == 0)) {
1107 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001108 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001109 goto cleanup;
1110 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001111 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001112 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001113 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001114 goto cleanup;
1115 }
1116 lock_buffer(bh);
1117
Avantika Mathur7e028972006-12-06 20:41:33 -08001118 err = ext4_journal_get_create_access(handle, bh);
1119 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001120 goto cleanup;
1121
1122 neh = ext_block_hdr(bh);
1123 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001124 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001125 neh->eh_magic = EXT4_EXT_MAGIC;
1126 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001127
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001128 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001129 if (unlikely(path[depth].p_hdr->eh_entries !=
1130 path[depth].p_hdr->eh_max)) {
1131 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1132 path[depth].p_hdr->eh_entries,
1133 path[depth].p_hdr->eh_max);
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 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001138 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1139 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001140 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001141 struct ext4_extent *ex;
1142 ex = EXT_FIRST_EXTENT(neh);
1143 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001144 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001145 }
1146
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001147 /* zero out unused area in the extent block */
1148 ext_size = sizeof(struct ext4_extent_header) +
1149 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1150 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001151 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 set_buffer_uptodate(bh);
1153 unlock_buffer(bh);
1154
Frank Mayhar03901312009-01-07 00:06:22 -05001155 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001156 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001157 goto cleanup;
1158 brelse(bh);
1159 bh = NULL;
1160
1161 /* correct old leaf */
1162 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001163 err = ext4_ext_get_access(handle, inode, path + depth);
1164 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001165 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001166 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001167 err = ext4_ext_dirty(handle, inode, path + depth);
1168 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001169 goto cleanup;
1170
1171 }
1172
1173 /* create intermediate indexes */
1174 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001175 if (unlikely(k < 0)) {
1176 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001177 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001178 goto cleanup;
1179 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001180 if (k)
1181 ext_debug("create %d intermediate indices\n", k);
1182 /* insert new index into current index block */
1183 /* current depth stored in i var */
1184 i = depth - 1;
1185 while (k--) {
1186 oldblock = newblock;
1187 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001188 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001189 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001190 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001191 goto cleanup;
1192 }
1193 lock_buffer(bh);
1194
Avantika Mathur7e028972006-12-06 20:41:33 -08001195 err = ext4_journal_get_create_access(handle, bh);
1196 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001197 goto cleanup;
1198
1199 neh = ext_block_hdr(bh);
1200 neh->eh_entries = cpu_to_le16(1);
1201 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001202 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001203 neh->eh_depth = cpu_to_le16(depth - i);
1204 fidx = EXT_FIRST_INDEX(neh);
1205 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001206 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001207
Eric Sandeenbba90742008-01-28 23:58:27 -05001208 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1209 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001210
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001211 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001212 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1213 EXT_LAST_INDEX(path[i].p_hdr))) {
1214 EXT4_ERROR_INODE(inode,
1215 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1216 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001217 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001218 goto cleanup;
1219 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001220 /* start copy indexes */
1221 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1222 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1223 EXT_MAX_INDEX(path[i].p_hdr));
1224 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001225 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001226 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001227 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001228 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001229 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001230 /* zero out unused area in the extent block */
1231 ext_size = sizeof(struct ext4_extent_header) +
1232 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1233 memset(bh->b_data + ext_size, 0,
1234 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001235 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001236 set_buffer_uptodate(bh);
1237 unlock_buffer(bh);
1238
Frank Mayhar03901312009-01-07 00:06:22 -05001239 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001240 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001241 goto cleanup;
1242 brelse(bh);
1243 bh = NULL;
1244
1245 /* correct old index */
1246 if (m) {
1247 err = ext4_ext_get_access(handle, inode, path + i);
1248 if (err)
1249 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001250 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001251 err = ext4_ext_dirty(handle, inode, path + i);
1252 if (err)
1253 goto cleanup;
1254 }
1255
1256 i--;
1257 }
1258
1259 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001260 err = ext4_ext_insert_index(handle, inode, path + at,
1261 le32_to_cpu(border), newblock);
1262
1263cleanup:
1264 if (bh) {
1265 if (buffer_locked(bh))
1266 unlock_buffer(bh);
1267 brelse(bh);
1268 }
1269
1270 if (err) {
1271 /* free all allocated blocks in error case */
1272 for (i = 0; i < depth; i++) {
1273 if (!ablocks[i])
1274 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001275 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001276 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001277 }
1278 }
1279 kfree(ablocks);
1280
1281 return err;
1282}
1283
1284/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001285 * ext4_ext_grow_indepth:
1286 * implements tree growing procedure:
1287 * - allocates new block
1288 * - moves top-level data (index block or leaf) into the new block
1289 * - initializes new top-level, creating index that points to the
1290 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001291 */
1292static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001293 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001294{
Alex Tomasa86c6182006-10-11 01:21:03 -07001295 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001296 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001297 ext4_fsblk_t newblock, goal = 0;
1298 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001299 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001300 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001301
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001302 /* Try to prepend new index to old one */
1303 if (ext_depth(inode))
1304 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1305 if (goal > le32_to_cpu(es->s_first_data_block)) {
1306 flags |= EXT4_MB_HINT_TRY_GOAL;
1307 goal--;
1308 } else
1309 goal = ext4_inode_to_goal_block(inode);
1310 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1311 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001312 if (newblock == 0)
1313 return err;
1314
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001315 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001316 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001317 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001318 lock_buffer(bh);
1319
Avantika Mathur7e028972006-12-06 20:41:33 -08001320 err = ext4_journal_get_create_access(handle, bh);
1321 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001322 unlock_buffer(bh);
1323 goto out;
1324 }
1325
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001326 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001327 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001328 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1329 /* zero out unused area in the extent block */
1330 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001331
1332 /* set size of new block */
1333 neh = ext_block_hdr(bh);
1334 /* old root could have indexes or leaves
1335 * so calculate e_max right way */
1336 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001337 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001338 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001339 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001340 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001341 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001342 set_buffer_uptodate(bh);
1343 unlock_buffer(bh);
1344
Frank Mayhar03901312009-01-07 00:06:22 -05001345 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001346 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001347 goto out;
1348
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001349 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001350 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001351 neh->eh_entries = cpu_to_le16(1);
1352 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1353 if (neh->eh_depth == 0) {
1354 /* Root extent block becomes index block */
1355 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1356 EXT_FIRST_INDEX(neh)->ei_block =
1357 EXT_FIRST_EXTENT(neh)->ee_block;
1358 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001359 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001360 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001361 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001362 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001363
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001364 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001365 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001366out:
1367 brelse(bh);
1368
1369 return err;
1370}
1371
1372/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001373 * ext4_ext_create_new_leaf:
1374 * finds empty index and adds new leaf.
1375 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001376 */
1377static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001378 unsigned int mb_flags,
1379 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001380 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001381 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001382{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001383 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001384 struct ext4_ext_path *curp;
1385 int depth, i, err = 0;
1386
1387repeat:
1388 i = depth = ext_depth(inode);
1389
1390 /* walk up to the tree and look for free index entry */
1391 curp = path + depth;
1392 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1393 i--;
1394 curp--;
1395 }
1396
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001397 /* we use already allocated block for index block,
1398 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001399 if (EXT_HAS_FREE_INDEX(curp)) {
1400 /* if we found index with free entry, then use that
1401 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001402 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001403 if (err)
1404 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001405
1406 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001407 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001408 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001409 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001410 if (IS_ERR(path))
1411 err = PTR_ERR(path);
1412 } else {
1413 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001414 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001415 if (err)
1416 goto out;
1417
1418 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001419 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001420 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001421 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001422 if (IS_ERR(path)) {
1423 err = PTR_ERR(path);
1424 goto out;
1425 }
1426
1427 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001428 * only first (depth 0 -> 1) produces free space;
1429 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001430 */
1431 depth = ext_depth(inode);
1432 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001433 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001434 goto repeat;
1435 }
1436 }
1437
1438out:
1439 return err;
1440}
1441
1442/*
Alex Tomas1988b512008-01-28 23:58:27 -05001443 * search the closest allocated block to the left for *logical
1444 * and returns it at @logical + it's physical address at @phys
1445 * if *logical is the smallest allocated block, the function
1446 * returns 0 at @phys
1447 * return value contains 0 (success) or error code
1448 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001449static int ext4_ext_search_left(struct inode *inode,
1450 struct ext4_ext_path *path,
1451 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001452{
1453 struct ext4_extent_idx *ix;
1454 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001455 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001456
Frank Mayhar273df552010-03-02 11:46:09 -05001457 if (unlikely(path == NULL)) {
1458 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001459 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001460 }
Alex Tomas1988b512008-01-28 23:58:27 -05001461 depth = path->p_depth;
1462 *phys = 0;
1463
1464 if (depth == 0 && path->p_ext == NULL)
1465 return 0;
1466
1467 /* usually extent in the path covers blocks smaller
1468 * then *logical, but it can be that extent is the
1469 * first one in the file */
1470
1471 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001472 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001473 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001474 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1475 EXT4_ERROR_INODE(inode,
1476 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1477 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001478 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001479 }
Alex Tomas1988b512008-01-28 23:58:27 -05001480 while (--depth >= 0) {
1481 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001482 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1483 EXT4_ERROR_INODE(inode,
1484 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001485 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001486 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001487 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001488 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001489 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001490 }
Alex Tomas1988b512008-01-28 23:58:27 -05001491 }
1492 return 0;
1493 }
1494
Frank Mayhar273df552010-03-02 11:46:09 -05001495 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1496 EXT4_ERROR_INODE(inode,
1497 "logical %d < ee_block %d + ee_len %d!",
1498 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001499 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001500 }
Alex Tomas1988b512008-01-28 23:58:27 -05001501
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001502 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001503 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001504 return 0;
1505}
1506
1507/*
1508 * search the closest allocated block to the right for *logical
1509 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001510 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001511 * returns 0 at @phys
1512 * return value contains 0 (success) or error code
1513 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001514static int ext4_ext_search_right(struct inode *inode,
1515 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001516 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1517 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001518{
1519 struct buffer_head *bh = NULL;
1520 struct ext4_extent_header *eh;
1521 struct ext4_extent_idx *ix;
1522 struct ext4_extent *ex;
1523 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001524 int depth; /* Note, NOT eh_depth; depth from top of tree */
1525 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001526
Frank Mayhar273df552010-03-02 11:46:09 -05001527 if (unlikely(path == NULL)) {
1528 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001529 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001530 }
Alex Tomas1988b512008-01-28 23:58:27 -05001531 depth = path->p_depth;
1532 *phys = 0;
1533
1534 if (depth == 0 && path->p_ext == NULL)
1535 return 0;
1536
1537 /* usually extent in the path covers blocks smaller
1538 * then *logical, but it can be that extent is the
1539 * first one in the file */
1540
1541 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001542 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001543 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001544 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1545 EXT4_ERROR_INODE(inode,
1546 "first_extent(path[%d].p_hdr) != ex",
1547 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001548 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001549 }
Alex Tomas1988b512008-01-28 23:58:27 -05001550 while (--depth >= 0) {
1551 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001552 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1553 EXT4_ERROR_INODE(inode,
1554 "ix != EXT_FIRST_INDEX *logical %d!",
1555 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001556 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001557 }
Alex Tomas1988b512008-01-28 23:58:27 -05001558 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001559 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001560 }
1561
Frank Mayhar273df552010-03-02 11:46:09 -05001562 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1563 EXT4_ERROR_INODE(inode,
1564 "logical %d < ee_block %d + ee_len %d!",
1565 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001566 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001567 }
Alex Tomas1988b512008-01-28 23:58:27 -05001568
1569 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1570 /* next allocated block in this leaf */
1571 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001572 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001573 }
1574
1575 /* go up and search for index to the right */
1576 while (--depth >= 0) {
1577 ix = path[depth].p_idx;
1578 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001579 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001580 }
1581
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001582 /* we've gone up to the root and found no index to the right */
1583 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001584
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001585got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001586 /* we've found index to the right, let's
1587 * follow it and find the closest allocated
1588 * block to the right */
1589 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001590 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001591 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001592 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001593 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001594 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001595 if (IS_ERR(bh))
1596 return PTR_ERR(bh);
1597 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001598 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001599 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001600 put_bh(bh);
1601 }
1602
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001603 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001604 if (IS_ERR(bh))
1605 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001606 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001607 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001608found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001609 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001610 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001611 *ret_ex = ex;
1612 if (bh)
1613 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001614 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001615}
1616
1617/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001618 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001619 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001620 * NOTE: it considers block number from index entry as
1621 * allocated block. Thus, index entries have to be consistent
1622 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001623 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001624ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001625ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1626{
1627 int depth;
1628
1629 BUG_ON(path == NULL);
1630 depth = path->p_depth;
1631
1632 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001633 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001634
1635 while (depth >= 0) {
1636 if (depth == path->p_depth) {
1637 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001638 if (path[depth].p_ext &&
1639 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001640 EXT_LAST_EXTENT(path[depth].p_hdr))
1641 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1642 } else {
1643 /* index */
1644 if (path[depth].p_idx !=
1645 EXT_LAST_INDEX(path[depth].p_hdr))
1646 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1647 }
1648 depth--;
1649 }
1650
Lukas Czernerf17722f2011-06-06 00:05:17 -04001651 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001652}
1653
1654/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001655 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001656 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001657 */
Robin Dong57187892011-07-23 21:49:07 -04001658static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001659{
1660 int depth;
1661
1662 BUG_ON(path == NULL);
1663 depth = path->p_depth;
1664
1665 /* zero-tree has no leaf blocks at all */
1666 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001667 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001668
1669 /* go to index block */
1670 depth--;
1671
1672 while (depth >= 0) {
1673 if (path[depth].p_idx !=
1674 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001675 return (ext4_lblk_t)
1676 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001677 depth--;
1678 }
1679
Lukas Czernerf17722f2011-06-06 00:05:17 -04001680 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001681}
1682
1683/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001684 * ext4_ext_correct_indexes:
1685 * if leaf gets modified and modified extent is first in the leaf,
1686 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001687 * TODO: do we need to correct tree in all cases?
1688 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001689static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001690 struct ext4_ext_path *path)
1691{
1692 struct ext4_extent_header *eh;
1693 int depth = ext_depth(inode);
1694 struct ext4_extent *ex;
1695 __le32 border;
1696 int k, err = 0;
1697
1698 eh = path[depth].p_hdr;
1699 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001700
1701 if (unlikely(ex == NULL || eh == NULL)) {
1702 EXT4_ERROR_INODE(inode,
1703 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001704 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001705 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001706
1707 if (depth == 0) {
1708 /* there is no tree at all */
1709 return 0;
1710 }
1711
1712 if (ex != EXT_FIRST_EXTENT(eh)) {
1713 /* we correct tree if first leaf got modified only */
1714 return 0;
1715 }
1716
1717 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001718 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001719 */
1720 k = depth - 1;
1721 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001722 err = ext4_ext_get_access(handle, inode, path + k);
1723 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001724 return err;
1725 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001726 err = ext4_ext_dirty(handle, inode, path + k);
1727 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001728 return err;
1729
1730 while (k--) {
1731 /* change all left-side indexes */
1732 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1733 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001734 err = ext4_ext_get_access(handle, inode, path + k);
1735 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001736 break;
1737 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001738 err = ext4_ext_dirty(handle, inode, path + k);
1739 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001740 break;
1741 }
1742
1743 return err;
1744}
1745
Akira Fujita748de672009-06-17 19:24:03 -04001746int
Alex Tomasa86c6182006-10-11 01:21:03 -07001747ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1748 struct ext4_extent *ex2)
1749{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001750 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001751
Lukas Czerner556615d2014-04-20 23:45:47 -04001752 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001753 return 0;
1754
1755 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1756 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1757
1758 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001759 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001760 return 0;
1761
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001762 /*
1763 * To allow future support for preallocated extents to be added
1764 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001765 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001766 */
Eric Sandeenda0169b2013-11-04 09:58:26 -05001767 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001768 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001769
Lukas Czerner556615d2014-04-20 23:45:47 -04001770 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001771 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001772 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001773#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001774 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001775 return 0;
1776#endif
1777
Theodore Ts'obf89d162010-10-27 21:30:14 -04001778 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001779 return 1;
1780 return 0;
1781}
1782
1783/*
Amit Arora56055d32007-07-17 21:42:38 -04001784 * This function tries to merge the "ex" extent to the next extent in the tree.
1785 * It always tries to merge towards right. If you want to merge towards
1786 * left, pass "ex - 1" as argument instead of "ex".
1787 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1788 * 1 if they got merged.
1789 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001790static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001791 struct ext4_ext_path *path,
1792 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001793{
1794 struct ext4_extent_header *eh;
1795 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001796 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001797
1798 depth = ext_depth(inode);
1799 BUG_ON(path[depth].p_hdr == NULL);
1800 eh = path[depth].p_hdr;
1801
1802 while (ex < EXT_LAST_EXTENT(eh)) {
1803 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1804 break;
1805 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001806 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001807 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1808 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001809 if (unwritten)
1810 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001811
1812 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1813 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1814 * sizeof(struct ext4_extent);
1815 memmove(ex + 1, ex + 2, len);
1816 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001817 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001818 merge_done = 1;
1819 WARN_ON(eh->eh_entries == 0);
1820 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001821 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001822 }
1823
1824 return merge_done;
1825}
1826
1827/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001828 * This function does a very simple check to see if we can collapse
1829 * an extent tree with a single extent tree leaf block into the inode.
1830 */
1831static void ext4_ext_try_to_merge_up(handle_t *handle,
1832 struct inode *inode,
1833 struct ext4_ext_path *path)
1834{
1835 size_t s;
1836 unsigned max_root = ext4_ext_space_root(inode, 0);
1837 ext4_fsblk_t blk;
1838
1839 if ((path[0].p_depth != 1) ||
1840 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1841 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1842 return;
1843
1844 /*
1845 * We need to modify the block allocation bitmap and the block
1846 * group descriptor to release the extent tree block. If we
1847 * can't get the journal credits, give up.
1848 */
Jan Kara83448bd2019-11-05 17:44:29 +01001849 if (ext4_journal_extend(handle, 2,
1850 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001851 return;
1852
1853 /*
1854 * Copy the extent data up to the inode
1855 */
1856 blk = ext4_idx_pblock(path[0].p_idx);
1857 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1858 sizeof(struct ext4_extent_idx);
1859 s += sizeof(struct ext4_extent_header);
1860
Theodore Ts'o10809df82014-09-01 14:40:09 -04001861 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001862 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1863 path[0].p_depth = 0;
1864 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1865 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1866 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1867
1868 brelse(path[1].p_bh);
1869 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001870 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001871}
1872
1873/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001874 * This function tries to merge the @ex extent to neighbours in the tree.
1875 * return 1 if merge left else 0.
1876 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001877static void ext4_ext_try_to_merge(handle_t *handle,
1878 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001879 struct ext4_ext_path *path,
1880 struct ext4_extent *ex) {
1881 struct ext4_extent_header *eh;
1882 unsigned int depth;
1883 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001884
1885 depth = ext_depth(inode);
1886 BUG_ON(path[depth].p_hdr == NULL);
1887 eh = path[depth].p_hdr;
1888
1889 if (ex > EXT_FIRST_EXTENT(eh))
1890 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1891
1892 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001893 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001894
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001895 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001896}
1897
1898/*
Amit Arora25d14f92007-05-24 13:04:13 -04001899 * check if a portion of the "newext" extent overlaps with an
1900 * existing extent.
1901 *
1902 * If there is an overlap discovered, it updates the length of the newext
1903 * such that there will be no overlap, and then returns 1.
1904 * If there is no overlap found, it returns 0.
1905 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001906static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1907 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001908 struct ext4_extent *newext,
1909 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001910{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001911 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001912 unsigned int depth, len1;
1913 unsigned int ret = 0;
1914
1915 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001916 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001917 depth = ext_depth(inode);
1918 if (!path[depth].p_ext)
1919 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001920 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001921
1922 /*
1923 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001924 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001925 */
1926 if (b2 < b1) {
1927 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001928 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001929 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001930 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001931 }
1932
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001933 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001934 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001935 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001936 newext->ee_len = cpu_to_le16(len1);
1937 ret = 1;
1938 }
1939
1940 /* check for overlap */
1941 if (b1 + len1 > b2) {
1942 newext->ee_len = cpu_to_le16(b2 - b1);
1943 ret = 1;
1944 }
1945out:
1946 return ret;
1947}
1948
1949/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001950 * ext4_ext_insert_extent:
1951 * tries to merge requsted extent into the existing extent or
1952 * inserts requested extent as new one into the tree,
1953 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001954 */
1955int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001956 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001957 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001958{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001959 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001960 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001961 struct ext4_extent *ex, *fex;
1962 struct ext4_extent *nearex; /* nearest extent */
1963 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001964 int depth, len, err;
1965 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001966 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001967
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001968 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1969 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001970 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1971 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001972 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001973 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001974 depth = ext_depth(inode);
1975 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001976 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001977 if (unlikely(path[depth].p_hdr == NULL)) {
1978 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001979 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001980 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001981
1982 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001983 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001984
1985 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001986 * Try to see whether we should rather test the extent on
1987 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001988 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001989 * left, or on the right from the searched position. This
1990 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001991 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001992 if (ex < EXT_LAST_EXTENT(eh) &&
1993 (le32_to_cpu(ex->ee_block) +
1994 ext4_ext_get_actual_len(ex) <
1995 le32_to_cpu(newext->ee_block))) {
1996 ex += 1;
1997 goto prepend;
1998 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1999 (le32_to_cpu(newext->ee_block) +
2000 ext4_ext_get_actual_len(newext) <
2001 le32_to_cpu(ex->ee_block)))
2002 ex -= 1;
2003
2004 /* Try to append newex to the ex */
2005 if (ext4_can_extents_be_merged(inode, ex, newext)) {
2006 ext_debug("append [%d]%d block to %u:[%d]%d"
2007 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04002008 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002009 ext4_ext_get_actual_len(newext),
2010 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002011 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002012 ext4_ext_get_actual_len(ex),
2013 ext4_ext_pblock(ex));
2014 err = ext4_ext_get_access(handle, inode,
2015 path + depth);
2016 if (err)
2017 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04002018 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002019 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002020 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002021 if (unwritten)
2022 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002023 eh = path[depth].p_hdr;
2024 nearex = ex;
2025 goto merge;
2026 }
2027
2028prepend:
2029 /* Try to prepend newex to the ex */
2030 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2031 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2032 "(from %llu)\n",
2033 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002034 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002035 ext4_ext_get_actual_len(newext),
2036 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002037 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002038 ext4_ext_get_actual_len(ex),
2039 ext4_ext_pblock(ex));
2040 err = ext4_ext_get_access(handle, inode,
2041 path + depth);
2042 if (err)
2043 return err;
2044
Lukas Czerner556615d2014-04-20 23:45:47 -04002045 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002046 ex->ee_block = newext->ee_block;
2047 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2048 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2049 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002050 if (unwritten)
2051 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002052 eh = path[depth].p_hdr;
2053 nearex = ex;
2054 goto merge;
2055 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002056 }
2057
Alex Tomasa86c6182006-10-11 01:21:03 -07002058 depth = ext_depth(inode);
2059 eh = path[depth].p_hdr;
2060 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2061 goto has_space;
2062
2063 /* probably next leaf has space for us? */
2064 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002065 next = EXT_MAX_BLOCKS;
2066 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002067 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002068 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002069 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002070 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002071 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002072 if (IS_ERR(npath))
2073 return PTR_ERR(npath);
2074 BUG_ON(npath->p_depth != path->p_depth);
2075 eh = npath[depth].p_hdr;
2076 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002077 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002078 le16_to_cpu(eh->eh_entries));
2079 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002080 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002081 }
2082 ext_debug("next leaf has no free space(%d,%d)\n",
2083 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2084 }
2085
2086 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002087 * There is no free space in the found leaf.
2088 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002089 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002090 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002091 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002092 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002093 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002094 if (err)
2095 goto cleanup;
2096 depth = ext_depth(inode);
2097 eh = path[depth].p_hdr;
2098
2099has_space:
2100 nearex = path[depth].p_ext;
2101
Avantika Mathur7e028972006-12-06 20:41:33 -08002102 err = ext4_ext_get_access(handle, inode, path + depth);
2103 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002104 goto cleanup;
2105
2106 if (!nearex) {
2107 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002108 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002109 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002110 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002111 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002112 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002113 nearex = EXT_FIRST_EXTENT(eh);
2114 } else {
2115 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002116 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002117 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002118 ext_debug("insert %u:%llu:[%d]%d before: "
2119 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002120 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002121 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002122 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002123 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002124 nearex);
2125 nearex++;
2126 } else {
2127 /* Insert before */
2128 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002129 ext_debug("insert %u:%llu:[%d]%d after: "
2130 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002131 le32_to_cpu(newext->ee_block),
2132 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002133 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002134 ext4_ext_get_actual_len(newext),
2135 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002136 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002137 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2138 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002139 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002140 "move %d extents from 0x%p to 0x%p\n",
2141 le32_to_cpu(newext->ee_block),
2142 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002143 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002144 ext4_ext_get_actual_len(newext),
2145 len, nearex, nearex + 1);
2146 memmove(nearex + 1, nearex,
2147 len * sizeof(struct ext4_extent));
2148 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002149 }
2150
Marcin Slusarze8546d02008-04-17 10:38:59 -04002151 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002152 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002153 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002154 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002155 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002156
2157merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002158 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002159 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002160 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002161
Alex Tomasa86c6182006-10-11 01:21:03 -07002162
2163 /* time to correct all indexes above */
2164 err = ext4_ext_correct_indexes(handle, inode, path);
2165 if (err)
2166 goto cleanup;
2167
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002168 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002169
2170cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002171 ext4_ext_drop_refs(npath);
2172 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002173 return err;
2174}
2175
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002176static int ext4_fill_fiemap_extents(struct inode *inode,
2177 ext4_lblk_t block, ext4_lblk_t num,
2178 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002179{
2180 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002181 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002182 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002183 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002184 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002185 int exists, depth = 0, err = 0;
2186 unsigned int flags = 0;
2187 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002188
Lukas Czernerf17722f2011-06-06 00:05:17 -04002189 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002190 num = last - block;
2191 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002192 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002193
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002194 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002195 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002196 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002197 err = PTR_ERR(path);
2198 path = NULL;
2199 break;
2200 }
2201
2202 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002203 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002204 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002205 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002206 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002207 break;
2208 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002209 ex = path[depth].p_ext;
2210 next = ext4_ext_next_allocated_block(path);
2211
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002212 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002213 exists = 0;
2214 if (!ex) {
2215 /* there is no extent yet, so try to allocate
2216 * all requested space */
2217 start = block;
2218 end = block + num;
2219 } else if (le32_to_cpu(ex->ee_block) > block) {
2220 /* need to allocate space before found extent */
2221 start = block;
2222 end = le32_to_cpu(ex->ee_block);
2223 if (block + num < end)
2224 end = block + num;
2225 } else if (block >= le32_to_cpu(ex->ee_block)
2226 + ext4_ext_get_actual_len(ex)) {
2227 /* need to allocate space after found extent */
2228 start = block;
2229 end = block + num;
2230 if (end >= next)
2231 end = next;
2232 } else if (block >= le32_to_cpu(ex->ee_block)) {
2233 /*
2234 * some part of requested space is covered
2235 * by found extent
2236 */
2237 start = block;
2238 end = le32_to_cpu(ex->ee_block)
2239 + ext4_ext_get_actual_len(ex);
2240 if (block + num < end)
2241 end = block + num;
2242 exists = 1;
2243 } else {
2244 BUG();
2245 }
2246 BUG_ON(end <= start);
2247
2248 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002249 es.es_lblk = start;
2250 es.es_len = end - start;
2251 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002252 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002253 es.es_lblk = le32_to_cpu(ex->ee_block);
2254 es.es_len = ext4_ext_get_actual_len(ex);
2255 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002256 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002257 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002258 }
2259
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002260 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002261 * Find delayed extent and update es accordingly. We call
2262 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002263 * last existing extent or not.
2264 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002265 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002266 if (!exists && next_del) {
2267 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002268 flags |= (FIEMAP_EXTENT_DELALLOC |
2269 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002270 }
2271 up_read(&EXT4_I(inode)->i_data_sem);
2272
Zheng Liu69eb33d2013-02-18 00:31:07 -05002273 if (unlikely(es.es_len == 0)) {
2274 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002275 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002276 break;
2277 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002278
Zheng Liuf7fec032013-02-18 00:28:47 -05002279 /*
2280 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2281 * we need to check next == EXT_MAX_BLOCKS because it is
2282 * possible that an extent is with unwritten and delayed
2283 * status due to when an extent is delayed allocated and
2284 * is allocated by fallocate status tree will track both of
2285 * them in a extent.
2286 *
2287 * So we could return a unwritten and delayed extent, and
2288 * its block is equal to 'next'.
2289 */
2290 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002291 flags |= FIEMAP_EXTENT_LAST;
2292 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2293 next != EXT_MAX_BLOCKS)) {
2294 EXT4_ERROR_INODE(inode,
2295 "next extent == %u, next "
2296 "delalloc extent = %u",
2297 next, next_del);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002298 err = -EFSCORRUPTED;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002299 break;
2300 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002301 }
2302
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002303 if (exists) {
2304 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002305 (__u64)es.es_lblk << blksize_bits,
2306 (__u64)es.es_pblk << blksize_bits,
2307 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002308 flags);
2309 if (err < 0)
2310 break;
2311 if (err == 1) {
2312 err = 0;
2313 break;
2314 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002315 }
2316
Zheng Liu69eb33d2013-02-18 00:31:07 -05002317 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002318 }
2319
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002320 ext4_ext_drop_refs(path);
2321 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002322 return err;
2323}
2324
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002325static int ext4_fill_es_cache_info(struct inode *inode,
2326 ext4_lblk_t block, ext4_lblk_t num,
2327 struct fiemap_extent_info *fieinfo)
2328{
2329 ext4_lblk_t next, end = block + num - 1;
2330 struct extent_status es;
2331 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2332 unsigned int flags;
2333 int err;
2334
2335 while (block <= end) {
2336 next = 0;
2337 flags = 0;
2338 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2339 break;
2340 if (ext4_es_is_unwritten(&es))
2341 flags |= FIEMAP_EXTENT_UNWRITTEN;
2342 if (ext4_es_is_delayed(&es))
2343 flags |= (FIEMAP_EXTENT_DELALLOC |
2344 FIEMAP_EXTENT_UNKNOWN);
2345 if (ext4_es_is_hole(&es))
2346 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2347 if (next == 0)
2348 flags |= FIEMAP_EXTENT_LAST;
2349 if (flags & (FIEMAP_EXTENT_DELALLOC|
2350 EXT4_FIEMAP_EXTENT_HOLE))
2351 es.es_pblk = 0;
2352 else
2353 es.es_pblk = ext4_es_pblock(&es);
2354 err = fiemap_fill_next_extent(fieinfo,
2355 (__u64)es.es_lblk << blksize_bits,
2356 (__u64)es.es_pblk << blksize_bits,
2357 (__u64)es.es_len << blksize_bits,
2358 flags);
2359 if (next == 0)
2360 break;
2361 block = next;
2362 if (err < 0)
2363 return err;
2364 if (err == 1)
2365 return 0;
2366 }
2367 return 0;
2368}
2369
2370
Alex Tomasa86c6182006-10-11 01:21:03 -07002371/*
Jan Kara140a5252016-03-09 22:46:57 -05002372 * ext4_ext_determine_hole - determine hole around given block
2373 * @inode: inode we lookup in
2374 * @path: path in extent tree to @lblk
2375 * @lblk: pointer to logical block around which we want to determine hole
2376 *
2377 * Determine hole length (and start if easily possible) around given logical
2378 * block. We don't try too hard to find the beginning of the hole but @path
2379 * actually points to extent before @lblk, we provide it.
2380 *
2381 * The function returns the length of a hole starting at @lblk. We update @lblk
2382 * to the beginning of the hole if we managed to find it.
2383 */
2384static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2385 struct ext4_ext_path *path,
2386 ext4_lblk_t *lblk)
2387{
2388 int depth = ext_depth(inode);
2389 struct ext4_extent *ex;
2390 ext4_lblk_t len;
2391
2392 ex = path[depth].p_ext;
2393 if (ex == NULL) {
2394 /* there is no extent yet, so gap is [0;-] */
2395 *lblk = 0;
2396 len = EXT_MAX_BLOCKS;
2397 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2398 len = le32_to_cpu(ex->ee_block) - *lblk;
2399 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2400 + ext4_ext_get_actual_len(ex)) {
2401 ext4_lblk_t next;
2402
2403 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2404 next = ext4_ext_next_allocated_block(path);
2405 BUG_ON(next == *lblk);
2406 len = next - *lblk;
2407 } else {
2408 BUG();
2409 }
2410 return len;
2411}
2412
2413/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002414 * ext4_ext_put_gap_in_cache:
2415 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002416 * and cache this gap
2417 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002418static void
Jan Kara140a5252016-03-09 22:46:57 -05002419ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2420 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002421{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002422 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002423
Eric Whitneyad431022018-10-01 14:10:39 -04002424 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2425 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002426 if (es.es_len) {
2427 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002428 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002429 return;
Jan Kara140a5252016-03-09 22:46:57 -05002430 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002431 }
Jan Kara140a5252016-03-09 22:46:57 -05002432 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2433 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2434 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002435}
2436
2437/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002438 * ext4_ext_rm_idx:
2439 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002440 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002441static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002442 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002443{
Alex Tomasa86c6182006-10-11 01:21:03 -07002444 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002445 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002446
2447 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002448 depth--;
2449 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002450 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002451 if (unlikely(path->p_hdr->eh_entries == 0)) {
2452 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002453 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002454 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002455 err = ext4_ext_get_access(handle, inode, path);
2456 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002457 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002458
2459 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2460 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2461 len *= sizeof(struct ext4_extent_idx);
2462 memmove(path->p_idx, path->p_idx + 1, len);
2463 }
2464
Marcin Slusarze8546d02008-04-17 10:38:59 -04002465 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002466 err = ext4_ext_dirty(handle, inode, path);
2467 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002468 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002469 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002470 trace_ext4_ext_rm_idx(inode, leaf);
2471
Peter Huewe7dc57612011-02-21 21:01:42 -05002472 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002473 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002474
2475 while (--depth >= 0) {
2476 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2477 break;
2478 path--;
2479 err = ext4_ext_get_access(handle, inode, path);
2480 if (err)
2481 break;
2482 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2483 err = ext4_ext_dirty(handle, inode, path);
2484 if (err)
2485 break;
2486 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002487 return err;
2488}
2489
2490/*
Mingming Caoee12b632008-08-19 22:16:05 -04002491 * ext4_ext_calc_credits_for_single_extent:
2492 * This routine returns max. credits that needed to insert an extent
2493 * to the extent tree.
2494 * When pass the actual path, the caller should calculate credits
2495 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002496 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002497int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002498 struct ext4_ext_path *path)
2499{
Alex Tomasa86c6182006-10-11 01:21:03 -07002500 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002501 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002502 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002503
Alex Tomasa86c6182006-10-11 01:21:03 -07002504 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002505 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002506 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2507
2508 /*
2509 * There are some space in the leaf tree, no
2510 * need to account for leaf block credit
2511 *
2512 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002513 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002514 * accounted.
2515 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002516 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002517 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002518 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002519 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002520 }
2521
Mingming Cao525f4ed2008-08-19 22:15:58 -04002522 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002523}
Alex Tomasa86c6182006-10-11 01:21:03 -07002524
Mingming Caoee12b632008-08-19 22:16:05 -04002525/*
Jan Karafffb2732013-06-04 13:01:11 -04002526 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002527 *
Jan Karafffb2732013-06-04 13:01:11 -04002528 * If we add a single extent, then in the worse case, each tree level
2529 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002530 *
Jan Karafffb2732013-06-04 13:01:11 -04002531 * If more extents are inserted, they could cause the whole tree split more
2532 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002533 */
Jan Karafffb2732013-06-04 13:01:11 -04002534int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002535{
2536 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002537 int depth;
2538
2539 /* If we are converting the inline data, only one is needed here. */
2540 if (ext4_has_inline_data(inode))
2541 return 1;
2542
2543 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002544
Jan Karafffb2732013-06-04 13:01:11 -04002545 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002546 index = depth * 2;
2547 else
2548 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002549
Mingming Caoee12b632008-08-19 22:16:05 -04002550 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002551}
2552
Theodore Ts'o981250c2013-06-12 11:48:29 -04002553static inline int get_default_free_blocks_flags(struct inode *inode)
2554{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002555 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2556 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002557 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2558 else if (ext4_should_journal_data(inode))
2559 return EXT4_FREE_BLOCKS_FORGET;
2560 return 0;
2561}
2562
Eric Whitney9fe67142018-10-01 14:25:08 -04002563/*
2564 * ext4_rereserve_cluster - increment the reserved cluster count when
2565 * freeing a cluster with a pending reservation
2566 *
2567 * @inode - file containing the cluster
2568 * @lblk - logical block in cluster to be reserved
2569 *
2570 * Increments the reserved cluster count and adjusts quota in a bigalloc
2571 * file system when freeing a partial cluster containing at least one
2572 * delayed and unwritten block. A partial cluster meeting that
2573 * requirement will have a pending reservation. If so, the
2574 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2575 * defer reserved and allocated space accounting to a subsequent call
2576 * to this function.
2577 */
2578static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2579{
2580 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2581 struct ext4_inode_info *ei = EXT4_I(inode);
2582
2583 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2584
2585 spin_lock(&ei->i_block_reservation_lock);
2586 ei->i_reserved_data_blocks++;
2587 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2588 spin_unlock(&ei->i_block_reservation_lock);
2589
2590 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2591 ext4_remove_pending(inode, lblk);
2592}
2593
Alex Tomasa86c6182006-10-11 01:21:03 -07002594static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002595 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002596 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002597 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002598{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002599 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002600 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002601 ext4_fsblk_t last_pblk, pblk;
2602 ext4_lblk_t num;
2603 int flags;
2604
2605 /* only extent tail removal is allowed */
2606 if (from < le32_to_cpu(ex->ee_block) ||
2607 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2608 ext4_error(sbi->s_sb,
2609 "strange request: removal(2) %u-%u from %u:%u",
2610 from, to, le32_to_cpu(ex->ee_block), ee_len);
2611 return 0;
2612 }
2613
2614#ifdef EXTENTS_STATS
2615 spin_lock(&sbi->s_ext_stats_lock);
2616 sbi->s_ext_blocks += ee_len;
2617 sbi->s_ext_extents++;
2618 if (ee_len < sbi->s_ext_min)
2619 sbi->s_ext_min = ee_len;
2620 if (ee_len > sbi->s_ext_max)
2621 sbi->s_ext_max = ee_len;
2622 if (ext_depth(inode) > sbi->s_depth_max)
2623 sbi->s_depth_max = ext_depth(inode);
2624 spin_unlock(&sbi->s_ext_stats_lock);
2625#endif
2626
2627 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2628
2629 /*
2630 * if we have a partial cluster, and it's different from the
2631 * cluster of the last block in the extent, we free it
2632 */
2633 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2634
2635 if (partial->state != initial &&
2636 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2637 if (partial->state == tofree) {
2638 flags = get_default_free_blocks_flags(inode);
2639 if (ext4_is_pending(inode, partial->lblk))
2640 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2641 ext4_free_blocks(handle, inode, NULL,
2642 EXT4_C2B(sbi, partial->pclu),
2643 sbi->s_cluster_ratio, flags);
2644 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2645 ext4_rereserve_cluster(inode, partial->lblk);
2646 }
2647 partial->state = initial;
2648 }
2649
2650 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2651 pblk = ext4_ext_pblock(ex) + ee_len - num;
2652
2653 /*
2654 * We free the partial cluster at the end of the extent (if any),
2655 * unless the cluster is used by another extent (partial_cluster
2656 * state is nofree). If a partial cluster exists here, it must be
2657 * shared with the last block in the extent.
2658 */
2659 flags = get_default_free_blocks_flags(inode);
2660
2661 /* partial, left end cluster aligned, right end unaligned */
2662 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2663 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2664 (partial->state != nofree)) {
2665 if (ext4_is_pending(inode, to))
2666 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2667 ext4_free_blocks(handle, inode, NULL,
2668 EXT4_PBLK_CMASK(sbi, last_pblk),
2669 sbi->s_cluster_ratio, flags);
2670 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2671 ext4_rereserve_cluster(inode, to);
2672 partial->state = initial;
2673 flags = get_default_free_blocks_flags(inode);
2674 }
2675
2676 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002677
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002678 /*
2679 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002680 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002681 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002682 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002683 */
2684 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002685 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002686
Eric Whitney9fe67142018-10-01 14:25:08 -04002687 /* reset the partial cluster if we've freed past it */
2688 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2689 partial->state = initial;
2690
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002691 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002692 * If we've freed the entire extent but the beginning is not left
2693 * cluster aligned and is not marked as ineligible for freeing we
2694 * record the partial cluster at the beginning of the extent. It
2695 * wasn't freed by the preceding ext4_free_blocks() call, and we
2696 * need to look farther to the left to determine if it's to be freed
2697 * (not shared with another extent). Else, reset the partial
2698 * cluster - we're either done freeing or the beginning of the
2699 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002700 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002701 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2702 if (partial->state == initial) {
2703 partial->pclu = EXT4_B2C(sbi, pblk);
2704 partial->lblk = from;
2705 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002706 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002707 } else {
2708 partial->state = initial;
2709 }
2710
Alex Tomasa86c6182006-10-11 01:21:03 -07002711 return 0;
2712}
2713
Allison Hendersond583fb82011-05-25 07:41:43 -04002714/*
2715 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002716 * blocks appearing between "start" and "end". Both "start"
2717 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002718 *
2719 * @handle: The journal handle
2720 * @inode: The files inode
2721 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002722 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002723 * has been released from it. However, if this value is
2724 * negative, it's a cluster just to the right of the
2725 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002726 * @start: The first block to remove
2727 * @end: The last block to remove
2728 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002729static int
2730ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002731 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002732 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002733 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002734{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002735 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002736 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002737 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002738 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002739 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002740 unsigned num;
2741 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002742 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002743 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002744 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002745 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002746
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002747 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002748 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002749 if (!path[depth].p_hdr)
2750 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2751 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002752 if (unlikely(path[depth].p_hdr == NULL)) {
2753 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002754 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002755 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002756 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002757 ex = path[depth].p_ext;
2758 if (!ex)
2759 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002760
2761 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002762 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002763
Eric Whitney9fe67142018-10-01 14:25:08 -04002764 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002765
Alex Tomasa86c6182006-10-11 01:21:03 -07002766 while (ex >= EXT_FIRST_EXTENT(eh) &&
2767 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002768
Lukas Czerner556615d2014-04-20 23:45:47 -04002769 if (ext4_ext_is_unwritten(ex))
2770 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002771 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002772 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002773
Mingming553f9002009-09-18 13:34:55 -04002774 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002775 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002776 path[depth].p_ext = ex;
2777
2778 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002779 b = ex_ee_block+ex_ee_len - 1 < end ?
2780 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002781
2782 ext_debug(" border %u:%u\n", a, b);
2783
Allison Hendersond583fb82011-05-25 07:41:43 -04002784 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002785 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002786 /*
2787 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002788 * so note that its first cluster is in use to avoid
2789 * freeing it when removing blocks. Eventually, the
2790 * right edge of the truncated/punched region will
2791 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002792 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002793 if (sbi->s_cluster_ratio > 1) {
2794 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002795 partial->pclu = EXT4_B2C(sbi, pblk);
2796 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002797 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002798 ex--;
2799 ex_ee_block = le32_to_cpu(ex->ee_block);
2800 ex_ee_len = ext4_ext_get_actual_len(ex);
2801 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002802 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002803 EXT4_ERROR_INODE(inode,
2804 "can not handle truncate %u:%u "
2805 "on extent %u:%u",
2806 start, end, ex_ee_block,
2807 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002808 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002809 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002810 } else if (a != ex_ee_block) {
2811 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002812 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002813 } else {
2814 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002815 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002816 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002817 /*
2818 * 3 for leaf, sb, and inode plus 2 (bmap and group
2819 * descriptor) for each block group; assume two block
2820 * groups plus ex_ee_len/blocks_per_block_group for
2821 * the worst case
2822 */
2823 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002824 if (ex == EXT_FIRST_EXTENT(eh)) {
2825 correct_index = 1;
2826 credits += (ext_depth(inode)) + 1;
2827 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002828 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002829 /*
2830 * We may end up freeing some index blocks and data from the
2831 * punched range. Note that partial clusters are accounted for
2832 * by ext4_free_data_revoke_credits().
2833 */
2834 revoke_credits =
2835 ext4_free_metadata_revoke_credits(inode->i_sb,
2836 ext_depth(inode)) +
2837 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002838
Jan Karaa4130362019-11-05 17:44:16 +01002839 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002840 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002841 if (err) {
2842 if (err > 0)
2843 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002844 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002845 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002846
2847 err = ext4_ext_get_access(handle, inode, path + depth);
2848 if (err)
2849 goto out;
2850
Eric Whitney9fe67142018-10-01 14:25:08 -04002851 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002852 if (err)
2853 goto out;
2854
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002855 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002856 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002857 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002858
Alex Tomasa86c6182006-10-11 01:21:03 -07002859 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002860 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002861 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002862 * extent have been removed.
2863 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002864 if (unwritten && num)
2865 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002866 /*
2867 * If the extent was completely released,
2868 * we need to remove it from the leaf
2869 */
2870 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002871 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002872 /*
2873 * For hole punching, we need to scoot all the
2874 * extents up when an extent is removed so that
2875 * we dont have blank extents in the middle
2876 */
2877 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2878 sizeof(struct ext4_extent));
2879
2880 /* Now get rid of the one at the end */
2881 memset(EXT_LAST_EXTENT(eh), 0,
2882 sizeof(struct ext4_extent));
2883 }
2884 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002885 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002886
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002887 err = ext4_ext_dirty(handle, inode, path + depth);
2888 if (err)
2889 goto out;
2890
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002891 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002892 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002893 ex--;
2894 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002895 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002896 }
2897
2898 if (correct_index && eh->eh_entries)
2899 err = ext4_ext_correct_indexes(handle, inode, path);
2900
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002901 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002902 * If there's a partial cluster and at least one extent remains in
2903 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002904 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002905 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002906 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002907 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002908 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002909 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002910 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2911 int flags = get_default_free_blocks_flags(inode);
2912
2913 if (ext4_is_pending(inode, partial->lblk))
2914 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002915 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002916 EXT4_C2B(sbi, partial->pclu),
2917 sbi->s_cluster_ratio, flags);
2918 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2919 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002920 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002921 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002922 }
2923
Alex Tomasa86c6182006-10-11 01:21:03 -07002924 /* if this leaf is free, then we should
2925 * remove it from index block above */
2926 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002927 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002928
2929out:
2930 return err;
2931}
2932
2933/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002934 * ext4_ext_more_to_rm:
2935 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002936 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002937static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002938ext4_ext_more_to_rm(struct ext4_ext_path *path)
2939{
2940 BUG_ON(path->p_idx == NULL);
2941
2942 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2943 return 0;
2944
2945 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002946 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002947 * so we have to consider current index for truncation
2948 */
2949 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2950 return 0;
2951 return 1;
2952}
2953
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002954int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2955 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002956{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002957 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002958 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002959 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002960 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002961 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002962 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002963
Eric Whitney9fe67142018-10-01 14:25:08 -04002964 partial.pclu = 0;
2965 partial.lblk = 0;
2966 partial.state = initial;
2967
Lukas Czerner5f95d212012-03-19 23:03:19 -04002968 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002969
2970 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002971 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2972 depth + 1,
2973 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002974 if (IS_ERR(handle))
2975 return PTR_ERR(handle);
2976
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002977again:
Lukas Czerner61801322013-05-27 23:32:35 -04002978 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002979
Alex Tomasa86c6182006-10-11 01:21:03 -07002980 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002981 * Check if we are removing extents inside the extent tree. If that
2982 * is the case, we are going to punch a hole inside the extent tree
2983 * so we have to check whether we need to split the extent covering
2984 * the last block to remove so we can easily remove the part of it
2985 * in ext4_ext_rm_leaf().
2986 */
2987 if (end < EXT_MAX_BLOCKS - 1) {
2988 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002989 ext4_lblk_t ee_block, ex_end, lblk;
2990 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002991
Eric Whitneyf4226d92014-11-23 00:55:42 -05002992 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002993 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002994 if (IS_ERR(path)) {
2995 ext4_journal_stop(handle);
2996 return PTR_ERR(path);
2997 }
2998 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002999 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04003000 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003001 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04003002 if (depth) {
3003 EXT4_ERROR_INODE(inode,
3004 "path[%d].p_hdr == NULL",
3005 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003006 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04003007 }
3008 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003009 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003010
3011 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05003012 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003013
3014 /*
3015 * See if the last block is inside the extent, if so split
3016 * the extent at 'end' block so we can easily remove the
3017 * tail of the first part of the split extent in
3018 * ext4_ext_rm_leaf().
3019 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05003020 if (end >= ee_block && end < ex_end) {
3021
3022 /*
3023 * If we're going to split the extent, note that
3024 * the cluster containing the block after 'end' is
3025 * in use to avoid freeing it when removing blocks.
3026 */
3027 if (sbi->s_cluster_ratio > 1) {
3028 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04003029 partial.pclu = EXT4_B2C(sbi, pblk);
3030 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05003031 }
3032
Lukas Czerner5f95d212012-03-19 23:03:19 -04003033 /*
3034 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04003035 * block in the first new extent. Also we should not
3036 * fail removing space due to ENOSPC so try to use
3037 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04003038 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04003039 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04003040 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04003041 if (err < 0)
3042 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05003043
Eric Whitney7bd75232019-02-28 23:34:11 -05003044 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
3045 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05003046 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05003047 * If we're punching, there's an extent to the right.
3048 * If the partial cluster hasn't been set, set it to
3049 * that extent's first cluster and its state to nofree
3050 * so it won't be freed should it contain blocks to be
3051 * removed. If it's already set (tofree/nofree), we're
3052 * retrying and keep the original partial cluster info
3053 * so a cluster marked tofree as a result of earlier
3054 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05003055 */
3056 lblk = ex_end + 1;
3057 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
3058 &ex);
3059 if (err)
3060 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04003061 if (pblk) {
3062 partial.pclu = EXT4_B2C(sbi, pblk);
3063 partial.state = nofree;
3064 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003065 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003066 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003067 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003068 * We start scanning from right side, freeing all the blocks
3069 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07003070 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04003071 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04003072 if (path) {
3073 int k = i = depth;
3074 while (--k > 0)
3075 path[k].p_block =
3076 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
3077 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003078 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Ashish Sangwan968dee72012-07-22 22:49:08 -04003079 GFP_NOFS);
3080 if (path == NULL) {
3081 ext4_journal_stop(handle);
3082 return -ENOMEM;
3083 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04003084 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003085 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04003086 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003087
Theodore Ts'oc3491792013-08-16 21:21:41 -04003088 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003089 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003090 goto out;
3091 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003092 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04003093 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07003094
3095 while (i >= 0 && err == 0) {
3096 if (i == depth) {
3097 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04003098 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04003099 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003100 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003101 brelse(path[i].p_bh);
3102 path[i].p_bh = NULL;
3103 i--;
3104 continue;
3105 }
3106
3107 /* this is index block */
3108 if (!path[i].p_hdr) {
3109 ext_debug("initialize header\n");
3110 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003111 }
3112
Alex Tomasa86c6182006-10-11 01:21:03 -07003113 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003114 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07003115 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
3116 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
3117 ext_debug("init index ptr: hdr 0x%p, num %d\n",
3118 path[i].p_hdr,
3119 le16_to_cpu(path[i].p_hdr->eh_entries));
3120 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003121 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07003122 path[i].p_idx--;
3123 }
3124
3125 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3126 i, EXT_FIRST_INDEX(path[i].p_hdr),
3127 path[i].p_idx);
3128 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003129 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003130 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07003131 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04003132 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07003133 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003134 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04003135 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
3136 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003137 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003138 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003139 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003140 break;
3141 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04003142 /* Yield here to deal with large extent trees.
3143 * Should be a no-op if we did IO above. */
3144 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003145 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003146 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003147 break;
3148 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003149 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003150
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003151 /* save actual number of indexes since this
3152 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07003153 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3154 i++;
3155 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003156 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07003157 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003158 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07003159 * handle must be already prepared by the
3160 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05003161 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07003162 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003163 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003164 brelse(path[i].p_bh);
3165 path[i].p_bh = NULL;
3166 i--;
3167 ext_debug("return to level %d\n", i);
3168 }
3169 }
3170
Eric Whitney9fe67142018-10-01 14:25:08 -04003171 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
3172 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003173
Eric Whitney0756b902014-11-23 00:59:39 -05003174 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04003175 * if there's a partial cluster and we have removed the first extent
3176 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05003177 */
Eric Whitney9fe67142018-10-01 14:25:08 -04003178 if (partial.state == tofree && err == 0) {
3179 int flags = get_default_free_blocks_flags(inode);
3180
3181 if (ext4_is_pending(inode, partial.lblk))
3182 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003183 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003184 EXT4_C2B(sbi, partial.pclu),
3185 sbi->s_cluster_ratio, flags);
3186 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3187 ext4_rereserve_cluster(inode, partial.lblk);
3188 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003189 }
3190
Alex Tomasa86c6182006-10-11 01:21:03 -07003191 /* TODO: flexible tree reduction should be here */
3192 if (path->p_hdr->eh_entries == 0) {
3193 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003194 * truncate to zero freed all the tree,
3195 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003196 */
3197 err = ext4_ext_get_access(handle, inode, path);
3198 if (err == 0) {
3199 ext_inode_hdr(inode)->eh_depth = 0;
3200 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003201 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003202 err = ext4_ext_dirty(handle, inode, path);
3203 }
3204 }
3205out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003206 ext4_ext_drop_refs(path);
3207 kfree(path);
3208 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003209 if (err == -EAGAIN)
3210 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003211 ext4_journal_stop(handle);
3212
3213 return err;
3214}
3215
3216/*
3217 * called at mount time
3218 */
3219void ext4_ext_init(struct super_block *sb)
3220{
3221 /*
3222 * possible initialization would be here
3223 */
3224
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003225 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003226#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003227 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003228#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003229 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003230#endif
3231#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003232 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003233#endif
3234#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003235 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003236#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003237 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003238#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003239#ifdef EXTENTS_STATS
3240 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3241 EXT4_SB(sb)->s_ext_min = 1 << 30;
3242 EXT4_SB(sb)->s_ext_max = 0;
3243#endif
3244 }
3245}
3246
3247/*
3248 * called at umount time
3249 */
3250void ext4_ext_release(struct super_block *sb)
3251{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003252 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003253 return;
3254
3255#ifdef EXTENTS_STATS
3256 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3257 struct ext4_sb_info *sbi = EXT4_SB(sb);
3258 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3259 sbi->s_ext_blocks, sbi->s_ext_extents,
3260 sbi->s_ext_blocks / sbi->s_ext_extents);
3261 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3262 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3263 }
3264#endif
3265}
3266
Zheng Liud7b2a002013-08-28 14:47:06 -04003267static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3268{
3269 ext4_lblk_t ee_block;
3270 ext4_fsblk_t ee_pblock;
3271 unsigned int ee_len;
3272
3273 ee_block = le32_to_cpu(ex->ee_block);
3274 ee_len = ext4_ext_get_actual_len(ex);
3275 ee_pblock = ext4_ext_pblock(ex);
3276
3277 if (ee_len == 0)
3278 return 0;
3279
3280 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3281 EXTENT_STATUS_WRITTEN);
3282}
3283
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003284/* FIXME!! we need to try to merge to left or right after zero-out */
3285static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3286{
Lukas Czerner24075182010-10-27 21:30:06 -04003287 ext4_fsblk_t ee_pblock;
3288 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003289
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003290 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003291 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003292 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3293 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003294}
3295
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003297 * ext4_split_extent_at() splits an extent at given block.
3298 *
3299 * @handle: the journal handle
3300 * @inode: the file inode
3301 * @path: the path to the extent
3302 * @split: the logical block where the extent is splitted.
3303 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003304 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003305 * @flags: flags used to insert new extent to extent tree.
3306 *
3307 *
3308 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3309 * of which are deterimined by split_flag.
3310 *
3311 * There are two cases:
3312 * a> the extent are splitted into two extent.
3313 * b> split is not needed, and just mark the extent.
3314 *
3315 * return 0 on success.
3316 */
3317static int ext4_split_extent_at(handle_t *handle,
3318 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003319 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003320 ext4_lblk_t split,
3321 int split_flag,
3322 int flags)
3323{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003324 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003325 ext4_fsblk_t newblock;
3326 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003327 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003328 struct ext4_extent *ex2 = NULL;
3329 unsigned int ee_len, depth;
3330 int err = 0;
3331
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003332 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3333 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3334
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003335 ext_debug("ext4_split_extents_at: inode %lu, logical"
3336 "block %llu\n", inode->i_ino, (unsigned long long)split);
3337
3338 ext4_ext_show_leaf(inode, path);
3339
3340 depth = ext_depth(inode);
3341 ex = path[depth].p_ext;
3342 ee_block = le32_to_cpu(ex->ee_block);
3343 ee_len = ext4_ext_get_actual_len(ex);
3344 newblock = split - ee_block + ext4_ext_pblock(ex);
3345
3346 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003347 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003348 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003349 EXT4_EXT_MARK_UNWRIT1 |
3350 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003351
3352 err = ext4_ext_get_access(handle, inode, path + depth);
3353 if (err)
3354 goto out;
3355
3356 if (split == ee_block) {
3357 /*
3358 * case b: block @split is the block that the extent begins with
3359 * then we just change the state of the extent, and splitting
3360 * is not needed.
3361 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003362 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3363 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003364 else
3365 ext4_ext_mark_initialized(ex);
3366
3367 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003368 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003369
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003370 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003371 goto out;
3372 }
3373
3374 /* case a */
3375 memcpy(&orig_ex, ex, sizeof(orig_ex));
3376 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003377 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3378 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003379
3380 /*
3381 * path may lead to new leaf, not to original leaf any more
3382 * after ext4_ext_insert_extent() returns,
3383 */
3384 err = ext4_ext_dirty(handle, inode, path + depth);
3385 if (err)
3386 goto fix_extent_len;
3387
3388 ex2 = &newex;
3389 ex2->ee_block = cpu_to_le32(split);
3390 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3391 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003392 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3393 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003394
Theodore Ts'odfe50802014-09-01 14:37:09 -04003395 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003396 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003397 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003398 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003399 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003400 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003401 zero_ex.ee_len = cpu_to_le16(
3402 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003403 ext4_ext_store_pblock(&zero_ex,
3404 ext4_ext_pblock(ex2));
3405 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003406 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003407 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003408 zero_ex.ee_len = cpu_to_le16(
3409 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003410 ext4_ext_store_pblock(&zero_ex,
3411 ext4_ext_pblock(ex));
3412 }
3413 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003414 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003415 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003416 zero_ex.ee_len = cpu_to_le16(
3417 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003418 ext4_ext_store_pblock(&zero_ex,
3419 ext4_ext_pblock(&orig_ex));
3420 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003421
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003422 if (err)
3423 goto fix_extent_len;
3424 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003425 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003426 ext4_ext_try_to_merge(handle, inode, path, ex);
3427 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003428 if (err)
3429 goto fix_extent_len;
3430
3431 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003432 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003433
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003434 goto out;
3435 } else if (err)
3436 goto fix_extent_len;
3437
3438out:
3439 ext4_ext_show_leaf(inode, path);
3440 return err;
3441
3442fix_extent_len:
3443 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003444 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003445 return err;
3446}
3447
3448/*
3449 * ext4_split_extents() splits an extent and mark extent which is covered
3450 * by @map as split_flags indicates
3451 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003452 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003453 * There are three possibilities:
3454 * a> There is no split required
3455 * b> Splits in two extents: Split is happening at either end of the extent
3456 * c> Splits in three extents: Somone is splitting in middle of the extent
3457 *
3458 */
3459static int ext4_split_extent(handle_t *handle,
3460 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003461 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003462 struct ext4_map_blocks *map,
3463 int split_flag,
3464 int flags)
3465{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003466 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003467 ext4_lblk_t ee_block;
3468 struct ext4_extent *ex;
3469 unsigned int ee_len, depth;
3470 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003471 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003472 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003473 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003474
3475 depth = ext_depth(inode);
3476 ex = path[depth].p_ext;
3477 ee_block = le32_to_cpu(ex->ee_block);
3478 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003479 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003480
3481 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003482 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003483 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003484 if (unwritten)
3485 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3486 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003487 if (split_flag & EXT4_EXT_DATA_VALID2)
3488 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003489 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003490 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003491 if (err)
3492 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003493 } else {
3494 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003495 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003496 /*
3497 * Update path is required because previous ext4_split_extent_at() may
3498 * result in split of original leaf or extent zeroout.
3499 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003500 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003501 if (IS_ERR(path))
3502 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003503 depth = ext_depth(inode);
3504 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003505 if (!ex) {
3506 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3507 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003508 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003509 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003510 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003511 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003512
3513 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003514 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003515 if (unwritten) {
3516 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003517 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003518 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003519 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003520 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003521 map->m_lblk, split_flag1, flags);
3522 if (err)
3523 goto out;
3524 }
3525
3526 ext4_ext_show_leaf(inode, path);
3527out:
Zheng Liu3a225672013-03-10 21:20:23 -04003528 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003529}
3530
Amit Arora56055d32007-07-17 21:42:38 -04003531/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003532 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003533 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003534 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003535 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003536 * There are three possibilities:
3537 * a> There is no split required: Entire extent should be initialized
3538 * b> Splits in two extents: Write is happening at either end of the extent
3539 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003540 *
3541 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003542 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003543 * - The extent pointed to by 'path' contains a superset
3544 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3545 *
3546 * Post-conditions on success:
3547 * - the returned value is the number of blocks beyond map->l_lblk
3548 * that are allocated and initialized.
3549 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003550 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003551static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003552 struct inode *inode,
3553 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003554 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003555 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003556{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003557 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003558 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003559 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003560 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003561 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003562 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003563 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003564 unsigned int ee_len, depth, map_len = map->m_len;
3565 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003566 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003567 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003568
3569 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3570 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003571 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003572
Zheng Liu67a5da52012-08-17 09:54:17 -04003573 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003574 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3575 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003576 if (eof_block < map->m_lblk + map_len)
3577 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003578
3579 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003580 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003581 ex = path[depth].p_ext;
3582 ee_block = le32_to_cpu(ex->ee_block);
3583 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003584 zero_ex1.ee_len = 0;
3585 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003586
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003587 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3588
3589 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003590 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003591 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003592
3593 /*
3594 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003595 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003596 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003597 * memmove() calls. Transferring to the left is the common case in
3598 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3599 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003600 *
3601 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003602 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003603 * This would require removing the extent if the transfer
3604 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003605 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003606 * same extent tree node.
3607 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003608 if ((map->m_lblk == ee_block) &&
3609 /* See if we can merge left */
3610 (map_len < ee_len) && /*L1*/
3611 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003612 ext4_lblk_t prev_lblk;
3613 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003614 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003615
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003616 abut_ex = ex - 1;
3617 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3618 prev_len = ext4_ext_get_actual_len(abut_ex);
3619 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003620 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003621
3622 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003623 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003624 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003625 * - C1: abut_ex is initialized,
3626 * - C2: abut_ex is logically abutting ex,
3627 * - C3: abut_ex is physically abutting ex,
3628 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003629 * overflowing the (initialized) length limit.
3630 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003631 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003632 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3633 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003634 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003635 err = ext4_ext_get_access(handle, inode, path + depth);
3636 if (err)
3637 goto out;
3638
3639 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003640 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003641
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003642 /* Shift the start of ex by 'map_len' blocks */
3643 ex->ee_block = cpu_to_le32(ee_block + map_len);
3644 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3645 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003646 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003647
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003648 /* Extend abut_ex by 'map_len' blocks */
3649 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003650
3651 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003652 allocated = map_len;
3653 }
3654 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3655 (map_len < ee_len) && /*L1*/
3656 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3657 /* See if we can merge right */
3658 ext4_lblk_t next_lblk;
3659 ext4_fsblk_t next_pblk, ee_pblk;
3660 unsigned int next_len;
3661
3662 abut_ex = ex + 1;
3663 next_lblk = le32_to_cpu(abut_ex->ee_block);
3664 next_len = ext4_ext_get_actual_len(abut_ex);
3665 next_pblk = ext4_ext_pblock(abut_ex);
3666 ee_pblk = ext4_ext_pblock(ex);
3667
3668 /*
3669 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3670 * upon those conditions:
3671 * - C1: abut_ex is initialized,
3672 * - C2: abut_ex is logically abutting ex,
3673 * - C3: abut_ex is physically abutting ex,
3674 * - C4: abut_ex can receive the additional blocks without
3675 * overflowing the (initialized) length limit.
3676 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003677 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003678 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3679 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3680 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3681 err = ext4_ext_get_access(handle, inode, path + depth);
3682 if (err)
3683 goto out;
3684
3685 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3686 map, ex, abut_ex);
3687
3688 /* Shift the start of abut_ex by 'map_len' blocks */
3689 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3690 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3691 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003692 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003693
3694 /* Extend abut_ex by 'map_len' blocks */
3695 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3696
3697 /* Result: number of initialized blocks past m_lblk */
3698 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003699 }
3700 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003701 if (allocated) {
3702 /* Mark the block containing both extents as dirty */
3703 ext4_ext_dirty(handle, inode, path + depth);
3704
3705 /* Update path to point to the right extent */
3706 path[depth].p_ext = abut_ex;
3707 goto out;
3708 } else
3709 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003710
Yongqiang Yang667eff32011-05-03 12:25:07 -04003711 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003712 /*
3713 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003714 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003715 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003716 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003717
Zheng Liu67a5da52012-08-17 09:54:17 -04003718 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3719 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003720 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003721
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303722 if (IS_ENCRYPTED(inode))
Theodore Ts'o36086d42015-10-03 10:49:29 -04003723 max_zeroout = 0;
3724
Amit Arora56055d32007-07-17 21:42:38 -04003725 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003726 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003727 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003728 * 2. split the extent into two extents, zeroout the head of the first
3729 * extent.
3730 * 3. split the extent into two extents, zeroout the tail of the second
3731 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003732 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003733 * 5. no splitting needed, just possibly zeroout the head and / or the
3734 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003735 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003736 split_map.m_lblk = map->m_lblk;
3737 split_map.m_len = map->m_len;
3738
Jan Kara4f8caa62017-05-26 17:40:52 -04003739 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003740 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003741 /* case 3 or 5 */
3742 zero_ex1.ee_block =
3743 cpu_to_le32(split_map.m_lblk +
3744 split_map.m_len);
3745 zero_ex1.ee_len =
3746 cpu_to_le16(allocated - split_map.m_len);
3747 ext4_ext_store_pblock(&zero_ex1,
3748 ext4_ext_pblock(ex) + split_map.m_lblk +
3749 split_map.m_len - ee_block);
3750 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003751 if (err)
3752 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003753 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003754 }
3755 if (split_map.m_lblk - ee_block + split_map.m_len <
3756 max_zeroout) {
3757 /* case 2 or 5 */
3758 if (split_map.m_lblk != ee_block) {
3759 zero_ex2.ee_block = ex->ee_block;
3760 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003761 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003762 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003763 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003764 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003765 if (err)
3766 goto out;
3767 }
3768
Jan Kara4f8caa62017-05-26 17:40:52 -04003769 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003770 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003771 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003772 }
3773 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003774
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003775 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3776 flags);
3777 if (err > 0)
3778 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003779out:
Zheng Liuadb23552013-03-10 21:13:05 -04003780 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003781 if (!err) {
3782 err = ext4_zeroout_es(inode, &zero_ex1);
3783 if (!err)
3784 err = ext4_zeroout_es(inode, &zero_ex2);
3785 }
Amit Arora56055d32007-07-17 21:42:38 -04003786 return err ? err : allocated;
3787}
3788
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003789/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003790 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003791 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003792 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003793 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003794 * Writing to an unwritten extent may result in splitting the unwritten
3795 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003796 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003797 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003798 * b> Splits in two extents: Write is happening at either end of the extent
3799 * c> Splits in three extents: Somone is writing in middle of the extent
3800 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003801 * This works the same way in the case of initialized -> unwritten conversion.
3802 *
Mingming Cao00314622009-09-28 15:49:08 -04003803 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003804 * the unwritten extent split. To prevent ENOSPC occur at the IO
3805 * complete, we need to split the unwritten extent before DIO submit
3806 * the IO. The unwritten extent called at this time will be split
3807 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003808 * being filled will be convert to initialized by the end_io callback function
3809 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003810 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003811 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003812 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003813static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003814 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003815 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003816 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003817 int flags)
3818{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003819 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003820 ext4_lblk_t eof_block;
3821 ext4_lblk_t ee_block;
3822 struct ext4_extent *ex;
3823 unsigned int ee_len;
3824 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003825
Lukas Czernerb8a86842014-03-18 18:05:35 -04003826 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3827 __func__, inode->i_ino,
3828 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003829
3830 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3831 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003832 if (eof_block < map->m_lblk + map->m_len)
3833 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003834 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003835 * It is safe to convert extent to initialized via explicit
3836 * zeroout only if extent is fully insde i_size or new_size.
3837 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003838 depth = ext_depth(inode);
3839 ex = path[depth].p_ext;
3840 ee_block = le32_to_cpu(ex->ee_block);
3841 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003842
Lukas Czernerb8a86842014-03-18 18:05:35 -04003843 /* Convert to unwritten */
3844 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3845 split_flag |= EXT4_EXT_DATA_VALID1;
3846 /* Convert to initialized */
3847 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3848 split_flag |= ee_block + ee_len <= eof_block ?
3849 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003850 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003851 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003852 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003853 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003854}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003855
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003856static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003857 struct inode *inode,
3858 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003859 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003860{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003861 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003862 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003863 ext4_lblk_t ee_block;
3864 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003865 int depth;
3866 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003867
3868 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003869 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003870 ee_block = le32_to_cpu(ex->ee_block);
3871 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003872
Yongqiang Yang197217a2011-05-03 11:45:29 -04003873 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3874 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003875 (unsigned long long)ee_block, ee_len);
3876
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003877 /* If extent is larger than requested it is a clear sign that we still
3878 * have some extent state machine issues left. So extent_split is still
3879 * required.
3880 * TODO: Once all related issues will be fixed this situation should be
3881 * illegal.
3882 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003883 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003884#ifdef CONFIG_EXT4_DEBUG
3885 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003886 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003887 inode->i_ino, (unsigned long long)ee_block, ee_len,
3888 (unsigned long long)map->m_lblk, map->m_len);
3889#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003890 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003891 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003892 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003893 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003894 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003895 if (IS_ERR(path))
3896 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003897 depth = ext_depth(inode);
3898 ex = path[depth].p_ext;
3899 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003900
Mingming Cao00314622009-09-28 15:49:08 -04003901 err = ext4_ext_get_access(handle, inode, path + depth);
3902 if (err)
3903 goto out;
3904 /* first mark the extent as initialized */
3905 ext4_ext_mark_initialized(ex);
3906
Yongqiang Yang197217a2011-05-03 11:45:29 -04003907 /* note: ext4_ext_correct_indexes() isn't needed here because
3908 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003909 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003910 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003911
Mingming Cao00314622009-09-28 15:49:08 -04003912 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003913 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003914out:
3915 ext4_ext_show_leaf(inode, path);
3916 return err;
3917}
3918
Theodore Ts'o58590b02010-10-27 21:23:12 -04003919/*
3920 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3921 */
3922static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003923 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003924 struct ext4_ext_path *path,
3925 unsigned int len)
3926{
3927 int i, depth;
3928 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003929 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003930
3931 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3932 return 0;
3933
3934 depth = ext_depth(inode);
3935 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003936
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003937 /*
3938 * We're going to remove EOFBLOCKS_FL entirely in future so we
3939 * do not care for this case anymore. Simply remove the flag
3940 * if there are no extents.
3941 */
3942 if (unlikely(!eh->eh_entries))
3943 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003944 last_ex = EXT_LAST_EXTENT(eh);
3945 /*
3946 * We should clear the EOFBLOCKS_FL flag if we are writing the
3947 * last block in the last extent in the file. We test this by
3948 * first checking to see if the caller to
3949 * ext4_ext_get_blocks() was interested in the last block (or
3950 * a block beyond the last block) in the current extent. If
3951 * this turns out to be false, we can bail out from this
3952 * function immediately.
3953 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003954 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003955 ext4_ext_get_actual_len(last_ex))
3956 return 0;
3957 /*
3958 * If the caller does appear to be planning to write at or
3959 * beyond the end of the current extent, we then test to see
3960 * if the current extent is the last extent in the file, by
3961 * checking to make sure it was reached via the rightmost node
3962 * at each level of the tree.
3963 */
3964 for (i = depth-1; i >= 0; i--)
3965 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3966 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003967out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003968 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3969 return ext4_mark_inode_dirty(handle, inode);
3970}
3971
Mingming Cao00314622009-09-28 15:49:08 -04003972static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003973convert_initialized_extent(handle_t *handle, struct inode *inode,
3974 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003975 struct ext4_ext_path **ppath,
Eryu Guan56263b42016-02-12 01:23:00 -05003976 unsigned int allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003977{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003978 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003979 struct ext4_extent *ex;
3980 ext4_lblk_t ee_block;
3981 unsigned int ee_len;
3982 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003983 int err = 0;
3984
3985 /*
3986 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003987 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003988 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003989 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3990 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003991
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003992 depth = ext_depth(inode);
3993 ex = path[depth].p_ext;
3994 ee_block = le32_to_cpu(ex->ee_block);
3995 ee_len = ext4_ext_get_actual_len(ex);
3996
3997 ext_debug("%s: inode %lu, logical"
3998 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3999 (unsigned long long)ee_block, ee_len);
4000
4001 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004002 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004003 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
4004 if (err < 0)
4005 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004006 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004007 if (IS_ERR(path))
4008 return PTR_ERR(path);
4009 depth = ext_depth(inode);
4010 ex = path[depth].p_ext;
4011 if (!ex) {
4012 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
4013 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004014 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004015 }
4016 }
4017
4018 err = ext4_ext_get_access(handle, inode, path + depth);
4019 if (err)
4020 return err;
4021 /* first mark the extent as unwritten */
4022 ext4_ext_mark_unwritten(ex);
4023
4024 /* note: ext4_ext_correct_indexes() isn't needed here because
4025 * borders are not changed
4026 */
4027 ext4_ext_try_to_merge(handle, inode, path, ex);
4028
4029 /* Mark modified extent as dirty */
4030 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4031 if (err)
4032 return err;
4033 ext4_ext_show_leaf(inode, path);
4034
4035 ext4_update_inode_fsync_trans(handle, inode, 1);
4036 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4037 if (err)
4038 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004039 map->m_flags |= EXT4_MAP_UNWRITTEN;
4040 if (allocated > map->m_len)
4041 allocated = map->m_len;
4042 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004043 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004044}
4045
4046static int
Lukas Czerner556615d2014-04-20 23:45:47 -04004047ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004048 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004049 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004050 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04004051{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004052 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004053 int ret = 0;
4054 int err = 0;
4055
Lukas Czerner556615d2014-04-20 23:45:47 -04004056 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004057 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004058 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004059 flags, allocated);
4060 ext4_ext_show_leaf(inode, path);
4061
Lukas Czerner27dd4382013-04-09 22:11:22 -04004062 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004063 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004064 * allocate metadata blocks for the new extent block if needed.
4065 */
4066 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4067
Lukas Czerner556615d2014-04-20 23:45:47 -04004068 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004069 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004070
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004071 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004072 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004073 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4074 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004075 if (ret <= 0)
4076 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004077 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004078 goto out;
4079 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004080 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004081 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05004082 if (flags & EXT4_GET_BLOCKS_ZERO) {
4083 if (allocated > map->m_len)
4084 allocated = map->m_len;
4085 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4086 allocated);
4087 if (err < 0)
4088 goto out2;
4089 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004090 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004091 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004092 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004093 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004094 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4095 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004096 } else
4097 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004098 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004099 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004100 if (allocated > map->m_len)
4101 allocated = map->m_len;
4102 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004103 goto out2;
4104 }
4105 /* buffered IO case */
4106 /*
4107 * repeat fallocate creation request
4108 * we already have an unwritten extent
4109 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004110 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004111 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004112 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004113 }
Mingming Cao00314622009-09-28 15:49:08 -04004114
4115 /* buffered READ or buffered write_begin() lookup */
4116 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4117 /*
4118 * We have blocks reserved already. We
4119 * return allocated blocks so that delalloc
4120 * won't do block reservation for us. But
4121 * the buffer head will be unmapped so that
4122 * a read from the block returns 0s.
4123 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004124 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004125 goto out1;
4126 }
4127
4128 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004129 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004130 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004131 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004132out:
4133 if (ret <= 0) {
4134 err = ret;
4135 goto out2;
4136 } else
4137 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004138 map->m_flags |= EXT4_MAP_NEW;
zhangyi (F)16e08b12019-02-10 23:32:07 -05004139 if (allocated > map->m_len)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004140 allocated = map->m_len;
Zheng Liu3a225672013-03-10 21:20:23 -04004141 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004142
Mingming Cao00314622009-09-28 15:49:08 -04004143map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004144 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004145 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4146 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4147 map->m_len);
4148 if (err < 0)
4149 goto out2;
4150 }
Mingming Cao00314622009-09-28 15:49:08 -04004151out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004152 if (allocated > map->m_len)
4153 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004154 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004155 map->m_pblk = newblock;
4156 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004157out2:
Mingming Cao00314622009-09-28 15:49:08 -04004158 return err ? err : allocated;
4159}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004160
Mingming Cao00314622009-09-28 15:49:08 -04004161/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004162 * get_implied_cluster_alloc - check to see if the requested
4163 * allocation (in the map structure) overlaps with a cluster already
4164 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004165 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004166 * @map The requested lblk->pblk mapping
4167 * @ex The extent structure which might contain an implied
4168 * cluster allocation
4169 *
4170 * This function is called by ext4_ext_map_blocks() after we failed to
4171 * find blocks that were already in the inode's extent tree. Hence,
4172 * we know that the beginning of the requested region cannot overlap
4173 * the extent from the inode's extent tree. There are three cases we
4174 * want to catch. The first is this case:
4175 *
4176 * |--- cluster # N--|
4177 * |--- extent ---| |---- requested region ---|
4178 * |==========|
4179 *
4180 * The second case that we need to test for is this one:
4181 *
4182 * |--------- cluster # N ----------------|
4183 * |--- requested region --| |------- extent ----|
4184 * |=======================|
4185 *
4186 * The third case is when the requested region lies between two extents
4187 * within the same cluster:
4188 * |------------- cluster # N-------------|
4189 * |----- ex -----| |---- ex_right ----|
4190 * |------ requested region ------|
4191 * |================|
4192 *
4193 * In each of the above cases, we need to set the map->m_pblk and
4194 * map->m_len so it corresponds to the return the extent labelled as
4195 * "|====|" from cluster #N, since it is already in use for data in
4196 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4197 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4198 * as a new "allocated" block region. Otherwise, we will return 0 and
4199 * ext4_ext_map_blocks() will then allocate one or more new clusters
4200 * by calling ext4_mb_new_blocks().
4201 */
Aditya Kalid8990242011-09-09 19:18:51 -04004202static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004203 struct ext4_map_blocks *map,
4204 struct ext4_extent *ex,
4205 struct ext4_ext_path *path)
4206{
Aditya Kalid8990242011-09-09 19:18:51 -04004207 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004208 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004209 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004210 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004211 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4212 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4213 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4214
4215 /* The extent passed in that we are trying to match */
4216 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4217 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4218
4219 /* The requested region passed into ext4_map_blocks() */
4220 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004221
4222 if ((rr_cluster_start == ex_cluster_end) ||
4223 (rr_cluster_start == ex_cluster_start)) {
4224 if (rr_cluster_start == ex_cluster_end)
4225 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004226 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004227 map->m_len = min(map->m_len,
4228 (unsigned) sbi->s_cluster_ratio - c_offset);
4229 /*
4230 * Check for and handle this case:
4231 *
4232 * |--------- cluster # N-------------|
4233 * |------- extent ----|
4234 * |--- requested region ---|
4235 * |===========|
4236 */
4237
4238 if (map->m_lblk < ee_block)
4239 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4240
4241 /*
4242 * Check for the case where there is already another allocated
4243 * block to the right of 'ex' but before the end of the cluster.
4244 *
4245 * |------------- cluster # N-------------|
4246 * |----- ex -----| |---- ex_right ----|
4247 * |------ requested region ------|
4248 * |================|
4249 */
4250 if (map->m_lblk > ee_block) {
4251 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4252 map->m_len = min(map->m_len, next - map->m_lblk);
4253 }
Aditya Kalid8990242011-09-09 19:18:51 -04004254
4255 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004256 return 1;
4257 }
Aditya Kalid8990242011-09-09 19:18:51 -04004258
4259 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004260 return 0;
4261}
4262
4263
4264/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004265 * Block allocation/map/preallocation routine for extents based files
4266 *
4267 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004268 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004269 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4270 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004271 *
4272 * return > 0, number of of blocks already mapped/allocated
4273 * if create == 0 and these are pre-allocated blocks
4274 * buffer head is unmapped
4275 * otherwise blocks are mapped
4276 *
4277 * return = 0, if plain look up failed (blocks have not been allocated)
4278 * buffer head is unmapped
4279 *
4280 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004281 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004282int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4283 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004284{
4285 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004286 struct ext4_extent newex, *ex, *ex2;
4287 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004288 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004289 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004290 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004291 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004292 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004293 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004294 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004295
Mingming84fe3be2009-09-01 08:44:37 -04004296 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004297 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004298 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004299
Alex Tomasa86c6182006-10-11 01:21:03 -07004300 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004301 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004302 if (IS_ERR(path)) {
4303 err = PTR_ERR(path);
4304 path = NULL;
4305 goto out2;
4306 }
4307
4308 depth = ext_depth(inode);
4309
4310 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004311 * consistent leaf must not be empty;
4312 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004313 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004314 */
Frank Mayhar273df552010-03-02 11:46:09 -05004315 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4316 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004317 "lblock: %lu, depth: %d pblock %lld",
4318 (unsigned long) map->m_lblk, depth,
4319 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004320 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004321 goto out2;
4322 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004323
Avantika Mathur7e028972006-12-06 20:41:33 -08004324 ex = path[depth].p_ext;
4325 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004326 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004327 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004328 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004329
Lukas Czernerb8a86842014-03-18 18:05:35 -04004330
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004331 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004332 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004333 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004334 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004335 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004336
4337 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4338
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004339 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004340 if (in_range(map->m_lblk, ee_block, ee_len)) {
4341 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004342 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004343 allocated = ee_len - (map->m_lblk - ee_block);
4344 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4345 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004346
Lukas Czernerb8a86842014-03-18 18:05:35 -04004347 /*
4348 * If the extent is initialized check whether the
4349 * caller wants to convert it to unwritten.
4350 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004351 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004352 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004353 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004354 handle, inode, map, &path,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05004355 allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004356 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004357 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004358 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004359
Lukas Czerner556615d2014-04-20 23:45:47 -04004360 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004361 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004362 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004363 if (ret < 0)
4364 err = ret;
4365 else
4366 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004367 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004368 }
4369 }
4370
4371 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004372 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004373 * we couldn't try to create block if create flag is zero
4374 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004375 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004376 ext4_lblk_t hole_start, hole_len;
4377
Jan Karafacab4d2016-03-09 22:54:00 -05004378 hole_start = map->m_lblk;
4379 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004380 /*
4381 * put just found gap into cache to speed up
4382 * subsequent requests
4383 */
Jan Kara140a5252016-03-09 22:46:57 -05004384 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004385
4386 /* Update hole_len to reflect hole size after map->m_lblk */
4387 if (hole_start != map->m_lblk)
4388 hole_len -= map->m_lblk - hole_start;
4389 map->m_pblk = 0;
4390 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4391
Alex Tomasa86c6182006-10-11 01:21:03 -07004392 goto out2;
4393 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004394
Alex Tomasa86c6182006-10-11 01:21:03 -07004395 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004396 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004397 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004398 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004399 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004400
4401 /*
4402 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004403 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004404 */
4405 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004406 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004407 ar.len = allocated = map->m_len;
4408 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004409 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004410 goto got_allocated_blocks;
4411 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004412
Alex Tomasc9de5602008-01-29 00:19:52 -05004413 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004414 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004415 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4416 if (err)
4417 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004418 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004419 ex2 = NULL;
4420 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004421 if (err)
4422 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004423
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004424 /* Check if the extent after searching to the right implies a
4425 * cluster we can use. */
4426 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004427 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004428 ar.len = allocated = map->m_len;
4429 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004430 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004431 goto got_allocated_blocks;
4432 }
4433
Amit Arora749269f2007-07-18 09:02:56 -04004434 /*
4435 * See if request is beyond maximum number of blocks we can have in
4436 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004437 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4438 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004439 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004440 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004441 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004442 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004443 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4444 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4445 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004446
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004447 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004448 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004449 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004450 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004451 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004452 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004453 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004454
4455 /* allocate new block */
4456 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004457 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4458 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004459 /*
4460 * We calculate the offset from the beginning of the cluster
4461 * for the logical block number, since when we allocate a
4462 * physical cluster, the physical block should start at the
4463 * same offset from the beginning of the cluster. This is
4464 * needed so that future calls to get_implied_cluster_alloc()
4465 * work correctly.
4466 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004467 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004468 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4469 ar.goal -= offset;
4470 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004471 if (S_ISREG(inode->i_mode))
4472 ar.flags = EXT4_MB_HINT_DATA;
4473 else
4474 /* disable in-core preallocation for non-regular files */
4475 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004476 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4477 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004478 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4479 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004480 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4481 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004482 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004483 if (!newblock)
4484 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004485 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004486 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004487 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004488 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004489 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4490 if (ar.len > allocated)
4491 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004492
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004493got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004494 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004495 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004496 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004497 /* Mark unwritten */
4498 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4499 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004500 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004501 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004502
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004503 err = 0;
4504 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4505 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4506 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004507 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004508 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004509 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004510
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004511 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004512 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4513 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004514 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004515 /* not a good idea to call discard here directly,
4516 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004517 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004518 ext4_free_blocks(handle, inode, NULL, newblock,
4519 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004520 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004521 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004522
Alex Tomasa86c6182006-10-11 01:21:03 -07004523 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004524 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004525 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004526 if (allocated > map->m_len)
4527 allocated = map->m_len;
4528 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004529
Jan Karab436b9b2009-12-08 23:51:10 -05004530 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004531 * Reduce the reserved cluster count to reflect successful deferred
4532 * allocation of delayed allocated clusters or direct allocation of
4533 * clusters discovered to be delayed allocated. Once allocated, a
4534 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004535 */
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004536 if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) {
4537 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004538 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004539 * When allocating delayed allocated clusters, simply
4540 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004541 */
4542 ext4_da_update_reserve_space(inode, allocated_clusters,
4543 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004544 } else {
4545 ext4_lblk_t lblk, len;
4546 unsigned int n;
4547
4548 /*
4549 * When allocating non-delayed allocated clusters
4550 * (from fallocate, filemap, DIO, or clusters
4551 * allocated when delalloc has been disabled by
4552 * ext4_nonda_switch), reduce the reserved cluster
4553 * count by the number of allocated clusters that
4554 * have previously been delayed allocated. Quota
4555 * has been claimed by ext4_mb_new_blocks() above,
4556 * so release the quota reservations made for any
4557 * previously delayed allocated clusters.
4558 */
4559 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4560 len = allocated_clusters << sbi->s_cluster_bits;
4561 n = ext4_es_delayed_clu(inode, lblk, len);
4562 if (n > 0)
4563 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004564 }
4565 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004566
4567 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004568 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004569 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004570 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004571 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004572 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004573 else
Jan Karab436b9b2009-12-08 23:51:10 -05004574 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004575out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004576 if (allocated > map->m_len)
4577 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004578 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004579 map->m_flags |= EXT4_MAP_MAPPED;
4580 map->m_pblk = newblock;
4581 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004582out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004583 ext4_ext_drop_refs(path);
4584 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004585
Theodore Ts'o63b99962013-07-16 10:28:47 -04004586 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4587 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004588 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004589}
4590
Theodore Ts'od0abb362016-11-13 22:02:28 -05004591int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004592{
Alex Tomasa86c6182006-10-11 01:21:03 -07004593 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004594 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004595 int err = 0;
4596
4597 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004598 * TODO: optimization is possible here.
4599 * Probably we need not scan at all,
4600 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004601 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004602
4603 /* we have to know where to truncate from in crash case */
4604 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004605 err = ext4_mark_inode_dirty(handle, inode);
4606 if (err)
4607 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004608
4609 last_block = (inode->i_size + sb->s_blocksize - 1)
4610 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004611retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004612 err = ext4_es_remove_extent(inode, last_block,
4613 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004614 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004615 cond_resched();
4616 congestion_wait(BLK_RW_ASYNC, HZ/50);
4617 goto retry;
4618 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004619 if (err)
4620 return err;
4621 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004622}
4623
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004624static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004625 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004626 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004627{
Al Viro496ad9a2013-01-23 17:07:38 -05004628 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004629 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004630 int ret = 0;
4631 int ret2 = 0;
4632 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004633 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004634 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004635 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004636 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004637
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004638 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004639 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004640 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004641 /*
4642 * Don't normalize the request if it can fit in one extent so
4643 * that it doesn't get unnecessarily split into multiple
4644 * extents.
4645 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004646 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004647 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004648
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004649 /*
4650 * credits to insert 1 extent into extent tree
4651 */
4652 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004653 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004654
Amit Aroraa2df2a62007-07-17 21:42:41 -04004655retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004656 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004657 /*
4658 * Recalculate credits when extent tree depth changes.
4659 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004660 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004661 credits = ext4_chunk_trans_blocks(inode, len);
4662 depth = ext_depth(inode);
4663 }
4664
Theodore Ts'o9924a922013-02-08 21:59:22 -05004665 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4666 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004667 if (IS_ERR(handle)) {
4668 ret = PTR_ERR(handle);
4669 break;
4670 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004671 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004672 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004673 ext4_debug("inode #%lu: block %u: len %u: "
4674 "ext4_ext_map_blocks returned %d",
4675 inode->i_ino, map.m_lblk,
4676 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004677 ext4_mark_inode_dirty(handle, inode);
4678 ret2 = ext4_journal_stop(handle);
4679 break;
4680 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004681 map.m_lblk += ret;
4682 map.m_len = len = len - ret;
4683 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004684 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004685 if (new_size) {
4686 if (epos > new_size)
4687 epos = new_size;
4688 if (ext4_update_inode_size(inode, epos) & 0x1)
4689 inode->i_mtime = inode->i_ctime;
4690 } else {
4691 if (epos > inode->i_size)
4692 ext4_set_inode_flag(inode,
4693 EXT4_INODE_EOFBLOCKS);
4694 }
4695 ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004696 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004697 ret2 = ext4_journal_stop(handle);
4698 if (ret2)
4699 break;
4700 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004701 if (ret == -ENOSPC &&
4702 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4703 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004704 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004705 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004706
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004707 return ret > 0 ? ret2 : ret;
4708}
4709
Lukas Czernerb8a86842014-03-18 18:05:35 -04004710static long ext4_zero_range(struct file *file, loff_t offset,
4711 loff_t len, int mode)
4712{
4713 struct inode *inode = file_inode(file);
4714 handle_t *handle = NULL;
4715 unsigned int max_blocks;
4716 loff_t new_size = 0;
4717 int ret = 0;
4718 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004719 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004720 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004721 loff_t start, end;
4722 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004723 unsigned int blkbits = inode->i_blkbits;
4724
4725 trace_ext4_zero_range(inode, offset, len, mode);
4726
jon ernst6c5e73d2014-04-18 11:50:35 -04004727 if (!S_ISREG(inode->i_mode))
4728 return -EINVAL;
4729
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004730 /* Call ext4_force_commit to flush all data in case of data=journal. */
4731 if (ext4_should_journal_data(inode)) {
4732 ret = ext4_force_commit(inode->i_sb);
4733 if (ret)
4734 return ret;
4735 }
4736
Lukas Czernerb8a86842014-03-18 18:05:35 -04004737 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004738 * Round up offset. This is not fallocate, we neet to zero out
4739 * blocks, so convert interior block aligned part of the range to
4740 * unwritten and possibly manually zero out unaligned parts of the
4741 * range.
4742 */
4743 start = round_up(offset, 1 << blkbits);
4744 end = round_down((offset + len), 1 << blkbits);
4745
4746 if (start < offset || end > offset + len)
4747 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004748 partial_begin = offset & ((1 << blkbits) - 1);
4749 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004750
4751 lblk = start >> blkbits;
4752 max_blocks = (end >> blkbits);
4753 if (max_blocks < lblk)
4754 max_blocks = 0;
4755 else
4756 max_blocks -= lblk;
4757
Al Viro59551022016-01-22 15:40:57 -05004758 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004759
4760 /*
4761 * Indirect files do not support unwritten extnets
4762 */
4763 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4764 ret = -EOPNOTSUPP;
4765 goto out_mutex;
4766 }
4767
4768 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004769 (offset + len > i_size_read(inode) ||
4770 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004771 new_size = offset + len;
4772 ret = inode_newsize_ok(inode, new_size);
4773 if (ret)
4774 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004775 }
4776
Lukas Czerner0f2af212015-04-03 00:09:13 -04004777 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4778 if (mode & FALLOC_FL_KEEP_SIZE)
4779 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4780
Jan Kara17048e82015-12-07 14:29:17 -05004781 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004782 inode_dio_wait(inode);
4783
Lukas Czerner0f2af212015-04-03 00:09:13 -04004784 /* Preallocate the range including the unaligned edges */
4785 if (partial_begin || partial_end) {
4786 ret = ext4_alloc_file_blocks(file,
4787 round_down(offset, 1 << blkbits) >> blkbits,
4788 (round_up((offset + len), 1 << blkbits) -
4789 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004790 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004791 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004792 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004793
4794 }
4795
4796 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004797 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004798 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4799 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004800
Jan Karaea3d7202015-12-07 14:28:03 -05004801 /*
4802 * Prevent page faults from reinstantiating pages we have
4803 * released from page cache.
4804 */
4805 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004806
4807 ret = ext4_break_layouts(inode);
4808 if (ret) {
4809 up_write(&EXT4_I(inode)->i_mmap_sem);
4810 goto out_mutex;
4811 }
4812
Jan Kara01127842015-12-07 14:34:49 -05004813 ret = ext4_update_disksize_before_punch(inode, offset, len);
4814 if (ret) {
4815 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004816 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004817 }
Jan Karaea3d7202015-12-07 14:28:03 -05004818 /* Now release the pages and zero block aligned part of pages */
4819 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004820 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004821
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004822 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004823 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004824 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004825 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004826 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004827 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004828 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004829 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004830
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004831 /*
4832 * In worst case we have to writeout two nonadjacent unwritten
4833 * blocks and update the inode
4834 */
4835 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4836 if (ext4_should_journal_data(inode))
4837 credits += 2;
4838 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004839 if (IS_ERR(handle)) {
4840 ret = PTR_ERR(handle);
4841 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004842 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004843 }
4844
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004845 inode->i_mtime = inode->i_ctime = current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004846 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004847 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004848 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004849 /*
4850 * Mark that we allocate beyond EOF so the subsequent truncate
4851 * can proceed even if the new size is the same as i_size.
4852 */
4853 if ((offset + len) > i_size_read(inode))
4854 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4855 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004856 ext4_mark_inode_dirty(handle, inode);
4857
4858 /* Zero out partial block at the edges of the range */
4859 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004860 if (ret >= 0)
4861 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004862
4863 if (file->f_flags & O_SYNC)
4864 ext4_handle_sync(handle);
4865
4866 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004867out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004868 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004869 return ret;
4870}
4871
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004872/*
4873 * preallocate space for a file. This implements ext4's fallocate file
4874 * operation, which gets called from sys_fallocate system call.
4875 * For block-mapped files, posix_fallocate should fall back to the method
4876 * of writing zeroes to the required new blocks (the same behavior which is
4877 * expected for file systems which do not support fallocate() system call).
4878 */
4879long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4880{
4881 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004882 loff_t new_size = 0;
4883 unsigned int max_blocks;
4884 int ret = 0;
4885 int flags;
4886 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004887 unsigned int blkbits = inode->i_blkbits;
4888
Michael Halcrow2058f832015-04-12 00:55:10 -04004889 /*
4890 * Encrypted inodes can't handle collapse range or insert
4891 * range since we would need to re-encrypt blocks with a
4892 * different IV or XTS tweak (which are based on the logical
4893 * block number).
4894 *
4895 * XXX It's not clear why zero range isn't working, but we'll
4896 * leave it disabled for encrypted inodes for now. This is a
4897 * bug we should fix....
4898 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304899 if (IS_ENCRYPTED(inode) &&
Namjae Jeon331573f2015-06-09 01:55:03 -04004900 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4901 FALLOC_FL_ZERO_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004902 return -EOPNOTSUPP;
4903
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004904 /* Return error if mode is not supported */
4905 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004906 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4907 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004908 return -EOPNOTSUPP;
4909
4910 if (mode & FALLOC_FL_PUNCH_HOLE)
4911 return ext4_punch_hole(inode, offset, len);
4912
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004913 ret = ext4_convert_inline_data(inode);
4914 if (ret)
4915 return ret;
4916
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004917 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4918 return ext4_collapse_range(inode, offset, len);
4919
Namjae Jeon331573f2015-06-09 01:55:03 -04004920 if (mode & FALLOC_FL_INSERT_RANGE)
4921 return ext4_insert_range(inode, offset, len);
4922
Lukas Czernerb8a86842014-03-18 18:05:35 -04004923 if (mode & FALLOC_FL_ZERO_RANGE)
4924 return ext4_zero_range(file, offset, len, mode);
4925
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004926 trace_ext4_fallocate_enter(inode, offset, len, mode);
4927 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004928
Fabian Frederick518eaa62016-09-15 11:55:01 -04004929 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004930 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004931 if (mode & FALLOC_FL_KEEP_SIZE)
4932 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4933
Al Viro59551022016-01-22 15:40:57 -05004934 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004935
Davide Italiano280227a2015-05-02 23:21:15 -04004936 /*
4937 * We only support preallocation for extent-based files only
4938 */
4939 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4940 ret = -EOPNOTSUPP;
4941 goto out;
4942 }
4943
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004944 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004945 (offset + len > i_size_read(inode) ||
4946 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004947 new_size = offset + len;
4948 ret = inode_newsize_ok(inode, new_size);
4949 if (ret)
4950 goto out;
4951 }
4952
Jan Kara17048e82015-12-07 14:29:17 -05004953 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004954 inode_dio_wait(inode);
4955
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004956 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004957 if (ret)
4958 goto out;
4959
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004960 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4961 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4962 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004963 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004964out:
Al Viro59551022016-01-22 15:40:57 -05004965 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004966 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4967 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004968}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004969
4970/*
Mingming Cao00314622009-09-28 15:49:08 -04004971 * This function convert a range of blocks to written extents
4972 * The caller of this function will pass the start offset and the size.
4973 * all unwritten extents within this range will be converted to
4974 * written extents.
4975 *
4976 * This function is called from the direct IO end io call back
4977 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004978 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004979 */
Jan Kara6b523df2013-06-04 13:21:11 -04004980int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4981 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004982{
Mingming Cao00314622009-09-28 15:49:08 -04004983 unsigned int max_blocks;
4984 int ret = 0;
4985 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004986 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304987 unsigned int blkbits = inode->i_blkbits;
4988 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004989
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004990 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004991 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4992
Ritesh Harjania00713e2019-10-16 13:07:08 +05304993 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004994 /*
4995 * credits to insert 1 extent into extent tree
4996 */
4997 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4998 }
Mingming Cao00314622009-09-28 15:49:08 -04004999 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005000 map.m_lblk += ret;
5001 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005002 if (credits) {
5003 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5004 credits);
5005 if (IS_ERR(handle)) {
5006 ret = PTR_ERR(handle);
5007 break;
5008 }
Mingming Cao00314622009-09-28 15:49:08 -04005009 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005010 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005011 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005012 if (ret <= 0)
5013 ext4_warning(inode->i_sb,
5014 "inode #%lu: block %u: len %u: "
5015 "ext4_ext_map_blocks returned %d",
5016 inode->i_ino, map.m_lblk,
5017 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005018 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005019 if (credits)
5020 ret2 = ext4_journal_stop(handle);
5021 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005022 break;
5023 }
5024 return ret > 0 ? ret2 : ret;
5025}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005026
Ritesh Harjania00713e2019-10-16 13:07:08 +05305027int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
5028{
5029 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05305030 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05305031
5032 /*
5033 * This is somewhat ugly but the idea is clear: When transaction is
5034 * reserved, everything goes into it. Otherwise we rather start several
5035 * smaller transactions for conversion of each extent separately.
5036 */
5037 if (handle) {
5038 handle = ext4_journal_start_reserved(handle,
5039 EXT4_HT_EXT_CONVERT);
5040 if (IS_ERR(handle))
5041 return PTR_ERR(handle);
5042 }
5043
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05305044 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
5045 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
5046 io_end_vec->offset,
5047 io_end_vec->size);
5048 if (ret)
5049 break;
5050 }
5051
Ritesh Harjania00713e2019-10-16 13:07:08 +05305052 if (handle)
5053 err = ext4_journal_stop(handle);
5054
5055 return ret < 0 ? ret : err;
5056}
5057
Mingming Cao00314622009-09-28 15:49:08 -04005058/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005059 * If newes is not existing extent (newes->ec_pblk equals zero) find
5060 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005061 * return start of the next delayed extent.
5062 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005063 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005064 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005065 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005066 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005067static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005068 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005069{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005070 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005071 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005072
Zheng Liu69eb33d2013-02-18 00:31:07 -05005073 if (newes->es_pblk == 0) {
Eric Whitneyad431022018-10-01 14:10:39 -04005074 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
5075 newes->es_lblk,
5076 newes->es_lblk + newes->es_len - 1,
5077 &es);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005078
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005079 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005080 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005081 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005082 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005083 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005084 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005085 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005086
Zheng Liu69eb33d2013-02-18 00:31:07 -05005087 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005088 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005089 newes->es_len = min(es.es_lblk - newes->es_lblk,
5090 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005091 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005092 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005093
Zheng Liu69eb33d2013-02-18 00:31:07 -05005094 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005095 }
5096
Zheng Liu69eb33d2013-02-18 00:31:07 -05005097 block = newes->es_lblk + newes->es_len;
Eric Whitneyad431022018-10-01 14:10:39 -04005098 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block,
5099 EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005100 if (es.es_len == 0)
5101 next_del = EXT_MAX_BLOCKS;
5102 else
5103 next_del = es.es_lblk;
5104
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005105 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005106}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005107
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005108static int ext4_xattr_fiemap(struct inode *inode,
5109 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005110{
5111 __u64 physical = 0;
5112 __u64 length;
5113 __u32 flags = FIEMAP_EXTENT_LAST;
5114 int blockbits = inode->i_sb->s_blocksize_bits;
5115 int error = 0;
5116
5117 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005118 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005119 struct ext4_iloc iloc;
5120 int offset; /* offset of xattr in inode */
5121
5122 error = ext4_get_inode_loc(inode, &iloc);
5123 if (error)
5124 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005125 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005126 offset = EXT4_GOOD_OLD_INODE_SIZE +
5127 EXT4_I(inode)->i_extra_isize;
5128 physical += offset;
5129 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5130 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005131 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005132 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005133 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005134 length = inode->i_sb->s_blocksize;
5135 }
5136
5137 if (physical)
5138 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5139 length, flags);
5140 return (error < 0 ? error : 0);
5141}
5142
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005143static int _ext4_fiemap(struct inode *inode,
5144 struct fiemap_extent_info *fieinfo,
5145 __u64 start, __u64 len,
5146 int (*fill)(struct inode *, ext4_lblk_t,
5147 ext4_lblk_t,
5148 struct fiemap_extent_info *))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005149{
5150 ext4_lblk_t start_blk;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005151 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR;
5152
Eric Sandeen6873fa02008-10-07 00:46:36 -04005153 int error = 0;
5154
Tao Ma94191982012-12-10 14:06:02 -05005155 if (ext4_has_inline_data(inode)) {
5156 int has_inline = 1;
5157
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005158 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5159 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005160
5161 if (has_inline)
5162 return error;
5163 }
5164
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005165 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5166 error = ext4_ext_precache(inode);
5167 if (error)
5168 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005169 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005170 }
5171
Eric Sandeen6873fa02008-10-07 00:46:36 -04005172 /* fallback to generic here if not in extents fmt */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005173 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
5174 fill == ext4_fill_fiemap_extents)
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005175 return generic_block_fiemap(inode, fieinfo, start, len,
5176 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005177
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005178 if (fill == ext4_fill_es_cache_info)
5179 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
5180 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005181 return -EBADR;
5182
5183 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5184 error = ext4_xattr_fiemap(inode, fieinfo);
5185 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005186 ext4_lblk_t len_blks;
5187 __u64 last_blk;
5188
Eric Sandeen6873fa02008-10-07 00:46:36 -04005189 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005190 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005191 if (last_blk >= EXT_MAX_BLOCKS)
5192 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005193 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005194
5195 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005196 * Walk the extent tree gathering extent information
5197 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005198 */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005199 error = fill(inode, start_blk, len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005200 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005201 return error;
5202}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005203
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005204int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5205 __u64 start, __u64 len)
5206{
5207 return _ext4_fiemap(inode, fieinfo, start, len,
5208 ext4_fill_fiemap_extents);
5209}
5210
5211int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
5212 __u64 start, __u64 len)
5213{
5214 if (ext4_has_inline_data(inode)) {
5215 int has_inline;
5216
5217 down_read(&EXT4_I(inode)->xattr_sem);
5218 has_inline = ext4_has_inline_data(inode);
5219 up_read(&EXT4_I(inode)->xattr_sem);
5220 if (has_inline)
5221 return 0;
5222 }
5223
5224 return _ext4_fiemap(inode, fieinfo, start, len,
5225 ext4_fill_es_cache_info);
5226}
5227
5228
Namjae Jeon9eb79482014-02-23 15:18:59 -05005229/*
5230 * ext4_access_path:
5231 * Function to access the path buffer for marking it dirty.
5232 * It also checks if there are sufficient credits left in the journal handle
5233 * to update path.
5234 */
5235static int
5236ext4_access_path(handle_t *handle, struct inode *inode,
5237 struct ext4_ext_path *path)
5238{
5239 int credits, err;
5240
5241 if (!ext4_handle_valid(handle))
5242 return 0;
5243
5244 /*
5245 * Check if need to extend journal credits
5246 * 3 for leaf, sb, and inode plus 2 (bmap and group
5247 * descriptor) for each block group; assume two block
5248 * groups
5249 */
Jan Karaa4130362019-11-05 17:44:16 +01005250 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01005251 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01005252 if (err < 0)
5253 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005254
5255 err = ext4_ext_get_access(handle, inode, path);
5256 return err;
5257}
5258
5259/*
5260 * ext4_ext_shift_path_extents:
5261 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005262 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5263 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005264 */
5265static int
5266ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5267 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005268 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005269{
5270 int depth, err = 0;
5271 struct ext4_extent *ex_start, *ex_last;
5272 bool update = 0;
5273 depth = path->p_depth;
5274
5275 while (depth >= 0) {
5276 if (depth == path->p_depth) {
5277 ex_start = path[depth].p_ext;
5278 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005279 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005280
5281 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005282
5283 err = ext4_access_path(handle, inode, path + depth);
5284 if (err)
5285 goto out;
5286
5287 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5288 update = 1;
5289
Namjae Jeon9eb79482014-02-23 15:18:59 -05005290 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005291 if (SHIFT == SHIFT_LEFT) {
5292 le32_add_cpu(&ex_start->ee_block,
5293 -shift);
5294 /* Try to merge to the left. */
5295 if ((ex_start >
5296 EXT_FIRST_EXTENT(path[depth].p_hdr))
5297 &&
5298 ext4_ext_try_to_merge_right(inode,
5299 path, ex_start - 1))
5300 ex_last--;
5301 else
5302 ex_start++;
5303 } else {
5304 le32_add_cpu(&ex_last->ee_block, shift);
5305 ext4_ext_try_to_merge_right(inode, path,
5306 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005307 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005308 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005309 }
5310 err = ext4_ext_dirty(handle, inode, path + depth);
5311 if (err)
5312 goto out;
5313
5314 if (--depth < 0 || !update)
5315 break;
5316 }
5317
5318 /* Update index too */
5319 err = ext4_access_path(handle, inode, path + depth);
5320 if (err)
5321 goto out;
5322
Namjae Jeon331573f2015-06-09 01:55:03 -04005323 if (SHIFT == SHIFT_LEFT)
5324 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5325 else
5326 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005327 err = ext4_ext_dirty(handle, inode, path + depth);
5328 if (err)
5329 goto out;
5330
5331 /* we are done if current index is not a starting index */
5332 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5333 break;
5334
5335 depth--;
5336 }
5337
5338out:
5339 return err;
5340}
5341
5342/*
5343 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005344 * All the extents which lies in the range from @start to the last allocated
5345 * block for the @inode are shifted either towards left or right (depending
5346 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005347 * On success, 0 is returned, error otherwise.
5348 */
5349static int
5350ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005351 ext4_lblk_t start, ext4_lblk_t shift,
5352 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005353{
5354 struct ext4_ext_path *path;
5355 int ret = 0, depth;
5356 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005357 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005358
5359 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005360 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5361 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005362 if (IS_ERR(path))
5363 return PTR_ERR(path);
5364
5365 depth = path->p_depth;
5366 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005367 if (!extent)
5368 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005369
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005370 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005371
Namjae Jeon331573f2015-06-09 01:55:03 -04005372 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005373 * For left shifts, make sure the hole on the left is big enough to
5374 * accommodate the shift. For right shifts, make sure the last extent
5375 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005376 */
5377 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005378 path = ext4_find_extent(inode, start - 1, &path,
5379 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005380 if (IS_ERR(path))
5381 return PTR_ERR(path);
5382 depth = path->p_depth;
5383 extent = path[depth].p_ext;
5384 if (extent) {
5385 ex_start = le32_to_cpu(extent->ee_block);
5386 ex_end = le32_to_cpu(extent->ee_block) +
5387 ext4_ext_get_actual_len(extent);
5388 } else {
5389 ex_start = 0;
5390 ex_end = 0;
5391 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005392
Namjae Jeon331573f2015-06-09 01:55:03 -04005393 if ((start == ex_start && shift > ex_start) ||
5394 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005395 ret = -EINVAL;
5396 goto out;
5397 }
5398 } else {
5399 if (shift > EXT_MAX_BLOCKS -
5400 (stop + ext4_ext_get_actual_len(extent))) {
5401 ret = -EINVAL;
5402 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005403 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005404 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005405
Namjae Jeon331573f2015-06-09 01:55:03 -04005406 /*
5407 * In case of left shift, iterator points to start and it is increased
5408 * till we reach stop. In case of right shift, iterator points to stop
5409 * and it is decreased till we reach start.
5410 */
5411 if (SHIFT == SHIFT_LEFT)
5412 iterator = &start;
5413 else
5414 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005415
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005416 /*
5417 * Its safe to start updating extents. Start and stop are unsigned, so
5418 * in case of right shift if extent with 0 block is reached, iterator
5419 * becomes NULL to indicate the end of the loop.
5420 */
5421 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005422 path = ext4_find_extent(inode, *iterator, &path,
5423 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005424 if (IS_ERR(path))
5425 return PTR_ERR(path);
5426 depth = path->p_depth;
5427 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005428 if (!extent) {
5429 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005430 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005431 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005432 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005433 if (SHIFT == SHIFT_LEFT && *iterator >
5434 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005435 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005436 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5437 path[depth].p_ext++;
5438 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005439 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005440 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005441 }
5442 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005443
5444 if (SHIFT == SHIFT_LEFT) {
5445 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5446 *iterator = le32_to_cpu(extent->ee_block) +
5447 ext4_ext_get_actual_len(extent);
5448 } else {
5449 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005450 if (le32_to_cpu(extent->ee_block) > 0)
5451 *iterator = le32_to_cpu(extent->ee_block) - 1;
5452 else
5453 /* Beginning is reached, end of the loop */
5454 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005455 /* Update path extent in case we need to stop */
5456 while (le32_to_cpu(extent->ee_block) < start)
5457 extent++;
5458 path[depth].p_ext = extent;
5459 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005460 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005461 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005462 if (ret)
5463 break;
5464 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005465out:
5466 ext4_ext_drop_refs(path);
5467 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005468 return ret;
5469}
5470
5471/*
5472 * ext4_collapse_range:
5473 * This implements the fallocate's collapse range functionality for ext4
5474 * Returns: 0 and non-zero on error.
5475 */
5476int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5477{
5478 struct super_block *sb = inode->i_sb;
5479 ext4_lblk_t punch_start, punch_stop;
5480 handle_t *handle;
5481 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005482 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005483 int ret;
5484
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005485 /*
5486 * We need to test this early because xfstests assumes that a
5487 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5488 * system does not support collapse range.
5489 */
5490 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5491 return -EOPNOTSUPP;
5492
Namjae Jeon9eb79482014-02-23 15:18:59 -05005493 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005494 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5495 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005496 return -EINVAL;
5497
5498 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005499 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005500
5501 trace_ext4_collapse_range(inode, offset, len);
5502
5503 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5504 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5505
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005506 /* Call ext4_force_commit to flush all data in case of data=journal. */
5507 if (ext4_should_journal_data(inode)) {
5508 ret = ext4_force_commit(inode->i_sb);
5509 if (ret)
5510 return ret;
5511 }
5512
Al Viro59551022016-01-22 15:40:57 -05005513 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005514 /*
5515 * There is no need to overlap collapse range with EOF, in which case
5516 * it is effectively a truncate operation
5517 */
5518 if (offset + len >= i_size_read(inode)) {
5519 ret = -EINVAL;
5520 goto out_mutex;
5521 }
5522
Namjae Jeon9eb79482014-02-23 15:18:59 -05005523 /* Currently just for extent based files */
5524 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5525 ret = -EOPNOTSUPP;
5526 goto out_mutex;
5527 }
5528
Namjae Jeon9eb79482014-02-23 15:18:59 -05005529 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005530 inode_dio_wait(inode);
5531
Jan Karaea3d7202015-12-07 14:28:03 -05005532 /*
5533 * Prevent page faults from reinstantiating pages we have released from
5534 * page cache.
5535 */
5536 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005537
5538 ret = ext4_break_layouts(inode);
5539 if (ret)
5540 goto out_mmap;
5541
Jan Kara32ebffd2015-12-07 14:31:11 -05005542 /*
5543 * Need to round down offset to be aligned with page size boundary
5544 * for page size > block size.
5545 */
5546 ioffset = round_down(offset, PAGE_SIZE);
5547 /*
5548 * Write tail of the last page before removed range since it will get
5549 * removed from the page cache below.
5550 */
5551 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5552 if (ret)
5553 goto out_mmap;
5554 /*
5555 * Write data that will be shifted to preserve them when discarding
5556 * page cache below. We are also protected from pages becoming dirty
5557 * by i_mmap_sem.
5558 */
5559 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5560 LLONG_MAX);
5561 if (ret)
5562 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005563 truncate_pagecache(inode, ioffset);
5564
Namjae Jeon9eb79482014-02-23 15:18:59 -05005565 credits = ext4_writepage_trans_blocks(inode);
5566 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5567 if (IS_ERR(handle)) {
5568 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005569 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005570 }
5571
5572 down_write(&EXT4_I(inode)->i_data_sem);
5573 ext4_discard_preallocations(inode);
5574
5575 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005576 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005577 if (ret) {
5578 up_write(&EXT4_I(inode)->i_data_sem);
5579 goto out_stop;
5580 }
5581
5582 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5583 if (ret) {
5584 up_write(&EXT4_I(inode)->i_data_sem);
5585 goto out_stop;
5586 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005587 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005588
5589 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005590 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005591 if (ret) {
5592 up_write(&EXT4_I(inode)->i_data_sem);
5593 goto out_stop;
5594 }
5595
5596 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005597 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005598 EXT4_I(inode)->i_disksize = new_size;
5599
Namjae Jeon9eb79482014-02-23 15:18:59 -05005600 up_write(&EXT4_I(inode)->i_data_sem);
5601 if (IS_SYNC(inode))
5602 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005603 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005604 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005605 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005606
5607out_stop:
5608 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005609out_mmap:
5610 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005611out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005612 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005613 return ret;
5614}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005615
Namjae Jeon331573f2015-06-09 01:55:03 -04005616/*
5617 * ext4_insert_range:
5618 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5619 * The data blocks starting from @offset to the EOF are shifted by @len
5620 * towards right to create a hole in the @inode. Inode size is increased
5621 * by len bytes.
5622 * Returns 0 on success, error otherwise.
5623 */
5624int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5625{
5626 struct super_block *sb = inode->i_sb;
5627 handle_t *handle;
5628 struct ext4_ext_path *path;
5629 struct ext4_extent *extent;
5630 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5631 unsigned int credits, ee_len;
5632 int ret = 0, depth, split_flag = 0;
5633 loff_t ioffset;
5634
5635 /*
5636 * We need to test this early because xfstests assumes that an
5637 * insert range of (0, 1) will return EOPNOTSUPP if the file
5638 * system does not support insert range.
5639 */
5640 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5641 return -EOPNOTSUPP;
5642
5643 /* Insert range works only on fs block size aligned offsets. */
5644 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5645 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5646 return -EINVAL;
5647
5648 if (!S_ISREG(inode->i_mode))
5649 return -EOPNOTSUPP;
5650
5651 trace_ext4_insert_range(inode, offset, len);
5652
5653 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5654 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5655
5656 /* Call ext4_force_commit to flush all data in case of data=journal */
5657 if (ext4_should_journal_data(inode)) {
5658 ret = ext4_force_commit(inode->i_sb);
5659 if (ret)
5660 return ret;
5661 }
5662
Al Viro59551022016-01-22 15:40:57 -05005663 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005664 /* Currently just for extent based files */
5665 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5666 ret = -EOPNOTSUPP;
5667 goto out_mutex;
5668 }
5669
5670 /* Check for wrap through zero */
5671 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5672 ret = -EFBIG;
5673 goto out_mutex;
5674 }
5675
5676 /* Offset should be less than i_size */
5677 if (offset >= i_size_read(inode)) {
5678 ret = -EINVAL;
5679 goto out_mutex;
5680 }
5681
Namjae Jeon331573f2015-06-09 01:55:03 -04005682 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005683 inode_dio_wait(inode);
5684
Jan Karaea3d7202015-12-07 14:28:03 -05005685 /*
5686 * Prevent page faults from reinstantiating pages we have released from
5687 * page cache.
5688 */
5689 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005690
5691 ret = ext4_break_layouts(inode);
5692 if (ret)
5693 goto out_mmap;
5694
Jan Kara32ebffd2015-12-07 14:31:11 -05005695 /*
5696 * Need to round down to align start offset to page size boundary
5697 * for page size > block size.
5698 */
5699 ioffset = round_down(offset, PAGE_SIZE);
5700 /* Write out all dirty pages */
5701 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5702 LLONG_MAX);
5703 if (ret)
5704 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005705 truncate_pagecache(inode, ioffset);
5706
Namjae Jeon331573f2015-06-09 01:55:03 -04005707 credits = ext4_writepage_trans_blocks(inode);
5708 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5709 if (IS_ERR(handle)) {
5710 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005711 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005712 }
5713
5714 /* Expand file to avoid data loss if there is error while shifting */
5715 inode->i_size += len;
5716 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005717 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005718 ret = ext4_mark_inode_dirty(handle, inode);
5719 if (ret)
5720 goto out_stop;
5721
5722 down_write(&EXT4_I(inode)->i_data_sem);
5723 ext4_discard_preallocations(inode);
5724
5725 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5726 if (IS_ERR(path)) {
5727 up_write(&EXT4_I(inode)->i_data_sem);
5728 goto out_stop;
5729 }
5730
5731 depth = ext_depth(inode);
5732 extent = path[depth].p_ext;
5733 if (extent) {
5734 ee_start_lblk = le32_to_cpu(extent->ee_block);
5735 ee_len = ext4_ext_get_actual_len(extent);
5736
5737 /*
5738 * If offset_lblk is not the starting block of extent, split
5739 * the extent @offset_lblk
5740 */
5741 if ((offset_lblk > ee_start_lblk) &&
5742 (offset_lblk < (ee_start_lblk + ee_len))) {
5743 if (ext4_ext_is_unwritten(extent))
5744 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5745 EXT4_EXT_MARK_UNWRIT2;
5746 ret = ext4_split_extent_at(handle, inode, &path,
5747 offset_lblk, split_flag,
5748 EXT4_EX_NOCACHE |
5749 EXT4_GET_BLOCKS_PRE_IO |
5750 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5751 }
5752
5753 ext4_ext_drop_refs(path);
5754 kfree(path);
5755 if (ret < 0) {
5756 up_write(&EXT4_I(inode)->i_data_sem);
5757 goto out_stop;
5758 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005759 } else {
5760 ext4_ext_drop_refs(path);
5761 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005762 }
5763
5764 ret = ext4_es_remove_extent(inode, offset_lblk,
5765 EXT_MAX_BLOCKS - offset_lblk);
5766 if (ret) {
5767 up_write(&EXT4_I(inode)->i_data_sem);
5768 goto out_stop;
5769 }
5770
5771 /*
5772 * if offset_lblk lies in a hole which is at start of file, use
5773 * ee_start_lblk to shift extents
5774 */
5775 ret = ext4_ext_shift_extents(inode, handle,
5776 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5777 len_lblk, SHIFT_RIGHT);
5778
5779 up_write(&EXT4_I(inode)->i_data_sem);
5780 if (IS_SYNC(inode))
5781 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005782 if (ret >= 0)
5783 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005784
5785out_stop:
5786 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005787out_mmap:
5788 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005789out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005790 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005791 return ret;
5792}
5793
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005794/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005795 * ext4_swap_extents() - Swap extents between two inodes
5796 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005797 * @inode1: First inode
5798 * @inode2: Second inode
5799 * @lblk1: Start block for first inode
5800 * @lblk2: Start block for second inode
5801 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005802 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005803 * @erp: Pointer to save error value
5804 *
5805 * This helper routine does exactly what is promise "swap extents". All other
5806 * stuff such as page-cache locking consistency, bh mapping consistency or
5807 * extent's data copying must be performed by caller.
5808 * Locking:
5809 * i_mutex is held for both inodes
5810 * i_data_sem is locked for write for both inodes
5811 * Assumptions:
5812 * All pages from requested range are locked for both inodes
5813 */
5814int
5815ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005816 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005817 ext4_lblk_t count, int unwritten, int *erp)
5818{
5819 struct ext4_ext_path *path1 = NULL;
5820 struct ext4_ext_path *path2 = NULL;
5821 int replaced_count = 0;
5822
5823 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5824 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005825 BUG_ON(!inode_is_locked(inode1));
5826 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005827
5828 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005829 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005830 return 0;
5831 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005832 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005833 return 0;
5834
5835 while (count) {
5836 struct ext4_extent *ex1, *ex2, tmp_ex;
5837 ext4_lblk_t e1_blk, e2_blk;
5838 int e1_len, e2_len, len;
5839 int split = 0;
5840
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005841 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305842 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005843 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005844 path1 = NULL;
5845 finish:
5846 count = 0;
5847 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005848 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005849 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305850 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005851 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005852 path2 = NULL;
5853 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005854 }
5855 ex1 = path1[path1->p_depth].p_ext;
5856 ex2 = path2[path2->p_depth].p_ext;
5857 /* Do we have somthing to swap ? */
5858 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005859 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005860
5861 e1_blk = le32_to_cpu(ex1->ee_block);
5862 e2_blk = le32_to_cpu(ex2->ee_block);
5863 e1_len = ext4_ext_get_actual_len(ex1);
5864 e2_len = ext4_ext_get_actual_len(ex2);
5865
5866 /* Hole handling */
5867 if (!in_range(lblk1, e1_blk, e1_len) ||
5868 !in_range(lblk2, e2_blk, e2_len)) {
5869 ext4_lblk_t next1, next2;
5870
5871 /* if hole after extent, then go to next extent */
5872 next1 = ext4_ext_next_allocated_block(path1);
5873 next2 = ext4_ext_next_allocated_block(path2);
5874 /* If hole before extent, then shift to that extent */
5875 if (e1_blk > lblk1)
5876 next1 = e1_blk;
5877 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005878 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005879 /* Do we have something to swap */
5880 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005881 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005882 /* Move to the rightest boundary */
5883 len = next1 - lblk1;
5884 if (len < next2 - lblk2)
5885 len = next2 - lblk2;
5886 if (len > count)
5887 len = count;
5888 lblk1 += len;
5889 lblk2 += len;
5890 count -= len;
5891 goto repeat;
5892 }
5893
5894 /* Prepare left boundary */
5895 if (e1_blk < lblk1) {
5896 split = 1;
5897 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005898 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005899 if (unlikely(*erp))
5900 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005901 }
5902 if (e2_blk < lblk2) {
5903 split = 1;
5904 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005905 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005906 if (unlikely(*erp))
5907 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005908 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005909 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005910 * path must to be revalidated. */
5911 if (split)
5912 goto repeat;
5913
5914 /* Prepare right boundary */
5915 len = count;
5916 if (len > e1_blk + e1_len - lblk1)
5917 len = e1_blk + e1_len - lblk1;
5918 if (len > e2_blk + e2_len - lblk2)
5919 len = e2_blk + e2_len - lblk2;
5920
5921 if (len != e1_len) {
5922 split = 1;
5923 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005924 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005925 if (unlikely(*erp))
5926 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005927 }
5928 if (len != e2_len) {
5929 split = 1;
5930 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005931 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005932 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005933 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005934 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005935 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005936 * path must to be revalidated. */
5937 if (split)
5938 goto repeat;
5939
5940 BUG_ON(e2_len != e1_len);
5941 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005942 if (unlikely(*erp))
5943 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005944 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005945 if (unlikely(*erp))
5946 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005947
5948 /* Both extents are fully inside boundaries. Swap it now */
5949 tmp_ex = *ex1;
5950 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5951 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5952 ex1->ee_len = cpu_to_le16(e2_len);
5953 ex2->ee_len = cpu_to_le16(e1_len);
5954 if (unwritten)
5955 ext4_ext_mark_unwritten(ex2);
5956 if (ext4_ext_is_unwritten(&tmp_ex))
5957 ext4_ext_mark_unwritten(ex1);
5958
5959 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5960 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5961 *erp = ext4_ext_dirty(handle, inode2, path2 +
5962 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005963 if (unlikely(*erp))
5964 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005965 *erp = ext4_ext_dirty(handle, inode1, path1 +
5966 path1->p_depth);
5967 /*
5968 * Looks scarry ah..? second inode already points to new blocks,
5969 * and it was successfully dirtied. But luckily error may happen
5970 * only due to journal error, so full transaction will be
5971 * aborted anyway.
5972 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005973 if (unlikely(*erp))
5974 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005975 lblk1 += len;
5976 lblk2 += len;
5977 replaced_count += len;
5978 count -= len;
5979
5980 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005981 ext4_ext_drop_refs(path1);
5982 kfree(path1);
5983 ext4_ext_drop_refs(path2);
5984 kfree(path2);
5985 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005986 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005987 return replaced_count;
5988}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005989
5990/*
5991 * ext4_clu_mapped - determine whether any block in a logical cluster has
5992 * been mapped to a physical cluster
5993 *
5994 * @inode - file containing the logical cluster
5995 * @lclu - logical cluster of interest
5996 *
5997 * Returns 1 if any block in the logical cluster is mapped, signifying
5998 * that a physical cluster has been allocated for it. Otherwise,
5999 * returns 0. Can also return negative error codes. Derived from
6000 * ext4_ext_map_blocks().
6001 */
6002int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
6003{
6004 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
6005 struct ext4_ext_path *path;
6006 int depth, mapped = 0, err = 0;
6007 struct ext4_extent *extent;
6008 ext4_lblk_t first_lblk, first_lclu, last_lclu;
6009
6010 /* search for the extent closest to the first block in the cluster */
6011 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
6012 if (IS_ERR(path)) {
6013 err = PTR_ERR(path);
6014 path = NULL;
6015 goto out;
6016 }
6017
6018 depth = ext_depth(inode);
6019
6020 /*
6021 * A consistent leaf must not be empty. This situation is possible,
6022 * though, _during_ tree modification, and it's why an assert can't
6023 * be put in ext4_find_extent().
6024 */
6025 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
6026 EXT4_ERROR_INODE(inode,
6027 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
6028 (unsigned long) EXT4_C2B(sbi, lclu),
6029 depth, path[depth].p_block);
6030 err = -EFSCORRUPTED;
6031 goto out;
6032 }
6033
6034 extent = path[depth].p_ext;
6035
6036 /* can't be mapped if the extent tree is empty */
6037 if (extent == NULL)
6038 goto out;
6039
6040 first_lblk = le32_to_cpu(extent->ee_block);
6041 first_lclu = EXT4_B2C(sbi, first_lblk);
6042
6043 /*
6044 * Three possible outcomes at this point - found extent spanning
6045 * the target cluster, to the left of the target cluster, or to the
6046 * right of the target cluster. The first two cases are handled here.
6047 * The last case indicates the target cluster is not mapped.
6048 */
6049 if (lclu >= first_lclu) {
6050 last_lclu = EXT4_B2C(sbi, first_lblk +
6051 ext4_ext_get_actual_len(extent) - 1);
6052 if (lclu <= last_lclu) {
6053 mapped = 1;
6054 } else {
6055 first_lblk = ext4_ext_next_allocated_block(path);
6056 first_lclu = EXT4_B2C(sbi, first_lblk);
6057 if (lclu == first_lclu)
6058 mapped = 1;
6059 }
6060 }
6061
6062out:
6063 ext4_ext_drop_refs(path);
6064 kfree(path);
6065
6066 return err ? err : mapped;
6067}