lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com |
| 3 | * Written by Alex Tomas <alex@clusterfs.com> |
| 4 | * |
| 5 | * Architecture independence: |
| 6 | * Copyright (c) 2005, Bull S.A. |
| 7 | * Written by Pierre Peiffer <pierre.peiffer@bull.net> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public Licens |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Extents support for EXT4 |
| 25 | * |
| 26 | * TODO: |
| 27 | * - ext4*_error() should be used in some situations |
| 28 | * - analyze all BUG()/BUG_ON(), use -EIO where appropriate |
| 29 | * - smart tree reduction |
| 30 | */ |
| 31 | |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/time.h> |
| 34 | #include <linux/jbd2.h> |
| 35 | #include <linux/highuid.h> |
| 36 | #include <linux/pagemap.h> |
| 37 | #include <linux/quotaops.h> |
| 38 | #include <linux/string.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/falloc.h> |
| 41 | #include <asm/uaccess.h> |
| 42 | #include <linux/fiemap.h> |
| 43 | #include "ext4_jbd2.h" |
| 44 | |
| 45 | #include <trace/events/ext4.h> |
| 46 | |
| 47 | /* |
| 48 | * used by extent splitting. |
| 49 | */ |
| 50 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ |
| 51 | due to ENOSPC */ |
| 52 | #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */ |
| 53 | #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */ |
| 54 | |
| 55 | #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */ |
| 56 | #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ |
| 57 | |
| 58 | static int ext4_split_extent(handle_t *handle, |
| 59 | struct inode *inode, |
| 60 | struct ext4_ext_path *path, |
| 61 | struct ext4_map_blocks *map, |
| 62 | int split_flag, |
| 63 | int flags); |
| 64 | |
| 65 | static int ext4_split_extent_at(handle_t *handle, |
| 66 | struct inode *inode, |
| 67 | struct ext4_ext_path *path, |
| 68 | ext4_lblk_t split, |
| 69 | int split_flag, |
| 70 | int flags); |
| 71 | |
| 72 | static int ext4_ext_truncate_extend_restart(handle_t *handle, |
| 73 | struct inode *inode, |
| 74 | int needed) |
| 75 | { |
| 76 | int err; |
| 77 | |
| 78 | if (!ext4_handle_valid(handle)) |
| 79 | return 0; |
| 80 | if (handle->h_buffer_credits > needed) |
| 81 | return 0; |
| 82 | err = ext4_journal_extend(handle, needed); |
| 83 | if (err <= 0) |
| 84 | return err; |
| 85 | err = ext4_truncate_restart_trans(handle, inode, needed); |
| 86 | if (err == 0) |
| 87 | err = -EAGAIN; |
| 88 | |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * could return: |
| 94 | * - EROFS |
| 95 | * - ENOMEM |
| 96 | */ |
| 97 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 98 | struct ext4_ext_path *path) |
| 99 | { |
| 100 | if (path->p_bh) { |
| 101 | /* path points to block */ |
| 102 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 103 | } |
| 104 | /* path points to leaf/index in inode body */ |
| 105 | /* we use in-core data, no need to protect them */ |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * could return: |
| 111 | * - EROFS |
| 112 | * - ENOMEM |
| 113 | * - EIO |
| 114 | */ |
| 115 | #define ext4_ext_dirty(handle, inode, path) \ |
| 116 | __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) |
| 117 | static int __ext4_ext_dirty(const char *where, unsigned int line, |
| 118 | handle_t *handle, struct inode *inode, |
| 119 | struct ext4_ext_path *path) |
| 120 | { |
| 121 | int err; |
| 122 | if (path->p_bh) { |
| 123 | /* path points to block */ |
| 124 | err = __ext4_handle_dirty_metadata(where, line, handle, |
| 125 | inode, path->p_bh); |
| 126 | } else { |
| 127 | /* path points to leaf/index in inode body */ |
| 128 | err = ext4_mark_inode_dirty(handle, inode); |
| 129 | } |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
| 134 | struct ext4_ext_path *path, |
| 135 | ext4_lblk_t block) |
| 136 | { |
| 137 | if (path) { |
| 138 | int depth = path->p_depth; |
| 139 | struct ext4_extent *ex; |
| 140 | |
| 141 | /* |
| 142 | * Try to predict block placement assuming that we are |
| 143 | * filling in a file which will eventually be |
| 144 | * non-sparse --- i.e., in the case of libbfd writing |
| 145 | * an ELF object sections out-of-order but in a way |
| 146 | * the eventually results in a contiguous object or |
| 147 | * executable file, or some database extending a table |
| 148 | * space file. However, this is actually somewhat |
| 149 | * non-ideal if we are writing a sparse file such as |
| 150 | * qemu or KVM writing a raw image file that is going |
| 151 | * to stay fairly sparse, since it will end up |
| 152 | * fragmenting the file system's free space. Maybe we |
| 153 | * should have some hueristics or some way to allow |
| 154 | * userspace to pass a hint to file system, |
| 155 | * especially if the latter case turns out to be |
| 156 | * common. |
| 157 | */ |
| 158 | ex = path[depth].p_ext; |
| 159 | if (ex) { |
| 160 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
| 161 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); |
| 162 | |
| 163 | if (block > ext_block) |
| 164 | return ext_pblk + (block - ext_block); |
| 165 | else |
| 166 | return ext_pblk - (ext_block - block); |
| 167 | } |
| 168 | |
| 169 | /* it looks like index is empty; |
| 170 | * try to find starting block from index itself */ |
| 171 | if (path[depth].p_bh) |
| 172 | return path[depth].p_bh->b_blocknr; |
| 173 | } |
| 174 | |
| 175 | /* OK. use inode's group */ |
| 176 | return ext4_inode_to_goal_block(inode); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Allocation for a meta data block |
| 181 | */ |
| 182 | static ext4_fsblk_t |
| 183 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
| 184 | struct ext4_ext_path *path, |
| 185 | struct ext4_extent *ex, int *err, unsigned int flags) |
| 186 | { |
| 187 | ext4_fsblk_t goal, newblock; |
| 188 | |
| 189 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); |
| 190 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 191 | NULL, err); |
| 192 | return newblock; |
| 193 | } |
| 194 | |
| 195 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
| 196 | { |
| 197 | int size; |
| 198 | |
| 199 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 200 | / sizeof(struct ext4_extent); |
| 201 | #ifdef AGGRESSIVE_TEST |
| 202 | if (!check && size > 6) |
| 203 | size = 6; |
| 204 | #endif |
| 205 | return size; |
| 206 | } |
| 207 | |
| 208 | static inline int ext4_ext_space_block_idx(struct inode *inode, int check) |
| 209 | { |
| 210 | int size; |
| 211 | |
| 212 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 213 | / sizeof(struct ext4_extent_idx); |
| 214 | #ifdef AGGRESSIVE_TEST |
| 215 | if (!check && size > 5) |
| 216 | size = 5; |
| 217 | #endif |
| 218 | return size; |
| 219 | } |
| 220 | |
| 221 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
| 222 | { |
| 223 | int size; |
| 224 | |
| 225 | size = sizeof(EXT4_I(inode)->i_data); |
| 226 | size -= sizeof(struct ext4_extent_header); |
| 227 | size /= sizeof(struct ext4_extent); |
| 228 | #ifdef AGGRESSIVE_TEST |
| 229 | if (!check && size > 3) |
| 230 | size = 3; |
| 231 | #endif |
| 232 | return size; |
| 233 | } |
| 234 | |
| 235 | static inline int ext4_ext_space_root_idx(struct inode *inode, int check) |
| 236 | { |
| 237 | int size; |
| 238 | |
| 239 | size = sizeof(EXT4_I(inode)->i_data); |
| 240 | size -= sizeof(struct ext4_extent_header); |
| 241 | size /= sizeof(struct ext4_extent_idx); |
| 242 | #ifdef AGGRESSIVE_TEST |
| 243 | if (!check && size > 4) |
| 244 | size = 4; |
| 245 | #endif |
| 246 | return size; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Calculate the number of metadata blocks needed |
| 251 | * to allocate @blocks |
| 252 | * Worse case is one block per extent |
| 253 | */ |
| 254 | int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) |
| 255 | { |
| 256 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 257 | int idxs; |
| 258 | |
| 259 | idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 260 | / sizeof(struct ext4_extent_idx)); |
| 261 | |
| 262 | /* |
| 263 | * If the new delayed allocation block is contiguous with the |
| 264 | * previous da block, it can share index blocks with the |
| 265 | * previous block, so we only need to allocate a new index |
| 266 | * block every idxs leaf blocks. At ldxs**2 blocks, we need |
| 267 | * an additional index block, and at ldxs**3 blocks, yet |
| 268 | * another index blocks. |
| 269 | */ |
| 270 | if (ei->i_da_metadata_calc_len && |
| 271 | ei->i_da_metadata_calc_last_lblock+1 == lblock) { |
| 272 | int num = 0; |
| 273 | |
| 274 | if ((ei->i_da_metadata_calc_len % idxs) == 0) |
| 275 | num++; |
| 276 | if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) |
| 277 | num++; |
| 278 | if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { |
| 279 | num++; |
| 280 | ei->i_da_metadata_calc_len = 0; |
| 281 | } else |
| 282 | ei->i_da_metadata_calc_len++; |
| 283 | ei->i_da_metadata_calc_last_lblock++; |
| 284 | return num; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * In the worst case we need a new set of index blocks at |
| 289 | * every level of the inode's extent tree. |
| 290 | */ |
| 291 | ei->i_da_metadata_calc_len = 1; |
| 292 | ei->i_da_metadata_calc_last_lblock = lblock; |
| 293 | return ext_depth(inode) + 1; |
| 294 | } |
| 295 | |
| 296 | static int |
| 297 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 298 | { |
| 299 | int max; |
| 300 | |
| 301 | if (depth == ext_depth(inode)) { |
| 302 | if (depth == 0) |
| 303 | max = ext4_ext_space_root(inode, 1); |
| 304 | else |
| 305 | max = ext4_ext_space_root_idx(inode, 1); |
| 306 | } else { |
| 307 | if (depth == 0) |
| 308 | max = ext4_ext_space_block(inode, 1); |
| 309 | else |
| 310 | max = ext4_ext_space_block_idx(inode, 1); |
| 311 | } |
| 312 | |
| 313 | return max; |
| 314 | } |
| 315 | |
| 316 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 317 | { |
| 318 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
| 319 | int len = ext4_ext_get_actual_len(ext); |
| 320 | ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); |
| 321 | ext4_lblk_t last = lblock + len - 1; |
| 322 | |
| 323 | if (len == 0 || lblock > last) |
| 324 | return 0; |
| 325 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); |
| 326 | } |
| 327 | |
| 328 | static int ext4_valid_extent_idx(struct inode *inode, |
| 329 | struct ext4_extent_idx *ext_idx) |
| 330 | { |
| 331 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
| 332 | |
| 333 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); |
| 334 | } |
| 335 | |
| 336 | static int ext4_valid_extent_entries(struct inode *inode, |
| 337 | struct ext4_extent_header *eh, |
| 338 | int depth) |
| 339 | { |
| 340 | unsigned short entries; |
| 341 | if (eh->eh_entries == 0) |
| 342 | return 1; |
| 343 | |
| 344 | entries = le16_to_cpu(eh->eh_entries); |
| 345 | |
| 346 | if (depth == 0) { |
| 347 | /* leaf entries */ |
| 348 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
| 349 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
| 350 | ext4_fsblk_t pblock = 0; |
| 351 | ext4_lblk_t lblock = 0; |
| 352 | ext4_lblk_t prev = 0; |
| 353 | int len = 0; |
| 354 | while (entries) { |
| 355 | if (!ext4_valid_extent(inode, ext)) |
| 356 | return 0; |
| 357 | |
| 358 | /* Check for overlapping extents */ |
| 359 | lblock = le32_to_cpu(ext->ee_block); |
| 360 | len = ext4_ext_get_actual_len(ext); |
| 361 | if ((lblock <= prev) && prev) { |
| 362 | pblock = ext4_ext_pblock(ext); |
| 363 | es->s_last_error_block = cpu_to_le64(pblock); |
| 364 | return 0; |
| 365 | } |
| 366 | ext++; |
| 367 | entries--; |
| 368 | prev = lblock + len - 1; |
| 369 | } |
| 370 | } else { |
| 371 | struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); |
| 372 | while (entries) { |
| 373 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 374 | return 0; |
| 375 | ext_idx++; |
| 376 | entries--; |
| 377 | } |
| 378 | } |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | static int __ext4_ext_check(const char *function, unsigned int line, |
| 383 | struct inode *inode, struct ext4_extent_header *eh, |
| 384 | int depth) |
| 385 | { |
| 386 | const char *error_msg; |
| 387 | int max = 0; |
| 388 | |
| 389 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 390 | error_msg = "invalid magic"; |
| 391 | goto corrupted; |
| 392 | } |
| 393 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 394 | error_msg = "unexpected eh_depth"; |
| 395 | goto corrupted; |
| 396 | } |
| 397 | if (unlikely(eh->eh_max == 0)) { |
| 398 | error_msg = "invalid eh_max"; |
| 399 | goto corrupted; |
| 400 | } |
| 401 | max = ext4_ext_max_entries(inode, depth); |
| 402 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 403 | error_msg = "too large eh_max"; |
| 404 | goto corrupted; |
| 405 | } |
| 406 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 407 | error_msg = "invalid eh_entries"; |
| 408 | goto corrupted; |
| 409 | } |
| 410 | if (!ext4_valid_extent_entries(inode, eh, depth)) { |
| 411 | error_msg = "invalid extent entries"; |
| 412 | goto corrupted; |
| 413 | } |
| 414 | return 0; |
| 415 | |
| 416 | corrupted: |
| 417 | ext4_error_inode(inode, function, line, 0, |
| 418 | "bad header/extent: %s - magic %x, " |
| 419 | "entries %u, max %u(%u), depth %u(%u)", |
| 420 | error_msg, le16_to_cpu(eh->eh_magic), |
| 421 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
| 422 | max, le16_to_cpu(eh->eh_depth), depth); |
| 423 | |
| 424 | return -EIO; |
| 425 | } |
| 426 | |
| 427 | #define ext4_ext_check(inode, eh, depth) \ |
| 428 | __ext4_ext_check(__func__, __LINE__, inode, eh, depth) |
| 429 | |
| 430 | int ext4_ext_check_inode(struct inode *inode) |
| 431 | { |
| 432 | return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); |
| 433 | } |
| 434 | |
| 435 | #ifdef EXT_DEBUG |
| 436 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 437 | { |
| 438 | int k, l = path->p_depth; |
| 439 | |
| 440 | ext_debug("path:"); |
| 441 | for (k = 0; k <= l; k++, path++) { |
| 442 | if (path->p_idx) { |
| 443 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
| 444 | ext4_idx_pblock(path->p_idx)); |
| 445 | } else if (path->p_ext) { |
| 446 | ext_debug(" %d:[%d]%d:%llu ", |
| 447 | le32_to_cpu(path->p_ext->ee_block), |
| 448 | ext4_ext_is_uninitialized(path->p_ext), |
| 449 | ext4_ext_get_actual_len(path->p_ext), |
| 450 | ext4_ext_pblock(path->p_ext)); |
| 451 | } else |
| 452 | ext_debug(" []"); |
| 453 | } |
| 454 | ext_debug("\n"); |
| 455 | } |
| 456 | |
| 457 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 458 | { |
| 459 | int depth = ext_depth(inode); |
| 460 | struct ext4_extent_header *eh; |
| 461 | struct ext4_extent *ex; |
| 462 | int i; |
| 463 | |
| 464 | if (!path) |
| 465 | return; |
| 466 | |
| 467 | eh = path[depth].p_hdr; |
| 468 | ex = EXT_FIRST_EXTENT(eh); |
| 469 | |
| 470 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
| 471 | |
| 472 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
| 473 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
| 474 | ext4_ext_is_uninitialized(ex), |
| 475 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
| 476 | } |
| 477 | ext_debug("\n"); |
| 478 | } |
| 479 | |
| 480 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 481 | ext4_fsblk_t newblock, int level) |
| 482 | { |
| 483 | int depth = ext_depth(inode); |
| 484 | struct ext4_extent *ex; |
| 485 | |
| 486 | if (depth != level) { |
| 487 | struct ext4_extent_idx *idx; |
| 488 | idx = path[level].p_idx; |
| 489 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
| 490 | ext_debug("%d: move %d:%llu in new index %llu\n", level, |
| 491 | le32_to_cpu(idx->ei_block), |
| 492 | ext4_idx_pblock(idx), |
| 493 | newblock); |
| 494 | idx++; |
| 495 | } |
| 496 | |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | ex = path[depth].p_ext; |
| 501 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 502 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
| 503 | le32_to_cpu(ex->ee_block), |
| 504 | ext4_ext_pblock(ex), |
| 505 | ext4_ext_is_uninitialized(ex), |
| 506 | ext4_ext_get_actual_len(ex), |
| 507 | newblock); |
| 508 | ex++; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | #else |
| 513 | #define ext4_ext_show_path(inode, path) |
| 514 | #define ext4_ext_show_leaf(inode, path) |
| 515 | #define ext4_ext_show_move(inode, path, newblock, level) |
| 516 | #endif |
| 517 | |
| 518 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
| 519 | { |
| 520 | int depth = path->p_depth; |
| 521 | int i; |
| 522 | |
| 523 | for (i = 0; i <= depth; i++, path++) |
| 524 | if (path->p_bh) { |
| 525 | brelse(path->p_bh); |
| 526 | path->p_bh = NULL; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * ext4_ext_binsearch_idx: |
| 532 | * binary search for the closest index of the given block |
| 533 | * the header must be checked before calling this |
| 534 | */ |
| 535 | static void |
| 536 | ext4_ext_binsearch_idx(struct inode *inode, |
| 537 | struct ext4_ext_path *path, ext4_lblk_t block) |
| 538 | { |
| 539 | struct ext4_extent_header *eh = path->p_hdr; |
| 540 | struct ext4_extent_idx *r, *l, *m; |
| 541 | |
| 542 | |
| 543 | ext_debug("binsearch for %u(idx): ", block); |
| 544 | |
| 545 | l = EXT_FIRST_INDEX(eh) + 1; |
| 546 | r = EXT_LAST_INDEX(eh); |
| 547 | while (l <= r) { |
| 548 | m = l + (r - l) / 2; |
| 549 | if (block < le32_to_cpu(m->ei_block)) |
| 550 | r = m - 1; |
| 551 | else |
| 552 | l = m + 1; |
| 553 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 554 | m, le32_to_cpu(m->ei_block), |
| 555 | r, le32_to_cpu(r->ei_block)); |
| 556 | } |
| 557 | |
| 558 | path->p_idx = l - 1; |
| 559 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), |
| 560 | ext4_idx_pblock(path->p_idx)); |
| 561 | |
| 562 | #ifdef CHECK_BINSEARCH |
| 563 | { |
| 564 | struct ext4_extent_idx *chix, *ix; |
| 565 | int k; |
| 566 | |
| 567 | chix = ix = EXT_FIRST_INDEX(eh); |
| 568 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
| 569 | if (k != 0 && |
| 570 | le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { |
| 571 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 572 | "first=0x%p\n", k, |
| 573 | ix, EXT_FIRST_INDEX(eh)); |
| 574 | printk(KERN_DEBUG "%u <= %u\n", |
| 575 | le32_to_cpu(ix->ei_block), |
| 576 | le32_to_cpu(ix[-1].ei_block)); |
| 577 | } |
| 578 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
| 579 | <= le32_to_cpu(ix[-1].ei_block)); |
| 580 | if (block < le32_to_cpu(ix->ei_block)) |
| 581 | break; |
| 582 | chix = ix; |
| 583 | } |
| 584 | BUG_ON(chix != path->p_idx); |
| 585 | } |
| 586 | #endif |
| 587 | |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * ext4_ext_binsearch: |
| 592 | * binary search for closest extent of the given block |
| 593 | * the header must be checked before calling this |
| 594 | */ |
| 595 | static void |
| 596 | ext4_ext_binsearch(struct inode *inode, |
| 597 | struct ext4_ext_path *path, ext4_lblk_t block) |
| 598 | { |
| 599 | struct ext4_extent_header *eh = path->p_hdr; |
| 600 | struct ext4_extent *r, *l, *m; |
| 601 | |
| 602 | if (eh->eh_entries == 0) { |
| 603 | /* |
| 604 | * this leaf is empty: |
| 605 | * we get such a leaf in split/add case |
| 606 | */ |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | ext_debug("binsearch for %u: ", block); |
| 611 | |
| 612 | l = EXT_FIRST_EXTENT(eh) + 1; |
| 613 | r = EXT_LAST_EXTENT(eh); |
| 614 | |
| 615 | while (l <= r) { |
| 616 | m = l + (r - l) / 2; |
| 617 | if (block < le32_to_cpu(m->ee_block)) |
| 618 | r = m - 1; |
| 619 | else |
| 620 | l = m + 1; |
| 621 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 622 | m, le32_to_cpu(m->ee_block), |
| 623 | r, le32_to_cpu(r->ee_block)); |
| 624 | } |
| 625 | |
| 626 | path->p_ext = l - 1; |
| 627 | ext_debug(" -> %d:%llu:[%d]%d ", |
| 628 | le32_to_cpu(path->p_ext->ee_block), |
| 629 | ext4_ext_pblock(path->p_ext), |
| 630 | ext4_ext_is_uninitialized(path->p_ext), |
| 631 | ext4_ext_get_actual_len(path->p_ext)); |
| 632 | |
| 633 | #ifdef CHECK_BINSEARCH |
| 634 | { |
| 635 | struct ext4_extent *chex, *ex; |
| 636 | int k; |
| 637 | |
| 638 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 639 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 640 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
| 641 | <= le32_to_cpu(ex[-1].ee_block)); |
| 642 | if (block < le32_to_cpu(ex->ee_block)) |
| 643 | break; |
| 644 | chex = ex; |
| 645 | } |
| 646 | BUG_ON(chex != path->p_ext); |
| 647 | } |
| 648 | #endif |
| 649 | |
| 650 | } |
| 651 | |
| 652 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 653 | { |
| 654 | struct ext4_extent_header *eh; |
| 655 | |
| 656 | eh = ext_inode_hdr(inode); |
| 657 | eh->eh_depth = 0; |
| 658 | eh->eh_entries = 0; |
| 659 | eh->eh_magic = EXT4_EXT_MAGIC; |
| 660 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
| 661 | ext4_mark_inode_dirty(handle, inode); |
| 662 | ext4_ext_invalidate_cache(inode); |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | struct ext4_ext_path * |
| 667 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, |
| 668 | struct ext4_ext_path *path) |
| 669 | { |
| 670 | struct ext4_extent_header *eh; |
| 671 | struct buffer_head *bh; |
| 672 | short int depth, i, ppos = 0, alloc = 0; |
| 673 | int ret; |
| 674 | |
| 675 | eh = ext_inode_hdr(inode); |
| 676 | depth = ext_depth(inode); |
| 677 | |
| 678 | /* account possible depth increase */ |
| 679 | if (!path) { |
| 680 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
| 681 | GFP_NOFS); |
| 682 | if (!path) |
| 683 | return ERR_PTR(-ENOMEM); |
| 684 | alloc = 1; |
| 685 | } |
| 686 | path[0].p_hdr = eh; |
| 687 | path[0].p_bh = NULL; |
| 688 | |
| 689 | i = depth; |
| 690 | /* walk through the tree */ |
| 691 | while (i) { |
| 692 | int need_to_validate = 0; |
| 693 | |
| 694 | ext_debug("depth %d: num %d, max %d\n", |
| 695 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 696 | |
| 697 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
| 698 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
| 699 | path[ppos].p_depth = i; |
| 700 | path[ppos].p_ext = NULL; |
| 701 | |
| 702 | bh = sb_getblk(inode->i_sb, path[ppos].p_block); |
| 703 | if (unlikely(!bh)) { |
| 704 | ret = -ENOMEM; |
| 705 | goto err; |
| 706 | } |
| 707 | if (!bh_uptodate_or_lock(bh)) { |
| 708 | trace_ext4_ext_load_extent(inode, block, |
| 709 | path[ppos].p_block); |
| 710 | ret = bh_submit_read(bh); |
| 711 | if (ret < 0) { |
| 712 | put_bh(bh); |
| 713 | goto err; |
| 714 | } |
| 715 | /* validate the extent entries */ |
| 716 | need_to_validate = 1; |
| 717 | } |
| 718 | eh = ext_block_hdr(bh); |
| 719 | ppos++; |
| 720 | if (unlikely(ppos > depth)) { |
| 721 | put_bh(bh); |
| 722 | EXT4_ERROR_INODE(inode, |
| 723 | "ppos %d > depth %d", ppos, depth); |
| 724 | ret = -EIO; |
| 725 | goto err; |
| 726 | } |
| 727 | path[ppos].p_bh = bh; |
| 728 | path[ppos].p_hdr = eh; |
| 729 | i--; |
| 730 | |
| 731 | ret = need_to_validate ? ext4_ext_check(inode, eh, i) : 0; |
| 732 | if (ret < 0) |
| 733 | goto err; |
| 734 | } |
| 735 | |
| 736 | path[ppos].p_depth = i; |
| 737 | path[ppos].p_ext = NULL; |
| 738 | path[ppos].p_idx = NULL; |
| 739 | |
| 740 | /* find extent */ |
| 741 | ext4_ext_binsearch(inode, path + ppos, block); |
| 742 | /* if not an empty leaf */ |
| 743 | if (path[ppos].p_ext) |
| 744 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
| 745 | |
| 746 | ext4_ext_show_path(inode, path); |
| 747 | |
| 748 | return path; |
| 749 | |
| 750 | err: |
| 751 | ext4_ext_drop_refs(path); |
| 752 | if (alloc) |
| 753 | kfree(path); |
| 754 | return ERR_PTR(ret); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * ext4_ext_insert_index: |
| 759 | * insert new index [@logical;@ptr] into the block at @curp; |
| 760 | * check where to insert: before @curp or after @curp |
| 761 | */ |
| 762 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 763 | struct ext4_ext_path *curp, |
| 764 | int logical, ext4_fsblk_t ptr) |
| 765 | { |
| 766 | struct ext4_extent_idx *ix; |
| 767 | int len, err; |
| 768 | |
| 769 | err = ext4_ext_get_access(handle, inode, curp); |
| 770 | if (err) |
| 771 | return err; |
| 772 | |
| 773 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 774 | EXT4_ERROR_INODE(inode, |
| 775 | "logical %d == ei_block %d!", |
| 776 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
| 777 | return -EIO; |
| 778 | } |
| 779 | |
| 780 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 781 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 782 | EXT4_ERROR_INODE(inode, |
| 783 | "eh_entries %d >= eh_max %d!", |
| 784 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 785 | le16_to_cpu(curp->p_hdr->eh_max)); |
| 786 | return -EIO; |
| 787 | } |
| 788 | |
| 789 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 790 | /* insert after */ |
| 791 | ext_debug("insert new index %d after: %llu\n", logical, ptr); |
| 792 | ix = curp->p_idx + 1; |
| 793 | } else { |
| 794 | /* insert before */ |
| 795 | ext_debug("insert new index %d before: %llu\n", logical, ptr); |
| 796 | ix = curp->p_idx; |
| 797 | } |
| 798 | |
| 799 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
| 800 | BUG_ON(len < 0); |
| 801 | if (len > 0) { |
| 802 | ext_debug("insert new index %d: " |
| 803 | "move %d indices from 0x%p to 0x%p\n", |
| 804 | logical, len, ix, ix + 1); |
| 805 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
| 806 | } |
| 807 | |
| 808 | if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { |
| 809 | EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); |
| 810 | return -EIO; |
| 811 | } |
| 812 | |
| 813 | ix->ei_block = cpu_to_le32(logical); |
| 814 | ext4_idx_store_pblock(ix, ptr); |
| 815 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
| 816 | |
| 817 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 818 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
| 819 | return -EIO; |
| 820 | } |
| 821 | |
| 822 | err = ext4_ext_dirty(handle, inode, curp); |
| 823 | ext4_std_error(inode->i_sb, err); |
| 824 | |
| 825 | return err; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * ext4_ext_split: |
| 830 | * inserts new subtree into the path, using free index entry |
| 831 | * at depth @at: |
| 832 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 833 | * - makes decision where to split |
| 834 | * - moves remaining extents and index entries (right to the split point) |
| 835 | * into the newly allocated blocks |
| 836 | * - initializes subtree |
| 837 | */ |
| 838 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
| 839 | unsigned int flags, |
| 840 | struct ext4_ext_path *path, |
| 841 | struct ext4_extent *newext, int at) |
| 842 | { |
| 843 | struct buffer_head *bh = NULL; |
| 844 | int depth = ext_depth(inode); |
| 845 | struct ext4_extent_header *neh; |
| 846 | struct ext4_extent_idx *fidx; |
| 847 | int i = at, k, m, a; |
| 848 | ext4_fsblk_t newblock, oldblock; |
| 849 | __le32 border; |
| 850 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
| 851 | int err = 0; |
| 852 | |
| 853 | /* make decision: where to split? */ |
| 854 | /* FIXME: now decision is simplest: at current extent */ |
| 855 | |
| 856 | /* if current leaf will be split, then we should use |
| 857 | * border from split point */ |
| 858 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 859 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
| 860 | return -EIO; |
| 861 | } |
| 862 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 863 | border = path[depth].p_ext[1].ee_block; |
| 864 | ext_debug("leaf will be split." |
| 865 | " next leaf starts at %d\n", |
| 866 | le32_to_cpu(border)); |
| 867 | } else { |
| 868 | border = newext->ee_block; |
| 869 | ext_debug("leaf will be added." |
| 870 | " next leaf starts at %d\n", |
| 871 | le32_to_cpu(border)); |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * If error occurs, then we break processing |
| 876 | * and mark filesystem read-only. index won't |
| 877 | * be inserted and tree will be in consistent |
| 878 | * state. Next mount will repair buffers too. |
| 879 | */ |
| 880 | |
| 881 | /* |
| 882 | * Get array to track all allocated blocks. |
| 883 | * We need this to handle errors and free blocks |
| 884 | * upon them. |
| 885 | */ |
| 886 | ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); |
| 887 | if (!ablocks) |
| 888 | return -ENOMEM; |
| 889 | |
| 890 | /* allocate all needed blocks */ |
| 891 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 892 | for (a = 0; a < depth - at; a++) { |
| 893 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
| 894 | newext, &err, flags); |
| 895 | if (newblock == 0) |
| 896 | goto cleanup; |
| 897 | ablocks[a] = newblock; |
| 898 | } |
| 899 | |
| 900 | /* initialize new leaf */ |
| 901 | newblock = ablocks[--a]; |
| 902 | if (unlikely(newblock == 0)) { |
| 903 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
| 904 | err = -EIO; |
| 905 | goto cleanup; |
| 906 | } |
| 907 | bh = sb_getblk(inode->i_sb, newblock); |
| 908 | if (!bh) { |
| 909 | err = -ENOMEM; |
| 910 | goto cleanup; |
| 911 | } |
| 912 | lock_buffer(bh); |
| 913 | |
| 914 | err = ext4_journal_get_create_access(handle, bh); |
| 915 | if (err) |
| 916 | goto cleanup; |
| 917 | |
| 918 | neh = ext_block_hdr(bh); |
| 919 | neh->eh_entries = 0; |
| 920 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
| 921 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 922 | neh->eh_depth = 0; |
| 923 | |
| 924 | /* move remainder of path[depth] to the new leaf */ |
| 925 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 926 | path[depth].p_hdr->eh_max)) { |
| 927 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 928 | path[depth].p_hdr->eh_entries, |
| 929 | path[depth].p_hdr->eh_max); |
| 930 | err = -EIO; |
| 931 | goto cleanup; |
| 932 | } |
| 933 | /* start copy from next extent */ |
| 934 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 935 | ext4_ext_show_move(inode, path, newblock, depth); |
| 936 | if (m) { |
| 937 | struct ext4_extent *ex; |
| 938 | ex = EXT_FIRST_EXTENT(neh); |
| 939 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
| 940 | le16_add_cpu(&neh->eh_entries, m); |
| 941 | } |
| 942 | |
| 943 | set_buffer_uptodate(bh); |
| 944 | unlock_buffer(bh); |
| 945 | |
| 946 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
| 947 | if (err) |
| 948 | goto cleanup; |
| 949 | brelse(bh); |
| 950 | bh = NULL; |
| 951 | |
| 952 | /* correct old leaf */ |
| 953 | if (m) { |
| 954 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 955 | if (err) |
| 956 | goto cleanup; |
| 957 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
| 958 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 959 | if (err) |
| 960 | goto cleanup; |
| 961 | |
| 962 | } |
| 963 | |
| 964 | /* create intermediate indexes */ |
| 965 | k = depth - at - 1; |
| 966 | if (unlikely(k < 0)) { |
| 967 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
| 968 | err = -EIO; |
| 969 | goto cleanup; |
| 970 | } |
| 971 | if (k) |
| 972 | ext_debug("create %d intermediate indices\n", k); |
| 973 | /* insert new index into current index block */ |
| 974 | /* current depth stored in i var */ |
| 975 | i = depth - 1; |
| 976 | while (k--) { |
| 977 | oldblock = newblock; |
| 978 | newblock = ablocks[--a]; |
| 979 | bh = sb_getblk(inode->i_sb, newblock); |
| 980 | if (!bh) { |
| 981 | err = -ENOMEM; |
| 982 | goto cleanup; |
| 983 | } |
| 984 | lock_buffer(bh); |
| 985 | |
| 986 | err = ext4_journal_get_create_access(handle, bh); |
| 987 | if (err) |
| 988 | goto cleanup; |
| 989 | |
| 990 | neh = ext_block_hdr(bh); |
| 991 | neh->eh_entries = cpu_to_le16(1); |
| 992 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 993 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); |
| 994 | neh->eh_depth = cpu_to_le16(depth - i); |
| 995 | fidx = EXT_FIRST_INDEX(neh); |
| 996 | fidx->ei_block = border; |
| 997 | ext4_idx_store_pblock(fidx, oldblock); |
| 998 | |
| 999 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 1000 | i, newblock, le32_to_cpu(border), oldblock); |
| 1001 | |
| 1002 | /* move remainder of path[i] to the new index block */ |
| 1003 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 1004 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 1005 | EXT4_ERROR_INODE(inode, |
| 1006 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 1007 | le32_to_cpu(path[i].p_ext->ee_block)); |
| 1008 | err = -EIO; |
| 1009 | goto cleanup; |
| 1010 | } |
| 1011 | /* start copy indexes */ |
| 1012 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
| 1013 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 1014 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 1015 | ext4_ext_show_move(inode, path, newblock, i); |
| 1016 | if (m) { |
| 1017 | memmove(++fidx, path[i].p_idx, |
| 1018 | sizeof(struct ext4_extent_idx) * m); |
| 1019 | le16_add_cpu(&neh->eh_entries, m); |
| 1020 | } |
| 1021 | set_buffer_uptodate(bh); |
| 1022 | unlock_buffer(bh); |
| 1023 | |
| 1024 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
| 1025 | if (err) |
| 1026 | goto cleanup; |
| 1027 | brelse(bh); |
| 1028 | bh = NULL; |
| 1029 | |
| 1030 | /* correct old index */ |
| 1031 | if (m) { |
| 1032 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1033 | if (err) |
| 1034 | goto cleanup; |
| 1035 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
| 1036 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1037 | if (err) |
| 1038 | goto cleanup; |
| 1039 | } |
| 1040 | |
| 1041 | i--; |
| 1042 | } |
| 1043 | |
| 1044 | /* insert new index */ |
| 1045 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1046 | le32_to_cpu(border), newblock); |
| 1047 | |
| 1048 | cleanup: |
| 1049 | if (bh) { |
| 1050 | if (buffer_locked(bh)) |
| 1051 | unlock_buffer(bh); |
| 1052 | brelse(bh); |
| 1053 | } |
| 1054 | |
| 1055 | if (err) { |
| 1056 | /* free all allocated blocks in error case */ |
| 1057 | for (i = 0; i < depth; i++) { |
| 1058 | if (!ablocks[i]) |
| 1059 | continue; |
| 1060 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
| 1061 | EXT4_FREE_BLOCKS_METADATA); |
| 1062 | } |
| 1063 | } |
| 1064 | kfree(ablocks); |
| 1065 | |
| 1066 | return err; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * ext4_ext_grow_indepth: |
| 1071 | * implements tree growing procedure: |
| 1072 | * - allocates new block |
| 1073 | * - moves top-level data (index block or leaf) into the new block |
| 1074 | * - initializes new top-level, creating index that points to the |
| 1075 | * just created block |
| 1076 | */ |
| 1077 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
| 1078 | unsigned int flags, |
| 1079 | struct ext4_extent *newext) |
| 1080 | { |
| 1081 | struct ext4_extent_header *neh; |
| 1082 | struct buffer_head *bh; |
| 1083 | ext4_fsblk_t newblock; |
| 1084 | int err = 0; |
| 1085 | |
| 1086 | newblock = ext4_ext_new_meta_block(handle, inode, NULL, |
| 1087 | newext, &err, flags); |
| 1088 | if (newblock == 0) |
| 1089 | return err; |
| 1090 | |
| 1091 | bh = sb_getblk(inode->i_sb, newblock); |
| 1092 | if (!bh) |
| 1093 | return -ENOMEM; |
| 1094 | lock_buffer(bh); |
| 1095 | |
| 1096 | err = ext4_journal_get_create_access(handle, bh); |
| 1097 | if (err) { |
| 1098 | unlock_buffer(bh); |
| 1099 | goto out; |
| 1100 | } |
| 1101 | |
| 1102 | /* move top-level index/leaf into new block */ |
| 1103 | memmove(bh->b_data, EXT4_I(inode)->i_data, |
| 1104 | sizeof(EXT4_I(inode)->i_data)); |
| 1105 | |
| 1106 | /* set size of new block */ |
| 1107 | neh = ext_block_hdr(bh); |
| 1108 | /* old root could have indexes or leaves |
| 1109 | * so calculate e_max right way */ |
| 1110 | if (ext_depth(inode)) |
| 1111 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); |
| 1112 | else |
| 1113 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
| 1114 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1115 | set_buffer_uptodate(bh); |
| 1116 | unlock_buffer(bh); |
| 1117 | |
| 1118 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
| 1119 | if (err) |
| 1120 | goto out; |
| 1121 | |
| 1122 | /* Update top-level index: num,max,pointer */ |
| 1123 | neh = ext_inode_hdr(inode); |
| 1124 | neh->eh_entries = cpu_to_le16(1); |
| 1125 | ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); |
| 1126 | if (neh->eh_depth == 0) { |
| 1127 | /* Root extent block becomes index block */ |
| 1128 | neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
| 1129 | EXT_FIRST_INDEX(neh)->ei_block = |
| 1130 | EXT_FIRST_EXTENT(neh)->ee_block; |
| 1131 | } |
| 1132 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
| 1133 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
| 1134 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
| 1135 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
| 1136 | |
| 1137 | neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1); |
| 1138 | ext4_mark_inode_dirty(handle, inode); |
| 1139 | out: |
| 1140 | brelse(bh); |
| 1141 | |
| 1142 | return err; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * ext4_ext_create_new_leaf: |
| 1147 | * finds empty index and adds new leaf. |
| 1148 | * if no free index is found, then it requests in-depth growing. |
| 1149 | */ |
| 1150 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
| 1151 | unsigned int flags, |
| 1152 | struct ext4_ext_path *path, |
| 1153 | struct ext4_extent *newext) |
| 1154 | { |
| 1155 | struct ext4_ext_path *curp; |
| 1156 | int depth, i, err = 0; |
| 1157 | |
| 1158 | repeat: |
| 1159 | i = depth = ext_depth(inode); |
| 1160 | |
| 1161 | /* walk up to the tree and look for free index entry */ |
| 1162 | curp = path + depth; |
| 1163 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1164 | i--; |
| 1165 | curp--; |
| 1166 | } |
| 1167 | |
| 1168 | /* we use already allocated block for index block, |
| 1169 | * so subsequent data blocks should be contiguous */ |
| 1170 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1171 | /* if we found index with free entry, then use that |
| 1172 | * entry: create all needed subtree and add new leaf */ |
| 1173 | err = ext4_ext_split(handle, inode, flags, path, newext, i); |
| 1174 | if (err) |
| 1175 | goto out; |
| 1176 | |
| 1177 | /* refill path */ |
| 1178 | ext4_ext_drop_refs(path); |
| 1179 | path = ext4_ext_find_extent(inode, |
| 1180 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1181 | path); |
| 1182 | if (IS_ERR(path)) |
| 1183 | err = PTR_ERR(path); |
| 1184 | } else { |
| 1185 | /* tree is full, time to grow in depth */ |
| 1186 | err = ext4_ext_grow_indepth(handle, inode, flags, newext); |
| 1187 | if (err) |
| 1188 | goto out; |
| 1189 | |
| 1190 | /* refill path */ |
| 1191 | ext4_ext_drop_refs(path); |
| 1192 | path = ext4_ext_find_extent(inode, |
| 1193 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1194 | path); |
| 1195 | if (IS_ERR(path)) { |
| 1196 | err = PTR_ERR(path); |
| 1197 | goto out; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * only first (depth 0 -> 1) produces free space; |
| 1202 | * in all other cases we have to split the grown tree |
| 1203 | */ |
| 1204 | depth = ext_depth(inode); |
| 1205 | if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { |
| 1206 | /* now we need to split */ |
| 1207 | goto repeat; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | out: |
| 1212 | return err; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * search the closest allocated block to the left for *logical |
| 1217 | * and returns it at @logical + it's physical address at @phys |
| 1218 | * if *logical is the smallest allocated block, the function |
| 1219 | * returns 0 at @phys |
| 1220 | * return value contains 0 (success) or error code |
| 1221 | */ |
| 1222 | static int ext4_ext_search_left(struct inode *inode, |
| 1223 | struct ext4_ext_path *path, |
| 1224 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
| 1225 | { |
| 1226 | struct ext4_extent_idx *ix; |
| 1227 | struct ext4_extent *ex; |
| 1228 | int depth, ee_len; |
| 1229 | |
| 1230 | if (unlikely(path == NULL)) { |
| 1231 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
| 1232 | return -EIO; |
| 1233 | } |
| 1234 | depth = path->p_depth; |
| 1235 | *phys = 0; |
| 1236 | |
| 1237 | if (depth == 0 && path->p_ext == NULL) |
| 1238 | return 0; |
| 1239 | |
| 1240 | /* usually extent in the path covers blocks smaller |
| 1241 | * then *logical, but it can be that extent is the |
| 1242 | * first one in the file */ |
| 1243 | |
| 1244 | ex = path[depth].p_ext; |
| 1245 | ee_len = ext4_ext_get_actual_len(ex); |
| 1246 | if (*logical < le32_to_cpu(ex->ee_block)) { |
| 1247 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1248 | EXT4_ERROR_INODE(inode, |
| 1249 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1250 | *logical, le32_to_cpu(ex->ee_block)); |
| 1251 | return -EIO; |
| 1252 | } |
| 1253 | while (--depth >= 0) { |
| 1254 | ix = path[depth].p_idx; |
| 1255 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1256 | EXT4_ERROR_INODE(inode, |
| 1257 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
| 1258 | ix != NULL ? le32_to_cpu(ix->ei_block) : 0, |
| 1259 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
| 1260 | le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0, |
| 1261 | depth); |
| 1262 | return -EIO; |
| 1263 | } |
| 1264 | } |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1269 | EXT4_ERROR_INODE(inode, |
| 1270 | "logical %d < ee_block %d + ee_len %d!", |
| 1271 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
| 1272 | return -EIO; |
| 1273 | } |
| 1274 | |
| 1275 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
| 1276 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * search the closest allocated block to the right for *logical |
| 1282 | * and returns it at @logical + it's physical address at @phys |
| 1283 | * if *logical is the largest allocated block, the function |
| 1284 | * returns 0 at @phys |
| 1285 | * return value contains 0 (success) or error code |
| 1286 | */ |
| 1287 | static int ext4_ext_search_right(struct inode *inode, |
| 1288 | struct ext4_ext_path *path, |
| 1289 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1290 | struct ext4_extent **ret_ex) |
| 1291 | { |
| 1292 | struct buffer_head *bh = NULL; |
| 1293 | struct ext4_extent_header *eh; |
| 1294 | struct ext4_extent_idx *ix; |
| 1295 | struct ext4_extent *ex; |
| 1296 | ext4_fsblk_t block; |
| 1297 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1298 | int ee_len; |
| 1299 | |
| 1300 | if (unlikely(path == NULL)) { |
| 1301 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
| 1302 | return -EIO; |
| 1303 | } |
| 1304 | depth = path->p_depth; |
| 1305 | *phys = 0; |
| 1306 | |
| 1307 | if (depth == 0 && path->p_ext == NULL) |
| 1308 | return 0; |
| 1309 | |
| 1310 | /* usually extent in the path covers blocks smaller |
| 1311 | * then *logical, but it can be that extent is the |
| 1312 | * first one in the file */ |
| 1313 | |
| 1314 | ex = path[depth].p_ext; |
| 1315 | ee_len = ext4_ext_get_actual_len(ex); |
| 1316 | if (*logical < le32_to_cpu(ex->ee_block)) { |
| 1317 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1318 | EXT4_ERROR_INODE(inode, |
| 1319 | "first_extent(path[%d].p_hdr) != ex", |
| 1320 | depth); |
| 1321 | return -EIO; |
| 1322 | } |
| 1323 | while (--depth >= 0) { |
| 1324 | ix = path[depth].p_idx; |
| 1325 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1326 | EXT4_ERROR_INODE(inode, |
| 1327 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1328 | *logical); |
| 1329 | return -EIO; |
| 1330 | } |
| 1331 | } |
| 1332 | goto found_extent; |
| 1333 | } |
| 1334 | |
| 1335 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1336 | EXT4_ERROR_INODE(inode, |
| 1337 | "logical %d < ee_block %d + ee_len %d!", |
| 1338 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
| 1339 | return -EIO; |
| 1340 | } |
| 1341 | |
| 1342 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1343 | /* next allocated block in this leaf */ |
| 1344 | ex++; |
| 1345 | goto found_extent; |
| 1346 | } |
| 1347 | |
| 1348 | /* go up and search for index to the right */ |
| 1349 | while (--depth >= 0) { |
| 1350 | ix = path[depth].p_idx; |
| 1351 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1352 | goto got_index; |
| 1353 | } |
| 1354 | |
| 1355 | /* we've gone up to the root and found no index to the right */ |
| 1356 | return 0; |
| 1357 | |
| 1358 | got_index: |
| 1359 | /* we've found index to the right, let's |
| 1360 | * follow it and find the closest allocated |
| 1361 | * block to the right */ |
| 1362 | ix++; |
| 1363 | block = ext4_idx_pblock(ix); |
| 1364 | while (++depth < path->p_depth) { |
| 1365 | bh = sb_bread(inode->i_sb, block); |
| 1366 | if (bh == NULL) |
| 1367 | return -EIO; |
| 1368 | eh = ext_block_hdr(bh); |
| 1369 | /* subtract from p_depth to get proper eh_depth */ |
| 1370 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
| 1371 | put_bh(bh); |
| 1372 | return -EIO; |
| 1373 | } |
| 1374 | ix = EXT_FIRST_INDEX(eh); |
| 1375 | block = ext4_idx_pblock(ix); |
| 1376 | put_bh(bh); |
| 1377 | } |
| 1378 | |
| 1379 | bh = sb_bread(inode->i_sb, block); |
| 1380 | if (bh == NULL) |
| 1381 | return -EIO; |
| 1382 | eh = ext_block_hdr(bh); |
| 1383 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
| 1384 | put_bh(bh); |
| 1385 | return -EIO; |
| 1386 | } |
| 1387 | ex = EXT_FIRST_EXTENT(eh); |
| 1388 | found_extent: |
| 1389 | *logical = le32_to_cpu(ex->ee_block); |
| 1390 | *phys = ext4_ext_pblock(ex); |
| 1391 | *ret_ex = ex; |
| 1392 | if (bh) |
| 1393 | put_bh(bh); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * ext4_ext_next_allocated_block: |
| 1399 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
| 1400 | * NOTE: it considers block number from index entry as |
| 1401 | * allocated block. Thus, index entries have to be consistent |
| 1402 | * with leaves. |
| 1403 | */ |
| 1404 | static ext4_lblk_t |
| 1405 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1406 | { |
| 1407 | int depth; |
| 1408 | |
| 1409 | BUG_ON(path == NULL); |
| 1410 | depth = path->p_depth; |
| 1411 | |
| 1412 | if (depth == 0 && path->p_ext == NULL) |
| 1413 | return EXT_MAX_BLOCKS; |
| 1414 | |
| 1415 | while (depth >= 0) { |
| 1416 | if (depth == path->p_depth) { |
| 1417 | /* leaf */ |
| 1418 | if (path[depth].p_ext && |
| 1419 | path[depth].p_ext != |
| 1420 | EXT_LAST_EXTENT(path[depth].p_hdr)) |
| 1421 | return le32_to_cpu(path[depth].p_ext[1].ee_block); |
| 1422 | } else { |
| 1423 | /* index */ |
| 1424 | if (path[depth].p_idx != |
| 1425 | EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1426 | return le32_to_cpu(path[depth].p_idx[1].ei_block); |
| 1427 | } |
| 1428 | depth--; |
| 1429 | } |
| 1430 | |
| 1431 | return EXT_MAX_BLOCKS; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * ext4_ext_next_leaf_block: |
| 1436 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
| 1437 | */ |
| 1438 | static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path) |
| 1439 | { |
| 1440 | int depth; |
| 1441 | |
| 1442 | BUG_ON(path == NULL); |
| 1443 | depth = path->p_depth; |
| 1444 | |
| 1445 | /* zero-tree has no leaf blocks at all */ |
| 1446 | if (depth == 0) |
| 1447 | return EXT_MAX_BLOCKS; |
| 1448 | |
| 1449 | /* go to index block */ |
| 1450 | depth--; |
| 1451 | |
| 1452 | while (depth >= 0) { |
| 1453 | if (path[depth].p_idx != |
| 1454 | EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1455 | return (ext4_lblk_t) |
| 1456 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
| 1457 | depth--; |
| 1458 | } |
| 1459 | |
| 1460 | return EXT_MAX_BLOCKS; |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * ext4_ext_correct_indexes: |
| 1465 | * if leaf gets modified and modified extent is first in the leaf, |
| 1466 | * then we have to correct all indexes above. |
| 1467 | * TODO: do we need to correct tree in all cases? |
| 1468 | */ |
| 1469 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
| 1470 | struct ext4_ext_path *path) |
| 1471 | { |
| 1472 | struct ext4_extent_header *eh; |
| 1473 | int depth = ext_depth(inode); |
| 1474 | struct ext4_extent *ex; |
| 1475 | __le32 border; |
| 1476 | int k, err = 0; |
| 1477 | |
| 1478 | eh = path[depth].p_hdr; |
| 1479 | ex = path[depth].p_ext; |
| 1480 | |
| 1481 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1482 | EXT4_ERROR_INODE(inode, |
| 1483 | "ex %p == NULL or eh %p == NULL", ex, eh); |
| 1484 | return -EIO; |
| 1485 | } |
| 1486 | |
| 1487 | if (depth == 0) { |
| 1488 | /* there is no tree at all */ |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1493 | /* we correct tree if first leaf got modified only */ |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * TODO: we need correction if border is smaller than current one |
| 1499 | */ |
| 1500 | k = depth - 1; |
| 1501 | border = path[depth].p_ext->ee_block; |
| 1502 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1503 | if (err) |
| 1504 | return err; |
| 1505 | path[k].p_idx->ei_block = border; |
| 1506 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1507 | if (err) |
| 1508 | return err; |
| 1509 | |
| 1510 | while (k--) { |
| 1511 | /* change all left-side indexes */ |
| 1512 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1513 | break; |
| 1514 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1515 | if (err) |
| 1516 | break; |
| 1517 | path[k].p_idx->ei_block = border; |
| 1518 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1519 | if (err) |
| 1520 | break; |
| 1521 | } |
| 1522 | |
| 1523 | return err; |
| 1524 | } |
| 1525 | |
| 1526 | int |
| 1527 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
| 1528 | struct ext4_extent *ex2) |
| 1529 | { |
| 1530 | unsigned short ext1_ee_len, ext2_ee_len, max_len; |
| 1531 | |
| 1532 | /* |
| 1533 | * Make sure that either both extents are uninitialized, or |
| 1534 | * both are _not_. |
| 1535 | */ |
| 1536 | if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) |
| 1537 | return 0; |
| 1538 | |
| 1539 | if (ext4_ext_is_uninitialized(ex1)) |
| 1540 | max_len = EXT_UNINIT_MAX_LEN; |
| 1541 | else |
| 1542 | max_len = EXT_INIT_MAX_LEN; |
| 1543 | |
| 1544 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1545 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1546 | |
| 1547 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
| 1548 | le32_to_cpu(ex2->ee_block)) |
| 1549 | return 0; |
| 1550 | |
| 1551 | /* |
| 1552 | * To allow future support for preallocated extents to be added |
| 1553 | * as an RO_COMPAT feature, refuse to merge to extents if |
| 1554 | * this can result in the top bit of ee_len being set. |
| 1555 | */ |
| 1556 | if (ext1_ee_len + ext2_ee_len > max_len) |
| 1557 | return 0; |
| 1558 | #ifdef AGGRESSIVE_TEST |
| 1559 | if (ext1_ee_len >= 4) |
| 1560 | return 0; |
| 1561 | #endif |
| 1562 | |
| 1563 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
| 1564 | return 1; |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1570 | * It always tries to merge towards right. If you want to merge towards |
| 1571 | * left, pass "ex - 1" as argument instead of "ex". |
| 1572 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1573 | * 1 if they got merged. |
| 1574 | */ |
| 1575 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
| 1576 | struct ext4_ext_path *path, |
| 1577 | struct ext4_extent *ex) |
| 1578 | { |
| 1579 | struct ext4_extent_header *eh; |
| 1580 | unsigned int depth, len; |
| 1581 | int merge_done = 0; |
| 1582 | int uninitialized = 0; |
| 1583 | |
| 1584 | depth = ext_depth(inode); |
| 1585 | BUG_ON(path[depth].p_hdr == NULL); |
| 1586 | eh = path[depth].p_hdr; |
| 1587 | |
| 1588 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1589 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1590 | break; |
| 1591 | /* merge with next extent! */ |
| 1592 | if (ext4_ext_is_uninitialized(ex)) |
| 1593 | uninitialized = 1; |
| 1594 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1595 | + ext4_ext_get_actual_len(ex + 1)); |
| 1596 | if (uninitialized) |
| 1597 | ext4_ext_mark_uninitialized(ex); |
| 1598 | |
| 1599 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1600 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1601 | * sizeof(struct ext4_extent); |
| 1602 | memmove(ex + 1, ex + 2, len); |
| 1603 | } |
| 1604 | le16_add_cpu(&eh->eh_entries, -1); |
| 1605 | merge_done = 1; |
| 1606 | WARN_ON(eh->eh_entries == 0); |
| 1607 | if (!eh->eh_entries) |
| 1608 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
| 1609 | } |
| 1610 | |
| 1611 | return merge_done; |
| 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | * This function tries to merge the @ex extent to neighbours in the tree. |
| 1616 | * return 1 if merge left else 0. |
| 1617 | */ |
| 1618 | static int ext4_ext_try_to_merge(struct inode *inode, |
| 1619 | struct ext4_ext_path *path, |
| 1620 | struct ext4_extent *ex) { |
| 1621 | struct ext4_extent_header *eh; |
| 1622 | unsigned int depth; |
| 1623 | int merge_done = 0; |
| 1624 | int ret = 0; |
| 1625 | |
| 1626 | depth = ext_depth(inode); |
| 1627 | BUG_ON(path[depth].p_hdr == NULL); |
| 1628 | eh = path[depth].p_hdr; |
| 1629 | |
| 1630 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1631 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1632 | |
| 1633 | if (!merge_done) |
| 1634 | ret = ext4_ext_try_to_merge_right(inode, path, ex); |
| 1635 | |
| 1636 | return ret; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * check if a portion of the "newext" extent overlaps with an |
| 1641 | * existing extent. |
| 1642 | * |
| 1643 | * If there is an overlap discovered, it updates the length of the newext |
| 1644 | * such that there will be no overlap, and then returns 1. |
| 1645 | * If there is no overlap found, it returns 0. |
| 1646 | */ |
| 1647 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1648 | struct inode *inode, |
| 1649 | struct ext4_extent *newext, |
| 1650 | struct ext4_ext_path *path) |
| 1651 | { |
| 1652 | ext4_lblk_t b1, b2; |
| 1653 | unsigned int depth, len1; |
| 1654 | unsigned int ret = 0; |
| 1655 | |
| 1656 | b1 = le32_to_cpu(newext->ee_block); |
| 1657 | len1 = ext4_ext_get_actual_len(newext); |
| 1658 | depth = ext_depth(inode); |
| 1659 | if (!path[depth].p_ext) |
| 1660 | goto out; |
| 1661 | b2 = le32_to_cpu(path[depth].p_ext->ee_block); |
| 1662 | b2 &= ~(sbi->s_cluster_ratio - 1); |
| 1663 | |
| 1664 | /* |
| 1665 | * get the next allocated block if the extent in the path |
| 1666 | * is before the requested block(s) |
| 1667 | */ |
| 1668 | if (b2 < b1) { |
| 1669 | b2 = ext4_ext_next_allocated_block(path); |
| 1670 | if (b2 == EXT_MAX_BLOCKS) |
| 1671 | goto out; |
| 1672 | b2 &= ~(sbi->s_cluster_ratio - 1); |
| 1673 | } |
| 1674 | |
| 1675 | /* check for wrap through zero on extent logical start block*/ |
| 1676 | if (b1 + len1 < b1) { |
| 1677 | len1 = EXT_MAX_BLOCKS - b1; |
| 1678 | newext->ee_len = cpu_to_le16(len1); |
| 1679 | ret = 1; |
| 1680 | } |
| 1681 | |
| 1682 | /* check for overlap */ |
| 1683 | if (b1 + len1 > b2) { |
| 1684 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1685 | ret = 1; |
| 1686 | } |
| 1687 | out: |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * ext4_ext_insert_extent: |
| 1693 | * tries to merge requsted extent into the existing extent or |
| 1694 | * inserts requested extent as new one into the tree, |
| 1695 | * creating new leaf in the no-space case. |
| 1696 | */ |
| 1697 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
| 1698 | struct ext4_ext_path *path, |
| 1699 | struct ext4_extent *newext, int flag) |
| 1700 | { |
| 1701 | struct ext4_extent_header *eh; |
| 1702 | struct ext4_extent *ex, *fex; |
| 1703 | struct ext4_extent *nearex; /* nearest extent */ |
| 1704 | struct ext4_ext_path *npath = NULL; |
| 1705 | int depth, len, err; |
| 1706 | ext4_lblk_t next; |
| 1707 | unsigned uninitialized = 0; |
| 1708 | int flags = 0; |
| 1709 | |
| 1710 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1711 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
| 1712 | return -EIO; |
| 1713 | } |
| 1714 | depth = ext_depth(inode); |
| 1715 | ex = path[depth].p_ext; |
| 1716 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1717 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
| 1718 | return -EIO; |
| 1719 | } |
| 1720 | |
| 1721 | /* try to insert block into found extent and return */ |
| 1722 | if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) |
| 1723 | && ext4_can_extents_be_merged(inode, ex, newext)) { |
| 1724 | ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n", |
| 1725 | ext4_ext_is_uninitialized(newext), |
| 1726 | ext4_ext_get_actual_len(newext), |
| 1727 | le32_to_cpu(ex->ee_block), |
| 1728 | ext4_ext_is_uninitialized(ex), |
| 1729 | ext4_ext_get_actual_len(ex), |
| 1730 | ext4_ext_pblock(ex)); |
| 1731 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1732 | if (err) |
| 1733 | return err; |
| 1734 | |
| 1735 | /* |
| 1736 | * ext4_can_extents_be_merged should have checked that either |
| 1737 | * both extents are uninitialized, or both aren't. Thus we |
| 1738 | * need to check only one of them here. |
| 1739 | */ |
| 1740 | if (ext4_ext_is_uninitialized(ex)) |
| 1741 | uninitialized = 1; |
| 1742 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1743 | + ext4_ext_get_actual_len(newext)); |
| 1744 | if (uninitialized) |
| 1745 | ext4_ext_mark_uninitialized(ex); |
| 1746 | eh = path[depth].p_hdr; |
| 1747 | nearex = ex; |
| 1748 | goto merge; |
| 1749 | } |
| 1750 | |
| 1751 | depth = ext_depth(inode); |
| 1752 | eh = path[depth].p_hdr; |
| 1753 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 1754 | goto has_space; |
| 1755 | |
| 1756 | /* probably next leaf has space for us? */ |
| 1757 | fex = EXT_LAST_EXTENT(eh); |
| 1758 | next = EXT_MAX_BLOCKS; |
| 1759 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) |
| 1760 | next = ext4_ext_next_leaf_block(path); |
| 1761 | if (next != EXT_MAX_BLOCKS) { |
| 1762 | ext_debug("next leaf block - %u\n", next); |
| 1763 | BUG_ON(npath != NULL); |
| 1764 | npath = ext4_ext_find_extent(inode, next, NULL); |
| 1765 | if (IS_ERR(npath)) |
| 1766 | return PTR_ERR(npath); |
| 1767 | BUG_ON(npath->p_depth != path->p_depth); |
| 1768 | eh = npath[depth].p_hdr; |
| 1769 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
| 1770 | ext_debug("next leaf isn't full(%d)\n", |
| 1771 | le16_to_cpu(eh->eh_entries)); |
| 1772 | path = npath; |
| 1773 | goto has_space; |
| 1774 | } |
| 1775 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 1776 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | * There is no free space in the found leaf. |
| 1781 | * We're gonna add a new leaf in the tree. |
| 1782 | */ |
| 1783 | if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) |
| 1784 | flags = EXT4_MB_USE_ROOT_BLOCKS; |
| 1785 | err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext); |
| 1786 | if (err) |
| 1787 | goto cleanup; |
| 1788 | depth = ext_depth(inode); |
| 1789 | eh = path[depth].p_hdr; |
| 1790 | |
| 1791 | has_space: |
| 1792 | nearex = path[depth].p_ext; |
| 1793 | |
| 1794 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1795 | if (err) |
| 1796 | goto cleanup; |
| 1797 | |
| 1798 | if (!nearex) { |
| 1799 | /* there is no extent in this leaf, create first one */ |
| 1800 | ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", |
| 1801 | le32_to_cpu(newext->ee_block), |
| 1802 | ext4_ext_pblock(newext), |
| 1803 | ext4_ext_is_uninitialized(newext), |
| 1804 | ext4_ext_get_actual_len(newext)); |
| 1805 | nearex = EXT_FIRST_EXTENT(eh); |
| 1806 | } else { |
| 1807 | if (le32_to_cpu(newext->ee_block) |
| 1808 | > le32_to_cpu(nearex->ee_block)) { |
| 1809 | /* Insert after */ |
| 1810 | ext_debug("insert %u:%llu:[%d]%d before: " |
| 1811 | "nearest %p\n", |
| 1812 | le32_to_cpu(newext->ee_block), |
| 1813 | ext4_ext_pblock(newext), |
| 1814 | ext4_ext_is_uninitialized(newext), |
| 1815 | ext4_ext_get_actual_len(newext), |
| 1816 | nearex); |
| 1817 | nearex++; |
| 1818 | } else { |
| 1819 | /* Insert before */ |
| 1820 | BUG_ON(newext->ee_block == nearex->ee_block); |
| 1821 | ext_debug("insert %u:%llu:[%d]%d after: " |
| 1822 | "nearest %p\n", |
| 1823 | le32_to_cpu(newext->ee_block), |
| 1824 | ext4_ext_pblock(newext), |
| 1825 | ext4_ext_is_uninitialized(newext), |
| 1826 | ext4_ext_get_actual_len(newext), |
| 1827 | nearex); |
| 1828 | } |
| 1829 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
| 1830 | if (len > 0) { |
| 1831 | ext_debug("insert %u:%llu:[%d]%d: " |
| 1832 | "move %d extents from 0x%p to 0x%p\n", |
| 1833 | le32_to_cpu(newext->ee_block), |
| 1834 | ext4_ext_pblock(newext), |
| 1835 | ext4_ext_is_uninitialized(newext), |
| 1836 | ext4_ext_get_actual_len(newext), |
| 1837 | len, nearex, nearex + 1); |
| 1838 | memmove(nearex + 1, nearex, |
| 1839 | len * sizeof(struct ext4_extent)); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | le16_add_cpu(&eh->eh_entries, 1); |
| 1844 | path[depth].p_ext = nearex; |
| 1845 | nearex->ee_block = newext->ee_block; |
| 1846 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
| 1847 | nearex->ee_len = newext->ee_len; |
| 1848 | |
| 1849 | merge: |
| 1850 | /* try to merge extents to the right */ |
| 1851 | if (!(flag & EXT4_GET_BLOCKS_PRE_IO)) |
| 1852 | ext4_ext_try_to_merge(inode, path, nearex); |
| 1853 | |
| 1854 | /* try to merge extents to the left */ |
| 1855 | |
| 1856 | /* time to correct all indexes above */ |
| 1857 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 1858 | if (err) |
| 1859 | goto cleanup; |
| 1860 | |
| 1861 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1862 | |
| 1863 | cleanup: |
| 1864 | if (npath) { |
| 1865 | ext4_ext_drop_refs(npath); |
| 1866 | kfree(npath); |
| 1867 | } |
| 1868 | ext4_ext_invalidate_cache(inode); |
| 1869 | return err; |
| 1870 | } |
| 1871 | |
| 1872 | static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, |
| 1873 | ext4_lblk_t num, ext_prepare_callback func, |
| 1874 | void *cbdata) |
| 1875 | { |
| 1876 | struct ext4_ext_path *path = NULL; |
| 1877 | struct ext4_ext_cache cbex; |
| 1878 | struct ext4_extent *ex; |
| 1879 | ext4_lblk_t next, start = 0, end = 0; |
| 1880 | ext4_lblk_t last = block + num; |
| 1881 | int depth, exists, err = 0; |
| 1882 | |
| 1883 | BUG_ON(func == NULL); |
| 1884 | BUG_ON(inode == NULL); |
| 1885 | |
| 1886 | while (block < last && block != EXT_MAX_BLOCKS) { |
| 1887 | num = last - block; |
| 1888 | /* find extent for this block */ |
| 1889 | down_read(&EXT4_I(inode)->i_data_sem); |
| 1890 | path = ext4_ext_find_extent(inode, block, path); |
| 1891 | up_read(&EXT4_I(inode)->i_data_sem); |
| 1892 | if (IS_ERR(path)) { |
| 1893 | err = PTR_ERR(path); |
| 1894 | path = NULL; |
| 1895 | break; |
| 1896 | } |
| 1897 | |
| 1898 | depth = ext_depth(inode); |
| 1899 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1900 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
| 1901 | err = -EIO; |
| 1902 | break; |
| 1903 | } |
| 1904 | ex = path[depth].p_ext; |
| 1905 | next = ext4_ext_next_allocated_block(path); |
| 1906 | |
| 1907 | exists = 0; |
| 1908 | if (!ex) { |
| 1909 | /* there is no extent yet, so try to allocate |
| 1910 | * all requested space */ |
| 1911 | start = block; |
| 1912 | end = block + num; |
| 1913 | } else if (le32_to_cpu(ex->ee_block) > block) { |
| 1914 | /* need to allocate space before found extent */ |
| 1915 | start = block; |
| 1916 | end = le32_to_cpu(ex->ee_block); |
| 1917 | if (block + num < end) |
| 1918 | end = block + num; |
| 1919 | } else if (block >= le32_to_cpu(ex->ee_block) |
| 1920 | + ext4_ext_get_actual_len(ex)) { |
| 1921 | /* need to allocate space after found extent */ |
| 1922 | start = block; |
| 1923 | end = block + num; |
| 1924 | if (end >= next) |
| 1925 | end = next; |
| 1926 | } else if (block >= le32_to_cpu(ex->ee_block)) { |
| 1927 | /* |
| 1928 | * some part of requested space is covered |
| 1929 | * by found extent |
| 1930 | */ |
| 1931 | start = block; |
| 1932 | end = le32_to_cpu(ex->ee_block) |
| 1933 | + ext4_ext_get_actual_len(ex); |
| 1934 | if (block + num < end) |
| 1935 | end = block + num; |
| 1936 | exists = 1; |
| 1937 | } else { |
| 1938 | BUG(); |
| 1939 | } |
| 1940 | BUG_ON(end <= start); |
| 1941 | |
| 1942 | if (!exists) { |
| 1943 | cbex.ec_block = start; |
| 1944 | cbex.ec_len = end - start; |
| 1945 | cbex.ec_start = 0; |
| 1946 | } else { |
| 1947 | cbex.ec_block = le32_to_cpu(ex->ee_block); |
| 1948 | cbex.ec_len = ext4_ext_get_actual_len(ex); |
| 1949 | cbex.ec_start = ext4_ext_pblock(ex); |
| 1950 | } |
| 1951 | |
| 1952 | if (unlikely(cbex.ec_len == 0)) { |
| 1953 | EXT4_ERROR_INODE(inode, "cbex.ec_len == 0"); |
| 1954 | err = -EIO; |
| 1955 | break; |
| 1956 | } |
| 1957 | err = func(inode, next, &cbex, ex, cbdata); |
| 1958 | ext4_ext_drop_refs(path); |
| 1959 | |
| 1960 | if (err < 0) |
| 1961 | break; |
| 1962 | |
| 1963 | if (err == EXT_REPEAT) |
| 1964 | continue; |
| 1965 | else if (err == EXT_BREAK) { |
| 1966 | err = 0; |
| 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | if (ext_depth(inode) != depth) { |
| 1971 | /* depth was changed. we have to realloc path */ |
| 1972 | kfree(path); |
| 1973 | path = NULL; |
| 1974 | } |
| 1975 | |
| 1976 | block = cbex.ec_block + cbex.ec_len; |
| 1977 | } |
| 1978 | |
| 1979 | if (path) { |
| 1980 | ext4_ext_drop_refs(path); |
| 1981 | kfree(path); |
| 1982 | } |
| 1983 | |
| 1984 | return err; |
| 1985 | } |
| 1986 | |
| 1987 | static void |
| 1988 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, |
| 1989 | __u32 len, ext4_fsblk_t start) |
| 1990 | { |
| 1991 | struct ext4_ext_cache *cex; |
| 1992 | BUG_ON(len == 0); |
| 1993 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1994 | trace_ext4_ext_put_in_cache(inode, block, len, start); |
| 1995 | cex = &EXT4_I(inode)->i_cached_extent; |
| 1996 | cex->ec_block = block; |
| 1997 | cex->ec_len = len; |
| 1998 | cex->ec_start = start; |
| 1999 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * ext4_ext_put_gap_in_cache: |
| 2004 | * calculate boundaries of the gap that the requested block fits into |
| 2005 | * and cache this gap |
| 2006 | */ |
| 2007 | static void |
| 2008 | ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, |
| 2009 | ext4_lblk_t block) |
| 2010 | { |
| 2011 | int depth = ext_depth(inode); |
| 2012 | unsigned long len; |
| 2013 | ext4_lblk_t lblock; |
| 2014 | struct ext4_extent *ex; |
| 2015 | |
| 2016 | ex = path[depth].p_ext; |
| 2017 | if (ex == NULL) { |
| 2018 | /* there is no extent yet, so gap is [0;-] */ |
| 2019 | lblock = 0; |
| 2020 | len = EXT_MAX_BLOCKS; |
| 2021 | ext_debug("cache gap(whole file):"); |
| 2022 | } else if (block < le32_to_cpu(ex->ee_block)) { |
| 2023 | lblock = block; |
| 2024 | len = le32_to_cpu(ex->ee_block) - block; |
| 2025 | ext_debug("cache gap(before): %u [%u:%u]", |
| 2026 | block, |
| 2027 | le32_to_cpu(ex->ee_block), |
| 2028 | ext4_ext_get_actual_len(ex)); |
| 2029 | } else if (block >= le32_to_cpu(ex->ee_block) |
| 2030 | + ext4_ext_get_actual_len(ex)) { |
| 2031 | ext4_lblk_t next; |
| 2032 | lblock = le32_to_cpu(ex->ee_block) |
| 2033 | + ext4_ext_get_actual_len(ex); |
| 2034 | |
| 2035 | next = ext4_ext_next_allocated_block(path); |
| 2036 | ext_debug("cache gap(after): [%u:%u] %u", |
| 2037 | le32_to_cpu(ex->ee_block), |
| 2038 | ext4_ext_get_actual_len(ex), |
| 2039 | block); |
| 2040 | BUG_ON(next == lblock); |
| 2041 | len = next - lblock; |
| 2042 | } else { |
| 2043 | lblock = len = 0; |
| 2044 | BUG(); |
| 2045 | } |
| 2046 | |
| 2047 | ext_debug(" -> %u:%lu\n", lblock, len); |
| 2048 | ext4_ext_put_in_cache(inode, lblock, len, 0); |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * ext4_ext_check_cache() |
| 2053 | * Checks to see if the given block is in the cache. |
| 2054 | * If it is, the cached extent is stored in the given |
| 2055 | * cache extent pointer. If the cached extent is a hole, |
| 2056 | * this routine should be used instead of |
| 2057 | * ext4_ext_in_cache if the calling function needs to |
| 2058 | * know the size of the hole. |
| 2059 | * |
| 2060 | * @inode: The files inode |
| 2061 | * @block: The block to look for in the cache |
| 2062 | * @ex: Pointer where the cached extent will be stored |
| 2063 | * if it contains block |
| 2064 | * |
| 2065 | * Return 0 if cache is invalid; 1 if the cache is valid |
| 2066 | */ |
| 2067 | static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block, |
| 2068 | struct ext4_ext_cache *ex){ |
| 2069 | struct ext4_ext_cache *cex; |
| 2070 | struct ext4_sb_info *sbi; |
| 2071 | int ret = 0; |
| 2072 | |
| 2073 | /* |
| 2074 | * We borrow i_block_reservation_lock to protect i_cached_extent |
| 2075 | */ |
| 2076 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 2077 | cex = &EXT4_I(inode)->i_cached_extent; |
| 2078 | sbi = EXT4_SB(inode->i_sb); |
| 2079 | |
| 2080 | /* has cache valid data? */ |
| 2081 | if (cex->ec_len == 0) |
| 2082 | goto errout; |
| 2083 | |
| 2084 | if (in_range(block, cex->ec_block, cex->ec_len)) { |
| 2085 | memcpy(ex, cex, sizeof(struct ext4_ext_cache)); |
| 2086 | ext_debug("%u cached by %u:%u:%llu\n", |
| 2087 | block, |
| 2088 | cex->ec_block, cex->ec_len, cex->ec_start); |
| 2089 | ret = 1; |
| 2090 | } |
| 2091 | errout: |
| 2092 | trace_ext4_ext_in_cache(inode, block, ret); |
| 2093 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 2094 | return ret; |
| 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * ext4_ext_in_cache() |
| 2099 | * Checks to see if the given block is in the cache. |
| 2100 | * If it is, the cached extent is stored in the given |
| 2101 | * extent pointer. |
| 2102 | * |
| 2103 | * @inode: The files inode |
| 2104 | * @block: The block to look for in the cache |
| 2105 | * @ex: Pointer where the cached extent will be stored |
| 2106 | * if it contains block |
| 2107 | * |
| 2108 | * Return 0 if cache is invalid; 1 if the cache is valid |
| 2109 | */ |
| 2110 | static int |
| 2111 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, |
| 2112 | struct ext4_extent *ex) |
| 2113 | { |
| 2114 | struct ext4_ext_cache cex; |
| 2115 | int ret = 0; |
| 2116 | |
| 2117 | if (ext4_ext_check_cache(inode, block, &cex)) { |
| 2118 | ex->ee_block = cpu_to_le32(cex.ec_block); |
| 2119 | ext4_ext_store_pblock(ex, cex.ec_start); |
| 2120 | ex->ee_len = cpu_to_le16(cex.ec_len); |
| 2121 | ret = 1; |
| 2122 | } |
| 2123 | |
| 2124 | return ret; |
| 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | /* |
| 2129 | * ext4_ext_rm_idx: |
| 2130 | * removes index from the index block. |
| 2131 | */ |
| 2132 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
| 2133 | struct ext4_ext_path *path, int depth) |
| 2134 | { |
| 2135 | int err; |
| 2136 | ext4_fsblk_t leaf; |
| 2137 | |
| 2138 | /* free index block */ |
| 2139 | depth--; |
| 2140 | path = path + depth; |
| 2141 | leaf = ext4_idx_pblock(path->p_idx); |
| 2142 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2143 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
| 2144 | return -EIO; |
| 2145 | } |
| 2146 | err = ext4_ext_get_access(handle, inode, path); |
| 2147 | if (err) |
| 2148 | return err; |
| 2149 | |
| 2150 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2151 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2152 | len *= sizeof(struct ext4_extent_idx); |
| 2153 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2154 | } |
| 2155 | |
| 2156 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
| 2157 | err = ext4_ext_dirty(handle, inode, path); |
| 2158 | if (err) |
| 2159 | return err; |
| 2160 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
| 2161 | trace_ext4_ext_rm_idx(inode, leaf); |
| 2162 | |
| 2163 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
| 2164 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
| 2165 | |
| 2166 | while (--depth >= 0) { |
| 2167 | if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) |
| 2168 | break; |
| 2169 | path--; |
| 2170 | err = ext4_ext_get_access(handle, inode, path); |
| 2171 | if (err) |
| 2172 | break; |
| 2173 | path->p_idx->ei_block = (path+1)->p_idx->ei_block; |
| 2174 | err = ext4_ext_dirty(handle, inode, path); |
| 2175 | if (err) |
| 2176 | break; |
| 2177 | } |
| 2178 | return err; |
| 2179 | } |
| 2180 | |
| 2181 | /* |
| 2182 | * ext4_ext_calc_credits_for_single_extent: |
| 2183 | * This routine returns max. credits that needed to insert an extent |
| 2184 | * to the extent tree. |
| 2185 | * When pass the actual path, the caller should calculate credits |
| 2186 | * under i_data_sem. |
| 2187 | */ |
| 2188 | int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, |
| 2189 | struct ext4_ext_path *path) |
| 2190 | { |
| 2191 | if (path) { |
| 2192 | int depth = ext_depth(inode); |
| 2193 | int ret = 0; |
| 2194 | |
| 2195 | /* probably there is space in leaf? */ |
| 2196 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
| 2197 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2198 | |
| 2199 | /* |
| 2200 | * There are some space in the leaf tree, no |
| 2201 | * need to account for leaf block credit |
| 2202 | * |
| 2203 | * bitmaps and block group descriptor blocks |
| 2204 | * and other metadata blocks still need to be |
| 2205 | * accounted. |
| 2206 | */ |
| 2207 | /* 1 bitmap, 1 block group descriptor */ |
| 2208 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
| 2209 | return ret; |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | return ext4_chunk_trans_blocks(inode, nrblocks); |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * How many index/leaf blocks need to change/allocate to modify nrblocks? |
| 2218 | * |
| 2219 | * if nrblocks are fit in a single extent (chunk flag is 1), then |
| 2220 | * in the worse case, each tree level index/leaf need to be changed |
| 2221 | * if the tree split due to insert a new extent, then the old tree |
| 2222 | * index/leaf need to be updated too |
| 2223 | * |
| 2224 | * If the nrblocks are discontiguous, they could cause |
| 2225 | * the whole tree split more than once, but this is really rare. |
| 2226 | */ |
| 2227 | int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
| 2228 | { |
| 2229 | int index; |
| 2230 | int depth = ext_depth(inode); |
| 2231 | |
| 2232 | if (chunk) |
| 2233 | index = depth * 2; |
| 2234 | else |
| 2235 | index = depth * 3; |
| 2236 | |
| 2237 | return index; |
| 2238 | } |
| 2239 | |
| 2240 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
| 2241 | struct ext4_extent *ex, |
| 2242 | ext4_fsblk_t *partial_cluster, |
| 2243 | ext4_lblk_t from, ext4_lblk_t to) |
| 2244 | { |
| 2245 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2246 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 2247 | ext4_fsblk_t pblk; |
| 2248 | int flags = EXT4_FREE_BLOCKS_FORGET; |
| 2249 | |
| 2250 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 2251 | flags |= EXT4_FREE_BLOCKS_METADATA; |
| 2252 | /* |
| 2253 | * For bigalloc file systems, we never free a partial cluster |
| 2254 | * at the beginning of the extent. Instead, we make a note |
| 2255 | * that we tried freeing the cluster, and check to see if we |
| 2256 | * need to free it on a subsequent call to ext4_remove_blocks, |
| 2257 | * or at the end of the ext4_truncate() operation. |
| 2258 | */ |
| 2259 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
| 2260 | |
| 2261 | trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster); |
| 2262 | /* |
| 2263 | * If we have a partial cluster, and it's different from the |
| 2264 | * cluster of the last block, we need to explicitly free the |
| 2265 | * partial cluster here. |
| 2266 | */ |
| 2267 | pblk = ext4_ext_pblock(ex) + ee_len - 1; |
| 2268 | if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) { |
| 2269 | ext4_free_blocks(handle, inode, NULL, |
| 2270 | EXT4_C2B(sbi, *partial_cluster), |
| 2271 | sbi->s_cluster_ratio, flags); |
| 2272 | *partial_cluster = 0; |
| 2273 | } |
| 2274 | |
| 2275 | #ifdef EXTENTS_STATS |
| 2276 | { |
| 2277 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2278 | spin_lock(&sbi->s_ext_stats_lock); |
| 2279 | sbi->s_ext_blocks += ee_len; |
| 2280 | sbi->s_ext_extents++; |
| 2281 | if (ee_len < sbi->s_ext_min) |
| 2282 | sbi->s_ext_min = ee_len; |
| 2283 | if (ee_len > sbi->s_ext_max) |
| 2284 | sbi->s_ext_max = ee_len; |
| 2285 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2286 | sbi->s_depth_max = ext_depth(inode); |
| 2287 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2288 | } |
| 2289 | #endif |
| 2290 | if (from >= le32_to_cpu(ex->ee_block) |
| 2291 | && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
| 2292 | /* tail removal */ |
| 2293 | ext4_lblk_t num; |
| 2294 | |
| 2295 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 2296 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
| 2297 | ext_debug("free last %u blocks starting %llu\n", num, pblk); |
| 2298 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
| 2299 | /* |
| 2300 | * If the block range to be freed didn't start at the |
| 2301 | * beginning of a cluster, and we removed the entire |
| 2302 | * extent, save the partial cluster here, since we |
| 2303 | * might need to delete if we determine that the |
| 2304 | * truncate operation has removed all of the blocks in |
| 2305 | * the cluster. |
| 2306 | */ |
| 2307 | if (pblk & (sbi->s_cluster_ratio - 1) && |
| 2308 | (ee_len == num)) |
| 2309 | *partial_cluster = EXT4_B2C(sbi, pblk); |
| 2310 | else |
| 2311 | *partial_cluster = 0; |
| 2312 | } else if (from == le32_to_cpu(ex->ee_block) |
| 2313 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { |
| 2314 | /* head removal */ |
| 2315 | ext4_lblk_t num; |
| 2316 | ext4_fsblk_t start; |
| 2317 | |
| 2318 | num = to - from; |
| 2319 | start = ext4_ext_pblock(ex); |
| 2320 | |
| 2321 | ext_debug("free first %u blocks starting %llu\n", num, start); |
| 2322 | ext4_free_blocks(handle, inode, NULL, start, num, flags); |
| 2323 | |
| 2324 | } else { |
| 2325 | printk(KERN_INFO "strange request: removal(2) " |
| 2326 | "%u-%u from %u:%u\n", |
| 2327 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
| 2328 | } |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /* |
| 2334 | * ext4_ext_rm_leaf() Removes the extents associated with the |
| 2335 | * blocks appearing between "start" and "end", and splits the extents |
| 2336 | * if "start" and "end" appear in the same extent |
| 2337 | * |
| 2338 | * @handle: The journal handle |
| 2339 | * @inode: The files inode |
| 2340 | * @path: The path to the leaf |
| 2341 | * @start: The first block to remove |
| 2342 | * @end: The last block to remove |
| 2343 | */ |
| 2344 | static int |
| 2345 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
| 2346 | struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster, |
| 2347 | ext4_lblk_t start, ext4_lblk_t end) |
| 2348 | { |
| 2349 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2350 | int err = 0, correct_index = 0; |
| 2351 | int depth = ext_depth(inode), credits; |
| 2352 | struct ext4_extent_header *eh; |
| 2353 | ext4_lblk_t a, b; |
| 2354 | unsigned num; |
| 2355 | ext4_lblk_t ex_ee_block; |
| 2356 | unsigned short ex_ee_len; |
| 2357 | unsigned uninitialized = 0; |
| 2358 | struct ext4_extent *ex; |
| 2359 | |
| 2360 | /* the header must be checked already in ext4_ext_remove_space() */ |
| 2361 | ext_debug("truncate since %u in leaf to %u\n", start, end); |
| 2362 | if (!path[depth].p_hdr) |
| 2363 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2364 | eh = path[depth].p_hdr; |
| 2365 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2366 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
| 2367 | return -EIO; |
| 2368 | } |
| 2369 | /* find where to start removing */ |
| 2370 | ex = EXT_LAST_EXTENT(eh); |
| 2371 | |
| 2372 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2373 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2374 | |
| 2375 | trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster); |
| 2376 | |
| 2377 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2378 | ex_ee_block + ex_ee_len > start) { |
| 2379 | |
| 2380 | if (ext4_ext_is_uninitialized(ex)) |
| 2381 | uninitialized = 1; |
| 2382 | else |
| 2383 | uninitialized = 0; |
| 2384 | |
| 2385 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
| 2386 | uninitialized, ex_ee_len); |
| 2387 | path[depth].p_ext = ex; |
| 2388 | |
| 2389 | a = ex_ee_block > start ? ex_ee_block : start; |
| 2390 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2391 | ex_ee_block+ex_ee_len - 1 : end; |
| 2392 | |
| 2393 | ext_debug(" border %u:%u\n", a, b); |
| 2394 | |
| 2395 | /* If this extent is beyond the end of the hole, skip it */ |
| 2396 | if (end < ex_ee_block) { |
| 2397 | ex--; |
| 2398 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2399 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2400 | continue; |
| 2401 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
| 2402 | EXT4_ERROR_INODE(inode, |
| 2403 | "can not handle truncate %u:%u " |
| 2404 | "on extent %u:%u", |
| 2405 | start, end, ex_ee_block, |
| 2406 | ex_ee_block + ex_ee_len - 1); |
| 2407 | err = -EIO; |
| 2408 | goto out; |
| 2409 | } else if (a != ex_ee_block) { |
| 2410 | /* remove tail of the extent */ |
| 2411 | num = a - ex_ee_block; |
| 2412 | } else { |
| 2413 | /* remove whole extent: excellent! */ |
| 2414 | num = 0; |
| 2415 | } |
| 2416 | /* |
| 2417 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2418 | * descriptor) for each block group; assume two block |
| 2419 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2420 | * the worst case |
| 2421 | */ |
| 2422 | credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); |
| 2423 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2424 | correct_index = 1; |
| 2425 | credits += (ext_depth(inode)) + 1; |
| 2426 | } |
| 2427 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
| 2428 | |
| 2429 | err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
| 2430 | if (err) |
| 2431 | goto out; |
| 2432 | |
| 2433 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2434 | if (err) |
| 2435 | goto out; |
| 2436 | |
| 2437 | err = ext4_remove_blocks(handle, inode, ex, partial_cluster, |
| 2438 | a, b); |
| 2439 | if (err) |
| 2440 | goto out; |
| 2441 | |
| 2442 | if (num == 0) |
| 2443 | /* this extent is removed; mark slot entirely unused */ |
| 2444 | ext4_ext_store_pblock(ex, 0); |
| 2445 | |
| 2446 | ex->ee_len = cpu_to_le16(num); |
| 2447 | /* |
| 2448 | * Do not mark uninitialized if all the blocks in the |
| 2449 | * extent have been removed. |
| 2450 | */ |
| 2451 | if (uninitialized && num) |
| 2452 | ext4_ext_mark_uninitialized(ex); |
| 2453 | /* |
| 2454 | * If the extent was completely released, |
| 2455 | * we need to remove it from the leaf |
| 2456 | */ |
| 2457 | if (num == 0) { |
| 2458 | if (end != EXT_MAX_BLOCKS - 1) { |
| 2459 | /* |
| 2460 | * For hole punching, we need to scoot all the |
| 2461 | * extents up when an extent is removed so that |
| 2462 | * we dont have blank extents in the middle |
| 2463 | */ |
| 2464 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2465 | sizeof(struct ext4_extent)); |
| 2466 | |
| 2467 | /* Now get rid of the one at the end */ |
| 2468 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2469 | sizeof(struct ext4_extent)); |
| 2470 | } |
| 2471 | le16_add_cpu(&eh->eh_entries, -1); |
| 2472 | } else |
| 2473 | *partial_cluster = 0; |
| 2474 | |
| 2475 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2476 | if (err) |
| 2477 | goto out; |
| 2478 | |
| 2479 | ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num, |
| 2480 | ext4_ext_pblock(ex)); |
| 2481 | ex--; |
| 2482 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2483 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2484 | } |
| 2485 | |
| 2486 | if (correct_index && eh->eh_entries) |
| 2487 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2488 | |
| 2489 | /* |
| 2490 | * If there is still a entry in the leaf node, check to see if |
| 2491 | * it references the partial cluster. This is the only place |
| 2492 | * where it could; if it doesn't, we can free the cluster. |
| 2493 | */ |
| 2494 | if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) && |
| 2495 | (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) != |
| 2496 | *partial_cluster)) { |
| 2497 | int flags = EXT4_FREE_BLOCKS_FORGET; |
| 2498 | |
| 2499 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 2500 | flags |= EXT4_FREE_BLOCKS_METADATA; |
| 2501 | |
| 2502 | ext4_free_blocks(handle, inode, NULL, |
| 2503 | EXT4_C2B(sbi, *partial_cluster), |
| 2504 | sbi->s_cluster_ratio, flags); |
| 2505 | *partial_cluster = 0; |
| 2506 | } |
| 2507 | |
| 2508 | /* if this leaf is free, then we should |
| 2509 | * remove it from index block above */ |
| 2510 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
| 2511 | err = ext4_ext_rm_idx(handle, inode, path, depth); |
| 2512 | |
| 2513 | out: |
| 2514 | return err; |
| 2515 | } |
| 2516 | |
| 2517 | /* |
| 2518 | * ext4_ext_more_to_rm: |
| 2519 | * returns 1 if current index has to be freed (even partial) |
| 2520 | */ |
| 2521 | static int |
| 2522 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2523 | { |
| 2524 | BUG_ON(path->p_idx == NULL); |
| 2525 | |
| 2526 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2527 | return 0; |
| 2528 | |
| 2529 | /* |
| 2530 | * if truncate on deeper level happened, it wasn't partial, |
| 2531 | * so we have to consider current index for truncation |
| 2532 | */ |
| 2533 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2534 | return 0; |
| 2535 | return 1; |
| 2536 | } |
| 2537 | |
| 2538 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
| 2539 | ext4_lblk_t end) |
| 2540 | { |
| 2541 | struct super_block *sb = inode->i_sb; |
| 2542 | int depth = ext_depth(inode); |
| 2543 | struct ext4_ext_path *path = NULL; |
| 2544 | ext4_fsblk_t partial_cluster = 0; |
| 2545 | handle_t *handle; |
| 2546 | int i = 0, err; |
| 2547 | |
| 2548 | ext_debug("truncate since %u to %u\n", start, end); |
| 2549 | |
| 2550 | /* probably first extent we're gonna free will be last in block */ |
| 2551 | handle = ext4_journal_start(inode, depth + 1); |
| 2552 | if (IS_ERR(handle)) |
| 2553 | return PTR_ERR(handle); |
| 2554 | |
| 2555 | again: |
| 2556 | ext4_ext_invalidate_cache(inode); |
| 2557 | |
| 2558 | trace_ext4_ext_remove_space(inode, start, depth); |
| 2559 | |
| 2560 | /* |
| 2561 | * Check if we are removing extents inside the extent tree. If that |
| 2562 | * is the case, we are going to punch a hole inside the extent tree |
| 2563 | * so we have to check whether we need to split the extent covering |
| 2564 | * the last block to remove so we can easily remove the part of it |
| 2565 | * in ext4_ext_rm_leaf(). |
| 2566 | */ |
| 2567 | if (end < EXT_MAX_BLOCKS - 1) { |
| 2568 | struct ext4_extent *ex; |
| 2569 | ext4_lblk_t ee_block; |
| 2570 | |
| 2571 | /* find extent for this block */ |
| 2572 | path = ext4_ext_find_extent(inode, end, NULL); |
| 2573 | if (IS_ERR(path)) { |
| 2574 | ext4_journal_stop(handle); |
| 2575 | return PTR_ERR(path); |
| 2576 | } |
| 2577 | depth = ext_depth(inode); |
| 2578 | ex = path[depth].p_ext; |
| 2579 | if (!ex) { |
| 2580 | ext4_ext_drop_refs(path); |
| 2581 | kfree(path); |
| 2582 | path = NULL; |
| 2583 | goto cont; |
| 2584 | } |
| 2585 | |
| 2586 | ee_block = le32_to_cpu(ex->ee_block); |
| 2587 | |
| 2588 | /* |
| 2589 | * See if the last block is inside the extent, if so split |
| 2590 | * the extent at 'end' block so we can easily remove the |
| 2591 | * tail of the first part of the split extent in |
| 2592 | * ext4_ext_rm_leaf(). |
| 2593 | */ |
| 2594 | if (end >= ee_block && |
| 2595 | end < ee_block + ext4_ext_get_actual_len(ex) - 1) { |
| 2596 | int split_flag = 0; |
| 2597 | |
| 2598 | if (ext4_ext_is_uninitialized(ex)) |
| 2599 | split_flag = EXT4_EXT_MARK_UNINIT1 | |
| 2600 | EXT4_EXT_MARK_UNINIT2; |
| 2601 | |
| 2602 | /* |
| 2603 | * Split the extent in two so that 'end' is the last |
| 2604 | * block in the first new extent |
| 2605 | */ |
| 2606 | err = ext4_split_extent_at(handle, inode, path, |
| 2607 | end + 1, split_flag, |
| 2608 | EXT4_GET_BLOCKS_PRE_IO | |
| 2609 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT); |
| 2610 | |
| 2611 | if (err < 0) |
| 2612 | goto out; |
| 2613 | } |
| 2614 | } |
| 2615 | cont: |
| 2616 | |
| 2617 | /* |
| 2618 | * We start scanning from right side, freeing all the blocks |
| 2619 | * after i_size and walking into the tree depth-wise. |
| 2620 | */ |
| 2621 | depth = ext_depth(inode); |
| 2622 | if (path) { |
| 2623 | int k = i = depth; |
| 2624 | while (--k > 0) |
| 2625 | path[k].p_block = |
| 2626 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
| 2627 | } else { |
| 2628 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), |
| 2629 | GFP_NOFS); |
| 2630 | if (path == NULL) { |
| 2631 | ext4_journal_stop(handle); |
| 2632 | return -ENOMEM; |
| 2633 | } |
| 2634 | path[0].p_depth = depth; |
| 2635 | path[0].p_hdr = ext_inode_hdr(inode); |
| 2636 | i = 0; |
| 2637 | |
| 2638 | if (ext4_ext_check(inode, path[0].p_hdr, depth)) { |
| 2639 | err = -EIO; |
| 2640 | goto out; |
| 2641 | } |
| 2642 | } |
| 2643 | err = 0; |
| 2644 | |
| 2645 | while (i >= 0 && err == 0) { |
| 2646 | if (i == depth) { |
| 2647 | /* this is leaf block */ |
| 2648 | err = ext4_ext_rm_leaf(handle, inode, path, |
| 2649 | &partial_cluster, start, |
| 2650 | end); |
| 2651 | /* root level has p_bh == NULL, brelse() eats this */ |
| 2652 | brelse(path[i].p_bh); |
| 2653 | path[i].p_bh = NULL; |
| 2654 | i--; |
| 2655 | continue; |
| 2656 | } |
| 2657 | |
| 2658 | /* this is index block */ |
| 2659 | if (!path[i].p_hdr) { |
| 2660 | ext_debug("initialize header\n"); |
| 2661 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
| 2662 | } |
| 2663 | |
| 2664 | if (!path[i].p_idx) { |
| 2665 | /* this level hasn't been touched yet */ |
| 2666 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2667 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2668 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2669 | path[i].p_hdr, |
| 2670 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2671 | } else { |
| 2672 | /* we were already here, see at next index */ |
| 2673 | path[i].p_idx--; |
| 2674 | } |
| 2675 | |
| 2676 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 2677 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2678 | path[i].p_idx); |
| 2679 | if (ext4_ext_more_to_rm(path + i)) { |
| 2680 | struct buffer_head *bh; |
| 2681 | /* go to the next level */ |
| 2682 | ext_debug("move to level %d (block %llu)\n", |
| 2683 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
| 2684 | memset(path + i + 1, 0, sizeof(*path)); |
| 2685 | bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx)); |
| 2686 | if (!bh) { |
| 2687 | /* should we reset i_size? */ |
| 2688 | err = -EIO; |
| 2689 | break; |
| 2690 | } |
| 2691 | if (WARN_ON(i + 1 > depth)) { |
| 2692 | err = -EIO; |
| 2693 | break; |
| 2694 | } |
| 2695 | if (ext4_ext_check(inode, ext_block_hdr(bh), |
| 2696 | depth - i - 1)) { |
| 2697 | err = -EIO; |
| 2698 | break; |
| 2699 | } |
| 2700 | path[i + 1].p_bh = bh; |
| 2701 | |
| 2702 | /* save actual number of indexes since this |
| 2703 | * number is changed at the next iteration */ |
| 2704 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2705 | i++; |
| 2706 | } else { |
| 2707 | /* we finished processing this index, go up */ |
| 2708 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
| 2709 | /* index is empty, remove it; |
| 2710 | * handle must be already prepared by the |
| 2711 | * truncatei_leaf() */ |
| 2712 | err = ext4_ext_rm_idx(handle, inode, path, i); |
| 2713 | } |
| 2714 | /* root level has p_bh == NULL, brelse() eats this */ |
| 2715 | brelse(path[i].p_bh); |
| 2716 | path[i].p_bh = NULL; |
| 2717 | i--; |
| 2718 | ext_debug("return to level %d\n", i); |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster, |
| 2723 | path->p_hdr->eh_entries); |
| 2724 | |
| 2725 | /* If we still have something in the partial cluster and we have removed |
| 2726 | * even the first extent, then we should free the blocks in the partial |
| 2727 | * cluster as well. */ |
| 2728 | if (partial_cluster && path->p_hdr->eh_entries == 0) { |
| 2729 | int flags = EXT4_FREE_BLOCKS_FORGET; |
| 2730 | |
| 2731 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 2732 | flags |= EXT4_FREE_BLOCKS_METADATA; |
| 2733 | |
| 2734 | ext4_free_blocks(handle, inode, NULL, |
| 2735 | EXT4_C2B(EXT4_SB(sb), partial_cluster), |
| 2736 | EXT4_SB(sb)->s_cluster_ratio, flags); |
| 2737 | partial_cluster = 0; |
| 2738 | } |
| 2739 | |
| 2740 | /* TODO: flexible tree reduction should be here */ |
| 2741 | if (path->p_hdr->eh_entries == 0) { |
| 2742 | /* |
| 2743 | * truncate to zero freed all the tree, |
| 2744 | * so we need to correct eh_depth |
| 2745 | */ |
| 2746 | err = ext4_ext_get_access(handle, inode, path); |
| 2747 | if (err == 0) { |
| 2748 | ext_inode_hdr(inode)->eh_depth = 0; |
| 2749 | ext_inode_hdr(inode)->eh_max = |
| 2750 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
| 2751 | err = ext4_ext_dirty(handle, inode, path); |
| 2752 | } |
| 2753 | } |
| 2754 | out: |
| 2755 | ext4_ext_drop_refs(path); |
| 2756 | kfree(path); |
| 2757 | if (err == -EAGAIN) { |
| 2758 | path = NULL; |
| 2759 | goto again; |
| 2760 | } |
| 2761 | ext4_journal_stop(handle); |
| 2762 | |
| 2763 | return err; |
| 2764 | } |
| 2765 | |
| 2766 | /* |
| 2767 | * called at mount time |
| 2768 | */ |
| 2769 | void ext4_ext_init(struct super_block *sb) |
| 2770 | { |
| 2771 | /* |
| 2772 | * possible initialization would be here |
| 2773 | */ |
| 2774 | |
| 2775 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
| 2776 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
| 2777 | printk(KERN_INFO "EXT4-fs: file extents enabled" |
| 2778 | #ifdef AGGRESSIVE_TEST |
| 2779 | ", aggressive tests" |
| 2780 | #endif |
| 2781 | #ifdef CHECK_BINSEARCH |
| 2782 | ", check binsearch" |
| 2783 | #endif |
| 2784 | #ifdef EXTENTS_STATS |
| 2785 | ", stats" |
| 2786 | #endif |
| 2787 | "\n"); |
| 2788 | #endif |
| 2789 | #ifdef EXTENTS_STATS |
| 2790 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 2791 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 2792 | EXT4_SB(sb)->s_ext_max = 0; |
| 2793 | #endif |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | /* |
| 2798 | * called at umount time |
| 2799 | */ |
| 2800 | void ext4_ext_release(struct super_block *sb) |
| 2801 | { |
| 2802 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) |
| 2803 | return; |
| 2804 | |
| 2805 | #ifdef EXTENTS_STATS |
| 2806 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 2807 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2808 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 2809 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 2810 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 2811 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 2812 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 2813 | } |
| 2814 | #endif |
| 2815 | } |
| 2816 | |
| 2817 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 2818 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 2819 | { |
| 2820 | ext4_fsblk_t ee_pblock; |
| 2821 | unsigned int ee_len; |
| 2822 | int ret; |
| 2823 | |
| 2824 | ee_len = ext4_ext_get_actual_len(ex); |
| 2825 | ee_pblock = ext4_ext_pblock(ex); |
| 2826 | |
| 2827 | ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS); |
| 2828 | if (ret > 0) |
| 2829 | ret = 0; |
| 2830 | |
| 2831 | return ret; |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * ext4_split_extent_at() splits an extent at given block. |
| 2836 | * |
| 2837 | * @handle: the journal handle |
| 2838 | * @inode: the file inode |
| 2839 | * @path: the path to the extent |
| 2840 | * @split: the logical block where the extent is splitted. |
| 2841 | * @split_flags: indicates if the extent could be zeroout if split fails, and |
| 2842 | * the states(init or uninit) of new extents. |
| 2843 | * @flags: flags used to insert new extent to extent tree. |
| 2844 | * |
| 2845 | * |
| 2846 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 2847 | * of which are deterimined by split_flag. |
| 2848 | * |
| 2849 | * There are two cases: |
| 2850 | * a> the extent are splitted into two extent. |
| 2851 | * b> split is not needed, and just mark the extent. |
| 2852 | * |
| 2853 | * return 0 on success. |
| 2854 | */ |
| 2855 | static int ext4_split_extent_at(handle_t *handle, |
| 2856 | struct inode *inode, |
| 2857 | struct ext4_ext_path *path, |
| 2858 | ext4_lblk_t split, |
| 2859 | int split_flag, |
| 2860 | int flags) |
| 2861 | { |
| 2862 | ext4_fsblk_t newblock; |
| 2863 | ext4_lblk_t ee_block; |
| 2864 | struct ext4_extent *ex, newex, orig_ex; |
| 2865 | struct ext4_extent *ex2 = NULL; |
| 2866 | unsigned int ee_len, depth; |
| 2867 | int err = 0; |
| 2868 | |
| 2869 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
| 2870 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
| 2871 | |
| 2872 | ext_debug("ext4_split_extents_at: inode %lu, logical" |
| 2873 | "block %llu\n", inode->i_ino, (unsigned long long)split); |
| 2874 | |
| 2875 | ext4_ext_show_leaf(inode, path); |
| 2876 | |
| 2877 | depth = ext_depth(inode); |
| 2878 | ex = path[depth].p_ext; |
| 2879 | ee_block = le32_to_cpu(ex->ee_block); |
| 2880 | ee_len = ext4_ext_get_actual_len(ex); |
| 2881 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 2882 | |
| 2883 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
| 2884 | |
| 2885 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2886 | if (err) |
| 2887 | goto out; |
| 2888 | |
| 2889 | if (split == ee_block) { |
| 2890 | /* |
| 2891 | * case b: block @split is the block that the extent begins with |
| 2892 | * then we just change the state of the extent, and splitting |
| 2893 | * is not needed. |
| 2894 | */ |
| 2895 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
| 2896 | ext4_ext_mark_uninitialized(ex); |
| 2897 | else |
| 2898 | ext4_ext_mark_initialized(ex); |
| 2899 | |
| 2900 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
| 2901 | ext4_ext_try_to_merge(inode, path, ex); |
| 2902 | |
| 2903 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2904 | goto out; |
| 2905 | } |
| 2906 | |
| 2907 | /* case a */ |
| 2908 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 2909 | ex->ee_len = cpu_to_le16(split - ee_block); |
| 2910 | if (split_flag & EXT4_EXT_MARK_UNINIT1) |
| 2911 | ext4_ext_mark_uninitialized(ex); |
| 2912 | |
| 2913 | /* |
| 2914 | * path may lead to new leaf, not to original leaf any more |
| 2915 | * after ext4_ext_insert_extent() returns, |
| 2916 | */ |
| 2917 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2918 | if (err) |
| 2919 | goto fix_extent_len; |
| 2920 | |
| 2921 | ex2 = &newex; |
| 2922 | ex2->ee_block = cpu_to_le32(split); |
| 2923 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 2924 | ext4_ext_store_pblock(ex2, newblock); |
| 2925 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
| 2926 | ext4_ext_mark_uninitialized(ex2); |
| 2927 | |
| 2928 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); |
| 2929 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 2930 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
| 2931 | if (split_flag & EXT4_EXT_DATA_VALID1) |
| 2932 | err = ext4_ext_zeroout(inode, ex2); |
| 2933 | else |
| 2934 | err = ext4_ext_zeroout(inode, ex); |
| 2935 | } else |
| 2936 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2937 | |
| 2938 | if (err) |
| 2939 | goto fix_extent_len; |
| 2940 | /* update the extent length and mark as initialized */ |
| 2941 | ex->ee_len = cpu_to_le16(ee_len); |
| 2942 | ext4_ext_try_to_merge(inode, path, ex); |
| 2943 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2944 | goto out; |
| 2945 | } else if (err) |
| 2946 | goto fix_extent_len; |
| 2947 | |
| 2948 | out: |
| 2949 | ext4_ext_show_leaf(inode, path); |
| 2950 | return err; |
| 2951 | |
| 2952 | fix_extent_len: |
| 2953 | ex->ee_len = orig_ex.ee_len; |
| 2954 | ext4_ext_dirty(handle, inode, path + depth); |
| 2955 | return err; |
| 2956 | } |
| 2957 | |
| 2958 | /* |
| 2959 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 2960 | * by @map as split_flags indicates |
| 2961 | * |
| 2962 | * It may result in splitting the extent into multiple extents (upto three) |
| 2963 | * There are three possibilities: |
| 2964 | * a> There is no split required |
| 2965 | * b> Splits in two extents: Split is happening at either end of the extent |
| 2966 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 2967 | * |
| 2968 | */ |
| 2969 | static int ext4_split_extent(handle_t *handle, |
| 2970 | struct inode *inode, |
| 2971 | struct ext4_ext_path *path, |
| 2972 | struct ext4_map_blocks *map, |
| 2973 | int split_flag, |
| 2974 | int flags) |
| 2975 | { |
| 2976 | ext4_lblk_t ee_block; |
| 2977 | struct ext4_extent *ex; |
| 2978 | unsigned int ee_len, depth; |
| 2979 | int err = 0; |
| 2980 | int uninitialized; |
| 2981 | int split_flag1, flags1; |
| 2982 | int allocated = map->m_len; |
| 2983 | |
| 2984 | depth = ext_depth(inode); |
| 2985 | ex = path[depth].p_ext; |
| 2986 | ee_block = le32_to_cpu(ex->ee_block); |
| 2987 | ee_len = ext4_ext_get_actual_len(ex); |
| 2988 | uninitialized = ext4_ext_is_uninitialized(ex); |
| 2989 | |
| 2990 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
| 2991 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; |
| 2992 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
| 2993 | if (uninitialized) |
| 2994 | split_flag1 |= EXT4_EXT_MARK_UNINIT1 | |
| 2995 | EXT4_EXT_MARK_UNINIT2; |
| 2996 | if (split_flag & EXT4_EXT_DATA_VALID2) |
| 2997 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
| 2998 | err = ext4_split_extent_at(handle, inode, path, |
| 2999 | map->m_lblk + map->m_len, split_flag1, flags1); |
| 3000 | if (err) |
| 3001 | goto out; |
| 3002 | } else { |
| 3003 | allocated = ee_len - (map->m_lblk - ee_block); |
| 3004 | } |
| 3005 | |
| 3006 | ext4_ext_drop_refs(path); |
| 3007 | path = ext4_ext_find_extent(inode, map->m_lblk, path); |
| 3008 | if (IS_ERR(path)) |
| 3009 | return PTR_ERR(path); |
| 3010 | |
| 3011 | if (map->m_lblk >= ee_block) { |
| 3012 | split_flag1 = split_flag & (EXT4_EXT_MAY_ZEROOUT | |
| 3013 | EXT4_EXT_DATA_VALID2); |
| 3014 | if (uninitialized) |
| 3015 | split_flag1 |= EXT4_EXT_MARK_UNINIT1; |
| 3016 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
| 3017 | split_flag1 |= EXT4_EXT_MARK_UNINIT2; |
| 3018 | err = ext4_split_extent_at(handle, inode, path, |
| 3019 | map->m_lblk, split_flag1, flags); |
| 3020 | if (err) |
| 3021 | goto out; |
| 3022 | } |
| 3023 | |
| 3024 | ext4_ext_show_leaf(inode, path); |
| 3025 | out: |
| 3026 | return err ? err : allocated; |
| 3027 | } |
| 3028 | |
| 3029 | #define EXT4_EXT_ZERO_LEN 7 |
| 3030 | /* |
| 3031 | * This function is called by ext4_ext_map_blocks() if someone tries to write |
| 3032 | * to an uninitialized extent. It may result in splitting the uninitialized |
| 3033 | * extent into multiple extents (up to three - one initialized and two |
| 3034 | * uninitialized). |
| 3035 | * There are three possibilities: |
| 3036 | * a> There is no split required: Entire extent should be initialized |
| 3037 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3038 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3039 | * |
| 3040 | * Pre-conditions: |
| 3041 | * - The extent pointed to by 'path' is uninitialized. |
| 3042 | * - The extent pointed to by 'path' contains a superset |
| 3043 | * of the logical span [map->m_lblk, map->m_lblk + map->m_len). |
| 3044 | * |
| 3045 | * Post-conditions on success: |
| 3046 | * - the returned value is the number of blocks beyond map->l_lblk |
| 3047 | * that are allocated and initialized. |
| 3048 | * It is guaranteed to be >= map->m_len. |
| 3049 | */ |
| 3050 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
| 3051 | struct inode *inode, |
| 3052 | struct ext4_map_blocks *map, |
| 3053 | struct ext4_ext_path *path) |
| 3054 | { |
| 3055 | struct ext4_extent_header *eh; |
| 3056 | struct ext4_map_blocks split_map; |
| 3057 | struct ext4_extent zero_ex; |
| 3058 | struct ext4_extent *ex; |
| 3059 | ext4_lblk_t ee_block, eof_block; |
| 3060 | unsigned int ee_len, depth; |
| 3061 | int allocated; |
| 3062 | int err = 0; |
| 3063 | int split_flag = 0; |
| 3064 | |
| 3065 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
| 3066 | "block %llu, max_blocks %u\n", inode->i_ino, |
| 3067 | (unsigned long long)map->m_lblk, map->m_len); |
| 3068 | |
| 3069 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3070 | inode->i_sb->s_blocksize_bits; |
| 3071 | if (eof_block < map->m_lblk + map->m_len) |
| 3072 | eof_block = map->m_lblk + map->m_len; |
| 3073 | |
| 3074 | depth = ext_depth(inode); |
| 3075 | eh = path[depth].p_hdr; |
| 3076 | ex = path[depth].p_ext; |
| 3077 | ee_block = le32_to_cpu(ex->ee_block); |
| 3078 | ee_len = ext4_ext_get_actual_len(ex); |
| 3079 | allocated = ee_len - (map->m_lblk - ee_block); |
| 3080 | |
| 3081 | trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); |
| 3082 | |
| 3083 | /* Pre-conditions */ |
| 3084 | BUG_ON(!ext4_ext_is_uninitialized(ex)); |
| 3085 | BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); |
| 3086 | |
| 3087 | /* |
| 3088 | * Attempt to transfer newly initialized blocks from the currently |
| 3089 | * uninitialized extent to its left neighbor. This is much cheaper |
| 3090 | * than an insertion followed by a merge as those involve costly |
| 3091 | * memmove() calls. This is the common case in steady state for |
| 3092 | * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append |
| 3093 | * writes. |
| 3094 | * |
| 3095 | * Limitations of the current logic: |
| 3096 | * - L1: we only deal with writes at the start of the extent. |
| 3097 | * The approach could be extended to writes at the end |
| 3098 | * of the extent but this scenario was deemed less common. |
| 3099 | * - L2: we do not deal with writes covering the whole extent. |
| 3100 | * This would require removing the extent if the transfer |
| 3101 | * is possible. |
| 3102 | * - L3: we only attempt to merge with an extent stored in the |
| 3103 | * same extent tree node. |
| 3104 | */ |
| 3105 | if ((map->m_lblk == ee_block) && /*L1*/ |
| 3106 | (map->m_len < ee_len) && /*L2*/ |
| 3107 | (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/ |
| 3108 | struct ext4_extent *prev_ex; |
| 3109 | ext4_lblk_t prev_lblk; |
| 3110 | ext4_fsblk_t prev_pblk, ee_pblk; |
| 3111 | unsigned int prev_len, write_len; |
| 3112 | |
| 3113 | prev_ex = ex - 1; |
| 3114 | prev_lblk = le32_to_cpu(prev_ex->ee_block); |
| 3115 | prev_len = ext4_ext_get_actual_len(prev_ex); |
| 3116 | prev_pblk = ext4_ext_pblock(prev_ex); |
| 3117 | ee_pblk = ext4_ext_pblock(ex); |
| 3118 | write_len = map->m_len; |
| 3119 | |
| 3120 | /* |
| 3121 | * A transfer of blocks from 'ex' to 'prev_ex' is allowed |
| 3122 | * upon those conditions: |
| 3123 | * - C1: prev_ex is initialized, |
| 3124 | * - C2: prev_ex is logically abutting ex, |
| 3125 | * - C3: prev_ex is physically abutting ex, |
| 3126 | * - C4: prev_ex can receive the additional blocks without |
| 3127 | * overflowing the (initialized) length limit. |
| 3128 | */ |
| 3129 | if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/ |
| 3130 | ((prev_lblk + prev_len) == ee_block) && /*C2*/ |
| 3131 | ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ |
| 3132 | (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/ |
| 3133 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3134 | if (err) |
| 3135 | goto out; |
| 3136 | |
| 3137 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
| 3138 | map, ex, prev_ex); |
| 3139 | |
| 3140 | /* Shift the start of ex by 'write_len' blocks */ |
| 3141 | ex->ee_block = cpu_to_le32(ee_block + write_len); |
| 3142 | ext4_ext_store_pblock(ex, ee_pblk + write_len); |
| 3143 | ex->ee_len = cpu_to_le16(ee_len - write_len); |
| 3144 | ext4_ext_mark_uninitialized(ex); /* Restore the flag */ |
| 3145 | |
| 3146 | /* Extend prev_ex by 'write_len' blocks */ |
| 3147 | prev_ex->ee_len = cpu_to_le16(prev_len + write_len); |
| 3148 | |
| 3149 | /* Mark the block containing both extents as dirty */ |
| 3150 | ext4_ext_dirty(handle, inode, path + depth); |
| 3151 | |
| 3152 | /* Update path to point to the right extent */ |
| 3153 | path[depth].p_ext = prev_ex; |
| 3154 | |
| 3155 | /* Result: number of initialized blocks past m_lblk */ |
| 3156 | allocated = write_len; |
| 3157 | goto out; |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | WARN_ON(map->m_lblk < ee_block); |
| 3162 | /* |
| 3163 | * It is safe to convert extent to initialized via explicit |
| 3164 | * zeroout only if extent is fully insde i_size or new_size. |
| 3165 | */ |
| 3166 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
| 3167 | |
| 3168 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ |
| 3169 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN && |
| 3170 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 3171 | err = ext4_ext_zeroout(inode, ex); |
| 3172 | if (err) |
| 3173 | goto out; |
| 3174 | |
| 3175 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3176 | if (err) |
| 3177 | goto out; |
| 3178 | ext4_ext_mark_initialized(ex); |
| 3179 | ext4_ext_try_to_merge(inode, path, ex); |
| 3180 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3181 | goto out; |
| 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * four cases: |
| 3186 | * 1. split the extent into three extents. |
| 3187 | * 2. split the extent into two extents, zeroout the first half. |
| 3188 | * 3. split the extent into two extents, zeroout the second half. |
| 3189 | * 4. split the extent into two extents with out zeroout. |
| 3190 | */ |
| 3191 | split_map.m_lblk = map->m_lblk; |
| 3192 | split_map.m_len = map->m_len; |
| 3193 | |
| 3194 | if (allocated > map->m_len) { |
| 3195 | if (allocated <= EXT4_EXT_ZERO_LEN && |
| 3196 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 3197 | /* case 3 */ |
| 3198 | zero_ex.ee_block = |
| 3199 | cpu_to_le32(map->m_lblk); |
| 3200 | zero_ex.ee_len = cpu_to_le16(allocated); |
| 3201 | ext4_ext_store_pblock(&zero_ex, |
| 3202 | ext4_ext_pblock(ex) + map->m_lblk - ee_block); |
| 3203 | err = ext4_ext_zeroout(inode, &zero_ex); |
| 3204 | if (err) |
| 3205 | goto out; |
| 3206 | split_map.m_lblk = map->m_lblk; |
| 3207 | split_map.m_len = allocated; |
| 3208 | } else if ((map->m_lblk - ee_block + map->m_len < |
| 3209 | EXT4_EXT_ZERO_LEN) && |
| 3210 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 3211 | /* case 2 */ |
| 3212 | if (map->m_lblk != ee_block) { |
| 3213 | zero_ex.ee_block = ex->ee_block; |
| 3214 | zero_ex.ee_len = cpu_to_le16(map->m_lblk - |
| 3215 | ee_block); |
| 3216 | ext4_ext_store_pblock(&zero_ex, |
| 3217 | ext4_ext_pblock(ex)); |
| 3218 | err = ext4_ext_zeroout(inode, &zero_ex); |
| 3219 | if (err) |
| 3220 | goto out; |
| 3221 | } |
| 3222 | |
| 3223 | split_map.m_lblk = ee_block; |
| 3224 | split_map.m_len = map->m_lblk - ee_block + map->m_len; |
| 3225 | allocated = map->m_len; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | allocated = ext4_split_extent(handle, inode, path, |
| 3230 | &split_map, split_flag, 0); |
| 3231 | if (allocated < 0) |
| 3232 | err = allocated; |
| 3233 | |
| 3234 | out: |
| 3235 | return err ? err : allocated; |
| 3236 | } |
| 3237 | |
| 3238 | /* |
| 3239 | * This function is called by ext4_ext_map_blocks() from |
| 3240 | * ext4_get_blocks_dio_write() when DIO to write |
| 3241 | * to an uninitialized extent. |
| 3242 | * |
| 3243 | * Writing to an uninitialized extent may result in splitting the uninitialized |
| 3244 | * extent into multiple /initialized uninitialized extents (up to three) |
| 3245 | * There are three possibilities: |
| 3246 | * a> There is no split required: Entire extent should be uninitialized |
| 3247 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3248 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3249 | * |
| 3250 | * One of more index blocks maybe needed if the extent tree grow after |
| 3251 | * the uninitialized extent split. To prevent ENOSPC occur at the IO |
| 3252 | * complete, we need to split the uninitialized extent before DIO submit |
| 3253 | * the IO. The uninitialized extent called at this time will be split |
| 3254 | * into three uninitialized extent(at most). After IO complete, the part |
| 3255 | * being filled will be convert to initialized by the end_io callback function |
| 3256 | * via ext4_convert_unwritten_extents(). |
| 3257 | * |
| 3258 | * Returns the size of uninitialized extent to be written on success. |
| 3259 | */ |
| 3260 | static int ext4_split_unwritten_extents(handle_t *handle, |
| 3261 | struct inode *inode, |
| 3262 | struct ext4_map_blocks *map, |
| 3263 | struct ext4_ext_path *path, |
| 3264 | int flags) |
| 3265 | { |
| 3266 | ext4_lblk_t eof_block; |
| 3267 | ext4_lblk_t ee_block; |
| 3268 | struct ext4_extent *ex; |
| 3269 | unsigned int ee_len; |
| 3270 | int split_flag = 0, depth; |
| 3271 | |
| 3272 | ext_debug("ext4_split_unwritten_extents: inode %lu, logical" |
| 3273 | "block %llu, max_blocks %u\n", inode->i_ino, |
| 3274 | (unsigned long long)map->m_lblk, map->m_len); |
| 3275 | |
| 3276 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3277 | inode->i_sb->s_blocksize_bits; |
| 3278 | if (eof_block < map->m_lblk + map->m_len) |
| 3279 | eof_block = map->m_lblk + map->m_len; |
| 3280 | /* |
| 3281 | * It is safe to convert extent to initialized via explicit |
| 3282 | * zeroout only if extent is fully insde i_size or new_size. |
| 3283 | */ |
| 3284 | depth = ext_depth(inode); |
| 3285 | ex = path[depth].p_ext; |
| 3286 | ee_block = le32_to_cpu(ex->ee_block); |
| 3287 | ee_len = ext4_ext_get_actual_len(ex); |
| 3288 | |
| 3289 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
| 3290 | split_flag |= EXT4_EXT_MARK_UNINIT2; |
| 3291 | if (flags & EXT4_GET_BLOCKS_CONVERT) |
| 3292 | split_flag |= EXT4_EXT_DATA_VALID2; |
| 3293 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
| 3294 | return ext4_split_extent(handle, inode, path, map, split_flag, flags); |
| 3295 | } |
| 3296 | |
| 3297 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
| 3298 | struct inode *inode, |
| 3299 | struct ext4_map_blocks *map, |
| 3300 | struct ext4_ext_path *path) |
| 3301 | { |
| 3302 | struct ext4_extent *ex; |
| 3303 | ext4_lblk_t ee_block; |
| 3304 | unsigned int ee_len; |
| 3305 | int depth; |
| 3306 | int err = 0; |
| 3307 | |
| 3308 | depth = ext_depth(inode); |
| 3309 | ex = path[depth].p_ext; |
| 3310 | ee_block = le32_to_cpu(ex->ee_block); |
| 3311 | ee_len = ext4_ext_get_actual_len(ex); |
| 3312 | |
| 3313 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
| 3314 | "block %llu, max_blocks %u\n", inode->i_ino, |
| 3315 | (unsigned long long)ee_block, ee_len); |
| 3316 | |
| 3317 | /* If extent is larger than requested then split is required */ |
| 3318 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
| 3319 | err = ext4_split_unwritten_extents(handle, inode, map, path, |
| 3320 | EXT4_GET_BLOCKS_CONVERT); |
| 3321 | if (err < 0) |
| 3322 | goto out; |
| 3323 | ext4_ext_drop_refs(path); |
| 3324 | path = ext4_ext_find_extent(inode, map->m_lblk, path); |
| 3325 | if (IS_ERR(path)) { |
| 3326 | err = PTR_ERR(path); |
| 3327 | goto out; |
| 3328 | } |
| 3329 | depth = ext_depth(inode); |
| 3330 | ex = path[depth].p_ext; |
| 3331 | } |
| 3332 | |
| 3333 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3334 | if (err) |
| 3335 | goto out; |
| 3336 | /* first mark the extent as initialized */ |
| 3337 | ext4_ext_mark_initialized(ex); |
| 3338 | |
| 3339 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3340 | * borders are not changed |
| 3341 | */ |
| 3342 | ext4_ext_try_to_merge(inode, path, ex); |
| 3343 | |
| 3344 | /* Mark modified extent as dirty */ |
| 3345 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3346 | out: |
| 3347 | ext4_ext_show_leaf(inode, path); |
| 3348 | return err; |
| 3349 | } |
| 3350 | |
| 3351 | static void unmap_underlying_metadata_blocks(struct block_device *bdev, |
| 3352 | sector_t block, int count) |
| 3353 | { |
| 3354 | int i; |
| 3355 | for (i = 0; i < count; i++) |
| 3356 | unmap_underlying_metadata(bdev, block + i); |
| 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * Handle EOFBLOCKS_FL flag, clearing it if necessary |
| 3361 | */ |
| 3362 | static int check_eofblocks_fl(handle_t *handle, struct inode *inode, |
| 3363 | ext4_lblk_t lblk, |
| 3364 | struct ext4_ext_path *path, |
| 3365 | unsigned int len) |
| 3366 | { |
| 3367 | int i, depth; |
| 3368 | struct ext4_extent_header *eh; |
| 3369 | struct ext4_extent *last_ex; |
| 3370 | |
| 3371 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) |
| 3372 | return 0; |
| 3373 | |
| 3374 | depth = ext_depth(inode); |
| 3375 | eh = path[depth].p_hdr; |
| 3376 | |
| 3377 | /* |
| 3378 | * We're going to remove EOFBLOCKS_FL entirely in future so we |
| 3379 | * do not care for this case anymore. Simply remove the flag |
| 3380 | * if there are no extents. |
| 3381 | */ |
| 3382 | if (unlikely(!eh->eh_entries)) |
| 3383 | goto out; |
| 3384 | last_ex = EXT_LAST_EXTENT(eh); |
| 3385 | /* |
| 3386 | * We should clear the EOFBLOCKS_FL flag if we are writing the |
| 3387 | * last block in the last extent in the file. We test this by |
| 3388 | * first checking to see if the caller to |
| 3389 | * ext4_ext_get_blocks() was interested in the last block (or |
| 3390 | * a block beyond the last block) in the current extent. If |
| 3391 | * this turns out to be false, we can bail out from this |
| 3392 | * function immediately. |
| 3393 | */ |
| 3394 | if (lblk + len < le32_to_cpu(last_ex->ee_block) + |
| 3395 | ext4_ext_get_actual_len(last_ex)) |
| 3396 | return 0; |
| 3397 | /* |
| 3398 | * If the caller does appear to be planning to write at or |
| 3399 | * beyond the end of the current extent, we then test to see |
| 3400 | * if the current extent is the last extent in the file, by |
| 3401 | * checking to make sure it was reached via the rightmost node |
| 3402 | * at each level of the tree. |
| 3403 | */ |
| 3404 | for (i = depth-1; i >= 0; i--) |
| 3405 | if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) |
| 3406 | return 0; |
| 3407 | out: |
| 3408 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
| 3409 | return ext4_mark_inode_dirty(handle, inode); |
| 3410 | } |
| 3411 | |
| 3412 | /** |
| 3413 | * ext4_find_delalloc_range: find delayed allocated block in the given range. |
| 3414 | * |
| 3415 | * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns |
| 3416 | * whether there are any buffers marked for delayed allocation. It returns '1' |
| 3417 | * on the first delalloc'ed buffer head found. If no buffer head in the given |
| 3418 | * range is marked for delalloc, it returns 0. |
| 3419 | * lblk_start should always be <= lblk_end. |
| 3420 | * search_hint_reverse is to indicate that searching in reverse from lblk_end to |
| 3421 | * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed |
| 3422 | * block sooner). This is useful when blocks are truncated sequentially from |
| 3423 | * lblk_start towards lblk_end. |
| 3424 | */ |
| 3425 | static int ext4_find_delalloc_range(struct inode *inode, |
| 3426 | ext4_lblk_t lblk_start, |
| 3427 | ext4_lblk_t lblk_end, |
| 3428 | int search_hint_reverse) |
| 3429 | { |
| 3430 | struct address_space *mapping = inode->i_mapping; |
| 3431 | struct buffer_head *head, *bh = NULL; |
| 3432 | struct page *page; |
| 3433 | ext4_lblk_t i, pg_lblk; |
| 3434 | pgoff_t index; |
| 3435 | |
| 3436 | if (!test_opt(inode->i_sb, DELALLOC)) |
| 3437 | return 0; |
| 3438 | |
| 3439 | /* reverse search wont work if fs block size is less than page size */ |
| 3440 | if (inode->i_blkbits < PAGE_CACHE_SHIFT) |
| 3441 | search_hint_reverse = 0; |
| 3442 | |
| 3443 | if (search_hint_reverse) |
| 3444 | i = lblk_end; |
| 3445 | else |
| 3446 | i = lblk_start; |
| 3447 | |
| 3448 | index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 3449 | |
| 3450 | while ((i >= lblk_start) && (i <= lblk_end)) { |
| 3451 | page = find_get_page(mapping, index); |
| 3452 | if (!page) |
| 3453 | goto nextpage; |
| 3454 | |
| 3455 | if (!page_has_buffers(page)) |
| 3456 | goto nextpage; |
| 3457 | |
| 3458 | head = page_buffers(page); |
| 3459 | if (!head) |
| 3460 | goto nextpage; |
| 3461 | |
| 3462 | bh = head; |
| 3463 | pg_lblk = index << (PAGE_CACHE_SHIFT - |
| 3464 | inode->i_blkbits); |
| 3465 | do { |
| 3466 | if (unlikely(pg_lblk < lblk_start)) { |
| 3467 | /* |
| 3468 | * This is possible when fs block size is less |
| 3469 | * than page size and our cluster starts/ends in |
| 3470 | * middle of the page. So we need to skip the |
| 3471 | * initial few blocks till we reach the 'lblk' |
| 3472 | */ |
| 3473 | pg_lblk++; |
| 3474 | continue; |
| 3475 | } |
| 3476 | |
| 3477 | /* Check if the buffer is delayed allocated and that it |
| 3478 | * is not yet mapped. (when da-buffers are mapped during |
| 3479 | * their writeout, their da_mapped bit is set.) |
| 3480 | */ |
| 3481 | if (buffer_delay(bh) && !buffer_da_mapped(bh)) { |
| 3482 | page_cache_release(page); |
| 3483 | trace_ext4_find_delalloc_range(inode, |
| 3484 | lblk_start, lblk_end, |
| 3485 | search_hint_reverse, |
| 3486 | 1, i); |
| 3487 | return 1; |
| 3488 | } |
| 3489 | if (search_hint_reverse) |
| 3490 | i--; |
| 3491 | else |
| 3492 | i++; |
| 3493 | } while ((i >= lblk_start) && (i <= lblk_end) && |
| 3494 | ((bh = bh->b_this_page) != head)); |
| 3495 | nextpage: |
| 3496 | if (page) |
| 3497 | page_cache_release(page); |
| 3498 | /* |
| 3499 | * Move to next page. 'i' will be the first lblk in the next |
| 3500 | * page. |
| 3501 | */ |
| 3502 | if (search_hint_reverse) |
| 3503 | index--; |
| 3504 | else |
| 3505 | index++; |
| 3506 | i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 3507 | } |
| 3508 | |
| 3509 | trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end, |
| 3510 | search_hint_reverse, 0, 0); |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk, |
| 3515 | int search_hint_reverse) |
| 3516 | { |
| 3517 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 3518 | ext4_lblk_t lblk_start, lblk_end; |
| 3519 | lblk_start = lblk & (~(sbi->s_cluster_ratio - 1)); |
| 3520 | lblk_end = lblk_start + sbi->s_cluster_ratio - 1; |
| 3521 | |
| 3522 | return ext4_find_delalloc_range(inode, lblk_start, lblk_end, |
| 3523 | search_hint_reverse); |
| 3524 | } |
| 3525 | |
| 3526 | /** |
| 3527 | * Determines how many complete clusters (out of those specified by the 'map') |
| 3528 | * are under delalloc and were reserved quota for. |
| 3529 | * This function is called when we are writing out the blocks that were |
| 3530 | * originally written with their allocation delayed, but then the space was |
| 3531 | * allocated using fallocate() before the delayed allocation could be resolved. |
| 3532 | * The cases to look for are: |
| 3533 | * ('=' indicated delayed allocated blocks |
| 3534 | * '-' indicates non-delayed allocated blocks) |
| 3535 | * (a) partial clusters towards beginning and/or end outside of allocated range |
| 3536 | * are not delalloc'ed. |
| 3537 | * Ex: |
| 3538 | * |----c---=|====c====|====c====|===-c----| |
| 3539 | * |++++++ allocated ++++++| |
| 3540 | * ==> 4 complete clusters in above example |
| 3541 | * |
| 3542 | * (b) partial cluster (outside of allocated range) towards either end is |
| 3543 | * marked for delayed allocation. In this case, we will exclude that |
| 3544 | * cluster. |
| 3545 | * Ex: |
| 3546 | * |----====c========|========c========| |
| 3547 | * |++++++ allocated ++++++| |
| 3548 | * ==> 1 complete clusters in above example |
| 3549 | * |
| 3550 | * Ex: |
| 3551 | * |================c================| |
| 3552 | * |++++++ allocated ++++++| |
| 3553 | * ==> 0 complete clusters in above example |
| 3554 | * |
| 3555 | * The ext4_da_update_reserve_space will be called only if we |
| 3556 | * determine here that there were some "entire" clusters that span |
| 3557 | * this 'allocated' range. |
| 3558 | * In the non-bigalloc case, this function will just end up returning num_blks |
| 3559 | * without ever calling ext4_find_delalloc_range. |
| 3560 | */ |
| 3561 | static unsigned int |
| 3562 | get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start, |
| 3563 | unsigned int num_blks) |
| 3564 | { |
| 3565 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 3566 | ext4_lblk_t alloc_cluster_start, alloc_cluster_end; |
| 3567 | ext4_lblk_t lblk_from, lblk_to, c_offset; |
| 3568 | unsigned int allocated_clusters = 0; |
| 3569 | |
| 3570 | alloc_cluster_start = EXT4_B2C(sbi, lblk_start); |
| 3571 | alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1); |
| 3572 | |
| 3573 | /* max possible clusters for this allocation */ |
| 3574 | allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1; |
| 3575 | |
| 3576 | trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks); |
| 3577 | |
| 3578 | /* Check towards left side */ |
| 3579 | c_offset = lblk_start & (sbi->s_cluster_ratio - 1); |
| 3580 | if (c_offset) { |
| 3581 | lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1)); |
| 3582 | lblk_to = lblk_from + c_offset - 1; |
| 3583 | |
| 3584 | if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0)) |
| 3585 | allocated_clusters--; |
| 3586 | } |
| 3587 | |
| 3588 | /* Now check towards right. */ |
| 3589 | c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1); |
| 3590 | if (allocated_clusters && c_offset) { |
| 3591 | lblk_from = lblk_start + num_blks; |
| 3592 | lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1; |
| 3593 | |
| 3594 | if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0)) |
| 3595 | allocated_clusters--; |
| 3596 | } |
| 3597 | |
| 3598 | return allocated_clusters; |
| 3599 | } |
| 3600 | |
| 3601 | static int |
| 3602 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, |
| 3603 | struct ext4_map_blocks *map, |
| 3604 | struct ext4_ext_path *path, int flags, |
| 3605 | unsigned int allocated, ext4_fsblk_t newblock) |
| 3606 | { |
| 3607 | int ret = 0; |
| 3608 | int err = 0; |
| 3609 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; |
| 3610 | |
| 3611 | ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical " |
| 3612 | "block %llu, max_blocks %u, flags %x, allocated %u\n", |
| 3613 | inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
| 3614 | flags, allocated); |
| 3615 | ext4_ext_show_leaf(inode, path); |
| 3616 | |
| 3617 | trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated, |
| 3618 | newblock); |
| 3619 | |
| 3620 | /* get_block() before submit the IO, split the extent */ |
| 3621 | if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { |
| 3622 | ret = ext4_split_unwritten_extents(handle, inode, map, |
| 3623 | path, flags); |
| 3624 | /* |
| 3625 | * Flag the inode(non aio case) or end_io struct (aio case) |
| 3626 | * that this IO needs to conversion to written when IO is |
| 3627 | * completed |
| 3628 | */ |
| 3629 | if (io) |
| 3630 | ext4_set_io_unwritten_flag(inode, io); |
| 3631 | else |
| 3632 | ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); |
| 3633 | if (ext4_should_dioread_nolock(inode)) |
| 3634 | map->m_flags |= EXT4_MAP_UNINIT; |
| 3635 | goto out; |
| 3636 | } |
| 3637 | /* IO end_io complete, convert the filled extent to written */ |
| 3638 | if ((flags & EXT4_GET_BLOCKS_CONVERT)) { |
| 3639 | ret = ext4_convert_unwritten_extents_endio(handle, inode, map, |
| 3640 | path); |
| 3641 | if (ret >= 0) { |
| 3642 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3643 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
| 3644 | path, map->m_len); |
| 3645 | } else |
| 3646 | err = ret; |
| 3647 | goto out2; |
| 3648 | } |
| 3649 | /* buffered IO case */ |
| 3650 | /* |
| 3651 | * repeat fallocate creation request |
| 3652 | * we already have an unwritten extent |
| 3653 | */ |
| 3654 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) |
| 3655 | goto map_out; |
| 3656 | |
| 3657 | /* buffered READ or buffered write_begin() lookup */ |
| 3658 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3659 | /* |
| 3660 | * We have blocks reserved already. We |
| 3661 | * return allocated blocks so that delalloc |
| 3662 | * won't do block reservation for us. But |
| 3663 | * the buffer head will be unmapped so that |
| 3664 | * a read from the block returns 0s. |
| 3665 | */ |
| 3666 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
| 3667 | goto out1; |
| 3668 | } |
| 3669 | |
| 3670 | /* buffered write, writepage time, convert*/ |
| 3671 | ret = ext4_ext_convert_to_initialized(handle, inode, map, path); |
| 3672 | if (ret >= 0) |
| 3673 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3674 | out: |
| 3675 | if (ret <= 0) { |
| 3676 | err = ret; |
| 3677 | goto out2; |
| 3678 | } else |
| 3679 | allocated = ret; |
| 3680 | map->m_flags |= EXT4_MAP_NEW; |
| 3681 | /* |
| 3682 | * if we allocated more blocks than requested |
| 3683 | * we need to make sure we unmap the extra block |
| 3684 | * allocated. The actual needed block will get |
| 3685 | * unmapped later when we find the buffer_head marked |
| 3686 | * new. |
| 3687 | */ |
| 3688 | if (allocated > map->m_len) { |
| 3689 | unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, |
| 3690 | newblock + map->m_len, |
| 3691 | allocated - map->m_len); |
| 3692 | allocated = map->m_len; |
| 3693 | } |
| 3694 | map->m_len = allocated; |
| 3695 | |
| 3696 | /* |
| 3697 | * If we have done fallocate with the offset that is already |
| 3698 | * delayed allocated, we would have block reservation |
| 3699 | * and quota reservation done in the delayed write path. |
| 3700 | * But fallocate would have already updated quota and block |
| 3701 | * count for this offset. So cancel these reservation |
| 3702 | */ |
| 3703 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
| 3704 | unsigned int reserved_clusters; |
| 3705 | reserved_clusters = get_reserved_cluster_alloc(inode, |
| 3706 | map->m_lblk, map->m_len); |
| 3707 | if (reserved_clusters) |
| 3708 | ext4_da_update_reserve_space(inode, |
| 3709 | reserved_clusters, |
| 3710 | 0); |
| 3711 | } |
| 3712 | |
| 3713 | map_out: |
| 3714 | map->m_flags |= EXT4_MAP_MAPPED; |
| 3715 | if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) { |
| 3716 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, |
| 3717 | map->m_len); |
| 3718 | if (err < 0) |
| 3719 | goto out2; |
| 3720 | } |
| 3721 | out1: |
| 3722 | if (allocated > map->m_len) |
| 3723 | allocated = map->m_len; |
| 3724 | ext4_ext_show_leaf(inode, path); |
| 3725 | map->m_pblk = newblock; |
| 3726 | map->m_len = allocated; |
| 3727 | out2: |
| 3728 | if (path) { |
| 3729 | ext4_ext_drop_refs(path); |
| 3730 | kfree(path); |
| 3731 | } |
| 3732 | return err ? err : allocated; |
| 3733 | } |
| 3734 | |
| 3735 | /* |
| 3736 | * get_implied_cluster_alloc - check to see if the requested |
| 3737 | * allocation (in the map structure) overlaps with a cluster already |
| 3738 | * allocated in an extent. |
| 3739 | * @sb The filesystem superblock structure |
| 3740 | * @map The requested lblk->pblk mapping |
| 3741 | * @ex The extent structure which might contain an implied |
| 3742 | * cluster allocation |
| 3743 | * |
| 3744 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 3745 | * find blocks that were already in the inode's extent tree. Hence, |
| 3746 | * we know that the beginning of the requested region cannot overlap |
| 3747 | * the extent from the inode's extent tree. There are three cases we |
| 3748 | * want to catch. The first is this case: |
| 3749 | * |
| 3750 | * |--- cluster # N--| |
| 3751 | * |--- extent ---| |---- requested region ---| |
| 3752 | * |==========| |
| 3753 | * |
| 3754 | * The second case that we need to test for is this one: |
| 3755 | * |
| 3756 | * |--------- cluster # N ----------------| |
| 3757 | * |--- requested region --| |------- extent ----| |
| 3758 | * |=======================| |
| 3759 | * |
| 3760 | * The third case is when the requested region lies between two extents |
| 3761 | * within the same cluster: |
| 3762 | * |------------- cluster # N-------------| |
| 3763 | * |----- ex -----| |---- ex_right ----| |
| 3764 | * |------ requested region ------| |
| 3765 | * |================| |
| 3766 | * |
| 3767 | * In each of the above cases, we need to set the map->m_pblk and |
| 3768 | * map->m_len so it corresponds to the return the extent labelled as |
| 3769 | * "|====|" from cluster #N, since it is already in use for data in |
| 3770 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 3771 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 3772 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 3773 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 3774 | * by calling ext4_mb_new_blocks(). |
| 3775 | */ |
| 3776 | static int get_implied_cluster_alloc(struct super_block *sb, |
| 3777 | struct ext4_map_blocks *map, |
| 3778 | struct ext4_extent *ex, |
| 3779 | struct ext4_ext_path *path) |
| 3780 | { |
| 3781 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3782 | ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1); |
| 3783 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
| 3784 | ext4_lblk_t rr_cluster_start; |
| 3785 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 3786 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 3787 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 3788 | |
| 3789 | /* The extent passed in that we are trying to match */ |
| 3790 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 3791 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 3792 | |
| 3793 | /* The requested region passed into ext4_map_blocks() */ |
| 3794 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
| 3795 | |
| 3796 | if ((rr_cluster_start == ex_cluster_end) || |
| 3797 | (rr_cluster_start == ex_cluster_start)) { |
| 3798 | if (rr_cluster_start == ex_cluster_end) |
| 3799 | ee_start += ee_len - 1; |
| 3800 | map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) + |
| 3801 | c_offset; |
| 3802 | map->m_len = min(map->m_len, |
| 3803 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 3804 | /* |
| 3805 | * Check for and handle this case: |
| 3806 | * |
| 3807 | * |--------- cluster # N-------------| |
| 3808 | * |------- extent ----| |
| 3809 | * |--- requested region ---| |
| 3810 | * |===========| |
| 3811 | */ |
| 3812 | |
| 3813 | if (map->m_lblk < ee_block) |
| 3814 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 3815 | |
| 3816 | /* |
| 3817 | * Check for the case where there is already another allocated |
| 3818 | * block to the right of 'ex' but before the end of the cluster. |
| 3819 | * |
| 3820 | * |------------- cluster # N-------------| |
| 3821 | * |----- ex -----| |---- ex_right ----| |
| 3822 | * |------ requested region ------| |
| 3823 | * |================| |
| 3824 | */ |
| 3825 | if (map->m_lblk > ee_block) { |
| 3826 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 3827 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 3828 | } |
| 3829 | |
| 3830 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); |
| 3831 | return 1; |
| 3832 | } |
| 3833 | |
| 3834 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); |
| 3835 | return 0; |
| 3836 | } |
| 3837 | |
| 3838 | |
| 3839 | /* |
| 3840 | * Block allocation/map/preallocation routine for extents based files |
| 3841 | * |
| 3842 | * |
| 3843 | * Need to be called with |
| 3844 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 3845 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) |
| 3846 | * |
| 3847 | * return > 0, number of of blocks already mapped/allocated |
| 3848 | * if create == 0 and these are pre-allocated blocks |
| 3849 | * buffer head is unmapped |
| 3850 | * otherwise blocks are mapped |
| 3851 | * |
| 3852 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 3853 | * buffer head is unmapped |
| 3854 | * |
| 3855 | * return < 0, error case. |
| 3856 | */ |
| 3857 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 3858 | struct ext4_map_blocks *map, int flags) |
| 3859 | { |
| 3860 | struct ext4_ext_path *path = NULL; |
| 3861 | struct ext4_extent newex, *ex, *ex2; |
| 3862 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 3863 | ext4_fsblk_t newblock = 0; |
| 3864 | int free_on_err = 0, err = 0, depth, ret; |
| 3865 | unsigned int allocated = 0, offset = 0; |
| 3866 | unsigned int allocated_clusters = 0; |
| 3867 | struct ext4_allocation_request ar; |
| 3868 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; |
| 3869 | ext4_lblk_t cluster_offset; |
| 3870 | |
| 3871 | ext_debug("blocks %u/%u requested for inode %lu\n", |
| 3872 | map->m_lblk, map->m_len, inode->i_ino); |
| 3873 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); |
| 3874 | |
| 3875 | /* check in cache */ |
| 3876 | if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) { |
| 3877 | if (!newex.ee_start_lo && !newex.ee_start_hi) { |
| 3878 | if ((sbi->s_cluster_ratio > 1) && |
| 3879 | ext4_find_delalloc_cluster(inode, map->m_lblk, 0)) |
| 3880 | map->m_flags |= EXT4_MAP_FROM_CLUSTER; |
| 3881 | |
| 3882 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3883 | /* |
| 3884 | * block isn't allocated yet and |
| 3885 | * user doesn't want to allocate it |
| 3886 | */ |
| 3887 | goto out2; |
| 3888 | } |
| 3889 | /* we should allocate requested block */ |
| 3890 | } else { |
| 3891 | /* block is already allocated */ |
| 3892 | if (sbi->s_cluster_ratio > 1) |
| 3893 | map->m_flags |= EXT4_MAP_FROM_CLUSTER; |
| 3894 | newblock = map->m_lblk |
| 3895 | - le32_to_cpu(newex.ee_block) |
| 3896 | + ext4_ext_pblock(&newex); |
| 3897 | /* number of remaining blocks in the extent */ |
| 3898 | allocated = ext4_ext_get_actual_len(&newex) - |
| 3899 | (map->m_lblk - le32_to_cpu(newex.ee_block)); |
| 3900 | goto out; |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | /* find extent for this block */ |
| 3905 | path = ext4_ext_find_extent(inode, map->m_lblk, NULL); |
| 3906 | if (IS_ERR(path)) { |
| 3907 | err = PTR_ERR(path); |
| 3908 | path = NULL; |
| 3909 | goto out2; |
| 3910 | } |
| 3911 | |
| 3912 | depth = ext_depth(inode); |
| 3913 | |
| 3914 | /* |
| 3915 | * consistent leaf must not be empty; |
| 3916 | * this situation is possible, though, _during_ tree modification; |
| 3917 | * this is why assert can't be put in ext4_ext_find_extent() |
| 3918 | */ |
| 3919 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 3920 | EXT4_ERROR_INODE(inode, "bad extent address " |
| 3921 | "lblock: %lu, depth: %d pblock %lld", |
| 3922 | (unsigned long) map->m_lblk, depth, |
| 3923 | path[depth].p_block); |
| 3924 | err = -EIO; |
| 3925 | goto out2; |
| 3926 | } |
| 3927 | |
| 3928 | ex = path[depth].p_ext; |
| 3929 | if (ex) { |
| 3930 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 3931 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 3932 | unsigned short ee_len; |
| 3933 | |
| 3934 | /* |
| 3935 | * Uninitialized extents are treated as holes, except that |
| 3936 | * we split out initialized portions during a write. |
| 3937 | */ |
| 3938 | ee_len = ext4_ext_get_actual_len(ex); |
| 3939 | |
| 3940 | trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); |
| 3941 | |
| 3942 | /* if found extent covers block, simply return it */ |
| 3943 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
| 3944 | newblock = map->m_lblk - ee_block + ee_start; |
| 3945 | /* number of remaining blocks in the extent */ |
| 3946 | allocated = ee_len - (map->m_lblk - ee_block); |
| 3947 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
| 3948 | ee_block, ee_len, newblock); |
| 3949 | |
| 3950 | /* |
| 3951 | * Do not put uninitialized extent |
| 3952 | * in the cache |
| 3953 | */ |
| 3954 | if (!ext4_ext_is_uninitialized(ex)) { |
| 3955 | ext4_ext_put_in_cache(inode, ee_block, |
| 3956 | ee_len, ee_start); |
| 3957 | goto out; |
| 3958 | } |
| 3959 | ret = ext4_ext_handle_uninitialized_extents( |
| 3960 | handle, inode, map, path, flags, |
| 3961 | allocated, newblock); |
| 3962 | return ret; |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | if ((sbi->s_cluster_ratio > 1) && |
| 3967 | ext4_find_delalloc_cluster(inode, map->m_lblk, 0)) |
| 3968 | map->m_flags |= EXT4_MAP_FROM_CLUSTER; |
| 3969 | |
| 3970 | /* |
| 3971 | * requested block isn't allocated yet; |
| 3972 | * we couldn't try to create block if create flag is zero |
| 3973 | */ |
| 3974 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3975 | /* |
| 3976 | * put just found gap into cache to speed up |
| 3977 | * subsequent requests |
| 3978 | */ |
| 3979 | ext4_ext_put_gap_in_cache(inode, path, map->m_lblk); |
| 3980 | goto out2; |
| 3981 | } |
| 3982 | |
| 3983 | /* |
| 3984 | * Okay, we need to do block allocation. |
| 3985 | */ |
| 3986 | map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; |
| 3987 | newex.ee_block = cpu_to_le32(map->m_lblk); |
| 3988 | cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1); |
| 3989 | |
| 3990 | /* |
| 3991 | * If we are doing bigalloc, check to see if the extent returned |
| 3992 | * by ext4_ext_find_extent() implies a cluster we can use. |
| 3993 | */ |
| 3994 | if (cluster_offset && ex && |
| 3995 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
| 3996 | ar.len = allocated = map->m_len; |
| 3997 | newblock = map->m_pblk; |
| 3998 | map->m_flags |= EXT4_MAP_FROM_CLUSTER; |
| 3999 | goto got_allocated_blocks; |
| 4000 | } |
| 4001 | |
| 4002 | /* find neighbour allocated blocks */ |
| 4003 | ar.lleft = map->m_lblk; |
| 4004 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 4005 | if (err) |
| 4006 | goto out2; |
| 4007 | ar.lright = map->m_lblk; |
| 4008 | ex2 = NULL; |
| 4009 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
| 4010 | if (err) |
| 4011 | goto out2; |
| 4012 | |
| 4013 | /* Check if the extent after searching to the right implies a |
| 4014 | * cluster we can use. */ |
| 4015 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
| 4016 | get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
| 4017 | ar.len = allocated = map->m_len; |
| 4018 | newblock = map->m_pblk; |
| 4019 | map->m_flags |= EXT4_MAP_FROM_CLUSTER; |
| 4020 | goto got_allocated_blocks; |
| 4021 | } |
| 4022 | |
| 4023 | /* |
| 4024 | * See if request is beyond maximum number of blocks we can have in |
| 4025 | * a single extent. For an initialized extent this limit is |
| 4026 | * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is |
| 4027 | * EXT_UNINIT_MAX_LEN. |
| 4028 | */ |
| 4029 | if (map->m_len > EXT_INIT_MAX_LEN && |
| 4030 | !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) |
| 4031 | map->m_len = EXT_INIT_MAX_LEN; |
| 4032 | else if (map->m_len > EXT_UNINIT_MAX_LEN && |
| 4033 | (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) |
| 4034 | map->m_len = EXT_UNINIT_MAX_LEN; |
| 4035 | |
| 4036 | /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ |
| 4037 | newex.ee_len = cpu_to_le16(map->m_len); |
| 4038 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
| 4039 | if (err) |
| 4040 | allocated = ext4_ext_get_actual_len(&newex); |
| 4041 | else |
| 4042 | allocated = map->m_len; |
| 4043 | |
| 4044 | /* allocate new block */ |
| 4045 | ar.inode = inode; |
| 4046 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 4047 | ar.logical = map->m_lblk; |
| 4048 | /* |
| 4049 | * We calculate the offset from the beginning of the cluster |
| 4050 | * for the logical block number, since when we allocate a |
| 4051 | * physical cluster, the physical block should start at the |
| 4052 | * same offset from the beginning of the cluster. This is |
| 4053 | * needed so that future calls to get_implied_cluster_alloc() |
| 4054 | * work correctly. |
| 4055 | */ |
| 4056 | offset = map->m_lblk & (sbi->s_cluster_ratio - 1); |
| 4057 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 4058 | ar.goal -= offset; |
| 4059 | ar.logical -= offset; |
| 4060 | if (S_ISREG(inode->i_mode)) |
| 4061 | ar.flags = EXT4_MB_HINT_DATA; |
| 4062 | else |
| 4063 | /* disable in-core preallocation for non-regular files */ |
| 4064 | ar.flags = 0; |
| 4065 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 4066 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
| 4067 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
| 4068 | if (!newblock) |
| 4069 | goto out2; |
| 4070 | ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
| 4071 | ar.goal, newblock, allocated); |
| 4072 | free_on_err = 1; |
| 4073 | allocated_clusters = ar.len; |
| 4074 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
| 4075 | if (ar.len > allocated) |
| 4076 | ar.len = allocated; |
| 4077 | |
| 4078 | got_allocated_blocks: |
| 4079 | /* try to insert new extent into found leaf and return */ |
| 4080 | ext4_ext_store_pblock(&newex, newblock + offset); |
| 4081 | newex.ee_len = cpu_to_le16(ar.len); |
| 4082 | /* Mark uninitialized */ |
| 4083 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ |
| 4084 | ext4_ext_mark_uninitialized(&newex); |
| 4085 | /* |
| 4086 | * io_end structure was created for every IO write to an |
| 4087 | * uninitialized extent. To avoid unnecessary conversion, |
| 4088 | * here we flag the IO that really needs the conversion. |
| 4089 | * For non asycn direct IO case, flag the inode state |
| 4090 | * that we need to perform conversion when IO is done. |
| 4091 | */ |
| 4092 | if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { |
| 4093 | if (io) |
| 4094 | ext4_set_io_unwritten_flag(inode, io); |
| 4095 | else |
| 4096 | ext4_set_inode_state(inode, |
| 4097 | EXT4_STATE_DIO_UNWRITTEN); |
| 4098 | } |
| 4099 | if (ext4_should_dioread_nolock(inode)) |
| 4100 | map->m_flags |= EXT4_MAP_UNINIT; |
| 4101 | } |
| 4102 | |
| 4103 | err = 0; |
| 4104 | if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) |
| 4105 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
| 4106 | path, ar.len); |
| 4107 | if (!err) |
| 4108 | err = ext4_ext_insert_extent(handle, inode, path, |
| 4109 | &newex, flags); |
| 4110 | if (err && free_on_err) { |
| 4111 | int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? |
| 4112 | EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; |
| 4113 | /* free data blocks we just allocated */ |
| 4114 | /* not a good idea to call discard here directly, |
| 4115 | * but otherwise we'd need to call it every free() */ |
| 4116 | ext4_discard_preallocations(inode); |
| 4117 | ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex), |
| 4118 | ext4_ext_get_actual_len(&newex), fb_flags); |
| 4119 | goto out2; |
| 4120 | } |
| 4121 | |
| 4122 | /* previous routine could use block we allocated */ |
| 4123 | newblock = ext4_ext_pblock(&newex); |
| 4124 | allocated = ext4_ext_get_actual_len(&newex); |
| 4125 | if (allocated > map->m_len) |
| 4126 | allocated = map->m_len; |
| 4127 | map->m_flags |= EXT4_MAP_NEW; |
| 4128 | |
| 4129 | /* |
| 4130 | * Update reserved blocks/metadata blocks after successful |
| 4131 | * block allocation which had been deferred till now. |
| 4132 | */ |
| 4133 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
| 4134 | unsigned int reserved_clusters; |
| 4135 | /* |
| 4136 | * Check how many clusters we had reserved this allocated range |
| 4137 | */ |
| 4138 | reserved_clusters = get_reserved_cluster_alloc(inode, |
| 4139 | map->m_lblk, allocated); |
| 4140 | if (map->m_flags & EXT4_MAP_FROM_CLUSTER) { |
| 4141 | if (reserved_clusters) { |
| 4142 | /* |
| 4143 | * We have clusters reserved for this range. |
| 4144 | * But since we are not doing actual allocation |
| 4145 | * and are simply using blocks from previously |
| 4146 | * allocated cluster, we should release the |
| 4147 | * reservation and not claim quota. |
| 4148 | */ |
| 4149 | ext4_da_update_reserve_space(inode, |
| 4150 | reserved_clusters, 0); |
| 4151 | } |
| 4152 | } else { |
| 4153 | BUG_ON(allocated_clusters < reserved_clusters); |
| 4154 | /* We will claim quota for all newly allocated blocks.*/ |
| 4155 | ext4_da_update_reserve_space(inode, allocated_clusters, |
| 4156 | 1); |
| 4157 | if (reserved_clusters < allocated_clusters) { |
| 4158 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 4159 | int reservation = allocated_clusters - |
| 4160 | reserved_clusters; |
| 4161 | /* |
| 4162 | * It seems we claimed few clusters outside of |
| 4163 | * the range of this allocation. We should give |
| 4164 | * it back to the reservation pool. This can |
| 4165 | * happen in the following case: |
| 4166 | * |
| 4167 | * * Suppose s_cluster_ratio is 4 (i.e., each |
| 4168 | * cluster has 4 blocks. Thus, the clusters |
| 4169 | * are [0-3],[4-7],[8-11]... |
| 4170 | * * First comes delayed allocation write for |
| 4171 | * logical blocks 10 & 11. Since there were no |
| 4172 | * previous delayed allocated blocks in the |
| 4173 | * range [8-11], we would reserve 1 cluster |
| 4174 | * for this write. |
| 4175 | * * Next comes write for logical blocks 3 to 8. |
| 4176 | * In this case, we will reserve 2 clusters |
| 4177 | * (for [0-3] and [4-7]; and not for [8-11] as |
| 4178 | * that range has a delayed allocated blocks. |
| 4179 | * Thus total reserved clusters now becomes 3. |
| 4180 | * * Now, during the delayed allocation writeout |
| 4181 | * time, we will first write blocks [3-8] and |
| 4182 | * allocate 3 clusters for writing these |
| 4183 | * blocks. Also, we would claim all these |
| 4184 | * three clusters above. |
| 4185 | * * Now when we come here to writeout the |
| 4186 | * blocks [10-11], we would expect to claim |
| 4187 | * the reservation of 1 cluster we had made |
| 4188 | * (and we would claim it since there are no |
| 4189 | * more delayed allocated blocks in the range |
| 4190 | * [8-11]. But our reserved cluster count had |
| 4191 | * already gone to 0. |
| 4192 | * |
| 4193 | * Thus, at the step 4 above when we determine |
| 4194 | * that there are still some unwritten delayed |
| 4195 | * allocated blocks outside of our current |
| 4196 | * block range, we should increment the |
| 4197 | * reserved clusters count so that when the |
| 4198 | * remaining blocks finally gets written, we |
| 4199 | * could claim them. |
| 4200 | */ |
| 4201 | dquot_reserve_block(inode, |
| 4202 | EXT4_C2B(sbi, reservation)); |
| 4203 | spin_lock(&ei->i_block_reservation_lock); |
| 4204 | ei->i_reserved_data_blocks += reservation; |
| 4205 | spin_unlock(&ei->i_block_reservation_lock); |
| 4206 | } |
| 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | /* |
| 4211 | * Cache the extent and update transaction to commit on fdatasync only |
| 4212 | * when it is _not_ an uninitialized extent. |
| 4213 | */ |
| 4214 | if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { |
| 4215 | ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock); |
| 4216 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 4217 | } else |
| 4218 | ext4_update_inode_fsync_trans(handle, inode, 0); |
| 4219 | out: |
| 4220 | if (allocated > map->m_len) |
| 4221 | allocated = map->m_len; |
| 4222 | ext4_ext_show_leaf(inode, path); |
| 4223 | map->m_flags |= EXT4_MAP_MAPPED; |
| 4224 | map->m_pblk = newblock; |
| 4225 | map->m_len = allocated; |
| 4226 | out2: |
| 4227 | if (path) { |
| 4228 | ext4_ext_drop_refs(path); |
| 4229 | kfree(path); |
| 4230 | } |
| 4231 | |
| 4232 | trace_ext4_ext_map_blocks_exit(inode, map->m_lblk, |
| 4233 | newblock, map->m_len, err ? err : allocated); |
| 4234 | |
| 4235 | return err ? err : allocated; |
| 4236 | } |
| 4237 | |
| 4238 | void ext4_ext_truncate(struct inode *inode) |
| 4239 | { |
| 4240 | struct address_space *mapping = inode->i_mapping; |
| 4241 | struct super_block *sb = inode->i_sb; |
| 4242 | ext4_lblk_t last_block; |
| 4243 | handle_t *handle; |
| 4244 | loff_t page_len; |
| 4245 | int err = 0; |
| 4246 | |
| 4247 | /* |
| 4248 | * finish any pending end_io work so we won't run the risk of |
| 4249 | * converting any truncated blocks to initialized later |
| 4250 | */ |
| 4251 | ext4_flush_completed_IO(inode); |
| 4252 | |
| 4253 | /* |
| 4254 | * probably first extent we're gonna free will be last in block |
| 4255 | */ |
| 4256 | err = ext4_writepage_trans_blocks(inode); |
| 4257 | handle = ext4_journal_start(inode, err); |
| 4258 | if (IS_ERR(handle)) |
| 4259 | return; |
| 4260 | |
| 4261 | if (inode->i_size % PAGE_CACHE_SIZE != 0) { |
| 4262 | page_len = PAGE_CACHE_SIZE - |
| 4263 | (inode->i_size & (PAGE_CACHE_SIZE - 1)); |
| 4264 | |
| 4265 | err = ext4_discard_partial_page_buffers(handle, |
| 4266 | mapping, inode->i_size, page_len, 0); |
| 4267 | |
| 4268 | if (err) |
| 4269 | goto out_stop; |
| 4270 | } |
| 4271 | |
| 4272 | if (ext4_orphan_add(handle, inode)) |
| 4273 | goto out_stop; |
| 4274 | |
| 4275 | down_write(&EXT4_I(inode)->i_data_sem); |
| 4276 | ext4_ext_invalidate_cache(inode); |
| 4277 | |
| 4278 | ext4_discard_preallocations(inode); |
| 4279 | |
| 4280 | /* |
| 4281 | * TODO: optimization is possible here. |
| 4282 | * Probably we need not scan at all, |
| 4283 | * because page truncation is enough. |
| 4284 | */ |
| 4285 | |
| 4286 | /* we have to know where to truncate from in crash case */ |
| 4287 | EXT4_I(inode)->i_disksize = inode->i_size; |
| 4288 | ext4_mark_inode_dirty(handle, inode); |
| 4289 | |
| 4290 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 4291 | >> EXT4_BLOCK_SIZE_BITS(sb); |
| 4292 | err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
| 4293 | |
| 4294 | /* In a multi-transaction truncate, we only make the final |
| 4295 | * transaction synchronous. |
| 4296 | */ |
| 4297 | if (IS_SYNC(inode)) |
| 4298 | ext4_handle_sync(handle); |
| 4299 | |
| 4300 | up_write(&EXT4_I(inode)->i_data_sem); |
| 4301 | |
| 4302 | out_stop: |
| 4303 | /* |
| 4304 | * If this was a simple ftruncate() and the file will remain alive, |
| 4305 | * then we need to clear up the orphan record which we created above. |
| 4306 | * However, if this was a real unlink then we were called by |
| 4307 | * ext4_delete_inode(), and we allow that function to clean up the |
| 4308 | * orphan info for us. |
| 4309 | */ |
| 4310 | if (inode->i_nlink) |
| 4311 | ext4_orphan_del(handle, inode); |
| 4312 | |
| 4313 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 4314 | ext4_mark_inode_dirty(handle, inode); |
| 4315 | ext4_journal_stop(handle); |
| 4316 | } |
| 4317 | |
| 4318 | static void ext4_falloc_update_inode(struct inode *inode, |
| 4319 | int mode, loff_t new_size, int update_ctime) |
| 4320 | { |
| 4321 | struct timespec now; |
| 4322 | |
| 4323 | if (update_ctime) { |
| 4324 | now = current_fs_time(inode->i_sb); |
| 4325 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 4326 | inode->i_ctime = now; |
| 4327 | } |
| 4328 | /* |
| 4329 | * Update only when preallocation was requested beyond |
| 4330 | * the file size. |
| 4331 | */ |
| 4332 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
| 4333 | if (new_size > i_size_read(inode)) |
| 4334 | i_size_write(inode, new_size); |
| 4335 | if (new_size > EXT4_I(inode)->i_disksize) |
| 4336 | ext4_update_i_disksize(inode, new_size); |
| 4337 | } else { |
| 4338 | /* |
| 4339 | * Mark that we allocate beyond EOF so the subsequent truncate |
| 4340 | * can proceed even if the new size is the same as i_size. |
| 4341 | */ |
| 4342 | if (new_size > i_size_read(inode)) |
| 4343 | ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
| 4344 | } |
| 4345 | |
| 4346 | } |
| 4347 | |
| 4348 | /* |
| 4349 | * preallocate space for a file. This implements ext4's fallocate file |
| 4350 | * operation, which gets called from sys_fallocate system call. |
| 4351 | * For block-mapped files, posix_fallocate should fall back to the method |
| 4352 | * of writing zeroes to the required new blocks (the same behavior which is |
| 4353 | * expected for file systems which do not support fallocate() system call). |
| 4354 | */ |
| 4355 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 4356 | { |
| 4357 | struct inode *inode = file->f_path.dentry->d_inode; |
| 4358 | handle_t *handle; |
| 4359 | loff_t new_size; |
| 4360 | unsigned int max_blocks; |
| 4361 | int ret = 0; |
| 4362 | int ret2 = 0; |
| 4363 | int retries = 0; |
| 4364 | int flags; |
| 4365 | struct ext4_map_blocks map; |
| 4366 | unsigned int credits, blkbits = inode->i_blkbits; |
| 4367 | |
| 4368 | /* Return error if mode is not supported */ |
| 4369 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 4370 | return -EOPNOTSUPP; |
| 4371 | |
| 4372 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 4373 | return ext4_punch_hole(file, offset, len); |
| 4374 | |
| 4375 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
| 4376 | map.m_lblk = offset >> blkbits; |
| 4377 | /* |
| 4378 | * We can't just convert len to max_blocks because |
| 4379 | * If blocksize = 4096 offset = 3072 and len = 2048 |
| 4380 | */ |
| 4381 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) |
| 4382 | - map.m_lblk; |
| 4383 | /* |
| 4384 | * credits to insert 1 extent into extent tree |
| 4385 | */ |
| 4386 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4387 | mutex_lock(&inode->i_mutex); |
| 4388 | |
| 4389 | /* |
| 4390 | * We only support preallocation for extent-based files only |
| 4391 | */ |
| 4392 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4393 | mutex_unlock(&inode->i_mutex); |
| 4394 | return -EOPNOTSUPP; |
| 4395 | } |
| 4396 | |
| 4397 | ret = inode_newsize_ok(inode, (len + offset)); |
| 4398 | if (ret) { |
| 4399 | mutex_unlock(&inode->i_mutex); |
| 4400 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
| 4401 | return ret; |
| 4402 | } |
| 4403 | flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT; |
| 4404 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4405 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4406 | /* |
| 4407 | * Don't normalize the request if it can fit in one extent so |
| 4408 | * that it doesn't get unnecessarily split into multiple |
| 4409 | * extents. |
| 4410 | */ |
| 4411 | if (len <= EXT_UNINIT_MAX_LEN << blkbits) |
| 4412 | flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; |
| 4413 | retry: |
| 4414 | while (ret >= 0 && ret < max_blocks) { |
| 4415 | map.m_lblk = map.m_lblk + ret; |
| 4416 | map.m_len = max_blocks = max_blocks - ret; |
| 4417 | handle = ext4_journal_start(inode, credits); |
| 4418 | if (IS_ERR(handle)) { |
| 4419 | ret = PTR_ERR(handle); |
| 4420 | break; |
| 4421 | } |
| 4422 | ret = ext4_map_blocks(handle, inode, &map, flags); |
| 4423 | if (ret <= 0) { |
| 4424 | #ifdef EXT4FS_DEBUG |
| 4425 | WARN_ON(ret <= 0); |
| 4426 | printk(KERN_ERR "%s: ext4_ext_map_blocks " |
| 4427 | "returned error inode#%lu, block=%u, " |
| 4428 | "max_blocks=%u", __func__, |
| 4429 | inode->i_ino, map.m_lblk, max_blocks); |
| 4430 | #endif |
| 4431 | ext4_mark_inode_dirty(handle, inode); |
| 4432 | ret2 = ext4_journal_stop(handle); |
| 4433 | break; |
| 4434 | } |
| 4435 | if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len, |
| 4436 | blkbits) >> blkbits)) |
| 4437 | new_size = offset + len; |
| 4438 | else |
| 4439 | new_size = ((loff_t) map.m_lblk + ret) << blkbits; |
| 4440 | |
| 4441 | ext4_falloc_update_inode(inode, mode, new_size, |
| 4442 | (map.m_flags & EXT4_MAP_NEW)); |
| 4443 | ext4_mark_inode_dirty(handle, inode); |
| 4444 | ret2 = ext4_journal_stop(handle); |
| 4445 | if (ret2) |
| 4446 | break; |
| 4447 | } |
| 4448 | if (ret == -ENOSPC && |
| 4449 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4450 | ret = 0; |
| 4451 | goto retry; |
| 4452 | } |
| 4453 | mutex_unlock(&inode->i_mutex); |
| 4454 | trace_ext4_fallocate_exit(inode, offset, max_blocks, |
| 4455 | ret > 0 ? ret2 : ret); |
| 4456 | return ret > 0 ? ret2 : ret; |
| 4457 | } |
| 4458 | |
| 4459 | /* |
| 4460 | * This function convert a range of blocks to written extents |
| 4461 | * The caller of this function will pass the start offset and the size. |
| 4462 | * all unwritten extents within this range will be converted to |
| 4463 | * written extents. |
| 4464 | * |
| 4465 | * This function is called from the direct IO end io call back |
| 4466 | * function, to convert the fallocated extents after IO is completed. |
| 4467 | * Returns 0 on success. |
| 4468 | */ |
| 4469 | int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, |
| 4470 | ssize_t len) |
| 4471 | { |
| 4472 | handle_t *handle; |
| 4473 | unsigned int max_blocks; |
| 4474 | int ret = 0; |
| 4475 | int ret2 = 0; |
| 4476 | struct ext4_map_blocks map; |
| 4477 | unsigned int credits, blkbits = inode->i_blkbits; |
| 4478 | |
| 4479 | map.m_lblk = offset >> blkbits; |
| 4480 | /* |
| 4481 | * We can't just convert len to max_blocks because |
| 4482 | * If blocksize = 4096 offset = 3072 and len = 2048 |
| 4483 | */ |
| 4484 | max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) - |
| 4485 | map.m_lblk); |
| 4486 | /* |
| 4487 | * credits to insert 1 extent into extent tree |
| 4488 | */ |
| 4489 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4490 | while (ret >= 0 && ret < max_blocks) { |
| 4491 | map.m_lblk += ret; |
| 4492 | map.m_len = (max_blocks -= ret); |
| 4493 | handle = ext4_journal_start(inode, credits); |
| 4494 | if (IS_ERR(handle)) { |
| 4495 | ret = PTR_ERR(handle); |
| 4496 | break; |
| 4497 | } |
| 4498 | ret = ext4_map_blocks(handle, inode, &map, |
| 4499 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
| 4500 | if (ret <= 0) { |
| 4501 | WARN_ON(ret <= 0); |
| 4502 | ext4_msg(inode->i_sb, KERN_ERR, |
| 4503 | "%s:%d: inode #%lu: block %u: len %u: " |
| 4504 | "ext4_ext_map_blocks returned %d", |
| 4505 | __func__, __LINE__, inode->i_ino, map.m_lblk, |
| 4506 | map.m_len, ret); |
| 4507 | } |
| 4508 | ext4_mark_inode_dirty(handle, inode); |
| 4509 | ret2 = ext4_journal_stop(handle); |
| 4510 | if (ret <= 0 || ret2 ) |
| 4511 | break; |
| 4512 | } |
| 4513 | return ret > 0 ? ret2 : ret; |
| 4514 | } |
| 4515 | |
| 4516 | /* |
| 4517 | * Callback function called for each extent to gather FIEMAP information. |
| 4518 | */ |
| 4519 | static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next, |
| 4520 | struct ext4_ext_cache *newex, struct ext4_extent *ex, |
| 4521 | void *data) |
| 4522 | { |
| 4523 | __u64 logical; |
| 4524 | __u64 physical; |
| 4525 | __u64 length; |
| 4526 | __u32 flags = 0; |
| 4527 | int ret = 0; |
| 4528 | struct fiemap_extent_info *fieinfo = data; |
| 4529 | unsigned char blksize_bits; |
| 4530 | |
| 4531 | blksize_bits = inode->i_sb->s_blocksize_bits; |
| 4532 | logical = (__u64)newex->ec_block << blksize_bits; |
| 4533 | |
| 4534 | if (newex->ec_start == 0) { |
| 4535 | /* |
| 4536 | * No extent in extent-tree contains block @newex->ec_start, |
| 4537 | * then the block may stay in 1)a hole or 2)delayed-extent. |
| 4538 | * |
| 4539 | * Holes or delayed-extents are processed as follows. |
| 4540 | * 1. lookup dirty pages with specified range in pagecache. |
| 4541 | * If no page is got, then there is no delayed-extent and |
| 4542 | * return with EXT_CONTINUE. |
| 4543 | * 2. find the 1st mapped buffer, |
| 4544 | * 3. check if the mapped buffer is both in the request range |
| 4545 | * and a delayed buffer. If not, there is no delayed-extent, |
| 4546 | * then return. |
| 4547 | * 4. a delayed-extent is found, the extent will be collected. |
| 4548 | */ |
| 4549 | ext4_lblk_t end = 0; |
| 4550 | pgoff_t last_offset; |
| 4551 | pgoff_t offset; |
| 4552 | pgoff_t index; |
| 4553 | pgoff_t start_index = 0; |
| 4554 | struct page **pages = NULL; |
| 4555 | struct buffer_head *bh = NULL; |
| 4556 | struct buffer_head *head = NULL; |
| 4557 | unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *); |
| 4558 | |
| 4559 | pages = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4560 | if (pages == NULL) |
| 4561 | return -ENOMEM; |
| 4562 | |
| 4563 | offset = logical >> PAGE_SHIFT; |
| 4564 | repeat: |
| 4565 | last_offset = offset; |
| 4566 | head = NULL; |
| 4567 | ret = find_get_pages_tag(inode->i_mapping, &offset, |
| 4568 | PAGECACHE_TAG_DIRTY, nr_pages, pages); |
| 4569 | |
| 4570 | if (!(flags & FIEMAP_EXTENT_DELALLOC)) { |
| 4571 | /* First time, try to find a mapped buffer. */ |
| 4572 | if (ret == 0) { |
| 4573 | out: |
| 4574 | for (index = 0; index < ret; index++) |
| 4575 | page_cache_release(pages[index]); |
| 4576 | /* just a hole. */ |
| 4577 | kfree(pages); |
| 4578 | return EXT_CONTINUE; |
| 4579 | } |
| 4580 | index = 0; |
| 4581 | |
| 4582 | next_page: |
| 4583 | /* Try to find the 1st mapped buffer. */ |
| 4584 | end = ((__u64)pages[index]->index << PAGE_SHIFT) >> |
| 4585 | blksize_bits; |
| 4586 | if (!page_has_buffers(pages[index])) |
| 4587 | goto out; |
| 4588 | head = page_buffers(pages[index]); |
| 4589 | if (!head) |
| 4590 | goto out; |
| 4591 | |
| 4592 | index++; |
| 4593 | bh = head; |
| 4594 | do { |
| 4595 | if (end >= newex->ec_block + |
| 4596 | newex->ec_len) |
| 4597 | /* The buffer is out of |
| 4598 | * the request range. |
| 4599 | */ |
| 4600 | goto out; |
| 4601 | |
| 4602 | if (buffer_mapped(bh) && |
| 4603 | end >= newex->ec_block) { |
| 4604 | start_index = index - 1; |
| 4605 | /* get the 1st mapped buffer. */ |
| 4606 | goto found_mapped_buffer; |
| 4607 | } |
| 4608 | |
| 4609 | bh = bh->b_this_page; |
| 4610 | end++; |
| 4611 | } while (bh != head); |
| 4612 | |
| 4613 | /* No mapped buffer in the range found in this page, |
| 4614 | * We need to look up next page. |
| 4615 | */ |
| 4616 | if (index >= ret) { |
| 4617 | /* There is no page left, but we need to limit |
| 4618 | * newex->ec_len. |
| 4619 | */ |
| 4620 | newex->ec_len = end - newex->ec_block; |
| 4621 | goto out; |
| 4622 | } |
| 4623 | goto next_page; |
| 4624 | } else { |
| 4625 | /*Find contiguous delayed buffers. */ |
| 4626 | if (ret > 0 && pages[0]->index == last_offset) |
| 4627 | head = page_buffers(pages[0]); |
| 4628 | bh = head; |
| 4629 | index = 1; |
| 4630 | start_index = 0; |
| 4631 | } |
| 4632 | |
| 4633 | found_mapped_buffer: |
| 4634 | if (bh != NULL && buffer_delay(bh)) { |
| 4635 | /* 1st or contiguous delayed buffer found. */ |
| 4636 | if (!(flags & FIEMAP_EXTENT_DELALLOC)) { |
| 4637 | /* |
| 4638 | * 1st delayed buffer found, record |
| 4639 | * the start of extent. |
| 4640 | */ |
| 4641 | flags |= FIEMAP_EXTENT_DELALLOC; |
| 4642 | newex->ec_block = end; |
| 4643 | logical = (__u64)end << blksize_bits; |
| 4644 | } |
| 4645 | /* Find contiguous delayed buffers. */ |
| 4646 | do { |
| 4647 | if (!buffer_delay(bh)) |
| 4648 | goto found_delayed_extent; |
| 4649 | bh = bh->b_this_page; |
| 4650 | end++; |
| 4651 | } while (bh != head); |
| 4652 | |
| 4653 | for (; index < ret; index++) { |
| 4654 | if (!page_has_buffers(pages[index])) { |
| 4655 | bh = NULL; |
| 4656 | break; |
| 4657 | } |
| 4658 | head = page_buffers(pages[index]); |
| 4659 | if (!head) { |
| 4660 | bh = NULL; |
| 4661 | break; |
| 4662 | } |
| 4663 | |
| 4664 | if (pages[index]->index != |
| 4665 | pages[start_index]->index + index |
| 4666 | - start_index) { |
| 4667 | /* Blocks are not contiguous. */ |
| 4668 | bh = NULL; |
| 4669 | break; |
| 4670 | } |
| 4671 | bh = head; |
| 4672 | do { |
| 4673 | if (!buffer_delay(bh)) |
| 4674 | /* Delayed-extent ends. */ |
| 4675 | goto found_delayed_extent; |
| 4676 | bh = bh->b_this_page; |
| 4677 | end++; |
| 4678 | } while (bh != head); |
| 4679 | } |
| 4680 | } else if (!(flags & FIEMAP_EXTENT_DELALLOC)) |
| 4681 | /* a hole found. */ |
| 4682 | goto out; |
| 4683 | |
| 4684 | found_delayed_extent: |
| 4685 | newex->ec_len = min(end - newex->ec_block, |
| 4686 | (ext4_lblk_t)EXT_INIT_MAX_LEN); |
| 4687 | if (ret == nr_pages && bh != NULL && |
| 4688 | newex->ec_len < EXT_INIT_MAX_LEN && |
| 4689 | buffer_delay(bh)) { |
| 4690 | /* Have not collected an extent and continue. */ |
| 4691 | for (index = 0; index < ret; index++) |
| 4692 | page_cache_release(pages[index]); |
| 4693 | goto repeat; |
| 4694 | } |
| 4695 | |
| 4696 | for (index = 0; index < ret; index++) |
| 4697 | page_cache_release(pages[index]); |
| 4698 | kfree(pages); |
| 4699 | } |
| 4700 | |
| 4701 | physical = (__u64)newex->ec_start << blksize_bits; |
| 4702 | length = (__u64)newex->ec_len << blksize_bits; |
| 4703 | |
| 4704 | if (ex && ext4_ext_is_uninitialized(ex)) |
| 4705 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 4706 | |
| 4707 | if (next == EXT_MAX_BLOCKS) |
| 4708 | flags |= FIEMAP_EXTENT_LAST; |
| 4709 | |
| 4710 | ret = fiemap_fill_next_extent(fieinfo, logical, physical, |
| 4711 | length, flags); |
| 4712 | if (ret < 0) |
| 4713 | return ret; |
| 4714 | if (ret == 1) |
| 4715 | return EXT_BREAK; |
| 4716 | return EXT_CONTINUE; |
| 4717 | } |
| 4718 | /* fiemap flags we can handle specified here */ |
| 4719 | #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
| 4720 | |
| 4721 | static int ext4_xattr_fiemap(struct inode *inode, |
| 4722 | struct fiemap_extent_info *fieinfo) |
| 4723 | { |
| 4724 | __u64 physical = 0; |
| 4725 | __u64 length; |
| 4726 | __u32 flags = FIEMAP_EXTENT_LAST; |
| 4727 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 4728 | int error = 0; |
| 4729 | |
| 4730 | /* in-inode? */ |
| 4731 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
| 4732 | struct ext4_iloc iloc; |
| 4733 | int offset; /* offset of xattr in inode */ |
| 4734 | |
| 4735 | error = ext4_get_inode_loc(inode, &iloc); |
| 4736 | if (error) |
| 4737 | return error; |
| 4738 | physical = (__u64)iloc.bh->b_blocknr << blockbits; |
| 4739 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 4740 | EXT4_I(inode)->i_extra_isize; |
| 4741 | physical += offset; |
| 4742 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
| 4743 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
| 4744 | brelse(iloc.bh); |
| 4745 | } else { /* external block */ |
| 4746 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
| 4747 | length = inode->i_sb->s_blocksize; |
| 4748 | } |
| 4749 | |
| 4750 | if (physical) |
| 4751 | error = fiemap_fill_next_extent(fieinfo, 0, physical, |
| 4752 | length, flags); |
| 4753 | return (error < 0 ? error : 0); |
| 4754 | } |
| 4755 | |
| 4756 | /* |
| 4757 | * ext4_ext_punch_hole |
| 4758 | * |
| 4759 | * Punches a hole of "length" bytes in a file starting |
| 4760 | * at byte "offset" |
| 4761 | * |
| 4762 | * @inode: The inode of the file to punch a hole in |
| 4763 | * @offset: The starting byte offset of the hole |
| 4764 | * @length: The length of the hole |
| 4765 | * |
| 4766 | * Returns the number of blocks removed or negative on err |
| 4767 | */ |
| 4768 | int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length) |
| 4769 | { |
| 4770 | struct inode *inode = file->f_path.dentry->d_inode; |
| 4771 | struct super_block *sb = inode->i_sb; |
| 4772 | ext4_lblk_t first_block, stop_block; |
| 4773 | struct address_space *mapping = inode->i_mapping; |
| 4774 | handle_t *handle; |
| 4775 | loff_t first_page, last_page, page_len; |
| 4776 | loff_t first_page_offset, last_page_offset; |
| 4777 | int credits, err = 0; |
| 4778 | |
| 4779 | /* No need to punch hole beyond i_size */ |
| 4780 | if (offset >= inode->i_size) |
| 4781 | return 0; |
| 4782 | |
| 4783 | /* |
| 4784 | * If the hole extends beyond i_size, set the hole |
| 4785 | * to end after the page that contains i_size |
| 4786 | */ |
| 4787 | if (offset + length > inode->i_size) { |
| 4788 | length = inode->i_size + |
| 4789 | PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - |
| 4790 | offset; |
| 4791 | } |
| 4792 | |
| 4793 | first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 4794 | last_page = (offset + length) >> PAGE_CACHE_SHIFT; |
| 4795 | |
| 4796 | first_page_offset = first_page << PAGE_CACHE_SHIFT; |
| 4797 | last_page_offset = last_page << PAGE_CACHE_SHIFT; |
| 4798 | |
| 4799 | /* |
| 4800 | * Write out all dirty pages to avoid race conditions |
| 4801 | * Then release them. |
| 4802 | */ |
| 4803 | if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { |
| 4804 | err = filemap_write_and_wait_range(mapping, |
| 4805 | offset, offset + length - 1); |
| 4806 | |
| 4807 | if (err) |
| 4808 | return err; |
| 4809 | } |
| 4810 | |
| 4811 | /* Now release the pages */ |
| 4812 | if (last_page_offset > first_page_offset) { |
| 4813 | truncate_inode_pages_range(mapping, first_page_offset, |
| 4814 | last_page_offset-1); |
| 4815 | } |
| 4816 | |
| 4817 | /* finish any pending end_io work */ |
| 4818 | ext4_flush_completed_IO(inode); |
| 4819 | |
| 4820 | credits = ext4_writepage_trans_blocks(inode); |
| 4821 | handle = ext4_journal_start(inode, credits); |
| 4822 | if (IS_ERR(handle)) |
| 4823 | return PTR_ERR(handle); |
| 4824 | |
| 4825 | err = ext4_orphan_add(handle, inode); |
| 4826 | if (err) |
| 4827 | goto out; |
| 4828 | |
| 4829 | /* |
| 4830 | * Now we need to zero out the non-page-aligned data in the |
| 4831 | * pages at the start and tail of the hole, and unmap the buffer |
| 4832 | * heads for the block aligned regions of the page that were |
| 4833 | * completely zeroed. |
| 4834 | */ |
| 4835 | if (first_page > last_page) { |
| 4836 | /* |
| 4837 | * If the file space being truncated is contained within a page |
| 4838 | * just zero out and unmap the middle of that page |
| 4839 | */ |
| 4840 | err = ext4_discard_partial_page_buffers(handle, |
| 4841 | mapping, offset, length, 0); |
| 4842 | |
| 4843 | if (err) |
| 4844 | goto out; |
| 4845 | } else { |
| 4846 | /* |
| 4847 | * zero out and unmap the partial page that contains |
| 4848 | * the start of the hole |
| 4849 | */ |
| 4850 | page_len = first_page_offset - offset; |
| 4851 | if (page_len > 0) { |
| 4852 | err = ext4_discard_partial_page_buffers(handle, mapping, |
| 4853 | offset, page_len, 0); |
| 4854 | if (err) |
| 4855 | goto out; |
| 4856 | } |
| 4857 | |
| 4858 | /* |
| 4859 | * zero out and unmap the partial page that contains |
| 4860 | * the end of the hole |
| 4861 | */ |
| 4862 | page_len = offset + length - last_page_offset; |
| 4863 | if (page_len > 0) { |
| 4864 | err = ext4_discard_partial_page_buffers(handle, mapping, |
| 4865 | last_page_offset, page_len, 0); |
| 4866 | if (err) |
| 4867 | goto out; |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | /* |
| 4872 | * If i_size is contained in the last page, we need to |
| 4873 | * unmap and zero the partial page after i_size |
| 4874 | */ |
| 4875 | if (inode->i_size >> PAGE_CACHE_SHIFT == last_page && |
| 4876 | inode->i_size % PAGE_CACHE_SIZE != 0) { |
| 4877 | |
| 4878 | page_len = PAGE_CACHE_SIZE - |
| 4879 | (inode->i_size & (PAGE_CACHE_SIZE - 1)); |
| 4880 | |
| 4881 | if (page_len > 0) { |
| 4882 | err = ext4_discard_partial_page_buffers(handle, |
| 4883 | mapping, inode->i_size, page_len, 0); |
| 4884 | |
| 4885 | if (err) |
| 4886 | goto out; |
| 4887 | } |
| 4888 | } |
| 4889 | |
| 4890 | first_block = (offset + sb->s_blocksize - 1) >> |
| 4891 | EXT4_BLOCK_SIZE_BITS(sb); |
| 4892 | stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 4893 | |
| 4894 | /* If there are no blocks to remove, return now */ |
| 4895 | if (first_block >= stop_block) |
| 4896 | goto out; |
| 4897 | |
| 4898 | down_write(&EXT4_I(inode)->i_data_sem); |
| 4899 | ext4_ext_invalidate_cache(inode); |
| 4900 | ext4_discard_preallocations(inode); |
| 4901 | |
| 4902 | err = ext4_ext_remove_space(inode, first_block, stop_block - 1); |
| 4903 | |
| 4904 | ext4_ext_invalidate_cache(inode); |
| 4905 | ext4_discard_preallocations(inode); |
| 4906 | |
| 4907 | if (IS_SYNC(inode)) |
| 4908 | ext4_handle_sync(handle); |
| 4909 | |
| 4910 | up_write(&EXT4_I(inode)->i_data_sem); |
| 4911 | |
| 4912 | out: |
| 4913 | ext4_orphan_del(handle, inode); |
| 4914 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 4915 | ext4_mark_inode_dirty(handle, inode); |
| 4916 | ext4_journal_stop(handle); |
| 4917 | return err; |
| 4918 | } |
| 4919 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4920 | __u64 start, __u64 len) |
| 4921 | { |
| 4922 | ext4_lblk_t start_blk; |
| 4923 | int error = 0; |
| 4924 | |
| 4925 | /* fallback to generic here if not in extents fmt */ |
| 4926 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
| 4927 | return generic_block_fiemap(inode, fieinfo, start, len, |
| 4928 | ext4_get_block); |
| 4929 | |
| 4930 | if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) |
| 4931 | return -EBADR; |
| 4932 | |
| 4933 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 4934 | error = ext4_xattr_fiemap(inode, fieinfo); |
| 4935 | } else { |
| 4936 | ext4_lblk_t len_blks; |
| 4937 | __u64 last_blk; |
| 4938 | |
| 4939 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
| 4940 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
| 4941 | if (last_blk >= EXT_MAX_BLOCKS) |
| 4942 | last_blk = EXT_MAX_BLOCKS-1; |
| 4943 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
| 4944 | |
| 4945 | /* |
| 4946 | * Walk the extent tree gathering extent information. |
| 4947 | * ext4_ext_fiemap_cb will push extents back to user. |
| 4948 | */ |
| 4949 | error = ext4_ext_walk_space(inode, start_blk, len_blks, |
| 4950 | ext4_ext_fiemap_cb, fieinfo); |
| 4951 | } |
| 4952 | |
| 4953 | return error; |
| 4954 | } |