.. | .. |
---|
28 | 28 | #include <linux/uaccess.h> |
---|
29 | 29 | #include <linux/fiemap.h> |
---|
30 | 30 | #include <linux/backing-dev.h> |
---|
| 31 | +#include <linux/iomap.h> |
---|
31 | 32 | #include "ext4_jbd2.h" |
---|
32 | 33 | #include "ext4_extents.h" |
---|
33 | 34 | #include "xattr.h" |
---|
.. | .. |
---|
83 | 84 | et->et_checksum = ext4_extent_block_csum(inode, eh); |
---|
84 | 85 | } |
---|
85 | 86 | |
---|
86 | | -static int ext4_split_extent(handle_t *handle, |
---|
87 | | - struct inode *inode, |
---|
88 | | - struct ext4_ext_path **ppath, |
---|
89 | | - struct ext4_map_blocks *map, |
---|
90 | | - int split_flag, |
---|
91 | | - int flags); |
---|
92 | | - |
---|
93 | 87 | static int ext4_split_extent_at(handle_t *handle, |
---|
94 | 88 | struct inode *inode, |
---|
95 | 89 | struct ext4_ext_path **ppath, |
---|
.. | .. |
---|
97 | 91 | int split_flag, |
---|
98 | 92 | int flags); |
---|
99 | 93 | |
---|
100 | | -static int ext4_find_delayed_extent(struct inode *inode, |
---|
101 | | - struct extent_status *newes); |
---|
102 | | - |
---|
103 | | -static int ext4_ext_truncate_extend_restart(handle_t *handle, |
---|
104 | | - struct inode *inode, |
---|
105 | | - int needed) |
---|
| 94 | +static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) |
---|
106 | 95 | { |
---|
107 | | - int err; |
---|
108 | | - |
---|
109 | | - if (!ext4_handle_valid(handle)) |
---|
110 | | - return 0; |
---|
111 | | - if (handle->h_buffer_credits >= needed) |
---|
112 | | - return 0; |
---|
113 | 96 | /* |
---|
114 | | - * If we need to extend the journal get a few extra blocks |
---|
115 | | - * while we're at it for efficiency's sake. |
---|
| 97 | + * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
---|
| 98 | + * moment, get_block can be called only for blocks inside i_size since |
---|
| 99 | + * page cache has been already dropped and writes are blocked by |
---|
| 100 | + * i_mutex. So we can safely drop the i_data_sem here. |
---|
116 | 101 | */ |
---|
117 | | - needed += 3; |
---|
118 | | - err = ext4_journal_extend(handle, needed - handle->h_buffer_credits); |
---|
119 | | - if (err <= 0) |
---|
120 | | - return err; |
---|
121 | | - err = ext4_truncate_restart_trans(handle, inode, needed); |
---|
122 | | - if (err == 0) |
---|
123 | | - err = -EAGAIN; |
---|
| 102 | + BUG_ON(EXT4_JOURNAL(inode) == NULL); |
---|
| 103 | + ext4_discard_preallocations(inode, 0); |
---|
| 104 | + up_write(&EXT4_I(inode)->i_data_sem); |
---|
| 105 | + *dropped = 1; |
---|
| 106 | + return 0; |
---|
| 107 | +} |
---|
124 | 108 | |
---|
125 | | - return err; |
---|
| 109 | +/* |
---|
| 110 | + * Make sure 'handle' has at least 'check_cred' credits. If not, restart |
---|
| 111 | + * transaction with 'restart_cred' credits. The function drops i_data_sem |
---|
| 112 | + * when restarting transaction and gets it after transaction is restarted. |
---|
| 113 | + * |
---|
| 114 | + * The function returns 0 on success, 1 if transaction had to be restarted, |
---|
| 115 | + * and < 0 in case of fatal error. |
---|
| 116 | + */ |
---|
| 117 | +int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode, |
---|
| 118 | + int check_cred, int restart_cred, |
---|
| 119 | + int revoke_cred) |
---|
| 120 | +{ |
---|
| 121 | + int ret; |
---|
| 122 | + int dropped = 0; |
---|
| 123 | + |
---|
| 124 | + ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred, |
---|
| 125 | + revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped)); |
---|
| 126 | + if (dropped) |
---|
| 127 | + down_write(&EXT4_I(inode)->i_data_sem); |
---|
| 128 | + return ret; |
---|
126 | 129 | } |
---|
127 | 130 | |
---|
128 | 131 | /* |
---|
.. | .. |
---|
133 | 136 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
---|
134 | 137 | struct ext4_ext_path *path) |
---|
135 | 138 | { |
---|
| 139 | + int err = 0; |
---|
| 140 | + |
---|
136 | 141 | if (path->p_bh) { |
---|
137 | 142 | /* path points to block */ |
---|
138 | 143 | BUFFER_TRACE(path->p_bh, "get_write_access"); |
---|
139 | | - return ext4_journal_get_write_access(handle, path->p_bh); |
---|
| 144 | + err = ext4_journal_get_write_access(handle, path->p_bh); |
---|
| 145 | + /* |
---|
| 146 | + * The extent buffer's verified bit will be set again in |
---|
| 147 | + * __ext4_ext_dirty(). We could leave an inconsistent |
---|
| 148 | + * buffer if the extents updating procudure break off du |
---|
| 149 | + * to some error happens, force to check it again. |
---|
| 150 | + */ |
---|
| 151 | + if (!err) |
---|
| 152 | + clear_buffer_verified(path->p_bh); |
---|
140 | 153 | } |
---|
141 | 154 | /* path points to leaf/index in inode body */ |
---|
142 | 155 | /* we use in-core data, no need to protect them */ |
---|
143 | | - return 0; |
---|
| 156 | + return err; |
---|
144 | 157 | } |
---|
145 | 158 | |
---|
146 | 159 | /* |
---|
.. | .. |
---|
149 | 162 | * - ENOMEM |
---|
150 | 163 | * - EIO |
---|
151 | 164 | */ |
---|
152 | | -int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle, |
---|
153 | | - struct inode *inode, struct ext4_ext_path *path) |
---|
| 165 | +static int __ext4_ext_dirty(const char *where, unsigned int line, |
---|
| 166 | + handle_t *handle, struct inode *inode, |
---|
| 167 | + struct ext4_ext_path *path) |
---|
154 | 168 | { |
---|
155 | 169 | int err; |
---|
156 | 170 | |
---|
.. | .. |
---|
160 | 174 | /* path points to block */ |
---|
161 | 175 | err = __ext4_handle_dirty_metadata(where, line, handle, |
---|
162 | 176 | inode, path->p_bh); |
---|
| 177 | + /* Extents updating done, re-set verified flag */ |
---|
| 178 | + if (!err) |
---|
| 179 | + set_buffer_verified(path->p_bh); |
---|
163 | 180 | } else { |
---|
164 | 181 | /* path points to leaf/index in inode body */ |
---|
165 | 182 | err = ext4_mark_inode_dirty(handle, inode); |
---|
166 | 183 | } |
---|
167 | 184 | return err; |
---|
168 | 185 | } |
---|
| 186 | + |
---|
| 187 | +#define ext4_ext_dirty(handle, inode, path) \ |
---|
| 188 | + __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) |
---|
169 | 189 | |
---|
170 | 190 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
---|
171 | 191 | struct ext4_ext_path *path, |
---|
.. | .. |
---|
290 | 310 | { |
---|
291 | 311 | struct ext4_ext_path *path = *ppath; |
---|
292 | 312 | int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); |
---|
| 313 | + int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO; |
---|
| 314 | + |
---|
| 315 | + if (nofail) |
---|
| 316 | + flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL; |
---|
293 | 317 | |
---|
294 | 318 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? |
---|
295 | 319 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, |
---|
296 | | - EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | |
---|
297 | | - (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0)); |
---|
298 | | -} |
---|
299 | | - |
---|
300 | | -/* |
---|
301 | | - * Calculate the number of metadata blocks needed |
---|
302 | | - * to allocate @blocks |
---|
303 | | - * Worse case is one block per extent |
---|
304 | | - */ |
---|
305 | | -int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) |
---|
306 | | -{ |
---|
307 | | - struct ext4_inode_info *ei = EXT4_I(inode); |
---|
308 | | - int idxs; |
---|
309 | | - |
---|
310 | | - idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
---|
311 | | - / sizeof(struct ext4_extent_idx)); |
---|
312 | | - |
---|
313 | | - /* |
---|
314 | | - * If the new delayed allocation block is contiguous with the |
---|
315 | | - * previous da block, it can share index blocks with the |
---|
316 | | - * previous block, so we only need to allocate a new index |
---|
317 | | - * block every idxs leaf blocks. At ldxs**2 blocks, we need |
---|
318 | | - * an additional index block, and at ldxs**3 blocks, yet |
---|
319 | | - * another index blocks. |
---|
320 | | - */ |
---|
321 | | - if (ei->i_da_metadata_calc_len && |
---|
322 | | - ei->i_da_metadata_calc_last_lblock+1 == lblock) { |
---|
323 | | - int num = 0; |
---|
324 | | - |
---|
325 | | - if ((ei->i_da_metadata_calc_len % idxs) == 0) |
---|
326 | | - num++; |
---|
327 | | - if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) |
---|
328 | | - num++; |
---|
329 | | - if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { |
---|
330 | | - num++; |
---|
331 | | - ei->i_da_metadata_calc_len = 0; |
---|
332 | | - } else |
---|
333 | | - ei->i_da_metadata_calc_len++; |
---|
334 | | - ei->i_da_metadata_calc_last_lblock++; |
---|
335 | | - return num; |
---|
336 | | - } |
---|
337 | | - |
---|
338 | | - /* |
---|
339 | | - * In the worst case we need a new set of index blocks at |
---|
340 | | - * every level of the inode's extent tree. |
---|
341 | | - */ |
---|
342 | | - ei->i_da_metadata_calc_len = 1; |
---|
343 | | - ei->i_da_metadata_calc_last_lblock = lblock; |
---|
344 | | - return ext_depth(inode) + 1; |
---|
| 320 | + flags); |
---|
345 | 321 | } |
---|
346 | 322 | |
---|
347 | 323 | static int |
---|
.. | .. |
---|
389 | 365 | } |
---|
390 | 366 | |
---|
391 | 367 | static int ext4_valid_extent_entries(struct inode *inode, |
---|
392 | | - struct ext4_extent_header *eh, |
---|
393 | | - ext4_fsblk_t *pblk, int depth) |
---|
| 368 | + struct ext4_extent_header *eh, |
---|
| 369 | + ext4_lblk_t lblk, ext4_fsblk_t *pblk, |
---|
| 370 | + int depth) |
---|
394 | 371 | { |
---|
395 | 372 | unsigned short entries; |
---|
396 | 373 | ext4_lblk_t lblock = 0; |
---|
397 | | - ext4_lblk_t prev = 0; |
---|
| 374 | + ext4_lblk_t cur = 0; |
---|
398 | 375 | |
---|
399 | 376 | if (eh->eh_entries == 0) |
---|
400 | 377 | return 1; |
---|
.. | .. |
---|
404 | 381 | if (depth == 0) { |
---|
405 | 382 | /* leaf entries */ |
---|
406 | 383 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
---|
407 | | - struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
---|
408 | | - ext4_fsblk_t pblock = 0; |
---|
| 384 | + |
---|
| 385 | + /* |
---|
| 386 | + * The logical block in the first entry should equal to |
---|
| 387 | + * the number in the index block. |
---|
| 388 | + */ |
---|
| 389 | + if (depth != ext_depth(inode) && |
---|
| 390 | + lblk != le32_to_cpu(ext->ee_block)) |
---|
| 391 | + return 0; |
---|
409 | 392 | while (entries) { |
---|
410 | 393 | if (!ext4_valid_extent(inode, ext)) |
---|
411 | 394 | return 0; |
---|
412 | 395 | |
---|
413 | 396 | /* Check for overlapping extents */ |
---|
414 | 397 | lblock = le32_to_cpu(ext->ee_block); |
---|
415 | | - if ((lblock <= prev) && prev) { |
---|
416 | | - pblock = ext4_ext_pblock(ext); |
---|
417 | | - es->s_last_error_block = cpu_to_le64(pblock); |
---|
| 398 | + if (lblock < cur) { |
---|
| 399 | + *pblk = ext4_ext_pblock(ext); |
---|
418 | 400 | return 0; |
---|
419 | 401 | } |
---|
420 | | - prev = lblock + ext4_ext_get_actual_len(ext) - 1; |
---|
| 402 | + cur = lblock + ext4_ext_get_actual_len(ext); |
---|
421 | 403 | ext++; |
---|
422 | 404 | entries--; |
---|
423 | 405 | } |
---|
424 | 406 | } else { |
---|
425 | 407 | struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); |
---|
| 408 | + |
---|
| 409 | + /* |
---|
| 410 | + * The logical block in the first entry should equal to |
---|
| 411 | + * the number in the parent index block. |
---|
| 412 | + */ |
---|
| 413 | + if (depth != ext_depth(inode) && |
---|
| 414 | + lblk != le32_to_cpu(ext_idx->ei_block)) |
---|
| 415 | + return 0; |
---|
426 | 416 | while (entries) { |
---|
427 | 417 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
---|
428 | 418 | return 0; |
---|
429 | 419 | |
---|
430 | 420 | /* Check for overlapping index extents */ |
---|
431 | 421 | lblock = le32_to_cpu(ext_idx->ei_block); |
---|
432 | | - if ((lblock <= prev) && prev) { |
---|
| 422 | + if (lblock < cur) { |
---|
433 | 423 | *pblk = ext4_idx_pblock(ext_idx); |
---|
434 | 424 | return 0; |
---|
435 | 425 | } |
---|
436 | 426 | ext_idx++; |
---|
437 | 427 | entries--; |
---|
438 | | - prev = lblock; |
---|
| 428 | + cur = lblock + 1; |
---|
439 | 429 | } |
---|
440 | 430 | } |
---|
441 | 431 | return 1; |
---|
.. | .. |
---|
443 | 433 | |
---|
444 | 434 | static int __ext4_ext_check(const char *function, unsigned int line, |
---|
445 | 435 | struct inode *inode, struct ext4_extent_header *eh, |
---|
446 | | - int depth, ext4_fsblk_t pblk) |
---|
| 436 | + int depth, ext4_fsblk_t pblk, ext4_lblk_t lblk) |
---|
447 | 437 | { |
---|
448 | 438 | const char *error_msg; |
---|
449 | 439 | int max = 0, err = -EFSCORRUPTED; |
---|
.. | .. |
---|
469 | 459 | error_msg = "invalid eh_entries"; |
---|
470 | 460 | goto corrupted; |
---|
471 | 461 | } |
---|
472 | | - if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) { |
---|
| 462 | + if (unlikely((eh->eh_entries == 0) && (depth > 0))) { |
---|
| 463 | + error_msg = "eh_entries is 0 but eh_depth is > 0"; |
---|
| 464 | + goto corrupted; |
---|
| 465 | + } |
---|
| 466 | + if (!ext4_valid_extent_entries(inode, eh, lblk, &pblk, depth)) { |
---|
473 | 467 | error_msg = "invalid extent entries"; |
---|
474 | 468 | goto corrupted; |
---|
475 | 469 | } |
---|
.. | .. |
---|
487 | 481 | return 0; |
---|
488 | 482 | |
---|
489 | 483 | corrupted: |
---|
490 | | - ext4_error_inode(inode, function, line, 0, |
---|
491 | | - "pblk %llu bad header/extent: %s - magic %x, " |
---|
492 | | - "entries %u, max %u(%u), depth %u(%u)", |
---|
493 | | - (unsigned long long) pblk, error_msg, |
---|
494 | | - le16_to_cpu(eh->eh_magic), |
---|
495 | | - le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
---|
496 | | - max, le16_to_cpu(eh->eh_depth), depth); |
---|
| 484 | + ext4_error_inode_err(inode, function, line, 0, -err, |
---|
| 485 | + "pblk %llu bad header/extent: %s - magic %x, " |
---|
| 486 | + "entries %u, max %u(%u), depth %u(%u)", |
---|
| 487 | + (unsigned long long) pblk, error_msg, |
---|
| 488 | + le16_to_cpu(eh->eh_magic), |
---|
| 489 | + le16_to_cpu(eh->eh_entries), |
---|
| 490 | + le16_to_cpu(eh->eh_max), |
---|
| 491 | + max, le16_to_cpu(eh->eh_depth), depth); |
---|
497 | 492 | return err; |
---|
498 | 493 | } |
---|
499 | 494 | |
---|
500 | 495 | #define ext4_ext_check(inode, eh, depth, pblk) \ |
---|
501 | | - __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk)) |
---|
| 496 | + __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk), 0) |
---|
502 | 497 | |
---|
503 | 498 | int ext4_ext_check_inode(struct inode *inode) |
---|
504 | 499 | { |
---|
.. | .. |
---|
531 | 526 | |
---|
532 | 527 | static struct buffer_head * |
---|
533 | 528 | __read_extent_tree_block(const char *function, unsigned int line, |
---|
534 | | - struct inode *inode, ext4_fsblk_t pblk, int depth, |
---|
535 | | - int flags) |
---|
| 529 | + struct inode *inode, struct ext4_extent_idx *idx, |
---|
| 530 | + int depth, int flags) |
---|
536 | 531 | { |
---|
537 | 532 | struct buffer_head *bh; |
---|
538 | 533 | int err; |
---|
| 534 | + gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS; |
---|
| 535 | + ext4_fsblk_t pblk; |
---|
539 | 536 | |
---|
540 | | - bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS); |
---|
| 537 | + if (flags & EXT4_EX_NOFAIL) |
---|
| 538 | + gfp_flags |= __GFP_NOFAIL; |
---|
| 539 | + |
---|
| 540 | + pblk = ext4_idx_pblock(idx); |
---|
| 541 | + bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags); |
---|
541 | 542 | if (unlikely(!bh)) |
---|
542 | 543 | return ERR_PTR(-ENOMEM); |
---|
543 | 544 | |
---|
544 | 545 | if (!bh_uptodate_or_lock(bh)) { |
---|
545 | 546 | trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); |
---|
546 | | - err = bh_submit_read(bh); |
---|
| 547 | + err = ext4_read_bh(bh, 0, NULL); |
---|
547 | 548 | if (err < 0) |
---|
548 | 549 | goto errout; |
---|
549 | 550 | } |
---|
550 | 551 | if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) |
---|
551 | 552 | return bh; |
---|
552 | | - err = __ext4_ext_check(function, line, inode, |
---|
553 | | - ext_block_hdr(bh), depth, pblk); |
---|
| 553 | + err = __ext4_ext_check(function, line, inode, ext_block_hdr(bh), |
---|
| 554 | + depth, pblk, le32_to_cpu(idx->ei_block)); |
---|
554 | 555 | if (err) |
---|
555 | 556 | goto errout; |
---|
556 | 557 | set_buffer_verified(bh); |
---|
.. | .. |
---|
568 | 569 | |
---|
569 | 570 | } |
---|
570 | 571 | |
---|
571 | | -#define read_extent_tree_block(inode, pblk, depth, flags) \ |
---|
572 | | - __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ |
---|
| 572 | +#define read_extent_tree_block(inode, idx, depth, flags) \ |
---|
| 573 | + __read_extent_tree_block(__func__, __LINE__, (inode), (idx), \ |
---|
573 | 574 | (depth), (flags)) |
---|
574 | 575 | |
---|
575 | 576 | /* |
---|
.. | .. |
---|
589 | 590 | down_read(&ei->i_data_sem); |
---|
590 | 591 | depth = ext_depth(inode); |
---|
591 | 592 | |
---|
| 593 | + /* Don't cache anything if there are no external extent blocks */ |
---|
| 594 | + if (!depth) { |
---|
| 595 | + up_read(&ei->i_data_sem); |
---|
| 596 | + return ret; |
---|
| 597 | + } |
---|
| 598 | + |
---|
592 | 599 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
---|
593 | 600 | GFP_NOFS); |
---|
594 | 601 | if (path == NULL) { |
---|
.. | .. |
---|
596 | 603 | return -ENOMEM; |
---|
597 | 604 | } |
---|
598 | 605 | |
---|
599 | | - /* Don't cache anything if there are no external extent blocks */ |
---|
600 | | - if (depth == 0) |
---|
601 | | - goto out; |
---|
602 | 606 | path[0].p_hdr = ext_inode_hdr(inode); |
---|
603 | 607 | ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); |
---|
604 | 608 | if (ret) |
---|
.. | .. |
---|
616 | 620 | i--; |
---|
617 | 621 | continue; |
---|
618 | 622 | } |
---|
619 | | - bh = read_extent_tree_block(inode, |
---|
620 | | - ext4_idx_pblock(path[i].p_idx++), |
---|
| 623 | + bh = read_extent_tree_block(inode, path[i].p_idx++, |
---|
621 | 624 | depth - i - 1, |
---|
622 | 625 | EXT4_EX_FORCE_CACHE); |
---|
623 | 626 | if (IS_ERR(bh)) { |
---|
.. | .. |
---|
642 | 645 | { |
---|
643 | 646 | int k, l = path->p_depth; |
---|
644 | 647 | |
---|
645 | | - ext_debug("path:"); |
---|
| 648 | + ext_debug(inode, "path:"); |
---|
646 | 649 | for (k = 0; k <= l; k++, path++) { |
---|
647 | 650 | if (path->p_idx) { |
---|
648 | | - ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
---|
649 | | - ext4_idx_pblock(path->p_idx)); |
---|
| 651 | + ext_debug(inode, " %d->%llu", |
---|
| 652 | + le32_to_cpu(path->p_idx->ei_block), |
---|
| 653 | + ext4_idx_pblock(path->p_idx)); |
---|
650 | 654 | } else if (path->p_ext) { |
---|
651 | | - ext_debug(" %d:[%d]%d:%llu ", |
---|
| 655 | + ext_debug(inode, " %d:[%d]%d:%llu ", |
---|
652 | 656 | le32_to_cpu(path->p_ext->ee_block), |
---|
653 | 657 | ext4_ext_is_unwritten(path->p_ext), |
---|
654 | 658 | ext4_ext_get_actual_len(path->p_ext), |
---|
655 | 659 | ext4_ext_pblock(path->p_ext)); |
---|
656 | 660 | } else |
---|
657 | | - ext_debug(" []"); |
---|
| 661 | + ext_debug(inode, " []"); |
---|
658 | 662 | } |
---|
659 | | - ext_debug("\n"); |
---|
| 663 | + ext_debug(inode, "\n"); |
---|
660 | 664 | } |
---|
661 | 665 | |
---|
662 | 666 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
---|
.. | .. |
---|
672 | 676 | eh = path[depth].p_hdr; |
---|
673 | 677 | ex = EXT_FIRST_EXTENT(eh); |
---|
674 | 678 | |
---|
675 | | - ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
---|
| 679 | + ext_debug(inode, "Displaying leaf extents\n"); |
---|
676 | 680 | |
---|
677 | 681 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
---|
678 | | - ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
---|
| 682 | + ext_debug(inode, "%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
---|
679 | 683 | ext4_ext_is_unwritten(ex), |
---|
680 | 684 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
---|
681 | 685 | } |
---|
682 | | - ext_debug("\n"); |
---|
| 686 | + ext_debug(inode, "\n"); |
---|
683 | 687 | } |
---|
684 | 688 | |
---|
685 | 689 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
---|
.. | .. |
---|
692 | 696 | struct ext4_extent_idx *idx; |
---|
693 | 697 | idx = path[level].p_idx; |
---|
694 | 698 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
---|
695 | | - ext_debug("%d: move %d:%llu in new index %llu\n", level, |
---|
696 | | - le32_to_cpu(idx->ei_block), |
---|
697 | | - ext4_idx_pblock(idx), |
---|
698 | | - newblock); |
---|
| 699 | + ext_debug(inode, "%d: move %d:%llu in new index %llu\n", |
---|
| 700 | + level, le32_to_cpu(idx->ei_block), |
---|
| 701 | + ext4_idx_pblock(idx), newblock); |
---|
699 | 702 | idx++; |
---|
700 | 703 | } |
---|
701 | 704 | |
---|
.. | .. |
---|
704 | 707 | |
---|
705 | 708 | ex = path[depth].p_ext; |
---|
706 | 709 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
---|
707 | | - ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
---|
| 710 | + ext_debug(inode, "move %d:%llu:[%d]%d in new leaf %llu\n", |
---|
708 | 711 | le32_to_cpu(ex->ee_block), |
---|
709 | 712 | ext4_ext_pblock(ex), |
---|
710 | 713 | ext4_ext_is_unwritten(ex), |
---|
.. | .. |
---|
727 | 730 | if (!path) |
---|
728 | 731 | return; |
---|
729 | 732 | depth = path->p_depth; |
---|
730 | | - for (i = 0; i <= depth; i++, path++) |
---|
731 | | - if (path->p_bh) { |
---|
732 | | - brelse(path->p_bh); |
---|
733 | | - path->p_bh = NULL; |
---|
734 | | - } |
---|
| 733 | + for (i = 0; i <= depth; i++, path++) { |
---|
| 734 | + brelse(path->p_bh); |
---|
| 735 | + path->p_bh = NULL; |
---|
| 736 | + } |
---|
735 | 737 | } |
---|
736 | 738 | |
---|
737 | 739 | /* |
---|
.. | .. |
---|
747 | 749 | struct ext4_extent_idx *r, *l, *m; |
---|
748 | 750 | |
---|
749 | 751 | |
---|
750 | | - ext_debug("binsearch for %u(idx): ", block); |
---|
| 752 | + ext_debug(inode, "binsearch for %u(idx): ", block); |
---|
751 | 753 | |
---|
752 | 754 | l = EXT_FIRST_INDEX(eh) + 1; |
---|
753 | 755 | r = EXT_LAST_INDEX(eh); |
---|
.. | .. |
---|
757 | 759 | r = m - 1; |
---|
758 | 760 | else |
---|
759 | 761 | l = m + 1; |
---|
760 | | - ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
---|
761 | | - m, le32_to_cpu(m->ei_block), |
---|
762 | | - r, le32_to_cpu(r->ei_block)); |
---|
| 762 | + ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, |
---|
| 763 | + le32_to_cpu(l->ei_block), m, le32_to_cpu(m->ei_block), |
---|
| 764 | + r, le32_to_cpu(r->ei_block)); |
---|
763 | 765 | } |
---|
764 | 766 | |
---|
765 | 767 | path->p_idx = l - 1; |
---|
766 | | - ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), |
---|
| 768 | + ext_debug(inode, " -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), |
---|
767 | 769 | ext4_idx_pblock(path->p_idx)); |
---|
768 | 770 | |
---|
769 | 771 | #ifdef CHECK_BINSEARCH |
---|
.. | .. |
---|
773 | 775 | |
---|
774 | 776 | chix = ix = EXT_FIRST_INDEX(eh); |
---|
775 | 777 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
---|
776 | | - if (k != 0 && |
---|
777 | | - le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { |
---|
| 778 | + if (k != 0 && le32_to_cpu(ix->ei_block) <= |
---|
| 779 | + le32_to_cpu(ix[-1].ei_block)) { |
---|
778 | 780 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
---|
779 | 781 | "first=0x%p\n", k, |
---|
780 | 782 | ix, EXT_FIRST_INDEX(eh)); |
---|
.. | .. |
---|
814 | 816 | return; |
---|
815 | 817 | } |
---|
816 | 818 | |
---|
817 | | - ext_debug("binsearch for %u: ", block); |
---|
| 819 | + ext_debug(inode, "binsearch for %u: ", block); |
---|
818 | 820 | |
---|
819 | 821 | l = EXT_FIRST_EXTENT(eh) + 1; |
---|
820 | 822 | r = EXT_LAST_EXTENT(eh); |
---|
.. | .. |
---|
825 | 827 | r = m - 1; |
---|
826 | 828 | else |
---|
827 | 829 | l = m + 1; |
---|
828 | | - ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
---|
829 | | - m, le32_to_cpu(m->ee_block), |
---|
830 | | - r, le32_to_cpu(r->ee_block)); |
---|
| 830 | + ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, |
---|
| 831 | + le32_to_cpu(l->ee_block), m, le32_to_cpu(m->ee_block), |
---|
| 832 | + r, le32_to_cpu(r->ee_block)); |
---|
831 | 833 | } |
---|
832 | 834 | |
---|
833 | 835 | path->p_ext = l - 1; |
---|
834 | | - ext_debug(" -> %d:%llu:[%d]%d ", |
---|
| 836 | + ext_debug(inode, " -> %d:%llu:[%d]%d ", |
---|
835 | 837 | le32_to_cpu(path->p_ext->ee_block), |
---|
836 | 838 | ext4_ext_pblock(path->p_ext), |
---|
837 | 839 | ext4_ext_is_unwritten(path->p_ext), |
---|
.. | .. |
---|
856 | 858 | |
---|
857 | 859 | } |
---|
858 | 860 | |
---|
859 | | -int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
---|
| 861 | +void ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
---|
860 | 862 | { |
---|
861 | 863 | struct ext4_extent_header *eh; |
---|
862 | 864 | |
---|
.. | .. |
---|
867 | 869 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
---|
868 | 870 | eh->eh_generation = 0; |
---|
869 | 871 | ext4_mark_inode_dirty(handle, inode); |
---|
870 | | - return 0; |
---|
871 | 872 | } |
---|
872 | 873 | |
---|
873 | 874 | struct ext4_ext_path * |
---|
.. | .. |
---|
879 | 880 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
---|
880 | 881 | short int depth, i, ppos = 0; |
---|
881 | 882 | int ret; |
---|
| 883 | + gfp_t gfp_flags = GFP_NOFS; |
---|
| 884 | + |
---|
| 885 | + if (flags & EXT4_EX_NOFAIL) |
---|
| 886 | + gfp_flags |= __GFP_NOFAIL; |
---|
882 | 887 | |
---|
883 | 888 | eh = ext_inode_hdr(inode); |
---|
884 | 889 | depth = ext_depth(inode); |
---|
.. | .. |
---|
899 | 904 | if (!path) { |
---|
900 | 905 | /* account possible depth increase */ |
---|
901 | 906 | path = kcalloc(depth + 2, sizeof(struct ext4_ext_path), |
---|
902 | | - GFP_NOFS); |
---|
| 907 | + gfp_flags); |
---|
903 | 908 | if (unlikely(!path)) |
---|
904 | 909 | return ERR_PTR(-ENOMEM); |
---|
905 | 910 | path[0].p_maxdepth = depth + 1; |
---|
.. | .. |
---|
912 | 917 | ext4_cache_extents(inode, eh); |
---|
913 | 918 | /* walk through the tree */ |
---|
914 | 919 | while (i) { |
---|
915 | | - ext_debug("depth %d: num %d, max %d\n", |
---|
| 920 | + ext_debug(inode, "depth %d: num %d, max %d\n", |
---|
916 | 921 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
---|
917 | 922 | |
---|
918 | 923 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
---|
.. | .. |
---|
920 | 925 | path[ppos].p_depth = i; |
---|
921 | 926 | path[ppos].p_ext = NULL; |
---|
922 | 927 | |
---|
923 | | - bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
---|
924 | | - flags); |
---|
| 928 | + bh = read_extent_tree_block(inode, path[ppos].p_idx, --i, flags); |
---|
925 | 929 | if (IS_ERR(bh)) { |
---|
926 | 930 | ret = PTR_ERR(bh); |
---|
927 | 931 | goto err; |
---|
.. | .. |
---|
989 | 993 | |
---|
990 | 994 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
---|
991 | 995 | /* insert after */ |
---|
992 | | - ext_debug("insert new index %d after: %llu\n", logical, ptr); |
---|
| 996 | + ext_debug(inode, "insert new index %d after: %llu\n", |
---|
| 997 | + logical, ptr); |
---|
993 | 998 | ix = curp->p_idx + 1; |
---|
994 | 999 | } else { |
---|
995 | 1000 | /* insert before */ |
---|
996 | | - ext_debug("insert new index %d before: %llu\n", logical, ptr); |
---|
| 1001 | + ext_debug(inode, "insert new index %d before: %llu\n", |
---|
| 1002 | + logical, ptr); |
---|
997 | 1003 | ix = curp->p_idx; |
---|
998 | 1004 | } |
---|
999 | 1005 | |
---|
1000 | 1006 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
---|
1001 | 1007 | BUG_ON(len < 0); |
---|
1002 | 1008 | if (len > 0) { |
---|
1003 | | - ext_debug("insert new index %d: " |
---|
| 1009 | + ext_debug(inode, "insert new index %d: " |
---|
1004 | 1010 | "move %d indices from 0x%p to 0x%p\n", |
---|
1005 | 1011 | logical, len, ix, ix + 1); |
---|
1006 | 1012 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
---|
.. | .. |
---|
1049 | 1055 | ext4_fsblk_t newblock, oldblock; |
---|
1050 | 1056 | __le32 border; |
---|
1051 | 1057 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
---|
| 1058 | + gfp_t gfp_flags = GFP_NOFS; |
---|
1052 | 1059 | int err = 0; |
---|
1053 | 1060 | size_t ext_size = 0; |
---|
| 1061 | + |
---|
| 1062 | + if (flags & EXT4_EX_NOFAIL) |
---|
| 1063 | + gfp_flags |= __GFP_NOFAIL; |
---|
1054 | 1064 | |
---|
1055 | 1065 | /* make decision: where to split? */ |
---|
1056 | 1066 | /* FIXME: now decision is simplest: at current extent */ |
---|
.. | .. |
---|
1063 | 1073 | } |
---|
1064 | 1074 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
---|
1065 | 1075 | border = path[depth].p_ext[1].ee_block; |
---|
1066 | | - ext_debug("leaf will be split." |
---|
| 1076 | + ext_debug(inode, "leaf will be split." |
---|
1067 | 1077 | " next leaf starts at %d\n", |
---|
1068 | 1078 | le32_to_cpu(border)); |
---|
1069 | 1079 | } else { |
---|
1070 | 1080 | border = newext->ee_block; |
---|
1071 | | - ext_debug("leaf will be added." |
---|
| 1081 | + ext_debug(inode, "leaf will be added." |
---|
1072 | 1082 | " next leaf starts at %d\n", |
---|
1073 | 1083 | le32_to_cpu(border)); |
---|
1074 | 1084 | } |
---|
.. | .. |
---|
1085 | 1095 | * We need this to handle errors and free blocks |
---|
1086 | 1096 | * upon them. |
---|
1087 | 1097 | */ |
---|
1088 | | - ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS); |
---|
| 1098 | + ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags); |
---|
1089 | 1099 | if (!ablocks) |
---|
1090 | 1100 | return -ENOMEM; |
---|
1091 | 1101 | |
---|
1092 | 1102 | /* allocate all needed blocks */ |
---|
1093 | | - ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
---|
| 1103 | + ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at); |
---|
1094 | 1104 | for (a = 0; a < depth - at; a++) { |
---|
1095 | 1105 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
---|
1096 | 1106 | newext, &err, flags); |
---|
.. | .. |
---|
1177 | 1187 | goto cleanup; |
---|
1178 | 1188 | } |
---|
1179 | 1189 | if (k) |
---|
1180 | | - ext_debug("create %d intermediate indices\n", k); |
---|
| 1190 | + ext_debug(inode, "create %d intermediate indices\n", k); |
---|
1181 | 1191 | /* insert new index into current index block */ |
---|
1182 | 1192 | /* current depth stored in i var */ |
---|
1183 | 1193 | i = depth - 1; |
---|
.. | .. |
---|
1205 | 1215 | fidx->ei_block = border; |
---|
1206 | 1216 | ext4_idx_store_pblock(fidx, oldblock); |
---|
1207 | 1217 | |
---|
1208 | | - ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
---|
| 1218 | + ext_debug(inode, "int.index at %d (block %llu): %u -> %llu\n", |
---|
1209 | 1219 | i, newblock, le32_to_cpu(border), oldblock); |
---|
1210 | 1220 | |
---|
1211 | 1221 | /* move remainder of path[i] to the new index block */ |
---|
.. | .. |
---|
1219 | 1229 | } |
---|
1220 | 1230 | /* start copy indexes */ |
---|
1221 | 1231 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
---|
1222 | | - ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
---|
| 1232 | + ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx, |
---|
1223 | 1233 | EXT_MAX_INDEX(path[i].p_hdr)); |
---|
1224 | 1234 | ext4_ext_show_move(inode, path, newblock, i); |
---|
1225 | 1235 | if (m) { |
---|
.. | .. |
---|
1356 | 1366 | EXT_FIRST_INDEX(neh)->ei_block = |
---|
1357 | 1367 | EXT_FIRST_EXTENT(neh)->ee_block; |
---|
1358 | 1368 | } |
---|
1359 | | - ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
---|
| 1369 | + ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %llu\n", |
---|
1360 | 1370 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
---|
1361 | 1371 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
---|
1362 | 1372 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
---|
1363 | 1373 | |
---|
1364 | 1374 | le16_add_cpu(&neh->eh_depth, 1); |
---|
1365 | | - ext4_mark_inode_dirty(handle, inode); |
---|
| 1375 | + err = ext4_mark_inode_dirty(handle, inode); |
---|
1366 | 1376 | out: |
---|
1367 | 1377 | brelse(bh); |
---|
1368 | 1378 | |
---|
.. | .. |
---|
1505 | 1515 | } |
---|
1506 | 1516 | |
---|
1507 | 1517 | /* |
---|
1508 | | - * search the closest allocated block to the right for *logical |
---|
1509 | | - * and returns it at @logical + it's physical address at @phys |
---|
1510 | | - * if *logical is the largest allocated block, the function |
---|
1511 | | - * returns 0 at @phys |
---|
1512 | | - * return value contains 0 (success) or error code |
---|
| 1518 | + * Search the closest allocated block to the right for *logical |
---|
| 1519 | + * and returns it at @logical + it's physical address at @phys. |
---|
| 1520 | + * If not exists, return 0 and @phys is set to 0. We will return |
---|
| 1521 | + * 1 which means we found an allocated block and ret_ex is valid. |
---|
| 1522 | + * Or return a (< 0) error code. |
---|
1513 | 1523 | */ |
---|
1514 | 1524 | static int ext4_ext_search_right(struct inode *inode, |
---|
1515 | 1525 | struct ext4_ext_path *path, |
---|
1516 | 1526 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
---|
1517 | | - struct ext4_extent **ret_ex) |
---|
| 1527 | + struct ext4_extent *ret_ex) |
---|
1518 | 1528 | { |
---|
1519 | 1529 | struct buffer_head *bh = NULL; |
---|
1520 | 1530 | struct ext4_extent_header *eh; |
---|
1521 | 1531 | struct ext4_extent_idx *ix; |
---|
1522 | 1532 | struct ext4_extent *ex; |
---|
1523 | | - ext4_fsblk_t block; |
---|
1524 | 1533 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
---|
1525 | 1534 | int ee_len; |
---|
1526 | 1535 | |
---|
.. | .. |
---|
1587 | 1596 | * follow it and find the closest allocated |
---|
1588 | 1597 | * block to the right */ |
---|
1589 | 1598 | ix++; |
---|
1590 | | - block = ext4_idx_pblock(ix); |
---|
1591 | 1599 | while (++depth < path->p_depth) { |
---|
1592 | 1600 | /* subtract from p_depth to get proper eh_depth */ |
---|
1593 | | - bh = read_extent_tree_block(inode, block, |
---|
1594 | | - path->p_depth - depth, 0); |
---|
| 1601 | + bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 0); |
---|
1595 | 1602 | if (IS_ERR(bh)) |
---|
1596 | 1603 | return PTR_ERR(bh); |
---|
1597 | 1604 | eh = ext_block_hdr(bh); |
---|
1598 | 1605 | ix = EXT_FIRST_INDEX(eh); |
---|
1599 | | - block = ext4_idx_pblock(ix); |
---|
1600 | 1606 | put_bh(bh); |
---|
1601 | 1607 | } |
---|
1602 | 1608 | |
---|
1603 | | - bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0); |
---|
| 1609 | + bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 0); |
---|
1604 | 1610 | if (IS_ERR(bh)) |
---|
1605 | 1611 | return PTR_ERR(bh); |
---|
1606 | 1612 | eh = ext_block_hdr(bh); |
---|
.. | .. |
---|
1608 | 1614 | found_extent: |
---|
1609 | 1615 | *logical = le32_to_cpu(ex->ee_block); |
---|
1610 | 1616 | *phys = ext4_ext_pblock(ex); |
---|
1611 | | - *ret_ex = ex; |
---|
| 1617 | + if (ret_ex) |
---|
| 1618 | + *ret_ex = *ex; |
---|
1612 | 1619 | if (bh) |
---|
1613 | 1620 | put_bh(bh); |
---|
1614 | | - return 0; |
---|
| 1621 | + return 1; |
---|
1615 | 1622 | } |
---|
1616 | 1623 | |
---|
1617 | 1624 | /* |
---|
.. | .. |
---|
1633 | 1640 | return EXT_MAX_BLOCKS; |
---|
1634 | 1641 | |
---|
1635 | 1642 | while (depth >= 0) { |
---|
| 1643 | + struct ext4_ext_path *p = &path[depth]; |
---|
| 1644 | + |
---|
1636 | 1645 | if (depth == path->p_depth) { |
---|
1637 | 1646 | /* leaf */ |
---|
1638 | | - if (path[depth].p_ext && |
---|
1639 | | - path[depth].p_ext != |
---|
1640 | | - EXT_LAST_EXTENT(path[depth].p_hdr)) |
---|
1641 | | - return le32_to_cpu(path[depth].p_ext[1].ee_block); |
---|
| 1647 | + if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) |
---|
| 1648 | + return le32_to_cpu(p->p_ext[1].ee_block); |
---|
1642 | 1649 | } else { |
---|
1643 | 1650 | /* index */ |
---|
1644 | | - if (path[depth].p_idx != |
---|
1645 | | - EXT_LAST_INDEX(path[depth].p_hdr)) |
---|
1646 | | - return le32_to_cpu(path[depth].p_idx[1].ei_block); |
---|
| 1651 | + if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) |
---|
| 1652 | + return le32_to_cpu(p->p_idx[1].ei_block); |
---|
1647 | 1653 | } |
---|
1648 | 1654 | depth--; |
---|
1649 | 1655 | } |
---|
.. | .. |
---|
1743 | 1749 | return err; |
---|
1744 | 1750 | } |
---|
1745 | 1751 | |
---|
1746 | | -int |
---|
1747 | | -ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
---|
1748 | | - struct ext4_extent *ex2) |
---|
| 1752 | +static int ext4_can_extents_be_merged(struct inode *inode, |
---|
| 1753 | + struct ext4_extent *ex1, |
---|
| 1754 | + struct ext4_extent *ex2) |
---|
1749 | 1755 | { |
---|
1750 | 1756 | unsigned short ext1_ee_len, ext2_ee_len; |
---|
1751 | 1757 | |
---|
.. | .. |
---|
1759 | 1765 | le32_to_cpu(ex2->ee_block)) |
---|
1760 | 1766 | return 0; |
---|
1761 | 1767 | |
---|
1762 | | - /* |
---|
1763 | | - * To allow future support for preallocated extents to be added |
---|
1764 | | - * as an RO_COMPAT feature, refuse to merge to extents if |
---|
1765 | | - * this can result in the top bit of ee_len being set. |
---|
1766 | | - */ |
---|
1767 | 1768 | if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) |
---|
1768 | 1769 | return 0; |
---|
1769 | | - /* |
---|
1770 | | - * The check for IO to unwritten extent is somewhat racy as we |
---|
1771 | | - * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after |
---|
1772 | | - * dropping i_data_sem. But reserved blocks should save us in that |
---|
1773 | | - * case. |
---|
1774 | | - */ |
---|
| 1770 | + |
---|
1775 | 1771 | if (ext4_ext_is_unwritten(ex1) && |
---|
1776 | | - (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) || |
---|
1777 | | - atomic_read(&EXT4_I(inode)->i_unwritten) || |
---|
1778 | | - (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN))) |
---|
| 1772 | + ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) |
---|
1779 | 1773 | return 0; |
---|
1780 | 1774 | #ifdef AGGRESSIVE_TEST |
---|
1781 | 1775 | if (ext1_ee_len >= 4) |
---|
.. | .. |
---|
1853 | 1847 | * group descriptor to release the extent tree block. If we |
---|
1854 | 1848 | * can't get the journal credits, give up. |
---|
1855 | 1849 | */ |
---|
1856 | | - if (ext4_journal_extend(handle, 2)) |
---|
| 1850 | + if (ext4_journal_extend(handle, 2, |
---|
| 1851 | + ext4_free_metadata_revoke_credits(inode->i_sb, 1))) |
---|
1857 | 1852 | return; |
---|
1858 | 1853 | |
---|
1859 | 1854 | /* |
---|
.. | .. |
---|
1877 | 1872 | } |
---|
1878 | 1873 | |
---|
1879 | 1874 | /* |
---|
1880 | | - * This function tries to merge the @ex extent to neighbours in the tree. |
---|
1881 | | - * return 1 if merge left else 0. |
---|
| 1875 | + * This function tries to merge the @ex extent to neighbours in the tree, then |
---|
| 1876 | + * tries to collapse the extent tree into the inode. |
---|
1882 | 1877 | */ |
---|
1883 | 1878 | static void ext4_ext_try_to_merge(handle_t *handle, |
---|
1884 | 1879 | struct inode *inode, |
---|
1885 | 1880 | struct ext4_ext_path *path, |
---|
1886 | | - struct ext4_extent *ex) { |
---|
| 1881 | + struct ext4_extent *ex) |
---|
| 1882 | +{ |
---|
1887 | 1883 | struct ext4_extent_header *eh; |
---|
1888 | 1884 | unsigned int depth; |
---|
1889 | 1885 | int merge_done = 0; |
---|
.. | .. |
---|
1954 | 1950 | |
---|
1955 | 1951 | /* |
---|
1956 | 1952 | * ext4_ext_insert_extent: |
---|
1957 | | - * tries to merge requsted extent into the existing extent or |
---|
| 1953 | + * tries to merge requested extent into the existing extent or |
---|
1958 | 1954 | * inserts requested extent as new one into the tree, |
---|
1959 | 1955 | * creating new leaf in the no-space case. |
---|
1960 | 1956 | */ |
---|
.. | .. |
---|
2009 | 2005 | |
---|
2010 | 2006 | /* Try to append newex to the ex */ |
---|
2011 | 2007 | if (ext4_can_extents_be_merged(inode, ex, newext)) { |
---|
2012 | | - ext_debug("append [%d]%d block to %u:[%d]%d" |
---|
| 2008 | + ext_debug(inode, "append [%d]%d block to %u:[%d]%d" |
---|
2013 | 2009 | "(from %llu)\n", |
---|
2014 | 2010 | ext4_ext_is_unwritten(newext), |
---|
2015 | 2011 | ext4_ext_get_actual_len(newext), |
---|
.. | .. |
---|
2034 | 2030 | prepend: |
---|
2035 | 2031 | /* Try to prepend newex to the ex */ |
---|
2036 | 2032 | if (ext4_can_extents_be_merged(inode, newext, ex)) { |
---|
2037 | | - ext_debug("prepend %u[%d]%d block to %u:[%d]%d" |
---|
| 2033 | + ext_debug(inode, "prepend %u[%d]%d block to %u:[%d]%d" |
---|
2038 | 2034 | "(from %llu)\n", |
---|
2039 | 2035 | le32_to_cpu(newext->ee_block), |
---|
2040 | 2036 | ext4_ext_is_unwritten(newext), |
---|
.. | .. |
---|
2072 | 2068 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) |
---|
2073 | 2069 | next = ext4_ext_next_leaf_block(path); |
---|
2074 | 2070 | if (next != EXT_MAX_BLOCKS) { |
---|
2075 | | - ext_debug("next leaf block - %u\n", next); |
---|
| 2071 | + ext_debug(inode, "next leaf block - %u\n", next); |
---|
2076 | 2072 | BUG_ON(npath != NULL); |
---|
2077 | | - npath = ext4_find_extent(inode, next, NULL, 0); |
---|
| 2073 | + npath = ext4_find_extent(inode, next, NULL, gb_flags); |
---|
2078 | 2074 | if (IS_ERR(npath)) |
---|
2079 | 2075 | return PTR_ERR(npath); |
---|
2080 | 2076 | BUG_ON(npath->p_depth != path->p_depth); |
---|
2081 | 2077 | eh = npath[depth].p_hdr; |
---|
2082 | 2078 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
---|
2083 | | - ext_debug("next leaf isn't full(%d)\n", |
---|
| 2079 | + ext_debug(inode, "next leaf isn't full(%d)\n", |
---|
2084 | 2080 | le16_to_cpu(eh->eh_entries)); |
---|
2085 | 2081 | path = npath; |
---|
2086 | 2082 | goto has_space; |
---|
2087 | 2083 | } |
---|
2088 | | - ext_debug("next leaf has no free space(%d,%d)\n", |
---|
| 2084 | + ext_debug(inode, "next leaf has no free space(%d,%d)\n", |
---|
2089 | 2085 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
---|
2090 | 2086 | } |
---|
2091 | 2087 | |
---|
.. | .. |
---|
2111 | 2107 | |
---|
2112 | 2108 | if (!nearex) { |
---|
2113 | 2109 | /* there is no extent in this leaf, create first one */ |
---|
2114 | | - ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", |
---|
| 2110 | + ext_debug(inode, "first extent in the leaf: %u:%llu:[%d]%d\n", |
---|
2115 | 2111 | le32_to_cpu(newext->ee_block), |
---|
2116 | 2112 | ext4_ext_pblock(newext), |
---|
2117 | 2113 | ext4_ext_is_unwritten(newext), |
---|
.. | .. |
---|
2121 | 2117 | if (le32_to_cpu(newext->ee_block) |
---|
2122 | 2118 | > le32_to_cpu(nearex->ee_block)) { |
---|
2123 | 2119 | /* Insert after */ |
---|
2124 | | - ext_debug("insert %u:%llu:[%d]%d before: " |
---|
| 2120 | + ext_debug(inode, "insert %u:%llu:[%d]%d before: " |
---|
2125 | 2121 | "nearest %p\n", |
---|
2126 | 2122 | le32_to_cpu(newext->ee_block), |
---|
2127 | 2123 | ext4_ext_pblock(newext), |
---|
.. | .. |
---|
2132 | 2128 | } else { |
---|
2133 | 2129 | /* Insert before */ |
---|
2134 | 2130 | BUG_ON(newext->ee_block == nearex->ee_block); |
---|
2135 | | - ext_debug("insert %u:%llu:[%d]%d after: " |
---|
| 2131 | + ext_debug(inode, "insert %u:%llu:[%d]%d after: " |
---|
2136 | 2132 | "nearest %p\n", |
---|
2137 | 2133 | le32_to_cpu(newext->ee_block), |
---|
2138 | 2134 | ext4_ext_pblock(newext), |
---|
.. | .. |
---|
2142 | 2138 | } |
---|
2143 | 2139 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
---|
2144 | 2140 | if (len > 0) { |
---|
2145 | | - ext_debug("insert %u:%llu:[%d]%d: " |
---|
| 2141 | + ext_debug(inode, "insert %u:%llu:[%d]%d: " |
---|
2146 | 2142 | "move %d extents from 0x%p to 0x%p\n", |
---|
2147 | 2143 | le32_to_cpu(newext->ee_block), |
---|
2148 | 2144 | ext4_ext_pblock(newext), |
---|
.. | .. |
---|
2179 | 2175 | return err; |
---|
2180 | 2176 | } |
---|
2181 | 2177 | |
---|
2182 | | -static int ext4_fill_fiemap_extents(struct inode *inode, |
---|
2183 | | - ext4_lblk_t block, ext4_lblk_t num, |
---|
2184 | | - struct fiemap_extent_info *fieinfo) |
---|
| 2178 | +static int ext4_fill_es_cache_info(struct inode *inode, |
---|
| 2179 | + ext4_lblk_t block, ext4_lblk_t num, |
---|
| 2180 | + struct fiemap_extent_info *fieinfo) |
---|
2185 | 2181 | { |
---|
2186 | | - struct ext4_ext_path *path = NULL; |
---|
2187 | | - struct ext4_extent *ex; |
---|
| 2182 | + ext4_lblk_t next, end = block + num - 1; |
---|
2188 | 2183 | struct extent_status es; |
---|
2189 | | - ext4_lblk_t next, next_del, start = 0, end = 0; |
---|
2190 | | - ext4_lblk_t last = block + num; |
---|
2191 | | - int exists, depth = 0, err = 0; |
---|
2192 | | - unsigned int flags = 0; |
---|
2193 | 2184 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
---|
| 2185 | + unsigned int flags; |
---|
| 2186 | + int err; |
---|
2194 | 2187 | |
---|
2195 | | - while (block < last && block != EXT_MAX_BLOCKS) { |
---|
2196 | | - num = last - block; |
---|
2197 | | - /* find extent for this block */ |
---|
2198 | | - down_read(&EXT4_I(inode)->i_data_sem); |
---|
2199 | | - |
---|
2200 | | - path = ext4_find_extent(inode, block, &path, 0); |
---|
2201 | | - if (IS_ERR(path)) { |
---|
2202 | | - up_read(&EXT4_I(inode)->i_data_sem); |
---|
2203 | | - err = PTR_ERR(path); |
---|
2204 | | - path = NULL; |
---|
2205 | | - break; |
---|
2206 | | - } |
---|
2207 | | - |
---|
2208 | | - depth = ext_depth(inode); |
---|
2209 | | - if (unlikely(path[depth].p_hdr == NULL)) { |
---|
2210 | | - up_read(&EXT4_I(inode)->i_data_sem); |
---|
2211 | | - EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
---|
2212 | | - err = -EFSCORRUPTED; |
---|
2213 | | - break; |
---|
2214 | | - } |
---|
2215 | | - ex = path[depth].p_ext; |
---|
2216 | | - next = ext4_ext_next_allocated_block(path); |
---|
2217 | | - |
---|
| 2188 | + while (block <= end) { |
---|
| 2189 | + next = 0; |
---|
2218 | 2190 | flags = 0; |
---|
2219 | | - exists = 0; |
---|
2220 | | - if (!ex) { |
---|
2221 | | - /* there is no extent yet, so try to allocate |
---|
2222 | | - * all requested space */ |
---|
2223 | | - start = block; |
---|
2224 | | - end = block + num; |
---|
2225 | | - } else if (le32_to_cpu(ex->ee_block) > block) { |
---|
2226 | | - /* need to allocate space before found extent */ |
---|
2227 | | - start = block; |
---|
2228 | | - end = le32_to_cpu(ex->ee_block); |
---|
2229 | | - if (block + num < end) |
---|
2230 | | - end = block + num; |
---|
2231 | | - } else if (block >= le32_to_cpu(ex->ee_block) |
---|
2232 | | - + ext4_ext_get_actual_len(ex)) { |
---|
2233 | | - /* need to allocate space after found extent */ |
---|
2234 | | - start = block; |
---|
2235 | | - end = block + num; |
---|
2236 | | - if (end >= next) |
---|
2237 | | - end = next; |
---|
2238 | | - } else if (block >= le32_to_cpu(ex->ee_block)) { |
---|
2239 | | - /* |
---|
2240 | | - * some part of requested space is covered |
---|
2241 | | - * by found extent |
---|
2242 | | - */ |
---|
2243 | | - start = block; |
---|
2244 | | - end = le32_to_cpu(ex->ee_block) |
---|
2245 | | - + ext4_ext_get_actual_len(ex); |
---|
2246 | | - if (block + num < end) |
---|
2247 | | - end = block + num; |
---|
2248 | | - exists = 1; |
---|
2249 | | - } else { |
---|
2250 | | - BUG(); |
---|
2251 | | - } |
---|
2252 | | - BUG_ON(end <= start); |
---|
2253 | | - |
---|
2254 | | - if (!exists) { |
---|
2255 | | - es.es_lblk = start; |
---|
2256 | | - es.es_len = end - start; |
---|
2257 | | - es.es_pblk = 0; |
---|
2258 | | - } else { |
---|
2259 | | - es.es_lblk = le32_to_cpu(ex->ee_block); |
---|
2260 | | - es.es_len = ext4_ext_get_actual_len(ex); |
---|
2261 | | - es.es_pblk = ext4_ext_pblock(ex); |
---|
2262 | | - if (ext4_ext_is_unwritten(ex)) |
---|
2263 | | - flags |= FIEMAP_EXTENT_UNWRITTEN; |
---|
2264 | | - } |
---|
2265 | | - |
---|
2266 | | - /* |
---|
2267 | | - * Find delayed extent and update es accordingly. We call |
---|
2268 | | - * it even in !exists case to find out whether es is the |
---|
2269 | | - * last existing extent or not. |
---|
2270 | | - */ |
---|
2271 | | - next_del = ext4_find_delayed_extent(inode, &es); |
---|
2272 | | - if (!exists && next_del) { |
---|
2273 | | - exists = 1; |
---|
| 2191 | + if (!ext4_es_lookup_extent(inode, block, &next, &es)) |
---|
| 2192 | + break; |
---|
| 2193 | + if (ext4_es_is_unwritten(&es)) |
---|
| 2194 | + flags |= FIEMAP_EXTENT_UNWRITTEN; |
---|
| 2195 | + if (ext4_es_is_delayed(&es)) |
---|
2274 | 2196 | flags |= (FIEMAP_EXTENT_DELALLOC | |
---|
2275 | 2197 | FIEMAP_EXTENT_UNKNOWN); |
---|
2276 | | - } |
---|
2277 | | - up_read(&EXT4_I(inode)->i_data_sem); |
---|
2278 | | - |
---|
2279 | | - if (unlikely(es.es_len == 0)) { |
---|
2280 | | - EXT4_ERROR_INODE(inode, "es.es_len == 0"); |
---|
2281 | | - err = -EFSCORRUPTED; |
---|
2282 | | - break; |
---|
2283 | | - } |
---|
2284 | | - |
---|
2285 | | - /* |
---|
2286 | | - * This is possible iff next == next_del == EXT_MAX_BLOCKS. |
---|
2287 | | - * we need to check next == EXT_MAX_BLOCKS because it is |
---|
2288 | | - * possible that an extent is with unwritten and delayed |
---|
2289 | | - * status due to when an extent is delayed allocated and |
---|
2290 | | - * is allocated by fallocate status tree will track both of |
---|
2291 | | - * them in a extent. |
---|
2292 | | - * |
---|
2293 | | - * So we could return a unwritten and delayed extent, and |
---|
2294 | | - * its block is equal to 'next'. |
---|
2295 | | - */ |
---|
2296 | | - if (next == next_del && next == EXT_MAX_BLOCKS) { |
---|
| 2198 | + if (ext4_es_is_hole(&es)) |
---|
| 2199 | + flags |= EXT4_FIEMAP_EXTENT_HOLE; |
---|
| 2200 | + if (next == 0) |
---|
2297 | 2201 | flags |= FIEMAP_EXTENT_LAST; |
---|
2298 | | - if (unlikely(next_del != EXT_MAX_BLOCKS || |
---|
2299 | | - next != EXT_MAX_BLOCKS)) { |
---|
2300 | | - EXT4_ERROR_INODE(inode, |
---|
2301 | | - "next extent == %u, next " |
---|
2302 | | - "delalloc extent = %u", |
---|
2303 | | - next, next_del); |
---|
2304 | | - err = -EFSCORRUPTED; |
---|
2305 | | - break; |
---|
2306 | | - } |
---|
2307 | | - } |
---|
2308 | | - |
---|
2309 | | - if (exists) { |
---|
2310 | | - err = fiemap_fill_next_extent(fieinfo, |
---|
| 2202 | + if (flags & (FIEMAP_EXTENT_DELALLOC| |
---|
| 2203 | + EXT4_FIEMAP_EXTENT_HOLE)) |
---|
| 2204 | + es.es_pblk = 0; |
---|
| 2205 | + else |
---|
| 2206 | + es.es_pblk = ext4_es_pblock(&es); |
---|
| 2207 | + err = fiemap_fill_next_extent(fieinfo, |
---|
2311 | 2208 | (__u64)es.es_lblk << blksize_bits, |
---|
2312 | 2209 | (__u64)es.es_pblk << blksize_bits, |
---|
2313 | 2210 | (__u64)es.es_len << blksize_bits, |
---|
2314 | 2211 | flags); |
---|
2315 | | - if (err < 0) |
---|
2316 | | - break; |
---|
2317 | | - if (err == 1) { |
---|
2318 | | - err = 0; |
---|
2319 | | - break; |
---|
2320 | | - } |
---|
2321 | | - } |
---|
2322 | | - |
---|
2323 | | - block = es.es_lblk + es.es_len; |
---|
| 2212 | + if (next == 0) |
---|
| 2213 | + break; |
---|
| 2214 | + block = next; |
---|
| 2215 | + if (err < 0) |
---|
| 2216 | + return err; |
---|
| 2217 | + if (err == 1) |
---|
| 2218 | + return 0; |
---|
2324 | 2219 | } |
---|
2325 | | - |
---|
2326 | | - ext4_ext_drop_refs(path); |
---|
2327 | | - kfree(path); |
---|
2328 | | - return err; |
---|
| 2220 | + return 0; |
---|
2329 | 2221 | } |
---|
| 2222 | + |
---|
2330 | 2223 | |
---|
2331 | 2224 | /* |
---|
2332 | 2225 | * ext4_ext_determine_hole - determine hole around given block |
---|
.. | .. |
---|
2381 | 2274 | { |
---|
2382 | 2275 | struct extent_status es; |
---|
2383 | 2276 | |
---|
2384 | | - ext4_es_find_delayed_extent_range(inode, hole_start, |
---|
2385 | | - hole_start + hole_len - 1, &es); |
---|
| 2277 | + ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, |
---|
| 2278 | + hole_start + hole_len - 1, &es); |
---|
2386 | 2279 | if (es.es_len) { |
---|
2387 | 2280 | /* There's delayed extent containing lblock? */ |
---|
2388 | 2281 | if (es.es_lblk <= hole_start) |
---|
2389 | 2282 | return; |
---|
2390 | 2283 | hole_len = min(es.es_lblk - hole_start, hole_len); |
---|
2391 | 2284 | } |
---|
2392 | | - ext_debug(" -> %u:%u\n", hole_start, hole_len); |
---|
| 2285 | + ext_debug(inode, " -> %u:%u\n", hole_start, hole_len); |
---|
2393 | 2286 | ext4_es_insert_extent(inode, hole_start, hole_len, ~0, |
---|
2394 | 2287 | EXTENT_STATUS_HOLE); |
---|
2395 | 2288 | } |
---|
.. | .. |
---|
2426 | 2319 | err = ext4_ext_dirty(handle, inode, path); |
---|
2427 | 2320 | if (err) |
---|
2428 | 2321 | return err; |
---|
2429 | | - ext_debug("index is empty, remove it, free block %llu\n", leaf); |
---|
| 2322 | + ext_debug(inode, "index is empty, remove it, free block %llu\n", leaf); |
---|
2430 | 2323 | trace_ext4_ext_rm_idx(inode, leaf); |
---|
2431 | 2324 | |
---|
2432 | 2325 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
---|
.. | .. |
---|
2520 | 2413 | return 0; |
---|
2521 | 2414 | } |
---|
2522 | 2415 | |
---|
| 2416 | +/* |
---|
| 2417 | + * ext4_rereserve_cluster - increment the reserved cluster count when |
---|
| 2418 | + * freeing a cluster with a pending reservation |
---|
| 2419 | + * |
---|
| 2420 | + * @inode - file containing the cluster |
---|
| 2421 | + * @lblk - logical block in cluster to be reserved |
---|
| 2422 | + * |
---|
| 2423 | + * Increments the reserved cluster count and adjusts quota in a bigalloc |
---|
| 2424 | + * file system when freeing a partial cluster containing at least one |
---|
| 2425 | + * delayed and unwritten block. A partial cluster meeting that |
---|
| 2426 | + * requirement will have a pending reservation. If so, the |
---|
| 2427 | + * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to |
---|
| 2428 | + * defer reserved and allocated space accounting to a subsequent call |
---|
| 2429 | + * to this function. |
---|
| 2430 | + */ |
---|
| 2431 | +static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) |
---|
| 2432 | +{ |
---|
| 2433 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 2434 | + struct ext4_inode_info *ei = EXT4_I(inode); |
---|
| 2435 | + |
---|
| 2436 | + dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); |
---|
| 2437 | + |
---|
| 2438 | + spin_lock(&ei->i_block_reservation_lock); |
---|
| 2439 | + ei->i_reserved_data_blocks++; |
---|
| 2440 | + percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); |
---|
| 2441 | + spin_unlock(&ei->i_block_reservation_lock); |
---|
| 2442 | + |
---|
| 2443 | + percpu_counter_add(&sbi->s_freeclusters_counter, 1); |
---|
| 2444 | + ext4_remove_pending(inode, lblk); |
---|
| 2445 | +} |
---|
| 2446 | + |
---|
2523 | 2447 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
---|
2524 | 2448 | struct ext4_extent *ex, |
---|
2525 | | - long long *partial_cluster, |
---|
| 2449 | + struct partial_cluster *partial, |
---|
2526 | 2450 | ext4_lblk_t from, ext4_lblk_t to) |
---|
2527 | 2451 | { |
---|
2528 | 2452 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
2529 | 2453 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
---|
2530 | | - ext4_fsblk_t pblk; |
---|
2531 | | - int flags = get_default_free_blocks_flags(inode); |
---|
| 2454 | + ext4_fsblk_t last_pblk, pblk; |
---|
| 2455 | + ext4_lblk_t num; |
---|
| 2456 | + int flags; |
---|
| 2457 | + |
---|
| 2458 | + /* only extent tail removal is allowed */ |
---|
| 2459 | + if (from < le32_to_cpu(ex->ee_block) || |
---|
| 2460 | + to != le32_to_cpu(ex->ee_block) + ee_len - 1) { |
---|
| 2461 | + ext4_error(sbi->s_sb, |
---|
| 2462 | + "strange request: removal(2) %u-%u from %u:%u", |
---|
| 2463 | + from, to, le32_to_cpu(ex->ee_block), ee_len); |
---|
| 2464 | + return 0; |
---|
| 2465 | + } |
---|
| 2466 | + |
---|
| 2467 | +#ifdef EXTENTS_STATS |
---|
| 2468 | + spin_lock(&sbi->s_ext_stats_lock); |
---|
| 2469 | + sbi->s_ext_blocks += ee_len; |
---|
| 2470 | + sbi->s_ext_extents++; |
---|
| 2471 | + if (ee_len < sbi->s_ext_min) |
---|
| 2472 | + sbi->s_ext_min = ee_len; |
---|
| 2473 | + if (ee_len > sbi->s_ext_max) |
---|
| 2474 | + sbi->s_ext_max = ee_len; |
---|
| 2475 | + if (ext_depth(inode) > sbi->s_depth_max) |
---|
| 2476 | + sbi->s_depth_max = ext_depth(inode); |
---|
| 2477 | + spin_unlock(&sbi->s_ext_stats_lock); |
---|
| 2478 | +#endif |
---|
| 2479 | + |
---|
| 2480 | + trace_ext4_remove_blocks(inode, ex, from, to, partial); |
---|
| 2481 | + |
---|
| 2482 | + /* |
---|
| 2483 | + * if we have a partial cluster, and it's different from the |
---|
| 2484 | + * cluster of the last block in the extent, we free it |
---|
| 2485 | + */ |
---|
| 2486 | + last_pblk = ext4_ext_pblock(ex) + ee_len - 1; |
---|
| 2487 | + |
---|
| 2488 | + if (partial->state != initial && |
---|
| 2489 | + partial->pclu != EXT4_B2C(sbi, last_pblk)) { |
---|
| 2490 | + if (partial->state == tofree) { |
---|
| 2491 | + flags = get_default_free_blocks_flags(inode); |
---|
| 2492 | + if (ext4_is_pending(inode, partial->lblk)) |
---|
| 2493 | + flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
---|
| 2494 | + ext4_free_blocks(handle, inode, NULL, |
---|
| 2495 | + EXT4_C2B(sbi, partial->pclu), |
---|
| 2496 | + sbi->s_cluster_ratio, flags); |
---|
| 2497 | + if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
---|
| 2498 | + ext4_rereserve_cluster(inode, partial->lblk); |
---|
| 2499 | + } |
---|
| 2500 | + partial->state = initial; |
---|
| 2501 | + } |
---|
| 2502 | + |
---|
| 2503 | + num = le32_to_cpu(ex->ee_block) + ee_len - from; |
---|
| 2504 | + pblk = ext4_ext_pblock(ex) + ee_len - num; |
---|
| 2505 | + |
---|
| 2506 | + /* |
---|
| 2507 | + * We free the partial cluster at the end of the extent (if any), |
---|
| 2508 | + * unless the cluster is used by another extent (partial_cluster |
---|
| 2509 | + * state is nofree). If a partial cluster exists here, it must be |
---|
| 2510 | + * shared with the last block in the extent. |
---|
| 2511 | + */ |
---|
| 2512 | + flags = get_default_free_blocks_flags(inode); |
---|
| 2513 | + |
---|
| 2514 | + /* partial, left end cluster aligned, right end unaligned */ |
---|
| 2515 | + if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && |
---|
| 2516 | + (EXT4_LBLK_CMASK(sbi, to) >= from) && |
---|
| 2517 | + (partial->state != nofree)) { |
---|
| 2518 | + if (ext4_is_pending(inode, to)) |
---|
| 2519 | + flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
---|
| 2520 | + ext4_free_blocks(handle, inode, NULL, |
---|
| 2521 | + EXT4_PBLK_CMASK(sbi, last_pblk), |
---|
| 2522 | + sbi->s_cluster_ratio, flags); |
---|
| 2523 | + if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
---|
| 2524 | + ext4_rereserve_cluster(inode, to); |
---|
| 2525 | + partial->state = initial; |
---|
| 2526 | + flags = get_default_free_blocks_flags(inode); |
---|
| 2527 | + } |
---|
| 2528 | + |
---|
| 2529 | + flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
---|
2532 | 2530 | |
---|
2533 | 2531 | /* |
---|
2534 | 2532 | * For bigalloc file systems, we never free a partial cluster |
---|
2535 | | - * at the beginning of the extent. Instead, we make a note |
---|
2536 | | - * that we tried freeing the cluster, and check to see if we |
---|
| 2533 | + * at the beginning of the extent. Instead, we check to see if we |
---|
2537 | 2534 | * need to free it on a subsequent call to ext4_remove_blocks, |
---|
2538 | 2535 | * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space. |
---|
2539 | 2536 | */ |
---|
2540 | 2537 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
---|
| 2538 | + ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
---|
2541 | 2539 | |
---|
2542 | | - trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster); |
---|
| 2540 | + /* reset the partial cluster if we've freed past it */ |
---|
| 2541 | + if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) |
---|
| 2542 | + partial->state = initial; |
---|
| 2543 | + |
---|
2543 | 2544 | /* |
---|
2544 | | - * If we have a partial cluster, and it's different from the |
---|
2545 | | - * cluster of the last block, we need to explicitly free the |
---|
2546 | | - * partial cluster here. |
---|
| 2545 | + * If we've freed the entire extent but the beginning is not left |
---|
| 2546 | + * cluster aligned and is not marked as ineligible for freeing we |
---|
| 2547 | + * record the partial cluster at the beginning of the extent. It |
---|
| 2548 | + * wasn't freed by the preceding ext4_free_blocks() call, and we |
---|
| 2549 | + * need to look farther to the left to determine if it's to be freed |
---|
| 2550 | + * (not shared with another extent). Else, reset the partial |
---|
| 2551 | + * cluster - we're either done freeing or the beginning of the |
---|
| 2552 | + * extent is left cluster aligned. |
---|
2547 | 2553 | */ |
---|
2548 | | - pblk = ext4_ext_pblock(ex) + ee_len - 1; |
---|
2549 | | - if (*partial_cluster > 0 && |
---|
2550 | | - *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) { |
---|
2551 | | - ext4_free_blocks(handle, inode, NULL, |
---|
2552 | | - EXT4_C2B(sbi, *partial_cluster), |
---|
2553 | | - sbi->s_cluster_ratio, flags); |
---|
2554 | | - *partial_cluster = 0; |
---|
2555 | | - } |
---|
2556 | | - |
---|
2557 | | -#ifdef EXTENTS_STATS |
---|
2558 | | - { |
---|
2559 | | - struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
2560 | | - spin_lock(&sbi->s_ext_stats_lock); |
---|
2561 | | - sbi->s_ext_blocks += ee_len; |
---|
2562 | | - sbi->s_ext_extents++; |
---|
2563 | | - if (ee_len < sbi->s_ext_min) |
---|
2564 | | - sbi->s_ext_min = ee_len; |
---|
2565 | | - if (ee_len > sbi->s_ext_max) |
---|
2566 | | - sbi->s_ext_max = ee_len; |
---|
2567 | | - if (ext_depth(inode) > sbi->s_depth_max) |
---|
2568 | | - sbi->s_depth_max = ext_depth(inode); |
---|
2569 | | - spin_unlock(&sbi->s_ext_stats_lock); |
---|
2570 | | - } |
---|
2571 | | -#endif |
---|
2572 | | - if (from >= le32_to_cpu(ex->ee_block) |
---|
2573 | | - && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
---|
2574 | | - /* tail removal */ |
---|
2575 | | - ext4_lblk_t num; |
---|
2576 | | - long long first_cluster; |
---|
2577 | | - |
---|
2578 | | - num = le32_to_cpu(ex->ee_block) + ee_len - from; |
---|
2579 | | - pblk = ext4_ext_pblock(ex) + ee_len - num; |
---|
2580 | | - /* |
---|
2581 | | - * Usually we want to free partial cluster at the end of the |
---|
2582 | | - * extent, except for the situation when the cluster is still |
---|
2583 | | - * used by any other extent (partial_cluster is negative). |
---|
2584 | | - */ |
---|
2585 | | - if (*partial_cluster < 0 && |
---|
2586 | | - *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1)) |
---|
2587 | | - flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
---|
2588 | | - |
---|
2589 | | - ext_debug("free last %u blocks starting %llu partial %lld\n", |
---|
2590 | | - num, pblk, *partial_cluster); |
---|
2591 | | - ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
---|
2592 | | - /* |
---|
2593 | | - * If the block range to be freed didn't start at the |
---|
2594 | | - * beginning of a cluster, and we removed the entire |
---|
2595 | | - * extent and the cluster is not used by any other extent, |
---|
2596 | | - * save the partial cluster here, since we might need to |
---|
2597 | | - * delete if we determine that the truncate or punch hole |
---|
2598 | | - * operation has removed all of the blocks in the cluster. |
---|
2599 | | - * If that cluster is used by another extent, preserve its |
---|
2600 | | - * negative value so it isn't freed later on. |
---|
2601 | | - * |
---|
2602 | | - * If the whole extent wasn't freed, we've reached the |
---|
2603 | | - * start of the truncated/punched region and have finished |
---|
2604 | | - * removing blocks. If there's a partial cluster here it's |
---|
2605 | | - * shared with the remainder of the extent and is no longer |
---|
2606 | | - * a candidate for removal. |
---|
2607 | | - */ |
---|
2608 | | - if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) { |
---|
2609 | | - first_cluster = (long long) EXT4_B2C(sbi, pblk); |
---|
2610 | | - if (first_cluster != -*partial_cluster) |
---|
2611 | | - *partial_cluster = first_cluster; |
---|
2612 | | - } else { |
---|
2613 | | - *partial_cluster = 0; |
---|
| 2554 | + if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { |
---|
| 2555 | + if (partial->state == initial) { |
---|
| 2556 | + partial->pclu = EXT4_B2C(sbi, pblk); |
---|
| 2557 | + partial->lblk = from; |
---|
| 2558 | + partial->state = tofree; |
---|
2614 | 2559 | } |
---|
2615 | | - } else |
---|
2616 | | - ext4_error(sbi->s_sb, "strange request: removal(2) " |
---|
2617 | | - "%u-%u from %u:%u", |
---|
2618 | | - from, to, le32_to_cpu(ex->ee_block), ee_len); |
---|
| 2560 | + } else { |
---|
| 2561 | + partial->state = initial; |
---|
| 2562 | + } |
---|
| 2563 | + |
---|
2619 | 2564 | return 0; |
---|
2620 | 2565 | } |
---|
2621 | | - |
---|
2622 | 2566 | |
---|
2623 | 2567 | /* |
---|
2624 | 2568 | * ext4_ext_rm_leaf() Removes the extents associated with the |
---|
.. | .. |
---|
2638 | 2582 | static int |
---|
2639 | 2583 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
---|
2640 | 2584 | struct ext4_ext_path *path, |
---|
2641 | | - long long *partial_cluster, |
---|
| 2585 | + struct partial_cluster *partial, |
---|
2642 | 2586 | ext4_lblk_t start, ext4_lblk_t end) |
---|
2643 | 2587 | { |
---|
2644 | 2588 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
2645 | 2589 | int err = 0, correct_index = 0; |
---|
2646 | | - int depth = ext_depth(inode), credits; |
---|
| 2590 | + int depth = ext_depth(inode), credits, revoke_credits; |
---|
2647 | 2591 | struct ext4_extent_header *eh; |
---|
2648 | 2592 | ext4_lblk_t a, b; |
---|
2649 | 2593 | unsigned num; |
---|
.. | .. |
---|
2654 | 2598 | ext4_fsblk_t pblk; |
---|
2655 | 2599 | |
---|
2656 | 2600 | /* the header must be checked already in ext4_ext_remove_space() */ |
---|
2657 | | - ext_debug("truncate since %u in leaf to %u\n", start, end); |
---|
| 2601 | + ext_debug(inode, "truncate since %u in leaf to %u\n", start, end); |
---|
2658 | 2602 | if (!path[depth].p_hdr) |
---|
2659 | 2603 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
---|
2660 | 2604 | eh = path[depth].p_hdr; |
---|
.. | .. |
---|
2670 | 2614 | ex_ee_block = le32_to_cpu(ex->ee_block); |
---|
2671 | 2615 | ex_ee_len = ext4_ext_get_actual_len(ex); |
---|
2672 | 2616 | |
---|
2673 | | - trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster); |
---|
| 2617 | + trace_ext4_ext_rm_leaf(inode, start, ex, partial); |
---|
2674 | 2618 | |
---|
2675 | 2619 | while (ex >= EXT_FIRST_EXTENT(eh) && |
---|
2676 | 2620 | ex_ee_block + ex_ee_len > start) { |
---|
.. | .. |
---|
2680 | 2624 | else |
---|
2681 | 2625 | unwritten = 0; |
---|
2682 | 2626 | |
---|
2683 | | - ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
---|
| 2627 | + ext_debug(inode, "remove ext %u:[%d]%d\n", ex_ee_block, |
---|
2684 | 2628 | unwritten, ex_ee_len); |
---|
2685 | 2629 | path[depth].p_ext = ex; |
---|
2686 | 2630 | |
---|
.. | .. |
---|
2688 | 2632 | b = ex_ee_block+ex_ee_len - 1 < end ? |
---|
2689 | 2633 | ex_ee_block+ex_ee_len - 1 : end; |
---|
2690 | 2634 | |
---|
2691 | | - ext_debug(" border %u:%u\n", a, b); |
---|
| 2635 | + ext_debug(inode, " border %u:%u\n", a, b); |
---|
2692 | 2636 | |
---|
2693 | 2637 | /* If this extent is beyond the end of the hole, skip it */ |
---|
2694 | 2638 | if (end < ex_ee_block) { |
---|
.. | .. |
---|
2701 | 2645 | */ |
---|
2702 | 2646 | if (sbi->s_cluster_ratio > 1) { |
---|
2703 | 2647 | pblk = ext4_ext_pblock(ex); |
---|
2704 | | - *partial_cluster = |
---|
2705 | | - -(long long) EXT4_B2C(sbi, pblk); |
---|
| 2648 | + partial->pclu = EXT4_B2C(sbi, pblk); |
---|
| 2649 | + partial->state = nofree; |
---|
2706 | 2650 | } |
---|
2707 | 2651 | ex--; |
---|
2708 | 2652 | ex_ee_block = le32_to_cpu(ex->ee_block); |
---|
.. | .. |
---|
2735 | 2679 | credits += (ext_depth(inode)) + 1; |
---|
2736 | 2680 | } |
---|
2737 | 2681 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
---|
| 2682 | + /* |
---|
| 2683 | + * We may end up freeing some index blocks and data from the |
---|
| 2684 | + * punched range. Note that partial clusters are accounted for |
---|
| 2685 | + * by ext4_free_data_revoke_credits(). |
---|
| 2686 | + */ |
---|
| 2687 | + revoke_credits = |
---|
| 2688 | + ext4_free_metadata_revoke_credits(inode->i_sb, |
---|
| 2689 | + ext_depth(inode)) + |
---|
| 2690 | + ext4_free_data_revoke_credits(inode, b - a + 1); |
---|
2738 | 2691 | |
---|
2739 | | - err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
---|
2740 | | - if (err) |
---|
| 2692 | + err = ext4_datasem_ensure_credits(handle, inode, credits, |
---|
| 2693 | + credits, revoke_credits); |
---|
| 2694 | + if (err) { |
---|
| 2695 | + if (err > 0) |
---|
| 2696 | + err = -EAGAIN; |
---|
2741 | 2697 | goto out; |
---|
| 2698 | + } |
---|
2742 | 2699 | |
---|
2743 | 2700 | err = ext4_ext_get_access(handle, inode, path + depth); |
---|
2744 | 2701 | if (err) |
---|
2745 | 2702 | goto out; |
---|
2746 | 2703 | |
---|
2747 | | - err = ext4_remove_blocks(handle, inode, ex, partial_cluster, |
---|
2748 | | - a, b); |
---|
| 2704 | + err = ext4_remove_blocks(handle, inode, ex, partial, a, b); |
---|
2749 | 2705 | if (err) |
---|
2750 | 2706 | goto out; |
---|
2751 | 2707 | |
---|
.. | .. |
---|
2785 | 2741 | if (err) |
---|
2786 | 2742 | goto out; |
---|
2787 | 2743 | |
---|
2788 | | - ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num, |
---|
| 2744 | + ext_debug(inode, "new extent: %u:%u:%llu\n", ex_ee_block, num, |
---|
2789 | 2745 | ext4_ext_pblock(ex)); |
---|
2790 | 2746 | ex--; |
---|
2791 | 2747 | ex_ee_block = le32_to_cpu(ex->ee_block); |
---|
.. | .. |
---|
2799 | 2755 | * If there's a partial cluster and at least one extent remains in |
---|
2800 | 2756 | * the leaf, free the partial cluster if it isn't shared with the |
---|
2801 | 2757 | * current extent. If it is shared with the current extent |
---|
2802 | | - * we zero partial_cluster because we've reached the start of the |
---|
| 2758 | + * we reset the partial cluster because we've reached the start of the |
---|
2803 | 2759 | * truncated/punched region and we're done removing blocks. |
---|
2804 | 2760 | */ |
---|
2805 | | - if (*partial_cluster > 0 && ex >= EXT_FIRST_EXTENT(eh)) { |
---|
| 2761 | + if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { |
---|
2806 | 2762 | pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; |
---|
2807 | | - if (*partial_cluster != (long long) EXT4_B2C(sbi, pblk)) { |
---|
| 2763 | + if (partial->pclu != EXT4_B2C(sbi, pblk)) { |
---|
| 2764 | + int flags = get_default_free_blocks_flags(inode); |
---|
| 2765 | + |
---|
| 2766 | + if (ext4_is_pending(inode, partial->lblk)) |
---|
| 2767 | + flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
---|
2808 | 2768 | ext4_free_blocks(handle, inode, NULL, |
---|
2809 | | - EXT4_C2B(sbi, *partial_cluster), |
---|
2810 | | - sbi->s_cluster_ratio, |
---|
2811 | | - get_default_free_blocks_flags(inode)); |
---|
| 2769 | + EXT4_C2B(sbi, partial->pclu), |
---|
| 2770 | + sbi->s_cluster_ratio, flags); |
---|
| 2771 | + if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
---|
| 2772 | + ext4_rereserve_cluster(inode, partial->lblk); |
---|
2812 | 2773 | } |
---|
2813 | | - *partial_cluster = 0; |
---|
| 2774 | + partial->state = initial; |
---|
2814 | 2775 | } |
---|
2815 | 2776 | |
---|
2816 | 2777 | /* if this leaf is free, then we should |
---|
.. | .. |
---|
2849 | 2810 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
2850 | 2811 | int depth = ext_depth(inode); |
---|
2851 | 2812 | struct ext4_ext_path *path = NULL; |
---|
2852 | | - long long partial_cluster = 0; |
---|
| 2813 | + struct partial_cluster partial; |
---|
2853 | 2814 | handle_t *handle; |
---|
2854 | 2815 | int i = 0, err = 0; |
---|
2855 | 2816 | |
---|
2856 | | - ext_debug("truncate since %u to %u\n", start, end); |
---|
| 2817 | + partial.pclu = 0; |
---|
| 2818 | + partial.lblk = 0; |
---|
| 2819 | + partial.state = initial; |
---|
| 2820 | + |
---|
| 2821 | + ext_debug(inode, "truncate since %u to %u\n", start, end); |
---|
2857 | 2822 | |
---|
2858 | 2823 | /* probably first extent we're gonna free will be last in block */ |
---|
2859 | | - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1); |
---|
| 2824 | + handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, |
---|
| 2825 | + depth + 1, |
---|
| 2826 | + ext4_free_metadata_revoke_credits(inode->i_sb, depth)); |
---|
2860 | 2827 | if (IS_ERR(handle)) |
---|
2861 | 2828 | return PTR_ERR(handle); |
---|
2862 | 2829 | |
---|
.. | .. |
---|
2876 | 2843 | ext4_fsblk_t pblk; |
---|
2877 | 2844 | |
---|
2878 | 2845 | /* find extent for or closest extent to this block */ |
---|
2879 | | - path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); |
---|
| 2846 | + path = ext4_find_extent(inode, end, NULL, |
---|
| 2847 | + EXT4_EX_NOCACHE | EXT4_EX_NOFAIL); |
---|
2880 | 2848 | if (IS_ERR(path)) { |
---|
2881 | 2849 | ext4_journal_stop(handle); |
---|
2882 | 2850 | return PTR_ERR(path); |
---|
.. | .. |
---|
2912 | 2880 | */ |
---|
2913 | 2881 | if (sbi->s_cluster_ratio > 1) { |
---|
2914 | 2882 | pblk = ext4_ext_pblock(ex) + end - ee_block + 1; |
---|
2915 | | - partial_cluster = |
---|
2916 | | - -(long long) EXT4_B2C(sbi, pblk); |
---|
| 2883 | + partial.pclu = EXT4_B2C(sbi, pblk); |
---|
| 2884 | + partial.state = nofree; |
---|
2917 | 2885 | } |
---|
2918 | 2886 | |
---|
2919 | 2887 | /* |
---|
.. | .. |
---|
2927 | 2895 | if (err < 0) |
---|
2928 | 2896 | goto out; |
---|
2929 | 2897 | |
---|
2930 | | - } else if (sbi->s_cluster_ratio > 1 && end >= ex_end) { |
---|
| 2898 | + } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && |
---|
| 2899 | + partial.state == initial) { |
---|
2931 | 2900 | /* |
---|
2932 | | - * If there's an extent to the right its first cluster |
---|
2933 | | - * contains the immediate right boundary of the |
---|
2934 | | - * truncated/punched region. Set partial_cluster to |
---|
2935 | | - * its negative value so it won't be freed if shared |
---|
2936 | | - * with the current extent. The end < ee_block case |
---|
2937 | | - * is handled in ext4_ext_rm_leaf(). |
---|
| 2901 | + * If we're punching, there's an extent to the right. |
---|
| 2902 | + * If the partial cluster hasn't been set, set it to |
---|
| 2903 | + * that extent's first cluster and its state to nofree |
---|
| 2904 | + * so it won't be freed should it contain blocks to be |
---|
| 2905 | + * removed. If it's already set (tofree/nofree), we're |
---|
| 2906 | + * retrying and keep the original partial cluster info |
---|
| 2907 | + * so a cluster marked tofree as a result of earlier |
---|
| 2908 | + * extent removal is not lost. |
---|
2938 | 2909 | */ |
---|
2939 | 2910 | lblk = ex_end + 1; |
---|
2940 | 2911 | err = ext4_ext_search_right(inode, path, &lblk, &pblk, |
---|
2941 | | - &ex); |
---|
2942 | | - if (err) |
---|
| 2912 | + NULL); |
---|
| 2913 | + if (err < 0) |
---|
2943 | 2914 | goto out; |
---|
2944 | | - if (pblk) |
---|
2945 | | - partial_cluster = |
---|
2946 | | - -(long long) EXT4_B2C(sbi, pblk); |
---|
| 2915 | + if (pblk) { |
---|
| 2916 | + partial.pclu = EXT4_B2C(sbi, pblk); |
---|
| 2917 | + partial.state = nofree; |
---|
| 2918 | + } |
---|
2947 | 2919 | } |
---|
2948 | 2920 | } |
---|
2949 | 2921 | /* |
---|
.. | .. |
---|
2958 | 2930 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
---|
2959 | 2931 | } else { |
---|
2960 | 2932 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
---|
2961 | | - GFP_NOFS); |
---|
| 2933 | + GFP_NOFS | __GFP_NOFAIL); |
---|
2962 | 2934 | if (path == NULL) { |
---|
2963 | 2935 | ext4_journal_stop(handle); |
---|
2964 | 2936 | return -ENOMEM; |
---|
.. | .. |
---|
2978 | 2950 | if (i == depth) { |
---|
2979 | 2951 | /* this is leaf block */ |
---|
2980 | 2952 | err = ext4_ext_rm_leaf(handle, inode, path, |
---|
2981 | | - &partial_cluster, start, |
---|
2982 | | - end); |
---|
| 2953 | + &partial, start, end); |
---|
2983 | 2954 | /* root level has p_bh == NULL, brelse() eats this */ |
---|
2984 | 2955 | brelse(path[i].p_bh); |
---|
2985 | 2956 | path[i].p_bh = NULL; |
---|
.. | .. |
---|
2989 | 2960 | |
---|
2990 | 2961 | /* this is index block */ |
---|
2991 | 2962 | if (!path[i].p_hdr) { |
---|
2992 | | - ext_debug("initialize header\n"); |
---|
| 2963 | + ext_debug(inode, "initialize header\n"); |
---|
2993 | 2964 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
---|
2994 | 2965 | } |
---|
2995 | 2966 | |
---|
.. | .. |
---|
2997 | 2968 | /* this level hasn't been touched yet */ |
---|
2998 | 2969 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
---|
2999 | 2970 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
---|
3000 | | - ext_debug("init index ptr: hdr 0x%p, num %d\n", |
---|
| 2971 | + ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n", |
---|
3001 | 2972 | path[i].p_hdr, |
---|
3002 | 2973 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
---|
3003 | 2974 | } else { |
---|
.. | .. |
---|
3005 | 2976 | path[i].p_idx--; |
---|
3006 | 2977 | } |
---|
3007 | 2978 | |
---|
3008 | | - ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
---|
| 2979 | + ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n", |
---|
3009 | 2980 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
---|
3010 | 2981 | path[i].p_idx); |
---|
3011 | 2982 | if (ext4_ext_more_to_rm(path + i)) { |
---|
3012 | 2983 | struct buffer_head *bh; |
---|
3013 | 2984 | /* go to the next level */ |
---|
3014 | | - ext_debug("move to level %d (block %llu)\n", |
---|
| 2985 | + ext_debug(inode, "move to level %d (block %llu)\n", |
---|
3015 | 2986 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
---|
3016 | 2987 | memset(path + i + 1, 0, sizeof(*path)); |
---|
3017 | | - bh = read_extent_tree_block(inode, |
---|
3018 | | - ext4_idx_pblock(path[i].p_idx), depth - i - 1, |
---|
3019 | | - EXT4_EX_NOCACHE); |
---|
| 2988 | + bh = read_extent_tree_block(inode, path[i].p_idx, |
---|
| 2989 | + depth - i - 1, |
---|
| 2990 | + EXT4_EX_NOCACHE); |
---|
3020 | 2991 | if (IS_ERR(bh)) { |
---|
3021 | 2992 | /* should we reset i_size? */ |
---|
3022 | 2993 | err = PTR_ERR(bh); |
---|
.. | .. |
---|
3047 | 3018 | brelse(path[i].p_bh); |
---|
3048 | 3019 | path[i].p_bh = NULL; |
---|
3049 | 3020 | i--; |
---|
3050 | | - ext_debug("return to level %d\n", i); |
---|
| 3021 | + ext_debug(inode, "return to level %d\n", i); |
---|
3051 | 3022 | } |
---|
3052 | 3023 | } |
---|
3053 | 3024 | |
---|
3054 | | - trace_ext4_ext_remove_space_done(inode, start, end, depth, |
---|
3055 | | - partial_cluster, path->p_hdr->eh_entries); |
---|
| 3025 | + trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, |
---|
| 3026 | + path->p_hdr->eh_entries); |
---|
3056 | 3027 | |
---|
3057 | 3028 | /* |
---|
3058 | | - * If we still have something in the partial cluster and we have removed |
---|
3059 | | - * even the first extent, then we should free the blocks in the partial |
---|
3060 | | - * cluster as well. (This code will only run when there are no leaves |
---|
3061 | | - * to the immediate left of the truncated/punched region.) |
---|
| 3029 | + * if there's a partial cluster and we have removed the first extent |
---|
| 3030 | + * in the file, then we also free the partial cluster, if any |
---|
3062 | 3031 | */ |
---|
3063 | | - if (partial_cluster > 0 && err == 0) { |
---|
3064 | | - /* don't zero partial_cluster since it's not used afterwards */ |
---|
| 3032 | + if (partial.state == tofree && err == 0) { |
---|
| 3033 | + int flags = get_default_free_blocks_flags(inode); |
---|
| 3034 | + |
---|
| 3035 | + if (ext4_is_pending(inode, partial.lblk)) |
---|
| 3036 | + flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
---|
3065 | 3037 | ext4_free_blocks(handle, inode, NULL, |
---|
3066 | | - EXT4_C2B(sbi, partial_cluster), |
---|
3067 | | - sbi->s_cluster_ratio, |
---|
3068 | | - get_default_free_blocks_flags(inode)); |
---|
| 3038 | + EXT4_C2B(sbi, partial.pclu), |
---|
| 3039 | + sbi->s_cluster_ratio, flags); |
---|
| 3040 | + if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
---|
| 3041 | + ext4_rereserve_cluster(inode, partial.lblk); |
---|
| 3042 | + partial.state = initial; |
---|
3069 | 3043 | } |
---|
3070 | 3044 | |
---|
3071 | 3045 | /* TODO: flexible tree reduction should be here */ |
---|
.. | .. |
---|
3186 | 3160 | * |
---|
3187 | 3161 | * |
---|
3188 | 3162 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
---|
3189 | | - * of which are deterimined by split_flag. |
---|
| 3163 | + * of which are determined by split_flag. |
---|
3190 | 3164 | * |
---|
3191 | 3165 | * There are two cases: |
---|
3192 | 3166 | * a> the extent are splitted into two extent. |
---|
.. | .. |
---|
3212 | 3186 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
---|
3213 | 3187 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
---|
3214 | 3188 | |
---|
3215 | | - ext_debug("ext4_split_extents_at: inode %lu, logical" |
---|
3216 | | - "block %llu\n", inode->i_ino, (unsigned long long)split); |
---|
| 3189 | + ext_debug(inode, "logical block %llu\n", (unsigned long long)split); |
---|
3217 | 3190 | |
---|
3218 | 3191 | ext4_ext_show_leaf(inode, path); |
---|
3219 | 3192 | |
---|
.. | .. |
---|
3321 | 3294 | |
---|
3322 | 3295 | fix_extent_len: |
---|
3323 | 3296 | ex->ee_len = orig_ex.ee_len; |
---|
| 3297 | + /* |
---|
| 3298 | + * Ignore ext4_ext_dirty return value since we are already in error path |
---|
| 3299 | + * and err is a non-zero error code. |
---|
| 3300 | + */ |
---|
3324 | 3301 | ext4_ext_dirty(handle, inode, path + path->p_depth); |
---|
3325 | 3302 | return err; |
---|
3326 | 3303 | out: |
---|
.. | .. |
---|
3380 | 3357 | * Update path is required because previous ext4_split_extent_at() may |
---|
3381 | 3358 | * result in split of original leaf or extent zeroout. |
---|
3382 | 3359 | */ |
---|
3383 | | - path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
---|
| 3360 | + path = ext4_find_extent(inode, map->m_lblk, ppath, flags); |
---|
3384 | 3361 | if (IS_ERR(path)) |
---|
3385 | 3362 | return PTR_ERR(path); |
---|
3386 | 3363 | depth = ext_depth(inode); |
---|
.. | .. |
---|
3449 | 3426 | int err = 0; |
---|
3450 | 3427 | int split_flag = EXT4_EXT_DATA_VALID2; |
---|
3451 | 3428 | |
---|
3452 | | - ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
---|
3453 | | - "block %llu, max_blocks %u\n", inode->i_ino, |
---|
3454 | | - (unsigned long long)map->m_lblk, map_len); |
---|
| 3429 | + ext_debug(inode, "logical block %llu, max_blocks %u\n", |
---|
| 3430 | + (unsigned long long)map->m_lblk, map_len); |
---|
3455 | 3431 | |
---|
3456 | 3432 | sbi = EXT4_SB(inode->i_sb); |
---|
3457 | 3433 | eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) |
---|
.. | .. |
---|
3583 | 3559 | } |
---|
3584 | 3560 | if (allocated) { |
---|
3585 | 3561 | /* Mark the block containing both extents as dirty */ |
---|
3586 | | - ext4_ext_dirty(handle, inode, path + depth); |
---|
| 3562 | + err = ext4_ext_dirty(handle, inode, path + depth); |
---|
3587 | 3563 | |
---|
3588 | 3564 | /* Update path to point to the right extent */ |
---|
3589 | 3565 | path[depth].p_ext = abut_ex; |
---|
.. | .. |
---|
3601 | 3577 | if (EXT4_EXT_MAY_ZEROOUT & split_flag) |
---|
3602 | 3578 | max_zeroout = sbi->s_extent_max_zeroout_kb >> |
---|
3603 | 3579 | (inode->i_sb->s_blocksize_bits - 10); |
---|
3604 | | - |
---|
3605 | | - if (IS_ENCRYPTED(inode)) |
---|
3606 | | - max_zeroout = 0; |
---|
3607 | 3580 | |
---|
3608 | 3581 | /* |
---|
3609 | 3582 | * five cases: |
---|
.. | .. |
---|
3706 | 3679 | unsigned int ee_len; |
---|
3707 | 3680 | int split_flag = 0, depth; |
---|
3708 | 3681 | |
---|
3709 | | - ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n", |
---|
3710 | | - __func__, inode->i_ino, |
---|
| 3682 | + ext_debug(inode, "logical block %llu, max_blocks %u\n", |
---|
3711 | 3683 | (unsigned long long)map->m_lblk, map->m_len); |
---|
3712 | 3684 | |
---|
3713 | 3685 | eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) |
---|
.. | .. |
---|
3716 | 3688 | eof_block = map->m_lblk + map->m_len; |
---|
3717 | 3689 | /* |
---|
3718 | 3690 | * It is safe to convert extent to initialized via explicit |
---|
3719 | | - * zeroout only if extent is fully insde i_size or new_size. |
---|
| 3691 | + * zeroout only if extent is fully inside i_size or new_size. |
---|
3720 | 3692 | */ |
---|
3721 | 3693 | depth = ext_depth(inode); |
---|
3722 | 3694 | ex = path[depth].p_ext; |
---|
.. | .. |
---|
3753 | 3725 | ee_block = le32_to_cpu(ex->ee_block); |
---|
3754 | 3726 | ee_len = ext4_ext_get_actual_len(ex); |
---|
3755 | 3727 | |
---|
3756 | | - ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
---|
3757 | | - "block %llu, max_blocks %u\n", inode->i_ino, |
---|
| 3728 | + ext_debug(inode, "logical block %llu, max_blocks %u\n", |
---|
3758 | 3729 | (unsigned long long)ee_block, ee_len); |
---|
3759 | 3730 | |
---|
3760 | 3731 | /* If extent is larger than requested it is a clear sign that we still |
---|
.. | .. |
---|
3799 | 3770 | return err; |
---|
3800 | 3771 | } |
---|
3801 | 3772 | |
---|
3802 | | -/* |
---|
3803 | | - * Handle EOFBLOCKS_FL flag, clearing it if necessary |
---|
3804 | | - */ |
---|
3805 | | -static int check_eofblocks_fl(handle_t *handle, struct inode *inode, |
---|
3806 | | - ext4_lblk_t lblk, |
---|
3807 | | - struct ext4_ext_path *path, |
---|
3808 | | - unsigned int len) |
---|
3809 | | -{ |
---|
3810 | | - int i, depth; |
---|
3811 | | - struct ext4_extent_header *eh; |
---|
3812 | | - struct ext4_extent *last_ex; |
---|
3813 | | - |
---|
3814 | | - if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) |
---|
3815 | | - return 0; |
---|
3816 | | - |
---|
3817 | | - depth = ext_depth(inode); |
---|
3818 | | - eh = path[depth].p_hdr; |
---|
3819 | | - |
---|
3820 | | - /* |
---|
3821 | | - * We're going to remove EOFBLOCKS_FL entirely in future so we |
---|
3822 | | - * do not care for this case anymore. Simply remove the flag |
---|
3823 | | - * if there are no extents. |
---|
3824 | | - */ |
---|
3825 | | - if (unlikely(!eh->eh_entries)) |
---|
3826 | | - goto out; |
---|
3827 | | - last_ex = EXT_LAST_EXTENT(eh); |
---|
3828 | | - /* |
---|
3829 | | - * We should clear the EOFBLOCKS_FL flag if we are writing the |
---|
3830 | | - * last block in the last extent in the file. We test this by |
---|
3831 | | - * first checking to see if the caller to |
---|
3832 | | - * ext4_ext_get_blocks() was interested in the last block (or |
---|
3833 | | - * a block beyond the last block) in the current extent. If |
---|
3834 | | - * this turns out to be false, we can bail out from this |
---|
3835 | | - * function immediately. |
---|
3836 | | - */ |
---|
3837 | | - if (lblk + len < le32_to_cpu(last_ex->ee_block) + |
---|
3838 | | - ext4_ext_get_actual_len(last_ex)) |
---|
3839 | | - return 0; |
---|
3840 | | - /* |
---|
3841 | | - * If the caller does appear to be planning to write at or |
---|
3842 | | - * beyond the end of the current extent, we then test to see |
---|
3843 | | - * if the current extent is the last extent in the file, by |
---|
3844 | | - * checking to make sure it was reached via the rightmost node |
---|
3845 | | - * at each level of the tree. |
---|
3846 | | - */ |
---|
3847 | | - for (i = depth-1; i >= 0; i--) |
---|
3848 | | - if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) |
---|
3849 | | - return 0; |
---|
3850 | | -out: |
---|
3851 | | - ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
---|
3852 | | - return ext4_mark_inode_dirty(handle, inode); |
---|
3853 | | -} |
---|
3854 | | - |
---|
3855 | | -/** |
---|
3856 | | - * ext4_find_delalloc_range: find delayed allocated block in the given range. |
---|
3857 | | - * |
---|
3858 | | - * Return 1 if there is a delalloc block in the range, otherwise 0. |
---|
3859 | | - */ |
---|
3860 | | -int ext4_find_delalloc_range(struct inode *inode, |
---|
3861 | | - ext4_lblk_t lblk_start, |
---|
3862 | | - ext4_lblk_t lblk_end) |
---|
3863 | | -{ |
---|
3864 | | - struct extent_status es; |
---|
3865 | | - |
---|
3866 | | - ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es); |
---|
3867 | | - if (es.es_len == 0) |
---|
3868 | | - return 0; /* there is no delay extent in this tree */ |
---|
3869 | | - else if (es.es_lblk <= lblk_start && |
---|
3870 | | - lblk_start < es.es_lblk + es.es_len) |
---|
3871 | | - return 1; |
---|
3872 | | - else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end) |
---|
3873 | | - return 1; |
---|
3874 | | - else |
---|
3875 | | - return 0; |
---|
3876 | | -} |
---|
3877 | | - |
---|
3878 | | -int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk) |
---|
3879 | | -{ |
---|
3880 | | - struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
3881 | | - ext4_lblk_t lblk_start, lblk_end; |
---|
3882 | | - lblk_start = EXT4_LBLK_CMASK(sbi, lblk); |
---|
3883 | | - lblk_end = lblk_start + sbi->s_cluster_ratio - 1; |
---|
3884 | | - |
---|
3885 | | - return ext4_find_delalloc_range(inode, lblk_start, lblk_end); |
---|
3886 | | -} |
---|
3887 | | - |
---|
3888 | | -/** |
---|
3889 | | - * Determines how many complete clusters (out of those specified by the 'map') |
---|
3890 | | - * are under delalloc and were reserved quota for. |
---|
3891 | | - * This function is called when we are writing out the blocks that were |
---|
3892 | | - * originally written with their allocation delayed, but then the space was |
---|
3893 | | - * allocated using fallocate() before the delayed allocation could be resolved. |
---|
3894 | | - * The cases to look for are: |
---|
3895 | | - * ('=' indicated delayed allocated blocks |
---|
3896 | | - * '-' indicates non-delayed allocated blocks) |
---|
3897 | | - * (a) partial clusters towards beginning and/or end outside of allocated range |
---|
3898 | | - * are not delalloc'ed. |
---|
3899 | | - * Ex: |
---|
3900 | | - * |----c---=|====c====|====c====|===-c----| |
---|
3901 | | - * |++++++ allocated ++++++| |
---|
3902 | | - * ==> 4 complete clusters in above example |
---|
3903 | | - * |
---|
3904 | | - * (b) partial cluster (outside of allocated range) towards either end is |
---|
3905 | | - * marked for delayed allocation. In this case, we will exclude that |
---|
3906 | | - * cluster. |
---|
3907 | | - * Ex: |
---|
3908 | | - * |----====c========|========c========| |
---|
3909 | | - * |++++++ allocated ++++++| |
---|
3910 | | - * ==> 1 complete clusters in above example |
---|
3911 | | - * |
---|
3912 | | - * Ex: |
---|
3913 | | - * |================c================| |
---|
3914 | | - * |++++++ allocated ++++++| |
---|
3915 | | - * ==> 0 complete clusters in above example |
---|
3916 | | - * |
---|
3917 | | - * The ext4_da_update_reserve_space will be called only if we |
---|
3918 | | - * determine here that there were some "entire" clusters that span |
---|
3919 | | - * this 'allocated' range. |
---|
3920 | | - * In the non-bigalloc case, this function will just end up returning num_blks |
---|
3921 | | - * without ever calling ext4_find_delalloc_range. |
---|
3922 | | - */ |
---|
3923 | | -static unsigned int |
---|
3924 | | -get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start, |
---|
3925 | | - unsigned int num_blks) |
---|
3926 | | -{ |
---|
3927 | | - struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
3928 | | - ext4_lblk_t alloc_cluster_start, alloc_cluster_end; |
---|
3929 | | - ext4_lblk_t lblk_from, lblk_to, c_offset; |
---|
3930 | | - unsigned int allocated_clusters = 0; |
---|
3931 | | - |
---|
3932 | | - alloc_cluster_start = EXT4_B2C(sbi, lblk_start); |
---|
3933 | | - alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1); |
---|
3934 | | - |
---|
3935 | | - /* max possible clusters for this allocation */ |
---|
3936 | | - allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1; |
---|
3937 | | - |
---|
3938 | | - trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks); |
---|
3939 | | - |
---|
3940 | | - /* Check towards left side */ |
---|
3941 | | - c_offset = EXT4_LBLK_COFF(sbi, lblk_start); |
---|
3942 | | - if (c_offset) { |
---|
3943 | | - lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start); |
---|
3944 | | - lblk_to = lblk_from + c_offset - 1; |
---|
3945 | | - |
---|
3946 | | - if (ext4_find_delalloc_range(inode, lblk_from, lblk_to)) |
---|
3947 | | - allocated_clusters--; |
---|
3948 | | - } |
---|
3949 | | - |
---|
3950 | | - /* Now check towards right. */ |
---|
3951 | | - c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks); |
---|
3952 | | - if (allocated_clusters && c_offset) { |
---|
3953 | | - lblk_from = lblk_start + num_blks; |
---|
3954 | | - lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1; |
---|
3955 | | - |
---|
3956 | | - if (ext4_find_delalloc_range(inode, lblk_from, lblk_to)) |
---|
3957 | | - allocated_clusters--; |
---|
3958 | | - } |
---|
3959 | | - |
---|
3960 | | - return allocated_clusters; |
---|
3961 | | -} |
---|
3962 | | - |
---|
3963 | 3773 | static int |
---|
3964 | 3774 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
---|
3965 | 3775 | struct ext4_map_blocks *map, |
---|
3966 | 3776 | struct ext4_ext_path **ppath, |
---|
3967 | | - unsigned int allocated) |
---|
| 3777 | + unsigned int *allocated) |
---|
3968 | 3778 | { |
---|
3969 | 3779 | struct ext4_ext_path *path = *ppath; |
---|
3970 | 3780 | struct ext4_extent *ex; |
---|
.. | .. |
---|
3985 | 3795 | ee_block = le32_to_cpu(ex->ee_block); |
---|
3986 | 3796 | ee_len = ext4_ext_get_actual_len(ex); |
---|
3987 | 3797 | |
---|
3988 | | - ext_debug("%s: inode %lu, logical" |
---|
3989 | | - "block %llu, max_blocks %u\n", __func__, inode->i_ino, |
---|
| 3798 | + ext_debug(inode, "logical block %llu, max_blocks %u\n", |
---|
3990 | 3799 | (unsigned long long)ee_block, ee_len); |
---|
3991 | 3800 | |
---|
3992 | 3801 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
---|
.. | .. |
---|
4024 | 3833 | ext4_ext_show_leaf(inode, path); |
---|
4025 | 3834 | |
---|
4026 | 3835 | ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
4027 | | - err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len); |
---|
4028 | | - if (err) |
---|
4029 | | - return err; |
---|
| 3836 | + |
---|
4030 | 3837 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
---|
4031 | | - if (allocated > map->m_len) |
---|
4032 | | - allocated = map->m_len; |
---|
4033 | | - map->m_len = allocated; |
---|
4034 | | - return allocated; |
---|
| 3838 | + if (*allocated > map->m_len) |
---|
| 3839 | + *allocated = map->m_len; |
---|
| 3840 | + map->m_len = *allocated; |
---|
| 3841 | + return 0; |
---|
4035 | 3842 | } |
---|
4036 | 3843 | |
---|
4037 | 3844 | static int |
---|
.. | .. |
---|
4040 | 3847 | struct ext4_ext_path **ppath, int flags, |
---|
4041 | 3848 | unsigned int allocated, ext4_fsblk_t newblock) |
---|
4042 | 3849 | { |
---|
4043 | | - struct ext4_ext_path *path = *ppath; |
---|
| 3850 | + struct ext4_ext_path __maybe_unused *path = *ppath; |
---|
4044 | 3851 | int ret = 0; |
---|
4045 | 3852 | int err = 0; |
---|
4046 | 3853 | |
---|
4047 | | - ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical " |
---|
4048 | | - "block %llu, max_blocks %u, flags %x, allocated %u\n", |
---|
4049 | | - inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
---|
4050 | | - flags, allocated); |
---|
| 3854 | + ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n", |
---|
| 3855 | + (unsigned long long)map->m_lblk, map->m_len, flags, |
---|
| 3856 | + allocated); |
---|
4051 | 3857 | ext4_ext_show_leaf(inode, path); |
---|
4052 | 3858 | |
---|
4053 | 3859 | /* |
---|
.. | .. |
---|
4059 | 3865 | trace_ext4_ext_handle_unwritten_extents(inode, map, flags, |
---|
4060 | 3866 | allocated, newblock); |
---|
4061 | 3867 | |
---|
4062 | | - /* get_block() before submit the IO, split the extent */ |
---|
| 3868 | + /* get_block() before submitting IO, split the extent */ |
---|
4063 | 3869 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
---|
4064 | 3870 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
---|
4065 | 3871 | flags | EXT4_GET_BLOCKS_CONVERT); |
---|
4066 | | - if (ret <= 0) |
---|
4067 | | - goto out; |
---|
| 3872 | + if (ret < 0) { |
---|
| 3873 | + err = ret; |
---|
| 3874 | + goto out2; |
---|
| 3875 | + } |
---|
| 3876 | + /* |
---|
| 3877 | + * shouldn't get a 0 return when splitting an extent unless |
---|
| 3878 | + * m_len is 0 (bug) or extent has been corrupted |
---|
| 3879 | + */ |
---|
| 3880 | + if (unlikely(ret == 0)) { |
---|
| 3881 | + EXT4_ERROR_INODE(inode, |
---|
| 3882 | + "unexpected ret == 0, m_len = %u", |
---|
| 3883 | + map->m_len); |
---|
| 3884 | + err = -EFSCORRUPTED; |
---|
| 3885 | + goto out2; |
---|
| 3886 | + } |
---|
4068 | 3887 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
---|
4069 | 3888 | goto out; |
---|
4070 | 3889 | } |
---|
4071 | 3890 | /* IO end_io complete, convert the filled extent to written */ |
---|
4072 | 3891 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
---|
4073 | | - if (flags & EXT4_GET_BLOCKS_ZERO) { |
---|
4074 | | - if (allocated > map->m_len) |
---|
4075 | | - allocated = map->m_len; |
---|
4076 | | - err = ext4_issue_zeroout(inode, map->m_lblk, newblock, |
---|
4077 | | - allocated); |
---|
4078 | | - if (err < 0) |
---|
4079 | | - goto out2; |
---|
4080 | | - } |
---|
4081 | | - ret = ext4_convert_unwritten_extents_endio(handle, inode, map, |
---|
| 3892 | + err = ext4_convert_unwritten_extents_endio(handle, inode, map, |
---|
4082 | 3893 | ppath); |
---|
4083 | | - if (ret >= 0) { |
---|
4084 | | - ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
4085 | | - err = check_eofblocks_fl(handle, inode, map->m_lblk, |
---|
4086 | | - path, map->m_len); |
---|
4087 | | - } else |
---|
4088 | | - err = ret; |
---|
4089 | | - map->m_flags |= EXT4_MAP_MAPPED; |
---|
4090 | | - map->m_pblk = newblock; |
---|
4091 | | - if (allocated > map->m_len) |
---|
4092 | | - allocated = map->m_len; |
---|
4093 | | - map->m_len = allocated; |
---|
4094 | | - goto out2; |
---|
| 3894 | + if (err < 0) |
---|
| 3895 | + goto out2; |
---|
| 3896 | + ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
| 3897 | + goto map_out; |
---|
4095 | 3898 | } |
---|
4096 | | - /* buffered IO case */ |
---|
| 3899 | + /* buffered IO cases */ |
---|
4097 | 3900 | /* |
---|
4098 | 3901 | * repeat fallocate creation request |
---|
4099 | 3902 | * we already have an unwritten extent |
---|
.. | .. |
---|
4116 | 3919 | goto out1; |
---|
4117 | 3920 | } |
---|
4118 | 3921 | |
---|
4119 | | - /* buffered write, writepage time, convert*/ |
---|
| 3922 | + /* |
---|
| 3923 | + * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1. |
---|
| 3924 | + * For buffered writes, at writepage time, etc. Convert a |
---|
| 3925 | + * discovered unwritten extent to written. |
---|
| 3926 | + */ |
---|
4120 | 3927 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
---|
4121 | | - if (ret >= 0) |
---|
4122 | | - ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
4123 | | -out: |
---|
4124 | | - if (ret <= 0) { |
---|
| 3928 | + if (ret < 0) { |
---|
4125 | 3929 | err = ret; |
---|
4126 | 3930 | goto out2; |
---|
4127 | | - } else |
---|
4128 | | - allocated = ret; |
---|
| 3931 | + } |
---|
| 3932 | + ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
| 3933 | + /* |
---|
| 3934 | + * shouldn't get a 0 return when converting an unwritten extent |
---|
| 3935 | + * unless m_len is 0 (bug) or extent has been corrupted |
---|
| 3936 | + */ |
---|
| 3937 | + if (unlikely(ret == 0)) { |
---|
| 3938 | + EXT4_ERROR_INODE(inode, "unexpected ret == 0, m_len = %u", |
---|
| 3939 | + map->m_len); |
---|
| 3940 | + err = -EFSCORRUPTED; |
---|
| 3941 | + goto out2; |
---|
| 3942 | + } |
---|
| 3943 | + |
---|
| 3944 | +out: |
---|
| 3945 | + allocated = ret; |
---|
4129 | 3946 | map->m_flags |= EXT4_MAP_NEW; |
---|
4130 | | - /* |
---|
4131 | | - * if we allocated more blocks than requested |
---|
4132 | | - * we need to make sure we unmap the extra block |
---|
4133 | | - * allocated. The actual needed block will get |
---|
4134 | | - * unmapped later when we find the buffer_head marked |
---|
4135 | | - * new. |
---|
4136 | | - */ |
---|
4137 | | - if (allocated > map->m_len) { |
---|
4138 | | - clean_bdev_aliases(inode->i_sb->s_bdev, newblock + map->m_len, |
---|
4139 | | - allocated - map->m_len); |
---|
4140 | | - allocated = map->m_len; |
---|
4141 | | - } |
---|
4142 | | - map->m_len = allocated; |
---|
4143 | | - |
---|
4144 | | - /* |
---|
4145 | | - * If we have done fallocate with the offset that is already |
---|
4146 | | - * delayed allocated, we would have block reservation |
---|
4147 | | - * and quota reservation done in the delayed write path. |
---|
4148 | | - * But fallocate would have already updated quota and block |
---|
4149 | | - * count for this offset. So cancel these reservation |
---|
4150 | | - */ |
---|
4151 | | - if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
---|
4152 | | - unsigned int reserved_clusters; |
---|
4153 | | - reserved_clusters = get_reserved_cluster_alloc(inode, |
---|
4154 | | - map->m_lblk, map->m_len); |
---|
4155 | | - if (reserved_clusters) |
---|
4156 | | - ext4_da_update_reserve_space(inode, |
---|
4157 | | - reserved_clusters, |
---|
4158 | | - 0); |
---|
4159 | | - } |
---|
4160 | | - |
---|
4161 | 3947 | map_out: |
---|
4162 | 3948 | map->m_flags |= EXT4_MAP_MAPPED; |
---|
4163 | | - if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) { |
---|
4164 | | - err = check_eofblocks_fl(handle, inode, map->m_lblk, path, |
---|
4165 | | - map->m_len); |
---|
4166 | | - if (err < 0) |
---|
4167 | | - goto out2; |
---|
4168 | | - } |
---|
4169 | 3949 | out1: |
---|
| 3950 | + map->m_pblk = newblock; |
---|
4170 | 3951 | if (allocated > map->m_len) |
---|
4171 | 3952 | allocated = map->m_len; |
---|
4172 | | - ext4_ext_show_leaf(inode, path); |
---|
4173 | | - map->m_pblk = newblock; |
---|
4174 | 3953 | map->m_len = allocated; |
---|
| 3954 | + ext4_ext_show_leaf(inode, path); |
---|
4175 | 3955 | out2: |
---|
4176 | 3956 | return err ? err : allocated; |
---|
4177 | 3957 | } |
---|
.. | .. |
---|
4287 | 4067 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
---|
4288 | 4068 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) |
---|
4289 | 4069 | * |
---|
4290 | | - * return > 0, number of of blocks already mapped/allocated |
---|
| 4070 | + * return > 0, number of blocks already mapped/allocated |
---|
4291 | 4071 | * if create == 0 and these are pre-allocated blocks |
---|
4292 | 4072 | * buffer head is unmapped |
---|
4293 | 4073 | * otherwise blocks are mapped |
---|
.. | .. |
---|
4301 | 4081 | struct ext4_map_blocks *map, int flags) |
---|
4302 | 4082 | { |
---|
4303 | 4083 | struct ext4_ext_path *path = NULL; |
---|
4304 | | - struct ext4_extent newex, *ex, *ex2; |
---|
| 4084 | + struct ext4_extent newex, *ex, ex2; |
---|
4305 | 4085 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
4306 | | - ext4_fsblk_t newblock = 0; |
---|
4307 | | - int free_on_err = 0, err = 0, depth, ret; |
---|
| 4086 | + ext4_fsblk_t newblock = 0, pblk; |
---|
| 4087 | + int err = 0, depth, ret; |
---|
4308 | 4088 | unsigned int allocated = 0, offset = 0; |
---|
4309 | 4089 | unsigned int allocated_clusters = 0; |
---|
4310 | 4090 | struct ext4_allocation_request ar; |
---|
4311 | 4091 | ext4_lblk_t cluster_offset; |
---|
4312 | | - bool map_from_cluster = false; |
---|
4313 | 4092 | |
---|
4314 | | - ext_debug("blocks %u/%u requested for inode %lu\n", |
---|
4315 | | - map->m_lblk, map->m_len, inode->i_ino); |
---|
| 4093 | + ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len); |
---|
4316 | 4094 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); |
---|
4317 | 4095 | |
---|
4318 | 4096 | /* find extent for this block */ |
---|
.. | .. |
---|
4320 | 4098 | if (IS_ERR(path)) { |
---|
4321 | 4099 | err = PTR_ERR(path); |
---|
4322 | 4100 | path = NULL; |
---|
4323 | | - goto out2; |
---|
| 4101 | + goto out; |
---|
4324 | 4102 | } |
---|
4325 | 4103 | |
---|
4326 | 4104 | depth = ext_depth(inode); |
---|
.. | .. |
---|
4336 | 4114 | (unsigned long) map->m_lblk, depth, |
---|
4337 | 4115 | path[depth].p_block); |
---|
4338 | 4116 | err = -EFSCORRUPTED; |
---|
4339 | | - goto out2; |
---|
| 4117 | + goto out; |
---|
4340 | 4118 | } |
---|
4341 | 4119 | |
---|
4342 | 4120 | ex = path[depth].p_ext; |
---|
.. | .. |
---|
4359 | 4137 | newblock = map->m_lblk - ee_block + ee_start; |
---|
4360 | 4138 | /* number of remaining blocks in the extent */ |
---|
4361 | 4139 | allocated = ee_len - (map->m_lblk - ee_block); |
---|
4362 | | - ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
---|
4363 | | - ee_block, ee_len, newblock); |
---|
| 4140 | + ext_debug(inode, "%u fit into %u:%d -> %llu\n", |
---|
| 4141 | + map->m_lblk, ee_block, ee_len, newblock); |
---|
4364 | 4142 | |
---|
4365 | 4143 | /* |
---|
4366 | 4144 | * If the extent is initialized check whether the |
---|
.. | .. |
---|
4368 | 4146 | */ |
---|
4369 | 4147 | if ((!ext4_ext_is_unwritten(ex)) && |
---|
4370 | 4148 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
---|
4371 | | - allocated = convert_initialized_extent( |
---|
4372 | | - handle, inode, map, &path, |
---|
4373 | | - allocated); |
---|
4374 | | - goto out2; |
---|
4375 | | - } else if (!ext4_ext_is_unwritten(ex)) |
---|
| 4149 | + err = convert_initialized_extent(handle, |
---|
| 4150 | + inode, map, &path, &allocated); |
---|
4376 | 4151 | goto out; |
---|
| 4152 | + } else if (!ext4_ext_is_unwritten(ex)) { |
---|
| 4153 | + map->m_flags |= EXT4_MAP_MAPPED; |
---|
| 4154 | + map->m_pblk = newblock; |
---|
| 4155 | + if (allocated > map->m_len) |
---|
| 4156 | + allocated = map->m_len; |
---|
| 4157 | + map->m_len = allocated; |
---|
| 4158 | + ext4_ext_show_leaf(inode, path); |
---|
| 4159 | + goto out; |
---|
| 4160 | + } |
---|
4377 | 4161 | |
---|
4378 | 4162 | ret = ext4_ext_handle_unwritten_extents( |
---|
4379 | 4163 | handle, inode, map, &path, flags, |
---|
.. | .. |
---|
4382 | 4166 | err = ret; |
---|
4383 | 4167 | else |
---|
4384 | 4168 | allocated = ret; |
---|
4385 | | - goto out2; |
---|
| 4169 | + goto out; |
---|
4386 | 4170 | } |
---|
4387 | 4171 | } |
---|
4388 | 4172 | |
---|
.. | .. |
---|
4407 | 4191 | map->m_pblk = 0; |
---|
4408 | 4192 | map->m_len = min_t(unsigned int, map->m_len, hole_len); |
---|
4409 | 4193 | |
---|
4410 | | - goto out2; |
---|
| 4194 | + goto out; |
---|
4411 | 4195 | } |
---|
4412 | 4196 | |
---|
4413 | 4197 | /* |
---|
.. | .. |
---|
4424 | 4208 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
---|
4425 | 4209 | ar.len = allocated = map->m_len; |
---|
4426 | 4210 | newblock = map->m_pblk; |
---|
4427 | | - map_from_cluster = true; |
---|
4428 | 4211 | goto got_allocated_blocks; |
---|
4429 | 4212 | } |
---|
4430 | 4213 | |
---|
.. | .. |
---|
4432 | 4215 | ar.lleft = map->m_lblk; |
---|
4433 | 4216 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
---|
4434 | 4217 | if (err) |
---|
4435 | | - goto out2; |
---|
| 4218 | + goto out; |
---|
4436 | 4219 | ar.lright = map->m_lblk; |
---|
4437 | | - ex2 = NULL; |
---|
4438 | 4220 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
---|
4439 | | - if (err) |
---|
4440 | | - goto out2; |
---|
| 4221 | + if (err < 0) |
---|
| 4222 | + goto out; |
---|
4441 | 4223 | |
---|
4442 | 4224 | /* Check if the extent after searching to the right implies a |
---|
4443 | 4225 | * cluster we can use. */ |
---|
4444 | | - if ((sbi->s_cluster_ratio > 1) && ex2 && |
---|
4445 | | - get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
---|
| 4226 | + if ((sbi->s_cluster_ratio > 1) && err && |
---|
| 4227 | + get_implied_cluster_alloc(inode->i_sb, map, &ex2, path)) { |
---|
4446 | 4228 | ar.len = allocated = map->m_len; |
---|
4447 | 4229 | newblock = map->m_pblk; |
---|
4448 | | - map_from_cluster = true; |
---|
4449 | 4230 | goto got_allocated_blocks; |
---|
4450 | 4231 | } |
---|
4451 | 4232 | |
---|
.. | .. |
---|
4499 | 4280 | ar.flags |= EXT4_MB_USE_RESERVED; |
---|
4500 | 4281 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
---|
4501 | 4282 | if (!newblock) |
---|
4502 | | - goto out2; |
---|
4503 | | - ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
---|
4504 | | - ar.goal, newblock, allocated); |
---|
4505 | | - free_on_err = 1; |
---|
| 4283 | + goto out; |
---|
4506 | 4284 | allocated_clusters = ar.len; |
---|
4507 | 4285 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
---|
| 4286 | + ext_debug(inode, "allocate new block: goal %llu, found %llu/%u, requested %u\n", |
---|
| 4287 | + ar.goal, newblock, ar.len, allocated); |
---|
4508 | 4288 | if (ar.len > allocated) |
---|
4509 | 4289 | ar.len = allocated; |
---|
4510 | 4290 | |
---|
4511 | 4291 | got_allocated_blocks: |
---|
4512 | 4292 | /* try to insert new extent into found leaf and return */ |
---|
4513 | | - ext4_ext_store_pblock(&newex, newblock + offset); |
---|
| 4293 | + pblk = newblock + offset; |
---|
| 4294 | + ext4_ext_store_pblock(&newex, pblk); |
---|
4514 | 4295 | newex.ee_len = cpu_to_le16(ar.len); |
---|
4515 | 4296 | /* Mark unwritten */ |
---|
4516 | | - if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){ |
---|
| 4297 | + if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
---|
4517 | 4298 | ext4_ext_mark_unwritten(&newex); |
---|
4518 | 4299 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
---|
4519 | 4300 | } |
---|
4520 | 4301 | |
---|
4521 | | - err = 0; |
---|
4522 | | - if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) |
---|
4523 | | - err = check_eofblocks_fl(handle, inode, map->m_lblk, |
---|
4524 | | - path, ar.len); |
---|
4525 | | - if (!err) |
---|
4526 | | - err = ext4_ext_insert_extent(handle, inode, &path, |
---|
4527 | | - &newex, flags); |
---|
| 4302 | + err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags); |
---|
| 4303 | + if (err) { |
---|
| 4304 | + if (allocated_clusters) { |
---|
| 4305 | + int fb_flags = 0; |
---|
4528 | 4306 | |
---|
4529 | | - if (err && free_on_err) { |
---|
4530 | | - int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? |
---|
4531 | | - EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; |
---|
4532 | | - /* free data blocks we just allocated */ |
---|
4533 | | - /* not a good idea to call discard here directly, |
---|
4534 | | - * but otherwise we'd need to call it every free() */ |
---|
4535 | | - ext4_discard_preallocations(inode); |
---|
4536 | | - ext4_free_blocks(handle, inode, NULL, newblock, |
---|
4537 | | - EXT4_C2B(sbi, allocated_clusters), fb_flags); |
---|
4538 | | - goto out2; |
---|
| 4307 | + /* |
---|
| 4308 | + * free data blocks we just allocated. |
---|
| 4309 | + * not a good idea to call discard here directly, |
---|
| 4310 | + * but otherwise we'd need to call it every free(). |
---|
| 4311 | + */ |
---|
| 4312 | + ext4_discard_preallocations(inode, 0); |
---|
| 4313 | + if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
---|
| 4314 | + fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; |
---|
| 4315 | + ext4_free_blocks(handle, inode, NULL, newblock, |
---|
| 4316 | + EXT4_C2B(sbi, allocated_clusters), |
---|
| 4317 | + fb_flags); |
---|
| 4318 | + } |
---|
| 4319 | + goto out; |
---|
4539 | 4320 | } |
---|
4540 | 4321 | |
---|
4541 | | - /* previous routine could use block we allocated */ |
---|
4542 | | - newblock = ext4_ext_pblock(&newex); |
---|
4543 | | - allocated = ext4_ext_get_actual_len(&newex); |
---|
4544 | | - if (allocated > map->m_len) |
---|
4545 | | - allocated = map->m_len; |
---|
4546 | | - map->m_flags |= EXT4_MAP_NEW; |
---|
4547 | | - |
---|
4548 | 4322 | /* |
---|
4549 | | - * Update reserved blocks/metadata blocks after successful |
---|
4550 | | - * block allocation which had been deferred till now. |
---|
| 4323 | + * Reduce the reserved cluster count to reflect successful deferred |
---|
| 4324 | + * allocation of delayed allocated clusters or direct allocation of |
---|
| 4325 | + * clusters discovered to be delayed allocated. Once allocated, a |
---|
| 4326 | + * cluster is not included in the reserved count. |
---|
4551 | 4327 | */ |
---|
4552 | | - if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
---|
4553 | | - unsigned int reserved_clusters; |
---|
4554 | | - /* |
---|
4555 | | - * Check how many clusters we had reserved this allocated range |
---|
4556 | | - */ |
---|
4557 | | - reserved_clusters = get_reserved_cluster_alloc(inode, |
---|
4558 | | - map->m_lblk, allocated); |
---|
4559 | | - if (!map_from_cluster) { |
---|
4560 | | - BUG_ON(allocated_clusters < reserved_clusters); |
---|
4561 | | - if (reserved_clusters < allocated_clusters) { |
---|
4562 | | - struct ext4_inode_info *ei = EXT4_I(inode); |
---|
4563 | | - int reservation = allocated_clusters - |
---|
4564 | | - reserved_clusters; |
---|
4565 | | - /* |
---|
4566 | | - * It seems we claimed few clusters outside of |
---|
4567 | | - * the range of this allocation. We should give |
---|
4568 | | - * it back to the reservation pool. This can |
---|
4569 | | - * happen in the following case: |
---|
4570 | | - * |
---|
4571 | | - * * Suppose s_cluster_ratio is 4 (i.e., each |
---|
4572 | | - * cluster has 4 blocks. Thus, the clusters |
---|
4573 | | - * are [0-3],[4-7],[8-11]... |
---|
4574 | | - * * First comes delayed allocation write for |
---|
4575 | | - * logical blocks 10 & 11. Since there were no |
---|
4576 | | - * previous delayed allocated blocks in the |
---|
4577 | | - * range [8-11], we would reserve 1 cluster |
---|
4578 | | - * for this write. |
---|
4579 | | - * * Next comes write for logical blocks 3 to 8. |
---|
4580 | | - * In this case, we will reserve 2 clusters |
---|
4581 | | - * (for [0-3] and [4-7]; and not for [8-11] as |
---|
4582 | | - * that range has a delayed allocated blocks. |
---|
4583 | | - * Thus total reserved clusters now becomes 3. |
---|
4584 | | - * * Now, during the delayed allocation writeout |
---|
4585 | | - * time, we will first write blocks [3-8] and |
---|
4586 | | - * allocate 3 clusters for writing these |
---|
4587 | | - * blocks. Also, we would claim all these |
---|
4588 | | - * three clusters above. |
---|
4589 | | - * * Now when we come here to writeout the |
---|
4590 | | - * blocks [10-11], we would expect to claim |
---|
4591 | | - * the reservation of 1 cluster we had made |
---|
4592 | | - * (and we would claim it since there are no |
---|
4593 | | - * more delayed allocated blocks in the range |
---|
4594 | | - * [8-11]. But our reserved cluster count had |
---|
4595 | | - * already gone to 0. |
---|
4596 | | - * |
---|
4597 | | - * Thus, at the step 4 above when we determine |
---|
4598 | | - * that there are still some unwritten delayed |
---|
4599 | | - * allocated blocks outside of our current |
---|
4600 | | - * block range, we should increment the |
---|
4601 | | - * reserved clusters count so that when the |
---|
4602 | | - * remaining blocks finally gets written, we |
---|
4603 | | - * could claim them. |
---|
4604 | | - */ |
---|
4605 | | - dquot_reserve_block(inode, |
---|
4606 | | - EXT4_C2B(sbi, reservation)); |
---|
4607 | | - spin_lock(&ei->i_block_reservation_lock); |
---|
4608 | | - ei->i_reserved_data_blocks += reservation; |
---|
4609 | | - spin_unlock(&ei->i_block_reservation_lock); |
---|
4610 | | - } |
---|
| 4328 | + if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) { |
---|
| 4329 | + if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
---|
4611 | 4330 | /* |
---|
4612 | | - * We will claim quota for all newly allocated blocks. |
---|
4613 | | - * We're updating the reserved space *after* the |
---|
4614 | | - * correction above so we do not accidentally free |
---|
4615 | | - * all the metadata reservation because we might |
---|
4616 | | - * actually need it later on. |
---|
| 4331 | + * When allocating delayed allocated clusters, simply |
---|
| 4332 | + * reduce the reserved cluster count and claim quota |
---|
4617 | 4333 | */ |
---|
4618 | 4334 | ext4_da_update_reserve_space(inode, allocated_clusters, |
---|
4619 | 4335 | 1); |
---|
| 4336 | + } else { |
---|
| 4337 | + ext4_lblk_t lblk, len; |
---|
| 4338 | + unsigned int n; |
---|
| 4339 | + |
---|
| 4340 | + /* |
---|
| 4341 | + * When allocating non-delayed allocated clusters |
---|
| 4342 | + * (from fallocate, filemap, DIO, or clusters |
---|
| 4343 | + * allocated when delalloc has been disabled by |
---|
| 4344 | + * ext4_nonda_switch), reduce the reserved cluster |
---|
| 4345 | + * count by the number of allocated clusters that |
---|
| 4346 | + * have previously been delayed allocated. Quota |
---|
| 4347 | + * has been claimed by ext4_mb_new_blocks() above, |
---|
| 4348 | + * so release the quota reservations made for any |
---|
| 4349 | + * previously delayed allocated clusters. |
---|
| 4350 | + */ |
---|
| 4351 | + lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk); |
---|
| 4352 | + len = allocated_clusters << sbi->s_cluster_bits; |
---|
| 4353 | + n = ext4_es_delayed_clu(inode, lblk, len); |
---|
| 4354 | + if (n > 0) |
---|
| 4355 | + ext4_da_update_reserve_space(inode, (int) n, 0); |
---|
4620 | 4356 | } |
---|
4621 | 4357 | } |
---|
4622 | 4358 | |
---|
.. | .. |
---|
4628 | 4364 | ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
4629 | 4365 | else |
---|
4630 | 4366 | ext4_update_inode_fsync_trans(handle, inode, 0); |
---|
4631 | | -out: |
---|
4632 | | - if (allocated > map->m_len) |
---|
4633 | | - allocated = map->m_len; |
---|
| 4367 | + |
---|
| 4368 | + map->m_flags |= (EXT4_MAP_NEW | EXT4_MAP_MAPPED); |
---|
| 4369 | + map->m_pblk = pblk; |
---|
| 4370 | + map->m_len = ar.len; |
---|
| 4371 | + allocated = map->m_len; |
---|
4634 | 4372 | ext4_ext_show_leaf(inode, path); |
---|
4635 | | - map->m_flags |= EXT4_MAP_MAPPED; |
---|
4636 | | - map->m_pblk = newblock; |
---|
4637 | | - map->m_len = allocated; |
---|
4638 | | -out2: |
---|
| 4373 | +out: |
---|
4639 | 4374 | ext4_ext_drop_refs(path); |
---|
4640 | 4375 | kfree(path); |
---|
4641 | 4376 | |
---|
.. | .. |
---|
4674 | 4409 | } |
---|
4675 | 4410 | if (err) |
---|
4676 | 4411 | return err; |
---|
4677 | | - return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
---|
| 4412 | +retry_remove_space: |
---|
| 4413 | + err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
---|
| 4414 | + if (err == -ENOMEM) { |
---|
| 4415 | + cond_resched(); |
---|
| 4416 | + congestion_wait(BLK_RW_ASYNC, HZ/50); |
---|
| 4417 | + goto retry_remove_space; |
---|
| 4418 | + } |
---|
| 4419 | + return err; |
---|
4678 | 4420 | } |
---|
4679 | 4421 | |
---|
4680 | 4422 | static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset, |
---|
.. | .. |
---|
4684 | 4426 | struct inode *inode = file_inode(file); |
---|
4685 | 4427 | handle_t *handle; |
---|
4686 | 4428 | int ret = 0; |
---|
4687 | | - int ret2 = 0; |
---|
| 4429 | + int ret2 = 0, ret3 = 0; |
---|
4688 | 4430 | int retries = 0; |
---|
4689 | 4431 | int depth = 0; |
---|
4690 | 4432 | struct ext4_map_blocks map; |
---|
.. | .. |
---|
4743 | 4485 | epos = new_size; |
---|
4744 | 4486 | if (ext4_update_inode_size(inode, epos) & 0x1) |
---|
4745 | 4487 | inode->i_mtime = inode->i_ctime; |
---|
4746 | | - } else { |
---|
4747 | | - if (epos > inode->i_size) |
---|
4748 | | - ext4_set_inode_flag(inode, |
---|
4749 | | - EXT4_INODE_EOFBLOCKS); |
---|
4750 | 4488 | } |
---|
4751 | | - ext4_mark_inode_dirty(handle, inode); |
---|
| 4489 | + ret2 = ext4_mark_inode_dirty(handle, inode); |
---|
4752 | 4490 | ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
4753 | | - ret2 = ext4_journal_stop(handle); |
---|
4754 | | - if (ret2) |
---|
| 4491 | + ret3 = ext4_journal_stop(handle); |
---|
| 4492 | + ret2 = ret3 ? ret3 : ret2; |
---|
| 4493 | + if (unlikely(ret2)) |
---|
4755 | 4494 | break; |
---|
4756 | 4495 | } |
---|
4757 | 4496 | if (ret == -ENOSPC && |
---|
.. | .. |
---|
4762 | 4501 | |
---|
4763 | 4502 | return ret > 0 ? ret2 : ret; |
---|
4764 | 4503 | } |
---|
| 4504 | + |
---|
| 4505 | +static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len); |
---|
| 4506 | + |
---|
| 4507 | +static int ext4_insert_range(struct file *file, loff_t offset, loff_t len); |
---|
4765 | 4508 | |
---|
4766 | 4509 | static long ext4_zero_range(struct file *file, loff_t offset, |
---|
4767 | 4510 | loff_t len, int mode) |
---|
.. | .. |
---|
4780 | 4523 | |
---|
4781 | 4524 | trace_ext4_zero_range(inode, offset, len, mode); |
---|
4782 | 4525 | |
---|
4783 | | - if (!S_ISREG(inode->i_mode)) |
---|
4784 | | - return -EINVAL; |
---|
4785 | | - |
---|
4786 | 4526 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
---|
4787 | 4527 | if (ext4_should_journal_data(inode)) { |
---|
4788 | 4528 | ret = ext4_force_commit(inode->i_sb); |
---|
.. | .. |
---|
4791 | 4531 | } |
---|
4792 | 4532 | |
---|
4793 | 4533 | /* |
---|
4794 | | - * Round up offset. This is not fallocate, we neet to zero out |
---|
| 4534 | + * Round up offset. This is not fallocate, we need to zero out |
---|
4795 | 4535 | * blocks, so convert interior block aligned part of the range to |
---|
4796 | 4536 | * unwritten and possibly manually zero out unaligned parts of the |
---|
4797 | 4537 | * range. |
---|
.. | .. |
---|
4814 | 4554 | inode_lock(inode); |
---|
4815 | 4555 | |
---|
4816 | 4556 | /* |
---|
4817 | | - * Indirect files do not support unwritten extnets |
---|
| 4557 | + * Indirect files do not support unwritten extents |
---|
4818 | 4558 | */ |
---|
4819 | 4559 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
---|
4820 | 4560 | ret = -EOPNOTSUPP; |
---|
.. | .. |
---|
4822 | 4562 | } |
---|
4823 | 4563 | |
---|
4824 | 4564 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
---|
4825 | | - (offset + len > i_size_read(inode) || |
---|
| 4565 | + (offset + len > inode->i_size || |
---|
4826 | 4566 | offset + len > EXT4_I(inode)->i_disksize)) { |
---|
4827 | 4567 | new_size = offset + len; |
---|
4828 | 4568 | ret = inode_newsize_ok(inode, new_size); |
---|
.. | .. |
---|
4831 | 4571 | } |
---|
4832 | 4572 | |
---|
4833 | 4573 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
---|
4834 | | - if (mode & FALLOC_FL_KEEP_SIZE) |
---|
4835 | | - flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
---|
4836 | 4574 | |
---|
4837 | 4575 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
---|
4838 | 4576 | inode_dio_wait(inode); |
---|
| 4577 | + |
---|
| 4578 | + ret = file_modified(file); |
---|
| 4579 | + if (ret) |
---|
| 4580 | + goto out_mutex; |
---|
4839 | 4581 | |
---|
4840 | 4582 | /* Preallocate the range including the unaligned edges */ |
---|
4841 | 4583 | if (partial_begin || partial_end) { |
---|
.. | .. |
---|
4899 | 4641 | } |
---|
4900 | 4642 | |
---|
4901 | 4643 | inode->i_mtime = inode->i_ctime = current_time(inode); |
---|
4902 | | - if (new_size) { |
---|
| 4644 | + if (new_size) |
---|
4903 | 4645 | ext4_update_inode_size(inode, new_size); |
---|
4904 | | - } else { |
---|
4905 | | - /* |
---|
4906 | | - * Mark that we allocate beyond EOF so the subsequent truncate |
---|
4907 | | - * can proceed even if the new size is the same as i_size. |
---|
4908 | | - */ |
---|
4909 | | - if ((offset + len) > i_size_read(inode)) |
---|
4910 | | - ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
---|
4911 | | - } |
---|
4912 | | - ext4_mark_inode_dirty(handle, inode); |
---|
4913 | | - |
---|
| 4646 | + ret = ext4_mark_inode_dirty(handle, inode); |
---|
| 4647 | + if (unlikely(ret)) |
---|
| 4648 | + goto out_handle; |
---|
4914 | 4649 | /* Zero out partial block at the edges of the range */ |
---|
4915 | 4650 | ret = ext4_zero_partial_blocks(handle, inode, offset, len); |
---|
4916 | 4651 | if (ret >= 0) |
---|
.. | .. |
---|
4919 | 4654 | if (file->f_flags & O_SYNC) |
---|
4920 | 4655 | ext4_handle_sync(handle); |
---|
4921 | 4656 | |
---|
| 4657 | +out_handle: |
---|
4922 | 4658 | ext4_journal_stop(handle); |
---|
4923 | 4659 | out_mutex: |
---|
4924 | 4660 | inode_unlock(inode); |
---|
.. | .. |
---|
4947 | 4683 | * range since we would need to re-encrypt blocks with a |
---|
4948 | 4684 | * different IV or XTS tweak (which are based on the logical |
---|
4949 | 4685 | * block number). |
---|
4950 | | - * |
---|
4951 | | - * XXX It's not clear why zero range isn't working, but we'll |
---|
4952 | | - * leave it disabled for encrypted inodes for now. This is a |
---|
4953 | | - * bug we should fix.... |
---|
4954 | 4686 | */ |
---|
4955 | 4687 | if (IS_ENCRYPTED(inode) && |
---|
4956 | | - (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE | |
---|
4957 | | - FALLOC_FL_ZERO_RANGE))) |
---|
| 4688 | + (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
---|
4958 | 4689 | return -EOPNOTSUPP; |
---|
4959 | 4690 | |
---|
4960 | 4691 | /* Return error if mode is not supported */ |
---|
.. | .. |
---|
4963 | 4694 | FALLOC_FL_INSERT_RANGE)) |
---|
4964 | 4695 | return -EOPNOTSUPP; |
---|
4965 | 4696 | |
---|
4966 | | - if (mode & FALLOC_FL_PUNCH_HOLE) |
---|
4967 | | - return ext4_punch_hole(inode, offset, len); |
---|
4968 | | - |
---|
| 4697 | + inode_lock(inode); |
---|
4969 | 4698 | ret = ext4_convert_inline_data(inode); |
---|
| 4699 | + inode_unlock(inode); |
---|
4970 | 4700 | if (ret) |
---|
4971 | | - return ret; |
---|
| 4701 | + goto exit; |
---|
4972 | 4702 | |
---|
4973 | | - if (mode & FALLOC_FL_COLLAPSE_RANGE) |
---|
4974 | | - return ext4_collapse_range(inode, offset, len); |
---|
| 4703 | + if (mode & FALLOC_FL_PUNCH_HOLE) { |
---|
| 4704 | + ret = ext4_punch_hole(file, offset, len); |
---|
| 4705 | + goto exit; |
---|
| 4706 | + } |
---|
4975 | 4707 | |
---|
4976 | | - if (mode & FALLOC_FL_INSERT_RANGE) |
---|
4977 | | - return ext4_insert_range(inode, offset, len); |
---|
| 4708 | + if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
---|
| 4709 | + ret = ext4_collapse_range(file, offset, len); |
---|
| 4710 | + goto exit; |
---|
| 4711 | + } |
---|
4978 | 4712 | |
---|
4979 | | - if (mode & FALLOC_FL_ZERO_RANGE) |
---|
4980 | | - return ext4_zero_range(file, offset, len, mode); |
---|
| 4713 | + if (mode & FALLOC_FL_INSERT_RANGE) { |
---|
| 4714 | + ret = ext4_insert_range(file, offset, len); |
---|
| 4715 | + goto exit; |
---|
| 4716 | + } |
---|
4981 | 4717 | |
---|
| 4718 | + if (mode & FALLOC_FL_ZERO_RANGE) { |
---|
| 4719 | + ret = ext4_zero_range(file, offset, len, mode); |
---|
| 4720 | + goto exit; |
---|
| 4721 | + } |
---|
4982 | 4722 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
---|
4983 | 4723 | lblk = offset >> blkbits; |
---|
4984 | 4724 | |
---|
4985 | 4725 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
---|
4986 | 4726 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
---|
4987 | | - if (mode & FALLOC_FL_KEEP_SIZE) |
---|
4988 | | - flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
---|
4989 | 4727 | |
---|
4990 | 4728 | inode_lock(inode); |
---|
4991 | 4729 | |
---|
.. | .. |
---|
4998 | 4736 | } |
---|
4999 | 4737 | |
---|
5000 | 4738 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
---|
5001 | | - (offset + len > i_size_read(inode) || |
---|
| 4739 | + (offset + len > inode->i_size || |
---|
5002 | 4740 | offset + len > EXT4_I(inode)->i_disksize)) { |
---|
5003 | 4741 | new_size = offset + len; |
---|
5004 | 4742 | ret = inode_newsize_ok(inode, new_size); |
---|
.. | .. |
---|
5009 | 4747 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
---|
5010 | 4748 | inode_dio_wait(inode); |
---|
5011 | 4749 | |
---|
| 4750 | + ret = file_modified(file); |
---|
| 4751 | + if (ret) |
---|
| 4752 | + goto out; |
---|
| 4753 | + |
---|
5012 | 4754 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); |
---|
5013 | 4755 | if (ret) |
---|
5014 | 4756 | goto out; |
---|
5015 | 4757 | |
---|
5016 | 4758 | if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { |
---|
5017 | | - ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, |
---|
5018 | | - EXT4_I(inode)->i_sync_tid); |
---|
| 4759 | + ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, |
---|
| 4760 | + EXT4_I(inode)->i_sync_tid); |
---|
5019 | 4761 | } |
---|
5020 | 4762 | out: |
---|
5021 | 4763 | inode_unlock(inode); |
---|
5022 | 4764 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
---|
| 4765 | +exit: |
---|
5023 | 4766 | return ret; |
---|
5024 | 4767 | } |
---|
5025 | 4768 | |
---|
.. | .. |
---|
5037 | 4780 | loff_t offset, ssize_t len) |
---|
5038 | 4781 | { |
---|
5039 | 4782 | unsigned int max_blocks; |
---|
5040 | | - int ret = 0; |
---|
5041 | | - int ret2 = 0; |
---|
| 4783 | + int ret = 0, ret2 = 0, ret3 = 0; |
---|
5042 | 4784 | struct ext4_map_blocks map; |
---|
5043 | | - unsigned int credits, blkbits = inode->i_blkbits; |
---|
| 4785 | + unsigned int blkbits = inode->i_blkbits; |
---|
| 4786 | + unsigned int credits = 0; |
---|
5044 | 4787 | |
---|
5045 | 4788 | map.m_lblk = offset >> blkbits; |
---|
5046 | 4789 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
---|
5047 | 4790 | |
---|
5048 | | - /* |
---|
5049 | | - * This is somewhat ugly but the idea is clear: When transaction is |
---|
5050 | | - * reserved, everything goes into it. Otherwise we rather start several |
---|
5051 | | - * smaller transactions for conversion of each extent separately. |
---|
5052 | | - */ |
---|
5053 | | - if (handle) { |
---|
5054 | | - handle = ext4_journal_start_reserved(handle, |
---|
5055 | | - EXT4_HT_EXT_CONVERT); |
---|
5056 | | - if (IS_ERR(handle)) |
---|
5057 | | - return PTR_ERR(handle); |
---|
5058 | | - credits = 0; |
---|
5059 | | - } else { |
---|
| 4791 | + if (!handle) { |
---|
5060 | 4792 | /* |
---|
5061 | 4793 | * credits to insert 1 extent into extent tree |
---|
5062 | 4794 | */ |
---|
.. | .. |
---|
5081 | 4813 | "ext4_ext_map_blocks returned %d", |
---|
5082 | 4814 | inode->i_ino, map.m_lblk, |
---|
5083 | 4815 | map.m_len, ret); |
---|
5084 | | - ext4_mark_inode_dirty(handle, inode); |
---|
5085 | | - if (credits) |
---|
5086 | | - ret2 = ext4_journal_stop(handle); |
---|
| 4816 | + ret2 = ext4_mark_inode_dirty(handle, inode); |
---|
| 4817 | + if (credits) { |
---|
| 4818 | + ret3 = ext4_journal_stop(handle); |
---|
| 4819 | + if (unlikely(ret3)) |
---|
| 4820 | + ret2 = ret3; |
---|
| 4821 | + } |
---|
| 4822 | + |
---|
5087 | 4823 | if (ret <= 0 || ret2) |
---|
5088 | 4824 | break; |
---|
5089 | 4825 | } |
---|
5090 | | - if (!credits) |
---|
5091 | | - ret2 = ext4_journal_stop(handle); |
---|
5092 | 4826 | return ret > 0 ? ret2 : ret; |
---|
5093 | 4827 | } |
---|
5094 | 4828 | |
---|
5095 | | -/* |
---|
5096 | | - * If newes is not existing extent (newes->ec_pblk equals zero) find |
---|
5097 | | - * delayed extent at start of newes and update newes accordingly and |
---|
5098 | | - * return start of the next delayed extent. |
---|
5099 | | - * |
---|
5100 | | - * If newes is existing extent (newes->ec_pblk is not equal zero) |
---|
5101 | | - * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed |
---|
5102 | | - * extent found. Leave newes unmodified. |
---|
5103 | | - */ |
---|
5104 | | -static int ext4_find_delayed_extent(struct inode *inode, |
---|
5105 | | - struct extent_status *newes) |
---|
| 4829 | +int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) |
---|
5106 | 4830 | { |
---|
5107 | | - struct extent_status es; |
---|
5108 | | - ext4_lblk_t block, next_del; |
---|
| 4831 | + int ret = 0, err = 0; |
---|
| 4832 | + struct ext4_io_end_vec *io_end_vec; |
---|
5109 | 4833 | |
---|
5110 | | - if (newes->es_pblk == 0) { |
---|
5111 | | - ext4_es_find_delayed_extent_range(inode, newes->es_lblk, |
---|
5112 | | - newes->es_lblk + newes->es_len - 1, &es); |
---|
5113 | | - |
---|
5114 | | - /* |
---|
5115 | | - * No extent in extent-tree contains block @newes->es_pblk, |
---|
5116 | | - * then the block may stay in 1)a hole or 2)delayed-extent. |
---|
5117 | | - */ |
---|
5118 | | - if (es.es_len == 0) |
---|
5119 | | - /* A hole found. */ |
---|
5120 | | - return 0; |
---|
5121 | | - |
---|
5122 | | - if (es.es_lblk > newes->es_lblk) { |
---|
5123 | | - /* A hole found. */ |
---|
5124 | | - newes->es_len = min(es.es_lblk - newes->es_lblk, |
---|
5125 | | - newes->es_len); |
---|
5126 | | - return 0; |
---|
5127 | | - } |
---|
5128 | | - |
---|
5129 | | - newes->es_len = es.es_lblk + es.es_len - newes->es_lblk; |
---|
| 4834 | + /* |
---|
| 4835 | + * This is somewhat ugly but the idea is clear: When transaction is |
---|
| 4836 | + * reserved, everything goes into it. Otherwise we rather start several |
---|
| 4837 | + * smaller transactions for conversion of each extent separately. |
---|
| 4838 | + */ |
---|
| 4839 | + if (handle) { |
---|
| 4840 | + handle = ext4_journal_start_reserved(handle, |
---|
| 4841 | + EXT4_HT_EXT_CONVERT); |
---|
| 4842 | + if (IS_ERR(handle)) |
---|
| 4843 | + return PTR_ERR(handle); |
---|
5130 | 4844 | } |
---|
5131 | 4845 | |
---|
5132 | | - block = newes->es_lblk + newes->es_len; |
---|
5133 | | - ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es); |
---|
5134 | | - if (es.es_len == 0) |
---|
5135 | | - next_del = EXT_MAX_BLOCKS; |
---|
5136 | | - else |
---|
5137 | | - next_del = es.es_lblk; |
---|
| 4846 | + list_for_each_entry(io_end_vec, &io_end->list_vec, list) { |
---|
| 4847 | + ret = ext4_convert_unwritten_extents(handle, io_end->inode, |
---|
| 4848 | + io_end_vec->offset, |
---|
| 4849 | + io_end_vec->size); |
---|
| 4850 | + if (ret) |
---|
| 4851 | + break; |
---|
| 4852 | + } |
---|
5138 | 4853 | |
---|
5139 | | - return next_del; |
---|
| 4854 | + if (handle) |
---|
| 4855 | + err = ext4_journal_stop(handle); |
---|
| 4856 | + |
---|
| 4857 | + return ret < 0 ? ret : err; |
---|
5140 | 4858 | } |
---|
5141 | | -/* fiemap flags we can handle specified here */ |
---|
5142 | | -#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
---|
5143 | 4859 | |
---|
5144 | | -static int ext4_xattr_fiemap(struct inode *inode, |
---|
5145 | | - struct fiemap_extent_info *fieinfo) |
---|
| 4860 | +static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap) |
---|
5146 | 4861 | { |
---|
5147 | 4862 | __u64 physical = 0; |
---|
5148 | | - __u64 length; |
---|
5149 | | - __u32 flags = FIEMAP_EXTENT_LAST; |
---|
| 4863 | + __u64 length = 0; |
---|
5150 | 4864 | int blockbits = inode->i_sb->s_blocksize_bits; |
---|
5151 | 4865 | int error = 0; |
---|
| 4866 | + u16 iomap_type; |
---|
5152 | 4867 | |
---|
5153 | 4868 | /* in-inode? */ |
---|
5154 | 4869 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
---|
.. | .. |
---|
5163 | 4878 | EXT4_I(inode)->i_extra_isize; |
---|
5164 | 4879 | physical += offset; |
---|
5165 | 4880 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
---|
5166 | | - flags |= FIEMAP_EXTENT_DATA_INLINE; |
---|
5167 | 4881 | brelse(iloc.bh); |
---|
5168 | | - } else { /* external block */ |
---|
| 4882 | + iomap_type = IOMAP_INLINE; |
---|
| 4883 | + } else if (EXT4_I(inode)->i_file_acl) { /* external block */ |
---|
5169 | 4884 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
---|
5170 | 4885 | length = inode->i_sb->s_blocksize; |
---|
| 4886 | + iomap_type = IOMAP_MAPPED; |
---|
| 4887 | + } else { |
---|
| 4888 | + /* no in-inode or external block for xattr, so return -ENOENT */ |
---|
| 4889 | + error = -ENOENT; |
---|
| 4890 | + goto out; |
---|
5171 | 4891 | } |
---|
5172 | 4892 | |
---|
5173 | | - if (physical) |
---|
5174 | | - error = fiemap_fill_next_extent(fieinfo, 0, physical, |
---|
5175 | | - length, flags); |
---|
5176 | | - return (error < 0 ? error : 0); |
---|
| 4893 | + iomap->addr = physical; |
---|
| 4894 | + iomap->offset = 0; |
---|
| 4895 | + iomap->length = length; |
---|
| 4896 | + iomap->type = iomap_type; |
---|
| 4897 | + iomap->flags = 0; |
---|
| 4898 | +out: |
---|
| 4899 | + return error; |
---|
| 4900 | +} |
---|
| 4901 | + |
---|
| 4902 | +static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset, |
---|
| 4903 | + loff_t length, unsigned flags, |
---|
| 4904 | + struct iomap *iomap, struct iomap *srcmap) |
---|
| 4905 | +{ |
---|
| 4906 | + int error; |
---|
| 4907 | + |
---|
| 4908 | + error = ext4_iomap_xattr_fiemap(inode, iomap); |
---|
| 4909 | + if (error == 0 && (offset >= iomap->length)) |
---|
| 4910 | + error = -ENOENT; |
---|
| 4911 | + return error; |
---|
| 4912 | +} |
---|
| 4913 | + |
---|
| 4914 | +static const struct iomap_ops ext4_iomap_xattr_ops = { |
---|
| 4915 | + .iomap_begin = ext4_iomap_xattr_begin, |
---|
| 4916 | +}; |
---|
| 4917 | + |
---|
| 4918 | +static int ext4_fiemap_check_ranges(struct inode *inode, u64 start, u64 *len) |
---|
| 4919 | +{ |
---|
| 4920 | + u64 maxbytes; |
---|
| 4921 | + |
---|
| 4922 | + if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
---|
| 4923 | + maxbytes = inode->i_sb->s_maxbytes; |
---|
| 4924 | + else |
---|
| 4925 | + maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes; |
---|
| 4926 | + |
---|
| 4927 | + if (*len == 0) |
---|
| 4928 | + return -EINVAL; |
---|
| 4929 | + if (start > maxbytes) |
---|
| 4930 | + return -EFBIG; |
---|
| 4931 | + |
---|
| 4932 | + /* |
---|
| 4933 | + * Shrink request scope to what the fs can actually handle. |
---|
| 4934 | + */ |
---|
| 4935 | + if (*len > maxbytes || (maxbytes - *len) < start) |
---|
| 4936 | + *len = maxbytes - start; |
---|
| 4937 | + return 0; |
---|
5177 | 4938 | } |
---|
5178 | 4939 | |
---|
5179 | 4940 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
---|
5180 | | - __u64 start, __u64 len) |
---|
| 4941 | + u64 start, u64 len) |
---|
5181 | 4942 | { |
---|
5182 | | - ext4_lblk_t start_blk; |
---|
| 4943 | + int error = 0; |
---|
| 4944 | + |
---|
| 4945 | + if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
---|
| 4946 | + error = ext4_ext_precache(inode); |
---|
| 4947 | + if (error) |
---|
| 4948 | + return error; |
---|
| 4949 | + fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; |
---|
| 4950 | + } |
---|
| 4951 | + |
---|
| 4952 | + /* |
---|
| 4953 | + * For bitmap files the maximum size limit could be smaller than |
---|
| 4954 | + * s_maxbytes, so check len here manually instead of just relying on the |
---|
| 4955 | + * generic check. |
---|
| 4956 | + */ |
---|
| 4957 | + error = ext4_fiemap_check_ranges(inode, start, &len); |
---|
| 4958 | + if (error) |
---|
| 4959 | + return error; |
---|
| 4960 | + |
---|
| 4961 | + if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
---|
| 4962 | + fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; |
---|
| 4963 | + return iomap_fiemap(inode, fieinfo, start, len, |
---|
| 4964 | + &ext4_iomap_xattr_ops); |
---|
| 4965 | + } |
---|
| 4966 | + |
---|
| 4967 | + return iomap_fiemap(inode, fieinfo, start, len, &ext4_iomap_report_ops); |
---|
| 4968 | +} |
---|
| 4969 | + |
---|
| 4970 | +int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, |
---|
| 4971 | + __u64 start, __u64 len) |
---|
| 4972 | +{ |
---|
| 4973 | + ext4_lblk_t start_blk, len_blks; |
---|
| 4974 | + __u64 last_blk; |
---|
5183 | 4975 | int error = 0; |
---|
5184 | 4976 | |
---|
5185 | 4977 | if (ext4_has_inline_data(inode)) { |
---|
5186 | | - int has_inline = 1; |
---|
| 4978 | + int has_inline; |
---|
5187 | 4979 | |
---|
5188 | | - error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline, |
---|
5189 | | - start, len); |
---|
5190 | | - |
---|
| 4980 | + down_read(&EXT4_I(inode)->xattr_sem); |
---|
| 4981 | + has_inline = ext4_has_inline_data(inode); |
---|
| 4982 | + up_read(&EXT4_I(inode)->xattr_sem); |
---|
5191 | 4983 | if (has_inline) |
---|
5192 | | - return error; |
---|
| 4984 | + return 0; |
---|
5193 | 4985 | } |
---|
5194 | 4986 | |
---|
5195 | 4987 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
---|
5196 | 4988 | error = ext4_ext_precache(inode); |
---|
5197 | 4989 | if (error) |
---|
5198 | 4990 | return error; |
---|
| 4991 | + fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; |
---|
5199 | 4992 | } |
---|
5200 | 4993 | |
---|
5201 | | - /* fallback to generic here if not in extents fmt */ |
---|
5202 | | - if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
---|
5203 | | - return generic_block_fiemap(inode, fieinfo, start, len, |
---|
5204 | | - ext4_get_block); |
---|
| 4994 | + error = fiemap_prep(inode, fieinfo, start, &len, 0); |
---|
| 4995 | + if (error) |
---|
| 4996 | + return error; |
---|
5205 | 4997 | |
---|
5206 | | - if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) |
---|
5207 | | - return -EBADR; |
---|
| 4998 | + error = ext4_fiemap_check_ranges(inode, start, &len); |
---|
| 4999 | + if (error) |
---|
| 5000 | + return error; |
---|
5208 | 5001 | |
---|
5209 | | - if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
---|
5210 | | - error = ext4_xattr_fiemap(inode, fieinfo); |
---|
5211 | | - } else { |
---|
5212 | | - ext4_lblk_t len_blks; |
---|
5213 | | - __u64 last_blk; |
---|
5214 | | - |
---|
5215 | | - start_blk = start >> inode->i_sb->s_blocksize_bits; |
---|
5216 | | - last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
---|
5217 | | - if (last_blk >= EXT_MAX_BLOCKS) |
---|
5218 | | - last_blk = EXT_MAX_BLOCKS-1; |
---|
5219 | | - len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
---|
5220 | | - |
---|
5221 | | - /* |
---|
5222 | | - * Walk the extent tree gathering extent information |
---|
5223 | | - * and pushing extents back to the user. |
---|
5224 | | - */ |
---|
5225 | | - error = ext4_fill_fiemap_extents(inode, start_blk, |
---|
5226 | | - len_blks, fieinfo); |
---|
5227 | | - } |
---|
5228 | | - return error; |
---|
5229 | | -} |
---|
5230 | | - |
---|
5231 | | -/* |
---|
5232 | | - * ext4_access_path: |
---|
5233 | | - * Function to access the path buffer for marking it dirty. |
---|
5234 | | - * It also checks if there are sufficient credits left in the journal handle |
---|
5235 | | - * to update path. |
---|
5236 | | - */ |
---|
5237 | | -static int |
---|
5238 | | -ext4_access_path(handle_t *handle, struct inode *inode, |
---|
5239 | | - struct ext4_ext_path *path) |
---|
5240 | | -{ |
---|
5241 | | - int credits, err; |
---|
5242 | | - |
---|
5243 | | - if (!ext4_handle_valid(handle)) |
---|
5244 | | - return 0; |
---|
| 5002 | + start_blk = start >> inode->i_sb->s_blocksize_bits; |
---|
| 5003 | + last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
---|
| 5004 | + if (last_blk >= EXT_MAX_BLOCKS) |
---|
| 5005 | + last_blk = EXT_MAX_BLOCKS-1; |
---|
| 5006 | + len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
---|
5245 | 5007 | |
---|
5246 | 5008 | /* |
---|
5247 | | - * Check if need to extend journal credits |
---|
5248 | | - * 3 for leaf, sb, and inode plus 2 (bmap and group |
---|
5249 | | - * descriptor) for each block group; assume two block |
---|
5250 | | - * groups |
---|
| 5009 | + * Walk the extent tree gathering extent information |
---|
| 5010 | + * and pushing extents back to the user. |
---|
5251 | 5011 | */ |
---|
5252 | | - if (handle->h_buffer_credits < 7) { |
---|
5253 | | - credits = ext4_writepage_trans_blocks(inode); |
---|
5254 | | - err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
---|
5255 | | - /* EAGAIN is success */ |
---|
5256 | | - if (err && err != -EAGAIN) |
---|
5257 | | - return err; |
---|
5258 | | - } |
---|
5259 | | - |
---|
5260 | | - err = ext4_ext_get_access(handle, inode, path); |
---|
5261 | | - return err; |
---|
| 5012 | + return ext4_fill_es_cache_info(inode, start_blk, len_blks, fieinfo); |
---|
5262 | 5013 | } |
---|
5263 | 5014 | |
---|
5264 | 5015 | /* |
---|
.. | .. |
---|
5274 | 5025 | { |
---|
5275 | 5026 | int depth, err = 0; |
---|
5276 | 5027 | struct ext4_extent *ex_start, *ex_last; |
---|
5277 | | - bool update = 0; |
---|
| 5028 | + bool update = false; |
---|
| 5029 | + int credits, restart_credits; |
---|
5278 | 5030 | depth = path->p_depth; |
---|
5279 | 5031 | |
---|
5280 | 5032 | while (depth >= 0) { |
---|
.. | .. |
---|
5284 | 5036 | return -EFSCORRUPTED; |
---|
5285 | 5037 | |
---|
5286 | 5038 | ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); |
---|
| 5039 | + /* leaf + sb + inode */ |
---|
| 5040 | + credits = 3; |
---|
| 5041 | + if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) { |
---|
| 5042 | + update = true; |
---|
| 5043 | + /* extent tree + sb + inode */ |
---|
| 5044 | + credits = depth + 2; |
---|
| 5045 | + } |
---|
5287 | 5046 | |
---|
5288 | | - err = ext4_access_path(handle, inode, path + depth); |
---|
| 5047 | + restart_credits = ext4_writepage_trans_blocks(inode); |
---|
| 5048 | + err = ext4_datasem_ensure_credits(handle, inode, credits, |
---|
| 5049 | + restart_credits, 0); |
---|
| 5050 | + if (err) { |
---|
| 5051 | + if (err > 0) |
---|
| 5052 | + err = -EAGAIN; |
---|
| 5053 | + goto out; |
---|
| 5054 | + } |
---|
| 5055 | + |
---|
| 5056 | + err = ext4_ext_get_access(handle, inode, path + depth); |
---|
5289 | 5057 | if (err) |
---|
5290 | 5058 | goto out; |
---|
5291 | | - |
---|
5292 | | - if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) |
---|
5293 | | - update = 1; |
---|
5294 | 5059 | |
---|
5295 | 5060 | while (ex_start <= ex_last) { |
---|
5296 | 5061 | if (SHIFT == SHIFT_LEFT) { |
---|
.. | .. |
---|
5321 | 5086 | } |
---|
5322 | 5087 | |
---|
5323 | 5088 | /* Update index too */ |
---|
5324 | | - err = ext4_access_path(handle, inode, path + depth); |
---|
| 5089 | + err = ext4_ext_get_access(handle, inode, path + depth); |
---|
5325 | 5090 | if (err) |
---|
5326 | 5091 | goto out; |
---|
5327 | 5092 | |
---|
.. | .. |
---|
5360 | 5125 | int ret = 0, depth; |
---|
5361 | 5126 | struct ext4_extent *extent; |
---|
5362 | 5127 | ext4_lblk_t stop, *iterator, ex_start, ex_end; |
---|
| 5128 | + ext4_lblk_t tmp = EXT_MAX_BLOCKS; |
---|
5363 | 5129 | |
---|
5364 | 5130 | /* Let path point to the last extent */ |
---|
5365 | 5131 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
---|
.. | .. |
---|
5413 | 5179 | * till we reach stop. In case of right shift, iterator points to stop |
---|
5414 | 5180 | * and it is decreased till we reach start. |
---|
5415 | 5181 | */ |
---|
| 5182 | +again: |
---|
| 5183 | + ret = 0; |
---|
5416 | 5184 | if (SHIFT == SHIFT_LEFT) |
---|
5417 | 5185 | iterator = &start; |
---|
5418 | 5186 | else |
---|
5419 | 5187 | iterator = &stop; |
---|
| 5188 | + |
---|
| 5189 | + if (tmp != EXT_MAX_BLOCKS) |
---|
| 5190 | + *iterator = tmp; |
---|
5420 | 5191 | |
---|
5421 | 5192 | /* |
---|
5422 | 5193 | * Its safe to start updating extents. Start and stop are unsigned, so |
---|
.. | .. |
---|
5446 | 5217 | } |
---|
5447 | 5218 | } |
---|
5448 | 5219 | |
---|
| 5220 | + tmp = *iterator; |
---|
5449 | 5221 | if (SHIFT == SHIFT_LEFT) { |
---|
5450 | 5222 | extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
---|
5451 | 5223 | *iterator = le32_to_cpu(extent->ee_block) + |
---|
5452 | 5224 | ext4_ext_get_actual_len(extent); |
---|
5453 | 5225 | } else { |
---|
5454 | 5226 | extent = EXT_FIRST_EXTENT(path[depth].p_hdr); |
---|
5455 | | - if (le32_to_cpu(extent->ee_block) > 0) |
---|
| 5227 | + if (le32_to_cpu(extent->ee_block) > start) |
---|
5456 | 5228 | *iterator = le32_to_cpu(extent->ee_block) - 1; |
---|
5457 | | - else |
---|
5458 | | - /* Beginning is reached, end of the loop */ |
---|
| 5229 | + else if (le32_to_cpu(extent->ee_block) == start) |
---|
5459 | 5230 | iterator = NULL; |
---|
5460 | | - /* Update path extent in case we need to stop */ |
---|
5461 | | - while (le32_to_cpu(extent->ee_block) < start) |
---|
| 5231 | + else { |
---|
| 5232 | + extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
---|
| 5233 | + while (le32_to_cpu(extent->ee_block) >= start) |
---|
| 5234 | + extent--; |
---|
| 5235 | + |
---|
| 5236 | + if (extent == EXT_LAST_EXTENT(path[depth].p_hdr)) |
---|
| 5237 | + break; |
---|
| 5238 | + |
---|
5462 | 5239 | extent++; |
---|
| 5240 | + iterator = NULL; |
---|
| 5241 | + } |
---|
5463 | 5242 | path[depth].p_ext = extent; |
---|
5464 | 5243 | } |
---|
5465 | 5244 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
---|
5466 | 5245 | handle, SHIFT); |
---|
| 5246 | + /* iterator can be NULL which means we should break */ |
---|
| 5247 | + if (ret == -EAGAIN) |
---|
| 5248 | + goto again; |
---|
5467 | 5249 | if (ret) |
---|
5468 | 5250 | break; |
---|
5469 | 5251 | } |
---|
.. | .. |
---|
5478 | 5260 | * This implements the fallocate's collapse range functionality for ext4 |
---|
5479 | 5261 | * Returns: 0 and non-zero on error. |
---|
5480 | 5262 | */ |
---|
5481 | | -int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len) |
---|
| 5263 | +static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len) |
---|
5482 | 5264 | { |
---|
| 5265 | + struct inode *inode = file_inode(file); |
---|
5483 | 5266 | struct super_block *sb = inode->i_sb; |
---|
5484 | 5267 | ext4_lblk_t punch_start, punch_stop; |
---|
5485 | 5268 | handle_t *handle; |
---|
.. | .. |
---|
5495 | 5278 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
---|
5496 | 5279 | return -EOPNOTSUPP; |
---|
5497 | 5280 | |
---|
5498 | | - /* Collapse range works only on fs block size aligned offsets. */ |
---|
5499 | | - if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) || |
---|
5500 | | - len & (EXT4_CLUSTER_SIZE(sb) - 1)) |
---|
5501 | | - return -EINVAL; |
---|
5502 | | - |
---|
5503 | | - if (!S_ISREG(inode->i_mode)) |
---|
| 5281 | + /* Collapse range works only on fs cluster size aligned regions. */ |
---|
| 5282 | + if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
---|
5504 | 5283 | return -EINVAL; |
---|
5505 | 5284 | |
---|
5506 | 5285 | trace_ext4_collapse_range(inode, offset, len); |
---|
.. | .. |
---|
5520 | 5299 | * There is no need to overlap collapse range with EOF, in which case |
---|
5521 | 5300 | * it is effectively a truncate operation |
---|
5522 | 5301 | */ |
---|
5523 | | - if (offset + len >= i_size_read(inode)) { |
---|
| 5302 | + if (offset + len >= inode->i_size) { |
---|
5524 | 5303 | ret = -EINVAL; |
---|
5525 | 5304 | goto out_mutex; |
---|
5526 | 5305 | } |
---|
.. | .. |
---|
5533 | 5312 | |
---|
5534 | 5313 | /* Wait for existing dio to complete */ |
---|
5535 | 5314 | inode_dio_wait(inode); |
---|
| 5315 | + |
---|
| 5316 | + ret = file_modified(file); |
---|
| 5317 | + if (ret) |
---|
| 5318 | + goto out_mutex; |
---|
5536 | 5319 | |
---|
5537 | 5320 | /* |
---|
5538 | 5321 | * Prevent page faults from reinstantiating pages we have released from |
---|
.. | .. |
---|
5573 | 5356 | ret = PTR_ERR(handle); |
---|
5574 | 5357 | goto out_mmap; |
---|
5575 | 5358 | } |
---|
| 5359 | + ext4_fc_start_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE); |
---|
5576 | 5360 | |
---|
5577 | 5361 | down_write(&EXT4_I(inode)->i_data_sem); |
---|
5578 | | - ext4_discard_preallocations(inode); |
---|
| 5362 | + ext4_discard_preallocations(inode, 0); |
---|
5579 | 5363 | |
---|
5580 | 5364 | ret = ext4_es_remove_extent(inode, punch_start, |
---|
5581 | 5365 | EXT_MAX_BLOCKS - punch_start); |
---|
.. | .. |
---|
5589 | 5373 | up_write(&EXT4_I(inode)->i_data_sem); |
---|
5590 | 5374 | goto out_stop; |
---|
5591 | 5375 | } |
---|
5592 | | - ext4_discard_preallocations(inode); |
---|
| 5376 | + ext4_discard_preallocations(inode, 0); |
---|
5593 | 5377 | |
---|
5594 | 5378 | ret = ext4_ext_shift_extents(inode, handle, punch_stop, |
---|
5595 | 5379 | punch_stop - punch_start, SHIFT_LEFT); |
---|
.. | .. |
---|
5598 | 5382 | goto out_stop; |
---|
5599 | 5383 | } |
---|
5600 | 5384 | |
---|
5601 | | - new_size = i_size_read(inode) - len; |
---|
| 5385 | + new_size = inode->i_size - len; |
---|
5602 | 5386 | i_size_write(inode, new_size); |
---|
5603 | 5387 | EXT4_I(inode)->i_disksize = new_size; |
---|
5604 | 5388 | |
---|
.. | .. |
---|
5606 | 5390 | if (IS_SYNC(inode)) |
---|
5607 | 5391 | ext4_handle_sync(handle); |
---|
5608 | 5392 | inode->i_mtime = inode->i_ctime = current_time(inode); |
---|
5609 | | - ext4_mark_inode_dirty(handle, inode); |
---|
| 5393 | + ret = ext4_mark_inode_dirty(handle, inode); |
---|
5610 | 5394 | ext4_update_inode_fsync_trans(handle, inode, 1); |
---|
5611 | 5395 | |
---|
5612 | 5396 | out_stop: |
---|
5613 | 5397 | ext4_journal_stop(handle); |
---|
| 5398 | + ext4_fc_stop_ineligible(sb); |
---|
5614 | 5399 | out_mmap: |
---|
5615 | 5400 | up_write(&EXT4_I(inode)->i_mmap_sem); |
---|
5616 | 5401 | out_mutex: |
---|
.. | .. |
---|
5626 | 5411 | * by len bytes. |
---|
5627 | 5412 | * Returns 0 on success, error otherwise. |
---|
5628 | 5413 | */ |
---|
5629 | | -int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len) |
---|
| 5414 | +static int ext4_insert_range(struct file *file, loff_t offset, loff_t len) |
---|
5630 | 5415 | { |
---|
| 5416 | + struct inode *inode = file_inode(file); |
---|
5631 | 5417 | struct super_block *sb = inode->i_sb; |
---|
5632 | 5418 | handle_t *handle; |
---|
5633 | 5419 | struct ext4_ext_path *path; |
---|
.. | .. |
---|
5645 | 5431 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
---|
5646 | 5432 | return -EOPNOTSUPP; |
---|
5647 | 5433 | |
---|
5648 | | - /* Insert range works only on fs block size aligned offsets. */ |
---|
5649 | | - if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) || |
---|
5650 | | - len & (EXT4_CLUSTER_SIZE(sb) - 1)) |
---|
| 5434 | + /* Insert range works only on fs cluster size aligned regions. */ |
---|
| 5435 | + if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
---|
5651 | 5436 | return -EINVAL; |
---|
5652 | | - |
---|
5653 | | - if (!S_ISREG(inode->i_mode)) |
---|
5654 | | - return -EOPNOTSUPP; |
---|
5655 | 5437 | |
---|
5656 | 5438 | trace_ext4_insert_range(inode, offset, len); |
---|
5657 | 5439 | |
---|
.. | .. |
---|
5672 | 5454 | goto out_mutex; |
---|
5673 | 5455 | } |
---|
5674 | 5456 | |
---|
5675 | | - /* Check for wrap through zero */ |
---|
5676 | | - if (inode->i_size + len > inode->i_sb->s_maxbytes) { |
---|
| 5457 | + /* Check whether the maximum file size would be exceeded */ |
---|
| 5458 | + if (len > inode->i_sb->s_maxbytes - inode->i_size) { |
---|
5677 | 5459 | ret = -EFBIG; |
---|
5678 | 5460 | goto out_mutex; |
---|
5679 | 5461 | } |
---|
5680 | 5462 | |
---|
5681 | | - /* Offset should be less than i_size */ |
---|
5682 | | - if (offset >= i_size_read(inode)) { |
---|
| 5463 | + /* Offset must be less than i_size */ |
---|
| 5464 | + if (offset >= inode->i_size) { |
---|
5683 | 5465 | ret = -EINVAL; |
---|
5684 | 5466 | goto out_mutex; |
---|
5685 | 5467 | } |
---|
5686 | 5468 | |
---|
5687 | 5469 | /* Wait for existing dio to complete */ |
---|
5688 | 5470 | inode_dio_wait(inode); |
---|
| 5471 | + |
---|
| 5472 | + ret = file_modified(file); |
---|
| 5473 | + if (ret) |
---|
| 5474 | + goto out_mutex; |
---|
5689 | 5475 | |
---|
5690 | 5476 | /* |
---|
5691 | 5477 | * Prevent page faults from reinstantiating pages we have released from |
---|
.. | .. |
---|
5715 | 5501 | ret = PTR_ERR(handle); |
---|
5716 | 5502 | goto out_mmap; |
---|
5717 | 5503 | } |
---|
| 5504 | + ext4_fc_start_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE); |
---|
5718 | 5505 | |
---|
5719 | 5506 | /* Expand file to avoid data loss if there is error while shifting */ |
---|
5720 | 5507 | inode->i_size += len; |
---|
.. | .. |
---|
5725 | 5512 | goto out_stop; |
---|
5726 | 5513 | |
---|
5727 | 5514 | down_write(&EXT4_I(inode)->i_data_sem); |
---|
5728 | | - ext4_discard_preallocations(inode); |
---|
| 5515 | + ext4_discard_preallocations(inode, 0); |
---|
5729 | 5516 | |
---|
5730 | 5517 | path = ext4_find_extent(inode, offset_lblk, NULL, 0); |
---|
5731 | 5518 | if (IS_ERR(path)) { |
---|
.. | .. |
---|
5789 | 5576 | |
---|
5790 | 5577 | out_stop: |
---|
5791 | 5578 | ext4_journal_stop(handle); |
---|
| 5579 | + ext4_fc_stop_ineligible(sb); |
---|
5792 | 5580 | out_mmap: |
---|
5793 | 5581 | up_write(&EXT4_I(inode)->i_mmap_sem); |
---|
5794 | 5582 | out_mutex: |
---|
.. | .. |
---|
5797 | 5585 | } |
---|
5798 | 5586 | |
---|
5799 | 5587 | /** |
---|
5800 | | - * ext4_swap_extents - Swap extents between two inodes |
---|
5801 | | - * |
---|
| 5588 | + * ext4_swap_extents() - Swap extents between two inodes |
---|
| 5589 | + * @handle: handle for this transaction |
---|
5802 | 5590 | * @inode1: First inode |
---|
5803 | 5591 | * @inode2: Second inode |
---|
5804 | 5592 | * @lblk1: Start block for first inode |
---|
.. | .. |
---|
5859 | 5647 | } |
---|
5860 | 5648 | ex1 = path1[path1->p_depth].p_ext; |
---|
5861 | 5649 | ex2 = path2[path2->p_depth].p_ext; |
---|
5862 | | - /* Do we have somthing to swap ? */ |
---|
| 5650 | + /* Do we have something to swap ? */ |
---|
5863 | 5651 | if (unlikely(!ex2 || !ex1)) |
---|
5864 | 5652 | goto finish; |
---|
5865 | 5653 | |
---|
.. | .. |
---|
5991 | 5779 | } |
---|
5992 | 5780 | return replaced_count; |
---|
5993 | 5781 | } |
---|
| 5782 | + |
---|
| 5783 | +/* |
---|
| 5784 | + * ext4_clu_mapped - determine whether any block in a logical cluster has |
---|
| 5785 | + * been mapped to a physical cluster |
---|
| 5786 | + * |
---|
| 5787 | + * @inode - file containing the logical cluster |
---|
| 5788 | + * @lclu - logical cluster of interest |
---|
| 5789 | + * |
---|
| 5790 | + * Returns 1 if any block in the logical cluster is mapped, signifying |
---|
| 5791 | + * that a physical cluster has been allocated for it. Otherwise, |
---|
| 5792 | + * returns 0. Can also return negative error codes. Derived from |
---|
| 5793 | + * ext4_ext_map_blocks(). |
---|
| 5794 | + */ |
---|
| 5795 | +int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) |
---|
| 5796 | +{ |
---|
| 5797 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 5798 | + struct ext4_ext_path *path; |
---|
| 5799 | + int depth, mapped = 0, err = 0; |
---|
| 5800 | + struct ext4_extent *extent; |
---|
| 5801 | + ext4_lblk_t first_lblk, first_lclu, last_lclu; |
---|
| 5802 | + |
---|
| 5803 | + /* |
---|
| 5804 | + * if data can be stored inline, the logical cluster isn't |
---|
| 5805 | + * mapped - no physical clusters have been allocated, and the |
---|
| 5806 | + * file has no extents |
---|
| 5807 | + */ |
---|
| 5808 | + if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) || |
---|
| 5809 | + ext4_has_inline_data(inode)) |
---|
| 5810 | + return 0; |
---|
| 5811 | + |
---|
| 5812 | + /* search for the extent closest to the first block in the cluster */ |
---|
| 5813 | + path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); |
---|
| 5814 | + if (IS_ERR(path)) { |
---|
| 5815 | + err = PTR_ERR(path); |
---|
| 5816 | + path = NULL; |
---|
| 5817 | + goto out; |
---|
| 5818 | + } |
---|
| 5819 | + |
---|
| 5820 | + depth = ext_depth(inode); |
---|
| 5821 | + |
---|
| 5822 | + /* |
---|
| 5823 | + * A consistent leaf must not be empty. This situation is possible, |
---|
| 5824 | + * though, _during_ tree modification, and it's why an assert can't |
---|
| 5825 | + * be put in ext4_find_extent(). |
---|
| 5826 | + */ |
---|
| 5827 | + if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
---|
| 5828 | + EXT4_ERROR_INODE(inode, |
---|
| 5829 | + "bad extent address - lblock: %lu, depth: %d, pblock: %lld", |
---|
| 5830 | + (unsigned long) EXT4_C2B(sbi, lclu), |
---|
| 5831 | + depth, path[depth].p_block); |
---|
| 5832 | + err = -EFSCORRUPTED; |
---|
| 5833 | + goto out; |
---|
| 5834 | + } |
---|
| 5835 | + |
---|
| 5836 | + extent = path[depth].p_ext; |
---|
| 5837 | + |
---|
| 5838 | + /* can't be mapped if the extent tree is empty */ |
---|
| 5839 | + if (extent == NULL) |
---|
| 5840 | + goto out; |
---|
| 5841 | + |
---|
| 5842 | + first_lblk = le32_to_cpu(extent->ee_block); |
---|
| 5843 | + first_lclu = EXT4_B2C(sbi, first_lblk); |
---|
| 5844 | + |
---|
| 5845 | + /* |
---|
| 5846 | + * Three possible outcomes at this point - found extent spanning |
---|
| 5847 | + * the target cluster, to the left of the target cluster, or to the |
---|
| 5848 | + * right of the target cluster. The first two cases are handled here. |
---|
| 5849 | + * The last case indicates the target cluster is not mapped. |
---|
| 5850 | + */ |
---|
| 5851 | + if (lclu >= first_lclu) { |
---|
| 5852 | + last_lclu = EXT4_B2C(sbi, first_lblk + |
---|
| 5853 | + ext4_ext_get_actual_len(extent) - 1); |
---|
| 5854 | + if (lclu <= last_lclu) { |
---|
| 5855 | + mapped = 1; |
---|
| 5856 | + } else { |
---|
| 5857 | + first_lblk = ext4_ext_next_allocated_block(path); |
---|
| 5858 | + first_lclu = EXT4_B2C(sbi, first_lblk); |
---|
| 5859 | + if (lclu == first_lclu) |
---|
| 5860 | + mapped = 1; |
---|
| 5861 | + } |
---|
| 5862 | + } |
---|
| 5863 | + |
---|
| 5864 | +out: |
---|
| 5865 | + ext4_ext_drop_refs(path); |
---|
| 5866 | + kfree(path); |
---|
| 5867 | + |
---|
| 5868 | + return err ? err : mapped; |
---|
| 5869 | +} |
---|
| 5870 | + |
---|
| 5871 | +/* |
---|
| 5872 | + * Updates physical block address and unwritten status of extent |
---|
| 5873 | + * starting at lblk start and of len. If such an extent doesn't exist, |
---|
| 5874 | + * this function splits the extent tree appropriately to create an |
---|
| 5875 | + * extent like this. This function is called in the fast commit |
---|
| 5876 | + * replay path. Returns 0 on success and error on failure. |
---|
| 5877 | + */ |
---|
| 5878 | +int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start, |
---|
| 5879 | + int len, int unwritten, ext4_fsblk_t pblk) |
---|
| 5880 | +{ |
---|
| 5881 | + struct ext4_ext_path *path = NULL, *ppath; |
---|
| 5882 | + struct ext4_extent *ex; |
---|
| 5883 | + int ret; |
---|
| 5884 | + |
---|
| 5885 | + path = ext4_find_extent(inode, start, NULL, 0); |
---|
| 5886 | + if (IS_ERR(path)) |
---|
| 5887 | + return PTR_ERR(path); |
---|
| 5888 | + ex = path[path->p_depth].p_ext; |
---|
| 5889 | + if (!ex) { |
---|
| 5890 | + ret = -EFSCORRUPTED; |
---|
| 5891 | + goto out; |
---|
| 5892 | + } |
---|
| 5893 | + |
---|
| 5894 | + if (le32_to_cpu(ex->ee_block) != start || |
---|
| 5895 | + ext4_ext_get_actual_len(ex) != len) { |
---|
| 5896 | + /* We need to split this extent to match our extent first */ |
---|
| 5897 | + ppath = path; |
---|
| 5898 | + down_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5899 | + ret = ext4_force_split_extent_at(NULL, inode, &ppath, start, 1); |
---|
| 5900 | + up_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5901 | + if (ret) |
---|
| 5902 | + goto out; |
---|
| 5903 | + kfree(path); |
---|
| 5904 | + path = ext4_find_extent(inode, start, NULL, 0); |
---|
| 5905 | + if (IS_ERR(path)) |
---|
| 5906 | + return -1; |
---|
| 5907 | + ppath = path; |
---|
| 5908 | + ex = path[path->p_depth].p_ext; |
---|
| 5909 | + WARN_ON(le32_to_cpu(ex->ee_block) != start); |
---|
| 5910 | + if (ext4_ext_get_actual_len(ex) != len) { |
---|
| 5911 | + down_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5912 | + ret = ext4_force_split_extent_at(NULL, inode, &ppath, |
---|
| 5913 | + start + len, 1); |
---|
| 5914 | + up_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5915 | + if (ret) |
---|
| 5916 | + goto out; |
---|
| 5917 | + kfree(path); |
---|
| 5918 | + path = ext4_find_extent(inode, start, NULL, 0); |
---|
| 5919 | + if (IS_ERR(path)) |
---|
| 5920 | + return -EINVAL; |
---|
| 5921 | + ex = path[path->p_depth].p_ext; |
---|
| 5922 | + } |
---|
| 5923 | + } |
---|
| 5924 | + if (unwritten) |
---|
| 5925 | + ext4_ext_mark_unwritten(ex); |
---|
| 5926 | + else |
---|
| 5927 | + ext4_ext_mark_initialized(ex); |
---|
| 5928 | + ext4_ext_store_pblock(ex, pblk); |
---|
| 5929 | + down_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5930 | + ret = ext4_ext_dirty(NULL, inode, &path[path->p_depth]); |
---|
| 5931 | + up_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5932 | +out: |
---|
| 5933 | + ext4_ext_drop_refs(path); |
---|
| 5934 | + kfree(path); |
---|
| 5935 | + ext4_mark_inode_dirty(NULL, inode); |
---|
| 5936 | + return ret; |
---|
| 5937 | +} |
---|
| 5938 | + |
---|
| 5939 | +/* Try to shrink the extent tree */ |
---|
| 5940 | +void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end) |
---|
| 5941 | +{ |
---|
| 5942 | + struct ext4_ext_path *path = NULL; |
---|
| 5943 | + struct ext4_extent *ex; |
---|
| 5944 | + ext4_lblk_t old_cur, cur = 0; |
---|
| 5945 | + |
---|
| 5946 | + while (cur < end) { |
---|
| 5947 | + path = ext4_find_extent(inode, cur, NULL, 0); |
---|
| 5948 | + if (IS_ERR(path)) |
---|
| 5949 | + return; |
---|
| 5950 | + ex = path[path->p_depth].p_ext; |
---|
| 5951 | + if (!ex) { |
---|
| 5952 | + ext4_ext_drop_refs(path); |
---|
| 5953 | + kfree(path); |
---|
| 5954 | + ext4_mark_inode_dirty(NULL, inode); |
---|
| 5955 | + return; |
---|
| 5956 | + } |
---|
| 5957 | + old_cur = cur; |
---|
| 5958 | + cur = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
---|
| 5959 | + if (cur <= old_cur) |
---|
| 5960 | + cur = old_cur + 1; |
---|
| 5961 | + ext4_ext_try_to_merge(NULL, inode, path, ex); |
---|
| 5962 | + down_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5963 | + ext4_ext_dirty(NULL, inode, &path[path->p_depth]); |
---|
| 5964 | + up_write(&EXT4_I(inode)->i_data_sem); |
---|
| 5965 | + ext4_mark_inode_dirty(NULL, inode); |
---|
| 5966 | + ext4_ext_drop_refs(path); |
---|
| 5967 | + kfree(path); |
---|
| 5968 | + } |
---|
| 5969 | +} |
---|
| 5970 | + |
---|
| 5971 | +/* Check if *cur is a hole and if it is, skip it */ |
---|
| 5972 | +static int skip_hole(struct inode *inode, ext4_lblk_t *cur) |
---|
| 5973 | +{ |
---|
| 5974 | + int ret; |
---|
| 5975 | + struct ext4_map_blocks map; |
---|
| 5976 | + |
---|
| 5977 | + map.m_lblk = *cur; |
---|
| 5978 | + map.m_len = ((inode->i_size) >> inode->i_sb->s_blocksize_bits) - *cur; |
---|
| 5979 | + |
---|
| 5980 | + ret = ext4_map_blocks(NULL, inode, &map, 0); |
---|
| 5981 | + if (ret < 0) |
---|
| 5982 | + return ret; |
---|
| 5983 | + if (ret != 0) |
---|
| 5984 | + return 0; |
---|
| 5985 | + *cur = *cur + map.m_len; |
---|
| 5986 | + return 0; |
---|
| 5987 | +} |
---|
| 5988 | + |
---|
| 5989 | +/* Count number of blocks used by this inode and update i_blocks */ |
---|
| 5990 | +int ext4_ext_replay_set_iblocks(struct inode *inode) |
---|
| 5991 | +{ |
---|
| 5992 | + struct ext4_ext_path *path = NULL, *path2 = NULL; |
---|
| 5993 | + struct ext4_extent *ex; |
---|
| 5994 | + ext4_lblk_t cur = 0, end; |
---|
| 5995 | + int numblks = 0, i, ret = 0; |
---|
| 5996 | + ext4_fsblk_t cmp1, cmp2; |
---|
| 5997 | + struct ext4_map_blocks map; |
---|
| 5998 | + |
---|
| 5999 | + /* Determin the size of the file first */ |
---|
| 6000 | + path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
---|
| 6001 | + EXT4_EX_NOCACHE); |
---|
| 6002 | + if (IS_ERR(path)) |
---|
| 6003 | + return PTR_ERR(path); |
---|
| 6004 | + ex = path[path->p_depth].p_ext; |
---|
| 6005 | + if (!ex) { |
---|
| 6006 | + ext4_ext_drop_refs(path); |
---|
| 6007 | + kfree(path); |
---|
| 6008 | + goto out; |
---|
| 6009 | + } |
---|
| 6010 | + end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
---|
| 6011 | + ext4_ext_drop_refs(path); |
---|
| 6012 | + kfree(path); |
---|
| 6013 | + |
---|
| 6014 | + /* Count the number of data blocks */ |
---|
| 6015 | + cur = 0; |
---|
| 6016 | + while (cur < end) { |
---|
| 6017 | + map.m_lblk = cur; |
---|
| 6018 | + map.m_len = end - cur; |
---|
| 6019 | + ret = ext4_map_blocks(NULL, inode, &map, 0); |
---|
| 6020 | + if (ret < 0) |
---|
| 6021 | + break; |
---|
| 6022 | + if (ret > 0) |
---|
| 6023 | + numblks += ret; |
---|
| 6024 | + cur = cur + map.m_len; |
---|
| 6025 | + } |
---|
| 6026 | + |
---|
| 6027 | + /* |
---|
| 6028 | + * Count the number of extent tree blocks. We do it by looking up |
---|
| 6029 | + * two successive extents and determining the difference between |
---|
| 6030 | + * their paths. When path is different for 2 successive extents |
---|
| 6031 | + * we compare the blocks in the path at each level and increment |
---|
| 6032 | + * iblocks by total number of differences found. |
---|
| 6033 | + */ |
---|
| 6034 | + cur = 0; |
---|
| 6035 | + ret = skip_hole(inode, &cur); |
---|
| 6036 | + if (ret < 0) |
---|
| 6037 | + goto out; |
---|
| 6038 | + path = ext4_find_extent(inode, cur, NULL, 0); |
---|
| 6039 | + if (IS_ERR(path)) |
---|
| 6040 | + goto out; |
---|
| 6041 | + numblks += path->p_depth; |
---|
| 6042 | + ext4_ext_drop_refs(path); |
---|
| 6043 | + kfree(path); |
---|
| 6044 | + while (cur < end) { |
---|
| 6045 | + path = ext4_find_extent(inode, cur, NULL, 0); |
---|
| 6046 | + if (IS_ERR(path)) |
---|
| 6047 | + break; |
---|
| 6048 | + ex = path[path->p_depth].p_ext; |
---|
| 6049 | + if (!ex) { |
---|
| 6050 | + ext4_ext_drop_refs(path); |
---|
| 6051 | + kfree(path); |
---|
| 6052 | + return 0; |
---|
| 6053 | + } |
---|
| 6054 | + cur = max(cur + 1, le32_to_cpu(ex->ee_block) + |
---|
| 6055 | + ext4_ext_get_actual_len(ex)); |
---|
| 6056 | + ret = skip_hole(inode, &cur); |
---|
| 6057 | + if (ret < 0) { |
---|
| 6058 | + ext4_ext_drop_refs(path); |
---|
| 6059 | + kfree(path); |
---|
| 6060 | + break; |
---|
| 6061 | + } |
---|
| 6062 | + path2 = ext4_find_extent(inode, cur, NULL, 0); |
---|
| 6063 | + if (IS_ERR(path2)) { |
---|
| 6064 | + ext4_ext_drop_refs(path); |
---|
| 6065 | + kfree(path); |
---|
| 6066 | + break; |
---|
| 6067 | + } |
---|
| 6068 | + ex = path2[path2->p_depth].p_ext; |
---|
| 6069 | + for (i = 0; i <= max(path->p_depth, path2->p_depth); i++) { |
---|
| 6070 | + cmp1 = cmp2 = 0; |
---|
| 6071 | + if (i <= path->p_depth) |
---|
| 6072 | + cmp1 = path[i].p_bh ? |
---|
| 6073 | + path[i].p_bh->b_blocknr : 0; |
---|
| 6074 | + if (i <= path2->p_depth) |
---|
| 6075 | + cmp2 = path2[i].p_bh ? |
---|
| 6076 | + path2[i].p_bh->b_blocknr : 0; |
---|
| 6077 | + if (cmp1 != cmp2 && cmp2 != 0) |
---|
| 6078 | + numblks++; |
---|
| 6079 | + } |
---|
| 6080 | + ext4_ext_drop_refs(path); |
---|
| 6081 | + ext4_ext_drop_refs(path2); |
---|
| 6082 | + kfree(path); |
---|
| 6083 | + kfree(path2); |
---|
| 6084 | + } |
---|
| 6085 | + |
---|
| 6086 | +out: |
---|
| 6087 | + inode->i_blocks = numblks << (inode->i_sb->s_blocksize_bits - 9); |
---|
| 6088 | + ext4_mark_inode_dirty(NULL, inode); |
---|
| 6089 | + return 0; |
---|
| 6090 | +} |
---|
| 6091 | + |
---|
| 6092 | +int ext4_ext_clear_bb(struct inode *inode) |
---|
| 6093 | +{ |
---|
| 6094 | + struct ext4_ext_path *path = NULL; |
---|
| 6095 | + struct ext4_extent *ex; |
---|
| 6096 | + ext4_lblk_t cur = 0, end; |
---|
| 6097 | + int j, ret = 0; |
---|
| 6098 | + struct ext4_map_blocks map; |
---|
| 6099 | + |
---|
| 6100 | + /* Determin the size of the file first */ |
---|
| 6101 | + path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
---|
| 6102 | + EXT4_EX_NOCACHE); |
---|
| 6103 | + if (IS_ERR(path)) |
---|
| 6104 | + return PTR_ERR(path); |
---|
| 6105 | + ex = path[path->p_depth].p_ext; |
---|
| 6106 | + if (!ex) { |
---|
| 6107 | + ext4_ext_drop_refs(path); |
---|
| 6108 | + kfree(path); |
---|
| 6109 | + return 0; |
---|
| 6110 | + } |
---|
| 6111 | + end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
---|
| 6112 | + ext4_ext_drop_refs(path); |
---|
| 6113 | + kfree(path); |
---|
| 6114 | + |
---|
| 6115 | + cur = 0; |
---|
| 6116 | + while (cur < end) { |
---|
| 6117 | + map.m_lblk = cur; |
---|
| 6118 | + map.m_len = end - cur; |
---|
| 6119 | + ret = ext4_map_blocks(NULL, inode, &map, 0); |
---|
| 6120 | + if (ret < 0) |
---|
| 6121 | + break; |
---|
| 6122 | + if (ret > 0) { |
---|
| 6123 | + path = ext4_find_extent(inode, map.m_lblk, NULL, 0); |
---|
| 6124 | + if (!IS_ERR_OR_NULL(path)) { |
---|
| 6125 | + for (j = 0; j < path->p_depth; j++) { |
---|
| 6126 | + |
---|
| 6127 | + ext4_mb_mark_bb(inode->i_sb, |
---|
| 6128 | + path[j].p_block, 1, 0); |
---|
| 6129 | + ext4_fc_record_regions(inode->i_sb, inode->i_ino, |
---|
| 6130 | + 0, path[j].p_block, 1, 1); |
---|
| 6131 | + } |
---|
| 6132 | + ext4_ext_drop_refs(path); |
---|
| 6133 | + kfree(path); |
---|
| 6134 | + } |
---|
| 6135 | + ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0); |
---|
| 6136 | + ext4_fc_record_regions(inode->i_sb, inode->i_ino, |
---|
| 6137 | + map.m_lblk, map.m_pblk, map.m_len, 1); |
---|
| 6138 | + } |
---|
| 6139 | + cur = cur + map.m_len; |
---|
| 6140 | + } |
---|
| 6141 | + |
---|
| 6142 | + return 0; |
---|
| 6143 | +} |
---|