1// SPDX-License-Identifier: GPL-2.0 2/* 3 * linux/fs/ext4/inode.c 4 * 5 * Copyright (C) 1992, 1993, 1994, 1995 6 * Remy Card (card@masi.ibp.fr) 7 * Laboratoire MASI - Institut Blaise Pascal 8 * Universite Pierre et Marie Curie (Paris VI) 9 * 10 * from 11 * 12 * linux/fs/minix/inode.c 13 * 14 * Copyright (C) 1991, 1992 Linus Torvalds 15 * 16 * 64-bit file support on 64-bit platforms by Jakub Jelinek 17 * (jj@sunsite.ms.mff.cuni.cz) 18 * 19 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 20 */ 21 22#include <linux/fs.h> 23#include <linux/time.h> 24#include <linux/highuid.h> 25#include <linux/pagemap.h> 26#include <linux/dax.h> 27#include <linux/quotaops.h> 28#include <linux/string.h> 29#include <linux/buffer_head.h> 30#include <linux/writeback.h> 31#include <linux/pagevec.h> 32#include <linux/mpage.h> 33#include <linux/namei.h> 34#include <linux/uio.h> 35#include <linux/bio.h> 36#include <linux/workqueue.h> 37#include <linux/kernel.h> 38#include <linux/printk.h> 39#include <linux/slab.h> 40#include <linux/bitops.h> 41#include <linux/iomap.h> 42#include <linux/iversion.h> 43 44#include "ext4_jbd2.h" 45#include "xattr.h" 46#include "acl.h" 47#include "truncate.h" 48 49#include <trace/events/ext4.h> 50 51static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 52 struct ext4_inode_info *ei) 53{ 54 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55 __u32 csum; 56 __u16 dummy_csum = 0; 57 int offset = offsetof(struct ext4_inode, i_checksum_lo); 58 unsigned int csum_size = sizeof(dummy_csum); 59 60 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); 61 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); 62 offset += csum_size; 63 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 64 EXT4_GOOD_OLD_INODE_SIZE - offset); 65 66 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 67 offset = offsetof(struct ext4_inode, i_checksum_hi); 68 csum = ext4_chksum(sbi, csum, (__u8 *)raw + 69 EXT4_GOOD_OLD_INODE_SIZE, 70 offset - EXT4_GOOD_OLD_INODE_SIZE); 71 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 72 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, 73 csum_size); 74 offset += csum_size; 75 } 76 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 77 EXT4_INODE_SIZE(inode->i_sb) - offset); 78 } 79 80 return csum; 81} 82 83static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 84 struct ext4_inode_info *ei) 85{ 86 __u32 provided, calculated; 87 88 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 89 cpu_to_le32(EXT4_OS_LINUX) || 90 !ext4_has_metadata_csum(inode->i_sb)) 91 return 1; 92 93 provided = le16_to_cpu(raw->i_checksum_lo); 94 calculated = ext4_inode_csum(inode, raw, ei); 95 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 96 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 97 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 98 else 99 calculated &= 0xFFFF; 100 101 return provided == calculated; 102} 103 104void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 105 struct ext4_inode_info *ei) 106{ 107 __u32 csum; 108 109 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 110 cpu_to_le32(EXT4_OS_LINUX) || 111 !ext4_has_metadata_csum(inode->i_sb)) 112 return; 113 114 csum = ext4_inode_csum(inode, raw, ei); 115 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 116 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 117 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 118 raw->i_checksum_hi = cpu_to_le16(csum >> 16); 119} 120 121static inline int ext4_begin_ordered_truncate(struct inode *inode, 122 loff_t new_size) 123{ 124 trace_ext4_begin_ordered_truncate(inode, new_size); 125 /* 126 * If jinode is zero, then we never opened the file for 127 * writing, so there's no need to call 128 * jbd2_journal_begin_ordered_truncate() since there's no 129 * outstanding writes we need to flush. 130 */ 131 if (!EXT4_I(inode)->jinode) 132 return 0; 133 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 134 EXT4_I(inode)->jinode, 135 new_size); 136} 137 138static void ext4_invalidatepage(struct page *page, unsigned int offset, 139 unsigned int length); 140static int __ext4_journalled_writepage(struct page *page, unsigned int len); 141static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 142static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 143 int pextents); 144 145/* 146 * Test whether an inode is a fast symlink. 147 * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 148 */ 149int ext4_inode_is_fast_symlink(struct inode *inode) 150{ 151 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 152 int ea_blocks = EXT4_I(inode)->i_file_acl ? 153 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 154 155 if (ext4_has_inline_data(inode)) 156 return 0; 157 158 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 159 } 160 return S_ISLNK(inode->i_mode) && inode->i_size && 161 (inode->i_size < EXT4_N_BLOCKS * 4); 162} 163 164/* 165 * Called at the last iput() if i_nlink is zero. 166 */ 167void ext4_evict_inode(struct inode *inode) 168{ 169 handle_t *handle; 170 int err; 171 /* 172 * Credits for final inode cleanup and freeing: 173 * sb + inode (ext4_orphan_del()), block bitmap, group descriptor 174 * (xattr block freeing), bitmap, group descriptor (inode freeing) 175 */ 176 int extra_credits = 6; 177 struct ext4_xattr_inode_array *ea_inode_array = NULL; 178 bool freeze_protected = false; 179 180 trace_ext4_evict_inode(inode); 181 182 if (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL) 183 ext4_evict_ea_inode(inode); 184 if (inode->i_nlink) { 185 /* 186 * When journalling data dirty buffers are tracked only in the 187 * journal. So although mm thinks everything is clean and 188 * ready for reaping the inode might still have some pages to 189 * write in the running transaction or waiting to be 190 * checkpointed. Thus calling jbd2_journal_invalidatepage() 191 * (via truncate_inode_pages()) to discard these buffers can 192 * cause data loss. Also even if we did not discard these 193 * buffers, we would have no way to find them after the inode 194 * is reaped and thus user could see stale data if he tries to 195 * read them before the transaction is checkpointed. So be 196 * careful and force everything to disk here... We use 197 * ei->i_datasync_tid to store the newest transaction 198 * containing inode's data. 199 * 200 * Note that directories do not have this problem because they 201 * don't use page cache. 202 */ 203 if (inode->i_ino != EXT4_JOURNAL_INO && 204 ext4_should_journal_data(inode) && 205 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 206 inode->i_data.nrpages) { 207 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 208 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 209 210 jbd2_complete_transaction(journal, commit_tid); 211 filemap_write_and_wait(&inode->i_data); 212 } 213 truncate_inode_pages_final(&inode->i_data); 214 215 goto no_delete; 216 } 217 218 if (is_bad_inode(inode)) 219 goto no_delete; 220 dquot_initialize(inode); 221 222 if (ext4_should_order_data(inode)) 223 ext4_begin_ordered_truncate(inode, 0); 224 truncate_inode_pages_final(&inode->i_data); 225 226 /* 227 * For inodes with journalled data, transaction commit could have 228 * dirtied the inode. And for inodes with dioread_nolock, unwritten 229 * extents converting worker could merge extents and also have dirtied 230 * the inode. Flush worker is ignoring it because of I_FREEING flag but 231 * we still need to remove the inode from the writeback lists. 232 */ 233 if (!list_empty_careful(&inode->i_io_list)) 234 inode_io_list_del(inode); 235 236 /* 237 * Protect us against freezing - iput() caller didn't have to have any 238 * protection against it. When we are in a running transaction though, 239 * we are already protected against freezing and we cannot grab further 240 * protection due to lock ordering constraints. 241 */ 242 if (!ext4_journal_current_handle()) { 243 sb_start_intwrite(inode->i_sb); 244 freeze_protected = true; 245 } 246 247 if (!IS_NOQUOTA(inode)) 248 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 249 250 /* 251 * Block bitmap, group descriptor, and inode are accounted in both 252 * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 253 */ 254 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 255 ext4_blocks_for_truncate(inode) + extra_credits - 3); 256 if (IS_ERR(handle)) { 257 ext4_std_error(inode->i_sb, PTR_ERR(handle)); 258 /* 259 * If we're going to skip the normal cleanup, we still need to 260 * make sure that the in-core orphan linked list is properly 261 * cleaned up. 262 */ 263 ext4_orphan_del(NULL, inode); 264 if (freeze_protected) 265 sb_end_intwrite(inode->i_sb); 266 goto no_delete; 267 } 268 269 if (IS_SYNC(inode)) 270 ext4_handle_sync(handle); 271 272 /* 273 * Set inode->i_size to 0 before calling ext4_truncate(). We need 274 * special handling of symlinks here because i_size is used to 275 * determine whether ext4_inode_info->i_data contains symlink data or 276 * block mappings. Setting i_size to 0 will remove its fast symlink 277 * status. Erase i_data so that it becomes a valid empty block map. 278 */ 279 if (ext4_inode_is_fast_symlink(inode)) 280 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 281 inode->i_size = 0; 282 err = ext4_mark_inode_dirty(handle, inode); 283 if (err) { 284 ext4_warning(inode->i_sb, 285 "couldn't mark inode dirty (err %d)", err); 286 goto stop_handle; 287 } 288 if (inode->i_blocks) { 289 err = ext4_truncate(inode); 290 if (err) { 291 ext4_error_err(inode->i_sb, -err, 292 "couldn't truncate inode %lu (err %d)", 293 inode->i_ino, err); 294 goto stop_handle; 295 } 296 } 297 298 /* Remove xattr references. */ 299 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 300 extra_credits); 301 if (err) { 302 ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 303stop_handle: 304 ext4_journal_stop(handle); 305 ext4_orphan_del(NULL, inode); 306 if (freeze_protected) 307 sb_end_intwrite(inode->i_sb); 308 ext4_xattr_inode_array_free(ea_inode_array); 309 goto no_delete; 310 } 311 312 /* 313 * Kill off the orphan record which ext4_truncate created. 314 * AKPM: I think this can be inside the above `if'. 315 * Note that ext4_orphan_del() has to be able to cope with the 316 * deletion of a non-existent orphan - this is because we don't 317 * know if ext4_truncate() actually created an orphan record. 318 * (Well, we could do this if we need to, but heck - it works) 319 */ 320 ext4_orphan_del(handle, inode); 321 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 322 323 /* 324 * One subtle ordering requirement: if anything has gone wrong 325 * (transaction abort, IO errors, whatever), then we can still 326 * do these next steps (the fs will already have been marked as 327 * having errors), but we can't free the inode if the mark_dirty 328 * fails. 329 */ 330 if (ext4_mark_inode_dirty(handle, inode)) 331 /* If that failed, just do the required in-core inode clear. */ 332 ext4_clear_inode(inode); 333 else 334 ext4_free_inode(handle, inode); 335 ext4_journal_stop(handle); 336 if (freeze_protected) 337 sb_end_intwrite(inode->i_sb); 338 ext4_xattr_inode_array_free(ea_inode_array); 339 return; 340no_delete: 341 /* 342 * Check out some where else accidentally dirty the evicting inode, 343 * which may probably cause inode use-after-free issues later. 344 */ 345 WARN_ON_ONCE(!list_empty_careful(&inode->i_io_list)); 346 347 if (!list_empty(&EXT4_I(inode)->i_fc_list)) 348 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM); 349 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 350} 351 352#ifdef CONFIG_QUOTA 353qsize_t *ext4_get_reserved_space(struct inode *inode) 354{ 355 return &EXT4_I(inode)->i_reserved_quota; 356} 357#endif 358 359/* 360 * Called with i_data_sem down, which is important since we can call 361 * ext4_discard_preallocations() from here. 362 */ 363void ext4_da_update_reserve_space(struct inode *inode, 364 int used, int quota_claim) 365{ 366 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 367 struct ext4_inode_info *ei = EXT4_I(inode); 368 369 spin_lock(&ei->i_block_reservation_lock); 370 trace_ext4_da_update_reserve_space(inode, used, quota_claim); 371 if (unlikely(used > ei->i_reserved_data_blocks)) { 372 ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 373 "with only %d reserved data blocks", 374 __func__, inode->i_ino, used, 375 ei->i_reserved_data_blocks); 376 WARN_ON(1); 377 used = ei->i_reserved_data_blocks; 378 } 379 380 /* Update per-inode reservations */ 381 ei->i_reserved_data_blocks -= used; 382 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 383 384 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 385 386 /* Update quota subsystem for data blocks */ 387 if (quota_claim) 388 dquot_claim_block(inode, EXT4_C2B(sbi, used)); 389 else { 390 /* 391 * We did fallocate with an offset that is already delayed 392 * allocated. So on delayed allocated writeback we should 393 * not re-claim the quota for fallocated blocks. 394 */ 395 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 396 } 397 398 /* 399 * If we have done all the pending block allocations and if 400 * there aren't any writers on the inode, we can discard the 401 * inode's preallocations. 402 */ 403 if ((ei->i_reserved_data_blocks == 0) && 404 !inode_is_open_for_write(inode)) 405 ext4_discard_preallocations(inode, 0); 406} 407 408static int __check_block_validity(struct inode *inode, const char *func, 409 unsigned int line, 410 struct ext4_map_blocks *map) 411{ 412 if (ext4_has_feature_journal(inode->i_sb) && 413 (inode->i_ino == 414 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 415 return 0; 416 if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 417 ext4_error_inode(inode, func, line, map->m_pblk, 418 "lblock %lu mapped to illegal pblock %llu " 419 "(length %d)", (unsigned long) map->m_lblk, 420 map->m_pblk, map->m_len); 421 return -EFSCORRUPTED; 422 } 423 return 0; 424} 425 426int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 427 ext4_lblk_t len) 428{ 429 int ret; 430 431 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 432 return fscrypt_zeroout_range(inode, lblk, pblk, len); 433 434 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 435 if (ret > 0) 436 ret = 0; 437 438 return ret; 439} 440 441#define check_block_validity(inode, map) \ 442 __check_block_validity((inode), __func__, __LINE__, (map)) 443 444#ifdef ES_AGGRESSIVE_TEST 445static void ext4_map_blocks_es_recheck(handle_t *handle, 446 struct inode *inode, 447 struct ext4_map_blocks *es_map, 448 struct ext4_map_blocks *map, 449 int flags) 450{ 451 int retval; 452 453 map->m_flags = 0; 454 /* 455 * There is a race window that the result is not the same. 456 * e.g. xfstests #223 when dioread_nolock enables. The reason 457 * is that we lookup a block mapping in extent status tree with 458 * out taking i_data_sem. So at the time the unwritten extent 459 * could be converted. 460 */ 461 down_read(&EXT4_I(inode)->i_data_sem); 462 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 463 retval = ext4_ext_map_blocks(handle, inode, map, 0); 464 } else { 465 retval = ext4_ind_map_blocks(handle, inode, map, 0); 466 } 467 up_read((&EXT4_I(inode)->i_data_sem)); 468 469 /* 470 * We don't check m_len because extent will be collpased in status 471 * tree. So the m_len might not equal. 472 */ 473 if (es_map->m_lblk != map->m_lblk || 474 es_map->m_flags != map->m_flags || 475 es_map->m_pblk != map->m_pblk) { 476 printk("ES cache assertion failed for inode: %lu " 477 "es_cached ex [%d/%d/%llu/%x] != " 478 "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 479 inode->i_ino, es_map->m_lblk, es_map->m_len, 480 es_map->m_pblk, es_map->m_flags, map->m_lblk, 481 map->m_len, map->m_pblk, map->m_flags, 482 retval, flags); 483 } 484} 485#endif /* ES_AGGRESSIVE_TEST */ 486 487/* 488 * The ext4_map_blocks() function tries to look up the requested blocks, 489 * and returns if the blocks are already mapped. 490 * 491 * Otherwise it takes the write lock of the i_data_sem and allocate blocks 492 * and store the allocated blocks in the result buffer head and mark it 493 * mapped. 494 * 495 * If file type is extents based, it will call ext4_ext_map_blocks(), 496 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 497 * based files 498 * 499 * On success, it returns the number of blocks being mapped or allocated. if 500 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 501 * is marked as unwritten. If the create == 1, it will mark @map as mapped. 502 * 503 * It returns 0 if plain look up failed (blocks have not been allocated), in 504 * that case, @map is returned as unmapped but we still do fill map->m_len to 505 * indicate the length of a hole starting at map->m_lblk. 506 * 507 * It returns the error in case of allocation failure. 508 */ 509int ext4_map_blocks(handle_t *handle, struct inode *inode, 510 struct ext4_map_blocks *map, int flags) 511{ 512 struct extent_status es; 513 int retval; 514 int ret = 0; 515#ifdef ES_AGGRESSIVE_TEST 516 struct ext4_map_blocks orig_map; 517 518 memcpy(&orig_map, map, sizeof(*map)); 519#endif 520 521 map->m_flags = 0; 522 ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 523 flags, map->m_len, (unsigned long) map->m_lblk); 524 525 /* 526 * ext4_map_blocks returns an int, and m_len is an unsigned int 527 */ 528 if (unlikely(map->m_len > INT_MAX)) 529 map->m_len = INT_MAX; 530 531 /* We can handle the block number less than EXT_MAX_BLOCKS */ 532 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 533 return -EFSCORRUPTED; 534 535 /* Lookup extent status tree firstly */ 536 if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 537 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 538 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 539 map->m_pblk = ext4_es_pblock(&es) + 540 map->m_lblk - es.es_lblk; 541 map->m_flags |= ext4_es_is_written(&es) ? 542 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 543 retval = es.es_len - (map->m_lblk - es.es_lblk); 544 if (retval > map->m_len) 545 retval = map->m_len; 546 map->m_len = retval; 547 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 548 map->m_pblk = 0; 549 retval = es.es_len - (map->m_lblk - es.es_lblk); 550 if (retval > map->m_len) 551 retval = map->m_len; 552 map->m_len = retval; 553 retval = 0; 554 } else { 555 BUG(); 556 } 557#ifdef ES_AGGRESSIVE_TEST 558 ext4_map_blocks_es_recheck(handle, inode, map, 559 &orig_map, flags); 560#endif 561 goto found; 562 } 563 564 /* 565 * Try to see if we can get the block without requesting a new 566 * file system block. 567 */ 568 down_read(&EXT4_I(inode)->i_data_sem); 569 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 570 retval = ext4_ext_map_blocks(handle, inode, map, 0); 571 } else { 572 retval = ext4_ind_map_blocks(handle, inode, map, 0); 573 } 574 if (retval > 0) { 575 unsigned int status; 576 577 if (unlikely(retval != map->m_len)) { 578 ext4_warning(inode->i_sb, 579 "ES len assertion failed for inode " 580 "%lu: retval %d != map->m_len %d", 581 inode->i_ino, retval, map->m_len); 582 WARN_ON(1); 583 } 584 585 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 586 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 587 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 588 !(status & EXTENT_STATUS_WRITTEN) && 589 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 590 map->m_lblk + map->m_len - 1)) 591 status |= EXTENT_STATUS_DELAYED; 592 ret = ext4_es_insert_extent(inode, map->m_lblk, 593 map->m_len, map->m_pblk, status); 594 if (ret < 0) 595 retval = ret; 596 } 597 up_read((&EXT4_I(inode)->i_data_sem)); 598 599found: 600 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 601 ret = check_block_validity(inode, map); 602 if (ret != 0) 603 return ret; 604 } 605 606 /* If it is only a block(s) look up */ 607 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 608 return retval; 609 610 /* 611 * Returns if the blocks have already allocated 612 * 613 * Note that if blocks have been preallocated 614 * ext4_ext_get_block() returns the create = 0 615 * with buffer head unmapped. 616 */ 617 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 618 /* 619 * If we need to convert extent to unwritten 620 * we continue and do the actual work in 621 * ext4_ext_map_blocks() 622 */ 623 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 624 return retval; 625 626 /* 627 * Here we clear m_flags because after allocating an new extent, 628 * it will be set again. 629 */ 630 map->m_flags &= ~EXT4_MAP_FLAGS; 631 632 /* 633 * New blocks allocate and/or writing to unwritten extent 634 * will possibly result in updating i_data, so we take 635 * the write lock of i_data_sem, and call get_block() 636 * with create == 1 flag. 637 */ 638 down_write(&EXT4_I(inode)->i_data_sem); 639 640 /* 641 * We need to check for EXT4 here because migrate 642 * could have changed the inode type in between 643 */ 644 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 645 retval = ext4_ext_map_blocks(handle, inode, map, flags); 646 } else { 647 retval = ext4_ind_map_blocks(handle, inode, map, flags); 648 649 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 650 /* 651 * We allocated new blocks which will result in 652 * i_data's format changing. Force the migrate 653 * to fail by clearing migrate flags 654 */ 655 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 656 } 657 } 658 659 if (retval > 0) { 660 unsigned int status; 661 662 if (unlikely(retval != map->m_len)) { 663 ext4_warning(inode->i_sb, 664 "ES len assertion failed for inode " 665 "%lu: retval %d != map->m_len %d", 666 inode->i_ino, retval, map->m_len); 667 WARN_ON(1); 668 } 669 670 /* 671 * We have to zeroout blocks before inserting them into extent 672 * status tree. Otherwise someone could look them up there and 673 * use them before they are really zeroed. We also have to 674 * unmap metadata before zeroing as otherwise writeback can 675 * overwrite zeros with stale data from block device. 676 */ 677 if (flags & EXT4_GET_BLOCKS_ZERO && 678 map->m_flags & EXT4_MAP_MAPPED && 679 map->m_flags & EXT4_MAP_NEW) { 680 ret = ext4_issue_zeroout(inode, map->m_lblk, 681 map->m_pblk, map->m_len); 682 if (ret) { 683 retval = ret; 684 goto out_sem; 685 } 686 } 687 688 /* 689 * If the extent has been zeroed out, we don't need to update 690 * extent status tree. 691 */ 692 if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 693 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 694 if (ext4_es_is_written(&es)) 695 goto out_sem; 696 } 697 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 698 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 699 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 700 !(status & EXTENT_STATUS_WRITTEN) && 701 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 702 map->m_lblk + map->m_len - 1)) 703 status |= EXTENT_STATUS_DELAYED; 704 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 705 map->m_pblk, status); 706 if (ret < 0) { 707 retval = ret; 708 goto out_sem; 709 } 710 } 711 712out_sem: 713 up_write((&EXT4_I(inode)->i_data_sem)); 714 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 715 ret = check_block_validity(inode, map); 716 if (ret != 0) 717 return ret; 718 719 /* 720 * Inodes with freshly allocated blocks where contents will be 721 * visible after transaction commit must be on transaction's 722 * ordered data list. 723 */ 724 if (map->m_flags & EXT4_MAP_NEW && 725 !(map->m_flags & EXT4_MAP_UNWRITTEN) && 726 !(flags & EXT4_GET_BLOCKS_ZERO) && 727 !ext4_is_quota_file(inode) && 728 ext4_should_order_data(inode)) { 729 loff_t start_byte = 730 (loff_t)map->m_lblk << inode->i_blkbits; 731 loff_t length = (loff_t)map->m_len << inode->i_blkbits; 732 733 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 734 ret = ext4_jbd2_inode_add_wait(handle, inode, 735 start_byte, length); 736 else 737 ret = ext4_jbd2_inode_add_write(handle, inode, 738 start_byte, length); 739 if (ret) 740 return ret; 741 } 742 } 743 if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || 744 map->m_flags & EXT4_MAP_MAPPED)) 745 ext4_fc_track_range(handle, inode, map->m_lblk, 746 map->m_lblk + map->m_len - 1); 747 if (retval < 0) 748 ext_debug(inode, "failed with err %d\n", retval); 749 return retval; 750} 751 752/* 753 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 754 * we have to be careful as someone else may be manipulating b_state as well. 755 */ 756static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 757{ 758 unsigned long old_state; 759 unsigned long new_state; 760 761 flags &= EXT4_MAP_FLAGS; 762 763 /* Dummy buffer_head? Set non-atomically. */ 764 if (!bh->b_page) { 765 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 766 return; 767 } 768 /* 769 * Someone else may be modifying b_state. Be careful! This is ugly but 770 * once we get rid of using bh as a container for mapping information 771 * to pass to / from get_block functions, this can go away. 772 */ 773 do { 774 old_state = READ_ONCE(bh->b_state); 775 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 776 } while (unlikely( 777 cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 778} 779 780static int _ext4_get_block(struct inode *inode, sector_t iblock, 781 struct buffer_head *bh, int flags) 782{ 783 struct ext4_map_blocks map; 784 int ret = 0; 785 786 if (ext4_has_inline_data(inode)) 787 return -ERANGE; 788 789 map.m_lblk = iblock; 790 map.m_len = bh->b_size >> inode->i_blkbits; 791 792 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 793 flags); 794 if (ret > 0) { 795 map_bh(bh, inode->i_sb, map.m_pblk); 796 ext4_update_bh_state(bh, map.m_flags); 797 bh->b_size = inode->i_sb->s_blocksize * map.m_len; 798 ret = 0; 799 } else if (ret == 0) { 800 /* hole case, need to fill in bh->b_size */ 801 bh->b_size = inode->i_sb->s_blocksize * map.m_len; 802 } 803 return ret; 804} 805 806int ext4_get_block(struct inode *inode, sector_t iblock, 807 struct buffer_head *bh, int create) 808{ 809 return _ext4_get_block(inode, iblock, bh, 810 create ? EXT4_GET_BLOCKS_CREATE : 0); 811} 812 813/* 814 * Get block function used when preparing for buffered write if we require 815 * creating an unwritten extent if blocks haven't been allocated. The extent 816 * will be converted to written after the IO is complete. 817 */ 818int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 819 struct buffer_head *bh_result, int create) 820{ 821 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 822 inode->i_ino, create); 823 return _ext4_get_block(inode, iblock, bh_result, 824 EXT4_GET_BLOCKS_IO_CREATE_EXT); 825} 826 827/* Maximum number of blocks we map for direct IO at once. */ 828#define DIO_MAX_BLOCKS 4096 829 830/* 831 * `handle' can be NULL if create is zero 832 */ 833struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 834 ext4_lblk_t block, int map_flags) 835{ 836 struct ext4_map_blocks map; 837 struct buffer_head *bh; 838 int create = map_flags & EXT4_GET_BLOCKS_CREATE; 839 int err; 840 841 J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 842 || handle != NULL || create == 0); 843 844 map.m_lblk = block; 845 map.m_len = 1; 846 err = ext4_map_blocks(handle, inode, &map, map_flags); 847 848 if (err == 0) 849 return create ? ERR_PTR(-ENOSPC) : NULL; 850 if (err < 0) 851 return ERR_PTR(err); 852 853 bh = sb_getblk(inode->i_sb, map.m_pblk); 854 if (unlikely(!bh)) 855 return ERR_PTR(-ENOMEM); 856 if (map.m_flags & EXT4_MAP_NEW) { 857 J_ASSERT(create != 0); 858 J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 859 || (handle != NULL)); 860 861 /* 862 * Now that we do not always journal data, we should 863 * keep in mind whether this should always journal the 864 * new buffer as metadata. For now, regular file 865 * writes use ext4_get_block instead, so it's not a 866 * problem. 867 */ 868 lock_buffer(bh); 869 BUFFER_TRACE(bh, "call get_create_access"); 870 err = ext4_journal_get_create_access(handle, bh); 871 if (unlikely(err)) { 872 unlock_buffer(bh); 873 goto errout; 874 } 875 if (!buffer_uptodate(bh)) { 876 memset(bh->b_data, 0, inode->i_sb->s_blocksize); 877 set_buffer_uptodate(bh); 878 } 879 unlock_buffer(bh); 880 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 881 err = ext4_handle_dirty_metadata(handle, inode, bh); 882 if (unlikely(err)) 883 goto errout; 884 } else 885 BUFFER_TRACE(bh, "not a new buffer"); 886 return bh; 887errout: 888 brelse(bh); 889 return ERR_PTR(err); 890} 891 892struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 893 ext4_lblk_t block, int map_flags) 894{ 895 struct buffer_head *bh; 896 int ret; 897 898 bh = ext4_getblk(handle, inode, block, map_flags); 899 if (IS_ERR(bh)) 900 return bh; 901 if (!bh || ext4_buffer_uptodate(bh)) 902 return bh; 903 904 ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 905 if (ret) { 906 put_bh(bh); 907 return ERR_PTR(ret); 908 } 909 return bh; 910} 911 912/* Read a contiguous batch of blocks. */ 913int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 914 bool wait, struct buffer_head **bhs) 915{ 916 int i, err; 917 918 for (i = 0; i < bh_count; i++) { 919 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 920 if (IS_ERR(bhs[i])) { 921 err = PTR_ERR(bhs[i]); 922 bh_count = i; 923 goto out_brelse; 924 } 925 } 926 927 for (i = 0; i < bh_count; i++) 928 /* Note that NULL bhs[i] is valid because of holes. */ 929 if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 930 ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 931 932 if (!wait) 933 return 0; 934 935 for (i = 0; i < bh_count; i++) 936 if (bhs[i]) 937 wait_on_buffer(bhs[i]); 938 939 for (i = 0; i < bh_count; i++) { 940 if (bhs[i] && !buffer_uptodate(bhs[i])) { 941 err = -EIO; 942 goto out_brelse; 943 } 944 } 945 return 0; 946 947out_brelse: 948 for (i = 0; i < bh_count; i++) { 949 brelse(bhs[i]); 950 bhs[i] = NULL; 951 } 952 return err; 953} 954 955int ext4_walk_page_buffers(handle_t *handle, 956 struct buffer_head *head, 957 unsigned from, 958 unsigned to, 959 int *partial, 960 int (*fn)(handle_t *handle, 961 struct buffer_head *bh)) 962{ 963 struct buffer_head *bh; 964 unsigned block_start, block_end; 965 unsigned blocksize = head->b_size; 966 int err, ret = 0; 967 struct buffer_head *next; 968 969 for (bh = head, block_start = 0; 970 ret == 0 && (bh != head || !block_start); 971 block_start = block_end, bh = next) { 972 next = bh->b_this_page; 973 block_end = block_start + blocksize; 974 if (block_end <= from || block_start >= to) { 975 if (partial && !buffer_uptodate(bh)) 976 *partial = 1; 977 continue; 978 } 979 err = (*fn)(handle, bh); 980 if (!ret) 981 ret = err; 982 } 983 return ret; 984} 985 986/* 987 * To preserve ordering, it is essential that the hole instantiation and 988 * the data write be encapsulated in a single transaction. We cannot 989 * close off a transaction and start a new one between the ext4_get_block() 990 * and the commit_write(). So doing the jbd2_journal_start at the start of 991 * prepare_write() is the right place. 992 * 993 * Also, this function can nest inside ext4_writepage(). In that case, we 994 * *know* that ext4_writepage() has generated enough buffer credits to do the 995 * whole page. So we won't block on the journal in that case, which is good, 996 * because the caller may be PF_MEMALLOC. 997 * 998 * By accident, ext4 can be reentered when a transaction is open via 999 * quota file writes. If we were to commit the transaction while thus 1000 * reentered, there can be a deadlock - we would be holding a quota 1001 * lock, and the commit would never complete if another thread had a 1002 * transaction open and was blocking on the quota lock - a ranking 1003 * violation. 1004 * 1005 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1006 * will _not_ run commit under these circumstances because handle->h_ref 1007 * is elevated. We'll still have enough credits for the tiny quotafile 1008 * write. 1009 */ 1010int do_journal_get_write_access(handle_t *handle, 1011 struct buffer_head *bh) 1012{ 1013 int dirty = buffer_dirty(bh); 1014 int ret; 1015 1016 if (!buffer_mapped(bh) || buffer_freed(bh)) 1017 return 0; 1018 /* 1019 * __block_write_begin() could have dirtied some buffers. Clean 1020 * the dirty bit as jbd2_journal_get_write_access() could complain 1021 * otherwise about fs integrity issues. Setting of the dirty bit 1022 * by __block_write_begin() isn't a real problem here as we clear 1023 * the bit before releasing a page lock and thus writeback cannot 1024 * ever write the buffer. 1025 */ 1026 if (dirty) 1027 clear_buffer_dirty(bh); 1028 BUFFER_TRACE(bh, "get write access"); 1029 ret = ext4_journal_get_write_access(handle, bh); 1030 if (!ret && dirty) 1031 ret = ext4_handle_dirty_metadata(handle, NULL, bh); 1032 return ret; 1033} 1034 1035#ifdef CONFIG_FS_ENCRYPTION 1036static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 1037 get_block_t *get_block) 1038{ 1039 unsigned from = pos & (PAGE_SIZE - 1); 1040 unsigned to = from + len; 1041 struct inode *inode = page->mapping->host; 1042 unsigned block_start, block_end; 1043 sector_t block; 1044 int err = 0; 1045 unsigned blocksize = inode->i_sb->s_blocksize; 1046 unsigned bbits; 1047 struct buffer_head *bh, *head, *wait[2]; 1048 int nr_wait = 0; 1049 int i; 1050 1051 BUG_ON(!PageLocked(page)); 1052 BUG_ON(from > PAGE_SIZE); 1053 BUG_ON(to > PAGE_SIZE); 1054 BUG_ON(from > to); 1055 1056 if (!page_has_buffers(page)) 1057 create_empty_buffers(page, blocksize, 0); 1058 head = page_buffers(page); 1059 bbits = ilog2(blocksize); 1060 block = (sector_t)page->index << (PAGE_SHIFT - bbits); 1061 1062 for (bh = head, block_start = 0; bh != head || !block_start; 1063 block++, block_start = block_end, bh = bh->b_this_page) { 1064 block_end = block_start + blocksize; 1065 if (block_end <= from || block_start >= to) { 1066 if (PageUptodate(page)) { 1067 if (!buffer_uptodate(bh)) 1068 set_buffer_uptodate(bh); 1069 } 1070 continue; 1071 } 1072 if (buffer_new(bh)) 1073 clear_buffer_new(bh); 1074 if (!buffer_mapped(bh)) { 1075 WARN_ON(bh->b_size != blocksize); 1076 err = get_block(inode, block, bh, 1); 1077 if (err) 1078 break; 1079 if (buffer_new(bh)) { 1080 if (PageUptodate(page)) { 1081 clear_buffer_new(bh); 1082 set_buffer_uptodate(bh); 1083 mark_buffer_dirty(bh); 1084 continue; 1085 } 1086 if (block_end > to || block_start < from) 1087 zero_user_segments(page, to, block_end, 1088 block_start, from); 1089 continue; 1090 } 1091 } 1092 if (PageUptodate(page)) { 1093 if (!buffer_uptodate(bh)) 1094 set_buffer_uptodate(bh); 1095 continue; 1096 } 1097 if (!buffer_uptodate(bh) && !buffer_delay(bh) && 1098 !buffer_unwritten(bh) && 1099 (block_start < from || block_end > to)) { 1100 ext4_read_bh_lock(bh, 0, false); 1101 wait[nr_wait++] = bh; 1102 } 1103 } 1104 /* 1105 * If we issued read requests, let them complete. 1106 */ 1107 for (i = 0; i < nr_wait; i++) { 1108 wait_on_buffer(wait[i]); 1109 if (!buffer_uptodate(wait[i])) 1110 err = -EIO; 1111 } 1112 if (unlikely(err)) { 1113 page_zero_new_buffers(page, from, to); 1114 } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 1115 for (i = 0; i < nr_wait; i++) { 1116 int err2; 1117 1118 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 1119 bh_offset(wait[i])); 1120 if (err2) { 1121 clear_buffer_uptodate(wait[i]); 1122 err = err2; 1123 } 1124 } 1125 } 1126 1127 return err; 1128} 1129#endif 1130 1131static int ext4_write_begin(struct file *file, struct address_space *mapping, 1132 loff_t pos, unsigned len, unsigned flags, 1133 struct page **pagep, void **fsdata) 1134{ 1135 struct inode *inode = mapping->host; 1136 int ret, needed_blocks; 1137 handle_t *handle; 1138 int retries = 0; 1139 struct page *page; 1140 pgoff_t index; 1141 unsigned from, to; 1142 1143 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 1144 return -EIO; 1145 1146 trace_ext4_write_begin(inode, pos, len, flags); 1147 /* 1148 * Reserve one block more for addition to orphan list in case 1149 * we allocate blocks but write fails for some reason 1150 */ 1151 needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1152 index = pos >> PAGE_SHIFT; 1153 from = pos & (PAGE_SIZE - 1); 1154 to = from + len; 1155 1156 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1157 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1158 flags, pagep); 1159 if (ret < 0) 1160 return ret; 1161 if (ret == 1) 1162 return 0; 1163 } 1164 1165 /* 1166 * grab_cache_page_write_begin() can take a long time if the 1167 * system is thrashing due to memory pressure, or if the page 1168 * is being written back. So grab it first before we start 1169 * the transaction handle. This also allows us to allocate 1170 * the page (if needed) without using GFP_NOFS. 1171 */ 1172retry_grab: 1173 page = grab_cache_page_write_begin(mapping, index, flags); 1174 if (!page) 1175 return -ENOMEM; 1176 /* 1177 * The same as page allocation, we prealloc buffer heads before 1178 * starting the handle. 1179 */ 1180 if (!page_has_buffers(page)) 1181 create_empty_buffers(page, inode->i_sb->s_blocksize, 0); 1182 1183 unlock_page(page); 1184 1185retry_journal: 1186 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1187 if (IS_ERR(handle)) { 1188 put_page(page); 1189 return PTR_ERR(handle); 1190 } 1191 1192 lock_page(page); 1193 if (page->mapping != mapping) { 1194 /* The page got truncated from under us */ 1195 unlock_page(page); 1196 put_page(page); 1197 ext4_journal_stop(handle); 1198 goto retry_grab; 1199 } 1200 /* In case writeback began while the page was unlocked */ 1201 wait_for_stable_page(page); 1202 1203#ifdef CONFIG_FS_ENCRYPTION 1204 if (ext4_should_dioread_nolock(inode)) 1205 ret = ext4_block_write_begin(page, pos, len, 1206 ext4_get_block_unwritten); 1207 else 1208 ret = ext4_block_write_begin(page, pos, len, 1209 ext4_get_block); 1210#else 1211 if (ext4_should_dioread_nolock(inode)) 1212 ret = __block_write_begin(page, pos, len, 1213 ext4_get_block_unwritten); 1214 else 1215 ret = __block_write_begin(page, pos, len, ext4_get_block); 1216#endif 1217 if (!ret && ext4_should_journal_data(inode)) { 1218 ret = ext4_walk_page_buffers(handle, page_buffers(page), 1219 from, to, NULL, 1220 do_journal_get_write_access); 1221 } 1222 1223 if (ret) { 1224 bool extended = (pos + len > inode->i_size) && 1225 !ext4_verity_in_progress(inode); 1226 1227 unlock_page(page); 1228 /* 1229 * __block_write_begin may have instantiated a few blocks 1230 * outside i_size. Trim these off again. Don't need 1231 * i_size_read because we hold i_mutex. 1232 * 1233 * Add inode to orphan list in case we crash before 1234 * truncate finishes 1235 */ 1236 if (extended && ext4_can_truncate(inode)) 1237 ext4_orphan_add(handle, inode); 1238 1239 ext4_journal_stop(handle); 1240 if (extended) { 1241 ext4_truncate_failed_write(inode); 1242 /* 1243 * If truncate failed early the inode might 1244 * still be on the orphan list; we need to 1245 * make sure the inode is removed from the 1246 * orphan list in that case. 1247 */ 1248 if (inode->i_nlink) 1249 ext4_orphan_del(NULL, inode); 1250 } 1251 1252 if (ret == -ENOSPC && 1253 ext4_should_retry_alloc(inode->i_sb, &retries)) 1254 goto retry_journal; 1255 put_page(page); 1256 return ret; 1257 } 1258 *pagep = page; 1259 return ret; 1260} 1261 1262/* For write_end() in data=journal mode */ 1263static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1264{ 1265 int ret; 1266 if (!buffer_mapped(bh) || buffer_freed(bh)) 1267 return 0; 1268 set_buffer_uptodate(bh); 1269 ret = ext4_handle_dirty_metadata(handle, NULL, bh); 1270 clear_buffer_meta(bh); 1271 clear_buffer_prio(bh); 1272 return ret; 1273} 1274 1275/* 1276 * We need to pick up the new inode size which generic_commit_write gave us 1277 * `file' can be NULL - eg, when called from page_symlink(). 1278 * 1279 * ext4 never places buffers on inode->i_mapping->private_list. metadata 1280 * buffers are managed internally. 1281 */ 1282static int ext4_write_end(struct file *file, 1283 struct address_space *mapping, 1284 loff_t pos, unsigned len, unsigned copied, 1285 struct page *page, void *fsdata) 1286{ 1287 handle_t *handle = ext4_journal_current_handle(); 1288 struct inode *inode = mapping->host; 1289 loff_t old_size = inode->i_size; 1290 int ret = 0, ret2; 1291 int i_size_changed = 0; 1292 bool verity = ext4_verity_in_progress(inode); 1293 1294 trace_ext4_write_end(inode, pos, len, copied); 1295 1296 if (ext4_has_inline_data(inode) && 1297 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) 1298 return ext4_write_inline_data_end(inode, pos, len, copied, page); 1299 1300 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1301 /* 1302 * it's important to update i_size while still holding page lock: 1303 * page writeout could otherwise come in and zero beyond i_size. 1304 * 1305 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1306 * blocks are being written past EOF, so skip the i_size update. 1307 */ 1308 if (!verity) 1309 i_size_changed = ext4_update_inode_size(inode, pos + copied); 1310 unlock_page(page); 1311 put_page(page); 1312 1313 if (old_size < pos && !verity) 1314 pagecache_isize_extended(inode, old_size, pos); 1315 /* 1316 * Don't mark the inode dirty under page lock. First, it unnecessarily 1317 * makes the holding time of page lock longer. Second, it forces lock 1318 * ordering of page lock and transaction start for journaling 1319 * filesystems. 1320 */ 1321 if (i_size_changed) 1322 ret = ext4_mark_inode_dirty(handle, inode); 1323 1324 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1325 /* if we have allocated more blocks and copied 1326 * less. We will have blocks allocated outside 1327 * inode->i_size. So truncate them 1328 */ 1329 ext4_orphan_add(handle, inode); 1330 1331 ret2 = ext4_journal_stop(handle); 1332 if (!ret) 1333 ret = ret2; 1334 1335 if (pos + len > inode->i_size && !verity) { 1336 ext4_truncate_failed_write(inode); 1337 /* 1338 * If truncate failed early the inode might still be 1339 * on the orphan list; we need to make sure the inode 1340 * is removed from the orphan list in that case. 1341 */ 1342 if (inode->i_nlink) 1343 ext4_orphan_del(NULL, inode); 1344 } 1345 1346 return ret ? ret : copied; 1347} 1348 1349/* 1350 * This is a private version of page_zero_new_buffers() which doesn't 1351 * set the buffer to be dirty, since in data=journalled mode we need 1352 * to call ext4_handle_dirty_metadata() instead. 1353 */ 1354static void ext4_journalled_zero_new_buffers(handle_t *handle, 1355 struct page *page, 1356 unsigned from, unsigned to) 1357{ 1358 unsigned int block_start = 0, block_end; 1359 struct buffer_head *head, *bh; 1360 1361 bh = head = page_buffers(page); 1362 do { 1363 block_end = block_start + bh->b_size; 1364 if (buffer_new(bh)) { 1365 if (block_end > from && block_start < to) { 1366 if (!PageUptodate(page)) { 1367 unsigned start, size; 1368 1369 start = max(from, block_start); 1370 size = min(to, block_end) - start; 1371 1372 zero_user(page, start, size); 1373 write_end_fn(handle, bh); 1374 } 1375 clear_buffer_new(bh); 1376 } 1377 } 1378 block_start = block_end; 1379 bh = bh->b_this_page; 1380 } while (bh != head); 1381} 1382 1383static int ext4_journalled_write_end(struct file *file, 1384 struct address_space *mapping, 1385 loff_t pos, unsigned len, unsigned copied, 1386 struct page *page, void *fsdata) 1387{ 1388 handle_t *handle = ext4_journal_current_handle(); 1389 struct inode *inode = mapping->host; 1390 loff_t old_size = inode->i_size; 1391 int ret = 0, ret2; 1392 int partial = 0; 1393 unsigned from, to; 1394 int size_changed = 0; 1395 bool verity = ext4_verity_in_progress(inode); 1396 1397 trace_ext4_journalled_write_end(inode, pos, len, copied); 1398 from = pos & (PAGE_SIZE - 1); 1399 to = from + len; 1400 1401 BUG_ON(!ext4_handle_valid(handle)); 1402 1403 if (ext4_has_inline_data(inode)) 1404 return ext4_write_inline_data_end(inode, pos, len, copied, page); 1405 1406 if (unlikely(copied < len) && !PageUptodate(page)) { 1407 copied = 0; 1408 ext4_journalled_zero_new_buffers(handle, page, from, to); 1409 } else { 1410 if (unlikely(copied < len)) 1411 ext4_journalled_zero_new_buffers(handle, page, 1412 from + copied, to); 1413 ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1414 from + copied, &partial, 1415 write_end_fn); 1416 if (!partial) 1417 SetPageUptodate(page); 1418 } 1419 if (!verity) 1420 size_changed = ext4_update_inode_size(inode, pos + copied); 1421 ext4_set_inode_state(inode, EXT4_STATE_JDATA); 1422 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1423 unlock_page(page); 1424 put_page(page); 1425 1426 if (old_size < pos && !verity) 1427 pagecache_isize_extended(inode, old_size, pos); 1428 1429 if (size_changed) { 1430 ret2 = ext4_mark_inode_dirty(handle, inode); 1431 if (!ret) 1432 ret = ret2; 1433 } 1434 1435 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1436 /* if we have allocated more blocks and copied 1437 * less. We will have blocks allocated outside 1438 * inode->i_size. So truncate them 1439 */ 1440 ext4_orphan_add(handle, inode); 1441 1442 ret2 = ext4_journal_stop(handle); 1443 if (!ret) 1444 ret = ret2; 1445 if (pos + len > inode->i_size && !verity) { 1446 ext4_truncate_failed_write(inode); 1447 /* 1448 * If truncate failed early the inode might still be 1449 * on the orphan list; we need to make sure the inode 1450 * is removed from the orphan list in that case. 1451 */ 1452 if (inode->i_nlink) 1453 ext4_orphan_del(NULL, inode); 1454 } 1455 1456 return ret ? ret : copied; 1457} 1458 1459/* 1460 * Reserve space for a single cluster 1461 */ 1462static int ext4_da_reserve_space(struct inode *inode) 1463{ 1464 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1465 struct ext4_inode_info *ei = EXT4_I(inode); 1466 int ret; 1467 1468 /* 1469 * We will charge metadata quota at writeout time; this saves 1470 * us from metadata over-estimation, though we may go over by 1471 * a small amount in the end. Here we just reserve for data. 1472 */ 1473 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 1474 if (ret) 1475 return ret; 1476 1477 spin_lock(&ei->i_block_reservation_lock); 1478 if (ext4_claim_free_clusters(sbi, 1, 0)) { 1479 spin_unlock(&ei->i_block_reservation_lock); 1480 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1481 return -ENOSPC; 1482 } 1483 ei->i_reserved_data_blocks++; 1484 trace_ext4_da_reserve_space(inode); 1485 spin_unlock(&ei->i_block_reservation_lock); 1486 1487 return 0; /* success */ 1488} 1489 1490void ext4_da_release_space(struct inode *inode, int to_free) 1491{ 1492 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1493 struct ext4_inode_info *ei = EXT4_I(inode); 1494 1495 if (!to_free) 1496 return; /* Nothing to release, exit */ 1497 1498 spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1499 1500 trace_ext4_da_release_space(inode, to_free); 1501 if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1502 /* 1503 * if there aren't enough reserved blocks, then the 1504 * counter is messed up somewhere. Since this 1505 * function is called from invalidate page, it's 1506 * harmless to return without any action. 1507 */ 1508 ext4_warning(inode->i_sb, "ext4_da_release_space: " 1509 "ino %lu, to_free %d with only %d reserved " 1510 "data blocks", inode->i_ino, to_free, 1511 ei->i_reserved_data_blocks); 1512 WARN_ON(1); 1513 to_free = ei->i_reserved_data_blocks; 1514 } 1515 ei->i_reserved_data_blocks -= to_free; 1516 1517 /* update fs dirty data blocks counter */ 1518 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1519 1520 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1521 1522 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1523} 1524 1525/* 1526 * Delayed allocation stuff 1527 */ 1528 1529struct mpage_da_data { 1530 struct inode *inode; 1531 struct writeback_control *wbc; 1532 1533 pgoff_t first_page; /* The first page to write */ 1534 pgoff_t next_page; /* Current page to examine */ 1535 pgoff_t last_page; /* Last page to examine */ 1536 /* 1537 * Extent to map - this can be after first_page because that can be 1538 * fully mapped. We somewhat abuse m_flags to store whether the extent 1539 * is delalloc or unwritten. 1540 */ 1541 struct ext4_map_blocks map; 1542 struct ext4_io_submit io_submit; /* IO submission data */ 1543 unsigned int do_map:1; 1544 unsigned int scanned_until_end:1; 1545}; 1546 1547static void mpage_release_unused_pages(struct mpage_da_data *mpd, 1548 bool invalidate) 1549{ 1550 int nr_pages, i; 1551 pgoff_t index, end; 1552 struct pagevec pvec; 1553 struct inode *inode = mpd->inode; 1554 struct address_space *mapping = inode->i_mapping; 1555 1556 /* This is necessary when next_page == 0. */ 1557 if (mpd->first_page >= mpd->next_page) 1558 return; 1559 1560 mpd->scanned_until_end = 0; 1561 index = mpd->first_page; 1562 end = mpd->next_page - 1; 1563 if (invalidate) { 1564 ext4_lblk_t start, last; 1565 start = index << (PAGE_SHIFT - inode->i_blkbits); 1566 last = end << (PAGE_SHIFT - inode->i_blkbits); 1567 1568 /* 1569 * avoid racing with extent status tree scans made by 1570 * ext4_insert_delayed_block() 1571 */ 1572 down_write(&EXT4_I(inode)->i_data_sem); 1573 ext4_es_remove_extent(inode, start, last - start + 1); 1574 up_write(&EXT4_I(inode)->i_data_sem); 1575 } 1576 1577 pagevec_init(&pvec); 1578 while (index <= end) { 1579 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1580 if (nr_pages == 0) 1581 break; 1582 for (i = 0; i < nr_pages; i++) { 1583 struct page *page = pvec.pages[i]; 1584 1585 BUG_ON(!PageLocked(page)); 1586 BUG_ON(PageWriteback(page)); 1587 if (invalidate) { 1588 if (page_mapped(page)) 1589 clear_page_dirty_for_io(page); 1590 block_invalidatepage(page, 0, PAGE_SIZE); 1591 ClearPageUptodate(page); 1592 } 1593 unlock_page(page); 1594 } 1595 pagevec_release(&pvec); 1596 } 1597} 1598 1599static void ext4_print_free_blocks(struct inode *inode) 1600{ 1601 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1602 struct super_block *sb = inode->i_sb; 1603 struct ext4_inode_info *ei = EXT4_I(inode); 1604 1605 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 1606 EXT4_C2B(EXT4_SB(inode->i_sb), 1607 ext4_count_free_clusters(sb))); 1608 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 1609 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1610 (long long) EXT4_C2B(EXT4_SB(sb), 1611 percpu_counter_sum(&sbi->s_freeclusters_counter))); 1612 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1613 (long long) EXT4_C2B(EXT4_SB(sb), 1614 percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 1615 ext4_msg(sb, KERN_CRIT, "Block reservation details"); 1616 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1617 ei->i_reserved_data_blocks); 1618 return; 1619} 1620 1621static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 1622{ 1623 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 1624} 1625 1626/* 1627 * ext4_insert_delayed_block - adds a delayed block to the extents status 1628 * tree, incrementing the reserved cluster/block 1629 * count or making a pending reservation 1630 * where needed 1631 * 1632 * @inode - file containing the newly added block 1633 * @lblk - logical block to be added 1634 * 1635 * Returns 0 on success, negative error code on failure. 1636 */ 1637static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 1638{ 1639 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1640 int ret; 1641 bool allocated = false; 1642 bool reserved = false; 1643 1644 /* 1645 * If the cluster containing lblk is shared with a delayed, 1646 * written, or unwritten extent in a bigalloc file system, it's 1647 * already been accounted for and does not need to be reserved. 1648 * A pending reservation must be made for the cluster if it's 1649 * shared with a written or unwritten extent and doesn't already 1650 * have one. Written and unwritten extents can be purged from the 1651 * extents status tree if the system is under memory pressure, so 1652 * it's necessary to examine the extent tree if a search of the 1653 * extents status tree doesn't get a match. 1654 */ 1655 if (sbi->s_cluster_ratio == 1) { 1656 ret = ext4_da_reserve_space(inode); 1657 if (ret != 0) /* ENOSPC */ 1658 goto errout; 1659 reserved = true; 1660 } else { /* bigalloc */ 1661 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 1662 if (!ext4_es_scan_clu(inode, 1663 &ext4_es_is_mapped, lblk)) { 1664 ret = ext4_clu_mapped(inode, 1665 EXT4_B2C(sbi, lblk)); 1666 if (ret < 0) 1667 goto errout; 1668 if (ret == 0) { 1669 ret = ext4_da_reserve_space(inode); 1670 if (ret != 0) /* ENOSPC */ 1671 goto errout; 1672 reserved = true; 1673 } else { 1674 allocated = true; 1675 } 1676 } else { 1677 allocated = true; 1678 } 1679 } 1680 } 1681 1682 ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 1683 if (ret && reserved) 1684 ext4_da_release_space(inode, 1); 1685 1686errout: 1687 return ret; 1688} 1689 1690/* 1691 * This function is grabs code from the very beginning of 1692 * ext4_map_blocks, but assumes that the caller is from delayed write 1693 * time. This function looks up the requested blocks and sets the 1694 * buffer delay bit under the protection of i_data_sem. 1695 */ 1696static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 1697 struct ext4_map_blocks *map, 1698 struct buffer_head *bh) 1699{ 1700 struct extent_status es; 1701 int retval; 1702 sector_t invalid_block = ~((sector_t) 0xffff); 1703#ifdef ES_AGGRESSIVE_TEST 1704 struct ext4_map_blocks orig_map; 1705 1706 memcpy(&orig_map, map, sizeof(*map)); 1707#endif 1708 1709 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 1710 invalid_block = ~0; 1711 1712 map->m_flags = 0; 1713 ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 1714 (unsigned long) map->m_lblk); 1715 1716 /* Lookup extent status tree firstly */ 1717 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1718 if (ext4_es_is_hole(&es)) { 1719 retval = 0; 1720 down_read(&EXT4_I(inode)->i_data_sem); 1721 goto add_delayed; 1722 } 1723 1724 /* 1725 * Delayed extent could be allocated by fallocate. 1726 * So we need to check it. 1727 */ 1728 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1729 map_bh(bh, inode->i_sb, invalid_block); 1730 set_buffer_new(bh); 1731 set_buffer_delay(bh); 1732 return 0; 1733 } 1734 1735 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1736 retval = es.es_len - (iblock - es.es_lblk); 1737 if (retval > map->m_len) 1738 retval = map->m_len; 1739 map->m_len = retval; 1740 if (ext4_es_is_written(&es)) 1741 map->m_flags |= EXT4_MAP_MAPPED; 1742 else if (ext4_es_is_unwritten(&es)) 1743 map->m_flags |= EXT4_MAP_UNWRITTEN; 1744 else 1745 BUG(); 1746 1747#ifdef ES_AGGRESSIVE_TEST 1748 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1749#endif 1750 return retval; 1751 } 1752 1753 /* 1754 * Try to see if we can get the block without requesting a new 1755 * file system block. 1756 */ 1757 down_read(&EXT4_I(inode)->i_data_sem); 1758 if (ext4_has_inline_data(inode)) 1759 retval = 0; 1760 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1761 retval = ext4_ext_map_blocks(NULL, inode, map, 0); 1762 else 1763 retval = ext4_ind_map_blocks(NULL, inode, map, 0); 1764 1765add_delayed: 1766 if (retval == 0) { 1767 int ret; 1768 1769 /* 1770 * XXX: __block_prepare_write() unmaps passed block, 1771 * is it OK? 1772 */ 1773 1774 ret = ext4_insert_delayed_block(inode, map->m_lblk); 1775 if (ret != 0) { 1776 retval = ret; 1777 goto out_unlock; 1778 } 1779 1780 map_bh(bh, inode->i_sb, invalid_block); 1781 set_buffer_new(bh); 1782 set_buffer_delay(bh); 1783 } else if (retval > 0) { 1784 int ret; 1785 unsigned int status; 1786 1787 if (unlikely(retval != map->m_len)) { 1788 ext4_warning(inode->i_sb, 1789 "ES len assertion failed for inode " 1790 "%lu: retval %d != map->m_len %d", 1791 inode->i_ino, retval, map->m_len); 1792 WARN_ON(1); 1793 } 1794 1795 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1796 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1797 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1798 map->m_pblk, status); 1799 if (ret != 0) 1800 retval = ret; 1801 } 1802 1803out_unlock: 1804 up_read((&EXT4_I(inode)->i_data_sem)); 1805 1806 return retval; 1807} 1808 1809/* 1810 * This is a special get_block_t callback which is used by 1811 * ext4_da_write_begin(). It will either return mapped block or 1812 * reserve space for a single block. 1813 * 1814 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 1815 * We also have b_blocknr = -1 and b_bdev initialized properly 1816 * 1817 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 1818 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 1819 * initialized properly. 1820 */ 1821int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 1822 struct buffer_head *bh, int create) 1823{ 1824 struct ext4_map_blocks map; 1825 int ret = 0; 1826 1827 BUG_ON(create == 0); 1828 BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 1829 1830 map.m_lblk = iblock; 1831 map.m_len = 1; 1832 1833 /* 1834 * first, we need to know whether the block is allocated already 1835 * preallocated blocks are unmapped but should treated 1836 * the same as allocated blocks. 1837 */ 1838 ret = ext4_da_map_blocks(inode, iblock, &map, bh); 1839 if (ret <= 0) 1840 return ret; 1841 1842 map_bh(bh, inode->i_sb, map.m_pblk); 1843 ext4_update_bh_state(bh, map.m_flags); 1844 1845 if (buffer_unwritten(bh)) { 1846 /* A delayed write to unwritten bh should be marked 1847 * new and mapped. Mapped ensures that we don't do 1848 * get_block multiple times when we write to the same 1849 * offset and new ensures that we do proper zero out 1850 * for partial write. 1851 */ 1852 set_buffer_new(bh); 1853 set_buffer_mapped(bh); 1854 } 1855 return 0; 1856} 1857 1858static int bget_one(handle_t *handle, struct buffer_head *bh) 1859{ 1860 get_bh(bh); 1861 return 0; 1862} 1863 1864static int bput_one(handle_t *handle, struct buffer_head *bh) 1865{ 1866 put_bh(bh); 1867 return 0; 1868} 1869 1870static int __ext4_journalled_writepage(struct page *page, 1871 unsigned int len) 1872{ 1873 struct address_space *mapping = page->mapping; 1874 struct inode *inode = mapping->host; 1875 struct buffer_head *page_bufs = NULL; 1876 handle_t *handle = NULL; 1877 int ret = 0, err = 0; 1878 int inline_data = ext4_has_inline_data(inode); 1879 struct buffer_head *inode_bh = NULL; 1880 1881 ClearPageChecked(page); 1882 1883 if (inline_data) { 1884 BUG_ON(page->index != 0); 1885 BUG_ON(len > ext4_get_max_inline_size(inode)); 1886 inode_bh = ext4_journalled_write_inline_data(inode, len, page); 1887 if (inode_bh == NULL) 1888 goto out; 1889 } else { 1890 page_bufs = page_buffers(page); 1891 if (!page_bufs) { 1892 BUG(); 1893 goto out; 1894 } 1895 ext4_walk_page_buffers(handle, page_bufs, 0, len, 1896 NULL, bget_one); 1897 } 1898 /* 1899 * We need to release the page lock before we start the 1900 * journal, so grab a reference so the page won't disappear 1901 * out from under us. 1902 */ 1903 get_page(page); 1904 unlock_page(page); 1905 1906 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1907 ext4_writepage_trans_blocks(inode)); 1908 if (IS_ERR(handle)) { 1909 ret = PTR_ERR(handle); 1910 put_page(page); 1911 goto out_no_pagelock; 1912 } 1913 BUG_ON(!ext4_handle_valid(handle)); 1914 1915 lock_page(page); 1916 put_page(page); 1917 if (page->mapping != mapping) { 1918 /* The page got truncated from under us */ 1919 ext4_journal_stop(handle); 1920 ret = 0; 1921 goto out; 1922 } 1923 1924 if (inline_data) { 1925 ret = ext4_mark_inode_dirty(handle, inode); 1926 } else { 1927 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 1928 do_journal_get_write_access); 1929 1930 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 1931 write_end_fn); 1932 } 1933 if (ret == 0) 1934 ret = err; 1935 err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 1936 if (ret == 0) 1937 ret = err; 1938 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1939 err = ext4_journal_stop(handle); 1940 if (!ret) 1941 ret = err; 1942 1943 ext4_set_inode_state(inode, EXT4_STATE_JDATA); 1944out: 1945 unlock_page(page); 1946out_no_pagelock: 1947 if (!inline_data && page_bufs) 1948 ext4_walk_page_buffers(NULL, page_bufs, 0, len, 1949 NULL, bput_one); 1950 brelse(inode_bh); 1951 return ret; 1952} 1953 1954static void cancel_page_dirty_status(struct page *page) 1955{ 1956 struct address_space *mapping = page_mapping(page); 1957 unsigned long flags; 1958 1959 cancel_dirty_page(page); 1960 xa_lock_irqsave(&mapping->i_pages, flags); 1961 __xa_clear_mark(&mapping->i_pages, page_index(page), 1962 PAGECACHE_TAG_DIRTY); 1963 __xa_clear_mark(&mapping->i_pages, page_index(page), 1964 PAGECACHE_TAG_TOWRITE); 1965 xa_unlock_irqrestore(&mapping->i_pages, flags); 1966} 1967 1968/* 1969 * Note that we don't need to start a transaction unless we're journaling data 1970 * because we should have holes filled from ext4_page_mkwrite(). We even don't 1971 * need to file the inode to the transaction's list in ordered mode because if 1972 * we are writing back data added by write(), the inode is already there and if 1973 * we are writing back data modified via mmap(), no one guarantees in which 1974 * transaction the data will hit the disk. In case we are journaling data, we 1975 * cannot start transaction directly because transaction start ranks above page 1976 * lock so we have to do some magic. 1977 * 1978 * This function can get called via... 1979 * - ext4_writepages after taking page lock (have journal handle) 1980 * - journal_submit_inode_data_buffers (no journal handle) 1981 * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1982 * - grab_page_cache when doing write_begin (have journal handle) 1983 * 1984 * We don't do any block allocation in this function. If we have page with 1985 * multiple blocks we need to write those buffer_heads that are mapped. This 1986 * is important for mmaped based write. So if we do with blocksize 1K 1987 * truncate(f, 1024); 1988 * a = mmap(f, 0, 4096); 1989 * a[0] = 'a'; 1990 * truncate(f, 4096); 1991 * we have in the page first buffer_head mapped via page_mkwrite call back 1992 * but other buffer_heads would be unmapped but dirty (dirty done via the 1993 * do_wp_page). So writepage should write the first block. If we modify 1994 * the mmap area beyond 1024 we will again get a page_fault and the 1995 * page_mkwrite callback will do the block allocation and mark the 1996 * buffer_heads mapped. 1997 * 1998 * We redirty the page if we have any buffer_heads that is either delay or 1999 * unwritten in the page. 2000 * 2001 * We can get recursively called as show below. 2002 * 2003 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 2004 * ext4_writepage() 2005 * 2006 * But since we don't do any block allocation we should not deadlock. 2007 * Page also have the dirty flag cleared so we don't get recurive page_lock. 2008 */ 2009static int ext4_writepage(struct page *page, 2010 struct writeback_control *wbc) 2011{ 2012 int ret = 0; 2013 loff_t size; 2014 unsigned int len; 2015 struct buffer_head *page_bufs = NULL; 2016 struct inode *inode = page->mapping->host; 2017 struct ext4_io_submit io_submit; 2018 bool keep_towrite = false; 2019 2020 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 2021 inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 2022 unlock_page(page); 2023 return -EIO; 2024 } 2025 2026 if (WARN_ON(!page_has_buffers(page))) { 2027 cancel_page_dirty_status(page); 2028 unlock_page(page); 2029 return 0; 2030 } 2031 2032 trace_ext4_writepage(page); 2033 size = i_size_read(inode); 2034 if (page->index == size >> PAGE_SHIFT && 2035 !ext4_verity_in_progress(inode)) 2036 len = size & ~PAGE_MASK; 2037 else 2038 len = PAGE_SIZE; 2039 2040 /* Should never happen but for bugs in other kernel subsystems */ 2041 if (!page_has_buffers(page)) { 2042 ext4_warning_inode(inode, 2043 "page %lu does not have buffers attached", page->index); 2044 ClearPageDirty(page); 2045 unlock_page(page); 2046 return 0; 2047 } 2048 2049 page_bufs = page_buffers(page); 2050 /* 2051 * We cannot do block allocation or other extent handling in this 2052 * function. If there are buffers needing that, we have to redirty 2053 * the page. But we may reach here when we do a journal commit via 2054 * journal_submit_inode_data_buffers() and in that case we must write 2055 * allocated buffers to achieve data=ordered mode guarantees. 2056 * 2057 * Also, if there is only one buffer per page (the fs block 2058 * size == the page size), if one buffer needs block 2059 * allocation or needs to modify the extent tree to clear the 2060 * unwritten flag, we know that the page can't be written at 2061 * all, so we might as well refuse the write immediately. 2062 * Unfortunately if the block size != page size, we can't as 2063 * easily detect this case using ext4_walk_page_buffers(), but 2064 * for the extremely common case, this is an optimization that 2065 * skips a useless round trip through ext4_bio_write_page(). 2066 */ 2067 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2068 ext4_bh_delay_or_unwritten)) { 2069 redirty_page_for_writepage(wbc, page); 2070 if ((current->flags & PF_MEMALLOC) || 2071 (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2072 /* 2073 * For memory cleaning there's no point in writing only 2074 * some buffers. So just bail out. Warn if we came here 2075 * from direct reclaim. 2076 */ 2077 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2078 == PF_MEMALLOC); 2079 unlock_page(page); 2080 return 0; 2081 } 2082 keep_towrite = true; 2083 } 2084 2085 if (PageChecked(page) && ext4_should_journal_data(inode)) 2086 /* 2087 * It's mmapped pagecache. Add buffers and journal it. There 2088 * doesn't seem much point in redirtying the page here. 2089 */ 2090 return __ext4_journalled_writepage(page, len); 2091 2092 ext4_io_submit_init(&io_submit, wbc); 2093 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 2094 if (!io_submit.io_end) { 2095 redirty_page_for_writepage(wbc, page); 2096 unlock_page(page); 2097 return -ENOMEM; 2098 } 2099 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite); 2100 ext4_io_submit(&io_submit); 2101 /* Drop io_end reference we got from init */ 2102 ext4_put_io_end_defer(io_submit.io_end); 2103 return ret; 2104} 2105 2106static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 2107{ 2108 int len; 2109 loff_t size; 2110 int err; 2111 2112 BUG_ON(page->index != mpd->first_page); 2113 clear_page_dirty_for_io(page); 2114 /* 2115 * We have to be very careful here! Nothing protects writeback path 2116 * against i_size changes and the page can be writeably mapped into 2117 * page tables. So an application can be growing i_size and writing 2118 * data through mmap while writeback runs. clear_page_dirty_for_io() 2119 * write-protects our page in page tables and the page cannot get 2120 * written to again until we release page lock. So only after 2121 * clear_page_dirty_for_io() we are safe to sample i_size for 2122 * ext4_bio_write_page() to zero-out tail of the written page. We rely 2123 * on the barrier provided by TestClearPageDirty in 2124 * clear_page_dirty_for_io() to make sure i_size is really sampled only 2125 * after page tables are updated. 2126 */ 2127 size = i_size_read(mpd->inode); 2128 if (page->index == size >> PAGE_SHIFT && 2129 !ext4_verity_in_progress(mpd->inode)) 2130 len = size & ~PAGE_MASK; 2131 else 2132 len = PAGE_SIZE; 2133 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false); 2134 if (!err) 2135 mpd->wbc->nr_to_write--; 2136 mpd->first_page++; 2137 2138 return err; 2139} 2140 2141#define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 2142 2143/* 2144 * mballoc gives us at most this number of blocks... 2145 * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2146 * The rest of mballoc seems to handle chunks up to full group size. 2147 */ 2148#define MAX_WRITEPAGES_EXTENT_LEN 2048 2149 2150/* 2151 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 2152 * 2153 * @mpd - extent of blocks 2154 * @lblk - logical number of the block in the file 2155 * @bh - buffer head we want to add to the extent 2156 * 2157 * The function is used to collect contig. blocks in the same state. If the 2158 * buffer doesn't require mapping for writeback and we haven't started the 2159 * extent of buffers to map yet, the function returns 'true' immediately - the 2160 * caller can write the buffer right away. Otherwise the function returns true 2161 * if the block has been added to the extent, false if the block couldn't be 2162 * added. 2163 */ 2164static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 2165 struct buffer_head *bh) 2166{ 2167 struct ext4_map_blocks *map = &mpd->map; 2168 2169 /* Buffer that doesn't need mapping for writeback? */ 2170 if (!buffer_dirty(bh) || !buffer_mapped(bh) || 2171 (!buffer_delay(bh) && !buffer_unwritten(bh))) { 2172 /* So far no extent to map => we write the buffer right away */ 2173 if (map->m_len == 0) 2174 return true; 2175 return false; 2176 } 2177 2178 /* First block in the extent? */ 2179 if (map->m_len == 0) { 2180 /* We cannot map unless handle is started... */ 2181 if (!mpd->do_map) 2182 return false; 2183 map->m_lblk = lblk; 2184 map->m_len = 1; 2185 map->m_flags = bh->b_state & BH_FLAGS; 2186 return true; 2187 } 2188 2189 /* Don't go larger than mballoc is willing to allocate */ 2190 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 2191 return false; 2192 2193 /* Can we merge the block to our big extent? */ 2194 if (lblk == map->m_lblk + map->m_len && 2195 (bh->b_state & BH_FLAGS) == map->m_flags) { 2196 map->m_len++; 2197 return true; 2198 } 2199 return false; 2200} 2201 2202/* 2203 * mpage_process_page_bufs - submit page buffers for IO or add them to extent 2204 * 2205 * @mpd - extent of blocks for mapping 2206 * @head - the first buffer in the page 2207 * @bh - buffer we should start processing from 2208 * @lblk - logical number of the block in the file corresponding to @bh 2209 * 2210 * Walk through page buffers from @bh upto @head (exclusive) and either submit 2211 * the page for IO if all buffers in this page were mapped and there's no 2212 * accumulated extent of buffers to map or add buffers in the page to the 2213 * extent of buffers to map. The function returns 1 if the caller can continue 2214 * by processing the next page, 0 if it should stop adding buffers to the 2215 * extent to map because we cannot extend it anymore. It can also return value 2216 * < 0 in case of error during IO submission. 2217 */ 2218static int mpage_process_page_bufs(struct mpage_da_data *mpd, 2219 struct buffer_head *head, 2220 struct buffer_head *bh, 2221 ext4_lblk_t lblk) 2222{ 2223 struct inode *inode = mpd->inode; 2224 int err; 2225 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 2226 >> inode->i_blkbits; 2227 2228 if (ext4_verity_in_progress(inode)) 2229 blocks = EXT_MAX_BLOCKS; 2230 2231 do { 2232 BUG_ON(buffer_locked(bh)); 2233 2234 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 2235 /* Found extent to map? */ 2236 if (mpd->map.m_len) 2237 return 0; 2238 /* Buffer needs mapping and handle is not started? */ 2239 if (!mpd->do_map) 2240 return 0; 2241 /* Everything mapped so far and we hit EOF */ 2242 break; 2243 } 2244 } while (lblk++, (bh = bh->b_this_page) != head); 2245 /* So far everything mapped? Submit the page for IO. */ 2246 if (mpd->map.m_len == 0) { 2247 err = mpage_submit_page(mpd, head->b_page); 2248 if (err < 0) 2249 return err; 2250 } 2251 if (lblk >= blocks) { 2252 mpd->scanned_until_end = 1; 2253 return 0; 2254 } 2255 return 1; 2256} 2257 2258/* 2259 * mpage_process_page - update page buffers corresponding to changed extent and 2260 * may submit fully mapped page for IO 2261 * 2262 * @mpd - description of extent to map, on return next extent to map 2263 * @m_lblk - logical block mapping. 2264 * @m_pblk - corresponding physical mapping. 2265 * @map_bh - determines on return whether this page requires any further 2266 * mapping or not. 2267 * Scan given page buffers corresponding to changed extent and update buffer 2268 * state according to new extent state. 2269 * We map delalloc buffers to their physical location, clear unwritten bits. 2270 * If the given page is not fully mapped, we update @map to the next extent in 2271 * the given page that needs mapping & return @map_bh as true. 2272 */ 2273static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 2274 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 2275 bool *map_bh) 2276{ 2277 struct buffer_head *head, *bh; 2278 ext4_io_end_t *io_end = mpd->io_submit.io_end; 2279 ext4_lblk_t lblk = *m_lblk; 2280 ext4_fsblk_t pblock = *m_pblk; 2281 int err = 0; 2282 int blkbits = mpd->inode->i_blkbits; 2283 ssize_t io_end_size = 0; 2284 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 2285 2286 bh = head = page_buffers(page); 2287 do { 2288 if (lblk < mpd->map.m_lblk) 2289 continue; 2290 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 2291 /* 2292 * Buffer after end of mapped extent. 2293 * Find next buffer in the page to map. 2294 */ 2295 mpd->map.m_len = 0; 2296 mpd->map.m_flags = 0; 2297 io_end_vec->size += io_end_size; 2298 io_end_size = 0; 2299 2300 err = mpage_process_page_bufs(mpd, head, bh, lblk); 2301 if (err > 0) 2302 err = 0; 2303 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2304 io_end_vec = ext4_alloc_io_end_vec(io_end); 2305 if (IS_ERR(io_end_vec)) { 2306 err = PTR_ERR(io_end_vec); 2307 goto out; 2308 } 2309 io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2310 } 2311 *map_bh = true; 2312 goto out; 2313 } 2314 if (buffer_delay(bh)) { 2315 clear_buffer_delay(bh); 2316 bh->b_blocknr = pblock++; 2317 } 2318 clear_buffer_unwritten(bh); 2319 io_end_size += (1 << blkbits); 2320 } while (lblk++, (bh = bh->b_this_page) != head); 2321 2322 io_end_vec->size += io_end_size; 2323 io_end_size = 0; 2324 *map_bh = false; 2325out: 2326 *m_lblk = lblk; 2327 *m_pblk = pblock; 2328 return err; 2329} 2330 2331/* 2332 * mpage_map_buffers - update buffers corresponding to changed extent and 2333 * submit fully mapped pages for IO 2334 * 2335 * @mpd - description of extent to map, on return next extent to map 2336 * 2337 * Scan buffers corresponding to changed extent (we expect corresponding pages 2338 * to be already locked) and update buffer state according to new extent state. 2339 * We map delalloc buffers to their physical location, clear unwritten bits, 2340 * and mark buffers as uninit when we perform writes to unwritten extents 2341 * and do extent conversion after IO is finished. If the last page is not fully 2342 * mapped, we update @map to the next extent in the last page that needs 2343 * mapping. Otherwise we submit the page for IO. 2344 */ 2345static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 2346{ 2347 struct pagevec pvec; 2348 int nr_pages, i; 2349 struct inode *inode = mpd->inode; 2350 int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 2351 pgoff_t start, end; 2352 ext4_lblk_t lblk; 2353 ext4_fsblk_t pblock; 2354 int err; 2355 bool map_bh = false; 2356 2357 start = mpd->map.m_lblk >> bpp_bits; 2358 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 2359 lblk = start << bpp_bits; 2360 pblock = mpd->map.m_pblk; 2361 2362 pagevec_init(&pvec); 2363 while (start <= end) { 2364 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2365 &start, end); 2366 if (nr_pages == 0) 2367 break; 2368 for (i = 0; i < nr_pages; i++) { 2369 struct page *page = pvec.pages[i]; 2370 2371 err = mpage_process_page(mpd, page, &lblk, &pblock, 2372 &map_bh); 2373 /* 2374 * If map_bh is true, means page may require further bh 2375 * mapping, or maybe the page was submitted for IO. 2376 * So we return to call further extent mapping. 2377 */ 2378 if (err < 0 || map_bh) 2379 goto out; 2380 /* Page fully mapped - let IO run! */ 2381 err = mpage_submit_page(mpd, page); 2382 if (err < 0) 2383 goto out; 2384 } 2385 pagevec_release(&pvec); 2386 } 2387 /* Extent fully mapped and matches with page boundary. We are done. */ 2388 mpd->map.m_len = 0; 2389 mpd->map.m_flags = 0; 2390 return 0; 2391out: 2392 pagevec_release(&pvec); 2393 return err; 2394} 2395 2396static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 2397{ 2398 struct inode *inode = mpd->inode; 2399 struct ext4_map_blocks *map = &mpd->map; 2400 int get_blocks_flags; 2401 int err, dioread_nolock; 2402 2403 trace_ext4_da_write_pages_extent(inode, map); 2404 /* 2405 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2406 * to convert an unwritten extent to be initialized (in the case 2407 * where we have written into one or more preallocated blocks). It is 2408 * possible that we're going to need more metadata blocks than 2409 * previously reserved. However we must not fail because we're in 2410 * writeback and there is nothing we can do about it so it might result 2411 * in data loss. So use reserved blocks to allocate metadata if 2412 * possible. 2413 * 2414 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2415 * the blocks in question are delalloc blocks. This indicates 2416 * that the blocks and quotas has already been checked when 2417 * the data was copied into the page cache. 2418 */ 2419 get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2420 EXT4_GET_BLOCKS_METADATA_NOFAIL | 2421 EXT4_GET_BLOCKS_IO_SUBMIT; 2422 dioread_nolock = ext4_should_dioread_nolock(inode); 2423 if (dioread_nolock) 2424 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 2425 if (map->m_flags & BIT(BH_Delay)) 2426 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 2427 2428 err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 2429 if (err < 0) 2430 return err; 2431 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 2432 if (!mpd->io_submit.io_end->handle && 2433 ext4_handle_valid(handle)) { 2434 mpd->io_submit.io_end->handle = handle->h_rsv_handle; 2435 handle->h_rsv_handle = NULL; 2436 } 2437 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 2438 } 2439 2440 BUG_ON(map->m_len == 0); 2441 return 0; 2442} 2443 2444/* 2445 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 2446 * mpd->len and submit pages underlying it for IO 2447 * 2448 * @handle - handle for journal operations 2449 * @mpd - extent to map 2450 * @give_up_on_write - we set this to true iff there is a fatal error and there 2451 * is no hope of writing the data. The caller should discard 2452 * dirty pages to avoid infinite loops. 2453 * 2454 * The function maps extent starting at mpd->lblk of length mpd->len. If it is 2455 * delayed, blocks are allocated, if it is unwritten, we may need to convert 2456 * them to initialized or split the described range from larger unwritten 2457 * extent. Note that we need not map all the described range since allocation 2458 * can return less blocks or the range is covered by more unwritten extents. We 2459 * cannot map more because we are limited by reserved transaction credits. On 2460 * the other hand we always make sure that the last touched page is fully 2461 * mapped so that it can be written out (and thus forward progress is 2462 * guaranteed). After mapping we submit all mapped pages for IO. 2463 */ 2464static int mpage_map_and_submit_extent(handle_t *handle, 2465 struct mpage_da_data *mpd, 2466 bool *give_up_on_write) 2467{ 2468 struct inode *inode = mpd->inode; 2469 struct ext4_map_blocks *map = &mpd->map; 2470 int err; 2471 loff_t disksize; 2472 int progress = 0; 2473 ext4_io_end_t *io_end = mpd->io_submit.io_end; 2474 struct ext4_io_end_vec *io_end_vec; 2475 2476 io_end_vec = ext4_alloc_io_end_vec(io_end); 2477 if (IS_ERR(io_end_vec)) 2478 return PTR_ERR(io_end_vec); 2479 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 2480 do { 2481 err = mpage_map_one_extent(handle, mpd); 2482 if (err < 0) { 2483 struct super_block *sb = inode->i_sb; 2484 2485 if (ext4_forced_shutdown(EXT4_SB(sb)) || 2486 ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2487 goto invalidate_dirty_pages; 2488 /* 2489 * Let the uper layers retry transient errors. 2490 * In the case of ENOSPC, if ext4_count_free_blocks() 2491 * is non-zero, a commit should free up blocks. 2492 */ 2493 if ((err == -ENOMEM) || 2494 (err == -ENOSPC && ext4_count_free_clusters(sb))) { 2495 if (progress) 2496 goto update_disksize; 2497 return err; 2498 } 2499 ext4_msg(sb, KERN_CRIT, 2500 "Delayed block allocation failed for " 2501 "inode %lu at logical offset %llu with" 2502 " max blocks %u with error %d", 2503 inode->i_ino, 2504 (unsigned long long)map->m_lblk, 2505 (unsigned)map->m_len, -err); 2506 ext4_msg(sb, KERN_CRIT, 2507 "This should not happen!! Data will " 2508 "be lost\n"); 2509 if (err == -ENOSPC) 2510 ext4_print_free_blocks(inode); 2511 invalidate_dirty_pages: 2512 *give_up_on_write = true; 2513 return err; 2514 } 2515 progress = 1; 2516 /* 2517 * Update buffer state, submit mapped pages, and get us new 2518 * extent to map 2519 */ 2520 err = mpage_map_and_submit_buffers(mpd); 2521 if (err < 0) 2522 goto update_disksize; 2523 } while (map->m_len); 2524 2525update_disksize: 2526 /* 2527 * Update on-disk size after IO is submitted. Races with 2528 * truncate are avoided by checking i_size under i_data_sem. 2529 */ 2530 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 2531 if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 2532 int err2; 2533 loff_t i_size; 2534 2535 down_write(&EXT4_I(inode)->i_data_sem); 2536 i_size = i_size_read(inode); 2537 if (disksize > i_size) 2538 disksize = i_size; 2539 if (disksize > EXT4_I(inode)->i_disksize) 2540 EXT4_I(inode)->i_disksize = disksize; 2541 up_write(&EXT4_I(inode)->i_data_sem); 2542 err2 = ext4_mark_inode_dirty(handle, inode); 2543 if (err2) { 2544 ext4_error_err(inode->i_sb, -err2, 2545 "Failed to mark inode %lu dirty", 2546 inode->i_ino); 2547 } 2548 if (!err) 2549 err = err2; 2550 } 2551 return err; 2552} 2553 2554/* 2555 * Calculate the total number of credits to reserve for one writepages 2556 * iteration. This is called from ext4_writepages(). We map an extent of 2557 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2558 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2559 * bpp - 1 blocks in bpp different extents. 2560 */ 2561static int ext4_da_writepages_trans_blocks(struct inode *inode) 2562{ 2563 int bpp = ext4_journal_blocks_per_page(inode); 2564 2565 return ext4_meta_trans_blocks(inode, 2566 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2567} 2568 2569/* 2570 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 2571 * and underlying extent to map 2572 * 2573 * @mpd - where to look for pages 2574 * 2575 * Walk dirty pages in the mapping. If they are fully mapped, submit them for 2576 * IO immediately. When we find a page which isn't mapped we start accumulating 2577 * extent of buffers underlying these pages that needs mapping (formed by 2578 * either delayed or unwritten buffers). We also lock the pages containing 2579 * these buffers. The extent found is returned in @mpd structure (starting at 2580 * mpd->lblk with length mpd->len blocks). 2581 * 2582 * Note that this function can attach bios to one io_end structure which are 2583 * neither logically nor physically contiguous. Although it may seem as an 2584 * unnecessary complication, it is actually inevitable in blocksize < pagesize 2585 * case as we need to track IO to all buffers underlying a page in one io_end. 2586 */ 2587static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 2588{ 2589 struct address_space *mapping = mpd->inode->i_mapping; 2590 struct pagevec pvec; 2591 unsigned int nr_pages; 2592 long left = mpd->wbc->nr_to_write; 2593 pgoff_t index = mpd->first_page; 2594 pgoff_t end = mpd->last_page; 2595 xa_mark_t tag; 2596 int i, err = 0; 2597 int blkbits = mpd->inode->i_blkbits; 2598 ext4_lblk_t lblk; 2599 struct buffer_head *head; 2600 2601 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 2602 tag = PAGECACHE_TAG_TOWRITE; 2603 else 2604 tag = PAGECACHE_TAG_DIRTY; 2605 2606 pagevec_init(&pvec); 2607 mpd->map.m_len = 0; 2608 mpd->next_page = index; 2609 while (index <= end) { 2610 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 2611 tag); 2612 if (nr_pages == 0) 2613 break; 2614 2615 for (i = 0; i < nr_pages; i++) { 2616 struct page *page = pvec.pages[i]; 2617 2618 /* 2619 * Accumulated enough dirty pages? This doesn't apply 2620 * to WB_SYNC_ALL mode. For integrity sync we have to 2621 * keep going because someone may be concurrently 2622 * dirtying pages, and we might have synced a lot of 2623 * newly appeared dirty pages, but have not synced all 2624 * of the old dirty pages. 2625 */ 2626 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2627 goto out; 2628 2629 /* If we can't merge this page, we are done. */ 2630 if (mpd->map.m_len > 0 && mpd->next_page != page->index) 2631 goto out; 2632 2633 lock_page(page); 2634 /* 2635 * If the page is no longer dirty, or its mapping no 2636 * longer corresponds to inode we are writing (which 2637 * means it has been truncated or invalidated), or the 2638 * page is already under writeback and we are not doing 2639 * a data integrity writeback, skip the page 2640 */ 2641 if (!PageDirty(page) || 2642 (PageWriteback(page) && 2643 (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 2644 unlikely(page->mapping != mapping)) { 2645 unlock_page(page); 2646 continue; 2647 } 2648 2649 if (WARN_ON(!page_has_buffers(page))) { 2650 cancel_page_dirty_status(page); 2651 unlock_page(page); 2652 continue; 2653 } 2654 2655 wait_on_page_writeback(page); 2656 BUG_ON(PageWriteback(page)); 2657 2658 /* 2659 * Should never happen but for buggy code in 2660 * other subsystems that call 2661 * set_page_dirty() without properly warning 2662 * the file system first. See [1] for more 2663 * information. 2664 * 2665 * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz 2666 */ 2667 if (!page_has_buffers(page)) { 2668 ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", page->index); 2669 ClearPageDirty(page); 2670 unlock_page(page); 2671 continue; 2672 } 2673 2674 if (mpd->map.m_len == 0) 2675 mpd->first_page = page->index; 2676 mpd->next_page = page->index + 1; 2677 /* Add all dirty buffers to mpd */ 2678 lblk = ((ext4_lblk_t)page->index) << 2679 (PAGE_SHIFT - blkbits); 2680 head = page_buffers(page); 2681 err = mpage_process_page_bufs(mpd, head, head, lblk); 2682 if (err <= 0) 2683 goto out; 2684 err = 0; 2685 left--; 2686 } 2687 pagevec_release(&pvec); 2688 cond_resched(); 2689 } 2690 mpd->scanned_until_end = 1; 2691 return 0; 2692out: 2693 pagevec_release(&pvec); 2694 return err; 2695} 2696 2697static int ext4_writepages(struct address_space *mapping, 2698 struct writeback_control *wbc) 2699{ 2700 pgoff_t writeback_index = 0; 2701 long nr_to_write = wbc->nr_to_write; 2702 int range_whole = 0; 2703 int cycled = 1; 2704 handle_t *handle = NULL; 2705 struct mpage_da_data mpd; 2706 struct inode *inode = mapping->host; 2707 int needed_blocks, rsv_blocks = 0, ret = 0; 2708 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2709 struct blk_plug plug; 2710 bool give_up_on_write = false; 2711 2712 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 2713 return -EIO; 2714 2715 percpu_down_read(&sbi->s_writepages_rwsem); 2716 trace_ext4_writepages(inode, wbc); 2717 2718 /* 2719 * No pages to write? This is mainly a kludge to avoid starting 2720 * a transaction for special inodes like journal inode on last iput() 2721 * because that could violate lock ordering on umount 2722 */ 2723 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2724 goto out_writepages; 2725 2726 if (ext4_should_journal_data(inode)) { 2727 ret = generic_writepages(mapping, wbc); 2728 goto out_writepages; 2729 } 2730 2731 /* 2732 * If the filesystem has aborted, it is read-only, so return 2733 * right away instead of dumping stack traces later on that 2734 * will obscure the real source of the problem. We test 2735 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 2736 * the latter could be true if the filesystem is mounted 2737 * read-only, and in that case, ext4_writepages should 2738 * *never* be called, so if that ever happens, we would want 2739 * the stack trace. 2740 */ 2741 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 2742 ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2743 ret = -EROFS; 2744 goto out_writepages; 2745 } 2746 2747 /* 2748 * If we have inline data and arrive here, it means that 2749 * we will soon create the block for the 1st page, so 2750 * we'd better clear the inline data here. 2751 */ 2752 if (ext4_has_inline_data(inode)) { 2753 /* Just inode will be modified... */ 2754 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 2755 if (IS_ERR(handle)) { 2756 ret = PTR_ERR(handle); 2757 goto out_writepages; 2758 } 2759 BUG_ON(ext4_test_inode_state(inode, 2760 EXT4_STATE_MAY_INLINE_DATA)); 2761 ext4_destroy_inline_data(handle, inode); 2762 ext4_journal_stop(handle); 2763 } 2764 2765 if (ext4_should_dioread_nolock(inode)) { 2766 /* 2767 * We may need to convert up to one extent per block in 2768 * the page and we may dirty the inode. 2769 */ 2770 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 2771 PAGE_SIZE >> inode->i_blkbits); 2772 } 2773 2774 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 2775 range_whole = 1; 2776 2777 if (wbc->range_cyclic) { 2778 writeback_index = mapping->writeback_index; 2779 if (writeback_index) 2780 cycled = 0; 2781 mpd.first_page = writeback_index; 2782 mpd.last_page = -1; 2783 } else { 2784 mpd.first_page = wbc->range_start >> PAGE_SHIFT; 2785 mpd.last_page = wbc->range_end >> PAGE_SHIFT; 2786 } 2787 2788 mpd.inode = inode; 2789 mpd.wbc = wbc; 2790 ext4_io_submit_init(&mpd.io_submit, wbc); 2791retry: 2792 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2793 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 2794 blk_start_plug(&plug); 2795 2796 /* 2797 * First writeback pages that don't need mapping - we can avoid 2798 * starting a transaction unnecessarily and also avoid being blocked 2799 * in the block layer on device congestion while having transaction 2800 * started. 2801 */ 2802 mpd.do_map = 0; 2803 mpd.scanned_until_end = 0; 2804 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2805 if (!mpd.io_submit.io_end) { 2806 ret = -ENOMEM; 2807 goto unplug; 2808 } 2809 ret = mpage_prepare_extent_to_map(&mpd); 2810 /* Unlock pages we didn't use */ 2811 mpage_release_unused_pages(&mpd, false); 2812 /* Submit prepared bio */ 2813 ext4_io_submit(&mpd.io_submit); 2814 ext4_put_io_end_defer(mpd.io_submit.io_end); 2815 mpd.io_submit.io_end = NULL; 2816 if (ret < 0) 2817 goto unplug; 2818 2819 while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { 2820 /* For each extent of pages we use new io_end */ 2821 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2822 if (!mpd.io_submit.io_end) { 2823 ret = -ENOMEM; 2824 break; 2825 } 2826 2827 /* 2828 * We have two constraints: We find one extent to map and we 2829 * must always write out whole page (makes a difference when 2830 * blocksize < pagesize) so that we don't block on IO when we 2831 * try to write out the rest of the page. Journalled mode is 2832 * not supported by delalloc. 2833 */ 2834 BUG_ON(ext4_should_journal_data(inode)); 2835 needed_blocks = ext4_da_writepages_trans_blocks(inode); 2836 2837 /* start a new transaction */ 2838 handle = ext4_journal_start_with_reserve(inode, 2839 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 2840 if (IS_ERR(handle)) { 2841 ret = PTR_ERR(handle); 2842 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2843 "%ld pages, ino %lu; err %d", __func__, 2844 wbc->nr_to_write, inode->i_ino, ret); 2845 /* Release allocated io_end */ 2846 ext4_put_io_end(mpd.io_submit.io_end); 2847 mpd.io_submit.io_end = NULL; 2848 break; 2849 } 2850 mpd.do_map = 1; 2851 2852 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 2853 ret = mpage_prepare_extent_to_map(&mpd); 2854 if (!ret && mpd.map.m_len) 2855 ret = mpage_map_and_submit_extent(handle, &mpd, 2856 &give_up_on_write); 2857 /* 2858 * Caution: If the handle is synchronous, 2859 * ext4_journal_stop() can wait for transaction commit 2860 * to finish which may depend on writeback of pages to 2861 * complete or on page lock to be released. In that 2862 * case, we have to wait until after we have 2863 * submitted all the IO, released page locks we hold, 2864 * and dropped io_end reference (for extent conversion 2865 * to be able to complete) before stopping the handle. 2866 */ 2867 if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 2868 ext4_journal_stop(handle); 2869 handle = NULL; 2870 mpd.do_map = 0; 2871 } 2872 /* Unlock pages we didn't use */ 2873 mpage_release_unused_pages(&mpd, give_up_on_write); 2874 /* Submit prepared bio */ 2875 ext4_io_submit(&mpd.io_submit); 2876 2877 /* 2878 * Drop our io_end reference we got from init. We have 2879 * to be careful and use deferred io_end finishing if 2880 * we are still holding the transaction as we can 2881 * release the last reference to io_end which may end 2882 * up doing unwritten extent conversion. 2883 */ 2884 if (handle) { 2885 ext4_put_io_end_defer(mpd.io_submit.io_end); 2886 ext4_journal_stop(handle); 2887 } else 2888 ext4_put_io_end(mpd.io_submit.io_end); 2889 mpd.io_submit.io_end = NULL; 2890 2891 if (ret == -ENOSPC && sbi->s_journal) { 2892 /* 2893 * Commit the transaction which would 2894 * free blocks released in the transaction 2895 * and try again 2896 */ 2897 jbd2_journal_force_commit_nested(sbi->s_journal); 2898 ret = 0; 2899 continue; 2900 } 2901 /* Fatal error - ENOMEM, EIO... */ 2902 if (ret) 2903 break; 2904 } 2905unplug: 2906 blk_finish_plug(&plug); 2907 if (!ret && !cycled && wbc->nr_to_write > 0) { 2908 cycled = 1; 2909 mpd.last_page = writeback_index - 1; 2910 mpd.first_page = 0; 2911 goto retry; 2912 } 2913 2914 /* Update index */ 2915 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 2916 /* 2917 * Set the writeback_index so that range_cyclic 2918 * mode will write it back later 2919 */ 2920 mapping->writeback_index = mpd.first_page; 2921 2922out_writepages: 2923 trace_ext4_writepages_result(inode, wbc, ret, 2924 nr_to_write - wbc->nr_to_write); 2925 percpu_up_read(&sbi->s_writepages_rwsem); 2926 return ret; 2927} 2928 2929static int ext4_dax_writepages(struct address_space *mapping, 2930 struct writeback_control *wbc) 2931{ 2932 int ret; 2933 long nr_to_write = wbc->nr_to_write; 2934 struct inode *inode = mapping->host; 2935 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2936 2937 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 2938 return -EIO; 2939 2940 percpu_down_read(&sbi->s_writepages_rwsem); 2941 trace_ext4_writepages(inode, wbc); 2942 2943 ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 2944 trace_ext4_writepages_result(inode, wbc, ret, 2945 nr_to_write - wbc->nr_to_write); 2946 percpu_up_read(&sbi->s_writepages_rwsem); 2947 return ret; 2948} 2949 2950static int ext4_nonda_switch(struct super_block *sb) 2951{ 2952 s64 free_clusters, dirty_clusters; 2953 struct ext4_sb_info *sbi = EXT4_SB(sb); 2954 2955 /* 2956 * switch to non delalloc mode if we are running low 2957 * on free block. The free block accounting via percpu 2958 * counters can get slightly wrong with percpu_counter_batch getting 2959 * accumulated on each CPU without updating global counters 2960 * Delalloc need an accurate free block accounting. So switch 2961 * to non delalloc when we are near to error range. 2962 */ 2963 free_clusters = 2964 percpu_counter_read_positive(&sbi->s_freeclusters_counter); 2965 dirty_clusters = 2966 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 2967 /* 2968 * Start pushing delalloc when 1/2 of free blocks are dirty. 2969 */ 2970 if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 2971 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 2972 2973 if (2 * free_clusters < 3 * dirty_clusters || 2974 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 2975 /* 2976 * free block count is less than 150% of dirty blocks 2977 * or free blocks is less than watermark 2978 */ 2979 return 1; 2980 } 2981 return 0; 2982} 2983 2984static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 2985 loff_t pos, unsigned len, unsigned flags, 2986 struct page **pagep, void **fsdata) 2987{ 2988 int ret, retries = 0; 2989 struct page *page; 2990 pgoff_t index; 2991 struct inode *inode = mapping->host; 2992 2993 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 2994 return -EIO; 2995 2996 index = pos >> PAGE_SHIFT; 2997 2998 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2999 ext4_verity_in_progress(inode)) { 3000 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 3001 return ext4_write_begin(file, mapping, pos, 3002 len, flags, pagep, fsdata); 3003 } 3004 *fsdata = (void *)0; 3005 trace_ext4_da_write_begin(inode, pos, len, flags); 3006 3007 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 3008 ret = ext4_da_write_inline_data_begin(mapping, inode, 3009 pos, len, flags, 3010 pagep, fsdata); 3011 if (ret < 0) 3012 return ret; 3013 if (ret == 1) 3014 return 0; 3015 } 3016 3017retry: 3018 page = grab_cache_page_write_begin(mapping, index, flags); 3019 if (!page) 3020 return -ENOMEM; 3021 3022 /* In case writeback began while the page was unlocked */ 3023 wait_for_stable_page(page); 3024 3025#ifdef CONFIG_FS_ENCRYPTION 3026 ret = ext4_block_write_begin(page, pos, len, 3027 ext4_da_get_block_prep); 3028#else 3029 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 3030#endif 3031 if (ret < 0) { 3032 unlock_page(page); 3033 put_page(page); 3034 /* 3035 * block_write_begin may have instantiated a few blocks 3036 * outside i_size. Trim these off again. Don't need 3037 * i_size_read because we hold inode lock. 3038 */ 3039 if (pos + len > inode->i_size) 3040 ext4_truncate_failed_write(inode); 3041 3042 if (ret == -ENOSPC && 3043 ext4_should_retry_alloc(inode->i_sb, &retries)) 3044 goto retry; 3045 return ret; 3046 } 3047 3048 *pagep = page; 3049 return ret; 3050} 3051 3052/* 3053 * Check if we should update i_disksize 3054 * when write to the end of file but not require block allocation 3055 */ 3056static int ext4_da_should_update_i_disksize(struct page *page, 3057 unsigned long offset) 3058{ 3059 struct buffer_head *bh; 3060 struct inode *inode = page->mapping->host; 3061 unsigned int idx; 3062 int i; 3063 3064 bh = page_buffers(page); 3065 idx = offset >> inode->i_blkbits; 3066 3067 for (i = 0; i < idx; i++) 3068 bh = bh->b_this_page; 3069 3070 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3071 return 0; 3072 return 1; 3073} 3074 3075static int ext4_da_write_end(struct file *file, 3076 struct address_space *mapping, 3077 loff_t pos, unsigned len, unsigned copied, 3078 struct page *page, void *fsdata) 3079{ 3080 struct inode *inode = mapping->host; 3081 loff_t new_i_size; 3082 unsigned long start, end; 3083 int write_mode = (int)(unsigned long)fsdata; 3084 3085 if (write_mode == FALL_BACK_TO_NONDELALLOC) 3086 return ext4_write_end(file, mapping, pos, 3087 len, copied, page, fsdata); 3088 3089 trace_ext4_da_write_end(inode, pos, len, copied); 3090 3091 if (write_mode != CONVERT_INLINE_DATA && 3092 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 3093 ext4_has_inline_data(inode)) 3094 return ext4_write_inline_data_end(inode, pos, len, copied, page); 3095 3096 start = pos & (PAGE_SIZE - 1); 3097 end = start + copied - 1; 3098 3099 /* 3100 * Since we are holding inode lock, we are sure i_disksize <= 3101 * i_size. We also know that if i_disksize < i_size, there are 3102 * delalloc writes pending in the range upto i_size. If the end of 3103 * the current write is <= i_size, there's no need to touch 3104 * i_disksize since writeback will push i_disksize upto i_size 3105 * eventually. If the end of the current write is > i_size and 3106 * inside an allocated block (ext4_da_should_update_i_disksize() 3107 * check), we need to update i_disksize here as neither 3108 * ext4_writepage() nor certain ext4_writepages() paths not 3109 * allocating blocks update i_disksize. 3110 * 3111 * Note that we defer inode dirtying to generic_write_end() / 3112 * ext4_da_write_inline_data_end(). 3113 */ 3114 new_i_size = pos + copied; 3115 if (copied && new_i_size > inode->i_size && 3116 ext4_da_should_update_i_disksize(page, end)) 3117 ext4_update_i_disksize(inode, new_i_size); 3118 3119 return generic_write_end(file, mapping, pos, len, copied, page, fsdata); 3120} 3121 3122/* 3123 * Force all delayed allocation blocks to be allocated for a given inode. 3124 */ 3125int ext4_alloc_da_blocks(struct inode *inode) 3126{ 3127 trace_ext4_alloc_da_blocks(inode); 3128 3129 if (!EXT4_I(inode)->i_reserved_data_blocks) 3130 return 0; 3131 3132 /* 3133 * We do something simple for now. The filemap_flush() will 3134 * also start triggering a write of the data blocks, which is 3135 * not strictly speaking necessary (and for users of 3136 * laptop_mode, not even desirable). However, to do otherwise 3137 * would require replicating code paths in: 3138 * 3139 * ext4_writepages() -> 3140 * write_cache_pages() ---> (via passed in callback function) 3141 * __mpage_da_writepage() --> 3142 * mpage_add_bh_to_extent() 3143 * mpage_da_map_blocks() 3144 * 3145 * The problem is that write_cache_pages(), located in 3146 * mm/page-writeback.c, marks pages clean in preparation for 3147 * doing I/O, which is not desirable if we're not planning on 3148 * doing I/O at all. 3149 * 3150 * We could call write_cache_pages(), and then redirty all of 3151 * the pages by calling redirty_page_for_writepage() but that 3152 * would be ugly in the extreme. So instead we would need to 3153 * replicate parts of the code in the above functions, 3154 * simplifying them because we wouldn't actually intend to 3155 * write out the pages, but rather only collect contiguous 3156 * logical block extents, call the multi-block allocator, and 3157 * then update the buffer heads with the block allocations. 3158 * 3159 * For now, though, we'll cheat by calling filemap_flush(), 3160 * which will map the blocks, and start the I/O, but not 3161 * actually wait for the I/O to complete. 3162 */ 3163 return filemap_flush(inode->i_mapping); 3164} 3165 3166/* 3167 * bmap() is special. It gets used by applications such as lilo and by 3168 * the swapper to find the on-disk block of a specific piece of data. 3169 * 3170 * Naturally, this is dangerous if the block concerned is still in the 3171 * journal. If somebody makes a swapfile on an ext4 data-journaling 3172 * filesystem and enables swap, then they may get a nasty shock when the 3173 * data getting swapped to that swapfile suddenly gets overwritten by 3174 * the original zero's written out previously to the journal and 3175 * awaiting writeback in the kernel's buffer cache. 3176 * 3177 * So, if we see any bmap calls here on a modified, data-journaled file, 3178 * take extra steps to flush any blocks which might be in the cache. 3179 */ 3180static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3181{ 3182 struct inode *inode = mapping->host; 3183 journal_t *journal; 3184 sector_t ret = 0; 3185 int err; 3186 3187 inode_lock_shared(inode); 3188 /* 3189 * We can get here for an inline file via the FIBMAP ioctl 3190 */ 3191 if (ext4_has_inline_data(inode)) 3192 goto out; 3193 3194 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 3195 test_opt(inode->i_sb, DELALLOC)) { 3196 /* 3197 * With delalloc we want to sync the file 3198 * so that we can make sure we allocate 3199 * blocks for file 3200 */ 3201 filemap_write_and_wait(mapping); 3202 } 3203 3204 if (EXT4_JOURNAL(inode) && 3205 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3206 /* 3207 * This is a REALLY heavyweight approach, but the use of 3208 * bmap on dirty files is expected to be extremely rare: 3209 * only if we run lilo or swapon on a freshly made file 3210 * do we expect this to happen. 3211 * 3212 * (bmap requires CAP_SYS_RAWIO so this does not 3213 * represent an unprivileged user DOS attack --- we'd be 3214 * in trouble if mortal users could trigger this path at 3215 * will.) 3216 * 3217 * NB. EXT4_STATE_JDATA is not set on files other than 3218 * regular files. If somebody wants to bmap a directory 3219 * or symlink and gets confused because the buffer 3220 * hasn't yet been flushed to disk, they deserve 3221 * everything they get. 3222 */ 3223 3224 ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3225 journal = EXT4_JOURNAL(inode); 3226 jbd2_journal_lock_updates(journal); 3227 err = jbd2_journal_flush(journal); 3228 jbd2_journal_unlock_updates(journal); 3229 3230 if (err) 3231 goto out; 3232 } 3233 3234 ret = iomap_bmap(mapping, block, &ext4_iomap_ops); 3235 3236out: 3237 inode_unlock_shared(inode); 3238 return ret; 3239} 3240 3241static int ext4_readpage(struct file *file, struct page *page) 3242{ 3243 int ret = -EAGAIN; 3244 struct inode *inode = page->mapping->host; 3245 3246 trace_ext4_readpage(page); 3247 3248 if (ext4_has_inline_data(inode)) 3249 ret = ext4_readpage_inline(inode, page); 3250 3251 if (ret == -EAGAIN) 3252 return ext4_mpage_readpages(inode, NULL, page); 3253 3254 return ret; 3255} 3256 3257static void ext4_readahead(struct readahead_control *rac) 3258{ 3259 struct inode *inode = rac->mapping->host; 3260 3261 /* If the file has inline data, no need to do readahead. */ 3262 if (ext4_has_inline_data(inode)) 3263 return; 3264 3265 ext4_mpage_readpages(inode, rac, NULL); 3266} 3267 3268static void ext4_invalidatepage(struct page *page, unsigned int offset, 3269 unsigned int length) 3270{ 3271 trace_ext4_invalidatepage(page, offset, length); 3272 3273 /* No journalling happens on data buffers when this function is used */ 3274 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 3275 3276 block_invalidatepage(page, offset, length); 3277} 3278 3279static int __ext4_journalled_invalidatepage(struct page *page, 3280 unsigned int offset, 3281 unsigned int length) 3282{ 3283 journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3284 3285 trace_ext4_journalled_invalidatepage(page, offset, length); 3286 3287 /* 3288 * If it's a full truncate we just forget about the pending dirtying 3289 */ 3290 if (offset == 0 && length == PAGE_SIZE) 3291 ClearPageChecked(page); 3292 3293 return jbd2_journal_invalidatepage(journal, page, offset, length); 3294} 3295 3296/* Wrapper for aops... */ 3297static void ext4_journalled_invalidatepage(struct page *page, 3298 unsigned int offset, 3299 unsigned int length) 3300{ 3301 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3302} 3303 3304static int ext4_releasepage(struct page *page, gfp_t wait) 3305{ 3306 journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3307 3308 trace_ext4_releasepage(page); 3309 3310 /* Page has dirty journalled data -> cannot release */ 3311 if (PageChecked(page)) 3312 return 0; 3313 if (journal) 3314 return jbd2_journal_try_to_free_buffers(journal, page); 3315 else 3316 return try_to_free_buffers(page); 3317} 3318 3319static bool ext4_inode_datasync_dirty(struct inode *inode) 3320{ 3321 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3322 3323 if (journal) { 3324 if (jbd2_transaction_committed(journal, 3325 EXT4_I(inode)->i_datasync_tid)) 3326 return false; 3327 if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 3328 return !list_empty(&EXT4_I(inode)->i_fc_list); 3329 return true; 3330 } 3331 3332 /* Any metadata buffers to write? */ 3333 if (!list_empty(&inode->i_mapping->private_list)) 3334 return true; 3335 return inode->i_state & I_DIRTY_DATASYNC; 3336} 3337 3338static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3339 struct ext4_map_blocks *map, loff_t offset, 3340 loff_t length) 3341{ 3342 u8 blkbits = inode->i_blkbits; 3343 3344 /* 3345 * Writes that span EOF might trigger an I/O size update on completion, 3346 * so consider them to be dirty for the purpose of O_DSYNC, even if 3347 * there is no other metadata changes being made or are pending. 3348 */ 3349 iomap->flags = 0; 3350 if (ext4_inode_datasync_dirty(inode) || 3351 offset + length > i_size_read(inode)) 3352 iomap->flags |= IOMAP_F_DIRTY; 3353 3354 if (map->m_flags & EXT4_MAP_NEW) 3355 iomap->flags |= IOMAP_F_NEW; 3356 3357 iomap->bdev = inode->i_sb->s_bdev; 3358 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3359 iomap->offset = (u64) map->m_lblk << blkbits; 3360 iomap->length = (u64) map->m_len << blkbits; 3361 3362 if ((map->m_flags & EXT4_MAP_MAPPED) && 3363 !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3364 iomap->flags |= IOMAP_F_MERGED; 3365 3366 /* 3367 * Flags passed to ext4_map_blocks() for direct I/O writes can result 3368 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3369 * set. In order for any allocated unwritten extents to be converted 3370 * into written extents correctly within the ->end_io() handler, we 3371 * need to ensure that the iomap->type is set appropriately. Hence, the 3372 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3373 * been set first. 3374 */ 3375 if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3376 iomap->type = IOMAP_UNWRITTEN; 3377 iomap->addr = (u64) map->m_pblk << blkbits; 3378 } else if (map->m_flags & EXT4_MAP_MAPPED) { 3379 iomap->type = IOMAP_MAPPED; 3380 iomap->addr = (u64) map->m_pblk << blkbits; 3381 } else { 3382 iomap->type = IOMAP_HOLE; 3383 iomap->addr = IOMAP_NULL_ADDR; 3384 } 3385} 3386 3387static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3388 unsigned int flags) 3389{ 3390 handle_t *handle; 3391 u8 blkbits = inode->i_blkbits; 3392 int ret, dio_credits, m_flags = 0, retries = 0; 3393 3394 /* 3395 * Trim the mapping request to the maximum value that we can map at 3396 * once for direct I/O. 3397 */ 3398 if (map->m_len > DIO_MAX_BLOCKS) 3399 map->m_len = DIO_MAX_BLOCKS; 3400 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3401 3402retry: 3403 /* 3404 * Either we allocate blocks and then don't get an unwritten extent, so 3405 * in that case we have reserved enough credits. Or, the blocks are 3406 * already allocated and unwritten. In that case, the extent conversion 3407 * fits into the credits as well. 3408 */ 3409 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3410 if (IS_ERR(handle)) 3411 return PTR_ERR(handle); 3412 3413 /* 3414 * DAX and direct I/O are the only two operations that are currently 3415 * supported with IOMAP_WRITE. 3416 */ 3417 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3418 if (IS_DAX(inode)) 3419 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3420 /* 3421 * We use i_size instead of i_disksize here because delalloc writeback 3422 * can complete at any point during the I/O and subsequently push the 3423 * i_disksize out to i_size. This could be beyond where direct I/O is 3424 * happening and thus expose allocated blocks to direct I/O reads. 3425 */ 3426 else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3427 m_flags = EXT4_GET_BLOCKS_CREATE; 3428 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3429 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3430 3431 ret = ext4_map_blocks(handle, inode, map, m_flags); 3432 3433 /* 3434 * We cannot fill holes in indirect tree based inodes as that could 3435 * expose stale data in the case of a crash. Use the magic error code 3436 * to fallback to buffered I/O. 3437 */ 3438 if (!m_flags && !ret) 3439 ret = -ENOTBLK; 3440 3441 ext4_journal_stop(handle); 3442 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3443 goto retry; 3444 3445 return ret; 3446} 3447 3448 3449static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3450 unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3451{ 3452 int ret; 3453 struct ext4_map_blocks map; 3454 u8 blkbits = inode->i_blkbits; 3455 3456 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3457 return -EINVAL; 3458 3459 if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3460 return -ERANGE; 3461 3462 /* 3463 * Calculate the first and last logical blocks respectively. 3464 */ 3465 map.m_lblk = offset >> blkbits; 3466 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 3467 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3468 3469 if (flags & IOMAP_WRITE) { 3470 /* 3471 * We check here if the blocks are already allocated, then we 3472 * don't need to start a journal txn and we can directly return 3473 * the mapping information. This could boost performance 3474 * especially in multi-threaded overwrite requests. 3475 */ 3476 if (offset + length <= i_size_read(inode)) { 3477 ret = ext4_map_blocks(NULL, inode, &map, 0); 3478 if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 3479 goto out; 3480 } 3481 ret = ext4_iomap_alloc(inode, &map, flags); 3482 } else { 3483 ret = ext4_map_blocks(NULL, inode, &map, 0); 3484 } 3485 3486 if (ret < 0) 3487 return ret; 3488out: 3489 ext4_set_iomap(inode, iomap, &map, offset, length); 3490 3491 return 0; 3492} 3493 3494static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 3495 loff_t length, unsigned flags, struct iomap *iomap, 3496 struct iomap *srcmap) 3497{ 3498 int ret; 3499 3500 /* 3501 * Even for writes we don't need to allocate blocks, so just pretend 3502 * we are reading to save overhead of starting a transaction. 3503 */ 3504 flags &= ~IOMAP_WRITE; 3505 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 3506 WARN_ON_ONCE(!ret && iomap->type != IOMAP_MAPPED); 3507 return ret; 3508} 3509 3510static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3511 ssize_t written, unsigned flags, struct iomap *iomap) 3512{ 3513 /* 3514 * Check to see whether an error occurred while writing out the data to 3515 * the allocated blocks. If so, return the magic error code so that we 3516 * fallback to buffered I/O and attempt to complete the remainder of 3517 * the I/O. Any blocks that may have been allocated in preparation for 3518 * the direct I/O will be reused during buffered I/O. 3519 */ 3520 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3521 return -ENOTBLK; 3522 3523 return 0; 3524} 3525 3526const struct iomap_ops ext4_iomap_ops = { 3527 .iomap_begin = ext4_iomap_begin, 3528 .iomap_end = ext4_iomap_end, 3529}; 3530 3531const struct iomap_ops ext4_iomap_overwrite_ops = { 3532 .iomap_begin = ext4_iomap_overwrite_begin, 3533 .iomap_end = ext4_iomap_end, 3534}; 3535 3536static bool ext4_iomap_is_delalloc(struct inode *inode, 3537 struct ext4_map_blocks *map) 3538{ 3539 struct extent_status es; 3540 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 3541 3542 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 3543 map->m_lblk, end, &es); 3544 3545 if (!es.es_len || es.es_lblk > end) 3546 return false; 3547 3548 if (es.es_lblk > map->m_lblk) { 3549 map->m_len = es.es_lblk - map->m_lblk; 3550 return false; 3551 } 3552 3553 offset = map->m_lblk - es.es_lblk; 3554 map->m_len = es.es_len - offset; 3555 3556 return true; 3557} 3558 3559static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 3560 loff_t length, unsigned int flags, 3561 struct iomap *iomap, struct iomap *srcmap) 3562{ 3563 int ret; 3564 bool delalloc = false; 3565 struct ext4_map_blocks map; 3566 u8 blkbits = inode->i_blkbits; 3567 3568 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3569 return -EINVAL; 3570 3571 if (ext4_has_inline_data(inode)) { 3572 ret = ext4_inline_data_iomap(inode, iomap); 3573 if (ret != -EAGAIN) { 3574 if (ret == 0 && offset >= iomap->length) 3575 ret = -ENOENT; 3576 return ret; 3577 } 3578 } 3579 3580 /* 3581 * Calculate the first and last logical block respectively. 3582 */ 3583 map.m_lblk = offset >> blkbits; 3584 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 3585 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3586 3587 /* 3588 * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3589 * So handle it here itself instead of querying ext4_map_blocks(). 3590 * Since ext4_map_blocks() will warn about it and will return 3591 * -EIO error. 3592 */ 3593 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3594 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3595 3596 if (offset >= sbi->s_bitmap_maxbytes) { 3597 map.m_flags = 0; 3598 goto set_iomap; 3599 } 3600 } 3601 3602 ret = ext4_map_blocks(NULL, inode, &map, 0); 3603 if (ret < 0) 3604 return ret; 3605 if (ret == 0) 3606 delalloc = ext4_iomap_is_delalloc(inode, &map); 3607 3608set_iomap: 3609 ext4_set_iomap(inode, iomap, &map, offset, length); 3610 if (delalloc && iomap->type == IOMAP_HOLE) 3611 iomap->type = IOMAP_DELALLOC; 3612 3613 return 0; 3614} 3615 3616const struct iomap_ops ext4_iomap_report_ops = { 3617 .iomap_begin = ext4_iomap_begin_report, 3618}; 3619 3620/* 3621 * Pages can be marked dirty completely asynchronously from ext4's journalling 3622 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3623 * much here because ->set_page_dirty is called under VFS locks. The page is 3624 * not necessarily locked. 3625 * 3626 * We cannot just dirty the page and leave attached buffers clean, because the 3627 * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3628 * or jbddirty because all the journalling code will explode. 3629 * 3630 * So what we do is to mark the page "pending dirty" and next time writepage 3631 * is called, propagate that into the buffers appropriately. 3632 */ 3633static int ext4_journalled_set_page_dirty(struct page *page) 3634{ 3635 SetPageChecked(page); 3636 return __set_page_dirty_nobuffers(page); 3637} 3638 3639static int ext4_set_page_dirty(struct page *page) 3640{ 3641 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 3642 WARN_ON_ONCE(!page_has_buffers(page)); 3643 return __set_page_dirty_buffers(page); 3644} 3645 3646static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 3647 struct file *file, sector_t *span) 3648{ 3649 return iomap_swapfile_activate(sis, file, span, 3650 &ext4_iomap_report_ops); 3651} 3652 3653static const struct address_space_operations ext4_aops = { 3654 .readpage = ext4_readpage, 3655 .readahead = ext4_readahead, 3656 .writepage = ext4_writepage, 3657 .writepages = ext4_writepages, 3658 .write_begin = ext4_write_begin, 3659 .write_end = ext4_write_end, 3660 .set_page_dirty = ext4_set_page_dirty, 3661 .bmap = ext4_bmap, 3662 .invalidatepage = ext4_invalidatepage, 3663 .releasepage = ext4_releasepage, 3664 .direct_IO = noop_direct_IO, 3665 .migratepage = buffer_migrate_page, 3666 .is_partially_uptodate = block_is_partially_uptodate, 3667 .error_remove_page = generic_error_remove_page, 3668 .swap_activate = ext4_iomap_swap_activate, 3669}; 3670 3671static const struct address_space_operations ext4_journalled_aops = { 3672 .readpage = ext4_readpage, 3673 .readahead = ext4_readahead, 3674 .writepage = ext4_writepage, 3675 .writepages = ext4_writepages, 3676 .write_begin = ext4_write_begin, 3677 .write_end = ext4_journalled_write_end, 3678 .set_page_dirty = ext4_journalled_set_page_dirty, 3679 .bmap = ext4_bmap, 3680 .invalidatepage = ext4_journalled_invalidatepage, 3681 .releasepage = ext4_releasepage, 3682 .direct_IO = noop_direct_IO, 3683 .is_partially_uptodate = block_is_partially_uptodate, 3684 .error_remove_page = generic_error_remove_page, 3685 .swap_activate = ext4_iomap_swap_activate, 3686}; 3687 3688static const struct address_space_operations ext4_da_aops = { 3689 .readpage = ext4_readpage, 3690 .readahead = ext4_readahead, 3691 .writepage = ext4_writepage, 3692 .writepages = ext4_writepages, 3693 .write_begin = ext4_da_write_begin, 3694 .write_end = ext4_da_write_end, 3695 .set_page_dirty = ext4_set_page_dirty, 3696 .bmap = ext4_bmap, 3697 .invalidatepage = ext4_invalidatepage, 3698 .releasepage = ext4_releasepage, 3699 .direct_IO = noop_direct_IO, 3700 .migratepage = buffer_migrate_page, 3701 .is_partially_uptodate = block_is_partially_uptodate, 3702 .error_remove_page = generic_error_remove_page, 3703 .swap_activate = ext4_iomap_swap_activate, 3704}; 3705 3706static const struct address_space_operations ext4_dax_aops = { 3707 .writepages = ext4_dax_writepages, 3708 .direct_IO = noop_direct_IO, 3709 .set_page_dirty = noop_set_page_dirty, 3710 .bmap = ext4_bmap, 3711 .invalidatepage = noop_invalidatepage, 3712 .swap_activate = ext4_iomap_swap_activate, 3713}; 3714 3715void ext4_set_aops(struct inode *inode) 3716{ 3717 switch (ext4_inode_journal_mode(inode)) { 3718 case EXT4_INODE_ORDERED_DATA_MODE: 3719 case EXT4_INODE_WRITEBACK_DATA_MODE: 3720 break; 3721 case EXT4_INODE_JOURNAL_DATA_MODE: 3722 inode->i_mapping->a_ops = &ext4_journalled_aops; 3723 return; 3724 default: 3725 BUG(); 3726 } 3727 if (IS_DAX(inode)) 3728 inode->i_mapping->a_ops = &ext4_dax_aops; 3729 else if (test_opt(inode->i_sb, DELALLOC)) 3730 inode->i_mapping->a_ops = &ext4_da_aops; 3731 else 3732 inode->i_mapping->a_ops = &ext4_aops; 3733} 3734 3735static int __ext4_block_zero_page_range(handle_t *handle, 3736 struct address_space *mapping, loff_t from, loff_t length) 3737{ 3738 ext4_fsblk_t index = from >> PAGE_SHIFT; 3739 unsigned offset = from & (PAGE_SIZE-1); 3740 unsigned blocksize, pos; 3741 ext4_lblk_t iblock; 3742 struct inode *inode = mapping->host; 3743 struct buffer_head *bh; 3744 struct page *page; 3745 int err = 0; 3746 3747 page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3748 mapping_gfp_constraint(mapping, ~__GFP_FS)); 3749 if (!page) 3750 return -ENOMEM; 3751 3752 blocksize = inode->i_sb->s_blocksize; 3753 3754 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3755 3756 if (!page_has_buffers(page)) 3757 create_empty_buffers(page, blocksize, 0); 3758 3759 /* Find the buffer that contains "offset" */ 3760 bh = page_buffers(page); 3761 pos = blocksize; 3762 while (offset >= pos) { 3763 bh = bh->b_this_page; 3764 iblock++; 3765 pos += blocksize; 3766 } 3767 if (buffer_freed(bh)) { 3768 BUFFER_TRACE(bh, "freed: skip"); 3769 goto unlock; 3770 } 3771 if (!buffer_mapped(bh)) { 3772 BUFFER_TRACE(bh, "unmapped"); 3773 ext4_get_block(inode, iblock, bh, 0); 3774 /* unmapped? It's a hole - nothing to do */ 3775 if (!buffer_mapped(bh)) { 3776 BUFFER_TRACE(bh, "still unmapped"); 3777 goto unlock; 3778 } 3779 } 3780 3781 /* Ok, it's mapped. Make sure it's up-to-date */ 3782 if (PageUptodate(page)) 3783 set_buffer_uptodate(bh); 3784 3785 if (!buffer_uptodate(bh)) { 3786 err = ext4_read_bh_lock(bh, 0, true); 3787 if (err) 3788 goto unlock; 3789 if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3790 /* We expect the key to be set. */ 3791 BUG_ON(!fscrypt_has_encryption_key(inode)); 3792 err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3793 bh_offset(bh)); 3794 if (err) { 3795 clear_buffer_uptodate(bh); 3796 goto unlock; 3797 } 3798 } 3799 } 3800 if (ext4_should_journal_data(inode)) { 3801 BUFFER_TRACE(bh, "get write access"); 3802 err = ext4_journal_get_write_access(handle, bh); 3803 if (err) 3804 goto unlock; 3805 } 3806 zero_user(page, offset, length); 3807 BUFFER_TRACE(bh, "zeroed end of block"); 3808 3809 if (ext4_should_journal_data(inode)) { 3810 err = ext4_handle_dirty_metadata(handle, inode, bh); 3811 } else { 3812 err = 0; 3813 mark_buffer_dirty(bh); 3814 if (ext4_should_order_data(inode)) 3815 err = ext4_jbd2_inode_add_write(handle, inode, from, 3816 length); 3817 } 3818 3819unlock: 3820 unlock_page(page); 3821 put_page(page); 3822 return err; 3823} 3824 3825/* 3826 * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3827 * starting from file offset 'from'. The range to be zero'd must 3828 * be contained with in one block. If the specified range exceeds 3829 * the end of the block it will be shortened to end of the block 3830 * that corresponds to 'from' 3831 */ 3832static int ext4_block_zero_page_range(handle_t *handle, 3833 struct address_space *mapping, loff_t from, loff_t length) 3834{ 3835 struct inode *inode = mapping->host; 3836 unsigned offset = from & (PAGE_SIZE-1); 3837 unsigned blocksize = inode->i_sb->s_blocksize; 3838 unsigned max = blocksize - (offset & (blocksize - 1)); 3839 3840 /* 3841 * correct length if it does not fall between 3842 * 'from' and the end of the block 3843 */ 3844 if (length > max || length < 0) 3845 length = max; 3846 3847 if (IS_DAX(inode)) { 3848 return iomap_zero_range(inode, from, length, NULL, 3849 &ext4_iomap_ops); 3850 } 3851 return __ext4_block_zero_page_range(handle, mapping, from, length); 3852} 3853 3854/* 3855 * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3856 * up to the end of the block which corresponds to `from'. 3857 * This required during truncate. We need to physically zero the tail end 3858 * of that block so it doesn't yield old data if the file is later grown. 3859 */ 3860static int ext4_block_truncate_page(handle_t *handle, 3861 struct address_space *mapping, loff_t from) 3862{ 3863 unsigned offset = from & (PAGE_SIZE-1); 3864 unsigned length; 3865 unsigned blocksize; 3866 struct inode *inode = mapping->host; 3867 3868 /* If we are processing an encrypted inode during orphan list handling */ 3869 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 3870 return 0; 3871 3872 blocksize = inode->i_sb->s_blocksize; 3873 length = blocksize - (offset & (blocksize - 1)); 3874 3875 return ext4_block_zero_page_range(handle, mapping, from, length); 3876} 3877 3878int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3879 loff_t lstart, loff_t length) 3880{ 3881 struct super_block *sb = inode->i_sb; 3882 struct address_space *mapping = inode->i_mapping; 3883 unsigned partial_start, partial_end; 3884 ext4_fsblk_t start, end; 3885 loff_t byte_end = (lstart + length - 1); 3886 int err = 0; 3887 3888 partial_start = lstart & (sb->s_blocksize - 1); 3889 partial_end = byte_end & (sb->s_blocksize - 1); 3890 3891 start = lstart >> sb->s_blocksize_bits; 3892 end = byte_end >> sb->s_blocksize_bits; 3893 3894 /* Handle partial zero within the single block */ 3895 if (start == end && 3896 (partial_start || (partial_end != sb->s_blocksize - 1))) { 3897 err = ext4_block_zero_page_range(handle, mapping, 3898 lstart, length); 3899 return err; 3900 } 3901 /* Handle partial zero out on the start of the range */ 3902 if (partial_start) { 3903 err = ext4_block_zero_page_range(handle, mapping, 3904 lstart, sb->s_blocksize); 3905 if (err) 3906 return err; 3907 } 3908 /* Handle partial zero out on the end of the range */ 3909 if (partial_end != sb->s_blocksize - 1) 3910 err = ext4_block_zero_page_range(handle, mapping, 3911 byte_end - partial_end, 3912 partial_end + 1); 3913 return err; 3914} 3915 3916int ext4_can_truncate(struct inode *inode) 3917{ 3918 if (S_ISREG(inode->i_mode)) 3919 return 1; 3920 if (S_ISDIR(inode->i_mode)) 3921 return 1; 3922 if (S_ISLNK(inode->i_mode)) 3923 return !ext4_inode_is_fast_symlink(inode); 3924 return 0; 3925} 3926 3927/* 3928 * We have to make sure i_disksize gets properly updated before we truncate 3929 * page cache due to hole punching or zero range. Otherwise i_disksize update 3930 * can get lost as it may have been postponed to submission of writeback but 3931 * that will never happen after we truncate page cache. 3932 */ 3933int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 3934 loff_t len) 3935{ 3936 handle_t *handle; 3937 int ret; 3938 3939 loff_t size = i_size_read(inode); 3940 3941 WARN_ON(!inode_is_locked(inode)); 3942 if (offset > size || offset + len < size) 3943 return 0; 3944 3945 if (EXT4_I(inode)->i_disksize >= size) 3946 return 0; 3947 3948 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 3949 if (IS_ERR(handle)) 3950 return PTR_ERR(handle); 3951 ext4_update_i_disksize(inode, size); 3952 ret = ext4_mark_inode_dirty(handle, inode); 3953 ext4_journal_stop(handle); 3954 3955 return ret; 3956} 3957 3958static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3959{ 3960 up_write(&ei->i_mmap_sem); 3961 schedule(); 3962 down_write(&ei->i_mmap_sem); 3963} 3964 3965int ext4_break_layouts(struct inode *inode) 3966{ 3967 struct ext4_inode_info *ei = EXT4_I(inode); 3968 struct page *page; 3969 int error; 3970 3971 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3972 return -EINVAL; 3973 3974 do { 3975 page = dax_layout_busy_page(inode->i_mapping); 3976 if (!page) 3977 return 0; 3978 3979 error = ___wait_var_event(&page->_refcount, 3980 atomic_read(&page->_refcount) == 1, 3981 TASK_INTERRUPTIBLE, 0, 0, 3982 ext4_wait_dax_page(ei)); 3983 } while (error == 0); 3984 3985 return error; 3986} 3987 3988/* 3989 * ext4_punch_hole: punches a hole in a file by releasing the blocks 3990 * associated with the given offset and length 3991 * 3992 * @inode: File inode 3993 * @offset: The offset where the hole will begin 3994 * @len: The length of the hole 3995 * 3996 * Returns: 0 on success or negative on failure 3997 */ 3998 3999int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 4000{ 4001 struct inode *inode = file_inode(file); 4002 struct super_block *sb = inode->i_sb; 4003 ext4_lblk_t first_block, stop_block; 4004 struct address_space *mapping = inode->i_mapping; 4005 loff_t first_block_offset, last_block_offset, max_length; 4006 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4007 handle_t *handle; 4008 unsigned int credits; 4009 int ret = 0, ret2 = 0; 4010 4011 trace_ext4_punch_hole(inode, offset, length, 0); 4012 4013 /* 4014 * Write out all dirty pages to avoid race conditions 4015 * Then release them. 4016 */ 4017 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 4018 ret = filemap_write_and_wait_range(mapping, offset, 4019 offset + length - 1); 4020 if (ret) 4021 return ret; 4022 } 4023 4024 inode_lock(inode); 4025 4026 /* No need to punch hole beyond i_size */ 4027 if (offset >= inode->i_size) 4028 goto out_mutex; 4029 4030 /* 4031 * If the hole extends beyond i_size, set the hole 4032 * to end after the page that contains i_size 4033 */ 4034 if (offset + length > inode->i_size) { 4035 length = inode->i_size + 4036 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 4037 offset; 4038 } 4039 4040 /* 4041 * For punch hole the length + offset needs to be within one block 4042 * before last range. Adjust the length if it goes beyond that limit. 4043 */ 4044 max_length = sbi->s_bitmap_maxbytes - inode->i_sb->s_blocksize; 4045 if (offset + length > max_length) 4046 length = max_length - offset; 4047 4048 if (offset & (sb->s_blocksize - 1) || 4049 (offset + length) & (sb->s_blocksize - 1)) { 4050 /* 4051 * Attach jinode to inode for jbd2 if we do any zeroing of 4052 * partial block 4053 */ 4054 ret = ext4_inode_attach_jinode(inode); 4055 if (ret < 0) 4056 goto out_mutex; 4057 4058 } 4059 4060 /* Wait all existing dio workers, newcomers will block on i_mutex */ 4061 inode_dio_wait(inode); 4062 4063 ret = file_modified(file); 4064 if (ret) 4065 goto out_mutex; 4066 4067 /* 4068 * Prevent page faults from reinstantiating pages we have released from 4069 * page cache. 4070 */ 4071 down_write(&EXT4_I(inode)->i_mmap_sem); 4072 4073 ret = ext4_break_layouts(inode); 4074 if (ret) 4075 goto out_dio; 4076 4077 first_block_offset = round_up(offset, sb->s_blocksize); 4078 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 4079 4080 /* Now release the pages and zero block aligned part of pages*/ 4081 if (last_block_offset > first_block_offset) { 4082 ret = ext4_update_disksize_before_punch(inode, offset, length); 4083 if (ret) 4084 goto out_dio; 4085 truncate_pagecache_range(inode, first_block_offset, 4086 last_block_offset); 4087 } 4088 4089 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4090 credits = ext4_writepage_trans_blocks(inode); 4091 else 4092 credits = ext4_blocks_for_truncate(inode); 4093 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 4094 if (IS_ERR(handle)) { 4095 ret = PTR_ERR(handle); 4096 ext4_std_error(sb, ret); 4097 goto out_dio; 4098 } 4099 4100 ret = ext4_zero_partial_blocks(handle, inode, offset, 4101 length); 4102 if (ret) 4103 goto out_stop; 4104 4105 first_block = (offset + sb->s_blocksize - 1) >> 4106 EXT4_BLOCK_SIZE_BITS(sb); 4107 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 4108 4109 /* If there are blocks to remove, do it */ 4110 if (stop_block > first_block) { 4111 4112 down_write(&EXT4_I(inode)->i_data_sem); 4113 ext4_discard_preallocations(inode, 0); 4114 4115 ret = ext4_es_remove_extent(inode, first_block, 4116 stop_block - first_block); 4117 if (ret) { 4118 up_write(&EXT4_I(inode)->i_data_sem); 4119 goto out_stop; 4120 } 4121 4122 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4123 ret = ext4_ext_remove_space(inode, first_block, 4124 stop_block - 1); 4125 else 4126 ret = ext4_ind_remove_space(handle, inode, first_block, 4127 stop_block); 4128 4129 up_write(&EXT4_I(inode)->i_data_sem); 4130 } 4131 ext4_fc_track_range(handle, inode, first_block, stop_block); 4132 if (IS_SYNC(inode)) 4133 ext4_handle_sync(handle); 4134 4135 inode->i_mtime = inode->i_ctime = current_time(inode); 4136 ret2 = ext4_mark_inode_dirty(handle, inode); 4137 if (unlikely(ret2)) 4138 ret = ret2; 4139 if (ret >= 0) 4140 ext4_update_inode_fsync_trans(handle, inode, 1); 4141out_stop: 4142 ext4_journal_stop(handle); 4143out_dio: 4144 up_write(&EXT4_I(inode)->i_mmap_sem); 4145out_mutex: 4146 inode_unlock(inode); 4147 return ret; 4148} 4149 4150int ext4_inode_attach_jinode(struct inode *inode) 4151{ 4152 struct ext4_inode_info *ei = EXT4_I(inode); 4153 struct jbd2_inode *jinode; 4154 4155 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4156 return 0; 4157 4158 jinode = jbd2_alloc_inode(GFP_KERNEL); 4159 spin_lock(&inode->i_lock); 4160 if (!ei->jinode) { 4161 if (!jinode) { 4162 spin_unlock(&inode->i_lock); 4163 return -ENOMEM; 4164 } 4165 ei->jinode = jinode; 4166 jbd2_journal_init_jbd_inode(ei->jinode, inode); 4167 jinode = NULL; 4168 } 4169 spin_unlock(&inode->i_lock); 4170 if (unlikely(jinode != NULL)) 4171 jbd2_free_inode(jinode); 4172 return 0; 4173} 4174 4175/* 4176 * ext4_truncate() 4177 * 4178 * We block out ext4_get_block() block instantiations across the entire 4179 * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4180 * simultaneously on behalf of the same inode. 4181 * 4182 * As we work through the truncate and commit bits of it to the journal there 4183 * is one core, guiding principle: the file's tree must always be consistent on 4184 * disk. We must be able to restart the truncate after a crash. 4185 * 4186 * The file's tree may be transiently inconsistent in memory (although it 4187 * probably isn't), but whenever we close off and commit a journal transaction, 4188 * the contents of (the filesystem + the journal) must be consistent and 4189 * restartable. It's pretty simple, really: bottom up, right to left (although 4190 * left-to-right works OK too). 4191 * 4192 * Note that at recovery time, journal replay occurs *before* the restart of 4193 * truncate against the orphan inode list. 4194 * 4195 * The committed inode has the new, desired i_size (which is the same as 4196 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4197 * that this inode's truncate did not complete and it will again call 4198 * ext4_truncate() to have another go. So there will be instantiated blocks 4199 * to the right of the truncation point in a crashed ext4 filesystem. But 4200 * that's fine - as long as they are linked from the inode, the post-crash 4201 * ext4_truncate() run will find them and release them. 4202 */ 4203int ext4_truncate(struct inode *inode) 4204{ 4205 struct ext4_inode_info *ei = EXT4_I(inode); 4206 unsigned int credits; 4207 int err = 0, err2; 4208 handle_t *handle; 4209 struct address_space *mapping = inode->i_mapping; 4210 4211 /* 4212 * There is a possibility that we're either freeing the inode 4213 * or it's a completely new inode. In those cases we might not 4214 * have i_mutex locked because it's not necessary. 4215 */ 4216 if (!(inode->i_state & (I_NEW|I_FREEING))) 4217 WARN_ON(!inode_is_locked(inode)); 4218 trace_ext4_truncate_enter(inode); 4219 4220 if (!ext4_can_truncate(inode)) 4221 goto out_trace; 4222 4223 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 4224 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 4225 4226 if (ext4_has_inline_data(inode)) { 4227 int has_inline = 1; 4228 4229 err = ext4_inline_data_truncate(inode, &has_inline); 4230 if (err || has_inline) 4231 goto out_trace; 4232 } 4233 4234 /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4235 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4236 err = ext4_inode_attach_jinode(inode); 4237 if (err) 4238 goto out_trace; 4239 } 4240 4241 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4242 credits = ext4_writepage_trans_blocks(inode); 4243 else 4244 credits = ext4_blocks_for_truncate(inode); 4245 4246 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 4247 if (IS_ERR(handle)) { 4248 err = PTR_ERR(handle); 4249 goto out_trace; 4250 } 4251 4252 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4253 ext4_block_truncate_page(handle, mapping, inode->i_size); 4254 4255 /* 4256 * We add the inode to the orphan list, so that if this 4257 * truncate spans multiple transactions, and we crash, we will 4258 * resume the truncate when the filesystem recovers. It also 4259 * marks the inode dirty, to catch the new size. 4260 * 4261 * Implication: the file must always be in a sane, consistent 4262 * truncatable state while each transaction commits. 4263 */ 4264 err = ext4_orphan_add(handle, inode); 4265 if (err) 4266 goto out_stop; 4267 4268 down_write(&EXT4_I(inode)->i_data_sem); 4269 4270 ext4_discard_preallocations(inode, 0); 4271 4272 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4273 err = ext4_ext_truncate(handle, inode); 4274 else 4275 ext4_ind_truncate(handle, inode); 4276 4277 up_write(&ei->i_data_sem); 4278 if (err) 4279 goto out_stop; 4280 4281 if (IS_SYNC(inode)) 4282 ext4_handle_sync(handle); 4283 4284out_stop: 4285 /* 4286 * If this was a simple ftruncate() and the file will remain alive, 4287 * then we need to clear up the orphan record which we created above. 4288 * However, if this was a real unlink then we were called by 4289 * ext4_evict_inode(), and we allow that function to clean up the 4290 * orphan info for us. 4291 */ 4292 if (inode->i_nlink) 4293 ext4_orphan_del(handle, inode); 4294 4295 inode->i_mtime = inode->i_ctime = current_time(inode); 4296 err2 = ext4_mark_inode_dirty(handle, inode); 4297 if (unlikely(err2 && !err)) 4298 err = err2; 4299 ext4_journal_stop(handle); 4300 4301out_trace: 4302 trace_ext4_truncate_exit(inode); 4303 return err; 4304} 4305 4306static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4307{ 4308 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4309 return inode_peek_iversion_raw(inode); 4310 else 4311 return inode_peek_iversion(inode); 4312} 4313 4314static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, 4315 struct ext4_inode_info *ei) 4316{ 4317 struct inode *inode = &(ei->vfs_inode); 4318 u64 i_blocks = READ_ONCE(inode->i_blocks); 4319 struct super_block *sb = inode->i_sb; 4320 4321 if (i_blocks <= ~0U) { 4322 /* 4323 * i_blocks can be represented in a 32 bit variable 4324 * as multiple of 512 bytes 4325 */ 4326 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 4327 raw_inode->i_blocks_high = 0; 4328 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4329 return 0; 4330 } 4331 4332 /* 4333 * This should never happen since sb->s_maxbytes should not have 4334 * allowed this, sb->s_maxbytes was set according to the huge_file 4335 * feature in ext4_fill_super(). 4336 */ 4337 if (!ext4_has_feature_huge_file(sb)) 4338 return -EFSCORRUPTED; 4339 4340 if (i_blocks <= 0xffffffffffffULL) { 4341 /* 4342 * i_blocks can be represented in a 48 bit variable 4343 * as multiple of 512 bytes 4344 */ 4345 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 4346 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 4347 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4348 } else { 4349 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4350 /* i_block is stored in file system block size */ 4351 i_blocks = i_blocks >> (inode->i_blkbits - 9); 4352 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 4353 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 4354 } 4355 return 0; 4356} 4357 4358static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) 4359{ 4360 struct ext4_inode_info *ei = EXT4_I(inode); 4361 uid_t i_uid; 4362 gid_t i_gid; 4363 projid_t i_projid; 4364 int block; 4365 int err; 4366 4367 err = ext4_inode_blocks_set(raw_inode, ei); 4368 4369 raw_inode->i_mode = cpu_to_le16(inode->i_mode); 4370 i_uid = i_uid_read(inode); 4371 i_gid = i_gid_read(inode); 4372 i_projid = from_kprojid(&init_user_ns, ei->i_projid); 4373 if (!(test_opt(inode->i_sb, NO_UID32))) { 4374 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 4375 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4376 /* 4377 * Fix up interoperability with old kernels. Otherwise, 4378 * old inodes get re-used with the upper 16 bits of the 4379 * uid/gid intact. 4380 */ 4381 if (ei->i_dtime && list_empty(&ei->i_orphan)) { 4382 raw_inode->i_uid_high = 0; 4383 raw_inode->i_gid_high = 0; 4384 } else { 4385 raw_inode->i_uid_high = 4386 cpu_to_le16(high_16_bits(i_uid)); 4387 raw_inode->i_gid_high = 4388 cpu_to_le16(high_16_bits(i_gid)); 4389 } 4390 } else { 4391 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 4392 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4393 raw_inode->i_uid_high = 0; 4394 raw_inode->i_gid_high = 0; 4395 } 4396 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4397 4398 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4399 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4400 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4401 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4402 4403 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4404 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 4405 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 4406 raw_inode->i_file_acl_high = 4407 cpu_to_le16(ei->i_file_acl >> 32); 4408 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4409 ext4_isize_set(raw_inode, ei->i_disksize); 4410 4411 raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4412 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4413 if (old_valid_dev(inode->i_rdev)) { 4414 raw_inode->i_block[0] = 4415 cpu_to_le32(old_encode_dev(inode->i_rdev)); 4416 raw_inode->i_block[1] = 0; 4417 } else { 4418 raw_inode->i_block[0] = 0; 4419 raw_inode->i_block[1] = 4420 cpu_to_le32(new_encode_dev(inode->i_rdev)); 4421 raw_inode->i_block[2] = 0; 4422 } 4423 } else if (!ext4_has_inline_data(inode)) { 4424 for (block = 0; block < EXT4_N_BLOCKS; block++) 4425 raw_inode->i_block[block] = ei->i_data[block]; 4426 } 4427 4428 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4429 u64 ivers = ext4_inode_peek_iversion(inode); 4430 4431 raw_inode->i_disk_version = cpu_to_le32(ivers); 4432 if (ei->i_extra_isize) { 4433 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4434 raw_inode->i_version_hi = 4435 cpu_to_le32(ivers >> 32); 4436 raw_inode->i_extra_isize = 4437 cpu_to_le16(ei->i_extra_isize); 4438 } 4439 } 4440 4441 if (i_projid != EXT4_DEF_PROJID && 4442 !ext4_has_feature_project(inode->i_sb)) 4443 err = err ?: -EFSCORRUPTED; 4444 4445 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 4446 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4447 raw_inode->i_projid = cpu_to_le32(i_projid); 4448 4449 ext4_inode_csum_set(inode, raw_inode, ei); 4450 return err; 4451} 4452 4453/* 4454 * ext4_get_inode_loc returns with an extra refcount against the inode's 4455 * underlying buffer_head on success. If we pass 'inode' and it does not 4456 * have in-inode xattr, we have all inode data in memory that is needed 4457 * to recreate the on-disk version of this inode. 4458 */ 4459static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 4460 struct inode *inode, struct ext4_iloc *iloc, 4461 ext4_fsblk_t *ret_block) 4462{ 4463 struct ext4_group_desc *gdp; 4464 struct buffer_head *bh; 4465 ext4_fsblk_t block; 4466 struct blk_plug plug; 4467 int inodes_per_block, inode_offset; 4468 4469 iloc->bh = NULL; 4470 if (ino < EXT4_ROOT_INO || 4471 ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 4472 return -EFSCORRUPTED; 4473 4474 iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4475 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4476 if (!gdp) 4477 return -EIO; 4478 4479 /* 4480 * Figure out the offset within the block group inode table 4481 */ 4482 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4483 inode_offset = ((ino - 1) % 4484 EXT4_INODES_PER_GROUP(sb)); 4485 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4486 4487 block = ext4_inode_table(sb, gdp); 4488 if ((block <= le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) || 4489 (block >= ext4_blocks_count(EXT4_SB(sb)->s_es))) { 4490 ext4_error(sb, "Invalid inode table block %llu in " 4491 "block_group %u", block, iloc->block_group); 4492 return -EFSCORRUPTED; 4493 } 4494 block += (inode_offset / inodes_per_block); 4495 4496 bh = sb_getblk(sb, block); 4497 if (unlikely(!bh)) 4498 return -ENOMEM; 4499 if (!buffer_uptodate(bh)) { 4500 lock_buffer(bh); 4501 4502 if (ext4_buffer_uptodate(bh)) { 4503 /* someone brought it uptodate while we waited */ 4504 unlock_buffer(bh); 4505 goto has_buffer; 4506 } 4507 4508 /* 4509 * If we have all information of the inode in memory and this 4510 * is the only valid inode in the block, we need not read the 4511 * block. 4512 */ 4513 if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 4514 struct buffer_head *bitmap_bh; 4515 int i, start; 4516 4517 start = inode_offset & ~(inodes_per_block - 1); 4518 4519 /* Is the inode bitmap in cache? */ 4520 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4521 if (unlikely(!bitmap_bh)) 4522 goto make_io; 4523 4524 /* 4525 * If the inode bitmap isn't in cache then the 4526 * optimisation may end up performing two reads instead 4527 * of one, so skip it. 4528 */ 4529 if (!buffer_uptodate(bitmap_bh)) { 4530 brelse(bitmap_bh); 4531 goto make_io; 4532 } 4533 for (i = start; i < start + inodes_per_block; i++) { 4534 if (i == inode_offset) 4535 continue; 4536 if (ext4_test_bit(i, bitmap_bh->b_data)) 4537 break; 4538 } 4539 brelse(bitmap_bh); 4540 if (i == start + inodes_per_block) { 4541 struct ext4_inode *raw_inode = 4542 (struct ext4_inode *) (bh->b_data + iloc->offset); 4543 4544 /* all other inodes are free, so skip I/O */ 4545 memset(bh->b_data, 0, bh->b_size); 4546 if (!ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4547 ext4_fill_raw_inode(inode, raw_inode); 4548 set_buffer_uptodate(bh); 4549 unlock_buffer(bh); 4550 goto has_buffer; 4551 } 4552 } 4553 4554make_io: 4555 /* 4556 * If we need to do any I/O, try to pre-readahead extra 4557 * blocks from the inode table. 4558 */ 4559 blk_start_plug(&plug); 4560 if (EXT4_SB(sb)->s_inode_readahead_blks) { 4561 ext4_fsblk_t b, end, table; 4562 unsigned num; 4563 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4564 4565 table = ext4_inode_table(sb, gdp); 4566 /* s_inode_readahead_blks is always a power of 2 */ 4567 b = block & ~((ext4_fsblk_t) ra_blks - 1); 4568 if (table > b) 4569 b = table; 4570 end = b + ra_blks; 4571 num = EXT4_INODES_PER_GROUP(sb); 4572 if (ext4_has_group_desc_csum(sb)) 4573 num -= ext4_itable_unused_count(sb, gdp); 4574 table += num / inodes_per_block; 4575 if (end > table) 4576 end = table; 4577 while (b <= end) 4578 ext4_sb_breadahead_unmovable(sb, b++); 4579 } 4580 4581 /* 4582 * There are other valid inodes in the buffer, this inode 4583 * has in-inode xattrs, or we don't have this inode in memory. 4584 * Read the block from disk. 4585 */ 4586 trace_ext4_load_inode(sb, ino); 4587 ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 4588 blk_finish_plug(&plug); 4589 wait_on_buffer(bh); 4590 ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4591 if (!buffer_uptodate(bh)) { 4592 if (ret_block) 4593 *ret_block = block; 4594 brelse(bh); 4595 return -EIO; 4596 } 4597 } 4598has_buffer: 4599 iloc->bh = bh; 4600 return 0; 4601} 4602 4603static int __ext4_get_inode_loc_noinmem(struct inode *inode, 4604 struct ext4_iloc *iloc) 4605{ 4606 ext4_fsblk_t err_blk = 0; 4607 int ret; 4608 4609 ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc, 4610 &err_blk); 4611 4612 if (ret == -EIO) 4613 ext4_error_inode_block(inode, err_blk, EIO, 4614 "unable to read itable block"); 4615 4616 return ret; 4617} 4618 4619int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4620{ 4621 ext4_fsblk_t err_blk = 0; 4622 int ret; 4623 4624 ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc, 4625 &err_blk); 4626 4627 if (ret == -EIO) 4628 ext4_error_inode_block(inode, err_blk, EIO, 4629 "unable to read itable block"); 4630 4631 return ret; 4632} 4633 4634 4635int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 4636 struct ext4_iloc *iloc) 4637{ 4638 return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL); 4639} 4640 4641static bool ext4_should_enable_dax(struct inode *inode) 4642{ 4643 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4644 4645 if (test_opt2(inode->i_sb, DAX_NEVER)) 4646 return false; 4647 if (!S_ISREG(inode->i_mode)) 4648 return false; 4649 if (ext4_should_journal_data(inode)) 4650 return false; 4651 if (ext4_has_inline_data(inode)) 4652 return false; 4653 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 4654 return false; 4655 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4656 return false; 4657 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4658 return false; 4659 if (test_opt(inode->i_sb, DAX_ALWAYS)) 4660 return true; 4661 4662 return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 4663} 4664 4665void ext4_set_inode_flags(struct inode *inode, bool init) 4666{ 4667 unsigned int flags = EXT4_I(inode)->i_flags; 4668 unsigned int new_fl = 0; 4669 4670 WARN_ON_ONCE(IS_DAX(inode) && init); 4671 4672 if (flags & EXT4_SYNC_FL) 4673 new_fl |= S_SYNC; 4674 if (flags & EXT4_APPEND_FL) 4675 new_fl |= S_APPEND; 4676 if (flags & EXT4_IMMUTABLE_FL) 4677 new_fl |= S_IMMUTABLE; 4678 if (flags & EXT4_NOATIME_FL) 4679 new_fl |= S_NOATIME; 4680 if (flags & EXT4_DIRSYNC_FL) 4681 new_fl |= S_DIRSYNC; 4682 4683 /* Because of the way inode_set_flags() works we must preserve S_DAX 4684 * here if already set. */ 4685 new_fl |= (inode->i_flags & S_DAX); 4686 if (init && ext4_should_enable_dax(inode)) 4687 new_fl |= S_DAX; 4688 4689 if (flags & EXT4_ENCRYPT_FL) 4690 new_fl |= S_ENCRYPTED; 4691 if (flags & EXT4_CASEFOLD_FL) 4692 new_fl |= S_CASEFOLD; 4693 if (flags & EXT4_VERITY_FL) 4694 new_fl |= S_VERITY; 4695 inode_set_flags(inode, new_fl, 4696 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4697 S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4698} 4699 4700static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 4701 struct ext4_inode_info *ei) 4702{ 4703 blkcnt_t i_blocks ; 4704 struct inode *inode = &(ei->vfs_inode); 4705 struct super_block *sb = inode->i_sb; 4706 4707 if (ext4_has_feature_huge_file(sb)) { 4708 /* we are using combined 48 bit field */ 4709 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 4710 le32_to_cpu(raw_inode->i_blocks_lo); 4711 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 4712 /* i_blocks represent file system block size */ 4713 return i_blocks << (inode->i_blkbits - 9); 4714 } else { 4715 return i_blocks; 4716 } 4717 } else { 4718 return le32_to_cpu(raw_inode->i_blocks_lo); 4719 } 4720} 4721 4722static inline int ext4_iget_extra_inode(struct inode *inode, 4723 struct ext4_inode *raw_inode, 4724 struct ext4_inode_info *ei) 4725{ 4726 __le32 *magic = (void *)raw_inode + 4727 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4728 4729 if (EXT4_INODE_HAS_XATTR_SPACE(inode) && 4730 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4731 int err; 4732 4733 ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4734 err = ext4_find_inline_data_nolock(inode); 4735 if (!err && ext4_has_inline_data(inode)) 4736 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 4737 return err; 4738 } else 4739 EXT4_I(inode)->i_inline_off = 0; 4740 return 0; 4741} 4742 4743int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4744{ 4745 if (!ext4_has_feature_project(inode->i_sb)) 4746 return -EOPNOTSUPP; 4747 *projid = EXT4_I(inode)->i_projid; 4748 return 0; 4749} 4750 4751/* 4752 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4753 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4754 * set. 4755 */ 4756static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4757{ 4758 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4759 inode_set_iversion_raw(inode, val); 4760 else 4761 inode_set_iversion_queried(inode, val); 4762} 4763 4764static const char *check_igot_inode(struct inode *inode, ext4_iget_flags flags) 4765 4766{ 4767 if (flags & EXT4_IGET_EA_INODE) { 4768 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4769 return "missing EA_INODE flag"; 4770 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 4771 EXT4_I(inode)->i_file_acl) 4772 return "ea_inode with extended attributes"; 4773 } else { 4774 if ((EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4775 return "unexpected EA_INODE flag"; 4776 } 4777 if (is_bad_inode(inode) && !(flags & EXT4_IGET_BAD)) 4778 return "unexpected bad inode w/o EXT4_IGET_BAD"; 4779 return NULL; 4780} 4781 4782struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 4783 ext4_iget_flags flags, const char *function, 4784 unsigned int line) 4785{ 4786 struct ext4_iloc iloc; 4787 struct ext4_inode *raw_inode; 4788 struct ext4_inode_info *ei; 4789 struct inode *inode; 4790 const char *err_str; 4791 journal_t *journal = EXT4_SB(sb)->s_journal; 4792 long ret; 4793 loff_t size; 4794 int block; 4795 uid_t i_uid; 4796 gid_t i_gid; 4797 projid_t i_projid; 4798 4799 if ((!(flags & EXT4_IGET_SPECIAL) && 4800 (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 4801 (ino < EXT4_ROOT_INO) || 4802 (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 4803 if (flags & EXT4_IGET_HANDLE) 4804 return ERR_PTR(-ESTALE); 4805 __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 4806 "inode #%lu: comm %s: iget: illegal inode #", 4807 ino, current->comm); 4808 return ERR_PTR(-EFSCORRUPTED); 4809 } 4810 4811 inode = iget_locked(sb, ino); 4812 if (!inode) 4813 return ERR_PTR(-ENOMEM); 4814 if (!(inode->i_state & I_NEW)) { 4815 if ((err_str = check_igot_inode(inode, flags)) != NULL) { 4816 ext4_error_inode(inode, function, line, 0, err_str); 4817 iput(inode); 4818 return ERR_PTR(-EFSCORRUPTED); 4819 } 4820 return inode; 4821 } 4822 4823 ei = EXT4_I(inode); 4824 iloc.bh = NULL; 4825 4826 ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 4827 if (ret < 0) 4828 goto bad_inode; 4829 raw_inode = ext4_raw_inode(&iloc); 4830 4831 if ((flags & EXT4_IGET_HANDLE) && 4832 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 4833 ret = -ESTALE; 4834 goto bad_inode; 4835 } 4836 4837 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4838 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4839 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4840 EXT4_INODE_SIZE(inode->i_sb) || 4841 (ei->i_extra_isize & 3)) { 4842 ext4_error_inode(inode, function, line, 0, 4843 "iget: bad extra_isize %u " 4844 "(inode size %u)", 4845 ei->i_extra_isize, 4846 EXT4_INODE_SIZE(inode->i_sb)); 4847 ret = -EFSCORRUPTED; 4848 goto bad_inode; 4849 } 4850 } else 4851 ei->i_extra_isize = 0; 4852 4853 /* Precompute checksum seed for inode metadata */ 4854 if (ext4_has_metadata_csum(sb)) { 4855 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4856 __u32 csum; 4857 __le32 inum = cpu_to_le32(inode->i_ino); 4858 __le32 gen = raw_inode->i_generation; 4859 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4860 sizeof(inum)); 4861 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4862 sizeof(gen)); 4863 } 4864 4865 if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 4866 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 4867 (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 4868 ext4_error_inode_err(inode, function, line, 0, 4869 EFSBADCRC, "iget: checksum invalid"); 4870 ret = -EFSBADCRC; 4871 goto bad_inode; 4872 } 4873 4874 inode->i_mode = le16_to_cpu(raw_inode->i_mode); 4875 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 4876 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4877 if (ext4_has_feature_project(sb) && 4878 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4879 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4880 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4881 else 4882 i_projid = EXT4_DEF_PROJID; 4883 4884 if (!(test_opt(inode->i_sb, NO_UID32))) { 4885 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 4886 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4887 } 4888 i_uid_write(inode, i_uid); 4889 i_gid_write(inode, i_gid); 4890 ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4891 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4892 4893 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 4894 ei->i_inline_off = 0; 4895 ei->i_dir_start_lookup = 0; 4896 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4897 /* We now have enough fields to check if the inode was active or not. 4898 * This is needed because nfsd might try to access dead inodes 4899 * the test is that same one that e2fsck uses 4900 * NeilBrown 1999oct15 4901 */ 4902 if (inode->i_nlink == 0) { 4903 if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL || 4904 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4905 ino != EXT4_BOOT_LOADER_INO) { 4906 /* this inode is deleted or unallocated */ 4907 if (flags & EXT4_IGET_SPECIAL) { 4908 ext4_error_inode(inode, function, line, 0, 4909 "iget: special inode unallocated"); 4910 ret = -EFSCORRUPTED; 4911 } else 4912 ret = -ESTALE; 4913 goto bad_inode; 4914 } 4915 /* The only unlinked inodes we let through here have 4916 * valid i_mode and are being read by the orphan 4917 * recovery code: that's fine, we're about to complete 4918 * the process of deleting those. 4919 * OR it is the EXT4_BOOT_LOADER_INO which is 4920 * not initialized on a new filesystem. */ 4921 } 4922 ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4923 ext4_set_inode_flags(inode, true); 4924 inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 4925 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4926 if (ext4_has_feature_64bit(sb)) 4927 ei->i_file_acl |= 4928 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4929 inode->i_size = ext4_isize(sb, raw_inode); 4930 if ((size = i_size_read(inode)) < 0) { 4931 ext4_error_inode(inode, function, line, 0, 4932 "iget: bad i_size value: %lld", size); 4933 ret = -EFSCORRUPTED; 4934 goto bad_inode; 4935 } 4936 /* 4937 * If dir_index is not enabled but there's dir with INDEX flag set, 4938 * we'd normally treat htree data as empty space. But with metadata 4939 * checksumming that corrupts checksums so forbid that. 4940 */ 4941 if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 4942 ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 4943 ext4_error_inode(inode, function, line, 0, 4944 "iget: Dir with htree data on filesystem without dir_index feature."); 4945 ret = -EFSCORRUPTED; 4946 goto bad_inode; 4947 } 4948 ei->i_disksize = inode->i_size; 4949#ifdef CONFIG_QUOTA 4950 ei->i_reserved_quota = 0; 4951#endif 4952 inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4953 ei->i_block_group = iloc.block_group; 4954 ei->i_last_alloc_group = ~0; 4955 /* 4956 * NOTE! The in-memory inode i_data array is in little-endian order 4957 * even on big-endian machines: we do NOT byteswap the block numbers! 4958 */ 4959 for (block = 0; block < EXT4_N_BLOCKS; block++) 4960 ei->i_data[block] = raw_inode->i_block[block]; 4961 INIT_LIST_HEAD(&ei->i_orphan); 4962 ext4_fc_init_inode(&ei->vfs_inode); 4963 4964 /* 4965 * Set transaction id's of transactions that have to be committed 4966 * to finish f[data]sync. We set them to currently running transaction 4967 * as we cannot be sure that the inode or some of its metadata isn't 4968 * part of the transaction - the inode could have been reclaimed and 4969 * now it is reread from disk. 4970 */ 4971 if (journal) { 4972 transaction_t *transaction; 4973 tid_t tid; 4974 4975 read_lock(&journal->j_state_lock); 4976 if (journal->j_running_transaction) 4977 transaction = journal->j_running_transaction; 4978 else 4979 transaction = journal->j_committing_transaction; 4980 if (transaction) 4981 tid = transaction->t_tid; 4982 else 4983 tid = journal->j_commit_sequence; 4984 read_unlock(&journal->j_state_lock); 4985 ei->i_sync_tid = tid; 4986 ei->i_datasync_tid = tid; 4987 } 4988 4989 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4990 if (ei->i_extra_isize == 0) { 4991 /* The extra space is currently unused. Use it. */ 4992 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4993 ei->i_extra_isize = sizeof(struct ext4_inode) - 4994 EXT4_GOOD_OLD_INODE_SIZE; 4995 } else { 4996 ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4997 if (ret) 4998 goto bad_inode; 4999 } 5000 } 5001 5002 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 5003 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 5004 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 5005 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 5006 5007 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 5008 u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 5009 5010 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 5011 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 5012 ivers |= 5013 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 5014 } 5015 ext4_inode_set_iversion_queried(inode, ivers); 5016 } 5017 5018 ret = 0; 5019 if (ei->i_file_acl && 5020 !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 5021 ext4_error_inode(inode, function, line, 0, 5022 "iget: bad extended attribute block %llu", 5023 ei->i_file_acl); 5024 ret = -EFSCORRUPTED; 5025 goto bad_inode; 5026 } else if (!ext4_has_inline_data(inode)) { 5027 /* validate the block references in the inode */ 5028 if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 5029 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 5030 (S_ISLNK(inode->i_mode) && 5031 !ext4_inode_is_fast_symlink(inode)))) { 5032 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5033 ret = ext4_ext_check_inode(inode); 5034 else 5035 ret = ext4_ind_check_inode(inode); 5036 } 5037 } 5038 if (ret) 5039 goto bad_inode; 5040 5041 if (S_ISREG(inode->i_mode)) { 5042 inode->i_op = &ext4_file_inode_operations; 5043 inode->i_fop = &ext4_file_operations; 5044 ext4_set_aops(inode); 5045 } else if (S_ISDIR(inode->i_mode)) { 5046 inode->i_op = &ext4_dir_inode_operations; 5047 inode->i_fop = &ext4_dir_operations; 5048 } else if (S_ISLNK(inode->i_mode)) { 5049 /* VFS does not allow setting these so must be corruption */ 5050 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 5051 ext4_error_inode(inode, function, line, 0, 5052 "iget: immutable or append flags " 5053 "not allowed on symlinks"); 5054 ret = -EFSCORRUPTED; 5055 goto bad_inode; 5056 } 5057 if (IS_ENCRYPTED(inode)) { 5058 inode->i_op = &ext4_encrypted_symlink_inode_operations; 5059 ext4_set_aops(inode); 5060 } else if (ext4_inode_is_fast_symlink(inode)) { 5061 inode->i_link = (char *)ei->i_data; 5062 inode->i_op = &ext4_fast_symlink_inode_operations; 5063 nd_terminate_link(ei->i_data, inode->i_size, 5064 sizeof(ei->i_data) - 1); 5065 } else { 5066 inode->i_op = &ext4_symlink_inode_operations; 5067 ext4_set_aops(inode); 5068 } 5069 inode_nohighmem(inode); 5070 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 5071 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 5072 inode->i_op = &ext4_special_inode_operations; 5073 if (raw_inode->i_block[0]) 5074 init_special_inode(inode, inode->i_mode, 5075 old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 5076 else 5077 init_special_inode(inode, inode->i_mode, 5078 new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 5079 } else if (ino == EXT4_BOOT_LOADER_INO) { 5080 make_bad_inode(inode); 5081 } else { 5082 ret = -EFSCORRUPTED; 5083 ext4_error_inode(inode, function, line, 0, 5084 "iget: bogus i_mode (%o)", inode->i_mode); 5085 goto bad_inode; 5086 } 5087 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 5088 ext4_error_inode(inode, function, line, 0, 5089 "casefold flag without casefold feature"); 5090 if ((err_str = check_igot_inode(inode, flags)) != NULL) { 5091 ext4_error_inode(inode, function, line, 0, err_str); 5092 ret = -EFSCORRUPTED; 5093 goto bad_inode; 5094 } 5095 5096 brelse(iloc.bh); 5097 unlock_new_inode(inode); 5098 return inode; 5099 5100bad_inode: 5101 brelse(iloc.bh); 5102 iget_failed(inode); 5103 return ERR_PTR(ret); 5104} 5105 5106static void __ext4_update_other_inode_time(struct super_block *sb, 5107 unsigned long orig_ino, 5108 unsigned long ino, 5109 struct ext4_inode *raw_inode) 5110{ 5111 struct inode *inode; 5112 5113 inode = find_inode_by_ino_rcu(sb, ino); 5114 if (!inode) 5115 return; 5116 5117 if ((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 5118 I_DIRTY_INODE)) || 5119 ((inode->i_state & I_DIRTY_TIME) == 0)) 5120 return; 5121 5122 spin_lock(&inode->i_lock); 5123 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 5124 I_DIRTY_INODE)) == 0) && 5125 (inode->i_state & I_DIRTY_TIME)) { 5126 struct ext4_inode_info *ei = EXT4_I(inode); 5127 5128 inode->i_state &= ~I_DIRTY_TIME; 5129 spin_unlock(&inode->i_lock); 5130 5131 spin_lock(&ei->i_raw_lock); 5132 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5133 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5134 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5135 ext4_inode_csum_set(inode, raw_inode, ei); 5136 spin_unlock(&ei->i_raw_lock); 5137 trace_ext4_other_inode_update_time(inode, orig_ino); 5138 return; 5139 } 5140 spin_unlock(&inode->i_lock); 5141} 5142 5143/* 5144 * Opportunistically update the other time fields for other inodes in 5145 * the same inode table block. 5146 */ 5147static void ext4_update_other_inodes_time(struct super_block *sb, 5148 unsigned long orig_ino, char *buf) 5149{ 5150 unsigned long ino; 5151 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 5152 int inode_size = EXT4_INODE_SIZE(sb); 5153 5154 /* 5155 * Calculate the first inode in the inode table block. Inode 5156 * numbers are one-based. That is, the first inode in a block 5157 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 5158 */ 5159 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 5160 rcu_read_lock(); 5161 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5162 if (ino == orig_ino) 5163 continue; 5164 __ext4_update_other_inode_time(sb, orig_ino, ino, 5165 (struct ext4_inode *)buf); 5166 } 5167 rcu_read_unlock(); 5168} 5169 5170/* 5171 * Post the struct inode info into an on-disk inode location in the 5172 * buffer-cache. This gobbles the caller's reference to the 5173 * buffer_head in the inode location struct. 5174 * 5175 * The caller must have write access to iloc->bh. 5176 */ 5177static int ext4_do_update_inode(handle_t *handle, 5178 struct inode *inode, 5179 struct ext4_iloc *iloc) 5180{ 5181 struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5182 struct ext4_inode_info *ei = EXT4_I(inode); 5183 struct buffer_head *bh = iloc->bh; 5184 struct super_block *sb = inode->i_sb; 5185 int err; 5186 int need_datasync = 0, set_large_file = 0; 5187 5188 spin_lock(&ei->i_raw_lock); 5189 5190 /* 5191 * For fields not tracked in the in-memory inode, initialise them 5192 * to zero for new inodes. 5193 */ 5194 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5195 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5196 5197 if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) 5198 need_datasync = 1; 5199 if (ei->i_disksize > 0x7fffffffULL) { 5200 if (!ext4_has_feature_large_file(sb) || 5201 EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) 5202 set_large_file = 1; 5203 } 5204 5205 err = ext4_fill_raw_inode(inode, raw_inode); 5206 spin_unlock(&ei->i_raw_lock); 5207 if (err) { 5208 EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5209 goto out_brelse; 5210 } 5211 5212 if (inode->i_sb->s_flags & SB_LAZYTIME) 5213 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5214 bh->b_data); 5215 5216 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 5217 err = ext4_handle_dirty_metadata(handle, NULL, bh); 5218 if (err) 5219 goto out_error; 5220 ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5221 if (set_large_file) { 5222 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5223 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5224 if (err) 5225 goto out_error; 5226 lock_buffer(EXT4_SB(sb)->s_sbh); 5227 ext4_set_feature_large_file(sb); 5228 ext4_superblock_csum_set(sb); 5229 unlock_buffer(EXT4_SB(sb)->s_sbh); 5230 ext4_handle_sync(handle); 5231 err = ext4_handle_dirty_metadata(handle, NULL, 5232 EXT4_SB(sb)->s_sbh); 5233 } 5234 ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5235out_error: 5236 ext4_std_error(inode->i_sb, err); 5237out_brelse: 5238 brelse(bh); 5239 return err; 5240} 5241 5242/* 5243 * ext4_write_inode() 5244 * 5245 * We are called from a few places: 5246 * 5247 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5248 * Here, there will be no transaction running. We wait for any running 5249 * transaction to commit. 5250 * 5251 * - Within flush work (sys_sync(), kupdate and such). 5252 * We wait on commit, if told to. 5253 * 5254 * - Within iput_final() -> write_inode_now() 5255 * We wait on commit, if told to. 5256 * 5257 * In all cases it is actually safe for us to return without doing anything, 5258 * because the inode has been copied into a raw inode buffer in 5259 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 5260 * writeback. 5261 * 5262 * Note that we are absolutely dependent upon all inode dirtiers doing the 5263 * right thing: they *must* call mark_inode_dirty() after dirtying info in 5264 * which we are interested. 5265 * 5266 * It would be a bug for them to not do this. The code: 5267 * 5268 * mark_inode_dirty(inode) 5269 * stuff(); 5270 * inode->i_size = expr; 5271 * 5272 * is in error because write_inode() could occur while `stuff()' is running, 5273 * and the new i_size will be lost. Plus the inode will no longer be on the 5274 * superblock's dirty inode list. 5275 */ 5276int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5277{ 5278 int err; 5279 5280 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 5281 sb_rdonly(inode->i_sb)) 5282 return 0; 5283 5284 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 5285 return -EIO; 5286 5287 if (EXT4_SB(inode->i_sb)->s_journal) { 5288 if (ext4_journal_current_handle()) { 5289 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5290 dump_stack(); 5291 return -EIO; 5292 } 5293 5294 /* 5295 * No need to force transaction in WB_SYNC_NONE mode. Also 5296 * ext4_sync_fs() will force the commit after everything is 5297 * written. 5298 */ 5299 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5300 return 0; 5301 5302 err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 5303 EXT4_I(inode)->i_sync_tid); 5304 } else { 5305 struct ext4_iloc iloc; 5306 5307 err = __ext4_get_inode_loc_noinmem(inode, &iloc); 5308 if (err) 5309 return err; 5310 /* 5311 * sync(2) will flush the whole buffer cache. No need to do 5312 * it here separately for each inode. 5313 */ 5314 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5315 sync_dirty_buffer(iloc.bh); 5316 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 5317 ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5318 "IO error syncing inode"); 5319 err = -EIO; 5320 } 5321 brelse(iloc.bh); 5322 } 5323 return err; 5324} 5325 5326/* 5327 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 5328 * buffers that are attached to a page stradding i_size and are undergoing 5329 * commit. In that case we have to wait for commit to finish and try again. 5330 */ 5331static void ext4_wait_for_tail_page_commit(struct inode *inode) 5332{ 5333 struct page *page; 5334 unsigned offset; 5335 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 5336 tid_t commit_tid = 0; 5337 int ret; 5338 5339 offset = inode->i_size & (PAGE_SIZE - 1); 5340 /* 5341 * If the page is fully truncated, we don't need to wait for any commit 5342 * (and we even should not as __ext4_journalled_invalidatepage() may 5343 * strip all buffers from the page but keep the page dirty which can then 5344 * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5345 * buffers). Also we don't need to wait for any commit if all buffers in 5346 * the page remain valid. This is most beneficial for the common case of 5347 * blocksize == PAGESIZE. 5348 */ 5349 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 5350 return; 5351 while (1) { 5352 page = find_lock_page(inode->i_mapping, 5353 inode->i_size >> PAGE_SHIFT); 5354 if (!page) 5355 return; 5356 ret = __ext4_journalled_invalidatepage(page, offset, 5357 PAGE_SIZE - offset); 5358 unlock_page(page); 5359 put_page(page); 5360 if (ret != -EBUSY) 5361 return; 5362 commit_tid = 0; 5363 read_lock(&journal->j_state_lock); 5364 if (journal->j_committing_transaction) 5365 commit_tid = journal->j_committing_transaction->t_tid; 5366 read_unlock(&journal->j_state_lock); 5367 if (commit_tid) 5368 jbd2_log_wait_commit(journal, commit_tid); 5369 } 5370} 5371 5372/* 5373 * ext4_setattr() 5374 * 5375 * Called from notify_change. 5376 * 5377 * We want to trap VFS attempts to truncate the file as soon as 5378 * possible. In particular, we want to make sure that when the VFS 5379 * shrinks i_size, we put the inode on the orphan list and modify 5380 * i_disksize immediately, so that during the subsequent flushing of 5381 * dirty pages and freeing of disk blocks, we can guarantee that any 5382 * commit will leave the blocks being flushed in an unused state on 5383 * disk. (On recovery, the inode will get truncated and the blocks will 5384 * be freed, so we have a strong guarantee that no future commit will 5385 * leave these blocks visible to the user.) 5386 * 5387 * Another thing we have to assure is that if we are in ordered mode 5388 * and inode is still attached to the committing transaction, we must 5389 * we start writeout of all the dirty pages which are being truncated. 5390 * This way we are sure that all the data written in the previous 5391 * transaction are already on disk (truncate waits for pages under 5392 * writeback). 5393 * 5394 * Called with inode->i_mutex down. 5395 */ 5396int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5397{ 5398 struct inode *inode = d_inode(dentry); 5399 int error, rc = 0; 5400 int orphan = 0; 5401 const unsigned int ia_valid = attr->ia_valid; 5402 5403 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 5404 return -EIO; 5405 5406 if (unlikely(IS_IMMUTABLE(inode))) 5407 return -EPERM; 5408 5409 if (unlikely(IS_APPEND(inode) && 5410 (ia_valid & (ATTR_MODE | ATTR_UID | 5411 ATTR_GID | ATTR_TIMES_SET)))) 5412 return -EPERM; 5413 5414 error = setattr_prepare(dentry, attr); 5415 if (error) 5416 return error; 5417 5418 error = fscrypt_prepare_setattr(dentry, attr); 5419 if (error) 5420 return error; 5421 5422 error = fsverity_prepare_setattr(dentry, attr); 5423 if (error) 5424 return error; 5425 5426 if (is_quota_modification(inode, attr)) { 5427 error = dquot_initialize(inode); 5428 if (error) 5429 return error; 5430 } 5431 5432 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 5433 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5434 handle_t *handle; 5435 5436 /* (user+group)*(old+new) structure, inode write (sb, 5437 * inode block, ? - but truncate inode update has it) */ 5438 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 5439 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5440 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5441 if (IS_ERR(handle)) { 5442 error = PTR_ERR(handle); 5443 goto err_out; 5444 } 5445 5446 /* dquot_transfer() calls back ext4_get_inode_usage() which 5447 * counts xattr inode references. 5448 */ 5449 down_read(&EXT4_I(inode)->xattr_sem); 5450 error = dquot_transfer(inode, attr); 5451 up_read(&EXT4_I(inode)->xattr_sem); 5452 5453 if (error) { 5454 ext4_journal_stop(handle); 5455 return error; 5456 } 5457 /* Update corresponding info in inode so that everything is in 5458 * one transaction */ 5459 if (attr->ia_valid & ATTR_UID) 5460 inode->i_uid = attr->ia_uid; 5461 if (attr->ia_valid & ATTR_GID) 5462 inode->i_gid = attr->ia_gid; 5463 error = ext4_mark_inode_dirty(handle, inode); 5464 ext4_journal_stop(handle); 5465 if (unlikely(error)) { 5466 return error; 5467 } 5468 } 5469 5470 if (attr->ia_valid & ATTR_SIZE) { 5471 handle_t *handle; 5472 loff_t oldsize = inode->i_size; 5473 loff_t old_disksize; 5474 int shrink = (attr->ia_size < inode->i_size); 5475 5476 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5477 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5478 5479 if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5480 return -EFBIG; 5481 } 5482 } 5483 if (!S_ISREG(inode->i_mode)) { 5484 return -EINVAL; 5485 } 5486 5487 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5488 inode_inc_iversion(inode); 5489 5490 if (shrink) { 5491 if (ext4_should_order_data(inode)) { 5492 error = ext4_begin_ordered_truncate(inode, 5493 attr->ia_size); 5494 if (error) 5495 goto err_out; 5496 } 5497 /* 5498 * Blocks are going to be removed from the inode. Wait 5499 * for dio in flight. 5500 */ 5501 inode_dio_wait(inode); 5502 } 5503 5504 down_write(&EXT4_I(inode)->i_mmap_sem); 5505 5506 rc = ext4_break_layouts(inode); 5507 if (rc) { 5508 up_write(&EXT4_I(inode)->i_mmap_sem); 5509 goto err_out; 5510 } 5511 5512 if (attr->ia_size != inode->i_size) { 5513 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5514 if (IS_ERR(handle)) { 5515 error = PTR_ERR(handle); 5516 goto out_mmap_sem; 5517 } 5518 if (ext4_handle_valid(handle) && shrink) { 5519 error = ext4_orphan_add(handle, inode); 5520 orphan = 1; 5521 } 5522 /* 5523 * Update c/mtime on truncate up, ext4_truncate() will 5524 * update c/mtime in shrink case below 5525 */ 5526 if (!shrink) { 5527 inode->i_mtime = current_time(inode); 5528 inode->i_ctime = inode->i_mtime; 5529 } 5530 5531 if (shrink) 5532 ext4_fc_track_range(handle, inode, 5533 (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5534 inode->i_sb->s_blocksize_bits, 5535 EXT_MAX_BLOCKS - 1); 5536 else 5537 ext4_fc_track_range( 5538 handle, inode, 5539 (oldsize > 0 ? oldsize - 1 : oldsize) >> 5540 inode->i_sb->s_blocksize_bits, 5541 (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5542 inode->i_sb->s_blocksize_bits); 5543 5544 down_write(&EXT4_I(inode)->i_data_sem); 5545 old_disksize = EXT4_I(inode)->i_disksize; 5546 EXT4_I(inode)->i_disksize = attr->ia_size; 5547 rc = ext4_mark_inode_dirty(handle, inode); 5548 if (!error) 5549 error = rc; 5550 /* 5551 * We have to update i_size under i_data_sem together 5552 * with i_disksize to avoid races with writeback code 5553 * running ext4_wb_update_i_disksize(). 5554 */ 5555 if (!error) 5556 i_size_write(inode, attr->ia_size); 5557 else 5558 EXT4_I(inode)->i_disksize = old_disksize; 5559 up_write(&EXT4_I(inode)->i_data_sem); 5560 ext4_journal_stop(handle); 5561 if (error) 5562 goto out_mmap_sem; 5563 if (!shrink) { 5564 pagecache_isize_extended(inode, oldsize, 5565 inode->i_size); 5566 } else if (ext4_should_journal_data(inode)) { 5567 ext4_wait_for_tail_page_commit(inode); 5568 } 5569 } 5570 5571 /* 5572 * Truncate pagecache after we've waited for commit 5573 * in data=journal mode to make pages freeable. 5574 */ 5575 truncate_pagecache(inode, inode->i_size); 5576 /* 5577 * Call ext4_truncate() even if i_size didn't change to 5578 * truncate possible preallocated blocks. 5579 */ 5580 if (attr->ia_size <= oldsize) { 5581 rc = ext4_truncate(inode); 5582 if (rc) 5583 error = rc; 5584 } 5585out_mmap_sem: 5586 up_write(&EXT4_I(inode)->i_mmap_sem); 5587 } 5588 5589 if (!error) { 5590 setattr_copy(inode, attr); 5591 mark_inode_dirty(inode); 5592 } 5593 5594 /* 5595 * If the call to ext4_truncate failed to get a transaction handle at 5596 * all, we need to clean up the in-core orphan list manually. 5597 */ 5598 if (orphan && inode->i_nlink) 5599 ext4_orphan_del(NULL, inode); 5600 5601 if (!error && (ia_valid & ATTR_MODE)) 5602 rc = posix_acl_chmod(inode, inode->i_mode); 5603 5604err_out: 5605 if (error) 5606 ext4_std_error(inode->i_sb, error); 5607 if (!error) 5608 error = rc; 5609 return error; 5610} 5611 5612int ext4_getattr(const struct path *path, struct kstat *stat, 5613 u32 request_mask, unsigned int query_flags) 5614{ 5615 struct inode *inode = d_inode(path->dentry); 5616 struct ext4_inode *raw_inode; 5617 struct ext4_inode_info *ei = EXT4_I(inode); 5618 unsigned int flags; 5619 5620 if ((request_mask & STATX_BTIME) && 5621 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 5622 stat->result_mask |= STATX_BTIME; 5623 stat->btime.tv_sec = ei->i_crtime.tv_sec; 5624 stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 5625 } 5626 5627 flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 5628 if (flags & EXT4_APPEND_FL) 5629 stat->attributes |= STATX_ATTR_APPEND; 5630 if (flags & EXT4_COMPR_FL) 5631 stat->attributes |= STATX_ATTR_COMPRESSED; 5632 if (flags & EXT4_ENCRYPT_FL) 5633 stat->attributes |= STATX_ATTR_ENCRYPTED; 5634 if (flags & EXT4_IMMUTABLE_FL) 5635 stat->attributes |= STATX_ATTR_IMMUTABLE; 5636 if (flags & EXT4_NODUMP_FL) 5637 stat->attributes |= STATX_ATTR_NODUMP; 5638 if (flags & EXT4_VERITY_FL) 5639 stat->attributes |= STATX_ATTR_VERITY; 5640 5641 stat->attributes_mask |= (STATX_ATTR_APPEND | 5642 STATX_ATTR_COMPRESSED | 5643 STATX_ATTR_ENCRYPTED | 5644 STATX_ATTR_IMMUTABLE | 5645 STATX_ATTR_NODUMP | 5646 STATX_ATTR_VERITY); 5647 5648 generic_fillattr(inode, stat); 5649 return 0; 5650} 5651 5652int ext4_file_getattr(const struct path *path, struct kstat *stat, 5653 u32 request_mask, unsigned int query_flags) 5654{ 5655 struct inode *inode = d_inode(path->dentry); 5656 u64 delalloc_blocks; 5657 5658 ext4_getattr(path, stat, request_mask, query_flags); 5659 5660 /* 5661 * If there is inline data in the inode, the inode will normally not 5662 * have data blocks allocated (it may have an external xattr block). 5663 * Report at least one sector for such files, so tools like tar, rsync, 5664 * others don't incorrectly think the file is completely sparse. 5665 */ 5666 if (unlikely(ext4_has_inline_data(inode))) 5667 stat->blocks += (stat->size + 511) >> 9; 5668 5669 /* 5670 * We can't update i_blocks if the block allocation is delayed 5671 * otherwise in the case of system crash before the real block 5672 * allocation is done, we will have i_blocks inconsistent with 5673 * on-disk file blocks. 5674 * We always keep i_blocks updated together with real 5675 * allocation. But to not confuse with user, stat 5676 * will return the blocks that include the delayed allocation 5677 * blocks for this file. 5678 */ 5679 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 5680 EXT4_I(inode)->i_reserved_data_blocks); 5681 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 5682 return 0; 5683} 5684 5685static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5686 int pextents) 5687{ 5688 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5689 return ext4_ind_trans_blocks(inode, lblocks); 5690 return ext4_ext_index_trans_blocks(inode, pextents); 5691} 5692 5693/* 5694 * Account for index blocks, block groups bitmaps and block group 5695 * descriptor blocks if modify datablocks and index blocks 5696 * worse case, the indexs blocks spread over different block groups 5697 * 5698 * If datablocks are discontiguous, they are possible to spread over 5699 * different block groups too. If they are contiguous, with flexbg, 5700 * they could still across block group boundary. 5701 * 5702 * Also account for superblock, inode, quota and xattr blocks 5703 */ 5704static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5705 int pextents) 5706{ 5707 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 5708 int gdpblocks; 5709 int idxblocks; 5710 int ret = 0; 5711 5712 /* 5713 * How many index blocks need to touch to map @lblocks logical blocks 5714 * to @pextents physical extents? 5715 */ 5716 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5717 5718 ret = idxblocks; 5719 5720 /* 5721 * Now let's see how many group bitmaps and group descriptors need 5722 * to account 5723 */ 5724 groups = idxblocks + pextents; 5725 gdpblocks = groups; 5726 if (groups > ngroups) 5727 groups = ngroups; 5728 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5729 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5730 5731 /* bitmaps and block group descriptor blocks */ 5732 ret += groups + gdpblocks; 5733 5734 /* Blocks for super block, inode, quota and xattr blocks */ 5735 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5736 5737 return ret; 5738} 5739 5740/* 5741 * Calculate the total number of credits to reserve to fit 5742 * the modification of a single pages into a single transaction, 5743 * which may include multiple chunks of block allocations. 5744 * 5745 * This could be called via ext4_write_begin() 5746 * 5747 * We need to consider the worse case, when 5748 * one new block per extent. 5749 */ 5750int ext4_writepage_trans_blocks(struct inode *inode) 5751{ 5752 int bpp = ext4_journal_blocks_per_page(inode); 5753 int ret; 5754 5755 ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5756 5757 /* Account for data blocks for journalled mode */ 5758 if (ext4_should_journal_data(inode)) 5759 ret += bpp; 5760 return ret; 5761} 5762 5763/* 5764 * Calculate the journal credits for a chunk of data modification. 5765 * 5766 * This is called from DIO, fallocate or whoever calling 5767 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5768 * 5769 * journal buffers for data blocks are not included here, as DIO 5770 * and fallocate do no need to journal data buffers. 5771 */ 5772int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5773{ 5774 return ext4_meta_trans_blocks(inode, nrblocks, 1); 5775} 5776 5777/* 5778 * The caller must have previously called ext4_reserve_inode_write(). 5779 * Give this, we know that the caller already has write access to iloc->bh. 5780 */ 5781int ext4_mark_iloc_dirty(handle_t *handle, 5782 struct inode *inode, struct ext4_iloc *iloc) 5783{ 5784 int err = 0; 5785 5786 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5787 put_bh(iloc->bh); 5788 return -EIO; 5789 } 5790 ext4_fc_track_inode(handle, inode); 5791 5792 /* 5793 * ea_inodes are using i_version for storing reference count, don't 5794 * mess with it 5795 */ 5796 if (IS_I_VERSION(inode) && 5797 !(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 5798 inode_inc_iversion(inode); 5799 5800 /* the do_update_inode consumes one bh->b_count */ 5801 get_bh(iloc->bh); 5802 5803 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5804 err = ext4_do_update_inode(handle, inode, iloc); 5805 put_bh(iloc->bh); 5806 return err; 5807} 5808 5809/* 5810 * On success, We end up with an outstanding reference count against 5811 * iloc->bh. This _must_ be cleaned up later. 5812 */ 5813 5814int 5815ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5816 struct ext4_iloc *iloc) 5817{ 5818 int err; 5819 5820 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 5821 return -EIO; 5822 5823 err = ext4_get_inode_loc(inode, iloc); 5824 if (!err) { 5825 BUFFER_TRACE(iloc->bh, "get_write_access"); 5826 err = ext4_journal_get_write_access(handle, iloc->bh); 5827 if (err) { 5828 brelse(iloc->bh); 5829 iloc->bh = NULL; 5830 } 5831 } 5832 ext4_std_error(inode->i_sb, err); 5833 return err; 5834} 5835 5836static int __ext4_expand_extra_isize(struct inode *inode, 5837 unsigned int new_extra_isize, 5838 struct ext4_iloc *iloc, 5839 handle_t *handle, int *no_expand) 5840{ 5841 struct ext4_inode *raw_inode; 5842 struct ext4_xattr_ibody_header *header; 5843 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 5844 struct ext4_inode_info *ei = EXT4_I(inode); 5845 int error; 5846 5847 /* this was checked at iget time, but double check for good measure */ 5848 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 5849 (ei->i_extra_isize & 3)) { 5850 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 5851 ei->i_extra_isize, 5852 EXT4_INODE_SIZE(inode->i_sb)); 5853 return -EFSCORRUPTED; 5854 } 5855 if ((new_extra_isize < ei->i_extra_isize) || 5856 (new_extra_isize < 4) || 5857 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 5858 return -EINVAL; /* Should never happen */ 5859 5860 raw_inode = ext4_raw_inode(iloc); 5861 5862 header = IHDR(inode, raw_inode); 5863 5864 /* No extended attributes present */ 5865 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5866 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5867 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5868 EXT4_I(inode)->i_extra_isize, 0, 5869 new_extra_isize - EXT4_I(inode)->i_extra_isize); 5870 EXT4_I(inode)->i_extra_isize = new_extra_isize; 5871 return 0; 5872 } 5873 5874 /* 5875 * We may need to allocate external xattr block so we need quotas 5876 * initialized. Here we can be called with various locks held so we 5877 * cannot affort to initialize quotas ourselves. So just bail. 5878 */ 5879 if (dquot_initialize_needed(inode)) 5880 return -EAGAIN; 5881 5882 /* try to expand with EAs present */ 5883 error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5884 raw_inode, handle); 5885 if (error) { 5886 /* 5887 * Inode size expansion failed; don't try again 5888 */ 5889 *no_expand = 1; 5890 } 5891 5892 return error; 5893} 5894 5895/* 5896 * Expand an inode by new_extra_isize bytes. 5897 * Returns 0 on success or negative error number on failure. 5898 */ 5899static int ext4_try_to_expand_extra_isize(struct inode *inode, 5900 unsigned int new_extra_isize, 5901 struct ext4_iloc iloc, 5902 handle_t *handle) 5903{ 5904 int no_expand; 5905 int error; 5906 5907 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5908 return -EOVERFLOW; 5909 5910 /* 5911 * In nojournal mode, we can immediately attempt to expand 5912 * the inode. When journaled, we first need to obtain extra 5913 * buffer credits since we may write into the EA block 5914 * with this same handle. If journal_extend fails, then it will 5915 * only result in a minor loss of functionality for that inode. 5916 * If this is felt to be critical, then e2fsck should be run to 5917 * force a large enough s_min_extra_isize. 5918 */ 5919 if (ext4_journal_extend(handle, 5920 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5921 return -ENOSPC; 5922 5923 if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5924 return -EBUSY; 5925 5926 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5927 handle, &no_expand); 5928 ext4_write_unlock_xattr(inode, &no_expand); 5929 5930 return error; 5931} 5932 5933int ext4_expand_extra_isize(struct inode *inode, 5934 unsigned int new_extra_isize, 5935 struct ext4_iloc *iloc) 5936{ 5937 handle_t *handle; 5938 int no_expand; 5939 int error, rc; 5940 5941 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5942 brelse(iloc->bh); 5943 return -EOVERFLOW; 5944 } 5945 5946 handle = ext4_journal_start(inode, EXT4_HT_INODE, 5947 EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5948 if (IS_ERR(handle)) { 5949 error = PTR_ERR(handle); 5950 brelse(iloc->bh); 5951 return error; 5952 } 5953 5954 ext4_write_lock_xattr(inode, &no_expand); 5955 5956 BUFFER_TRACE(iloc->bh, "get_write_access"); 5957 error = ext4_journal_get_write_access(handle, iloc->bh); 5958 if (error) { 5959 brelse(iloc->bh); 5960 goto out_unlock; 5961 } 5962 5963 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5964 handle, &no_expand); 5965 5966 rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5967 if (!error) 5968 error = rc; 5969 5970out_unlock: 5971 ext4_write_unlock_xattr(inode, &no_expand); 5972 ext4_journal_stop(handle); 5973 return error; 5974} 5975 5976/* 5977 * What we do here is to mark the in-core inode as clean with respect to inode 5978 * dirtiness (it may still be data-dirty). 5979 * This means that the in-core inode may be reaped by prune_icache 5980 * without having to perform any I/O. This is a very good thing, 5981 * because *any* task may call prune_icache - even ones which 5982 * have a transaction open against a different journal. 5983 * 5984 * Is this cheating? Not really. Sure, we haven't written the 5985 * inode out, but prune_icache isn't a user-visible syncing function. 5986 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5987 * we start and wait on commits. 5988 */ 5989int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 5990 const char *func, unsigned int line) 5991{ 5992 struct ext4_iloc iloc; 5993 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5994 int err; 5995 5996 might_sleep(); 5997 trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5998 err = ext4_reserve_inode_write(handle, inode, &iloc); 5999 if (err) 6000 goto out; 6001 6002 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 6003 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 6004 iloc, handle); 6005 6006 err = ext4_mark_iloc_dirty(handle, inode, &iloc); 6007out: 6008 if (unlikely(err)) 6009 ext4_error_inode_err(inode, func, line, 0, err, 6010 "mark_inode_dirty error"); 6011 return err; 6012} 6013 6014/* 6015 * ext4_dirty_inode() is called from __mark_inode_dirty() 6016 * 6017 * We're really interested in the case where a file is being extended. 6018 * i_size has been changed by generic_commit_write() and we thus need 6019 * to include the updated inode in the current transaction. 6020 * 6021 * Also, dquot_alloc_block() will always dirty the inode when blocks 6022 * are allocated to the file. 6023 * 6024 * If the inode is marked synchronous, we don't honour that here - doing 6025 * so would cause a commit on atime updates, which we don't bother doing. 6026 * We handle synchronous inodes at the highest possible level. 6027 * 6028 * If only the I_DIRTY_TIME flag is set, we can skip everything. If 6029 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need 6030 * to copy into the on-disk inode structure are the timestamp files. 6031 */ 6032void ext4_dirty_inode(struct inode *inode, int flags) 6033{ 6034 handle_t *handle; 6035 6036 if (flags == I_DIRTY_TIME) 6037 return; 6038 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 6039 if (IS_ERR(handle)) 6040 goto out; 6041 6042 ext4_mark_inode_dirty(handle, inode); 6043 6044 ext4_journal_stop(handle); 6045out: 6046 return; 6047} 6048 6049int ext4_change_inode_journal_flag(struct inode *inode, int val) 6050{ 6051 journal_t *journal; 6052 handle_t *handle; 6053 int err; 6054 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 6055 6056 /* 6057 * We have to be very careful here: changing a data block's 6058 * journaling status dynamically is dangerous. If we write a 6059 * data block to the journal, change the status and then delete 6060 * that block, we risk forgetting to revoke the old log record 6061 * from the journal and so a subsequent replay can corrupt data. 6062 * So, first we make sure that the journal is empty and that 6063 * nobody is changing anything. 6064 */ 6065 6066 journal = EXT4_JOURNAL(inode); 6067 if (!journal) 6068 return 0; 6069 if (is_journal_aborted(journal)) 6070 return -EROFS; 6071 6072 /* Wait for all existing dio workers */ 6073 inode_dio_wait(inode); 6074 6075 /* 6076 * Before flushing the journal and switching inode's aops, we have 6077 * to flush all dirty data the inode has. There can be outstanding 6078 * delayed allocations, there can be unwritten extents created by 6079 * fallocate or buffered writes in dioread_nolock mode covered by 6080 * dirty data which can be converted only after flushing the dirty 6081 * data (and journalled aops don't know how to handle these cases). 6082 */ 6083 if (val) { 6084 down_write(&EXT4_I(inode)->i_mmap_sem); 6085 err = filemap_write_and_wait(inode->i_mapping); 6086 if (err < 0) { 6087 up_write(&EXT4_I(inode)->i_mmap_sem); 6088 return err; 6089 } 6090 } 6091 6092 percpu_down_write(&sbi->s_writepages_rwsem); 6093 jbd2_journal_lock_updates(journal); 6094 6095 /* 6096 * OK, there are no updates running now, and all cached data is 6097 * synced to disk. We are now in a completely consistent state 6098 * which doesn't have anything in the journal, and we know that 6099 * no filesystem updates are running, so it is safe to modify 6100 * the inode's in-core data-journaling state flag now. 6101 */ 6102 6103 if (val) 6104 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 6105 else { 6106 err = jbd2_journal_flush(journal); 6107 if (err < 0) { 6108 jbd2_journal_unlock_updates(journal); 6109 percpu_up_write(&sbi->s_writepages_rwsem); 6110 return err; 6111 } 6112 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 6113 } 6114 ext4_set_aops(inode); 6115 6116 jbd2_journal_unlock_updates(journal); 6117 percpu_up_write(&sbi->s_writepages_rwsem); 6118 6119 if (val) 6120 up_write(&EXT4_I(inode)->i_mmap_sem); 6121 6122 /* Finally we can mark the inode as dirty. */ 6123 6124 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 6125 if (IS_ERR(handle)) 6126 return PTR_ERR(handle); 6127 6128 ext4_fc_mark_ineligible(inode->i_sb, 6129 EXT4_FC_REASON_JOURNAL_FLAG_CHANGE); 6130 err = ext4_mark_inode_dirty(handle, inode); 6131 ext4_handle_sync(handle); 6132 ext4_journal_stop(handle); 6133 ext4_std_error(inode->i_sb, err); 6134 6135 return err; 6136} 6137 6138static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 6139{ 6140 return !buffer_mapped(bh); 6141} 6142 6143vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 6144{ 6145 struct vm_area_struct *vma = vmf->vma; 6146 struct page *page = vmf->page; 6147 loff_t size; 6148 unsigned long len; 6149 int err; 6150 vm_fault_t ret; 6151 struct file *file = vma->vm_file; 6152 struct inode *inode = file_inode(file); 6153 struct address_space *mapping = inode->i_mapping; 6154 handle_t *handle; 6155 get_block_t *get_block; 6156 int retries = 0; 6157 6158 if (unlikely(IS_IMMUTABLE(inode))) 6159 return VM_FAULT_SIGBUS; 6160 6161 sb_start_pagefault(inode->i_sb); 6162 file_update_time(vma->vm_file); 6163 6164 down_read(&EXT4_I(inode)->i_mmap_sem); 6165 6166 err = ext4_convert_inline_data(inode); 6167 if (err) 6168 goto out_ret; 6169 6170 /* 6171 * On data journalling we skip straight to the transaction handle: 6172 * there's no delalloc; page truncated will be checked later; the 6173 * early return w/ all buffers mapped (calculates size/len) can't 6174 * be used; and there's no dioread_nolock, so only ext4_get_block. 6175 */ 6176 if (ext4_should_journal_data(inode)) 6177 goto retry_alloc; 6178 6179 /* Delalloc case is easy... */ 6180 if (test_opt(inode->i_sb, DELALLOC) && 6181 !ext4_nonda_switch(inode->i_sb)) { 6182 do { 6183 err = block_page_mkwrite(vma, vmf, 6184 ext4_da_get_block_prep); 6185 } while (err == -ENOSPC && 6186 ext4_should_retry_alloc(inode->i_sb, &retries)); 6187 goto out_ret; 6188 } 6189 6190 lock_page(page); 6191 size = i_size_read(inode); 6192 /* Page got truncated from under us? */ 6193 if (page->mapping != mapping || page_offset(page) > size) { 6194 unlock_page(page); 6195 ret = VM_FAULT_NOPAGE; 6196 goto out; 6197 } 6198 6199 if (page->index == size >> PAGE_SHIFT) 6200 len = size & ~PAGE_MASK; 6201 else 6202 len = PAGE_SIZE; 6203 /* 6204 * Return if we have all the buffers mapped. This avoids the need to do 6205 * journal_start/journal_stop which can block and take a long time 6206 * 6207 * This cannot be done for data journalling, as we have to add the 6208 * inode to the transaction's list to writeprotect pages on commit. 6209 */ 6210 if (page_has_buffers(page)) { 6211 if (!ext4_walk_page_buffers(NULL, page_buffers(page), 6212 0, len, NULL, 6213 ext4_bh_unmapped)) { 6214 /* Wait so that we don't change page under IO */ 6215 wait_for_stable_page(page); 6216 ret = VM_FAULT_LOCKED; 6217 goto out; 6218 } 6219 } 6220 unlock_page(page); 6221 /* OK, we need to fill the hole... */ 6222 if (ext4_should_dioread_nolock(inode)) 6223 get_block = ext4_get_block_unwritten; 6224 else 6225 get_block = ext4_get_block; 6226retry_alloc: 6227 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 6228 ext4_writepage_trans_blocks(inode)); 6229 if (IS_ERR(handle)) { 6230 ret = VM_FAULT_SIGBUS; 6231 goto out; 6232 } 6233 /* 6234 * Data journalling can't use block_page_mkwrite() because it 6235 * will set_buffer_dirty() before do_journal_get_write_access() 6236 * thus might hit warning messages for dirty metadata buffers. 6237 */ 6238 if (!ext4_should_journal_data(inode)) { 6239 err = block_page_mkwrite(vma, vmf, get_block); 6240 } else { 6241 lock_page(page); 6242 size = i_size_read(inode); 6243 /* Page got truncated from under us? */ 6244 if (page->mapping != mapping || page_offset(page) > size) { 6245 ret = VM_FAULT_NOPAGE; 6246 goto out_error; 6247 } 6248 6249 if (page->index == size >> PAGE_SHIFT) 6250 len = size & ~PAGE_MASK; 6251 else 6252 len = PAGE_SIZE; 6253 6254 err = __block_write_begin(page, 0, len, ext4_get_block); 6255 if (!err) { 6256 ret = VM_FAULT_SIGBUS; 6257 if (ext4_walk_page_buffers(handle, page_buffers(page), 6258 0, len, NULL, do_journal_get_write_access)) 6259 goto out_error; 6260 if (ext4_walk_page_buffers(handle, page_buffers(page), 6261 0, len, NULL, write_end_fn)) 6262 goto out_error; 6263 if (ext4_jbd2_inode_add_write(handle, inode, 6264 page_offset(page), len)) 6265 goto out_error; 6266 ext4_set_inode_state(inode, EXT4_STATE_JDATA); 6267 } else { 6268 unlock_page(page); 6269 } 6270 } 6271 ext4_journal_stop(handle); 6272 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 6273 goto retry_alloc; 6274out_ret: 6275 ret = block_page_mkwrite_return(err); 6276out: 6277 up_read(&EXT4_I(inode)->i_mmap_sem); 6278 sb_end_pagefault(inode->i_sb); 6279 return ret; 6280out_error: 6281 unlock_page(page); 6282 ext4_journal_stop(handle); 6283 goto out; 6284} 6285 6286vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 6287{ 6288 struct inode *inode = file_inode(vmf->vma->vm_file); 6289 vm_fault_t ret; 6290 6291 down_read(&EXT4_I(inode)->i_mmap_sem); 6292 ret = filemap_fault(vmf); 6293 up_read(&EXT4_I(inode)->i_mmap_sem); 6294 6295 return ret; 6296} 6297