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); |
| 300 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 301 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 302 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, |
| 303 | EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | |
| 304 | (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0)); |
| 305 | } |
| 306 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 307 | static int |
| 308 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 309 | { |
| 310 | int max; |
| 311 | |
| 312 | if (depth == ext_depth(inode)) { |
| 313 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 314 | max = ext4_ext_space_root(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 315 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 316 | max = ext4_ext_space_root_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 317 | } else { |
| 318 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 319 | max = ext4_ext_space_block(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 320 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 321 | max = ext4_ext_space_block_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return max; |
| 325 | } |
| 326 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 327 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 328 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 329 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 330 | int len = ext4_ext_get_actual_len(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 331 | ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 332 | |
Vegard Nossum | f70749c | 2016-06-30 11:53:46 -0400 | [diff] [blame] | 333 | /* |
| 334 | * We allow neither: |
| 335 | * - zero length |
| 336 | * - overflow/wrap-around |
| 337 | */ |
| 338 | if (lblock + len <= lblock) |
Theodore Ts'o | 31d4f3a | 2012-03-11 23:30:16 -0400 | [diff] [blame] | 339 | return 0; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 340 | 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] | 341 | } |
| 342 | |
| 343 | static int ext4_valid_extent_idx(struct inode *inode, |
| 344 | struct ext4_extent_idx *ext_idx) |
| 345 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 346 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 347 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 348 | 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] | 349 | } |
| 350 | |
| 351 | static int ext4_valid_extent_entries(struct inode *inode, |
| 352 | struct ext4_extent_header *eh, |
| 353 | int depth) |
| 354 | { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 355 | unsigned short entries; |
| 356 | if (eh->eh_entries == 0) |
| 357 | return 1; |
| 358 | |
| 359 | entries = le16_to_cpu(eh->eh_entries); |
| 360 | |
| 361 | if (depth == 0) { |
| 362 | /* leaf entries */ |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 363 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 364 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
| 365 | ext4_fsblk_t pblock = 0; |
| 366 | ext4_lblk_t lblock = 0; |
| 367 | ext4_lblk_t prev = 0; |
| 368 | int len = 0; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 369 | while (entries) { |
| 370 | if (!ext4_valid_extent(inode, ext)) |
| 371 | return 0; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 372 | |
| 373 | /* Check for overlapping extents */ |
| 374 | lblock = le32_to_cpu(ext->ee_block); |
| 375 | len = ext4_ext_get_actual_len(ext); |
| 376 | if ((lblock <= prev) && prev) { |
| 377 | pblock = ext4_ext_pblock(ext); |
| 378 | es->s_last_error_block = cpu_to_le64(pblock); |
| 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 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 425 | if (!ext4_valid_extent_entries(inode, eh, depth)) { |
| 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 | 878520a | 2019-11-19 21:54:15 -0500 | [diff] [blame] | 443 | ext4_set_errno(inode->i_sb, -err); |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 444 | ext4_error_inode(inode, function, line, 0, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 445 | "pblk %llu bad header/extent: %s - magic %x, " |
| 446 | "entries %u, max %u(%u), depth %u(%u)", |
| 447 | (unsigned long long) pblk, error_msg, |
| 448 | le16_to_cpu(eh->eh_magic), |
| 449 | le16_to_cpu(eh->eh_entries), 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; |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 493 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 494 | bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 495 | if (unlikely(!bh)) |
| 496 | return ERR_PTR(-ENOMEM); |
| 497 | |
| 498 | if (!bh_uptodate_or_lock(bh)) { |
| 499 | trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); |
| 500 | err = bh_submit_read(bh); |
| 501 | if (err < 0) |
| 502 | goto errout; |
| 503 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 504 | if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 505 | return bh; |
Theodore Ts'o | 0a944e8 | 2019-05-22 10:27:01 -0400 | [diff] [blame] | 506 | if (!ext4_has_feature_journal(inode->i_sb) || |
| 507 | (inode->i_ino != |
| 508 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) { |
| 509 | err = __ext4_ext_check(function, line, inode, |
| 510 | ext_block_hdr(bh), depth, pblk); |
| 511 | if (err) |
| 512 | goto errout; |
| 513 | } |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 514 | set_buffer_verified(bh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 515 | /* |
| 516 | * If this is a leaf block, cache all of its entries |
| 517 | */ |
| 518 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { |
| 519 | struct ext4_extent_header *eh = ext_block_hdr(bh); |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 520 | ext4_cache_extents(inode, eh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 521 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 522 | return bh; |
| 523 | errout: |
| 524 | put_bh(bh); |
| 525 | return ERR_PTR(err); |
| 526 | |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 529 | #define read_extent_tree_block(inode, pblk, depth, flags) \ |
| 530 | __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ |
| 531 | (depth), (flags)) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 532 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 533 | /* |
| 534 | * This function is called to cache a file's extent information in the |
| 535 | * extent status tree |
| 536 | */ |
| 537 | int ext4_ext_precache(struct inode *inode) |
| 538 | { |
| 539 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 540 | struct ext4_ext_path *path = NULL; |
| 541 | struct buffer_head *bh; |
| 542 | int i = 0, depth, ret = 0; |
| 543 | |
| 544 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 545 | return 0; /* not an extent-mapped inode */ |
| 546 | |
| 547 | down_read(&ei->i_data_sem); |
| 548 | depth = ext_depth(inode); |
| 549 | |
Ritesh Harjani | 2f424a5 | 2020-02-28 14:56:55 +0530 | [diff] [blame] | 550 | /* Don't cache anything if there are no external extent blocks */ |
| 551 | if (!depth) { |
| 552 | up_read(&ei->i_data_sem); |
| 553 | return ret; |
| 554 | } |
| 555 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 556 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 557 | GFP_NOFS); |
| 558 | if (path == NULL) { |
| 559 | up_read(&ei->i_data_sem); |
| 560 | return -ENOMEM; |
| 561 | } |
| 562 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 563 | path[0].p_hdr = ext_inode_hdr(inode); |
| 564 | ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); |
| 565 | if (ret) |
| 566 | goto out; |
| 567 | path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); |
| 568 | while (i >= 0) { |
| 569 | /* |
| 570 | * If this is a leaf block or we've reached the end of |
| 571 | * the index block, go up |
| 572 | */ |
| 573 | if ((i == depth) || |
| 574 | path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { |
| 575 | brelse(path[i].p_bh); |
| 576 | path[i].p_bh = NULL; |
| 577 | i--; |
| 578 | continue; |
| 579 | } |
| 580 | bh = read_extent_tree_block(inode, |
| 581 | ext4_idx_pblock(path[i].p_idx++), |
| 582 | depth - i - 1, |
| 583 | EXT4_EX_FORCE_CACHE); |
| 584 | if (IS_ERR(bh)) { |
| 585 | ret = PTR_ERR(bh); |
| 586 | break; |
| 587 | } |
| 588 | i++; |
| 589 | path[i].p_bh = bh; |
| 590 | path[i].p_hdr = ext_block_hdr(bh); |
| 591 | path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); |
| 592 | } |
| 593 | ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); |
| 594 | out: |
| 595 | up_read(&ei->i_data_sem); |
| 596 | ext4_ext_drop_refs(path); |
| 597 | kfree(path); |
| 598 | return ret; |
| 599 | } |
| 600 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 601 | #ifdef EXT_DEBUG |
| 602 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 603 | { |
| 604 | int k, l = path->p_depth; |
| 605 | |
| 606 | ext_debug("path:"); |
| 607 | for (k = 0; k <= l; k++, path++) { |
| 608 | if (path->p_idx) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 609 | ext_debug(" %d->%llu", |
| 610 | le32_to_cpu(path->p_idx->ei_block), |
| 611 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 612 | } else if (path->p_ext) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 613 | ext_debug(" %d:[%d]%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 614 | le32_to_cpu(path->p_ext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 615 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 616 | ext4_ext_get_actual_len(path->p_ext), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 617 | ext4_ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 618 | } else |
| 619 | ext_debug(" []"); |
| 620 | } |
| 621 | ext_debug("\n"); |
| 622 | } |
| 623 | |
| 624 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 625 | { |
| 626 | int depth = ext_depth(inode); |
| 627 | struct ext4_extent_header *eh; |
| 628 | struct ext4_extent *ex; |
| 629 | int i; |
| 630 | |
| 631 | if (!path) |
| 632 | return; |
| 633 | |
| 634 | eh = path[depth].p_hdr; |
| 635 | ex = EXT_FIRST_EXTENT(eh); |
| 636 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 637 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
| 638 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 639 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 640 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 641 | ext4_ext_is_unwritten(ex), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 642 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 643 | } |
| 644 | ext_debug("\n"); |
| 645 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 646 | |
| 647 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 648 | ext4_fsblk_t newblock, int level) |
| 649 | { |
| 650 | int depth = ext_depth(inode); |
| 651 | struct ext4_extent *ex; |
| 652 | |
| 653 | if (depth != level) { |
| 654 | struct ext4_extent_idx *idx; |
| 655 | idx = path[level].p_idx; |
| 656 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
| 657 | ext_debug("%d: move %d:%llu in new index %llu\n", level, |
| 658 | le32_to_cpu(idx->ei_block), |
| 659 | ext4_idx_pblock(idx), |
| 660 | newblock); |
| 661 | idx++; |
| 662 | } |
| 663 | |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | ex = path[depth].p_ext; |
| 668 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 669 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
| 670 | le32_to_cpu(ex->ee_block), |
| 671 | ext4_ext_pblock(ex), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 672 | ext4_ext_is_unwritten(ex), |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 673 | ext4_ext_get_actual_len(ex), |
| 674 | newblock); |
| 675 | ex++; |
| 676 | } |
| 677 | } |
| 678 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 679 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 680 | #define ext4_ext_show_path(inode, path) |
| 681 | #define ext4_ext_show_leaf(inode, path) |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 682 | #define ext4_ext_show_move(inode, path, newblock, level) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 683 | #endif |
| 684 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 685 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 686 | { |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 687 | int depth, i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 688 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 689 | if (!path) |
| 690 | return; |
| 691 | depth = path->p_depth; |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 692 | for (i = 0; i <= depth; i++, path++) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 693 | if (path->p_bh) { |
| 694 | brelse(path->p_bh); |
| 695 | path->p_bh = NULL; |
| 696 | } |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 697 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 701 | * ext4_ext_binsearch_idx: |
| 702 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 703 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 704 | */ |
| 705 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 706 | ext4_ext_binsearch_idx(struct inode *inode, |
| 707 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 708 | { |
| 709 | struct ext4_extent_header *eh = path->p_hdr; |
| 710 | struct ext4_extent_idx *r, *l, *m; |
| 711 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 712 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 713 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 714 | |
| 715 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 716 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 717 | while (l <= r) { |
| 718 | m = l + (r - l) / 2; |
| 719 | if (block < le32_to_cpu(m->ei_block)) |
| 720 | r = m - 1; |
| 721 | else |
| 722 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 723 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 724 | m, le32_to_cpu(m->ei_block), |
| 725 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | path->p_idx = l - 1; |
Zheng Liu | 4a3c3a5 | 2012-05-28 17:55:16 -0400 | [diff] [blame] | 729 | ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 730 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 731 | |
| 732 | #ifdef CHECK_BINSEARCH |
| 733 | { |
| 734 | struct ext4_extent_idx *chix, *ix; |
| 735 | int k; |
| 736 | |
| 737 | chix = ix = EXT_FIRST_INDEX(eh); |
| 738 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 739 | if (k != 0 && le32_to_cpu(ix->ei_block) <= |
| 740 | le32_to_cpu(ix[-1].ei_block)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 741 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 742 | "first=0x%p\n", k, |
| 743 | ix, EXT_FIRST_INDEX(eh)); |
| 744 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 745 | le32_to_cpu(ix->ei_block), |
| 746 | le32_to_cpu(ix[-1].ei_block)); |
| 747 | } |
| 748 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 749 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 750 | if (block < le32_to_cpu(ix->ei_block)) |
| 751 | break; |
| 752 | chix = ix; |
| 753 | } |
| 754 | BUG_ON(chix != path->p_idx); |
| 755 | } |
| 756 | #endif |
| 757 | |
| 758 | } |
| 759 | |
| 760 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 761 | * ext4_ext_binsearch: |
| 762 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 763 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 764 | */ |
| 765 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 766 | ext4_ext_binsearch(struct inode *inode, |
| 767 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 768 | { |
| 769 | struct ext4_extent_header *eh = path->p_hdr; |
| 770 | struct ext4_extent *r, *l, *m; |
| 771 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 772 | if (eh->eh_entries == 0) { |
| 773 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 774 | * this leaf is empty: |
| 775 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 776 | */ |
| 777 | return; |
| 778 | } |
| 779 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 780 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 781 | |
| 782 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 783 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 784 | |
| 785 | while (l <= r) { |
| 786 | m = l + (r - l) / 2; |
| 787 | if (block < le32_to_cpu(m->ee_block)) |
| 788 | r = m - 1; |
| 789 | else |
| 790 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 791 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 792 | m, le32_to_cpu(m->ee_block), |
| 793 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | path->p_ext = l - 1; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 797 | ext_debug(" -> %d:%llu:[%d]%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 798 | le32_to_cpu(path->p_ext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 799 | ext4_ext_pblock(path->p_ext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 800 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 801 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 802 | |
| 803 | #ifdef CHECK_BINSEARCH |
| 804 | { |
| 805 | struct ext4_extent *chex, *ex; |
| 806 | int k; |
| 807 | |
| 808 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 809 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 810 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 811 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 812 | if (block < le32_to_cpu(ex->ee_block)) |
| 813 | break; |
| 814 | chex = ex; |
| 815 | } |
| 816 | BUG_ON(chex != path->p_ext); |
| 817 | } |
| 818 | #endif |
| 819 | |
| 820 | } |
| 821 | |
| 822 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 823 | { |
| 824 | struct ext4_extent_header *eh; |
| 825 | |
| 826 | eh = ext_inode_hdr(inode); |
| 827 | eh->eh_depth = 0; |
| 828 | eh->eh_entries = 0; |
| 829 | eh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 830 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 831 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | struct ext4_ext_path * |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 836 | ext4_find_extent(struct inode *inode, ext4_lblk_t block, |
| 837 | struct ext4_ext_path **orig_path, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 838 | { |
| 839 | struct ext4_extent_header *eh; |
| 840 | struct buffer_head *bh; |
Theodore Ts'o | 705912c | 2014-09-01 14:34:09 -0400 | [diff] [blame] | 841 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
| 842 | short int depth, i, ppos = 0; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 843 | int ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 844 | |
| 845 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 846 | depth = ext_depth(inode); |
Theodore Ts'o | bc890a6 | 2018-06-14 12:55:10 -0400 | [diff] [blame] | 847 | if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { |
| 848 | EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", |
| 849 | depth); |
| 850 | ret = -EFSCORRUPTED; |
| 851 | goto err; |
| 852 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 853 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 854 | if (path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 855 | ext4_ext_drop_refs(path); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 856 | if (depth > path[0].p_maxdepth) { |
| 857 | kfree(path); |
| 858 | *orig_path = path = NULL; |
| 859 | } |
| 860 | } |
| 861 | if (!path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 862 | /* account possible depth increase */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 863 | path = kcalloc(depth + 2, sizeof(struct ext4_ext_path), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 864 | GFP_NOFS); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 865 | if (unlikely(!path)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 866 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 867 | path[0].p_maxdepth = depth + 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 868 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 869 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 870 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 871 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 872 | i = depth; |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 873 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) |
| 874 | ext4_cache_extents(inode, eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 875 | /* walk through the tree */ |
| 876 | while (i) { |
| 877 | ext_debug("depth %d: num %d, max %d\n", |
| 878 | 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] | 879 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 880 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 881 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 882 | path[ppos].p_depth = i; |
| 883 | path[ppos].p_ext = NULL; |
| 884 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 885 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
| 886 | flags); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 887 | if (IS_ERR(bh)) { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 888 | ret = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 889 | goto err; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 890 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 891 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 892 | eh = ext_block_hdr(bh); |
| 893 | ppos++; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 894 | path[ppos].p_bh = bh; |
| 895 | path[ppos].p_hdr = eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 899 | path[ppos].p_ext = NULL; |
| 900 | path[ppos].p_idx = NULL; |
| 901 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 902 | /* find extent */ |
| 903 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 904 | /* if not an empty leaf */ |
| 905 | if (path[ppos].p_ext) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 906 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 907 | |
| 908 | ext4_ext_show_path(inode, path); |
| 909 | |
| 910 | return path; |
| 911 | |
| 912 | err: |
| 913 | ext4_ext_drop_refs(path); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 914 | kfree(path); |
| 915 | if (orig_path) |
| 916 | *orig_path = NULL; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 917 | return ERR_PTR(ret); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 921 | * ext4_ext_insert_index: |
| 922 | * insert new index [@logical;@ptr] into the block at @curp; |
| 923 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 924 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 925 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 926 | struct ext4_ext_path *curp, |
| 927 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 928 | { |
| 929 | struct ext4_extent_idx *ix; |
| 930 | int len, err; |
| 931 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 932 | err = ext4_ext_get_access(handle, inode, curp); |
| 933 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 934 | return err; |
| 935 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 936 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 937 | EXT4_ERROR_INODE(inode, |
| 938 | "logical %d == ei_block %d!", |
| 939 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 940 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 941 | } |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 942 | |
| 943 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 944 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 945 | EXT4_ERROR_INODE(inode, |
| 946 | "eh_entries %d >= eh_max %d!", |
| 947 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 948 | le16_to_cpu(curp->p_hdr->eh_max)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 949 | return -EFSCORRUPTED; |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 950 | } |
| 951 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 952 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 953 | /* insert after */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 954 | ext_debug("insert new index %d after: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 955 | ix = curp->p_idx + 1; |
| 956 | } else { |
| 957 | /* insert before */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 958 | ext_debug("insert new index %d before: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 959 | ix = curp->p_idx; |
| 960 | } |
| 961 | |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 962 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
| 963 | BUG_ON(len < 0); |
| 964 | if (len > 0) { |
| 965 | ext_debug("insert new index %d: " |
| 966 | "move %d indices from 0x%p to 0x%p\n", |
| 967 | logical, len, ix, ix + 1); |
| 968 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
| 969 | } |
| 970 | |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 971 | if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { |
| 972 | EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 973 | return -EFSCORRUPTED; |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 974 | } |
| 975 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 976 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 977 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 978 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 979 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 980 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 981 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 982 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 983 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 984 | |
| 985 | err = ext4_ext_dirty(handle, inode, curp); |
| 986 | ext4_std_error(inode->i_sb, err); |
| 987 | |
| 988 | return err; |
| 989 | } |
| 990 | |
| 991 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 992 | * ext4_ext_split: |
| 993 | * inserts new subtree into the path, using free index entry |
| 994 | * at depth @at: |
| 995 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 996 | * - makes decision where to split |
| 997 | * - moves remaining extents and index entries (right to the split point) |
| 998 | * into the newly allocated blocks |
| 999 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1000 | */ |
| 1001 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1002 | unsigned int flags, |
| 1003 | struct ext4_ext_path *path, |
| 1004 | struct ext4_extent *newext, int at) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1005 | { |
| 1006 | struct buffer_head *bh = NULL; |
| 1007 | int depth = ext_depth(inode); |
| 1008 | struct ext4_extent_header *neh; |
| 1009 | struct ext4_extent_idx *fidx; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1010 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1011 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1012 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1013 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1014 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1015 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1016 | |
| 1017 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1018 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1019 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1020 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1021 | * border from split point */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1022 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 1023 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1024 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1025 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1026 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 1027 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1028 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1029 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1030 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1031 | } else { |
| 1032 | border = newext->ee_block; |
| 1033 | ext_debug("leaf will be added." |
| 1034 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1035 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1039 | * If error occurs, then we break processing |
| 1040 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1041 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1042 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1043 | */ |
| 1044 | |
| 1045 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1046 | * Get array to track all allocated blocks. |
| 1047 | * We need this to handle errors and free blocks |
| 1048 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1049 | */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1050 | ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1051 | if (!ablocks) |
| 1052 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* allocate all needed blocks */ |
| 1055 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 1056 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1057 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1058 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1059 | if (newblock == 0) |
| 1060 | goto cleanup; |
| 1061 | ablocks[a] = newblock; |
| 1062 | } |
| 1063 | |
| 1064 | /* initialize new leaf */ |
| 1065 | newblock = ablocks[--a]; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1066 | if (unlikely(newblock == 0)) { |
| 1067 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1068 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1069 | goto cleanup; |
| 1070 | } |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1071 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1072 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1073 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1074 | goto cleanup; |
| 1075 | } |
| 1076 | lock_buffer(bh); |
| 1077 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1078 | err = ext4_journal_get_create_access(handle, bh); |
| 1079 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1080 | goto cleanup; |
| 1081 | |
| 1082 | neh = ext_block_hdr(bh); |
| 1083 | neh->eh_entries = 0; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1084 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1085 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1086 | neh->eh_depth = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1087 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1088 | /* move remainder of path[depth] to the new leaf */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1089 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 1090 | path[depth].p_hdr->eh_max)) { |
| 1091 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 1092 | path[depth].p_hdr->eh_entries, |
| 1093 | path[depth].p_hdr->eh_max); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1094 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1095 | goto cleanup; |
| 1096 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1097 | /* start copy from next extent */ |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1098 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 1099 | ext4_ext_show_move(inode, path, newblock, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1100 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1101 | struct ext4_extent *ex; |
| 1102 | ex = EXT_FIRST_EXTENT(neh); |
| 1103 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1104 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1107 | /* zero out unused area in the extent block */ |
| 1108 | ext_size = sizeof(struct ext4_extent_header) + |
| 1109 | sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); |
| 1110 | 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] | 1111 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1112 | set_buffer_uptodate(bh); |
| 1113 | unlock_buffer(bh); |
| 1114 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1115 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1116 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1117 | goto cleanup; |
| 1118 | brelse(bh); |
| 1119 | bh = NULL; |
| 1120 | |
| 1121 | /* correct old leaf */ |
| 1122 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1123 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1124 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1125 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1126 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1127 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1128 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1129 | goto cleanup; |
| 1130 | |
| 1131 | } |
| 1132 | |
| 1133 | /* create intermediate indexes */ |
| 1134 | k = depth - at - 1; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1135 | if (unlikely(k < 0)) { |
| 1136 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1137 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1138 | goto cleanup; |
| 1139 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1140 | if (k) |
| 1141 | ext_debug("create %d intermediate indices\n", k); |
| 1142 | /* insert new index into current index block */ |
| 1143 | /* current depth stored in i var */ |
| 1144 | i = depth - 1; |
| 1145 | while (k--) { |
| 1146 | oldblock = newblock; |
| 1147 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1148 | bh = sb_getblk(inode->i_sb, newblock); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1149 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1150 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1151 | goto cleanup; |
| 1152 | } |
| 1153 | lock_buffer(bh); |
| 1154 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1155 | err = ext4_journal_get_create_access(handle, bh); |
| 1156 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1157 | goto cleanup; |
| 1158 | |
| 1159 | neh = ext_block_hdr(bh); |
| 1160 | neh->eh_entries = cpu_to_le16(1); |
| 1161 | neh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1162 | 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] | 1163 | neh->eh_depth = cpu_to_le16(depth - i); |
| 1164 | fidx = EXT_FIRST_INDEX(neh); |
| 1165 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1166 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1167 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1168 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 1169 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1170 | |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1171 | /* move remainder of path[i] to the new index block */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1172 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 1173 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 1174 | EXT4_ERROR_INODE(inode, |
| 1175 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 1176 | le32_to_cpu(path[i].p_ext->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1177 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1178 | goto cleanup; |
| 1179 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1180 | /* start copy indexes */ |
| 1181 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
| 1182 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 1183 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 1184 | ext4_ext_show_move(inode, path, newblock, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1185 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1186 | memmove(++fidx, path[i].p_idx, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1187 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1188 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1189 | } |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1190 | /* zero out unused area in the extent block */ |
| 1191 | ext_size = sizeof(struct ext4_extent_header) + |
| 1192 | (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); |
| 1193 | memset(bh->b_data + ext_size, 0, |
| 1194 | inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1195 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1196 | set_buffer_uptodate(bh); |
| 1197 | unlock_buffer(bh); |
| 1198 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1199 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1200 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1201 | goto cleanup; |
| 1202 | brelse(bh); |
| 1203 | bh = NULL; |
| 1204 | |
| 1205 | /* correct old index */ |
| 1206 | if (m) { |
| 1207 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1208 | if (err) |
| 1209 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1210 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1211 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1212 | if (err) |
| 1213 | goto cleanup; |
| 1214 | } |
| 1215 | |
| 1216 | i--; |
| 1217 | } |
| 1218 | |
| 1219 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1220 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1221 | le32_to_cpu(border), newblock); |
| 1222 | |
| 1223 | cleanup: |
| 1224 | if (bh) { |
| 1225 | if (buffer_locked(bh)) |
| 1226 | unlock_buffer(bh); |
| 1227 | brelse(bh); |
| 1228 | } |
| 1229 | |
| 1230 | if (err) { |
| 1231 | /* free all allocated blocks in error case */ |
| 1232 | for (i = 0; i < depth; i++) { |
| 1233 | if (!ablocks[i]) |
| 1234 | continue; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1235 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1236 | EXT4_FREE_BLOCKS_METADATA); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | kfree(ablocks); |
| 1240 | |
| 1241 | return err; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1245 | * ext4_ext_grow_indepth: |
| 1246 | * implements tree growing procedure: |
| 1247 | * - allocates new block |
| 1248 | * - moves top-level data (index block or leaf) into the new block |
| 1249 | * - initializes new top-level, creating index that points to the |
| 1250 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1251 | */ |
| 1252 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1253 | unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1254 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1255 | struct ext4_extent_header *neh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1256 | struct buffer_head *bh; |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1257 | ext4_fsblk_t newblock, goal = 0; |
| 1258 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1259 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1260 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1261 | |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1262 | /* Try to prepend new index to old one */ |
| 1263 | if (ext_depth(inode)) |
| 1264 | goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); |
| 1265 | if (goal > le32_to_cpu(es->s_first_data_block)) { |
| 1266 | flags |= EXT4_MB_HINT_TRY_GOAL; |
| 1267 | goal--; |
| 1268 | } else |
| 1269 | goal = ext4_inode_to_goal_block(inode); |
| 1270 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 1271 | NULL, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1272 | if (newblock == 0) |
| 1273 | return err; |
| 1274 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1275 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1276 | if (unlikely(!bh)) |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1277 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1278 | lock_buffer(bh); |
| 1279 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1280 | err = ext4_journal_get_create_access(handle, bh); |
| 1281 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1282 | unlock_buffer(bh); |
| 1283 | goto out; |
| 1284 | } |
| 1285 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1286 | ext_size = sizeof(EXT4_I(inode)->i_data); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1287 | /* move top-level index/leaf into new block */ |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1288 | memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); |
| 1289 | /* zero out unused area in the extent block */ |
| 1290 | 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] | 1291 | |
| 1292 | /* set size of new block */ |
| 1293 | neh = ext_block_hdr(bh); |
| 1294 | /* old root could have indexes or leaves |
| 1295 | * so calculate e_max right way */ |
| 1296 | if (ext_depth(inode)) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1297 | 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] | 1298 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1299 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1300 | neh->eh_magic = EXT4_EXT_MAGIC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1301 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1302 | set_buffer_uptodate(bh); |
| 1303 | unlock_buffer(bh); |
| 1304 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1305 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1306 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1307 | goto out; |
| 1308 | |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1309 | /* Update top-level index: num,max,pointer */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1310 | neh = ext_inode_hdr(inode); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1311 | neh->eh_entries = cpu_to_le16(1); |
| 1312 | ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); |
| 1313 | if (neh->eh_depth == 0) { |
| 1314 | /* Root extent block becomes index block */ |
| 1315 | neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
| 1316 | EXT_FIRST_INDEX(neh)->ei_block = |
| 1317 | EXT_FIRST_EXTENT(neh)->ee_block; |
| 1318 | } |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1319 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1320 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1321 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1322 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1323 | |
Wei Yongjun | ba39ebb | 2012-09-27 09:37:53 -0400 | [diff] [blame] | 1324 | le16_add_cpu(&neh->eh_depth, 1); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1325 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1326 | out: |
| 1327 | brelse(bh); |
| 1328 | |
| 1329 | return err; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1333 | * ext4_ext_create_new_leaf: |
| 1334 | * finds empty index and adds new leaf. |
| 1335 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1336 | */ |
| 1337 | 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] | 1338 | unsigned int mb_flags, |
| 1339 | unsigned int gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1340 | struct ext4_ext_path **ppath, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1341 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1342 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1343 | struct ext4_ext_path *path = *ppath; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1344 | struct ext4_ext_path *curp; |
| 1345 | int depth, i, err = 0; |
| 1346 | |
| 1347 | repeat: |
| 1348 | i = depth = ext_depth(inode); |
| 1349 | |
| 1350 | /* walk up to the tree and look for free index entry */ |
| 1351 | curp = path + depth; |
| 1352 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1353 | i--; |
| 1354 | curp--; |
| 1355 | } |
| 1356 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1357 | /* we use already allocated block for index block, |
| 1358 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1359 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1360 | /* if we found index with free entry, then use that |
| 1361 | * entry: create all needed subtree and add new leaf */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1362 | err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1363 | if (err) |
| 1364 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1367 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1368 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1369 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1370 | if (IS_ERR(path)) |
| 1371 | err = PTR_ERR(path); |
| 1372 | } else { |
| 1373 | /* tree is full, time to grow in depth */ |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1374 | err = ext4_ext_grow_indepth(handle, inode, mb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1375 | if (err) |
| 1376 | goto out; |
| 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 | goto out; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1388 | * only first (depth 0 -> 1) produces free space; |
| 1389 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1390 | */ |
| 1391 | depth = ext_depth(inode); |
| 1392 | 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] | 1393 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1394 | goto repeat; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | out: |
| 1399 | return err; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1403 | * search the closest allocated block to the left for *logical |
| 1404 | * and returns it at @logical + it's physical address at @phys |
| 1405 | * if *logical is the smallest allocated block, the function |
| 1406 | * returns 0 at @phys |
| 1407 | * return value contains 0 (success) or error code |
| 1408 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1409 | static int ext4_ext_search_left(struct inode *inode, |
| 1410 | struct ext4_ext_path *path, |
| 1411 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1412 | { |
| 1413 | struct ext4_extent_idx *ix; |
| 1414 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1415 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1416 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1417 | if (unlikely(path == NULL)) { |
| 1418 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1419 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1420 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1421 | depth = path->p_depth; |
| 1422 | *phys = 0; |
| 1423 | |
| 1424 | if (depth == 0 && path->p_ext == NULL) |
| 1425 | return 0; |
| 1426 | |
| 1427 | /* usually extent in the path covers blocks smaller |
| 1428 | * then *logical, but it can be that extent is the |
| 1429 | * first one in the file */ |
| 1430 | |
| 1431 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1432 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1433 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1434 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1435 | EXT4_ERROR_INODE(inode, |
| 1436 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1437 | *logical, le32_to_cpu(ex->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1438 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1439 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1440 | while (--depth >= 0) { |
| 1441 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1442 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1443 | EXT4_ERROR_INODE(inode, |
| 1444 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1445 | ix != NULL ? le32_to_cpu(ix->ei_block) : 0, |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1446 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1447 | 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] | 1448 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1449 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1450 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1451 | } |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1455 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1456 | EXT4_ERROR_INODE(inode, |
| 1457 | "logical %d < ee_block %d + ee_len %d!", |
| 1458 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1459 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1460 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1461 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1462 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1463 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * search the closest allocated block to the right for *logical |
| 1469 | * and returns it at @logical + it's physical address at @phys |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 1470 | * if *logical is the largest allocated block, the function |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1471 | * returns 0 at @phys |
| 1472 | * return value contains 0 (success) or error code |
| 1473 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1474 | static int ext4_ext_search_right(struct inode *inode, |
| 1475 | struct ext4_ext_path *path, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1476 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1477 | struct ext4_extent **ret_ex) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1478 | { |
| 1479 | struct buffer_head *bh = NULL; |
| 1480 | struct ext4_extent_header *eh; |
| 1481 | struct ext4_extent_idx *ix; |
| 1482 | struct ext4_extent *ex; |
| 1483 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1484 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1485 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1486 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1487 | if (unlikely(path == NULL)) { |
| 1488 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1489 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1490 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1491 | depth = path->p_depth; |
| 1492 | *phys = 0; |
| 1493 | |
| 1494 | if (depth == 0 && path->p_ext == NULL) |
| 1495 | return 0; |
| 1496 | |
| 1497 | /* usually extent in the path covers blocks smaller |
| 1498 | * then *logical, but it can be that extent is the |
| 1499 | * first one in the file */ |
| 1500 | |
| 1501 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1502 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1503 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1504 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1505 | EXT4_ERROR_INODE(inode, |
| 1506 | "first_extent(path[%d].p_hdr) != ex", |
| 1507 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1508 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1509 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1510 | while (--depth >= 0) { |
| 1511 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1512 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1513 | EXT4_ERROR_INODE(inode, |
| 1514 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1515 | *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1516 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1517 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1518 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1519 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1520 | } |
| 1521 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1522 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1523 | EXT4_ERROR_INODE(inode, |
| 1524 | "logical %d < ee_block %d + ee_len %d!", |
| 1525 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1526 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1527 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1528 | |
| 1529 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1530 | /* next allocated block in this leaf */ |
| 1531 | ex++; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1532 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /* go up and search for index to the right */ |
| 1536 | while (--depth >= 0) { |
| 1537 | ix = path[depth].p_idx; |
| 1538 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1539 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1540 | } |
| 1541 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1542 | /* we've gone up to the root and found no index to the right */ |
| 1543 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1544 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1545 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1546 | /* we've found index to the right, let's |
| 1547 | * follow it and find the closest allocated |
| 1548 | * block to the right */ |
| 1549 | ix++; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1550 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1551 | while (++depth < path->p_depth) { |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1552 | /* subtract from p_depth to get proper eh_depth */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1553 | bh = read_extent_tree_block(inode, block, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1554 | path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1555 | if (IS_ERR(bh)) |
| 1556 | return PTR_ERR(bh); |
| 1557 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1558 | ix = EXT_FIRST_INDEX(eh); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1559 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1560 | put_bh(bh); |
| 1561 | } |
| 1562 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1563 | 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] | 1564 | if (IS_ERR(bh)) |
| 1565 | return PTR_ERR(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1566 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1567 | ex = EXT_FIRST_EXTENT(eh); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1568 | found_extent: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1569 | *logical = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1570 | *phys = ext4_ext_pblock(ex); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1571 | *ret_ex = ex; |
| 1572 | if (bh) |
| 1573 | put_bh(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1574 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1578 | * ext4_ext_next_allocated_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1579 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1580 | * NOTE: it considers block number from index entry as |
| 1581 | * allocated block. Thus, index entries have to be consistent |
| 1582 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1583 | */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 1584 | ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1585 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1586 | { |
| 1587 | int depth; |
| 1588 | |
| 1589 | BUG_ON(path == NULL); |
| 1590 | depth = path->p_depth; |
| 1591 | |
| 1592 | if (depth == 0 && path->p_ext == NULL) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1593 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1594 | |
| 1595 | while (depth >= 0) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1596 | struct ext4_ext_path *p = &path[depth]; |
| 1597 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1598 | if (depth == path->p_depth) { |
| 1599 | /* leaf */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1600 | if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) |
| 1601 | return le32_to_cpu(p->p_ext[1].ee_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1602 | } else { |
| 1603 | /* index */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1604 | if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) |
| 1605 | return le32_to_cpu(p->p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1606 | } |
| 1607 | depth--; |
| 1608 | } |
| 1609 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1610 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1614 | * ext4_ext_next_leaf_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1615 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1616 | */ |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1617 | 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] | 1618 | { |
| 1619 | int depth; |
| 1620 | |
| 1621 | BUG_ON(path == NULL); |
| 1622 | depth = path->p_depth; |
| 1623 | |
| 1624 | /* zero-tree has no leaf blocks at all */ |
| 1625 | if (depth == 0) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1626 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1627 | |
| 1628 | /* go to index block */ |
| 1629 | depth--; |
| 1630 | |
| 1631 | while (depth >= 0) { |
| 1632 | if (path[depth].p_idx != |
| 1633 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1634 | return (ext4_lblk_t) |
| 1635 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1636 | depth--; |
| 1637 | } |
| 1638 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1639 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1643 | * ext4_ext_correct_indexes: |
| 1644 | * if leaf gets modified and modified extent is first in the leaf, |
| 1645 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1646 | * TODO: do we need to correct tree in all cases? |
| 1647 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1648 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1649 | struct ext4_ext_path *path) |
| 1650 | { |
| 1651 | struct ext4_extent_header *eh; |
| 1652 | int depth = ext_depth(inode); |
| 1653 | struct ext4_extent *ex; |
| 1654 | __le32 border; |
| 1655 | int k, err = 0; |
| 1656 | |
| 1657 | eh = path[depth].p_hdr; |
| 1658 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1659 | |
| 1660 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1661 | EXT4_ERROR_INODE(inode, |
| 1662 | "ex %p == NULL or eh %p == NULL", ex, eh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1663 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1664 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1665 | |
| 1666 | if (depth == 0) { |
| 1667 | /* there is no tree at all */ |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1672 | /* we correct tree if first leaf got modified only */ |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1677 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1678 | */ |
| 1679 | k = depth - 1; |
| 1680 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1681 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1682 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1683 | return err; |
| 1684 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1685 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1686 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1687 | return err; |
| 1688 | |
| 1689 | while (k--) { |
| 1690 | /* change all left-side indexes */ |
| 1691 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1692 | break; |
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 | break; |
| 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 | break; |
| 1700 | } |
| 1701 | |
| 1702 | return err; |
| 1703 | } |
| 1704 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 1705 | static int ext4_can_extents_be_merged(struct inode *inode, |
| 1706 | struct ext4_extent *ex1, |
| 1707 | struct ext4_extent *ex2) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1708 | { |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1709 | unsigned short ext1_ee_len, ext2_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1710 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1711 | if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1712 | return 0; |
| 1713 | |
| 1714 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1715 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1716 | |
| 1717 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1718 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1719 | return 0; |
| 1720 | |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1721 | if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1722 | return 0; |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1723 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1724 | if (ext4_ext_is_unwritten(ex1) && |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1725 | ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) |
Darrick J. Wong | a9b8241 | 2014-02-20 21:17:35 -0500 | [diff] [blame] | 1726 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1727 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1728 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1729 | return 0; |
| 1730 | #endif |
| 1731 | |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1732 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1733 | return 1; |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1738 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1739 | * It always tries to merge towards right. If you want to merge towards |
| 1740 | * left, pass "ex - 1" as argument instead of "ex". |
| 1741 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1742 | * 1 if they got merged. |
| 1743 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1744 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1745 | struct ext4_ext_path *path, |
| 1746 | struct ext4_extent *ex) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1747 | { |
| 1748 | struct ext4_extent_header *eh; |
| 1749 | unsigned int depth, len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1750 | int merge_done = 0, unwritten; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1751 | |
| 1752 | depth = ext_depth(inode); |
| 1753 | BUG_ON(path[depth].p_hdr == NULL); |
| 1754 | eh = path[depth].p_hdr; |
| 1755 | |
| 1756 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1757 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1758 | break; |
| 1759 | /* merge with next extent! */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1760 | unwritten = ext4_ext_is_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1761 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1762 | + ext4_ext_get_actual_len(ex + 1)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1763 | if (unwritten) |
| 1764 | ext4_ext_mark_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1765 | |
| 1766 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1767 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1768 | * sizeof(struct ext4_extent); |
| 1769 | memmove(ex + 1, ex + 2, len); |
| 1770 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1771 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1772 | merge_done = 1; |
| 1773 | WARN_ON(eh->eh_entries == 0); |
| 1774 | if (!eh->eh_entries) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1775 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | return merge_done; |
| 1779 | } |
| 1780 | |
| 1781 | /* |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1782 | * This function does a very simple check to see if we can collapse |
| 1783 | * an extent tree with a single extent tree leaf block into the inode. |
| 1784 | */ |
| 1785 | static void ext4_ext_try_to_merge_up(handle_t *handle, |
| 1786 | struct inode *inode, |
| 1787 | struct ext4_ext_path *path) |
| 1788 | { |
| 1789 | size_t s; |
| 1790 | unsigned max_root = ext4_ext_space_root(inode, 0); |
| 1791 | ext4_fsblk_t blk; |
| 1792 | |
| 1793 | if ((path[0].p_depth != 1) || |
| 1794 | (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || |
| 1795 | (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) |
| 1796 | return; |
| 1797 | |
| 1798 | /* |
| 1799 | * We need to modify the block allocation bitmap and the block |
| 1800 | * group descriptor to release the extent tree block. If we |
| 1801 | * can't get the journal credits, give up. |
| 1802 | */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 1803 | if (ext4_journal_extend(handle, 2, |
| 1804 | ext4_free_metadata_revoke_credits(inode->i_sb, 1))) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1805 | return; |
| 1806 | |
| 1807 | /* |
| 1808 | * Copy the extent data up to the inode |
| 1809 | */ |
| 1810 | blk = ext4_idx_pblock(path[0].p_idx); |
| 1811 | s = le16_to_cpu(path[1].p_hdr->eh_entries) * |
| 1812 | sizeof(struct ext4_extent_idx); |
| 1813 | s += sizeof(struct ext4_extent_header); |
| 1814 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 1815 | path[1].p_maxdepth = path[0].p_maxdepth; |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1816 | memcpy(path[0].p_hdr, path[1].p_hdr, s); |
| 1817 | path[0].p_depth = 0; |
| 1818 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + |
| 1819 | (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); |
| 1820 | path[0].p_hdr->eh_max = cpu_to_le16(max_root); |
| 1821 | |
| 1822 | brelse(path[1].p_bh); |
| 1823 | ext4_free_blocks(handle, inode, NULL, blk, 1, |
Theodore Ts'o | 71d4f7d | 2014-07-15 06:02:38 -0400 | [diff] [blame] | 1824 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | /* |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1828 | * This function tries to merge the @ex extent to neighbours in the tree, then |
| 1829 | * tries to collapse the extent tree into the inode. |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1830 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1831 | static void ext4_ext_try_to_merge(handle_t *handle, |
| 1832 | struct inode *inode, |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1833 | struct ext4_ext_path *path, |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1834 | struct ext4_extent *ex) |
| 1835 | { |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1836 | struct ext4_extent_header *eh; |
| 1837 | unsigned int depth; |
| 1838 | int merge_done = 0; |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1839 | |
| 1840 | depth = ext_depth(inode); |
| 1841 | BUG_ON(path[depth].p_hdr == NULL); |
| 1842 | eh = path[depth].p_hdr; |
| 1843 | |
| 1844 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1845 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1846 | |
| 1847 | if (!merge_done) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1848 | (void) ext4_ext_try_to_merge_right(inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1849 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1850 | ext4_ext_try_to_merge_up(handle, inode, path); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1854 | * check if a portion of the "newext" extent overlaps with an |
| 1855 | * existing extent. |
| 1856 | * |
| 1857 | * If there is an overlap discovered, it updates the length of the newext |
| 1858 | * such that there will be no overlap, and then returns 1. |
| 1859 | * If there is no overlap found, it returns 0. |
| 1860 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1861 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1862 | struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1863 | struct ext4_extent *newext, |
| 1864 | struct ext4_ext_path *path) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1865 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1866 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1867 | unsigned int depth, len1; |
| 1868 | unsigned int ret = 0; |
| 1869 | |
| 1870 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1871 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1872 | depth = ext_depth(inode); |
| 1873 | if (!path[depth].p_ext) |
| 1874 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1875 | 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] | 1876 | |
| 1877 | /* |
| 1878 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1879 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1880 | */ |
| 1881 | if (b2 < b1) { |
| 1882 | b2 = ext4_ext_next_allocated_block(path); |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1883 | if (b2 == EXT_MAX_BLOCKS) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1884 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1885 | b2 = EXT4_LBLK_CMASK(sbi, b2); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1886 | } |
| 1887 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1888 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1889 | if (b1 + len1 < b1) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1890 | len1 = EXT_MAX_BLOCKS - b1; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1891 | newext->ee_len = cpu_to_le16(len1); |
| 1892 | ret = 1; |
| 1893 | } |
| 1894 | |
| 1895 | /* check for overlap */ |
| 1896 | if (b1 + len1 > b2) { |
| 1897 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1898 | ret = 1; |
| 1899 | } |
| 1900 | out: |
| 1901 | return ret; |
| 1902 | } |
| 1903 | |
| 1904 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1905 | * ext4_ext_insert_extent: |
| 1906 | * tries to merge requsted extent into the existing extent or |
| 1907 | * inserts requested extent as new one into the tree, |
| 1908 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1909 | */ |
| 1910 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1911 | struct ext4_ext_path **ppath, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1912 | struct ext4_extent *newext, int gb_flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1913 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1914 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1915 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1916 | struct ext4_extent *ex, *fex; |
| 1917 | struct ext4_extent *nearex; /* nearest extent */ |
| 1918 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1919 | int depth, len, err; |
| 1920 | ext4_lblk_t next; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1921 | int mb_flags = 0, unwritten; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1922 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 1923 | if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 1924 | mb_flags |= EXT4_MB_DELALLOC_RESERVED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1925 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1926 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1927 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1928 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1929 | depth = ext_depth(inode); |
| 1930 | ex = path[depth].p_ext; |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1931 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1932 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1933 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1934 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1935 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1936 | |
| 1937 | /* try to insert block into found extent and return */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1938 | if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1939 | |
| 1940 | /* |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1941 | * Try to see whether we should rather test the extent on |
| 1942 | * 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] | 1943 | * ext4_find_extent() can return either extent on the |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1944 | * left, or on the right from the searched position. This |
| 1945 | * will make merging more effective. |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1946 | */ |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1947 | if (ex < EXT_LAST_EXTENT(eh) && |
| 1948 | (le32_to_cpu(ex->ee_block) + |
| 1949 | ext4_ext_get_actual_len(ex) < |
| 1950 | le32_to_cpu(newext->ee_block))) { |
| 1951 | ex += 1; |
| 1952 | goto prepend; |
| 1953 | } else if ((ex > EXT_FIRST_EXTENT(eh)) && |
| 1954 | (le32_to_cpu(newext->ee_block) + |
| 1955 | ext4_ext_get_actual_len(newext) < |
| 1956 | le32_to_cpu(ex->ee_block))) |
| 1957 | ex -= 1; |
| 1958 | |
| 1959 | /* Try to append newex to the ex */ |
| 1960 | if (ext4_can_extents_be_merged(inode, ex, newext)) { |
| 1961 | ext_debug("append [%d]%d block to %u:[%d]%d" |
| 1962 | "(from %llu)\n", |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1963 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1964 | ext4_ext_get_actual_len(newext), |
| 1965 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1966 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1967 | ext4_ext_get_actual_len(ex), |
| 1968 | ext4_ext_pblock(ex)); |
| 1969 | err = ext4_ext_get_access(handle, inode, |
| 1970 | path + depth); |
| 1971 | if (err) |
| 1972 | return err; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1973 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1974 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1975 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1976 | if (unwritten) |
| 1977 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1978 | eh = path[depth].p_hdr; |
| 1979 | nearex = ex; |
| 1980 | goto merge; |
| 1981 | } |
| 1982 | |
| 1983 | prepend: |
| 1984 | /* Try to prepend newex to the ex */ |
| 1985 | if (ext4_can_extents_be_merged(inode, newext, ex)) { |
| 1986 | ext_debug("prepend %u[%d]%d block to %u:[%d]%d" |
| 1987 | "(from %llu)\n", |
| 1988 | le32_to_cpu(newext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1989 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1990 | ext4_ext_get_actual_len(newext), |
| 1991 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1992 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1993 | ext4_ext_get_actual_len(ex), |
| 1994 | ext4_ext_pblock(ex)); |
| 1995 | err = ext4_ext_get_access(handle, inode, |
| 1996 | path + depth); |
| 1997 | if (err) |
| 1998 | return err; |
| 1999 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2000 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2001 | ex->ee_block = newext->ee_block; |
| 2002 | ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); |
| 2003 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 2004 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2005 | if (unwritten) |
| 2006 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2007 | eh = path[depth].p_hdr; |
| 2008 | nearex = ex; |
| 2009 | goto merge; |
| 2010 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2013 | depth = ext_depth(inode); |
| 2014 | eh = path[depth].p_hdr; |
| 2015 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 2016 | goto has_space; |
| 2017 | |
| 2018 | /* probably next leaf has space for us? */ |
| 2019 | fex = EXT_LAST_EXTENT(eh); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2020 | next = EXT_MAX_BLOCKS; |
| 2021 | 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] | 2022 | next = ext4_ext_next_leaf_block(path); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2023 | if (next != EXT_MAX_BLOCKS) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2024 | ext_debug("next leaf block - %u\n", next); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2025 | BUG_ON(npath != NULL); |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2026 | npath = ext4_find_extent(inode, next, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2027 | if (IS_ERR(npath)) |
| 2028 | return PTR_ERR(npath); |
| 2029 | BUG_ON(npath->p_depth != path->p_depth); |
| 2030 | eh = npath[depth].p_hdr; |
| 2031 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2032 | ext_debug("next leaf isn't full(%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2033 | le16_to_cpu(eh->eh_entries)); |
| 2034 | path = npath; |
Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 2035 | goto has_space; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2036 | } |
| 2037 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 2038 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 2039 | } |
| 2040 | |
| 2041 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2042 | * There is no free space in the found leaf. |
| 2043 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2044 | */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2045 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 2046 | mb_flags |= EXT4_MB_USE_RESERVED; |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2047 | 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] | 2048 | ppath, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2049 | if (err) |
| 2050 | goto cleanup; |
| 2051 | depth = ext_depth(inode); |
| 2052 | eh = path[depth].p_hdr; |
| 2053 | |
| 2054 | has_space: |
| 2055 | nearex = path[depth].p_ext; |
| 2056 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2057 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2058 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2059 | goto cleanup; |
| 2060 | |
| 2061 | if (!nearex) { |
| 2062 | /* there is no extent in this leaf, create first one */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2063 | ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2064 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2065 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2066 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2067 | ext4_ext_get_actual_len(newext)); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2068 | nearex = EXT_FIRST_EXTENT(eh); |
| 2069 | } else { |
| 2070 | if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2071 | > le32_to_cpu(nearex->ee_block)) { |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2072 | /* Insert after */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2073 | ext_debug("insert %u:%llu:[%d]%d before: " |
| 2074 | "nearest %p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2075 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2076 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2077 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2078 | ext4_ext_get_actual_len(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2079 | nearex); |
| 2080 | nearex++; |
| 2081 | } else { |
| 2082 | /* Insert before */ |
| 2083 | BUG_ON(newext->ee_block == nearex->ee_block); |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2084 | ext_debug("insert %u:%llu:[%d]%d after: " |
| 2085 | "nearest %p\n", |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2086 | le32_to_cpu(newext->ee_block), |
| 2087 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2088 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2089 | ext4_ext_get_actual_len(newext), |
| 2090 | nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2091 | } |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2092 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
| 2093 | if (len > 0) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2094 | ext_debug("insert %u:%llu:[%d]%d: " |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2095 | "move %d extents from 0x%p to 0x%p\n", |
| 2096 | le32_to_cpu(newext->ee_block), |
| 2097 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2098 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2099 | ext4_ext_get_actual_len(newext), |
| 2100 | len, nearex, nearex + 1); |
| 2101 | memmove(nearex + 1, nearex, |
| 2102 | len * sizeof(struct ext4_extent)); |
| 2103 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2106 | le16_add_cpu(&eh->eh_entries, 1); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2107 | path[depth].p_ext = nearex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2108 | nearex->ee_block = newext->ee_block; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2109 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2110 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2111 | |
| 2112 | merge: |
HaiboLiu | e7bcf82 | 2012-07-09 16:29:28 -0400 | [diff] [blame] | 2113 | /* try to merge extents */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2114 | if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2115 | ext4_ext_try_to_merge(handle, inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2116 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2117 | |
| 2118 | /* time to correct all indexes above */ |
| 2119 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2120 | if (err) |
| 2121 | goto cleanup; |
| 2122 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2123 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2124 | |
| 2125 | cleanup: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2126 | ext4_ext_drop_refs(npath); |
| 2127 | kfree(npath); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2128 | return err; |
| 2129 | } |
| 2130 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 2131 | static int ext4_fill_es_cache_info(struct inode *inode, |
| 2132 | ext4_lblk_t block, ext4_lblk_t num, |
| 2133 | struct fiemap_extent_info *fieinfo) |
| 2134 | { |
| 2135 | ext4_lblk_t next, end = block + num - 1; |
| 2136 | struct extent_status es; |
| 2137 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
| 2138 | unsigned int flags; |
| 2139 | int err; |
| 2140 | |
| 2141 | while (block <= end) { |
| 2142 | next = 0; |
| 2143 | flags = 0; |
| 2144 | if (!ext4_es_lookup_extent(inode, block, &next, &es)) |
| 2145 | break; |
| 2146 | if (ext4_es_is_unwritten(&es)) |
| 2147 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 2148 | if (ext4_es_is_delayed(&es)) |
| 2149 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 2150 | FIEMAP_EXTENT_UNKNOWN); |
| 2151 | if (ext4_es_is_hole(&es)) |
| 2152 | flags |= EXT4_FIEMAP_EXTENT_HOLE; |
| 2153 | if (next == 0) |
| 2154 | flags |= FIEMAP_EXTENT_LAST; |
| 2155 | if (flags & (FIEMAP_EXTENT_DELALLOC| |
| 2156 | EXT4_FIEMAP_EXTENT_HOLE)) |
| 2157 | es.es_pblk = 0; |
| 2158 | else |
| 2159 | es.es_pblk = ext4_es_pblock(&es); |
| 2160 | err = fiemap_fill_next_extent(fieinfo, |
| 2161 | (__u64)es.es_lblk << blksize_bits, |
| 2162 | (__u64)es.es_pblk << blksize_bits, |
| 2163 | (__u64)es.es_len << blksize_bits, |
| 2164 | flags); |
| 2165 | if (next == 0) |
| 2166 | break; |
| 2167 | block = next; |
| 2168 | if (err < 0) |
| 2169 | return err; |
| 2170 | if (err == 1) |
| 2171 | return 0; |
| 2172 | } |
| 2173 | return 0; |
| 2174 | } |
| 2175 | |
| 2176 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2177 | /* |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2178 | * ext4_ext_determine_hole - determine hole around given block |
| 2179 | * @inode: inode we lookup in |
| 2180 | * @path: path in extent tree to @lblk |
| 2181 | * @lblk: pointer to logical block around which we want to determine hole |
| 2182 | * |
| 2183 | * Determine hole length (and start if easily possible) around given logical |
| 2184 | * block. We don't try too hard to find the beginning of the hole but @path |
| 2185 | * actually points to extent before @lblk, we provide it. |
| 2186 | * |
| 2187 | * The function returns the length of a hole starting at @lblk. We update @lblk |
| 2188 | * to the beginning of the hole if we managed to find it. |
| 2189 | */ |
| 2190 | static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, |
| 2191 | struct ext4_ext_path *path, |
| 2192 | ext4_lblk_t *lblk) |
| 2193 | { |
| 2194 | int depth = ext_depth(inode); |
| 2195 | struct ext4_extent *ex; |
| 2196 | ext4_lblk_t len; |
| 2197 | |
| 2198 | ex = path[depth].p_ext; |
| 2199 | if (ex == NULL) { |
| 2200 | /* there is no extent yet, so gap is [0;-] */ |
| 2201 | *lblk = 0; |
| 2202 | len = EXT_MAX_BLOCKS; |
| 2203 | } else if (*lblk < le32_to_cpu(ex->ee_block)) { |
| 2204 | len = le32_to_cpu(ex->ee_block) - *lblk; |
| 2205 | } else if (*lblk >= le32_to_cpu(ex->ee_block) |
| 2206 | + ext4_ext_get_actual_len(ex)) { |
| 2207 | ext4_lblk_t next; |
| 2208 | |
| 2209 | *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
| 2210 | next = ext4_ext_next_allocated_block(path); |
| 2211 | BUG_ON(next == *lblk); |
| 2212 | len = next - *lblk; |
| 2213 | } else { |
| 2214 | BUG(); |
| 2215 | } |
| 2216 | return len; |
| 2217 | } |
| 2218 | |
| 2219 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2220 | * ext4_ext_put_gap_in_cache: |
| 2221 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2222 | * and cache this gap |
| 2223 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2224 | static void |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2225 | ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, |
| 2226 | ext4_lblk_t hole_len) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2227 | { |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2228 | struct extent_status es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2229 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 2230 | ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, |
| 2231 | hole_start + hole_len - 1, &es); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2232 | if (es.es_len) { |
| 2233 | /* There's delayed extent containing lblock? */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2234 | if (es.es_lblk <= hole_start) |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2235 | return; |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2236 | hole_len = min(es.es_lblk - hole_start, hole_len); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2237 | } |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2238 | ext_debug(" -> %u:%u\n", hole_start, hole_len); |
| 2239 | ext4_es_insert_extent(inode, hole_start, hole_len, ~0, |
| 2240 | EXTENT_STATUS_HOLE); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2244 | * ext4_ext_rm_idx: |
| 2245 | * removes index from the index block. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2246 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2247 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2248 | struct ext4_ext_path *path, int depth) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2249 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2250 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2251 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2252 | |
| 2253 | /* free index block */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2254 | depth--; |
| 2255 | path = path + depth; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2256 | leaf = ext4_idx_pblock(path->p_idx); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2257 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2258 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2259 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2260 | } |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2261 | err = ext4_ext_get_access(handle, inode, path); |
| 2262 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2263 | return err; |
Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2264 | |
| 2265 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2266 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2267 | len *= sizeof(struct ext4_extent_idx); |
| 2268 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2269 | } |
| 2270 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2271 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2272 | err = ext4_ext_dirty(handle, inode, path); |
| 2273 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2274 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2275 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2276 | trace_ext4_ext_rm_idx(inode, leaf); |
| 2277 | |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2278 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2279 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2280 | |
| 2281 | while (--depth >= 0) { |
| 2282 | if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) |
| 2283 | break; |
| 2284 | path--; |
| 2285 | err = ext4_ext_get_access(handle, inode, path); |
| 2286 | if (err) |
| 2287 | break; |
| 2288 | path->p_idx->ei_block = (path+1)->p_idx->ei_block; |
| 2289 | err = ext4_ext_dirty(handle, inode, path); |
| 2290 | if (err) |
| 2291 | break; |
| 2292 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2293 | return err; |
| 2294 | } |
| 2295 | |
| 2296 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2297 | * ext4_ext_calc_credits_for_single_extent: |
| 2298 | * This routine returns max. credits that needed to insert an extent |
| 2299 | * to the extent tree. |
| 2300 | * When pass the actual path, the caller should calculate credits |
| 2301 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2302 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2303 | 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] | 2304 | struct ext4_ext_path *path) |
| 2305 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2306 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2307 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2308 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2309 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2310 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2311 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2312 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2313 | |
| 2314 | /* |
| 2315 | * There are some space in the leaf tree, no |
| 2316 | * need to account for leaf block credit |
| 2317 | * |
| 2318 | * bitmaps and block group descriptor blocks |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 2319 | * and other metadata blocks still need to be |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2320 | * accounted. |
| 2321 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2322 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2323 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2324 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2325 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2328 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2329 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2330 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2331 | /* |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2332 | * 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] | 2333 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2334 | * If we add a single extent, then in the worse case, each tree level |
| 2335 | * index/leaf need to be changed in case of the tree split. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2336 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2337 | * If more extents are inserted, they could cause the whole tree split more |
| 2338 | * than once, but this is really rare. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2339 | */ |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2340 | int ext4_ext_index_trans_blocks(struct inode *inode, int extents) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2341 | { |
| 2342 | int index; |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 2343 | int depth; |
| 2344 | |
| 2345 | /* If we are converting the inline data, only one is needed here. */ |
| 2346 | if (ext4_has_inline_data(inode)) |
| 2347 | return 1; |
| 2348 | |
| 2349 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2350 | |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2351 | if (extents <= 1) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2352 | index = depth * 2; |
| 2353 | else |
| 2354 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2355 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2356 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2359 | static inline int get_default_free_blocks_flags(struct inode *inode) |
| 2360 | { |
Tahsin Erdogan | ddfa17e | 2017-06-21 21:36:51 -0400 | [diff] [blame] | 2361 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || |
| 2362 | ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE)) |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2363 | return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET; |
| 2364 | else if (ext4_should_journal_data(inode)) |
| 2365 | return EXT4_FREE_BLOCKS_FORGET; |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2369 | /* |
| 2370 | * ext4_rereserve_cluster - increment the reserved cluster count when |
| 2371 | * freeing a cluster with a pending reservation |
| 2372 | * |
| 2373 | * @inode - file containing the cluster |
| 2374 | * @lblk - logical block in cluster to be reserved |
| 2375 | * |
| 2376 | * Increments the reserved cluster count and adjusts quota in a bigalloc |
| 2377 | * file system when freeing a partial cluster containing at least one |
| 2378 | * delayed and unwritten block. A partial cluster meeting that |
| 2379 | * requirement will have a pending reservation. If so, the |
| 2380 | * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to |
| 2381 | * defer reserved and allocated space accounting to a subsequent call |
| 2382 | * to this function. |
| 2383 | */ |
| 2384 | static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) |
| 2385 | { |
| 2386 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2387 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 2388 | |
| 2389 | dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); |
| 2390 | |
| 2391 | spin_lock(&ei->i_block_reservation_lock); |
| 2392 | ei->i_reserved_data_blocks++; |
| 2393 | percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); |
| 2394 | spin_unlock(&ei->i_block_reservation_lock); |
| 2395 | |
| 2396 | percpu_counter_add(&sbi->s_freeclusters_counter, 1); |
| 2397 | ext4_remove_pending(inode, lblk); |
| 2398 | } |
| 2399 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2400 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2401 | struct ext4_extent *ex, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2402 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2403 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2404 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2405 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2406 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2407 | ext4_fsblk_t last_pblk, pblk; |
| 2408 | ext4_lblk_t num; |
| 2409 | int flags; |
| 2410 | |
| 2411 | /* only extent tail removal is allowed */ |
| 2412 | if (from < le32_to_cpu(ex->ee_block) || |
| 2413 | to != le32_to_cpu(ex->ee_block) + ee_len - 1) { |
| 2414 | ext4_error(sbi->s_sb, |
| 2415 | "strange request: removal(2) %u-%u from %u:%u", |
| 2416 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
| 2417 | return 0; |
| 2418 | } |
| 2419 | |
| 2420 | #ifdef EXTENTS_STATS |
| 2421 | spin_lock(&sbi->s_ext_stats_lock); |
| 2422 | sbi->s_ext_blocks += ee_len; |
| 2423 | sbi->s_ext_extents++; |
| 2424 | if (ee_len < sbi->s_ext_min) |
| 2425 | sbi->s_ext_min = ee_len; |
| 2426 | if (ee_len > sbi->s_ext_max) |
| 2427 | sbi->s_ext_max = ee_len; |
| 2428 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2429 | sbi->s_depth_max = ext_depth(inode); |
| 2430 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2431 | #endif |
| 2432 | |
| 2433 | trace_ext4_remove_blocks(inode, ex, from, to, partial); |
| 2434 | |
| 2435 | /* |
| 2436 | * if we have a partial cluster, and it's different from the |
| 2437 | * cluster of the last block in the extent, we free it |
| 2438 | */ |
| 2439 | last_pblk = ext4_ext_pblock(ex) + ee_len - 1; |
| 2440 | |
| 2441 | if (partial->state != initial && |
| 2442 | partial->pclu != EXT4_B2C(sbi, last_pblk)) { |
| 2443 | if (partial->state == tofree) { |
| 2444 | flags = get_default_free_blocks_flags(inode); |
| 2445 | if (ext4_is_pending(inode, partial->lblk)) |
| 2446 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2447 | ext4_free_blocks(handle, inode, NULL, |
| 2448 | EXT4_C2B(sbi, partial->pclu), |
| 2449 | sbi->s_cluster_ratio, flags); |
| 2450 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2451 | ext4_rereserve_cluster(inode, partial->lblk); |
| 2452 | } |
| 2453 | partial->state = initial; |
| 2454 | } |
| 2455 | |
| 2456 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 2457 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
| 2458 | |
| 2459 | /* |
| 2460 | * We free the partial cluster at the end of the extent (if any), |
| 2461 | * unless the cluster is used by another extent (partial_cluster |
| 2462 | * state is nofree). If a partial cluster exists here, it must be |
| 2463 | * shared with the last block in the extent. |
| 2464 | */ |
| 2465 | flags = get_default_free_blocks_flags(inode); |
| 2466 | |
| 2467 | /* partial, left end cluster aligned, right end unaligned */ |
| 2468 | if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && |
| 2469 | (EXT4_LBLK_CMASK(sbi, to) >= from) && |
| 2470 | (partial->state != nofree)) { |
| 2471 | if (ext4_is_pending(inode, to)) |
| 2472 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2473 | ext4_free_blocks(handle, inode, NULL, |
| 2474 | EXT4_PBLK_CMASK(sbi, last_pblk), |
| 2475 | sbi->s_cluster_ratio, flags); |
| 2476 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2477 | ext4_rereserve_cluster(inode, to); |
| 2478 | partial->state = initial; |
| 2479 | flags = get_default_free_blocks_flags(inode); |
| 2480 | } |
| 2481 | |
| 2482 | flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
Andrey Sidorov | 18888cf | 2012-09-19 14:14:53 -0400 | [diff] [blame] | 2483 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2484 | /* |
| 2485 | * For bigalloc file systems, we never free a partial cluster |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2486 | * 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] | 2487 | * need to free it on a subsequent call to ext4_remove_blocks, |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2488 | * 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] | 2489 | */ |
| 2490 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2491 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2492 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2493 | /* reset the partial cluster if we've freed past it */ |
| 2494 | if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) |
| 2495 | partial->state = initial; |
| 2496 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2497 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2498 | * If we've freed the entire extent but the beginning is not left |
| 2499 | * cluster aligned and is not marked as ineligible for freeing we |
| 2500 | * record the partial cluster at the beginning of the extent. It |
| 2501 | * wasn't freed by the preceding ext4_free_blocks() call, and we |
| 2502 | * need to look farther to the left to determine if it's to be freed |
| 2503 | * (not shared with another extent). Else, reset the partial |
| 2504 | * cluster - we're either done freeing or the beginning of the |
| 2505 | * extent is left cluster aligned. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2506 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2507 | if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { |
| 2508 | if (partial->state == initial) { |
| 2509 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2510 | partial->lblk = from; |
| 2511 | partial->state = tofree; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2512 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2513 | } else { |
| 2514 | partial->state = initial; |
| 2515 | } |
| 2516 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2517 | return 0; |
| 2518 | } |
| 2519 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2520 | /* |
| 2521 | * ext4_ext_rm_leaf() Removes the extents associated with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2522 | * blocks appearing between "start" and "end". Both "start" |
| 2523 | * and "end" must appear in the same extent or EIO is returned. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2524 | * |
| 2525 | * @handle: The journal handle |
| 2526 | * @inode: The files inode |
| 2527 | * @path: The path to the leaf |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2528 | * @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] | 2529 | * has been released from it. However, if this value is |
| 2530 | * negative, it's a cluster just to the right of the |
| 2531 | * punched region and it must not be freed. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2532 | * @start: The first block to remove |
| 2533 | * @end: The last block to remove |
| 2534 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2535 | static int |
| 2536 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2537 | struct ext4_ext_path *path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2538 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2539 | ext4_lblk_t start, ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2540 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2541 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2542 | int err = 0, correct_index = 0; |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2543 | int depth = ext_depth(inode), credits, revoke_credits; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2544 | struct ext4_extent_header *eh; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2545 | ext4_lblk_t a, b; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2546 | unsigned num; |
| 2547 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2548 | unsigned short ex_ee_len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2549 | unsigned unwritten = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2550 | struct ext4_extent *ex; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2551 | ext4_fsblk_t pblk; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2552 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2553 | /* the header must be checked already in ext4_ext_remove_space() */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2554 | ext_debug("truncate since %u in leaf to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2555 | if (!path[depth].p_hdr) |
| 2556 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2557 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2558 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2559 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2560 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2561 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2562 | /* find where to start removing */ |
Ashish Sangwan | 6ae06ff | 2013-07-01 08:12:41 -0400 | [diff] [blame] | 2563 | ex = path[depth].p_ext; |
| 2564 | if (!ex) |
| 2565 | ex = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2566 | |
| 2567 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2568 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2569 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2570 | trace_ext4_ext_rm_leaf(inode, start, ex, partial); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2571 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2572 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2573 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2574 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2575 | if (ext4_ext_is_unwritten(ex)) |
| 2576 | unwritten = 1; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2577 | else |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2578 | unwritten = 0; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2579 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 2580 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2581 | unwritten, ex_ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2582 | path[depth].p_ext = ex; |
| 2583 | |
| 2584 | a = ex_ee_block > start ? ex_ee_block : start; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2585 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2586 | ex_ee_block+ex_ee_len - 1 : end; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2587 | |
| 2588 | ext_debug(" border %u:%u\n", a, b); |
| 2589 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2590 | /* If this extent is beyond the end of the hole, skip it */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2591 | if (end < ex_ee_block) { |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2592 | /* |
| 2593 | * We're going to skip this extent and move to another, |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2594 | * so note that its first cluster is in use to avoid |
| 2595 | * freeing it when removing blocks. Eventually, the |
| 2596 | * right edge of the truncated/punched region will |
| 2597 | * be just to the left. |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2598 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2599 | if (sbi->s_cluster_ratio > 1) { |
| 2600 | pblk = ext4_ext_pblock(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2601 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2602 | partial->state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2603 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2604 | ex--; |
| 2605 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2606 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2607 | continue; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2608 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
Lukas Czerner | dc1841d | 2012-03-19 23:07:43 -0400 | [diff] [blame] | 2609 | EXT4_ERROR_INODE(inode, |
| 2610 | "can not handle truncate %u:%u " |
| 2611 | "on extent %u:%u", |
| 2612 | start, end, ex_ee_block, |
| 2613 | ex_ee_block + ex_ee_len - 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2614 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2615 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2616 | } else if (a != ex_ee_block) { |
| 2617 | /* remove tail of the extent */ |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2618 | num = a - ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2619 | } else { |
| 2620 | /* remove whole extent: excellent! */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2621 | num = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2622 | } |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2623 | /* |
| 2624 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2625 | * descriptor) for each block group; assume two block |
| 2626 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2627 | * the worst case |
| 2628 | */ |
| 2629 | 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] | 2630 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2631 | correct_index = 1; |
| 2632 | credits += (ext_depth(inode)) + 1; |
| 2633 | } |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2634 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2635 | /* |
| 2636 | * We may end up freeing some index blocks and data from the |
| 2637 | * punched range. Note that partial clusters are accounted for |
| 2638 | * by ext4_free_data_revoke_credits(). |
| 2639 | */ |
| 2640 | revoke_credits = |
| 2641 | ext4_free_metadata_revoke_credits(inode->i_sb, |
| 2642 | ext_depth(inode)) + |
| 2643 | ext4_free_data_revoke_credits(inode, b - a + 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2644 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2645 | err = ext4_datasem_ensure_credits(handle, inode, credits, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2646 | credits, revoke_credits); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2647 | if (err) { |
| 2648 | if (err > 0) |
| 2649 | err = -EAGAIN; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2650 | goto out; |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2651 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2652 | |
| 2653 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2654 | if (err) |
| 2655 | goto out; |
| 2656 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2657 | err = ext4_remove_blocks(handle, inode, ex, partial, a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2658 | if (err) |
| 2659 | goto out; |
| 2660 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2661 | if (num == 0) |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2662 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2663 | ext4_ext_store_pblock(ex, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2664 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2665 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2666 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2667 | * Do not mark unwritten if all the blocks in the |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2668 | * extent have been removed. |
| 2669 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2670 | if (unwritten && num) |
| 2671 | ext4_ext_mark_unwritten(ex); |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2672 | /* |
| 2673 | * If the extent was completely released, |
| 2674 | * we need to remove it from the leaf |
| 2675 | */ |
| 2676 | if (num == 0) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2677 | if (end != EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2678 | /* |
| 2679 | * For hole punching, we need to scoot all the |
| 2680 | * extents up when an extent is removed so that |
| 2681 | * we dont have blank extents in the middle |
| 2682 | */ |
| 2683 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2684 | sizeof(struct ext4_extent)); |
| 2685 | |
| 2686 | /* Now get rid of the one at the end */ |
| 2687 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2688 | sizeof(struct ext4_extent)); |
| 2689 | } |
| 2690 | le16_add_cpu(&eh->eh_entries, -1); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2691 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2692 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2693 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2694 | if (err) |
| 2695 | goto out; |
| 2696 | |
Yongqiang Yang | bf52c6f | 2011-11-01 18:59:26 -0400 | [diff] [blame] | 2697 | ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num, |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2698 | ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2699 | ex--; |
| 2700 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2701 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | if (correct_index && eh->eh_entries) |
| 2705 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2706 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2707 | /* |
Eric Whitney | ad6599a | 2014-04-01 19:49:30 -0400 | [diff] [blame] | 2708 | * If there's a partial cluster and at least one extent remains in |
| 2709 | * 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] | 2710 | * current extent. If it is shared with the current extent |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2711 | * 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] | 2712 | * truncated/punched region and we're done removing blocks. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2713 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2714 | if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2715 | pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2716 | if (partial->pclu != EXT4_B2C(sbi, pblk)) { |
| 2717 | int flags = get_default_free_blocks_flags(inode); |
| 2718 | |
| 2719 | if (ext4_is_pending(inode, partial->lblk)) |
| 2720 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2721 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2722 | EXT4_C2B(sbi, partial->pclu), |
| 2723 | sbi->s_cluster_ratio, flags); |
| 2724 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2725 | ext4_rereserve_cluster(inode, partial->lblk); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2726 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2727 | partial->state = initial; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2728 | } |
| 2729 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2730 | /* if this leaf is free, then we should |
| 2731 | * remove it from index block above */ |
| 2732 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2733 | err = ext4_ext_rm_idx(handle, inode, path, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2734 | |
| 2735 | out: |
| 2736 | return err; |
| 2737 | } |
| 2738 | |
| 2739 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2740 | * ext4_ext_more_to_rm: |
| 2741 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2742 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2743 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2744 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2745 | { |
| 2746 | BUG_ON(path->p_idx == NULL); |
| 2747 | |
| 2748 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2749 | return 0; |
| 2750 | |
| 2751 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2752 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2753 | * so we have to consider current index for truncation |
| 2754 | */ |
| 2755 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2756 | return 0; |
| 2757 | return 1; |
| 2758 | } |
| 2759 | |
Theodore Ts'o | 26a4c0c | 2013-04-03 12:45:17 -0400 | [diff] [blame] | 2760 | int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
| 2761 | ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2762 | { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2763 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2764 | int depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2765 | struct ext4_ext_path *path = NULL; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2766 | struct partial_cluster partial; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2767 | handle_t *handle; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2768 | int i = 0, err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2769 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2770 | partial.pclu = 0; |
| 2771 | partial.lblk = 0; |
| 2772 | partial.state = initial; |
| 2773 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2774 | ext_debug("truncate since %u to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2775 | |
| 2776 | /* probably first extent we're gonna free will be last in block */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2777 | handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, |
| 2778 | depth + 1, |
| 2779 | ext4_free_metadata_revoke_credits(inode->i_sb, depth)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2780 | if (IS_ERR(handle)) |
| 2781 | return PTR_ERR(handle); |
| 2782 | |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2783 | again: |
Lukas Czerner | 6180132 | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2784 | trace_ext4_ext_remove_space(inode, start, end, depth); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2785 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2786 | /* |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2787 | * Check if we are removing extents inside the extent tree. If that |
| 2788 | * is the case, we are going to punch a hole inside the extent tree |
| 2789 | * so we have to check whether we need to split the extent covering |
| 2790 | * the last block to remove so we can easily remove the part of it |
| 2791 | * in ext4_ext_rm_leaf(). |
| 2792 | */ |
| 2793 | if (end < EXT_MAX_BLOCKS - 1) { |
| 2794 | struct ext4_extent *ex; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2795 | ext4_lblk_t ee_block, ex_end, lblk; |
| 2796 | ext4_fsblk_t pblk; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2797 | |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2798 | /* find extent for or closest extent to this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2799 | path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2800 | if (IS_ERR(path)) { |
| 2801 | ext4_journal_stop(handle); |
| 2802 | return PTR_ERR(path); |
| 2803 | } |
| 2804 | depth = ext_depth(inode); |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2805 | /* 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] | 2806 | ex = path[depth].p_ext; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2807 | if (!ex) { |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2808 | if (depth) { |
| 2809 | EXT4_ERROR_INODE(inode, |
| 2810 | "path[%d].p_hdr == NULL", |
| 2811 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2812 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2813 | } |
| 2814 | goto out; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2815 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2816 | |
| 2817 | ee_block = le32_to_cpu(ex->ee_block); |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2818 | ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2819 | |
| 2820 | /* |
| 2821 | * See if the last block is inside the extent, if so split |
| 2822 | * the extent at 'end' block so we can easily remove the |
| 2823 | * tail of the first part of the split extent in |
| 2824 | * ext4_ext_rm_leaf(). |
| 2825 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2826 | if (end >= ee_block && end < ex_end) { |
| 2827 | |
| 2828 | /* |
| 2829 | * If we're going to split the extent, note that |
| 2830 | * the cluster containing the block after 'end' is |
| 2831 | * in use to avoid freeing it when removing blocks. |
| 2832 | */ |
| 2833 | if (sbi->s_cluster_ratio > 1) { |
| 2834 | pblk = ext4_ext_pblock(ex) + end - ee_block + 2; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2835 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2836 | partial.state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2837 | } |
| 2838 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2839 | /* |
| 2840 | * Split the extent in two so that 'end' is the last |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 2841 | * block in the first new extent. Also we should not |
| 2842 | * fail removing space due to ENOSPC so try to use |
| 2843 | * reserved block if that happens. |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2844 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2845 | err = ext4_force_split_extent_at(handle, inode, &path, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 2846 | end + 1, 1); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2847 | if (err < 0) |
| 2848 | goto out; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2849 | |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2850 | } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && |
| 2851 | partial.state == initial) { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2852 | /* |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2853 | * If we're punching, there's an extent to the right. |
| 2854 | * If the partial cluster hasn't been set, set it to |
| 2855 | * that extent's first cluster and its state to nofree |
| 2856 | * so it won't be freed should it contain blocks to be |
| 2857 | * removed. If it's already set (tofree/nofree), we're |
| 2858 | * retrying and keep the original partial cluster info |
| 2859 | * so a cluster marked tofree as a result of earlier |
| 2860 | * extent removal is not lost. |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2861 | */ |
| 2862 | lblk = ex_end + 1; |
| 2863 | err = ext4_ext_search_right(inode, path, &lblk, &pblk, |
| 2864 | &ex); |
| 2865 | if (err) |
| 2866 | goto out; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2867 | if (pblk) { |
| 2868 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2869 | partial.state = nofree; |
| 2870 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2871 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2872 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2873 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2874 | * We start scanning from right side, freeing all the blocks |
| 2875 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2876 | */ |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2877 | depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2878 | if (path) { |
| 2879 | int k = i = depth; |
| 2880 | while (--k > 0) |
| 2881 | path[k].p_block = |
| 2882 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
| 2883 | } else { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2884 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2885 | GFP_NOFS); |
| 2886 | if (path == NULL) { |
| 2887 | ext4_journal_stop(handle); |
| 2888 | return -ENOMEM; |
| 2889 | } |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 2890 | path[0].p_maxdepth = path[0].p_depth = depth; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2891 | path[0].p_hdr = ext_inode_hdr(inode); |
Theodore Ts'o | 89a4e48 | 2012-08-17 08:54:52 -0400 | [diff] [blame] | 2892 | i = 0; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2893 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 2894 | if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2895 | err = -EFSCORRUPTED; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2896 | goto out; |
| 2897 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2898 | } |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2899 | err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2900 | |
| 2901 | while (i >= 0 && err == 0) { |
| 2902 | if (i == depth) { |
| 2903 | /* this is leaf block */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2904 | err = ext4_ext_rm_leaf(handle, inode, path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2905 | &partial, start, end); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2906 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2907 | brelse(path[i].p_bh); |
| 2908 | path[i].p_bh = NULL; |
| 2909 | i--; |
| 2910 | continue; |
| 2911 | } |
| 2912 | |
| 2913 | /* this is index block */ |
| 2914 | if (!path[i].p_hdr) { |
| 2915 | ext_debug("initialize header\n"); |
| 2916 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2919 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2920 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2921 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2922 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2923 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2924 | path[i].p_hdr, |
| 2925 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2926 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2927 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2928 | path[i].p_idx--; |
| 2929 | } |
| 2930 | |
| 2931 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 2932 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2933 | path[i].p_idx); |
| 2934 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2935 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2936 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2937 | ext_debug("move to level %d (block %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2938 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2939 | memset(path + i + 1, 0, sizeof(*path)); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2940 | bh = read_extent_tree_block(inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2941 | ext4_idx_pblock(path[i].p_idx), depth - i - 1, |
| 2942 | EXT4_EX_NOCACHE); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2943 | if (IS_ERR(bh)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2944 | /* should we reset i_size? */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2945 | err = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2946 | break; |
| 2947 | } |
Theodore Ts'o | 76828c88 | 2013-07-15 12:27:47 -0400 | [diff] [blame] | 2948 | /* Yield here to deal with large extent trees. |
| 2949 | * Should be a no-op if we did IO above. */ |
| 2950 | cond_resched(); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2951 | if (WARN_ON(i + 1 > depth)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2952 | err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2953 | break; |
| 2954 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2955 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2956 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2957 | /* save actual number of indexes since this |
| 2958 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2959 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2960 | i++; |
| 2961 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2962 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2963 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2964 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2965 | * handle must be already prepared by the |
| 2966 | * truncatei_leaf() */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2967 | err = ext4_ext_rm_idx(handle, inode, path, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2968 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2969 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2970 | brelse(path[i].p_bh); |
| 2971 | path[i].p_bh = NULL; |
| 2972 | i--; |
| 2973 | ext_debug("return to level %d\n", i); |
| 2974 | } |
| 2975 | } |
| 2976 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2977 | trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, |
| 2978 | path->p_hdr->eh_entries); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2979 | |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2980 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2981 | * if there's a partial cluster and we have removed the first extent |
| 2982 | * in the file, then we also free the partial cluster, if any |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2983 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2984 | if (partial.state == tofree && err == 0) { |
| 2985 | int flags = get_default_free_blocks_flags(inode); |
| 2986 | |
| 2987 | if (ext4_is_pending(inode, partial.lblk)) |
| 2988 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 2989 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2990 | EXT4_C2B(sbi, partial.pclu), |
| 2991 | sbi->s_cluster_ratio, flags); |
| 2992 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2993 | ext4_rereserve_cluster(inode, partial.lblk); |
| 2994 | partial.state = initial; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 2995 | } |
| 2996 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2997 | /* TODO: flexible tree reduction should be here */ |
| 2998 | if (path->p_hdr->eh_entries == 0) { |
| 2999 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3000 | * truncate to zero freed all the tree, |
| 3001 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3002 | */ |
| 3003 | err = ext4_ext_get_access(handle, inode, path); |
| 3004 | if (err == 0) { |
| 3005 | ext_inode_hdr(inode)->eh_depth = 0; |
| 3006 | ext_inode_hdr(inode)->eh_max = |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 3007 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3008 | err = ext4_ext_dirty(handle, inode, path); |
| 3009 | } |
| 3010 | } |
| 3011 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 3012 | ext4_ext_drop_refs(path); |
| 3013 | kfree(path); |
| 3014 | path = NULL; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3015 | if (err == -EAGAIN) |
| 3016 | goto again; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3017 | ext4_journal_stop(handle); |
| 3018 | |
| 3019 | return err; |
| 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * called at mount time |
| 3024 | */ |
| 3025 | void ext4_ext_init(struct super_block *sb) |
| 3026 | { |
| 3027 | /* |
| 3028 | * possible initialization would be here |
| 3029 | */ |
| 3030 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3031 | if (ext4_has_feature_extents(sb)) { |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3032 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3033 | printk(KERN_INFO "EXT4-fs: file extents enabled" |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 3034 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3035 | ", aggressive tests" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3036 | #endif |
| 3037 | #ifdef CHECK_BINSEARCH |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3038 | ", check binsearch" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3039 | #endif |
| 3040 | #ifdef EXTENTS_STATS |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3041 | ", stats" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3042 | #endif |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3043 | "\n"); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3044 | #endif |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3045 | #ifdef EXTENTS_STATS |
| 3046 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 3047 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 3048 | EXT4_SB(sb)->s_ext_max = 0; |
| 3049 | #endif |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | /* |
| 3054 | * called at umount time |
| 3055 | */ |
| 3056 | void ext4_ext_release(struct super_block *sb) |
| 3057 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3058 | if (!ext4_has_feature_extents(sb)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3059 | return; |
| 3060 | |
| 3061 | #ifdef EXTENTS_STATS |
| 3062 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 3063 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3064 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 3065 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 3066 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 3067 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 3068 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 3069 | } |
| 3070 | #endif |
| 3071 | } |
| 3072 | |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3073 | static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) |
| 3074 | { |
| 3075 | ext4_lblk_t ee_block; |
| 3076 | ext4_fsblk_t ee_pblock; |
| 3077 | unsigned int ee_len; |
| 3078 | |
| 3079 | ee_block = le32_to_cpu(ex->ee_block); |
| 3080 | ee_len = ext4_ext_get_actual_len(ex); |
| 3081 | ee_pblock = ext4_ext_pblock(ex); |
| 3082 | |
| 3083 | if (ee_len == 0) |
| 3084 | return 0; |
| 3085 | |
| 3086 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, |
| 3087 | EXTENT_STATUS_WRITTEN); |
| 3088 | } |
| 3089 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3090 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 3091 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 3092 | { |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 3093 | ext4_fsblk_t ee_pblock; |
| 3094 | unsigned int ee_len; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3095 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3096 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3097 | ee_pblock = ext4_ext_pblock(ex); |
Jan Kara | 53085fa | 2015-12-07 15:09:35 -0500 | [diff] [blame] | 3098 | return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, |
| 3099 | ee_len); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3100 | } |
| 3101 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3102 | /* |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3103 | * ext4_split_extent_at() splits an extent at given block. |
| 3104 | * |
| 3105 | * @handle: the journal handle |
| 3106 | * @inode: the file inode |
| 3107 | * @path: the path to the extent |
| 3108 | * @split: the logical block where the extent is splitted. |
| 3109 | * @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] | 3110 | * the states(init or unwritten) of new extents. |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3111 | * @flags: flags used to insert new extent to extent tree. |
| 3112 | * |
| 3113 | * |
| 3114 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 3115 | * of which are deterimined by split_flag. |
| 3116 | * |
| 3117 | * There are two cases: |
| 3118 | * a> the extent are splitted into two extent. |
| 3119 | * b> split is not needed, and just mark the extent. |
| 3120 | * |
| 3121 | * return 0 on success. |
| 3122 | */ |
| 3123 | static int ext4_split_extent_at(handle_t *handle, |
| 3124 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3125 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3126 | ext4_lblk_t split, |
| 3127 | int split_flag, |
| 3128 | int flags) |
| 3129 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3130 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3131 | ext4_fsblk_t newblock; |
| 3132 | ext4_lblk_t ee_block; |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3133 | struct ext4_extent *ex, newex, orig_ex, zero_ex; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3134 | struct ext4_extent *ex2 = NULL; |
| 3135 | unsigned int ee_len, depth; |
| 3136 | int err = 0; |
| 3137 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3138 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
| 3139 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
| 3140 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3141 | ext_debug("ext4_split_extents_at: inode %lu, logical" |
| 3142 | "block %llu\n", inode->i_ino, (unsigned long long)split); |
| 3143 | |
| 3144 | ext4_ext_show_leaf(inode, path); |
| 3145 | |
| 3146 | depth = ext_depth(inode); |
| 3147 | ex = path[depth].p_ext; |
| 3148 | ee_block = le32_to_cpu(ex->ee_block); |
| 3149 | ee_len = ext4_ext_get_actual_len(ex); |
| 3150 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 3151 | |
| 3152 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3153 | BUG_ON(!ext4_ext_is_unwritten(ex) && |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3154 | split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3155 | EXT4_EXT_MARK_UNWRIT1 | |
| 3156 | EXT4_EXT_MARK_UNWRIT2)); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3157 | |
| 3158 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3159 | if (err) |
| 3160 | goto out; |
| 3161 | |
| 3162 | if (split == ee_block) { |
| 3163 | /* |
| 3164 | * case b: block @split is the block that the extent begins with |
| 3165 | * then we just change the state of the extent, and splitting |
| 3166 | * is not needed. |
| 3167 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3168 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3169 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3170 | else |
| 3171 | ext4_ext_mark_initialized(ex); |
| 3172 | |
| 3173 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3174 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3175 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3176 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3177 | goto out; |
| 3178 | } |
| 3179 | |
| 3180 | /* case a */ |
| 3181 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 3182 | ex->ee_len = cpu_to_le16(split - ee_block); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3183 | if (split_flag & EXT4_EXT_MARK_UNWRIT1) |
| 3184 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3185 | |
| 3186 | /* |
| 3187 | * path may lead to new leaf, not to original leaf any more |
| 3188 | * after ext4_ext_insert_extent() returns, |
| 3189 | */ |
| 3190 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3191 | if (err) |
| 3192 | goto fix_extent_len; |
| 3193 | |
| 3194 | ex2 = &newex; |
| 3195 | ex2->ee_block = cpu_to_le32(split); |
| 3196 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 3197 | ext4_ext_store_pblock(ex2, newblock); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3198 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3199 | ext4_ext_mark_unwritten(ex2); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3200 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3201 | err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3202 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3203 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3204 | if (split_flag & EXT4_EXT_DATA_VALID1) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3205 | err = ext4_ext_zeroout(inode, ex2); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3206 | zero_ex.ee_block = ex2->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3207 | zero_ex.ee_len = cpu_to_le16( |
| 3208 | ext4_ext_get_actual_len(ex2)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3209 | ext4_ext_store_pblock(&zero_ex, |
| 3210 | ext4_ext_pblock(ex2)); |
| 3211 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3212 | err = ext4_ext_zeroout(inode, ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3213 | zero_ex.ee_block = ex->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3214 | zero_ex.ee_len = cpu_to_le16( |
| 3215 | ext4_ext_get_actual_len(ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3216 | ext4_ext_store_pblock(&zero_ex, |
| 3217 | ext4_ext_pblock(ex)); |
| 3218 | } |
| 3219 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3220 | err = ext4_ext_zeroout(inode, &orig_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3221 | zero_ex.ee_block = orig_ex.ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3222 | zero_ex.ee_len = cpu_to_le16( |
| 3223 | ext4_ext_get_actual_len(&orig_ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3224 | ext4_ext_store_pblock(&zero_ex, |
| 3225 | ext4_ext_pblock(&orig_ex)); |
| 3226 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3227 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3228 | if (err) |
| 3229 | goto fix_extent_len; |
| 3230 | /* update the extent length and mark as initialized */ |
Al Viro | af1584f | 2012-04-12 20:32:25 -0400 | [diff] [blame] | 3231 | ex->ee_len = cpu_to_le16(ee_len); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3232 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3233 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3234 | if (err) |
| 3235 | goto fix_extent_len; |
| 3236 | |
| 3237 | /* update extent status tree */ |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3238 | err = ext4_zeroout_es(inode, &zero_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3239 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3240 | goto out; |
| 3241 | } else if (err) |
| 3242 | goto fix_extent_len; |
| 3243 | |
| 3244 | out: |
| 3245 | ext4_ext_show_leaf(inode, path); |
| 3246 | return err; |
| 3247 | |
| 3248 | fix_extent_len: |
| 3249 | ex->ee_len = orig_ex.ee_len; |
Dmitry Monakhov | 29faed1 | 2014-07-27 22:30:29 -0400 | [diff] [blame] | 3250 | ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3251 | return err; |
| 3252 | } |
| 3253 | |
| 3254 | /* |
| 3255 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 3256 | * by @map as split_flags indicates |
| 3257 | * |
Anatol Pomozov | 70261f5 | 2013-08-28 14:40:12 -0400 | [diff] [blame] | 3258 | * 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] | 3259 | * There are three possibilities: |
| 3260 | * a> There is no split required |
| 3261 | * b> Splits in two extents: Split is happening at either end of the extent |
| 3262 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 3263 | * |
| 3264 | */ |
| 3265 | static int ext4_split_extent(handle_t *handle, |
| 3266 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3267 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3268 | struct ext4_map_blocks *map, |
| 3269 | int split_flag, |
| 3270 | int flags) |
| 3271 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3272 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3273 | ext4_lblk_t ee_block; |
| 3274 | struct ext4_extent *ex; |
| 3275 | unsigned int ee_len, depth; |
| 3276 | int err = 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3277 | int unwritten; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3278 | int split_flag1, flags1; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3279 | int allocated = map->m_len; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3280 | |
| 3281 | depth = ext_depth(inode); |
| 3282 | ex = path[depth].p_ext; |
| 3283 | ee_block = le32_to_cpu(ex->ee_block); |
| 3284 | ee_len = ext4_ext_get_actual_len(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3285 | unwritten = ext4_ext_is_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3286 | |
| 3287 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3288 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3289 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3290 | if (unwritten) |
| 3291 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1 | |
| 3292 | EXT4_EXT_MARK_UNWRIT2; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3293 | if (split_flag & EXT4_EXT_DATA_VALID2) |
| 3294 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3295 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3296 | map->m_lblk + map->m_len, split_flag1, flags1); |
Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 3297 | if (err) |
| 3298 | goto out; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3299 | } else { |
| 3300 | allocated = ee_len - (map->m_lblk - ee_block); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3301 | } |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3302 | /* |
| 3303 | * Update path is required because previous ext4_split_extent_at() may |
| 3304 | * result in split of original leaf or extent zeroout. |
| 3305 | */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3306 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3307 | if (IS_ERR(path)) |
| 3308 | return PTR_ERR(path); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3309 | depth = ext_depth(inode); |
| 3310 | ex = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3311 | if (!ex) { |
| 3312 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3313 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3314 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3315 | } |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3316 | unwritten = ext4_ext_is_unwritten(ex); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3317 | split_flag1 = 0; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3318 | |
| 3319 | if (map->m_lblk >= ee_block) { |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3320 | split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3321 | if (unwritten) { |
| 3322 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1; |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3323 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3324 | EXT4_EXT_MARK_UNWRIT2); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3325 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3326 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3327 | map->m_lblk, split_flag1, flags); |
| 3328 | if (err) |
| 3329 | goto out; |
| 3330 | } |
| 3331 | |
| 3332 | ext4_ext_show_leaf(inode, path); |
| 3333 | out: |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3334 | return err ? err : allocated; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3335 | } |
| 3336 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3337 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3338 | * 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] | 3339 | * to an unwritten extent. It may result in splitting the unwritten |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3340 | * extent into multiple extents (up to three - one initialized and two |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3341 | * unwritten). |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3342 | * There are three possibilities: |
| 3343 | * a> There is no split required: Entire extent should be initialized |
| 3344 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3345 | * 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] | 3346 | * |
| 3347 | * Pre-conditions: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3348 | * - The extent pointed to by 'path' is unwritten. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3349 | * - The extent pointed to by 'path' contains a superset |
| 3350 | * of the logical span [map->m_lblk, map->m_lblk + map->m_len). |
| 3351 | * |
| 3352 | * Post-conditions on success: |
| 3353 | * - the returned value is the number of blocks beyond map->l_lblk |
| 3354 | * that are allocated and initialized. |
| 3355 | * It is guaranteed to be >= map->m_len. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3356 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3357 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3358 | struct inode *inode, |
| 3359 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3360 | struct ext4_ext_path **ppath, |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3361 | int flags) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3362 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3363 | struct ext4_ext_path *path = *ppath; |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3364 | struct ext4_sb_info *sbi; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3365 | struct ext4_extent_header *eh; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3366 | struct ext4_map_blocks split_map; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3367 | struct ext4_extent zero_ex1, zero_ex2; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3368 | struct ext4_extent *ex, *abut_ex; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3369 | ext4_lblk_t ee_block, eof_block; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3370 | unsigned int ee_len, depth, map_len = map->m_len; |
| 3371 | int allocated = 0, max_zeroout = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3372 | int err = 0; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3373 | int split_flag = EXT4_EXT_DATA_VALID2; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3374 | |
| 3375 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
| 3376 | "block %llu, max_blocks %u\n", inode->i_ino, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3377 | (unsigned long long)map->m_lblk, map_len); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3378 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3379 | sbi = EXT4_SB(inode->i_sb); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3380 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3381 | inode->i_sb->s_blocksize_bits; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3382 | if (eof_block < map->m_lblk + map_len) |
| 3383 | eof_block = map->m_lblk + map_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3384 | |
| 3385 | depth = ext_depth(inode); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3386 | eh = path[depth].p_hdr; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3387 | ex = path[depth].p_ext; |
| 3388 | ee_block = le32_to_cpu(ex->ee_block); |
| 3389 | ee_len = ext4_ext_get_actual_len(ex); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3390 | zero_ex1.ee_len = 0; |
| 3391 | zero_ex2.ee_len = 0; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3392 | |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3393 | trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); |
| 3394 | |
| 3395 | /* Pre-conditions */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3396 | BUG_ON(!ext4_ext_is_unwritten(ex)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3397 | BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3398 | |
| 3399 | /* |
| 3400 | * Attempt to transfer newly initialized blocks from the currently |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3401 | * unwritten extent to its neighbor. This is much cheaper |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3402 | * than an insertion followed by a merge as those involve costly |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3403 | * memmove() calls. Transferring to the left is the common case in |
| 3404 | * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) |
| 3405 | * followed by append writes. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3406 | * |
| 3407 | * Limitations of the current logic: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3408 | * - L1: we do not deal with writes covering the whole extent. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3409 | * This would require removing the extent if the transfer |
| 3410 | * is possible. |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3411 | * - L2: we only attempt to merge with an extent stored in the |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3412 | * same extent tree node. |
| 3413 | */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3414 | if ((map->m_lblk == ee_block) && |
| 3415 | /* See if we can merge left */ |
| 3416 | (map_len < ee_len) && /*L1*/ |
| 3417 | (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3418 | ext4_lblk_t prev_lblk; |
| 3419 | ext4_fsblk_t prev_pblk, ee_pblk; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3420 | unsigned int prev_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3421 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3422 | abut_ex = ex - 1; |
| 3423 | prev_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3424 | prev_len = ext4_ext_get_actual_len(abut_ex); |
| 3425 | prev_pblk = ext4_ext_pblock(abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3426 | ee_pblk = ext4_ext_pblock(ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3427 | |
| 3428 | /* |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3429 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3430 | * upon those conditions: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3431 | * - C1: abut_ex is initialized, |
| 3432 | * - C2: abut_ex is logically abutting ex, |
| 3433 | * - C3: abut_ex is physically abutting ex, |
| 3434 | * - C4: abut_ex can receive the additional blocks without |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3435 | * overflowing the (initialized) length limit. |
| 3436 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3437 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3438 | ((prev_lblk + prev_len) == ee_block) && /*C2*/ |
| 3439 | ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3440 | (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3441 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3442 | if (err) |
| 3443 | goto out; |
| 3444 | |
| 3445 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3446 | map, ex, abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3447 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3448 | /* Shift the start of ex by 'map_len' blocks */ |
| 3449 | ex->ee_block = cpu_to_le32(ee_block + map_len); |
| 3450 | ext4_ext_store_pblock(ex, ee_pblk + map_len); |
| 3451 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3452 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3453 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3454 | /* Extend abut_ex by 'map_len' blocks */ |
| 3455 | abut_ex->ee_len = cpu_to_le16(prev_len + map_len); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3456 | |
| 3457 | /* Result: number of initialized blocks past m_lblk */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3458 | allocated = map_len; |
| 3459 | } |
| 3460 | } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && |
| 3461 | (map_len < ee_len) && /*L1*/ |
| 3462 | ex < EXT_LAST_EXTENT(eh)) { /*L2*/ |
| 3463 | /* See if we can merge right */ |
| 3464 | ext4_lblk_t next_lblk; |
| 3465 | ext4_fsblk_t next_pblk, ee_pblk; |
| 3466 | unsigned int next_len; |
| 3467 | |
| 3468 | abut_ex = ex + 1; |
| 3469 | next_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3470 | next_len = ext4_ext_get_actual_len(abut_ex); |
| 3471 | next_pblk = ext4_ext_pblock(abut_ex); |
| 3472 | ee_pblk = ext4_ext_pblock(ex); |
| 3473 | |
| 3474 | /* |
| 3475 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
| 3476 | * upon those conditions: |
| 3477 | * - C1: abut_ex is initialized, |
| 3478 | * - C2: abut_ex is logically abutting ex, |
| 3479 | * - C3: abut_ex is physically abutting ex, |
| 3480 | * - C4: abut_ex can receive the additional blocks without |
| 3481 | * overflowing the (initialized) length limit. |
| 3482 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3483 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3484 | ((map->m_lblk + map_len) == next_lblk) && /*C2*/ |
| 3485 | ((ee_pblk + ee_len) == next_pblk) && /*C3*/ |
| 3486 | (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
| 3487 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3488 | if (err) |
| 3489 | goto out; |
| 3490 | |
| 3491 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
| 3492 | map, ex, abut_ex); |
| 3493 | |
| 3494 | /* Shift the start of abut_ex by 'map_len' blocks */ |
| 3495 | abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); |
| 3496 | ext4_ext_store_pblock(abut_ex, next_pblk - map_len); |
| 3497 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3498 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3499 | |
| 3500 | /* Extend abut_ex by 'map_len' blocks */ |
| 3501 | abut_ex->ee_len = cpu_to_le16(next_len + map_len); |
| 3502 | |
| 3503 | /* Result: number of initialized blocks past m_lblk */ |
| 3504 | allocated = map_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3505 | } |
| 3506 | } |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3507 | if (allocated) { |
| 3508 | /* Mark the block containing both extents as dirty */ |
| 3509 | ext4_ext_dirty(handle, inode, path + depth); |
| 3510 | |
| 3511 | /* Update path to point to the right extent */ |
| 3512 | path[depth].p_ext = abut_ex; |
| 3513 | goto out; |
| 3514 | } else |
| 3515 | allocated = ee_len - (map->m_lblk - ee_block); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3516 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3517 | WARN_ON(map->m_lblk < ee_block); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3518 | /* |
| 3519 | * It is safe to convert extent to initialized via explicit |
Yongqiang Yang | 9e74056 | 2014-01-06 14:05:23 -0500 | [diff] [blame] | 3520 | * zeroout only if extent is fully inside i_size or new_size. |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3521 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3522 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3523 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3524 | if (EXT4_EXT_MAY_ZEROOUT & split_flag) |
| 3525 | max_zeroout = sbi->s_extent_max_zeroout_kb >> |
Lukas Czerner | 4f42f80 | 2013-03-12 12:40:04 -0400 | [diff] [blame] | 3526 | (inode->i_sb->s_blocksize_bits - 10); |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3527 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3528 | /* |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3529 | * five cases: |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3530 | * 1. split the extent into three extents. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3531 | * 2. split the extent into two extents, zeroout the head of the first |
| 3532 | * extent. |
| 3533 | * 3. split the extent into two extents, zeroout the tail of the second |
| 3534 | * extent. |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3535 | * 4. split the extent into two extents with out zeroout. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3536 | * 5. no splitting needed, just possibly zeroout the head and / or the |
| 3537 | * tail of the extent. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3538 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3539 | split_map.m_lblk = map->m_lblk; |
| 3540 | split_map.m_len = map->m_len; |
| 3541 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3542 | if (max_zeroout && (allocated > split_map.m_len)) { |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3543 | if (allocated <= max_zeroout) { |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3544 | /* case 3 or 5 */ |
| 3545 | zero_ex1.ee_block = |
| 3546 | cpu_to_le32(split_map.m_lblk + |
| 3547 | split_map.m_len); |
| 3548 | zero_ex1.ee_len = |
| 3549 | cpu_to_le16(allocated - split_map.m_len); |
| 3550 | ext4_ext_store_pblock(&zero_ex1, |
| 3551 | ext4_ext_pblock(ex) + split_map.m_lblk + |
| 3552 | split_map.m_len - ee_block); |
| 3553 | err = ext4_ext_zeroout(inode, &zero_ex1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3554 | if (err) |
| 3555 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3556 | split_map.m_len = allocated; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3557 | } |
| 3558 | if (split_map.m_lblk - ee_block + split_map.m_len < |
| 3559 | max_zeroout) { |
| 3560 | /* case 2 or 5 */ |
| 3561 | if (split_map.m_lblk != ee_block) { |
| 3562 | zero_ex2.ee_block = ex->ee_block; |
| 3563 | zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3564 | ee_block); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3565 | ext4_ext_store_pblock(&zero_ex2, |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3566 | ext4_ext_pblock(ex)); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3567 | err = ext4_ext_zeroout(inode, &zero_ex2); |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3568 | if (err) |
| 3569 | goto out; |
| 3570 | } |
| 3571 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3572 | split_map.m_len += split_map.m_lblk - ee_block; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3573 | split_map.m_lblk = ee_block; |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3574 | allocated = map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3575 | } |
| 3576 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3577 | |
Jan Kara | ae9e9c6a | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 3578 | err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, |
| 3579 | flags); |
| 3580 | if (err > 0) |
| 3581 | err = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3582 | out: |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3583 | /* If we have gotten a failure, don't zero out status tree */ |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3584 | if (!err) { |
| 3585 | err = ext4_zeroout_es(inode, &zero_ex1); |
| 3586 | if (!err) |
| 3587 | err = ext4_zeroout_es(inode, &zero_ex2); |
| 3588 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3589 | return err ? err : allocated; |
| 3590 | } |
| 3591 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3592 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3593 | * This function is called by ext4_ext_map_blocks() from |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3594 | * ext4_get_blocks_dio_write() when DIO to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3595 | * to an unwritten extent. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3596 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3597 | * Writing to an unwritten extent may result in splitting the unwritten |
| 3598 | * extent into multiple initialized/unwritten extents (up to three) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3599 | * There are three possibilities: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3600 | * a> There is no split required: Entire extent should be unwritten |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3601 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3602 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3603 | * |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3604 | * This works the same way in the case of initialized -> unwritten conversion. |
| 3605 | * |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3606 | * 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] | 3607 | * the unwritten extent split. To prevent ENOSPC occur at the IO |
| 3608 | * complete, we need to split the unwritten extent before DIO submit |
| 3609 | * the IO. The unwritten extent called at this time will be split |
| 3610 | * into three unwritten extent(at most). After IO complete, the part |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3611 | * being filled will be convert to initialized by the end_io callback function |
| 3612 | * via ext4_convert_unwritten_extents(). |
Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3613 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3614 | * Returns the size of unwritten extent to be written on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3615 | */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3616 | static int ext4_split_convert_extents(handle_t *handle, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3617 | struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3618 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3619 | struct ext4_ext_path **ppath, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3620 | int flags) |
| 3621 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3622 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3623 | ext4_lblk_t eof_block; |
| 3624 | ext4_lblk_t ee_block; |
| 3625 | struct ext4_extent *ex; |
| 3626 | unsigned int ee_len; |
| 3627 | int split_flag = 0, depth; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3628 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3629 | ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n", |
| 3630 | __func__, inode->i_ino, |
| 3631 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3632 | |
| 3633 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3634 | inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3635 | if (eof_block < map->m_lblk + map->m_len) |
| 3636 | eof_block = map->m_lblk + map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3637 | /* |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3638 | * It is safe to convert extent to initialized via explicit |
| 3639 | * zeroout only if extent is fully insde i_size or new_size. |
| 3640 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3641 | depth = ext_depth(inode); |
| 3642 | ex = path[depth].p_ext; |
| 3643 | ee_block = le32_to_cpu(ex->ee_block); |
| 3644 | ee_len = ext4_ext_get_actual_len(ex); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3645 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3646 | /* Convert to unwritten */ |
| 3647 | if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { |
| 3648 | split_flag |= EXT4_EXT_DATA_VALID1; |
| 3649 | /* Convert to initialized */ |
| 3650 | } else if (flags & EXT4_GET_BLOCKS_CONVERT) { |
| 3651 | split_flag |= ee_block + ee_len <= eof_block ? |
| 3652 | EXT4_EXT_MAY_ZEROOUT : 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3653 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3654 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3655 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3656 | return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3657 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3658 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3659 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3660 | struct inode *inode, |
| 3661 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3662 | struct ext4_ext_path **ppath) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3663 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3664 | struct ext4_ext_path *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3665 | struct ext4_extent *ex; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3666 | ext4_lblk_t ee_block; |
| 3667 | unsigned int ee_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3668 | int depth; |
| 3669 | int err = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3670 | |
| 3671 | depth = ext_depth(inode); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3672 | ex = path[depth].p_ext; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3673 | ee_block = le32_to_cpu(ex->ee_block); |
| 3674 | ee_len = ext4_ext_get_actual_len(ex); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3675 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3676 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
| 3677 | "block %llu, max_blocks %u\n", inode->i_ino, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3678 | (unsigned long long)ee_block, ee_len); |
| 3679 | |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3680 | /* If extent is larger than requested it is a clear sign that we still |
| 3681 | * have some extent state machine issues left. So extent_split is still |
| 3682 | * required. |
| 3683 | * TODO: Once all related issues will be fixed this situation should be |
| 3684 | * illegal. |
| 3685 | */ |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3686 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Rakesh Pandit | e3d550c | 2019-08-22 22:53:46 -0400 | [diff] [blame] | 3687 | #ifdef CONFIG_EXT4_DEBUG |
| 3688 | ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu," |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 3689 | " len %u; IO logical block %llu, len %u", |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3690 | inode->i_ino, (unsigned long long)ee_block, ee_len, |
| 3691 | (unsigned long long)map->m_lblk, map->m_len); |
| 3692 | #endif |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3693 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3694 | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3695 | if (err < 0) |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3696 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3697 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3698 | if (IS_ERR(path)) |
| 3699 | return PTR_ERR(path); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3700 | depth = ext_depth(inode); |
| 3701 | ex = path[depth].p_ext; |
| 3702 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3703 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3704 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3705 | if (err) |
| 3706 | goto out; |
| 3707 | /* first mark the extent as initialized */ |
| 3708 | ext4_ext_mark_initialized(ex); |
| 3709 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3710 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3711 | * borders are not changed |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3712 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3713 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3714 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3715 | /* Mark modified extent as dirty */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3716 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3717 | out: |
| 3718 | ext4_ext_show_leaf(inode, path); |
| 3719 | return err; |
| 3720 | } |
| 3721 | |
| 3722 | static int |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3723 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
| 3724 | struct ext4_map_blocks *map, |
Eric Whitney | 29c6eaf | 2016-02-22 22:58:55 -0500 | [diff] [blame] | 3725 | struct ext4_ext_path **ppath, |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 3726 | unsigned int *allocated) |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3727 | { |
Theodore Ts'o | 4f224b8 | 2014-09-01 14:36:09 -0400 | [diff] [blame] | 3728 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3729 | struct ext4_extent *ex; |
| 3730 | ext4_lblk_t ee_block; |
| 3731 | unsigned int ee_len; |
| 3732 | int depth; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3733 | int err = 0; |
| 3734 | |
| 3735 | /* |
| 3736 | * Make sure that the extent is no bigger than we support with |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3737 | * unwritten extent |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3738 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3739 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) |
| 3740 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3741 | |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3742 | depth = ext_depth(inode); |
| 3743 | ex = path[depth].p_ext; |
| 3744 | ee_block = le32_to_cpu(ex->ee_block); |
| 3745 | ee_len = ext4_ext_get_actual_len(ex); |
| 3746 | |
| 3747 | ext_debug("%s: inode %lu, logical" |
| 3748 | "block %llu, max_blocks %u\n", __func__, inode->i_ino, |
| 3749 | (unsigned long long)ee_block, ee_len); |
| 3750 | |
| 3751 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3752 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3753 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); |
| 3754 | if (err < 0) |
| 3755 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3756 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3757 | if (IS_ERR(path)) |
| 3758 | return PTR_ERR(path); |
| 3759 | depth = ext_depth(inode); |
| 3760 | ex = path[depth].p_ext; |
| 3761 | if (!ex) { |
| 3762 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3763 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3764 | return -EFSCORRUPTED; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3769 | if (err) |
| 3770 | return err; |
| 3771 | /* first mark the extent as unwritten */ |
| 3772 | ext4_ext_mark_unwritten(ex); |
| 3773 | |
| 3774 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3775 | * borders are not changed |
| 3776 | */ |
| 3777 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3778 | |
| 3779 | /* Mark modified extent as dirty */ |
| 3780 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
| 3781 | if (err) |
| 3782 | return err; |
| 3783 | ext4_ext_show_leaf(inode, path); |
| 3784 | |
| 3785 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3786 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3787 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 3788 | if (*allocated > map->m_len) |
| 3789 | *allocated = map->m_len; |
| 3790 | map->m_len = *allocated; |
| 3791 | return 0; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | static int |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3795 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3796 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3797 | struct ext4_ext_path **ppath, int flags, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3798 | unsigned int allocated, ext4_fsblk_t newblock) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3799 | { |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3800 | #ifdef EXT_DEBUG |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3801 | struct ext4_ext_path *path = *ppath; |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3802 | #endif |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3803 | int ret = 0; |
| 3804 | int err = 0; |
| 3805 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3806 | ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical " |
Zheng Liu | 88635ca | 2011-12-28 19:00:25 -0500 | [diff] [blame] | 3807 | "block %llu, max_blocks %u, flags %x, allocated %u\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3808 | inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3809 | flags, allocated); |
| 3810 | ext4_ext_show_leaf(inode, path); |
| 3811 | |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3812 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3813 | * When writing into unwritten space, we should not fail to |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3814 | * allocate metadata blocks for the new extent block if needed. |
| 3815 | */ |
| 3816 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; |
| 3817 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3818 | trace_ext4_ext_handle_unwritten_extents(inode, map, flags, |
Zheng Liu | b564553 | 2012-11-08 14:33:43 -0500 | [diff] [blame] | 3819 | allocated, newblock); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3820 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3821 | /* get_block() before submit the IO, split the extent */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3822 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3823 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
| 3824 | flags | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 3825 | if (ret <= 0) |
| 3826 | goto out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3827 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3828 | goto out; |
| 3829 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3830 | /* IO end_io complete, convert the filled extent to written */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3831 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
Jan Kara | c86d8db | 2015-12-07 15:10:26 -0500 | [diff] [blame] | 3832 | if (flags & EXT4_GET_BLOCKS_ZERO) { |
| 3833 | if (allocated > map->m_len) |
| 3834 | allocated = map->m_len; |
| 3835 | err = ext4_issue_zeroout(inode, map->m_lblk, newblock, |
| 3836 | allocated); |
| 3837 | if (err < 0) |
| 3838 | goto out2; |
| 3839 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3840 | ret = ext4_convert_unwritten_extents_endio(handle, inode, map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3841 | ppath); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3842 | if (ret >= 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3843 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3844 | else |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3845 | err = ret; |
Zheng Liu | cdee784 | 2013-03-10 21:08:52 -0400 | [diff] [blame] | 3846 | map->m_flags |= EXT4_MAP_MAPPED; |
Eric Whitney | 15cc176 | 2014-02-12 10:42:45 -0500 | [diff] [blame] | 3847 | map->m_pblk = newblock; |
Zheng Liu | cdee784 | 2013-03-10 21:08:52 -0400 | [diff] [blame] | 3848 | if (allocated > map->m_len) |
| 3849 | allocated = map->m_len; |
| 3850 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3851 | goto out2; |
| 3852 | } |
| 3853 | /* buffered IO case */ |
| 3854 | /* |
| 3855 | * repeat fallocate creation request |
| 3856 | * we already have an unwritten extent |
| 3857 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3858 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3859 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3860 | goto map_out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3861 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3862 | |
| 3863 | /* buffered READ or buffered write_begin() lookup */ |
| 3864 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3865 | /* |
| 3866 | * We have blocks reserved already. We |
| 3867 | * return allocated blocks so that delalloc |
| 3868 | * won't do block reservation for us. But |
| 3869 | * the buffer head will be unmapped so that |
| 3870 | * a read from the block returns 0s. |
| 3871 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3872 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3873 | goto out1; |
| 3874 | } |
| 3875 | |
| 3876 | /* buffered write, writepage time, convert*/ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3877 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 3878 | if (ret >= 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3879 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3880 | out: |
| 3881 | if (ret <= 0) { |
| 3882 | err = ret; |
| 3883 | goto out2; |
| 3884 | } else |
| 3885 | allocated = ret; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3886 | map->m_flags |= EXT4_MAP_NEW; |
zhangyi (F) | 16e08b1 | 2019-02-10 23:32:07 -0500 | [diff] [blame] | 3887 | if (allocated > map->m_len) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3888 | allocated = map->m_len; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3889 | map->m_len = allocated; |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3890 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3891 | map_out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3892 | map->m_flags |= EXT4_MAP_MAPPED; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3893 | out1: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3894 | if (allocated > map->m_len) |
| 3895 | allocated = map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3896 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3897 | map->m_pblk = newblock; |
| 3898 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3899 | out2: |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3900 | return err ? err : allocated; |
| 3901 | } |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3902 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3903 | /* |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3904 | * get_implied_cluster_alloc - check to see if the requested |
| 3905 | * allocation (in the map structure) overlaps with a cluster already |
| 3906 | * allocated in an extent. |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3907 | * @sb The filesystem superblock structure |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3908 | * @map The requested lblk->pblk mapping |
| 3909 | * @ex The extent structure which might contain an implied |
| 3910 | * cluster allocation |
| 3911 | * |
| 3912 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 3913 | * find blocks that were already in the inode's extent tree. Hence, |
| 3914 | * we know that the beginning of the requested region cannot overlap |
| 3915 | * the extent from the inode's extent tree. There are three cases we |
| 3916 | * want to catch. The first is this case: |
| 3917 | * |
| 3918 | * |--- cluster # N--| |
| 3919 | * |--- extent ---| |---- requested region ---| |
| 3920 | * |==========| |
| 3921 | * |
| 3922 | * The second case that we need to test for is this one: |
| 3923 | * |
| 3924 | * |--------- cluster # N ----------------| |
| 3925 | * |--- requested region --| |------- extent ----| |
| 3926 | * |=======================| |
| 3927 | * |
| 3928 | * The third case is when the requested region lies between two extents |
| 3929 | * within the same cluster: |
| 3930 | * |------------- cluster # N-------------| |
| 3931 | * |----- ex -----| |---- ex_right ----| |
| 3932 | * |------ requested region ------| |
| 3933 | * |================| |
| 3934 | * |
| 3935 | * In each of the above cases, we need to set the map->m_pblk and |
| 3936 | * map->m_len so it corresponds to the return the extent labelled as |
| 3937 | * "|====|" from cluster #N, since it is already in use for data in |
| 3938 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 3939 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 3940 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 3941 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 3942 | * by calling ext4_mb_new_blocks(). |
| 3943 | */ |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3944 | static int get_implied_cluster_alloc(struct super_block *sb, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3945 | struct ext4_map_blocks *map, |
| 3946 | struct ext4_extent *ex, |
| 3947 | struct ext4_ext_path *path) |
| 3948 | { |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3949 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3950 | 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] | 3951 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
Curt Wohlgemuth | 14d7f3e | 2011-12-18 17:39:02 -0500 | [diff] [blame] | 3952 | ext4_lblk_t rr_cluster_start; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3953 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 3954 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 3955 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 3956 | |
| 3957 | /* The extent passed in that we are trying to match */ |
| 3958 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 3959 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 3960 | |
| 3961 | /* The requested region passed into ext4_map_blocks() */ |
| 3962 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3963 | |
| 3964 | if ((rr_cluster_start == ex_cluster_end) || |
| 3965 | (rr_cluster_start == ex_cluster_start)) { |
| 3966 | if (rr_cluster_start == ex_cluster_end) |
| 3967 | ee_start += ee_len - 1; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3968 | map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3969 | map->m_len = min(map->m_len, |
| 3970 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 3971 | /* |
| 3972 | * Check for and handle this case: |
| 3973 | * |
| 3974 | * |--------- cluster # N-------------| |
| 3975 | * |------- extent ----| |
| 3976 | * |--- requested region ---| |
| 3977 | * |===========| |
| 3978 | */ |
| 3979 | |
| 3980 | if (map->m_lblk < ee_block) |
| 3981 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 3982 | |
| 3983 | /* |
| 3984 | * Check for the case where there is already another allocated |
| 3985 | * block to the right of 'ex' but before the end of the cluster. |
| 3986 | * |
| 3987 | * |------------- cluster # N-------------| |
| 3988 | * |----- ex -----| |---- ex_right ----| |
| 3989 | * |------ requested region ------| |
| 3990 | * |================| |
| 3991 | */ |
| 3992 | if (map->m_lblk > ee_block) { |
| 3993 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 3994 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 3995 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3996 | |
| 3997 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3998 | return 1; |
| 3999 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4000 | |
| 4001 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4002 | return 0; |
| 4003 | } |
| 4004 | |
| 4005 | |
| 4006 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 4007 | * Block allocation/map/preallocation routine for extents based files |
| 4008 | * |
| 4009 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4010 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4011 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 4012 | * (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] | 4013 | * |
| 4014 | * return > 0, number of of blocks already mapped/allocated |
| 4015 | * if create == 0 and these are pre-allocated blocks |
| 4016 | * buffer head is unmapped |
| 4017 | * otherwise blocks are mapped |
| 4018 | * |
| 4019 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 4020 | * buffer head is unmapped |
| 4021 | * |
| 4022 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4023 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4024 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 4025 | struct ext4_map_blocks *map, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4026 | { |
| 4027 | struct ext4_ext_path *path = NULL; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4028 | struct ext4_extent newex, *ex, *ex2; |
| 4029 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4030 | ext4_fsblk_t newblock = 0; |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4031 | int err = 0, depth, ret; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4032 | unsigned int allocated = 0, offset = 0; |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4033 | unsigned int allocated_clusters = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4034 | struct ext4_allocation_request ar; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4035 | ext4_lblk_t cluster_offset; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4036 | |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4037 | ext_debug("blocks %u/%u requested for inode %lu\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4038 | map->m_lblk, map->m_len, inode->i_ino); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4039 | 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] | 4040 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4041 | /* find extent for this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4042 | path = ext4_find_extent(inode, map->m_lblk, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4043 | if (IS_ERR(path)) { |
| 4044 | err = PTR_ERR(path); |
| 4045 | path = NULL; |
| 4046 | goto out2; |
| 4047 | } |
| 4048 | |
| 4049 | depth = ext_depth(inode); |
| 4050 | |
| 4051 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4052 | * consistent leaf must not be empty; |
| 4053 | * this situation is possible, though, _during_ tree modification; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4054 | * this is why assert can't be put in ext4_find_extent() |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4055 | */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 4056 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 4057 | EXT4_ERROR_INODE(inode, "bad extent address " |
Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 4058 | "lblock: %lu, depth: %d pblock %lld", |
| 4059 | (unsigned long) map->m_lblk, depth, |
| 4060 | path[depth].p_block); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4061 | err = -EFSCORRUPTED; |
Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 4062 | goto out2; |
| 4063 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4064 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 4065 | ex = path[depth].p_ext; |
| 4066 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4067 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4068 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4069 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4070 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4071 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4072 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4073 | * unwritten extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4074 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4075 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4076 | ee_len = ext4_ext_get_actual_len(ex); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4077 | |
| 4078 | trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); |
| 4079 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4080 | /* if found extent covers block, simply return it */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4081 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
| 4082 | newblock = map->m_lblk - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4083 | /* number of remaining blocks in the extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4084 | allocated = ee_len - (map->m_lblk - ee_block); |
| 4085 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
| 4086 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4087 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4088 | /* |
| 4089 | * If the extent is initialized check whether the |
| 4090 | * caller wants to convert it to unwritten. |
| 4091 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4092 | if ((!ext4_ext_is_unwritten(ex)) && |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4093 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4094 | err = convert_initialized_extent(handle, |
| 4095 | inode, map, &path, &allocated); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4096 | goto out2; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4097 | } else if (!ext4_ext_is_unwritten(ex)) { |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4098 | goto out; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4099 | } |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4100 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4101 | ret = ext4_ext_handle_unwritten_extents( |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4102 | handle, inode, map, &path, flags, |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4103 | allocated, newblock); |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4104 | if (ret < 0) |
| 4105 | err = ret; |
| 4106 | else |
| 4107 | allocated = ret; |
Eric Whitney | 31cf0f2 | 2014-03-13 23:14:46 -0400 | [diff] [blame] | 4108 | goto out2; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4113 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4114 | * we couldn't try to create block if create flag is zero |
| 4115 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 4116 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4117 | ext4_lblk_t hole_start, hole_len; |
| 4118 | |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4119 | hole_start = map->m_lblk; |
| 4120 | hole_len = ext4_ext_determine_hole(inode, path, &hole_start); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4121 | /* |
| 4122 | * put just found gap into cache to speed up |
| 4123 | * subsequent requests |
| 4124 | */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4125 | ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4126 | |
| 4127 | /* Update hole_len to reflect hole size after map->m_lblk */ |
| 4128 | if (hole_start != map->m_lblk) |
| 4129 | hole_len -= map->m_lblk - hole_start; |
| 4130 | map->m_pblk = 0; |
| 4131 | map->m_len = min_t(unsigned int, map->m_len, hole_len); |
| 4132 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4133 | goto out2; |
| 4134 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4135 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4136 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4137 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 4138 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4139 | newex.ee_block = cpu_to_le32(map->m_lblk); |
Eric Whitney | d0abafa | 2014-01-06 14:00:23 -0500 | [diff] [blame] | 4140 | cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4141 | |
| 4142 | /* |
| 4143 | * 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] | 4144 | * by ext4_find_extent() implies a cluster we can use. |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4145 | */ |
| 4146 | if (cluster_offset && ex && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4147 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4148 | ar.len = allocated = map->m_len; |
| 4149 | newblock = map->m_pblk; |
| 4150 | goto got_allocated_blocks; |
| 4151 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4152 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4153 | /* find neighbour allocated blocks */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4154 | ar.lleft = map->m_lblk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4155 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 4156 | if (err) |
| 4157 | goto out2; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4158 | ar.lright = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4159 | ex2 = NULL; |
| 4160 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4161 | if (err) |
| 4162 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4163 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4164 | /* Check if the extent after searching to the right implies a |
| 4165 | * cluster we can use. */ |
| 4166 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4167 | get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4168 | ar.len = allocated = map->m_len; |
| 4169 | newblock = map->m_pblk; |
| 4170 | goto got_allocated_blocks; |
| 4171 | } |
| 4172 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4173 | /* |
| 4174 | * See if request is beyond maximum number of blocks we can have in |
| 4175 | * a single extent. For an initialized extent this limit is |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4176 | * EXT_INIT_MAX_LEN and for an unwritten extent this limit is |
| 4177 | * EXT_UNWRITTEN_MAX_LEN. |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4178 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4179 | if (map->m_len > EXT_INIT_MAX_LEN && |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4180 | !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4181 | map->m_len = EXT_INIT_MAX_LEN; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4182 | else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && |
| 4183 | (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
| 4184 | map->m_len = EXT_UNWRITTEN_MAX_LEN; |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4185 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4186 | /* 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] | 4187 | newex.ee_len = cpu_to_le16(map->m_len); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4188 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4189 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4190 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4191 | else |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4192 | allocated = map->m_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4193 | |
| 4194 | /* allocate new block */ |
| 4195 | ar.inode = inode; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4196 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 4197 | ar.logical = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4198 | /* |
| 4199 | * We calculate the offset from the beginning of the cluster |
| 4200 | * for the logical block number, since when we allocate a |
| 4201 | * physical cluster, the physical block should start at the |
| 4202 | * same offset from the beginning of the cluster. This is |
| 4203 | * needed so that future calls to get_implied_cluster_alloc() |
| 4204 | * work correctly. |
| 4205 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4206 | offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4207 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 4208 | ar.goal -= offset; |
| 4209 | ar.logical -= offset; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4210 | if (S_ISREG(inode->i_mode)) |
| 4211 | ar.flags = EXT4_MB_HINT_DATA; |
| 4212 | else |
| 4213 | /* disable in-core preallocation for non-regular files */ |
| 4214 | ar.flags = 0; |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4215 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 4216 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4217 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4218 | ar.flags |= EXT4_MB_DELALLOC_RESERVED; |
Theodore Ts'o | c5e298a | 2015-06-21 01:25:29 -0400 | [diff] [blame] | 4219 | if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
| 4220 | ar.flags |= EXT4_MB_USE_RESERVED; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4221 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4222 | if (!newblock) |
| 4223 | goto out2; |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4224 | ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4225 | ar.goal, newblock, allocated); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4226 | allocated_clusters = ar.len; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4227 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
| 4228 | if (ar.len > allocated) |
| 4229 | ar.len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4230 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4231 | got_allocated_blocks: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4232 | /* try to insert new extent into found leaf and return */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4233 | ext4_ext_store_pblock(&newex, newblock + offset); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4234 | newex.ee_len = cpu_to_le16(ar.len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4235 | /* Mark unwritten */ |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4236 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4237 | ext4_ext_mark_unwritten(&newex); |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4238 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4239 | } |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4240 | |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4241 | err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags); |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4242 | if (err) { |
| 4243 | if (allocated_clusters) { |
| 4244 | int fb_flags = 0; |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 4245 | |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4246 | /* |
| 4247 | * free data blocks we just allocated. |
| 4248 | * not a good idea to call discard here directly, |
| 4249 | * but otherwise we'd need to call it every free(). |
| 4250 | */ |
| 4251 | ext4_discard_preallocations(inode); |
| 4252 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4253 | fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; |
| 4254 | ext4_free_blocks(handle, inode, NULL, newblock, |
| 4255 | EXT4_C2B(sbi, allocated_clusters), |
| 4256 | fb_flags); |
| 4257 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4258 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4259 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4260 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4261 | /* previous routine could use block we allocated */ |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4262 | newblock = ext4_ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4263 | allocated = ext4_ext_get_actual_len(&newex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4264 | if (allocated > map->m_len) |
| 4265 | allocated = map->m_len; |
| 4266 | map->m_flags |= EXT4_MAP_NEW; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4267 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4268 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4269 | * Reduce the reserved cluster count to reflect successful deferred |
| 4270 | * allocation of delayed allocated clusters or direct allocation of |
| 4271 | * clusters discovered to be delayed allocated. Once allocated, a |
| 4272 | * cluster is not included in the reserved count. |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4273 | */ |
Eric Whitney | 2971148 | 2020-03-11 16:51:25 -0400 | [diff] [blame^] | 4274 | if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) { |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4275 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4276 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4277 | * When allocating delayed allocated clusters, simply |
| 4278 | * reduce the reserved cluster count and claim quota |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4279 | */ |
| 4280 | ext4_da_update_reserve_space(inode, allocated_clusters, |
| 4281 | 1); |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4282 | } else { |
| 4283 | ext4_lblk_t lblk, len; |
| 4284 | unsigned int n; |
| 4285 | |
| 4286 | /* |
| 4287 | * When allocating non-delayed allocated clusters |
| 4288 | * (from fallocate, filemap, DIO, or clusters |
| 4289 | * allocated when delalloc has been disabled by |
| 4290 | * ext4_nonda_switch), reduce the reserved cluster |
| 4291 | * count by the number of allocated clusters that |
| 4292 | * have previously been delayed allocated. Quota |
| 4293 | * has been claimed by ext4_mb_new_blocks() above, |
| 4294 | * so release the quota reservations made for any |
| 4295 | * previously delayed allocated clusters. |
| 4296 | */ |
| 4297 | lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk); |
| 4298 | len = allocated_clusters << sbi->s_cluster_bits; |
| 4299 | n = ext4_es_delayed_clu(inode, lblk, len); |
| 4300 | if (n > 0) |
| 4301 | ext4_da_update_reserve_space(inode, (int) n, 0); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4302 | } |
| 4303 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4304 | |
| 4305 | /* |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4306 | * Cache the extent and update transaction to commit on fdatasync only |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4307 | * when it is _not_ an unwritten extent. |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4308 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4309 | if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4310 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4311 | else |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4312 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4313 | out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4314 | if (allocated > map->m_len) |
| 4315 | allocated = map->m_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4316 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4317 | map->m_flags |= EXT4_MAP_MAPPED; |
| 4318 | map->m_pblk = newblock; |
| 4319 | map->m_len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4320 | out2: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 4321 | ext4_ext_drop_refs(path); |
| 4322 | kfree(path); |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4323 | |
Theodore Ts'o | 63b9996 | 2013-07-16 10:28:47 -0400 | [diff] [blame] | 4324 | trace_ext4_ext_map_blocks_exit(inode, flags, map, |
| 4325 | err ? err : allocated); |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4326 | return err ? err : allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4327 | } |
| 4328 | |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4329 | int ext4_ext_truncate(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4330 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4331 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4332 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4333 | int err = 0; |
| 4334 | |
| 4335 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4336 | * TODO: optimization is possible here. |
| 4337 | * Probably we need not scan at all, |
| 4338 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4339 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4340 | |
| 4341 | /* we have to know where to truncate from in crash case */ |
| 4342 | EXT4_I(inode)->i_disksize = inode->i_size; |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4343 | err = ext4_mark_inode_dirty(handle, inode); |
| 4344 | if (err) |
| 4345 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4346 | |
| 4347 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 4348 | >> EXT4_BLOCK_SIZE_BITS(sb); |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4349 | retry: |
Zheng Liu | 51865fd | 2012-11-08 21:57:32 -0500 | [diff] [blame] | 4350 | err = ext4_es_remove_extent(inode, last_block, |
| 4351 | EXT_MAX_BLOCKS - last_block); |
Theodore Ts'o | 94eec0f | 2013-07-29 12:12:56 -0400 | [diff] [blame] | 4352 | if (err == -ENOMEM) { |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4353 | cond_resched(); |
| 4354 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4355 | goto retry; |
| 4356 | } |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4357 | if (err) |
| 4358 | return err; |
| 4359 | return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4360 | } |
| 4361 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4362 | 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] | 4363 | ext4_lblk_t len, loff_t new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4364 | int flags) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4365 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4366 | struct inode *inode = file_inode(file); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4367 | handle_t *handle; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4368 | int ret = 0; |
| 4369 | int ret2 = 0; |
| 4370 | int retries = 0; |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4371 | int depth = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4372 | struct ext4_map_blocks map; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4373 | unsigned int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4374 | loff_t epos; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4375 | |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4376 | BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4377 | map.m_lblk = offset; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4378 | map.m_len = len; |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4379 | /* |
| 4380 | * Don't normalize the request if it can fit in one extent so |
| 4381 | * that it doesn't get unnecessarily split into multiple |
| 4382 | * extents. |
| 4383 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4384 | if (len <= EXT_UNWRITTEN_MAX_LEN) |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4385 | flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; |
Dmitry Monakhov | 60d4616 | 2012-10-05 11:32:02 -0400 | [diff] [blame] | 4386 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4387 | /* |
| 4388 | * credits to insert 1 extent into extent tree |
| 4389 | */ |
| 4390 | credits = ext4_chunk_trans_blocks(inode, len); |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4391 | depth = ext_depth(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4392 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4393 | retry: |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4394 | while (ret >= 0 && len) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4395 | /* |
| 4396 | * Recalculate credits when extent tree depth changes. |
| 4397 | */ |
Dan Carpenter | 011c88e | 2016-12-03 16:46:58 -0500 | [diff] [blame] | 4398 | if (depth != ext_depth(inode)) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4399 | credits = ext4_chunk_trans_blocks(inode, len); |
| 4400 | depth = ext_depth(inode); |
| 4401 | } |
| 4402 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 4403 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4404 | credits); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4405 | if (IS_ERR(handle)) { |
| 4406 | ret = PTR_ERR(handle); |
| 4407 | break; |
| 4408 | } |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4409 | ret = ext4_map_blocks(handle, inode, &map, flags); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4410 | if (ret <= 0) { |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4411 | ext4_debug("inode #%lu: block %u: len %u: " |
| 4412 | "ext4_ext_map_blocks returned %d", |
| 4413 | inode->i_ino, map.m_lblk, |
| 4414 | map.m_len, ret); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4415 | ext4_mark_inode_dirty(handle, inode); |
| 4416 | ret2 = ext4_journal_stop(handle); |
| 4417 | break; |
| 4418 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4419 | map.m_lblk += ret; |
| 4420 | map.m_len = len = len - ret; |
| 4421 | epos = (loff_t)map.m_lblk << inode->i_blkbits; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4422 | inode->i_ctime = current_time(inode); |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4423 | if (new_size) { |
| 4424 | if (epos > new_size) |
| 4425 | epos = new_size; |
| 4426 | if (ext4_update_inode_size(inode, epos) & 0x1) |
| 4427 | inode->i_mtime = inode->i_ctime; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4428 | } |
| 4429 | ext4_mark_inode_dirty(handle, inode); |
Eryu Guan | c894aa9 | 2017-12-03 22:52:51 -0500 | [diff] [blame] | 4430 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4431 | ret2 = ext4_journal_stop(handle); |
| 4432 | if (ret2) |
| 4433 | break; |
| 4434 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4435 | if (ret == -ENOSPC && |
| 4436 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4437 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4438 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4439 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4440 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4441 | return ret > 0 ? ret2 : ret; |
| 4442 | } |
| 4443 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 4444 | static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len); |
| 4445 | |
| 4446 | static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len); |
| 4447 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4448 | static long ext4_zero_range(struct file *file, loff_t offset, |
| 4449 | loff_t len, int mode) |
| 4450 | { |
| 4451 | struct inode *inode = file_inode(file); |
| 4452 | handle_t *handle = NULL; |
| 4453 | unsigned int max_blocks; |
| 4454 | loff_t new_size = 0; |
| 4455 | int ret = 0; |
| 4456 | int flags; |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4457 | int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4458 | int partial_begin, partial_end; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4459 | loff_t start, end; |
| 4460 | ext4_lblk_t lblk; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4461 | unsigned int blkbits = inode->i_blkbits; |
| 4462 | |
| 4463 | trace_ext4_zero_range(inode, offset, len, mode); |
| 4464 | |
Namjae Jeon | e1ee60f | 2014-05-27 12:48:55 -0400 | [diff] [blame] | 4465 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 4466 | if (ext4_should_journal_data(inode)) { |
| 4467 | ret = ext4_force_commit(inode->i_sb); |
| 4468 | if (ret) |
| 4469 | return ret; |
| 4470 | } |
| 4471 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4472 | /* |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4473 | * Round up offset. This is not fallocate, we neet to zero out |
| 4474 | * blocks, so convert interior block aligned part of the range to |
| 4475 | * unwritten and possibly manually zero out unaligned parts of the |
| 4476 | * range. |
| 4477 | */ |
| 4478 | start = round_up(offset, 1 << blkbits); |
| 4479 | end = round_down((offset + len), 1 << blkbits); |
| 4480 | |
| 4481 | if (start < offset || end > offset + len) |
| 4482 | return -EINVAL; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4483 | partial_begin = offset & ((1 << blkbits) - 1); |
| 4484 | partial_end = (offset + len) & ((1 << blkbits) - 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4485 | |
| 4486 | lblk = start >> blkbits; |
| 4487 | max_blocks = (end >> blkbits); |
| 4488 | if (max_blocks < lblk) |
| 4489 | max_blocks = 0; |
| 4490 | else |
| 4491 | max_blocks -= lblk; |
| 4492 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4493 | inode_lock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4494 | |
| 4495 | /* |
| 4496 | * Indirect files do not support unwritten extnets |
| 4497 | */ |
| 4498 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4499 | ret = -EOPNOTSUPP; |
| 4500 | goto out_mutex; |
| 4501 | } |
| 4502 | |
| 4503 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4504 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4505 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4506 | new_size = offset + len; |
| 4507 | ret = inode_newsize_ok(inode, new_size); |
| 4508 | if (ret) |
| 4509 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4510 | } |
| 4511 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4512 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
| 4513 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4514 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4515 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4516 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4517 | inode_dio_wait(inode); |
| 4518 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4519 | /* Preallocate the range including the unaligned edges */ |
| 4520 | if (partial_begin || partial_end) { |
| 4521 | ret = ext4_alloc_file_blocks(file, |
| 4522 | round_down(offset, 1 << blkbits) >> blkbits, |
| 4523 | (round_up((offset + len), 1 << blkbits) - |
| 4524 | round_down(offset, 1 << blkbits)) >> blkbits, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4525 | new_size, flags); |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4526 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4527 | goto out_mutex; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4528 | |
| 4529 | } |
| 4530 | |
| 4531 | /* Zero range excluding the unaligned edges */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4532 | if (max_blocks > 0) { |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4533 | flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | |
| 4534 | EXT4_EX_NOCACHE); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4535 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4536 | /* |
| 4537 | * Prevent page faults from reinstantiating pages we have |
| 4538 | * released from page cache. |
| 4539 | */ |
| 4540 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 4541 | |
| 4542 | ret = ext4_break_layouts(inode); |
| 4543 | if (ret) { |
| 4544 | up_write(&EXT4_I(inode)->i_mmap_sem); |
| 4545 | goto out_mutex; |
| 4546 | } |
| 4547 | |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4548 | ret = ext4_update_disksize_before_punch(inode, offset, len); |
| 4549 | if (ret) { |
| 4550 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4551 | goto out_mutex; |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4552 | } |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4553 | /* Now release the pages and zero block aligned part of pages */ |
| 4554 | truncate_pagecache_range(inode, start, end - 1); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4555 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4556 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4557 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4558 | flags); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4559 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4560 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4561 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4562 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4563 | if (!partial_begin && !partial_end) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4564 | goto out_mutex; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4565 | |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4566 | /* |
| 4567 | * In worst case we have to writeout two nonadjacent unwritten |
| 4568 | * blocks and update the inode |
| 4569 | */ |
| 4570 | credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1; |
| 4571 | if (ext4_should_journal_data(inode)) |
| 4572 | credits += 2; |
| 4573 | handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4574 | if (IS_ERR(handle)) { |
| 4575 | ret = PTR_ERR(handle); |
| 4576 | ext4_std_error(inode->i_sb, ret); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4577 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4578 | } |
| 4579 | |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4580 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4581 | if (new_size) |
Dmitry Monakhov | 4631dbf | 2014-08-23 17:48:28 -0400 | [diff] [blame] | 4582 | ext4_update_inode_size(inode, new_size); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4583 | ext4_mark_inode_dirty(handle, inode); |
| 4584 | |
| 4585 | /* Zero out partial block at the edges of the range */ |
| 4586 | ret = ext4_zero_partial_blocks(handle, inode, offset, len); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 4587 | if (ret >= 0) |
| 4588 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4589 | |
| 4590 | if (file->f_flags & O_SYNC) |
| 4591 | ext4_handle_sync(handle); |
| 4592 | |
| 4593 | ext4_journal_stop(handle); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4594 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4595 | inode_unlock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4596 | return ret; |
| 4597 | } |
| 4598 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4599 | /* |
| 4600 | * preallocate space for a file. This implements ext4's fallocate file |
| 4601 | * operation, which gets called from sys_fallocate system call. |
| 4602 | * For block-mapped files, posix_fallocate should fall back to the method |
| 4603 | * of writing zeroes to the required new blocks (the same behavior which is |
| 4604 | * expected for file systems which do not support fallocate() system call). |
| 4605 | */ |
| 4606 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 4607 | { |
| 4608 | struct inode *inode = file_inode(file); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4609 | loff_t new_size = 0; |
| 4610 | unsigned int max_blocks; |
| 4611 | int ret = 0; |
| 4612 | int flags; |
| 4613 | ext4_lblk_t lblk; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4614 | unsigned int blkbits = inode->i_blkbits; |
| 4615 | |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4616 | /* |
| 4617 | * Encrypted inodes can't handle collapse range or insert |
| 4618 | * range since we would need to re-encrypt blocks with a |
| 4619 | * different IV or XTS tweak (which are based on the logical |
| 4620 | * block number). |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4621 | */ |
Chandan Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 4622 | if (IS_ENCRYPTED(inode) && |
Eric Biggers | 457b1e3 | 2019-12-26 09:42:16 -0600 | [diff] [blame] | 4623 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4624 | return -EOPNOTSUPP; |
| 4625 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4626 | /* Return error if mode is not supported */ |
| 4627 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4628 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
| 4629 | FALLOC_FL_INSERT_RANGE)) |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4630 | return -EOPNOTSUPP; |
| 4631 | |
| 4632 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 4633 | return ext4_punch_hole(inode, offset, len); |
| 4634 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4635 | ret = ext4_convert_inline_data(inode); |
| 4636 | if (ret) |
| 4637 | return ret; |
| 4638 | |
Theodore Ts'o | 40c406c | 2014-04-12 22:53:53 -0400 | [diff] [blame] | 4639 | if (mode & FALLOC_FL_COLLAPSE_RANGE) |
| 4640 | return ext4_collapse_range(inode, offset, len); |
| 4641 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4642 | if (mode & FALLOC_FL_INSERT_RANGE) |
| 4643 | return ext4_insert_range(inode, offset, len); |
| 4644 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4645 | if (mode & FALLOC_FL_ZERO_RANGE) |
| 4646 | return ext4_zero_range(file, offset, len, mode); |
| 4647 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4648 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
| 4649 | lblk = offset >> blkbits; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4650 | |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4651 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4652 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4653 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4654 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4655 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4656 | inode_lock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4657 | |
Davide Italiano | 280227a | 2015-05-02 23:21:15 -0400 | [diff] [blame] | 4658 | /* |
| 4659 | * We only support preallocation for extent-based files only |
| 4660 | */ |
| 4661 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4662 | ret = -EOPNOTSUPP; |
| 4663 | goto out; |
| 4664 | } |
| 4665 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4666 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4667 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4668 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4669 | new_size = offset + len; |
| 4670 | ret = inode_newsize_ok(inode, new_size); |
| 4671 | if (ret) |
| 4672 | goto out; |
| 4673 | } |
| 4674 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4675 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4676 | inode_dio_wait(inode); |
| 4677 | |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4678 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4679 | if (ret) |
| 4680 | goto out; |
| 4681 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4682 | if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { |
| 4683 | ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, |
| 4684 | EXT4_I(inode)->i_sync_tid); |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4685 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4686 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4687 | inode_unlock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4688 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
| 4689 | return ret; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4690 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4691 | |
| 4692 | /* |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4693 | * This function convert a range of blocks to written extents |
| 4694 | * The caller of this function will pass the start offset and the size. |
| 4695 | * all unwritten extents within this range will be converted to |
| 4696 | * written extents. |
| 4697 | * |
| 4698 | * This function is called from the direct IO end io call back |
| 4699 | * function, to convert the fallocated extents after IO is completed. |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 4700 | * Returns 0 on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4701 | */ |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4702 | int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, |
| 4703 | loff_t offset, ssize_t len) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4704 | { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4705 | unsigned int max_blocks; |
| 4706 | int ret = 0; |
| 4707 | int ret2 = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4708 | struct ext4_map_blocks map; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4709 | unsigned int blkbits = inode->i_blkbits; |
| 4710 | unsigned int credits = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4711 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4712 | map.m_lblk = offset >> blkbits; |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4713 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
| 4714 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4715 | if (!handle) { |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4716 | /* |
| 4717 | * credits to insert 1 extent into extent tree |
| 4718 | */ |
| 4719 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4720 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4721 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4722 | map.m_lblk += ret; |
| 4723 | map.m_len = (max_blocks -= ret); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4724 | if (credits) { |
| 4725 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4726 | credits); |
| 4727 | if (IS_ERR(handle)) { |
| 4728 | ret = PTR_ERR(handle); |
| 4729 | break; |
| 4730 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4731 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4732 | ret = ext4_map_blocks(handle, inode, &map, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4733 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
Lukas Czerner | b06acd3 | 2013-01-28 21:21:12 -0500 | [diff] [blame] | 4734 | if (ret <= 0) |
| 4735 | ext4_warning(inode->i_sb, |
| 4736 | "inode #%lu: block %u: len %u: " |
| 4737 | "ext4_ext_map_blocks returned %d", |
| 4738 | inode->i_ino, map.m_lblk, |
| 4739 | map.m_len, ret); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4740 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4741 | if (credits) |
| 4742 | ret2 = ext4_journal_stop(handle); |
| 4743 | if (ret <= 0 || ret2) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4744 | break; |
| 4745 | } |
| 4746 | return ret > 0 ? ret2 : ret; |
| 4747 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4748 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4749 | int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) |
| 4750 | { |
| 4751 | int ret, err = 0; |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4752 | struct ext4_io_end_vec *io_end_vec; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4753 | |
| 4754 | /* |
| 4755 | * This is somewhat ugly but the idea is clear: When transaction is |
| 4756 | * reserved, everything goes into it. Otherwise we rather start several |
| 4757 | * smaller transactions for conversion of each extent separately. |
| 4758 | */ |
| 4759 | if (handle) { |
| 4760 | handle = ext4_journal_start_reserved(handle, |
| 4761 | EXT4_HT_EXT_CONVERT); |
| 4762 | if (IS_ERR(handle)) |
| 4763 | return PTR_ERR(handle); |
| 4764 | } |
| 4765 | |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4766 | list_for_each_entry(io_end_vec, &io_end->list_vec, list) { |
| 4767 | ret = ext4_convert_unwritten_extents(handle, io_end->inode, |
| 4768 | io_end_vec->offset, |
| 4769 | io_end_vec->size); |
| 4770 | if (ret) |
| 4771 | break; |
| 4772 | } |
| 4773 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4774 | if (handle) |
| 4775 | err = ext4_journal_stop(handle); |
| 4776 | |
| 4777 | return ret < 0 ? ret : err; |
| 4778 | } |
| 4779 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4780 | static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4781 | { |
| 4782 | __u64 physical = 0; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4783 | __u64 length = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4784 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 4785 | int error = 0; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4786 | u16 iomap_type; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4787 | |
| 4788 | /* in-inode? */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4789 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4790 | struct ext4_iloc iloc; |
| 4791 | int offset; /* offset of xattr in inode */ |
| 4792 | |
| 4793 | error = ext4_get_inode_loc(inode, &iloc); |
| 4794 | if (error) |
| 4795 | return error; |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4796 | physical = (__u64)iloc.bh->b_blocknr << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4797 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 4798 | EXT4_I(inode)->i_extra_isize; |
| 4799 | physical += offset; |
| 4800 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 4801 | brelse(iloc.bh); |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4802 | iomap_type = IOMAP_INLINE; |
| 4803 | } else if (EXT4_I(inode)->i_file_acl) { /* external block */ |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4804 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4805 | length = inode->i_sb->s_blocksize; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4806 | iomap_type = IOMAP_MAPPED; |
| 4807 | } else { |
| 4808 | /* no in-inode or external block for xattr, so return -ENOENT */ |
| 4809 | error = -ENOENT; |
| 4810 | goto out; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4811 | } |
| 4812 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4813 | iomap->addr = physical; |
| 4814 | iomap->offset = 0; |
| 4815 | iomap->length = length; |
| 4816 | iomap->type = iomap_type; |
| 4817 | iomap->flags = 0; |
| 4818 | out: |
| 4819 | return error; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4820 | } |
| 4821 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4822 | static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset, |
| 4823 | loff_t length, unsigned flags, |
| 4824 | struct iomap *iomap, struct iomap *srcmap) |
| 4825 | { |
| 4826 | int error; |
| 4827 | |
| 4828 | error = ext4_iomap_xattr_fiemap(inode, iomap); |
| 4829 | if (error == 0 && (offset >= iomap->length)) |
| 4830 | error = -ENOENT; |
| 4831 | return error; |
| 4832 | } |
| 4833 | |
| 4834 | static const struct iomap_ops ext4_iomap_xattr_ops = { |
| 4835 | .iomap_begin = ext4_iomap_xattr_begin, |
| 4836 | }; |
| 4837 | |
| 4838 | static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4839 | __u64 start, __u64 len, bool from_es_cache) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4840 | { |
| 4841 | ext4_lblk_t start_blk; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4842 | u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4843 | int error = 0; |
| 4844 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 4845 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 4846 | error = ext4_ext_precache(inode); |
| 4847 | if (error) |
| 4848 | return error; |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4849 | fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 4850 | } |
| 4851 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4852 | if (from_es_cache) |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4853 | ext4_fiemap_flags &= FIEMAP_FLAG_XATTR; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4854 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4855 | if (fiemap_check_flags(fieinfo, ext4_fiemap_flags)) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4856 | return -EBADR; |
| 4857 | |
| 4858 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4859 | fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; |
| 4860 | error = iomap_fiemap(inode, fieinfo, start, len, |
| 4861 | &ext4_iomap_xattr_ops); |
| 4862 | } else if (!from_es_cache) { |
| 4863 | error = iomap_fiemap(inode, fieinfo, start, len, |
| 4864 | &ext4_iomap_report_ops); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4865 | } else { |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4866 | ext4_lblk_t len_blks; |
| 4867 | __u64 last_blk; |
| 4868 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4869 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4870 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 4871 | if (last_blk >= EXT_MAX_BLOCKS) |
| 4872 | last_blk = EXT_MAX_BLOCKS-1; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4873 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4874 | |
| 4875 | /* |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4876 | * Walk the extent tree gathering extent information |
| 4877 | * and pushing extents back to the user. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4878 | */ |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4879 | error = ext4_fill_es_cache_info(inode, start_blk, len_blks, |
| 4880 | fieinfo); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4881 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4882 | return error; |
| 4883 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4884 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4885 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4886 | __u64 start, __u64 len) |
| 4887 | { |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4888 | return _ext4_fiemap(inode, fieinfo, start, len, false); |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4892 | __u64 start, __u64 len) |
| 4893 | { |
| 4894 | if (ext4_has_inline_data(inode)) { |
| 4895 | int has_inline; |
| 4896 | |
| 4897 | down_read(&EXT4_I(inode)->xattr_sem); |
| 4898 | has_inline = ext4_has_inline_data(inode); |
| 4899 | up_read(&EXT4_I(inode)->xattr_sem); |
| 4900 | if (has_inline) |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4904 | return _ext4_fiemap(inode, fieinfo, start, len, true); |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4908 | /* |
| 4909 | * ext4_access_path: |
| 4910 | * Function to access the path buffer for marking it dirty. |
| 4911 | * It also checks if there are sufficient credits left in the journal handle |
| 4912 | * to update path. |
| 4913 | */ |
| 4914 | static int |
| 4915 | ext4_access_path(handle_t *handle, struct inode *inode, |
| 4916 | struct ext4_ext_path *path) |
| 4917 | { |
| 4918 | int credits, err; |
| 4919 | |
| 4920 | if (!ext4_handle_valid(handle)) |
| 4921 | return 0; |
| 4922 | |
| 4923 | /* |
| 4924 | * Check if need to extend journal credits |
| 4925 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 4926 | * descriptor) for each block group; assume two block |
| 4927 | * groups |
| 4928 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 4929 | credits = ext4_writepage_trans_blocks(inode); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 4930 | err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 4931 | if (err < 0) |
| 4932 | return err; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4933 | |
| 4934 | err = ext4_ext_get_access(handle, inode, path); |
| 4935 | return err; |
| 4936 | } |
| 4937 | |
| 4938 | /* |
| 4939 | * ext4_ext_shift_path_extents: |
| 4940 | * 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] | 4941 | * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells |
| 4942 | * if it is right shift or left shift operation. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4943 | */ |
| 4944 | static int |
| 4945 | ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, |
| 4946 | struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4947 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4948 | { |
| 4949 | int depth, err = 0; |
| 4950 | struct ext4_extent *ex_start, *ex_last; |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 4951 | bool update = false; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4952 | depth = path->p_depth; |
| 4953 | |
| 4954 | while (depth >= 0) { |
| 4955 | if (depth == path->p_depth) { |
| 4956 | ex_start = path[depth].p_ext; |
| 4957 | if (!ex_start) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4958 | return -EFSCORRUPTED; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4959 | |
| 4960 | ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4961 | |
| 4962 | err = ext4_access_path(handle, inode, path + depth); |
| 4963 | if (err) |
| 4964 | goto out; |
| 4965 | |
| 4966 | if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 4967 | update = true; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4968 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4969 | while (ex_start <= ex_last) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4970 | if (SHIFT == SHIFT_LEFT) { |
| 4971 | le32_add_cpu(&ex_start->ee_block, |
| 4972 | -shift); |
| 4973 | /* Try to merge to the left. */ |
| 4974 | if ((ex_start > |
| 4975 | EXT_FIRST_EXTENT(path[depth].p_hdr)) |
| 4976 | && |
| 4977 | ext4_ext_try_to_merge_right(inode, |
| 4978 | path, ex_start - 1)) |
| 4979 | ex_last--; |
| 4980 | else |
| 4981 | ex_start++; |
| 4982 | } else { |
| 4983 | le32_add_cpu(&ex_last->ee_block, shift); |
| 4984 | ext4_ext_try_to_merge_right(inode, path, |
| 4985 | ex_last); |
Lukas Czerner | 6dd834e | 2014-04-18 10:55:24 -0400 | [diff] [blame] | 4986 | ex_last--; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4987 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4988 | } |
| 4989 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 4990 | if (err) |
| 4991 | goto out; |
| 4992 | |
| 4993 | if (--depth < 0 || !update) |
| 4994 | break; |
| 4995 | } |
| 4996 | |
| 4997 | /* Update index too */ |
| 4998 | err = ext4_access_path(handle, inode, path + depth); |
| 4999 | if (err) |
| 5000 | goto out; |
| 5001 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5002 | if (SHIFT == SHIFT_LEFT) |
| 5003 | le32_add_cpu(&path[depth].p_idx->ei_block, -shift); |
| 5004 | else |
| 5005 | le32_add_cpu(&path[depth].p_idx->ei_block, shift); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5006 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5007 | if (err) |
| 5008 | goto out; |
| 5009 | |
| 5010 | /* we are done if current index is not a starting index */ |
| 5011 | if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) |
| 5012 | break; |
| 5013 | |
| 5014 | depth--; |
| 5015 | } |
| 5016 | |
| 5017 | out: |
| 5018 | return err; |
| 5019 | } |
| 5020 | |
| 5021 | /* |
| 5022 | * ext4_ext_shift_extents: |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5023 | * All the extents which lies in the range from @start to the last allocated |
| 5024 | * block for the @inode are shifted either towards left or right (depending |
| 5025 | * upon @SHIFT) by @shift blocks. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5026 | * On success, 0 is returned, error otherwise. |
| 5027 | */ |
| 5028 | static int |
| 5029 | ext4_ext_shift_extents(struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5030 | ext4_lblk_t start, ext4_lblk_t shift, |
| 5031 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5032 | { |
| 5033 | struct ext4_ext_path *path; |
| 5034 | int ret = 0, depth; |
| 5035 | struct ext4_extent *extent; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5036 | ext4_lblk_t stop, *iterator, ex_start, ex_end; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5037 | |
| 5038 | /* Let path point to the last extent */ |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5039 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
| 5040 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5041 | if (IS_ERR(path)) |
| 5042 | return PTR_ERR(path); |
| 5043 | |
| 5044 | depth = path->p_depth; |
| 5045 | extent = path[depth].p_ext; |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5046 | if (!extent) |
| 5047 | goto out; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5048 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5049 | stop = le32_to_cpu(extent->ee_block); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5050 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5051 | /* |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5052 | * For left shifts, make sure the hole on the left is big enough to |
| 5053 | * accommodate the shift. For right shifts, make sure the last extent |
| 5054 | * won't be shifted beyond EXT_MAX_BLOCKS. |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5055 | */ |
| 5056 | if (SHIFT == SHIFT_LEFT) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5057 | path = ext4_find_extent(inode, start - 1, &path, |
| 5058 | EXT4_EX_NOCACHE); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5059 | if (IS_ERR(path)) |
| 5060 | return PTR_ERR(path); |
| 5061 | depth = path->p_depth; |
| 5062 | extent = path[depth].p_ext; |
| 5063 | if (extent) { |
| 5064 | ex_start = le32_to_cpu(extent->ee_block); |
| 5065 | ex_end = le32_to_cpu(extent->ee_block) + |
| 5066 | ext4_ext_get_actual_len(extent); |
| 5067 | } else { |
| 5068 | ex_start = 0; |
| 5069 | ex_end = 0; |
| 5070 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5071 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5072 | if ((start == ex_start && shift > ex_start) || |
| 5073 | (shift > start - ex_end)) { |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5074 | ret = -EINVAL; |
| 5075 | goto out; |
| 5076 | } |
| 5077 | } else { |
| 5078 | if (shift > EXT_MAX_BLOCKS - |
| 5079 | (stop + ext4_ext_get_actual_len(extent))) { |
| 5080 | ret = -EINVAL; |
| 5081 | goto out; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5082 | } |
Dmitry Monakhov | 8dc79ec | 2014-04-13 15:05:42 -0400 | [diff] [blame] | 5083 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5084 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5085 | /* |
| 5086 | * In case of left shift, iterator points to start and it is increased |
| 5087 | * till we reach stop. In case of right shift, iterator points to stop |
| 5088 | * and it is decreased till we reach start. |
| 5089 | */ |
| 5090 | if (SHIFT == SHIFT_LEFT) |
| 5091 | iterator = &start; |
| 5092 | else |
| 5093 | iterator = &stop; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5094 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5095 | /* |
| 5096 | * Its safe to start updating extents. Start and stop are unsigned, so |
| 5097 | * in case of right shift if extent with 0 block is reached, iterator |
| 5098 | * becomes NULL to indicate the end of the loop. |
| 5099 | */ |
| 5100 | while (iterator && start <= stop) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5101 | path = ext4_find_extent(inode, *iterator, &path, |
| 5102 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5103 | if (IS_ERR(path)) |
| 5104 | return PTR_ERR(path); |
| 5105 | depth = path->p_depth; |
| 5106 | extent = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5107 | if (!extent) { |
| 5108 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5109 | (unsigned long) *iterator); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5110 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5111 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5112 | if (SHIFT == SHIFT_LEFT && *iterator > |
| 5113 | le32_to_cpu(extent->ee_block)) { |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5114 | /* Hole, move to the next extent */ |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5115 | if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 5116 | path[depth].p_ext++; |
| 5117 | } else { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5118 | *iterator = ext4_ext_next_allocated_block(path); |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5119 | continue; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5120 | } |
| 5121 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5122 | |
| 5123 | if (SHIFT == SHIFT_LEFT) { |
| 5124 | extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
| 5125 | *iterator = le32_to_cpu(extent->ee_block) + |
| 5126 | ext4_ext_get_actual_len(extent); |
| 5127 | } else { |
| 5128 | extent = EXT_FIRST_EXTENT(path[depth].p_hdr); |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5129 | if (le32_to_cpu(extent->ee_block) > 0) |
| 5130 | *iterator = le32_to_cpu(extent->ee_block) - 1; |
| 5131 | else |
| 5132 | /* Beginning is reached, end of the loop */ |
| 5133 | iterator = NULL; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5134 | /* Update path extent in case we need to stop */ |
| 5135 | while (le32_to_cpu(extent->ee_block) < start) |
| 5136 | extent++; |
| 5137 | path[depth].p_ext = extent; |
| 5138 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5139 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5140 | handle, SHIFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5141 | if (ret) |
| 5142 | break; |
| 5143 | } |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5144 | out: |
| 5145 | ext4_ext_drop_refs(path); |
| 5146 | kfree(path); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5147 | return ret; |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * ext4_collapse_range: |
| 5152 | * This implements the fallocate's collapse range functionality for ext4 |
| 5153 | * Returns: 0 and non-zero on error. |
| 5154 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5155 | 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] | 5156 | { |
| 5157 | struct super_block *sb = inode->i_sb; |
| 5158 | ext4_lblk_t punch_start, punch_stop; |
| 5159 | handle_t *handle; |
| 5160 | unsigned int credits; |
Namjae Jeon | a8680e0 | 2014-04-19 16:37:31 -0400 | [diff] [blame] | 5161 | loff_t new_size, ioffset; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5162 | int ret; |
| 5163 | |
Theodore Ts'o | b9576fc | 2015-05-15 00:24:10 -0400 | [diff] [blame] | 5164 | /* |
| 5165 | * We need to test this early because xfstests assumes that a |
| 5166 | * collapse range of (0, 1) will return EOPNOTSUPP if the file |
| 5167 | * system does not support collapse range. |
| 5168 | */ |
| 5169 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5170 | return -EOPNOTSUPP; |
| 5171 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5172 | /* Collapse range works only on fs cluster size aligned regions. */ |
| 5173 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5174 | return -EINVAL; |
| 5175 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5176 | trace_ext4_collapse_range(inode, offset, len); |
| 5177 | |
| 5178 | punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5179 | punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5180 | |
Namjae Jeon | 1ce01c4 | 2014-04-10 22:58:20 -0400 | [diff] [blame] | 5181 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 5182 | if (ext4_should_journal_data(inode)) { |
| 5183 | ret = ext4_force_commit(inode->i_sb); |
| 5184 | if (ret) |
| 5185 | return ret; |
| 5186 | } |
| 5187 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5188 | inode_lock(inode); |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5189 | /* |
| 5190 | * There is no need to overlap collapse range with EOF, in which case |
| 5191 | * it is effectively a truncate operation |
| 5192 | */ |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5193 | if (offset + len >= inode->i_size) { |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5194 | ret = -EINVAL; |
| 5195 | goto out_mutex; |
| 5196 | } |
| 5197 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5198 | /* Currently just for extent based files */ |
| 5199 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5200 | ret = -EOPNOTSUPP; |
| 5201 | goto out_mutex; |
| 5202 | } |
| 5203 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5204 | /* Wait for existing dio to complete */ |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5205 | inode_dio_wait(inode); |
| 5206 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5207 | /* |
| 5208 | * Prevent page faults from reinstantiating pages we have released from |
| 5209 | * page cache. |
| 5210 | */ |
| 5211 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5212 | |
| 5213 | ret = ext4_break_layouts(inode); |
| 5214 | if (ret) |
| 5215 | goto out_mmap; |
| 5216 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5217 | /* |
| 5218 | * Need to round down offset to be aligned with page size boundary |
| 5219 | * for page size > block size. |
| 5220 | */ |
| 5221 | ioffset = round_down(offset, PAGE_SIZE); |
| 5222 | /* |
| 5223 | * Write tail of the last page before removed range since it will get |
| 5224 | * removed from the page cache below. |
| 5225 | */ |
| 5226 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset); |
| 5227 | if (ret) |
| 5228 | goto out_mmap; |
| 5229 | /* |
| 5230 | * Write data that will be shifted to preserve them when discarding |
| 5231 | * page cache below. We are also protected from pages becoming dirty |
| 5232 | * by i_mmap_sem. |
| 5233 | */ |
| 5234 | ret = filemap_write_and_wait_range(inode->i_mapping, offset + len, |
| 5235 | LLONG_MAX); |
| 5236 | if (ret) |
| 5237 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5238 | truncate_pagecache(inode, ioffset); |
| 5239 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5240 | credits = ext4_writepage_trans_blocks(inode); |
| 5241 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5242 | if (IS_ERR(handle)) { |
| 5243 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5244 | goto out_mmap; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5248 | ext4_discard_preallocations(inode); |
| 5249 | |
| 5250 | ret = ext4_es_remove_extent(inode, punch_start, |
Lukas Czerner | 2c1d232 | 2014-04-18 10:43:21 -0400 | [diff] [blame] | 5251 | EXT_MAX_BLOCKS - punch_start); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5252 | if (ret) { |
| 5253 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5254 | goto out_stop; |
| 5255 | } |
| 5256 | |
| 5257 | ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); |
| 5258 | if (ret) { |
| 5259 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5260 | goto out_stop; |
| 5261 | } |
Lukas Czerner | ef24f6c | 2014-04-18 10:50:23 -0400 | [diff] [blame] | 5262 | ext4_discard_preallocations(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5263 | |
| 5264 | ret = ext4_ext_shift_extents(inode, handle, punch_stop, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5265 | punch_stop - punch_start, SHIFT_LEFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5266 | if (ret) { |
| 5267 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5268 | goto out_stop; |
| 5269 | } |
| 5270 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5271 | new_size = inode->i_size - len; |
Lukas Czerner | 9337d5d | 2014-04-18 10:48:25 -0400 | [diff] [blame] | 5272 | i_size_write(inode, new_size); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5273 | EXT4_I(inode)->i_disksize = new_size; |
| 5274 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5275 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5276 | if (IS_SYNC(inode)) |
| 5277 | ext4_handle_sync(handle); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5278 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5279 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5280 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5281 | |
| 5282 | out_stop: |
| 5283 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5284 | out_mmap: |
| 5285 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5286 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5287 | inode_unlock(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5288 | return ret; |
| 5289 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5290 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5291 | /* |
| 5292 | * ext4_insert_range: |
| 5293 | * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. |
| 5294 | * The data blocks starting from @offset to the EOF are shifted by @len |
| 5295 | * towards right to create a hole in the @inode. Inode size is increased |
| 5296 | * by len bytes. |
| 5297 | * Returns 0 on success, error otherwise. |
| 5298 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5299 | 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] | 5300 | { |
| 5301 | struct super_block *sb = inode->i_sb; |
| 5302 | handle_t *handle; |
| 5303 | struct ext4_ext_path *path; |
| 5304 | struct ext4_extent *extent; |
| 5305 | ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0; |
| 5306 | unsigned int credits, ee_len; |
| 5307 | int ret = 0, depth, split_flag = 0; |
| 5308 | loff_t ioffset; |
| 5309 | |
| 5310 | /* |
| 5311 | * We need to test this early because xfstests assumes that an |
| 5312 | * insert range of (0, 1) will return EOPNOTSUPP if the file |
| 5313 | * system does not support insert range. |
| 5314 | */ |
| 5315 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5316 | return -EOPNOTSUPP; |
| 5317 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5318 | /* Insert range works only on fs cluster size aligned regions. */ |
| 5319 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5320 | return -EINVAL; |
| 5321 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5322 | trace_ext4_insert_range(inode, offset, len); |
| 5323 | |
| 5324 | offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5325 | len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5326 | |
| 5327 | /* Call ext4_force_commit to flush all data in case of data=journal */ |
| 5328 | if (ext4_should_journal_data(inode)) { |
| 5329 | ret = ext4_force_commit(inode->i_sb); |
| 5330 | if (ret) |
| 5331 | return ret; |
| 5332 | } |
| 5333 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5334 | inode_lock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5335 | /* Currently just for extent based files */ |
| 5336 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5337 | ret = -EOPNOTSUPP; |
| 5338 | goto out_mutex; |
| 5339 | } |
| 5340 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5341 | /* Check whether the maximum file size would be exceeded */ |
| 5342 | if (len > inode->i_sb->s_maxbytes - inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5343 | ret = -EFBIG; |
| 5344 | goto out_mutex; |
| 5345 | } |
| 5346 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5347 | /* Offset must be less than i_size */ |
| 5348 | if (offset >= inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5349 | ret = -EINVAL; |
| 5350 | goto out_mutex; |
| 5351 | } |
| 5352 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5353 | /* Wait for existing dio to complete */ |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5354 | inode_dio_wait(inode); |
| 5355 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5356 | /* |
| 5357 | * Prevent page faults from reinstantiating pages we have released from |
| 5358 | * page cache. |
| 5359 | */ |
| 5360 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5361 | |
| 5362 | ret = ext4_break_layouts(inode); |
| 5363 | if (ret) |
| 5364 | goto out_mmap; |
| 5365 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5366 | /* |
| 5367 | * Need to round down to align start offset to page size boundary |
| 5368 | * for page size > block size. |
| 5369 | */ |
| 5370 | ioffset = round_down(offset, PAGE_SIZE); |
| 5371 | /* Write out all dirty pages */ |
| 5372 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, |
| 5373 | LLONG_MAX); |
| 5374 | if (ret) |
| 5375 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5376 | truncate_pagecache(inode, ioffset); |
| 5377 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5378 | credits = ext4_writepage_trans_blocks(inode); |
| 5379 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5380 | if (IS_ERR(handle)) { |
| 5381 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5382 | goto out_mmap; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | /* Expand file to avoid data loss if there is error while shifting */ |
| 5386 | inode->i_size += len; |
| 5387 | EXT4_I(inode)->i_disksize += len; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5388 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5389 | ret = ext4_mark_inode_dirty(handle, inode); |
| 5390 | if (ret) |
| 5391 | goto out_stop; |
| 5392 | |
| 5393 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5394 | ext4_discard_preallocations(inode); |
| 5395 | |
| 5396 | path = ext4_find_extent(inode, offset_lblk, NULL, 0); |
| 5397 | if (IS_ERR(path)) { |
| 5398 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5399 | goto out_stop; |
| 5400 | } |
| 5401 | |
| 5402 | depth = ext_depth(inode); |
| 5403 | extent = path[depth].p_ext; |
| 5404 | if (extent) { |
| 5405 | ee_start_lblk = le32_to_cpu(extent->ee_block); |
| 5406 | ee_len = ext4_ext_get_actual_len(extent); |
| 5407 | |
| 5408 | /* |
| 5409 | * If offset_lblk is not the starting block of extent, split |
| 5410 | * the extent @offset_lblk |
| 5411 | */ |
| 5412 | if ((offset_lblk > ee_start_lblk) && |
| 5413 | (offset_lblk < (ee_start_lblk + ee_len))) { |
| 5414 | if (ext4_ext_is_unwritten(extent)) |
| 5415 | split_flag = EXT4_EXT_MARK_UNWRIT1 | |
| 5416 | EXT4_EXT_MARK_UNWRIT2; |
| 5417 | ret = ext4_split_extent_at(handle, inode, &path, |
| 5418 | offset_lblk, split_flag, |
| 5419 | EXT4_EX_NOCACHE | |
| 5420 | EXT4_GET_BLOCKS_PRE_IO | |
| 5421 | EXT4_GET_BLOCKS_METADATA_NOFAIL); |
| 5422 | } |
| 5423 | |
| 5424 | ext4_ext_drop_refs(path); |
| 5425 | kfree(path); |
| 5426 | if (ret < 0) { |
| 5427 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5428 | goto out_stop; |
| 5429 | } |
Fabian Frederick | edf15aa1 | 2016-09-15 11:39:52 -0400 | [diff] [blame] | 5430 | } else { |
| 5431 | ext4_ext_drop_refs(path); |
| 5432 | kfree(path); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | ret = ext4_es_remove_extent(inode, offset_lblk, |
| 5436 | EXT_MAX_BLOCKS - offset_lblk); |
| 5437 | if (ret) { |
| 5438 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5439 | goto out_stop; |
| 5440 | } |
| 5441 | |
| 5442 | /* |
| 5443 | * if offset_lblk lies in a hole which is at start of file, use |
| 5444 | * ee_start_lblk to shift extents |
| 5445 | */ |
| 5446 | ret = ext4_ext_shift_extents(inode, handle, |
| 5447 | ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk, |
| 5448 | len_lblk, SHIFT_RIGHT); |
| 5449 | |
| 5450 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5451 | if (IS_SYNC(inode)) |
| 5452 | ext4_handle_sync(handle); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5453 | if (ret >= 0) |
| 5454 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5455 | |
| 5456 | out_stop: |
| 5457 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5458 | out_mmap: |
| 5459 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5460 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5461 | inode_unlock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5462 | return ret; |
| 5463 | } |
| 5464 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5465 | /** |
Theodore Ts'o | c60990b | 2019-06-19 16:30:03 -0400 | [diff] [blame] | 5466 | * ext4_swap_extents() - Swap extents between two inodes |
| 5467 | * @handle: handle for this transaction |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5468 | * @inode1: First inode |
| 5469 | * @inode2: Second inode |
| 5470 | * @lblk1: Start block for first inode |
| 5471 | * @lblk2: Start block for second inode |
| 5472 | * @count: Number of blocks to swap |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5473 | * @unwritten: Mark second inode's extents as unwritten after swap |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5474 | * @erp: Pointer to save error value |
| 5475 | * |
| 5476 | * This helper routine does exactly what is promise "swap extents". All other |
| 5477 | * stuff such as page-cache locking consistency, bh mapping consistency or |
| 5478 | * extent's data copying must be performed by caller. |
| 5479 | * Locking: |
| 5480 | * i_mutex is held for both inodes |
| 5481 | * i_data_sem is locked for write for both inodes |
| 5482 | * Assumptions: |
| 5483 | * All pages from requested range are locked for both inodes |
| 5484 | */ |
| 5485 | int |
| 5486 | ext4_swap_extents(handle_t *handle, struct inode *inode1, |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5487 | struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5488 | ext4_lblk_t count, int unwritten, int *erp) |
| 5489 | { |
| 5490 | struct ext4_ext_path *path1 = NULL; |
| 5491 | struct ext4_ext_path *path2 = NULL; |
| 5492 | int replaced_count = 0; |
| 5493 | |
| 5494 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); |
| 5495 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5496 | BUG_ON(!inode_is_locked(inode1)); |
| 5497 | BUG_ON(!inode_is_locked(inode2)); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5498 | |
| 5499 | *erp = ext4_es_remove_extent(inode1, lblk1, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5500 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5501 | return 0; |
| 5502 | *erp = ext4_es_remove_extent(inode2, lblk2, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5503 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5504 | return 0; |
| 5505 | |
| 5506 | while (count) { |
| 5507 | struct ext4_extent *ex1, *ex2, tmp_ex; |
| 5508 | ext4_lblk_t e1_blk, e2_blk; |
| 5509 | int e1_len, e2_len, len; |
| 5510 | int split = 0; |
| 5511 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5512 | path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5513 | if (IS_ERR(path1)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5514 | *erp = PTR_ERR(path1); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5515 | path1 = NULL; |
| 5516 | finish: |
| 5517 | count = 0; |
| 5518 | goto repeat; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5519 | } |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5520 | path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5521 | if (IS_ERR(path2)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5522 | *erp = PTR_ERR(path2); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5523 | path2 = NULL; |
| 5524 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5525 | } |
| 5526 | ex1 = path1[path1->p_depth].p_ext; |
| 5527 | ex2 = path2[path2->p_depth].p_ext; |
| 5528 | /* Do we have somthing to swap ? */ |
| 5529 | if (unlikely(!ex2 || !ex1)) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5530 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5531 | |
| 5532 | e1_blk = le32_to_cpu(ex1->ee_block); |
| 5533 | e2_blk = le32_to_cpu(ex2->ee_block); |
| 5534 | e1_len = ext4_ext_get_actual_len(ex1); |
| 5535 | e2_len = ext4_ext_get_actual_len(ex2); |
| 5536 | |
| 5537 | /* Hole handling */ |
| 5538 | if (!in_range(lblk1, e1_blk, e1_len) || |
| 5539 | !in_range(lblk2, e2_blk, e2_len)) { |
| 5540 | ext4_lblk_t next1, next2; |
| 5541 | |
| 5542 | /* if hole after extent, then go to next extent */ |
| 5543 | next1 = ext4_ext_next_allocated_block(path1); |
| 5544 | next2 = ext4_ext_next_allocated_block(path2); |
| 5545 | /* If hole before extent, then shift to that extent */ |
| 5546 | if (e1_blk > lblk1) |
| 5547 | next1 = e1_blk; |
| 5548 | if (e2_blk > lblk2) |
Maninder Singh | 4e56201 | 2017-08-06 01:33:07 -0400 | [diff] [blame] | 5549 | next2 = e2_blk; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5550 | /* Do we have something to swap */ |
| 5551 | if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5552 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5553 | /* Move to the rightest boundary */ |
| 5554 | len = next1 - lblk1; |
| 5555 | if (len < next2 - lblk2) |
| 5556 | len = next2 - lblk2; |
| 5557 | if (len > count) |
| 5558 | len = count; |
| 5559 | lblk1 += len; |
| 5560 | lblk2 += len; |
| 5561 | count -= len; |
| 5562 | goto repeat; |
| 5563 | } |
| 5564 | |
| 5565 | /* Prepare left boundary */ |
| 5566 | if (e1_blk < lblk1) { |
| 5567 | split = 1; |
| 5568 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5569 | &path1, lblk1, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5570 | if (unlikely(*erp)) |
| 5571 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5572 | } |
| 5573 | if (e2_blk < lblk2) { |
| 5574 | split = 1; |
| 5575 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5576 | &path2, lblk2, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5577 | if (unlikely(*erp)) |
| 5578 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5579 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5580 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5581 | * path must to be revalidated. */ |
| 5582 | if (split) |
| 5583 | goto repeat; |
| 5584 | |
| 5585 | /* Prepare right boundary */ |
| 5586 | len = count; |
| 5587 | if (len > e1_blk + e1_len - lblk1) |
| 5588 | len = e1_blk + e1_len - lblk1; |
| 5589 | if (len > e2_blk + e2_len - lblk2) |
| 5590 | len = e2_blk + e2_len - lblk2; |
| 5591 | |
| 5592 | if (len != e1_len) { |
| 5593 | split = 1; |
| 5594 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5595 | &path1, lblk1 + len, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5596 | if (unlikely(*erp)) |
| 5597 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5598 | } |
| 5599 | if (len != e2_len) { |
| 5600 | split = 1; |
| 5601 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5602 | &path2, lblk2 + len, 0); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5603 | if (*erp) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5604 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5605 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5606 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5607 | * path must to be revalidated. */ |
| 5608 | if (split) |
| 5609 | goto repeat; |
| 5610 | |
| 5611 | BUG_ON(e2_len != e1_len); |
| 5612 | *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5613 | if (unlikely(*erp)) |
| 5614 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5615 | *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5616 | if (unlikely(*erp)) |
| 5617 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5618 | |
| 5619 | /* Both extents are fully inside boundaries. Swap it now */ |
| 5620 | tmp_ex = *ex1; |
| 5621 | ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); |
| 5622 | ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); |
| 5623 | ex1->ee_len = cpu_to_le16(e2_len); |
| 5624 | ex2->ee_len = cpu_to_le16(e1_len); |
| 5625 | if (unwritten) |
| 5626 | ext4_ext_mark_unwritten(ex2); |
| 5627 | if (ext4_ext_is_unwritten(&tmp_ex)) |
| 5628 | ext4_ext_mark_unwritten(ex1); |
| 5629 | |
| 5630 | ext4_ext_try_to_merge(handle, inode2, path2, ex2); |
| 5631 | ext4_ext_try_to_merge(handle, inode1, path1, ex1); |
| 5632 | *erp = ext4_ext_dirty(handle, inode2, path2 + |
| 5633 | path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5634 | if (unlikely(*erp)) |
| 5635 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5636 | *erp = ext4_ext_dirty(handle, inode1, path1 + |
| 5637 | path1->p_depth); |
| 5638 | /* |
| 5639 | * Looks scarry ah..? second inode already points to new blocks, |
| 5640 | * and it was successfully dirtied. But luckily error may happen |
| 5641 | * only due to journal error, so full transaction will be |
| 5642 | * aborted anyway. |
| 5643 | */ |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5644 | if (unlikely(*erp)) |
| 5645 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5646 | lblk1 += len; |
| 5647 | lblk2 += len; |
| 5648 | replaced_count += len; |
| 5649 | count -= len; |
| 5650 | |
| 5651 | repeat: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 5652 | ext4_ext_drop_refs(path1); |
| 5653 | kfree(path1); |
| 5654 | ext4_ext_drop_refs(path2); |
| 5655 | kfree(path2); |
| 5656 | path1 = path2 = NULL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5657 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5658 | return replaced_count; |
| 5659 | } |
Eric Whitney | 0b02f4c | 2018-10-01 14:19:37 -0400 | [diff] [blame] | 5660 | |
| 5661 | /* |
| 5662 | * ext4_clu_mapped - determine whether any block in a logical cluster has |
| 5663 | * been mapped to a physical cluster |
| 5664 | * |
| 5665 | * @inode - file containing the logical cluster |
| 5666 | * @lclu - logical cluster of interest |
| 5667 | * |
| 5668 | * Returns 1 if any block in the logical cluster is mapped, signifying |
| 5669 | * that a physical cluster has been allocated for it. Otherwise, |
| 5670 | * returns 0. Can also return negative error codes. Derived from |
| 5671 | * ext4_ext_map_blocks(). |
| 5672 | */ |
| 5673 | int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) |
| 5674 | { |
| 5675 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5676 | struct ext4_ext_path *path; |
| 5677 | int depth, mapped = 0, err = 0; |
| 5678 | struct ext4_extent *extent; |
| 5679 | ext4_lblk_t first_lblk, first_lclu, last_lclu; |
| 5680 | |
| 5681 | /* search for the extent closest to the first block in the cluster */ |
| 5682 | path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); |
| 5683 | if (IS_ERR(path)) { |
| 5684 | err = PTR_ERR(path); |
| 5685 | path = NULL; |
| 5686 | goto out; |
| 5687 | } |
| 5688 | |
| 5689 | depth = ext_depth(inode); |
| 5690 | |
| 5691 | /* |
| 5692 | * A consistent leaf must not be empty. This situation is possible, |
| 5693 | * though, _during_ tree modification, and it's why an assert can't |
| 5694 | * be put in ext4_find_extent(). |
| 5695 | */ |
| 5696 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 5697 | EXT4_ERROR_INODE(inode, |
| 5698 | "bad extent address - lblock: %lu, depth: %d, pblock: %lld", |
| 5699 | (unsigned long) EXT4_C2B(sbi, lclu), |
| 5700 | depth, path[depth].p_block); |
| 5701 | err = -EFSCORRUPTED; |
| 5702 | goto out; |
| 5703 | } |
| 5704 | |
| 5705 | extent = path[depth].p_ext; |
| 5706 | |
| 5707 | /* can't be mapped if the extent tree is empty */ |
| 5708 | if (extent == NULL) |
| 5709 | goto out; |
| 5710 | |
| 5711 | first_lblk = le32_to_cpu(extent->ee_block); |
| 5712 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5713 | |
| 5714 | /* |
| 5715 | * Three possible outcomes at this point - found extent spanning |
| 5716 | * the target cluster, to the left of the target cluster, or to the |
| 5717 | * right of the target cluster. The first two cases are handled here. |
| 5718 | * The last case indicates the target cluster is not mapped. |
| 5719 | */ |
| 5720 | if (lclu >= first_lclu) { |
| 5721 | last_lclu = EXT4_B2C(sbi, first_lblk + |
| 5722 | ext4_ext_get_actual_len(extent) - 1); |
| 5723 | if (lclu <= last_lclu) { |
| 5724 | mapped = 1; |
| 5725 | } else { |
| 5726 | first_lblk = ext4_ext_next_allocated_block(path); |
| 5727 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5728 | if (lclu == first_lclu) |
| 5729 | mapped = 1; |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | out: |
| 5734 | ext4_ext_drop_refs(path); |
| 5735 | kfree(path); |
| 5736 | |
| 5737 | return err ? err : mapped; |
| 5738 | } |