Theodore Ts'o | f516676 | 2017-12-17 22:00:59 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2 | /* |
| 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 Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 9 | */ |
| 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 Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 20 | #include <linux/fs.h> |
| 21 | #include <linux/time.h> |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 22 | #include <linux/jbd2.h> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 23 | #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 Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 29 | #include <linux/fiemap.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 31 | #include <linux/iomap.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 32 | #include "ext4_jbd2.h" |
Theodore Ts'o | 4a092d7 | 2012-11-28 13:03:30 -0500 | [diff] [blame] | 33 | #include "ext4_extents.h" |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 34 | #include "xattr.h" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 35 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 36 | #include <trace/events/ext4.h> |
| 37 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 38 | /* |
| 39 | * used by extent splitting. |
| 40 | */ |
| 41 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ |
| 42 | due to ENOSPC */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 43 | #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ |
| 44 | #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 45 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 46 | #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */ |
| 47 | #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ |
| 48 | |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 49 | static __le32 ext4_extent_block_csum(struct inode *inode, |
| 50 | struct ext4_extent_header *eh) |
| 51 | { |
| 52 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 53 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 54 | __u32 csum; |
| 55 | |
| 56 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh, |
| 57 | EXT4_EXTENT_TAIL_OFFSET(eh)); |
| 58 | return cpu_to_le32(csum); |
| 59 | } |
| 60 | |
| 61 | static int ext4_extent_block_csum_verify(struct inode *inode, |
| 62 | struct ext4_extent_header *eh) |
| 63 | { |
| 64 | struct ext4_extent_tail *et; |
| 65 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 66 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 67 | return 1; |
| 68 | |
| 69 | et = find_ext4_extent_tail(eh); |
| 70 | if (et->et_checksum != ext4_extent_block_csum(inode, eh)) |
| 71 | return 0; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static void ext4_extent_block_csum_set(struct inode *inode, |
| 76 | struct ext4_extent_header *eh) |
| 77 | { |
| 78 | struct ext4_extent_tail *et; |
| 79 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 80 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 81 | return; |
| 82 | |
| 83 | et = find_ext4_extent_tail(eh); |
| 84 | et->et_checksum = ext4_extent_block_csum(inode, eh); |
| 85 | } |
| 86 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 87 | static int ext4_split_extent_at(handle_t *handle, |
| 88 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 89 | struct ext4_ext_path **ppath, |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 90 | ext4_lblk_t split, |
| 91 | int split_flag, |
| 92 | int flags); |
| 93 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 94 | static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 95 | { |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 96 | /* |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 97 | * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
| 98 | * moment, get_block can be called only for blocks inside i_size since |
| 99 | * page cache has been already dropped and writes are blocked by |
| 100 | * i_mutex. So we can safely drop the i_data_sem here. |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 101 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 102 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
| 103 | ext4_discard_preallocations(inode); |
| 104 | up_write(&EXT4_I(inode)->i_data_sem); |
| 105 | *dropped = 1; |
| 106 | return 0; |
| 107 | } |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 108 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Make sure 'handle' has at least 'check_cred' credits. If not, restart |
| 111 | * transaction with 'restart_cred' credits. The function drops i_data_sem |
| 112 | * when restarting transaction and gets it after transaction is restarted. |
| 113 | * |
| 114 | * The function returns 0 on success, 1 if transaction had to be restarted, |
| 115 | * and < 0 in case of fatal error. |
| 116 | */ |
| 117 | int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 118 | int check_cred, int restart_cred, |
| 119 | int revoke_cred) |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 120 | { |
| 121 | int ret; |
| 122 | int dropped = 0; |
| 123 | |
| 124 | ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 125 | revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped)); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 126 | if (dropped) |
| 127 | down_write(&EXT4_I(inode)->i_data_sem); |
| 128 | return ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * could return: |
| 133 | * - EROFS |
| 134 | * - ENOMEM |
| 135 | */ |
| 136 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 137 | struct ext4_ext_path *path) |
| 138 | { |
| 139 | if (path->p_bh) { |
| 140 | /* path points to block */ |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 141 | BUFFER_TRACE(path->p_bh, "get_write_access"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 142 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 143 | } |
| 144 | /* path points to leaf/index in inode body */ |
| 145 | /* we use in-core data, no need to protect them */ |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * could return: |
| 151 | * - EROFS |
| 152 | * - ENOMEM |
| 153 | * - EIO |
| 154 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 155 | static int __ext4_ext_dirty(const char *where, unsigned int line, |
| 156 | handle_t *handle, struct inode *inode, |
| 157 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 158 | { |
| 159 | int err; |
Dmitry Monakhov | 4b1f166 | 2014-07-27 22:28:15 -0400 | [diff] [blame] | 160 | |
| 161 | WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 162 | if (path->p_bh) { |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 163 | ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 164 | /* path points to block */ |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 165 | err = __ext4_handle_dirty_metadata(where, line, handle, |
| 166 | inode, path->p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 167 | } else { |
| 168 | /* path points to leaf/index in inode body */ |
| 169 | err = ext4_mark_inode_dirty(handle, inode); |
| 170 | } |
| 171 | return err; |
| 172 | } |
| 173 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 174 | #define ext4_ext_dirty(handle, inode, path) \ |
| 175 | __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) |
| 176 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 177 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 178 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 179 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 180 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 181 | if (path) { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 182 | int depth = path->p_depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 183 | struct ext4_extent *ex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 184 | |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 185 | /* |
| 186 | * Try to predict block placement assuming that we are |
| 187 | * filling in a file which will eventually be |
| 188 | * non-sparse --- i.e., in the case of libbfd writing |
| 189 | * an ELF object sections out-of-order but in a way |
| 190 | * the eventually results in a contiguous object or |
| 191 | * executable file, or some database extending a table |
| 192 | * space file. However, this is actually somewhat |
| 193 | * non-ideal if we are writing a sparse file such as |
| 194 | * qemu or KVM writing a raw image file that is going |
| 195 | * to stay fairly sparse, since it will end up |
| 196 | * fragmenting the file system's free space. Maybe we |
| 197 | * should have some hueristics or some way to allow |
| 198 | * userspace to pass a hint to file system, |
Tao Ma | b8d6568 | 2011-01-21 23:21:31 +0800 | [diff] [blame] | 199 | * especially if the latter case turns out to be |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 200 | * common. |
| 201 | */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 202 | ex = path[depth].p_ext; |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 203 | if (ex) { |
| 204 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
| 205 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); |
| 206 | |
| 207 | if (block > ext_block) |
| 208 | return ext_pblk + (block - ext_block); |
| 209 | else |
| 210 | return ext_pblk - (ext_block - block); |
| 211 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 212 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 213 | /* it looks like index is empty; |
| 214 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 215 | if (path[depth].p_bh) |
| 216 | return path[depth].p_bh->b_blocknr; |
| 217 | } |
| 218 | |
| 219 | /* OK. use inode's group */ |
Eric Sandeen | f86186b | 2011-06-28 10:01:31 -0400 | [diff] [blame] | 220 | return ext4_inode_to_goal_block(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 223 | /* |
| 224 | * Allocation for a meta data block |
| 225 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 226 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 227 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 228 | struct ext4_ext_path *path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 229 | struct ext4_extent *ex, int *err, unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 230 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 231 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 232 | |
| 233 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 234 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 235 | NULL, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 236 | return newblock; |
| 237 | } |
| 238 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 239 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 240 | { |
| 241 | int size; |
| 242 | |
| 243 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 244 | / sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 245 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 246 | if (!check && size > 6) |
| 247 | size = 6; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 248 | #endif |
| 249 | return size; |
| 250 | } |
| 251 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 252 | static inline int ext4_ext_space_block_idx(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 253 | { |
| 254 | int size; |
| 255 | |
| 256 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 257 | / sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 258 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 259 | if (!check && size > 5) |
| 260 | size = 5; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 261 | #endif |
| 262 | return size; |
| 263 | } |
| 264 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 265 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 266 | { |
| 267 | int size; |
| 268 | |
| 269 | size = sizeof(EXT4_I(inode)->i_data); |
| 270 | size -= sizeof(struct ext4_extent_header); |
| 271 | size /= sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 272 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 273 | if (!check && size > 3) |
| 274 | size = 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 275 | #endif |
| 276 | return size; |
| 277 | } |
| 278 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 279 | static inline int ext4_ext_space_root_idx(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 280 | { |
| 281 | int size; |
| 282 | |
| 283 | size = sizeof(EXT4_I(inode)->i_data); |
| 284 | size -= sizeof(struct ext4_extent_header); |
| 285 | size /= sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 286 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 287 | if (!check && size > 4) |
| 288 | size = 4; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 289 | #endif |
| 290 | return size; |
| 291 | } |
| 292 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 293 | static inline int |
| 294 | ext4_force_split_extent_at(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 295 | struct ext4_ext_path **ppath, ext4_lblk_t lblk, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 296 | int nofail) |
| 297 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 298 | struct ext4_ext_path *path = *ppath; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 299 | int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 300 | int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO; |
| 301 | |
| 302 | if (nofail) |
| 303 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 304 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 305 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 306 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 307 | flags); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 310 | static int |
| 311 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 312 | { |
| 313 | int max; |
| 314 | |
| 315 | if (depth == ext_depth(inode)) { |
| 316 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 317 | max = ext4_ext_space_root(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 318 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 319 | max = ext4_ext_space_root_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 320 | } else { |
| 321 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 322 | max = ext4_ext_space_block(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 323 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 324 | max = ext4_ext_space_block_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return max; |
| 328 | } |
| 329 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 330 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 331 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 332 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 333 | int len = ext4_ext_get_actual_len(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 334 | ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 335 | |
Vegard Nossum | f70749c | 2016-06-30 11:53:46 -0400 | [diff] [blame] | 336 | /* |
| 337 | * We allow neither: |
| 338 | * - zero length |
| 339 | * - overflow/wrap-around |
| 340 | */ |
| 341 | if (lblock + len <= lblock) |
Theodore Ts'o | 31d4f3a | 2012-03-11 23:30:16 -0400 | [diff] [blame] | 342 | return 0; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 343 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static int ext4_valid_extent_idx(struct inode *inode, |
| 347 | struct ext4_extent_idx *ext_idx) |
| 348 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 349 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 350 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 351 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static int ext4_valid_extent_entries(struct inode *inode, |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 355 | struct ext4_extent_header *eh, |
| 356 | ext4_fsblk_t *pblk, int depth) |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 357 | { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 358 | unsigned short entries; |
| 359 | if (eh->eh_entries == 0) |
| 360 | return 1; |
| 361 | |
| 362 | entries = le16_to_cpu(eh->eh_entries); |
| 363 | |
| 364 | if (depth == 0) { |
| 365 | /* leaf entries */ |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 366 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 367 | ext4_lblk_t lblock = 0; |
| 368 | ext4_lblk_t prev = 0; |
| 369 | int len = 0; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 370 | while (entries) { |
| 371 | if (!ext4_valid_extent(inode, ext)) |
| 372 | return 0; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 373 | |
| 374 | /* Check for overlapping extents */ |
| 375 | lblock = le32_to_cpu(ext->ee_block); |
| 376 | len = ext4_ext_get_actual_len(ext); |
| 377 | if ((lblock <= prev) && prev) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 378 | *pblk = ext4_ext_pblock(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 379 | return 0; |
| 380 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 381 | ext++; |
| 382 | entries--; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 383 | prev = lblock + len - 1; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 384 | } |
| 385 | } else { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 386 | struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 387 | while (entries) { |
| 388 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 389 | return 0; |
| 390 | ext_idx++; |
| 391 | entries--; |
| 392 | } |
| 393 | } |
| 394 | return 1; |
| 395 | } |
| 396 | |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 397 | static int __ext4_ext_check(const char *function, unsigned int line, |
| 398 | struct inode *inode, struct ext4_extent_header *eh, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 399 | int depth, ext4_fsblk_t pblk) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 400 | { |
| 401 | const char *error_msg; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 402 | int max = 0, err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 403 | |
| 404 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 405 | error_msg = "invalid magic"; |
| 406 | goto corrupted; |
| 407 | } |
| 408 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 409 | error_msg = "unexpected eh_depth"; |
| 410 | goto corrupted; |
| 411 | } |
| 412 | if (unlikely(eh->eh_max == 0)) { |
| 413 | error_msg = "invalid eh_max"; |
| 414 | goto corrupted; |
| 415 | } |
| 416 | max = ext4_ext_max_entries(inode, depth); |
| 417 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 418 | error_msg = "too large eh_max"; |
| 419 | goto corrupted; |
| 420 | } |
| 421 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 422 | error_msg = "invalid eh_entries"; |
| 423 | goto corrupted; |
| 424 | } |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 425 | if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 426 | error_msg = "invalid extent entries"; |
| 427 | goto corrupted; |
| 428 | } |
Vegard Nossum | 7bc9491 | 2016-07-15 00:22:07 -0400 | [diff] [blame] | 429 | if (unlikely(depth > 32)) { |
| 430 | error_msg = "too large eh_depth"; |
| 431 | goto corrupted; |
| 432 | } |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 433 | /* Verify checksum on non-root extent tree nodes */ |
| 434 | if (ext_depth(inode) != depth && |
| 435 | !ext4_extent_block_csum_verify(inode, eh)) { |
| 436 | error_msg = "extent tree corrupted"; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 437 | err = -EFSBADCRC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 438 | goto corrupted; |
| 439 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 440 | return 0; |
| 441 | |
| 442 | corrupted: |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 443 | ext4_error_inode_err(inode, function, line, 0, -err, |
| 444 | "pblk %llu bad header/extent: %s - magic %x, " |
| 445 | "entries %u, max %u(%u), depth %u(%u)", |
| 446 | (unsigned long long) pblk, error_msg, |
| 447 | le16_to_cpu(eh->eh_magic), |
| 448 | le16_to_cpu(eh->eh_entries), |
| 449 | le16_to_cpu(eh->eh_max), |
| 450 | max, le16_to_cpu(eh->eh_depth), depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 451 | return err; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 454 | #define ext4_ext_check(inode, eh, depth, pblk) \ |
| 455 | __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk)) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 456 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 457 | int ext4_ext_check_inode(struct inode *inode) |
| 458 | { |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 459 | return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0); |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 460 | } |
| 461 | |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 462 | static void ext4_cache_extents(struct inode *inode, |
| 463 | struct ext4_extent_header *eh) |
| 464 | { |
| 465 | struct ext4_extent *ex = EXT_FIRST_EXTENT(eh); |
| 466 | ext4_lblk_t prev = 0; |
| 467 | int i; |
| 468 | |
| 469 | for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { |
| 470 | unsigned int status = EXTENT_STATUS_WRITTEN; |
| 471 | ext4_lblk_t lblk = le32_to_cpu(ex->ee_block); |
| 472 | int len = ext4_ext_get_actual_len(ex); |
| 473 | |
| 474 | if (prev && (prev != lblk)) |
| 475 | ext4_es_cache_extent(inode, prev, lblk - prev, ~0, |
| 476 | EXTENT_STATUS_HOLE); |
| 477 | |
| 478 | if (ext4_ext_is_unwritten(ex)) |
| 479 | status = EXTENT_STATUS_UNWRITTEN; |
| 480 | ext4_es_cache_extent(inode, lblk, len, |
| 481 | ext4_ext_pblock(ex), status); |
| 482 | prev = lblk + len; |
| 483 | } |
| 484 | } |
| 485 | |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 486 | static struct buffer_head * |
| 487 | __read_extent_tree_block(const char *function, unsigned int line, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 488 | struct inode *inode, ext4_fsblk_t pblk, int depth, |
| 489 | int flags) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 490 | { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 491 | struct buffer_head *bh; |
| 492 | int err; |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 493 | gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS; |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 494 | |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 495 | if (flags & EXT4_EX_NOFAIL) |
| 496 | gfp_flags |= __GFP_NOFAIL; |
| 497 | |
| 498 | bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 499 | if (unlikely(!bh)) |
| 500 | return ERR_PTR(-ENOMEM); |
| 501 | |
| 502 | if (!bh_uptodate_or_lock(bh)) { |
| 503 | trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); |
| 504 | err = bh_submit_read(bh); |
| 505 | if (err < 0) |
| 506 | goto errout; |
| 507 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 508 | if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 509 | return bh; |
Theodore Ts'o | 0a944e8 | 2019-05-22 10:27:01 -0400 | [diff] [blame] | 510 | if (!ext4_has_feature_journal(inode->i_sb) || |
| 511 | (inode->i_ino != |
| 512 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) { |
| 513 | err = __ext4_ext_check(function, line, inode, |
| 514 | ext_block_hdr(bh), depth, pblk); |
| 515 | if (err) |
| 516 | goto errout; |
| 517 | } |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 518 | set_buffer_verified(bh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 519 | /* |
| 520 | * If this is a leaf block, cache all of its entries |
| 521 | */ |
| 522 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { |
| 523 | struct ext4_extent_header *eh = ext_block_hdr(bh); |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 524 | ext4_cache_extents(inode, eh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 525 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 526 | return bh; |
| 527 | errout: |
| 528 | put_bh(bh); |
| 529 | return ERR_PTR(err); |
| 530 | |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 533 | #define read_extent_tree_block(inode, pblk, depth, flags) \ |
| 534 | __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ |
| 535 | (depth), (flags)) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 536 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 537 | /* |
| 538 | * This function is called to cache a file's extent information in the |
| 539 | * extent status tree |
| 540 | */ |
| 541 | int ext4_ext_precache(struct inode *inode) |
| 542 | { |
| 543 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 544 | struct ext4_ext_path *path = NULL; |
| 545 | struct buffer_head *bh; |
| 546 | int i = 0, depth, ret = 0; |
| 547 | |
| 548 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 549 | return 0; /* not an extent-mapped inode */ |
| 550 | |
| 551 | down_read(&ei->i_data_sem); |
| 552 | depth = ext_depth(inode); |
| 553 | |
Ritesh Harjani | 2f424a5 | 2020-02-28 14:56:55 +0530 | [diff] [blame] | 554 | /* Don't cache anything if there are no external extent blocks */ |
| 555 | if (!depth) { |
| 556 | up_read(&ei->i_data_sem); |
| 557 | return ret; |
| 558 | } |
| 559 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 560 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 561 | GFP_NOFS); |
| 562 | if (path == NULL) { |
| 563 | up_read(&ei->i_data_sem); |
| 564 | return -ENOMEM; |
| 565 | } |
| 566 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 567 | path[0].p_hdr = ext_inode_hdr(inode); |
| 568 | ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); |
| 569 | if (ret) |
| 570 | goto out; |
| 571 | path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); |
| 572 | while (i >= 0) { |
| 573 | /* |
| 574 | * If this is a leaf block or we've reached the end of |
| 575 | * the index block, go up |
| 576 | */ |
| 577 | if ((i == depth) || |
| 578 | path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { |
| 579 | brelse(path[i].p_bh); |
| 580 | path[i].p_bh = NULL; |
| 581 | i--; |
| 582 | continue; |
| 583 | } |
| 584 | bh = read_extent_tree_block(inode, |
| 585 | ext4_idx_pblock(path[i].p_idx++), |
| 586 | depth - i - 1, |
| 587 | EXT4_EX_FORCE_CACHE); |
| 588 | if (IS_ERR(bh)) { |
| 589 | ret = PTR_ERR(bh); |
| 590 | break; |
| 591 | } |
| 592 | i++; |
| 593 | path[i].p_bh = bh; |
| 594 | path[i].p_hdr = ext_block_hdr(bh); |
| 595 | path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); |
| 596 | } |
| 597 | ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); |
| 598 | out: |
| 599 | up_read(&ei->i_data_sem); |
| 600 | ext4_ext_drop_refs(path); |
| 601 | kfree(path); |
| 602 | return ret; |
| 603 | } |
| 604 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 605 | #ifdef EXT_DEBUG |
| 606 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 607 | { |
| 608 | int k, l = path->p_depth; |
| 609 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 610 | ext_debug(inode, "path:"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 611 | for (k = 0; k <= l; k++, path++) { |
| 612 | if (path->p_idx) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 613 | ext_debug(inode, " %d->%llu", |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 614 | le32_to_cpu(path->p_idx->ei_block), |
| 615 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 616 | } else if (path->p_ext) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 617 | ext_debug(inode, " %d:[%d]%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 618 | le32_to_cpu(path->p_ext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 619 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 620 | ext4_ext_get_actual_len(path->p_ext), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 621 | ext4_ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 622 | } else |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 623 | ext_debug(inode, " []"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 624 | } |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 625 | ext_debug(inode, "\n"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 629 | { |
| 630 | int depth = ext_depth(inode); |
| 631 | struct ext4_extent_header *eh; |
| 632 | struct ext4_extent *ex; |
| 633 | int i; |
| 634 | |
| 635 | if (!path) |
| 636 | return; |
| 637 | |
| 638 | eh = path[depth].p_hdr; |
| 639 | ex = EXT_FIRST_EXTENT(eh); |
| 640 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 641 | ext_debug(inode, "Displaying leaf extents\n"); |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 642 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 643 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 644 | ext_debug(inode, "%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 645 | ext4_ext_is_unwritten(ex), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 646 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 647 | } |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 648 | ext_debug(inode, "\n"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 649 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 650 | |
| 651 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 652 | ext4_fsblk_t newblock, int level) |
| 653 | { |
| 654 | int depth = ext_depth(inode); |
| 655 | struct ext4_extent *ex; |
| 656 | |
| 657 | if (depth != level) { |
| 658 | struct ext4_extent_idx *idx; |
| 659 | idx = path[level].p_idx; |
| 660 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 661 | ext_debug(inode, "%d: move %d:%llu in new index %llu\n", |
| 662 | level, le32_to_cpu(idx->ei_block), |
| 663 | ext4_idx_pblock(idx), newblock); |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 664 | idx++; |
| 665 | } |
| 666 | |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | ex = path[depth].p_ext; |
| 671 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 672 | ext_debug(inode, "move %d:%llu:[%d]%d in new leaf %llu\n", |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 673 | le32_to_cpu(ex->ee_block), |
| 674 | ext4_ext_pblock(ex), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 675 | ext4_ext_is_unwritten(ex), |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 676 | ext4_ext_get_actual_len(ex), |
| 677 | newblock); |
| 678 | ex++; |
| 679 | } |
| 680 | } |
| 681 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 682 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 683 | #define ext4_ext_show_path(inode, path) |
| 684 | #define ext4_ext_show_leaf(inode, path) |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 685 | #define ext4_ext_show_move(inode, path, newblock, level) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 686 | #endif |
| 687 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 688 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 689 | { |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 690 | int depth, i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 691 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 692 | if (!path) |
| 693 | return; |
| 694 | depth = path->p_depth; |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 695 | for (i = 0; i <= depth; i++, path++) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 696 | if (path->p_bh) { |
| 697 | brelse(path->p_bh); |
| 698 | path->p_bh = NULL; |
| 699 | } |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 700 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 704 | * ext4_ext_binsearch_idx: |
| 705 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 706 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 707 | */ |
| 708 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 709 | ext4_ext_binsearch_idx(struct inode *inode, |
| 710 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 711 | { |
| 712 | struct ext4_extent_header *eh = path->p_hdr; |
| 713 | struct ext4_extent_idx *r, *l, *m; |
| 714 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 715 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 716 | ext_debug(inode, "binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 717 | |
| 718 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 719 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 720 | while (l <= r) { |
| 721 | m = l + (r - l) / 2; |
| 722 | if (block < le32_to_cpu(m->ei_block)) |
| 723 | r = m - 1; |
| 724 | else |
| 725 | l = m + 1; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 726 | ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, |
| 727 | le32_to_cpu(l->ei_block), m, le32_to_cpu(m->ei_block), |
| 728 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | path->p_idx = l - 1; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 732 | ext_debug(inode, " -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 733 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 734 | |
| 735 | #ifdef CHECK_BINSEARCH |
| 736 | { |
| 737 | struct ext4_extent_idx *chix, *ix; |
| 738 | int k; |
| 739 | |
| 740 | chix = ix = EXT_FIRST_INDEX(eh); |
| 741 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 742 | if (k != 0 && le32_to_cpu(ix->ei_block) <= |
| 743 | le32_to_cpu(ix[-1].ei_block)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 744 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 745 | "first=0x%p\n", k, |
| 746 | ix, EXT_FIRST_INDEX(eh)); |
| 747 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 748 | le32_to_cpu(ix->ei_block), |
| 749 | le32_to_cpu(ix[-1].ei_block)); |
| 750 | } |
| 751 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 752 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 753 | if (block < le32_to_cpu(ix->ei_block)) |
| 754 | break; |
| 755 | chix = ix; |
| 756 | } |
| 757 | BUG_ON(chix != path->p_idx); |
| 758 | } |
| 759 | #endif |
| 760 | |
| 761 | } |
| 762 | |
| 763 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 764 | * ext4_ext_binsearch: |
| 765 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 766 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 767 | */ |
| 768 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 769 | ext4_ext_binsearch(struct inode *inode, |
| 770 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 771 | { |
| 772 | struct ext4_extent_header *eh = path->p_hdr; |
| 773 | struct ext4_extent *r, *l, *m; |
| 774 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 775 | if (eh->eh_entries == 0) { |
| 776 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 777 | * this leaf is empty: |
| 778 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 779 | */ |
| 780 | return; |
| 781 | } |
| 782 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 783 | ext_debug(inode, "binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 784 | |
| 785 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 786 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 787 | |
| 788 | while (l <= r) { |
| 789 | m = l + (r - l) / 2; |
| 790 | if (block < le32_to_cpu(m->ee_block)) |
| 791 | r = m - 1; |
| 792 | else |
| 793 | l = m + 1; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 794 | ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, |
| 795 | le32_to_cpu(l->ee_block), m, le32_to_cpu(m->ee_block), |
| 796 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | path->p_ext = l - 1; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 800 | ext_debug(inode, " -> %d:%llu:[%d]%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 801 | le32_to_cpu(path->p_ext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 802 | ext4_ext_pblock(path->p_ext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 803 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 804 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 805 | |
| 806 | #ifdef CHECK_BINSEARCH |
| 807 | { |
| 808 | struct ext4_extent *chex, *ex; |
| 809 | int k; |
| 810 | |
| 811 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 812 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 813 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 814 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 815 | if (block < le32_to_cpu(ex->ee_block)) |
| 816 | break; |
| 817 | chex = ex; |
| 818 | } |
| 819 | BUG_ON(chex != path->p_ext); |
| 820 | } |
| 821 | #endif |
| 822 | |
| 823 | } |
| 824 | |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 825 | void ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 826 | { |
| 827 | struct ext4_extent_header *eh; |
| 828 | |
| 829 | eh = ext_inode_hdr(inode); |
| 830 | eh->eh_depth = 0; |
| 831 | eh->eh_entries = 0; |
| 832 | eh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 833 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 834 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | struct ext4_ext_path * |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 838 | ext4_find_extent(struct inode *inode, ext4_lblk_t block, |
| 839 | struct ext4_ext_path **orig_path, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 840 | { |
| 841 | struct ext4_extent_header *eh; |
| 842 | struct buffer_head *bh; |
Theodore Ts'o | 705912c | 2014-09-01 14:34:09 -0400 | [diff] [blame] | 843 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
| 844 | short int depth, i, ppos = 0; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 845 | int ret; |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 846 | gfp_t gfp_flags = GFP_NOFS; |
| 847 | |
| 848 | if (flags & EXT4_EX_NOFAIL) |
| 849 | gfp_flags |= __GFP_NOFAIL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 850 | |
| 851 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 852 | depth = ext_depth(inode); |
Theodore Ts'o | bc890a6 | 2018-06-14 12:55:10 -0400 | [diff] [blame] | 853 | if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { |
| 854 | EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", |
| 855 | depth); |
| 856 | ret = -EFSCORRUPTED; |
| 857 | goto err; |
| 858 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 859 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 860 | if (path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 861 | ext4_ext_drop_refs(path); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 862 | if (depth > path[0].p_maxdepth) { |
| 863 | kfree(path); |
| 864 | *orig_path = path = NULL; |
| 865 | } |
| 866 | } |
| 867 | if (!path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 868 | /* account possible depth increase */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 869 | path = kcalloc(depth + 2, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 870 | gfp_flags); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 871 | if (unlikely(!path)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 872 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 873 | path[0].p_maxdepth = depth + 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 874 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 875 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 876 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 877 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 878 | i = depth; |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 879 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) |
| 880 | ext4_cache_extents(inode, eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 881 | /* walk through the tree */ |
| 882 | while (i) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 883 | ext_debug(inode, "depth %d: num %d, max %d\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 884 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 885 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 886 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 887 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 888 | path[ppos].p_depth = i; |
| 889 | path[ppos].p_ext = NULL; |
| 890 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 891 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
| 892 | flags); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 893 | if (IS_ERR(bh)) { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 894 | ret = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 895 | goto err; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 896 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 897 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 898 | eh = ext_block_hdr(bh); |
| 899 | ppos++; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 900 | path[ppos].p_bh = bh; |
| 901 | path[ppos].p_hdr = eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 905 | path[ppos].p_ext = NULL; |
| 906 | path[ppos].p_idx = NULL; |
| 907 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 908 | /* find extent */ |
| 909 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 910 | /* if not an empty leaf */ |
| 911 | if (path[ppos].p_ext) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 912 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 913 | |
| 914 | ext4_ext_show_path(inode, path); |
| 915 | |
| 916 | return path; |
| 917 | |
| 918 | err: |
| 919 | ext4_ext_drop_refs(path); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 920 | kfree(path); |
| 921 | if (orig_path) |
| 922 | *orig_path = NULL; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 923 | return ERR_PTR(ret); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 927 | * ext4_ext_insert_index: |
| 928 | * insert new index [@logical;@ptr] into the block at @curp; |
| 929 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 930 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 931 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 932 | struct ext4_ext_path *curp, |
| 933 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 934 | { |
| 935 | struct ext4_extent_idx *ix; |
| 936 | int len, err; |
| 937 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 938 | err = ext4_ext_get_access(handle, inode, curp); |
| 939 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 940 | return err; |
| 941 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 942 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 943 | EXT4_ERROR_INODE(inode, |
| 944 | "logical %d == ei_block %d!", |
| 945 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 946 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 947 | } |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 948 | |
| 949 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 950 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 951 | EXT4_ERROR_INODE(inode, |
| 952 | "eh_entries %d >= eh_max %d!", |
| 953 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 954 | le16_to_cpu(curp->p_hdr->eh_max)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 955 | return -EFSCORRUPTED; |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 956 | } |
| 957 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 958 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 959 | /* insert after */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 960 | ext_debug(inode, "insert new index %d after: %llu\n", |
| 961 | logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 962 | ix = curp->p_idx + 1; |
| 963 | } else { |
| 964 | /* insert before */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 965 | ext_debug(inode, "insert new index %d before: %llu\n", |
| 966 | logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 967 | ix = curp->p_idx; |
| 968 | } |
| 969 | |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 970 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
| 971 | BUG_ON(len < 0); |
| 972 | if (len > 0) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 973 | ext_debug(inode, "insert new index %d: " |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 974 | "move %d indices from 0x%p to 0x%p\n", |
| 975 | logical, len, ix, ix + 1); |
| 976 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
| 977 | } |
| 978 | |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 979 | if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { |
| 980 | EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 981 | return -EFSCORRUPTED; |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 982 | } |
| 983 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 984 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 985 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 986 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 987 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 988 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 989 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 990 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 991 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 992 | |
| 993 | err = ext4_ext_dirty(handle, inode, curp); |
| 994 | ext4_std_error(inode->i_sb, err); |
| 995 | |
| 996 | return err; |
| 997 | } |
| 998 | |
| 999 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1000 | * ext4_ext_split: |
| 1001 | * inserts new subtree into the path, using free index entry |
| 1002 | * at depth @at: |
| 1003 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 1004 | * - makes decision where to split |
| 1005 | * - moves remaining extents and index entries (right to the split point) |
| 1006 | * into the newly allocated blocks |
| 1007 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1008 | */ |
| 1009 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1010 | unsigned int flags, |
| 1011 | struct ext4_ext_path *path, |
| 1012 | struct ext4_extent *newext, int at) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct buffer_head *bh = NULL; |
| 1015 | int depth = ext_depth(inode); |
| 1016 | struct ext4_extent_header *neh; |
| 1017 | struct ext4_extent_idx *fidx; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1018 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1019 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1020 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1021 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 1022 | gfp_t gfp_flags = GFP_NOFS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1023 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1024 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1025 | |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 1026 | if (flags & EXT4_EX_NOFAIL) |
| 1027 | gfp_flags |= __GFP_NOFAIL; |
| 1028 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1029 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1030 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1031 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1032 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1033 | * border from split point */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1034 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 1035 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1036 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1037 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1038 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 1039 | border = path[depth].p_ext[1].ee_block; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1040 | ext_debug(inode, "leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1041 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1042 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1043 | } else { |
| 1044 | border = newext->ee_block; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1045 | ext_debug(inode, "leaf will be added." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1046 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1047 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1051 | * If error occurs, then we break processing |
| 1052 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1053 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1054 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1055 | */ |
| 1056 | |
| 1057 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1058 | * Get array to track all allocated blocks. |
| 1059 | * We need this to handle errors and free blocks |
| 1060 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1061 | */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 1062 | ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1063 | if (!ablocks) |
| 1064 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1065 | |
| 1066 | /* allocate all needed blocks */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1067 | ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1068 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1069 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1070 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1071 | if (newblock == 0) |
| 1072 | goto cleanup; |
| 1073 | ablocks[a] = newblock; |
| 1074 | } |
| 1075 | |
| 1076 | /* initialize new leaf */ |
| 1077 | newblock = ablocks[--a]; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1078 | if (unlikely(newblock == 0)) { |
| 1079 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1080 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1081 | goto cleanup; |
| 1082 | } |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1083 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1084 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1085 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1086 | goto cleanup; |
| 1087 | } |
| 1088 | lock_buffer(bh); |
| 1089 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1090 | err = ext4_journal_get_create_access(handle, bh); |
| 1091 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1092 | goto cleanup; |
| 1093 | |
| 1094 | neh = ext_block_hdr(bh); |
| 1095 | neh->eh_entries = 0; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1096 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1097 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1098 | neh->eh_depth = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1099 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1100 | /* move remainder of path[depth] to the new leaf */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1101 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 1102 | path[depth].p_hdr->eh_max)) { |
| 1103 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 1104 | path[depth].p_hdr->eh_entries, |
| 1105 | path[depth].p_hdr->eh_max); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1106 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1107 | goto cleanup; |
| 1108 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1109 | /* start copy from next extent */ |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1110 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 1111 | ext4_ext_show_move(inode, path, newblock, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1112 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1113 | struct ext4_extent *ex; |
| 1114 | ex = EXT_FIRST_EXTENT(neh); |
| 1115 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1116 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1119 | /* zero out unused area in the extent block */ |
| 1120 | ext_size = sizeof(struct ext4_extent_header) + |
| 1121 | sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); |
| 1122 | memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1123 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1124 | set_buffer_uptodate(bh); |
| 1125 | unlock_buffer(bh); |
| 1126 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1127 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1128 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1129 | goto cleanup; |
| 1130 | brelse(bh); |
| 1131 | bh = NULL; |
| 1132 | |
| 1133 | /* correct old leaf */ |
| 1134 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1135 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1136 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1137 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1138 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1139 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1140 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1141 | goto cleanup; |
| 1142 | |
| 1143 | } |
| 1144 | |
| 1145 | /* create intermediate indexes */ |
| 1146 | k = depth - at - 1; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1147 | if (unlikely(k < 0)) { |
| 1148 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1149 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1150 | goto cleanup; |
| 1151 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1152 | if (k) |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1153 | ext_debug(inode, "create %d intermediate indices\n", k); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1154 | /* insert new index into current index block */ |
| 1155 | /* current depth stored in i var */ |
| 1156 | i = depth - 1; |
| 1157 | while (k--) { |
| 1158 | oldblock = newblock; |
| 1159 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1160 | bh = sb_getblk(inode->i_sb, newblock); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1161 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1162 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1163 | goto cleanup; |
| 1164 | } |
| 1165 | lock_buffer(bh); |
| 1166 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1167 | err = ext4_journal_get_create_access(handle, bh); |
| 1168 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1169 | goto cleanup; |
| 1170 | |
| 1171 | neh = ext_block_hdr(bh); |
| 1172 | neh->eh_entries = cpu_to_le16(1); |
| 1173 | neh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1174 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1175 | neh->eh_depth = cpu_to_le16(depth - i); |
| 1176 | fidx = EXT_FIRST_INDEX(neh); |
| 1177 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1178 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1179 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1180 | ext_debug(inode, "int.index at %d (block %llu): %u -> %llu\n", |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1181 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1182 | |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1183 | /* move remainder of path[i] to the new index block */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1184 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 1185 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 1186 | EXT4_ERROR_INODE(inode, |
| 1187 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 1188 | le32_to_cpu(path[i].p_ext->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1189 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1190 | goto cleanup; |
| 1191 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1192 | /* start copy indexes */ |
| 1193 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1194 | ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx, |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1195 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 1196 | ext4_ext_show_move(inode, path, newblock, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1197 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1198 | memmove(++fidx, path[i].p_idx, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1199 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1200 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1201 | } |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1202 | /* zero out unused area in the extent block */ |
| 1203 | ext_size = sizeof(struct ext4_extent_header) + |
| 1204 | (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); |
| 1205 | memset(bh->b_data + ext_size, 0, |
| 1206 | inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1207 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1208 | set_buffer_uptodate(bh); |
| 1209 | unlock_buffer(bh); |
| 1210 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1211 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1212 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1213 | goto cleanup; |
| 1214 | brelse(bh); |
| 1215 | bh = NULL; |
| 1216 | |
| 1217 | /* correct old index */ |
| 1218 | if (m) { |
| 1219 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1220 | if (err) |
| 1221 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1222 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1223 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1224 | if (err) |
| 1225 | goto cleanup; |
| 1226 | } |
| 1227 | |
| 1228 | i--; |
| 1229 | } |
| 1230 | |
| 1231 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1232 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1233 | le32_to_cpu(border), newblock); |
| 1234 | |
| 1235 | cleanup: |
| 1236 | if (bh) { |
| 1237 | if (buffer_locked(bh)) |
| 1238 | unlock_buffer(bh); |
| 1239 | brelse(bh); |
| 1240 | } |
| 1241 | |
| 1242 | if (err) { |
| 1243 | /* free all allocated blocks in error case */ |
| 1244 | for (i = 0; i < depth; i++) { |
| 1245 | if (!ablocks[i]) |
| 1246 | continue; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1247 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1248 | EXT4_FREE_BLOCKS_METADATA); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | kfree(ablocks); |
| 1252 | |
| 1253 | return err; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1257 | * ext4_ext_grow_indepth: |
| 1258 | * implements tree growing procedure: |
| 1259 | * - allocates new block |
| 1260 | * - moves top-level data (index block or leaf) into the new block |
| 1261 | * - initializes new top-level, creating index that points to the |
| 1262 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1263 | */ |
| 1264 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1265 | unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1266 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1267 | struct ext4_extent_header *neh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1268 | struct buffer_head *bh; |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1269 | ext4_fsblk_t newblock, goal = 0; |
| 1270 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1271 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1272 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1273 | |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1274 | /* Try to prepend new index to old one */ |
| 1275 | if (ext_depth(inode)) |
| 1276 | goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); |
| 1277 | if (goal > le32_to_cpu(es->s_first_data_block)) { |
| 1278 | flags |= EXT4_MB_HINT_TRY_GOAL; |
| 1279 | goal--; |
| 1280 | } else |
| 1281 | goal = ext4_inode_to_goal_block(inode); |
| 1282 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 1283 | NULL, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1284 | if (newblock == 0) |
| 1285 | return err; |
| 1286 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1287 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1288 | if (unlikely(!bh)) |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1289 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1290 | lock_buffer(bh); |
| 1291 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1292 | err = ext4_journal_get_create_access(handle, bh); |
| 1293 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1294 | unlock_buffer(bh); |
| 1295 | goto out; |
| 1296 | } |
| 1297 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1298 | ext_size = sizeof(EXT4_I(inode)->i_data); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1299 | /* move top-level index/leaf into new block */ |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1300 | memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); |
| 1301 | /* zero out unused area in the extent block */ |
| 1302 | memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1303 | |
| 1304 | /* set size of new block */ |
| 1305 | neh = ext_block_hdr(bh); |
| 1306 | /* old root could have indexes or leaves |
| 1307 | * so calculate e_max right way */ |
| 1308 | if (ext_depth(inode)) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1309 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1310 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1311 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1312 | neh->eh_magic = EXT4_EXT_MAGIC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1313 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1314 | set_buffer_uptodate(bh); |
| 1315 | unlock_buffer(bh); |
| 1316 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1317 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1318 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1319 | goto out; |
| 1320 | |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1321 | /* Update top-level index: num,max,pointer */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1322 | neh = ext_inode_hdr(inode); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1323 | neh->eh_entries = cpu_to_le16(1); |
| 1324 | ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); |
| 1325 | if (neh->eh_depth == 0) { |
| 1326 | /* Root extent block becomes index block */ |
| 1327 | neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
| 1328 | EXT_FIRST_INDEX(neh)->ei_block = |
| 1329 | EXT_FIRST_EXTENT(neh)->ee_block; |
| 1330 | } |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1331 | ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1332 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1333 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1334 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1335 | |
Wei Yongjun | ba39ebb | 2012-09-27 09:37:53 -0400 | [diff] [blame] | 1336 | le16_add_cpu(&neh->eh_depth, 1); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 1337 | err = ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1338 | out: |
| 1339 | brelse(bh); |
| 1340 | |
| 1341 | return err; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1345 | * ext4_ext_create_new_leaf: |
| 1346 | * finds empty index and adds new leaf. |
| 1347 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1348 | */ |
| 1349 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1350 | unsigned int mb_flags, |
| 1351 | unsigned int gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1352 | struct ext4_ext_path **ppath, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1353 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1354 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1355 | struct ext4_ext_path *path = *ppath; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1356 | struct ext4_ext_path *curp; |
| 1357 | int depth, i, err = 0; |
| 1358 | |
| 1359 | repeat: |
| 1360 | i = depth = ext_depth(inode); |
| 1361 | |
| 1362 | /* walk up to the tree and look for free index entry */ |
| 1363 | curp = path + depth; |
| 1364 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1365 | i--; |
| 1366 | curp--; |
| 1367 | } |
| 1368 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1369 | /* we use already allocated block for index block, |
| 1370 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1371 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1372 | /* if we found index with free entry, then use that |
| 1373 | * entry: create all needed subtree and add new leaf */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1374 | err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1375 | if (err) |
| 1376 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1379 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1380 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1381 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1382 | if (IS_ERR(path)) |
| 1383 | err = PTR_ERR(path); |
| 1384 | } else { |
| 1385 | /* tree is full, time to grow in depth */ |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1386 | err = ext4_ext_grow_indepth(handle, inode, mb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1387 | if (err) |
| 1388 | goto out; |
| 1389 | |
| 1390 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1391 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1392 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1393 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1394 | if (IS_ERR(path)) { |
| 1395 | err = PTR_ERR(path); |
| 1396 | goto out; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1400 | * only first (depth 0 -> 1) produces free space; |
| 1401 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1402 | */ |
| 1403 | depth = ext_depth(inode); |
| 1404 | if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1405 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1406 | goto repeat; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | out: |
| 1411 | return err; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1415 | * search the closest allocated block to the left for *logical |
| 1416 | * and returns it at @logical + it's physical address at @phys |
| 1417 | * if *logical is the smallest allocated block, the function |
| 1418 | * returns 0 at @phys |
| 1419 | * return value contains 0 (success) or error code |
| 1420 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1421 | static int ext4_ext_search_left(struct inode *inode, |
| 1422 | struct ext4_ext_path *path, |
| 1423 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1424 | { |
| 1425 | struct ext4_extent_idx *ix; |
| 1426 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1427 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1428 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1429 | if (unlikely(path == NULL)) { |
| 1430 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1431 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1432 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1433 | depth = path->p_depth; |
| 1434 | *phys = 0; |
| 1435 | |
| 1436 | if (depth == 0 && path->p_ext == NULL) |
| 1437 | return 0; |
| 1438 | |
| 1439 | /* usually extent in the path covers blocks smaller |
| 1440 | * then *logical, but it can be that extent is the |
| 1441 | * first one in the file */ |
| 1442 | |
| 1443 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1444 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1445 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1446 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1447 | EXT4_ERROR_INODE(inode, |
| 1448 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1449 | *logical, le32_to_cpu(ex->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1450 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1451 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1452 | while (--depth >= 0) { |
| 1453 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1454 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1455 | EXT4_ERROR_INODE(inode, |
| 1456 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1457 | ix != NULL ? le32_to_cpu(ix->ei_block) : 0, |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1458 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1459 | le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0, |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1460 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1461 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1462 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1463 | } |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1467 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1468 | EXT4_ERROR_INODE(inode, |
| 1469 | "logical %d < ee_block %d + ee_len %d!", |
| 1470 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1471 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1472 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1473 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1474 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1475 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * search the closest allocated block to the right for *logical |
| 1481 | * and returns it at @logical + it's physical address at @phys |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 1482 | * if *logical is the largest allocated block, the function |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1483 | * returns 0 at @phys |
| 1484 | * return value contains 0 (success) or error code |
| 1485 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1486 | static int ext4_ext_search_right(struct inode *inode, |
| 1487 | struct ext4_ext_path *path, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1488 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1489 | struct ext4_extent **ret_ex) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1490 | { |
| 1491 | struct buffer_head *bh = NULL; |
| 1492 | struct ext4_extent_header *eh; |
| 1493 | struct ext4_extent_idx *ix; |
| 1494 | struct ext4_extent *ex; |
| 1495 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1496 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1497 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1498 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1499 | if (unlikely(path == NULL)) { |
| 1500 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1501 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1502 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1503 | depth = path->p_depth; |
| 1504 | *phys = 0; |
| 1505 | |
| 1506 | if (depth == 0 && path->p_ext == NULL) |
| 1507 | return 0; |
| 1508 | |
| 1509 | /* usually extent in the path covers blocks smaller |
| 1510 | * then *logical, but it can be that extent is the |
| 1511 | * first one in the file */ |
| 1512 | |
| 1513 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1514 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1515 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1516 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1517 | EXT4_ERROR_INODE(inode, |
| 1518 | "first_extent(path[%d].p_hdr) != ex", |
| 1519 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1520 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1521 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1522 | while (--depth >= 0) { |
| 1523 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1524 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1525 | EXT4_ERROR_INODE(inode, |
| 1526 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1527 | *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1528 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1529 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1530 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1531 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1532 | } |
| 1533 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1534 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1535 | EXT4_ERROR_INODE(inode, |
| 1536 | "logical %d < ee_block %d + ee_len %d!", |
| 1537 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1538 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1539 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1540 | |
| 1541 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1542 | /* next allocated block in this leaf */ |
| 1543 | ex++; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1544 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* go up and search for index to the right */ |
| 1548 | while (--depth >= 0) { |
| 1549 | ix = path[depth].p_idx; |
| 1550 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1551 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1552 | } |
| 1553 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1554 | /* we've gone up to the root and found no index to the right */ |
| 1555 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1556 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1557 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1558 | /* we've found index to the right, let's |
| 1559 | * follow it and find the closest allocated |
| 1560 | * block to the right */ |
| 1561 | ix++; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1562 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1563 | while (++depth < path->p_depth) { |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1564 | /* subtract from p_depth to get proper eh_depth */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1565 | bh = read_extent_tree_block(inode, block, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1566 | path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1567 | if (IS_ERR(bh)) |
| 1568 | return PTR_ERR(bh); |
| 1569 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1570 | ix = EXT_FIRST_INDEX(eh); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1571 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1572 | put_bh(bh); |
| 1573 | } |
| 1574 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1575 | bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1576 | if (IS_ERR(bh)) |
| 1577 | return PTR_ERR(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1578 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1579 | ex = EXT_FIRST_EXTENT(eh); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1580 | found_extent: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1581 | *logical = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1582 | *phys = ext4_ext_pblock(ex); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1583 | *ret_ex = ex; |
| 1584 | if (bh) |
| 1585 | put_bh(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1586 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1590 | * ext4_ext_next_allocated_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1591 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1592 | * NOTE: it considers block number from index entry as |
| 1593 | * allocated block. Thus, index entries have to be consistent |
| 1594 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1595 | */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 1596 | ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1597 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1598 | { |
| 1599 | int depth; |
| 1600 | |
| 1601 | BUG_ON(path == NULL); |
| 1602 | depth = path->p_depth; |
| 1603 | |
| 1604 | if (depth == 0 && path->p_ext == NULL) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1605 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1606 | |
| 1607 | while (depth >= 0) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1608 | struct ext4_ext_path *p = &path[depth]; |
| 1609 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1610 | if (depth == path->p_depth) { |
| 1611 | /* leaf */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1612 | if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) |
| 1613 | return le32_to_cpu(p->p_ext[1].ee_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1614 | } else { |
| 1615 | /* index */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1616 | if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) |
| 1617 | return le32_to_cpu(p->p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1618 | } |
| 1619 | depth--; |
| 1620 | } |
| 1621 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1622 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1626 | * ext4_ext_next_leaf_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1627 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1628 | */ |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1629 | static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1630 | { |
| 1631 | int depth; |
| 1632 | |
| 1633 | BUG_ON(path == NULL); |
| 1634 | depth = path->p_depth; |
| 1635 | |
| 1636 | /* zero-tree has no leaf blocks at all */ |
| 1637 | if (depth == 0) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1638 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1639 | |
| 1640 | /* go to index block */ |
| 1641 | depth--; |
| 1642 | |
| 1643 | while (depth >= 0) { |
| 1644 | if (path[depth].p_idx != |
| 1645 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1646 | return (ext4_lblk_t) |
| 1647 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1648 | depth--; |
| 1649 | } |
| 1650 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1651 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1655 | * ext4_ext_correct_indexes: |
| 1656 | * if leaf gets modified and modified extent is first in the leaf, |
| 1657 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1658 | * TODO: do we need to correct tree in all cases? |
| 1659 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1660 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1661 | struct ext4_ext_path *path) |
| 1662 | { |
| 1663 | struct ext4_extent_header *eh; |
| 1664 | int depth = ext_depth(inode); |
| 1665 | struct ext4_extent *ex; |
| 1666 | __le32 border; |
| 1667 | int k, err = 0; |
| 1668 | |
| 1669 | eh = path[depth].p_hdr; |
| 1670 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1671 | |
| 1672 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1673 | EXT4_ERROR_INODE(inode, |
| 1674 | "ex %p == NULL or eh %p == NULL", ex, eh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1675 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1676 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1677 | |
| 1678 | if (depth == 0) { |
| 1679 | /* there is no tree at all */ |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1684 | /* we correct tree if first leaf got modified only */ |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1689 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1690 | */ |
| 1691 | k = depth - 1; |
| 1692 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1693 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1694 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1695 | return err; |
| 1696 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1697 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1698 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1699 | return err; |
| 1700 | |
| 1701 | while (k--) { |
| 1702 | /* change all left-side indexes */ |
| 1703 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1704 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1705 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1706 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1707 | break; |
| 1708 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1709 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1710 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1711 | break; |
| 1712 | } |
| 1713 | |
| 1714 | return err; |
| 1715 | } |
| 1716 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 1717 | static int ext4_can_extents_be_merged(struct inode *inode, |
| 1718 | struct ext4_extent *ex1, |
| 1719 | struct ext4_extent *ex2) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1720 | { |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1721 | unsigned short ext1_ee_len, ext2_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1722 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1723 | if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1724 | return 0; |
| 1725 | |
| 1726 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1727 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1728 | |
| 1729 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1730 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1731 | return 0; |
| 1732 | |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1733 | if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1734 | return 0; |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1735 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1736 | if (ext4_ext_is_unwritten(ex1) && |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1737 | ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) |
Darrick J. Wong | a9b8241 | 2014-02-20 21:17:35 -0500 | [diff] [blame] | 1738 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1739 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1740 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1741 | return 0; |
| 1742 | #endif |
| 1743 | |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1744 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1745 | return 1; |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1750 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1751 | * It always tries to merge towards right. If you want to merge towards |
| 1752 | * left, pass "ex - 1" as argument instead of "ex". |
| 1753 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1754 | * 1 if they got merged. |
| 1755 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1756 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1757 | struct ext4_ext_path *path, |
| 1758 | struct ext4_extent *ex) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1759 | { |
| 1760 | struct ext4_extent_header *eh; |
| 1761 | unsigned int depth, len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1762 | int merge_done = 0, unwritten; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1763 | |
| 1764 | depth = ext_depth(inode); |
| 1765 | BUG_ON(path[depth].p_hdr == NULL); |
| 1766 | eh = path[depth].p_hdr; |
| 1767 | |
| 1768 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1769 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1770 | break; |
| 1771 | /* merge with next extent! */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1772 | unwritten = ext4_ext_is_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1773 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1774 | + ext4_ext_get_actual_len(ex + 1)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1775 | if (unwritten) |
| 1776 | ext4_ext_mark_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1777 | |
| 1778 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1779 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1780 | * sizeof(struct ext4_extent); |
| 1781 | memmove(ex + 1, ex + 2, len); |
| 1782 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1783 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1784 | merge_done = 1; |
| 1785 | WARN_ON(eh->eh_entries == 0); |
| 1786 | if (!eh->eh_entries) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1787 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | return merge_done; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1794 | * This function does a very simple check to see if we can collapse |
| 1795 | * an extent tree with a single extent tree leaf block into the inode. |
| 1796 | */ |
| 1797 | static void ext4_ext_try_to_merge_up(handle_t *handle, |
| 1798 | struct inode *inode, |
| 1799 | struct ext4_ext_path *path) |
| 1800 | { |
| 1801 | size_t s; |
| 1802 | unsigned max_root = ext4_ext_space_root(inode, 0); |
| 1803 | ext4_fsblk_t blk; |
| 1804 | |
| 1805 | if ((path[0].p_depth != 1) || |
| 1806 | (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || |
| 1807 | (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) |
| 1808 | return; |
| 1809 | |
| 1810 | /* |
| 1811 | * We need to modify the block allocation bitmap and the block |
| 1812 | * group descriptor to release the extent tree block. If we |
| 1813 | * can't get the journal credits, give up. |
| 1814 | */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 1815 | if (ext4_journal_extend(handle, 2, |
| 1816 | ext4_free_metadata_revoke_credits(inode->i_sb, 1))) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1817 | return; |
| 1818 | |
| 1819 | /* |
| 1820 | * Copy the extent data up to the inode |
| 1821 | */ |
| 1822 | blk = ext4_idx_pblock(path[0].p_idx); |
| 1823 | s = le16_to_cpu(path[1].p_hdr->eh_entries) * |
| 1824 | sizeof(struct ext4_extent_idx); |
| 1825 | s += sizeof(struct ext4_extent_header); |
| 1826 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 1827 | path[1].p_maxdepth = path[0].p_maxdepth; |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1828 | memcpy(path[0].p_hdr, path[1].p_hdr, s); |
| 1829 | path[0].p_depth = 0; |
| 1830 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + |
| 1831 | (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); |
| 1832 | path[0].p_hdr->eh_max = cpu_to_le16(max_root); |
| 1833 | |
| 1834 | brelse(path[1].p_bh); |
| 1835 | ext4_free_blocks(handle, inode, NULL, blk, 1, |
Theodore Ts'o | 71d4f7d | 2014-07-15 06:02:38 -0400 | [diff] [blame] | 1836 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | /* |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1840 | * This function tries to merge the @ex extent to neighbours in the tree, then |
| 1841 | * tries to collapse the extent tree into the inode. |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1842 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1843 | static void ext4_ext_try_to_merge(handle_t *handle, |
| 1844 | struct inode *inode, |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1845 | struct ext4_ext_path *path, |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1846 | struct ext4_extent *ex) |
| 1847 | { |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1848 | struct ext4_extent_header *eh; |
| 1849 | unsigned int depth; |
| 1850 | int merge_done = 0; |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1851 | |
| 1852 | depth = ext_depth(inode); |
| 1853 | BUG_ON(path[depth].p_hdr == NULL); |
| 1854 | eh = path[depth].p_hdr; |
| 1855 | |
| 1856 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1857 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1858 | |
| 1859 | if (!merge_done) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1860 | (void) ext4_ext_try_to_merge_right(inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1861 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1862 | ext4_ext_try_to_merge_up(handle, inode, path); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1866 | * check if a portion of the "newext" extent overlaps with an |
| 1867 | * existing extent. |
| 1868 | * |
| 1869 | * If there is an overlap discovered, it updates the length of the newext |
| 1870 | * such that there will be no overlap, and then returns 1. |
| 1871 | * If there is no overlap found, it returns 0. |
| 1872 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1873 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1874 | struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1875 | struct ext4_extent *newext, |
| 1876 | struct ext4_ext_path *path) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1877 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1878 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1879 | unsigned int depth, len1; |
| 1880 | unsigned int ret = 0; |
| 1881 | |
| 1882 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1883 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1884 | depth = ext_depth(inode); |
| 1885 | if (!path[depth].p_ext) |
| 1886 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1887 | b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block)); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1888 | |
| 1889 | /* |
| 1890 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1891 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1892 | */ |
| 1893 | if (b2 < b1) { |
| 1894 | b2 = ext4_ext_next_allocated_block(path); |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1895 | if (b2 == EXT_MAX_BLOCKS) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1896 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1897 | b2 = EXT4_LBLK_CMASK(sbi, b2); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1898 | } |
| 1899 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1900 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1901 | if (b1 + len1 < b1) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1902 | len1 = EXT_MAX_BLOCKS - b1; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1903 | newext->ee_len = cpu_to_le16(len1); |
| 1904 | ret = 1; |
| 1905 | } |
| 1906 | |
| 1907 | /* check for overlap */ |
| 1908 | if (b1 + len1 > b2) { |
| 1909 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1910 | ret = 1; |
| 1911 | } |
| 1912 | out: |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1917 | * ext4_ext_insert_extent: |
| 1918 | * tries to merge requsted extent into the existing extent or |
| 1919 | * inserts requested extent as new one into the tree, |
| 1920 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1921 | */ |
| 1922 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1923 | struct ext4_ext_path **ppath, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1924 | struct ext4_extent *newext, int gb_flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1925 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1926 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1927 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1928 | struct ext4_extent *ex, *fex; |
| 1929 | struct ext4_extent *nearex; /* nearest extent */ |
| 1930 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1931 | int depth, len, err; |
| 1932 | ext4_lblk_t next; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1933 | int mb_flags = 0, unwritten; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1934 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 1935 | if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 1936 | mb_flags |= EXT4_MB_DELALLOC_RESERVED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1937 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1938 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1939 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1940 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1941 | depth = ext_depth(inode); |
| 1942 | ex = path[depth].p_ext; |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1943 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1944 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1945 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1946 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1947 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1948 | |
| 1949 | /* try to insert block into found extent and return */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1950 | if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1951 | |
| 1952 | /* |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1953 | * Try to see whether we should rather test the extent on |
| 1954 | * right from ex, or from the left of ex. This is because |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1955 | * ext4_find_extent() can return either extent on the |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1956 | * left, or on the right from the searched position. This |
| 1957 | * will make merging more effective. |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1958 | */ |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1959 | if (ex < EXT_LAST_EXTENT(eh) && |
| 1960 | (le32_to_cpu(ex->ee_block) + |
| 1961 | ext4_ext_get_actual_len(ex) < |
| 1962 | le32_to_cpu(newext->ee_block))) { |
| 1963 | ex += 1; |
| 1964 | goto prepend; |
| 1965 | } else if ((ex > EXT_FIRST_EXTENT(eh)) && |
| 1966 | (le32_to_cpu(newext->ee_block) + |
| 1967 | ext4_ext_get_actual_len(newext) < |
| 1968 | le32_to_cpu(ex->ee_block))) |
| 1969 | ex -= 1; |
| 1970 | |
| 1971 | /* Try to append newex to the ex */ |
| 1972 | if (ext4_can_extents_be_merged(inode, ex, newext)) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1973 | ext_debug(inode, "append [%d]%d block to %u:[%d]%d" |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1974 | "(from %llu)\n", |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1975 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1976 | ext4_ext_get_actual_len(newext), |
| 1977 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1978 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1979 | ext4_ext_get_actual_len(ex), |
| 1980 | ext4_ext_pblock(ex)); |
| 1981 | err = ext4_ext_get_access(handle, inode, |
| 1982 | path + depth); |
| 1983 | if (err) |
| 1984 | return err; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1985 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1986 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1987 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1988 | if (unwritten) |
| 1989 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1990 | eh = path[depth].p_hdr; |
| 1991 | nearex = ex; |
| 1992 | goto merge; |
| 1993 | } |
| 1994 | |
| 1995 | prepend: |
| 1996 | /* Try to prepend newex to the ex */ |
| 1997 | if (ext4_can_extents_be_merged(inode, newext, ex)) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 1998 | ext_debug(inode, "prepend %u[%d]%d block to %u:[%d]%d" |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1999 | "(from %llu)\n", |
| 2000 | le32_to_cpu(newext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2001 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2002 | ext4_ext_get_actual_len(newext), |
| 2003 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2004 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2005 | ext4_ext_get_actual_len(ex), |
| 2006 | ext4_ext_pblock(ex)); |
| 2007 | err = ext4_ext_get_access(handle, inode, |
| 2008 | path + depth); |
| 2009 | if (err) |
| 2010 | return err; |
| 2011 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2012 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2013 | ex->ee_block = newext->ee_block; |
| 2014 | ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); |
| 2015 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 2016 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2017 | if (unwritten) |
| 2018 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2019 | eh = path[depth].p_hdr; |
| 2020 | nearex = ex; |
| 2021 | goto merge; |
| 2022 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2025 | depth = ext_depth(inode); |
| 2026 | eh = path[depth].p_hdr; |
| 2027 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 2028 | goto has_space; |
| 2029 | |
| 2030 | /* probably next leaf has space for us? */ |
| 2031 | fex = EXT_LAST_EXTENT(eh); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2032 | next = EXT_MAX_BLOCKS; |
| 2033 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 2034 | next = ext4_ext_next_leaf_block(path); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2035 | if (next != EXT_MAX_BLOCKS) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2036 | ext_debug(inode, "next leaf block - %u\n", next); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2037 | BUG_ON(npath != NULL); |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 2038 | npath = ext4_find_extent(inode, next, NULL, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2039 | if (IS_ERR(npath)) |
| 2040 | return PTR_ERR(npath); |
| 2041 | BUG_ON(npath->p_depth != path->p_depth); |
| 2042 | eh = npath[depth].p_hdr; |
| 2043 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2044 | ext_debug(inode, "next leaf isn't full(%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2045 | le16_to_cpu(eh->eh_entries)); |
| 2046 | path = npath; |
Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 2047 | goto has_space; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2048 | } |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2049 | ext_debug(inode, "next leaf has no free space(%d,%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2050 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 2051 | } |
| 2052 | |
| 2053 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2054 | * There is no free space in the found leaf. |
| 2055 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2056 | */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2057 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 2058 | mb_flags |= EXT4_MB_USE_RESERVED; |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2059 | err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2060 | ppath, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2061 | if (err) |
| 2062 | goto cleanup; |
| 2063 | depth = ext_depth(inode); |
| 2064 | eh = path[depth].p_hdr; |
| 2065 | |
| 2066 | has_space: |
| 2067 | nearex = path[depth].p_ext; |
| 2068 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2069 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2070 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2071 | goto cleanup; |
| 2072 | |
| 2073 | if (!nearex) { |
| 2074 | /* there is no extent in this leaf, create first one */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2075 | ext_debug(inode, "first extent in the leaf: %u:%llu:[%d]%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2076 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2077 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2078 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2079 | ext4_ext_get_actual_len(newext)); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2080 | nearex = EXT_FIRST_EXTENT(eh); |
| 2081 | } else { |
| 2082 | if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2083 | > le32_to_cpu(nearex->ee_block)) { |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2084 | /* Insert after */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2085 | ext_debug(inode, "insert %u:%llu:[%d]%d before: " |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2086 | "nearest %p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2087 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2088 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2089 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2090 | ext4_ext_get_actual_len(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2091 | nearex); |
| 2092 | nearex++; |
| 2093 | } else { |
| 2094 | /* Insert before */ |
| 2095 | BUG_ON(newext->ee_block == nearex->ee_block); |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2096 | ext_debug(inode, "insert %u:%llu:[%d]%d after: " |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2097 | "nearest %p\n", |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2098 | le32_to_cpu(newext->ee_block), |
| 2099 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2100 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2101 | ext4_ext_get_actual_len(newext), |
| 2102 | nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2103 | } |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2104 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
| 2105 | if (len > 0) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2106 | ext_debug(inode, "insert %u:%llu:[%d]%d: " |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2107 | "move %d extents from 0x%p to 0x%p\n", |
| 2108 | le32_to_cpu(newext->ee_block), |
| 2109 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2110 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2111 | ext4_ext_get_actual_len(newext), |
| 2112 | len, nearex, nearex + 1); |
| 2113 | memmove(nearex + 1, nearex, |
| 2114 | len * sizeof(struct ext4_extent)); |
| 2115 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2118 | le16_add_cpu(&eh->eh_entries, 1); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2119 | path[depth].p_ext = nearex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2120 | nearex->ee_block = newext->ee_block; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2121 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2122 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2123 | |
| 2124 | merge: |
HaiboLiu | e7bcf82 | 2012-07-09 16:29:28 -0400 | [diff] [blame] | 2125 | /* try to merge extents */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2126 | if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2127 | ext4_ext_try_to_merge(handle, inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2128 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2129 | |
| 2130 | /* time to correct all indexes above */ |
| 2131 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2132 | if (err) |
| 2133 | goto cleanup; |
| 2134 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2135 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2136 | |
| 2137 | cleanup: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2138 | ext4_ext_drop_refs(npath); |
| 2139 | kfree(npath); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2140 | return err; |
| 2141 | } |
| 2142 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 2143 | static int ext4_fill_es_cache_info(struct inode *inode, |
| 2144 | ext4_lblk_t block, ext4_lblk_t num, |
| 2145 | struct fiemap_extent_info *fieinfo) |
| 2146 | { |
| 2147 | ext4_lblk_t next, end = block + num - 1; |
| 2148 | struct extent_status es; |
| 2149 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
| 2150 | unsigned int flags; |
| 2151 | int err; |
| 2152 | |
| 2153 | while (block <= end) { |
| 2154 | next = 0; |
| 2155 | flags = 0; |
| 2156 | if (!ext4_es_lookup_extent(inode, block, &next, &es)) |
| 2157 | break; |
| 2158 | if (ext4_es_is_unwritten(&es)) |
| 2159 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 2160 | if (ext4_es_is_delayed(&es)) |
| 2161 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 2162 | FIEMAP_EXTENT_UNKNOWN); |
| 2163 | if (ext4_es_is_hole(&es)) |
| 2164 | flags |= EXT4_FIEMAP_EXTENT_HOLE; |
| 2165 | if (next == 0) |
| 2166 | flags |= FIEMAP_EXTENT_LAST; |
| 2167 | if (flags & (FIEMAP_EXTENT_DELALLOC| |
| 2168 | EXT4_FIEMAP_EXTENT_HOLE)) |
| 2169 | es.es_pblk = 0; |
| 2170 | else |
| 2171 | es.es_pblk = ext4_es_pblock(&es); |
| 2172 | err = fiemap_fill_next_extent(fieinfo, |
| 2173 | (__u64)es.es_lblk << blksize_bits, |
| 2174 | (__u64)es.es_pblk << blksize_bits, |
| 2175 | (__u64)es.es_len << blksize_bits, |
| 2176 | flags); |
| 2177 | if (next == 0) |
| 2178 | break; |
| 2179 | block = next; |
| 2180 | if (err < 0) |
| 2181 | return err; |
| 2182 | if (err == 1) |
| 2183 | return 0; |
| 2184 | } |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2189 | /* |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2190 | * ext4_ext_determine_hole - determine hole around given block |
| 2191 | * @inode: inode we lookup in |
| 2192 | * @path: path in extent tree to @lblk |
| 2193 | * @lblk: pointer to logical block around which we want to determine hole |
| 2194 | * |
| 2195 | * Determine hole length (and start if easily possible) around given logical |
| 2196 | * block. We don't try too hard to find the beginning of the hole but @path |
| 2197 | * actually points to extent before @lblk, we provide it. |
| 2198 | * |
| 2199 | * The function returns the length of a hole starting at @lblk. We update @lblk |
| 2200 | * to the beginning of the hole if we managed to find it. |
| 2201 | */ |
| 2202 | static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, |
| 2203 | struct ext4_ext_path *path, |
| 2204 | ext4_lblk_t *lblk) |
| 2205 | { |
| 2206 | int depth = ext_depth(inode); |
| 2207 | struct ext4_extent *ex; |
| 2208 | ext4_lblk_t len; |
| 2209 | |
| 2210 | ex = path[depth].p_ext; |
| 2211 | if (ex == NULL) { |
| 2212 | /* there is no extent yet, so gap is [0;-] */ |
| 2213 | *lblk = 0; |
| 2214 | len = EXT_MAX_BLOCKS; |
| 2215 | } else if (*lblk < le32_to_cpu(ex->ee_block)) { |
| 2216 | len = le32_to_cpu(ex->ee_block) - *lblk; |
| 2217 | } else if (*lblk >= le32_to_cpu(ex->ee_block) |
| 2218 | + ext4_ext_get_actual_len(ex)) { |
| 2219 | ext4_lblk_t next; |
| 2220 | |
| 2221 | *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
| 2222 | next = ext4_ext_next_allocated_block(path); |
| 2223 | BUG_ON(next == *lblk); |
| 2224 | len = next - *lblk; |
| 2225 | } else { |
| 2226 | BUG(); |
| 2227 | } |
| 2228 | return len; |
| 2229 | } |
| 2230 | |
| 2231 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2232 | * ext4_ext_put_gap_in_cache: |
| 2233 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2234 | * and cache this gap |
| 2235 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2236 | static void |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2237 | ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, |
| 2238 | ext4_lblk_t hole_len) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2239 | { |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2240 | struct extent_status es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2241 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 2242 | ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, |
| 2243 | hole_start + hole_len - 1, &es); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2244 | if (es.es_len) { |
| 2245 | /* There's delayed extent containing lblock? */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2246 | if (es.es_lblk <= hole_start) |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2247 | return; |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2248 | hole_len = min(es.es_lblk - hole_start, hole_len); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2249 | } |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2250 | ext_debug(inode, " -> %u:%u\n", hole_start, hole_len); |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2251 | ext4_es_insert_extent(inode, hole_start, hole_len, ~0, |
| 2252 | EXTENT_STATUS_HOLE); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2256 | * ext4_ext_rm_idx: |
| 2257 | * removes index from the index block. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2258 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2259 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2260 | struct ext4_ext_path *path, int depth) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2261 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2262 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2263 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2264 | |
| 2265 | /* free index block */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2266 | depth--; |
| 2267 | path = path + depth; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2268 | leaf = ext4_idx_pblock(path->p_idx); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2269 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2270 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2271 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2272 | } |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2273 | err = ext4_ext_get_access(handle, inode, path); |
| 2274 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2275 | return err; |
Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2276 | |
| 2277 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2278 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2279 | len *= sizeof(struct ext4_extent_idx); |
| 2280 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2281 | } |
| 2282 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2283 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2284 | err = ext4_ext_dirty(handle, inode, path); |
| 2285 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2286 | return err; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2287 | ext_debug(inode, "index is empty, remove it, free block %llu\n", leaf); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2288 | trace_ext4_ext_rm_idx(inode, leaf); |
| 2289 | |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2290 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2291 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2292 | |
| 2293 | while (--depth >= 0) { |
| 2294 | if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) |
| 2295 | break; |
| 2296 | path--; |
| 2297 | err = ext4_ext_get_access(handle, inode, path); |
| 2298 | if (err) |
| 2299 | break; |
| 2300 | path->p_idx->ei_block = (path+1)->p_idx->ei_block; |
| 2301 | err = ext4_ext_dirty(handle, inode, path); |
| 2302 | if (err) |
| 2303 | break; |
| 2304 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2305 | return err; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2309 | * ext4_ext_calc_credits_for_single_extent: |
| 2310 | * This routine returns max. credits that needed to insert an extent |
| 2311 | * to the extent tree. |
| 2312 | * When pass the actual path, the caller should calculate credits |
| 2313 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2314 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2315 | int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2316 | struct ext4_ext_path *path) |
| 2317 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2318 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2319 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2320 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2321 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2322 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2323 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2324 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2325 | |
| 2326 | /* |
| 2327 | * There are some space in the leaf tree, no |
| 2328 | * need to account for leaf block credit |
| 2329 | * |
| 2330 | * bitmaps and block group descriptor blocks |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 2331 | * and other metadata blocks still need to be |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2332 | * accounted. |
| 2333 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2334 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2335 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2336 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2337 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2338 | } |
| 2339 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2340 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2341 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2342 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2343 | /* |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2344 | * How many index/leaf blocks need to change/allocate to add @extents extents? |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2345 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2346 | * If we add a single extent, then in the worse case, each tree level |
| 2347 | * index/leaf need to be changed in case of the tree split. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2348 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2349 | * If more extents are inserted, they could cause the whole tree split more |
| 2350 | * than once, but this is really rare. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2351 | */ |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2352 | int ext4_ext_index_trans_blocks(struct inode *inode, int extents) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2353 | { |
| 2354 | int index; |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 2355 | int depth; |
| 2356 | |
| 2357 | /* If we are converting the inline data, only one is needed here. */ |
| 2358 | if (ext4_has_inline_data(inode)) |
| 2359 | return 1; |
| 2360 | |
| 2361 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2362 | |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2363 | if (extents <= 1) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2364 | index = depth * 2; |
| 2365 | else |
| 2366 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2367 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2368 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2371 | static inline int get_default_free_blocks_flags(struct inode *inode) |
| 2372 | { |
Tahsin Erdogan | ddfa17e | 2017-06-21 21:36:51 -0400 | [diff] [blame] | 2373 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || |
| 2374 | ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE)) |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2375 | return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET; |
| 2376 | else if (ext4_should_journal_data(inode)) |
| 2377 | return EXT4_FREE_BLOCKS_FORGET; |
| 2378 | return 0; |
| 2379 | } |
| 2380 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2381 | /* |
| 2382 | * ext4_rereserve_cluster - increment the reserved cluster count when |
| 2383 | * freeing a cluster with a pending reservation |
| 2384 | * |
| 2385 | * @inode - file containing the cluster |
| 2386 | * @lblk - logical block in cluster to be reserved |
| 2387 | * |
| 2388 | * Increments the reserved cluster count and adjusts quota in a bigalloc |
| 2389 | * file system when freeing a partial cluster containing at least one |
| 2390 | * delayed and unwritten block. A partial cluster meeting that |
| 2391 | * requirement will have a pending reservation. If so, the |
| 2392 | * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to |
| 2393 | * defer reserved and allocated space accounting to a subsequent call |
| 2394 | * to this function. |
| 2395 | */ |
| 2396 | static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) |
| 2397 | { |
| 2398 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2399 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 2400 | |
| 2401 | dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); |
| 2402 | |
| 2403 | spin_lock(&ei->i_block_reservation_lock); |
| 2404 | ei->i_reserved_data_blocks++; |
| 2405 | percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); |
| 2406 | spin_unlock(&ei->i_block_reservation_lock); |
| 2407 | |
| 2408 | percpu_counter_add(&sbi->s_freeclusters_counter, 1); |
| 2409 | ext4_remove_pending(inode, lblk); |
| 2410 | } |
| 2411 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2412 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2413 | struct ext4_extent *ex, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2414 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2415 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2416 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2417 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2418 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2419 | ext4_fsblk_t last_pblk, pblk; |
| 2420 | ext4_lblk_t num; |
| 2421 | int flags; |
| 2422 | |
| 2423 | /* only extent tail removal is allowed */ |
| 2424 | if (from < le32_to_cpu(ex->ee_block) || |
| 2425 | to != le32_to_cpu(ex->ee_block) + ee_len - 1) { |
| 2426 | ext4_error(sbi->s_sb, |
| 2427 | "strange request: removal(2) %u-%u from %u:%u", |
| 2428 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | #ifdef EXTENTS_STATS |
| 2433 | spin_lock(&sbi->s_ext_stats_lock); |
| 2434 | sbi->s_ext_blocks += ee_len; |
| 2435 | sbi->s_ext_extents++; |
| 2436 | if (ee_len < sbi->s_ext_min) |
| 2437 | sbi->s_ext_min = ee_len; |
| 2438 | if (ee_len > sbi->s_ext_max) |
| 2439 | sbi->s_ext_max = ee_len; |
| 2440 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2441 | sbi->s_depth_max = ext_depth(inode); |
| 2442 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2443 | #endif |
| 2444 | |
| 2445 | trace_ext4_remove_blocks(inode, ex, from, to, partial); |
| 2446 | |
| 2447 | /* |
| 2448 | * if we have a partial cluster, and it's different from the |
| 2449 | * cluster of the last block in the extent, we free it |
| 2450 | */ |
| 2451 | last_pblk = ext4_ext_pblock(ex) + ee_len - 1; |
| 2452 | |
| 2453 | if (partial->state != initial && |
| 2454 | partial->pclu != EXT4_B2C(sbi, last_pblk)) { |
| 2455 | if (partial->state == tofree) { |
| 2456 | flags = get_default_free_blocks_flags(inode); |
| 2457 | if (ext4_is_pending(inode, partial->lblk)) |
| 2458 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2459 | ext4_free_blocks(handle, inode, NULL, |
| 2460 | EXT4_C2B(sbi, partial->pclu), |
| 2461 | sbi->s_cluster_ratio, flags); |
| 2462 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2463 | ext4_rereserve_cluster(inode, partial->lblk); |
| 2464 | } |
| 2465 | partial->state = initial; |
| 2466 | } |
| 2467 | |
| 2468 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 2469 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
| 2470 | |
| 2471 | /* |
| 2472 | * We free the partial cluster at the end of the extent (if any), |
| 2473 | * unless the cluster is used by another extent (partial_cluster |
| 2474 | * state is nofree). If a partial cluster exists here, it must be |
| 2475 | * shared with the last block in the extent. |
| 2476 | */ |
| 2477 | flags = get_default_free_blocks_flags(inode); |
| 2478 | |
| 2479 | /* partial, left end cluster aligned, right end unaligned */ |
| 2480 | if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && |
| 2481 | (EXT4_LBLK_CMASK(sbi, to) >= from) && |
| 2482 | (partial->state != nofree)) { |
| 2483 | if (ext4_is_pending(inode, to)) |
| 2484 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2485 | ext4_free_blocks(handle, inode, NULL, |
| 2486 | EXT4_PBLK_CMASK(sbi, last_pblk), |
| 2487 | sbi->s_cluster_ratio, flags); |
| 2488 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2489 | ext4_rereserve_cluster(inode, to); |
| 2490 | partial->state = initial; |
| 2491 | flags = get_default_free_blocks_flags(inode); |
| 2492 | } |
| 2493 | |
| 2494 | flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
Andrey Sidorov | 18888cf | 2012-09-19 14:14:53 -0400 | [diff] [blame] | 2495 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2496 | /* |
| 2497 | * For bigalloc file systems, we never free a partial cluster |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2498 | * at the beginning of the extent. Instead, we check to see if we |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2499 | * need to free it on a subsequent call to ext4_remove_blocks, |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2500 | * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2501 | */ |
| 2502 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2503 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2504 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2505 | /* reset the partial cluster if we've freed past it */ |
| 2506 | if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) |
| 2507 | partial->state = initial; |
| 2508 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2509 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2510 | * If we've freed the entire extent but the beginning is not left |
| 2511 | * cluster aligned and is not marked as ineligible for freeing we |
| 2512 | * record the partial cluster at the beginning of the extent. It |
| 2513 | * wasn't freed by the preceding ext4_free_blocks() call, and we |
| 2514 | * need to look farther to the left to determine if it's to be freed |
| 2515 | * (not shared with another extent). Else, reset the partial |
| 2516 | * cluster - we're either done freeing or the beginning of the |
| 2517 | * extent is left cluster aligned. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2518 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2519 | if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { |
| 2520 | if (partial->state == initial) { |
| 2521 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2522 | partial->lblk = from; |
| 2523 | partial->state = tofree; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2524 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2525 | } else { |
| 2526 | partial->state = initial; |
| 2527 | } |
| 2528 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2529 | return 0; |
| 2530 | } |
| 2531 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2532 | /* |
| 2533 | * ext4_ext_rm_leaf() Removes the extents associated with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2534 | * blocks appearing between "start" and "end". Both "start" |
| 2535 | * and "end" must appear in the same extent or EIO is returned. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2536 | * |
| 2537 | * @handle: The journal handle |
| 2538 | * @inode: The files inode |
| 2539 | * @path: The path to the leaf |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2540 | * @partial_cluster: The cluster which we'll have to free if all extents |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2541 | * has been released from it. However, if this value is |
| 2542 | * negative, it's a cluster just to the right of the |
| 2543 | * punched region and it must not be freed. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2544 | * @start: The first block to remove |
| 2545 | * @end: The last block to remove |
| 2546 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2547 | static int |
| 2548 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2549 | struct ext4_ext_path *path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2550 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2551 | ext4_lblk_t start, ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2552 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2553 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2554 | int err = 0, correct_index = 0; |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2555 | int depth = ext_depth(inode), credits, revoke_credits; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2556 | struct ext4_extent_header *eh; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2557 | ext4_lblk_t a, b; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2558 | unsigned num; |
| 2559 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2560 | unsigned short ex_ee_len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2561 | unsigned unwritten = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2562 | struct ext4_extent *ex; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2563 | ext4_fsblk_t pblk; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2564 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2565 | /* the header must be checked already in ext4_ext_remove_space() */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2566 | ext_debug(inode, "truncate since %u in leaf to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2567 | if (!path[depth].p_hdr) |
| 2568 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2569 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2570 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2571 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2572 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2573 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2574 | /* find where to start removing */ |
Ashish Sangwan | 6ae06ff | 2013-07-01 08:12:41 -0400 | [diff] [blame] | 2575 | ex = path[depth].p_ext; |
| 2576 | if (!ex) |
| 2577 | ex = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2578 | |
| 2579 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2580 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2581 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2582 | trace_ext4_ext_rm_leaf(inode, start, ex, partial); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2583 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2584 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2585 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2586 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2587 | if (ext4_ext_is_unwritten(ex)) |
| 2588 | unwritten = 1; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2589 | else |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2590 | unwritten = 0; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2591 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2592 | ext_debug(inode, "remove ext %u:[%d]%d\n", ex_ee_block, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2593 | unwritten, ex_ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2594 | path[depth].p_ext = ex; |
| 2595 | |
| 2596 | a = ex_ee_block > start ? ex_ee_block : start; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2597 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2598 | ex_ee_block+ex_ee_len - 1 : end; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2599 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2600 | ext_debug(inode, " border %u:%u\n", a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2601 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2602 | /* If this extent is beyond the end of the hole, skip it */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2603 | if (end < ex_ee_block) { |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2604 | /* |
| 2605 | * We're going to skip this extent and move to another, |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2606 | * so note that its first cluster is in use to avoid |
| 2607 | * freeing it when removing blocks. Eventually, the |
| 2608 | * right edge of the truncated/punched region will |
| 2609 | * be just to the left. |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2610 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2611 | if (sbi->s_cluster_ratio > 1) { |
| 2612 | pblk = ext4_ext_pblock(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2613 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2614 | partial->state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2615 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2616 | ex--; |
| 2617 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2618 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2619 | continue; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2620 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
Lukas Czerner | dc1841d | 2012-03-19 23:07:43 -0400 | [diff] [blame] | 2621 | EXT4_ERROR_INODE(inode, |
| 2622 | "can not handle truncate %u:%u " |
| 2623 | "on extent %u:%u", |
| 2624 | start, end, ex_ee_block, |
| 2625 | ex_ee_block + ex_ee_len - 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2626 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2627 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2628 | } else if (a != ex_ee_block) { |
| 2629 | /* remove tail of the extent */ |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2630 | num = a - ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2631 | } else { |
| 2632 | /* remove whole extent: excellent! */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2633 | num = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2634 | } |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2635 | /* |
| 2636 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2637 | * descriptor) for each block group; assume two block |
| 2638 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2639 | * the worst case |
| 2640 | */ |
| 2641 | credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2642 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2643 | correct_index = 1; |
| 2644 | credits += (ext_depth(inode)) + 1; |
| 2645 | } |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2646 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2647 | /* |
| 2648 | * We may end up freeing some index blocks and data from the |
| 2649 | * punched range. Note that partial clusters are accounted for |
| 2650 | * by ext4_free_data_revoke_credits(). |
| 2651 | */ |
| 2652 | revoke_credits = |
| 2653 | ext4_free_metadata_revoke_credits(inode->i_sb, |
| 2654 | ext_depth(inode)) + |
| 2655 | ext4_free_data_revoke_credits(inode, b - a + 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2656 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2657 | err = ext4_datasem_ensure_credits(handle, inode, credits, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2658 | credits, revoke_credits); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2659 | if (err) { |
| 2660 | if (err > 0) |
| 2661 | err = -EAGAIN; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2662 | goto out; |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2663 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2664 | |
| 2665 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2666 | if (err) |
| 2667 | goto out; |
| 2668 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2669 | err = ext4_remove_blocks(handle, inode, ex, partial, a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2670 | if (err) |
| 2671 | goto out; |
| 2672 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2673 | if (num == 0) |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2674 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2675 | ext4_ext_store_pblock(ex, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2676 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2677 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2678 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2679 | * Do not mark unwritten if all the blocks in the |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2680 | * extent have been removed. |
| 2681 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2682 | if (unwritten && num) |
| 2683 | ext4_ext_mark_unwritten(ex); |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2684 | /* |
| 2685 | * If the extent was completely released, |
| 2686 | * we need to remove it from the leaf |
| 2687 | */ |
| 2688 | if (num == 0) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2689 | if (end != EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2690 | /* |
| 2691 | * For hole punching, we need to scoot all the |
| 2692 | * extents up when an extent is removed so that |
| 2693 | * we dont have blank extents in the middle |
| 2694 | */ |
| 2695 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2696 | sizeof(struct ext4_extent)); |
| 2697 | |
| 2698 | /* Now get rid of the one at the end */ |
| 2699 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2700 | sizeof(struct ext4_extent)); |
| 2701 | } |
| 2702 | le16_add_cpu(&eh->eh_entries, -1); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2703 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2704 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2705 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2706 | if (err) |
| 2707 | goto out; |
| 2708 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2709 | ext_debug(inode, "new extent: %u:%u:%llu\n", ex_ee_block, num, |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2710 | ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2711 | ex--; |
| 2712 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2713 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | if (correct_index && eh->eh_entries) |
| 2717 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2718 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2719 | /* |
Eric Whitney | ad6599a | 2014-04-01 19:49:30 -0400 | [diff] [blame] | 2720 | * If there's a partial cluster and at least one extent remains in |
| 2721 | * the leaf, free the partial cluster if it isn't shared with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2722 | * current extent. If it is shared with the current extent |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2723 | * we reset the partial cluster because we've reached the start of the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2724 | * truncated/punched region and we're done removing blocks. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2725 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2726 | if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2727 | pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2728 | if (partial->pclu != EXT4_B2C(sbi, pblk)) { |
| 2729 | int flags = get_default_free_blocks_flags(inode); |
| 2730 | |
| 2731 | if (ext4_is_pending(inode, partial->lblk)) |
| 2732 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2733 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2734 | EXT4_C2B(sbi, partial->pclu), |
| 2735 | sbi->s_cluster_ratio, flags); |
| 2736 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2737 | ext4_rereserve_cluster(inode, partial->lblk); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2738 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2739 | partial->state = initial; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2740 | } |
| 2741 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2742 | /* if this leaf is free, then we should |
| 2743 | * remove it from index block above */ |
| 2744 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2745 | err = ext4_ext_rm_idx(handle, inode, path, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2746 | |
| 2747 | out: |
| 2748 | return err; |
| 2749 | } |
| 2750 | |
| 2751 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2752 | * ext4_ext_more_to_rm: |
| 2753 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2754 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2755 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2756 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2757 | { |
| 2758 | BUG_ON(path->p_idx == NULL); |
| 2759 | |
| 2760 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2761 | return 0; |
| 2762 | |
| 2763 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2764 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2765 | * so we have to consider current index for truncation |
| 2766 | */ |
| 2767 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2768 | return 0; |
| 2769 | return 1; |
| 2770 | } |
| 2771 | |
Theodore Ts'o | 26a4c0c | 2013-04-03 12:45:17 -0400 | [diff] [blame] | 2772 | int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
| 2773 | ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2774 | { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2775 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2776 | int depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2777 | struct ext4_ext_path *path = NULL; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2778 | struct partial_cluster partial; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2779 | handle_t *handle; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2780 | int i = 0, err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2781 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2782 | partial.pclu = 0; |
| 2783 | partial.lblk = 0; |
| 2784 | partial.state = initial; |
| 2785 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2786 | ext_debug(inode, "truncate since %u to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2787 | |
| 2788 | /* probably first extent we're gonna free will be last in block */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2789 | handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, |
| 2790 | depth + 1, |
| 2791 | ext4_free_metadata_revoke_credits(inode->i_sb, depth)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2792 | if (IS_ERR(handle)) |
| 2793 | return PTR_ERR(handle); |
| 2794 | |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2795 | again: |
Lukas Czerner | 6180132 | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2796 | trace_ext4_ext_remove_space(inode, start, end, depth); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2797 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2798 | /* |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2799 | * Check if we are removing extents inside the extent tree. If that |
| 2800 | * is the case, we are going to punch a hole inside the extent tree |
| 2801 | * so we have to check whether we need to split the extent covering |
| 2802 | * the last block to remove so we can easily remove the part of it |
| 2803 | * in ext4_ext_rm_leaf(). |
| 2804 | */ |
| 2805 | if (end < EXT_MAX_BLOCKS - 1) { |
| 2806 | struct ext4_extent *ex; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2807 | ext4_lblk_t ee_block, ex_end, lblk; |
| 2808 | ext4_fsblk_t pblk; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2809 | |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2810 | /* find extent for or closest extent to this block */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 2811 | path = ext4_find_extent(inode, end, NULL, |
| 2812 | EXT4_EX_NOCACHE | EXT4_EX_NOFAIL); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2813 | if (IS_ERR(path)) { |
| 2814 | ext4_journal_stop(handle); |
| 2815 | return PTR_ERR(path); |
| 2816 | } |
| 2817 | depth = ext_depth(inode); |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2818 | /* Leaf not may not exist only if inode has no blocks at all */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2819 | ex = path[depth].p_ext; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2820 | if (!ex) { |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2821 | if (depth) { |
| 2822 | EXT4_ERROR_INODE(inode, |
| 2823 | "path[%d].p_hdr == NULL", |
| 2824 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2825 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2826 | } |
| 2827 | goto out; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2828 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2829 | |
| 2830 | ee_block = le32_to_cpu(ex->ee_block); |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2831 | ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2832 | |
| 2833 | /* |
| 2834 | * See if the last block is inside the extent, if so split |
| 2835 | * the extent at 'end' block so we can easily remove the |
| 2836 | * tail of the first part of the split extent in |
| 2837 | * ext4_ext_rm_leaf(). |
| 2838 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2839 | if (end >= ee_block && end < ex_end) { |
| 2840 | |
| 2841 | /* |
| 2842 | * If we're going to split the extent, note that |
| 2843 | * the cluster containing the block after 'end' is |
| 2844 | * in use to avoid freeing it when removing blocks. |
| 2845 | */ |
| 2846 | if (sbi->s_cluster_ratio > 1) { |
| 2847 | pblk = ext4_ext_pblock(ex) + end - ee_block + 2; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2848 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2849 | partial.state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2850 | } |
| 2851 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2852 | /* |
| 2853 | * Split the extent in two so that 'end' is the last |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 2854 | * block in the first new extent. Also we should not |
| 2855 | * fail removing space due to ENOSPC so try to use |
| 2856 | * reserved block if that happens. |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2857 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2858 | err = ext4_force_split_extent_at(handle, inode, &path, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 2859 | end + 1, 1); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2860 | if (err < 0) |
| 2861 | goto out; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2862 | |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2863 | } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && |
| 2864 | partial.state == initial) { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2865 | /* |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2866 | * If we're punching, there's an extent to the right. |
| 2867 | * If the partial cluster hasn't been set, set it to |
| 2868 | * that extent's first cluster and its state to nofree |
| 2869 | * so it won't be freed should it contain blocks to be |
| 2870 | * removed. If it's already set (tofree/nofree), we're |
| 2871 | * retrying and keep the original partial cluster info |
| 2872 | * so a cluster marked tofree as a result of earlier |
| 2873 | * extent removal is not lost. |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2874 | */ |
| 2875 | lblk = ex_end + 1; |
| 2876 | err = ext4_ext_search_right(inode, path, &lblk, &pblk, |
| 2877 | &ex); |
| 2878 | if (err) |
| 2879 | goto out; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2880 | if (pblk) { |
| 2881 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2882 | partial.state = nofree; |
| 2883 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2884 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2885 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2886 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2887 | * We start scanning from right side, freeing all the blocks |
| 2888 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2889 | */ |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2890 | depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2891 | if (path) { |
| 2892 | int k = i = depth; |
| 2893 | while (--k > 0) |
| 2894 | path[k].p_block = |
| 2895 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
| 2896 | } else { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2897 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 2898 | GFP_NOFS | __GFP_NOFAIL); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2899 | if (path == NULL) { |
| 2900 | ext4_journal_stop(handle); |
| 2901 | return -ENOMEM; |
| 2902 | } |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 2903 | path[0].p_maxdepth = path[0].p_depth = depth; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2904 | path[0].p_hdr = ext_inode_hdr(inode); |
Theodore Ts'o | 89a4e48 | 2012-08-17 08:54:52 -0400 | [diff] [blame] | 2905 | i = 0; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2906 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 2907 | if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2908 | err = -EFSCORRUPTED; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2909 | goto out; |
| 2910 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2911 | } |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2912 | err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2913 | |
| 2914 | while (i >= 0 && err == 0) { |
| 2915 | if (i == depth) { |
| 2916 | /* this is leaf block */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2917 | err = ext4_ext_rm_leaf(handle, inode, path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2918 | &partial, start, end); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2919 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2920 | brelse(path[i].p_bh); |
| 2921 | path[i].p_bh = NULL; |
| 2922 | i--; |
| 2923 | continue; |
| 2924 | } |
| 2925 | |
| 2926 | /* this is index block */ |
| 2927 | if (!path[i].p_hdr) { |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2928 | ext_debug(inode, "initialize header\n"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2929 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2932 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2933 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2934 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2935 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2936 | ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2937 | path[i].p_hdr, |
| 2938 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2939 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2940 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2941 | path[i].p_idx--; |
| 2942 | } |
| 2943 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2944 | ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2945 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2946 | path[i].p_idx); |
| 2947 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2948 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2949 | /* go to the next level */ |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2950 | ext_debug(inode, "move to level %d (block %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2951 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2952 | memset(path + i + 1, 0, sizeof(*path)); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2953 | bh = read_extent_tree_block(inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2954 | ext4_idx_pblock(path[i].p_idx), depth - i - 1, |
| 2955 | EXT4_EX_NOCACHE); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2956 | if (IS_ERR(bh)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2957 | /* should we reset i_size? */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2958 | err = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2959 | break; |
| 2960 | } |
Theodore Ts'o | 76828c88 | 2013-07-15 12:27:47 -0400 | [diff] [blame] | 2961 | /* Yield here to deal with large extent trees. |
| 2962 | * Should be a no-op if we did IO above. */ |
| 2963 | cond_resched(); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2964 | if (WARN_ON(i + 1 > depth)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2965 | err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2966 | break; |
| 2967 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2968 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2969 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2970 | /* save actual number of indexes since this |
| 2971 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2972 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2973 | i++; |
| 2974 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2975 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2976 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2977 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2978 | * handle must be already prepared by the |
| 2979 | * truncatei_leaf() */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2980 | err = ext4_ext_rm_idx(handle, inode, path, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2981 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2982 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2983 | brelse(path[i].p_bh); |
| 2984 | path[i].p_bh = NULL; |
| 2985 | i--; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 2986 | ext_debug(inode, "return to level %d\n", i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2990 | trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, |
| 2991 | path->p_hdr->eh_entries); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2992 | |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2993 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2994 | * if there's a partial cluster and we have removed the first extent |
| 2995 | * in the file, then we also free the partial cluster, if any |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2996 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2997 | if (partial.state == tofree && err == 0) { |
| 2998 | int flags = get_default_free_blocks_flags(inode); |
| 2999 | |
| 3000 | if (ext4_is_pending(inode, partial.lblk)) |
| 3001 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3002 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3003 | EXT4_C2B(sbi, partial.pclu), |
| 3004 | sbi->s_cluster_ratio, flags); |
| 3005 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 3006 | ext4_rereserve_cluster(inode, partial.lblk); |
| 3007 | partial.state = initial; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3008 | } |
| 3009 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3010 | /* TODO: flexible tree reduction should be here */ |
| 3011 | if (path->p_hdr->eh_entries == 0) { |
| 3012 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3013 | * truncate to zero freed all the tree, |
| 3014 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3015 | */ |
| 3016 | err = ext4_ext_get_access(handle, inode, path); |
| 3017 | if (err == 0) { |
| 3018 | ext_inode_hdr(inode)->eh_depth = 0; |
| 3019 | ext_inode_hdr(inode)->eh_max = |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 3020 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3021 | err = ext4_ext_dirty(handle, inode, path); |
| 3022 | } |
| 3023 | } |
| 3024 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 3025 | ext4_ext_drop_refs(path); |
| 3026 | kfree(path); |
| 3027 | path = NULL; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3028 | if (err == -EAGAIN) |
| 3029 | goto again; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3030 | ext4_journal_stop(handle); |
| 3031 | |
| 3032 | return err; |
| 3033 | } |
| 3034 | |
| 3035 | /* |
| 3036 | * called at mount time |
| 3037 | */ |
| 3038 | void ext4_ext_init(struct super_block *sb) |
| 3039 | { |
| 3040 | /* |
| 3041 | * possible initialization would be here |
| 3042 | */ |
| 3043 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3044 | if (ext4_has_feature_extents(sb)) { |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3045 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3046 | printk(KERN_INFO "EXT4-fs: file extents enabled" |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 3047 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3048 | ", aggressive tests" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3049 | #endif |
| 3050 | #ifdef CHECK_BINSEARCH |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3051 | ", check binsearch" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3052 | #endif |
| 3053 | #ifdef EXTENTS_STATS |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3054 | ", stats" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3055 | #endif |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3056 | "\n"); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3057 | #endif |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3058 | #ifdef EXTENTS_STATS |
| 3059 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 3060 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 3061 | EXT4_SB(sb)->s_ext_max = 0; |
| 3062 | #endif |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | /* |
| 3067 | * called at umount time |
| 3068 | */ |
| 3069 | void ext4_ext_release(struct super_block *sb) |
| 3070 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3071 | if (!ext4_has_feature_extents(sb)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3072 | return; |
| 3073 | |
| 3074 | #ifdef EXTENTS_STATS |
| 3075 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 3076 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3077 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 3078 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 3079 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 3080 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 3081 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 3082 | } |
| 3083 | #endif |
| 3084 | } |
| 3085 | |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3086 | static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) |
| 3087 | { |
| 3088 | ext4_lblk_t ee_block; |
| 3089 | ext4_fsblk_t ee_pblock; |
| 3090 | unsigned int ee_len; |
| 3091 | |
| 3092 | ee_block = le32_to_cpu(ex->ee_block); |
| 3093 | ee_len = ext4_ext_get_actual_len(ex); |
| 3094 | ee_pblock = ext4_ext_pblock(ex); |
| 3095 | |
| 3096 | if (ee_len == 0) |
| 3097 | return 0; |
| 3098 | |
| 3099 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, |
| 3100 | EXTENT_STATUS_WRITTEN); |
| 3101 | } |
| 3102 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3103 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 3104 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 3105 | { |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 3106 | ext4_fsblk_t ee_pblock; |
| 3107 | unsigned int ee_len; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3108 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3109 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3110 | ee_pblock = ext4_ext_pblock(ex); |
Jan Kara | 53085fa | 2015-12-07 15:09:35 -0500 | [diff] [blame] | 3111 | return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, |
| 3112 | ee_len); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3113 | } |
| 3114 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3115 | /* |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3116 | * ext4_split_extent_at() splits an extent at given block. |
| 3117 | * |
| 3118 | * @handle: the journal handle |
| 3119 | * @inode: the file inode |
| 3120 | * @path: the path to the extent |
| 3121 | * @split: the logical block where the extent is splitted. |
| 3122 | * @split_flags: indicates if the extent could be zeroout if split fails, and |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3123 | * the states(init or unwritten) of new extents. |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3124 | * @flags: flags used to insert new extent to extent tree. |
| 3125 | * |
| 3126 | * |
| 3127 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 3128 | * of which are deterimined by split_flag. |
| 3129 | * |
| 3130 | * There are two cases: |
| 3131 | * a> the extent are splitted into two extent. |
| 3132 | * b> split is not needed, and just mark the extent. |
| 3133 | * |
| 3134 | * return 0 on success. |
| 3135 | */ |
| 3136 | static int ext4_split_extent_at(handle_t *handle, |
| 3137 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3138 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3139 | ext4_lblk_t split, |
| 3140 | int split_flag, |
| 3141 | int flags) |
| 3142 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3143 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3144 | ext4_fsblk_t newblock; |
| 3145 | ext4_lblk_t ee_block; |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3146 | struct ext4_extent *ex, newex, orig_ex, zero_ex; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3147 | struct ext4_extent *ex2 = NULL; |
| 3148 | unsigned int ee_len, depth; |
| 3149 | int err = 0; |
| 3150 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3151 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
| 3152 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
| 3153 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 3154 | ext_debug(inode, "logical block %llu\n", (unsigned long long)split); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3155 | |
| 3156 | ext4_ext_show_leaf(inode, path); |
| 3157 | |
| 3158 | depth = ext_depth(inode); |
| 3159 | ex = path[depth].p_ext; |
| 3160 | ee_block = le32_to_cpu(ex->ee_block); |
| 3161 | ee_len = ext4_ext_get_actual_len(ex); |
| 3162 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 3163 | |
| 3164 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3165 | BUG_ON(!ext4_ext_is_unwritten(ex) && |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3166 | split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3167 | EXT4_EXT_MARK_UNWRIT1 | |
| 3168 | EXT4_EXT_MARK_UNWRIT2)); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3169 | |
| 3170 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3171 | if (err) |
| 3172 | goto out; |
| 3173 | |
| 3174 | if (split == ee_block) { |
| 3175 | /* |
| 3176 | * case b: block @split is the block that the extent begins with |
| 3177 | * then we just change the state of the extent, and splitting |
| 3178 | * is not needed. |
| 3179 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3180 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3181 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3182 | else |
| 3183 | ext4_ext_mark_initialized(ex); |
| 3184 | |
| 3185 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3186 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3187 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3188 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3189 | goto out; |
| 3190 | } |
| 3191 | |
| 3192 | /* case a */ |
| 3193 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 3194 | ex->ee_len = cpu_to_le16(split - ee_block); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3195 | if (split_flag & EXT4_EXT_MARK_UNWRIT1) |
| 3196 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3197 | |
| 3198 | /* |
| 3199 | * path may lead to new leaf, not to original leaf any more |
| 3200 | * after ext4_ext_insert_extent() returns, |
| 3201 | */ |
| 3202 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3203 | if (err) |
| 3204 | goto fix_extent_len; |
| 3205 | |
| 3206 | ex2 = &newex; |
| 3207 | ex2->ee_block = cpu_to_le32(split); |
| 3208 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 3209 | ext4_ext_store_pblock(ex2, newblock); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3210 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3211 | ext4_ext_mark_unwritten(ex2); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3212 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3213 | err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3214 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3215 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3216 | if (split_flag & EXT4_EXT_DATA_VALID1) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3217 | err = ext4_ext_zeroout(inode, ex2); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3218 | zero_ex.ee_block = ex2->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3219 | zero_ex.ee_len = cpu_to_le16( |
| 3220 | ext4_ext_get_actual_len(ex2)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3221 | ext4_ext_store_pblock(&zero_ex, |
| 3222 | ext4_ext_pblock(ex2)); |
| 3223 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3224 | err = ext4_ext_zeroout(inode, ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3225 | zero_ex.ee_block = ex->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3226 | zero_ex.ee_len = cpu_to_le16( |
| 3227 | ext4_ext_get_actual_len(ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3228 | ext4_ext_store_pblock(&zero_ex, |
| 3229 | ext4_ext_pblock(ex)); |
| 3230 | } |
| 3231 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3232 | err = ext4_ext_zeroout(inode, &orig_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3233 | zero_ex.ee_block = orig_ex.ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3234 | zero_ex.ee_len = cpu_to_le16( |
| 3235 | ext4_ext_get_actual_len(&orig_ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3236 | ext4_ext_store_pblock(&zero_ex, |
| 3237 | ext4_ext_pblock(&orig_ex)); |
| 3238 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3239 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3240 | if (err) |
| 3241 | goto fix_extent_len; |
| 3242 | /* update the extent length and mark as initialized */ |
Al Viro | af1584f | 2012-04-12 20:32:25 -0400 | [diff] [blame] | 3243 | ex->ee_len = cpu_to_le16(ee_len); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3244 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3245 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3246 | if (err) |
| 3247 | goto fix_extent_len; |
| 3248 | |
| 3249 | /* update extent status tree */ |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3250 | err = ext4_zeroout_es(inode, &zero_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3251 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3252 | goto out; |
| 3253 | } else if (err) |
| 3254 | goto fix_extent_len; |
| 3255 | |
| 3256 | out: |
| 3257 | ext4_ext_show_leaf(inode, path); |
| 3258 | return err; |
| 3259 | |
| 3260 | fix_extent_len: |
| 3261 | ex->ee_len = orig_ex.ee_len; |
Harshad Shirwadkar | b60ca33 | 2020-04-26 18:34:38 -0700 | [diff] [blame] | 3262 | /* |
| 3263 | * Ignore ext4_ext_dirty return value since we are already in error path |
| 3264 | * and err is a non-zero error code. |
| 3265 | */ |
Dmitry Monakhov | 29faed1 | 2014-07-27 22:30:29 -0400 | [diff] [blame] | 3266 | ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3267 | return err; |
| 3268 | } |
| 3269 | |
| 3270 | /* |
| 3271 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 3272 | * by @map as split_flags indicates |
| 3273 | * |
Anatol Pomozov | 70261f5 | 2013-08-28 14:40:12 -0400 | [diff] [blame] | 3274 | * It may result in splitting the extent into multiple extents (up to three) |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3275 | * There are three possibilities: |
| 3276 | * a> There is no split required |
| 3277 | * b> Splits in two extents: Split is happening at either end of the extent |
| 3278 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 3279 | * |
| 3280 | */ |
| 3281 | static int ext4_split_extent(handle_t *handle, |
| 3282 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3283 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3284 | struct ext4_map_blocks *map, |
| 3285 | int split_flag, |
| 3286 | int flags) |
| 3287 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3288 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3289 | ext4_lblk_t ee_block; |
| 3290 | struct ext4_extent *ex; |
| 3291 | unsigned int ee_len, depth; |
| 3292 | int err = 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3293 | int unwritten; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3294 | int split_flag1, flags1; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3295 | int allocated = map->m_len; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3296 | |
| 3297 | depth = ext_depth(inode); |
| 3298 | ex = path[depth].p_ext; |
| 3299 | ee_block = le32_to_cpu(ex->ee_block); |
| 3300 | ee_len = ext4_ext_get_actual_len(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3301 | unwritten = ext4_ext_is_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3302 | |
| 3303 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3304 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3305 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3306 | if (unwritten) |
| 3307 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1 | |
| 3308 | EXT4_EXT_MARK_UNWRIT2; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3309 | if (split_flag & EXT4_EXT_DATA_VALID2) |
| 3310 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3311 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3312 | map->m_lblk + map->m_len, split_flag1, flags1); |
Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 3313 | if (err) |
| 3314 | goto out; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3315 | } else { |
| 3316 | allocated = ee_len - (map->m_lblk - ee_block); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3317 | } |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3318 | /* |
| 3319 | * Update path is required because previous ext4_split_extent_at() may |
| 3320 | * result in split of original leaf or extent zeroout. |
| 3321 | */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 3322 | path = ext4_find_extent(inode, map->m_lblk, ppath, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3323 | if (IS_ERR(path)) |
| 3324 | return PTR_ERR(path); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3325 | depth = ext_depth(inode); |
| 3326 | ex = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3327 | if (!ex) { |
| 3328 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3329 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3330 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3331 | } |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3332 | unwritten = ext4_ext_is_unwritten(ex); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3333 | split_flag1 = 0; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3334 | |
| 3335 | if (map->m_lblk >= ee_block) { |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3336 | split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3337 | if (unwritten) { |
| 3338 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1; |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3339 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3340 | EXT4_EXT_MARK_UNWRIT2); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3341 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3342 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3343 | map->m_lblk, split_flag1, flags); |
| 3344 | if (err) |
| 3345 | goto out; |
| 3346 | } |
| 3347 | |
| 3348 | ext4_ext_show_leaf(inode, path); |
| 3349 | out: |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3350 | return err ? err : allocated; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3351 | } |
| 3352 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3353 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3354 | * This function is called by ext4_ext_map_blocks() if someone tries to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3355 | * to an unwritten extent. It may result in splitting the unwritten |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3356 | * extent into multiple extents (up to three - one initialized and two |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3357 | * unwritten). |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3358 | * There are three possibilities: |
| 3359 | * a> There is no split required: Entire extent should be initialized |
| 3360 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3361 | * c> Splits in three extents: Somone is writing in middle of the extent |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3362 | * |
| 3363 | * Pre-conditions: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3364 | * - The extent pointed to by 'path' is unwritten. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3365 | * - The extent pointed to by 'path' contains a superset |
| 3366 | * of the logical span [map->m_lblk, map->m_lblk + map->m_len). |
| 3367 | * |
| 3368 | * Post-conditions on success: |
| 3369 | * - the returned value is the number of blocks beyond map->l_lblk |
| 3370 | * that are allocated and initialized. |
| 3371 | * It is guaranteed to be >= map->m_len. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3372 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3373 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3374 | struct inode *inode, |
| 3375 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3376 | struct ext4_ext_path **ppath, |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3377 | int flags) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3378 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3379 | struct ext4_ext_path *path = *ppath; |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3380 | struct ext4_sb_info *sbi; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3381 | struct ext4_extent_header *eh; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3382 | struct ext4_map_blocks split_map; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3383 | struct ext4_extent zero_ex1, zero_ex2; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3384 | struct ext4_extent *ex, *abut_ex; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3385 | ext4_lblk_t ee_block, eof_block; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3386 | unsigned int ee_len, depth, map_len = map->m_len; |
| 3387 | int allocated = 0, max_zeroout = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3388 | int err = 0; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3389 | int split_flag = EXT4_EXT_DATA_VALID2; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3390 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 3391 | ext_debug(inode, "logical block %llu, max_blocks %u\n", |
| 3392 | (unsigned long long)map->m_lblk, map_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3393 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3394 | sbi = EXT4_SB(inode->i_sb); |
Jan Kara | 801674f | 2020-03-31 12:50:16 +0200 | [diff] [blame] | 3395 | eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) |
| 3396 | >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3397 | if (eof_block < map->m_lblk + map_len) |
| 3398 | eof_block = map->m_lblk + map_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3399 | |
| 3400 | depth = ext_depth(inode); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3401 | eh = path[depth].p_hdr; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3402 | ex = path[depth].p_ext; |
| 3403 | ee_block = le32_to_cpu(ex->ee_block); |
| 3404 | ee_len = ext4_ext_get_actual_len(ex); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3405 | zero_ex1.ee_len = 0; |
| 3406 | zero_ex2.ee_len = 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3407 | |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3408 | trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); |
| 3409 | |
| 3410 | /* Pre-conditions */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3411 | BUG_ON(!ext4_ext_is_unwritten(ex)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3412 | BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3413 | |
| 3414 | /* |
| 3415 | * Attempt to transfer newly initialized blocks from the currently |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3416 | * unwritten extent to its neighbor. This is much cheaper |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3417 | * than an insertion followed by a merge as those involve costly |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3418 | * memmove() calls. Transferring to the left is the common case in |
| 3419 | * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) |
| 3420 | * followed by append writes. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3421 | * |
| 3422 | * Limitations of the current logic: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3423 | * - L1: we do not deal with writes covering the whole extent. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3424 | * This would require removing the extent if the transfer |
| 3425 | * is possible. |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3426 | * - L2: we only attempt to merge with an extent stored in the |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3427 | * same extent tree node. |
| 3428 | */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3429 | if ((map->m_lblk == ee_block) && |
| 3430 | /* See if we can merge left */ |
| 3431 | (map_len < ee_len) && /*L1*/ |
| 3432 | (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3433 | ext4_lblk_t prev_lblk; |
| 3434 | ext4_fsblk_t prev_pblk, ee_pblk; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3435 | unsigned int prev_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3436 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3437 | abut_ex = ex - 1; |
| 3438 | prev_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3439 | prev_len = ext4_ext_get_actual_len(abut_ex); |
| 3440 | prev_pblk = ext4_ext_pblock(abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3441 | ee_pblk = ext4_ext_pblock(ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3442 | |
| 3443 | /* |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3444 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3445 | * upon those conditions: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3446 | * - C1: abut_ex is initialized, |
| 3447 | * - C2: abut_ex is logically abutting ex, |
| 3448 | * - C3: abut_ex is physically abutting ex, |
| 3449 | * - C4: abut_ex can receive the additional blocks without |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3450 | * overflowing the (initialized) length limit. |
| 3451 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3452 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3453 | ((prev_lblk + prev_len) == ee_block) && /*C2*/ |
| 3454 | ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3455 | (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3456 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3457 | if (err) |
| 3458 | goto out; |
| 3459 | |
| 3460 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3461 | map, ex, abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3462 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3463 | /* Shift the start of ex by 'map_len' blocks */ |
| 3464 | ex->ee_block = cpu_to_le32(ee_block + map_len); |
| 3465 | ext4_ext_store_pblock(ex, ee_pblk + map_len); |
| 3466 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3467 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3468 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3469 | /* Extend abut_ex by 'map_len' blocks */ |
| 3470 | abut_ex->ee_len = cpu_to_le16(prev_len + map_len); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3471 | |
| 3472 | /* Result: number of initialized blocks past m_lblk */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3473 | allocated = map_len; |
| 3474 | } |
| 3475 | } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && |
| 3476 | (map_len < ee_len) && /*L1*/ |
| 3477 | ex < EXT_LAST_EXTENT(eh)) { /*L2*/ |
| 3478 | /* See if we can merge right */ |
| 3479 | ext4_lblk_t next_lblk; |
| 3480 | ext4_fsblk_t next_pblk, ee_pblk; |
| 3481 | unsigned int next_len; |
| 3482 | |
| 3483 | abut_ex = ex + 1; |
| 3484 | next_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3485 | next_len = ext4_ext_get_actual_len(abut_ex); |
| 3486 | next_pblk = ext4_ext_pblock(abut_ex); |
| 3487 | ee_pblk = ext4_ext_pblock(ex); |
| 3488 | |
| 3489 | /* |
| 3490 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
| 3491 | * upon those conditions: |
| 3492 | * - C1: abut_ex is initialized, |
| 3493 | * - C2: abut_ex is logically abutting ex, |
| 3494 | * - C3: abut_ex is physically abutting ex, |
| 3495 | * - C4: abut_ex can receive the additional blocks without |
| 3496 | * overflowing the (initialized) length limit. |
| 3497 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3498 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3499 | ((map->m_lblk + map_len) == next_lblk) && /*C2*/ |
| 3500 | ((ee_pblk + ee_len) == next_pblk) && /*C3*/ |
| 3501 | (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
| 3502 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3503 | if (err) |
| 3504 | goto out; |
| 3505 | |
| 3506 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
| 3507 | map, ex, abut_ex); |
| 3508 | |
| 3509 | /* Shift the start of abut_ex by 'map_len' blocks */ |
| 3510 | abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); |
| 3511 | ext4_ext_store_pblock(abut_ex, next_pblk - map_len); |
| 3512 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3513 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3514 | |
| 3515 | /* Extend abut_ex by 'map_len' blocks */ |
| 3516 | abut_ex->ee_len = cpu_to_le16(next_len + map_len); |
| 3517 | |
| 3518 | /* Result: number of initialized blocks past m_lblk */ |
| 3519 | allocated = map_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3520 | } |
| 3521 | } |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3522 | if (allocated) { |
| 3523 | /* Mark the block containing both extents as dirty */ |
Harshad Shirwadkar | b60ca33 | 2020-04-26 18:34:38 -0700 | [diff] [blame] | 3524 | err = ext4_ext_dirty(handle, inode, path + depth); |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3525 | |
| 3526 | /* Update path to point to the right extent */ |
| 3527 | path[depth].p_ext = abut_ex; |
| 3528 | goto out; |
| 3529 | } else |
| 3530 | allocated = ee_len - (map->m_lblk - ee_block); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3531 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3532 | WARN_ON(map->m_lblk < ee_block); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3533 | /* |
| 3534 | * It is safe to convert extent to initialized via explicit |
Yongqiang Yang | 9e74056 | 2014-01-06 14:05:23 -0500 | [diff] [blame] | 3535 | * zeroout only if extent is fully inside i_size or new_size. |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3536 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3537 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3538 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3539 | if (EXT4_EXT_MAY_ZEROOUT & split_flag) |
| 3540 | max_zeroout = sbi->s_extent_max_zeroout_kb >> |
Lukas Czerner | 4f42f80 | 2013-03-12 12:40:04 -0400 | [diff] [blame] | 3541 | (inode->i_sb->s_blocksize_bits - 10); |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3542 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3543 | /* |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3544 | * five cases: |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3545 | * 1. split the extent into three extents. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3546 | * 2. split the extent into two extents, zeroout the head of the first |
| 3547 | * extent. |
| 3548 | * 3. split the extent into two extents, zeroout the tail of the second |
| 3549 | * extent. |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3550 | * 4. split the extent into two extents with out zeroout. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3551 | * 5. no splitting needed, just possibly zeroout the head and / or the |
| 3552 | * tail of the extent. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3553 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3554 | split_map.m_lblk = map->m_lblk; |
| 3555 | split_map.m_len = map->m_len; |
| 3556 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3557 | if (max_zeroout && (allocated > split_map.m_len)) { |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3558 | if (allocated <= max_zeroout) { |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3559 | /* case 3 or 5 */ |
| 3560 | zero_ex1.ee_block = |
| 3561 | cpu_to_le32(split_map.m_lblk + |
| 3562 | split_map.m_len); |
| 3563 | zero_ex1.ee_len = |
| 3564 | cpu_to_le16(allocated - split_map.m_len); |
| 3565 | ext4_ext_store_pblock(&zero_ex1, |
| 3566 | ext4_ext_pblock(ex) + split_map.m_lblk + |
| 3567 | split_map.m_len - ee_block); |
| 3568 | err = ext4_ext_zeroout(inode, &zero_ex1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3569 | if (err) |
| 3570 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3571 | split_map.m_len = allocated; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3572 | } |
| 3573 | if (split_map.m_lblk - ee_block + split_map.m_len < |
| 3574 | max_zeroout) { |
| 3575 | /* case 2 or 5 */ |
| 3576 | if (split_map.m_lblk != ee_block) { |
| 3577 | zero_ex2.ee_block = ex->ee_block; |
| 3578 | zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3579 | ee_block); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3580 | ext4_ext_store_pblock(&zero_ex2, |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3581 | ext4_ext_pblock(ex)); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3582 | err = ext4_ext_zeroout(inode, &zero_ex2); |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3583 | if (err) |
| 3584 | goto out; |
| 3585 | } |
| 3586 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3587 | split_map.m_len += split_map.m_lblk - ee_block; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3588 | split_map.m_lblk = ee_block; |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3589 | allocated = map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3590 | } |
| 3591 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3592 | |
Jan Kara | ae9e9c6a | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 3593 | err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, |
| 3594 | flags); |
| 3595 | if (err > 0) |
| 3596 | err = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3597 | out: |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3598 | /* If we have gotten a failure, don't zero out status tree */ |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3599 | if (!err) { |
| 3600 | err = ext4_zeroout_es(inode, &zero_ex1); |
| 3601 | if (!err) |
| 3602 | err = ext4_zeroout_es(inode, &zero_ex2); |
| 3603 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3604 | return err ? err : allocated; |
| 3605 | } |
| 3606 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3607 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3608 | * This function is called by ext4_ext_map_blocks() from |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3609 | * ext4_get_blocks_dio_write() when DIO to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3610 | * to an unwritten extent. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3611 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3612 | * Writing to an unwritten extent may result in splitting the unwritten |
| 3613 | * extent into multiple initialized/unwritten extents (up to three) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3614 | * There are three possibilities: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3615 | * a> There is no split required: Entire extent should be unwritten |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3616 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3617 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3618 | * |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3619 | * This works the same way in the case of initialized -> unwritten conversion. |
| 3620 | * |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3621 | * One of more index blocks maybe needed if the extent tree grow after |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3622 | * the unwritten extent split. To prevent ENOSPC occur at the IO |
| 3623 | * complete, we need to split the unwritten extent before DIO submit |
| 3624 | * the IO. The unwritten extent called at this time will be split |
| 3625 | * into three unwritten extent(at most). After IO complete, the part |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3626 | * being filled will be convert to initialized by the end_io callback function |
| 3627 | * via ext4_convert_unwritten_extents(). |
Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3628 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3629 | * Returns the size of unwritten extent to be written on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3630 | */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3631 | static int ext4_split_convert_extents(handle_t *handle, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3632 | struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3633 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3634 | struct ext4_ext_path **ppath, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3635 | int flags) |
| 3636 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3637 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3638 | ext4_lblk_t eof_block; |
| 3639 | ext4_lblk_t ee_block; |
| 3640 | struct ext4_extent *ex; |
| 3641 | unsigned int ee_len; |
| 3642 | int split_flag = 0, depth; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3643 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 3644 | ext_debug(inode, "logical block %llu, max_blocks %u\n", |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3645 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3646 | |
Jan Kara | 801674f | 2020-03-31 12:50:16 +0200 | [diff] [blame] | 3647 | eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) |
| 3648 | >> inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3649 | if (eof_block < map->m_lblk + map->m_len) |
| 3650 | eof_block = map->m_lblk + map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3651 | /* |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3652 | * It is safe to convert extent to initialized via explicit |
| 3653 | * zeroout only if extent is fully insde i_size or new_size. |
| 3654 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3655 | depth = ext_depth(inode); |
| 3656 | ex = path[depth].p_ext; |
| 3657 | ee_block = le32_to_cpu(ex->ee_block); |
| 3658 | ee_len = ext4_ext_get_actual_len(ex); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3659 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3660 | /* Convert to unwritten */ |
| 3661 | if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { |
| 3662 | split_flag |= EXT4_EXT_DATA_VALID1; |
| 3663 | /* Convert to initialized */ |
| 3664 | } else if (flags & EXT4_GET_BLOCKS_CONVERT) { |
| 3665 | split_flag |= ee_block + ee_len <= eof_block ? |
| 3666 | EXT4_EXT_MAY_ZEROOUT : 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3667 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3668 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3669 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3670 | return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3671 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3672 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3673 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3674 | struct inode *inode, |
| 3675 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3676 | struct ext4_ext_path **ppath) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3677 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3678 | struct ext4_ext_path *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3679 | struct ext4_extent *ex; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3680 | ext4_lblk_t ee_block; |
| 3681 | unsigned int ee_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3682 | int depth; |
| 3683 | int err = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3684 | |
| 3685 | depth = ext_depth(inode); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3686 | ex = path[depth].p_ext; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3687 | ee_block = le32_to_cpu(ex->ee_block); |
| 3688 | ee_len = ext4_ext_get_actual_len(ex); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3689 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 3690 | ext_debug(inode, "logical block %llu, max_blocks %u\n", |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3691 | (unsigned long long)ee_block, ee_len); |
| 3692 | |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3693 | /* If extent is larger than requested it is a clear sign that we still |
| 3694 | * have some extent state machine issues left. So extent_split is still |
| 3695 | * required. |
| 3696 | * TODO: Once all related issues will be fixed this situation should be |
| 3697 | * illegal. |
| 3698 | */ |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3699 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Rakesh Pandit | e3d550c | 2019-08-22 22:53:46 -0400 | [diff] [blame] | 3700 | #ifdef CONFIG_EXT4_DEBUG |
| 3701 | ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu," |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 3702 | " len %u; IO logical block %llu, len %u", |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3703 | inode->i_ino, (unsigned long long)ee_block, ee_len, |
| 3704 | (unsigned long long)map->m_lblk, map->m_len); |
| 3705 | #endif |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3706 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3707 | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3708 | if (err < 0) |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3709 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3710 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3711 | if (IS_ERR(path)) |
| 3712 | return PTR_ERR(path); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3713 | depth = ext_depth(inode); |
| 3714 | ex = path[depth].p_ext; |
| 3715 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3716 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3717 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3718 | if (err) |
| 3719 | goto out; |
| 3720 | /* first mark the extent as initialized */ |
| 3721 | ext4_ext_mark_initialized(ex); |
| 3722 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3723 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3724 | * borders are not changed |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3725 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3726 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3727 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3728 | /* Mark modified extent as dirty */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3729 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3730 | out: |
| 3731 | ext4_ext_show_leaf(inode, path); |
| 3732 | return err; |
| 3733 | } |
| 3734 | |
| 3735 | static int |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3736 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
| 3737 | struct ext4_map_blocks *map, |
Eric Whitney | 29c6eaf | 2016-02-22 22:58:55 -0500 | [diff] [blame] | 3738 | struct ext4_ext_path **ppath, |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 3739 | unsigned int *allocated) |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3740 | { |
Theodore Ts'o | 4f224b8 | 2014-09-01 14:36:09 -0400 | [diff] [blame] | 3741 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3742 | struct ext4_extent *ex; |
| 3743 | ext4_lblk_t ee_block; |
| 3744 | unsigned int ee_len; |
| 3745 | int depth; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3746 | int err = 0; |
| 3747 | |
| 3748 | /* |
| 3749 | * Make sure that the extent is no bigger than we support with |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3750 | * unwritten extent |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3751 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3752 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) |
| 3753 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3754 | |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3755 | depth = ext_depth(inode); |
| 3756 | ex = path[depth].p_ext; |
| 3757 | ee_block = le32_to_cpu(ex->ee_block); |
| 3758 | ee_len = ext4_ext_get_actual_len(ex); |
| 3759 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 3760 | ext_debug(inode, "logical block %llu, max_blocks %u\n", |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3761 | (unsigned long long)ee_block, ee_len); |
| 3762 | |
| 3763 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3764 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3765 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); |
| 3766 | if (err < 0) |
| 3767 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3768 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3769 | if (IS_ERR(path)) |
| 3770 | return PTR_ERR(path); |
| 3771 | depth = ext_depth(inode); |
| 3772 | ex = path[depth].p_ext; |
| 3773 | if (!ex) { |
| 3774 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3775 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3776 | return -EFSCORRUPTED; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3781 | if (err) |
| 3782 | return err; |
| 3783 | /* first mark the extent as unwritten */ |
| 3784 | ext4_ext_mark_unwritten(ex); |
| 3785 | |
| 3786 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3787 | * borders are not changed |
| 3788 | */ |
| 3789 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3790 | |
| 3791 | /* Mark modified extent as dirty */ |
| 3792 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
| 3793 | if (err) |
| 3794 | return err; |
| 3795 | ext4_ext_show_leaf(inode, path); |
| 3796 | |
| 3797 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3798 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3799 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 3800 | if (*allocated > map->m_len) |
| 3801 | *allocated = map->m_len; |
| 3802 | map->m_len = *allocated; |
| 3803 | return 0; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | static int |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3807 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3808 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3809 | struct ext4_ext_path **ppath, int flags, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3810 | unsigned int allocated, ext4_fsblk_t newblock) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3811 | { |
Ritesh Harjani | 8ec2d31 | 2020-05-10 11:54:53 +0530 | [diff] [blame] | 3812 | struct ext4_ext_path __maybe_unused *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3813 | int ret = 0; |
| 3814 | int err = 0; |
| 3815 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 3816 | ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n", |
| 3817 | (unsigned long long)map->m_lblk, map->m_len, flags, |
| 3818 | allocated); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3819 | ext4_ext_show_leaf(inode, path); |
| 3820 | |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3821 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3822 | * When writing into unwritten space, we should not fail to |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3823 | * allocate metadata blocks for the new extent block if needed. |
| 3824 | */ |
| 3825 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; |
| 3826 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3827 | trace_ext4_ext_handle_unwritten_extents(inode, map, flags, |
Zheng Liu | b564553 | 2012-11-08 14:33:43 -0500 | [diff] [blame] | 3828 | allocated, newblock); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3829 | |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3830 | /* get_block() before submitting IO, split the extent */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3831 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3832 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
| 3833 | flags | EXT4_GET_BLOCKS_CONVERT); |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3834 | if (ret < 0) { |
| 3835 | err = ret; |
| 3836 | goto out2; |
| 3837 | } |
| 3838 | /* |
| 3839 | * shouldn't get a 0 return when splitting an extent unless |
| 3840 | * m_len is 0 (bug) or extent has been corrupted |
| 3841 | */ |
| 3842 | if (unlikely(ret == 0)) { |
| 3843 | EXT4_ERROR_INODE(inode, |
| 3844 | "unexpected ret == 0, m_len = %u", |
| 3845 | map->m_len); |
| 3846 | err = -EFSCORRUPTED; |
| 3847 | goto out2; |
| 3848 | } |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3849 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3850 | goto out; |
| 3851 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3852 | /* IO end_io complete, convert the filled extent to written */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3853 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3854 | err = ext4_convert_unwritten_extents_endio(handle, inode, map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3855 | ppath); |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3856 | if (err < 0) |
| 3857 | goto out2; |
| 3858 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3859 | goto map_out; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3860 | } |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3861 | /* buffered IO cases */ |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3862 | /* |
| 3863 | * repeat fallocate creation request |
| 3864 | * we already have an unwritten extent |
| 3865 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3866 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3867 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3868 | goto map_out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3869 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3870 | |
| 3871 | /* buffered READ or buffered write_begin() lookup */ |
| 3872 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3873 | /* |
| 3874 | * We have blocks reserved already. We |
| 3875 | * return allocated blocks so that delalloc |
| 3876 | * won't do block reservation for us. But |
| 3877 | * the buffer head will be unmapped so that |
| 3878 | * a read from the block returns 0s. |
| 3879 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3880 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3881 | goto out1; |
| 3882 | } |
| 3883 | |
Eric Whitney | be809e1 | 2020-04-30 14:53:20 -0400 | [diff] [blame] | 3884 | /* |
| 3885 | * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1. |
| 3886 | * For buffered writes, at writepage time, etc. Convert a |
| 3887 | * discovered unwritten extent to written. |
| 3888 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3889 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
Eric Whitney | be809e1 | 2020-04-30 14:53:20 -0400 | [diff] [blame] | 3890 | if (ret < 0) { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3891 | err = ret; |
| 3892 | goto out2; |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3893 | } |
Eric Whitney | be809e1 | 2020-04-30 14:53:20 -0400 | [diff] [blame] | 3894 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3895 | /* |
| 3896 | * shouldn't get a 0 return when converting an unwritten extent |
| 3897 | * unless m_len is 0 (bug) or extent has been corrupted |
| 3898 | */ |
| 3899 | if (unlikely(ret == 0)) { |
| 3900 | EXT4_ERROR_INODE(inode, "unexpected ret == 0, m_len = %u", |
| 3901 | map->m_len); |
| 3902 | err = -EFSCORRUPTED; |
| 3903 | goto out2; |
| 3904 | } |
| 3905 | |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3906 | out: |
| 3907 | allocated = ret; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3908 | map->m_flags |= EXT4_MAP_NEW; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3909 | map_out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3910 | map->m_flags |= EXT4_MAP_MAPPED; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3911 | out1: |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3912 | map->m_pblk = newblock; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3913 | if (allocated > map->m_len) |
| 3914 | allocated = map->m_len; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3915 | map->m_len = allocated; |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3916 | ext4_ext_show_leaf(inode, path); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3917 | out2: |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3918 | return err ? err : allocated; |
| 3919 | } |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3920 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3921 | /* |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3922 | * get_implied_cluster_alloc - check to see if the requested |
| 3923 | * allocation (in the map structure) overlaps with a cluster already |
| 3924 | * allocated in an extent. |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3925 | * @sb The filesystem superblock structure |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3926 | * @map The requested lblk->pblk mapping |
| 3927 | * @ex The extent structure which might contain an implied |
| 3928 | * cluster allocation |
| 3929 | * |
| 3930 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 3931 | * find blocks that were already in the inode's extent tree. Hence, |
| 3932 | * we know that the beginning of the requested region cannot overlap |
| 3933 | * the extent from the inode's extent tree. There are three cases we |
| 3934 | * want to catch. The first is this case: |
| 3935 | * |
| 3936 | * |--- cluster # N--| |
| 3937 | * |--- extent ---| |---- requested region ---| |
| 3938 | * |==========| |
| 3939 | * |
| 3940 | * The second case that we need to test for is this one: |
| 3941 | * |
| 3942 | * |--------- cluster # N ----------------| |
| 3943 | * |--- requested region --| |------- extent ----| |
| 3944 | * |=======================| |
| 3945 | * |
| 3946 | * The third case is when the requested region lies between two extents |
| 3947 | * within the same cluster: |
| 3948 | * |------------- cluster # N-------------| |
| 3949 | * |----- ex -----| |---- ex_right ----| |
| 3950 | * |------ requested region ------| |
| 3951 | * |================| |
| 3952 | * |
| 3953 | * In each of the above cases, we need to set the map->m_pblk and |
| 3954 | * map->m_len so it corresponds to the return the extent labelled as |
| 3955 | * "|====|" from cluster #N, since it is already in use for data in |
| 3956 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 3957 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 3958 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 3959 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 3960 | * by calling ext4_mb_new_blocks(). |
| 3961 | */ |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3962 | static int get_implied_cluster_alloc(struct super_block *sb, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3963 | struct ext4_map_blocks *map, |
| 3964 | struct ext4_extent *ex, |
| 3965 | struct ext4_ext_path *path) |
| 3966 | { |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3967 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3968 | ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3969 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
Curt Wohlgemuth | 14d7f3e | 2011-12-18 17:39:02 -0500 | [diff] [blame] | 3970 | ext4_lblk_t rr_cluster_start; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3971 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 3972 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 3973 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 3974 | |
| 3975 | /* The extent passed in that we are trying to match */ |
| 3976 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 3977 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 3978 | |
| 3979 | /* The requested region passed into ext4_map_blocks() */ |
| 3980 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3981 | |
| 3982 | if ((rr_cluster_start == ex_cluster_end) || |
| 3983 | (rr_cluster_start == ex_cluster_start)) { |
| 3984 | if (rr_cluster_start == ex_cluster_end) |
| 3985 | ee_start += ee_len - 1; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3986 | map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3987 | map->m_len = min(map->m_len, |
| 3988 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 3989 | /* |
| 3990 | * Check for and handle this case: |
| 3991 | * |
| 3992 | * |--------- cluster # N-------------| |
| 3993 | * |------- extent ----| |
| 3994 | * |--- requested region ---| |
| 3995 | * |===========| |
| 3996 | */ |
| 3997 | |
| 3998 | if (map->m_lblk < ee_block) |
| 3999 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 4000 | |
| 4001 | /* |
| 4002 | * Check for the case where there is already another allocated |
| 4003 | * block to the right of 'ex' but before the end of the cluster. |
| 4004 | * |
| 4005 | * |------------- cluster # N-------------| |
| 4006 | * |----- ex -----| |---- ex_right ----| |
| 4007 | * |------ requested region ------| |
| 4008 | * |================| |
| 4009 | */ |
| 4010 | if (map->m_lblk > ee_block) { |
| 4011 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 4012 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 4013 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4014 | |
| 4015 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4016 | return 1; |
| 4017 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4018 | |
| 4019 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4020 | return 0; |
| 4021 | } |
| 4022 | |
| 4023 | |
| 4024 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 4025 | * Block allocation/map/preallocation routine for extents based files |
| 4026 | * |
| 4027 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4028 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4029 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 4030 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 4031 | * |
| 4032 | * return > 0, number of of blocks already mapped/allocated |
| 4033 | * if create == 0 and these are pre-allocated blocks |
| 4034 | * buffer head is unmapped |
| 4035 | * otherwise blocks are mapped |
| 4036 | * |
| 4037 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 4038 | * buffer head is unmapped |
| 4039 | * |
| 4040 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4041 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4042 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 4043 | struct ext4_map_blocks *map, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4044 | { |
| 4045 | struct ext4_ext_path *path = NULL; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4046 | struct ext4_extent newex, *ex, *ex2; |
| 4047 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4048 | ext4_fsblk_t newblock = 0; |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4049 | int err = 0, depth, ret; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4050 | unsigned int allocated = 0, offset = 0; |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4051 | unsigned int allocated_clusters = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4052 | struct ext4_allocation_request ar; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4053 | ext4_lblk_t cluster_offset; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4054 | |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 4055 | ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4056 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4057 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4058 | /* find extent for this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4059 | path = ext4_find_extent(inode, map->m_lblk, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4060 | if (IS_ERR(path)) { |
| 4061 | err = PTR_ERR(path); |
| 4062 | path = NULL; |
| 4063 | goto out2; |
| 4064 | } |
| 4065 | |
| 4066 | depth = ext_depth(inode); |
| 4067 | |
| 4068 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4069 | * consistent leaf must not be empty; |
| 4070 | * this situation is possible, though, _during_ tree modification; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4071 | * this is why assert can't be put in ext4_find_extent() |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4072 | */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 4073 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 4074 | EXT4_ERROR_INODE(inode, "bad extent address " |
Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 4075 | "lblock: %lu, depth: %d pblock %lld", |
| 4076 | (unsigned long) map->m_lblk, depth, |
| 4077 | path[depth].p_block); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4078 | err = -EFSCORRUPTED; |
Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 4079 | goto out2; |
| 4080 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4081 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 4082 | ex = path[depth].p_ext; |
| 4083 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4084 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4085 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4086 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4087 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4088 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4089 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4090 | * unwritten extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4091 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4092 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4093 | ee_len = ext4_ext_get_actual_len(ex); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4094 | |
| 4095 | trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); |
| 4096 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4097 | /* if found extent covers block, simply return it */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4098 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
| 4099 | newblock = map->m_lblk - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4100 | /* number of remaining blocks in the extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4101 | allocated = ee_len - (map->m_lblk - ee_block); |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 4102 | ext_debug(inode, "%u fit into %u:%d -> %llu\n", |
| 4103 | map->m_lblk, ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4104 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4105 | /* |
| 4106 | * If the extent is initialized check whether the |
| 4107 | * caller wants to convert it to unwritten. |
| 4108 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4109 | if ((!ext4_ext_is_unwritten(ex)) && |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4110 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4111 | err = convert_initialized_extent(handle, |
| 4112 | inode, map, &path, &allocated); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4113 | goto out2; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4114 | } else if (!ext4_ext_is_unwritten(ex)) { |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4115 | goto out; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4116 | } |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4117 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4118 | ret = ext4_ext_handle_unwritten_extents( |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4119 | handle, inode, map, &path, flags, |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4120 | allocated, newblock); |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4121 | if (ret < 0) |
| 4122 | err = ret; |
| 4123 | else |
| 4124 | allocated = ret; |
Eric Whitney | 31cf0f2 | 2014-03-13 23:14:46 -0400 | [diff] [blame] | 4125 | goto out2; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4130 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4131 | * we couldn't try to create block if create flag is zero |
| 4132 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 4133 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4134 | ext4_lblk_t hole_start, hole_len; |
| 4135 | |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4136 | hole_start = map->m_lblk; |
| 4137 | hole_len = ext4_ext_determine_hole(inode, path, &hole_start); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4138 | /* |
| 4139 | * put just found gap into cache to speed up |
| 4140 | * subsequent requests |
| 4141 | */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4142 | ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4143 | |
| 4144 | /* Update hole_len to reflect hole size after map->m_lblk */ |
| 4145 | if (hole_start != map->m_lblk) |
| 4146 | hole_len -= map->m_lblk - hole_start; |
| 4147 | map->m_pblk = 0; |
| 4148 | map->m_len = min_t(unsigned int, map->m_len, hole_len); |
| 4149 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4150 | goto out2; |
| 4151 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4152 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4153 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4154 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 4155 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4156 | newex.ee_block = cpu_to_le32(map->m_lblk); |
Eric Whitney | d0abafa | 2014-01-06 14:00:23 -0500 | [diff] [blame] | 4157 | cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4158 | |
| 4159 | /* |
| 4160 | * If we are doing bigalloc, check to see if the extent returned |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4161 | * by ext4_find_extent() implies a cluster we can use. |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4162 | */ |
| 4163 | if (cluster_offset && ex && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4164 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4165 | ar.len = allocated = map->m_len; |
| 4166 | newblock = map->m_pblk; |
| 4167 | goto got_allocated_blocks; |
| 4168 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4169 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4170 | /* find neighbour allocated blocks */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4171 | ar.lleft = map->m_lblk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4172 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 4173 | if (err) |
| 4174 | goto out2; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4175 | ar.lright = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4176 | ex2 = NULL; |
| 4177 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4178 | if (err) |
| 4179 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4180 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4181 | /* Check if the extent after searching to the right implies a |
| 4182 | * cluster we can use. */ |
| 4183 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4184 | get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4185 | ar.len = allocated = map->m_len; |
| 4186 | newblock = map->m_pblk; |
| 4187 | goto got_allocated_blocks; |
| 4188 | } |
| 4189 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4190 | /* |
| 4191 | * See if request is beyond maximum number of blocks we can have in |
| 4192 | * a single extent. For an initialized extent this limit is |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4193 | * EXT_INIT_MAX_LEN and for an unwritten extent this limit is |
| 4194 | * EXT_UNWRITTEN_MAX_LEN. |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4195 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4196 | if (map->m_len > EXT_INIT_MAX_LEN && |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4197 | !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4198 | map->m_len = EXT_INIT_MAX_LEN; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4199 | else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && |
| 4200 | (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
| 4201 | map->m_len = EXT_UNWRITTEN_MAX_LEN; |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4202 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4203 | /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4204 | newex.ee_len = cpu_to_le16(map->m_len); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4205 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4206 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4207 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4208 | else |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4209 | allocated = map->m_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4210 | |
| 4211 | /* allocate new block */ |
| 4212 | ar.inode = inode; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4213 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 4214 | ar.logical = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4215 | /* |
| 4216 | * We calculate the offset from the beginning of the cluster |
| 4217 | * for the logical block number, since when we allocate a |
| 4218 | * physical cluster, the physical block should start at the |
| 4219 | * same offset from the beginning of the cluster. This is |
| 4220 | * needed so that future calls to get_implied_cluster_alloc() |
| 4221 | * work correctly. |
| 4222 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4223 | offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4224 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 4225 | ar.goal -= offset; |
| 4226 | ar.logical -= offset; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4227 | if (S_ISREG(inode->i_mode)) |
| 4228 | ar.flags = EXT4_MB_HINT_DATA; |
| 4229 | else |
| 4230 | /* disable in-core preallocation for non-regular files */ |
| 4231 | ar.flags = 0; |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4232 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 4233 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4234 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4235 | ar.flags |= EXT4_MB_DELALLOC_RESERVED; |
Theodore Ts'o | c5e298a | 2015-06-21 01:25:29 -0400 | [diff] [blame] | 4236 | if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
| 4237 | ar.flags |= EXT4_MB_USE_RESERVED; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4238 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4239 | if (!newblock) |
| 4240 | goto out2; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4241 | allocated_clusters = ar.len; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4242 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
Ritesh Harjani | 70aa155 | 2020-05-10 11:54:55 +0530 | [diff] [blame^] | 4243 | ext_debug(inode, "allocate new block: goal %llu, found %llu/%u, requested %u\n", |
Ritesh Harjani | ec8c60b | 2020-05-10 11:54:52 +0530 | [diff] [blame] | 4244 | ar.goal, newblock, ar.len, allocated); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4245 | if (ar.len > allocated) |
| 4246 | ar.len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4247 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4248 | got_allocated_blocks: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4249 | /* try to insert new extent into found leaf and return */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4250 | ext4_ext_store_pblock(&newex, newblock + offset); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4251 | newex.ee_len = cpu_to_le16(ar.len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4252 | /* Mark unwritten */ |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4253 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4254 | ext4_ext_mark_unwritten(&newex); |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4255 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4256 | } |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4257 | |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4258 | err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags); |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4259 | if (err) { |
| 4260 | if (allocated_clusters) { |
| 4261 | int fb_flags = 0; |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 4262 | |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4263 | /* |
| 4264 | * free data blocks we just allocated. |
| 4265 | * not a good idea to call discard here directly, |
| 4266 | * but otherwise we'd need to call it every free(). |
| 4267 | */ |
| 4268 | ext4_discard_preallocations(inode); |
| 4269 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4270 | fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; |
| 4271 | ext4_free_blocks(handle, inode, NULL, newblock, |
| 4272 | EXT4_C2B(sbi, allocated_clusters), |
| 4273 | fb_flags); |
| 4274 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4275 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4276 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4277 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4278 | /* previous routine could use block we allocated */ |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4279 | newblock = ext4_ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4280 | allocated = ext4_ext_get_actual_len(&newex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4281 | if (allocated > map->m_len) |
| 4282 | allocated = map->m_len; |
| 4283 | map->m_flags |= EXT4_MAP_NEW; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4284 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4285 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4286 | * Reduce the reserved cluster count to reflect successful deferred |
| 4287 | * allocation of delayed allocated clusters or direct allocation of |
| 4288 | * clusters discovered to be delayed allocated. Once allocated, a |
| 4289 | * cluster is not included in the reserved count. |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4290 | */ |
Eric Whitney | 2971148 | 2020-03-11 16:51:25 -0400 | [diff] [blame] | 4291 | if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) { |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4292 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4293 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4294 | * When allocating delayed allocated clusters, simply |
| 4295 | * reduce the reserved cluster count and claim quota |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4296 | */ |
| 4297 | ext4_da_update_reserve_space(inode, allocated_clusters, |
| 4298 | 1); |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4299 | } else { |
| 4300 | ext4_lblk_t lblk, len; |
| 4301 | unsigned int n; |
| 4302 | |
| 4303 | /* |
| 4304 | * When allocating non-delayed allocated clusters |
| 4305 | * (from fallocate, filemap, DIO, or clusters |
| 4306 | * allocated when delalloc has been disabled by |
| 4307 | * ext4_nonda_switch), reduce the reserved cluster |
| 4308 | * count by the number of allocated clusters that |
| 4309 | * have previously been delayed allocated. Quota |
| 4310 | * has been claimed by ext4_mb_new_blocks() above, |
| 4311 | * so release the quota reservations made for any |
| 4312 | * previously delayed allocated clusters. |
| 4313 | */ |
| 4314 | lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk); |
| 4315 | len = allocated_clusters << sbi->s_cluster_bits; |
| 4316 | n = ext4_es_delayed_clu(inode, lblk, len); |
| 4317 | if (n > 0) |
| 4318 | ext4_da_update_reserve_space(inode, (int) n, 0); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4319 | } |
| 4320 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4321 | |
| 4322 | /* |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4323 | * Cache the extent and update transaction to commit on fdatasync only |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4324 | * when it is _not_ an unwritten extent. |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4325 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4326 | if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4327 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4328 | else |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4329 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4330 | out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4331 | if (allocated > map->m_len) |
| 4332 | allocated = map->m_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4333 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4334 | map->m_flags |= EXT4_MAP_MAPPED; |
| 4335 | map->m_pblk = newblock; |
| 4336 | map->m_len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4337 | out2: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 4338 | ext4_ext_drop_refs(path); |
| 4339 | kfree(path); |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4340 | |
Theodore Ts'o | 63b9996 | 2013-07-16 10:28:47 -0400 | [diff] [blame] | 4341 | trace_ext4_ext_map_blocks_exit(inode, flags, map, |
| 4342 | err ? err : allocated); |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4343 | return err ? err : allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4344 | } |
| 4345 | |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4346 | int ext4_ext_truncate(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4347 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4348 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4349 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4350 | int err = 0; |
| 4351 | |
| 4352 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4353 | * TODO: optimization is possible here. |
| 4354 | * Probably we need not scan at all, |
| 4355 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4356 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4357 | |
| 4358 | /* we have to know where to truncate from in crash case */ |
| 4359 | EXT4_I(inode)->i_disksize = inode->i_size; |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4360 | err = ext4_mark_inode_dirty(handle, inode); |
| 4361 | if (err) |
| 4362 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4363 | |
| 4364 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 4365 | >> EXT4_BLOCK_SIZE_BITS(sb); |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4366 | retry: |
Zheng Liu | 51865fd | 2012-11-08 21:57:32 -0500 | [diff] [blame] | 4367 | err = ext4_es_remove_extent(inode, last_block, |
| 4368 | EXT_MAX_BLOCKS - last_block); |
Theodore Ts'o | 94eec0f | 2013-07-29 12:12:56 -0400 | [diff] [blame] | 4369 | if (err == -ENOMEM) { |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4370 | cond_resched(); |
| 4371 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4372 | goto retry; |
| 4373 | } |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4374 | if (err) |
| 4375 | return err; |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 4376 | retry_remove_space: |
| 4377 | err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
| 4378 | if (err == -ENOMEM) { |
| 4379 | cond_resched(); |
| 4380 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4381 | goto retry_remove_space; |
| 4382 | } |
| 4383 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4386 | static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset, |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4387 | ext4_lblk_t len, loff_t new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4388 | int flags) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4389 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4390 | struct inode *inode = file_inode(file); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4391 | handle_t *handle; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4392 | int ret = 0; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4393 | int ret2 = 0, ret3 = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4394 | int retries = 0; |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4395 | int depth = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4396 | struct ext4_map_blocks map; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4397 | unsigned int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4398 | loff_t epos; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4399 | |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4400 | BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4401 | map.m_lblk = offset; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4402 | map.m_len = len; |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4403 | /* |
| 4404 | * Don't normalize the request if it can fit in one extent so |
| 4405 | * that it doesn't get unnecessarily split into multiple |
| 4406 | * extents. |
| 4407 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4408 | if (len <= EXT_UNWRITTEN_MAX_LEN) |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4409 | flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; |
Dmitry Monakhov | 60d4616 | 2012-10-05 11:32:02 -0400 | [diff] [blame] | 4410 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4411 | /* |
| 4412 | * credits to insert 1 extent into extent tree |
| 4413 | */ |
| 4414 | credits = ext4_chunk_trans_blocks(inode, len); |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4415 | depth = ext_depth(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4416 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4417 | retry: |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4418 | while (ret >= 0 && len) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4419 | /* |
| 4420 | * Recalculate credits when extent tree depth changes. |
| 4421 | */ |
Dan Carpenter | 011c88e | 2016-12-03 16:46:58 -0500 | [diff] [blame] | 4422 | if (depth != ext_depth(inode)) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4423 | credits = ext4_chunk_trans_blocks(inode, len); |
| 4424 | depth = ext_depth(inode); |
| 4425 | } |
| 4426 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 4427 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4428 | credits); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4429 | if (IS_ERR(handle)) { |
| 4430 | ret = PTR_ERR(handle); |
| 4431 | break; |
| 4432 | } |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4433 | ret = ext4_map_blocks(handle, inode, &map, flags); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4434 | if (ret <= 0) { |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4435 | ext4_debug("inode #%lu: block %u: len %u: " |
| 4436 | "ext4_ext_map_blocks returned %d", |
| 4437 | inode->i_ino, map.m_lblk, |
| 4438 | map.m_len, ret); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4439 | ext4_mark_inode_dirty(handle, inode); |
| 4440 | ret2 = ext4_journal_stop(handle); |
| 4441 | break; |
| 4442 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4443 | map.m_lblk += ret; |
| 4444 | map.m_len = len = len - ret; |
| 4445 | epos = (loff_t)map.m_lblk << inode->i_blkbits; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4446 | inode->i_ctime = current_time(inode); |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4447 | if (new_size) { |
| 4448 | if (epos > new_size) |
| 4449 | epos = new_size; |
| 4450 | if (ext4_update_inode_size(inode, epos) & 0x1) |
| 4451 | inode->i_mtime = inode->i_ctime; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4452 | } |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4453 | ret2 = ext4_mark_inode_dirty(handle, inode); |
Eryu Guan | c894aa9 | 2017-12-03 22:52:51 -0500 | [diff] [blame] | 4454 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4455 | ret3 = ext4_journal_stop(handle); |
| 4456 | ret2 = ret3 ? ret3 : ret2; |
| 4457 | if (unlikely(ret2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4458 | break; |
| 4459 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4460 | if (ret == -ENOSPC && |
| 4461 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4462 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4463 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4464 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4465 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4466 | return ret > 0 ? ret2 : ret; |
| 4467 | } |
| 4468 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 4469 | static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len); |
| 4470 | |
| 4471 | static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len); |
| 4472 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4473 | static long ext4_zero_range(struct file *file, loff_t offset, |
| 4474 | loff_t len, int mode) |
| 4475 | { |
| 4476 | struct inode *inode = file_inode(file); |
| 4477 | handle_t *handle = NULL; |
| 4478 | unsigned int max_blocks; |
| 4479 | loff_t new_size = 0; |
| 4480 | int ret = 0; |
| 4481 | int flags; |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4482 | int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4483 | int partial_begin, partial_end; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4484 | loff_t start, end; |
| 4485 | ext4_lblk_t lblk; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4486 | unsigned int blkbits = inode->i_blkbits; |
| 4487 | |
| 4488 | trace_ext4_zero_range(inode, offset, len, mode); |
| 4489 | |
Namjae Jeon | e1ee60f | 2014-05-27 12:48:55 -0400 | [diff] [blame] | 4490 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 4491 | if (ext4_should_journal_data(inode)) { |
| 4492 | ret = ext4_force_commit(inode->i_sb); |
| 4493 | if (ret) |
| 4494 | return ret; |
| 4495 | } |
| 4496 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4497 | /* |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4498 | * Round up offset. This is not fallocate, we neet to zero out |
| 4499 | * blocks, so convert interior block aligned part of the range to |
| 4500 | * unwritten and possibly manually zero out unaligned parts of the |
| 4501 | * range. |
| 4502 | */ |
| 4503 | start = round_up(offset, 1 << blkbits); |
| 4504 | end = round_down((offset + len), 1 << blkbits); |
| 4505 | |
| 4506 | if (start < offset || end > offset + len) |
| 4507 | return -EINVAL; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4508 | partial_begin = offset & ((1 << blkbits) - 1); |
| 4509 | partial_end = (offset + len) & ((1 << blkbits) - 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4510 | |
| 4511 | lblk = start >> blkbits; |
| 4512 | max_blocks = (end >> blkbits); |
| 4513 | if (max_blocks < lblk) |
| 4514 | max_blocks = 0; |
| 4515 | else |
| 4516 | max_blocks -= lblk; |
| 4517 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4518 | inode_lock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4519 | |
| 4520 | /* |
Christophe JAILLET | 80dd497 | 2020-05-03 22:06:47 +0200 | [diff] [blame] | 4521 | * Indirect files do not support unwritten extents |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4522 | */ |
| 4523 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4524 | ret = -EOPNOTSUPP; |
| 4525 | goto out_mutex; |
| 4526 | } |
| 4527 | |
| 4528 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4529 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4530 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4531 | new_size = offset + len; |
| 4532 | ret = inode_newsize_ok(inode, new_size); |
| 4533 | if (ret) |
| 4534 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4535 | } |
| 4536 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4537 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4538 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4539 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4540 | inode_dio_wait(inode); |
| 4541 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4542 | /* Preallocate the range including the unaligned edges */ |
| 4543 | if (partial_begin || partial_end) { |
| 4544 | ret = ext4_alloc_file_blocks(file, |
| 4545 | round_down(offset, 1 << blkbits) >> blkbits, |
| 4546 | (round_up((offset + len), 1 << blkbits) - |
| 4547 | round_down(offset, 1 << blkbits)) >> blkbits, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4548 | new_size, flags); |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4549 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4550 | goto out_mutex; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4551 | |
| 4552 | } |
| 4553 | |
| 4554 | /* Zero range excluding the unaligned edges */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4555 | if (max_blocks > 0) { |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4556 | flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | |
| 4557 | EXT4_EX_NOCACHE); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4558 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4559 | /* |
| 4560 | * Prevent page faults from reinstantiating pages we have |
| 4561 | * released from page cache. |
| 4562 | */ |
| 4563 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 4564 | |
| 4565 | ret = ext4_break_layouts(inode); |
| 4566 | if (ret) { |
| 4567 | up_write(&EXT4_I(inode)->i_mmap_sem); |
| 4568 | goto out_mutex; |
| 4569 | } |
| 4570 | |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4571 | ret = ext4_update_disksize_before_punch(inode, offset, len); |
| 4572 | if (ret) { |
| 4573 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4574 | goto out_mutex; |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4575 | } |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4576 | /* Now release the pages and zero block aligned part of pages */ |
| 4577 | truncate_pagecache_range(inode, start, end - 1); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4578 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4579 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4580 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4581 | flags); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4582 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4583 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4584 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4585 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4586 | if (!partial_begin && !partial_end) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4587 | goto out_mutex; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4588 | |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4589 | /* |
| 4590 | * In worst case we have to writeout two nonadjacent unwritten |
| 4591 | * blocks and update the inode |
| 4592 | */ |
| 4593 | credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1; |
| 4594 | if (ext4_should_journal_data(inode)) |
| 4595 | credits += 2; |
| 4596 | handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4597 | if (IS_ERR(handle)) { |
| 4598 | ret = PTR_ERR(handle); |
| 4599 | ext4_std_error(inode->i_sb, ret); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4600 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4601 | } |
| 4602 | |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4603 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4604 | if (new_size) |
Dmitry Monakhov | 4631dbf | 2014-08-23 17:48:28 -0400 | [diff] [blame] | 4605 | ext4_update_inode_size(inode, new_size); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4606 | ret = ext4_mark_inode_dirty(handle, inode); |
| 4607 | if (unlikely(ret)) |
| 4608 | goto out_handle; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4609 | |
| 4610 | /* Zero out partial block at the edges of the range */ |
| 4611 | ret = ext4_zero_partial_blocks(handle, inode, offset, len); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 4612 | if (ret >= 0) |
| 4613 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4614 | |
| 4615 | if (file->f_flags & O_SYNC) |
| 4616 | ext4_handle_sync(handle); |
| 4617 | |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4618 | out_handle: |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4619 | ext4_journal_stop(handle); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4620 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4621 | inode_unlock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4622 | return ret; |
| 4623 | } |
| 4624 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4625 | /* |
| 4626 | * preallocate space for a file. This implements ext4's fallocate file |
| 4627 | * operation, which gets called from sys_fallocate system call. |
| 4628 | * For block-mapped files, posix_fallocate should fall back to the method |
| 4629 | * of writing zeroes to the required new blocks (the same behavior which is |
| 4630 | * expected for file systems which do not support fallocate() system call). |
| 4631 | */ |
| 4632 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 4633 | { |
| 4634 | struct inode *inode = file_inode(file); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4635 | loff_t new_size = 0; |
| 4636 | unsigned int max_blocks; |
| 4637 | int ret = 0; |
| 4638 | int flags; |
| 4639 | ext4_lblk_t lblk; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4640 | unsigned int blkbits = inode->i_blkbits; |
| 4641 | |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4642 | /* |
| 4643 | * Encrypted inodes can't handle collapse range or insert |
| 4644 | * range since we would need to re-encrypt blocks with a |
| 4645 | * different IV or XTS tweak (which are based on the logical |
| 4646 | * block number). |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4647 | */ |
Chandan Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 4648 | if (IS_ENCRYPTED(inode) && |
Eric Biggers | 457b1e3 | 2019-12-26 09:42:16 -0600 | [diff] [blame] | 4649 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4650 | return -EOPNOTSUPP; |
| 4651 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4652 | /* Return error if mode is not supported */ |
| 4653 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4654 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
| 4655 | FALLOC_FL_INSERT_RANGE)) |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4656 | return -EOPNOTSUPP; |
| 4657 | |
| 4658 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 4659 | return ext4_punch_hole(inode, offset, len); |
| 4660 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4661 | ret = ext4_convert_inline_data(inode); |
| 4662 | if (ret) |
| 4663 | return ret; |
| 4664 | |
Theodore Ts'o | 40c406c | 2014-04-12 22:53:53 -0400 | [diff] [blame] | 4665 | if (mode & FALLOC_FL_COLLAPSE_RANGE) |
| 4666 | return ext4_collapse_range(inode, offset, len); |
| 4667 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4668 | if (mode & FALLOC_FL_INSERT_RANGE) |
| 4669 | return ext4_insert_range(inode, offset, len); |
| 4670 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4671 | if (mode & FALLOC_FL_ZERO_RANGE) |
| 4672 | return ext4_zero_range(file, offset, len, mode); |
| 4673 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4674 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
| 4675 | lblk = offset >> blkbits; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4676 | |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4677 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4678 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4679 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4680 | inode_lock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4681 | |
Davide Italiano | 280227a | 2015-05-02 23:21:15 -0400 | [diff] [blame] | 4682 | /* |
| 4683 | * We only support preallocation for extent-based files only |
| 4684 | */ |
| 4685 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4686 | ret = -EOPNOTSUPP; |
| 4687 | goto out; |
| 4688 | } |
| 4689 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4690 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4691 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4692 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4693 | new_size = offset + len; |
| 4694 | ret = inode_newsize_ok(inode, new_size); |
| 4695 | if (ret) |
| 4696 | goto out; |
| 4697 | } |
| 4698 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4699 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4700 | inode_dio_wait(inode); |
| 4701 | |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4702 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4703 | if (ret) |
| 4704 | goto out; |
| 4705 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4706 | if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { |
| 4707 | ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, |
| 4708 | EXT4_I(inode)->i_sync_tid); |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4709 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4710 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4711 | inode_unlock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4712 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
| 4713 | return ret; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4714 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4715 | |
| 4716 | /* |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4717 | * This function convert a range of blocks to written extents |
| 4718 | * The caller of this function will pass the start offset and the size. |
| 4719 | * all unwritten extents within this range will be converted to |
| 4720 | * written extents. |
| 4721 | * |
| 4722 | * This function is called from the direct IO end io call back |
| 4723 | * function, to convert the fallocated extents after IO is completed. |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 4724 | * Returns 0 on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4725 | */ |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4726 | int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, |
| 4727 | loff_t offset, ssize_t len) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4728 | { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4729 | unsigned int max_blocks; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4730 | int ret = 0, ret2 = 0, ret3 = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4731 | struct ext4_map_blocks map; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4732 | unsigned int blkbits = inode->i_blkbits; |
| 4733 | unsigned int credits = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4734 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4735 | map.m_lblk = offset >> blkbits; |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4736 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
| 4737 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4738 | if (!handle) { |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4739 | /* |
| 4740 | * credits to insert 1 extent into extent tree |
| 4741 | */ |
| 4742 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4743 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4744 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4745 | map.m_lblk += ret; |
| 4746 | map.m_len = (max_blocks -= ret); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4747 | if (credits) { |
| 4748 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4749 | credits); |
| 4750 | if (IS_ERR(handle)) { |
| 4751 | ret = PTR_ERR(handle); |
| 4752 | break; |
| 4753 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4754 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4755 | ret = ext4_map_blocks(handle, inode, &map, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4756 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
Lukas Czerner | b06acd3 | 2013-01-28 21:21:12 -0500 | [diff] [blame] | 4757 | if (ret <= 0) |
| 4758 | ext4_warning(inode->i_sb, |
| 4759 | "inode #%lu: block %u: len %u: " |
| 4760 | "ext4_ext_map_blocks returned %d", |
| 4761 | inode->i_ino, map.m_lblk, |
| 4762 | map.m_len, ret); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4763 | ret2 = ext4_mark_inode_dirty(handle, inode); |
| 4764 | if (credits) { |
| 4765 | ret3 = ext4_journal_stop(handle); |
| 4766 | if (unlikely(ret3)) |
| 4767 | ret2 = ret3; |
| 4768 | } |
| 4769 | |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4770 | if (ret <= 0 || ret2) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4771 | break; |
| 4772 | } |
| 4773 | return ret > 0 ? ret2 : ret; |
| 4774 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4775 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4776 | int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) |
| 4777 | { |
| 4778 | int ret, err = 0; |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4779 | struct ext4_io_end_vec *io_end_vec; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4780 | |
| 4781 | /* |
| 4782 | * This is somewhat ugly but the idea is clear: When transaction is |
| 4783 | * reserved, everything goes into it. Otherwise we rather start several |
| 4784 | * smaller transactions for conversion of each extent separately. |
| 4785 | */ |
| 4786 | if (handle) { |
| 4787 | handle = ext4_journal_start_reserved(handle, |
| 4788 | EXT4_HT_EXT_CONVERT); |
| 4789 | if (IS_ERR(handle)) |
| 4790 | return PTR_ERR(handle); |
| 4791 | } |
| 4792 | |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4793 | list_for_each_entry(io_end_vec, &io_end->list_vec, list) { |
| 4794 | ret = ext4_convert_unwritten_extents(handle, io_end->inode, |
| 4795 | io_end_vec->offset, |
| 4796 | io_end_vec->size); |
| 4797 | if (ret) |
| 4798 | break; |
| 4799 | } |
| 4800 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4801 | if (handle) |
| 4802 | err = ext4_journal_stop(handle); |
| 4803 | |
| 4804 | return ret < 0 ? ret : err; |
| 4805 | } |
| 4806 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4807 | static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4808 | { |
| 4809 | __u64 physical = 0; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4810 | __u64 length = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4811 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 4812 | int error = 0; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4813 | u16 iomap_type; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4814 | |
| 4815 | /* in-inode? */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4816 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4817 | struct ext4_iloc iloc; |
| 4818 | int offset; /* offset of xattr in inode */ |
| 4819 | |
| 4820 | error = ext4_get_inode_loc(inode, &iloc); |
| 4821 | if (error) |
| 4822 | return error; |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4823 | physical = (__u64)iloc.bh->b_blocknr << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4824 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 4825 | EXT4_I(inode)->i_extra_isize; |
| 4826 | physical += offset; |
| 4827 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 4828 | brelse(iloc.bh); |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4829 | iomap_type = IOMAP_INLINE; |
| 4830 | } else if (EXT4_I(inode)->i_file_acl) { /* external block */ |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4831 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4832 | length = inode->i_sb->s_blocksize; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4833 | iomap_type = IOMAP_MAPPED; |
| 4834 | } else { |
| 4835 | /* no in-inode or external block for xattr, so return -ENOENT */ |
| 4836 | error = -ENOENT; |
| 4837 | goto out; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4838 | } |
| 4839 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4840 | iomap->addr = physical; |
| 4841 | iomap->offset = 0; |
| 4842 | iomap->length = length; |
| 4843 | iomap->type = iomap_type; |
| 4844 | iomap->flags = 0; |
| 4845 | out: |
| 4846 | return error; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4847 | } |
| 4848 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4849 | static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset, |
| 4850 | loff_t length, unsigned flags, |
| 4851 | struct iomap *iomap, struct iomap *srcmap) |
| 4852 | { |
| 4853 | int error; |
| 4854 | |
| 4855 | error = ext4_iomap_xattr_fiemap(inode, iomap); |
| 4856 | if (error == 0 && (offset >= iomap->length)) |
| 4857 | error = -ENOENT; |
| 4858 | return error; |
| 4859 | } |
| 4860 | |
| 4861 | static const struct iomap_ops ext4_iomap_xattr_ops = { |
| 4862 | .iomap_begin = ext4_iomap_xattr_begin, |
| 4863 | }; |
| 4864 | |
| 4865 | static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4866 | __u64 start, __u64 len, bool from_es_cache) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4867 | { |
| 4868 | ext4_lblk_t start_blk; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4869 | u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4870 | int error = 0; |
| 4871 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 4872 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 4873 | error = ext4_ext_precache(inode); |
| 4874 | if (error) |
| 4875 | return error; |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4876 | fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 4877 | } |
| 4878 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4879 | if (from_es_cache) |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4880 | ext4_fiemap_flags &= FIEMAP_FLAG_XATTR; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4881 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4882 | if (fiemap_check_flags(fieinfo, ext4_fiemap_flags)) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4883 | return -EBADR; |
| 4884 | |
| 4885 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4886 | fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; |
| 4887 | error = iomap_fiemap(inode, fieinfo, start, len, |
| 4888 | &ext4_iomap_xattr_ops); |
| 4889 | } else if (!from_es_cache) { |
| 4890 | error = iomap_fiemap(inode, fieinfo, start, len, |
| 4891 | &ext4_iomap_report_ops); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4892 | } else { |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4893 | ext4_lblk_t len_blks; |
| 4894 | __u64 last_blk; |
| 4895 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4896 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4897 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 4898 | if (last_blk >= EXT_MAX_BLOCKS) |
| 4899 | last_blk = EXT_MAX_BLOCKS-1; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4900 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4901 | |
| 4902 | /* |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4903 | * Walk the extent tree gathering extent information |
| 4904 | * and pushing extents back to the user. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4905 | */ |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4906 | error = ext4_fill_es_cache_info(inode, start_blk, len_blks, |
| 4907 | fieinfo); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4908 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4909 | return error; |
| 4910 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4911 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4912 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4913 | __u64 start, __u64 len) |
| 4914 | { |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4915 | return _ext4_fiemap(inode, fieinfo, start, len, false); |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4919 | __u64 start, __u64 len) |
| 4920 | { |
| 4921 | if (ext4_has_inline_data(inode)) { |
| 4922 | int has_inline; |
| 4923 | |
| 4924 | down_read(&EXT4_I(inode)->xattr_sem); |
| 4925 | has_inline = ext4_has_inline_data(inode); |
| 4926 | up_read(&EXT4_I(inode)->xattr_sem); |
| 4927 | if (has_inline) |
| 4928 | return 0; |
| 4929 | } |
| 4930 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4931 | return _ext4_fiemap(inode, fieinfo, start, len, true); |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4935 | /* |
| 4936 | * ext4_access_path: |
| 4937 | * Function to access the path buffer for marking it dirty. |
| 4938 | * It also checks if there are sufficient credits left in the journal handle |
| 4939 | * to update path. |
| 4940 | */ |
| 4941 | static int |
| 4942 | ext4_access_path(handle_t *handle, struct inode *inode, |
| 4943 | struct ext4_ext_path *path) |
| 4944 | { |
| 4945 | int credits, err; |
| 4946 | |
| 4947 | if (!ext4_handle_valid(handle)) |
| 4948 | return 0; |
| 4949 | |
| 4950 | /* |
| 4951 | * Check if need to extend journal credits |
| 4952 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 4953 | * descriptor) for each block group; assume two block |
| 4954 | * groups |
| 4955 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 4956 | credits = ext4_writepage_trans_blocks(inode); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 4957 | err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 4958 | if (err < 0) |
| 4959 | return err; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4960 | |
| 4961 | err = ext4_ext_get_access(handle, inode, path); |
| 4962 | return err; |
| 4963 | } |
| 4964 | |
| 4965 | /* |
| 4966 | * ext4_ext_shift_path_extents: |
| 4967 | * Shift the extents of a path structure lying between path[depth].p_ext |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4968 | * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells |
| 4969 | * if it is right shift or left shift operation. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4970 | */ |
| 4971 | static int |
| 4972 | ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, |
| 4973 | struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4974 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4975 | { |
| 4976 | int depth, err = 0; |
| 4977 | struct ext4_extent *ex_start, *ex_last; |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 4978 | bool update = false; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4979 | depth = path->p_depth; |
| 4980 | |
| 4981 | while (depth >= 0) { |
| 4982 | if (depth == path->p_depth) { |
| 4983 | ex_start = path[depth].p_ext; |
| 4984 | if (!ex_start) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4985 | return -EFSCORRUPTED; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4986 | |
| 4987 | ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4988 | |
| 4989 | err = ext4_access_path(handle, inode, path + depth); |
| 4990 | if (err) |
| 4991 | goto out; |
| 4992 | |
| 4993 | if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 4994 | update = true; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4995 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4996 | while (ex_start <= ex_last) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4997 | if (SHIFT == SHIFT_LEFT) { |
| 4998 | le32_add_cpu(&ex_start->ee_block, |
| 4999 | -shift); |
| 5000 | /* Try to merge to the left. */ |
| 5001 | if ((ex_start > |
| 5002 | EXT_FIRST_EXTENT(path[depth].p_hdr)) |
| 5003 | && |
| 5004 | ext4_ext_try_to_merge_right(inode, |
| 5005 | path, ex_start - 1)) |
| 5006 | ex_last--; |
| 5007 | else |
| 5008 | ex_start++; |
| 5009 | } else { |
| 5010 | le32_add_cpu(&ex_last->ee_block, shift); |
| 5011 | ext4_ext_try_to_merge_right(inode, path, |
| 5012 | ex_last); |
Lukas Czerner | 6dd834e | 2014-04-18 10:55:24 -0400 | [diff] [blame] | 5013 | ex_last--; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5014 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5015 | } |
| 5016 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5017 | if (err) |
| 5018 | goto out; |
| 5019 | |
| 5020 | if (--depth < 0 || !update) |
| 5021 | break; |
| 5022 | } |
| 5023 | |
| 5024 | /* Update index too */ |
| 5025 | err = ext4_access_path(handle, inode, path + depth); |
| 5026 | if (err) |
| 5027 | goto out; |
| 5028 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5029 | if (SHIFT == SHIFT_LEFT) |
| 5030 | le32_add_cpu(&path[depth].p_idx->ei_block, -shift); |
| 5031 | else |
| 5032 | le32_add_cpu(&path[depth].p_idx->ei_block, shift); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5033 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5034 | if (err) |
| 5035 | goto out; |
| 5036 | |
| 5037 | /* we are done if current index is not a starting index */ |
| 5038 | if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) |
| 5039 | break; |
| 5040 | |
| 5041 | depth--; |
| 5042 | } |
| 5043 | |
| 5044 | out: |
| 5045 | return err; |
| 5046 | } |
| 5047 | |
| 5048 | /* |
| 5049 | * ext4_ext_shift_extents: |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5050 | * All the extents which lies in the range from @start to the last allocated |
| 5051 | * block for the @inode are shifted either towards left or right (depending |
| 5052 | * upon @SHIFT) by @shift blocks. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5053 | * On success, 0 is returned, error otherwise. |
| 5054 | */ |
| 5055 | static int |
| 5056 | ext4_ext_shift_extents(struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5057 | ext4_lblk_t start, ext4_lblk_t shift, |
| 5058 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5059 | { |
| 5060 | struct ext4_ext_path *path; |
| 5061 | int ret = 0, depth; |
| 5062 | struct ext4_extent *extent; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5063 | ext4_lblk_t stop, *iterator, ex_start, ex_end; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5064 | |
| 5065 | /* Let path point to the last extent */ |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5066 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
| 5067 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5068 | if (IS_ERR(path)) |
| 5069 | return PTR_ERR(path); |
| 5070 | |
| 5071 | depth = path->p_depth; |
| 5072 | extent = path[depth].p_ext; |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5073 | if (!extent) |
| 5074 | goto out; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5075 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5076 | stop = le32_to_cpu(extent->ee_block); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5077 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5078 | /* |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5079 | * For left shifts, make sure the hole on the left is big enough to |
| 5080 | * accommodate the shift. For right shifts, make sure the last extent |
| 5081 | * won't be shifted beyond EXT_MAX_BLOCKS. |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5082 | */ |
| 5083 | if (SHIFT == SHIFT_LEFT) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5084 | path = ext4_find_extent(inode, start - 1, &path, |
| 5085 | EXT4_EX_NOCACHE); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5086 | if (IS_ERR(path)) |
| 5087 | return PTR_ERR(path); |
| 5088 | depth = path->p_depth; |
| 5089 | extent = path[depth].p_ext; |
| 5090 | if (extent) { |
| 5091 | ex_start = le32_to_cpu(extent->ee_block); |
| 5092 | ex_end = le32_to_cpu(extent->ee_block) + |
| 5093 | ext4_ext_get_actual_len(extent); |
| 5094 | } else { |
| 5095 | ex_start = 0; |
| 5096 | ex_end = 0; |
| 5097 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5098 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5099 | if ((start == ex_start && shift > ex_start) || |
| 5100 | (shift > start - ex_end)) { |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5101 | ret = -EINVAL; |
| 5102 | goto out; |
| 5103 | } |
| 5104 | } else { |
| 5105 | if (shift > EXT_MAX_BLOCKS - |
| 5106 | (stop + ext4_ext_get_actual_len(extent))) { |
| 5107 | ret = -EINVAL; |
| 5108 | goto out; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5109 | } |
Dmitry Monakhov | 8dc79ec | 2014-04-13 15:05:42 -0400 | [diff] [blame] | 5110 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5111 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5112 | /* |
| 5113 | * In case of left shift, iterator points to start and it is increased |
| 5114 | * till we reach stop. In case of right shift, iterator points to stop |
| 5115 | * and it is decreased till we reach start. |
| 5116 | */ |
| 5117 | if (SHIFT == SHIFT_LEFT) |
| 5118 | iterator = &start; |
| 5119 | else |
| 5120 | iterator = &stop; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5121 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5122 | /* |
| 5123 | * Its safe to start updating extents. Start and stop are unsigned, so |
| 5124 | * in case of right shift if extent with 0 block is reached, iterator |
| 5125 | * becomes NULL to indicate the end of the loop. |
| 5126 | */ |
| 5127 | while (iterator && start <= stop) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5128 | path = ext4_find_extent(inode, *iterator, &path, |
| 5129 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5130 | if (IS_ERR(path)) |
| 5131 | return PTR_ERR(path); |
| 5132 | depth = path->p_depth; |
| 5133 | extent = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5134 | if (!extent) { |
| 5135 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5136 | (unsigned long) *iterator); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5137 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5138 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5139 | if (SHIFT == SHIFT_LEFT && *iterator > |
| 5140 | le32_to_cpu(extent->ee_block)) { |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5141 | /* Hole, move to the next extent */ |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5142 | if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 5143 | path[depth].p_ext++; |
| 5144 | } else { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5145 | *iterator = ext4_ext_next_allocated_block(path); |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5146 | continue; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5147 | } |
| 5148 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5149 | |
| 5150 | if (SHIFT == SHIFT_LEFT) { |
| 5151 | extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
| 5152 | *iterator = le32_to_cpu(extent->ee_block) + |
| 5153 | ext4_ext_get_actual_len(extent); |
| 5154 | } else { |
| 5155 | extent = EXT_FIRST_EXTENT(path[depth].p_hdr); |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5156 | if (le32_to_cpu(extent->ee_block) > 0) |
| 5157 | *iterator = le32_to_cpu(extent->ee_block) - 1; |
| 5158 | else |
| 5159 | /* Beginning is reached, end of the loop */ |
| 5160 | iterator = NULL; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5161 | /* Update path extent in case we need to stop */ |
| 5162 | while (le32_to_cpu(extent->ee_block) < start) |
| 5163 | extent++; |
| 5164 | path[depth].p_ext = extent; |
| 5165 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5166 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5167 | handle, SHIFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5168 | if (ret) |
| 5169 | break; |
| 5170 | } |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5171 | out: |
| 5172 | ext4_ext_drop_refs(path); |
| 5173 | kfree(path); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5174 | return ret; |
| 5175 | } |
| 5176 | |
| 5177 | /* |
| 5178 | * ext4_collapse_range: |
| 5179 | * This implements the fallocate's collapse range functionality for ext4 |
| 5180 | * Returns: 0 and non-zero on error. |
| 5181 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5182 | static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5183 | { |
| 5184 | struct super_block *sb = inode->i_sb; |
| 5185 | ext4_lblk_t punch_start, punch_stop; |
| 5186 | handle_t *handle; |
| 5187 | unsigned int credits; |
Namjae Jeon | a8680e0 | 2014-04-19 16:37:31 -0400 | [diff] [blame] | 5188 | loff_t new_size, ioffset; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5189 | int ret; |
| 5190 | |
Theodore Ts'o | b9576fc | 2015-05-15 00:24:10 -0400 | [diff] [blame] | 5191 | /* |
| 5192 | * We need to test this early because xfstests assumes that a |
| 5193 | * collapse range of (0, 1) will return EOPNOTSUPP if the file |
| 5194 | * system does not support collapse range. |
| 5195 | */ |
| 5196 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5197 | return -EOPNOTSUPP; |
| 5198 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5199 | /* Collapse range works only on fs cluster size aligned regions. */ |
| 5200 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5201 | return -EINVAL; |
| 5202 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5203 | trace_ext4_collapse_range(inode, offset, len); |
| 5204 | |
| 5205 | punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5206 | punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5207 | |
Namjae Jeon | 1ce01c4 | 2014-04-10 22:58:20 -0400 | [diff] [blame] | 5208 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 5209 | if (ext4_should_journal_data(inode)) { |
| 5210 | ret = ext4_force_commit(inode->i_sb); |
| 5211 | if (ret) |
| 5212 | return ret; |
| 5213 | } |
| 5214 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5215 | inode_lock(inode); |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5216 | /* |
| 5217 | * There is no need to overlap collapse range with EOF, in which case |
| 5218 | * it is effectively a truncate operation |
| 5219 | */ |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5220 | if (offset + len >= inode->i_size) { |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5221 | ret = -EINVAL; |
| 5222 | goto out_mutex; |
| 5223 | } |
| 5224 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5225 | /* Currently just for extent based files */ |
| 5226 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5227 | ret = -EOPNOTSUPP; |
| 5228 | goto out_mutex; |
| 5229 | } |
| 5230 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5231 | /* Wait for existing dio to complete */ |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5232 | inode_dio_wait(inode); |
| 5233 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5234 | /* |
| 5235 | * Prevent page faults from reinstantiating pages we have released from |
| 5236 | * page cache. |
| 5237 | */ |
| 5238 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5239 | |
| 5240 | ret = ext4_break_layouts(inode); |
| 5241 | if (ret) |
| 5242 | goto out_mmap; |
| 5243 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5244 | /* |
| 5245 | * Need to round down offset to be aligned with page size boundary |
| 5246 | * for page size > block size. |
| 5247 | */ |
| 5248 | ioffset = round_down(offset, PAGE_SIZE); |
| 5249 | /* |
| 5250 | * Write tail of the last page before removed range since it will get |
| 5251 | * removed from the page cache below. |
| 5252 | */ |
| 5253 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset); |
| 5254 | if (ret) |
| 5255 | goto out_mmap; |
| 5256 | /* |
| 5257 | * Write data that will be shifted to preserve them when discarding |
| 5258 | * page cache below. We are also protected from pages becoming dirty |
| 5259 | * by i_mmap_sem. |
| 5260 | */ |
| 5261 | ret = filemap_write_and_wait_range(inode->i_mapping, offset + len, |
| 5262 | LLONG_MAX); |
| 5263 | if (ret) |
| 5264 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5265 | truncate_pagecache(inode, ioffset); |
| 5266 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5267 | credits = ext4_writepage_trans_blocks(inode); |
| 5268 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5269 | if (IS_ERR(handle)) { |
| 5270 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5271 | goto out_mmap; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5275 | ext4_discard_preallocations(inode); |
| 5276 | |
| 5277 | ret = ext4_es_remove_extent(inode, punch_start, |
Lukas Czerner | 2c1d232 | 2014-04-18 10:43:21 -0400 | [diff] [blame] | 5278 | EXT_MAX_BLOCKS - punch_start); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5279 | if (ret) { |
| 5280 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5281 | goto out_stop; |
| 5282 | } |
| 5283 | |
| 5284 | ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); |
| 5285 | if (ret) { |
| 5286 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5287 | goto out_stop; |
| 5288 | } |
Lukas Czerner | ef24f6c | 2014-04-18 10:50:23 -0400 | [diff] [blame] | 5289 | ext4_discard_preallocations(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5290 | |
| 5291 | ret = ext4_ext_shift_extents(inode, handle, punch_stop, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5292 | punch_stop - punch_start, SHIFT_LEFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5293 | if (ret) { |
| 5294 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5295 | goto out_stop; |
| 5296 | } |
| 5297 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5298 | new_size = inode->i_size - len; |
Lukas Czerner | 9337d5d | 2014-04-18 10:48:25 -0400 | [diff] [blame] | 5299 | i_size_write(inode, new_size); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5300 | EXT4_I(inode)->i_disksize = new_size; |
| 5301 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5302 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5303 | if (IS_SYNC(inode)) |
| 5304 | ext4_handle_sync(handle); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5305 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 5306 | ret = ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5307 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5308 | |
| 5309 | out_stop: |
| 5310 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5311 | out_mmap: |
| 5312 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5313 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5314 | inode_unlock(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5315 | return ret; |
| 5316 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5317 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5318 | /* |
| 5319 | * ext4_insert_range: |
| 5320 | * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. |
| 5321 | * The data blocks starting from @offset to the EOF are shifted by @len |
| 5322 | * towards right to create a hole in the @inode. Inode size is increased |
| 5323 | * by len bytes. |
| 5324 | * Returns 0 on success, error otherwise. |
| 5325 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5326 | static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len) |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5327 | { |
| 5328 | struct super_block *sb = inode->i_sb; |
| 5329 | handle_t *handle; |
| 5330 | struct ext4_ext_path *path; |
| 5331 | struct ext4_extent *extent; |
| 5332 | ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0; |
| 5333 | unsigned int credits, ee_len; |
| 5334 | int ret = 0, depth, split_flag = 0; |
| 5335 | loff_t ioffset; |
| 5336 | |
| 5337 | /* |
| 5338 | * We need to test this early because xfstests assumes that an |
| 5339 | * insert range of (0, 1) will return EOPNOTSUPP if the file |
| 5340 | * system does not support insert range. |
| 5341 | */ |
| 5342 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5343 | return -EOPNOTSUPP; |
| 5344 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5345 | /* Insert range works only on fs cluster size aligned regions. */ |
| 5346 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5347 | return -EINVAL; |
| 5348 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5349 | trace_ext4_insert_range(inode, offset, len); |
| 5350 | |
| 5351 | offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5352 | len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5353 | |
| 5354 | /* Call ext4_force_commit to flush all data in case of data=journal */ |
| 5355 | if (ext4_should_journal_data(inode)) { |
| 5356 | ret = ext4_force_commit(inode->i_sb); |
| 5357 | if (ret) |
| 5358 | return ret; |
| 5359 | } |
| 5360 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5361 | inode_lock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5362 | /* Currently just for extent based files */ |
| 5363 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5364 | ret = -EOPNOTSUPP; |
| 5365 | goto out_mutex; |
| 5366 | } |
| 5367 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5368 | /* Check whether the maximum file size would be exceeded */ |
| 5369 | if (len > inode->i_sb->s_maxbytes - inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5370 | ret = -EFBIG; |
| 5371 | goto out_mutex; |
| 5372 | } |
| 5373 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5374 | /* Offset must be less than i_size */ |
| 5375 | if (offset >= inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5376 | ret = -EINVAL; |
| 5377 | goto out_mutex; |
| 5378 | } |
| 5379 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5380 | /* Wait for existing dio to complete */ |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5381 | inode_dio_wait(inode); |
| 5382 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5383 | /* |
| 5384 | * Prevent page faults from reinstantiating pages we have released from |
| 5385 | * page cache. |
| 5386 | */ |
| 5387 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5388 | |
| 5389 | ret = ext4_break_layouts(inode); |
| 5390 | if (ret) |
| 5391 | goto out_mmap; |
| 5392 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5393 | /* |
| 5394 | * Need to round down to align start offset to page size boundary |
| 5395 | * for page size > block size. |
| 5396 | */ |
| 5397 | ioffset = round_down(offset, PAGE_SIZE); |
| 5398 | /* Write out all dirty pages */ |
| 5399 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, |
| 5400 | LLONG_MAX); |
| 5401 | if (ret) |
| 5402 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5403 | truncate_pagecache(inode, ioffset); |
| 5404 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5405 | credits = ext4_writepage_trans_blocks(inode); |
| 5406 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5407 | if (IS_ERR(handle)) { |
| 5408 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5409 | goto out_mmap; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | /* Expand file to avoid data loss if there is error while shifting */ |
| 5413 | inode->i_size += len; |
| 5414 | EXT4_I(inode)->i_disksize += len; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5415 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5416 | ret = ext4_mark_inode_dirty(handle, inode); |
| 5417 | if (ret) |
| 5418 | goto out_stop; |
| 5419 | |
| 5420 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5421 | ext4_discard_preallocations(inode); |
| 5422 | |
| 5423 | path = ext4_find_extent(inode, offset_lblk, NULL, 0); |
| 5424 | if (IS_ERR(path)) { |
| 5425 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5426 | goto out_stop; |
| 5427 | } |
| 5428 | |
| 5429 | depth = ext_depth(inode); |
| 5430 | extent = path[depth].p_ext; |
| 5431 | if (extent) { |
| 5432 | ee_start_lblk = le32_to_cpu(extent->ee_block); |
| 5433 | ee_len = ext4_ext_get_actual_len(extent); |
| 5434 | |
| 5435 | /* |
| 5436 | * If offset_lblk is not the starting block of extent, split |
| 5437 | * the extent @offset_lblk |
| 5438 | */ |
| 5439 | if ((offset_lblk > ee_start_lblk) && |
| 5440 | (offset_lblk < (ee_start_lblk + ee_len))) { |
| 5441 | if (ext4_ext_is_unwritten(extent)) |
| 5442 | split_flag = EXT4_EXT_MARK_UNWRIT1 | |
| 5443 | EXT4_EXT_MARK_UNWRIT2; |
| 5444 | ret = ext4_split_extent_at(handle, inode, &path, |
| 5445 | offset_lblk, split_flag, |
| 5446 | EXT4_EX_NOCACHE | |
| 5447 | EXT4_GET_BLOCKS_PRE_IO | |
| 5448 | EXT4_GET_BLOCKS_METADATA_NOFAIL); |
| 5449 | } |
| 5450 | |
| 5451 | ext4_ext_drop_refs(path); |
| 5452 | kfree(path); |
| 5453 | if (ret < 0) { |
| 5454 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5455 | goto out_stop; |
| 5456 | } |
Fabian Frederick | edf15aa1 | 2016-09-15 11:39:52 -0400 | [diff] [blame] | 5457 | } else { |
| 5458 | ext4_ext_drop_refs(path); |
| 5459 | kfree(path); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | ret = ext4_es_remove_extent(inode, offset_lblk, |
| 5463 | EXT_MAX_BLOCKS - offset_lblk); |
| 5464 | if (ret) { |
| 5465 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5466 | goto out_stop; |
| 5467 | } |
| 5468 | |
| 5469 | /* |
| 5470 | * if offset_lblk lies in a hole which is at start of file, use |
| 5471 | * ee_start_lblk to shift extents |
| 5472 | */ |
| 5473 | ret = ext4_ext_shift_extents(inode, handle, |
| 5474 | ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk, |
| 5475 | len_lblk, SHIFT_RIGHT); |
| 5476 | |
| 5477 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5478 | if (IS_SYNC(inode)) |
| 5479 | ext4_handle_sync(handle); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5480 | if (ret >= 0) |
| 5481 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5482 | |
| 5483 | out_stop: |
| 5484 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5485 | out_mmap: |
| 5486 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5487 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5488 | inode_unlock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5489 | return ret; |
| 5490 | } |
| 5491 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5492 | /** |
Theodore Ts'o | c60990b | 2019-06-19 16:30:03 -0400 | [diff] [blame] | 5493 | * ext4_swap_extents() - Swap extents between two inodes |
| 5494 | * @handle: handle for this transaction |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5495 | * @inode1: First inode |
| 5496 | * @inode2: Second inode |
| 5497 | * @lblk1: Start block for first inode |
| 5498 | * @lblk2: Start block for second inode |
| 5499 | * @count: Number of blocks to swap |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5500 | * @unwritten: Mark second inode's extents as unwritten after swap |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5501 | * @erp: Pointer to save error value |
| 5502 | * |
| 5503 | * This helper routine does exactly what is promise "swap extents". All other |
| 5504 | * stuff such as page-cache locking consistency, bh mapping consistency or |
| 5505 | * extent's data copying must be performed by caller. |
| 5506 | * Locking: |
| 5507 | * i_mutex is held for both inodes |
| 5508 | * i_data_sem is locked for write for both inodes |
| 5509 | * Assumptions: |
| 5510 | * All pages from requested range are locked for both inodes |
| 5511 | */ |
| 5512 | int |
| 5513 | ext4_swap_extents(handle_t *handle, struct inode *inode1, |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5514 | struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5515 | ext4_lblk_t count, int unwritten, int *erp) |
| 5516 | { |
| 5517 | struct ext4_ext_path *path1 = NULL; |
| 5518 | struct ext4_ext_path *path2 = NULL; |
| 5519 | int replaced_count = 0; |
| 5520 | |
| 5521 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); |
| 5522 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5523 | BUG_ON(!inode_is_locked(inode1)); |
| 5524 | BUG_ON(!inode_is_locked(inode2)); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5525 | |
| 5526 | *erp = ext4_es_remove_extent(inode1, lblk1, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5527 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5528 | return 0; |
| 5529 | *erp = ext4_es_remove_extent(inode2, lblk2, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5530 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5531 | return 0; |
| 5532 | |
| 5533 | while (count) { |
| 5534 | struct ext4_extent *ex1, *ex2, tmp_ex; |
| 5535 | ext4_lblk_t e1_blk, e2_blk; |
| 5536 | int e1_len, e2_len, len; |
| 5537 | int split = 0; |
| 5538 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5539 | path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5540 | if (IS_ERR(path1)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5541 | *erp = PTR_ERR(path1); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5542 | path1 = NULL; |
| 5543 | finish: |
| 5544 | count = 0; |
| 5545 | goto repeat; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5546 | } |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5547 | path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5548 | if (IS_ERR(path2)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5549 | *erp = PTR_ERR(path2); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5550 | path2 = NULL; |
| 5551 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5552 | } |
| 5553 | ex1 = path1[path1->p_depth].p_ext; |
| 5554 | ex2 = path2[path2->p_depth].p_ext; |
| 5555 | /* Do we have somthing to swap ? */ |
| 5556 | if (unlikely(!ex2 || !ex1)) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5557 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5558 | |
| 5559 | e1_blk = le32_to_cpu(ex1->ee_block); |
| 5560 | e2_blk = le32_to_cpu(ex2->ee_block); |
| 5561 | e1_len = ext4_ext_get_actual_len(ex1); |
| 5562 | e2_len = ext4_ext_get_actual_len(ex2); |
| 5563 | |
| 5564 | /* Hole handling */ |
| 5565 | if (!in_range(lblk1, e1_blk, e1_len) || |
| 5566 | !in_range(lblk2, e2_blk, e2_len)) { |
| 5567 | ext4_lblk_t next1, next2; |
| 5568 | |
| 5569 | /* if hole after extent, then go to next extent */ |
| 5570 | next1 = ext4_ext_next_allocated_block(path1); |
| 5571 | next2 = ext4_ext_next_allocated_block(path2); |
| 5572 | /* If hole before extent, then shift to that extent */ |
| 5573 | if (e1_blk > lblk1) |
| 5574 | next1 = e1_blk; |
| 5575 | if (e2_blk > lblk2) |
Maninder Singh | 4e56201 | 2017-08-06 01:33:07 -0400 | [diff] [blame] | 5576 | next2 = e2_blk; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5577 | /* Do we have something to swap */ |
| 5578 | if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5579 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5580 | /* Move to the rightest boundary */ |
| 5581 | len = next1 - lblk1; |
| 5582 | if (len < next2 - lblk2) |
| 5583 | len = next2 - lblk2; |
| 5584 | if (len > count) |
| 5585 | len = count; |
| 5586 | lblk1 += len; |
| 5587 | lblk2 += len; |
| 5588 | count -= len; |
| 5589 | goto repeat; |
| 5590 | } |
| 5591 | |
| 5592 | /* Prepare left boundary */ |
| 5593 | if (e1_blk < lblk1) { |
| 5594 | split = 1; |
| 5595 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5596 | &path1, lblk1, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5597 | if (unlikely(*erp)) |
| 5598 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5599 | } |
| 5600 | if (e2_blk < lblk2) { |
| 5601 | split = 1; |
| 5602 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5603 | &path2, lblk2, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5604 | if (unlikely(*erp)) |
| 5605 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5606 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5607 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5608 | * path must to be revalidated. */ |
| 5609 | if (split) |
| 5610 | goto repeat; |
| 5611 | |
| 5612 | /* Prepare right boundary */ |
| 5613 | len = count; |
| 5614 | if (len > e1_blk + e1_len - lblk1) |
| 5615 | len = e1_blk + e1_len - lblk1; |
| 5616 | if (len > e2_blk + e2_len - lblk2) |
| 5617 | len = e2_blk + e2_len - lblk2; |
| 5618 | |
| 5619 | if (len != e1_len) { |
| 5620 | split = 1; |
| 5621 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5622 | &path1, lblk1 + len, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5623 | if (unlikely(*erp)) |
| 5624 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5625 | } |
| 5626 | if (len != e2_len) { |
| 5627 | split = 1; |
| 5628 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5629 | &path2, lblk2 + len, 0); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5630 | if (*erp) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5631 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5632 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5633 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5634 | * path must to be revalidated. */ |
| 5635 | if (split) |
| 5636 | goto repeat; |
| 5637 | |
| 5638 | BUG_ON(e2_len != e1_len); |
| 5639 | *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5640 | if (unlikely(*erp)) |
| 5641 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5642 | *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5643 | if (unlikely(*erp)) |
| 5644 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5645 | |
| 5646 | /* Both extents are fully inside boundaries. Swap it now */ |
| 5647 | tmp_ex = *ex1; |
| 5648 | ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); |
| 5649 | ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); |
| 5650 | ex1->ee_len = cpu_to_le16(e2_len); |
| 5651 | ex2->ee_len = cpu_to_le16(e1_len); |
| 5652 | if (unwritten) |
| 5653 | ext4_ext_mark_unwritten(ex2); |
| 5654 | if (ext4_ext_is_unwritten(&tmp_ex)) |
| 5655 | ext4_ext_mark_unwritten(ex1); |
| 5656 | |
| 5657 | ext4_ext_try_to_merge(handle, inode2, path2, ex2); |
| 5658 | ext4_ext_try_to_merge(handle, inode1, path1, ex1); |
| 5659 | *erp = ext4_ext_dirty(handle, inode2, path2 + |
| 5660 | path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5661 | if (unlikely(*erp)) |
| 5662 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5663 | *erp = ext4_ext_dirty(handle, inode1, path1 + |
| 5664 | path1->p_depth); |
| 5665 | /* |
| 5666 | * Looks scarry ah..? second inode already points to new blocks, |
| 5667 | * and it was successfully dirtied. But luckily error may happen |
| 5668 | * only due to journal error, so full transaction will be |
| 5669 | * aborted anyway. |
| 5670 | */ |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5671 | if (unlikely(*erp)) |
| 5672 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5673 | lblk1 += len; |
| 5674 | lblk2 += len; |
| 5675 | replaced_count += len; |
| 5676 | count -= len; |
| 5677 | |
| 5678 | repeat: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 5679 | ext4_ext_drop_refs(path1); |
| 5680 | kfree(path1); |
| 5681 | ext4_ext_drop_refs(path2); |
| 5682 | kfree(path2); |
| 5683 | path1 = path2 = NULL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5684 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5685 | return replaced_count; |
| 5686 | } |
Eric Whitney | 0b02f4c | 2018-10-01 14:19:37 -0400 | [diff] [blame] | 5687 | |
| 5688 | /* |
| 5689 | * ext4_clu_mapped - determine whether any block in a logical cluster has |
| 5690 | * been mapped to a physical cluster |
| 5691 | * |
| 5692 | * @inode - file containing the logical cluster |
| 5693 | * @lclu - logical cluster of interest |
| 5694 | * |
| 5695 | * Returns 1 if any block in the logical cluster is mapped, signifying |
| 5696 | * that a physical cluster has been allocated for it. Otherwise, |
| 5697 | * returns 0. Can also return negative error codes. Derived from |
| 5698 | * ext4_ext_map_blocks(). |
| 5699 | */ |
| 5700 | int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) |
| 5701 | { |
| 5702 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5703 | struct ext4_ext_path *path; |
| 5704 | int depth, mapped = 0, err = 0; |
| 5705 | struct ext4_extent *extent; |
| 5706 | ext4_lblk_t first_lblk, first_lclu, last_lclu; |
| 5707 | |
| 5708 | /* search for the extent closest to the first block in the cluster */ |
| 5709 | path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); |
| 5710 | if (IS_ERR(path)) { |
| 5711 | err = PTR_ERR(path); |
| 5712 | path = NULL; |
| 5713 | goto out; |
| 5714 | } |
| 5715 | |
| 5716 | depth = ext_depth(inode); |
| 5717 | |
| 5718 | /* |
| 5719 | * A consistent leaf must not be empty. This situation is possible, |
| 5720 | * though, _during_ tree modification, and it's why an assert can't |
| 5721 | * be put in ext4_find_extent(). |
| 5722 | */ |
| 5723 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 5724 | EXT4_ERROR_INODE(inode, |
| 5725 | "bad extent address - lblock: %lu, depth: %d, pblock: %lld", |
| 5726 | (unsigned long) EXT4_C2B(sbi, lclu), |
| 5727 | depth, path[depth].p_block); |
| 5728 | err = -EFSCORRUPTED; |
| 5729 | goto out; |
| 5730 | } |
| 5731 | |
| 5732 | extent = path[depth].p_ext; |
| 5733 | |
| 5734 | /* can't be mapped if the extent tree is empty */ |
| 5735 | if (extent == NULL) |
| 5736 | goto out; |
| 5737 | |
| 5738 | first_lblk = le32_to_cpu(extent->ee_block); |
| 5739 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5740 | |
| 5741 | /* |
| 5742 | * Three possible outcomes at this point - found extent spanning |
| 5743 | * the target cluster, to the left of the target cluster, or to the |
| 5744 | * right of the target cluster. The first two cases are handled here. |
| 5745 | * The last case indicates the target cluster is not mapped. |
| 5746 | */ |
| 5747 | if (lclu >= first_lclu) { |
| 5748 | last_lclu = EXT4_B2C(sbi, first_lblk + |
| 5749 | ext4_ext_get_actual_len(extent) - 1); |
| 5750 | if (lclu <= last_lclu) { |
| 5751 | mapped = 1; |
| 5752 | } else { |
| 5753 | first_lblk = ext4_ext_next_allocated_block(path); |
| 5754 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5755 | if (lclu == first_lclu) |
| 5756 | mapped = 1; |
| 5757 | } |
| 5758 | } |
| 5759 | |
| 5760 | out: |
| 5761 | ext4_ext_drop_refs(path); |
| 5762 | kfree(path); |
| 5763 | |
| 5764 | return err ? err : mapped; |
| 5765 | } |