blob: 0e8708b77da68269af57a7cc94f39689b902887f [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'oc398eda2010-07-27 11:56:40 -0400495 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400496 "pblk %llu bad header/extent: %s - magic %x, "
497 "entries %u, max %u(%u), depth %u(%u)",
498 (unsigned long long) pblk, error_msg,
499 le16_to_cpu(eh->eh_magic),
500 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
501 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400502 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400503}
504
Theodore Ts'oc3491792013-08-16 21:21:41 -0400505#define ext4_ext_check(inode, eh, depth, pblk) \
506 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400507
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400508int ext4_ext_check_inode(struct inode *inode)
509{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400510 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400511}
512
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400513static struct buffer_head *
514__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400515 struct inode *inode, ext4_fsblk_t pblk, int depth,
516 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400517{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400518 struct buffer_head *bh;
519 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400520
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400521 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400522 if (unlikely(!bh))
523 return ERR_PTR(-ENOMEM);
524
525 if (!bh_uptodate_or_lock(bh)) {
526 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
527 err = bh_submit_read(bh);
528 if (err < 0)
529 goto errout;
530 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400531 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400532 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400533 if (!ext4_has_feature_journal(inode->i_sb) ||
534 (inode->i_ino !=
535 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
536 err = __ext4_ext_check(function, line, inode,
537 ext_block_hdr(bh), depth, pblk);
538 if (err)
539 goto errout;
540 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400541 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400542 /*
543 * If this is a leaf block, cache all of its entries
544 */
545 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
546 struct ext4_extent_header *eh = ext_block_hdr(bh);
547 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
548 ext4_lblk_t prev = 0;
549 int i;
550
551 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
552 unsigned int status = EXTENT_STATUS_WRITTEN;
553 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
554 int len = ext4_ext_get_actual_len(ex);
555
556 if (prev && (prev != lblk))
557 ext4_es_cache_extent(inode, prev,
558 lblk - prev, ~0,
559 EXTENT_STATUS_HOLE);
560
Lukas Czerner556615d2014-04-20 23:45:47 -0400561 if (ext4_ext_is_unwritten(ex))
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400562 status = EXTENT_STATUS_UNWRITTEN;
563 ext4_es_cache_extent(inode, lblk, len,
564 ext4_ext_pblock(ex), status);
565 prev = lblk + len;
566 }
567 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400568 return bh;
569errout:
570 put_bh(bh);
571 return ERR_PTR(err);
572
Darrick J. Wongf8489122012-04-29 18:21:10 -0400573}
574
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400575#define read_extent_tree_block(inode, pblk, depth, flags) \
576 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
577 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400578
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400579/*
580 * This function is called to cache a file's extent information in the
581 * extent status tree
582 */
583int ext4_ext_precache(struct inode *inode)
584{
585 struct ext4_inode_info *ei = EXT4_I(inode);
586 struct ext4_ext_path *path = NULL;
587 struct buffer_head *bh;
588 int i = 0, depth, ret = 0;
589
590 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
591 return 0; /* not an extent-mapped inode */
592
593 down_read(&ei->i_data_sem);
594 depth = ext_depth(inode);
595
Kees Cook6396bb22018-06-12 14:03:40 -0700596 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400597 GFP_NOFS);
598 if (path == NULL) {
599 up_read(&ei->i_data_sem);
600 return -ENOMEM;
601 }
602
603 /* Don't cache anything if there are no external extent blocks */
604 if (depth == 0)
605 goto out;
606 path[0].p_hdr = ext_inode_hdr(inode);
607 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
608 if (ret)
609 goto out;
610 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
611 while (i >= 0) {
612 /*
613 * If this is a leaf block or we've reached the end of
614 * the index block, go up
615 */
616 if ((i == depth) ||
617 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
618 brelse(path[i].p_bh);
619 path[i].p_bh = NULL;
620 i--;
621 continue;
622 }
623 bh = read_extent_tree_block(inode,
624 ext4_idx_pblock(path[i].p_idx++),
625 depth - i - 1,
626 EXT4_EX_FORCE_CACHE);
627 if (IS_ERR(bh)) {
628 ret = PTR_ERR(bh);
629 break;
630 }
631 i++;
632 path[i].p_bh = bh;
633 path[i].p_hdr = ext_block_hdr(bh);
634 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
635 }
636 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
637out:
638 up_read(&ei->i_data_sem);
639 ext4_ext_drop_refs(path);
640 kfree(path);
641 return ret;
642}
643
Alex Tomasa86c6182006-10-11 01:21:03 -0700644#ifdef EXT_DEBUG
645static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
646{
647 int k, l = path->p_depth;
648
649 ext_debug("path:");
650 for (k = 0; k <= l; k++, path++) {
651 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700652 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400653 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700654 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400655 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700656 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400657 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400658 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400659 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700660 } else
661 ext_debug(" []");
662 }
663 ext_debug("\n");
664}
665
666static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
667{
668 int depth = ext_depth(inode);
669 struct ext4_extent_header *eh;
670 struct ext4_extent *ex;
671 int i;
672
673 if (!path)
674 return;
675
676 eh = path[depth].p_hdr;
677 ex = EXT_FIRST_EXTENT(eh);
678
Mingming553f9002009-09-18 13:34:55 -0400679 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
680
Alex Tomasa86c6182006-10-11 01:21:03 -0700681 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400682 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400683 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400684 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700685 }
686 ext_debug("\n");
687}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400688
689static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
690 ext4_fsblk_t newblock, int level)
691{
692 int depth = ext_depth(inode);
693 struct ext4_extent *ex;
694
695 if (depth != level) {
696 struct ext4_extent_idx *idx;
697 idx = path[level].p_idx;
698 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
699 ext_debug("%d: move %d:%llu in new index %llu\n", level,
700 le32_to_cpu(idx->ei_block),
701 ext4_idx_pblock(idx),
702 newblock);
703 idx++;
704 }
705
706 return;
707 }
708
709 ex = path[depth].p_ext;
710 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
711 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
712 le32_to_cpu(ex->ee_block),
713 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400714 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400715 ext4_ext_get_actual_len(ex),
716 newblock);
717 ex++;
718 }
719}
720
Alex Tomasa86c6182006-10-11 01:21:03 -0700721#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400722#define ext4_ext_show_path(inode, path)
723#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400724#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700725#endif
726
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500727void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700728{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400729 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700730
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400731 if (!path)
732 return;
733 depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700734 for (i = 0; i <= depth; i++, path++)
735 if (path->p_bh) {
736 brelse(path->p_bh);
737 path->p_bh = NULL;
738 }
739}
740
741/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700742 * ext4_ext_binsearch_idx:
743 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400744 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700745 */
746static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500747ext4_ext_binsearch_idx(struct inode *inode,
748 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700749{
750 struct ext4_extent_header *eh = path->p_hdr;
751 struct ext4_extent_idx *r, *l, *m;
752
Alex Tomasa86c6182006-10-11 01:21:03 -0700753
Eric Sandeenbba90742008-01-28 23:58:27 -0500754 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700755
756 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400757 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700758 while (l <= r) {
759 m = l + (r - l) / 2;
760 if (block < le32_to_cpu(m->ei_block))
761 r = m - 1;
762 else
763 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400764 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
765 m, le32_to_cpu(m->ei_block),
766 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700767 }
768
769 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400770 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400771 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700772
773#ifdef CHECK_BINSEARCH
774 {
775 struct ext4_extent_idx *chix, *ix;
776 int k;
777
778 chix = ix = EXT_FIRST_INDEX(eh);
779 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
780 if (k != 0 &&
781 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400782 printk(KERN_DEBUG "k=%d, ix=0x%p, "
783 "first=0x%p\n", k,
784 ix, EXT_FIRST_INDEX(eh));
785 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700786 le32_to_cpu(ix->ei_block),
787 le32_to_cpu(ix[-1].ei_block));
788 }
789 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400790 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700791 if (block < le32_to_cpu(ix->ei_block))
792 break;
793 chix = ix;
794 }
795 BUG_ON(chix != path->p_idx);
796 }
797#endif
798
799}
800
801/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700802 * ext4_ext_binsearch:
803 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400804 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700805 */
806static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500807ext4_ext_binsearch(struct inode *inode,
808 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700809{
810 struct ext4_extent_header *eh = path->p_hdr;
811 struct ext4_extent *r, *l, *m;
812
Alex Tomasa86c6182006-10-11 01:21:03 -0700813 if (eh->eh_entries == 0) {
814 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700815 * this leaf is empty:
816 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700817 */
818 return;
819 }
820
Eric Sandeenbba90742008-01-28 23:58:27 -0500821 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700822
823 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400824 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700825
826 while (l <= r) {
827 m = l + (r - l) / 2;
828 if (block < le32_to_cpu(m->ee_block))
829 r = m - 1;
830 else
831 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400832 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
833 m, le32_to_cpu(m->ee_block),
834 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 }
836
837 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400838 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400839 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400840 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400841 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400842 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700843
844#ifdef CHECK_BINSEARCH
845 {
846 struct ext4_extent *chex, *ex;
847 int k;
848
849 chex = ex = EXT_FIRST_EXTENT(eh);
850 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
851 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400852 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 if (block < le32_to_cpu(ex->ee_block))
854 break;
855 chex = ex;
856 }
857 BUG_ON(chex != path->p_ext);
858 }
859#endif
860
861}
862
863int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
864{
865 struct ext4_extent_header *eh;
866
867 eh = ext_inode_hdr(inode);
868 eh->eh_depth = 0;
869 eh->eh_entries = 0;
870 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400871 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 return 0;
874}
875
876struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400877ext4_find_extent(struct inode *inode, ext4_lblk_t block,
878 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700879{
880 struct ext4_extent_header *eh;
881 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400882 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
883 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500884 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700885
886 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400887 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400888 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
889 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
890 depth);
891 ret = -EFSCORRUPTED;
892 goto err;
893 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700894
Theodore Ts'o10809df82014-09-01 14:40:09 -0400895 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400896 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400897 if (depth > path[0].p_maxdepth) {
898 kfree(path);
899 *orig_path = path = NULL;
900 }
901 }
902 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400903 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700904 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Alex Tomasa86c6182006-10-11 01:21:03 -0700905 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400906 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400908 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700909 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400911 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700912
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400913 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700914 /* walk through the tree */
915 while (i) {
916 ext_debug("depth %d: num %d, max %d\n",
917 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400918
Alex Tomasa86c6182006-10-11 01:21:03 -0700919 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400920 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700921 path[ppos].p_depth = i;
922 path[ppos].p_ext = NULL;
923
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400924 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
925 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530926 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400927 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700928 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500929 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400930
Alex Tomasa86c6182006-10-11 01:21:03 -0700931 eh = ext_block_hdr(bh);
932 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700933 path[ppos].p_bh = bh;
934 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700935 }
936
937 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700938 path[ppos].p_ext = NULL;
939 path[ppos].p_idx = NULL;
940
Alex Tomasa86c6182006-10-11 01:21:03 -0700941 /* find extent */
942 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400943 /* if not an empty leaf */
944 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400945 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700946
947 ext4_ext_show_path(inode, path);
948
949 return path;
950
951err:
952 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400953 kfree(path);
954 if (orig_path)
955 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500956 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700957}
958
959/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700960 * ext4_ext_insert_index:
961 * insert new index [@logical;@ptr] into the block at @curp;
962 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700963 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400964static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
965 struct ext4_ext_path *curp,
966 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700967{
968 struct ext4_extent_idx *ix;
969 int len, err;
970
Avantika Mathur7e028972006-12-06 20:41:33 -0800971 err = ext4_ext_get_access(handle, inode, curp);
972 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700973 return err;
974
Frank Mayhar273df552010-03-02 11:46:09 -0500975 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
976 EXT4_ERROR_INODE(inode,
977 "logical %d == ei_block %d!",
978 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400979 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500980 }
Robin Dongd4620312011-07-17 23:43:42 -0400981
982 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
983 >= le16_to_cpu(curp->p_hdr->eh_max))) {
984 EXT4_ERROR_INODE(inode,
985 "eh_entries %d >= eh_max %d!",
986 le16_to_cpu(curp->p_hdr->eh_entries),
987 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400988 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400989 }
990
Alex Tomasa86c6182006-10-11 01:21:03 -0700991 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
992 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400993 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 ix = curp->p_idx + 1;
995 } else {
996 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400997 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700998 ix = curp->p_idx;
999 }
1000
Eric Gouriou80e675f2011-10-27 11:52:18 -04001001 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
1002 BUG_ON(len < 0);
1003 if (len > 0) {
1004 ext_debug("insert new index %d: "
1005 "move %d indices from 0x%p to 0x%p\n",
1006 logical, len, ix, ix + 1);
1007 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
1008 }
1009
Tao Maf472e022011-10-17 10:13:46 -04001010 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1011 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001012 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -04001013 }
1014
Alex Tomasa86c6182006-10-11 01:21:03 -07001015 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001016 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001017 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001018
Frank Mayhar273df552010-03-02 11:46:09 -05001019 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1020 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001021 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001022 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001023
1024 err = ext4_ext_dirty(handle, inode, curp);
1025 ext4_std_error(inode->i_sb, err);
1026
1027 return err;
1028}
1029
1030/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001031 * ext4_ext_split:
1032 * inserts new subtree into the path, using free index entry
1033 * at depth @at:
1034 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1035 * - makes decision where to split
1036 * - moves remaining extents and index entries (right to the split point)
1037 * into the newly allocated blocks
1038 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 */
1040static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001041 unsigned int flags,
1042 struct ext4_ext_path *path,
1043 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001044{
1045 struct buffer_head *bh = NULL;
1046 int depth = ext_depth(inode);
1047 struct ext4_extent_header *neh;
1048 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001049 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001050 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001051 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001052 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001054 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001055
1056 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001057 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001058
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001059 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001061 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1062 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001063 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001064 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001065 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1066 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001067 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001068 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001069 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001070 } else {
1071 border = newext->ee_block;
1072 ext_debug("leaf will be added."
1073 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001074 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001075 }
1076
1077 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001078 * If error occurs, then we break processing
1079 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001080 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001081 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 */
1083
1084 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001085 * Get array to track all allocated blocks.
1086 * We need this to handle errors and free blocks
1087 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001088 */
Kees Cook6396bb22018-06-12 14:03:40 -07001089 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001090 if (!ablocks)
1091 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001092
1093 /* allocate all needed blocks */
1094 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1095 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001096 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001097 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001098 if (newblock == 0)
1099 goto cleanup;
1100 ablocks[a] = newblock;
1101 }
1102
1103 /* initialize new leaf */
1104 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001105 if (unlikely(newblock == 0)) {
1106 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001107 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001108 goto cleanup;
1109 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001110 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001111 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001112 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001113 goto cleanup;
1114 }
1115 lock_buffer(bh);
1116
Avantika Mathur7e028972006-12-06 20:41:33 -08001117 err = ext4_journal_get_create_access(handle, bh);
1118 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001119 goto cleanup;
1120
1121 neh = ext_block_hdr(bh);
1122 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001123 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 neh->eh_magic = EXT4_EXT_MAGIC;
1125 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001126
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001127 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001128 if (unlikely(path[depth].p_hdr->eh_entries !=
1129 path[depth].p_hdr->eh_max)) {
1130 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1131 path[depth].p_hdr->eh_entries,
1132 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001133 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001134 goto cleanup;
1135 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001136 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001137 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1138 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001139 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001140 struct ext4_extent *ex;
1141 ex = EXT_FIRST_EXTENT(neh);
1142 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001143 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001144 }
1145
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001146 /* zero out unused area in the extent block */
1147 ext_size = sizeof(struct ext4_extent_header) +
1148 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1149 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001150 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001151 set_buffer_uptodate(bh);
1152 unlock_buffer(bh);
1153
Frank Mayhar03901312009-01-07 00:06:22 -05001154 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001155 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001156 goto cleanup;
1157 brelse(bh);
1158 bh = NULL;
1159
1160 /* correct old leaf */
1161 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001162 err = ext4_ext_get_access(handle, inode, path + depth);
1163 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001164 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001165 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001166 err = ext4_ext_dirty(handle, inode, path + depth);
1167 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001168 goto cleanup;
1169
1170 }
1171
1172 /* create intermediate indexes */
1173 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001174 if (unlikely(k < 0)) {
1175 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001176 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001177 goto cleanup;
1178 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001179 if (k)
1180 ext_debug("create %d intermediate indices\n", k);
1181 /* insert new index into current index block */
1182 /* current depth stored in i var */
1183 i = depth - 1;
1184 while (k--) {
1185 oldblock = newblock;
1186 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001187 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001188 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001189 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001190 goto cleanup;
1191 }
1192 lock_buffer(bh);
1193
Avantika Mathur7e028972006-12-06 20:41:33 -08001194 err = ext4_journal_get_create_access(handle, bh);
1195 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001196 goto cleanup;
1197
1198 neh = ext_block_hdr(bh);
1199 neh->eh_entries = cpu_to_le16(1);
1200 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001201 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001202 neh->eh_depth = cpu_to_le16(depth - i);
1203 fidx = EXT_FIRST_INDEX(neh);
1204 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001205 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001206
Eric Sandeenbba90742008-01-28 23:58:27 -05001207 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1208 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001209
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001210 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001211 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1212 EXT_LAST_INDEX(path[i].p_hdr))) {
1213 EXT4_ERROR_INODE(inode,
1214 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1215 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001216 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001217 goto cleanup;
1218 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001219 /* start copy indexes */
1220 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1221 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1222 EXT_MAX_INDEX(path[i].p_hdr));
1223 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001224 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001225 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001226 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001227 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001228 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001229 /* zero out unused area in the extent block */
1230 ext_size = sizeof(struct ext4_extent_header) +
1231 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1232 memset(bh->b_data + ext_size, 0,
1233 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001234 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001235 set_buffer_uptodate(bh);
1236 unlock_buffer(bh);
1237
Frank Mayhar03901312009-01-07 00:06:22 -05001238 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001239 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001240 goto cleanup;
1241 brelse(bh);
1242 bh = NULL;
1243
1244 /* correct old index */
1245 if (m) {
1246 err = ext4_ext_get_access(handle, inode, path + i);
1247 if (err)
1248 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001249 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001250 err = ext4_ext_dirty(handle, inode, path + i);
1251 if (err)
1252 goto cleanup;
1253 }
1254
1255 i--;
1256 }
1257
1258 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001259 err = ext4_ext_insert_index(handle, inode, path + at,
1260 le32_to_cpu(border), newblock);
1261
1262cleanup:
1263 if (bh) {
1264 if (buffer_locked(bh))
1265 unlock_buffer(bh);
1266 brelse(bh);
1267 }
1268
1269 if (err) {
1270 /* free all allocated blocks in error case */
1271 for (i = 0; i < depth; i++) {
1272 if (!ablocks[i])
1273 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001274 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001275 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001276 }
1277 }
1278 kfree(ablocks);
1279
1280 return err;
1281}
1282
1283/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001284 * ext4_ext_grow_indepth:
1285 * implements tree growing procedure:
1286 * - allocates new block
1287 * - moves top-level data (index block or leaf) into the new block
1288 * - initializes new top-level, creating index that points to the
1289 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001290 */
1291static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001292 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001293{
Alex Tomasa86c6182006-10-11 01:21:03 -07001294 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001295 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001296 ext4_fsblk_t newblock, goal = 0;
1297 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001298 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001299 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001300
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001301 /* Try to prepend new index to old one */
1302 if (ext_depth(inode))
1303 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1304 if (goal > le32_to_cpu(es->s_first_data_block)) {
1305 flags |= EXT4_MB_HINT_TRY_GOAL;
1306 goal--;
1307 } else
1308 goal = ext4_inode_to_goal_block(inode);
1309 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1310 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001311 if (newblock == 0)
1312 return err;
1313
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001314 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001315 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001316 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001317 lock_buffer(bh);
1318
Avantika Mathur7e028972006-12-06 20:41:33 -08001319 err = ext4_journal_get_create_access(handle, bh);
1320 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001321 unlock_buffer(bh);
1322 goto out;
1323 }
1324
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001325 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001326 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001327 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1328 /* zero out unused area in the extent block */
1329 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001330
1331 /* set size of new block */
1332 neh = ext_block_hdr(bh);
1333 /* old root could have indexes or leaves
1334 * so calculate e_max right way */
1335 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001336 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001337 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001338 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001339 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001340 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001341 set_buffer_uptodate(bh);
1342 unlock_buffer(bh);
1343
Frank Mayhar03901312009-01-07 00:06:22 -05001344 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001345 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001346 goto out;
1347
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001348 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001349 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001350 neh->eh_entries = cpu_to_le16(1);
1351 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1352 if (neh->eh_depth == 0) {
1353 /* Root extent block becomes index block */
1354 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1355 EXT_FIRST_INDEX(neh)->ei_block =
1356 EXT_FIRST_EXTENT(neh)->ee_block;
1357 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001358 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001359 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001360 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001361 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001362
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001363 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001364 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001365out:
1366 brelse(bh);
1367
1368 return err;
1369}
1370
1371/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001372 * ext4_ext_create_new_leaf:
1373 * finds empty index and adds new leaf.
1374 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001375 */
1376static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001377 unsigned int mb_flags,
1378 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001379 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001380 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001381{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001382 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001383 struct ext4_ext_path *curp;
1384 int depth, i, err = 0;
1385
1386repeat:
1387 i = depth = ext_depth(inode);
1388
1389 /* walk up to the tree and look for free index entry */
1390 curp = path + depth;
1391 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1392 i--;
1393 curp--;
1394 }
1395
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001396 /* we use already allocated block for index block,
1397 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001398 if (EXT_HAS_FREE_INDEX(curp)) {
1399 /* if we found index with free entry, then use that
1400 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001401 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001402 if (err)
1403 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001404
1405 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001406 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001407 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001408 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001409 if (IS_ERR(path))
1410 err = PTR_ERR(path);
1411 } else {
1412 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001413 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001414 if (err)
1415 goto out;
1416
1417 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001418 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001419 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001420 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001421 if (IS_ERR(path)) {
1422 err = PTR_ERR(path);
1423 goto out;
1424 }
1425
1426 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001427 * only first (depth 0 -> 1) produces free space;
1428 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001429 */
1430 depth = ext_depth(inode);
1431 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001432 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001433 goto repeat;
1434 }
1435 }
1436
1437out:
1438 return err;
1439}
1440
1441/*
Alex Tomas1988b512008-01-28 23:58:27 -05001442 * search the closest allocated block to the left for *logical
1443 * and returns it at @logical + it's physical address at @phys
1444 * if *logical is the smallest allocated block, the function
1445 * returns 0 at @phys
1446 * return value contains 0 (success) or error code
1447 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001448static int ext4_ext_search_left(struct inode *inode,
1449 struct ext4_ext_path *path,
1450 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001451{
1452 struct ext4_extent_idx *ix;
1453 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001454 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001455
Frank Mayhar273df552010-03-02 11:46:09 -05001456 if (unlikely(path == NULL)) {
1457 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001458 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001459 }
Alex Tomas1988b512008-01-28 23:58:27 -05001460 depth = path->p_depth;
1461 *phys = 0;
1462
1463 if (depth == 0 && path->p_ext == NULL)
1464 return 0;
1465
1466 /* usually extent in the path covers blocks smaller
1467 * then *logical, but it can be that extent is the
1468 * first one in the file */
1469
1470 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001471 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001472 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001473 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1474 EXT4_ERROR_INODE(inode,
1475 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1476 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001477 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001478 }
Alex Tomas1988b512008-01-28 23:58:27 -05001479 while (--depth >= 0) {
1480 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001481 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1482 EXT4_ERROR_INODE(inode,
1483 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001484 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001485 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001486 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001487 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001488 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001489 }
Alex Tomas1988b512008-01-28 23:58:27 -05001490 }
1491 return 0;
1492 }
1493
Frank Mayhar273df552010-03-02 11:46:09 -05001494 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1495 EXT4_ERROR_INODE(inode,
1496 "logical %d < ee_block %d + ee_len %d!",
1497 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001498 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001499 }
Alex Tomas1988b512008-01-28 23:58:27 -05001500
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001501 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001502 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001503 return 0;
1504}
1505
1506/*
1507 * search the closest allocated block to the right for *logical
1508 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001509 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001510 * returns 0 at @phys
1511 * return value contains 0 (success) or error code
1512 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001513static int ext4_ext_search_right(struct inode *inode,
1514 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001515 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1516 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001517{
1518 struct buffer_head *bh = NULL;
1519 struct ext4_extent_header *eh;
1520 struct ext4_extent_idx *ix;
1521 struct ext4_extent *ex;
1522 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001523 int depth; /* Note, NOT eh_depth; depth from top of tree */
1524 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001525
Frank Mayhar273df552010-03-02 11:46:09 -05001526 if (unlikely(path == NULL)) {
1527 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001528 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001529 }
Alex Tomas1988b512008-01-28 23:58:27 -05001530 depth = path->p_depth;
1531 *phys = 0;
1532
1533 if (depth == 0 && path->p_ext == NULL)
1534 return 0;
1535
1536 /* usually extent in the path covers blocks smaller
1537 * then *logical, but it can be that extent is the
1538 * first one in the file */
1539
1540 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001541 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001542 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001543 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1544 EXT4_ERROR_INODE(inode,
1545 "first_extent(path[%d].p_hdr) != ex",
1546 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001547 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001548 }
Alex Tomas1988b512008-01-28 23:58:27 -05001549 while (--depth >= 0) {
1550 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001551 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1552 EXT4_ERROR_INODE(inode,
1553 "ix != EXT_FIRST_INDEX *logical %d!",
1554 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001555 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001556 }
Alex Tomas1988b512008-01-28 23:58:27 -05001557 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001558 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001559 }
1560
Frank Mayhar273df552010-03-02 11:46:09 -05001561 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1562 EXT4_ERROR_INODE(inode,
1563 "logical %d < ee_block %d + ee_len %d!",
1564 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001565 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001566 }
Alex Tomas1988b512008-01-28 23:58:27 -05001567
1568 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1569 /* next allocated block in this leaf */
1570 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001571 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001572 }
1573
1574 /* go up and search for index to the right */
1575 while (--depth >= 0) {
1576 ix = path[depth].p_idx;
1577 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001578 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001579 }
1580
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001581 /* we've gone up to the root and found no index to the right */
1582 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001583
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001584got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001585 /* we've found index to the right, let's
1586 * follow it and find the closest allocated
1587 * block to the right */
1588 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001589 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001590 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001591 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001592 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001593 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001594 if (IS_ERR(bh))
1595 return PTR_ERR(bh);
1596 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001597 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001598 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001599 put_bh(bh);
1600 }
1601
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001602 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001603 if (IS_ERR(bh))
1604 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001605 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001606 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001607found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001608 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001609 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001610 *ret_ex = ex;
1611 if (bh)
1612 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001613 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001614}
1615
1616/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001617 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001618 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001619 * NOTE: it considers block number from index entry as
1620 * allocated block. Thus, index entries have to be consistent
1621 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001622 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001623ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001624ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1625{
1626 int depth;
1627
1628 BUG_ON(path == NULL);
1629 depth = path->p_depth;
1630
1631 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001632 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001633
1634 while (depth >= 0) {
1635 if (depth == path->p_depth) {
1636 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001637 if (path[depth].p_ext &&
1638 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001639 EXT_LAST_EXTENT(path[depth].p_hdr))
1640 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1641 } else {
1642 /* index */
1643 if (path[depth].p_idx !=
1644 EXT_LAST_INDEX(path[depth].p_hdr))
1645 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1646 }
1647 depth--;
1648 }
1649
Lukas Czernerf17722f2011-06-06 00:05:17 -04001650 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001651}
1652
1653/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001654 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001655 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001656 */
Robin Dong57187892011-07-23 21:49:07 -04001657static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001658{
1659 int depth;
1660
1661 BUG_ON(path == NULL);
1662 depth = path->p_depth;
1663
1664 /* zero-tree has no leaf blocks at all */
1665 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001666 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001667
1668 /* go to index block */
1669 depth--;
1670
1671 while (depth >= 0) {
1672 if (path[depth].p_idx !=
1673 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001674 return (ext4_lblk_t)
1675 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001676 depth--;
1677 }
1678
Lukas Czernerf17722f2011-06-06 00:05:17 -04001679 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001680}
1681
1682/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001683 * ext4_ext_correct_indexes:
1684 * if leaf gets modified and modified extent is first in the leaf,
1685 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001686 * TODO: do we need to correct tree in all cases?
1687 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001688static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001689 struct ext4_ext_path *path)
1690{
1691 struct ext4_extent_header *eh;
1692 int depth = ext_depth(inode);
1693 struct ext4_extent *ex;
1694 __le32 border;
1695 int k, err = 0;
1696
1697 eh = path[depth].p_hdr;
1698 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001699
1700 if (unlikely(ex == NULL || eh == NULL)) {
1701 EXT4_ERROR_INODE(inode,
1702 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001703 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001704 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001705
1706 if (depth == 0) {
1707 /* there is no tree at all */
1708 return 0;
1709 }
1710
1711 if (ex != EXT_FIRST_EXTENT(eh)) {
1712 /* we correct tree if first leaf got modified only */
1713 return 0;
1714 }
1715
1716 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001717 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001718 */
1719 k = depth - 1;
1720 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001721 err = ext4_ext_get_access(handle, inode, path + k);
1722 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001723 return err;
1724 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001725 err = ext4_ext_dirty(handle, inode, path + k);
1726 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001727 return err;
1728
1729 while (k--) {
1730 /* change all left-side indexes */
1731 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1732 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001733 err = ext4_ext_get_access(handle, inode, path + k);
1734 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001735 break;
1736 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001737 err = ext4_ext_dirty(handle, inode, path + k);
1738 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001739 break;
1740 }
1741
1742 return err;
1743}
1744
Akira Fujita748de672009-06-17 19:24:03 -04001745int
Alex Tomasa86c6182006-10-11 01:21:03 -07001746ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1747 struct ext4_extent *ex2)
1748{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001749 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001750
Lukas Czerner556615d2014-04-20 23:45:47 -04001751 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001752 return 0;
1753
1754 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1755 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1756
1757 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001758 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001759 return 0;
1760
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001761 /*
1762 * To allow future support for preallocated extents to be added
1763 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001764 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001765 */
Eric Sandeenda0169b2013-11-04 09:58:26 -05001766 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001767 return 0;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001768
Lukas Czerner556615d2014-04-20 23:45:47 -04001769 if (ext4_ext_is_unwritten(ex1) &&
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11001770 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001771 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001772#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001773 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001774 return 0;
1775#endif
1776
Theodore Ts'obf89d162010-10-27 21:30:14 -04001777 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001778 return 1;
1779 return 0;
1780}
1781
1782/*
Amit Arora56055d32007-07-17 21:42:38 -04001783 * This function tries to merge the "ex" extent to the next extent in the tree.
1784 * It always tries to merge towards right. If you want to merge towards
1785 * left, pass "ex - 1" as argument instead of "ex".
1786 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1787 * 1 if they got merged.
1788 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001789static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001790 struct ext4_ext_path *path,
1791 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001792{
1793 struct ext4_extent_header *eh;
1794 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001795 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001796
1797 depth = ext_depth(inode);
1798 BUG_ON(path[depth].p_hdr == NULL);
1799 eh = path[depth].p_hdr;
1800
1801 while (ex < EXT_LAST_EXTENT(eh)) {
1802 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1803 break;
1804 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001805 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001806 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1807 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001808 if (unwritten)
1809 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001810
1811 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1812 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1813 * sizeof(struct ext4_extent);
1814 memmove(ex + 1, ex + 2, len);
1815 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001816 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001817 merge_done = 1;
1818 WARN_ON(eh->eh_entries == 0);
1819 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001820 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001821 }
1822
1823 return merge_done;
1824}
1825
1826/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001827 * This function does a very simple check to see if we can collapse
1828 * an extent tree with a single extent tree leaf block into the inode.
1829 */
1830static void ext4_ext_try_to_merge_up(handle_t *handle,
1831 struct inode *inode,
1832 struct ext4_ext_path *path)
1833{
1834 size_t s;
1835 unsigned max_root = ext4_ext_space_root(inode, 0);
1836 ext4_fsblk_t blk;
1837
1838 if ((path[0].p_depth != 1) ||
1839 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1840 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1841 return;
1842
1843 /*
1844 * We need to modify the block allocation bitmap and the block
1845 * group descriptor to release the extent tree block. If we
1846 * can't get the journal credits, give up.
1847 */
Jan Kara83448bd2019-11-05 17:44:29 +01001848 if (ext4_journal_extend(handle, 2,
1849 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001850 return;
1851
1852 /*
1853 * Copy the extent data up to the inode
1854 */
1855 blk = ext4_idx_pblock(path[0].p_idx);
1856 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1857 sizeof(struct ext4_extent_idx);
1858 s += sizeof(struct ext4_extent_header);
1859
Theodore Ts'o10809df82014-09-01 14:40:09 -04001860 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001861 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1862 path[0].p_depth = 0;
1863 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1864 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1865 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1866
1867 brelse(path[1].p_bh);
1868 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001869 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001870}
1871
1872/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001873 * This function tries to merge the @ex extent to neighbours in the tree.
1874 * return 1 if merge left else 0.
1875 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001876static void ext4_ext_try_to_merge(handle_t *handle,
1877 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001878 struct ext4_ext_path *path,
1879 struct ext4_extent *ex) {
1880 struct ext4_extent_header *eh;
1881 unsigned int depth;
1882 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001883
1884 depth = ext_depth(inode);
1885 BUG_ON(path[depth].p_hdr == NULL);
1886 eh = path[depth].p_hdr;
1887
1888 if (ex > EXT_FIRST_EXTENT(eh))
1889 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1890
1891 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001892 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001893
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001894 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001895}
1896
1897/*
Amit Arora25d14f92007-05-24 13:04:13 -04001898 * check if a portion of the "newext" extent overlaps with an
1899 * existing extent.
1900 *
1901 * If there is an overlap discovered, it updates the length of the newext
1902 * such that there will be no overlap, and then returns 1.
1903 * If there is no overlap found, it returns 0.
1904 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001905static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1906 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001907 struct ext4_extent *newext,
1908 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001909{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001910 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001911 unsigned int depth, len1;
1912 unsigned int ret = 0;
1913
1914 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001915 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001916 depth = ext_depth(inode);
1917 if (!path[depth].p_ext)
1918 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001919 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001920
1921 /*
1922 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001923 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001924 */
1925 if (b2 < b1) {
1926 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001927 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001928 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001929 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001930 }
1931
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001932 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001933 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001934 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001935 newext->ee_len = cpu_to_le16(len1);
1936 ret = 1;
1937 }
1938
1939 /* check for overlap */
1940 if (b1 + len1 > b2) {
1941 newext->ee_len = cpu_to_le16(b2 - b1);
1942 ret = 1;
1943 }
1944out:
1945 return ret;
1946}
1947
1948/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001949 * ext4_ext_insert_extent:
1950 * tries to merge requsted extent into the existing extent or
1951 * inserts requested extent as new one into the tree,
1952 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001953 */
1954int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001955 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001956 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001957{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001958 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001959 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001960 struct ext4_extent *ex, *fex;
1961 struct ext4_extent *nearex; /* nearest extent */
1962 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001963 int depth, len, err;
1964 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001965 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001966
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001967 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1968 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001969 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1970 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001971 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001972 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001973 depth = ext_depth(inode);
1974 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001975 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001976 if (unlikely(path[depth].p_hdr == NULL)) {
1977 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001978 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001979 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001980
1981 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001982 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001983
1984 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001985 * Try to see whether we should rather test the extent on
1986 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001987 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001988 * left, or on the right from the searched position. This
1989 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001990 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001991 if (ex < EXT_LAST_EXTENT(eh) &&
1992 (le32_to_cpu(ex->ee_block) +
1993 ext4_ext_get_actual_len(ex) <
1994 le32_to_cpu(newext->ee_block))) {
1995 ex += 1;
1996 goto prepend;
1997 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1998 (le32_to_cpu(newext->ee_block) +
1999 ext4_ext_get_actual_len(newext) <
2000 le32_to_cpu(ex->ee_block)))
2001 ex -= 1;
2002
2003 /* Try to append newex to the ex */
2004 if (ext4_can_extents_be_merged(inode, ex, newext)) {
2005 ext_debug("append [%d]%d block to %u:[%d]%d"
2006 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04002007 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002008 ext4_ext_get_actual_len(newext),
2009 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002010 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002011 ext4_ext_get_actual_len(ex),
2012 ext4_ext_pblock(ex));
2013 err = ext4_ext_get_access(handle, inode,
2014 path + depth);
2015 if (err)
2016 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04002017 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002018 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002019 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002020 if (unwritten)
2021 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002022 eh = path[depth].p_hdr;
2023 nearex = ex;
2024 goto merge;
2025 }
2026
2027prepend:
2028 /* Try to prepend newex to the ex */
2029 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2030 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2031 "(from %llu)\n",
2032 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002033 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002034 ext4_ext_get_actual_len(newext),
2035 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002036 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002037 ext4_ext_get_actual_len(ex),
2038 ext4_ext_pblock(ex));
2039 err = ext4_ext_get_access(handle, inode,
2040 path + depth);
2041 if (err)
2042 return err;
2043
Lukas Czerner556615d2014-04-20 23:45:47 -04002044 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002045 ex->ee_block = newext->ee_block;
2046 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2047 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2048 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002049 if (unwritten)
2050 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002051 eh = path[depth].p_hdr;
2052 nearex = ex;
2053 goto merge;
2054 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002055 }
2056
Alex Tomasa86c6182006-10-11 01:21:03 -07002057 depth = ext_depth(inode);
2058 eh = path[depth].p_hdr;
2059 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2060 goto has_space;
2061
2062 /* probably next leaf has space for us? */
2063 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002064 next = EXT_MAX_BLOCKS;
2065 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002066 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002067 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002068 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002069 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002070 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002071 if (IS_ERR(npath))
2072 return PTR_ERR(npath);
2073 BUG_ON(npath->p_depth != path->p_depth);
2074 eh = npath[depth].p_hdr;
2075 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002076 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002077 le16_to_cpu(eh->eh_entries));
2078 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002079 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002080 }
2081 ext_debug("next leaf has no free space(%d,%d)\n",
2082 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2083 }
2084
2085 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002086 * There is no free space in the found leaf.
2087 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002088 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002089 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002090 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002091 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002092 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002093 if (err)
2094 goto cleanup;
2095 depth = ext_depth(inode);
2096 eh = path[depth].p_hdr;
2097
2098has_space:
2099 nearex = path[depth].p_ext;
2100
Avantika Mathur7e028972006-12-06 20:41:33 -08002101 err = ext4_ext_get_access(handle, inode, path + depth);
2102 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002103 goto cleanup;
2104
2105 if (!nearex) {
2106 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002107 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002108 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002109 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002110 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002111 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002112 nearex = EXT_FIRST_EXTENT(eh);
2113 } else {
2114 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002115 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002116 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002117 ext_debug("insert %u:%llu:[%d]%d before: "
2118 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002119 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002120 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002121 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002122 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002123 nearex);
2124 nearex++;
2125 } else {
2126 /* Insert before */
2127 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002128 ext_debug("insert %u:%llu:[%d]%d after: "
2129 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002130 le32_to_cpu(newext->ee_block),
2131 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002132 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002133 ext4_ext_get_actual_len(newext),
2134 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002135 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002136 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2137 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002138 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002139 "move %d extents from 0x%p to 0x%p\n",
2140 le32_to_cpu(newext->ee_block),
2141 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002142 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002143 ext4_ext_get_actual_len(newext),
2144 len, nearex, nearex + 1);
2145 memmove(nearex + 1, nearex,
2146 len * sizeof(struct ext4_extent));
2147 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002148 }
2149
Marcin Slusarze8546d02008-04-17 10:38:59 -04002150 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002151 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002152 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002153 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002154 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002155
2156merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002157 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002158 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002159 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002160
Alex Tomasa86c6182006-10-11 01:21:03 -07002161
2162 /* time to correct all indexes above */
2163 err = ext4_ext_correct_indexes(handle, inode, path);
2164 if (err)
2165 goto cleanup;
2166
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002167 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002168
2169cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002170 ext4_ext_drop_refs(npath);
2171 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002172 return err;
2173}
2174
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002175static int ext4_fill_fiemap_extents(struct inode *inode,
2176 ext4_lblk_t block, ext4_lblk_t num,
2177 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002178{
2179 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002180 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002181 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002182 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002183 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002184 int exists, depth = 0, err = 0;
2185 unsigned int flags = 0;
2186 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002187
Lukas Czernerf17722f2011-06-06 00:05:17 -04002188 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002189 num = last - block;
2190 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002191 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002192
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002193 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002194 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002195 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002196 err = PTR_ERR(path);
2197 path = NULL;
2198 break;
2199 }
2200
2201 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002202 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002203 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002204 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002205 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002206 break;
2207 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002208 ex = path[depth].p_ext;
2209 next = ext4_ext_next_allocated_block(path);
2210
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002211 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002212 exists = 0;
2213 if (!ex) {
2214 /* there is no extent yet, so try to allocate
2215 * all requested space */
2216 start = block;
2217 end = block + num;
2218 } else if (le32_to_cpu(ex->ee_block) > block) {
2219 /* need to allocate space before found extent */
2220 start = block;
2221 end = le32_to_cpu(ex->ee_block);
2222 if (block + num < end)
2223 end = block + num;
2224 } else if (block >= le32_to_cpu(ex->ee_block)
2225 + ext4_ext_get_actual_len(ex)) {
2226 /* need to allocate space after found extent */
2227 start = block;
2228 end = block + num;
2229 if (end >= next)
2230 end = next;
2231 } else if (block >= le32_to_cpu(ex->ee_block)) {
2232 /*
2233 * some part of requested space is covered
2234 * by found extent
2235 */
2236 start = block;
2237 end = le32_to_cpu(ex->ee_block)
2238 + ext4_ext_get_actual_len(ex);
2239 if (block + num < end)
2240 end = block + num;
2241 exists = 1;
2242 } else {
2243 BUG();
2244 }
2245 BUG_ON(end <= start);
2246
2247 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002248 es.es_lblk = start;
2249 es.es_len = end - start;
2250 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002251 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002252 es.es_lblk = le32_to_cpu(ex->ee_block);
2253 es.es_len = ext4_ext_get_actual_len(ex);
2254 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002255 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002256 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002257 }
2258
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002259 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002260 * Find delayed extent and update es accordingly. We call
2261 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002262 * last existing extent or not.
2263 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002264 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002265 if (!exists && next_del) {
2266 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002267 flags |= (FIEMAP_EXTENT_DELALLOC |
2268 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002269 }
2270 up_read(&EXT4_I(inode)->i_data_sem);
2271
Zheng Liu69eb33d2013-02-18 00:31:07 -05002272 if (unlikely(es.es_len == 0)) {
2273 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002274 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002275 break;
2276 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002277
Zheng Liuf7fec032013-02-18 00:28:47 -05002278 /*
2279 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2280 * we need to check next == EXT_MAX_BLOCKS because it is
2281 * possible that an extent is with unwritten and delayed
2282 * status due to when an extent is delayed allocated and
2283 * is allocated by fallocate status tree will track both of
2284 * them in a extent.
2285 *
2286 * So we could return a unwritten and delayed extent, and
2287 * its block is equal to 'next'.
2288 */
2289 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002290 flags |= FIEMAP_EXTENT_LAST;
2291 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2292 next != EXT_MAX_BLOCKS)) {
2293 EXT4_ERROR_INODE(inode,
2294 "next extent == %u, next "
2295 "delalloc extent = %u",
2296 next, next_del);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002297 err = -EFSCORRUPTED;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002298 break;
2299 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002300 }
2301
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002302 if (exists) {
2303 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002304 (__u64)es.es_lblk << blksize_bits,
2305 (__u64)es.es_pblk << blksize_bits,
2306 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002307 flags);
2308 if (err < 0)
2309 break;
2310 if (err == 1) {
2311 err = 0;
2312 break;
2313 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002314 }
2315
Zheng Liu69eb33d2013-02-18 00:31:07 -05002316 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002317 }
2318
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002319 ext4_ext_drop_refs(path);
2320 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002321 return err;
2322}
2323
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002324static int ext4_fill_es_cache_info(struct inode *inode,
2325 ext4_lblk_t block, ext4_lblk_t num,
2326 struct fiemap_extent_info *fieinfo)
2327{
2328 ext4_lblk_t next, end = block + num - 1;
2329 struct extent_status es;
2330 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2331 unsigned int flags;
2332 int err;
2333
2334 while (block <= end) {
2335 next = 0;
2336 flags = 0;
2337 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2338 break;
2339 if (ext4_es_is_unwritten(&es))
2340 flags |= FIEMAP_EXTENT_UNWRITTEN;
2341 if (ext4_es_is_delayed(&es))
2342 flags |= (FIEMAP_EXTENT_DELALLOC |
2343 FIEMAP_EXTENT_UNKNOWN);
2344 if (ext4_es_is_hole(&es))
2345 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2346 if (next == 0)
2347 flags |= FIEMAP_EXTENT_LAST;
2348 if (flags & (FIEMAP_EXTENT_DELALLOC|
2349 EXT4_FIEMAP_EXTENT_HOLE))
2350 es.es_pblk = 0;
2351 else
2352 es.es_pblk = ext4_es_pblock(&es);
2353 err = fiemap_fill_next_extent(fieinfo,
2354 (__u64)es.es_lblk << blksize_bits,
2355 (__u64)es.es_pblk << blksize_bits,
2356 (__u64)es.es_len << blksize_bits,
2357 flags);
2358 if (next == 0)
2359 break;
2360 block = next;
2361 if (err < 0)
2362 return err;
2363 if (err == 1)
2364 return 0;
2365 }
2366 return 0;
2367}
2368
2369
Alex Tomasa86c6182006-10-11 01:21:03 -07002370/*
Jan Kara140a5252016-03-09 22:46:57 -05002371 * ext4_ext_determine_hole - determine hole around given block
2372 * @inode: inode we lookup in
2373 * @path: path in extent tree to @lblk
2374 * @lblk: pointer to logical block around which we want to determine hole
2375 *
2376 * Determine hole length (and start if easily possible) around given logical
2377 * block. We don't try too hard to find the beginning of the hole but @path
2378 * actually points to extent before @lblk, we provide it.
2379 *
2380 * The function returns the length of a hole starting at @lblk. We update @lblk
2381 * to the beginning of the hole if we managed to find it.
2382 */
2383static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2384 struct ext4_ext_path *path,
2385 ext4_lblk_t *lblk)
2386{
2387 int depth = ext_depth(inode);
2388 struct ext4_extent *ex;
2389 ext4_lblk_t len;
2390
2391 ex = path[depth].p_ext;
2392 if (ex == NULL) {
2393 /* there is no extent yet, so gap is [0;-] */
2394 *lblk = 0;
2395 len = EXT_MAX_BLOCKS;
2396 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2397 len = le32_to_cpu(ex->ee_block) - *lblk;
2398 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2399 + ext4_ext_get_actual_len(ex)) {
2400 ext4_lblk_t next;
2401
2402 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2403 next = ext4_ext_next_allocated_block(path);
2404 BUG_ON(next == *lblk);
2405 len = next - *lblk;
2406 } else {
2407 BUG();
2408 }
2409 return len;
2410}
2411
2412/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002413 * ext4_ext_put_gap_in_cache:
2414 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002415 * and cache this gap
2416 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002417static void
Jan Kara140a5252016-03-09 22:46:57 -05002418ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2419 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002420{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002421 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002422
Eric Whitneyad431022018-10-01 14:10:39 -04002423 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2424 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002425 if (es.es_len) {
2426 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002427 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002428 return;
Jan Kara140a5252016-03-09 22:46:57 -05002429 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002430 }
Jan Kara140a5252016-03-09 22:46:57 -05002431 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2432 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2433 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002434}
2435
2436/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002437 * ext4_ext_rm_idx:
2438 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002439 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002440static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002441 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002442{
Alex Tomasa86c6182006-10-11 01:21:03 -07002443 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002444 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002445
2446 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002447 depth--;
2448 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002449 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002450 if (unlikely(path->p_hdr->eh_entries == 0)) {
2451 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002452 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002453 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002454 err = ext4_ext_get_access(handle, inode, path);
2455 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002456 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002457
2458 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2459 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2460 len *= sizeof(struct ext4_extent_idx);
2461 memmove(path->p_idx, path->p_idx + 1, len);
2462 }
2463
Marcin Slusarze8546d02008-04-17 10:38:59 -04002464 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002465 err = ext4_ext_dirty(handle, inode, path);
2466 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002467 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002468 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002469 trace_ext4_ext_rm_idx(inode, leaf);
2470
Peter Huewe7dc57612011-02-21 21:01:42 -05002471 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002472 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002473
2474 while (--depth >= 0) {
2475 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2476 break;
2477 path--;
2478 err = ext4_ext_get_access(handle, inode, path);
2479 if (err)
2480 break;
2481 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2482 err = ext4_ext_dirty(handle, inode, path);
2483 if (err)
2484 break;
2485 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002486 return err;
2487}
2488
2489/*
Mingming Caoee12b632008-08-19 22:16:05 -04002490 * ext4_ext_calc_credits_for_single_extent:
2491 * This routine returns max. credits that needed to insert an extent
2492 * to the extent tree.
2493 * When pass the actual path, the caller should calculate credits
2494 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002495 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002496int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002497 struct ext4_ext_path *path)
2498{
Alex Tomasa86c6182006-10-11 01:21:03 -07002499 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002500 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002501 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002502
Alex Tomasa86c6182006-10-11 01:21:03 -07002503 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002504 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002505 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2506
2507 /*
2508 * There are some space in the leaf tree, no
2509 * need to account for leaf block credit
2510 *
2511 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002512 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002513 * accounted.
2514 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002515 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002516 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002517 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002518 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002519 }
2520
Mingming Cao525f4ed2008-08-19 22:15:58 -04002521 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002522}
Alex Tomasa86c6182006-10-11 01:21:03 -07002523
Mingming Caoee12b632008-08-19 22:16:05 -04002524/*
Jan Karafffb2732013-06-04 13:01:11 -04002525 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002526 *
Jan Karafffb2732013-06-04 13:01:11 -04002527 * If we add a single extent, then in the worse case, each tree level
2528 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002529 *
Jan Karafffb2732013-06-04 13:01:11 -04002530 * If more extents are inserted, they could cause the whole tree split more
2531 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002532 */
Jan Karafffb2732013-06-04 13:01:11 -04002533int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002534{
2535 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002536 int depth;
2537
2538 /* If we are converting the inline data, only one is needed here. */
2539 if (ext4_has_inline_data(inode))
2540 return 1;
2541
2542 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002543
Jan Karafffb2732013-06-04 13:01:11 -04002544 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002545 index = depth * 2;
2546 else
2547 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002548
Mingming Caoee12b632008-08-19 22:16:05 -04002549 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002550}
2551
Theodore Ts'o981250c2013-06-12 11:48:29 -04002552static inline int get_default_free_blocks_flags(struct inode *inode)
2553{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002554 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2555 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002556 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2557 else if (ext4_should_journal_data(inode))
2558 return EXT4_FREE_BLOCKS_FORGET;
2559 return 0;
2560}
2561
Eric Whitney9fe67142018-10-01 14:25:08 -04002562/*
2563 * ext4_rereserve_cluster - increment the reserved cluster count when
2564 * freeing a cluster with a pending reservation
2565 *
2566 * @inode - file containing the cluster
2567 * @lblk - logical block in cluster to be reserved
2568 *
2569 * Increments the reserved cluster count and adjusts quota in a bigalloc
2570 * file system when freeing a partial cluster containing at least one
2571 * delayed and unwritten block. A partial cluster meeting that
2572 * requirement will have a pending reservation. If so, the
2573 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2574 * defer reserved and allocated space accounting to a subsequent call
2575 * to this function.
2576 */
2577static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2578{
2579 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2580 struct ext4_inode_info *ei = EXT4_I(inode);
2581
2582 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2583
2584 spin_lock(&ei->i_block_reservation_lock);
2585 ei->i_reserved_data_blocks++;
2586 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2587 spin_unlock(&ei->i_block_reservation_lock);
2588
2589 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2590 ext4_remove_pending(inode, lblk);
2591}
2592
Alex Tomasa86c6182006-10-11 01:21:03 -07002593static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002594 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002595 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002596 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002597{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002598 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002599 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002600 ext4_fsblk_t last_pblk, pblk;
2601 ext4_lblk_t num;
2602 int flags;
2603
2604 /* only extent tail removal is allowed */
2605 if (from < le32_to_cpu(ex->ee_block) ||
2606 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2607 ext4_error(sbi->s_sb,
2608 "strange request: removal(2) %u-%u from %u:%u",
2609 from, to, le32_to_cpu(ex->ee_block), ee_len);
2610 return 0;
2611 }
2612
2613#ifdef EXTENTS_STATS
2614 spin_lock(&sbi->s_ext_stats_lock);
2615 sbi->s_ext_blocks += ee_len;
2616 sbi->s_ext_extents++;
2617 if (ee_len < sbi->s_ext_min)
2618 sbi->s_ext_min = ee_len;
2619 if (ee_len > sbi->s_ext_max)
2620 sbi->s_ext_max = ee_len;
2621 if (ext_depth(inode) > sbi->s_depth_max)
2622 sbi->s_depth_max = ext_depth(inode);
2623 spin_unlock(&sbi->s_ext_stats_lock);
2624#endif
2625
2626 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2627
2628 /*
2629 * if we have a partial cluster, and it's different from the
2630 * cluster of the last block in the extent, we free it
2631 */
2632 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2633
2634 if (partial->state != initial &&
2635 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2636 if (partial->state == tofree) {
2637 flags = get_default_free_blocks_flags(inode);
2638 if (ext4_is_pending(inode, partial->lblk))
2639 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2640 ext4_free_blocks(handle, inode, NULL,
2641 EXT4_C2B(sbi, partial->pclu),
2642 sbi->s_cluster_ratio, flags);
2643 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2644 ext4_rereserve_cluster(inode, partial->lblk);
2645 }
2646 partial->state = initial;
2647 }
2648
2649 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2650 pblk = ext4_ext_pblock(ex) + ee_len - num;
2651
2652 /*
2653 * We free the partial cluster at the end of the extent (if any),
2654 * unless the cluster is used by another extent (partial_cluster
2655 * state is nofree). If a partial cluster exists here, it must be
2656 * shared with the last block in the extent.
2657 */
2658 flags = get_default_free_blocks_flags(inode);
2659
2660 /* partial, left end cluster aligned, right end unaligned */
2661 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2662 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2663 (partial->state != nofree)) {
2664 if (ext4_is_pending(inode, to))
2665 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2666 ext4_free_blocks(handle, inode, NULL,
2667 EXT4_PBLK_CMASK(sbi, last_pblk),
2668 sbi->s_cluster_ratio, flags);
2669 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2670 ext4_rereserve_cluster(inode, to);
2671 partial->state = initial;
2672 flags = get_default_free_blocks_flags(inode);
2673 }
2674
2675 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002676
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002677 /*
2678 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002679 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002680 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002681 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002682 */
2683 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002684 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002685
Eric Whitney9fe67142018-10-01 14:25:08 -04002686 /* reset the partial cluster if we've freed past it */
2687 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2688 partial->state = initial;
2689
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002690 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002691 * If we've freed the entire extent but the beginning is not left
2692 * cluster aligned and is not marked as ineligible for freeing we
2693 * record the partial cluster at the beginning of the extent. It
2694 * wasn't freed by the preceding ext4_free_blocks() call, and we
2695 * need to look farther to the left to determine if it's to be freed
2696 * (not shared with another extent). Else, reset the partial
2697 * cluster - we're either done freeing or the beginning of the
2698 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002699 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002700 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2701 if (partial->state == initial) {
2702 partial->pclu = EXT4_B2C(sbi, pblk);
2703 partial->lblk = from;
2704 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002705 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002706 } else {
2707 partial->state = initial;
2708 }
2709
Alex Tomasa86c6182006-10-11 01:21:03 -07002710 return 0;
2711}
2712
Allison Hendersond583fb82011-05-25 07:41:43 -04002713/*
2714 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002715 * blocks appearing between "start" and "end". Both "start"
2716 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002717 *
2718 * @handle: The journal handle
2719 * @inode: The files inode
2720 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002721 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002722 * has been released from it. However, if this value is
2723 * negative, it's a cluster just to the right of the
2724 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002725 * @start: The first block to remove
2726 * @end: The last block to remove
2727 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002728static int
2729ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002730 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002731 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002732 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002733{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002734 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002735 int err = 0, correct_index = 0;
Jan Kara83448bd2019-11-05 17:44:29 +01002736 int depth = ext_depth(inode), credits, revoke_credits;
Alex Tomasa86c6182006-10-11 01:21:03 -07002737 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002738 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002739 unsigned num;
2740 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002741 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002742 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002743 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002744 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002745
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002746 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002747 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002748 if (!path[depth].p_hdr)
2749 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2750 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002751 if (unlikely(path[depth].p_hdr == NULL)) {
2752 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002753 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002754 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002755 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002756 ex = path[depth].p_ext;
2757 if (!ex)
2758 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002759
2760 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002761 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002762
Eric Whitney9fe67142018-10-01 14:25:08 -04002763 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002764
Alex Tomasa86c6182006-10-11 01:21:03 -07002765 while (ex >= EXT_FIRST_EXTENT(eh) &&
2766 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002767
Lukas Czerner556615d2014-04-20 23:45:47 -04002768 if (ext4_ext_is_unwritten(ex))
2769 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002770 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002771 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002772
Mingming553f9002009-09-18 13:34:55 -04002773 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002774 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002775 path[depth].p_ext = ex;
2776
2777 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002778 b = ex_ee_block+ex_ee_len - 1 < end ?
2779 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002780
2781 ext_debug(" border %u:%u\n", a, b);
2782
Allison Hendersond583fb82011-05-25 07:41:43 -04002783 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002784 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002785 /*
2786 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002787 * so note that its first cluster is in use to avoid
2788 * freeing it when removing blocks. Eventually, the
2789 * right edge of the truncated/punched region will
2790 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002791 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002792 if (sbi->s_cluster_ratio > 1) {
2793 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002794 partial->pclu = EXT4_B2C(sbi, pblk);
2795 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002796 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002797 ex--;
2798 ex_ee_block = le32_to_cpu(ex->ee_block);
2799 ex_ee_len = ext4_ext_get_actual_len(ex);
2800 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002801 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002802 EXT4_ERROR_INODE(inode,
2803 "can not handle truncate %u:%u "
2804 "on extent %u:%u",
2805 start, end, ex_ee_block,
2806 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002807 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002808 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002809 } else if (a != ex_ee_block) {
2810 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002811 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002812 } else {
2813 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002814 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002815 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002816 /*
2817 * 3 for leaf, sb, and inode plus 2 (bmap and group
2818 * descriptor) for each block group; assume two block
2819 * groups plus ex_ee_len/blocks_per_block_group for
2820 * the worst case
2821 */
2822 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002823 if (ex == EXT_FIRST_EXTENT(eh)) {
2824 correct_index = 1;
2825 credits += (ext_depth(inode)) + 1;
2826 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002827 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Jan Kara83448bd2019-11-05 17:44:29 +01002828 /*
2829 * We may end up freeing some index blocks and data from the
2830 * punched range. Note that partial clusters are accounted for
2831 * by ext4_free_data_revoke_credits().
2832 */
2833 revoke_credits =
2834 ext4_free_metadata_revoke_credits(inode->i_sb,
2835 ext_depth(inode)) +
2836 ext4_free_data_revoke_credits(inode, b - a + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002837
Jan Karaa4130362019-11-05 17:44:16 +01002838 err = ext4_datasem_ensure_credits(handle, inode, credits,
Jan Kara83448bd2019-11-05 17:44:29 +01002839 credits, revoke_credits);
Jan Karaa4130362019-11-05 17:44:16 +01002840 if (err) {
2841 if (err > 0)
2842 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002843 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002844 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002845
2846 err = ext4_ext_get_access(handle, inode, path + depth);
2847 if (err)
2848 goto out;
2849
Eric Whitney9fe67142018-10-01 14:25:08 -04002850 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002851 if (err)
2852 goto out;
2853
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002854 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002855 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002856 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002857
Alex Tomasa86c6182006-10-11 01:21:03 -07002858 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002859 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002860 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002861 * extent have been removed.
2862 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002863 if (unwritten && num)
2864 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002865 /*
2866 * If the extent was completely released,
2867 * we need to remove it from the leaf
2868 */
2869 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002870 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002871 /*
2872 * For hole punching, we need to scoot all the
2873 * extents up when an extent is removed so that
2874 * we dont have blank extents in the middle
2875 */
2876 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2877 sizeof(struct ext4_extent));
2878
2879 /* Now get rid of the one at the end */
2880 memset(EXT_LAST_EXTENT(eh), 0,
2881 sizeof(struct ext4_extent));
2882 }
2883 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002884 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002885
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002886 err = ext4_ext_dirty(handle, inode, path + depth);
2887 if (err)
2888 goto out;
2889
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002890 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002891 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002892 ex--;
2893 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002894 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002895 }
2896
2897 if (correct_index && eh->eh_entries)
2898 err = ext4_ext_correct_indexes(handle, inode, path);
2899
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002900 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002901 * If there's a partial cluster and at least one extent remains in
2902 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002903 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002904 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002905 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002906 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002907 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002908 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002909 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2910 int flags = get_default_free_blocks_flags(inode);
2911
2912 if (ext4_is_pending(inode, partial->lblk))
2913 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002914 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002915 EXT4_C2B(sbi, partial->pclu),
2916 sbi->s_cluster_ratio, flags);
2917 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2918 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002919 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002920 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002921 }
2922
Alex Tomasa86c6182006-10-11 01:21:03 -07002923 /* if this leaf is free, then we should
2924 * remove it from index block above */
2925 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002926 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002927
2928out:
2929 return err;
2930}
2931
2932/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002933 * ext4_ext_more_to_rm:
2934 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002935 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002936static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002937ext4_ext_more_to_rm(struct ext4_ext_path *path)
2938{
2939 BUG_ON(path->p_idx == NULL);
2940
2941 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2942 return 0;
2943
2944 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002945 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002946 * so we have to consider current index for truncation
2947 */
2948 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2949 return 0;
2950 return 1;
2951}
2952
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002953int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2954 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002955{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002956 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002957 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002958 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002959 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002960 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002961 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002962
Eric Whitney9fe67142018-10-01 14:25:08 -04002963 partial.pclu = 0;
2964 partial.lblk = 0;
2965 partial.state = initial;
2966
Lukas Czerner5f95d212012-03-19 23:03:19 -04002967 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002968
2969 /* probably first extent we're gonna free will be last in block */
Jan Kara83448bd2019-11-05 17:44:29 +01002970 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2971 depth + 1,
2972 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
Alex Tomasa86c6182006-10-11 01:21:03 -07002973 if (IS_ERR(handle))
2974 return PTR_ERR(handle);
2975
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002976again:
Lukas Czerner61801322013-05-27 23:32:35 -04002977 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002978
Alex Tomasa86c6182006-10-11 01:21:03 -07002979 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002980 * Check if we are removing extents inside the extent tree. If that
2981 * is the case, we are going to punch a hole inside the extent tree
2982 * so we have to check whether we need to split the extent covering
2983 * the last block to remove so we can easily remove the part of it
2984 * in ext4_ext_rm_leaf().
2985 */
2986 if (end < EXT_MAX_BLOCKS - 1) {
2987 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002988 ext4_lblk_t ee_block, ex_end, lblk;
2989 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002990
Eric Whitneyf4226d92014-11-23 00:55:42 -05002991 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002992 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002993 if (IS_ERR(path)) {
2994 ext4_journal_stop(handle);
2995 return PTR_ERR(path);
2996 }
2997 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002998 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002999 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003000 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04003001 if (depth) {
3002 EXT4_ERROR_INODE(inode,
3003 "path[%d].p_hdr == NULL",
3004 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003005 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04003006 }
3007 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003008 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003009
3010 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05003011 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003012
3013 /*
3014 * See if the last block is inside the extent, if so split
3015 * the extent at 'end' block so we can easily remove the
3016 * tail of the first part of the split extent in
3017 * ext4_ext_rm_leaf().
3018 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05003019 if (end >= ee_block && end < ex_end) {
3020
3021 /*
3022 * If we're going to split the extent, note that
3023 * the cluster containing the block after 'end' is
3024 * in use to avoid freeing it when removing blocks.
3025 */
3026 if (sbi->s_cluster_ratio > 1) {
3027 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04003028 partial.pclu = EXT4_B2C(sbi, pblk);
3029 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05003030 }
3031
Lukas Czerner5f95d212012-03-19 23:03:19 -04003032 /*
3033 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04003034 * block in the first new extent. Also we should not
3035 * fail removing space due to ENOSPC so try to use
3036 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04003037 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04003038 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04003039 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04003040 if (err < 0)
3041 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05003042
Eric Whitney7bd75232019-02-28 23:34:11 -05003043 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
3044 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05003045 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05003046 * If we're punching, there's an extent to the right.
3047 * If the partial cluster hasn't been set, set it to
3048 * that extent's first cluster and its state to nofree
3049 * so it won't be freed should it contain blocks to be
3050 * removed. If it's already set (tofree/nofree), we're
3051 * retrying and keep the original partial cluster info
3052 * so a cluster marked tofree as a result of earlier
3053 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05003054 */
3055 lblk = ex_end + 1;
3056 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
3057 &ex);
3058 if (err)
3059 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04003060 if (pblk) {
3061 partial.pclu = EXT4_B2C(sbi, pblk);
3062 partial.state = nofree;
3063 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003064 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003065 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003066 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003067 * We start scanning from right side, freeing all the blocks
3068 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07003069 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04003070 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04003071 if (path) {
3072 int k = i = depth;
3073 while (--k > 0)
3074 path[k].p_block =
3075 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
3076 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003077 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Ashish Sangwan968dee72012-07-22 22:49:08 -04003078 GFP_NOFS);
3079 if (path == NULL) {
3080 ext4_journal_stop(handle);
3081 return -ENOMEM;
3082 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04003083 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003084 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04003085 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003086
Theodore Ts'oc3491792013-08-16 21:21:41 -04003087 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003088 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003089 goto out;
3090 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003091 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04003092 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07003093
3094 while (i >= 0 && err == 0) {
3095 if (i == depth) {
3096 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04003097 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04003098 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003099 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003100 brelse(path[i].p_bh);
3101 path[i].p_bh = NULL;
3102 i--;
3103 continue;
3104 }
3105
3106 /* this is index block */
3107 if (!path[i].p_hdr) {
3108 ext_debug("initialize header\n");
3109 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003110 }
3111
Alex Tomasa86c6182006-10-11 01:21:03 -07003112 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003113 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07003114 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
3115 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
3116 ext_debug("init index ptr: hdr 0x%p, num %d\n",
3117 path[i].p_hdr,
3118 le16_to_cpu(path[i].p_hdr->eh_entries));
3119 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003120 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07003121 path[i].p_idx--;
3122 }
3123
3124 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3125 i, EXT_FIRST_INDEX(path[i].p_hdr),
3126 path[i].p_idx);
3127 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003128 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003129 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07003130 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04003131 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07003132 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003133 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04003134 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
3135 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003136 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003137 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003138 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003139 break;
3140 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04003141 /* Yield here to deal with large extent trees.
3142 * Should be a no-op if we did IO above. */
3143 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003144 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003145 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003146 break;
3147 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003148 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003149
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003150 /* save actual number of indexes since this
3151 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07003152 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3153 i++;
3154 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003155 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07003156 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003157 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07003158 * handle must be already prepared by the
3159 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05003160 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07003161 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003162 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003163 brelse(path[i].p_bh);
3164 path[i].p_bh = NULL;
3165 i--;
3166 ext_debug("return to level %d\n", i);
3167 }
3168 }
3169
Eric Whitney9fe67142018-10-01 14:25:08 -04003170 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
3171 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003172
Eric Whitney0756b902014-11-23 00:59:39 -05003173 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04003174 * if there's a partial cluster and we have removed the first extent
3175 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05003176 */
Eric Whitney9fe67142018-10-01 14:25:08 -04003177 if (partial.state == tofree && err == 0) {
3178 int flags = get_default_free_blocks_flags(inode);
3179
3180 if (ext4_is_pending(inode, partial.lblk))
3181 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003182 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003183 EXT4_C2B(sbi, partial.pclu),
3184 sbi->s_cluster_ratio, flags);
3185 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3186 ext4_rereserve_cluster(inode, partial.lblk);
3187 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003188 }
3189
Alex Tomasa86c6182006-10-11 01:21:03 -07003190 /* TODO: flexible tree reduction should be here */
3191 if (path->p_hdr->eh_entries == 0) {
3192 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003193 * truncate to zero freed all the tree,
3194 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003195 */
3196 err = ext4_ext_get_access(handle, inode, path);
3197 if (err == 0) {
3198 ext_inode_hdr(inode)->eh_depth = 0;
3199 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003200 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003201 err = ext4_ext_dirty(handle, inode, path);
3202 }
3203 }
3204out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003205 ext4_ext_drop_refs(path);
3206 kfree(path);
3207 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003208 if (err == -EAGAIN)
3209 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003210 ext4_journal_stop(handle);
3211
3212 return err;
3213}
3214
3215/*
3216 * called at mount time
3217 */
3218void ext4_ext_init(struct super_block *sb)
3219{
3220 /*
3221 * possible initialization would be here
3222 */
3223
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003224 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003225#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003226 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003227#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003228 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003229#endif
3230#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003231 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003232#endif
3233#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003234 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003235#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003236 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003237#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003238#ifdef EXTENTS_STATS
3239 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3240 EXT4_SB(sb)->s_ext_min = 1 << 30;
3241 EXT4_SB(sb)->s_ext_max = 0;
3242#endif
3243 }
3244}
3245
3246/*
3247 * called at umount time
3248 */
3249void ext4_ext_release(struct super_block *sb)
3250{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003251 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003252 return;
3253
3254#ifdef EXTENTS_STATS
3255 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3256 struct ext4_sb_info *sbi = EXT4_SB(sb);
3257 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3258 sbi->s_ext_blocks, sbi->s_ext_extents,
3259 sbi->s_ext_blocks / sbi->s_ext_extents);
3260 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3261 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3262 }
3263#endif
3264}
3265
Zheng Liud7b2a002013-08-28 14:47:06 -04003266static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3267{
3268 ext4_lblk_t ee_block;
3269 ext4_fsblk_t ee_pblock;
3270 unsigned int ee_len;
3271
3272 ee_block = le32_to_cpu(ex->ee_block);
3273 ee_len = ext4_ext_get_actual_len(ex);
3274 ee_pblock = ext4_ext_pblock(ex);
3275
3276 if (ee_len == 0)
3277 return 0;
3278
3279 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3280 EXTENT_STATUS_WRITTEN);
3281}
3282
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003283/* FIXME!! we need to try to merge to left or right after zero-out */
3284static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3285{
Lukas Czerner24075182010-10-27 21:30:06 -04003286 ext4_fsblk_t ee_pblock;
3287 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003288
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003289 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003290 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003291 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3292 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003293}
3294
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003295/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296 * ext4_split_extent_at() splits an extent at given block.
3297 *
3298 * @handle: the journal handle
3299 * @inode: the file inode
3300 * @path: the path to the extent
3301 * @split: the logical block where the extent is splitted.
3302 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003303 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003304 * @flags: flags used to insert new extent to extent tree.
3305 *
3306 *
3307 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3308 * of which are deterimined by split_flag.
3309 *
3310 * There are two cases:
3311 * a> the extent are splitted into two extent.
3312 * b> split is not needed, and just mark the extent.
3313 *
3314 * return 0 on success.
3315 */
3316static int ext4_split_extent_at(handle_t *handle,
3317 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003318 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003319 ext4_lblk_t split,
3320 int split_flag,
3321 int flags)
3322{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003323 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003324 ext4_fsblk_t newblock;
3325 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003326 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003327 struct ext4_extent *ex2 = NULL;
3328 unsigned int ee_len, depth;
3329 int err = 0;
3330
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003331 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3332 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3333
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003334 ext_debug("ext4_split_extents_at: inode %lu, logical"
3335 "block %llu\n", inode->i_ino, (unsigned long long)split);
3336
3337 ext4_ext_show_leaf(inode, path);
3338
3339 depth = ext_depth(inode);
3340 ex = path[depth].p_ext;
3341 ee_block = le32_to_cpu(ex->ee_block);
3342 ee_len = ext4_ext_get_actual_len(ex);
3343 newblock = split - ee_block + ext4_ext_pblock(ex);
3344
3345 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003346 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003347 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003348 EXT4_EXT_MARK_UNWRIT1 |
3349 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003350
3351 err = ext4_ext_get_access(handle, inode, path + depth);
3352 if (err)
3353 goto out;
3354
3355 if (split == ee_block) {
3356 /*
3357 * case b: block @split is the block that the extent begins with
3358 * then we just change the state of the extent, and splitting
3359 * is not needed.
3360 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003361 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3362 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003363 else
3364 ext4_ext_mark_initialized(ex);
3365
3366 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003367 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003368
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003369 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003370 goto out;
3371 }
3372
3373 /* case a */
3374 memcpy(&orig_ex, ex, sizeof(orig_ex));
3375 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003376 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3377 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003378
3379 /*
3380 * path may lead to new leaf, not to original leaf any more
3381 * after ext4_ext_insert_extent() returns,
3382 */
3383 err = ext4_ext_dirty(handle, inode, path + depth);
3384 if (err)
3385 goto fix_extent_len;
3386
3387 ex2 = &newex;
3388 ex2->ee_block = cpu_to_le32(split);
3389 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3390 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003391 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3392 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003393
Theodore Ts'odfe50802014-09-01 14:37:09 -04003394 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003395 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003396 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003397 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003398 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003399 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003400 zero_ex.ee_len = cpu_to_le16(
3401 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003402 ext4_ext_store_pblock(&zero_ex,
3403 ext4_ext_pblock(ex2));
3404 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003405 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003406 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003407 zero_ex.ee_len = cpu_to_le16(
3408 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003409 ext4_ext_store_pblock(&zero_ex,
3410 ext4_ext_pblock(ex));
3411 }
3412 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003413 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003414 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003415 zero_ex.ee_len = cpu_to_le16(
3416 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003417 ext4_ext_store_pblock(&zero_ex,
3418 ext4_ext_pblock(&orig_ex));
3419 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003420
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003421 if (err)
3422 goto fix_extent_len;
3423 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003424 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003425 ext4_ext_try_to_merge(handle, inode, path, ex);
3426 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003427 if (err)
3428 goto fix_extent_len;
3429
3430 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003431 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003432
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003433 goto out;
3434 } else if (err)
3435 goto fix_extent_len;
3436
3437out:
3438 ext4_ext_show_leaf(inode, path);
3439 return err;
3440
3441fix_extent_len:
3442 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003443 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003444 return err;
3445}
3446
3447/*
3448 * ext4_split_extents() splits an extent and mark extent which is covered
3449 * by @map as split_flags indicates
3450 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003451 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003452 * There are three possibilities:
3453 * a> There is no split required
3454 * b> Splits in two extents: Split is happening at either end of the extent
3455 * c> Splits in three extents: Somone is splitting in middle of the extent
3456 *
3457 */
3458static int ext4_split_extent(handle_t *handle,
3459 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003460 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003461 struct ext4_map_blocks *map,
3462 int split_flag,
3463 int flags)
3464{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003465 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003466 ext4_lblk_t ee_block;
3467 struct ext4_extent *ex;
3468 unsigned int ee_len, depth;
3469 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003470 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003471 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003472 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003473
3474 depth = ext_depth(inode);
3475 ex = path[depth].p_ext;
3476 ee_block = le32_to_cpu(ex->ee_block);
3477 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003478 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003479
3480 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003481 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003482 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003483 if (unwritten)
3484 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3485 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003486 if (split_flag & EXT4_EXT_DATA_VALID2)
3487 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003488 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003489 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003490 if (err)
3491 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003492 } else {
3493 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003494 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003495 /*
3496 * Update path is required because previous ext4_split_extent_at() may
3497 * result in split of original leaf or extent zeroout.
3498 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003499 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003500 if (IS_ERR(path))
3501 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003502 depth = ext_depth(inode);
3503 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003504 if (!ex) {
3505 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3506 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003507 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003508 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003509 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003510 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003511
3512 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003513 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003514 if (unwritten) {
3515 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003516 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003517 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003518 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003519 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003520 map->m_lblk, split_flag1, flags);
3521 if (err)
3522 goto out;
3523 }
3524
3525 ext4_ext_show_leaf(inode, path);
3526out:
Zheng Liu3a225672013-03-10 21:20:23 -04003527 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003528}
3529
Amit Arora56055d32007-07-17 21:42:38 -04003530/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003531 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003532 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003533 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003534 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003535 * There are three possibilities:
3536 * a> There is no split required: Entire extent should be initialized
3537 * b> Splits in two extents: Write is happening at either end of the extent
3538 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003539 *
3540 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003541 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003542 * - The extent pointed to by 'path' contains a superset
3543 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3544 *
3545 * Post-conditions on success:
3546 * - the returned value is the number of blocks beyond map->l_lblk
3547 * that are allocated and initialized.
3548 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003549 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003550static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003551 struct inode *inode,
3552 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003553 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003554 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003555{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003556 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003557 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003558 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003559 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003560 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003561 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003562 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003563 unsigned int ee_len, depth, map_len = map->m_len;
3564 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003565 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003566 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003567
3568 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3569 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003570 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003571
Zheng Liu67a5da52012-08-17 09:54:17 -04003572 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003573 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3574 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003575 if (eof_block < map->m_lblk + map_len)
3576 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003577
3578 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003579 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003580 ex = path[depth].p_ext;
3581 ee_block = le32_to_cpu(ex->ee_block);
3582 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003583 zero_ex1.ee_len = 0;
3584 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003585
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003586 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3587
3588 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003589 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003590 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003591
3592 /*
3593 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003594 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003595 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003596 * memmove() calls. Transferring to the left is the common case in
3597 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3598 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003599 *
3600 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003601 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003602 * This would require removing the extent if the transfer
3603 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003604 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003605 * same extent tree node.
3606 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003607 if ((map->m_lblk == ee_block) &&
3608 /* See if we can merge left */
3609 (map_len < ee_len) && /*L1*/
3610 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003611 ext4_lblk_t prev_lblk;
3612 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003613 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003614
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003615 abut_ex = ex - 1;
3616 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3617 prev_len = ext4_ext_get_actual_len(abut_ex);
3618 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003619 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003620
3621 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003622 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003623 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003624 * - C1: abut_ex is initialized,
3625 * - C2: abut_ex is logically abutting ex,
3626 * - C3: abut_ex is physically abutting ex,
3627 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003628 * overflowing the (initialized) length limit.
3629 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003630 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003631 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3632 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003633 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003634 err = ext4_ext_get_access(handle, inode, path + depth);
3635 if (err)
3636 goto out;
3637
3638 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003639 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003640
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003641 /* Shift the start of ex by 'map_len' blocks */
3642 ex->ee_block = cpu_to_le32(ee_block + map_len);
3643 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3644 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003645 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003646
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003647 /* Extend abut_ex by 'map_len' blocks */
3648 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003649
3650 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003651 allocated = map_len;
3652 }
3653 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3654 (map_len < ee_len) && /*L1*/
3655 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3656 /* See if we can merge right */
3657 ext4_lblk_t next_lblk;
3658 ext4_fsblk_t next_pblk, ee_pblk;
3659 unsigned int next_len;
3660
3661 abut_ex = ex + 1;
3662 next_lblk = le32_to_cpu(abut_ex->ee_block);
3663 next_len = ext4_ext_get_actual_len(abut_ex);
3664 next_pblk = ext4_ext_pblock(abut_ex);
3665 ee_pblk = ext4_ext_pblock(ex);
3666
3667 /*
3668 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3669 * upon those conditions:
3670 * - C1: abut_ex is initialized,
3671 * - C2: abut_ex is logically abutting ex,
3672 * - C3: abut_ex is physically abutting ex,
3673 * - C4: abut_ex can receive the additional blocks without
3674 * overflowing the (initialized) length limit.
3675 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003676 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003677 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3678 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3679 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3680 err = ext4_ext_get_access(handle, inode, path + depth);
3681 if (err)
3682 goto out;
3683
3684 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3685 map, ex, abut_ex);
3686
3687 /* Shift the start of abut_ex by 'map_len' blocks */
3688 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3689 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3690 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003691 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003692
3693 /* Extend abut_ex by 'map_len' blocks */
3694 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3695
3696 /* Result: number of initialized blocks past m_lblk */
3697 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003698 }
3699 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003700 if (allocated) {
3701 /* Mark the block containing both extents as dirty */
3702 ext4_ext_dirty(handle, inode, path + depth);
3703
3704 /* Update path to point to the right extent */
3705 path[depth].p_ext = abut_ex;
3706 goto out;
3707 } else
3708 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003709
Yongqiang Yang667eff32011-05-03 12:25:07 -04003710 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003711 /*
3712 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003713 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003714 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003715 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003716
Zheng Liu67a5da52012-08-17 09:54:17 -04003717 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3718 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003719 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003720
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303721 if (IS_ENCRYPTED(inode))
Theodore Ts'o36086d42015-10-03 10:49:29 -04003722 max_zeroout = 0;
3723
Amit Arora56055d32007-07-17 21:42:38 -04003724 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003725 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003726 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003727 * 2. split the extent into two extents, zeroout the head of the first
3728 * extent.
3729 * 3. split the extent into two extents, zeroout the tail of the second
3730 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003731 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003732 * 5. no splitting needed, just possibly zeroout the head and / or the
3733 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003734 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003735 split_map.m_lblk = map->m_lblk;
3736 split_map.m_len = map->m_len;
3737
Jan Kara4f8caa62017-05-26 17:40:52 -04003738 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003739 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003740 /* case 3 or 5 */
3741 zero_ex1.ee_block =
3742 cpu_to_le32(split_map.m_lblk +
3743 split_map.m_len);
3744 zero_ex1.ee_len =
3745 cpu_to_le16(allocated - split_map.m_len);
3746 ext4_ext_store_pblock(&zero_ex1,
3747 ext4_ext_pblock(ex) + split_map.m_lblk +
3748 split_map.m_len - ee_block);
3749 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003750 if (err)
3751 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003752 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003753 }
3754 if (split_map.m_lblk - ee_block + split_map.m_len <
3755 max_zeroout) {
3756 /* case 2 or 5 */
3757 if (split_map.m_lblk != ee_block) {
3758 zero_ex2.ee_block = ex->ee_block;
3759 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003760 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003761 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003762 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003763 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003764 if (err)
3765 goto out;
3766 }
3767
Jan Kara4f8caa62017-05-26 17:40:52 -04003768 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003769 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003770 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003771 }
3772 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003773
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003774 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3775 flags);
3776 if (err > 0)
3777 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003778out:
Zheng Liuadb23552013-03-10 21:13:05 -04003779 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003780 if (!err) {
3781 err = ext4_zeroout_es(inode, &zero_ex1);
3782 if (!err)
3783 err = ext4_zeroout_es(inode, &zero_ex2);
3784 }
Amit Arora56055d32007-07-17 21:42:38 -04003785 return err ? err : allocated;
3786}
3787
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003788/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003789 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003790 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003791 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003792 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003793 * Writing to an unwritten extent may result in splitting the unwritten
3794 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003795 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003796 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003797 * b> Splits in two extents: Write is happening at either end of the extent
3798 * c> Splits in three extents: Somone is writing in middle of the extent
3799 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003800 * This works the same way in the case of initialized -> unwritten conversion.
3801 *
Mingming Cao00314622009-09-28 15:49:08 -04003802 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003803 * the unwritten extent split. To prevent ENOSPC occur at the IO
3804 * complete, we need to split the unwritten extent before DIO submit
3805 * the IO. The unwritten extent called at this time will be split
3806 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003807 * being filled will be convert to initialized by the end_io callback function
3808 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003809 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003810 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003811 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003812static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003813 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003814 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003815 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003816 int flags)
3817{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003818 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003819 ext4_lblk_t eof_block;
3820 ext4_lblk_t ee_block;
3821 struct ext4_extent *ex;
3822 unsigned int ee_len;
3823 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003824
Lukas Czernerb8a86842014-03-18 18:05:35 -04003825 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3826 __func__, inode->i_ino,
3827 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003828
3829 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3830 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003831 if (eof_block < map->m_lblk + map->m_len)
3832 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003833 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003834 * It is safe to convert extent to initialized via explicit
3835 * zeroout only if extent is fully insde i_size or new_size.
3836 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003837 depth = ext_depth(inode);
3838 ex = path[depth].p_ext;
3839 ee_block = le32_to_cpu(ex->ee_block);
3840 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003841
Lukas Czernerb8a86842014-03-18 18:05:35 -04003842 /* Convert to unwritten */
3843 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3844 split_flag |= EXT4_EXT_DATA_VALID1;
3845 /* Convert to initialized */
3846 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3847 split_flag |= ee_block + ee_len <= eof_block ?
3848 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003849 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003850 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003851 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003852 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003853}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003854
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003855static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003856 struct inode *inode,
3857 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003858 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003859{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003860 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003861 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003862 ext4_lblk_t ee_block;
3863 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003864 int depth;
3865 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003866
3867 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003868 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003869 ee_block = le32_to_cpu(ex->ee_block);
3870 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003871
Yongqiang Yang197217a2011-05-03 11:45:29 -04003872 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3873 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003874 (unsigned long long)ee_block, ee_len);
3875
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003876 /* If extent is larger than requested it is a clear sign that we still
3877 * have some extent state machine issues left. So extent_split is still
3878 * required.
3879 * TODO: Once all related issues will be fixed this situation should be
3880 * illegal.
3881 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003882 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003883#ifdef CONFIG_EXT4_DEBUG
3884 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003885 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003886 inode->i_ino, (unsigned long long)ee_block, ee_len,
3887 (unsigned long long)map->m_lblk, map->m_len);
3888#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003889 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003890 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003891 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003892 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003893 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003894 if (IS_ERR(path))
3895 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003896 depth = ext_depth(inode);
3897 ex = path[depth].p_ext;
3898 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003899
Mingming Cao00314622009-09-28 15:49:08 -04003900 err = ext4_ext_get_access(handle, inode, path + depth);
3901 if (err)
3902 goto out;
3903 /* first mark the extent as initialized */
3904 ext4_ext_mark_initialized(ex);
3905
Yongqiang Yang197217a2011-05-03 11:45:29 -04003906 /* note: ext4_ext_correct_indexes() isn't needed here because
3907 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003908 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003909 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003910
Mingming Cao00314622009-09-28 15:49:08 -04003911 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003912 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003913out:
3914 ext4_ext_show_leaf(inode, path);
3915 return err;
3916}
3917
Theodore Ts'o58590b02010-10-27 21:23:12 -04003918/*
3919 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3920 */
3921static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003922 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003923 struct ext4_ext_path *path,
3924 unsigned int len)
3925{
3926 int i, depth;
3927 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003928 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003929
3930 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3931 return 0;
3932
3933 depth = ext_depth(inode);
3934 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003935
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003936 /*
3937 * We're going to remove EOFBLOCKS_FL entirely in future so we
3938 * do not care for this case anymore. Simply remove the flag
3939 * if there are no extents.
3940 */
3941 if (unlikely(!eh->eh_entries))
3942 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003943 last_ex = EXT_LAST_EXTENT(eh);
3944 /*
3945 * We should clear the EOFBLOCKS_FL flag if we are writing the
3946 * last block in the last extent in the file. We test this by
3947 * first checking to see if the caller to
3948 * ext4_ext_get_blocks() was interested in the last block (or
3949 * a block beyond the last block) in the current extent. If
3950 * this turns out to be false, we can bail out from this
3951 * function immediately.
3952 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003953 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003954 ext4_ext_get_actual_len(last_ex))
3955 return 0;
3956 /*
3957 * If the caller does appear to be planning to write at or
3958 * beyond the end of the current extent, we then test to see
3959 * if the current extent is the last extent in the file, by
3960 * checking to make sure it was reached via the rightmost node
3961 * at each level of the tree.
3962 */
3963 for (i = depth-1; i >= 0; i--)
3964 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3965 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003966out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003967 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3968 return ext4_mark_inode_dirty(handle, inode);
3969}
3970
Mingming Cao00314622009-09-28 15:49:08 -04003971static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003972convert_initialized_extent(handle_t *handle, struct inode *inode,
3973 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003974 struct ext4_ext_path **ppath,
Eryu Guan56263b42016-02-12 01:23:00 -05003975 unsigned int allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003976{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003977 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003978 struct ext4_extent *ex;
3979 ext4_lblk_t ee_block;
3980 unsigned int ee_len;
3981 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003982 int err = 0;
3983
3984 /*
3985 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003986 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003987 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003988 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3989 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003990
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003991 depth = ext_depth(inode);
3992 ex = path[depth].p_ext;
3993 ee_block = le32_to_cpu(ex->ee_block);
3994 ee_len = ext4_ext_get_actual_len(ex);
3995
3996 ext_debug("%s: inode %lu, logical"
3997 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3998 (unsigned long long)ee_block, ee_len);
3999
4000 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004001 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004002 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
4003 if (err < 0)
4004 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004005 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004006 if (IS_ERR(path))
4007 return PTR_ERR(path);
4008 depth = ext_depth(inode);
4009 ex = path[depth].p_ext;
4010 if (!ex) {
4011 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
4012 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004013 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004014 }
4015 }
4016
4017 err = ext4_ext_get_access(handle, inode, path + depth);
4018 if (err)
4019 return err;
4020 /* first mark the extent as unwritten */
4021 ext4_ext_mark_unwritten(ex);
4022
4023 /* note: ext4_ext_correct_indexes() isn't needed here because
4024 * borders are not changed
4025 */
4026 ext4_ext_try_to_merge(handle, inode, path, ex);
4027
4028 /* Mark modified extent as dirty */
4029 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4030 if (err)
4031 return err;
4032 ext4_ext_show_leaf(inode, path);
4033
4034 ext4_update_inode_fsync_trans(handle, inode, 1);
4035 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4036 if (err)
4037 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004038 map->m_flags |= EXT4_MAP_UNWRITTEN;
4039 if (allocated > map->m_len)
4040 allocated = map->m_len;
4041 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004042 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004043}
4044
4045static int
Lukas Czerner556615d2014-04-20 23:45:47 -04004046ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004047 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004048 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004049 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04004050{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004051 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004052 int ret = 0;
4053 int err = 0;
4054
Lukas Czerner556615d2014-04-20 23:45:47 -04004055 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004056 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004057 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004058 flags, allocated);
4059 ext4_ext_show_leaf(inode, path);
4060
Lukas Czerner27dd4382013-04-09 22:11:22 -04004061 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004062 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004063 * allocate metadata blocks for the new extent block if needed.
4064 */
4065 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4066
Lukas Czerner556615d2014-04-20 23:45:47 -04004067 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004068 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004069
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004070 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004071 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004072 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4073 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004074 if (ret <= 0)
4075 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004076 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004077 goto out;
4078 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004079 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004080 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05004081 if (flags & EXT4_GET_BLOCKS_ZERO) {
4082 if (allocated > map->m_len)
4083 allocated = map->m_len;
4084 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4085 allocated);
4086 if (err < 0)
4087 goto out2;
4088 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004089 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004090 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004091 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004092 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004093 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4094 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004095 } else
4096 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004097 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004098 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004099 if (allocated > map->m_len)
4100 allocated = map->m_len;
4101 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004102 goto out2;
4103 }
4104 /* buffered IO case */
4105 /*
4106 * repeat fallocate creation request
4107 * we already have an unwritten extent
4108 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004109 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004110 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004111 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004112 }
Mingming Cao00314622009-09-28 15:49:08 -04004113
4114 /* buffered READ or buffered write_begin() lookup */
4115 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4116 /*
4117 * We have blocks reserved already. We
4118 * return allocated blocks so that delalloc
4119 * won't do block reservation for us. But
4120 * the buffer head will be unmapped so that
4121 * a read from the block returns 0s.
4122 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004123 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004124 goto out1;
4125 }
4126
4127 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004128 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004129 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004130 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004131out:
4132 if (ret <= 0) {
4133 err = ret;
4134 goto out2;
4135 } else
4136 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004137 map->m_flags |= EXT4_MAP_NEW;
zhangyi (F)16e08b12019-02-10 23:32:07 -05004138 if (allocated > map->m_len)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004139 allocated = map->m_len;
Zheng Liu3a225672013-03-10 21:20:23 -04004140 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004141
Mingming Cao00314622009-09-28 15:49:08 -04004142map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004143 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004144 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4145 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4146 map->m_len);
4147 if (err < 0)
4148 goto out2;
4149 }
Mingming Cao00314622009-09-28 15:49:08 -04004150out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004151 if (allocated > map->m_len)
4152 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004153 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004154 map->m_pblk = newblock;
4155 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004156out2:
Mingming Cao00314622009-09-28 15:49:08 -04004157 return err ? err : allocated;
4158}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004159
Mingming Cao00314622009-09-28 15:49:08 -04004160/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004161 * get_implied_cluster_alloc - check to see if the requested
4162 * allocation (in the map structure) overlaps with a cluster already
4163 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004164 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004165 * @map The requested lblk->pblk mapping
4166 * @ex The extent structure which might contain an implied
4167 * cluster allocation
4168 *
4169 * This function is called by ext4_ext_map_blocks() after we failed to
4170 * find blocks that were already in the inode's extent tree. Hence,
4171 * we know that the beginning of the requested region cannot overlap
4172 * the extent from the inode's extent tree. There are three cases we
4173 * want to catch. The first is this case:
4174 *
4175 * |--- cluster # N--|
4176 * |--- extent ---| |---- requested region ---|
4177 * |==========|
4178 *
4179 * The second case that we need to test for is this one:
4180 *
4181 * |--------- cluster # N ----------------|
4182 * |--- requested region --| |------- extent ----|
4183 * |=======================|
4184 *
4185 * The third case is when the requested region lies between two extents
4186 * within the same cluster:
4187 * |------------- cluster # N-------------|
4188 * |----- ex -----| |---- ex_right ----|
4189 * |------ requested region ------|
4190 * |================|
4191 *
4192 * In each of the above cases, we need to set the map->m_pblk and
4193 * map->m_len so it corresponds to the return the extent labelled as
4194 * "|====|" from cluster #N, since it is already in use for data in
4195 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4196 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4197 * as a new "allocated" block region. Otherwise, we will return 0 and
4198 * ext4_ext_map_blocks() will then allocate one or more new clusters
4199 * by calling ext4_mb_new_blocks().
4200 */
Aditya Kalid8990242011-09-09 19:18:51 -04004201static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004202 struct ext4_map_blocks *map,
4203 struct ext4_extent *ex,
4204 struct ext4_ext_path *path)
4205{
Aditya Kalid8990242011-09-09 19:18:51 -04004206 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004207 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004208 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004209 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004210 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4211 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4212 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4213
4214 /* The extent passed in that we are trying to match */
4215 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4216 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4217
4218 /* The requested region passed into ext4_map_blocks() */
4219 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004220
4221 if ((rr_cluster_start == ex_cluster_end) ||
4222 (rr_cluster_start == ex_cluster_start)) {
4223 if (rr_cluster_start == ex_cluster_end)
4224 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004225 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004226 map->m_len = min(map->m_len,
4227 (unsigned) sbi->s_cluster_ratio - c_offset);
4228 /*
4229 * Check for and handle this case:
4230 *
4231 * |--------- cluster # N-------------|
4232 * |------- extent ----|
4233 * |--- requested region ---|
4234 * |===========|
4235 */
4236
4237 if (map->m_lblk < ee_block)
4238 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4239
4240 /*
4241 * Check for the case where there is already another allocated
4242 * block to the right of 'ex' but before the end of the cluster.
4243 *
4244 * |------------- cluster # N-------------|
4245 * |----- ex -----| |---- ex_right ----|
4246 * |------ requested region ------|
4247 * |================|
4248 */
4249 if (map->m_lblk > ee_block) {
4250 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4251 map->m_len = min(map->m_len, next - map->m_lblk);
4252 }
Aditya Kalid8990242011-09-09 19:18:51 -04004253
4254 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004255 return 1;
4256 }
Aditya Kalid8990242011-09-09 19:18:51 -04004257
4258 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004259 return 0;
4260}
4261
4262
4263/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004264 * Block allocation/map/preallocation routine for extents based files
4265 *
4266 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004267 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004268 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4269 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004270 *
4271 * return > 0, number of of blocks already mapped/allocated
4272 * if create == 0 and these are pre-allocated blocks
4273 * buffer head is unmapped
4274 * otherwise blocks are mapped
4275 *
4276 * return = 0, if plain look up failed (blocks have not been allocated)
4277 * buffer head is unmapped
4278 *
4279 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004280 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004281int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4282 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004283{
4284 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004285 struct ext4_extent newex, *ex, *ex2;
4286 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004287 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004288 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004289 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004290 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004291 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004292 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004293 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004294
Mingming84fe3be2009-09-01 08:44:37 -04004295 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004296 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004297 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004298
Alex Tomasa86c6182006-10-11 01:21:03 -07004299 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004300 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004301 if (IS_ERR(path)) {
4302 err = PTR_ERR(path);
4303 path = NULL;
4304 goto out2;
4305 }
4306
4307 depth = ext_depth(inode);
4308
4309 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004310 * consistent leaf must not be empty;
4311 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004312 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004313 */
Frank Mayhar273df552010-03-02 11:46:09 -05004314 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4315 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004316 "lblock: %lu, depth: %d pblock %lld",
4317 (unsigned long) map->m_lblk, depth,
4318 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004319 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004320 goto out2;
4321 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004322
Avantika Mathur7e028972006-12-06 20:41:33 -08004323 ex = path[depth].p_ext;
4324 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004325 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004326 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004327 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004328
Lukas Czernerb8a86842014-03-18 18:05:35 -04004329
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004330 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004331 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004332 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004333 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004334 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004335
4336 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4337
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004338 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004339 if (in_range(map->m_lblk, ee_block, ee_len)) {
4340 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004341 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004342 allocated = ee_len - (map->m_lblk - ee_block);
4343 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4344 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004345
Lukas Czernerb8a86842014-03-18 18:05:35 -04004346 /*
4347 * If the extent is initialized check whether the
4348 * caller wants to convert it to unwritten.
4349 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004350 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004351 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004352 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004353 handle, inode, map, &path,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05004354 allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004355 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004356 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004357 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004358
Lukas Czerner556615d2014-04-20 23:45:47 -04004359 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004360 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004361 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004362 if (ret < 0)
4363 err = ret;
4364 else
4365 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004366 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004367 }
4368 }
4369
4370 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004371 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004372 * we couldn't try to create block if create flag is zero
4373 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004374 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004375 ext4_lblk_t hole_start, hole_len;
4376
Jan Karafacab4d2016-03-09 22:54:00 -05004377 hole_start = map->m_lblk;
4378 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004379 /*
4380 * put just found gap into cache to speed up
4381 * subsequent requests
4382 */
Jan Kara140a5252016-03-09 22:46:57 -05004383 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004384
4385 /* Update hole_len to reflect hole size after map->m_lblk */
4386 if (hole_start != map->m_lblk)
4387 hole_len -= map->m_lblk - hole_start;
4388 map->m_pblk = 0;
4389 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4390
Alex Tomasa86c6182006-10-11 01:21:03 -07004391 goto out2;
4392 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004393
Alex Tomasa86c6182006-10-11 01:21:03 -07004394 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004395 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004396 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004397 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004398 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004399
4400 /*
4401 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004402 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004403 */
4404 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004405 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004406 ar.len = allocated = map->m_len;
4407 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004408 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004409 goto got_allocated_blocks;
4410 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004411
Alex Tomasc9de5602008-01-29 00:19:52 -05004412 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004413 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004414 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4415 if (err)
4416 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004417 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004418 ex2 = NULL;
4419 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004420 if (err)
4421 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004422
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004423 /* Check if the extent after searching to the right implies a
4424 * cluster we can use. */
4425 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004426 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004427 ar.len = allocated = map->m_len;
4428 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004429 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004430 goto got_allocated_blocks;
4431 }
4432
Amit Arora749269f2007-07-18 09:02:56 -04004433 /*
4434 * See if request is beyond maximum number of blocks we can have in
4435 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004436 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4437 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004438 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004439 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004440 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004441 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004442 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4443 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4444 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004445
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004446 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004447 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004448 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004449 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004450 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004451 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004452 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004453
4454 /* allocate new block */
4455 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004456 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4457 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004458 /*
4459 * We calculate the offset from the beginning of the cluster
4460 * for the logical block number, since when we allocate a
4461 * physical cluster, the physical block should start at the
4462 * same offset from the beginning of the cluster. This is
4463 * needed so that future calls to get_implied_cluster_alloc()
4464 * work correctly.
4465 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004466 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004467 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4468 ar.goal -= offset;
4469 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004470 if (S_ISREG(inode->i_mode))
4471 ar.flags = EXT4_MB_HINT_DATA;
4472 else
4473 /* disable in-core preallocation for non-regular files */
4474 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004475 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4476 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004477 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4478 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004479 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4480 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004481 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004482 if (!newblock)
4483 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004484 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004485 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004486 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004487 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004488 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4489 if (ar.len > allocated)
4490 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004491
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004492got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004493 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004494 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004495 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004496 /* Mark unwritten */
4497 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4498 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004499 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004500 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004501
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004502 err = 0;
4503 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4504 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4505 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004506 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004507 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004508 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004509
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004510 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004511 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4512 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004513 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004514 /* not a good idea to call discard here directly,
4515 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004516 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004517 ext4_free_blocks(handle, inode, NULL, newblock,
4518 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004519 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004520 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004521
Alex Tomasa86c6182006-10-11 01:21:03 -07004522 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004523 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004524 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004525 if (allocated > map->m_len)
4526 allocated = map->m_len;
4527 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004528
Jan Karab436b9b2009-12-08 23:51:10 -05004529 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004530 * Reduce the reserved cluster count to reflect successful deferred
4531 * allocation of delayed allocated clusters or direct allocation of
4532 * clusters discovered to be delayed allocated. Once allocated, a
4533 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004534 */
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004535 if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) {
4536 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004537 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004538 * When allocating delayed allocated clusters, simply
4539 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004540 */
4541 ext4_da_update_reserve_space(inode, allocated_clusters,
4542 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004543 } else {
4544 ext4_lblk_t lblk, len;
4545 unsigned int n;
4546
4547 /*
4548 * When allocating non-delayed allocated clusters
4549 * (from fallocate, filemap, DIO, or clusters
4550 * allocated when delalloc has been disabled by
4551 * ext4_nonda_switch), reduce the reserved cluster
4552 * count by the number of allocated clusters that
4553 * have previously been delayed allocated. Quota
4554 * has been claimed by ext4_mb_new_blocks() above,
4555 * so release the quota reservations made for any
4556 * previously delayed allocated clusters.
4557 */
4558 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4559 len = allocated_clusters << sbi->s_cluster_bits;
4560 n = ext4_es_delayed_clu(inode, lblk, len);
4561 if (n > 0)
4562 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004563 }
4564 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004565
4566 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004567 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004568 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004569 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004570 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004571 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004572 else
Jan Karab436b9b2009-12-08 23:51:10 -05004573 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004574out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004575 if (allocated > map->m_len)
4576 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004577 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004578 map->m_flags |= EXT4_MAP_MAPPED;
4579 map->m_pblk = newblock;
4580 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004581out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004582 ext4_ext_drop_refs(path);
4583 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004584
Theodore Ts'o63b99962013-07-16 10:28:47 -04004585 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4586 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004587 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004588}
4589
Theodore Ts'od0abb362016-11-13 22:02:28 -05004590int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004591{
Alex Tomasa86c6182006-10-11 01:21:03 -07004592 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004593 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004594 int err = 0;
4595
4596 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004597 * TODO: optimization is possible here.
4598 * Probably we need not scan at all,
4599 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004600 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004601
4602 /* we have to know where to truncate from in crash case */
4603 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004604 err = ext4_mark_inode_dirty(handle, inode);
4605 if (err)
4606 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004607
4608 last_block = (inode->i_size + sb->s_blocksize - 1)
4609 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004610retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004611 err = ext4_es_remove_extent(inode, last_block,
4612 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004613 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004614 cond_resched();
4615 congestion_wait(BLK_RW_ASYNC, HZ/50);
4616 goto retry;
4617 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004618 if (err)
4619 return err;
4620 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004621}
4622
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004623static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004624 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004625 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004626{
Al Viro496ad9a2013-01-23 17:07:38 -05004627 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004628 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004629 int ret = 0;
4630 int ret2 = 0;
4631 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004632 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004633 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004634 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004635 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004636
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004637 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004638 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004639 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004640 /*
4641 * Don't normalize the request if it can fit in one extent so
4642 * that it doesn't get unnecessarily split into multiple
4643 * extents.
4644 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004645 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004646 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004647
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004648 /*
4649 * credits to insert 1 extent into extent tree
4650 */
4651 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004652 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004653
Amit Aroraa2df2a62007-07-17 21:42:41 -04004654retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004655 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004656 /*
4657 * Recalculate credits when extent tree depth changes.
4658 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004659 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004660 credits = ext4_chunk_trans_blocks(inode, len);
4661 depth = ext_depth(inode);
4662 }
4663
Theodore Ts'o9924a922013-02-08 21:59:22 -05004664 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4665 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004666 if (IS_ERR(handle)) {
4667 ret = PTR_ERR(handle);
4668 break;
4669 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004670 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004671 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004672 ext4_debug("inode #%lu: block %u: len %u: "
4673 "ext4_ext_map_blocks returned %d",
4674 inode->i_ino, map.m_lblk,
4675 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004676 ext4_mark_inode_dirty(handle, inode);
4677 ret2 = ext4_journal_stop(handle);
4678 break;
4679 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004680 map.m_lblk += ret;
4681 map.m_len = len = len - ret;
4682 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004683 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004684 if (new_size) {
4685 if (epos > new_size)
4686 epos = new_size;
4687 if (ext4_update_inode_size(inode, epos) & 0x1)
4688 inode->i_mtime = inode->i_ctime;
4689 } else {
4690 if (epos > inode->i_size)
4691 ext4_set_inode_flag(inode,
4692 EXT4_INODE_EOFBLOCKS);
4693 }
4694 ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004695 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004696 ret2 = ext4_journal_stop(handle);
4697 if (ret2)
4698 break;
4699 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004700 if (ret == -ENOSPC &&
4701 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4702 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004703 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004704 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004705
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004706 return ret > 0 ? ret2 : ret;
4707}
4708
Lukas Czernerb8a86842014-03-18 18:05:35 -04004709static long ext4_zero_range(struct file *file, loff_t offset,
4710 loff_t len, int mode)
4711{
4712 struct inode *inode = file_inode(file);
4713 handle_t *handle = NULL;
4714 unsigned int max_blocks;
4715 loff_t new_size = 0;
4716 int ret = 0;
4717 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004718 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004719 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004720 loff_t start, end;
4721 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004722 unsigned int blkbits = inode->i_blkbits;
4723
4724 trace_ext4_zero_range(inode, offset, len, mode);
4725
jon ernst6c5e73d2014-04-18 11:50:35 -04004726 if (!S_ISREG(inode->i_mode))
4727 return -EINVAL;
4728
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004729 /* Call ext4_force_commit to flush all data in case of data=journal. */
4730 if (ext4_should_journal_data(inode)) {
4731 ret = ext4_force_commit(inode->i_sb);
4732 if (ret)
4733 return ret;
4734 }
4735
Lukas Czernerb8a86842014-03-18 18:05:35 -04004736 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004737 * Round up offset. This is not fallocate, we neet to zero out
4738 * blocks, so convert interior block aligned part of the range to
4739 * unwritten and possibly manually zero out unaligned parts of the
4740 * range.
4741 */
4742 start = round_up(offset, 1 << blkbits);
4743 end = round_down((offset + len), 1 << blkbits);
4744
4745 if (start < offset || end > offset + len)
4746 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004747 partial_begin = offset & ((1 << blkbits) - 1);
4748 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004749
4750 lblk = start >> blkbits;
4751 max_blocks = (end >> blkbits);
4752 if (max_blocks < lblk)
4753 max_blocks = 0;
4754 else
4755 max_blocks -= lblk;
4756
Al Viro59551022016-01-22 15:40:57 -05004757 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004758
4759 /*
4760 * Indirect files do not support unwritten extnets
4761 */
4762 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4763 ret = -EOPNOTSUPP;
4764 goto out_mutex;
4765 }
4766
4767 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004768 (offset + len > i_size_read(inode) ||
4769 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004770 new_size = offset + len;
4771 ret = inode_newsize_ok(inode, new_size);
4772 if (ret)
4773 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004774 }
4775
Lukas Czerner0f2af212015-04-03 00:09:13 -04004776 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4777 if (mode & FALLOC_FL_KEEP_SIZE)
4778 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4779
Jan Kara17048e82015-12-07 14:29:17 -05004780 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004781 inode_dio_wait(inode);
4782
Lukas Czerner0f2af212015-04-03 00:09:13 -04004783 /* Preallocate the range including the unaligned edges */
4784 if (partial_begin || partial_end) {
4785 ret = ext4_alloc_file_blocks(file,
4786 round_down(offset, 1 << blkbits) >> blkbits,
4787 (round_up((offset + len), 1 << blkbits) -
4788 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004789 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004790 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004791 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004792
4793 }
4794
4795 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004796 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004797 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4798 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004799
Jan Karaea3d7202015-12-07 14:28:03 -05004800 /*
4801 * Prevent page faults from reinstantiating pages we have
4802 * released from page cache.
4803 */
4804 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004805
4806 ret = ext4_break_layouts(inode);
4807 if (ret) {
4808 up_write(&EXT4_I(inode)->i_mmap_sem);
4809 goto out_mutex;
4810 }
4811
Jan Kara01127842015-12-07 14:34:49 -05004812 ret = ext4_update_disksize_before_punch(inode, offset, len);
4813 if (ret) {
4814 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004815 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004816 }
Jan Karaea3d7202015-12-07 14:28:03 -05004817 /* Now release the pages and zero block aligned part of pages */
4818 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004819 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004820
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004821 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004822 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004823 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004824 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004825 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004826 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004827 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004828 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004829
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004830 /*
4831 * In worst case we have to writeout two nonadjacent unwritten
4832 * blocks and update the inode
4833 */
4834 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4835 if (ext4_should_journal_data(inode))
4836 credits += 2;
4837 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004838 if (IS_ERR(handle)) {
4839 ret = PTR_ERR(handle);
4840 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004841 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004842 }
4843
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004844 inode->i_mtime = inode->i_ctime = current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004845 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004846 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004847 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004848 /*
4849 * Mark that we allocate beyond EOF so the subsequent truncate
4850 * can proceed even if the new size is the same as i_size.
4851 */
4852 if ((offset + len) > i_size_read(inode))
4853 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4854 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004855 ext4_mark_inode_dirty(handle, inode);
4856
4857 /* Zero out partial block at the edges of the range */
4858 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004859 if (ret >= 0)
4860 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004861
4862 if (file->f_flags & O_SYNC)
4863 ext4_handle_sync(handle);
4864
4865 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004866out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004867 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004868 return ret;
4869}
4870
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004871/*
4872 * preallocate space for a file. This implements ext4's fallocate file
4873 * operation, which gets called from sys_fallocate system call.
4874 * For block-mapped files, posix_fallocate should fall back to the method
4875 * of writing zeroes to the required new blocks (the same behavior which is
4876 * expected for file systems which do not support fallocate() system call).
4877 */
4878long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4879{
4880 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004881 loff_t new_size = 0;
4882 unsigned int max_blocks;
4883 int ret = 0;
4884 int flags;
4885 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004886 unsigned int blkbits = inode->i_blkbits;
4887
Michael Halcrow2058f832015-04-12 00:55:10 -04004888 /*
4889 * Encrypted inodes can't handle collapse range or insert
4890 * range since we would need to re-encrypt blocks with a
4891 * different IV or XTS tweak (which are based on the logical
4892 * block number).
4893 *
4894 * XXX It's not clear why zero range isn't working, but we'll
4895 * leave it disabled for encrypted inodes for now. This is a
4896 * bug we should fix....
4897 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304898 if (IS_ENCRYPTED(inode) &&
Namjae Jeon331573f2015-06-09 01:55:03 -04004899 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4900 FALLOC_FL_ZERO_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004901 return -EOPNOTSUPP;
4902
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004903 /* Return error if mode is not supported */
4904 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004905 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4906 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004907 return -EOPNOTSUPP;
4908
4909 if (mode & FALLOC_FL_PUNCH_HOLE)
4910 return ext4_punch_hole(inode, offset, len);
4911
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004912 ret = ext4_convert_inline_data(inode);
4913 if (ret)
4914 return ret;
4915
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004916 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4917 return ext4_collapse_range(inode, offset, len);
4918
Namjae Jeon331573f2015-06-09 01:55:03 -04004919 if (mode & FALLOC_FL_INSERT_RANGE)
4920 return ext4_insert_range(inode, offset, len);
4921
Lukas Czernerb8a86842014-03-18 18:05:35 -04004922 if (mode & FALLOC_FL_ZERO_RANGE)
4923 return ext4_zero_range(file, offset, len, mode);
4924
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004925 trace_ext4_fallocate_enter(inode, offset, len, mode);
4926 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004927
Fabian Frederick518eaa62016-09-15 11:55:01 -04004928 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004929 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004930 if (mode & FALLOC_FL_KEEP_SIZE)
4931 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4932
Al Viro59551022016-01-22 15:40:57 -05004933 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004934
Davide Italiano280227a2015-05-02 23:21:15 -04004935 /*
4936 * We only support preallocation for extent-based files only
4937 */
4938 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4939 ret = -EOPNOTSUPP;
4940 goto out;
4941 }
4942
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004943 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004944 (offset + len > i_size_read(inode) ||
4945 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004946 new_size = offset + len;
4947 ret = inode_newsize_ok(inode, new_size);
4948 if (ret)
4949 goto out;
4950 }
4951
Jan Kara17048e82015-12-07 14:29:17 -05004952 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004953 inode_dio_wait(inode);
4954
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004955 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004956 if (ret)
4957 goto out;
4958
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004959 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4960 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4961 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004962 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004963out:
Al Viro59551022016-01-22 15:40:57 -05004964 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004965 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4966 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004967}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004968
4969/*
Mingming Cao00314622009-09-28 15:49:08 -04004970 * This function convert a range of blocks to written extents
4971 * The caller of this function will pass the start offset and the size.
4972 * all unwritten extents within this range will be converted to
4973 * written extents.
4974 *
4975 * This function is called from the direct IO end io call back
4976 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004977 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004978 */
Jan Kara6b523df2013-06-04 13:21:11 -04004979int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4980 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004981{
Mingming Cao00314622009-09-28 15:49:08 -04004982 unsigned int max_blocks;
4983 int ret = 0;
4984 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004985 struct ext4_map_blocks map;
Ritesh Harjania00713e2019-10-16 13:07:08 +05304986 unsigned int blkbits = inode->i_blkbits;
4987 unsigned int credits = 0;
Mingming Cao00314622009-09-28 15:49:08 -04004988
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004989 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004990 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4991
Ritesh Harjania00713e2019-10-16 13:07:08 +05304992 if (!handle) {
Jan Kara6b523df2013-06-04 13:21:11 -04004993 /*
4994 * credits to insert 1 extent into extent tree
4995 */
4996 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4997 }
Mingming Cao00314622009-09-28 15:49:08 -04004998 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004999 map.m_lblk += ret;
5000 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005001 if (credits) {
5002 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5003 credits);
5004 if (IS_ERR(handle)) {
5005 ret = PTR_ERR(handle);
5006 break;
5007 }
Mingming Cao00314622009-09-28 15:49:08 -04005008 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005009 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005010 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005011 if (ret <= 0)
5012 ext4_warning(inode->i_sb,
5013 "inode #%lu: block %u: len %u: "
5014 "ext4_ext_map_blocks returned %d",
5015 inode->i_ino, map.m_lblk,
5016 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005017 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005018 if (credits)
5019 ret2 = ext4_journal_stop(handle);
5020 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005021 break;
5022 }
5023 return ret > 0 ? ret2 : ret;
5024}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005025
Ritesh Harjania00713e2019-10-16 13:07:08 +05305026int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
5027{
5028 int ret, err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05305029 struct ext4_io_end_vec *io_end_vec;
Ritesh Harjania00713e2019-10-16 13:07:08 +05305030
5031 /*
5032 * This is somewhat ugly but the idea is clear: When transaction is
5033 * reserved, everything goes into it. Otherwise we rather start several
5034 * smaller transactions for conversion of each extent separately.
5035 */
5036 if (handle) {
5037 handle = ext4_journal_start_reserved(handle,
5038 EXT4_HT_EXT_CONVERT);
5039 if (IS_ERR(handle))
5040 return PTR_ERR(handle);
5041 }
5042
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05305043 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
5044 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
5045 io_end_vec->offset,
5046 io_end_vec->size);
5047 if (ret)
5048 break;
5049 }
5050
Ritesh Harjania00713e2019-10-16 13:07:08 +05305051 if (handle)
5052 err = ext4_journal_stop(handle);
5053
5054 return ret < 0 ? ret : err;
5055}
5056
Mingming Cao00314622009-09-28 15:49:08 -04005057/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005058 * If newes is not existing extent (newes->ec_pblk equals zero) find
5059 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005060 * return start of the next delayed extent.
5061 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005062 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005063 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005064 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005065 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005066static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005067 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005068{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005069 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005070 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005071
Zheng Liu69eb33d2013-02-18 00:31:07 -05005072 if (newes->es_pblk == 0) {
Eric Whitneyad431022018-10-01 14:10:39 -04005073 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
5074 newes->es_lblk,
5075 newes->es_lblk + newes->es_len - 1,
5076 &es);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005077
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005078 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005079 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005080 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005081 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005082 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005083 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005084 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005085
Zheng Liu69eb33d2013-02-18 00:31:07 -05005086 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005087 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005088 newes->es_len = min(es.es_lblk - newes->es_lblk,
5089 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005090 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005091 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005092
Zheng Liu69eb33d2013-02-18 00:31:07 -05005093 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005094 }
5095
Zheng Liu69eb33d2013-02-18 00:31:07 -05005096 block = newes->es_lblk + newes->es_len;
Eric Whitneyad431022018-10-01 14:10:39 -04005097 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block,
5098 EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005099 if (es.es_len == 0)
5100 next_del = EXT_MAX_BLOCKS;
5101 else
5102 next_del = es.es_lblk;
5103
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005104 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005105}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005106
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005107static int ext4_xattr_fiemap(struct inode *inode,
5108 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005109{
5110 __u64 physical = 0;
5111 __u64 length;
5112 __u32 flags = FIEMAP_EXTENT_LAST;
5113 int blockbits = inode->i_sb->s_blocksize_bits;
5114 int error = 0;
5115
5116 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005117 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005118 struct ext4_iloc iloc;
5119 int offset; /* offset of xattr in inode */
5120
5121 error = ext4_get_inode_loc(inode, &iloc);
5122 if (error)
5123 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005124 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005125 offset = EXT4_GOOD_OLD_INODE_SIZE +
5126 EXT4_I(inode)->i_extra_isize;
5127 physical += offset;
5128 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5129 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005130 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005131 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005132 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005133 length = inode->i_sb->s_blocksize;
5134 }
5135
5136 if (physical)
5137 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5138 length, flags);
5139 return (error < 0 ? error : 0);
5140}
5141
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005142static int _ext4_fiemap(struct inode *inode,
5143 struct fiemap_extent_info *fieinfo,
5144 __u64 start, __u64 len,
5145 int (*fill)(struct inode *, ext4_lblk_t,
5146 ext4_lblk_t,
5147 struct fiemap_extent_info *))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005148{
5149 ext4_lblk_t start_blk;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005150 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR;
5151
Eric Sandeen6873fa02008-10-07 00:46:36 -04005152 int error = 0;
5153
Tao Ma94191982012-12-10 14:06:02 -05005154 if (ext4_has_inline_data(inode)) {
5155 int has_inline = 1;
5156
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005157 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5158 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005159
5160 if (has_inline)
5161 return error;
5162 }
5163
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005164 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5165 error = ext4_ext_precache(inode);
5166 if (error)
5167 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005168 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005169 }
5170
Eric Sandeen6873fa02008-10-07 00:46:36 -04005171 /* fallback to generic here if not in extents fmt */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005172 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
5173 fill == ext4_fill_fiemap_extents)
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005174 return generic_block_fiemap(inode, fieinfo, start, len,
5175 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005176
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005177 if (fill == ext4_fill_es_cache_info)
5178 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
5179 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005180 return -EBADR;
5181
5182 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5183 error = ext4_xattr_fiemap(inode, fieinfo);
5184 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005185 ext4_lblk_t len_blks;
5186 __u64 last_blk;
5187
Eric Sandeen6873fa02008-10-07 00:46:36 -04005188 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005189 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005190 if (last_blk >= EXT_MAX_BLOCKS)
5191 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005192 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005193
5194 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005195 * Walk the extent tree gathering extent information
5196 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005197 */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005198 error = fill(inode, start_blk, len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005199 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005200 return error;
5201}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005202
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005203int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5204 __u64 start, __u64 len)
5205{
5206 return _ext4_fiemap(inode, fieinfo, start, len,
5207 ext4_fill_fiemap_extents);
5208}
5209
5210int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
5211 __u64 start, __u64 len)
5212{
5213 if (ext4_has_inline_data(inode)) {
5214 int has_inline;
5215
5216 down_read(&EXT4_I(inode)->xattr_sem);
5217 has_inline = ext4_has_inline_data(inode);
5218 up_read(&EXT4_I(inode)->xattr_sem);
5219 if (has_inline)
5220 return 0;
5221 }
5222
5223 return _ext4_fiemap(inode, fieinfo, start, len,
5224 ext4_fill_es_cache_info);
5225}
5226
5227
Namjae Jeon9eb79482014-02-23 15:18:59 -05005228/*
5229 * ext4_access_path:
5230 * Function to access the path buffer for marking it dirty.
5231 * It also checks if there are sufficient credits left in the journal handle
5232 * to update path.
5233 */
5234static int
5235ext4_access_path(handle_t *handle, struct inode *inode,
5236 struct ext4_ext_path *path)
5237{
5238 int credits, err;
5239
5240 if (!ext4_handle_valid(handle))
5241 return 0;
5242
5243 /*
5244 * Check if need to extend journal credits
5245 * 3 for leaf, sb, and inode plus 2 (bmap and group
5246 * descriptor) for each block group; assume two block
5247 * groups
5248 */
Jan Karaa4130362019-11-05 17:44:16 +01005249 credits = ext4_writepage_trans_blocks(inode);
Jan Kara83448bd2019-11-05 17:44:29 +01005250 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
Jan Karaa4130362019-11-05 17:44:16 +01005251 if (err < 0)
5252 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005253
5254 err = ext4_ext_get_access(handle, inode, path);
5255 return err;
5256}
5257
5258/*
5259 * ext4_ext_shift_path_extents:
5260 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005261 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5262 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005263 */
5264static int
5265ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5266 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005267 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005268{
5269 int depth, err = 0;
5270 struct ext4_extent *ex_start, *ex_last;
5271 bool update = 0;
5272 depth = path->p_depth;
5273
5274 while (depth >= 0) {
5275 if (depth == path->p_depth) {
5276 ex_start = path[depth].p_ext;
5277 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005278 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005279
5280 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005281
5282 err = ext4_access_path(handle, inode, path + depth);
5283 if (err)
5284 goto out;
5285
5286 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5287 update = 1;
5288
Namjae Jeon9eb79482014-02-23 15:18:59 -05005289 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005290 if (SHIFT == SHIFT_LEFT) {
5291 le32_add_cpu(&ex_start->ee_block,
5292 -shift);
5293 /* Try to merge to the left. */
5294 if ((ex_start >
5295 EXT_FIRST_EXTENT(path[depth].p_hdr))
5296 &&
5297 ext4_ext_try_to_merge_right(inode,
5298 path, ex_start - 1))
5299 ex_last--;
5300 else
5301 ex_start++;
5302 } else {
5303 le32_add_cpu(&ex_last->ee_block, shift);
5304 ext4_ext_try_to_merge_right(inode, path,
5305 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005306 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005307 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005308 }
5309 err = ext4_ext_dirty(handle, inode, path + depth);
5310 if (err)
5311 goto out;
5312
5313 if (--depth < 0 || !update)
5314 break;
5315 }
5316
5317 /* Update index too */
5318 err = ext4_access_path(handle, inode, path + depth);
5319 if (err)
5320 goto out;
5321
Namjae Jeon331573f2015-06-09 01:55:03 -04005322 if (SHIFT == SHIFT_LEFT)
5323 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5324 else
5325 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005326 err = ext4_ext_dirty(handle, inode, path + depth);
5327 if (err)
5328 goto out;
5329
5330 /* we are done if current index is not a starting index */
5331 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5332 break;
5333
5334 depth--;
5335 }
5336
5337out:
5338 return err;
5339}
5340
5341/*
5342 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005343 * All the extents which lies in the range from @start to the last allocated
5344 * block for the @inode are shifted either towards left or right (depending
5345 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005346 * On success, 0 is returned, error otherwise.
5347 */
5348static int
5349ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005350 ext4_lblk_t start, ext4_lblk_t shift,
5351 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005352{
5353 struct ext4_ext_path *path;
5354 int ret = 0, depth;
5355 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005356 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005357
5358 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005359 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5360 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005361 if (IS_ERR(path))
5362 return PTR_ERR(path);
5363
5364 depth = path->p_depth;
5365 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005366 if (!extent)
5367 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005368
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005369 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005370
Namjae Jeon331573f2015-06-09 01:55:03 -04005371 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005372 * For left shifts, make sure the hole on the left is big enough to
5373 * accommodate the shift. For right shifts, make sure the last extent
5374 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005375 */
5376 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005377 path = ext4_find_extent(inode, start - 1, &path,
5378 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005379 if (IS_ERR(path))
5380 return PTR_ERR(path);
5381 depth = path->p_depth;
5382 extent = path[depth].p_ext;
5383 if (extent) {
5384 ex_start = le32_to_cpu(extent->ee_block);
5385 ex_end = le32_to_cpu(extent->ee_block) +
5386 ext4_ext_get_actual_len(extent);
5387 } else {
5388 ex_start = 0;
5389 ex_end = 0;
5390 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005391
Namjae Jeon331573f2015-06-09 01:55:03 -04005392 if ((start == ex_start && shift > ex_start) ||
5393 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005394 ret = -EINVAL;
5395 goto out;
5396 }
5397 } else {
5398 if (shift > EXT_MAX_BLOCKS -
5399 (stop + ext4_ext_get_actual_len(extent))) {
5400 ret = -EINVAL;
5401 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005402 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005403 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005404
Namjae Jeon331573f2015-06-09 01:55:03 -04005405 /*
5406 * In case of left shift, iterator points to start and it is increased
5407 * till we reach stop. In case of right shift, iterator points to stop
5408 * and it is decreased till we reach start.
5409 */
5410 if (SHIFT == SHIFT_LEFT)
5411 iterator = &start;
5412 else
5413 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005414
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005415 /*
5416 * Its safe to start updating extents. Start and stop are unsigned, so
5417 * in case of right shift if extent with 0 block is reached, iterator
5418 * becomes NULL to indicate the end of the loop.
5419 */
5420 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005421 path = ext4_find_extent(inode, *iterator, &path,
5422 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005423 if (IS_ERR(path))
5424 return PTR_ERR(path);
5425 depth = path->p_depth;
5426 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005427 if (!extent) {
5428 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005429 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005430 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005431 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005432 if (SHIFT == SHIFT_LEFT && *iterator >
5433 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005434 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005435 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5436 path[depth].p_ext++;
5437 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005438 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005439 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005440 }
5441 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005442
5443 if (SHIFT == SHIFT_LEFT) {
5444 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5445 *iterator = le32_to_cpu(extent->ee_block) +
5446 ext4_ext_get_actual_len(extent);
5447 } else {
5448 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005449 if (le32_to_cpu(extent->ee_block) > 0)
5450 *iterator = le32_to_cpu(extent->ee_block) - 1;
5451 else
5452 /* Beginning is reached, end of the loop */
5453 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005454 /* Update path extent in case we need to stop */
5455 while (le32_to_cpu(extent->ee_block) < start)
5456 extent++;
5457 path[depth].p_ext = extent;
5458 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005459 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005460 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005461 if (ret)
5462 break;
5463 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005464out:
5465 ext4_ext_drop_refs(path);
5466 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005467 return ret;
5468}
5469
5470/*
5471 * ext4_collapse_range:
5472 * This implements the fallocate's collapse range functionality for ext4
5473 * Returns: 0 and non-zero on error.
5474 */
5475int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5476{
5477 struct super_block *sb = inode->i_sb;
5478 ext4_lblk_t punch_start, punch_stop;
5479 handle_t *handle;
5480 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005481 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005482 int ret;
5483
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005484 /*
5485 * We need to test this early because xfstests assumes that a
5486 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5487 * system does not support collapse range.
5488 */
5489 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5490 return -EOPNOTSUPP;
5491
Namjae Jeon9eb79482014-02-23 15:18:59 -05005492 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005493 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5494 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005495 return -EINVAL;
5496
5497 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005498 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005499
5500 trace_ext4_collapse_range(inode, offset, len);
5501
5502 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5503 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5504
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005505 /* Call ext4_force_commit to flush all data in case of data=journal. */
5506 if (ext4_should_journal_data(inode)) {
5507 ret = ext4_force_commit(inode->i_sb);
5508 if (ret)
5509 return ret;
5510 }
5511
Al Viro59551022016-01-22 15:40:57 -05005512 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005513 /*
5514 * There is no need to overlap collapse range with EOF, in which case
5515 * it is effectively a truncate operation
5516 */
5517 if (offset + len >= i_size_read(inode)) {
5518 ret = -EINVAL;
5519 goto out_mutex;
5520 }
5521
Namjae Jeon9eb79482014-02-23 15:18:59 -05005522 /* Currently just for extent based files */
5523 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5524 ret = -EOPNOTSUPP;
5525 goto out_mutex;
5526 }
5527
Namjae Jeon9eb79482014-02-23 15:18:59 -05005528 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005529 inode_dio_wait(inode);
5530
Jan Karaea3d7202015-12-07 14:28:03 -05005531 /*
5532 * Prevent page faults from reinstantiating pages we have released from
5533 * page cache.
5534 */
5535 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005536
5537 ret = ext4_break_layouts(inode);
5538 if (ret)
5539 goto out_mmap;
5540
Jan Kara32ebffd2015-12-07 14:31:11 -05005541 /*
5542 * Need to round down offset to be aligned with page size boundary
5543 * for page size > block size.
5544 */
5545 ioffset = round_down(offset, PAGE_SIZE);
5546 /*
5547 * Write tail of the last page before removed range since it will get
5548 * removed from the page cache below.
5549 */
5550 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5551 if (ret)
5552 goto out_mmap;
5553 /*
5554 * Write data that will be shifted to preserve them when discarding
5555 * page cache below. We are also protected from pages becoming dirty
5556 * by i_mmap_sem.
5557 */
5558 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5559 LLONG_MAX);
5560 if (ret)
5561 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005562 truncate_pagecache(inode, ioffset);
5563
Namjae Jeon9eb79482014-02-23 15:18:59 -05005564 credits = ext4_writepage_trans_blocks(inode);
5565 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5566 if (IS_ERR(handle)) {
5567 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005568 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005569 }
5570
5571 down_write(&EXT4_I(inode)->i_data_sem);
5572 ext4_discard_preallocations(inode);
5573
5574 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005575 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005576 if (ret) {
5577 up_write(&EXT4_I(inode)->i_data_sem);
5578 goto out_stop;
5579 }
5580
5581 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5582 if (ret) {
5583 up_write(&EXT4_I(inode)->i_data_sem);
5584 goto out_stop;
5585 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005586 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005587
5588 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005589 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005590 if (ret) {
5591 up_write(&EXT4_I(inode)->i_data_sem);
5592 goto out_stop;
5593 }
5594
5595 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005596 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005597 EXT4_I(inode)->i_disksize = new_size;
5598
Namjae Jeon9eb79482014-02-23 15:18:59 -05005599 up_write(&EXT4_I(inode)->i_data_sem);
5600 if (IS_SYNC(inode))
5601 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005602 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005603 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005604 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005605
5606out_stop:
5607 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005608out_mmap:
5609 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005610out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005611 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005612 return ret;
5613}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005614
Namjae Jeon331573f2015-06-09 01:55:03 -04005615/*
5616 * ext4_insert_range:
5617 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5618 * The data blocks starting from @offset to the EOF are shifted by @len
5619 * towards right to create a hole in the @inode. Inode size is increased
5620 * by len bytes.
5621 * Returns 0 on success, error otherwise.
5622 */
5623int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5624{
5625 struct super_block *sb = inode->i_sb;
5626 handle_t *handle;
5627 struct ext4_ext_path *path;
5628 struct ext4_extent *extent;
5629 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5630 unsigned int credits, ee_len;
5631 int ret = 0, depth, split_flag = 0;
5632 loff_t ioffset;
5633
5634 /*
5635 * We need to test this early because xfstests assumes that an
5636 * insert range of (0, 1) will return EOPNOTSUPP if the file
5637 * system does not support insert range.
5638 */
5639 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5640 return -EOPNOTSUPP;
5641
5642 /* Insert range works only on fs block size aligned offsets. */
5643 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5644 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5645 return -EINVAL;
5646
5647 if (!S_ISREG(inode->i_mode))
5648 return -EOPNOTSUPP;
5649
5650 trace_ext4_insert_range(inode, offset, len);
5651
5652 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5653 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5654
5655 /* Call ext4_force_commit to flush all data in case of data=journal */
5656 if (ext4_should_journal_data(inode)) {
5657 ret = ext4_force_commit(inode->i_sb);
5658 if (ret)
5659 return ret;
5660 }
5661
Al Viro59551022016-01-22 15:40:57 -05005662 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005663 /* Currently just for extent based files */
5664 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5665 ret = -EOPNOTSUPP;
5666 goto out_mutex;
5667 }
5668
5669 /* Check for wrap through zero */
5670 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5671 ret = -EFBIG;
5672 goto out_mutex;
5673 }
5674
5675 /* Offset should be less than i_size */
5676 if (offset >= i_size_read(inode)) {
5677 ret = -EINVAL;
5678 goto out_mutex;
5679 }
5680
Namjae Jeon331573f2015-06-09 01:55:03 -04005681 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005682 inode_dio_wait(inode);
5683
Jan Karaea3d7202015-12-07 14:28:03 -05005684 /*
5685 * Prevent page faults from reinstantiating pages we have released from
5686 * page cache.
5687 */
5688 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005689
5690 ret = ext4_break_layouts(inode);
5691 if (ret)
5692 goto out_mmap;
5693
Jan Kara32ebffd2015-12-07 14:31:11 -05005694 /*
5695 * Need to round down to align start offset to page size boundary
5696 * for page size > block size.
5697 */
5698 ioffset = round_down(offset, PAGE_SIZE);
5699 /* Write out all dirty pages */
5700 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5701 LLONG_MAX);
5702 if (ret)
5703 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005704 truncate_pagecache(inode, ioffset);
5705
Namjae Jeon331573f2015-06-09 01:55:03 -04005706 credits = ext4_writepage_trans_blocks(inode);
5707 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5708 if (IS_ERR(handle)) {
5709 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005710 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005711 }
5712
5713 /* Expand file to avoid data loss if there is error while shifting */
5714 inode->i_size += len;
5715 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005716 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005717 ret = ext4_mark_inode_dirty(handle, inode);
5718 if (ret)
5719 goto out_stop;
5720
5721 down_write(&EXT4_I(inode)->i_data_sem);
5722 ext4_discard_preallocations(inode);
5723
5724 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5725 if (IS_ERR(path)) {
5726 up_write(&EXT4_I(inode)->i_data_sem);
5727 goto out_stop;
5728 }
5729
5730 depth = ext_depth(inode);
5731 extent = path[depth].p_ext;
5732 if (extent) {
5733 ee_start_lblk = le32_to_cpu(extent->ee_block);
5734 ee_len = ext4_ext_get_actual_len(extent);
5735
5736 /*
5737 * If offset_lblk is not the starting block of extent, split
5738 * the extent @offset_lblk
5739 */
5740 if ((offset_lblk > ee_start_lblk) &&
5741 (offset_lblk < (ee_start_lblk + ee_len))) {
5742 if (ext4_ext_is_unwritten(extent))
5743 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5744 EXT4_EXT_MARK_UNWRIT2;
5745 ret = ext4_split_extent_at(handle, inode, &path,
5746 offset_lblk, split_flag,
5747 EXT4_EX_NOCACHE |
5748 EXT4_GET_BLOCKS_PRE_IO |
5749 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5750 }
5751
5752 ext4_ext_drop_refs(path);
5753 kfree(path);
5754 if (ret < 0) {
5755 up_write(&EXT4_I(inode)->i_data_sem);
5756 goto out_stop;
5757 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005758 } else {
5759 ext4_ext_drop_refs(path);
5760 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005761 }
5762
5763 ret = ext4_es_remove_extent(inode, offset_lblk,
5764 EXT_MAX_BLOCKS - offset_lblk);
5765 if (ret) {
5766 up_write(&EXT4_I(inode)->i_data_sem);
5767 goto out_stop;
5768 }
5769
5770 /*
5771 * if offset_lblk lies in a hole which is at start of file, use
5772 * ee_start_lblk to shift extents
5773 */
5774 ret = ext4_ext_shift_extents(inode, handle,
5775 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5776 len_lblk, SHIFT_RIGHT);
5777
5778 up_write(&EXT4_I(inode)->i_data_sem);
5779 if (IS_SYNC(inode))
5780 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005781 if (ret >= 0)
5782 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005783
5784out_stop:
5785 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005786out_mmap:
5787 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005788out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005789 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005790 return ret;
5791}
5792
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005793/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005794 * ext4_swap_extents() - Swap extents between two inodes
5795 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005796 * @inode1: First inode
5797 * @inode2: Second inode
5798 * @lblk1: Start block for first inode
5799 * @lblk2: Start block for second inode
5800 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005801 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005802 * @erp: Pointer to save error value
5803 *
5804 * This helper routine does exactly what is promise "swap extents". All other
5805 * stuff such as page-cache locking consistency, bh mapping consistency or
5806 * extent's data copying must be performed by caller.
5807 * Locking:
5808 * i_mutex is held for both inodes
5809 * i_data_sem is locked for write for both inodes
5810 * Assumptions:
5811 * All pages from requested range are locked for both inodes
5812 */
5813int
5814ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005815 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005816 ext4_lblk_t count, int unwritten, int *erp)
5817{
5818 struct ext4_ext_path *path1 = NULL;
5819 struct ext4_ext_path *path2 = NULL;
5820 int replaced_count = 0;
5821
5822 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5823 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005824 BUG_ON(!inode_is_locked(inode1));
5825 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005826
5827 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005828 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005829 return 0;
5830 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005831 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005832 return 0;
5833
5834 while (count) {
5835 struct ext4_extent *ex1, *ex2, tmp_ex;
5836 ext4_lblk_t e1_blk, e2_blk;
5837 int e1_len, e2_len, len;
5838 int split = 0;
5839
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005840 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305841 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005842 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005843 path1 = NULL;
5844 finish:
5845 count = 0;
5846 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005847 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005848 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305849 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005850 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005851 path2 = NULL;
5852 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005853 }
5854 ex1 = path1[path1->p_depth].p_ext;
5855 ex2 = path2[path2->p_depth].p_ext;
5856 /* Do we have somthing to swap ? */
5857 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005858 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005859
5860 e1_blk = le32_to_cpu(ex1->ee_block);
5861 e2_blk = le32_to_cpu(ex2->ee_block);
5862 e1_len = ext4_ext_get_actual_len(ex1);
5863 e2_len = ext4_ext_get_actual_len(ex2);
5864
5865 /* Hole handling */
5866 if (!in_range(lblk1, e1_blk, e1_len) ||
5867 !in_range(lblk2, e2_blk, e2_len)) {
5868 ext4_lblk_t next1, next2;
5869
5870 /* if hole after extent, then go to next extent */
5871 next1 = ext4_ext_next_allocated_block(path1);
5872 next2 = ext4_ext_next_allocated_block(path2);
5873 /* If hole before extent, then shift to that extent */
5874 if (e1_blk > lblk1)
5875 next1 = e1_blk;
5876 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005877 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005878 /* Do we have something to swap */
5879 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005880 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005881 /* Move to the rightest boundary */
5882 len = next1 - lblk1;
5883 if (len < next2 - lblk2)
5884 len = next2 - lblk2;
5885 if (len > count)
5886 len = count;
5887 lblk1 += len;
5888 lblk2 += len;
5889 count -= len;
5890 goto repeat;
5891 }
5892
5893 /* Prepare left boundary */
5894 if (e1_blk < lblk1) {
5895 split = 1;
5896 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005897 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005898 if (unlikely(*erp))
5899 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005900 }
5901 if (e2_blk < lblk2) {
5902 split = 1;
5903 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005904 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005905 if (unlikely(*erp))
5906 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005907 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005908 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005909 * path must to be revalidated. */
5910 if (split)
5911 goto repeat;
5912
5913 /* Prepare right boundary */
5914 len = count;
5915 if (len > e1_blk + e1_len - lblk1)
5916 len = e1_blk + e1_len - lblk1;
5917 if (len > e2_blk + e2_len - lblk2)
5918 len = e2_blk + e2_len - lblk2;
5919
5920 if (len != e1_len) {
5921 split = 1;
5922 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005923 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005924 if (unlikely(*erp))
5925 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005926 }
5927 if (len != e2_len) {
5928 split = 1;
5929 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005930 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005931 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005932 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005933 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005934 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005935 * path must to be revalidated. */
5936 if (split)
5937 goto repeat;
5938
5939 BUG_ON(e2_len != e1_len);
5940 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005941 if (unlikely(*erp))
5942 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005943 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005944 if (unlikely(*erp))
5945 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005946
5947 /* Both extents are fully inside boundaries. Swap it now */
5948 tmp_ex = *ex1;
5949 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5950 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5951 ex1->ee_len = cpu_to_le16(e2_len);
5952 ex2->ee_len = cpu_to_le16(e1_len);
5953 if (unwritten)
5954 ext4_ext_mark_unwritten(ex2);
5955 if (ext4_ext_is_unwritten(&tmp_ex))
5956 ext4_ext_mark_unwritten(ex1);
5957
5958 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5959 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5960 *erp = ext4_ext_dirty(handle, inode2, path2 +
5961 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005962 if (unlikely(*erp))
5963 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005964 *erp = ext4_ext_dirty(handle, inode1, path1 +
5965 path1->p_depth);
5966 /*
5967 * Looks scarry ah..? second inode already points to new blocks,
5968 * and it was successfully dirtied. But luckily error may happen
5969 * only due to journal error, so full transaction will be
5970 * aborted anyway.
5971 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005972 if (unlikely(*erp))
5973 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005974 lblk1 += len;
5975 lblk2 += len;
5976 replaced_count += len;
5977 count -= len;
5978
5979 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005980 ext4_ext_drop_refs(path1);
5981 kfree(path1);
5982 ext4_ext_drop_refs(path2);
5983 kfree(path2);
5984 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005985 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005986 return replaced_count;
5987}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005988
5989/*
5990 * ext4_clu_mapped - determine whether any block in a logical cluster has
5991 * been mapped to a physical cluster
5992 *
5993 * @inode - file containing the logical cluster
5994 * @lclu - logical cluster of interest
5995 *
5996 * Returns 1 if any block in the logical cluster is mapped, signifying
5997 * that a physical cluster has been allocated for it. Otherwise,
5998 * returns 0. Can also return negative error codes. Derived from
5999 * ext4_ext_map_blocks().
6000 */
6001int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
6002{
6003 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
6004 struct ext4_ext_path *path;
6005 int depth, mapped = 0, err = 0;
6006 struct ext4_extent *extent;
6007 ext4_lblk_t first_lblk, first_lclu, last_lclu;
6008
6009 /* search for the extent closest to the first block in the cluster */
6010 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
6011 if (IS_ERR(path)) {
6012 err = PTR_ERR(path);
6013 path = NULL;
6014 goto out;
6015 }
6016
6017 depth = ext_depth(inode);
6018
6019 /*
6020 * A consistent leaf must not be empty. This situation is possible,
6021 * though, _during_ tree modification, and it's why an assert can't
6022 * be put in ext4_find_extent().
6023 */
6024 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
6025 EXT4_ERROR_INODE(inode,
6026 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
6027 (unsigned long) EXT4_C2B(sbi, lclu),
6028 depth, path[depth].p_block);
6029 err = -EFSCORRUPTED;
6030 goto out;
6031 }
6032
6033 extent = path[depth].p_ext;
6034
6035 /* can't be mapped if the extent tree is empty */
6036 if (extent == NULL)
6037 goto out;
6038
6039 first_lblk = le32_to_cpu(extent->ee_block);
6040 first_lclu = EXT4_B2C(sbi, first_lblk);
6041
6042 /*
6043 * Three possible outcomes at this point - found extent spanning
6044 * the target cluster, to the left of the target cluster, or to the
6045 * right of the target cluster. The first two cases are handled here.
6046 * The last case indicates the target cluster is not mapped.
6047 */
6048 if (lclu >= first_lclu) {
6049 last_lclu = EXT4_B2C(sbi, first_lblk +
6050 ext4_ext_get_actual_len(extent) - 1);
6051 if (lclu <= last_lclu) {
6052 mapped = 1;
6053 } else {
6054 first_lblk = ext4_ext_next_allocated_block(path);
6055 first_lclu = EXT4_B2C(sbi, first_lblk);
6056 if (lclu == first_lclu)
6057 mapped = 1;
6058 }
6059 }
6060
6061out:
6062 ext4_ext_drop_refs(path);
6063 kfree(path);
6064
6065 return err ? err : mapped;
6066}