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