blob: 32f2c22c7ef210f696b4ab190fa1885e60e5c2ce [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,
127 int check_cred, int restart_cred)
128{
129 int ret;
130 int dropped = 0;
131
132 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
133 ext4_ext_trunc_restart_fn(inode, &dropped));
134 if (dropped)
135 down_write(&EXT4_I(inode)->i_data_sem);
136 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700137}
138
139/*
140 * could return:
141 * - EROFS
142 * - ENOMEM
143 */
144static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
145 struct ext4_ext_path *path)
146{
147 if (path->p_bh) {
148 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400149 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700150 return ext4_journal_get_write_access(handle, path->p_bh);
151 }
152 /* path points to leaf/index in inode body */
153 /* we use in-core data, no need to protect them */
154 return 0;
155}
156
157/*
158 * could return:
159 * - EROFS
160 * - ENOMEM
161 * - EIO
162 */
Darrick J. Wong26564972013-04-19 14:04:12 -0400163int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
164 struct inode *inode, struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700165{
166 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400167
168 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700169 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400170 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700171 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400172 err = __ext4_handle_dirty_metadata(where, line, handle,
173 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700174 } else {
175 /* path points to leaf/index in inode body */
176 err = ext4_mark_inode_dirty(handle, inode);
177 }
178 return err;
179}
180
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700181static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700182 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500183 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700184{
Alex Tomasa86c6182006-10-11 01:21:03 -0700185 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400186 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700187 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700188
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500189 /*
190 * Try to predict block placement assuming that we are
191 * filling in a file which will eventually be
192 * non-sparse --- i.e., in the case of libbfd writing
193 * an ELF object sections out-of-order but in a way
194 * the eventually results in a contiguous object or
195 * executable file, or some database extending a table
196 * space file. However, this is actually somewhat
197 * non-ideal if we are writing a sparse file such as
198 * qemu or KVM writing a raw image file that is going
199 * to stay fairly sparse, since it will end up
200 * fragmenting the file system's free space. Maybe we
201 * should have some hueristics or some way to allow
202 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800203 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500204 * common.
205 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800206 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500207 if (ex) {
208 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
209 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
210
211 if (block > ext_block)
212 return ext_pblk + (block - ext_block);
213 else
214 return ext_pblk - (ext_block - block);
215 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700216
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700217 /* it looks like index is empty;
218 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700219 if (path[depth].p_bh)
220 return path[depth].p_bh->b_blocknr;
221 }
222
223 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400224 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700225}
226
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400227/*
228 * Allocation for a meta data block
229 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700230static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400231ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700232 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400233 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700234{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700235 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700236
237 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400238 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
239 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700240 return newblock;
241}
242
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400243static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700244{
245 int size;
246
247 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
248 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100249#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400250 if (!check && size > 6)
251 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700252#endif
253 return size;
254}
255
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400256static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700257{
258 int size;
259
260 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
261 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100262#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400263 if (!check && size > 5)
264 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700265#endif
266 return size;
267}
268
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400269static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700270{
271 int size;
272
273 size = sizeof(EXT4_I(inode)->i_data);
274 size -= sizeof(struct ext4_extent_header);
275 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100276#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400277 if (!check && size > 3)
278 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700279#endif
280 return size;
281}
282
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400283static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700284{
285 int size;
286
287 size = sizeof(EXT4_I(inode)->i_data);
288 size -= sizeof(struct ext4_extent_header);
289 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100290#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400291 if (!check && size > 4)
292 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700293#endif
294 return size;
295}
296
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400297static inline int
298ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400299 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400300 int nofail)
301{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400302 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400303 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
304
Theodore Ts'odfe50802014-09-01 14:37:09 -0400305 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400306 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
307 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
308 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
309}
310
Mingming Caod2a17632008-07-14 17:52:37 -0400311/*
312 * Calculate the number of metadata blocks needed
313 * to allocate @blocks
314 * Worse case is one block per extent
315 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500316int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400317{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500318 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400319 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400320
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500321 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
322 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400323
324 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500325 * If the new delayed allocation block is contiguous with the
326 * previous da block, it can share index blocks with the
327 * previous block, so we only need to allocate a new index
328 * block every idxs leaf blocks. At ldxs**2 blocks, we need
329 * an additional index block, and at ldxs**3 blocks, yet
330 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400331 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500332 if (ei->i_da_metadata_calc_len &&
333 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400334 int num = 0;
335
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500336 if ((ei->i_da_metadata_calc_len % idxs) == 0)
337 num++;
338 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
339 num++;
340 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
341 num++;
342 ei->i_da_metadata_calc_len = 0;
343 } else
344 ei->i_da_metadata_calc_len++;
345 ei->i_da_metadata_calc_last_lblock++;
346 return num;
347 }
Mingming Caod2a17632008-07-14 17:52:37 -0400348
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500349 /*
350 * In the worst case we need a new set of index blocks at
351 * every level of the inode's extent tree.
352 */
353 ei->i_da_metadata_calc_len = 1;
354 ei->i_da_metadata_calc_last_lblock = lblock;
355 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400356}
357
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400358static int
359ext4_ext_max_entries(struct inode *inode, int depth)
360{
361 int max;
362
363 if (depth == ext_depth(inode)) {
364 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400365 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400366 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400367 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400368 } else {
369 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400370 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400371 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400372 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400373 }
374
375 return max;
376}
377
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400378static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
379{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400380 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400381 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500382 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400383
Vegard Nossumf70749c2016-06-30 11:53:46 -0400384 /*
385 * We allow neither:
386 * - zero length
387 * - overflow/wrap-around
388 */
389 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400390 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400391 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400392}
393
394static int ext4_valid_extent_idx(struct inode *inode,
395 struct ext4_extent_idx *ext_idx)
396{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400397 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400398
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400399 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400400}
401
402static int ext4_valid_extent_entries(struct inode *inode,
403 struct ext4_extent_header *eh,
404 int depth)
405{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400406 unsigned short entries;
407 if (eh->eh_entries == 0)
408 return 1;
409
410 entries = le16_to_cpu(eh->eh_entries);
411
412 if (depth == 0) {
413 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400414 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500415 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
416 ext4_fsblk_t pblock = 0;
417 ext4_lblk_t lblock = 0;
418 ext4_lblk_t prev = 0;
419 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400420 while (entries) {
421 if (!ext4_valid_extent(inode, ext))
422 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500423
424 /* Check for overlapping extents */
425 lblock = le32_to_cpu(ext->ee_block);
426 len = ext4_ext_get_actual_len(ext);
427 if ((lblock <= prev) && prev) {
428 pblock = ext4_ext_pblock(ext);
429 es->s_last_error_block = cpu_to_le64(pblock);
430 return 0;
431 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400432 ext++;
433 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500434 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400435 }
436 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400437 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400438 while (entries) {
439 if (!ext4_valid_extent_idx(inode, ext_idx))
440 return 0;
441 ext_idx++;
442 entries--;
443 }
444 }
445 return 1;
446}
447
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400448static int __ext4_ext_check(const char *function, unsigned int line,
449 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400450 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400451{
452 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400453 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400454
455 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
456 error_msg = "invalid magic";
457 goto corrupted;
458 }
459 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
460 error_msg = "unexpected eh_depth";
461 goto corrupted;
462 }
463 if (unlikely(eh->eh_max == 0)) {
464 error_msg = "invalid eh_max";
465 goto corrupted;
466 }
467 max = ext4_ext_max_entries(inode, depth);
468 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
469 error_msg = "too large eh_max";
470 goto corrupted;
471 }
472 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
473 error_msg = "invalid eh_entries";
474 goto corrupted;
475 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400476 if (!ext4_valid_extent_entries(inode, eh, depth)) {
477 error_msg = "invalid extent entries";
478 goto corrupted;
479 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400480 if (unlikely(depth > 32)) {
481 error_msg = "too large eh_depth";
482 goto corrupted;
483 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400484 /* Verify checksum on non-root extent tree nodes */
485 if (ext_depth(inode) != depth &&
486 !ext4_extent_block_csum_verify(inode, eh)) {
487 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400488 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400489 goto corrupted;
490 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400491 return 0;
492
493corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400494 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400495 "pblk %llu bad header/extent: %s - magic %x, "
496 "entries %u, max %u(%u), depth %u(%u)",
497 (unsigned long long) pblk, error_msg,
498 le16_to_cpu(eh->eh_magic),
499 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
500 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400501 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400502}
503
Theodore Ts'oc3491792013-08-16 21:21:41 -0400504#define ext4_ext_check(inode, eh, depth, pblk) \
505 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400506
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400507int ext4_ext_check_inode(struct inode *inode)
508{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400509 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400510}
511
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400512static struct buffer_head *
513__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400514 struct inode *inode, ext4_fsblk_t pblk, int depth,
515 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400516{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400517 struct buffer_head *bh;
518 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400519
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400520 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400521 if (unlikely(!bh))
522 return ERR_PTR(-ENOMEM);
523
524 if (!bh_uptodate_or_lock(bh)) {
525 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
526 err = bh_submit_read(bh);
527 if (err < 0)
528 goto errout;
529 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400530 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400531 return bh;
Theodore Ts'o0a944e82019-05-22 10:27:01 -0400532 if (!ext4_has_feature_journal(inode->i_sb) ||
533 (inode->i_ino !=
534 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
535 err = __ext4_ext_check(function, line, inode,
536 ext_block_hdr(bh), depth, pblk);
537 if (err)
538 goto errout;
539 }
Darrick J. Wongf8489122012-04-29 18:21:10 -0400540 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400541 /*
542 * If this is a leaf block, cache all of its entries
543 */
544 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
545 struct ext4_extent_header *eh = ext_block_hdr(bh);
546 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
547 ext4_lblk_t prev = 0;
548 int i;
549
550 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
551 unsigned int status = EXTENT_STATUS_WRITTEN;
552 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
553 int len = ext4_ext_get_actual_len(ex);
554
555 if (prev && (prev != lblk))
556 ext4_es_cache_extent(inode, prev,
557 lblk - prev, ~0,
558 EXTENT_STATUS_HOLE);
559
Lukas Czerner556615d2014-04-20 23:45:47 -0400560 if (ext4_ext_is_unwritten(ex))
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400561 status = EXTENT_STATUS_UNWRITTEN;
562 ext4_es_cache_extent(inode, lblk, len,
563 ext4_ext_pblock(ex), status);
564 prev = lblk + len;
565 }
566 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400567 return bh;
568errout:
569 put_bh(bh);
570 return ERR_PTR(err);
571
Darrick J. Wongf8489122012-04-29 18:21:10 -0400572}
573
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400574#define read_extent_tree_block(inode, pblk, depth, flags) \
575 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
576 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400577
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400578/*
579 * This function is called to cache a file's extent information in the
580 * extent status tree
581 */
582int ext4_ext_precache(struct inode *inode)
583{
584 struct ext4_inode_info *ei = EXT4_I(inode);
585 struct ext4_ext_path *path = NULL;
586 struct buffer_head *bh;
587 int i = 0, depth, ret = 0;
588
589 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
590 return 0; /* not an extent-mapped inode */
591
592 down_read(&ei->i_data_sem);
593 depth = ext_depth(inode);
594
Kees Cook6396bb22018-06-12 14:03:40 -0700595 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400596 GFP_NOFS);
597 if (path == NULL) {
598 up_read(&ei->i_data_sem);
599 return -ENOMEM;
600 }
601
602 /* Don't cache anything if there are no external extent blocks */
603 if (depth == 0)
604 goto out;
605 path[0].p_hdr = ext_inode_hdr(inode);
606 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
607 if (ret)
608 goto out;
609 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
610 while (i >= 0) {
611 /*
612 * If this is a leaf block or we've reached the end of
613 * the index block, go up
614 */
615 if ((i == depth) ||
616 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
617 brelse(path[i].p_bh);
618 path[i].p_bh = NULL;
619 i--;
620 continue;
621 }
622 bh = read_extent_tree_block(inode,
623 ext4_idx_pblock(path[i].p_idx++),
624 depth - i - 1,
625 EXT4_EX_FORCE_CACHE);
626 if (IS_ERR(bh)) {
627 ret = PTR_ERR(bh);
628 break;
629 }
630 i++;
631 path[i].p_bh = bh;
632 path[i].p_hdr = ext_block_hdr(bh);
633 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
634 }
635 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
636out:
637 up_read(&ei->i_data_sem);
638 ext4_ext_drop_refs(path);
639 kfree(path);
640 return ret;
641}
642
Alex Tomasa86c6182006-10-11 01:21:03 -0700643#ifdef EXT_DEBUG
644static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
645{
646 int k, l = path->p_depth;
647
648 ext_debug("path:");
649 for (k = 0; k <= l; k++, path++) {
650 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700651 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400652 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700653 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400654 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700655 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400656 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400657 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400658 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700659 } else
660 ext_debug(" []");
661 }
662 ext_debug("\n");
663}
664
665static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
666{
667 int depth = ext_depth(inode);
668 struct ext4_extent_header *eh;
669 struct ext4_extent *ex;
670 int i;
671
672 if (!path)
673 return;
674
675 eh = path[depth].p_hdr;
676 ex = EXT_FIRST_EXTENT(eh);
677
Mingming553f9002009-09-18 13:34:55 -0400678 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
679
Alex Tomasa86c6182006-10-11 01:21:03 -0700680 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400681 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400682 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400683 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700684 }
685 ext_debug("\n");
686}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400687
688static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
689 ext4_fsblk_t newblock, int level)
690{
691 int depth = ext_depth(inode);
692 struct ext4_extent *ex;
693
694 if (depth != level) {
695 struct ext4_extent_idx *idx;
696 idx = path[level].p_idx;
697 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
698 ext_debug("%d: move %d:%llu in new index %llu\n", level,
699 le32_to_cpu(idx->ei_block),
700 ext4_idx_pblock(idx),
701 newblock);
702 idx++;
703 }
704
705 return;
706 }
707
708 ex = path[depth].p_ext;
709 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
710 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
711 le32_to_cpu(ex->ee_block),
712 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400713 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400714 ext4_ext_get_actual_len(ex),
715 newblock);
716 ex++;
717 }
718}
719
Alex Tomasa86c6182006-10-11 01:21:03 -0700720#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400721#define ext4_ext_show_path(inode, path)
722#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400723#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700724#endif
725
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500726void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700727{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400728 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700729
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400730 if (!path)
731 return;
732 depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700733 for (i = 0; i <= depth; i++, path++)
734 if (path->p_bh) {
735 brelse(path->p_bh);
736 path->p_bh = NULL;
737 }
738}
739
740/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700741 * ext4_ext_binsearch_idx:
742 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400743 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700744 */
745static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500746ext4_ext_binsearch_idx(struct inode *inode,
747 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700748{
749 struct ext4_extent_header *eh = path->p_hdr;
750 struct ext4_extent_idx *r, *l, *m;
751
Alex Tomasa86c6182006-10-11 01:21:03 -0700752
Eric Sandeenbba90742008-01-28 23:58:27 -0500753 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700754
755 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400756 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700757 while (l <= r) {
758 m = l + (r - l) / 2;
759 if (block < le32_to_cpu(m->ei_block))
760 r = m - 1;
761 else
762 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400763 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
764 m, le32_to_cpu(m->ei_block),
765 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700766 }
767
768 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400769 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400770 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700771
772#ifdef CHECK_BINSEARCH
773 {
774 struct ext4_extent_idx *chix, *ix;
775 int k;
776
777 chix = ix = EXT_FIRST_INDEX(eh);
778 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
779 if (k != 0 &&
780 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400781 printk(KERN_DEBUG "k=%d, ix=0x%p, "
782 "first=0x%p\n", k,
783 ix, EXT_FIRST_INDEX(eh));
784 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700785 le32_to_cpu(ix->ei_block),
786 le32_to_cpu(ix[-1].ei_block));
787 }
788 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400789 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700790 if (block < le32_to_cpu(ix->ei_block))
791 break;
792 chix = ix;
793 }
794 BUG_ON(chix != path->p_idx);
795 }
796#endif
797
798}
799
800/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700801 * ext4_ext_binsearch:
802 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400803 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700804 */
805static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500806ext4_ext_binsearch(struct inode *inode,
807 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700808{
809 struct ext4_extent_header *eh = path->p_hdr;
810 struct ext4_extent *r, *l, *m;
811
Alex Tomasa86c6182006-10-11 01:21:03 -0700812 if (eh->eh_entries == 0) {
813 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700814 * this leaf is empty:
815 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700816 */
817 return;
818 }
819
Eric Sandeenbba90742008-01-28 23:58:27 -0500820 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700821
822 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400823 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700824
825 while (l <= r) {
826 m = l + (r - l) / 2;
827 if (block < le32_to_cpu(m->ee_block))
828 r = m - 1;
829 else
830 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400831 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
832 m, le32_to_cpu(m->ee_block),
833 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700834 }
835
836 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400837 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400838 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400839 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400840 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400841 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700842
843#ifdef CHECK_BINSEARCH
844 {
845 struct ext4_extent *chex, *ex;
846 int k;
847
848 chex = ex = EXT_FIRST_EXTENT(eh);
849 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
850 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400851 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700852 if (block < le32_to_cpu(ex->ee_block))
853 break;
854 chex = ex;
855 }
856 BUG_ON(chex != path->p_ext);
857 }
858#endif
859
860}
861
862int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
863{
864 struct ext4_extent_header *eh;
865
866 eh = ext_inode_hdr(inode);
867 eh->eh_depth = 0;
868 eh->eh_entries = 0;
869 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400870 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700871 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 return 0;
873}
874
875struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400876ext4_find_extent(struct inode *inode, ext4_lblk_t block,
877 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700878{
879 struct ext4_extent_header *eh;
880 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400881 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
882 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500883 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700884
885 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400886 depth = ext_depth(inode);
Theodore Ts'obc890a62018-06-14 12:55:10 -0400887 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
888 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
889 depth);
890 ret = -EFSCORRUPTED;
891 goto err;
892 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700893
Theodore Ts'o10809df82014-09-01 14:40:09 -0400894 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400895 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400896 if (depth > path[0].p_maxdepth) {
897 kfree(path);
898 *orig_path = path = NULL;
899 }
900 }
901 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400902 /* account possible depth increase */
Kees Cook6396bb22018-06-12 14:03:40 -0700903 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
Alex Tomasa86c6182006-10-11 01:21:03 -0700904 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400905 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400907 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700908 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700909 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400910 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700911
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400912 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700913 /* walk through the tree */
914 while (i) {
915 ext_debug("depth %d: num %d, max %d\n",
916 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400917
Alex Tomasa86c6182006-10-11 01:21:03 -0700918 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400919 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 path[ppos].p_depth = i;
921 path[ppos].p_ext = NULL;
922
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400923 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
924 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530925 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400926 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500928 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400929
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 eh = ext_block_hdr(bh);
931 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700932 path[ppos].p_bh = bh;
933 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700934 }
935
936 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700937 path[ppos].p_ext = NULL;
938 path[ppos].p_idx = NULL;
939
Alex Tomasa86c6182006-10-11 01:21:03 -0700940 /* find extent */
941 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400942 /* if not an empty leaf */
943 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400944 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700945
946 ext4_ext_show_path(inode, path);
947
948 return path;
949
950err:
951 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400952 kfree(path);
953 if (orig_path)
954 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500955 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700956}
957
958/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700959 * ext4_ext_insert_index:
960 * insert new index [@logical;@ptr] into the block at @curp;
961 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400963static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
964 struct ext4_ext_path *curp,
965 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700966{
967 struct ext4_extent_idx *ix;
968 int len, err;
969
Avantika Mathur7e028972006-12-06 20:41:33 -0800970 err = ext4_ext_get_access(handle, inode, curp);
971 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700972 return err;
973
Frank Mayhar273df552010-03-02 11:46:09 -0500974 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
975 EXT4_ERROR_INODE(inode,
976 "logical %d == ei_block %d!",
977 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400978 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500979 }
Robin Dongd4620312011-07-17 23:43:42 -0400980
981 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
982 >= le16_to_cpu(curp->p_hdr->eh_max))) {
983 EXT4_ERROR_INODE(inode,
984 "eh_entries %d >= eh_max %d!",
985 le16_to_cpu(curp->p_hdr->eh_entries),
986 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400987 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400988 }
989
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
991 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400992 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700993 ix = curp->p_idx + 1;
994 } else {
995 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400996 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700997 ix = curp->p_idx;
998 }
999
Eric Gouriou80e675f2011-10-27 11:52:18 -04001000 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
1001 BUG_ON(len < 0);
1002 if (len > 0) {
1003 ext_debug("insert new index %d: "
1004 "move %d indices from 0x%p to 0x%p\n",
1005 logical, len, ix, ix + 1);
1006 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
1007 }
1008
Tao Maf472e022011-10-17 10:13:46 -04001009 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1010 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001011 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -04001012 }
1013
Alex Tomasa86c6182006-10-11 01:21:03 -07001014 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001015 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001016 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001017
Frank Mayhar273df552010-03-02 11:46:09 -05001018 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1019 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001020 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001021 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001022
1023 err = ext4_ext_dirty(handle, inode, curp);
1024 ext4_std_error(inode->i_sb, err);
1025
1026 return err;
1027}
1028
1029/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001030 * ext4_ext_split:
1031 * inserts new subtree into the path, using free index entry
1032 * at depth @at:
1033 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1034 * - makes decision where to split
1035 * - moves remaining extents and index entries (right to the split point)
1036 * into the newly allocated blocks
1037 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001038 */
1039static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001040 unsigned int flags,
1041 struct ext4_ext_path *path,
1042 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001043{
1044 struct buffer_head *bh = NULL;
1045 int depth = ext_depth(inode);
1046 struct ext4_extent_header *neh;
1047 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001049 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001051 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001053 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001054
1055 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001056 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001057
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001058 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001059 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001060 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1061 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001062 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001063 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001064 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1065 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001066 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001067 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001068 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001069 } else {
1070 border = newext->ee_block;
1071 ext_debug("leaf will be added."
1072 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001073 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001074 }
1075
1076 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001077 * If error occurs, then we break processing
1078 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001079 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001080 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001081 */
1082
1083 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001084 * Get array to track all allocated blocks.
1085 * We need this to handle errors and free blocks
1086 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001087 */
Kees Cook6396bb22018-06-12 14:03:40 -07001088 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001089 if (!ablocks)
1090 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001091
1092 /* allocate all needed blocks */
1093 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1094 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001095 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001096 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001097 if (newblock == 0)
1098 goto cleanup;
1099 ablocks[a] = newblock;
1100 }
1101
1102 /* initialize new leaf */
1103 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001104 if (unlikely(newblock == 0)) {
1105 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001106 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001107 goto cleanup;
1108 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001109 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001110 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001111 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001112 goto cleanup;
1113 }
1114 lock_buffer(bh);
1115
Avantika Mathur7e028972006-12-06 20:41:33 -08001116 err = ext4_journal_get_create_access(handle, bh);
1117 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001118 goto cleanup;
1119
1120 neh = ext_block_hdr(bh);
1121 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001122 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001123 neh->eh_magic = EXT4_EXT_MAGIC;
1124 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001125
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001126 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001127 if (unlikely(path[depth].p_hdr->eh_entries !=
1128 path[depth].p_hdr->eh_max)) {
1129 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1130 path[depth].p_hdr->eh_entries,
1131 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001132 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001133 goto cleanup;
1134 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001136 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1137 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001138 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001139 struct ext4_extent *ex;
1140 ex = EXT_FIRST_EXTENT(neh);
1141 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001142 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001143 }
1144
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001145 /* zero out unused area in the extent block */
1146 ext_size = sizeof(struct ext4_extent_header) +
1147 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1148 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001149 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001150 set_buffer_uptodate(bh);
1151 unlock_buffer(bh);
1152
Frank Mayhar03901312009-01-07 00:06:22 -05001153 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001154 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001155 goto cleanup;
1156 brelse(bh);
1157 bh = NULL;
1158
1159 /* correct old leaf */
1160 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001161 err = ext4_ext_get_access(handle, inode, path + depth);
1162 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001163 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001164 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001165 err = ext4_ext_dirty(handle, inode, path + depth);
1166 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001167 goto cleanup;
1168
1169 }
1170
1171 /* create intermediate indexes */
1172 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001173 if (unlikely(k < 0)) {
1174 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001175 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001176 goto cleanup;
1177 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001178 if (k)
1179 ext_debug("create %d intermediate indices\n", k);
1180 /* insert new index into current index block */
1181 /* current depth stored in i var */
1182 i = depth - 1;
1183 while (k--) {
1184 oldblock = newblock;
1185 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001186 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001187 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001188 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001189 goto cleanup;
1190 }
1191 lock_buffer(bh);
1192
Avantika Mathur7e028972006-12-06 20:41:33 -08001193 err = ext4_journal_get_create_access(handle, bh);
1194 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001195 goto cleanup;
1196
1197 neh = ext_block_hdr(bh);
1198 neh->eh_entries = cpu_to_le16(1);
1199 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001200 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001201 neh->eh_depth = cpu_to_le16(depth - i);
1202 fidx = EXT_FIRST_INDEX(neh);
1203 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001204 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001205
Eric Sandeenbba90742008-01-28 23:58:27 -05001206 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1207 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001208
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001209 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001210 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1211 EXT_LAST_INDEX(path[i].p_hdr))) {
1212 EXT4_ERROR_INODE(inode,
1213 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1214 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001215 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001216 goto cleanup;
1217 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001218 /* start copy indexes */
1219 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1220 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1221 EXT_MAX_INDEX(path[i].p_hdr));
1222 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001223 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001224 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001225 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001226 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001227 }
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001228 /* zero out unused area in the extent block */
1229 ext_size = sizeof(struct ext4_extent_header) +
1230 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1231 memset(bh->b_data + ext_size, 0,
1232 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001233 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001234 set_buffer_uptodate(bh);
1235 unlock_buffer(bh);
1236
Frank Mayhar03901312009-01-07 00:06:22 -05001237 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001238 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001239 goto cleanup;
1240 brelse(bh);
1241 bh = NULL;
1242
1243 /* correct old index */
1244 if (m) {
1245 err = ext4_ext_get_access(handle, inode, path + i);
1246 if (err)
1247 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001248 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001249 err = ext4_ext_dirty(handle, inode, path + i);
1250 if (err)
1251 goto cleanup;
1252 }
1253
1254 i--;
1255 }
1256
1257 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001258 err = ext4_ext_insert_index(handle, inode, path + at,
1259 le32_to_cpu(border), newblock);
1260
1261cleanup:
1262 if (bh) {
1263 if (buffer_locked(bh))
1264 unlock_buffer(bh);
1265 brelse(bh);
1266 }
1267
1268 if (err) {
1269 /* free all allocated blocks in error case */
1270 for (i = 0; i < depth; i++) {
1271 if (!ablocks[i])
1272 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001273 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001274 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001275 }
1276 }
1277 kfree(ablocks);
1278
1279 return err;
1280}
1281
1282/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001283 * ext4_ext_grow_indepth:
1284 * implements tree growing procedure:
1285 * - allocates new block
1286 * - moves top-level data (index block or leaf) into the new block
1287 * - initializes new top-level, creating index that points to the
1288 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001289 */
1290static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001291 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001292{
Alex Tomasa86c6182006-10-11 01:21:03 -07001293 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001294 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001295 ext4_fsblk_t newblock, goal = 0;
1296 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001297 int err = 0;
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001298 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001299
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001300 /* Try to prepend new index to old one */
1301 if (ext_depth(inode))
1302 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1303 if (goal > le32_to_cpu(es->s_first_data_block)) {
1304 flags |= EXT4_MB_HINT_TRY_GOAL;
1305 goal--;
1306 } else
1307 goal = ext4_inode_to_goal_block(inode);
1308 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1309 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001310 if (newblock == 0)
1311 return err;
1312
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001313 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001314 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001315 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001316 lock_buffer(bh);
1317
Avantika Mathur7e028972006-12-06 20:41:33 -08001318 err = ext4_journal_get_create_access(handle, bh);
1319 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001320 unlock_buffer(bh);
1321 goto out;
1322 }
1323
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001324 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001325 /* move top-level index/leaf into new block */
Sriram Rajagopalan592acbf2019-05-10 19:28:06 -04001326 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1327 /* zero out unused area in the extent block */
1328 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001329
1330 /* set size of new block */
1331 neh = ext_block_hdr(bh);
1332 /* old root could have indexes or leaves
1333 * so calculate e_max right way */
1334 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001335 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001336 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001337 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001338 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001339 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001340 set_buffer_uptodate(bh);
1341 unlock_buffer(bh);
1342
Frank Mayhar03901312009-01-07 00:06:22 -05001343 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001344 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001345 goto out;
1346
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001347 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001348 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001349 neh->eh_entries = cpu_to_le16(1);
1350 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1351 if (neh->eh_depth == 0) {
1352 /* Root extent block becomes index block */
1353 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1354 EXT_FIRST_INDEX(neh)->ei_block =
1355 EXT_FIRST_EXTENT(neh)->ee_block;
1356 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001357 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001358 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001359 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001360 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001361
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001362 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001363 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001364out:
1365 brelse(bh);
1366
1367 return err;
1368}
1369
1370/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001371 * ext4_ext_create_new_leaf:
1372 * finds empty index and adds new leaf.
1373 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001374 */
1375static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001376 unsigned int mb_flags,
1377 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001378 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001379 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001380{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001381 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001382 struct ext4_ext_path *curp;
1383 int depth, i, err = 0;
1384
1385repeat:
1386 i = depth = ext_depth(inode);
1387
1388 /* walk up to the tree and look for free index entry */
1389 curp = path + depth;
1390 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1391 i--;
1392 curp--;
1393 }
1394
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001395 /* we use already allocated block for index block,
1396 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001397 if (EXT_HAS_FREE_INDEX(curp)) {
1398 /* if we found index with free entry, then use that
1399 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001400 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001401 if (err)
1402 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001403
1404 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001405 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001406 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001407 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001408 if (IS_ERR(path))
1409 err = PTR_ERR(path);
1410 } else {
1411 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001412 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001413 if (err)
1414 goto out;
1415
1416 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001417 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001418 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001419 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001420 if (IS_ERR(path)) {
1421 err = PTR_ERR(path);
1422 goto out;
1423 }
1424
1425 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001426 * only first (depth 0 -> 1) produces free space;
1427 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001428 */
1429 depth = ext_depth(inode);
1430 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001431 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001432 goto repeat;
1433 }
1434 }
1435
1436out:
1437 return err;
1438}
1439
1440/*
Alex Tomas1988b512008-01-28 23:58:27 -05001441 * search the closest allocated block to the left for *logical
1442 * and returns it at @logical + it's physical address at @phys
1443 * if *logical is the smallest allocated block, the function
1444 * returns 0 at @phys
1445 * return value contains 0 (success) or error code
1446 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001447static int ext4_ext_search_left(struct inode *inode,
1448 struct ext4_ext_path *path,
1449 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001450{
1451 struct ext4_extent_idx *ix;
1452 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001453 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001454
Frank Mayhar273df552010-03-02 11:46:09 -05001455 if (unlikely(path == NULL)) {
1456 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001457 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001458 }
Alex Tomas1988b512008-01-28 23:58:27 -05001459 depth = path->p_depth;
1460 *phys = 0;
1461
1462 if (depth == 0 && path->p_ext == NULL)
1463 return 0;
1464
1465 /* usually extent in the path covers blocks smaller
1466 * then *logical, but it can be that extent is the
1467 * first one in the file */
1468
1469 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001470 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001471 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001472 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1473 EXT4_ERROR_INODE(inode,
1474 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1475 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001476 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001477 }
Alex Tomas1988b512008-01-28 23:58:27 -05001478 while (--depth >= 0) {
1479 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001480 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1481 EXT4_ERROR_INODE(inode,
1482 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001483 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001484 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001485 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001486 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001487 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001488 }
Alex Tomas1988b512008-01-28 23:58:27 -05001489 }
1490 return 0;
1491 }
1492
Frank Mayhar273df552010-03-02 11:46:09 -05001493 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1494 EXT4_ERROR_INODE(inode,
1495 "logical %d < ee_block %d + ee_len %d!",
1496 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001497 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001498 }
Alex Tomas1988b512008-01-28 23:58:27 -05001499
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001500 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001501 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001502 return 0;
1503}
1504
1505/*
1506 * search the closest allocated block to the right for *logical
1507 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001508 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001509 * returns 0 at @phys
1510 * return value contains 0 (success) or error code
1511 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001512static int ext4_ext_search_right(struct inode *inode,
1513 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001514 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1515 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001516{
1517 struct buffer_head *bh = NULL;
1518 struct ext4_extent_header *eh;
1519 struct ext4_extent_idx *ix;
1520 struct ext4_extent *ex;
1521 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001522 int depth; /* Note, NOT eh_depth; depth from top of tree */
1523 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001524
Frank Mayhar273df552010-03-02 11:46:09 -05001525 if (unlikely(path == NULL)) {
1526 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001527 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001528 }
Alex Tomas1988b512008-01-28 23:58:27 -05001529 depth = path->p_depth;
1530 *phys = 0;
1531
1532 if (depth == 0 && path->p_ext == NULL)
1533 return 0;
1534
1535 /* usually extent in the path covers blocks smaller
1536 * then *logical, but it can be that extent is the
1537 * first one in the file */
1538
1539 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001540 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001541 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001542 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1543 EXT4_ERROR_INODE(inode,
1544 "first_extent(path[%d].p_hdr) != ex",
1545 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001546 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001547 }
Alex Tomas1988b512008-01-28 23:58:27 -05001548 while (--depth >= 0) {
1549 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001550 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1551 EXT4_ERROR_INODE(inode,
1552 "ix != EXT_FIRST_INDEX *logical %d!",
1553 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001554 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001555 }
Alex Tomas1988b512008-01-28 23:58:27 -05001556 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001557 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001558 }
1559
Frank Mayhar273df552010-03-02 11:46:09 -05001560 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1561 EXT4_ERROR_INODE(inode,
1562 "logical %d < ee_block %d + ee_len %d!",
1563 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001564 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001565 }
Alex Tomas1988b512008-01-28 23:58:27 -05001566
1567 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1568 /* next allocated block in this leaf */
1569 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001570 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001571 }
1572
1573 /* go up and search for index to the right */
1574 while (--depth >= 0) {
1575 ix = path[depth].p_idx;
1576 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001577 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001578 }
1579
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001580 /* we've gone up to the root and found no index to the right */
1581 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001582
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001583got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001584 /* we've found index to the right, let's
1585 * follow it and find the closest allocated
1586 * block to the right */
1587 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001588 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001589 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001590 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001591 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001592 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001593 if (IS_ERR(bh))
1594 return PTR_ERR(bh);
1595 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001596 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001597 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001598 put_bh(bh);
1599 }
1600
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001601 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001602 if (IS_ERR(bh))
1603 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001604 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001605 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001606found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001607 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001608 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001609 *ret_ex = ex;
1610 if (bh)
1611 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001612 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001613}
1614
1615/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001616 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001617 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001618 * NOTE: it considers block number from index entry as
1619 * allocated block. Thus, index entries have to be consistent
1620 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001621 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001622ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001623ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1624{
1625 int depth;
1626
1627 BUG_ON(path == NULL);
1628 depth = path->p_depth;
1629
1630 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001631 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001632
1633 while (depth >= 0) {
1634 if (depth == path->p_depth) {
1635 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001636 if (path[depth].p_ext &&
1637 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001638 EXT_LAST_EXTENT(path[depth].p_hdr))
1639 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1640 } else {
1641 /* index */
1642 if (path[depth].p_idx !=
1643 EXT_LAST_INDEX(path[depth].p_hdr))
1644 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1645 }
1646 depth--;
1647 }
1648
Lukas Czernerf17722f2011-06-06 00:05:17 -04001649 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001650}
1651
1652/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001653 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001654 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001655 */
Robin Dong57187892011-07-23 21:49:07 -04001656static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001657{
1658 int depth;
1659
1660 BUG_ON(path == NULL);
1661 depth = path->p_depth;
1662
1663 /* zero-tree has no leaf blocks at all */
1664 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001665 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001666
1667 /* go to index block */
1668 depth--;
1669
1670 while (depth >= 0) {
1671 if (path[depth].p_idx !=
1672 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001673 return (ext4_lblk_t)
1674 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001675 depth--;
1676 }
1677
Lukas Czernerf17722f2011-06-06 00:05:17 -04001678 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001679}
1680
1681/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001682 * ext4_ext_correct_indexes:
1683 * if leaf gets modified and modified extent is first in the leaf,
1684 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001685 * TODO: do we need to correct tree in all cases?
1686 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001687static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001688 struct ext4_ext_path *path)
1689{
1690 struct ext4_extent_header *eh;
1691 int depth = ext_depth(inode);
1692 struct ext4_extent *ex;
1693 __le32 border;
1694 int k, err = 0;
1695
1696 eh = path[depth].p_hdr;
1697 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001698
1699 if (unlikely(ex == NULL || eh == NULL)) {
1700 EXT4_ERROR_INODE(inode,
1701 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001702 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001703 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001704
1705 if (depth == 0) {
1706 /* there is no tree at all */
1707 return 0;
1708 }
1709
1710 if (ex != EXT_FIRST_EXTENT(eh)) {
1711 /* we correct tree if first leaf got modified only */
1712 return 0;
1713 }
1714
1715 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001716 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001717 */
1718 k = depth - 1;
1719 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001720 err = ext4_ext_get_access(handle, inode, path + k);
1721 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001722 return err;
1723 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001724 err = ext4_ext_dirty(handle, inode, path + k);
1725 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001726 return err;
1727
1728 while (k--) {
1729 /* change all left-side indexes */
1730 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1731 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001732 err = ext4_ext_get_access(handle, inode, path + k);
1733 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001734 break;
1735 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001736 err = ext4_ext_dirty(handle, inode, path + k);
1737 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001738 break;
1739 }
1740
1741 return err;
1742}
1743
Akira Fujita748de672009-06-17 19:24:03 -04001744int
Alex Tomasa86c6182006-10-11 01:21:03 -07001745ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1746 struct ext4_extent *ex2)
1747{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001748 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001749
Lukas Czerner556615d2014-04-20 23:45:47 -04001750 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001751 return 0;
1752
1753 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1754 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1755
1756 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001757 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001758 return 0;
1759
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001760 /*
1761 * To allow future support for preallocated extents to be added
1762 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001763 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001764 */
Eric Sandeenda0169b2013-11-04 09:58:26 -05001765 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001766 return 0;
Jan Kara109811c2016-03-08 23:36:46 -05001767 /*
1768 * The check for IO to unwritten extent is somewhat racy as we
1769 * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after
1770 * dropping i_data_sem. But reserved blocks should save us in that
1771 * case.
1772 */
Lukas Czerner556615d2014-04-20 23:45:47 -04001773 if (ext4_ext_is_unwritten(ex1) &&
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001774 (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
1775 atomic_read(&EXT4_I(inode)->i_unwritten) ||
Lukas Czerner556615d2014-04-20 23:45:47 -04001776 (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001777 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001778#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001779 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001780 return 0;
1781#endif
1782
Theodore Ts'obf89d162010-10-27 21:30:14 -04001783 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001784 return 1;
1785 return 0;
1786}
1787
1788/*
Amit Arora56055d32007-07-17 21:42:38 -04001789 * This function tries to merge the "ex" extent to the next extent in the tree.
1790 * It always tries to merge towards right. If you want to merge towards
1791 * left, pass "ex - 1" as argument instead of "ex".
1792 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1793 * 1 if they got merged.
1794 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001795static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001796 struct ext4_ext_path *path,
1797 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001798{
1799 struct ext4_extent_header *eh;
1800 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001801 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001802
1803 depth = ext_depth(inode);
1804 BUG_ON(path[depth].p_hdr == NULL);
1805 eh = path[depth].p_hdr;
1806
1807 while (ex < EXT_LAST_EXTENT(eh)) {
1808 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1809 break;
1810 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001811 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001812 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1813 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001814 if (unwritten)
1815 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001816
1817 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1818 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1819 * sizeof(struct ext4_extent);
1820 memmove(ex + 1, ex + 2, len);
1821 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001822 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001823 merge_done = 1;
1824 WARN_ON(eh->eh_entries == 0);
1825 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001826 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001827 }
1828
1829 return merge_done;
1830}
1831
1832/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001833 * This function does a very simple check to see if we can collapse
1834 * an extent tree with a single extent tree leaf block into the inode.
1835 */
1836static void ext4_ext_try_to_merge_up(handle_t *handle,
1837 struct inode *inode,
1838 struct ext4_ext_path *path)
1839{
1840 size_t s;
1841 unsigned max_root = ext4_ext_space_root(inode, 0);
1842 ext4_fsblk_t blk;
1843
1844 if ((path[0].p_depth != 1) ||
1845 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1846 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1847 return;
1848
1849 /*
1850 * We need to modify the block allocation bitmap and the block
1851 * group descriptor to release the extent tree block. If we
1852 * can't get the journal credits, give up.
1853 */
1854 if (ext4_journal_extend(handle, 2))
1855 return;
1856
1857 /*
1858 * Copy the extent data up to the inode
1859 */
1860 blk = ext4_idx_pblock(path[0].p_idx);
1861 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1862 sizeof(struct ext4_extent_idx);
1863 s += sizeof(struct ext4_extent_header);
1864
Theodore Ts'o10809df82014-09-01 14:40:09 -04001865 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001866 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1867 path[0].p_depth = 0;
1868 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1869 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1870 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1871
1872 brelse(path[1].p_bh);
1873 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001874 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001875}
1876
1877/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001878 * This function tries to merge the @ex extent to neighbours in the tree.
1879 * return 1 if merge left else 0.
1880 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001881static void ext4_ext_try_to_merge(handle_t *handle,
1882 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001883 struct ext4_ext_path *path,
1884 struct ext4_extent *ex) {
1885 struct ext4_extent_header *eh;
1886 unsigned int depth;
1887 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001888
1889 depth = ext_depth(inode);
1890 BUG_ON(path[depth].p_hdr == NULL);
1891 eh = path[depth].p_hdr;
1892
1893 if (ex > EXT_FIRST_EXTENT(eh))
1894 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1895
1896 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001897 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001898
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001899 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001900}
1901
1902/*
Amit Arora25d14f92007-05-24 13:04:13 -04001903 * check if a portion of the "newext" extent overlaps with an
1904 * existing extent.
1905 *
1906 * If there is an overlap discovered, it updates the length of the newext
1907 * such that there will be no overlap, and then returns 1.
1908 * If there is no overlap found, it returns 0.
1909 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001910static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1911 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001912 struct ext4_extent *newext,
1913 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001914{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001915 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001916 unsigned int depth, len1;
1917 unsigned int ret = 0;
1918
1919 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001920 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001921 depth = ext_depth(inode);
1922 if (!path[depth].p_ext)
1923 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001924 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001925
1926 /*
1927 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001928 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001929 */
1930 if (b2 < b1) {
1931 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001932 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001933 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001934 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001935 }
1936
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001937 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001938 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001939 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001940 newext->ee_len = cpu_to_le16(len1);
1941 ret = 1;
1942 }
1943
1944 /* check for overlap */
1945 if (b1 + len1 > b2) {
1946 newext->ee_len = cpu_to_le16(b2 - b1);
1947 ret = 1;
1948 }
1949out:
1950 return ret;
1951}
1952
1953/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001954 * ext4_ext_insert_extent:
1955 * tries to merge requsted extent into the existing extent or
1956 * inserts requested extent as new one into the tree,
1957 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001958 */
1959int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001960 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001961 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001962{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001963 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001964 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001965 struct ext4_extent *ex, *fex;
1966 struct ext4_extent *nearex; /* nearest extent */
1967 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001968 int depth, len, err;
1969 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001970 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001971
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001972 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1973 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001974 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1975 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001976 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001977 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001978 depth = ext_depth(inode);
1979 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001980 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001981 if (unlikely(path[depth].p_hdr == NULL)) {
1982 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001983 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001984 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001985
1986 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001987 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001988
1989 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001990 * Try to see whether we should rather test the extent on
1991 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001992 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001993 * left, or on the right from the searched position. This
1994 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001995 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001996 if (ex < EXT_LAST_EXTENT(eh) &&
1997 (le32_to_cpu(ex->ee_block) +
1998 ext4_ext_get_actual_len(ex) <
1999 le32_to_cpu(newext->ee_block))) {
2000 ex += 1;
2001 goto prepend;
2002 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
2003 (le32_to_cpu(newext->ee_block) +
2004 ext4_ext_get_actual_len(newext) <
2005 le32_to_cpu(ex->ee_block)))
2006 ex -= 1;
2007
2008 /* Try to append newex to the ex */
2009 if (ext4_can_extents_be_merged(inode, ex, newext)) {
2010 ext_debug("append [%d]%d block to %u:[%d]%d"
2011 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04002012 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002013 ext4_ext_get_actual_len(newext),
2014 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002015 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002016 ext4_ext_get_actual_len(ex),
2017 ext4_ext_pblock(ex));
2018 err = ext4_ext_get_access(handle, inode,
2019 path + depth);
2020 if (err)
2021 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04002022 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002023 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002024 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002025 if (unwritten)
2026 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002027 eh = path[depth].p_hdr;
2028 nearex = ex;
2029 goto merge;
2030 }
2031
2032prepend:
2033 /* Try to prepend newex to the ex */
2034 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2035 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2036 "(from %llu)\n",
2037 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002038 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002039 ext4_ext_get_actual_len(newext),
2040 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002041 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002042 ext4_ext_get_actual_len(ex),
2043 ext4_ext_pblock(ex));
2044 err = ext4_ext_get_access(handle, inode,
2045 path + depth);
2046 if (err)
2047 return err;
2048
Lukas Czerner556615d2014-04-20 23:45:47 -04002049 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002050 ex->ee_block = newext->ee_block;
2051 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2052 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2053 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002054 if (unwritten)
2055 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002056 eh = path[depth].p_hdr;
2057 nearex = ex;
2058 goto merge;
2059 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002060 }
2061
Alex Tomasa86c6182006-10-11 01:21:03 -07002062 depth = ext_depth(inode);
2063 eh = path[depth].p_hdr;
2064 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2065 goto has_space;
2066
2067 /* probably next leaf has space for us? */
2068 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002069 next = EXT_MAX_BLOCKS;
2070 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002071 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002072 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002073 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002074 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002075 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002076 if (IS_ERR(npath))
2077 return PTR_ERR(npath);
2078 BUG_ON(npath->p_depth != path->p_depth);
2079 eh = npath[depth].p_hdr;
2080 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002081 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002082 le16_to_cpu(eh->eh_entries));
2083 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002084 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002085 }
2086 ext_debug("next leaf has no free space(%d,%d)\n",
2087 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2088 }
2089
2090 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002091 * There is no free space in the found leaf.
2092 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002093 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002094 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002095 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002096 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002097 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002098 if (err)
2099 goto cleanup;
2100 depth = ext_depth(inode);
2101 eh = path[depth].p_hdr;
2102
2103has_space:
2104 nearex = path[depth].p_ext;
2105
Avantika Mathur7e028972006-12-06 20:41:33 -08002106 err = ext4_ext_get_access(handle, inode, path + depth);
2107 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002108 goto cleanup;
2109
2110 if (!nearex) {
2111 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002112 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002113 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002114 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002115 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002116 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002117 nearex = EXT_FIRST_EXTENT(eh);
2118 } else {
2119 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002120 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002121 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002122 ext_debug("insert %u:%llu:[%d]%d before: "
2123 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002124 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002125 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002126 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002127 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002128 nearex);
2129 nearex++;
2130 } else {
2131 /* Insert before */
2132 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002133 ext_debug("insert %u:%llu:[%d]%d after: "
2134 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002135 le32_to_cpu(newext->ee_block),
2136 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002137 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002138 ext4_ext_get_actual_len(newext),
2139 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002140 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002141 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2142 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002143 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002144 "move %d extents from 0x%p to 0x%p\n",
2145 le32_to_cpu(newext->ee_block),
2146 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002147 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002148 ext4_ext_get_actual_len(newext),
2149 len, nearex, nearex + 1);
2150 memmove(nearex + 1, nearex,
2151 len * sizeof(struct ext4_extent));
2152 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002153 }
2154
Marcin Slusarze8546d02008-04-17 10:38:59 -04002155 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002156 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002157 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002158 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002159 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002160
2161merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002162 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002163 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002164 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002165
Alex Tomasa86c6182006-10-11 01:21:03 -07002166
2167 /* time to correct all indexes above */
2168 err = ext4_ext_correct_indexes(handle, inode, path);
2169 if (err)
2170 goto cleanup;
2171
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002172 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002173
2174cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002175 ext4_ext_drop_refs(npath);
2176 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002177 return err;
2178}
2179
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002180static int ext4_fill_fiemap_extents(struct inode *inode,
2181 ext4_lblk_t block, ext4_lblk_t num,
2182 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002183{
2184 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002185 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002186 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002187 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002188 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002189 int exists, depth = 0, err = 0;
2190 unsigned int flags = 0;
2191 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002192
Lukas Czernerf17722f2011-06-06 00:05:17 -04002193 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002194 num = last - block;
2195 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002196 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002197
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002198 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002199 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002200 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002201 err = PTR_ERR(path);
2202 path = NULL;
2203 break;
2204 }
2205
2206 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002207 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002208 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002209 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002210 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002211 break;
2212 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002213 ex = path[depth].p_ext;
2214 next = ext4_ext_next_allocated_block(path);
2215
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002216 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002217 exists = 0;
2218 if (!ex) {
2219 /* there is no extent yet, so try to allocate
2220 * all requested space */
2221 start = block;
2222 end = block + num;
2223 } else if (le32_to_cpu(ex->ee_block) > block) {
2224 /* need to allocate space before found extent */
2225 start = block;
2226 end = le32_to_cpu(ex->ee_block);
2227 if (block + num < end)
2228 end = block + num;
2229 } else if (block >= le32_to_cpu(ex->ee_block)
2230 + ext4_ext_get_actual_len(ex)) {
2231 /* need to allocate space after found extent */
2232 start = block;
2233 end = block + num;
2234 if (end >= next)
2235 end = next;
2236 } else if (block >= le32_to_cpu(ex->ee_block)) {
2237 /*
2238 * some part of requested space is covered
2239 * by found extent
2240 */
2241 start = block;
2242 end = le32_to_cpu(ex->ee_block)
2243 + ext4_ext_get_actual_len(ex);
2244 if (block + num < end)
2245 end = block + num;
2246 exists = 1;
2247 } else {
2248 BUG();
2249 }
2250 BUG_ON(end <= start);
2251
2252 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002253 es.es_lblk = start;
2254 es.es_len = end - start;
2255 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002256 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002257 es.es_lblk = le32_to_cpu(ex->ee_block);
2258 es.es_len = ext4_ext_get_actual_len(ex);
2259 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002260 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002261 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002262 }
2263
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002264 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002265 * Find delayed extent and update es accordingly. We call
2266 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002267 * last existing extent or not.
2268 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002269 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002270 if (!exists && next_del) {
2271 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002272 flags |= (FIEMAP_EXTENT_DELALLOC |
2273 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002274 }
2275 up_read(&EXT4_I(inode)->i_data_sem);
2276
Zheng Liu69eb33d2013-02-18 00:31:07 -05002277 if (unlikely(es.es_len == 0)) {
2278 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002279 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002280 break;
2281 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002282
Zheng Liuf7fec032013-02-18 00:28:47 -05002283 /*
2284 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2285 * we need to check next == EXT_MAX_BLOCKS because it is
2286 * possible that an extent is with unwritten and delayed
2287 * status due to when an extent is delayed allocated and
2288 * is allocated by fallocate status tree will track both of
2289 * them in a extent.
2290 *
2291 * So we could return a unwritten and delayed extent, and
2292 * its block is equal to 'next'.
2293 */
2294 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002295 flags |= FIEMAP_EXTENT_LAST;
2296 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2297 next != EXT_MAX_BLOCKS)) {
2298 EXT4_ERROR_INODE(inode,
2299 "next extent == %u, next "
2300 "delalloc extent = %u",
2301 next, next_del);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002302 err = -EFSCORRUPTED;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002303 break;
2304 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002305 }
2306
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002307 if (exists) {
2308 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002309 (__u64)es.es_lblk << blksize_bits,
2310 (__u64)es.es_pblk << blksize_bits,
2311 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002312 flags);
2313 if (err < 0)
2314 break;
2315 if (err == 1) {
2316 err = 0;
2317 break;
2318 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002319 }
2320
Zheng Liu69eb33d2013-02-18 00:31:07 -05002321 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002322 }
2323
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002324 ext4_ext_drop_refs(path);
2325 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002326 return err;
2327}
2328
Theodore Ts'obb5835e2019-08-11 16:32:41 -04002329static int ext4_fill_es_cache_info(struct inode *inode,
2330 ext4_lblk_t block, ext4_lblk_t num,
2331 struct fiemap_extent_info *fieinfo)
2332{
2333 ext4_lblk_t next, end = block + num - 1;
2334 struct extent_status es;
2335 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2336 unsigned int flags;
2337 int err;
2338
2339 while (block <= end) {
2340 next = 0;
2341 flags = 0;
2342 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2343 break;
2344 if (ext4_es_is_unwritten(&es))
2345 flags |= FIEMAP_EXTENT_UNWRITTEN;
2346 if (ext4_es_is_delayed(&es))
2347 flags |= (FIEMAP_EXTENT_DELALLOC |
2348 FIEMAP_EXTENT_UNKNOWN);
2349 if (ext4_es_is_hole(&es))
2350 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2351 if (next == 0)
2352 flags |= FIEMAP_EXTENT_LAST;
2353 if (flags & (FIEMAP_EXTENT_DELALLOC|
2354 EXT4_FIEMAP_EXTENT_HOLE))
2355 es.es_pblk = 0;
2356 else
2357 es.es_pblk = ext4_es_pblock(&es);
2358 err = fiemap_fill_next_extent(fieinfo,
2359 (__u64)es.es_lblk << blksize_bits,
2360 (__u64)es.es_pblk << blksize_bits,
2361 (__u64)es.es_len << blksize_bits,
2362 flags);
2363 if (next == 0)
2364 break;
2365 block = next;
2366 if (err < 0)
2367 return err;
2368 if (err == 1)
2369 return 0;
2370 }
2371 return 0;
2372}
2373
2374
Alex Tomasa86c6182006-10-11 01:21:03 -07002375/*
Jan Kara140a5252016-03-09 22:46:57 -05002376 * ext4_ext_determine_hole - determine hole around given block
2377 * @inode: inode we lookup in
2378 * @path: path in extent tree to @lblk
2379 * @lblk: pointer to logical block around which we want to determine hole
2380 *
2381 * Determine hole length (and start if easily possible) around given logical
2382 * block. We don't try too hard to find the beginning of the hole but @path
2383 * actually points to extent before @lblk, we provide it.
2384 *
2385 * The function returns the length of a hole starting at @lblk. We update @lblk
2386 * to the beginning of the hole if we managed to find it.
2387 */
2388static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2389 struct ext4_ext_path *path,
2390 ext4_lblk_t *lblk)
2391{
2392 int depth = ext_depth(inode);
2393 struct ext4_extent *ex;
2394 ext4_lblk_t len;
2395
2396 ex = path[depth].p_ext;
2397 if (ex == NULL) {
2398 /* there is no extent yet, so gap is [0;-] */
2399 *lblk = 0;
2400 len = EXT_MAX_BLOCKS;
2401 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2402 len = le32_to_cpu(ex->ee_block) - *lblk;
2403 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2404 + ext4_ext_get_actual_len(ex)) {
2405 ext4_lblk_t next;
2406
2407 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2408 next = ext4_ext_next_allocated_block(path);
2409 BUG_ON(next == *lblk);
2410 len = next - *lblk;
2411 } else {
2412 BUG();
2413 }
2414 return len;
2415}
2416
2417/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002418 * ext4_ext_put_gap_in_cache:
2419 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002420 * and cache this gap
2421 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002422static void
Jan Kara140a5252016-03-09 22:46:57 -05002423ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2424 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002425{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002426 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002427
Eric Whitneyad431022018-10-01 14:10:39 -04002428 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2429 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002430 if (es.es_len) {
2431 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002432 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002433 return;
Jan Kara140a5252016-03-09 22:46:57 -05002434 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002435 }
Jan Kara140a5252016-03-09 22:46:57 -05002436 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2437 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2438 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002439}
2440
2441/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002442 * ext4_ext_rm_idx:
2443 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002444 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002445static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002446 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002447{
Alex Tomasa86c6182006-10-11 01:21:03 -07002448 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002449 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002450
2451 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002452 depth--;
2453 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002454 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002455 if (unlikely(path->p_hdr->eh_entries == 0)) {
2456 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002457 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002458 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002459 err = ext4_ext_get_access(handle, inode, path);
2460 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002461 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002462
2463 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2464 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2465 len *= sizeof(struct ext4_extent_idx);
2466 memmove(path->p_idx, path->p_idx + 1, len);
2467 }
2468
Marcin Slusarze8546d02008-04-17 10:38:59 -04002469 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002470 err = ext4_ext_dirty(handle, inode, path);
2471 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002472 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002473 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002474 trace_ext4_ext_rm_idx(inode, leaf);
2475
Peter Huewe7dc57612011-02-21 21:01:42 -05002476 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002477 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002478
2479 while (--depth >= 0) {
2480 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2481 break;
2482 path--;
2483 err = ext4_ext_get_access(handle, inode, path);
2484 if (err)
2485 break;
2486 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2487 err = ext4_ext_dirty(handle, inode, path);
2488 if (err)
2489 break;
2490 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002491 return err;
2492}
2493
2494/*
Mingming Caoee12b632008-08-19 22:16:05 -04002495 * ext4_ext_calc_credits_for_single_extent:
2496 * This routine returns max. credits that needed to insert an extent
2497 * to the extent tree.
2498 * When pass the actual path, the caller should calculate credits
2499 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002500 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002501int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002502 struct ext4_ext_path *path)
2503{
Alex Tomasa86c6182006-10-11 01:21:03 -07002504 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002505 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002506 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002507
Alex Tomasa86c6182006-10-11 01:21:03 -07002508 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002509 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002510 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2511
2512 /*
2513 * There are some space in the leaf tree, no
2514 * need to account for leaf block credit
2515 *
2516 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002517 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002518 * accounted.
2519 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002520 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002521 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002522 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002523 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002524 }
2525
Mingming Cao525f4ed2008-08-19 22:15:58 -04002526 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002527}
Alex Tomasa86c6182006-10-11 01:21:03 -07002528
Mingming Caoee12b632008-08-19 22:16:05 -04002529/*
Jan Karafffb2732013-06-04 13:01:11 -04002530 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002531 *
Jan Karafffb2732013-06-04 13:01:11 -04002532 * If we add a single extent, then in the worse case, each tree level
2533 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002534 *
Jan Karafffb2732013-06-04 13:01:11 -04002535 * If more extents are inserted, they could cause the whole tree split more
2536 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002537 */
Jan Karafffb2732013-06-04 13:01:11 -04002538int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002539{
2540 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002541 int depth;
2542
2543 /* If we are converting the inline data, only one is needed here. */
2544 if (ext4_has_inline_data(inode))
2545 return 1;
2546
2547 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002548
Jan Karafffb2732013-06-04 13:01:11 -04002549 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002550 index = depth * 2;
2551 else
2552 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002553
Mingming Caoee12b632008-08-19 22:16:05 -04002554 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002555}
2556
Theodore Ts'o981250c2013-06-12 11:48:29 -04002557static inline int get_default_free_blocks_flags(struct inode *inode)
2558{
Tahsin Erdoganddfa17e2017-06-21 21:36:51 -04002559 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2560 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
Theodore Ts'o981250c2013-06-12 11:48:29 -04002561 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2562 else if (ext4_should_journal_data(inode))
2563 return EXT4_FREE_BLOCKS_FORGET;
2564 return 0;
2565}
2566
Eric Whitney9fe67142018-10-01 14:25:08 -04002567/*
2568 * ext4_rereserve_cluster - increment the reserved cluster count when
2569 * freeing a cluster with a pending reservation
2570 *
2571 * @inode - file containing the cluster
2572 * @lblk - logical block in cluster to be reserved
2573 *
2574 * Increments the reserved cluster count and adjusts quota in a bigalloc
2575 * file system when freeing a partial cluster containing at least one
2576 * delayed and unwritten block. A partial cluster meeting that
2577 * requirement will have a pending reservation. If so, the
2578 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2579 * defer reserved and allocated space accounting to a subsequent call
2580 * to this function.
2581 */
2582static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2583{
2584 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2585 struct ext4_inode_info *ei = EXT4_I(inode);
2586
2587 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2588
2589 spin_lock(&ei->i_block_reservation_lock);
2590 ei->i_reserved_data_blocks++;
2591 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2592 spin_unlock(&ei->i_block_reservation_lock);
2593
2594 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2595 ext4_remove_pending(inode, lblk);
2596}
2597
Alex Tomasa86c6182006-10-11 01:21:03 -07002598static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002599 struct ext4_extent *ex,
Eric Whitney9fe67142018-10-01 14:25:08 -04002600 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002601 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002602{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002603 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002604 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002605 ext4_fsblk_t last_pblk, pblk;
2606 ext4_lblk_t num;
2607 int flags;
2608
2609 /* only extent tail removal is allowed */
2610 if (from < le32_to_cpu(ex->ee_block) ||
2611 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2612 ext4_error(sbi->s_sb,
2613 "strange request: removal(2) %u-%u from %u:%u",
2614 from, to, le32_to_cpu(ex->ee_block), ee_len);
2615 return 0;
2616 }
2617
2618#ifdef EXTENTS_STATS
2619 spin_lock(&sbi->s_ext_stats_lock);
2620 sbi->s_ext_blocks += ee_len;
2621 sbi->s_ext_extents++;
2622 if (ee_len < sbi->s_ext_min)
2623 sbi->s_ext_min = ee_len;
2624 if (ee_len > sbi->s_ext_max)
2625 sbi->s_ext_max = ee_len;
2626 if (ext_depth(inode) > sbi->s_depth_max)
2627 sbi->s_depth_max = ext_depth(inode);
2628 spin_unlock(&sbi->s_ext_stats_lock);
2629#endif
2630
2631 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2632
2633 /*
2634 * if we have a partial cluster, and it's different from the
2635 * cluster of the last block in the extent, we free it
2636 */
2637 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2638
2639 if (partial->state != initial &&
2640 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2641 if (partial->state == tofree) {
2642 flags = get_default_free_blocks_flags(inode);
2643 if (ext4_is_pending(inode, partial->lblk))
2644 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2645 ext4_free_blocks(handle, inode, NULL,
2646 EXT4_C2B(sbi, partial->pclu),
2647 sbi->s_cluster_ratio, flags);
2648 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2649 ext4_rereserve_cluster(inode, partial->lblk);
2650 }
2651 partial->state = initial;
2652 }
2653
2654 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2655 pblk = ext4_ext_pblock(ex) + ee_len - num;
2656
2657 /*
2658 * We free the partial cluster at the end of the extent (if any),
2659 * unless the cluster is used by another extent (partial_cluster
2660 * state is nofree). If a partial cluster exists here, it must be
2661 * shared with the last block in the extent.
2662 */
2663 flags = get_default_free_blocks_flags(inode);
2664
2665 /* partial, left end cluster aligned, right end unaligned */
2666 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2667 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2668 (partial->state != nofree)) {
2669 if (ext4_is_pending(inode, to))
2670 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2671 ext4_free_blocks(handle, inode, NULL,
2672 EXT4_PBLK_CMASK(sbi, last_pblk),
2673 sbi->s_cluster_ratio, flags);
2674 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2675 ext4_rereserve_cluster(inode, to);
2676 partial->state = initial;
2677 flags = get_default_free_blocks_flags(inode);
2678 }
2679
2680 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002681
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002682 /*
2683 * For bigalloc file systems, we never free a partial cluster
Eric Whitney9fe67142018-10-01 14:25:08 -04002684 * at the beginning of the extent. Instead, we check to see if we
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002685 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002686 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002687 */
2688 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
Eric Whitney9fe67142018-10-01 14:25:08 -04002689 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002690
Eric Whitney9fe67142018-10-01 14:25:08 -04002691 /* reset the partial cluster if we've freed past it */
2692 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2693 partial->state = initial;
2694
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002695 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04002696 * If we've freed the entire extent but the beginning is not left
2697 * cluster aligned and is not marked as ineligible for freeing we
2698 * record the partial cluster at the beginning of the extent. It
2699 * wasn't freed by the preceding ext4_free_blocks() call, and we
2700 * need to look farther to the left to determine if it's to be freed
2701 * (not shared with another extent). Else, reset the partial
2702 * cluster - we're either done freeing or the beginning of the
2703 * extent is left cluster aligned.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002704 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002705 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2706 if (partial->state == initial) {
2707 partial->pclu = EXT4_B2C(sbi, pblk);
2708 partial->lblk = from;
2709 partial->state = tofree;
Eric Whitney345ee942014-11-23 00:59:39 -05002710 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002711 } else {
2712 partial->state = initial;
2713 }
2714
Alex Tomasa86c6182006-10-11 01:21:03 -07002715 return 0;
2716}
2717
Allison Hendersond583fb82011-05-25 07:41:43 -04002718/*
2719 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002720 * blocks appearing between "start" and "end". Both "start"
2721 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002722 *
2723 * @handle: The journal handle
2724 * @inode: The files inode
2725 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002726 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002727 * has been released from it. However, if this value is
2728 * negative, it's a cluster just to the right of the
2729 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002730 * @start: The first block to remove
2731 * @end: The last block to remove
2732 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002733static int
2734ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002735 struct ext4_ext_path *path,
Eric Whitney9fe67142018-10-01 14:25:08 -04002736 struct partial_cluster *partial,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002737 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002738{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002739 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002740 int err = 0, correct_index = 0;
2741 int depth = ext_depth(inode), credits;
2742 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002743 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002744 unsigned num;
2745 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002746 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002747 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002748 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002749 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002750
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002751 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002752 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002753 if (!path[depth].p_hdr)
2754 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2755 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002756 if (unlikely(path[depth].p_hdr == NULL)) {
2757 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002758 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002759 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002760 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002761 ex = path[depth].p_ext;
2762 if (!ex)
2763 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002764
2765 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002766 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002767
Eric Whitney9fe67142018-10-01 14:25:08 -04002768 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
Aditya Kalid8990242011-09-09 19:18:51 -04002769
Alex Tomasa86c6182006-10-11 01:21:03 -07002770 while (ex >= EXT_FIRST_EXTENT(eh) &&
2771 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002772
Lukas Czerner556615d2014-04-20 23:45:47 -04002773 if (ext4_ext_is_unwritten(ex))
2774 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002775 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002776 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002777
Mingming553f9002009-09-18 13:34:55 -04002778 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002779 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002780 path[depth].p_ext = ex;
2781
2782 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002783 b = ex_ee_block+ex_ee_len - 1 < end ?
2784 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002785
2786 ext_debug(" border %u:%u\n", a, b);
2787
Allison Hendersond583fb82011-05-25 07:41:43 -04002788 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002789 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002790 /*
2791 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002792 * so note that its first cluster is in use to avoid
2793 * freeing it when removing blocks. Eventually, the
2794 * right edge of the truncated/punched region will
2795 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002796 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002797 if (sbi->s_cluster_ratio > 1) {
2798 pblk = ext4_ext_pblock(ex);
Eric Whitney9fe67142018-10-01 14:25:08 -04002799 partial->pclu = EXT4_B2C(sbi, pblk);
2800 partial->state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002801 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002802 ex--;
2803 ex_ee_block = le32_to_cpu(ex->ee_block);
2804 ex_ee_len = ext4_ext_get_actual_len(ex);
2805 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002806 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002807 EXT4_ERROR_INODE(inode,
2808 "can not handle truncate %u:%u "
2809 "on extent %u:%u",
2810 start, end, ex_ee_block,
2811 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002812 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002813 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002814 } else if (a != ex_ee_block) {
2815 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002816 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002817 } else {
2818 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002819 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002820 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002821 /*
2822 * 3 for leaf, sb, and inode plus 2 (bmap and group
2823 * descriptor) for each block group; assume two block
2824 * groups plus ex_ee_len/blocks_per_block_group for
2825 * the worst case
2826 */
2827 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002828 if (ex == EXT_FIRST_EXTENT(eh)) {
2829 correct_index = 1;
2830 credits += (ext_depth(inode)) + 1;
2831 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002832 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002833
Jan Karaa4130362019-11-05 17:44:16 +01002834 err = ext4_datasem_ensure_credits(handle, inode, credits,
2835 credits);
2836 if (err) {
2837 if (err > 0)
2838 err = -EAGAIN;
Alex Tomasa86c6182006-10-11 01:21:03 -07002839 goto out;
Jan Karaa4130362019-11-05 17:44:16 +01002840 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002841
2842 err = ext4_ext_get_access(handle, inode, path + depth);
2843 if (err)
2844 goto out;
2845
Eric Whitney9fe67142018-10-01 14:25:08 -04002846 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002847 if (err)
2848 goto out;
2849
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002850 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002851 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002852 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002853
Alex Tomasa86c6182006-10-11 01:21:03 -07002854 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002855 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002856 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002857 * extent have been removed.
2858 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002859 if (unwritten && num)
2860 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002861 /*
2862 * If the extent was completely released,
2863 * we need to remove it from the leaf
2864 */
2865 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002866 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002867 /*
2868 * For hole punching, we need to scoot all the
2869 * extents up when an extent is removed so that
2870 * we dont have blank extents in the middle
2871 */
2872 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2873 sizeof(struct ext4_extent));
2874
2875 /* Now get rid of the one at the end */
2876 memset(EXT_LAST_EXTENT(eh), 0,
2877 sizeof(struct ext4_extent));
2878 }
2879 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002880 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002881
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002882 err = ext4_ext_dirty(handle, inode, path + depth);
2883 if (err)
2884 goto out;
2885
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002886 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002887 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002888 ex--;
2889 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002890 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002891 }
2892
2893 if (correct_index && eh->eh_entries)
2894 err = ext4_ext_correct_indexes(handle, inode, path);
2895
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002896 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002897 * If there's a partial cluster and at least one extent remains in
2898 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002899 * current extent. If it is shared with the current extent
Eric Whitney9fe67142018-10-01 14:25:08 -04002900 * we reset the partial cluster because we've reached the start of the
Eric Whitney5bf43762014-11-23 00:58:11 -05002901 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002902 */
Eric Whitney9fe67142018-10-01 14:25:08 -04002903 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
Eric Whitney5bf43762014-11-23 00:58:11 -05002904 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
Eric Whitney9fe67142018-10-01 14:25:08 -04002905 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2906 int flags = get_default_free_blocks_flags(inode);
2907
2908 if (ext4_is_pending(inode, partial->lblk))
2909 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Eric Whitney5bf43762014-11-23 00:58:11 -05002910 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04002911 EXT4_C2B(sbi, partial->pclu),
2912 sbi->s_cluster_ratio, flags);
2913 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2914 ext4_rereserve_cluster(inode, partial->lblk);
Eric Whitney5bf43762014-11-23 00:58:11 -05002915 }
Eric Whitney9fe67142018-10-01 14:25:08 -04002916 partial->state = initial;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002917 }
2918
Alex Tomasa86c6182006-10-11 01:21:03 -07002919 /* if this leaf is free, then we should
2920 * remove it from index block above */
2921 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002922 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002923
2924out:
2925 return err;
2926}
2927
2928/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002929 * ext4_ext_more_to_rm:
2930 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002931 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002932static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002933ext4_ext_more_to_rm(struct ext4_ext_path *path)
2934{
2935 BUG_ON(path->p_idx == NULL);
2936
2937 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2938 return 0;
2939
2940 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002941 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002942 * so we have to consider current index for truncation
2943 */
2944 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2945 return 0;
2946 return 1;
2947}
2948
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002949int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2950 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002951{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002952 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002953 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002954 struct ext4_ext_path *path = NULL;
Eric Whitney9fe67142018-10-01 14:25:08 -04002955 struct partial_cluster partial;
Alex Tomasa86c6182006-10-11 01:21:03 -07002956 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002957 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002958
Eric Whitney9fe67142018-10-01 14:25:08 -04002959 partial.pclu = 0;
2960 partial.lblk = 0;
2961 partial.state = initial;
2962
Lukas Czerner5f95d212012-03-19 23:03:19 -04002963 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002964
2965 /* probably first extent we're gonna free will be last in block */
Theodore Ts'o9924a922013-02-08 21:59:22 -05002966 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002967 if (IS_ERR(handle))
2968 return PTR_ERR(handle);
2969
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002970again:
Lukas Czerner61801322013-05-27 23:32:35 -04002971 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002972
Alex Tomasa86c6182006-10-11 01:21:03 -07002973 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002974 * Check if we are removing extents inside the extent tree. If that
2975 * is the case, we are going to punch a hole inside the extent tree
2976 * so we have to check whether we need to split the extent covering
2977 * the last block to remove so we can easily remove the part of it
2978 * in ext4_ext_rm_leaf().
2979 */
2980 if (end < EXT_MAX_BLOCKS - 1) {
2981 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002982 ext4_lblk_t ee_block, ex_end, lblk;
2983 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002984
Eric Whitneyf4226d92014-11-23 00:55:42 -05002985 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002986 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002987 if (IS_ERR(path)) {
2988 ext4_journal_stop(handle);
2989 return PTR_ERR(path);
2990 }
2991 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002992 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002993 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002994 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002995 if (depth) {
2996 EXT4_ERROR_INODE(inode,
2997 "path[%d].p_hdr == NULL",
2998 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002999 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04003000 }
3001 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003002 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003003
3004 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05003005 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003006
3007 /*
3008 * See if the last block is inside the extent, if so split
3009 * the extent at 'end' block so we can easily remove the
3010 * tail of the first part of the split extent in
3011 * ext4_ext_rm_leaf().
3012 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05003013 if (end >= ee_block && end < ex_end) {
3014
3015 /*
3016 * If we're going to split the extent, note that
3017 * the cluster containing the block after 'end' is
3018 * in use to avoid freeing it when removing blocks.
3019 */
3020 if (sbi->s_cluster_ratio > 1) {
3021 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
Eric Whitney9fe67142018-10-01 14:25:08 -04003022 partial.pclu = EXT4_B2C(sbi, pblk);
3023 partial.state = nofree;
Eric Whitneyf4226d92014-11-23 00:55:42 -05003024 }
3025
Lukas Czerner5f95d212012-03-19 23:03:19 -04003026 /*
3027 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04003028 * block in the first new extent. Also we should not
3029 * fail removing space due to ENOSPC so try to use
3030 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04003031 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04003032 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04003033 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04003034 if (err < 0)
3035 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05003036
Eric Whitney7bd75232019-02-28 23:34:11 -05003037 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
3038 partial.state == initial) {
Eric Whitneyf4226d92014-11-23 00:55:42 -05003039 /*
Eric Whitney7bd75232019-02-28 23:34:11 -05003040 * If we're punching, there's an extent to the right.
3041 * If the partial cluster hasn't been set, set it to
3042 * that extent's first cluster and its state to nofree
3043 * so it won't be freed should it contain blocks to be
3044 * removed. If it's already set (tofree/nofree), we're
3045 * retrying and keep the original partial cluster info
3046 * so a cluster marked tofree as a result of earlier
3047 * extent removal is not lost.
Eric Whitneyf4226d92014-11-23 00:55:42 -05003048 */
3049 lblk = ex_end + 1;
3050 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
3051 &ex);
3052 if (err)
3053 goto out;
Eric Whitney9fe67142018-10-01 14:25:08 -04003054 if (pblk) {
3055 partial.pclu = EXT4_B2C(sbi, pblk);
3056 partial.state = nofree;
3057 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003058 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003059 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04003060 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003061 * We start scanning from right side, freeing all the blocks
3062 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07003063 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04003064 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04003065 if (path) {
3066 int k = i = depth;
3067 while (--k > 0)
3068 path[k].p_block =
3069 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
3070 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003071 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
Ashish Sangwan968dee72012-07-22 22:49:08 -04003072 GFP_NOFS);
3073 if (path == NULL) {
3074 ext4_journal_stop(handle);
3075 return -ENOMEM;
3076 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04003077 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003078 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04003079 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04003080
Theodore Ts'oc3491792013-08-16 21:21:41 -04003081 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003082 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04003083 goto out;
3084 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003085 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04003086 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07003087
3088 while (i >= 0 && err == 0) {
3089 if (i == depth) {
3090 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04003091 err = ext4_ext_rm_leaf(handle, inode, path,
Eric Whitney9fe67142018-10-01 14:25:08 -04003092 &partial, start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003093 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003094 brelse(path[i].p_bh);
3095 path[i].p_bh = NULL;
3096 i--;
3097 continue;
3098 }
3099
3100 /* this is index block */
3101 if (!path[i].p_hdr) {
3102 ext_debug("initialize header\n");
3103 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003104 }
3105
Alex Tomasa86c6182006-10-11 01:21:03 -07003106 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003107 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07003108 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
3109 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
3110 ext_debug("init index ptr: hdr 0x%p, num %d\n",
3111 path[i].p_hdr,
3112 le16_to_cpu(path[i].p_hdr->eh_entries));
3113 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003114 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07003115 path[i].p_idx--;
3116 }
3117
3118 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3119 i, EXT_FIRST_INDEX(path[i].p_hdr),
3120 path[i].p_idx);
3121 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003122 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003123 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07003124 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04003125 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07003126 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003127 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04003128 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
3129 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003130 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003131 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003132 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003133 break;
3134 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04003135 /* Yield here to deal with large extent trees.
3136 * Should be a no-op if we did IO above. */
3137 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003138 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003139 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003140 break;
3141 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003142 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003143
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003144 /* save actual number of indexes since this
3145 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07003146 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3147 i++;
3148 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003149 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07003150 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003151 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07003152 * handle must be already prepared by the
3153 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05003154 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07003155 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003156 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003157 brelse(path[i].p_bh);
3158 path[i].p_bh = NULL;
3159 i--;
3160 ext_debug("return to level %d\n", i);
3161 }
3162 }
3163
Eric Whitney9fe67142018-10-01 14:25:08 -04003164 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
3165 path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003166
Eric Whitney0756b902014-11-23 00:59:39 -05003167 /*
Eric Whitney9fe67142018-10-01 14:25:08 -04003168 * if there's a partial cluster and we have removed the first extent
3169 * in the file, then we also free the partial cluster, if any
Eric Whitney0756b902014-11-23 00:59:39 -05003170 */
Eric Whitney9fe67142018-10-01 14:25:08 -04003171 if (partial.state == tofree && err == 0) {
3172 int flags = get_default_free_blocks_flags(inode);
3173
3174 if (ext4_is_pending(inode, partial.lblk))
3175 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003176 ext4_free_blocks(handle, inode, NULL,
Eric Whitney9fe67142018-10-01 14:25:08 -04003177 EXT4_C2B(sbi, partial.pclu),
3178 sbi->s_cluster_ratio, flags);
3179 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3180 ext4_rereserve_cluster(inode, partial.lblk);
3181 partial.state = initial;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003182 }
3183
Alex Tomasa86c6182006-10-11 01:21:03 -07003184 /* TODO: flexible tree reduction should be here */
3185 if (path->p_hdr->eh_entries == 0) {
3186 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003187 * truncate to zero freed all the tree,
3188 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003189 */
3190 err = ext4_ext_get_access(handle, inode, path);
3191 if (err == 0) {
3192 ext_inode_hdr(inode)->eh_depth = 0;
3193 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003194 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003195 err = ext4_ext_dirty(handle, inode, path);
3196 }
3197 }
3198out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003199 ext4_ext_drop_refs(path);
3200 kfree(path);
3201 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003202 if (err == -EAGAIN)
3203 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003204 ext4_journal_stop(handle);
3205
3206 return err;
3207}
3208
3209/*
3210 * called at mount time
3211 */
3212void ext4_ext_init(struct super_block *sb)
3213{
3214 /*
3215 * possible initialization would be here
3216 */
3217
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003218 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003219#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003220 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003221#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003222 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003223#endif
3224#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003225 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003226#endif
3227#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003228 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003229#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003230 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003231#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003232#ifdef EXTENTS_STATS
3233 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3234 EXT4_SB(sb)->s_ext_min = 1 << 30;
3235 EXT4_SB(sb)->s_ext_max = 0;
3236#endif
3237 }
3238}
3239
3240/*
3241 * called at umount time
3242 */
3243void ext4_ext_release(struct super_block *sb)
3244{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003245 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003246 return;
3247
3248#ifdef EXTENTS_STATS
3249 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3250 struct ext4_sb_info *sbi = EXT4_SB(sb);
3251 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3252 sbi->s_ext_blocks, sbi->s_ext_extents,
3253 sbi->s_ext_blocks / sbi->s_ext_extents);
3254 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3255 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3256 }
3257#endif
3258}
3259
Zheng Liud7b2a002013-08-28 14:47:06 -04003260static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3261{
3262 ext4_lblk_t ee_block;
3263 ext4_fsblk_t ee_pblock;
3264 unsigned int ee_len;
3265
3266 ee_block = le32_to_cpu(ex->ee_block);
3267 ee_len = ext4_ext_get_actual_len(ex);
3268 ee_pblock = ext4_ext_pblock(ex);
3269
3270 if (ee_len == 0)
3271 return 0;
3272
3273 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3274 EXTENT_STATUS_WRITTEN);
3275}
3276
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003277/* FIXME!! we need to try to merge to left or right after zero-out */
3278static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3279{
Lukas Czerner24075182010-10-27 21:30:06 -04003280 ext4_fsblk_t ee_pblock;
3281 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003282
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003283 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003284 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003285 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3286 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003287}
3288
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003289/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003290 * ext4_split_extent_at() splits an extent at given block.
3291 *
3292 * @handle: the journal handle
3293 * @inode: the file inode
3294 * @path: the path to the extent
3295 * @split: the logical block where the extent is splitted.
3296 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003297 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003298 * @flags: flags used to insert new extent to extent tree.
3299 *
3300 *
3301 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3302 * of which are deterimined by split_flag.
3303 *
3304 * There are two cases:
3305 * a> the extent are splitted into two extent.
3306 * b> split is not needed, and just mark the extent.
3307 *
3308 * return 0 on success.
3309 */
3310static int ext4_split_extent_at(handle_t *handle,
3311 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003312 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003313 ext4_lblk_t split,
3314 int split_flag,
3315 int flags)
3316{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003317 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003318 ext4_fsblk_t newblock;
3319 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003320 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003321 struct ext4_extent *ex2 = NULL;
3322 unsigned int ee_len, depth;
3323 int err = 0;
3324
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003325 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3326 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3327
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003328 ext_debug("ext4_split_extents_at: inode %lu, logical"
3329 "block %llu\n", inode->i_ino, (unsigned long long)split);
3330
3331 ext4_ext_show_leaf(inode, path);
3332
3333 depth = ext_depth(inode);
3334 ex = path[depth].p_ext;
3335 ee_block = le32_to_cpu(ex->ee_block);
3336 ee_len = ext4_ext_get_actual_len(ex);
3337 newblock = split - ee_block + ext4_ext_pblock(ex);
3338
3339 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003340 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003341 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003342 EXT4_EXT_MARK_UNWRIT1 |
3343 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003344
3345 err = ext4_ext_get_access(handle, inode, path + depth);
3346 if (err)
3347 goto out;
3348
3349 if (split == ee_block) {
3350 /*
3351 * case b: block @split is the block that the extent begins with
3352 * then we just change the state of the extent, and splitting
3353 * is not needed.
3354 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003355 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3356 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003357 else
3358 ext4_ext_mark_initialized(ex);
3359
3360 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003361 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003362
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003363 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003364 goto out;
3365 }
3366
3367 /* case a */
3368 memcpy(&orig_ex, ex, sizeof(orig_ex));
3369 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003370 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3371 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003372
3373 /*
3374 * path may lead to new leaf, not to original leaf any more
3375 * after ext4_ext_insert_extent() returns,
3376 */
3377 err = ext4_ext_dirty(handle, inode, path + depth);
3378 if (err)
3379 goto fix_extent_len;
3380
3381 ex2 = &newex;
3382 ex2->ee_block = cpu_to_le32(split);
3383 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3384 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003385 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3386 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003387
Theodore Ts'odfe50802014-09-01 14:37:09 -04003388 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003389 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003390 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003391 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003392 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003393 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003394 zero_ex.ee_len = cpu_to_le16(
3395 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003396 ext4_ext_store_pblock(&zero_ex,
3397 ext4_ext_pblock(ex2));
3398 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003399 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003400 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003401 zero_ex.ee_len = cpu_to_le16(
3402 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003403 ext4_ext_store_pblock(&zero_ex,
3404 ext4_ext_pblock(ex));
3405 }
3406 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003407 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003408 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003409 zero_ex.ee_len = cpu_to_le16(
3410 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003411 ext4_ext_store_pblock(&zero_ex,
3412 ext4_ext_pblock(&orig_ex));
3413 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003414
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003415 if (err)
3416 goto fix_extent_len;
3417 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003418 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003419 ext4_ext_try_to_merge(handle, inode, path, ex);
3420 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003421 if (err)
3422 goto fix_extent_len;
3423
3424 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003425 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003426
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003427 goto out;
3428 } else if (err)
3429 goto fix_extent_len;
3430
3431out:
3432 ext4_ext_show_leaf(inode, path);
3433 return err;
3434
3435fix_extent_len:
3436 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003437 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003438 return err;
3439}
3440
3441/*
3442 * ext4_split_extents() splits an extent and mark extent which is covered
3443 * by @map as split_flags indicates
3444 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003445 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003446 * There are three possibilities:
3447 * a> There is no split required
3448 * b> Splits in two extents: Split is happening at either end of the extent
3449 * c> Splits in three extents: Somone is splitting in middle of the extent
3450 *
3451 */
3452static int ext4_split_extent(handle_t *handle,
3453 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003454 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003455 struct ext4_map_blocks *map,
3456 int split_flag,
3457 int flags)
3458{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003459 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003460 ext4_lblk_t ee_block;
3461 struct ext4_extent *ex;
3462 unsigned int ee_len, depth;
3463 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003464 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003465 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003466 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003467
3468 depth = ext_depth(inode);
3469 ex = path[depth].p_ext;
3470 ee_block = le32_to_cpu(ex->ee_block);
3471 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003472 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003473
3474 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003475 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003476 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003477 if (unwritten)
3478 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3479 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003480 if (split_flag & EXT4_EXT_DATA_VALID2)
3481 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003482 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003483 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003484 if (err)
3485 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003486 } else {
3487 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003488 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003489 /*
3490 * Update path is required because previous ext4_split_extent_at() may
3491 * result in split of original leaf or extent zeroout.
3492 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003493 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003494 if (IS_ERR(path))
3495 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003496 depth = ext_depth(inode);
3497 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003498 if (!ex) {
3499 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3500 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003501 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003502 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003503 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003504 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003505
3506 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003507 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003508 if (unwritten) {
3509 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003510 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003511 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003512 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003513 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003514 map->m_lblk, split_flag1, flags);
3515 if (err)
3516 goto out;
3517 }
3518
3519 ext4_ext_show_leaf(inode, path);
3520out:
Zheng Liu3a225672013-03-10 21:20:23 -04003521 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003522}
3523
Amit Arora56055d32007-07-17 21:42:38 -04003524/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003525 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003526 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003527 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003528 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003529 * There are three possibilities:
3530 * a> There is no split required: Entire extent should be initialized
3531 * b> Splits in two extents: Write is happening at either end of the extent
3532 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003533 *
3534 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003535 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003536 * - The extent pointed to by 'path' contains a superset
3537 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3538 *
3539 * Post-conditions on success:
3540 * - the returned value is the number of blocks beyond map->l_lblk
3541 * that are allocated and initialized.
3542 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003543 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003544static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003545 struct inode *inode,
3546 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003547 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003548 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003549{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003550 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003551 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003552 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003553 struct ext4_map_blocks split_map;
Jan Kara4f8caa62017-05-26 17:40:52 -04003554 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003555 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003556 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003557 unsigned int ee_len, depth, map_len = map->m_len;
3558 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003559 int err = 0;
Jan Kara4f8caa62017-05-26 17:40:52 -04003560 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003561
3562 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3563 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003564 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003565
Zheng Liu67a5da52012-08-17 09:54:17 -04003566 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003567 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3568 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003569 if (eof_block < map->m_lblk + map_len)
3570 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003571
3572 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003573 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003574 ex = path[depth].p_ext;
3575 ee_block = le32_to_cpu(ex->ee_block);
3576 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara4f8caa62017-05-26 17:40:52 -04003577 zero_ex1.ee_len = 0;
3578 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003579
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003580 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3581
3582 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003583 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003584 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003585
3586 /*
3587 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003588 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003589 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003590 * memmove() calls. Transferring to the left is the common case in
3591 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3592 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003593 *
3594 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003595 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003596 * This would require removing the extent if the transfer
3597 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003598 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003599 * same extent tree node.
3600 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003601 if ((map->m_lblk == ee_block) &&
3602 /* See if we can merge left */
3603 (map_len < ee_len) && /*L1*/
3604 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003605 ext4_lblk_t prev_lblk;
3606 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003607 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003608
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003609 abut_ex = ex - 1;
3610 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3611 prev_len = ext4_ext_get_actual_len(abut_ex);
3612 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003613 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003614
3615 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003616 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003617 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003618 * - C1: abut_ex is initialized,
3619 * - C2: abut_ex is logically abutting ex,
3620 * - C3: abut_ex is physically abutting ex,
3621 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003622 * overflowing the (initialized) length limit.
3623 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003624 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003625 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3626 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003627 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003628 err = ext4_ext_get_access(handle, inode, path + depth);
3629 if (err)
3630 goto out;
3631
3632 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003633 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003634
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003635 /* Shift the start of ex by 'map_len' blocks */
3636 ex->ee_block = cpu_to_le32(ee_block + map_len);
3637 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3638 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003639 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003640
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003641 /* Extend abut_ex by 'map_len' blocks */
3642 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003643
3644 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003645 allocated = map_len;
3646 }
3647 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3648 (map_len < ee_len) && /*L1*/
3649 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3650 /* See if we can merge right */
3651 ext4_lblk_t next_lblk;
3652 ext4_fsblk_t next_pblk, ee_pblk;
3653 unsigned int next_len;
3654
3655 abut_ex = ex + 1;
3656 next_lblk = le32_to_cpu(abut_ex->ee_block);
3657 next_len = ext4_ext_get_actual_len(abut_ex);
3658 next_pblk = ext4_ext_pblock(abut_ex);
3659 ee_pblk = ext4_ext_pblock(ex);
3660
3661 /*
3662 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3663 * upon those conditions:
3664 * - C1: abut_ex is initialized,
3665 * - C2: abut_ex is logically abutting ex,
3666 * - C3: abut_ex is physically abutting ex,
3667 * - C4: abut_ex can receive the additional blocks without
3668 * overflowing the (initialized) length limit.
3669 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003670 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003671 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3672 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3673 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3674 err = ext4_ext_get_access(handle, inode, path + depth);
3675 if (err)
3676 goto out;
3677
3678 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3679 map, ex, abut_ex);
3680
3681 /* Shift the start of abut_ex by 'map_len' blocks */
3682 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3683 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3684 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003685 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003686
3687 /* Extend abut_ex by 'map_len' blocks */
3688 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3689
3690 /* Result: number of initialized blocks past m_lblk */
3691 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003692 }
3693 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003694 if (allocated) {
3695 /* Mark the block containing both extents as dirty */
3696 ext4_ext_dirty(handle, inode, path + depth);
3697
3698 /* Update path to point to the right extent */
3699 path[depth].p_ext = abut_ex;
3700 goto out;
3701 } else
3702 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003703
Yongqiang Yang667eff32011-05-03 12:25:07 -04003704 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003705 /*
3706 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003707 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003708 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003709 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003710
Zheng Liu67a5da52012-08-17 09:54:17 -04003711 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3712 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003713 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003714
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303715 if (IS_ENCRYPTED(inode))
Theodore Ts'o36086d42015-10-03 10:49:29 -04003716 max_zeroout = 0;
3717
Amit Arora56055d32007-07-17 21:42:38 -04003718 /*
Jan Kara4f8caa62017-05-26 17:40:52 -04003719 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003720 * 1. split the extent into three extents.
Jan Kara4f8caa62017-05-26 17:40:52 -04003721 * 2. split the extent into two extents, zeroout the head of the first
3722 * extent.
3723 * 3. split the extent into two extents, zeroout the tail of the second
3724 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003725 * 4. split the extent into two extents with out zeroout.
Jan Kara4f8caa62017-05-26 17:40:52 -04003726 * 5. no splitting needed, just possibly zeroout the head and / or the
3727 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003728 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003729 split_map.m_lblk = map->m_lblk;
3730 split_map.m_len = map->m_len;
3731
Jan Kara4f8caa62017-05-26 17:40:52 -04003732 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003733 if (allocated <= max_zeroout) {
Jan Kara4f8caa62017-05-26 17:40:52 -04003734 /* case 3 or 5 */
3735 zero_ex1.ee_block =
3736 cpu_to_le32(split_map.m_lblk +
3737 split_map.m_len);
3738 zero_ex1.ee_len =
3739 cpu_to_le16(allocated - split_map.m_len);
3740 ext4_ext_store_pblock(&zero_ex1,
3741 ext4_ext_pblock(ex) + split_map.m_lblk +
3742 split_map.m_len - ee_block);
3743 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003744 if (err)
3745 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003746 split_map.m_len = allocated;
Jan Kara4f8caa62017-05-26 17:40:52 -04003747 }
3748 if (split_map.m_lblk - ee_block + split_map.m_len <
3749 max_zeroout) {
3750 /* case 2 or 5 */
3751 if (split_map.m_lblk != ee_block) {
3752 zero_ex2.ee_block = ex->ee_block;
3753 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003754 ee_block);
Jan Kara4f8caa62017-05-26 17:40:52 -04003755 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003756 ext4_ext_pblock(ex));
Jan Kara4f8caa62017-05-26 17:40:52 -04003757 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003758 if (err)
3759 goto out;
3760 }
3761
Jan Kara4f8caa62017-05-26 17:40:52 -04003762 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003763 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003764 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003765 }
3766 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003767
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003768 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3769 flags);
3770 if (err > 0)
3771 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003772out:
Zheng Liuadb23552013-03-10 21:13:05 -04003773 /* If we have gotten a failure, don't zero out status tree */
Jan Kara4f8caa62017-05-26 17:40:52 -04003774 if (!err) {
3775 err = ext4_zeroout_es(inode, &zero_ex1);
3776 if (!err)
3777 err = ext4_zeroout_es(inode, &zero_ex2);
3778 }
Amit Arora56055d32007-07-17 21:42:38 -04003779 return err ? err : allocated;
3780}
3781
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003782/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003783 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003784 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003785 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003786 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003787 * Writing to an unwritten extent may result in splitting the unwritten
3788 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003789 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003790 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003791 * b> Splits in two extents: Write is happening at either end of the extent
3792 * c> Splits in three extents: Somone is writing in middle of the extent
3793 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003794 * This works the same way in the case of initialized -> unwritten conversion.
3795 *
Mingming Cao00314622009-09-28 15:49:08 -04003796 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003797 * the unwritten extent split. To prevent ENOSPC occur at the IO
3798 * complete, we need to split the unwritten extent before DIO submit
3799 * the IO. The unwritten extent called at this time will be split
3800 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003801 * being filled will be convert to initialized by the end_io callback function
3802 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003803 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003804 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003805 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003806static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003807 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003808 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003809 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003810 int flags)
3811{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003812 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003813 ext4_lblk_t eof_block;
3814 ext4_lblk_t ee_block;
3815 struct ext4_extent *ex;
3816 unsigned int ee_len;
3817 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003818
Lukas Czernerb8a86842014-03-18 18:05:35 -04003819 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3820 __func__, inode->i_ino,
3821 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003822
3823 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3824 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003825 if (eof_block < map->m_lblk + map->m_len)
3826 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003827 /*
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003828 * It is safe to convert extent to initialized via explicit
3829 * zeroout only if extent is fully insde i_size or new_size.
3830 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003831 depth = ext_depth(inode);
3832 ex = path[depth].p_ext;
3833 ee_block = le32_to_cpu(ex->ee_block);
3834 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003835
Lukas Czernerb8a86842014-03-18 18:05:35 -04003836 /* Convert to unwritten */
3837 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3838 split_flag |= EXT4_EXT_DATA_VALID1;
3839 /* Convert to initialized */
3840 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3841 split_flag |= ee_block + ee_len <= eof_block ?
3842 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003843 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003844 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003845 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003846 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003847}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003848
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003849static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003850 struct inode *inode,
3851 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003852 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003853{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003854 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003855 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003856 ext4_lblk_t ee_block;
3857 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003858 int depth;
3859 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003860
3861 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003862 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003863 ee_block = le32_to_cpu(ex->ee_block);
3864 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003865
Yongqiang Yang197217a2011-05-03 11:45:29 -04003866 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3867 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003868 (unsigned long long)ee_block, ee_len);
3869
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003870 /* If extent is larger than requested it is a clear sign that we still
3871 * have some extent state machine issues left. So extent_split is still
3872 * required.
3873 * TODO: Once all related issues will be fixed this situation should be
3874 * illegal.
3875 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003876 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Pandite3d550c2019-08-22 22:53:46 -04003877#ifdef CONFIG_EXT4_DEBUG
3878 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003879 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003880 inode->i_ino, (unsigned long long)ee_block, ee_len,
3881 (unsigned long long)map->m_lblk, map->m_len);
3882#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003883 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003884 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003885 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003886 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003887 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003888 if (IS_ERR(path))
3889 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003890 depth = ext_depth(inode);
3891 ex = path[depth].p_ext;
3892 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003893
Mingming Cao00314622009-09-28 15:49:08 -04003894 err = ext4_ext_get_access(handle, inode, path + depth);
3895 if (err)
3896 goto out;
3897 /* first mark the extent as initialized */
3898 ext4_ext_mark_initialized(ex);
3899
Yongqiang Yang197217a2011-05-03 11:45:29 -04003900 /* note: ext4_ext_correct_indexes() isn't needed here because
3901 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003902 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003903 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003904
Mingming Cao00314622009-09-28 15:49:08 -04003905 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003906 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003907out:
3908 ext4_ext_show_leaf(inode, path);
3909 return err;
3910}
3911
Theodore Ts'o58590b02010-10-27 21:23:12 -04003912/*
3913 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3914 */
3915static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003916 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003917 struct ext4_ext_path *path,
3918 unsigned int len)
3919{
3920 int i, depth;
3921 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003922 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003923
3924 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3925 return 0;
3926
3927 depth = ext_depth(inode);
3928 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003929
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003930 /*
3931 * We're going to remove EOFBLOCKS_FL entirely in future so we
3932 * do not care for this case anymore. Simply remove the flag
3933 * if there are no extents.
3934 */
3935 if (unlikely(!eh->eh_entries))
3936 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003937 last_ex = EXT_LAST_EXTENT(eh);
3938 /*
3939 * We should clear the EOFBLOCKS_FL flag if we are writing the
3940 * last block in the last extent in the file. We test this by
3941 * first checking to see if the caller to
3942 * ext4_ext_get_blocks() was interested in the last block (or
3943 * a block beyond the last block) in the current extent. If
3944 * this turns out to be false, we can bail out from this
3945 * function immediately.
3946 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003947 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003948 ext4_ext_get_actual_len(last_ex))
3949 return 0;
3950 /*
3951 * If the caller does appear to be planning to write at or
3952 * beyond the end of the current extent, we then test to see
3953 * if the current extent is the last extent in the file, by
3954 * checking to make sure it was reached via the rightmost node
3955 * at each level of the tree.
3956 */
3957 for (i = depth-1; i >= 0; i--)
3958 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3959 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003960out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003961 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3962 return ext4_mark_inode_dirty(handle, inode);
3963}
3964
Mingming Cao00314622009-09-28 15:49:08 -04003965static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003966convert_initialized_extent(handle_t *handle, struct inode *inode,
3967 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003968 struct ext4_ext_path **ppath,
Eryu Guan56263b42016-02-12 01:23:00 -05003969 unsigned int allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003970{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003971 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003972 struct ext4_extent *ex;
3973 ext4_lblk_t ee_block;
3974 unsigned int ee_len;
3975 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003976 int err = 0;
3977
3978 /*
3979 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003980 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003981 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003982 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3983 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003984
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003985 depth = ext_depth(inode);
3986 ex = path[depth].p_ext;
3987 ee_block = le32_to_cpu(ex->ee_block);
3988 ee_len = ext4_ext_get_actual_len(ex);
3989
3990 ext_debug("%s: inode %lu, logical"
3991 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3992 (unsigned long long)ee_block, ee_len);
3993
3994 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003995 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003996 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3997 if (err < 0)
3998 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003999 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004000 if (IS_ERR(path))
4001 return PTR_ERR(path);
4002 depth = ext_depth(inode);
4003 ex = path[depth].p_ext;
4004 if (!ex) {
4005 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
4006 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004007 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004008 }
4009 }
4010
4011 err = ext4_ext_get_access(handle, inode, path + depth);
4012 if (err)
4013 return err;
4014 /* first mark the extent as unwritten */
4015 ext4_ext_mark_unwritten(ex);
4016
4017 /* note: ext4_ext_correct_indexes() isn't needed here because
4018 * borders are not changed
4019 */
4020 ext4_ext_try_to_merge(handle, inode, path, ex);
4021
4022 /* Mark modified extent as dirty */
4023 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4024 if (err)
4025 return err;
4026 ext4_ext_show_leaf(inode, path);
4027
4028 ext4_update_inode_fsync_trans(handle, inode, 1);
4029 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4030 if (err)
4031 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004032 map->m_flags |= EXT4_MAP_UNWRITTEN;
4033 if (allocated > map->m_len)
4034 allocated = map->m_len;
4035 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004036 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004037}
4038
4039static int
Lukas Czerner556615d2014-04-20 23:45:47 -04004040ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004041 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004042 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004043 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04004044{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004045 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004046 int ret = 0;
4047 int err = 0;
4048
Lukas Czerner556615d2014-04-20 23:45:47 -04004049 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004050 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004051 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004052 flags, allocated);
4053 ext4_ext_show_leaf(inode, path);
4054
Lukas Czerner27dd4382013-04-09 22:11:22 -04004055 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004056 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004057 * allocate metadata blocks for the new extent block if needed.
4058 */
4059 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4060
Lukas Czerner556615d2014-04-20 23:45:47 -04004061 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004062 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004063
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004064 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004065 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004066 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4067 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004068 if (ret <= 0)
4069 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004070 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004071 goto out;
4072 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004073 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004074 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05004075 if (flags & EXT4_GET_BLOCKS_ZERO) {
4076 if (allocated > map->m_len)
4077 allocated = map->m_len;
4078 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4079 allocated);
4080 if (err < 0)
4081 goto out2;
4082 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004083 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004084 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004085 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004086 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004087 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4088 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004089 } else
4090 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004091 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004092 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004093 if (allocated > map->m_len)
4094 allocated = map->m_len;
4095 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004096 goto out2;
4097 }
4098 /* buffered IO case */
4099 /*
4100 * repeat fallocate creation request
4101 * we already have an unwritten extent
4102 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004103 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004104 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004105 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004106 }
Mingming Cao00314622009-09-28 15:49:08 -04004107
4108 /* buffered READ or buffered write_begin() lookup */
4109 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4110 /*
4111 * We have blocks reserved already. We
4112 * return allocated blocks so that delalloc
4113 * won't do block reservation for us. But
4114 * the buffer head will be unmapped so that
4115 * a read from the block returns 0s.
4116 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004117 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004118 goto out1;
4119 }
4120
4121 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004122 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004123 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004124 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004125out:
4126 if (ret <= 0) {
4127 err = ret;
4128 goto out2;
4129 } else
4130 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004131 map->m_flags |= EXT4_MAP_NEW;
zhangyi (F)16e08b12019-02-10 23:32:07 -05004132 if (allocated > map->m_len)
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004133 allocated = map->m_len;
Zheng Liu3a225672013-03-10 21:20:23 -04004134 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004135
Mingming Cao00314622009-09-28 15:49:08 -04004136map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004137 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004138 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4139 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4140 map->m_len);
4141 if (err < 0)
4142 goto out2;
4143 }
Mingming Cao00314622009-09-28 15:49:08 -04004144out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004145 if (allocated > map->m_len)
4146 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004147 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004148 map->m_pblk = newblock;
4149 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004150out2:
Mingming Cao00314622009-09-28 15:49:08 -04004151 return err ? err : allocated;
4152}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004153
Mingming Cao00314622009-09-28 15:49:08 -04004154/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004155 * get_implied_cluster_alloc - check to see if the requested
4156 * allocation (in the map structure) overlaps with a cluster already
4157 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004158 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004159 * @map The requested lblk->pblk mapping
4160 * @ex The extent structure which might contain an implied
4161 * cluster allocation
4162 *
4163 * This function is called by ext4_ext_map_blocks() after we failed to
4164 * find blocks that were already in the inode's extent tree. Hence,
4165 * we know that the beginning of the requested region cannot overlap
4166 * the extent from the inode's extent tree. There are three cases we
4167 * want to catch. The first is this case:
4168 *
4169 * |--- cluster # N--|
4170 * |--- extent ---| |---- requested region ---|
4171 * |==========|
4172 *
4173 * The second case that we need to test for is this one:
4174 *
4175 * |--------- cluster # N ----------------|
4176 * |--- requested region --| |------- extent ----|
4177 * |=======================|
4178 *
4179 * The third case is when the requested region lies between two extents
4180 * within the same cluster:
4181 * |------------- cluster # N-------------|
4182 * |----- ex -----| |---- ex_right ----|
4183 * |------ requested region ------|
4184 * |================|
4185 *
4186 * In each of the above cases, we need to set the map->m_pblk and
4187 * map->m_len so it corresponds to the return the extent labelled as
4188 * "|====|" from cluster #N, since it is already in use for data in
4189 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4190 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4191 * as a new "allocated" block region. Otherwise, we will return 0 and
4192 * ext4_ext_map_blocks() will then allocate one or more new clusters
4193 * by calling ext4_mb_new_blocks().
4194 */
Aditya Kalid8990242011-09-09 19:18:51 -04004195static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004196 struct ext4_map_blocks *map,
4197 struct ext4_extent *ex,
4198 struct ext4_ext_path *path)
4199{
Aditya Kalid8990242011-09-09 19:18:51 -04004200 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004201 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004202 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004203 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004204 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4205 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4206 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4207
4208 /* The extent passed in that we are trying to match */
4209 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4210 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4211
4212 /* The requested region passed into ext4_map_blocks() */
4213 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004214
4215 if ((rr_cluster_start == ex_cluster_end) ||
4216 (rr_cluster_start == ex_cluster_start)) {
4217 if (rr_cluster_start == ex_cluster_end)
4218 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004219 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004220 map->m_len = min(map->m_len,
4221 (unsigned) sbi->s_cluster_ratio - c_offset);
4222 /*
4223 * Check for and handle this case:
4224 *
4225 * |--------- cluster # N-------------|
4226 * |------- extent ----|
4227 * |--- requested region ---|
4228 * |===========|
4229 */
4230
4231 if (map->m_lblk < ee_block)
4232 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4233
4234 /*
4235 * Check for the case where there is already another allocated
4236 * block to the right of 'ex' but before the end of the cluster.
4237 *
4238 * |------------- cluster # N-------------|
4239 * |----- ex -----| |---- ex_right ----|
4240 * |------ requested region ------|
4241 * |================|
4242 */
4243 if (map->m_lblk > ee_block) {
4244 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4245 map->m_len = min(map->m_len, next - map->m_lblk);
4246 }
Aditya Kalid8990242011-09-09 19:18:51 -04004247
4248 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004249 return 1;
4250 }
Aditya Kalid8990242011-09-09 19:18:51 -04004251
4252 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004253 return 0;
4254}
4255
4256
4257/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004258 * Block allocation/map/preallocation routine for extents based files
4259 *
4260 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004261 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004262 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4263 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004264 *
4265 * return > 0, number of of blocks already mapped/allocated
4266 * if create == 0 and these are pre-allocated blocks
4267 * buffer head is unmapped
4268 * otherwise blocks are mapped
4269 *
4270 * return = 0, if plain look up failed (blocks have not been allocated)
4271 * buffer head is unmapped
4272 *
4273 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004274 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004275int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4276 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004277{
4278 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004279 struct ext4_extent newex, *ex, *ex2;
4280 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004281 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004282 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004283 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004284 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004285 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004286 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004287 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004288
Mingming84fe3be2009-09-01 08:44:37 -04004289 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004290 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004291 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004292
Alex Tomasa86c6182006-10-11 01:21:03 -07004293 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004294 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004295 if (IS_ERR(path)) {
4296 err = PTR_ERR(path);
4297 path = NULL;
4298 goto out2;
4299 }
4300
4301 depth = ext_depth(inode);
4302
4303 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004304 * consistent leaf must not be empty;
4305 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004306 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004307 */
Frank Mayhar273df552010-03-02 11:46:09 -05004308 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4309 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004310 "lblock: %lu, depth: %d pblock %lld",
4311 (unsigned long) map->m_lblk, depth,
4312 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004313 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004314 goto out2;
4315 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004316
Avantika Mathur7e028972006-12-06 20:41:33 -08004317 ex = path[depth].p_ext;
4318 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004319 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004320 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004321 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004322
Lukas Czernerb8a86842014-03-18 18:05:35 -04004323
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004324 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004325 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004326 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004327 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004328 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004329
4330 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4331
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004332 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004333 if (in_range(map->m_lblk, ee_block, ee_len)) {
4334 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004335 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004336 allocated = ee_len - (map->m_lblk - ee_block);
4337 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4338 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004339
Lukas Czernerb8a86842014-03-18 18:05:35 -04004340 /*
4341 * If the extent is initialized check whether the
4342 * caller wants to convert it to unwritten.
4343 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004344 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004345 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004346 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004347 handle, inode, map, &path,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05004348 allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004349 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004350 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004351 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004352
Lukas Czerner556615d2014-04-20 23:45:47 -04004353 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004354 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004355 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004356 if (ret < 0)
4357 err = ret;
4358 else
4359 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004360 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004361 }
4362 }
4363
4364 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004365 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004366 * we couldn't try to create block if create flag is zero
4367 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004368 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004369 ext4_lblk_t hole_start, hole_len;
4370
Jan Karafacab4d2016-03-09 22:54:00 -05004371 hole_start = map->m_lblk;
4372 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004373 /*
4374 * put just found gap into cache to speed up
4375 * subsequent requests
4376 */
Jan Kara140a5252016-03-09 22:46:57 -05004377 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004378
4379 /* Update hole_len to reflect hole size after map->m_lblk */
4380 if (hole_start != map->m_lblk)
4381 hole_len -= map->m_lblk - hole_start;
4382 map->m_pblk = 0;
4383 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4384
Alex Tomasa86c6182006-10-11 01:21:03 -07004385 goto out2;
4386 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004387
Alex Tomasa86c6182006-10-11 01:21:03 -07004388 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004389 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004390 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004391 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004392 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004393
4394 /*
4395 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004396 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004397 */
4398 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004399 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004400 ar.len = allocated = map->m_len;
4401 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004402 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004403 goto got_allocated_blocks;
4404 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004405
Alex Tomasc9de5602008-01-29 00:19:52 -05004406 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004407 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004408 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4409 if (err)
4410 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004411 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004412 ex2 = NULL;
4413 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004414 if (err)
4415 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004416
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004417 /* Check if the extent after searching to the right implies a
4418 * cluster we can use. */
4419 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004420 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004421 ar.len = allocated = map->m_len;
4422 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004423 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004424 goto got_allocated_blocks;
4425 }
4426
Amit Arora749269f2007-07-18 09:02:56 -04004427 /*
4428 * See if request is beyond maximum number of blocks we can have in
4429 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004430 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4431 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004432 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004433 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004434 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004435 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004436 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4437 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4438 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004439
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004440 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004441 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004442 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004443 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004444 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004445 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004446 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004447
4448 /* allocate new block */
4449 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004450 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4451 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004452 /*
4453 * We calculate the offset from the beginning of the cluster
4454 * for the logical block number, since when we allocate a
4455 * physical cluster, the physical block should start at the
4456 * same offset from the beginning of the cluster. This is
4457 * needed so that future calls to get_implied_cluster_alloc()
4458 * work correctly.
4459 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004460 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004461 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4462 ar.goal -= offset;
4463 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004464 if (S_ISREG(inode->i_mode))
4465 ar.flags = EXT4_MB_HINT_DATA;
4466 else
4467 /* disable in-core preallocation for non-regular files */
4468 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004469 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4470 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004471 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4472 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004473 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4474 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004475 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004476 if (!newblock)
4477 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004478 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004479 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004480 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004481 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004482 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4483 if (ar.len > allocated)
4484 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004485
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004486got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004487 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004488 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004489 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004490 /* Mark unwritten */
4491 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4492 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004493 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004494 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004495
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004496 err = 0;
4497 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4498 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4499 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004500 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004501 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004502 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004503
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004504 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004505 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4506 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004507 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004508 /* not a good idea to call discard here directly,
4509 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004510 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004511 ext4_free_blocks(handle, inode, NULL, newblock,
4512 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004513 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004514 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004515
Alex Tomasa86c6182006-10-11 01:21:03 -07004516 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004517 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004518 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004519 if (allocated > map->m_len)
4520 allocated = map->m_len;
4521 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004522
Jan Karab436b9b2009-12-08 23:51:10 -05004523 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004524 * Reduce the reserved cluster count to reflect successful deferred
4525 * allocation of delayed allocated clusters or direct allocation of
4526 * clusters discovered to be delayed allocated. Once allocated, a
4527 * cluster is not included in the reserved count.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004528 */
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004529 if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) {
4530 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Lukas Czerner232ec872013-03-10 22:46:30 -04004531 /*
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004532 * When allocating delayed allocated clusters, simply
4533 * reduce the reserved cluster count and claim quota
Lukas Czerner232ec872013-03-10 22:46:30 -04004534 */
4535 ext4_da_update_reserve_space(inode, allocated_clusters,
4536 1);
Eric Whitneyb6bf9172018-10-01 14:24:08 -04004537 } else {
4538 ext4_lblk_t lblk, len;
4539 unsigned int n;
4540
4541 /*
4542 * When allocating non-delayed allocated clusters
4543 * (from fallocate, filemap, DIO, or clusters
4544 * allocated when delalloc has been disabled by
4545 * ext4_nonda_switch), reduce the reserved cluster
4546 * count by the number of allocated clusters that
4547 * have previously been delayed allocated. Quota
4548 * has been claimed by ext4_mb_new_blocks() above,
4549 * so release the quota reservations made for any
4550 * previously delayed allocated clusters.
4551 */
4552 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4553 len = allocated_clusters << sbi->s_cluster_bits;
4554 n = ext4_es_delayed_clu(inode, lblk, len);
4555 if (n > 0)
4556 ext4_da_update_reserve_space(inode, (int) n, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004557 }
4558 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004559
4560 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004561 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004562 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004563 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004564 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004565 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004566 else
Jan Karab436b9b2009-12-08 23:51:10 -05004567 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004568out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004569 if (allocated > map->m_len)
4570 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004571 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004572 map->m_flags |= EXT4_MAP_MAPPED;
4573 map->m_pblk = newblock;
4574 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004575out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004576 ext4_ext_drop_refs(path);
4577 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004578
Theodore Ts'o63b99962013-07-16 10:28:47 -04004579 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4580 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004581 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004582}
4583
Theodore Ts'od0abb362016-11-13 22:02:28 -05004584int ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004585{
Alex Tomasa86c6182006-10-11 01:21:03 -07004586 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004587 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004588 int err = 0;
4589
4590 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004591 * TODO: optimization is possible here.
4592 * Probably we need not scan at all,
4593 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004594 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004595
4596 /* we have to know where to truncate from in crash case */
4597 EXT4_I(inode)->i_disksize = inode->i_size;
Theodore Ts'od0abb362016-11-13 22:02:28 -05004598 err = ext4_mark_inode_dirty(handle, inode);
4599 if (err)
4600 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -07004601
4602 last_block = (inode->i_size + sb->s_blocksize - 1)
4603 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004604retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004605 err = ext4_es_remove_extent(inode, last_block,
4606 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004607 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004608 cond_resched();
4609 congestion_wait(BLK_RW_ASYNC, HZ/50);
4610 goto retry;
4611 }
Theodore Ts'od0abb362016-11-13 22:02:28 -05004612 if (err)
4613 return err;
4614 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004615}
4616
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004617static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004618 ext4_lblk_t len, loff_t new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004619 int flags)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004620{
Al Viro496ad9a2013-01-23 17:07:38 -05004621 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004622 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004623 int ret = 0;
4624 int ret2 = 0;
4625 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004626 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004627 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004628 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004629 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004630
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004631 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004632 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004633 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004634 /*
4635 * Don't normalize the request if it can fit in one extent so
4636 * that it doesn't get unnecessarily split into multiple
4637 * extents.
4638 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004639 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004640 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004641
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004642 /*
4643 * credits to insert 1 extent into extent tree
4644 */
4645 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004646 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004647
Amit Aroraa2df2a62007-07-17 21:42:41 -04004648retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004649 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004650 /*
4651 * Recalculate credits when extent tree depth changes.
4652 */
Dan Carpenter011c88e2016-12-03 16:46:58 -05004653 if (depth != ext_depth(inode)) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004654 credits = ext4_chunk_trans_blocks(inode, len);
4655 depth = ext_depth(inode);
4656 }
4657
Theodore Ts'o9924a922013-02-08 21:59:22 -05004658 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4659 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004660 if (IS_ERR(handle)) {
4661 ret = PTR_ERR(handle);
4662 break;
4663 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004664 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004665 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004666 ext4_debug("inode #%lu: block %u: len %u: "
4667 "ext4_ext_map_blocks returned %d",
4668 inode->i_ino, map.m_lblk,
4669 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004670 ext4_mark_inode_dirty(handle, inode);
4671 ret2 = ext4_journal_stop(handle);
4672 break;
4673 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004674 map.m_lblk += ret;
4675 map.m_len = len = len - ret;
4676 epos = (loff_t)map.m_lblk << inode->i_blkbits;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004677 inode->i_ctime = current_time(inode);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004678 if (new_size) {
4679 if (epos > new_size)
4680 epos = new_size;
4681 if (ext4_update_inode_size(inode, epos) & 0x1)
4682 inode->i_mtime = inode->i_ctime;
4683 } else {
4684 if (epos > inode->i_size)
4685 ext4_set_inode_flag(inode,
4686 EXT4_INODE_EOFBLOCKS);
4687 }
4688 ext4_mark_inode_dirty(handle, inode);
Eryu Guanc894aa92017-12-03 22:52:51 -05004689 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004690 ret2 = ext4_journal_stop(handle);
4691 if (ret2)
4692 break;
4693 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004694 if (ret == -ENOSPC &&
4695 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4696 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004697 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004698 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004699
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004700 return ret > 0 ? ret2 : ret;
4701}
4702
Lukas Czernerb8a86842014-03-18 18:05:35 -04004703static long ext4_zero_range(struct file *file, loff_t offset,
4704 loff_t len, int mode)
4705{
4706 struct inode *inode = file_inode(file);
4707 handle_t *handle = NULL;
4708 unsigned int max_blocks;
4709 loff_t new_size = 0;
4710 int ret = 0;
4711 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004712 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004713 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004714 loff_t start, end;
4715 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004716 unsigned int blkbits = inode->i_blkbits;
4717
4718 trace_ext4_zero_range(inode, offset, len, mode);
4719
jon ernst6c5e73d2014-04-18 11:50:35 -04004720 if (!S_ISREG(inode->i_mode))
4721 return -EINVAL;
4722
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004723 /* Call ext4_force_commit to flush all data in case of data=journal. */
4724 if (ext4_should_journal_data(inode)) {
4725 ret = ext4_force_commit(inode->i_sb);
4726 if (ret)
4727 return ret;
4728 }
4729
Lukas Czernerb8a86842014-03-18 18:05:35 -04004730 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004731 * Round up offset. This is not fallocate, we neet to zero out
4732 * blocks, so convert interior block aligned part of the range to
4733 * unwritten and possibly manually zero out unaligned parts of the
4734 * range.
4735 */
4736 start = round_up(offset, 1 << blkbits);
4737 end = round_down((offset + len), 1 << blkbits);
4738
4739 if (start < offset || end > offset + len)
4740 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004741 partial_begin = offset & ((1 << blkbits) - 1);
4742 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004743
4744 lblk = start >> blkbits;
4745 max_blocks = (end >> blkbits);
4746 if (max_blocks < lblk)
4747 max_blocks = 0;
4748 else
4749 max_blocks -= lblk;
4750
Al Viro59551022016-01-22 15:40:57 -05004751 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004752
4753 /*
4754 * Indirect files do not support unwritten extnets
4755 */
4756 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4757 ret = -EOPNOTSUPP;
4758 goto out_mutex;
4759 }
4760
4761 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004762 (offset + len > i_size_read(inode) ||
4763 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004764 new_size = offset + len;
4765 ret = inode_newsize_ok(inode, new_size);
4766 if (ret)
4767 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004768 }
4769
Lukas Czerner0f2af212015-04-03 00:09:13 -04004770 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4771 if (mode & FALLOC_FL_KEEP_SIZE)
4772 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4773
Jan Kara17048e82015-12-07 14:29:17 -05004774 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004775 inode_dio_wait(inode);
4776
Lukas Czerner0f2af212015-04-03 00:09:13 -04004777 /* Preallocate the range including the unaligned edges */
4778 if (partial_begin || partial_end) {
4779 ret = ext4_alloc_file_blocks(file,
4780 round_down(offset, 1 << blkbits) >> blkbits,
4781 (round_up((offset + len), 1 << blkbits) -
4782 round_down(offset, 1 << blkbits)) >> blkbits,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004783 new_size, flags);
Lukas Czerner0f2af212015-04-03 00:09:13 -04004784 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004785 goto out_mutex;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004786
4787 }
4788
4789 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004790 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004791 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4792 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004793
Jan Karaea3d7202015-12-07 14:28:03 -05004794 /*
4795 * Prevent page faults from reinstantiating pages we have
4796 * released from page cache.
4797 */
4798 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004799
4800 ret = ext4_break_layouts(inode);
4801 if (ret) {
4802 up_write(&EXT4_I(inode)->i_mmap_sem);
4803 goto out_mutex;
4804 }
4805
Jan Kara01127842015-12-07 14:34:49 -05004806 ret = ext4_update_disksize_before_punch(inode, offset, len);
4807 if (ret) {
4808 up_write(&EXT4_I(inode)->i_mmap_sem);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004809 goto out_mutex;
Jan Kara01127842015-12-07 14:34:49 -05004810 }
Jan Karaea3d7202015-12-07 14:28:03 -05004811 /* Now release the pages and zero block aligned part of pages */
4812 truncate_pagecache_range(inode, start, end - 1);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004813 inode->i_mtime = inode->i_ctime = current_time(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05004814
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004815 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004816 flags);
Jan Karaea3d7202015-12-07 14:28:03 -05004817 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004818 if (ret)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004819 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004820 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004821 if (!partial_begin && !partial_end)
Nikolay Borisov1d398342018-03-22 11:52:10 -04004822 goto out_mutex;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004823
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004824 /*
4825 * In worst case we have to writeout two nonadjacent unwritten
4826 * blocks and update the inode
4827 */
4828 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4829 if (ext4_should_journal_data(inode))
4830 credits += 2;
4831 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004832 if (IS_ERR(handle)) {
4833 ret = PTR_ERR(handle);
4834 ext4_std_error(inode->i_sb, ret);
Nikolay Borisov1d398342018-03-22 11:52:10 -04004835 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004836 }
4837
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004838 inode->i_mtime = inode->i_ctime = current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004839 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004840 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004841 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004842 /*
4843 * Mark that we allocate beyond EOF so the subsequent truncate
4844 * can proceed even if the new size is the same as i_size.
4845 */
4846 if ((offset + len) > i_size_read(inode))
4847 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4848 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004849 ext4_mark_inode_dirty(handle, inode);
4850
4851 /* Zero out partial block at the edges of the range */
4852 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004853 if (ret >= 0)
4854 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004855
4856 if (file->f_flags & O_SYNC)
4857 ext4_handle_sync(handle);
4858
4859 ext4_journal_stop(handle);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004860out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004861 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004862 return ret;
4863}
4864
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004865/*
4866 * preallocate space for a file. This implements ext4's fallocate file
4867 * operation, which gets called from sys_fallocate system call.
4868 * For block-mapped files, posix_fallocate should fall back to the method
4869 * of writing zeroes to the required new blocks (the same behavior which is
4870 * expected for file systems which do not support fallocate() system call).
4871 */
4872long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4873{
4874 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004875 loff_t new_size = 0;
4876 unsigned int max_blocks;
4877 int ret = 0;
4878 int flags;
4879 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004880 unsigned int blkbits = inode->i_blkbits;
4881
Michael Halcrow2058f832015-04-12 00:55:10 -04004882 /*
4883 * Encrypted inodes can't handle collapse range or insert
4884 * range since we would need to re-encrypt blocks with a
4885 * different IV or XTS tweak (which are based on the logical
4886 * block number).
4887 *
4888 * XXX It's not clear why zero range isn't working, but we'll
4889 * leave it disabled for encrypted inodes for now. This is a
4890 * bug we should fix....
4891 */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304892 if (IS_ENCRYPTED(inode) &&
Namjae Jeon331573f2015-06-09 01:55:03 -04004893 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4894 FALLOC_FL_ZERO_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004895 return -EOPNOTSUPP;
4896
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004897 /* Return error if mode is not supported */
4898 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004899 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4900 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004901 return -EOPNOTSUPP;
4902
4903 if (mode & FALLOC_FL_PUNCH_HOLE)
4904 return ext4_punch_hole(inode, offset, len);
4905
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004906 ret = ext4_convert_inline_data(inode);
4907 if (ret)
4908 return ret;
4909
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004910 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4911 return ext4_collapse_range(inode, offset, len);
4912
Namjae Jeon331573f2015-06-09 01:55:03 -04004913 if (mode & FALLOC_FL_INSERT_RANGE)
4914 return ext4_insert_range(inode, offset, len);
4915
Lukas Czernerb8a86842014-03-18 18:05:35 -04004916 if (mode & FALLOC_FL_ZERO_RANGE)
4917 return ext4_zero_range(file, offset, len, mode);
4918
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004919 trace_ext4_fallocate_enter(inode, offset, len, mode);
4920 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004921
Fabian Frederick518eaa62016-09-15 11:55:01 -04004922 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004923 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004924 if (mode & FALLOC_FL_KEEP_SIZE)
4925 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4926
Al Viro59551022016-01-22 15:40:57 -05004927 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004928
Davide Italiano280227a2015-05-02 23:21:15 -04004929 /*
4930 * We only support preallocation for extent-based files only
4931 */
4932 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4933 ret = -EOPNOTSUPP;
4934 goto out;
4935 }
4936
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004937 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o51e3ae82017-10-06 23:09:55 -04004938 (offset + len > i_size_read(inode) ||
4939 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004940 new_size = offset + len;
4941 ret = inode_newsize_ok(inode, new_size);
4942 if (ret)
4943 goto out;
4944 }
4945
Jan Kara17048e82015-12-07 14:29:17 -05004946 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Kara17048e82015-12-07 14:29:17 -05004947 inode_dio_wait(inode);
4948
Tahsin Erdogan77a2e842017-08-05 22:15:45 -04004949 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004950 if (ret)
4951 goto out;
4952
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004953 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4954 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4955 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004956 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004957out:
Al Viro59551022016-01-22 15:40:57 -05004958 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004959 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4960 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004961}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004962
4963/*
Mingming Cao00314622009-09-28 15:49:08 -04004964 * This function convert a range of blocks to written extents
4965 * The caller of this function will pass the start offset and the size.
4966 * all unwritten extents within this range will be converted to
4967 * written extents.
4968 *
4969 * This function is called from the direct IO end io call back
4970 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004971 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004972 */
Jan Kara6b523df2013-06-04 13:21:11 -04004973int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4974 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004975{
Mingming Cao00314622009-09-28 15:49:08 -04004976 unsigned int max_blocks;
4977 int ret = 0;
4978 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004979 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004980 unsigned int credits, blkbits = inode->i_blkbits;
4981
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004982 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04004983 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4984
Mingming Cao00314622009-09-28 15:49:08 -04004985 /*
Jan Kara6b523df2013-06-04 13:21:11 -04004986 * This is somewhat ugly but the idea is clear: When transaction is
4987 * reserved, everything goes into it. Otherwise we rather start several
4988 * smaller transactions for conversion of each extent separately.
Mingming Cao00314622009-09-28 15:49:08 -04004989 */
Jan Kara6b523df2013-06-04 13:21:11 -04004990 if (handle) {
4991 handle = ext4_journal_start_reserved(handle,
4992 EXT4_HT_EXT_CONVERT);
4993 if (IS_ERR(handle))
4994 return PTR_ERR(handle);
4995 credits = 0;
4996 } else {
4997 /*
4998 * credits to insert 1 extent into extent tree
4999 */
5000 credits = ext4_chunk_trans_blocks(inode, max_blocks);
5001 }
Mingming Cao00314622009-09-28 15:49:08 -04005002 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005003 map.m_lblk += ret;
5004 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005005 if (credits) {
5006 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5007 credits);
5008 if (IS_ERR(handle)) {
5009 ret = PTR_ERR(handle);
5010 break;
5011 }
Mingming Cao00314622009-09-28 15:49:08 -04005012 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005013 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005014 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005015 if (ret <= 0)
5016 ext4_warning(inode->i_sb,
5017 "inode #%lu: block %u: len %u: "
5018 "ext4_ext_map_blocks returned %d",
5019 inode->i_ino, map.m_lblk,
5020 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005021 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005022 if (credits)
5023 ret2 = ext4_journal_stop(handle);
5024 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005025 break;
5026 }
Jan Kara6b523df2013-06-04 13:21:11 -04005027 if (!credits)
5028 ret2 = ext4_journal_stop(handle);
Mingming Cao00314622009-09-28 15:49:08 -04005029 return ret > 0 ? ret2 : ret;
5030}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005031
Mingming Cao00314622009-09-28 15:49:08 -04005032/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005033 * If newes is not existing extent (newes->ec_pblk equals zero) find
5034 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005035 * return start of the next delayed extent.
5036 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005037 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005038 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005039 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005040 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005041static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005042 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005043{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005044 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005045 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005046
Zheng Liu69eb33d2013-02-18 00:31:07 -05005047 if (newes->es_pblk == 0) {
Eric Whitneyad431022018-10-01 14:10:39 -04005048 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
5049 newes->es_lblk,
5050 newes->es_lblk + newes->es_len - 1,
5051 &es);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005052
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005053 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005054 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005055 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005056 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005057 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005058 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005059 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005060
Zheng Liu69eb33d2013-02-18 00:31:07 -05005061 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005062 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005063 newes->es_len = min(es.es_lblk - newes->es_lblk,
5064 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005065 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005066 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005067
Zheng Liu69eb33d2013-02-18 00:31:07 -05005068 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005069 }
5070
Zheng Liu69eb33d2013-02-18 00:31:07 -05005071 block = newes->es_lblk + newes->es_len;
Eric Whitneyad431022018-10-01 14:10:39 -04005072 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block,
5073 EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005074 if (es.es_len == 0)
5075 next_del = EXT_MAX_BLOCKS;
5076 else
5077 next_del = es.es_lblk;
5078
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005079 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005080}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005081
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005082static int ext4_xattr_fiemap(struct inode *inode,
5083 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005084{
5085 __u64 physical = 0;
5086 __u64 length;
5087 __u32 flags = FIEMAP_EXTENT_LAST;
5088 int blockbits = inode->i_sb->s_blocksize_bits;
5089 int error = 0;
5090
5091 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005092 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005093 struct ext4_iloc iloc;
5094 int offset; /* offset of xattr in inode */
5095
5096 error = ext4_get_inode_loc(inode, &iloc);
5097 if (error)
5098 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005099 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005100 offset = EXT4_GOOD_OLD_INODE_SIZE +
5101 EXT4_I(inode)->i_extra_isize;
5102 physical += offset;
5103 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5104 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005105 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005106 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005107 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005108 length = inode->i_sb->s_blocksize;
5109 }
5110
5111 if (physical)
5112 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5113 length, flags);
5114 return (error < 0 ? error : 0);
5115}
5116
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005117static int _ext4_fiemap(struct inode *inode,
5118 struct fiemap_extent_info *fieinfo,
5119 __u64 start, __u64 len,
5120 int (*fill)(struct inode *, ext4_lblk_t,
5121 ext4_lblk_t,
5122 struct fiemap_extent_info *))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005123{
5124 ext4_lblk_t start_blk;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005125 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR;
5126
Eric Sandeen6873fa02008-10-07 00:46:36 -04005127 int error = 0;
5128
Tao Ma94191982012-12-10 14:06:02 -05005129 if (ext4_has_inline_data(inode)) {
5130 int has_inline = 1;
5131
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005132 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5133 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005134
5135 if (has_inline)
5136 return error;
5137 }
5138
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005139 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5140 error = ext4_ext_precache(inode);
5141 if (error)
5142 return error;
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005143 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005144 }
5145
Eric Sandeen6873fa02008-10-07 00:46:36 -04005146 /* fallback to generic here if not in extents fmt */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005147 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
5148 fill == ext4_fill_fiemap_extents)
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005149 return generic_block_fiemap(inode, fieinfo, start, len,
5150 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005151
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005152 if (fill == ext4_fill_es_cache_info)
5153 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
5154 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005155 return -EBADR;
5156
5157 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5158 error = ext4_xattr_fiemap(inode, fieinfo);
5159 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005160 ext4_lblk_t len_blks;
5161 __u64 last_blk;
5162
Eric Sandeen6873fa02008-10-07 00:46:36 -04005163 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005164 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005165 if (last_blk >= EXT_MAX_BLOCKS)
5166 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005167 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005168
5169 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005170 * Walk the extent tree gathering extent information
5171 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005172 */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005173 error = fill(inode, start_blk, len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005174 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005175 return error;
5176}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005177
Theodore Ts'obb5835e2019-08-11 16:32:41 -04005178int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5179 __u64 start, __u64 len)
5180{
5181 return _ext4_fiemap(inode, fieinfo, start, len,
5182 ext4_fill_fiemap_extents);
5183}
5184
5185int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
5186 __u64 start, __u64 len)
5187{
5188 if (ext4_has_inline_data(inode)) {
5189 int has_inline;
5190
5191 down_read(&EXT4_I(inode)->xattr_sem);
5192 has_inline = ext4_has_inline_data(inode);
5193 up_read(&EXT4_I(inode)->xattr_sem);
5194 if (has_inline)
5195 return 0;
5196 }
5197
5198 return _ext4_fiemap(inode, fieinfo, start, len,
5199 ext4_fill_es_cache_info);
5200}
5201
5202
Namjae Jeon9eb79482014-02-23 15:18:59 -05005203/*
5204 * ext4_access_path:
5205 * Function to access the path buffer for marking it dirty.
5206 * It also checks if there are sufficient credits left in the journal handle
5207 * to update path.
5208 */
5209static int
5210ext4_access_path(handle_t *handle, struct inode *inode,
5211 struct ext4_ext_path *path)
5212{
5213 int credits, err;
5214
5215 if (!ext4_handle_valid(handle))
5216 return 0;
5217
5218 /*
5219 * Check if need to extend journal credits
5220 * 3 for leaf, sb, and inode plus 2 (bmap and group
5221 * descriptor) for each block group; assume two block
5222 * groups
5223 */
Jan Karaa4130362019-11-05 17:44:16 +01005224 credits = ext4_writepage_trans_blocks(inode);
5225 err = ext4_datasem_ensure_credits(handle, inode, 7, credits);
5226 if (err < 0)
5227 return err;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005228
5229 err = ext4_ext_get_access(handle, inode, path);
5230 return err;
5231}
5232
5233/*
5234 * ext4_ext_shift_path_extents:
5235 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005236 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5237 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005238 */
5239static int
5240ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5241 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005242 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005243{
5244 int depth, err = 0;
5245 struct ext4_extent *ex_start, *ex_last;
5246 bool update = 0;
5247 depth = path->p_depth;
5248
5249 while (depth >= 0) {
5250 if (depth == path->p_depth) {
5251 ex_start = path[depth].p_ext;
5252 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005253 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005254
5255 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005256
5257 err = ext4_access_path(handle, inode, path + depth);
5258 if (err)
5259 goto out;
5260
5261 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5262 update = 1;
5263
Namjae Jeon9eb79482014-02-23 15:18:59 -05005264 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005265 if (SHIFT == SHIFT_LEFT) {
5266 le32_add_cpu(&ex_start->ee_block,
5267 -shift);
5268 /* Try to merge to the left. */
5269 if ((ex_start >
5270 EXT_FIRST_EXTENT(path[depth].p_hdr))
5271 &&
5272 ext4_ext_try_to_merge_right(inode,
5273 path, ex_start - 1))
5274 ex_last--;
5275 else
5276 ex_start++;
5277 } else {
5278 le32_add_cpu(&ex_last->ee_block, shift);
5279 ext4_ext_try_to_merge_right(inode, path,
5280 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005281 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005282 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005283 }
5284 err = ext4_ext_dirty(handle, inode, path + depth);
5285 if (err)
5286 goto out;
5287
5288 if (--depth < 0 || !update)
5289 break;
5290 }
5291
5292 /* Update index too */
5293 err = ext4_access_path(handle, inode, path + depth);
5294 if (err)
5295 goto out;
5296
Namjae Jeon331573f2015-06-09 01:55:03 -04005297 if (SHIFT == SHIFT_LEFT)
5298 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5299 else
5300 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005301 err = ext4_ext_dirty(handle, inode, path + depth);
5302 if (err)
5303 goto out;
5304
5305 /* we are done if current index is not a starting index */
5306 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5307 break;
5308
5309 depth--;
5310 }
5311
5312out:
5313 return err;
5314}
5315
5316/*
5317 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005318 * All the extents which lies in the range from @start to the last allocated
5319 * block for the @inode are shifted either towards left or right (depending
5320 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005321 * On success, 0 is returned, error otherwise.
5322 */
5323static int
5324ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005325 ext4_lblk_t start, ext4_lblk_t shift,
5326 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005327{
5328 struct ext4_ext_path *path;
5329 int ret = 0, depth;
5330 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005331 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005332
5333 /* Let path point to the last extent */
Roman Pen03e916f2017-01-08 21:00:35 -05005334 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5335 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005336 if (IS_ERR(path))
5337 return PTR_ERR(path);
5338
5339 depth = path->p_depth;
5340 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005341 if (!extent)
5342 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005343
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005344 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005345
Namjae Jeon331573f2015-06-09 01:55:03 -04005346 /*
Eric Biggers349fa7d2018-04-12 11:48:09 -04005347 * For left shifts, make sure the hole on the left is big enough to
5348 * accommodate the shift. For right shifts, make sure the last extent
5349 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005350 */
5351 if (SHIFT == SHIFT_LEFT) {
Roman Pen03e916f2017-01-08 21:00:35 -05005352 path = ext4_find_extent(inode, start - 1, &path,
5353 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005354 if (IS_ERR(path))
5355 return PTR_ERR(path);
5356 depth = path->p_depth;
5357 extent = path[depth].p_ext;
5358 if (extent) {
5359 ex_start = le32_to_cpu(extent->ee_block);
5360 ex_end = le32_to_cpu(extent->ee_block) +
5361 ext4_ext_get_actual_len(extent);
5362 } else {
5363 ex_start = 0;
5364 ex_end = 0;
5365 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005366
Namjae Jeon331573f2015-06-09 01:55:03 -04005367 if ((start == ex_start && shift > ex_start) ||
5368 (shift > start - ex_end)) {
Eric Biggers349fa7d2018-04-12 11:48:09 -04005369 ret = -EINVAL;
5370 goto out;
5371 }
5372 } else {
5373 if (shift > EXT_MAX_BLOCKS -
5374 (stop + ext4_ext_get_actual_len(extent))) {
5375 ret = -EINVAL;
5376 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005377 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005378 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005379
Namjae Jeon331573f2015-06-09 01:55:03 -04005380 /*
5381 * In case of left shift, iterator points to start and it is increased
5382 * till we reach stop. In case of right shift, iterator points to stop
5383 * and it is decreased till we reach start.
5384 */
5385 if (SHIFT == SHIFT_LEFT)
5386 iterator = &start;
5387 else
5388 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005389
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005390 /*
5391 * Its safe to start updating extents. Start and stop are unsigned, so
5392 * in case of right shift if extent with 0 block is reached, iterator
5393 * becomes NULL to indicate the end of the loop.
5394 */
5395 while (iterator && start <= stop) {
Roman Pen03e916f2017-01-08 21:00:35 -05005396 path = ext4_find_extent(inode, *iterator, &path,
5397 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005398 if (IS_ERR(path))
5399 return PTR_ERR(path);
5400 depth = path->p_depth;
5401 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005402 if (!extent) {
5403 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005404 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005405 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005406 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005407 if (SHIFT == SHIFT_LEFT && *iterator >
5408 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005409 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005410 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5411 path[depth].p_ext++;
5412 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005413 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005414 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005415 }
5416 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005417
5418 if (SHIFT == SHIFT_LEFT) {
5419 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5420 *iterator = le32_to_cpu(extent->ee_block) +
5421 ext4_ext_get_actual_len(extent);
5422 } else {
5423 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen2a9b8cb2017-01-08 20:59:35 -05005424 if (le32_to_cpu(extent->ee_block) > 0)
5425 *iterator = le32_to_cpu(extent->ee_block) - 1;
5426 else
5427 /* Beginning is reached, end of the loop */
5428 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005429 /* Update path extent in case we need to stop */
5430 while (le32_to_cpu(extent->ee_block) < start)
5431 extent++;
5432 path[depth].p_ext = extent;
5433 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005434 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005435 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005436 if (ret)
5437 break;
5438 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005439out:
5440 ext4_ext_drop_refs(path);
5441 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005442 return ret;
5443}
5444
5445/*
5446 * ext4_collapse_range:
5447 * This implements the fallocate's collapse range functionality for ext4
5448 * Returns: 0 and non-zero on error.
5449 */
5450int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5451{
5452 struct super_block *sb = inode->i_sb;
5453 ext4_lblk_t punch_start, punch_stop;
5454 handle_t *handle;
5455 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005456 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005457 int ret;
5458
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005459 /*
5460 * We need to test this early because xfstests assumes that a
5461 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5462 * system does not support collapse range.
5463 */
5464 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5465 return -EOPNOTSUPP;
5466
Namjae Jeon9eb79482014-02-23 15:18:59 -05005467 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005468 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5469 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005470 return -EINVAL;
5471
5472 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005473 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005474
5475 trace_ext4_collapse_range(inode, offset, len);
5476
5477 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5478 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5479
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005480 /* Call ext4_force_commit to flush all data in case of data=journal. */
5481 if (ext4_should_journal_data(inode)) {
5482 ret = ext4_force_commit(inode->i_sb);
5483 if (ret)
5484 return ret;
5485 }
5486
Al Viro59551022016-01-22 15:40:57 -05005487 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005488 /*
5489 * There is no need to overlap collapse range with EOF, in which case
5490 * it is effectively a truncate operation
5491 */
5492 if (offset + len >= i_size_read(inode)) {
5493 ret = -EINVAL;
5494 goto out_mutex;
5495 }
5496
Namjae Jeon9eb79482014-02-23 15:18:59 -05005497 /* Currently just for extent based files */
5498 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5499 ret = -EOPNOTSUPP;
5500 goto out_mutex;
5501 }
5502
Namjae Jeon9eb79482014-02-23 15:18:59 -05005503 /* Wait for existing dio to complete */
Namjae Jeon9eb79482014-02-23 15:18:59 -05005504 inode_dio_wait(inode);
5505
Jan Karaea3d7202015-12-07 14:28:03 -05005506 /*
5507 * Prevent page faults from reinstantiating pages we have released from
5508 * page cache.
5509 */
5510 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005511
5512 ret = ext4_break_layouts(inode);
5513 if (ret)
5514 goto out_mmap;
5515
Jan Kara32ebffd2015-12-07 14:31:11 -05005516 /*
5517 * Need to round down offset to be aligned with page size boundary
5518 * for page size > block size.
5519 */
5520 ioffset = round_down(offset, PAGE_SIZE);
5521 /*
5522 * Write tail of the last page before removed range since it will get
5523 * removed from the page cache below.
5524 */
5525 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5526 if (ret)
5527 goto out_mmap;
5528 /*
5529 * Write data that will be shifted to preserve them when discarding
5530 * page cache below. We are also protected from pages becoming dirty
5531 * by i_mmap_sem.
5532 */
5533 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5534 LLONG_MAX);
5535 if (ret)
5536 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005537 truncate_pagecache(inode, ioffset);
5538
Namjae Jeon9eb79482014-02-23 15:18:59 -05005539 credits = ext4_writepage_trans_blocks(inode);
5540 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5541 if (IS_ERR(handle)) {
5542 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005543 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005544 }
5545
5546 down_write(&EXT4_I(inode)->i_data_sem);
5547 ext4_discard_preallocations(inode);
5548
5549 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005550 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005551 if (ret) {
5552 up_write(&EXT4_I(inode)->i_data_sem);
5553 goto out_stop;
5554 }
5555
5556 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5557 if (ret) {
5558 up_write(&EXT4_I(inode)->i_data_sem);
5559 goto out_stop;
5560 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005561 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005562
5563 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005564 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005565 if (ret) {
5566 up_write(&EXT4_I(inode)->i_data_sem);
5567 goto out_stop;
5568 }
5569
5570 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005571 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005572 EXT4_I(inode)->i_disksize = new_size;
5573
Namjae Jeon9eb79482014-02-23 15:18:59 -05005574 up_write(&EXT4_I(inode)->i_data_sem);
5575 if (IS_SYNC(inode))
5576 ext4_handle_sync(handle);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005577 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005578 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005579 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005580
5581out_stop:
5582 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005583out_mmap:
5584 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005585out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005586 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005587 return ret;
5588}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005589
Namjae Jeon331573f2015-06-09 01:55:03 -04005590/*
5591 * ext4_insert_range:
5592 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5593 * The data blocks starting from @offset to the EOF are shifted by @len
5594 * towards right to create a hole in the @inode. Inode size is increased
5595 * by len bytes.
5596 * Returns 0 on success, error otherwise.
5597 */
5598int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5599{
5600 struct super_block *sb = inode->i_sb;
5601 handle_t *handle;
5602 struct ext4_ext_path *path;
5603 struct ext4_extent *extent;
5604 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5605 unsigned int credits, ee_len;
5606 int ret = 0, depth, split_flag = 0;
5607 loff_t ioffset;
5608
5609 /*
5610 * We need to test this early because xfstests assumes that an
5611 * insert range of (0, 1) will return EOPNOTSUPP if the file
5612 * system does not support insert range.
5613 */
5614 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5615 return -EOPNOTSUPP;
5616
5617 /* Insert range works only on fs block size aligned offsets. */
5618 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5619 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5620 return -EINVAL;
5621
5622 if (!S_ISREG(inode->i_mode))
5623 return -EOPNOTSUPP;
5624
5625 trace_ext4_insert_range(inode, offset, len);
5626
5627 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5628 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5629
5630 /* Call ext4_force_commit to flush all data in case of data=journal */
5631 if (ext4_should_journal_data(inode)) {
5632 ret = ext4_force_commit(inode->i_sb);
5633 if (ret)
5634 return ret;
5635 }
5636
Al Viro59551022016-01-22 15:40:57 -05005637 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005638 /* Currently just for extent based files */
5639 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5640 ret = -EOPNOTSUPP;
5641 goto out_mutex;
5642 }
5643
5644 /* Check for wrap through zero */
5645 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5646 ret = -EFBIG;
5647 goto out_mutex;
5648 }
5649
5650 /* Offset should be less than i_size */
5651 if (offset >= i_size_read(inode)) {
5652 ret = -EINVAL;
5653 goto out_mutex;
5654 }
5655
Namjae Jeon331573f2015-06-09 01:55:03 -04005656 /* Wait for existing dio to complete */
Namjae Jeon331573f2015-06-09 01:55:03 -04005657 inode_dio_wait(inode);
5658
Jan Karaea3d7202015-12-07 14:28:03 -05005659 /*
5660 * Prevent page faults from reinstantiating pages we have released from
5661 * page cache.
5662 */
5663 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04005664
5665 ret = ext4_break_layouts(inode);
5666 if (ret)
5667 goto out_mmap;
5668
Jan Kara32ebffd2015-12-07 14:31:11 -05005669 /*
5670 * Need to round down to align start offset to page size boundary
5671 * for page size > block size.
5672 */
5673 ioffset = round_down(offset, PAGE_SIZE);
5674 /* Write out all dirty pages */
5675 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5676 LLONG_MAX);
5677 if (ret)
5678 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005679 truncate_pagecache(inode, ioffset);
5680
Namjae Jeon331573f2015-06-09 01:55:03 -04005681 credits = ext4_writepage_trans_blocks(inode);
5682 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5683 if (IS_ERR(handle)) {
5684 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005685 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005686 }
5687
5688 /* Expand file to avoid data loss if there is error while shifting */
5689 inode->i_size += len;
5690 EXT4_I(inode)->i_disksize += len;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005691 inode->i_mtime = inode->i_ctime = current_time(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005692 ret = ext4_mark_inode_dirty(handle, inode);
5693 if (ret)
5694 goto out_stop;
5695
5696 down_write(&EXT4_I(inode)->i_data_sem);
5697 ext4_discard_preallocations(inode);
5698
5699 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5700 if (IS_ERR(path)) {
5701 up_write(&EXT4_I(inode)->i_data_sem);
5702 goto out_stop;
5703 }
5704
5705 depth = ext_depth(inode);
5706 extent = path[depth].p_ext;
5707 if (extent) {
5708 ee_start_lblk = le32_to_cpu(extent->ee_block);
5709 ee_len = ext4_ext_get_actual_len(extent);
5710
5711 /*
5712 * If offset_lblk is not the starting block of extent, split
5713 * the extent @offset_lblk
5714 */
5715 if ((offset_lblk > ee_start_lblk) &&
5716 (offset_lblk < (ee_start_lblk + ee_len))) {
5717 if (ext4_ext_is_unwritten(extent))
5718 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5719 EXT4_EXT_MARK_UNWRIT2;
5720 ret = ext4_split_extent_at(handle, inode, &path,
5721 offset_lblk, split_flag,
5722 EXT4_EX_NOCACHE |
5723 EXT4_GET_BLOCKS_PRE_IO |
5724 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5725 }
5726
5727 ext4_ext_drop_refs(path);
5728 kfree(path);
5729 if (ret < 0) {
5730 up_write(&EXT4_I(inode)->i_data_sem);
5731 goto out_stop;
5732 }
Fabian Frederickedf15aa12016-09-15 11:39:52 -04005733 } else {
5734 ext4_ext_drop_refs(path);
5735 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005736 }
5737
5738 ret = ext4_es_remove_extent(inode, offset_lblk,
5739 EXT_MAX_BLOCKS - offset_lblk);
5740 if (ret) {
5741 up_write(&EXT4_I(inode)->i_data_sem);
5742 goto out_stop;
5743 }
5744
5745 /*
5746 * if offset_lblk lies in a hole which is at start of file, use
5747 * ee_start_lblk to shift extents
5748 */
5749 ret = ext4_ext_shift_extents(inode, handle,
5750 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5751 len_lblk, SHIFT_RIGHT);
5752
5753 up_write(&EXT4_I(inode)->i_data_sem);
5754 if (IS_SYNC(inode))
5755 ext4_handle_sync(handle);
Jan Kara67a7d5f2017-05-29 13:24:55 -04005756 if (ret >= 0)
5757 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005758
5759out_stop:
5760 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005761out_mmap:
5762 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005763out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005764 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005765 return ret;
5766}
5767
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005768/**
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005769 * ext4_swap_extents() - Swap extents between two inodes
5770 * @handle: handle for this transaction
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005771 * @inode1: First inode
5772 * @inode2: Second inode
5773 * @lblk1: Start block for first inode
5774 * @lblk2: Start block for second inode
5775 * @count: Number of blocks to swap
zhenwei.pidcae0582018-03-26 01:44:03 -04005776 * @unwritten: Mark second inode's extents as unwritten after swap
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005777 * @erp: Pointer to save error value
5778 *
5779 * This helper routine does exactly what is promise "swap extents". All other
5780 * stuff such as page-cache locking consistency, bh mapping consistency or
5781 * extent's data copying must be performed by caller.
5782 * Locking:
5783 * i_mutex is held for both inodes
5784 * i_data_sem is locked for write for both inodes
5785 * Assumptions:
5786 * All pages from requested range are locked for both inodes
5787 */
5788int
5789ext4_swap_extents(handle_t *handle, struct inode *inode1,
zhenwei.pidcae0582018-03-26 01:44:03 -04005790 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005791 ext4_lblk_t count, int unwritten, int *erp)
5792{
5793 struct ext4_ext_path *path1 = NULL;
5794 struct ext4_ext_path *path2 = NULL;
5795 int replaced_count = 0;
5796
5797 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5798 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005799 BUG_ON(!inode_is_locked(inode1));
5800 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005801
5802 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005803 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005804 return 0;
5805 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005806 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005807 return 0;
5808
5809 while (count) {
5810 struct ext4_extent *ex1, *ex2, tmp_ex;
5811 ext4_lblk_t e1_blk, e2_blk;
5812 int e1_len, e2_len, len;
5813 int split = 0;
5814
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005815 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305816 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005817 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005818 path1 = NULL;
5819 finish:
5820 count = 0;
5821 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005822 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005823 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305824 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005825 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005826 path2 = NULL;
5827 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005828 }
5829 ex1 = path1[path1->p_depth].p_ext;
5830 ex2 = path2[path2->p_depth].p_ext;
5831 /* Do we have somthing to swap ? */
5832 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005833 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005834
5835 e1_blk = le32_to_cpu(ex1->ee_block);
5836 e2_blk = le32_to_cpu(ex2->ee_block);
5837 e1_len = ext4_ext_get_actual_len(ex1);
5838 e2_len = ext4_ext_get_actual_len(ex2);
5839
5840 /* Hole handling */
5841 if (!in_range(lblk1, e1_blk, e1_len) ||
5842 !in_range(lblk2, e2_blk, e2_len)) {
5843 ext4_lblk_t next1, next2;
5844
5845 /* if hole after extent, then go to next extent */
5846 next1 = ext4_ext_next_allocated_block(path1);
5847 next2 = ext4_ext_next_allocated_block(path2);
5848 /* If hole before extent, then shift to that extent */
5849 if (e1_blk > lblk1)
5850 next1 = e1_blk;
5851 if (e2_blk > lblk2)
Maninder Singh4e562012017-08-06 01:33:07 -04005852 next2 = e2_blk;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005853 /* Do we have something to swap */
5854 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005855 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005856 /* Move to the rightest boundary */
5857 len = next1 - lblk1;
5858 if (len < next2 - lblk2)
5859 len = next2 - lblk2;
5860 if (len > count)
5861 len = count;
5862 lblk1 += len;
5863 lblk2 += len;
5864 count -= len;
5865 goto repeat;
5866 }
5867
5868 /* Prepare left boundary */
5869 if (e1_blk < lblk1) {
5870 split = 1;
5871 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005872 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005873 if (unlikely(*erp))
5874 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005875 }
5876 if (e2_blk < lblk2) {
5877 split = 1;
5878 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005879 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005880 if (unlikely(*erp))
5881 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005882 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005883 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005884 * path must to be revalidated. */
5885 if (split)
5886 goto repeat;
5887
5888 /* Prepare right boundary */
5889 len = count;
5890 if (len > e1_blk + e1_len - lblk1)
5891 len = e1_blk + e1_len - lblk1;
5892 if (len > e2_blk + e2_len - lblk2)
5893 len = e2_blk + e2_len - lblk2;
5894
5895 if (len != e1_len) {
5896 split = 1;
5897 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005898 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005899 if (unlikely(*erp))
5900 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005901 }
5902 if (len != e2_len) {
5903 split = 1;
5904 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005905 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005906 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005907 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005908 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005909 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005910 * path must to be revalidated. */
5911 if (split)
5912 goto repeat;
5913
5914 BUG_ON(e2_len != e1_len);
5915 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005916 if (unlikely(*erp))
5917 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005918 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005919 if (unlikely(*erp))
5920 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005921
5922 /* Both extents are fully inside boundaries. Swap it now */
5923 tmp_ex = *ex1;
5924 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5925 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5926 ex1->ee_len = cpu_to_le16(e2_len);
5927 ex2->ee_len = cpu_to_le16(e1_len);
5928 if (unwritten)
5929 ext4_ext_mark_unwritten(ex2);
5930 if (ext4_ext_is_unwritten(&tmp_ex))
5931 ext4_ext_mark_unwritten(ex1);
5932
5933 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5934 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5935 *erp = ext4_ext_dirty(handle, inode2, path2 +
5936 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005937 if (unlikely(*erp))
5938 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005939 *erp = ext4_ext_dirty(handle, inode1, path1 +
5940 path1->p_depth);
5941 /*
5942 * Looks scarry ah..? second inode already points to new blocks,
5943 * and it was successfully dirtied. But luckily error may happen
5944 * only due to journal error, so full transaction will be
5945 * aborted anyway.
5946 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005947 if (unlikely(*erp))
5948 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005949 lblk1 += len;
5950 lblk2 += len;
5951 replaced_count += len;
5952 count -= len;
5953
5954 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005955 ext4_ext_drop_refs(path1);
5956 kfree(path1);
5957 ext4_ext_drop_refs(path2);
5958 kfree(path2);
5959 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005960 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005961 return replaced_count;
5962}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04005963
5964/*
5965 * ext4_clu_mapped - determine whether any block in a logical cluster has
5966 * been mapped to a physical cluster
5967 *
5968 * @inode - file containing the logical cluster
5969 * @lclu - logical cluster of interest
5970 *
5971 * Returns 1 if any block in the logical cluster is mapped, signifying
5972 * that a physical cluster has been allocated for it. Otherwise,
5973 * returns 0. Can also return negative error codes. Derived from
5974 * ext4_ext_map_blocks().
5975 */
5976int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
5977{
5978 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5979 struct ext4_ext_path *path;
5980 int depth, mapped = 0, err = 0;
5981 struct ext4_extent *extent;
5982 ext4_lblk_t first_lblk, first_lclu, last_lclu;
5983
5984 /* search for the extent closest to the first block in the cluster */
5985 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
5986 if (IS_ERR(path)) {
5987 err = PTR_ERR(path);
5988 path = NULL;
5989 goto out;
5990 }
5991
5992 depth = ext_depth(inode);
5993
5994 /*
5995 * A consistent leaf must not be empty. This situation is possible,
5996 * though, _during_ tree modification, and it's why an assert can't
5997 * be put in ext4_find_extent().
5998 */
5999 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
6000 EXT4_ERROR_INODE(inode,
6001 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
6002 (unsigned long) EXT4_C2B(sbi, lclu),
6003 depth, path[depth].p_block);
6004 err = -EFSCORRUPTED;
6005 goto out;
6006 }
6007
6008 extent = path[depth].p_ext;
6009
6010 /* can't be mapped if the extent tree is empty */
6011 if (extent == NULL)
6012 goto out;
6013
6014 first_lblk = le32_to_cpu(extent->ee_block);
6015 first_lclu = EXT4_B2C(sbi, first_lblk);
6016
6017 /*
6018 * Three possible outcomes at this point - found extent spanning
6019 * the target cluster, to the left of the target cluster, or to the
6020 * right of the target cluster. The first two cases are handled here.
6021 * The last case indicates the target cluster is not mapped.
6022 */
6023 if (lclu >= first_lclu) {
6024 last_lclu = EXT4_B2C(sbi, first_lblk +
6025 ext4_ext_get_actual_len(extent) - 1);
6026 if (lclu <= last_lclu) {
6027 mapped = 1;
6028 } else {
6029 first_lblk = ext4_ext_next_allocated_block(path);
6030 first_lclu = EXT4_B2C(sbi, first_lblk);
6031 if (lclu == first_lclu)
6032 mapped = 1;
6033 }
6034 }
6035
6036out:
6037 ext4_ext_drop_refs(path);
6038 kfree(path);
6039
6040 return err ? err : mapped;
6041}