1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2ac27a0ecSDave Kleikamp /* 3617ba13bSMingming Cao * linux/fs/ext4/inode.c 4ac27a0ecSDave Kleikamp * 5ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 6ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 7ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 8ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 9ac27a0ecSDave Kleikamp * 10ac27a0ecSDave Kleikamp * from 11ac27a0ecSDave Kleikamp * 12ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 13ac27a0ecSDave Kleikamp * 14ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 15ac27a0ecSDave Kleikamp * 16ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek 17ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 18ac27a0ecSDave Kleikamp * 19617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 20ac27a0ecSDave Kleikamp */ 21ac27a0ecSDave Kleikamp 22ac27a0ecSDave Kleikamp #include <linux/fs.h> 2314f3db55SChristian Brauner #include <linux/mount.h> 24ac27a0ecSDave Kleikamp #include <linux/time.h> 25ac27a0ecSDave Kleikamp #include <linux/highuid.h> 26ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 27c94c2acfSMatthew Wilcox #include <linux/dax.h> 28ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 29ac27a0ecSDave Kleikamp #include <linux/string.h> 30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 31ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3264769240SAlex Tomas #include <linux/pagevec.h> 33ac27a0ecSDave Kleikamp #include <linux/mpage.h> 34e83c1397SDuane Griffin #include <linux/namei.h> 35ac27a0ecSDave Kleikamp #include <linux/uio.h> 36ac27a0ecSDave Kleikamp #include <linux/bio.h> 374c0425ffSMingming Cao #include <linux/workqueue.h> 38744692dcSJiaying Zhang #include <linux/kernel.h> 396db26ffcSAndrew Morton #include <linux/printk.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 4100a1a053STheodore Ts'o #include <linux/bitops.h> 42364443cbSJan Kara #include <linux/iomap.h> 43ae5e165dSJeff Layton #include <linux/iversion.h> 449bffad1eSTheodore Ts'o 453dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 46ac27a0ecSDave Kleikamp #include "xattr.h" 47ac27a0ecSDave Kleikamp #include "acl.h" 489f125d64STheodore Ts'o #include "truncate.h" 49ac27a0ecSDave Kleikamp 509bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 519bffad1eSTheodore Ts'o 52814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 53814525f4SDarrick J. Wong struct ext4_inode_info *ei) 54814525f4SDarrick J. Wong { 55814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 56814525f4SDarrick J. Wong __u32 csum; 57b47820edSDaeho Jeong __u16 dummy_csum = 0; 58b47820edSDaeho Jeong int offset = offsetof(struct ext4_inode, i_checksum_lo); 59b47820edSDaeho Jeong unsigned int csum_size = sizeof(dummy_csum); 60814525f4SDarrick J. Wong 61b47820edSDaeho Jeong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); 62b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); 63b47820edSDaeho Jeong offset += csum_size; 64b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 65b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE - offset); 66b47820edSDaeho Jeong 67b47820edSDaeho Jeong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 68b47820edSDaeho Jeong offset = offsetof(struct ext4_inode, i_checksum_hi); 69b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + 70b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE, 71b47820edSDaeho Jeong offset - EXT4_GOOD_OLD_INODE_SIZE); 72b47820edSDaeho Jeong if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 73b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, 74b47820edSDaeho Jeong csum_size); 75b47820edSDaeho Jeong offset += csum_size; 76814525f4SDarrick J. Wong } 7705ac5aa1SDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 7805ac5aa1SDaeho Jeong EXT4_INODE_SIZE(inode->i_sb) - offset); 79b47820edSDaeho Jeong } 80814525f4SDarrick J. Wong 81814525f4SDarrick J. Wong return csum; 82814525f4SDarrick J. Wong } 83814525f4SDarrick J. Wong 84814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 85814525f4SDarrick J. Wong struct ext4_inode_info *ei) 86814525f4SDarrick J. Wong { 87814525f4SDarrick J. Wong __u32 provided, calculated; 88814525f4SDarrick J. Wong 89814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 90814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 919aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 92814525f4SDarrick J. Wong return 1; 93814525f4SDarrick J. Wong 94814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 95814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 96814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 97814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 98814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 99814525f4SDarrick J. Wong else 100814525f4SDarrick J. Wong calculated &= 0xFFFF; 101814525f4SDarrick J. Wong 102814525f4SDarrick J. Wong return provided == calculated; 103814525f4SDarrick J. Wong } 104814525f4SDarrick J. Wong 1058016e29fSHarshad Shirwadkar void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 106814525f4SDarrick J. Wong struct ext4_inode_info *ei) 107814525f4SDarrick J. Wong { 108814525f4SDarrick J. Wong __u32 csum; 109814525f4SDarrick J. Wong 110814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 111814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 1129aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 113814525f4SDarrick J. Wong return; 114814525f4SDarrick J. Wong 115814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 116814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 117814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 118814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 119814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 120814525f4SDarrick J. Wong } 121814525f4SDarrick J. Wong 122678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 123678aaf48SJan Kara loff_t new_size) 124678aaf48SJan Kara { 1257ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1268aefcd55STheodore Ts'o /* 1278aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1288aefcd55STheodore Ts'o * writing, so there's no need to call 1298aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1308aefcd55STheodore Ts'o * outstanding writes we need to flush. 1318aefcd55STheodore Ts'o */ 1328aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1338aefcd55STheodore Ts'o return 0; 1348aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1358aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 136678aaf48SJan Kara new_size); 137678aaf48SJan Kara } 138678aaf48SJan Kara 139dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 140dec214d0STahsin Erdogan int pextents); 14164769240SAlex Tomas 142ac27a0ecSDave Kleikamp /* 143ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 144407cd7fbSTahsin Erdogan * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 145ac27a0ecSDave Kleikamp */ 146f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode) 147ac27a0ecSDave Kleikamp { 148fc82228aSAndi Kleen if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 149fc82228aSAndi Kleen int ea_blocks = EXT4_I(inode)->i_file_acl ? 150fc82228aSAndi Kleen EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 151fc82228aSAndi Kleen 152fc82228aSAndi Kleen if (ext4_has_inline_data(inode)) 153fc82228aSAndi Kleen return 0; 154fc82228aSAndi Kleen 155fc82228aSAndi Kleen return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 156fc82228aSAndi Kleen } 157407cd7fbSTahsin Erdogan return S_ISLNK(inode->i_mode) && inode->i_size && 158407cd7fbSTahsin Erdogan (inode->i_size < EXT4_N_BLOCKS * 4); 159ac27a0ecSDave Kleikamp } 160ac27a0ecSDave Kleikamp 161ac27a0ecSDave Kleikamp /* 162ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 163ac27a0ecSDave Kleikamp */ 1640930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 165ac27a0ecSDave Kleikamp { 166ac27a0ecSDave Kleikamp handle_t *handle; 167bc965ab3STheodore Ts'o int err; 16865db869cSJan Kara /* 16965db869cSJan Kara * Credits for final inode cleanup and freeing: 17065db869cSJan Kara * sb + inode (ext4_orphan_del()), block bitmap, group descriptor 17165db869cSJan Kara * (xattr block freeing), bitmap, group descriptor (inode freeing) 17265db869cSJan Kara */ 17365db869cSJan Kara int extra_credits = 6; 1740421a189STahsin Erdogan struct ext4_xattr_inode_array *ea_inode_array = NULL; 17546e294efSJan Kara bool freeze_protected = false; 176ac27a0ecSDave Kleikamp 1777ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1782581fdc8SJiaying Zhang 1796bc0d63dSJan Kara if (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL) 1806bc0d63dSJan Kara ext4_evict_ea_inode(inode); 1810930fcc1SAl Viro if (inode->i_nlink) { 18291b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 1835dc23bddSJan Kara 1840930fcc1SAl Viro goto no_delete; 1850930fcc1SAl Viro } 1860930fcc1SAl Viro 187e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 188e2bfb088STheodore Ts'o goto no_delete; 189871a2931SChristoph Hellwig dquot_initialize(inode); 190907f4554SChristoph Hellwig 191678aaf48SJan Kara if (ext4_should_order_data(inode)) 192678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 19391b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 194ac27a0ecSDave Kleikamp 1958e8ad8a5SJan Kara /* 196ceff86fdSJan Kara * For inodes with journalled data, transaction commit could have 197bc12ac98SZhang Yi * dirtied the inode. And for inodes with dioread_nolock, unwritten 198bc12ac98SZhang Yi * extents converting worker could merge extents and also have dirtied 199bc12ac98SZhang Yi * the inode. Flush worker is ignoring it because of I_FREEING flag but 200bc12ac98SZhang Yi * we still need to remove the inode from the writeback lists. 201ceff86fdSJan Kara */ 202bc12ac98SZhang Yi if (!list_empty_careful(&inode->i_io_list)) 203ceff86fdSJan Kara inode_io_list_del(inode); 204ceff86fdSJan Kara 205ceff86fdSJan Kara /* 2068e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 20746e294efSJan Kara * protection against it. When we are in a running transaction though, 20846e294efSJan Kara * we are already protected against freezing and we cannot grab further 20946e294efSJan Kara * protection due to lock ordering constraints. 2108e8ad8a5SJan Kara */ 21146e294efSJan Kara if (!ext4_journal_current_handle()) { 2128e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 21346e294efSJan Kara freeze_protected = true; 21446e294efSJan Kara } 215e50e5129SAndreas Dilger 21630a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 21730a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 21830a7eb97STahsin Erdogan 21965db869cSJan Kara /* 22065db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 22165db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 22265db869cSJan Kara */ 22330a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 22465db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 225ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 226bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 227ac27a0ecSDave Kleikamp /* 228ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 229ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 230ac27a0ecSDave Kleikamp * cleaned up. 231ac27a0ecSDave Kleikamp */ 232617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 23346e294efSJan Kara if (freeze_protected) 2348e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 235ac27a0ecSDave Kleikamp goto no_delete; 236ac27a0ecSDave Kleikamp } 23730a7eb97STahsin Erdogan 238ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2390390131bSFrank Mayhar ext4_handle_sync(handle); 240407cd7fbSTahsin Erdogan 241407cd7fbSTahsin Erdogan /* 242407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 243407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 244407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 245407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 246407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 247407cd7fbSTahsin Erdogan */ 248407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 249407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 250ac27a0ecSDave Kleikamp inode->i_size = 0; 251bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 252bc965ab3STheodore Ts'o if (err) { 25312062dddSEric Sandeen ext4_warning(inode->i_sb, 254bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 255bc965ab3STheodore Ts'o goto stop_handle; 256bc965ab3STheodore Ts'o } 2572c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2582c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2592c98eb5eSTheodore Ts'o if (err) { 26054d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err, 2612c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2622c98eb5eSTheodore Ts'o inode->i_ino, err); 2632c98eb5eSTheodore Ts'o goto stop_handle; 2642c98eb5eSTheodore Ts'o } 2652c98eb5eSTheodore Ts'o } 266bc965ab3STheodore Ts'o 26730a7eb97STahsin Erdogan /* Remove xattr references. */ 26830a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 26930a7eb97STahsin Erdogan extra_credits); 27030a7eb97STahsin Erdogan if (err) { 27130a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 272bc965ab3STheodore Ts'o stop_handle: 273bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27445388219STheodore Ts'o ext4_orphan_del(NULL, inode); 27546e294efSJan Kara if (freeze_protected) 2768e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 27730a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 278bc965ab3STheodore Ts'o goto no_delete; 279bc965ab3STheodore Ts'o } 280bc965ab3STheodore Ts'o 281ac27a0ecSDave Kleikamp /* 282617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 283ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 284617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 285ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 286617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 287ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 288ac27a0ecSDave Kleikamp */ 289617ba13bSMingming Cao ext4_orphan_del(handle, inode); 2905ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 291ac27a0ecSDave Kleikamp 292ac27a0ecSDave Kleikamp /* 293ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 294ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 295ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 296ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 297ac27a0ecSDave Kleikamp * fails. 298ac27a0ecSDave Kleikamp */ 299617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 300ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3010930fcc1SAl Viro ext4_clear_inode(inode); 302ac27a0ecSDave Kleikamp else 303617ba13bSMingming Cao ext4_free_inode(handle, inode); 304617ba13bSMingming Cao ext4_journal_stop(handle); 30546e294efSJan Kara if (freeze_protected) 3068e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3070421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 308ac27a0ecSDave Kleikamp return; 309ac27a0ecSDave Kleikamp no_delete: 310318cdc82SZhang Yi /* 311318cdc82SZhang Yi * Check out some where else accidentally dirty the evicting inode, 312318cdc82SZhang Yi * which may probably cause inode use-after-free issues later. 313318cdc82SZhang Yi */ 314318cdc82SZhang Yi WARN_ON_ONCE(!list_empty_careful(&inode->i_io_list)); 315318cdc82SZhang Yi 316b21ebf14SHarshad Shirwadkar if (!list_empty(&EXT4_I(inode)->i_fc_list)) 317e85c81baSXin Yin ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM, NULL); 3180930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 319ac27a0ecSDave Kleikamp } 320ac27a0ecSDave Kleikamp 321a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 322a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 32360e58e0fSMingming Cao { 324a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 32560e58e0fSMingming Cao } 326a9e7f447SDmitry Monakhov #endif 3279d0be502STheodore Ts'o 32812219aeaSAneesh Kumar K.V /* 3290637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3300637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3310637c6f4STheodore Ts'o */ 3325f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3335f634d06SAneesh Kumar K.V int used, int quota_claim) 33412219aeaSAneesh Kumar K.V { 33512219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3360637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 33712219aeaSAneesh Kumar K.V 3380637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 339d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3400637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3418de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3421084f252STheodore Ts'o "with only %d reserved data blocks", 3430637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3440637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3450637c6f4STheodore Ts'o WARN_ON(1); 3460637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3476bc6e63fSAneesh Kumar K.V } 34812219aeaSAneesh Kumar K.V 3490637c6f4STheodore Ts'o /* Update per-inode reservations */ 3500637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 35171d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3520637c6f4STheodore Ts'o 353f9505c72Schenyichong spin_unlock(&ei->i_block_reservation_lock); 35460e58e0fSMingming Cao 35572b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 35672b8ab9dSEric Sandeen if (quota_claim) 3577b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 35872b8ab9dSEric Sandeen else { 3595f634d06SAneesh Kumar K.V /* 3605f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3615f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 36272b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3635f634d06SAneesh Kumar K.V */ 3647b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3655f634d06SAneesh Kumar K.V } 366d6014301SAneesh Kumar K.V 367d6014301SAneesh Kumar K.V /* 368d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 369d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 370d6014301SAneesh Kumar K.V * inode's preallocations. 371d6014301SAneesh Kumar K.V */ 3720637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 37382dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 37427bc446eSbrookxu ext4_discard_preallocations(inode, 0); 37512219aeaSAneesh Kumar K.V } 37612219aeaSAneesh Kumar K.V 377e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 378c398eda0STheodore Ts'o unsigned int line, 37924676da4STheodore Ts'o struct ext4_map_blocks *map) 3806fd058f7STheodore Ts'o { 381345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 382345c0dbfSTheodore Ts'o (inode->i_ino == 383345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 384345c0dbfSTheodore Ts'o return 0; 385ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 386c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 387bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 38824676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 389bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 3906a797d27SDarrick J. Wong return -EFSCORRUPTED; 3916fd058f7STheodore Ts'o } 3926fd058f7STheodore Ts'o return 0; 3936fd058f7STheodore Ts'o } 3946fd058f7STheodore Ts'o 39553085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 39653085facSJan Kara ext4_lblk_t len) 39753085facSJan Kara { 39853085facSJan Kara int ret; 39953085facSJan Kara 40033b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 401a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 40253085facSJan Kara 40353085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 40453085facSJan Kara if (ret > 0) 40553085facSJan Kara ret = 0; 40653085facSJan Kara 40753085facSJan Kara return ret; 40853085facSJan Kara } 40953085facSJan Kara 410e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 411c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 412e29136f8STheodore Ts'o 413921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 414921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 415921f266bSDmitry Monakhov struct inode *inode, 416921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 417921f266bSDmitry Monakhov struct ext4_map_blocks *map, 418921f266bSDmitry Monakhov int flags) 419921f266bSDmitry Monakhov { 420921f266bSDmitry Monakhov int retval; 421921f266bSDmitry Monakhov 422921f266bSDmitry Monakhov map->m_flags = 0; 423921f266bSDmitry Monakhov /* 424921f266bSDmitry Monakhov * There is a race window that the result is not the same. 425921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 426921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 427921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 428921f266bSDmitry Monakhov * could be converted. 429921f266bSDmitry Monakhov */ 430c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 431921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4329e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 433921f266bSDmitry Monakhov } else { 4349e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 435921f266bSDmitry Monakhov } 436921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 437921f266bSDmitry Monakhov 438921f266bSDmitry Monakhov /* 439921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 440921f266bSDmitry Monakhov * tree. So the m_len might not equal. 441921f266bSDmitry Monakhov */ 442921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 443921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 444921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 445bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 446921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 447921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 448921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 449921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 450921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 451921f266bSDmitry Monakhov retval, flags); 452921f266bSDmitry Monakhov } 453921f266bSDmitry Monakhov } 454921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 455921f266bSDmitry Monakhov 45655138e0bSTheodore Ts'o /* 457e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4582b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 459f5ab0d1fSMingming Cao * 460f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 461f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 462f5ab0d1fSMingming Cao * mapped. 463f5ab0d1fSMingming Cao * 464e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 465e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 466f5ab0d1fSMingming Cao * based files 467f5ab0d1fSMingming Cao * 468facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 469facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 470facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 471f5ab0d1fSMingming Cao * 472f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 473facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 474facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 475f5ab0d1fSMingming Cao * 476f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 477f5ab0d1fSMingming Cao */ 478e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 479e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4800e855ac8SAneesh Kumar K.V { 481d100eef2SZheng Liu struct extent_status es; 4820e855ac8SAneesh Kumar K.V int retval; 483b8a86845SLukas Czerner int ret = 0; 484921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 485921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 486921f266bSDmitry Monakhov 487921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 488921f266bSDmitry Monakhov #endif 489f5ab0d1fSMingming Cao 490e35fd660STheodore Ts'o map->m_flags = 0; 49170aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 49270aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 493d100eef2SZheng Liu 494e861b5e9STheodore Ts'o /* 495e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 496e861b5e9STheodore Ts'o */ 497e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 498e861b5e9STheodore Ts'o map->m_len = INT_MAX; 499e861b5e9STheodore Ts'o 5004adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5014adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5026a797d27SDarrick J. Wong return -EFSCORRUPTED; 5034adb6ab3SKazuya Mio 504d100eef2SZheng Liu /* Lookup extent status tree firstly */ 5058016e29fSHarshad Shirwadkar if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 5068016e29fSHarshad Shirwadkar ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 507d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 508d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 509d100eef2SZheng Liu map->m_lblk - es.es_lblk; 510d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 511d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 512d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 513d100eef2SZheng Liu if (retval > map->m_len) 514d100eef2SZheng Liu retval = map->m_len; 515d100eef2SZheng Liu map->m_len = retval; 516d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 517facab4d9SJan Kara map->m_pblk = 0; 518facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 519facab4d9SJan Kara if (retval > map->m_len) 520facab4d9SJan Kara retval = map->m_len; 521facab4d9SJan Kara map->m_len = retval; 522d100eef2SZheng Liu retval = 0; 523d100eef2SZheng Liu } else { 5241e83bc81SArnd Bergmann BUG(); 525d100eef2SZheng Liu } 5269558cf14SZhang Yi 5279558cf14SZhang Yi if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) 5289558cf14SZhang Yi return retval; 529921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 530921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 531921f266bSDmitry Monakhov &orig_map, flags); 532921f266bSDmitry Monakhov #endif 533d100eef2SZheng Liu goto found; 534d100eef2SZheng Liu } 5359558cf14SZhang Yi /* 5369558cf14SZhang Yi * In the query cache no-wait mode, nothing we can do more if we 5379558cf14SZhang Yi * cannot find extent in the cache. 5389558cf14SZhang Yi */ 5399558cf14SZhang Yi if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) 5409558cf14SZhang Yi return 0; 541d100eef2SZheng Liu 5424df3d265SAneesh Kumar K.V /* 543b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 544b920c755STheodore Ts'o * file system block. 5454df3d265SAneesh Kumar K.V */ 546c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 54712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5489e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5494df3d265SAneesh Kumar K.V } else { 5509e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5510e855ac8SAneesh Kumar K.V } 552f7fec032SZheng Liu if (retval > 0) { 5533be78c73STheodore Ts'o unsigned int status; 554f7fec032SZheng Liu 55544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 55644fb851dSZheng Liu ext4_warning(inode->i_sb, 55744fb851dSZheng Liu "ES len assertion failed for inode " 55844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 55944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 56044fb851dSZheng Liu WARN_ON(1); 561921f266bSDmitry Monakhov } 562921f266bSDmitry Monakhov 563f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 564f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 565f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 566d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 567ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 568f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 569f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 570f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 571f7fec032SZheng Liu map->m_len, map->m_pblk, status); 572f7fec032SZheng Liu if (ret < 0) 573f7fec032SZheng Liu retval = ret; 574f7fec032SZheng Liu } 5754df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 576f5ab0d1fSMingming Cao 577d100eef2SZheng Liu found: 578e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 579b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5806fd058f7STheodore Ts'o if (ret != 0) 5816fd058f7STheodore Ts'o return ret; 5826fd058f7STheodore Ts'o } 5836fd058f7STheodore Ts'o 584f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 585c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5864df3d265SAneesh Kumar K.V return retval; 5874df3d265SAneesh Kumar K.V 5884df3d265SAneesh Kumar K.V /* 589f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 590f5ab0d1fSMingming Cao * 591f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 592df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 593f5ab0d1fSMingming Cao * with buffer head unmapped. 594f5ab0d1fSMingming Cao */ 595e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 596b8a86845SLukas Czerner /* 597b8a86845SLukas Czerner * If we need to convert extent to unwritten 598b8a86845SLukas Czerner * we continue and do the actual work in 599b8a86845SLukas Czerner * ext4_ext_map_blocks() 600b8a86845SLukas Czerner */ 601b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 602f5ab0d1fSMingming Cao return retval; 603f5ab0d1fSMingming Cao 604f5ab0d1fSMingming Cao /* 605a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 606a25a4e1aSZheng Liu * it will be set again. 6072a8964d6SAneesh Kumar K.V */ 608a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6092a8964d6SAneesh Kumar K.V 6102a8964d6SAneesh Kumar K.V /* 611556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 612f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 613d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 614f5ab0d1fSMingming Cao * with create == 1 flag. 6154df3d265SAneesh Kumar K.V */ 616c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 617d2a17637SMingming Cao 618d2a17637SMingming Cao /* 6194df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6204df3d265SAneesh Kumar K.V * could have changed the inode type in between 6214df3d265SAneesh Kumar K.V */ 62212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 623e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6240e855ac8SAneesh Kumar K.V } else { 625e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 626267e4db9SAneesh Kumar K.V 627e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 628267e4db9SAneesh Kumar K.V /* 629267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 630267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 631267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 632267e4db9SAneesh Kumar K.V */ 63319f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 634267e4db9SAneesh Kumar K.V } 6355f634d06SAneesh Kumar K.V } 636d2a17637SMingming Cao 637f7fec032SZheng Liu if (retval > 0) { 6383be78c73STheodore Ts'o unsigned int status; 639f7fec032SZheng Liu 64044fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 64144fb851dSZheng Liu ext4_warning(inode->i_sb, 64244fb851dSZheng Liu "ES len assertion failed for inode " 64344fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 64444fb851dSZheng Liu inode->i_ino, retval, map->m_len); 64544fb851dSZheng Liu WARN_ON(1); 646921f266bSDmitry Monakhov } 647921f266bSDmitry Monakhov 648adb23551SZheng Liu /* 649c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 650c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6519b623df6SJan Kara * use them before they are really zeroed. We also have to 6529b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6539b623df6SJan Kara * overwrite zeros with stale data from block device. 654c86d8db3SJan Kara */ 655c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 656c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 657c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 658c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 659c86d8db3SJan Kara map->m_pblk, map->m_len); 660c86d8db3SJan Kara if (ret) { 661c86d8db3SJan Kara retval = ret; 662c86d8db3SJan Kara goto out_sem; 663c86d8db3SJan Kara } 664c86d8db3SJan Kara } 665c86d8db3SJan Kara 666c86d8db3SJan Kara /* 667adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 668adb23551SZheng Liu * extent status tree. 669adb23551SZheng Liu */ 670adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 671bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 672adb23551SZheng Liu if (ext4_es_is_written(&es)) 673c86d8db3SJan Kara goto out_sem; 674adb23551SZheng Liu } 675f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 676f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 677f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 678d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 679ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 680f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 681f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 682f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 683f7fec032SZheng Liu map->m_pblk, status); 684c86d8db3SJan Kara if (ret < 0) { 68551865fdaSZheng Liu retval = ret; 686c86d8db3SJan Kara goto out_sem; 687c86d8db3SJan Kara } 68851865fdaSZheng Liu } 6895356f261SAditya Kali 690c86d8db3SJan Kara out_sem: 6910e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 692e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 693b8a86845SLukas Czerner ret = check_block_validity(inode, map); 6946fd058f7STheodore Ts'o if (ret != 0) 6956fd058f7STheodore Ts'o return ret; 69606bd3c36SJan Kara 69706bd3c36SJan Kara /* 69806bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 69906bd3c36SJan Kara * visible after transaction commit must be on transaction's 70006bd3c36SJan Kara * ordered data list. 70106bd3c36SJan Kara */ 70206bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 70306bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 70406bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 70502749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 70606bd3c36SJan Kara ext4_should_order_data(inode)) { 70773131fbbSRoss Zwisler loff_t start_byte = 70873131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 70973131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 71073131fbbSRoss Zwisler 711ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 71273131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 71373131fbbSRoss Zwisler start_byte, length); 714ee0876bcSJan Kara else 71573131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 71673131fbbSRoss Zwisler start_byte, length); 71706bd3c36SJan Kara if (ret) 71806bd3c36SJan Kara return ret; 71906bd3c36SJan Kara } 7205e4d0ebaSXin Yin } 7215e4d0ebaSXin Yin if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || 7225e4d0ebaSXin Yin map->m_flags & EXT4_MAP_MAPPED)) 723a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 724aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 725ec8c60beSRitesh Harjani if (retval < 0) 72670aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7270e855ac8SAneesh Kumar K.V return retval; 7280e855ac8SAneesh Kumar K.V } 7290e855ac8SAneesh Kumar K.V 730ed8ad838SJan Kara /* 731ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 732ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 733ed8ad838SJan Kara */ 734ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 735ed8ad838SJan Kara { 736ed8ad838SJan Kara unsigned long old_state; 737ed8ad838SJan Kara unsigned long new_state; 738ed8ad838SJan Kara 739ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 740ed8ad838SJan Kara 741ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 742ed8ad838SJan Kara if (!bh->b_page) { 743ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 744ed8ad838SJan Kara return; 745ed8ad838SJan Kara } 746ed8ad838SJan Kara /* 747ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 748ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 749ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 750ed8ad838SJan Kara */ 751ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 7523ee2a3e7SUros Bizjak do { 753ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 7543ee2a3e7SUros Bizjak } while (unlikely(!try_cmpxchg(&bh->b_state, &old_state, new_state))); 755ed8ad838SJan Kara } 756ed8ad838SJan Kara 7572ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7582ed88685STheodore Ts'o struct buffer_head *bh, int flags) 759ac27a0ecSDave Kleikamp { 7602ed88685STheodore Ts'o struct ext4_map_blocks map; 761efe70c29SJan Kara int ret = 0; 762ac27a0ecSDave Kleikamp 76346c7f254STao Ma if (ext4_has_inline_data(inode)) 76446c7f254STao Ma return -ERANGE; 76546c7f254STao Ma 7662ed88685STheodore Ts'o map.m_lblk = iblock; 7672ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7682ed88685STheodore Ts'o 769efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 770efe70c29SJan Kara flags); 771ac27a0ecSDave Kleikamp if (ret > 0) { 7722ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 773ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7742ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 775ac27a0ecSDave Kleikamp ret = 0; 776547edce3SRoss Zwisler } else if (ret == 0) { 777547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 778547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 779ac27a0ecSDave Kleikamp } 780ac27a0ecSDave Kleikamp return ret; 781ac27a0ecSDave Kleikamp } 782ac27a0ecSDave Kleikamp 7832ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7842ed88685STheodore Ts'o struct buffer_head *bh, int create) 7852ed88685STheodore Ts'o { 7862ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7872ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7882ed88685STheodore Ts'o } 7892ed88685STheodore Ts'o 790ac27a0ecSDave Kleikamp /* 791705965bdSJan Kara * Get block function used when preparing for buffered write if we require 792705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 793705965bdSJan Kara * will be converted to written after the IO is complete. 794705965bdSJan Kara */ 795705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 796705965bdSJan Kara struct buffer_head *bh_result, int create) 797705965bdSJan Kara { 798705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 799705965bdSJan Kara inode->i_ino, create); 800705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 8018d5459c1SJan Kara EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); 802705965bdSJan Kara } 803705965bdSJan Kara 804efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 805efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 806efe70c29SJan Kara 807e84dfbe2SJan Kara /* 808ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 809ac27a0ecSDave Kleikamp */ 810617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 811c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 812ac27a0ecSDave Kleikamp { 8132ed88685STheodore Ts'o struct ext4_map_blocks map; 8142ed88685STheodore Ts'o struct buffer_head *bh; 815c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 8169558cf14SZhang Yi bool nowait = map_flags & EXT4_GET_BLOCKS_CACHED_NOWAIT; 81710560082STheodore Ts'o int err; 818ac27a0ecSDave Kleikamp 819837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8208016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 8219558cf14SZhang Yi ASSERT(create == 0 || !nowait); 822ac27a0ecSDave Kleikamp 8232ed88685STheodore Ts'o map.m_lblk = block; 8242ed88685STheodore Ts'o map.m_len = 1; 825c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8262ed88685STheodore Ts'o 82710560082STheodore Ts'o if (err == 0) 82810560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8292ed88685STheodore Ts'o if (err < 0) 83010560082STheodore Ts'o return ERR_PTR(err); 8312ed88685STheodore Ts'o 8329558cf14SZhang Yi if (nowait) 8339558cf14SZhang Yi return sb_find_get_block(inode->i_sb, map.m_pblk); 8349558cf14SZhang Yi 8352ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 83610560082STheodore Ts'o if (unlikely(!bh)) 83710560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8382ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 839837c23fbSChunguang Xu ASSERT(create != 0); 840837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8418016e29fSHarshad Shirwadkar || (handle != NULL)); 842ac27a0ecSDave Kleikamp 843ac27a0ecSDave Kleikamp /* 844ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 845ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 846ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 847617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 848ac27a0ecSDave Kleikamp * problem. 849ac27a0ecSDave Kleikamp */ 850ac27a0ecSDave Kleikamp lock_buffer(bh); 851ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 852188c299eSJan Kara err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 853188c299eSJan Kara EXT4_JTR_NONE); 85410560082STheodore Ts'o if (unlikely(err)) { 85510560082STheodore Ts'o unlock_buffer(bh); 85610560082STheodore Ts'o goto errout; 85710560082STheodore Ts'o } 85810560082STheodore Ts'o if (!buffer_uptodate(bh)) { 859ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 860ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 861ac27a0ecSDave Kleikamp } 862ac27a0ecSDave Kleikamp unlock_buffer(bh); 8630390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8640390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 86510560082STheodore Ts'o if (unlikely(err)) 86610560082STheodore Ts'o goto errout; 86710560082STheodore Ts'o } else 868ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 869ac27a0ecSDave Kleikamp return bh; 87010560082STheodore Ts'o errout: 87110560082STheodore Ts'o brelse(bh); 87210560082STheodore Ts'o return ERR_PTR(err); 873ac27a0ecSDave Kleikamp } 874ac27a0ecSDave Kleikamp 875617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 876c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 877ac27a0ecSDave Kleikamp { 878ac27a0ecSDave Kleikamp struct buffer_head *bh; 8792d069c08Szhangyi (F) int ret; 880ac27a0ecSDave Kleikamp 881c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 8821c215028STheodore Ts'o if (IS_ERR(bh)) 883ac27a0ecSDave Kleikamp return bh; 8847963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 885ac27a0ecSDave Kleikamp return bh; 8862d069c08Szhangyi (F) 8872d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 8882d069c08Szhangyi (F) if (ret) { 889ac27a0ecSDave Kleikamp put_bh(bh); 8902d069c08Szhangyi (F) return ERR_PTR(ret); 8912d069c08Szhangyi (F) } 8922d069c08Szhangyi (F) return bh; 893ac27a0ecSDave Kleikamp } 894ac27a0ecSDave Kleikamp 8959699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 8969699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 8979699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 8989699d4f9STahsin Erdogan { 8999699d4f9STahsin Erdogan int i, err; 9009699d4f9STahsin Erdogan 9019699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9029699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9039699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9049699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9059699d4f9STahsin Erdogan bh_count = i; 9069699d4f9STahsin Erdogan goto out_brelse; 9079699d4f9STahsin Erdogan } 9089699d4f9STahsin Erdogan } 9099699d4f9STahsin Erdogan 9109699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9119699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9122d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9132d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9149699d4f9STahsin Erdogan 9159699d4f9STahsin Erdogan if (!wait) 9169699d4f9STahsin Erdogan return 0; 9179699d4f9STahsin Erdogan 9189699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9199699d4f9STahsin Erdogan if (bhs[i]) 9209699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9219699d4f9STahsin Erdogan 9229699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9239699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9249699d4f9STahsin Erdogan err = -EIO; 9259699d4f9STahsin Erdogan goto out_brelse; 9269699d4f9STahsin Erdogan } 9279699d4f9STahsin Erdogan } 9289699d4f9STahsin Erdogan return 0; 9299699d4f9STahsin Erdogan 9309699d4f9STahsin Erdogan out_brelse: 9319699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9329699d4f9STahsin Erdogan brelse(bhs[i]); 9339699d4f9STahsin Erdogan bhs[i] = NULL; 9349699d4f9STahsin Erdogan } 9359699d4f9STahsin Erdogan return err; 9369699d4f9STahsin Erdogan } 9379699d4f9STahsin Erdogan 938188c299eSJan Kara int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, 939ac27a0ecSDave Kleikamp struct buffer_head *head, 940ac27a0ecSDave Kleikamp unsigned from, 941ac27a0ecSDave Kleikamp unsigned to, 942ac27a0ecSDave Kleikamp int *partial, 943188c299eSJan Kara int (*fn)(handle_t *handle, struct inode *inode, 944ac27a0ecSDave Kleikamp struct buffer_head *bh)) 945ac27a0ecSDave Kleikamp { 946ac27a0ecSDave Kleikamp struct buffer_head *bh; 947ac27a0ecSDave Kleikamp unsigned block_start, block_end; 948ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 949ac27a0ecSDave Kleikamp int err, ret = 0; 950ac27a0ecSDave Kleikamp struct buffer_head *next; 951ac27a0ecSDave Kleikamp 952ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 953ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 954de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 955ac27a0ecSDave Kleikamp next = bh->b_this_page; 956ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 957ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 958ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 959ac27a0ecSDave Kleikamp *partial = 1; 960ac27a0ecSDave Kleikamp continue; 961ac27a0ecSDave Kleikamp } 962188c299eSJan Kara err = (*fn)(handle, inode, bh); 963ac27a0ecSDave Kleikamp if (!ret) 964ac27a0ecSDave Kleikamp ret = err; 965ac27a0ecSDave Kleikamp } 966ac27a0ecSDave Kleikamp return ret; 967ac27a0ecSDave Kleikamp } 968ac27a0ecSDave Kleikamp 969d84c9ebdSJan Kara /* 970d84c9ebdSJan Kara * Helper for handling dirtying of journalled data. We also mark the folio as 971d84c9ebdSJan Kara * dirty so that writeback code knows about this page (and inode) contains 972d84c9ebdSJan Kara * dirty data. ext4_writepages() then commits appropriate transaction to 973d84c9ebdSJan Kara * make data stable. 974d84c9ebdSJan Kara */ 975d84c9ebdSJan Kara static int ext4_dirty_journalled_data(handle_t *handle, struct buffer_head *bh) 976d84c9ebdSJan Kara { 977d84c9ebdSJan Kara folio_mark_dirty(bh->b_folio); 978d84c9ebdSJan Kara return ext4_handle_dirty_metadata(handle, NULL, bh); 979d84c9ebdSJan Kara } 980d84c9ebdSJan Kara 981188c299eSJan Kara int do_journal_get_write_access(handle_t *handle, struct inode *inode, 982ac27a0ecSDave Kleikamp struct buffer_head *bh) 983ac27a0ecSDave Kleikamp { 98456d35a4cSJan Kara int dirty = buffer_dirty(bh); 98556d35a4cSJan Kara int ret; 98656d35a4cSJan Kara 987ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 988ac27a0ecSDave Kleikamp return 0; 98956d35a4cSJan Kara /* 990ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 99156d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 99256d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 993ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 99456d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 99556d35a4cSJan Kara * ever write the buffer. 99656d35a4cSJan Kara */ 99756d35a4cSJan Kara if (dirty) 99856d35a4cSJan Kara clear_buffer_dirty(bh); 9995d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 1000188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1001188c299eSJan Kara EXT4_JTR_NONE); 100256d35a4cSJan Kara if (!ret && dirty) 1003d84c9ebdSJan Kara ret = ext4_dirty_journalled_data(handle, bh); 100456d35a4cSJan Kara return ret; 1005ac27a0ecSDave Kleikamp } 1006ac27a0ecSDave Kleikamp 1007643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 100886b38c27SMatthew Wilcox static int ext4_block_write_begin(struct folio *folio, loff_t pos, unsigned len, 10092058f83aSMichael Halcrow get_block_t *get_block) 10102058f83aSMichael Halcrow { 101109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10122058f83aSMichael Halcrow unsigned to = from + len; 101386b38c27SMatthew Wilcox struct inode *inode = folio->mapping->host; 10142058f83aSMichael Halcrow unsigned block_start, block_end; 10152058f83aSMichael Halcrow sector_t block; 10162058f83aSMichael Halcrow int err = 0; 10172058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10182058f83aSMichael Halcrow unsigned bbits; 10190b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10200b578f35SChandan Rajendra int nr_wait = 0; 10210b578f35SChandan Rajendra int i; 10222058f83aSMichael Halcrow 102386b38c27SMatthew Wilcox BUG_ON(!folio_test_locked(folio)); 102409cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 102509cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10262058f83aSMichael Halcrow BUG_ON(from > to); 10272058f83aSMichael Halcrow 102886b38c27SMatthew Wilcox head = folio_buffers(folio); 102986b38c27SMatthew Wilcox if (!head) { 103086b38c27SMatthew Wilcox create_empty_buffers(&folio->page, blocksize, 0); 103186b38c27SMatthew Wilcox head = folio_buffers(folio); 103286b38c27SMatthew Wilcox } 10332058f83aSMichael Halcrow bbits = ilog2(blocksize); 103486b38c27SMatthew Wilcox block = (sector_t)folio->index << (PAGE_SHIFT - bbits); 10352058f83aSMichael Halcrow 10362058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10372058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10382058f83aSMichael Halcrow block_end = block_start + blocksize; 10392058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 104086b38c27SMatthew Wilcox if (folio_test_uptodate(folio)) { 10412058f83aSMichael Halcrow set_buffer_uptodate(bh); 10422058f83aSMichael Halcrow } 10432058f83aSMichael Halcrow continue; 10442058f83aSMichael Halcrow } 10452058f83aSMichael Halcrow if (buffer_new(bh)) 10462058f83aSMichael Halcrow clear_buffer_new(bh); 10472058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10482058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10492058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10502058f83aSMichael Halcrow if (err) 10512058f83aSMichael Halcrow break; 10522058f83aSMichael Halcrow if (buffer_new(bh)) { 105386b38c27SMatthew Wilcox if (folio_test_uptodate(folio)) { 10542058f83aSMichael Halcrow clear_buffer_new(bh); 10552058f83aSMichael Halcrow set_buffer_uptodate(bh); 10562058f83aSMichael Halcrow mark_buffer_dirty(bh); 10572058f83aSMichael Halcrow continue; 10582058f83aSMichael Halcrow } 10592058f83aSMichael Halcrow if (block_end > to || block_start < from) 106086b38c27SMatthew Wilcox folio_zero_segments(folio, to, 106186b38c27SMatthew Wilcox block_end, 10622058f83aSMichael Halcrow block_start, from); 10632058f83aSMichael Halcrow continue; 10642058f83aSMichael Halcrow } 10652058f83aSMichael Halcrow } 106686b38c27SMatthew Wilcox if (folio_test_uptodate(folio)) { 10672058f83aSMichael Halcrow set_buffer_uptodate(bh); 10682058f83aSMichael Halcrow continue; 10692058f83aSMichael Halcrow } 10702058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10712058f83aSMichael Halcrow !buffer_unwritten(bh) && 10722058f83aSMichael Halcrow (block_start < from || block_end > to)) { 10732d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 10740b578f35SChandan Rajendra wait[nr_wait++] = bh; 10752058f83aSMichael Halcrow } 10762058f83aSMichael Halcrow } 10772058f83aSMichael Halcrow /* 10782058f83aSMichael Halcrow * If we issued read requests, let them complete. 10792058f83aSMichael Halcrow */ 10800b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10810b578f35SChandan Rajendra wait_on_buffer(wait[i]); 10820b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 10832058f83aSMichael Halcrow err = -EIO; 10842058f83aSMichael Halcrow } 10857e0785fcSChandan Rajendra if (unlikely(err)) { 108686b38c27SMatthew Wilcox page_zero_new_buffers(&folio->page, from, to); 10874f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 10880b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10890b578f35SChandan Rajendra int err2; 10900b578f35SChandan Rajendra 109186b38c27SMatthew Wilcox err2 = fscrypt_decrypt_pagecache_blocks(folio, 109286b38c27SMatthew Wilcox blocksize, bh_offset(wait[i])); 10930b578f35SChandan Rajendra if (err2) { 10940b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 10950b578f35SChandan Rajendra err = err2; 10960b578f35SChandan Rajendra } 10970b578f35SChandan Rajendra } 10987e0785fcSChandan Rajendra } 10997e0785fcSChandan Rajendra 11002058f83aSMichael Halcrow return err; 11012058f83aSMichael Halcrow } 11022058f83aSMichael Halcrow #endif 11032058f83aSMichael Halcrow 11049462f770SJan Kara /* 11059462f770SJan Kara * To preserve ordering, it is essential that the hole instantiation and 11069462f770SJan Kara * the data write be encapsulated in a single transaction. We cannot 11079462f770SJan Kara * close off a transaction and start a new one between the ext4_get_block() 11089462f770SJan Kara * and the ext4_write_end(). So doing the jbd2_journal_start at the start of 11099462f770SJan Kara * ext4_write_begin() is the right place. 11109462f770SJan Kara */ 1111bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 11129d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 1113bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1114ac27a0ecSDave Kleikamp { 1115bfc1af65SNick Piggin struct inode *inode = mapping->host; 11161938a150SAneesh Kumar K.V int ret, needed_blocks; 1117ac27a0ecSDave Kleikamp handle_t *handle; 1118ac27a0ecSDave Kleikamp int retries = 0; 11194d934a5eSMatthew Wilcox struct folio *folio; 1120bfc1af65SNick Piggin pgoff_t index; 1121bfc1af65SNick Piggin unsigned from, to; 1122bfc1af65SNick Piggin 11230db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11240db1ff22STheodore Ts'o return -EIO; 11250db1ff22STheodore Ts'o 11269d6b0cd7SMatthew Wilcox (Oracle) trace_ext4_write_begin(inode, pos, len); 11271938a150SAneesh Kumar K.V /* 11281938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11291938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11301938a150SAneesh Kumar K.V */ 11311938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 113209cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 113309cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1134bfc1af65SNick Piggin to = from + len; 1135ac27a0ecSDave Kleikamp 1136f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1137f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1138832ee62dSMatthew Wilcox (Oracle) pagep); 1139f19d5870STao Ma if (ret < 0) 114047564bfbSTheodore Ts'o return ret; 114147564bfbSTheodore Ts'o if (ret == 1) 114247564bfbSTheodore Ts'o return 0; 1143f19d5870STao Ma } 1144f19d5870STao Ma 114547564bfbSTheodore Ts'o /* 11464d934a5eSMatthew Wilcox * __filemap_get_folio() can take a long time if the 11474d934a5eSMatthew Wilcox * system is thrashing due to memory pressure, or if the folio 114847564bfbSTheodore Ts'o * is being written back. So grab it first before we start 114947564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 11504d934a5eSMatthew Wilcox * the folio (if needed) without using GFP_NOFS. 115147564bfbSTheodore Ts'o */ 115247564bfbSTheodore Ts'o retry_grab: 11534d934a5eSMatthew Wilcox folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, 11544d934a5eSMatthew Wilcox mapping_gfp_mask(mapping)); 11557fa8a8eeSLinus Torvalds if (IS_ERR(folio)) 11567fa8a8eeSLinus Torvalds return PTR_ERR(folio); 1157d1052d23SJinke Han /* 1158d1052d23SJinke Han * The same as page allocation, we prealloc buffer heads before 1159d1052d23SJinke Han * starting the handle. 1160d1052d23SJinke Han */ 11614d934a5eSMatthew Wilcox if (!folio_buffers(folio)) 11624d934a5eSMatthew Wilcox create_empty_buffers(&folio->page, inode->i_sb->s_blocksize, 0); 1163d1052d23SJinke Han 11644d934a5eSMatthew Wilcox folio_unlock(folio); 116547564bfbSTheodore Ts'o 116647564bfbSTheodore Ts'o retry_journal: 11679924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 11687479d2b9SAndrew Morton if (IS_ERR(handle)) { 11694d934a5eSMatthew Wilcox folio_put(folio); 117047564bfbSTheodore Ts'o return PTR_ERR(handle); 1171cf108bcaSJan Kara } 1172f19d5870STao Ma 11734d934a5eSMatthew Wilcox folio_lock(folio); 11744d934a5eSMatthew Wilcox if (folio->mapping != mapping) { 11754d934a5eSMatthew Wilcox /* The folio got truncated from under us */ 11764d934a5eSMatthew Wilcox folio_unlock(folio); 11774d934a5eSMatthew Wilcox folio_put(folio); 1178cf108bcaSJan Kara ext4_journal_stop(handle); 117947564bfbSTheodore Ts'o goto retry_grab; 1180cf108bcaSJan Kara } 11814d934a5eSMatthew Wilcox /* In case writeback began while the folio was unlocked */ 11824d934a5eSMatthew Wilcox folio_wait_stable(folio); 1183cf108bcaSJan Kara 1184643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11852058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 118686b38c27SMatthew Wilcox ret = ext4_block_write_begin(folio, pos, len, 1187705965bdSJan Kara ext4_get_block_unwritten); 11882058f83aSMichael Halcrow else 118986b38c27SMatthew Wilcox ret = ext4_block_write_begin(folio, pos, len, ext4_get_block); 11902058f83aSMichael Halcrow #else 1191744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 11924d934a5eSMatthew Wilcox ret = __block_write_begin(&folio->page, pos, len, 1193705965bdSJan Kara ext4_get_block_unwritten); 1194744692dcSJiaying Zhang else 11954d934a5eSMatthew Wilcox ret = __block_write_begin(&folio->page, pos, len, ext4_get_block); 11962058f83aSMichael Halcrow #endif 1197bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1198188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, 11994d934a5eSMatthew Wilcox folio_buffers(folio), from, to, 12004d934a5eSMatthew Wilcox NULL, do_journal_get_write_access); 1201b46be050SAndrey Savochkin } 1202bfc1af65SNick Piggin 1203bfc1af65SNick Piggin if (ret) { 1204c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1205c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1206c93d8f88SEric Biggers 12074d934a5eSMatthew Wilcox folio_unlock(folio); 1208ae4d5372SAneesh Kumar K.V /* 12096e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1210ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1211f340b3d9Shongnanli * i_size_read because we hold i_rwsem. 12121938a150SAneesh Kumar K.V * 12131938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12141938a150SAneesh Kumar K.V * truncate finishes 1215ae4d5372SAneesh Kumar K.V */ 1216c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12171938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12181938a150SAneesh Kumar K.V 12191938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1220c93d8f88SEric Biggers if (extended) { 1221b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12221938a150SAneesh Kumar K.V /* 1223ffacfa7aSJan Kara * If truncate failed early the inode might 12241938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12251938a150SAneesh Kumar K.V * make sure the inode is removed from the 12261938a150SAneesh Kumar K.V * orphan list in that case. 12271938a150SAneesh Kumar K.V */ 12281938a150SAneesh Kumar K.V if (inode->i_nlink) 12291938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12301938a150SAneesh Kumar K.V } 1231bfc1af65SNick Piggin 123247564bfbSTheodore Ts'o if (ret == -ENOSPC && 123347564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 123447564bfbSTheodore Ts'o goto retry_journal; 12354d934a5eSMatthew Wilcox folio_put(folio); 123647564bfbSTheodore Ts'o return ret; 123747564bfbSTheodore Ts'o } 12384d934a5eSMatthew Wilcox *pagep = &folio->page; 1239ac27a0ecSDave Kleikamp return ret; 1240ac27a0ecSDave Kleikamp } 1241ac27a0ecSDave Kleikamp 1242bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1243188c299eSJan Kara static int write_end_fn(handle_t *handle, struct inode *inode, 1244188c299eSJan Kara struct buffer_head *bh) 1245ac27a0ecSDave Kleikamp { 124613fca323STheodore Ts'o int ret; 1247ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1248ac27a0ecSDave Kleikamp return 0; 1249ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1250d84c9ebdSJan Kara ret = ext4_dirty_journalled_data(handle, bh); 125113fca323STheodore Ts'o clear_buffer_meta(bh); 125213fca323STheodore Ts'o clear_buffer_prio(bh); 125313fca323STheodore Ts'o return ret; 1254ac27a0ecSDave Kleikamp } 1255ac27a0ecSDave Kleikamp 1256eed4333fSZheng Liu /* 1257eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1258eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1259eed4333fSZheng Liu * 1260eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1261eed4333fSZheng Liu * buffers are managed internally. 1262eed4333fSZheng Liu */ 1263eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1264f8514083SAneesh Kumar K.V struct address_space *mapping, 1265f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1266f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1267f8514083SAneesh Kumar K.V { 126864fb3136SMatthew Wilcox struct folio *folio = page_folio(page); 1269f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1270eed4333fSZheng Liu struct inode *inode = mapping->host; 12710572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1272eed4333fSZheng Liu int ret = 0, ret2; 1273eed4333fSZheng Liu int i_size_changed = 0; 1274c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1275eed4333fSZheng Liu 1276eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 12776984aef5SZhang Yi 12785c099c4fSYe Bin if (ext4_has_inline_data(inode) && 12795c099c4fSYe Bin ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) 1280d19500daSRitesh Harjani return ext4_write_inline_data_end(inode, pos, len, copied, 1281d19500daSRitesh Harjani folio); 12826984aef5SZhang Yi 12836984aef5SZhang Yi copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1284f8514083SAneesh Kumar K.V /* 128564fb3136SMatthew Wilcox * it's important to update i_size while still holding folio lock: 1286f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1287c93d8f88SEric Biggers * 1288c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1289c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1290f8514083SAneesh Kumar K.V */ 1291c93d8f88SEric Biggers if (!verity) 12924631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 129364fb3136SMatthew Wilcox folio_unlock(folio); 129464fb3136SMatthew Wilcox folio_put(folio); 1295f8514083SAneesh Kumar K.V 1296c93d8f88SEric Biggers if (old_size < pos && !verity) 12970572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1298f8514083SAneesh Kumar K.V /* 129964fb3136SMatthew Wilcox * Don't mark the inode dirty under folio lock. First, it unnecessarily 130064fb3136SMatthew Wilcox * makes the holding time of folio lock longer. Second, it forces lock 130164fb3136SMatthew Wilcox * ordering of folio lock and transaction start for journaling 1302f8514083SAneesh Kumar K.V * filesystems. 1303f8514083SAneesh Kumar K.V */ 13046984aef5SZhang Yi if (i_size_changed) 13054209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1306f8514083SAneesh Kumar K.V 1307c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1308f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1309f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1310f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1311f8514083SAneesh Kumar K.V */ 1312f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 131355ce2f64SZhang Yi 1314617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1315ac27a0ecSDave Kleikamp if (!ret) 1316ac27a0ecSDave Kleikamp ret = ret2; 1317bfc1af65SNick Piggin 1318c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1319b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1320f8514083SAneesh Kumar K.V /* 1321ffacfa7aSJan Kara * If truncate failed early the inode might still be 1322f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1323f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1324f8514083SAneesh Kumar K.V */ 1325f8514083SAneesh Kumar K.V if (inode->i_nlink) 1326f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1327f8514083SAneesh Kumar K.V } 1328f8514083SAneesh Kumar K.V 1329bfc1af65SNick Piggin return ret ? ret : copied; 1330ac27a0ecSDave Kleikamp } 1331ac27a0ecSDave Kleikamp 1332b90197b6STheodore Ts'o /* 1333b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1334b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1335d84c9ebdSJan Kara * to call ext4_dirty_journalled_data() instead. 1336b90197b6STheodore Ts'o */ 13373b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 1338188c299eSJan Kara struct inode *inode, 133986324a21SMatthew Wilcox struct folio *folio, 13403b136499SJan Kara unsigned from, unsigned to) 1341b90197b6STheodore Ts'o { 1342b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1343b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1344b90197b6STheodore Ts'o 134586324a21SMatthew Wilcox bh = head = folio_buffers(folio); 1346b90197b6STheodore Ts'o do { 1347b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1348b90197b6STheodore Ts'o if (buffer_new(bh)) { 1349b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 135086324a21SMatthew Wilcox if (!folio_test_uptodate(folio)) { 1351b90197b6STheodore Ts'o unsigned start, size; 1352b90197b6STheodore Ts'o 1353b90197b6STheodore Ts'o start = max(from, block_start); 1354b90197b6STheodore Ts'o size = min(to, block_end) - start; 1355b90197b6STheodore Ts'o 135686324a21SMatthew Wilcox folio_zero_range(folio, start, size); 1357188c299eSJan Kara write_end_fn(handle, inode, bh); 1358b90197b6STheodore Ts'o } 1359b90197b6STheodore Ts'o clear_buffer_new(bh); 1360b90197b6STheodore Ts'o } 1361b90197b6STheodore Ts'o } 1362b90197b6STheodore Ts'o block_start = block_end; 1363b90197b6STheodore Ts'o bh = bh->b_this_page; 1364b90197b6STheodore Ts'o } while (bh != head); 1365b90197b6STheodore Ts'o } 1366b90197b6STheodore Ts'o 1367bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1368bfc1af65SNick Piggin struct address_space *mapping, 1369bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1370bfc1af65SNick Piggin struct page *page, void *fsdata) 1371ac27a0ecSDave Kleikamp { 1372feb22b77SMatthew Wilcox struct folio *folio = page_folio(page); 1373617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1374bfc1af65SNick Piggin struct inode *inode = mapping->host; 13750572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1376ac27a0ecSDave Kleikamp int ret = 0, ret2; 1377ac27a0ecSDave Kleikamp int partial = 0; 1378bfc1af65SNick Piggin unsigned from, to; 13794631dbf6SDmitry Monakhov int size_changed = 0; 1380c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1381ac27a0ecSDave Kleikamp 13829bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 138309cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1384bfc1af65SNick Piggin to = from + len; 1385bfc1af65SNick Piggin 1386441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1387441c8508SCurt Wohlgemuth 13886984aef5SZhang Yi if (ext4_has_inline_data(inode)) 1389d19500daSRitesh Harjani return ext4_write_inline_data_end(inode, pos, len, copied, 1390d19500daSRitesh Harjani folio); 13916984aef5SZhang Yi 1392feb22b77SMatthew Wilcox if (unlikely(copied < len) && !folio_test_uptodate(folio)) { 1393bfc1af65SNick Piggin copied = 0; 139486324a21SMatthew Wilcox ext4_journalled_zero_new_buffers(handle, inode, folio, 139586324a21SMatthew Wilcox from, to); 13963b136499SJan Kara } else { 13973b136499SJan Kara if (unlikely(copied < len)) 139886324a21SMatthew Wilcox ext4_journalled_zero_new_buffers(handle, inode, folio, 13993b136499SJan Kara from + copied, to); 1400feb22b77SMatthew Wilcox ret = ext4_walk_page_buffers(handle, inode, 1401feb22b77SMatthew Wilcox folio_buffers(folio), 1402188c299eSJan Kara from, from + copied, &partial, 14033b136499SJan Kara write_end_fn); 1404ac27a0ecSDave Kleikamp if (!partial) 1405feb22b77SMatthew Wilcox folio_mark_uptodate(folio); 14063fdcfb66STao Ma } 1407c93d8f88SEric Biggers if (!verity) 14084631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 14092d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1410feb22b77SMatthew Wilcox folio_unlock(folio); 1411feb22b77SMatthew Wilcox folio_put(folio); 14124631dbf6SDmitry Monakhov 1413c93d8f88SEric Biggers if (old_size < pos && !verity) 14140572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14150572639fSXiaoguang Wang 14166984aef5SZhang Yi if (size_changed) { 1417617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1418ac27a0ecSDave Kleikamp if (!ret) 1419ac27a0ecSDave Kleikamp ret = ret2; 1420ac27a0ecSDave Kleikamp } 1421bfc1af65SNick Piggin 1422c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1423f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1424f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1425f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1426f8514083SAneesh Kumar K.V */ 1427f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1428f8514083SAneesh Kumar K.V 1429617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1430ac27a0ecSDave Kleikamp if (!ret) 1431ac27a0ecSDave Kleikamp ret = ret2; 1432c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1433b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1434f8514083SAneesh Kumar K.V /* 1435ffacfa7aSJan Kara * If truncate failed early the inode might still be 1436f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1437f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1438f8514083SAneesh Kumar K.V */ 1439f8514083SAneesh Kumar K.V if (inode->i_nlink) 1440f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1441f8514083SAneesh Kumar K.V } 1442bfc1af65SNick Piggin 1443bfc1af65SNick Piggin return ret ? ret : copied; 1444ac27a0ecSDave Kleikamp } 1445d2a17637SMingming Cao 14469d0be502STheodore Ts'o /* 1447c27e43a1SEric Whitney * Reserve space for a single cluster 14489d0be502STheodore Ts'o */ 1449c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1450d2a17637SMingming Cao { 1451d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14520637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14535dd4056dSChristoph Hellwig int ret; 1454d2a17637SMingming Cao 145560e58e0fSMingming Cao /* 145672b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 145772b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 145872b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 145960e58e0fSMingming Cao */ 14607b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14615dd4056dSChristoph Hellwig if (ret) 14625dd4056dSChristoph Hellwig return ret; 146303179fe9STheodore Ts'o 146403179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 146571d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 146603179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 146703179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1468d2a17637SMingming Cao return -ENOSPC; 1469d2a17637SMingming Cao } 14709d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1471c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14720637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147339bc680aSDmitry Monakhov 1474d2a17637SMingming Cao return 0; /* success */ 1475d2a17637SMingming Cao } 1476d2a17637SMingming Cao 1477f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1478d2a17637SMingming Cao { 1479d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14800637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1481d2a17637SMingming Cao 1482cd213226SMingming Cao if (!to_free) 1483cd213226SMingming Cao return; /* Nothing to release, exit */ 1484cd213226SMingming Cao 1485d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1486cd213226SMingming Cao 14875a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14880637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1489cd213226SMingming Cao /* 14900637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14910637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14920637c6f4STheodore Ts'o * function is called from invalidate page, it's 14930637c6f4STheodore Ts'o * harmless to return without any action. 1494cd213226SMingming Cao */ 14958de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 14960637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 14971084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 14980637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 14990637c6f4STheodore Ts'o WARN_ON(1); 15000637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15010637c6f4STheodore Ts'o } 15020637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15030637c6f4STheodore Ts'o 150472b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 150557042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1506d2a17637SMingming Cao 1507d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 150860e58e0fSMingming Cao 15097b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1510d2a17637SMingming Cao } 1511d2a17637SMingming Cao 1512ac27a0ecSDave Kleikamp /* 151364769240SAlex Tomas * Delayed allocation stuff 151464769240SAlex Tomas */ 151564769240SAlex Tomas 15164e7ea81dSJan Kara struct mpage_da_data { 151715648d59SJan Kara /* These are input fields for ext4_do_writepages() */ 15184e7ea81dSJan Kara struct inode *inode; 15194e7ea81dSJan Kara struct writeback_control *wbc; 152015648d59SJan Kara unsigned int can_map:1; /* Can writepages call map blocks? */ 15216b523df4SJan Kara 152215648d59SJan Kara /* These are internal state of ext4_do_writepages() */ 15234e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15244e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15254e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 152664769240SAlex Tomas /* 15274e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15284e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15294e7ea81dSJan Kara * is delalloc or unwritten. 153064769240SAlex Tomas */ 15314e7ea81dSJan Kara struct ext4_map_blocks map; 15324e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1533dddbd6acSJan Kara unsigned int do_map:1; 15346b8ed620SJan Kara unsigned int scanned_until_end:1; 15351f1a55f0SJan Kara unsigned int journalled_more_data:1; 15364e7ea81dSJan Kara }; 153764769240SAlex Tomas 15384e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15394e7ea81dSJan Kara bool invalidate) 1540c4a0c46eSAneesh Kumar K.V { 1541fb5a5be0SMatthew Wilcox (Oracle) unsigned nr, i; 1542c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1543fb5a5be0SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1544c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1545c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15464e7ea81dSJan Kara 15474e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15484e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15494e7ea81dSJan Kara return; 1550c4a0c46eSAneesh Kumar K.V 15516b8ed620SJan Kara mpd->scanned_until_end = 0; 1552c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1553c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15544e7ea81dSJan Kara if (invalidate) { 15554e7ea81dSJan Kara ext4_lblk_t start, last; 155609cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 155709cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 15587f0d8e1dSEric Whitney 15597f0d8e1dSEric Whitney /* 15607f0d8e1dSEric Whitney * avoid racing with extent status tree scans made by 15617f0d8e1dSEric Whitney * ext4_insert_delayed_block() 15627f0d8e1dSEric Whitney */ 15637f0d8e1dSEric Whitney down_write(&EXT4_I(inode)->i_data_sem); 156451865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15657f0d8e1dSEric Whitney up_write(&EXT4_I(inode)->i_data_sem); 15664e7ea81dSJan Kara } 156751865fdaSZheng Liu 1568fb5a5be0SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1569c4a0c46eSAneesh Kumar K.V while (index <= end) { 1570fb5a5be0SMatthew Wilcox (Oracle) nr = filemap_get_folios(mapping, &index, end, &fbatch); 1571fb5a5be0SMatthew Wilcox (Oracle) if (nr == 0) 1572c4a0c46eSAneesh Kumar K.V break; 1573fb5a5be0SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) { 1574fb5a5be0SMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 15752b85a617SJan Kara 1576fb5a5be0SMatthew Wilcox (Oracle) if (folio->index < mpd->first_page) 1577fb5a5be0SMatthew Wilcox (Oracle) continue; 1578fb5a5be0SMatthew Wilcox (Oracle) if (folio->index + folio_nr_pages(folio) - 1 > end) 1579fb5a5be0SMatthew Wilcox (Oracle) continue; 15807ba13abbSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 15817ba13abbSMatthew Wilcox (Oracle) BUG_ON(folio_test_writeback(folio)); 15824e7ea81dSJan Kara if (invalidate) { 15837ba13abbSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 15847ba13abbSMatthew Wilcox (Oracle) folio_clear_dirty_for_io(folio); 15857ba13abbSMatthew Wilcox (Oracle) block_invalidate_folio(folio, 0, 15867ba13abbSMatthew Wilcox (Oracle) folio_size(folio)); 15877ba13abbSMatthew Wilcox (Oracle) folio_clear_uptodate(folio); 15884e7ea81dSJan Kara } 15897ba13abbSMatthew Wilcox (Oracle) folio_unlock(folio); 1590c4a0c46eSAneesh Kumar K.V } 1591fb5a5be0SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1592c4a0c46eSAneesh Kumar K.V } 1593c4a0c46eSAneesh Kumar K.V } 1594c4a0c46eSAneesh Kumar K.V 1595df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1596df22291fSAneesh Kumar K.V { 1597df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 159892b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1599f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 160092b97816STheodore Ts'o 160192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16025dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1603f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 160492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 160592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1606f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 160757042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 160892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1609f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16107b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 161192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 161292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1613f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1614df22291fSAneesh Kumar K.V return; 1615df22291fSAneesh Kumar K.V } 1616df22291fSAneesh Kumar K.V 161764769240SAlex Tomas /* 16180b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16190b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16200b02f4c0SEric Whitney * count or making a pending reservation 16210b02f4c0SEric Whitney * where needed 16220b02f4c0SEric Whitney * 16230b02f4c0SEric Whitney * @inode - file containing the newly added block 16240b02f4c0SEric Whitney * @lblk - logical block to be added 16250b02f4c0SEric Whitney * 16260b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16270b02f4c0SEric Whitney */ 16280b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16290b02f4c0SEric Whitney { 16300b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16310b02f4c0SEric Whitney int ret; 16320b02f4c0SEric Whitney bool allocated = false; 16336fed8395SJeffle Xu bool reserved = false; 16340b02f4c0SEric Whitney 16350b02f4c0SEric Whitney /* 16360b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16370b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16380b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16390b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16400b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16410b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16420b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16430b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16440b02f4c0SEric Whitney * extents status tree doesn't get a match. 16450b02f4c0SEric Whitney */ 16460b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16470b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16480b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16490b02f4c0SEric Whitney goto errout; 16506fed8395SJeffle Xu reserved = true; 16510b02f4c0SEric Whitney } else { /* bigalloc */ 16520b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16530b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16540b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16550b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16560b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16570b02f4c0SEric Whitney if (ret < 0) 16580b02f4c0SEric Whitney goto errout; 16590b02f4c0SEric Whitney if (ret == 0) { 16600b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16610b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16620b02f4c0SEric Whitney goto errout; 16636fed8395SJeffle Xu reserved = true; 16640b02f4c0SEric Whitney } else { 16650b02f4c0SEric Whitney allocated = true; 16660b02f4c0SEric Whitney } 16670b02f4c0SEric Whitney } else { 16680b02f4c0SEric Whitney allocated = true; 16690b02f4c0SEric Whitney } 16700b02f4c0SEric Whitney } 16710b02f4c0SEric Whitney } 16720b02f4c0SEric Whitney 16730b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16746fed8395SJeffle Xu if (ret && reserved) 16756fed8395SJeffle Xu ext4_da_release_space(inode, 1); 16760b02f4c0SEric Whitney 16770b02f4c0SEric Whitney errout: 16780b02f4c0SEric Whitney return ret; 16790b02f4c0SEric Whitney } 16800b02f4c0SEric Whitney 16810b02f4c0SEric Whitney /* 16825356f261SAditya Kali * This function is grabs code from the very beginning of 16835356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16845356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16855356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16865356f261SAditya Kali */ 16875356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16885356f261SAditya Kali struct ext4_map_blocks *map, 16895356f261SAditya Kali struct buffer_head *bh) 16905356f261SAditya Kali { 1691d100eef2SZheng Liu struct extent_status es; 16925356f261SAditya Kali int retval; 16935356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1694921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1695921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1696921f266bSDmitry Monakhov 1697921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1698921f266bSDmitry Monakhov #endif 16995356f261SAditya Kali 17005356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17015356f261SAditya Kali invalid_block = ~0; 17025356f261SAditya Kali 17035356f261SAditya Kali map->m_flags = 0; 170470aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 17055356f261SAditya Kali (unsigned long) map->m_lblk); 1706d100eef2SZheng Liu 1707d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1708bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1709d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1710d100eef2SZheng Liu retval = 0; 1711c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1712d100eef2SZheng Liu goto add_delayed; 1713d100eef2SZheng Liu } 1714d100eef2SZheng Liu 1715d100eef2SZheng Liu /* 17163eda41dfSEric Whitney * Delayed extent could be allocated by fallocate. 17173eda41dfSEric Whitney * So we need to check it. 1718d100eef2SZheng Liu */ 17193eda41dfSEric Whitney if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 17203eda41dfSEric Whitney map_bh(bh, inode->i_sb, invalid_block); 17213eda41dfSEric Whitney set_buffer_new(bh); 17223eda41dfSEric Whitney set_buffer_delay(bh); 1723d100eef2SZheng Liu return 0; 1724d100eef2SZheng Liu } 1725d100eef2SZheng Liu 1726d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1727d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1728d100eef2SZheng Liu if (retval > map->m_len) 1729d100eef2SZheng Liu retval = map->m_len; 1730d100eef2SZheng Liu map->m_len = retval; 1731d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1732d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1733d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1734d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1735d100eef2SZheng Liu else 17361e83bc81SArnd Bergmann BUG(); 1737d100eef2SZheng Liu 1738921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1739921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1740921f266bSDmitry Monakhov #endif 1741d100eef2SZheng Liu return retval; 1742d100eef2SZheng Liu } 1743d100eef2SZheng Liu 17445356f261SAditya Kali /* 17455356f261SAditya Kali * Try to see if we can get the block without requesting a new 17465356f261SAditya Kali * file system block. 17475356f261SAditya Kali */ 1748c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1749cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17509c3569b5STao Ma retval = 0; 1751cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17522f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17535356f261SAditya Kali else 17542f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17555356f261SAditya Kali 1756d100eef2SZheng Liu add_delayed: 17575356f261SAditya Kali if (retval == 0) { 1758f7fec032SZheng Liu int ret; 1759ad431025SEric Whitney 17605356f261SAditya Kali /* 17615356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17625356f261SAditya Kali * is it OK? 17635356f261SAditya Kali */ 17645356f261SAditya Kali 17650b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17660b02f4c0SEric Whitney if (ret != 0) { 1767f7fec032SZheng Liu retval = ret; 176851865fdaSZheng Liu goto out_unlock; 1769f7fec032SZheng Liu } 177051865fdaSZheng Liu 17715356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17725356f261SAditya Kali set_buffer_new(bh); 17735356f261SAditya Kali set_buffer_delay(bh); 1774f7fec032SZheng Liu } else if (retval > 0) { 1775f7fec032SZheng Liu int ret; 17763be78c73STheodore Ts'o unsigned int status; 1777f7fec032SZheng Liu 177844fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 177944fb851dSZheng Liu ext4_warning(inode->i_sb, 178044fb851dSZheng Liu "ES len assertion failed for inode " 178144fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 178244fb851dSZheng Liu inode->i_ino, retval, map->m_len); 178344fb851dSZheng Liu WARN_ON(1); 1784921f266bSDmitry Monakhov } 1785921f266bSDmitry Monakhov 1786f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1787f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1788f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1789f7fec032SZheng Liu map->m_pblk, status); 1790f7fec032SZheng Liu if (ret != 0) 1791f7fec032SZheng Liu retval = ret; 17925356f261SAditya Kali } 17935356f261SAditya Kali 17945356f261SAditya Kali out_unlock: 17955356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17965356f261SAditya Kali 17975356f261SAditya Kali return retval; 17985356f261SAditya Kali } 17995356f261SAditya Kali 18005356f261SAditya Kali /* 1801d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1802b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1803b920c755STheodore Ts'o * reserve space for a single block. 180429fa89d0SAneesh Kumar K.V * 180529fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 180629fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 180729fa89d0SAneesh Kumar K.V * 180829fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 180929fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 181029fa89d0SAneesh Kumar K.V * initialized properly. 181164769240SAlex Tomas */ 18129c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18132ed88685STheodore Ts'o struct buffer_head *bh, int create) 181464769240SAlex Tomas { 18152ed88685STheodore Ts'o struct ext4_map_blocks map; 181664769240SAlex Tomas int ret = 0; 181764769240SAlex Tomas 181864769240SAlex Tomas BUG_ON(create == 0); 18192ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18202ed88685STheodore Ts'o 18212ed88685STheodore Ts'o map.m_lblk = iblock; 18222ed88685STheodore Ts'o map.m_len = 1; 182364769240SAlex Tomas 182464769240SAlex Tomas /* 182564769240SAlex Tomas * first, we need to know whether the block is allocated already 182664769240SAlex Tomas * preallocated blocks are unmapped but should treated 182764769240SAlex Tomas * the same as allocated blocks. 182864769240SAlex Tomas */ 18295356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18305356f261SAditya Kali if (ret <= 0) 18312ed88685STheodore Ts'o return ret; 183264769240SAlex Tomas 18332ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1834ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18352ed88685STheodore Ts'o 18362ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18372ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18382ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18392ed88685STheodore Ts'o * get_block multiple times when we write to the same 18402ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18412ed88685STheodore Ts'o * for partial write. 18422ed88685STheodore Ts'o */ 18432ed88685STheodore Ts'o set_buffer_new(bh); 1844c8205636STheodore Ts'o set_buffer_mapped(bh); 18452ed88685STheodore Ts'o } 18462ed88685STheodore Ts'o return 0; 184764769240SAlex Tomas } 184861628a3fSMingming Cao 184933483b3bSMatthew Wilcox static void mpage_folio_done(struct mpage_da_data *mpd, struct folio *folio) 1850eaf2ca10SJan Kara { 185133483b3bSMatthew Wilcox mpd->first_page += folio_nr_pages(folio); 185233483b3bSMatthew Wilcox folio_unlock(folio); 1853eaf2ca10SJan Kara } 1854eaf2ca10SJan Kara 185581a0d3e1SMatthew Wilcox static int mpage_submit_folio(struct mpage_da_data *mpd, struct folio *folio) 18565f1132b2SJan Kara { 185781a0d3e1SMatthew Wilcox size_t len; 1858a056bdaaSJan Kara loff_t size; 18595f1132b2SJan Kara int err; 18605f1132b2SJan Kara 186181a0d3e1SMatthew Wilcox BUG_ON(folio->index != mpd->first_page); 186281a0d3e1SMatthew Wilcox folio_clear_dirty_for_io(folio); 1863a056bdaaSJan Kara /* 1864a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 1865a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 1866a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 186781a0d3e1SMatthew Wilcox * data through mmap while writeback runs. folio_clear_dirty_for_io() 1868a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 186981a0d3e1SMatthew Wilcox * written to again until we release folio lock. So only after 187081a0d3e1SMatthew Wilcox * folio_clear_dirty_for_io() we are safe to sample i_size for 1871e8d6062cSMatthew Wilcox * ext4_bio_write_folio() to zero-out tail of the written page. We rely 1872e8d6062cSMatthew Wilcox * on the barrier provided by folio_test_clear_dirty() in 187381a0d3e1SMatthew Wilcox * folio_clear_dirty_for_io() to make sure i_size is really sampled only 1874a056bdaaSJan Kara * after page tables are updated. 1875a056bdaaSJan Kara */ 1876a056bdaaSJan Kara size = i_size_read(mpd->inode); 187781a0d3e1SMatthew Wilcox len = folio_size(folio); 187881a0d3e1SMatthew Wilcox if (folio_pos(folio) + len > size && 1879c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 188009cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 1881e8d6062cSMatthew Wilcox err = ext4_bio_write_folio(&mpd->io_submit, folio, len); 18825f1132b2SJan Kara if (!err) 18835f1132b2SJan Kara mpd->wbc->nr_to_write--; 18845f1132b2SJan Kara 18855f1132b2SJan Kara return err; 18865f1132b2SJan Kara } 18875f1132b2SJan Kara 18886db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 18894e7ea81dSJan Kara 189061628a3fSMingming Cao /* 1891fffb2739SJan Kara * mballoc gives us at most this number of blocks... 1892fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 1893fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 189461628a3fSMingming Cao */ 1895fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 1896525f4ed8SMingming Cao 1897525f4ed8SMingming Cao /* 18984e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 18994e7ea81dSJan Kara * 19004e7ea81dSJan Kara * @mpd - extent of blocks 19014e7ea81dSJan Kara * @lblk - logical number of the block in the file 190209930042SJan Kara * @bh - buffer head we want to add to the extent 19034e7ea81dSJan Kara * 190409930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 190509930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 190609930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 190709930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 190809930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 190909930042SJan Kara * added. 19104e7ea81dSJan Kara */ 191109930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 191209930042SJan Kara struct buffer_head *bh) 19134e7ea81dSJan Kara { 19144e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 19154e7ea81dSJan Kara 191609930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 191709930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 191809930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 191909930042SJan Kara /* So far no extent to map => we write the buffer right away */ 192009930042SJan Kara if (map->m_len == 0) 192109930042SJan Kara return true; 192209930042SJan Kara return false; 192309930042SJan Kara } 19244e7ea81dSJan Kara 19254e7ea81dSJan Kara /* First block in the extent? */ 19264e7ea81dSJan Kara if (map->m_len == 0) { 1927dddbd6acSJan Kara /* We cannot map unless handle is started... */ 1928dddbd6acSJan Kara if (!mpd->do_map) 1929dddbd6acSJan Kara return false; 19304e7ea81dSJan Kara map->m_lblk = lblk; 19314e7ea81dSJan Kara map->m_len = 1; 193209930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 193309930042SJan Kara return true; 19344e7ea81dSJan Kara } 19354e7ea81dSJan Kara 193609930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 193709930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 193809930042SJan Kara return false; 193909930042SJan Kara 19404e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 19414e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 194209930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 19434e7ea81dSJan Kara map->m_len++; 194409930042SJan Kara return true; 19454e7ea81dSJan Kara } 194609930042SJan Kara return false; 19474e7ea81dSJan Kara } 19484e7ea81dSJan Kara 19495f1132b2SJan Kara /* 19505f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 19515f1132b2SJan Kara * 19525f1132b2SJan Kara * @mpd - extent of blocks for mapping 19535f1132b2SJan Kara * @head - the first buffer in the page 19545f1132b2SJan Kara * @bh - buffer we should start processing from 19555f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 19565f1132b2SJan Kara * 19575f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 19585f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 19595f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 19605f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 19615f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 19625f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 19635f1132b2SJan Kara * < 0 in case of error during IO submission. 19645f1132b2SJan Kara */ 19655f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 19664e7ea81dSJan Kara struct buffer_head *head, 19674e7ea81dSJan Kara struct buffer_head *bh, 19684e7ea81dSJan Kara ext4_lblk_t lblk) 19694e7ea81dSJan Kara { 19704e7ea81dSJan Kara struct inode *inode = mpd->inode; 19715f1132b2SJan Kara int err; 197293407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 19734e7ea81dSJan Kara >> inode->i_blkbits; 19744e7ea81dSJan Kara 1975c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 1976c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 1977c93d8f88SEric Biggers 19784e7ea81dSJan Kara do { 19794e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 19804e7ea81dSJan Kara 198109930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 19824e7ea81dSJan Kara /* Found extent to map? */ 19834e7ea81dSJan Kara if (mpd->map.m_len) 19845f1132b2SJan Kara return 0; 1985dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 1986dddbd6acSJan Kara if (!mpd->do_map) 1987dddbd6acSJan Kara return 0; 198809930042SJan Kara /* Everything mapped so far and we hit EOF */ 19895f1132b2SJan Kara break; 19904e7ea81dSJan Kara } 19914e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 19925f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 19935f1132b2SJan Kara if (mpd->map.m_len == 0) { 199481a0d3e1SMatthew Wilcox err = mpage_submit_folio(mpd, head->b_folio); 19955f1132b2SJan Kara if (err < 0) 19964e7ea81dSJan Kara return err; 199733483b3bSMatthew Wilcox mpage_folio_done(mpd, head->b_folio); 19984e7ea81dSJan Kara } 19996b8ed620SJan Kara if (lblk >= blocks) { 20006b8ed620SJan Kara mpd->scanned_until_end = 1; 20016b8ed620SJan Kara return 0; 20026b8ed620SJan Kara } 20036b8ed620SJan Kara return 1; 20045f1132b2SJan Kara } 20054e7ea81dSJan Kara 20064e7ea81dSJan Kara /* 20074da2f6e3SMatthew Wilcox * mpage_process_folio - update folio buffers corresponding to changed extent 20084da2f6e3SMatthew Wilcox * and may submit fully mapped page for IO 20094da2f6e3SMatthew Wilcox * @mpd: description of extent to map, on return next extent to map 20104da2f6e3SMatthew Wilcox * @folio: Contains these buffers. 20114da2f6e3SMatthew Wilcox * @m_lblk: logical block mapping. 20124da2f6e3SMatthew Wilcox * @m_pblk: corresponding physical mapping. 20134da2f6e3SMatthew Wilcox * @map_bh: determines on return whether this page requires any further 20142943fdbcSRitesh Harjani * mapping or not. 20154da2f6e3SMatthew Wilcox * 20164da2f6e3SMatthew Wilcox * Scan given folio buffers corresponding to changed extent and update buffer 20172943fdbcSRitesh Harjani * state according to new extent state. 20182943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 20194da2f6e3SMatthew Wilcox * If the given folio is not fully mapped, we update @mpd to the next extent in 20204da2f6e3SMatthew Wilcox * the given folio that needs mapping & return @map_bh as true. 20212943fdbcSRitesh Harjani */ 20224da2f6e3SMatthew Wilcox static int mpage_process_folio(struct mpage_da_data *mpd, struct folio *folio, 20232943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 20242943fdbcSRitesh Harjani bool *map_bh) 20252943fdbcSRitesh Harjani { 20262943fdbcSRitesh Harjani struct buffer_head *head, *bh; 20272943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 20282943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 20292943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 20302943fdbcSRitesh Harjani int err = 0; 2031c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2032c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2033c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 20342943fdbcSRitesh Harjani 20354da2f6e3SMatthew Wilcox bh = head = folio_buffers(folio); 20362943fdbcSRitesh Harjani do { 20372943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 20382943fdbcSRitesh Harjani continue; 20392943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 20402943fdbcSRitesh Harjani /* 20412943fdbcSRitesh Harjani * Buffer after end of mapped extent. 20424da2f6e3SMatthew Wilcox * Find next buffer in the folio to map. 20432943fdbcSRitesh Harjani */ 20442943fdbcSRitesh Harjani mpd->map.m_len = 0; 20452943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2046c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 20472943fdbcSRitesh Harjani 20482943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 20492943fdbcSRitesh Harjani if (err > 0) 20502943fdbcSRitesh Harjani err = 0; 2051c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2052c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 20534d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 20544d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 20554d06bfb9SRitesh Harjani goto out; 20564d06bfb9SRitesh Harjani } 2057d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2058c8cc8816SRitesh Harjani } 20592943fdbcSRitesh Harjani *map_bh = true; 20602943fdbcSRitesh Harjani goto out; 20612943fdbcSRitesh Harjani } 20622943fdbcSRitesh Harjani if (buffer_delay(bh)) { 20632943fdbcSRitesh Harjani clear_buffer_delay(bh); 20642943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 20652943fdbcSRitesh Harjani } 20662943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2067c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 20682943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2069c8cc8816SRitesh Harjani 2070c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 20712943fdbcSRitesh Harjani *map_bh = false; 20722943fdbcSRitesh Harjani out: 20732943fdbcSRitesh Harjani *m_lblk = lblk; 20742943fdbcSRitesh Harjani *m_pblk = pblock; 20752943fdbcSRitesh Harjani return err; 20762943fdbcSRitesh Harjani } 20772943fdbcSRitesh Harjani 20782943fdbcSRitesh Harjani /* 20794e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 20804e7ea81dSJan Kara * submit fully mapped pages for IO 20814e7ea81dSJan Kara * 20824e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 20834e7ea81dSJan Kara * 20844e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 20854e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 20864e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2087556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 20884e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 20894e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 20904e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 20914e7ea81dSJan Kara */ 20924e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 20934e7ea81dSJan Kara { 20947530d093SMatthew Wilcox (Oracle) struct folio_batch fbatch; 20957530d093SMatthew Wilcox (Oracle) unsigned nr, i; 20964e7ea81dSJan Kara struct inode *inode = mpd->inode; 209709cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 20984e7ea81dSJan Kara pgoff_t start, end; 20994e7ea81dSJan Kara ext4_lblk_t lblk; 21002943fdbcSRitesh Harjani ext4_fsblk_t pblock; 21014e7ea81dSJan Kara int err; 21022943fdbcSRitesh Harjani bool map_bh = false; 21034e7ea81dSJan Kara 21044e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 21054e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 21064e7ea81dSJan Kara lblk = start << bpp_bits; 21074e7ea81dSJan Kara pblock = mpd->map.m_pblk; 21084e7ea81dSJan Kara 21097530d093SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 21104e7ea81dSJan Kara while (start <= end) { 21117530d093SMatthew Wilcox (Oracle) nr = filemap_get_folios(inode->i_mapping, &start, end, &fbatch); 21127530d093SMatthew Wilcox (Oracle) if (nr == 0) 21134e7ea81dSJan Kara break; 21147530d093SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) { 21154da2f6e3SMatthew Wilcox struct folio *folio = fbatch.folios[i]; 21164e7ea81dSJan Kara 21174da2f6e3SMatthew Wilcox err = mpage_process_folio(mpd, folio, &lblk, &pblock, 21182943fdbcSRitesh Harjani &map_bh); 21194e7ea81dSJan Kara /* 21202943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 21212943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 21222943fdbcSRitesh Harjani * So we return to call further extent mapping. 21234e7ea81dSJan Kara */ 212439c0ae16SJason Yan if (err < 0 || map_bh) 21252943fdbcSRitesh Harjani goto out; 21264e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 212781a0d3e1SMatthew Wilcox err = mpage_submit_folio(mpd, folio); 21282943fdbcSRitesh Harjani if (err < 0) 21292943fdbcSRitesh Harjani goto out; 213033483b3bSMatthew Wilcox mpage_folio_done(mpd, folio); 21314e7ea81dSJan Kara } 21327530d093SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 21334e7ea81dSJan Kara } 21344e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 21354e7ea81dSJan Kara mpd->map.m_len = 0; 21364e7ea81dSJan Kara mpd->map.m_flags = 0; 21374e7ea81dSJan Kara return 0; 21382943fdbcSRitesh Harjani out: 21397530d093SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 21402943fdbcSRitesh Harjani return err; 21414e7ea81dSJan Kara } 21424e7ea81dSJan Kara 21434e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 21444e7ea81dSJan Kara { 21454e7ea81dSJan Kara struct inode *inode = mpd->inode; 21464e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21474e7ea81dSJan Kara int get_blocks_flags; 2148090f32eeSLukas Czerner int err, dioread_nolock; 21494e7ea81dSJan Kara 21504e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 21514e7ea81dSJan Kara /* 21524e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2153556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 21544e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 21554e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 21564e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 21574e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 21584e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 21594e7ea81dSJan Kara * possible. 21604e7ea81dSJan Kara * 2161754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2162754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2163754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2164754cfed6STheodore Ts'o * the data was copied into the page cache. 21654e7ea81dSJan Kara */ 21664e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2167ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2168ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2169090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2170090f32eeSLukas Czerner if (dioread_nolock) 21714e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 21726db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 21734e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 21744e7ea81dSJan Kara 21754e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 21764e7ea81dSJan Kara if (err < 0) 21774e7ea81dSJan Kara return err; 2178090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 21796b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 21806b523df4SJan Kara ext4_handle_valid(handle)) { 21816b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 21826b523df4SJan Kara handle->h_rsv_handle = NULL; 21836b523df4SJan Kara } 21843613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 21856b523df4SJan Kara } 21864e7ea81dSJan Kara 21874e7ea81dSJan Kara BUG_ON(map->m_len == 0); 21884e7ea81dSJan Kara return 0; 21894e7ea81dSJan Kara } 21904e7ea81dSJan Kara 21914e7ea81dSJan Kara /* 21924e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 21934e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 21944e7ea81dSJan Kara * 21954e7ea81dSJan Kara * @handle - handle for journal operations 21964e7ea81dSJan Kara * @mpd - extent to map 21977534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 21987534e854SJan Kara * is no hope of writing the data. The caller should discard 21997534e854SJan Kara * dirty pages to avoid infinite loops. 22004e7ea81dSJan Kara * 22014e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 22024e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 22034e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 22044e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 22054e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 22064e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 22074e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 22084e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 22094e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 22104e7ea81dSJan Kara */ 22114e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2212cb530541STheodore Ts'o struct mpage_da_data *mpd, 2213cb530541STheodore Ts'o bool *give_up_on_write) 22144e7ea81dSJan Kara { 22154e7ea81dSJan Kara struct inode *inode = mpd->inode; 22164e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 22174e7ea81dSJan Kara int err; 22184e7ea81dSJan Kara loff_t disksize; 22196603120eSDmitry Monakhov int progress = 0; 2220c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22214d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 22224e7ea81dSJan Kara 22234d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22244d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 22254d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2226c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 222727d7c4edSJan Kara do { 22284e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 22294e7ea81dSJan Kara if (err < 0) { 22304e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 22314e7ea81dSJan Kara 22320db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 22339b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2234cb530541STheodore Ts'o goto invalidate_dirty_pages; 22354e7ea81dSJan Kara /* 2236cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2237cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2238cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 22394e7ea81dSJan Kara */ 2240cb530541STheodore Ts'o if ((err == -ENOMEM) || 22416603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 22426603120eSDmitry Monakhov if (progress) 22436603120eSDmitry Monakhov goto update_disksize; 2244cb530541STheodore Ts'o return err; 22456603120eSDmitry Monakhov } 22464e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 22474e7ea81dSJan Kara "Delayed block allocation failed for " 22484e7ea81dSJan Kara "inode %lu at logical offset %llu with" 22494e7ea81dSJan Kara " max blocks %u with error %d", 22504e7ea81dSJan Kara inode->i_ino, 22514e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2252cb530541STheodore Ts'o (unsigned)map->m_len, -err); 22534e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 22544e7ea81dSJan Kara "This should not happen!! Data will " 22554e7ea81dSJan Kara "be lost\n"); 22564e7ea81dSJan Kara if (err == -ENOSPC) 22574e7ea81dSJan Kara ext4_print_free_blocks(inode); 2258cb530541STheodore Ts'o invalidate_dirty_pages: 2259cb530541STheodore Ts'o *give_up_on_write = true; 22604e7ea81dSJan Kara return err; 22614e7ea81dSJan Kara } 22626603120eSDmitry Monakhov progress = 1; 22634e7ea81dSJan Kara /* 22644e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 22654e7ea81dSJan Kara * extent to map 22664e7ea81dSJan Kara */ 22674e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 22684e7ea81dSJan Kara if (err < 0) 22696603120eSDmitry Monakhov goto update_disksize; 227027d7c4edSJan Kara } while (map->m_len); 22714e7ea81dSJan Kara 22726603120eSDmitry Monakhov update_disksize: 2273622cad13STheodore Ts'o /* 2274622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2275622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2276622cad13STheodore Ts'o */ 227709cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 227835df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 22794e7ea81dSJan Kara int err2; 2280622cad13STheodore Ts'o loff_t i_size; 22814e7ea81dSJan Kara 2282622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2283622cad13STheodore Ts'o i_size = i_size_read(inode); 2284622cad13STheodore Ts'o if (disksize > i_size) 2285622cad13STheodore Ts'o disksize = i_size; 2286622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2287622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2288622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2289b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2290878520acSTheodore Ts'o if (err2) { 229154d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 22924e7ea81dSJan Kara "Failed to mark inode %lu dirty", 22934e7ea81dSJan Kara inode->i_ino); 2294878520acSTheodore Ts'o } 22954e7ea81dSJan Kara if (!err) 22964e7ea81dSJan Kara err = err2; 22974e7ea81dSJan Kara } 22984e7ea81dSJan Kara return err; 22994e7ea81dSJan Kara } 23004e7ea81dSJan Kara 23014e7ea81dSJan Kara /* 2302fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 230320970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2304fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2305fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2306fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2307525f4ed8SMingming Cao */ 2308fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2309fffb2739SJan Kara { 2310fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2311525f4ed8SMingming Cao 2312fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2313fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2314525f4ed8SMingming Cao } 231561628a3fSMingming Cao 231680be8c5cSRitesh Harjani static int ext4_journal_folio_buffers(handle_t *handle, struct folio *folio, 231780be8c5cSRitesh Harjani size_t len) 23183f079114SJan Kara { 231980be8c5cSRitesh Harjani struct buffer_head *page_bufs = folio_buffers(folio); 232080be8c5cSRitesh Harjani struct inode *inode = folio->mapping->host; 23213f079114SJan Kara int ret, err; 23223f079114SJan Kara 23233f079114SJan Kara ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 23243f079114SJan Kara NULL, do_journal_get_write_access); 23253f079114SJan Kara err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 23263f079114SJan Kara NULL, write_end_fn); 23273f079114SJan Kara if (ret == 0) 23283f079114SJan Kara ret = err; 232980be8c5cSRitesh Harjani err = ext4_jbd2_inode_add_write(handle, inode, folio_pos(folio), len); 23303f079114SJan Kara if (ret == 0) 23313f079114SJan Kara ret = err; 23323f079114SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 23333f079114SJan Kara 23343f079114SJan Kara return ret; 23353f079114SJan Kara } 23363f079114SJan Kara 23373f079114SJan Kara static int mpage_journal_page_buffers(handle_t *handle, 23383f079114SJan Kara struct mpage_da_data *mpd, 233980be8c5cSRitesh Harjani struct folio *folio) 23403f079114SJan Kara { 23413f079114SJan Kara struct inode *inode = mpd->inode; 23423f079114SJan Kara loff_t size = i_size_read(inode); 234380be8c5cSRitesh Harjani size_t len = folio_size(folio); 23443f079114SJan Kara 234580be8c5cSRitesh Harjani folio_clear_checked(folio); 23463f079114SJan Kara mpd->wbc->nr_to_write--; 23473f079114SJan Kara 234880be8c5cSRitesh Harjani if (folio_pos(folio) + len > size && 23493f079114SJan Kara !ext4_verity_in_progress(inode)) 235080be8c5cSRitesh Harjani len = size - folio_pos(folio); 23513f079114SJan Kara 235280be8c5cSRitesh Harjani return ext4_journal_folio_buffers(handle, folio, len); 23533f079114SJan Kara } 23543f079114SJan Kara 23558e48dcfbSTheodore Ts'o /* 23564e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 2357de0039f6SJan Kara * needing mapping, submit mapped pages 23584e7ea81dSJan Kara * 23594e7ea81dSJan Kara * @mpd - where to look for pages 23604e7ea81dSJan Kara * 23614e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 2362de0039f6SJan Kara * IO immediately. If we cannot map blocks, we submit just already mapped 2363de0039f6SJan Kara * buffers in the page for IO and keep page dirty. When we can map blocks and 2364de0039f6SJan Kara * we find a page which isn't mapped we start accumulating extent of buffers 2365de0039f6SJan Kara * underlying these pages that needs mapping (formed by either delayed or 2366de0039f6SJan Kara * unwritten buffers). We also lock the pages containing these buffers. The 2367de0039f6SJan Kara * extent found is returned in @mpd structure (starting at mpd->lblk with 2368de0039f6SJan Kara * length mpd->len blocks). 23694e7ea81dSJan Kara * 23704e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 23714e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 23724e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 23734e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 23748e48dcfbSTheodore Ts'o */ 23754e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 23768e48dcfbSTheodore Ts'o { 23774e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 237850ead253SVishal Moola (Oracle) struct folio_batch fbatch; 237950ead253SVishal Moola (Oracle) unsigned int nr_folios; 23804e7ea81dSJan Kara pgoff_t index = mpd->first_page; 23814e7ea81dSJan Kara pgoff_t end = mpd->last_page; 238210bbd235SMatthew Wilcox xa_mark_t tag; 23834e7ea81dSJan Kara int i, err = 0; 23844e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 23854e7ea81dSJan Kara ext4_lblk_t lblk; 23864e7ea81dSJan Kara struct buffer_head *head; 23873f079114SJan Kara handle_t *handle = NULL; 23883f079114SJan Kara int bpp = ext4_journal_blocks_per_page(mpd->inode); 23898e48dcfbSTheodore Ts'o 23904e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 23915b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 23925b41d924SEric Sandeen else 23935b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 23943f079114SJan Kara 2395e6c28a26SJan Kara mpd->map.m_len = 0; 2396e6c28a26SJan Kara mpd->next_page = index; 2397d0ab8368SJan Kara if (ext4_should_journal_data(mpd->inode)) { 23983f079114SJan Kara handle = ext4_journal_start(mpd->inode, EXT4_HT_WRITE_PAGE, 23993f079114SJan Kara bpp); 24003f079114SJan Kara if (IS_ERR(handle)) 24013f079114SJan Kara return PTR_ERR(handle); 24023f079114SJan Kara } 240350ead253SVishal Moola (Oracle) folio_batch_init(&fbatch); 24044f01b02cSTheodore Ts'o while (index <= end) { 240550ead253SVishal Moola (Oracle) nr_folios = filemap_get_folios_tag(mapping, &index, end, 240650ead253SVishal Moola (Oracle) tag, &fbatch); 240750ead253SVishal Moola (Oracle) if (nr_folios == 0) 24086b8ed620SJan Kara break; 24098e48dcfbSTheodore Ts'o 241050ead253SVishal Moola (Oracle) for (i = 0; i < nr_folios; i++) { 241150ead253SVishal Moola (Oracle) struct folio *folio = fbatch.folios[i]; 24128e48dcfbSTheodore Ts'o 24138e48dcfbSTheodore Ts'o /* 2414aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2415aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2416aeac589aSMing Lei * keep going because someone may be concurrently 2417aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2418aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2419aeac589aSMing Lei * of the old dirty pages. 2420aeac589aSMing Lei */ 2421c8e8e16dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_NONE && 2422c8e8e16dSJan Kara mpd->wbc->nr_to_write <= 2423c8e8e16dSJan Kara mpd->map.m_len >> (PAGE_SHIFT - blkbits)) 2424aeac589aSMing Lei goto out; 2425aeac589aSMing Lei 24264e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 242750ead253SVishal Moola (Oracle) if (mpd->map.m_len > 0 && mpd->next_page != folio->index) 24284e7ea81dSJan Kara goto out; 242978aaced3STheodore Ts'o 24303f079114SJan Kara if (handle) { 24313f079114SJan Kara err = ext4_journal_ensure_credits(handle, bpp, 24323f079114SJan Kara 0); 24333f079114SJan Kara if (err < 0) 24343f079114SJan Kara goto out; 24353f079114SJan Kara } 24363f079114SJan Kara 243750ead253SVishal Moola (Oracle) folio_lock(folio); 24388e48dcfbSTheodore Ts'o /* 24394e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 24404e7ea81dSJan Kara * longer corresponds to inode we are writing (which 24414e7ea81dSJan Kara * means it has been truncated or invalidated), or the 24424e7ea81dSJan Kara * page is already under writeback and we are not doing 24434e7ea81dSJan Kara * a data integrity writeback, skip the page 24448e48dcfbSTheodore Ts'o */ 244550ead253SVishal Moola (Oracle) if (!folio_test_dirty(folio) || 244650ead253SVishal Moola (Oracle) (folio_test_writeback(folio) && 24474e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 244850ead253SVishal Moola (Oracle) unlikely(folio->mapping != mapping)) { 244950ead253SVishal Moola (Oracle) folio_unlock(folio); 24508e48dcfbSTheodore Ts'o continue; 24518e48dcfbSTheodore Ts'o } 24528e48dcfbSTheodore Ts'o 245350ead253SVishal Moola (Oracle) folio_wait_writeback(folio); 245450ead253SVishal Moola (Oracle) BUG_ON(folio_test_writeback(folio)); 24558e48dcfbSTheodore Ts'o 2456cc509574STheodore Ts'o /* 2457cc509574STheodore Ts'o * Should never happen but for buggy code in 2458cc509574STheodore Ts'o * other subsystems that call 2459cc509574STheodore Ts'o * set_page_dirty() without properly warning 2460cc509574STheodore Ts'o * the file system first. See [1] for more 2461cc509574STheodore Ts'o * information. 2462cc509574STheodore Ts'o * 2463cc509574STheodore Ts'o * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz 2464cc509574STheodore Ts'o */ 246550ead253SVishal Moola (Oracle) if (!folio_buffers(folio)) { 246650ead253SVishal Moola (Oracle) ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", folio->index); 246750ead253SVishal Moola (Oracle) folio_clear_dirty(folio); 246850ead253SVishal Moola (Oracle) folio_unlock(folio); 2469cc509574STheodore Ts'o continue; 2470cc509574STheodore Ts'o } 2471cc509574STheodore Ts'o 24724e7ea81dSJan Kara if (mpd->map.m_len == 0) 247350ead253SVishal Moola (Oracle) mpd->first_page = folio->index; 247450ead253SVishal Moola (Oracle) mpd->next_page = folio->index + folio_nr_pages(folio); 2475de0039f6SJan Kara /* 24763f079114SJan Kara * Writeout when we cannot modify metadata is simple. 24773f079114SJan Kara * Just submit the page. For data=journal mode we 24783f079114SJan Kara * first handle writeout of the page for checkpoint and 24793f079114SJan Kara * only after that handle delayed page dirtying. This 2480ab382539SJan Kara * makes sure current data is checkpointed to the final 2481ab382539SJan Kara * location before possibly journalling it again which 2482ab382539SJan Kara * is desirable when the page is frequently dirtied 2483ab382539SJan Kara * through a pin. 2484de0039f6SJan Kara */ 2485de0039f6SJan Kara if (!mpd->can_map) { 248681a0d3e1SMatthew Wilcox err = mpage_submit_folio(mpd, folio); 2487de0039f6SJan Kara if (err < 0) 2488de0039f6SJan Kara goto out; 24893f079114SJan Kara /* Pending dirtying of journalled data? */ 249081a0d3e1SMatthew Wilcox if (folio_test_checked(folio)) { 24913f079114SJan Kara err = mpage_journal_page_buffers(handle, 249280be8c5cSRitesh Harjani mpd, folio); 24933f079114SJan Kara if (err < 0) 24943f079114SJan Kara goto out; 24951f1a55f0SJan Kara mpd->journalled_more_data = 1; 24963f079114SJan Kara } 249733483b3bSMatthew Wilcox mpage_folio_done(mpd, folio); 2498de0039f6SJan Kara } else { 2499f8bec370SJan Kara /* Add all dirty buffers to mpd */ 250050ead253SVishal Moola (Oracle) lblk = ((ext4_lblk_t)folio->index) << 250109cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 250250ead253SVishal Moola (Oracle) head = folio_buffers(folio); 2503de0039f6SJan Kara err = mpage_process_page_bufs(mpd, head, head, 2504de0039f6SJan Kara lblk); 25055f1132b2SJan Kara if (err <= 0) 25064e7ea81dSJan Kara goto out; 25075f1132b2SJan Kara err = 0; 2508de0039f6SJan Kara } 25098e48dcfbSTheodore Ts'o } 251050ead253SVishal Moola (Oracle) folio_batch_release(&fbatch); 25118e48dcfbSTheodore Ts'o cond_resched(); 25128e48dcfbSTheodore Ts'o } 25136b8ed620SJan Kara mpd->scanned_until_end = 1; 25143f079114SJan Kara if (handle) 25153f079114SJan Kara ext4_journal_stop(handle); 25164f01b02cSTheodore Ts'o return 0; 25178eb9e5ceSTheodore Ts'o out: 251850ead253SVishal Moola (Oracle) folio_batch_release(&fbatch); 25193f079114SJan Kara if (handle) 25203f079114SJan Kara ext4_journal_stop(handle); 25214e7ea81dSJan Kara return err; 25228e48dcfbSTheodore Ts'o } 25238e48dcfbSTheodore Ts'o 252415648d59SJan Kara static int ext4_do_writepages(struct mpage_da_data *mpd) 252564769240SAlex Tomas { 252615648d59SJan Kara struct writeback_control *wbc = mpd->wbc; 25274e7ea81dSJan Kara pgoff_t writeback_index = 0; 25284e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 252922208dedSAneesh Kumar K.V int range_whole = 0; 25304e7ea81dSJan Kara int cycled = 1; 253161628a3fSMingming Cao handle_t *handle = NULL; 253215648d59SJan Kara struct inode *inode = mpd->inode; 253315648d59SJan Kara struct address_space *mapping = inode->i_mapping; 25346b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 25355e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 25361bce63d1SShaohua Li struct blk_plug plug; 2537cb530541STheodore Ts'o bool give_up_on_write = false; 253861628a3fSMingming Cao 253920970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2540ba80b101STheodore Ts'o 254161628a3fSMingming Cao /* 254261628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 254361628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 254461628a3fSMingming Cao * because that could violate lock ordering on umount 254561628a3fSMingming Cao */ 2546a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2547bbf023c7SMing Lei goto out_writepages; 25482a21e37eSTheodore Ts'o 25492a21e37eSTheodore Ts'o /* 25502a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 25512a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 25522a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 25531751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 25542a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 255520970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 25562a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 25572a21e37eSTheodore Ts'o * the stack trace. 25582a21e37eSTheodore Ts'o */ 25590db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 25609b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2561bbf023c7SMing Lei ret = -EROFS; 2562bbf023c7SMing Lei goto out_writepages; 2563bbf023c7SMing Lei } 25642a21e37eSTheodore Ts'o 25654e7ea81dSJan Kara /* 25664e7ea81dSJan Kara * If we have inline data and arrive here, it means that 25674e7ea81dSJan Kara * we will soon create the block for the 1st page, so 25684e7ea81dSJan Kara * we'd better clear the inline data here. 25694e7ea81dSJan Kara */ 25704e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 25714e7ea81dSJan Kara /* Just inode will be modified... */ 25724e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 25734e7ea81dSJan Kara if (IS_ERR(handle)) { 25744e7ea81dSJan Kara ret = PTR_ERR(handle); 25754e7ea81dSJan Kara goto out_writepages; 25764e7ea81dSJan Kara } 25774e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 25784e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 25794e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 25804e7ea81dSJan Kara ext4_journal_stop(handle); 25814e7ea81dSJan Kara } 25824e7ea81dSJan Kara 25833f079114SJan Kara /* 25843f079114SJan Kara * data=journal mode does not do delalloc so we just need to writeout / 25851f1a55f0SJan Kara * journal already mapped buffers. On the other hand we need to commit 25861f1a55f0SJan Kara * transaction to make data stable. We expect all the data to be 25871f1a55f0SJan Kara * already in the journal (the only exception are DMA pinned pages 25881f1a55f0SJan Kara * dirtied behind our back) so we commit transaction here and run the 25891f1a55f0SJan Kara * writeback loop to checkpoint them. The checkpointing is not actually 25901f1a55f0SJan Kara * necessary to make data persistent *but* quite a few places (extent 25911f1a55f0SJan Kara * shifting operations, fsverity, ...) depend on being able to drop 25921f1a55f0SJan Kara * pagecache pages after calling filemap_write_and_wait() and for that 25931f1a55f0SJan Kara * checkpointing needs to happen. 25943f079114SJan Kara */ 25951f1a55f0SJan Kara if (ext4_should_journal_data(inode)) { 25963f079114SJan Kara mpd->can_map = 0; 25971f1a55f0SJan Kara if (wbc->sync_mode == WB_SYNC_ALL) 25981f1a55f0SJan Kara ext4_fc_commit(sbi->s_journal, 25991f1a55f0SJan Kara EXT4_I(inode)->i_datasync_tid); 26001f1a55f0SJan Kara } 26011f1a55f0SJan Kara mpd->journalled_more_data = 0; 26023f079114SJan Kara 26034e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26044e343231Syangerkun /* 26054e343231Syangerkun * We may need to convert up to one extent per block in 26064e343231Syangerkun * the page and we may dirty the inode. 26074e343231Syangerkun */ 26084e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 26094e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 26104e343231Syangerkun } 26114e343231Syangerkun 261222208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 261322208dedSAneesh Kumar K.V range_whole = 1; 261461628a3fSMingming Cao 26152acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 26164e7ea81dSJan Kara writeback_index = mapping->writeback_index; 26174e7ea81dSJan Kara if (writeback_index) 26182acf2c26SAneesh Kumar K.V cycled = 0; 261915648d59SJan Kara mpd->first_page = writeback_index; 262015648d59SJan Kara mpd->last_page = -1; 26215b41d924SEric Sandeen } else { 262215648d59SJan Kara mpd->first_page = wbc->range_start >> PAGE_SHIFT; 262315648d59SJan Kara mpd->last_page = wbc->range_end >> PAGE_SHIFT; 26245b41d924SEric Sandeen } 2625a1d6cc56SAneesh Kumar K.V 262615648d59SJan Kara ext4_io_submit_init(&mpd->io_submit, wbc); 26272acf2c26SAneesh Kumar K.V retry: 26286e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 262915648d59SJan Kara tag_pages_for_writeback(mapping, mpd->first_page, 263015648d59SJan Kara mpd->last_page); 26311bce63d1SShaohua Li blk_start_plug(&plug); 2632dddbd6acSJan Kara 2633dddbd6acSJan Kara /* 2634dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2635dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2636dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2637dddbd6acSJan Kara * started. 2638dddbd6acSJan Kara */ 263915648d59SJan Kara mpd->do_map = 0; 264015648d59SJan Kara mpd->scanned_until_end = 0; 264115648d59SJan Kara mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 264215648d59SJan Kara if (!mpd->io_submit.io_end) { 2643dddbd6acSJan Kara ret = -ENOMEM; 2644dddbd6acSJan Kara goto unplug; 2645dddbd6acSJan Kara } 264615648d59SJan Kara ret = mpage_prepare_extent_to_map(mpd); 2647a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 264815648d59SJan Kara mpage_release_unused_pages(mpd, false); 2649dddbd6acSJan Kara /* Submit prepared bio */ 265015648d59SJan Kara ext4_io_submit(&mpd->io_submit); 265115648d59SJan Kara ext4_put_io_end_defer(mpd->io_submit.io_end); 265215648d59SJan Kara mpd->io_submit.io_end = NULL; 2653dddbd6acSJan Kara if (ret < 0) 2654dddbd6acSJan Kara goto unplug; 2655dddbd6acSJan Kara 265615648d59SJan Kara while (!mpd->scanned_until_end && wbc->nr_to_write > 0) { 26574e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 265815648d59SJan Kara mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 265915648d59SJan Kara if (!mpd->io_submit.io_end) { 26604e7ea81dSJan Kara ret = -ENOMEM; 26614e7ea81dSJan Kara break; 26624e7ea81dSJan Kara } 2663a1d6cc56SAneesh Kumar K.V 2664de0039f6SJan Kara WARN_ON_ONCE(!mpd->can_map); 2665a1d6cc56SAneesh Kumar K.V /* 26664e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 26674e7ea81dSJan Kara * must always write out whole page (makes a difference when 26684e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 26694e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 26704e7ea81dSJan Kara * not supported by delalloc. 2671a1d6cc56SAneesh Kumar K.V */ 2672a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2673525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2674a1d6cc56SAneesh Kumar K.V 267561628a3fSMingming Cao /* start a new transaction */ 26766b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 26776b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 267861628a3fSMingming Cao if (IS_ERR(handle)) { 267961628a3fSMingming Cao ret = PTR_ERR(handle); 26801693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2681fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2682a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 26834e7ea81dSJan Kara /* Release allocated io_end */ 268415648d59SJan Kara ext4_put_io_end(mpd->io_submit.io_end); 268515648d59SJan Kara mpd->io_submit.io_end = NULL; 26864e7ea81dSJan Kara break; 268761628a3fSMingming Cao } 268815648d59SJan Kara mpd->do_map = 1; 2689f63e6005STheodore Ts'o 269015648d59SJan Kara trace_ext4_da_write_pages(inode, mpd->first_page, wbc); 269115648d59SJan Kara ret = mpage_prepare_extent_to_map(mpd); 269215648d59SJan Kara if (!ret && mpd->map.m_len) 269315648d59SJan Kara ret = mpage_map_and_submit_extent(handle, mpd, 2694cb530541STheodore Ts'o &give_up_on_write); 2695646caa9cSJan Kara /* 2696646caa9cSJan Kara * Caution: If the handle is synchronous, 2697646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2698646caa9cSJan Kara * to finish which may depend on writeback of pages to 2699646caa9cSJan Kara * complete or on page lock to be released. In that 2700b483bb77SRandy Dunlap * case, we have to wait until after we have 2701646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2702646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2703646caa9cSJan Kara * to be able to complete) before stopping the handle. 2704646caa9cSJan Kara */ 2705646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 270661628a3fSMingming Cao ext4_journal_stop(handle); 2707646caa9cSJan Kara handle = NULL; 270815648d59SJan Kara mpd->do_map = 0; 2709646caa9cSJan Kara } 27104e7ea81dSJan Kara /* Unlock pages we didn't use */ 271115648d59SJan Kara mpage_release_unused_pages(mpd, give_up_on_write); 2712a297b2fcSXiaoguang Wang /* Submit prepared bio */ 271315648d59SJan Kara ext4_io_submit(&mpd->io_submit); 2714a297b2fcSXiaoguang Wang 2715646caa9cSJan Kara /* 2716646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2717646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2718646caa9cSJan Kara * we are still holding the transaction as we can 2719646caa9cSJan Kara * release the last reference to io_end which may end 2720646caa9cSJan Kara * up doing unwritten extent conversion. 2721646caa9cSJan Kara */ 2722646caa9cSJan Kara if (handle) { 272315648d59SJan Kara ext4_put_io_end_defer(mpd->io_submit.io_end); 2724646caa9cSJan Kara ext4_journal_stop(handle); 2725646caa9cSJan Kara } else 272615648d59SJan Kara ext4_put_io_end(mpd->io_submit.io_end); 272715648d59SJan Kara mpd->io_submit.io_end = NULL; 2728df22291fSAneesh Kumar K.V 27294e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 27304e7ea81dSJan Kara /* 27314e7ea81dSJan Kara * Commit the transaction which would 273222208dedSAneesh Kumar K.V * free blocks released in the transaction 273322208dedSAneesh Kumar K.V * and try again 273422208dedSAneesh Kumar K.V */ 2735df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 273622208dedSAneesh Kumar K.V ret = 0; 27374e7ea81dSJan Kara continue; 27384e7ea81dSJan Kara } 27394e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 27404e7ea81dSJan Kara if (ret) 274161628a3fSMingming Cao break; 274261628a3fSMingming Cao } 2743dddbd6acSJan Kara unplug: 27441bce63d1SShaohua Li blk_finish_plug(&plug); 27459c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 27462acf2c26SAneesh Kumar K.V cycled = 1; 274715648d59SJan Kara mpd->last_page = writeback_index - 1; 274815648d59SJan Kara mpd->first_page = 0; 27492acf2c26SAneesh Kumar K.V goto retry; 27502acf2c26SAneesh Kumar K.V } 275161628a3fSMingming Cao 275222208dedSAneesh Kumar K.V /* Update index */ 275322208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 275422208dedSAneesh Kumar K.V /* 27554e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 275622208dedSAneesh Kumar K.V * mode will write it back later 275722208dedSAneesh Kumar K.V */ 275815648d59SJan Kara mapping->writeback_index = mpd->first_page; 2759a1d6cc56SAneesh Kumar K.V 276061628a3fSMingming Cao out_writepages: 276120970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 27624e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 276361628a3fSMingming Cao return ret; 276464769240SAlex Tomas } 276564769240SAlex Tomas 276615648d59SJan Kara static int ext4_writepages(struct address_space *mapping, 276715648d59SJan Kara struct writeback_control *wbc) 276815648d59SJan Kara { 276929bc9ceaSJan Kara struct super_block *sb = mapping->host->i_sb; 277015648d59SJan Kara struct mpage_da_data mpd = { 277115648d59SJan Kara .inode = mapping->host, 277215648d59SJan Kara .wbc = wbc, 277315648d59SJan Kara .can_map = 1, 277415648d59SJan Kara }; 277529bc9ceaSJan Kara int ret; 277600d873c1SJan Kara int alloc_ctx; 277715648d59SJan Kara 277829bc9ceaSJan Kara if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 277929bc9ceaSJan Kara return -EIO; 278029bc9ceaSJan Kara 278100d873c1SJan Kara alloc_ctx = ext4_writepages_down_read(sb); 278229bc9ceaSJan Kara ret = ext4_do_writepages(&mpd); 27831f1a55f0SJan Kara /* 27841f1a55f0SJan Kara * For data=journal writeback we could have come across pages marked 27851f1a55f0SJan Kara * for delayed dirtying (PageChecked) which were just added to the 27861f1a55f0SJan Kara * running transaction. Try once more to get them to stable storage. 27871f1a55f0SJan Kara */ 27881f1a55f0SJan Kara if (!ret && mpd.journalled_more_data) 27891f1a55f0SJan Kara ret = ext4_do_writepages(&mpd); 279000d873c1SJan Kara ext4_writepages_up_read(sb, alloc_ctx); 279129bc9ceaSJan Kara 279229bc9ceaSJan Kara return ret; 279315648d59SJan Kara } 279415648d59SJan Kara 279559205c8dSJan Kara int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode) 279659205c8dSJan Kara { 279759205c8dSJan Kara struct writeback_control wbc = { 279859205c8dSJan Kara .sync_mode = WB_SYNC_ALL, 279959205c8dSJan Kara .nr_to_write = LONG_MAX, 280059205c8dSJan Kara .range_start = jinode->i_dirty_start, 280159205c8dSJan Kara .range_end = jinode->i_dirty_end, 280259205c8dSJan Kara }; 280359205c8dSJan Kara struct mpage_da_data mpd = { 280459205c8dSJan Kara .inode = jinode->i_vfs_inode, 280559205c8dSJan Kara .wbc = &wbc, 280659205c8dSJan Kara .can_map = 0, 280759205c8dSJan Kara }; 280859205c8dSJan Kara return ext4_do_writepages(&mpd); 280959205c8dSJan Kara } 281059205c8dSJan Kara 28115f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28125f0663bbSDan Williams struct writeback_control *wbc) 28135f0663bbSDan Williams { 28145f0663bbSDan Williams int ret; 28155f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28165f0663bbSDan Williams struct inode *inode = mapping->host; 28175f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 281800d873c1SJan Kara int alloc_ctx; 28195f0663bbSDan Williams 28205f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28215f0663bbSDan Williams return -EIO; 28225f0663bbSDan Williams 282300d873c1SJan Kara alloc_ctx = ext4_writepages_down_read(inode->i_sb); 28245f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28255f0663bbSDan Williams 28263f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28275f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28285f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 282900d873c1SJan Kara ext4_writepages_up_read(inode->i_sb, alloc_ctx); 28305f0663bbSDan Williams return ret; 28315f0663bbSDan Williams } 28325f0663bbSDan Williams 283379f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 283479f0be8dSAneesh Kumar K.V { 28355c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 283679f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 283779f0be8dSAneesh Kumar K.V 283879f0be8dSAneesh Kumar K.V /* 283979f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 284079f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2841179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 284279f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 284379f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 284479f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 284579f0be8dSAneesh Kumar K.V */ 28465c1ff336SEric Whitney free_clusters = 28475c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28485c1ff336SEric Whitney dirty_clusters = 28495c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 285000d4e736STheodore Ts'o /* 285100d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 285200d4e736STheodore Ts'o */ 28535c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 285410ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 285500d4e736STheodore Ts'o 28565c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 28575c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 285879f0be8dSAneesh Kumar K.V /* 2859c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2860c8afb446SEric Sandeen * or free blocks is less than watermark 286179f0be8dSAneesh Kumar K.V */ 286279f0be8dSAneesh Kumar K.V return 1; 286379f0be8dSAneesh Kumar K.V } 286479f0be8dSAneesh Kumar K.V return 0; 286579f0be8dSAneesh Kumar K.V } 286679f0be8dSAneesh Kumar K.V 286764769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 28689d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 286964769240SAlex Tomas struct page **pagep, void **fsdata) 287064769240SAlex Tomas { 287172b8ab9dSEric Sandeen int ret, retries = 0; 28720b5a2543SMatthew Wilcox struct folio *folio; 287364769240SAlex Tomas pgoff_t index; 287464769240SAlex Tomas struct inode *inode = mapping->host; 287564769240SAlex Tomas 28760db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28770db1ff22STheodore Ts'o return -EIO; 28780db1ff22STheodore Ts'o 287909cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 288079f0be8dSAneesh Kumar K.V 28816493792dSZhang Yi if (ext4_nonda_switch(inode->i_sb) || ext4_verity_in_progress(inode)) { 288279f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 288379f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 28849d6b0cd7SMatthew Wilcox (Oracle) len, pagep, fsdata); 288579f0be8dSAneesh Kumar K.V } 288679f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 28879d6b0cd7SMatthew Wilcox (Oracle) trace_ext4_da_write_begin(inode, pos, len); 28889c3569b5STao Ma 28899c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 289036d116e9SMatthew Wilcox (Oracle) ret = ext4_da_write_inline_data_begin(mapping, inode, pos, len, 28919c3569b5STao Ma pagep, fsdata); 28929c3569b5STao Ma if (ret < 0) 289347564bfbSTheodore Ts'o return ret; 289447564bfbSTheodore Ts'o if (ret == 1) 289547564bfbSTheodore Ts'o return 0; 28969c3569b5STao Ma } 28979c3569b5STao Ma 2898cc883236SZhang Yi retry: 28990b5a2543SMatthew Wilcox folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, 29000b5a2543SMatthew Wilcox mapping_gfp_mask(mapping)); 29017fa8a8eeSLinus Torvalds if (IS_ERR(folio)) 29027fa8a8eeSLinus Torvalds return PTR_ERR(folio); 290347564bfbSTheodore Ts'o 29040b5a2543SMatthew Wilcox /* In case writeback began while the folio was unlocked */ 29050b5a2543SMatthew Wilcox folio_wait_stable(folio); 290664769240SAlex Tomas 2907643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 290886b38c27SMatthew Wilcox ret = ext4_block_write_begin(folio, pos, len, ext4_da_get_block_prep); 29092058f83aSMichael Halcrow #else 29100b5a2543SMatthew Wilcox ret = __block_write_begin(&folio->page, pos, len, ext4_da_get_block_prep); 29112058f83aSMichael Halcrow #endif 291264769240SAlex Tomas if (ret < 0) { 29130b5a2543SMatthew Wilcox folio_unlock(folio); 29140b5a2543SMatthew Wilcox folio_put(folio); 2915ae4d5372SAneesh Kumar K.V /* 2916ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2917ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2918cc883236SZhang Yi * i_size_read because we hold inode lock. 2919ae4d5372SAneesh Kumar K.V */ 2920ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2921b9a4207dSJan Kara ext4_truncate_failed_write(inode); 292247564bfbSTheodore Ts'o 292347564bfbSTheodore Ts'o if (ret == -ENOSPC && 292447564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 2925cc883236SZhang Yi goto retry; 292647564bfbSTheodore Ts'o return ret; 292764769240SAlex Tomas } 292864769240SAlex Tomas 29290b5a2543SMatthew Wilcox *pagep = &folio->page; 293064769240SAlex Tomas return ret; 293164769240SAlex Tomas } 293264769240SAlex Tomas 2933632eaeabSMingming Cao /* 2934632eaeabSMingming Cao * Check if we should update i_disksize 2935632eaeabSMingming Cao * when write to the end of file but not require block allocation 2936632eaeabSMingming Cao */ 2937d19500daSRitesh Harjani static int ext4_da_should_update_i_disksize(struct folio *folio, 2938632eaeabSMingming Cao unsigned long offset) 2939632eaeabSMingming Cao { 2940632eaeabSMingming Cao struct buffer_head *bh; 2941d19500daSRitesh Harjani struct inode *inode = folio->mapping->host; 2942632eaeabSMingming Cao unsigned int idx; 2943632eaeabSMingming Cao int i; 2944632eaeabSMingming Cao 2945d19500daSRitesh Harjani bh = folio_buffers(folio); 2946632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2947632eaeabSMingming Cao 2948632eaeabSMingming Cao for (i = 0; i < idx; i++) 2949632eaeabSMingming Cao bh = bh->b_this_page; 2950632eaeabSMingming Cao 295129fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2952632eaeabSMingming Cao return 0; 2953632eaeabSMingming Cao return 1; 2954632eaeabSMingming Cao } 2955632eaeabSMingming Cao 295664769240SAlex Tomas static int ext4_da_write_end(struct file *file, 295764769240SAlex Tomas struct address_space *mapping, 295864769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 295964769240SAlex Tomas struct page *page, void *fsdata) 296064769240SAlex Tomas { 296164769240SAlex Tomas struct inode *inode = mapping->host; 296264769240SAlex Tomas loff_t new_i_size; 2963632eaeabSMingming Cao unsigned long start, end; 296479f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 2965d19500daSRitesh Harjani struct folio *folio = page_folio(page); 296679f0be8dSAneesh Kumar K.V 296774d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 296874d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 2969d19500daSRitesh Harjani len, copied, &folio->page, fsdata); 2970632eaeabSMingming Cao 29719bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 29729c3569b5STao Ma 29739c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 29749c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 29759c3569b5STao Ma ext4_has_inline_data(inode)) 2976d19500daSRitesh Harjani return ext4_write_inline_data_end(inode, pos, len, copied, 2977d19500daSRitesh Harjani folio); 29789c3569b5STao Ma 29791dedde69SZhihao Cheng if (unlikely(copied < len) && !PageUptodate(page)) 29801dedde69SZhihao Cheng copied = 0; 29811dedde69SZhihao Cheng 298264769240SAlex Tomas start = pos & (PAGE_SIZE - 1); 298364769240SAlex Tomas end = start + copied - 1; 298464769240SAlex Tomas 298564769240SAlex Tomas /* 29864df031ffSZhang Yi * Since we are holding inode lock, we are sure i_disksize <= 29874df031ffSZhang Yi * i_size. We also know that if i_disksize < i_size, there are 29884df031ffSZhang Yi * delalloc writes pending in the range upto i_size. If the end of 29894df031ffSZhang Yi * the current write is <= i_size, there's no need to touch 29904df031ffSZhang Yi * i_disksize since writeback will push i_disksize upto i_size 29914df031ffSZhang Yi * eventually. If the end of the current write is > i_size and 29924df031ffSZhang Yi * inside an allocated block (ext4_da_should_update_i_disksize() 29933f079114SJan Kara * check), we need to update i_disksize here as certain 29943f079114SJan Kara * ext4_writepages() paths not allocating blocks update i_disksize. 29954df031ffSZhang Yi * 29964df031ffSZhang Yi * Note that we defer inode dirtying to generic_write_end() / 29974df031ffSZhang Yi * ext4_da_write_inline_data_end(). 2998d2a17637SMingming Cao */ 299964769240SAlex Tomas new_i_size = pos + copied; 30006984aef5SZhang Yi if (copied && new_i_size > inode->i_size && 3001d19500daSRitesh Harjani ext4_da_should_update_i_disksize(folio, end)) 300264769240SAlex Tomas ext4_update_i_disksize(inode, new_i_size); 3003ccd2506bSTheodore Ts'o 3004d19500daSRitesh Harjani return generic_write_end(file, mapping, pos, len, copied, &folio->page, 3005d19500daSRitesh Harjani fsdata); 3006ac27a0ecSDave Kleikamp } 3007ac27a0ecSDave Kleikamp 3008ccd2506bSTheodore Ts'o /* 3009ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3010ccd2506bSTheodore Ts'o */ 3011ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3012ccd2506bSTheodore Ts'o { 3013ccd2506bSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3014ccd2506bSTheodore Ts'o 301571d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3016ccd2506bSTheodore Ts'o return 0; 3017ccd2506bSTheodore Ts'o 3018ccd2506bSTheodore Ts'o /* 3019ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3020ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3021ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3022ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3023ccd2506bSTheodore Ts'o * would require replicating code paths in: 3024ccd2506bSTheodore Ts'o * 302520970ba6STheodore Ts'o * ext4_writepages() -> 3026ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3027ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3028ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3029ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3030ccd2506bSTheodore Ts'o * 3031ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3032ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3033ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3034ccd2506bSTheodore Ts'o * doing I/O at all. 3035ccd2506bSTheodore Ts'o * 3036ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3037380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3038ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3039ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 304025985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3041ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3042ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3043ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3044ccd2506bSTheodore Ts'o * 3045ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3046ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3047ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3048ccd2506bSTheodore Ts'o */ 3049ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3050ccd2506bSTheodore Ts'o } 3051ac27a0ecSDave Kleikamp 3052ac27a0ecSDave Kleikamp /* 3053ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3054ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3055ac27a0ecSDave Kleikamp * 3056ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3057ac27a0ecSDave Kleikamp * journal. If somebody makes a swapfile on an ext4 data-journaling 3058ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3059ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3060ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3061ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3062ac27a0ecSDave Kleikamp * 3063ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3064ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3065ac27a0ecSDave Kleikamp */ 3066ac27a0ecSDave Kleikamp static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3067ac27a0ecSDave Kleikamp { 3068ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 306951ae846cSYe Bin sector_t ret = 0; 3070ac27a0ecSDave Kleikamp 307151ae846cSYe Bin inode_lock_shared(inode); 307246c7f254STao Ma /* 307346c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 307446c7f254STao Ma */ 307546c7f254STao Ma if (ext4_has_inline_data(inode)) 307651ae846cSYe Bin goto out; 307746c7f254STao Ma 3078ac27a0ecSDave Kleikamp if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 3079951cafa6SJan Kara (test_opt(inode->i_sb, DELALLOC) || 3080951cafa6SJan Kara ext4_should_journal_data(inode))) { 3081ac27a0ecSDave Kleikamp /* 3082951cafa6SJan Kara * With delalloc or journalled data we want to sync the file so 3083951cafa6SJan Kara * that we can make sure we allocate blocks for file and data 3084951cafa6SJan Kara * is in place for the user to see it 3085ac27a0ecSDave Kleikamp */ 3086ac27a0ecSDave Kleikamp filemap_write_and_wait(mapping); 3087ac27a0ecSDave Kleikamp } 3088ac27a0ecSDave Kleikamp 308951ae846cSYe Bin ret = iomap_bmap(mapping, block, &ext4_iomap_ops); 309051ae846cSYe Bin 309151ae846cSYe Bin out: 309251ae846cSYe Bin inode_unlock_shared(inode); 309351ae846cSYe Bin return ret; 3094ac27a0ecSDave Kleikamp } 3095ac27a0ecSDave Kleikamp 3096fe5ddf6bSMatthew Wilcox (Oracle) static int ext4_read_folio(struct file *file, struct folio *folio) 3097ac27a0ecSDave Kleikamp { 309846c7f254STao Ma int ret = -EAGAIN; 3099c0be8e6fSMatthew Wilcox struct inode *inode = folio->mapping->host; 310046c7f254STao Ma 310136c9b450SRitesh Harjani trace_ext4_read_folio(inode, folio); 310246c7f254STao Ma 310346c7f254STao Ma if (ext4_has_inline_data(inode)) 31043edde93eSMatthew Wilcox ret = ext4_readpage_inline(inode, folio); 310546c7f254STao Ma 310646c7f254STao Ma if (ret == -EAGAIN) 3107c0be8e6fSMatthew Wilcox return ext4_mpage_readpages(inode, NULL, folio); 310846c7f254STao Ma 310946c7f254STao Ma return ret; 3110ac27a0ecSDave Kleikamp } 3111ac27a0ecSDave Kleikamp 31126311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3113ac27a0ecSDave Kleikamp { 31146311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 311546c7f254STao Ma 31166311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 311746c7f254STao Ma if (ext4_has_inline_data(inode)) 31186311f91fSMatthew Wilcox (Oracle) return; 311946c7f254STao Ma 3120a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3121ac27a0ecSDave Kleikamp } 3122ac27a0ecSDave Kleikamp 31237ba13abbSMatthew Wilcox (Oracle) static void ext4_invalidate_folio(struct folio *folio, size_t offset, 31247ba13abbSMatthew Wilcox (Oracle) size_t length) 3125ac27a0ecSDave Kleikamp { 3126ccd16945SMatthew Wilcox (Oracle) trace_ext4_invalidate_folio(folio, offset, length); 31270562e0baSJiaying Zhang 31284520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 31297ba13abbSMatthew Wilcox (Oracle) WARN_ON(folio_buffers(folio) && buffer_jbd(folio_buffers(folio))); 31304520fb3cSJan Kara 31317ba13abbSMatthew Wilcox (Oracle) block_invalidate_folio(folio, offset, length); 31324520fb3cSJan Kara } 31334520fb3cSJan Kara 3134ccd16945SMatthew Wilcox (Oracle) static int __ext4_journalled_invalidate_folio(struct folio *folio, 3135ccd16945SMatthew Wilcox (Oracle) size_t offset, size_t length) 31364520fb3cSJan Kara { 3137ccd16945SMatthew Wilcox (Oracle) journal_t *journal = EXT4_JOURNAL(folio->mapping->host); 31384520fb3cSJan Kara 3139ccd16945SMatthew Wilcox (Oracle) trace_ext4_journalled_invalidate_folio(folio, offset, length); 31404520fb3cSJan Kara 3141744692dcSJiaying Zhang /* 3142ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3143ac27a0ecSDave Kleikamp */ 3144ccd16945SMatthew Wilcox (Oracle) if (offset == 0 && length == folio_size(folio)) 3145ccd16945SMatthew Wilcox (Oracle) folio_clear_checked(folio); 3146ac27a0ecSDave Kleikamp 3147ccd16945SMatthew Wilcox (Oracle) return jbd2_journal_invalidate_folio(journal, folio, offset, length); 314853e87268SJan Kara } 314953e87268SJan Kara 315053e87268SJan Kara /* Wrapper for aops... */ 3151ccd16945SMatthew Wilcox (Oracle) static void ext4_journalled_invalidate_folio(struct folio *folio, 3152ccd16945SMatthew Wilcox (Oracle) size_t offset, 3153ccd16945SMatthew Wilcox (Oracle) size_t length) 315453e87268SJan Kara { 3155ccd16945SMatthew Wilcox (Oracle) WARN_ON(__ext4_journalled_invalidate_folio(folio, offset, length) < 0); 3156ac27a0ecSDave Kleikamp } 3157ac27a0ecSDave Kleikamp 31583c402f15SMatthew Wilcox (Oracle) static bool ext4_release_folio(struct folio *folio, gfp_t wait) 3159ac27a0ecSDave Kleikamp { 316036c9b450SRitesh Harjani struct inode *inode = folio->mapping->host; 316136c9b450SRitesh Harjani journal_t *journal = EXT4_JOURNAL(inode); 3162ac27a0ecSDave Kleikamp 316336c9b450SRitesh Harjani trace_ext4_release_folio(inode, folio); 31640562e0baSJiaying Zhang 3165e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 31663c402f15SMatthew Wilcox (Oracle) if (folio_test_checked(folio)) 31673c402f15SMatthew Wilcox (Oracle) return false; 31680390131bSFrank Mayhar if (journal) 3169c56a6eb0SMatthew Wilcox (Oracle) return jbd2_journal_try_to_free_buffers(journal, folio); 31700390131bSFrank Mayhar else 317168189fefSMatthew Wilcox (Oracle) return try_to_free_buffers(folio); 3172ac27a0ecSDave Kleikamp } 3173ac27a0ecSDave Kleikamp 3174b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3175b8a6176cSJan Kara { 3176b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3177b8a6176cSJan Kara 3178aa75f4d3SHarshad Shirwadkar if (journal) { 3179aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3180aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3181d0520df7SAndrea Righi return false; 3182d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 31831ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3184d0520df7SAndrea Righi return true; 3185aa75f4d3SHarshad Shirwadkar } 3186aa75f4d3SHarshad Shirwadkar 3187b8a6176cSJan Kara /* Any metadata buffers to write? */ 3188b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3189b8a6176cSJan Kara return true; 3190b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3191b8a6176cSJan Kara } 3192b8a6176cSJan Kara 3193c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3194c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3195de205114SChristoph Hellwig loff_t length, unsigned int flags) 3196364443cbSJan Kara { 3197c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3198c8fdfe29SMatthew Bobrowski 3199c8fdfe29SMatthew Bobrowski /* 3200c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3201c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3202c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3203c8fdfe29SMatthew Bobrowski */ 3204c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3205c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3206c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3207c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3208c8fdfe29SMatthew Bobrowski 3209c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3210c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3211c8fdfe29SMatthew Bobrowski 3212de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3213c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3214de205114SChristoph Hellwig else 3215de205114SChristoph Hellwig iomap->bdev = inode->i_sb->s_bdev; 3216c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3217c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3218c8fdfe29SMatthew Bobrowski 32196386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 32206386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32216386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 32226386722aSRitesh Harjani 3223c8fdfe29SMatthew Bobrowski /* 3224c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3225c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3226c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3227c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3228c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3229c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3230c8fdfe29SMatthew Bobrowski * been set first. 3231c8fdfe29SMatthew Bobrowski */ 3232c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3233c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3234c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3235de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3236de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3237c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3238c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3239c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3240de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3241de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3242c8fdfe29SMatthew Bobrowski } else { 3243c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3244c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3245c8fdfe29SMatthew Bobrowski } 3246c8fdfe29SMatthew Bobrowski } 3247c8fdfe29SMatthew Bobrowski 3248f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3249f063db5eSMatthew Bobrowski unsigned int flags) 3250f063db5eSMatthew Bobrowski { 3251f063db5eSMatthew Bobrowski handle_t *handle; 3252378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3253378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3254f063db5eSMatthew Bobrowski 3255f063db5eSMatthew Bobrowski /* 3256f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3257f063db5eSMatthew Bobrowski * once for direct I/O. 3258f063db5eSMatthew Bobrowski */ 3259f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3260f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3261f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3262f063db5eSMatthew Bobrowski 3263f063db5eSMatthew Bobrowski retry: 3264f063db5eSMatthew Bobrowski /* 3265f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3266f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3267f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3268f063db5eSMatthew Bobrowski * fits into the credits as well. 3269f063db5eSMatthew Bobrowski */ 3270f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3271f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3272f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3273f063db5eSMatthew Bobrowski 3274378f32baSMatthew Bobrowski /* 3275378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3276378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3277378f32baSMatthew Bobrowski */ 3278952da063SChristoph Hellwig WARN_ON(!(flags & (IOMAP_DAX | IOMAP_DIRECT))); 3279952da063SChristoph Hellwig if (flags & IOMAP_DAX) 3280378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3281378f32baSMatthew Bobrowski /* 3282378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3283378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3284378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3285378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3286378f32baSMatthew Bobrowski */ 3287d0b040f5SJan Kara else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3288378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3289378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3290378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3291378f32baSMatthew Bobrowski 3292378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3293378f32baSMatthew Bobrowski 3294378f32baSMatthew Bobrowski /* 3295378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3296378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3297378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3298378f32baSMatthew Bobrowski */ 3299378f32baSMatthew Bobrowski if (!m_flags && !ret) 3300378f32baSMatthew Bobrowski ret = -ENOTBLK; 3301f063db5eSMatthew Bobrowski 3302f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3303f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3304f063db5eSMatthew Bobrowski goto retry; 3305f063db5eSMatthew Bobrowski 3306f063db5eSMatthew Bobrowski return ret; 3307f063db5eSMatthew Bobrowski } 3308f063db5eSMatthew Bobrowski 3309f063db5eSMatthew Bobrowski 3310364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3311c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3312364443cbSJan Kara { 3313364443cbSJan Kara int ret; 331409edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 331509edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3316364443cbSJan Kara 3317bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3318bcd8e91fSTheodore Ts'o return -EINVAL; 33197046ae35SAndreas Gruenbacher 3320364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3321364443cbSJan Kara return -ERANGE; 3322364443cbSJan Kara 332309edf4d3SMatthew Bobrowski /* 332409edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 332509edf4d3SMatthew Bobrowski */ 332609edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 332709edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 332809edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3329364443cbSJan Kara 33309faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 33319faac62dSRitesh Harjani /* 33329faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 33339faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 33349faac62dSRitesh Harjani * the mapping information. This could boost performance 33359faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 33369faac62dSRitesh Harjani */ 33379faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3338545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 33399faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 33409faac62dSRitesh Harjani goto out; 33419faac62dSRitesh Harjani } 33429faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 33439faac62dSRitesh Harjani } else { 33449faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 33459faac62dSRitesh Harjani } 3346f063db5eSMatthew Bobrowski 3347545052e9SChristoph Hellwig if (ret < 0) 3348545052e9SChristoph Hellwig return ret; 33499faac62dSRitesh Harjani out: 335038ea50daSEric Biggers /* 335138ea50daSEric Biggers * When inline encryption is enabled, sometimes I/O to an encrypted file 335238ea50daSEric Biggers * has to be broken up to guarantee DUN contiguity. Handle this by 335338ea50daSEric Biggers * limiting the length of the mapping returned. 335438ea50daSEric Biggers */ 335538ea50daSEric Biggers map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); 335638ea50daSEric Biggers 3357de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 3358545052e9SChristoph Hellwig 3359364443cbSJan Kara return 0; 3360364443cbSJan Kara } 3361364443cbSJan Kara 33628cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 33638cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 33648cd115bdSJan Kara struct iomap *srcmap) 33658cd115bdSJan Kara { 33668cd115bdSJan Kara int ret; 33678cd115bdSJan Kara 33688cd115bdSJan Kara /* 33698cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 33708cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 33718cd115bdSJan Kara */ 33728cd115bdSJan Kara flags &= ~IOMAP_WRITE; 33738cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 3374fa83c34eSBaokun Li WARN_ON_ONCE(!ret && iomap->type != IOMAP_MAPPED); 33758cd115bdSJan Kara return ret; 33768cd115bdSJan Kara } 33778cd115bdSJan Kara 3378776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3379776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3380776722e8SJan Kara { 3381378f32baSMatthew Bobrowski /* 3382378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3383378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3384378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3385378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3386378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3387378f32baSMatthew Bobrowski */ 3388378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3389378f32baSMatthew Bobrowski return -ENOTBLK; 3390378f32baSMatthew Bobrowski 3391776722e8SJan Kara return 0; 3392776722e8SJan Kara } 3393776722e8SJan Kara 33948ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3395364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3396776722e8SJan Kara .iomap_end = ext4_iomap_end, 3397364443cbSJan Kara }; 3398364443cbSJan Kara 33998cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 34008cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 34018cd115bdSJan Kara .iomap_end = ext4_iomap_end, 34028cd115bdSJan Kara }; 34038cd115bdSJan Kara 340409edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 340509edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 340609edf4d3SMatthew Bobrowski { 340709edf4d3SMatthew Bobrowski struct extent_status es; 340809edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 340909edf4d3SMatthew Bobrowski 341009edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 341109edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 341209edf4d3SMatthew Bobrowski 341309edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 341409edf4d3SMatthew Bobrowski return false; 341509edf4d3SMatthew Bobrowski 341609edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 341709edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 341809edf4d3SMatthew Bobrowski return false; 341909edf4d3SMatthew Bobrowski } 342009edf4d3SMatthew Bobrowski 342109edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 342209edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 342309edf4d3SMatthew Bobrowski 342409edf4d3SMatthew Bobrowski return true; 342509edf4d3SMatthew Bobrowski } 342609edf4d3SMatthew Bobrowski 342709edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 342809edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 342909edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 343009edf4d3SMatthew Bobrowski { 343109edf4d3SMatthew Bobrowski int ret; 343209edf4d3SMatthew Bobrowski bool delalloc = false; 343309edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 343409edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 343509edf4d3SMatthew Bobrowski 343609edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 343709edf4d3SMatthew Bobrowski return -EINVAL; 343809edf4d3SMatthew Bobrowski 34397cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 34407cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3441ba5843f5SJan Kara if (ret != -EAGAIN) { 3442ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3443ba5843f5SJan Kara ret = -ENOENT; 3444ba5843f5SJan Kara return ret; 3445ba5843f5SJan Kara } 3446ba5843f5SJan Kara } 344712735f88SJan Kara 344809edf4d3SMatthew Bobrowski /* 344909edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 345009edf4d3SMatthew Bobrowski */ 345109edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 345209edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 345309edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 345412735f88SJan Kara 3455b2c57642SRitesh Harjani /* 3456b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3457b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3458b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3459b2c57642SRitesh Harjani * -EIO error. 3460b2c57642SRitesh Harjani */ 3461b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3462b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3463b2c57642SRitesh Harjani 3464b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3465b2c57642SRitesh Harjani map.m_flags = 0; 3466b2c57642SRitesh Harjani goto set_iomap; 3467b2c57642SRitesh Harjani } 3468b2c57642SRitesh Harjani } 3469b2c57642SRitesh Harjani 347012735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3471ba5843f5SJan Kara if (ret < 0) 3472ba5843f5SJan Kara return ret; 3473914f82a3SJan Kara if (ret == 0) 347409edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 347509edf4d3SMatthew Bobrowski 3476b2c57642SRitesh Harjani set_iomap: 3477de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 347809edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 347909edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 348009edf4d3SMatthew Bobrowski 348109edf4d3SMatthew Bobrowski return 0; 3482914f82a3SJan Kara } 3483914f82a3SJan Kara 348409edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 348509edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 348609edf4d3SMatthew Bobrowski }; 34874c0425ffSMingming Cao 3488ac27a0ecSDave Kleikamp /* 34893f5d3063SJan Kara * For data=journal mode, folio should be marked dirty only when it was 34903f5d3063SJan Kara * writeably mapped. When that happens, it was already attached to the 34913f5d3063SJan Kara * transaction and marked as jbddirty (we take care of this in 34923f5d3063SJan Kara * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings 34933f5d3063SJan Kara * so we should have nothing to do here, except for the case when someone 34943f5d3063SJan Kara * had the page pinned and dirtied the page through this pin (e.g. by doing 34953f5d3063SJan Kara * direct IO to it). In that case we'd need to attach buffers here to the 34963f5d3063SJan Kara * transaction but we cannot due to lock ordering. We cannot just dirty the 34973f5d3063SJan Kara * folio and leave attached buffers clean, because the buffers' dirty state is 34983f5d3063SJan Kara * "definitive". We cannot just set the buffers dirty or jbddirty because all 34993f5d3063SJan Kara * the journalling code will explode. So what we do is to mark the folio 35003f5d3063SJan Kara * "pending dirty" and next time ext4_writepages() is called, attach buffers 35013f5d3063SJan Kara * to the transaction appropriately. 3502ac27a0ecSDave Kleikamp */ 3503187c82cbSMatthew Wilcox (Oracle) static bool ext4_journalled_dirty_folio(struct address_space *mapping, 3504187c82cbSMatthew Wilcox (Oracle) struct folio *folio) 3505ac27a0ecSDave Kleikamp { 35060f252336SMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_buffers(folio)); 35073f5d3063SJan Kara if (folio_maybe_dma_pinned(folio)) 3508187c82cbSMatthew Wilcox (Oracle) folio_set_checked(folio); 3509187c82cbSMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 3510ac27a0ecSDave Kleikamp } 3511ac27a0ecSDave Kleikamp 3512e621900aSMatthew Wilcox (Oracle) static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio) 35136dcc693bSJan Kara { 3514e621900aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio)); 3515e621900aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_buffers(folio)); 3516e621900aSMatthew Wilcox (Oracle) return block_dirty_folio(mapping, folio); 35176dcc693bSJan Kara } 35186dcc693bSJan Kara 35190e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 35200e6895baSRitesh Harjani struct file *file, sector_t *span) 35210e6895baSRitesh Harjani { 35220e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 35230e6895baSRitesh Harjani &ext4_iomap_report_ops); 35240e6895baSRitesh Harjani } 35250e6895baSRitesh Harjani 352674d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3527fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 35286311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 352920970ba6STheodore Ts'o .writepages = ext4_writepages, 3530bfc1af65SNick Piggin .write_begin = ext4_write_begin, 353174d553aaSTheodore Ts'o .write_end = ext4_write_end, 3532e621900aSMatthew Wilcox (Oracle) .dirty_folio = ext4_dirty_folio, 3533617ba13bSMingming Cao .bmap = ext4_bmap, 35347ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = ext4_invalidate_folio, 35353c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3536378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 353767235182SMatthew Wilcox (Oracle) .migrate_folio = buffer_migrate_folio, 35388ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3539aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 35400e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3541ac27a0ecSDave Kleikamp }; 3542ac27a0ecSDave Kleikamp 3543617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3544fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 35456311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 354620970ba6STheodore Ts'o .writepages = ext4_writepages, 3547bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3548bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3549187c82cbSMatthew Wilcox (Oracle) .dirty_folio = ext4_journalled_dirty_folio, 3550617ba13bSMingming Cao .bmap = ext4_bmap, 3551ccd16945SMatthew Wilcox (Oracle) .invalidate_folio = ext4_journalled_invalidate_folio, 35523c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3553378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3554dae99960SJan Kara .migrate_folio = buffer_migrate_folio_norefs, 35558ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3556aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 35570e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3558ac27a0ecSDave Kleikamp }; 3559ac27a0ecSDave Kleikamp 356064769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 3561fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 35626311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 356320970ba6STheodore Ts'o .writepages = ext4_writepages, 356464769240SAlex Tomas .write_begin = ext4_da_write_begin, 356564769240SAlex Tomas .write_end = ext4_da_write_end, 3566e621900aSMatthew Wilcox (Oracle) .dirty_folio = ext4_dirty_folio, 356764769240SAlex Tomas .bmap = ext4_bmap, 35687ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = ext4_invalidate_folio, 35693c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3570378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 357167235182SMatthew Wilcox (Oracle) .migrate_folio = buffer_migrate_folio, 35728ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3573aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 35740e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 357564769240SAlex Tomas }; 357664769240SAlex Tomas 35775f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 35785f0663bbSDan Williams .writepages = ext4_dax_writepages, 35795f0663bbSDan Williams .direct_IO = noop_direct_IO, 358046de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 358194dbb631SToshi Kani .bmap = ext4_bmap, 35820e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 35835f0663bbSDan Williams }; 35845f0663bbSDan Williams 3585617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3586ac27a0ecSDave Kleikamp { 35873d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 35883d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 35893d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 35903d2b1582SLukas Czerner break; 35913d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3592617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 359374d553aaSTheodore Ts'o return; 35943d2b1582SLukas Czerner default: 35953d2b1582SLukas Czerner BUG(); 35963d2b1582SLukas Czerner } 35975f0663bbSDan Williams if (IS_DAX(inode)) 35985f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 35995f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 360074d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 360174d553aaSTheodore Ts'o else 360274d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3603ac27a0ecSDave Kleikamp } 3604ac27a0ecSDave Kleikamp 3605923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3606d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3607d863dc36SLukas Czerner { 360809cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 360909cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3610923ae0ffSRoss Zwisler unsigned blocksize, pos; 3611d863dc36SLukas Czerner ext4_lblk_t iblock; 3612d863dc36SLukas Czerner struct inode *inode = mapping->host; 3613d863dc36SLukas Czerner struct buffer_head *bh; 36149d3973deSMatthew Wilcox struct folio *folio; 3615d863dc36SLukas Czerner int err = 0; 3616d863dc36SLukas Czerner 36179d3973deSMatthew Wilcox folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT, 36189d3973deSMatthew Wilcox FGP_LOCK | FGP_ACCESSED | FGP_CREAT, 3619c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 36207fa8a8eeSLinus Torvalds if (IS_ERR(folio)) 36217fa8a8eeSLinus Torvalds return PTR_ERR(folio); 3622d863dc36SLukas Czerner 3623d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3624d863dc36SLukas Czerner 362509cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3626d863dc36SLukas Czerner 36279d3973deSMatthew Wilcox bh = folio_buffers(folio); 36289d3973deSMatthew Wilcox if (!bh) { 36299d3973deSMatthew Wilcox create_empty_buffers(&folio->page, blocksize, 0); 36309d3973deSMatthew Wilcox bh = folio_buffers(folio); 36319d3973deSMatthew Wilcox } 3632d863dc36SLukas Czerner 3633d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3634d863dc36SLukas Czerner pos = blocksize; 3635d863dc36SLukas Czerner while (offset >= pos) { 3636d863dc36SLukas Czerner bh = bh->b_this_page; 3637d863dc36SLukas Czerner iblock++; 3638d863dc36SLukas Czerner pos += blocksize; 3639d863dc36SLukas Czerner } 3640d863dc36SLukas Czerner if (buffer_freed(bh)) { 3641d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3642d863dc36SLukas Czerner goto unlock; 3643d863dc36SLukas Czerner } 3644d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3645d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3646d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3647d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3648d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3649d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3650d863dc36SLukas Czerner goto unlock; 3651d863dc36SLukas Czerner } 3652d863dc36SLukas Czerner } 3653d863dc36SLukas Czerner 3654d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 36559d3973deSMatthew Wilcox if (folio_test_uptodate(folio)) 3656d863dc36SLukas Czerner set_buffer_uptodate(bh); 3657d863dc36SLukas Czerner 3658d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 36592d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 36602d069c08Szhangyi (F) if (err) 3661d863dc36SLukas Czerner goto unlock; 36624f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3663c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3664a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 36659d3973deSMatthew Wilcox err = fscrypt_decrypt_pagecache_blocks(folio, 366651e4e315SEric Biggers blocksize, 3667834f1565SEric Biggers bh_offset(bh)); 3668834f1565SEric Biggers if (err) { 3669834f1565SEric Biggers clear_buffer_uptodate(bh); 3670834f1565SEric Biggers goto unlock; 3671834f1565SEric Biggers } 3672c9c7429cSMichael Halcrow } 3673d863dc36SLukas Czerner } 3674d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3675d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3676188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 3677188c299eSJan Kara EXT4_JTR_NONE); 3678d863dc36SLukas Czerner if (err) 3679d863dc36SLukas Czerner goto unlock; 3680d863dc36SLukas Czerner } 36819d3973deSMatthew Wilcox folio_zero_range(folio, offset, length); 3682d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3683d863dc36SLukas Czerner 3684d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3685d84c9ebdSJan Kara err = ext4_dirty_journalled_data(handle, bh); 36860713ed0cSLukas Czerner } else { 3687353eefd3Sjon ernst err = 0; 3688d863dc36SLukas Czerner mark_buffer_dirty(bh); 36893957ef53SJan Kara if (ext4_should_order_data(inode)) 369073131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 369173131fbbSRoss Zwisler length); 36920713ed0cSLukas Czerner } 3693d863dc36SLukas Czerner 3694d863dc36SLukas Czerner unlock: 36959d3973deSMatthew Wilcox folio_unlock(folio); 36969d3973deSMatthew Wilcox folio_put(folio); 3697d863dc36SLukas Czerner return err; 3698d863dc36SLukas Czerner } 3699d863dc36SLukas Czerner 370094350ab5SMatthew Wilcox /* 3701923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3702923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3703923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3704923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 37053088e5a5SBhaskar Chowdhury * that corresponds to 'from' 3706923ae0ffSRoss Zwisler */ 3707923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3708923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3709923ae0ffSRoss Zwisler { 3710923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 371109cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3712923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3713923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3714923ae0ffSRoss Zwisler 3715923ae0ffSRoss Zwisler /* 3716923ae0ffSRoss Zwisler * correct length if it does not fall between 3717923ae0ffSRoss Zwisler * 'from' and the end of the block 3718923ae0ffSRoss Zwisler */ 3719923ae0ffSRoss Zwisler if (length > max || length < 0) 3720923ae0ffSRoss Zwisler length = max; 3721923ae0ffSRoss Zwisler 372247e69351SJan Kara if (IS_DAX(inode)) { 3723c6f40468SChristoph Hellwig return dax_zero_range(inode, from, length, NULL, 372447e69351SJan Kara &ext4_iomap_ops); 372547e69351SJan Kara } 3726923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3727923ae0ffSRoss Zwisler } 3728923ae0ffSRoss Zwisler 3729923ae0ffSRoss Zwisler /* 373094350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 373194350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 373294350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 373394350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 373494350ab5SMatthew Wilcox */ 3735c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 373694350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 373794350ab5SMatthew Wilcox { 373809cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 373994350ab5SMatthew Wilcox unsigned length; 374094350ab5SMatthew Wilcox unsigned blocksize; 374194350ab5SMatthew Wilcox struct inode *inode = mapping->host; 374294350ab5SMatthew Wilcox 37430d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3744592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 37450d06863fSTheodore Ts'o return 0; 37460d06863fSTheodore Ts'o 374794350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 374894350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 374994350ab5SMatthew Wilcox 375094350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 375194350ab5SMatthew Wilcox } 375294350ab5SMatthew Wilcox 3753a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3754a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3755a87dd18cSLukas Czerner { 3756a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3757a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3758e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3759a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3760a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3761a87dd18cSLukas Czerner int err = 0; 3762a87dd18cSLukas Czerner 3763e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3764e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3765e1be3a92SLukas Czerner 3766a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3767a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3768a87dd18cSLukas Czerner 3769a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3770e1be3a92SLukas Czerner if (start == end && 3771e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3772a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3773a87dd18cSLukas Czerner lstart, length); 3774a87dd18cSLukas Czerner return err; 3775a87dd18cSLukas Czerner } 3776a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3777e1be3a92SLukas Czerner if (partial_start) { 3778a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3779a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3780a87dd18cSLukas Czerner if (err) 3781a87dd18cSLukas Czerner return err; 3782a87dd18cSLukas Czerner } 3783a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3784e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3785a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3786e1be3a92SLukas Czerner byte_end - partial_end, 3787e1be3a92SLukas Czerner partial_end + 1); 3788a87dd18cSLukas Czerner return err; 3789a87dd18cSLukas Czerner } 3790a87dd18cSLukas Czerner 379191ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 379291ef4cafSDuane Griffin { 379391ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 379491ef4cafSDuane Griffin return 1; 379591ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 379691ef4cafSDuane Griffin return 1; 379791ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 379891ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 379991ef4cafSDuane Griffin return 0; 380091ef4cafSDuane Griffin } 380191ef4cafSDuane Griffin 3802ac27a0ecSDave Kleikamp /* 380301127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 380401127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 380501127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 380601127848SJan Kara * that will never happen after we truncate page cache. 380701127848SJan Kara */ 380801127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 380901127848SJan Kara loff_t len) 381001127848SJan Kara { 381101127848SJan Kara handle_t *handle; 38124209ae12SHarshad Shirwadkar int ret; 38134209ae12SHarshad Shirwadkar 381401127848SJan Kara loff_t size = i_size_read(inode); 381501127848SJan Kara 38165955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 381701127848SJan Kara if (offset > size || offset + len < size) 381801127848SJan Kara return 0; 381901127848SJan Kara 382001127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 382101127848SJan Kara return 0; 382201127848SJan Kara 382301127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 382401127848SJan Kara if (IS_ERR(handle)) 382501127848SJan Kara return PTR_ERR(handle); 382601127848SJan Kara ext4_update_i_disksize(inode, size); 38274209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 382801127848SJan Kara ext4_journal_stop(handle); 382901127848SJan Kara 38304209ae12SHarshad Shirwadkar return ret; 383101127848SJan Kara } 383201127848SJan Kara 3833d4f5258eSJan Kara static void ext4_wait_dax_page(struct inode *inode) 3834430657b6SRoss Zwisler { 3835d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 3836430657b6SRoss Zwisler schedule(); 3837d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 3838430657b6SRoss Zwisler } 3839430657b6SRoss Zwisler 3840430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3841430657b6SRoss Zwisler { 3842430657b6SRoss Zwisler struct page *page; 3843430657b6SRoss Zwisler int error; 3844430657b6SRoss Zwisler 3845d4f5258eSJan Kara if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock))) 3846430657b6SRoss Zwisler return -EINVAL; 3847430657b6SRoss Zwisler 3848430657b6SRoss Zwisler do { 3849430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3850430657b6SRoss Zwisler if (!page) 3851430657b6SRoss Zwisler return 0; 3852430657b6SRoss Zwisler 3853430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3854430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3855430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3856d4f5258eSJan Kara ext4_wait_dax_page(inode)); 3857b1f38217SRoss Zwisler } while (error == 0); 3858430657b6SRoss Zwisler 3859430657b6SRoss Zwisler return error; 3860430657b6SRoss Zwisler } 3861430657b6SRoss Zwisler 386201127848SJan Kara /* 3863cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3864a4bb6b64SAllison Henderson * associated with the given offset and length 3865a4bb6b64SAllison Henderson * 3866a4bb6b64SAllison Henderson * @inode: File inode 3867a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3868a4bb6b64SAllison Henderson * @len: The length of the hole 3869a4bb6b64SAllison Henderson * 38704907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3871a4bb6b64SAllison Henderson */ 3872a4bb6b64SAllison Henderson 3873ad5cd4f4SDarrick J. Wong int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3874a4bb6b64SAllison Henderson { 3875ad5cd4f4SDarrick J. Wong struct inode *inode = file_inode(file); 387626a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 387726a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 387826a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 38792da37622STadeusz Struk loff_t first_block_offset, last_block_offset, max_length; 38802da37622STadeusz Struk struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 388126a4c0c6STheodore Ts'o handle_t *handle; 388226a4c0c6STheodore Ts'o unsigned int credits; 38834209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 388426a4c0c6STheodore Ts'o 3885b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3886aaddea81SZheng Liu 388726a4c0c6STheodore Ts'o /* 388826a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 388926a4c0c6STheodore Ts'o * Then release them. 389026a4c0c6STheodore Ts'o */ 3891cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 389226a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 389326a4c0c6STheodore Ts'o offset + length - 1); 389426a4c0c6STheodore Ts'o if (ret) 389526a4c0c6STheodore Ts'o return ret; 389626a4c0c6STheodore Ts'o } 389726a4c0c6STheodore Ts'o 38985955102cSAl Viro inode_lock(inode); 38999ef06cecSLukas Czerner 390026a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 390126a4c0c6STheodore Ts'o if (offset >= inode->i_size) 390226a4c0c6STheodore Ts'o goto out_mutex; 390326a4c0c6STheodore Ts'o 390426a4c0c6STheodore Ts'o /* 390526a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 390626a4c0c6STheodore Ts'o * to end after the page that contains i_size 390726a4c0c6STheodore Ts'o */ 390826a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 390926a4c0c6STheodore Ts'o length = inode->i_size + 391009cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 391126a4c0c6STheodore Ts'o offset; 391226a4c0c6STheodore Ts'o } 391326a4c0c6STheodore Ts'o 39142da37622STadeusz Struk /* 39152da37622STadeusz Struk * For punch hole the length + offset needs to be within one block 39162da37622STadeusz Struk * before last range. Adjust the length if it goes beyond that limit. 39172da37622STadeusz Struk */ 39182da37622STadeusz Struk max_length = sbi->s_bitmap_maxbytes - inode->i_sb->s_blocksize; 39192da37622STadeusz Struk if (offset + length > max_length) 39202da37622STadeusz Struk length = max_length - offset; 39212da37622STadeusz Struk 3922a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 3923a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 3924a361293fSJan Kara /* 3925a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 3926a361293fSJan Kara * partial block 3927a361293fSJan Kara */ 3928a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 3929a361293fSJan Kara if (ret < 0) 3930a361293fSJan Kara goto out_mutex; 3931a361293fSJan Kara 3932a361293fSJan Kara } 3933a361293fSJan Kara 3934f340b3d9Shongnanli /* Wait all existing dio workers, newcomers will block on i_rwsem */ 3935ea3d7209SJan Kara inode_dio_wait(inode); 3936ea3d7209SJan Kara 3937ad5cd4f4SDarrick J. Wong ret = file_modified(file); 3938ad5cd4f4SDarrick J. Wong if (ret) 3939ad5cd4f4SDarrick J. Wong goto out_mutex; 3940ad5cd4f4SDarrick J. Wong 3941ea3d7209SJan Kara /* 3942ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 3943ea3d7209SJan Kara * page cache. 3944ea3d7209SJan Kara */ 3945d4f5258eSJan Kara filemap_invalidate_lock(mapping); 3946430657b6SRoss Zwisler 3947430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 3948430657b6SRoss Zwisler if (ret) 3949430657b6SRoss Zwisler goto out_dio; 3950430657b6SRoss Zwisler 3951a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 3952a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 395326a4c0c6STheodore Ts'o 3954a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 395501127848SJan Kara if (last_block_offset > first_block_offset) { 395601127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 395701127848SJan Kara if (ret) 395801127848SJan Kara goto out_dio; 3959a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 3960a87dd18cSLukas Czerner last_block_offset); 396101127848SJan Kara } 396226a4c0c6STheodore Ts'o 396326a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 396426a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 396526a4c0c6STheodore Ts'o else 396626a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 396726a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 396826a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 396926a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 397026a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 397126a4c0c6STheodore Ts'o goto out_dio; 397226a4c0c6STheodore Ts'o } 397326a4c0c6STheodore Ts'o 3974a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 3975a87dd18cSLukas Czerner length); 397626a4c0c6STheodore Ts'o if (ret) 397726a4c0c6STheodore Ts'o goto out_stop; 397826a4c0c6STheodore Ts'o 397926a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 398026a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 398126a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 398226a4c0c6STheodore Ts'o 3983eee597acSLukas Czerner /* If there are blocks to remove, do it */ 3984eee597acSLukas Czerner if (stop_block > first_block) { 398526a4c0c6STheodore Ts'o 398626a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 398727bc446eSbrookxu ext4_discard_preallocations(inode, 0); 398826a4c0c6STheodore Ts'o 3989*ed5d285bSBaokun Li ext4_es_remove_extent(inode, first_block, 399026a4c0c6STheodore Ts'o stop_block - first_block); 399126a4c0c6STheodore Ts'o 399226a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 399326a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 399426a4c0c6STheodore Ts'o stop_block - 1); 399526a4c0c6STheodore Ts'o else 39964f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 399726a4c0c6STheodore Ts'o stop_block); 399826a4c0c6STheodore Ts'o 3999819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4000eee597acSLukas Czerner } 4001a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 400226a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 400326a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4004e251f9bcSMaxim Patlasov 4005eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 40064209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 40074209ae12SHarshad Shirwadkar if (unlikely(ret2)) 40084209ae12SHarshad Shirwadkar ret = ret2; 400967a7d5f5SJan Kara if (ret >= 0) 401067a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 401126a4c0c6STheodore Ts'o out_stop: 401226a4c0c6STheodore Ts'o ext4_journal_stop(handle); 401326a4c0c6STheodore Ts'o out_dio: 4014d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 401526a4c0c6STheodore Ts'o out_mutex: 40165955102cSAl Viro inode_unlock(inode); 401726a4c0c6STheodore Ts'o return ret; 4018a4bb6b64SAllison Henderson } 4019a4bb6b64SAllison Henderson 4020a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4021a361293fSJan Kara { 4022a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4023a361293fSJan Kara struct jbd2_inode *jinode; 4024a361293fSJan Kara 4025a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4026a361293fSJan Kara return 0; 4027a361293fSJan Kara 4028a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4029a361293fSJan Kara spin_lock(&inode->i_lock); 4030a361293fSJan Kara if (!ei->jinode) { 4031a361293fSJan Kara if (!jinode) { 4032a361293fSJan Kara spin_unlock(&inode->i_lock); 4033a361293fSJan Kara return -ENOMEM; 4034a361293fSJan Kara } 4035a361293fSJan Kara ei->jinode = jinode; 4036a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4037a361293fSJan Kara jinode = NULL; 4038a361293fSJan Kara } 4039a361293fSJan Kara spin_unlock(&inode->i_lock); 4040a361293fSJan Kara if (unlikely(jinode != NULL)) 4041a361293fSJan Kara jbd2_free_inode(jinode); 4042a361293fSJan Kara return 0; 4043a361293fSJan Kara } 4044a361293fSJan Kara 4045a4bb6b64SAllison Henderson /* 4046617ba13bSMingming Cao * ext4_truncate() 4047ac27a0ecSDave Kleikamp * 4048617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4049617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4050ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4051ac27a0ecSDave Kleikamp * 405242b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4053ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4054ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4055ac27a0ecSDave Kleikamp * 4056ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4057ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4058ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4059ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4060ac27a0ecSDave Kleikamp * left-to-right works OK too). 4061ac27a0ecSDave Kleikamp * 4062ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4063ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4064ac27a0ecSDave Kleikamp * 4065ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4066617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4067ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4068617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4069617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4070ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4071617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4072ac27a0ecSDave Kleikamp */ 40732c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4074ac27a0ecSDave Kleikamp { 4075819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4076819c4920STheodore Ts'o unsigned int credits; 40774209ae12SHarshad Shirwadkar int err = 0, err2; 4078819c4920STheodore Ts'o handle_t *handle; 4079819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4080819c4920STheodore Ts'o 408119b5ef61STheodore Ts'o /* 408219b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4083e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 4084f340b3d9Shongnanli * have i_rwsem locked because it's not necessary. 408519b5ef61STheodore Ts'o */ 408619b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 40875955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 40880562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 40890562e0baSJiaying Zhang 409091ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 40919a5d265fSzhengliang goto out_trace; 4092ac27a0ecSDave Kleikamp 40935534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 409419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 40957d8f9f7dSTheodore Ts'o 4096aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4097aef1c851STao Ma int has_inline = 1; 4098aef1c851STao Ma 409901daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 41009a5d265fSzhengliang if (err || has_inline) 41019a5d265fSzhengliang goto out_trace; 4102aef1c851STao Ma } 4103aef1c851STao Ma 4104a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4105a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4106a71248b1SBaokun Li err = ext4_inode_attach_jinode(inode); 4107a71248b1SBaokun Li if (err) 41089a5d265fSzhengliang goto out_trace; 4109a361293fSJan Kara } 4110a361293fSJan Kara 4111ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4112819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4113ff9893dcSAmir Goldstein else 4114819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4115819c4920STheodore Ts'o 4116819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 41179a5d265fSzhengliang if (IS_ERR(handle)) { 41189a5d265fSzhengliang err = PTR_ERR(handle); 41199a5d265fSzhengliang goto out_trace; 41209a5d265fSzhengliang } 4121819c4920STheodore Ts'o 4122eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4123eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4124819c4920STheodore Ts'o 4125819c4920STheodore Ts'o /* 4126819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4127819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4128819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4129819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4130819c4920STheodore Ts'o * 4131819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4132819c4920STheodore Ts'o * truncatable state while each transaction commits. 4133819c4920STheodore Ts'o */ 41342c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 41352c98eb5eSTheodore Ts'o if (err) 4136819c4920STheodore Ts'o goto out_stop; 4137819c4920STheodore Ts'o 4138819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4139819c4920STheodore Ts'o 414027bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4141819c4920STheodore Ts'o 4142819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4143d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4144819c4920STheodore Ts'o else 4145819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4146819c4920STheodore Ts'o 4147819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4148d0abb36dSTheodore Ts'o if (err) 4149d0abb36dSTheodore Ts'o goto out_stop; 4150819c4920STheodore Ts'o 4151819c4920STheodore Ts'o if (IS_SYNC(inode)) 4152819c4920STheodore Ts'o ext4_handle_sync(handle); 4153819c4920STheodore Ts'o 4154819c4920STheodore Ts'o out_stop: 4155819c4920STheodore Ts'o /* 4156819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4157819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4158819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 415958d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4160819c4920STheodore Ts'o * orphan info for us. 4161819c4920STheodore Ts'o */ 4162819c4920STheodore Ts'o if (inode->i_nlink) 4163819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4164819c4920STheodore Ts'o 4165eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 41664209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 41674209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 41684209ae12SHarshad Shirwadkar err = err2; 4169819c4920STheodore Ts'o ext4_journal_stop(handle); 4170a86c6181SAlex Tomas 41719a5d265fSzhengliang out_trace: 41720562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 41732c98eb5eSTheodore Ts'o return err; 4174ac27a0ecSDave Kleikamp } 4175ac27a0ecSDave Kleikamp 41769a1bf32cSZhang Yi static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 41779a1bf32cSZhang Yi { 41789a1bf32cSZhang Yi if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 41799a1bf32cSZhang Yi return inode_peek_iversion_raw(inode); 41809a1bf32cSZhang Yi else 41819a1bf32cSZhang Yi return inode_peek_iversion(inode); 41829a1bf32cSZhang Yi } 41839a1bf32cSZhang Yi 41849a1bf32cSZhang Yi static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, 41859a1bf32cSZhang Yi struct ext4_inode_info *ei) 41869a1bf32cSZhang Yi { 41879a1bf32cSZhang Yi struct inode *inode = &(ei->vfs_inode); 41889a1bf32cSZhang Yi u64 i_blocks = READ_ONCE(inode->i_blocks); 41899a1bf32cSZhang Yi struct super_block *sb = inode->i_sb; 41909a1bf32cSZhang Yi 41919a1bf32cSZhang Yi if (i_blocks <= ~0U) { 41929a1bf32cSZhang Yi /* 41939a1bf32cSZhang Yi * i_blocks can be represented in a 32 bit variable 41949a1bf32cSZhang Yi * as multiple of 512 bytes 41959a1bf32cSZhang Yi */ 41969a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 41979a1bf32cSZhang Yi raw_inode->i_blocks_high = 0; 41989a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 41999a1bf32cSZhang Yi return 0; 42009a1bf32cSZhang Yi } 42019a1bf32cSZhang Yi 42029a1bf32cSZhang Yi /* 42039a1bf32cSZhang Yi * This should never happen since sb->s_maxbytes should not have 42049a1bf32cSZhang Yi * allowed this, sb->s_maxbytes was set according to the huge_file 42059a1bf32cSZhang Yi * feature in ext4_fill_super(). 42069a1bf32cSZhang Yi */ 42079a1bf32cSZhang Yi if (!ext4_has_feature_huge_file(sb)) 42089a1bf32cSZhang Yi return -EFSCORRUPTED; 42099a1bf32cSZhang Yi 42109a1bf32cSZhang Yi if (i_blocks <= 0xffffffffffffULL) { 42119a1bf32cSZhang Yi /* 42129a1bf32cSZhang Yi * i_blocks can be represented in a 48 bit variable 42139a1bf32cSZhang Yi * as multiple of 512 bytes 42149a1bf32cSZhang Yi */ 42159a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42169a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42179a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42189a1bf32cSZhang Yi } else { 42199a1bf32cSZhang Yi ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42209a1bf32cSZhang Yi /* i_block is stored in file system block size */ 42219a1bf32cSZhang Yi i_blocks = i_blocks >> (inode->i_blkbits - 9); 42229a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42239a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42249a1bf32cSZhang Yi } 42259a1bf32cSZhang Yi return 0; 42269a1bf32cSZhang Yi } 42279a1bf32cSZhang Yi 42289a1bf32cSZhang Yi static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) 42299a1bf32cSZhang Yi { 42309a1bf32cSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 42319a1bf32cSZhang Yi uid_t i_uid; 42329a1bf32cSZhang Yi gid_t i_gid; 42339a1bf32cSZhang Yi projid_t i_projid; 42349a1bf32cSZhang Yi int block; 42359a1bf32cSZhang Yi int err; 42369a1bf32cSZhang Yi 42379a1bf32cSZhang Yi err = ext4_inode_blocks_set(raw_inode, ei); 42389a1bf32cSZhang Yi 42399a1bf32cSZhang Yi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 42409a1bf32cSZhang Yi i_uid = i_uid_read(inode); 42419a1bf32cSZhang Yi i_gid = i_gid_read(inode); 42429a1bf32cSZhang Yi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 42439a1bf32cSZhang Yi if (!(test_opt(inode->i_sb, NO_UID32))) { 42449a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 42459a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 42469a1bf32cSZhang Yi /* 42479a1bf32cSZhang Yi * Fix up interoperability with old kernels. Otherwise, 42489a1bf32cSZhang Yi * old inodes get re-used with the upper 16 bits of the 42499a1bf32cSZhang Yi * uid/gid intact. 42509a1bf32cSZhang Yi */ 42519a1bf32cSZhang Yi if (ei->i_dtime && list_empty(&ei->i_orphan)) { 42529a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 42539a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 42549a1bf32cSZhang Yi } else { 42559a1bf32cSZhang Yi raw_inode->i_uid_high = 42569a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_uid)); 42579a1bf32cSZhang Yi raw_inode->i_gid_high = 42589a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_gid)); 42599a1bf32cSZhang Yi } 42609a1bf32cSZhang Yi } else { 42619a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 42629a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 42639a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 42649a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 42659a1bf32cSZhang Yi } 42669a1bf32cSZhang Yi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 42679a1bf32cSZhang Yi 42689a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 42699a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 42709a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 42719a1bf32cSZhang Yi EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 42729a1bf32cSZhang Yi 42739a1bf32cSZhang Yi raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 42749a1bf32cSZhang Yi raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 42759a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 42769a1bf32cSZhang Yi raw_inode->i_file_acl_high = 42779a1bf32cSZhang Yi cpu_to_le16(ei->i_file_acl >> 32); 42789a1bf32cSZhang Yi raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 42799a1bf32cSZhang Yi ext4_isize_set(raw_inode, ei->i_disksize); 42809a1bf32cSZhang Yi 42819a1bf32cSZhang Yi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 42829a1bf32cSZhang Yi if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 42839a1bf32cSZhang Yi if (old_valid_dev(inode->i_rdev)) { 42849a1bf32cSZhang Yi raw_inode->i_block[0] = 42859a1bf32cSZhang Yi cpu_to_le32(old_encode_dev(inode->i_rdev)); 42869a1bf32cSZhang Yi raw_inode->i_block[1] = 0; 42879a1bf32cSZhang Yi } else { 42889a1bf32cSZhang Yi raw_inode->i_block[0] = 0; 42899a1bf32cSZhang Yi raw_inode->i_block[1] = 42909a1bf32cSZhang Yi cpu_to_le32(new_encode_dev(inode->i_rdev)); 42919a1bf32cSZhang Yi raw_inode->i_block[2] = 0; 42929a1bf32cSZhang Yi } 42939a1bf32cSZhang Yi } else if (!ext4_has_inline_data(inode)) { 42949a1bf32cSZhang Yi for (block = 0; block < EXT4_N_BLOCKS; block++) 42959a1bf32cSZhang Yi raw_inode->i_block[block] = ei->i_data[block]; 42969a1bf32cSZhang Yi } 42979a1bf32cSZhang Yi 42989a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 42999a1bf32cSZhang Yi u64 ivers = ext4_inode_peek_iversion(inode); 43009a1bf32cSZhang Yi 43019a1bf32cSZhang Yi raw_inode->i_disk_version = cpu_to_le32(ivers); 43029a1bf32cSZhang Yi if (ei->i_extra_isize) { 43039a1bf32cSZhang Yi if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 43049a1bf32cSZhang Yi raw_inode->i_version_hi = 43059a1bf32cSZhang Yi cpu_to_le32(ivers >> 32); 43069a1bf32cSZhang Yi raw_inode->i_extra_isize = 43079a1bf32cSZhang Yi cpu_to_le16(ei->i_extra_isize); 43089a1bf32cSZhang Yi } 43099a1bf32cSZhang Yi } 43109a1bf32cSZhang Yi 43119a1bf32cSZhang Yi if (i_projid != EXT4_DEF_PROJID && 43129a1bf32cSZhang Yi !ext4_has_feature_project(inode->i_sb)) 43139a1bf32cSZhang Yi err = err ?: -EFSCORRUPTED; 43149a1bf32cSZhang Yi 43159a1bf32cSZhang Yi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 43169a1bf32cSZhang Yi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 43179a1bf32cSZhang Yi raw_inode->i_projid = cpu_to_le32(i_projid); 43189a1bf32cSZhang Yi 43199a1bf32cSZhang Yi ext4_inode_csum_set(inode, raw_inode, ei); 43209a1bf32cSZhang Yi return err; 43219a1bf32cSZhang Yi } 43229a1bf32cSZhang Yi 4323ac27a0ecSDave Kleikamp /* 4324617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4325de01f484SZhang Yi * underlying buffer_head on success. If we pass 'inode' and it does not 4326de01f484SZhang Yi * have in-inode xattr, we have all inode data in memory that is needed 4327de01f484SZhang Yi * to recreate the on-disk version of this inode. 4328ac27a0ecSDave Kleikamp */ 43298016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 4330de01f484SZhang Yi struct inode *inode, struct ext4_iloc *iloc, 43318016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4332ac27a0ecSDave Kleikamp { 4333240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4334ac27a0ecSDave Kleikamp struct buffer_head *bh; 4335240799cdSTheodore Ts'o ext4_fsblk_t block; 433602f03c42SLinus Torvalds struct blk_plug plug; 4337240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4338ac27a0ecSDave Kleikamp 43393a06d778SAneesh Kumar K.V iloc->bh = NULL; 43408016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 43418016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 43426a797d27SDarrick J. Wong return -EFSCORRUPTED; 4343ac27a0ecSDave Kleikamp 43448016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4345240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4346240799cdSTheodore Ts'o if (!gdp) 4347240799cdSTheodore Ts'o return -EIO; 4348240799cdSTheodore Ts'o 4349240799cdSTheodore Ts'o /* 4350240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4351240799cdSTheodore Ts'o */ 435200d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 43538016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4354240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4355240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4356240799cdSTheodore Ts'o 4357eee22187SBaokun Li block = ext4_inode_table(sb, gdp); 4358eee22187SBaokun Li if ((block <= le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) || 4359eee22187SBaokun Li (block >= ext4_blocks_count(EXT4_SB(sb)->s_es))) { 4360eee22187SBaokun Li ext4_error(sb, "Invalid inode table block %llu in " 4361eee22187SBaokun Li "block_group %u", block, iloc->block_group); 4362eee22187SBaokun Li return -EFSCORRUPTED; 4363eee22187SBaokun Li } 4364eee22187SBaokun Li block += (inode_offset / inodes_per_block); 4365eee22187SBaokun Li 4366240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4367aebf0243SWang Shilong if (unlikely(!bh)) 4368860d21e2STheodore Ts'o return -ENOMEM; 43698e33fadfSZhang Yi if (ext4_buffer_uptodate(bh)) 4370ac27a0ecSDave Kleikamp goto has_buffer; 4371ac27a0ecSDave Kleikamp 43728e33fadfSZhang Yi lock_buffer(bh); 4373f2c77973SZhang Yi if (ext4_buffer_uptodate(bh)) { 4374f2c77973SZhang Yi /* Someone brought it uptodate while we waited */ 4375f2c77973SZhang Yi unlock_buffer(bh); 4376f2c77973SZhang Yi goto has_buffer; 4377f2c77973SZhang Yi } 4378f2c77973SZhang Yi 4379ac27a0ecSDave Kleikamp /* 4380ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4381ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4382ac27a0ecSDave Kleikamp * block. 4383ac27a0ecSDave Kleikamp */ 4384de01f484SZhang Yi if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 4385ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4386240799cdSTheodore Ts'o int i, start; 4387ac27a0ecSDave Kleikamp 4388240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4389ac27a0ecSDave Kleikamp 4390ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4391240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4392aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4393ac27a0ecSDave Kleikamp goto make_io; 4394ac27a0ecSDave Kleikamp 4395ac27a0ecSDave Kleikamp /* 4396ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4397ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4398ac27a0ecSDave Kleikamp * of one, so skip it. 4399ac27a0ecSDave Kleikamp */ 4400ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4401ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4402ac27a0ecSDave Kleikamp goto make_io; 4403ac27a0ecSDave Kleikamp } 4404240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4405ac27a0ecSDave Kleikamp if (i == inode_offset) 4406ac27a0ecSDave Kleikamp continue; 4407617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4408ac27a0ecSDave Kleikamp break; 4409ac27a0ecSDave Kleikamp } 4410ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4411240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4412de01f484SZhang Yi struct ext4_inode *raw_inode = 4413de01f484SZhang Yi (struct ext4_inode *) (bh->b_data + iloc->offset); 4414de01f484SZhang Yi 4415ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4416ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4417de01f484SZhang Yi if (!ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4418de01f484SZhang Yi ext4_fill_raw_inode(inode, raw_inode); 4419ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4420ac27a0ecSDave Kleikamp unlock_buffer(bh); 4421ac27a0ecSDave Kleikamp goto has_buffer; 4422ac27a0ecSDave Kleikamp } 4423ac27a0ecSDave Kleikamp } 4424ac27a0ecSDave Kleikamp 4425ac27a0ecSDave Kleikamp make_io: 4426ac27a0ecSDave Kleikamp /* 4427240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4428240799cdSTheodore Ts'o * blocks from the inode table. 4429240799cdSTheodore Ts'o */ 443002f03c42SLinus Torvalds blk_start_plug(&plug); 4431240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4432240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4433240799cdSTheodore Ts'o unsigned num; 44340d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4435240799cdSTheodore Ts'o 4436240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4437b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 44380d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4439240799cdSTheodore Ts'o if (table > b) 4440240799cdSTheodore Ts'o b = table; 44410d606e2cSTheodore Ts'o end = b + ra_blks; 4442240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4443feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4444560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4445240799cdSTheodore Ts'o table += num / inodes_per_block; 4446240799cdSTheodore Ts'o if (end > table) 4447240799cdSTheodore Ts'o end = table; 4448240799cdSTheodore Ts'o while (b <= end) 44495df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4450240799cdSTheodore Ts'o } 4451240799cdSTheodore Ts'o 4452240799cdSTheodore Ts'o /* 4453ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4454ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4455ac27a0ecSDave Kleikamp * Read the block from disk. 4456ac27a0ecSDave Kleikamp */ 44578016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 44582d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 445902f03c42SLinus Torvalds blk_finish_plug(&plug); 4460ac27a0ecSDave Kleikamp wait_on_buffer(bh); 44610904c9aeSZhang Yi ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4462ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 44638016e29fSHarshad Shirwadkar if (ret_block) 44648016e29fSHarshad Shirwadkar *ret_block = block; 4465ac27a0ecSDave Kleikamp brelse(bh); 4466ac27a0ecSDave Kleikamp return -EIO; 4467ac27a0ecSDave Kleikamp } 4468ac27a0ecSDave Kleikamp has_buffer: 4469ac27a0ecSDave Kleikamp iloc->bh = bh; 4470ac27a0ecSDave Kleikamp return 0; 4471ac27a0ecSDave Kleikamp } 4472ac27a0ecSDave Kleikamp 44738016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 44748016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 44758016e29fSHarshad Shirwadkar { 4476c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 44778016e29fSHarshad Shirwadkar int ret; 44788016e29fSHarshad Shirwadkar 4479de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc, 44808016e29fSHarshad Shirwadkar &err_blk); 44818016e29fSHarshad Shirwadkar 44828016e29fSHarshad Shirwadkar if (ret == -EIO) 44838016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 44848016e29fSHarshad Shirwadkar "unable to read itable block"); 44858016e29fSHarshad Shirwadkar 44868016e29fSHarshad Shirwadkar return ret; 44878016e29fSHarshad Shirwadkar } 44888016e29fSHarshad Shirwadkar 4489617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4490ac27a0ecSDave Kleikamp { 4491c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 44928016e29fSHarshad Shirwadkar int ret; 44938016e29fSHarshad Shirwadkar 4494de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc, 4495de01f484SZhang Yi &err_blk); 44968016e29fSHarshad Shirwadkar 44978016e29fSHarshad Shirwadkar if (ret == -EIO) 44988016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 44998016e29fSHarshad Shirwadkar "unable to read itable block"); 45008016e29fSHarshad Shirwadkar 45018016e29fSHarshad Shirwadkar return ret; 45028016e29fSHarshad Shirwadkar } 45038016e29fSHarshad Shirwadkar 45048016e29fSHarshad Shirwadkar 45058016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 45068016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45078016e29fSHarshad Shirwadkar { 4508de01f484SZhang Yi return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL); 4509ac27a0ecSDave Kleikamp } 4510ac27a0ecSDave Kleikamp 4511a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 45126642586bSRoss Zwisler { 4513a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4514a8ab6d38SIra Weiny 45159cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 45166642586bSRoss Zwisler return false; 45176642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 45186642586bSRoss Zwisler return false; 45196642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 45206642586bSRoss Zwisler return false; 45216642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 45226642586bSRoss Zwisler return false; 4523592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 45246642586bSRoss Zwisler return false; 4525c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4526c93d8f88SEric Biggers return false; 4527a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4528a8ab6d38SIra Weiny return false; 4529a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 45306642586bSRoss Zwisler return true; 4531a8ab6d38SIra Weiny 4532b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 45336642586bSRoss Zwisler } 45346642586bSRoss Zwisler 4535043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4536ac27a0ecSDave Kleikamp { 4537617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 453800a1a053STheodore Ts'o unsigned int new_fl = 0; 4539ac27a0ecSDave Kleikamp 4540043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4541043546e4SIra Weiny 4542617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 454300a1a053STheodore Ts'o new_fl |= S_SYNC; 4544617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 454500a1a053STheodore Ts'o new_fl |= S_APPEND; 4546617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 454700a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4548617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 454900a1a053STheodore Ts'o new_fl |= S_NOATIME; 4550617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 455100a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4552043546e4SIra Weiny 4553043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4554043546e4SIra Weiny * here if already set. */ 4555043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4556043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4557923ae0ffSRoss Zwisler new_fl |= S_DAX; 4558043546e4SIra Weiny 45592ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 45602ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4561b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4562b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4563c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4564c93d8f88SEric Biggers new_fl |= S_VERITY; 45655f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 45662ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4567c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4568ac27a0ecSDave Kleikamp } 4569ac27a0ecSDave Kleikamp 45700fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 45710fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 45720fc1b451SAneesh Kumar K.V { 45730fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 45748180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 45758180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 45760fc1b451SAneesh Kumar K.V 4577e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 45780fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 45790fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 45800fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 458107a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 45828180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 45838180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 45848180a562SAneesh Kumar K.V } else { 45850fc1b451SAneesh Kumar K.V return i_blocks; 45868180a562SAneesh Kumar K.V } 45870fc1b451SAneesh Kumar K.V } else { 45880fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 45890fc1b451SAneesh Kumar K.V } 45900fc1b451SAneesh Kumar K.V } 4591ff9ddf7eSJan Kara 4592eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4593152a7b0aSTao Ma struct ext4_inode *raw_inode, 4594152a7b0aSTao Ma struct ext4_inode_info *ei) 4595152a7b0aSTao Ma { 4596152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4597152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4598eb9b5f01STheodore Ts'o 4599fd7e672eSBaokun Li if (EXT4_INODE_HAS_XATTR_SPACE(inode) && 4600290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 46011dcdce59SYe Bin int err; 46021dcdce59SYe Bin 4603152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 46041dcdce59SYe Bin err = ext4_find_inline_data_nolock(inode); 46051dcdce59SYe Bin if (!err && ext4_has_inline_data(inode)) 46061dcdce59SYe Bin ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 46071dcdce59SYe Bin return err; 4608f19d5870STao Ma } else 4609f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4610eb9b5f01STheodore Ts'o return 0; 4611152a7b0aSTao Ma } 4612152a7b0aSTao Ma 4613040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4614040cb378SLi Xi { 46150b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4616040cb378SLi Xi return -EOPNOTSUPP; 4617040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4618040cb378SLi Xi return 0; 4619040cb378SLi Xi } 4620040cb378SLi Xi 4621e254d1afSEryu Guan /* 4622e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4623e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4624e254d1afSEryu Guan * set. 4625e254d1afSEryu Guan */ 4626e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4627e254d1afSEryu Guan { 4628e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4629e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4630e254d1afSEryu Guan else 4631e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4632e254d1afSEryu Guan } 4633e254d1afSEryu Guan 4634b3e6bcb9STheodore Ts'o static const char *check_igot_inode(struct inode *inode, ext4_iget_flags flags) 4635b3e6bcb9STheodore Ts'o 4636b3e6bcb9STheodore Ts'o { 4637b3e6bcb9STheodore Ts'o if (flags & EXT4_IGET_EA_INODE) { 4638b3e6bcb9STheodore Ts'o if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4639b3e6bcb9STheodore Ts'o return "missing EA_INODE flag"; 46402bc7e7c1STheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 46412bc7e7c1STheodore Ts'o EXT4_I(inode)->i_file_acl) 46422bc7e7c1STheodore Ts'o return "ea_inode with extended attributes"; 4643b3e6bcb9STheodore Ts'o } else { 4644b3e6bcb9STheodore Ts'o if ((EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4645b3e6bcb9STheodore Ts'o return "unexpected EA_INODE flag"; 4646b3e6bcb9STheodore Ts'o } 4647b3e6bcb9STheodore Ts'o if (is_bad_inode(inode) && !(flags & EXT4_IGET_BAD)) 4648b3e6bcb9STheodore Ts'o return "unexpected bad inode w/o EXT4_IGET_BAD"; 4649b3e6bcb9STheodore Ts'o return NULL; 4650b3e6bcb9STheodore Ts'o } 4651b3e6bcb9STheodore Ts'o 46528a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 46538a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 46548a363970STheodore Ts'o unsigned int line) 4655ac27a0ecSDave Kleikamp { 4656617ba13bSMingming Cao struct ext4_iloc iloc; 4657617ba13bSMingming Cao struct ext4_inode *raw_inode; 46581d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4659bd2c38cfSJan Kara struct ext4_super_block *es = EXT4_SB(sb)->s_es; 46601d1fe1eeSDavid Howells struct inode *inode; 4661b3e6bcb9STheodore Ts'o const char *err_str; 4662b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 46631d1fe1eeSDavid Howells long ret; 46647e6e1ef4SDarrick J. Wong loff_t size; 4665ac27a0ecSDave Kleikamp int block; 466608cefc7aSEric W. Biederman uid_t i_uid; 466708cefc7aSEric W. Biederman gid_t i_gid; 4668040cb378SLi Xi projid_t i_projid; 4669ac27a0ecSDave Kleikamp 4670191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 4671bd2c38cfSJan Kara ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4672bd2c38cfSJan Kara ino == le32_to_cpu(es->s_usr_quota_inum) || 4673bd2c38cfSJan Kara ino == le32_to_cpu(es->s_grp_quota_inum) || 467402f310fcSJan Kara ino == le32_to_cpu(es->s_prj_quota_inum) || 467502f310fcSJan Kara ino == le32_to_cpu(es->s_orphan_file_inum))) || 46768a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 4677bd2c38cfSJan Kara (ino > le32_to_cpu(es->s_inodes_count))) { 46788a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 46798a363970STheodore Ts'o return ERR_PTR(-ESTALE); 4680014c9caaSJan Kara __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 46818a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 46828a363970STheodore Ts'o ino, current->comm); 46838a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 46848a363970STheodore Ts'o } 46858a363970STheodore Ts'o 46861d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 46871d1fe1eeSDavid Howells if (!inode) 46881d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 4689b3e6bcb9STheodore Ts'o if (!(inode->i_state & I_NEW)) { 4690b3e6bcb9STheodore Ts'o if ((err_str = check_igot_inode(inode, flags)) != NULL) { 4691b3e6bcb9STheodore Ts'o ext4_error_inode(inode, function, line, 0, err_str); 4692b3e6bcb9STheodore Ts'o iput(inode); 4693b3e6bcb9STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 4694b3e6bcb9STheodore Ts'o } 46951d1fe1eeSDavid Howells return inode; 4696b3e6bcb9STheodore Ts'o } 46971d1fe1eeSDavid Howells 46981d1fe1eeSDavid Howells ei = EXT4_I(inode); 46997dc57615SPeter Huewe iloc.bh = NULL; 4700ac27a0ecSDave Kleikamp 47018016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 47021d1fe1eeSDavid Howells if (ret < 0) 4703ac27a0ecSDave Kleikamp goto bad_inode; 4704617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4705814525f4SDarrick J. Wong 47068a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 47078a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 47088a363970STheodore Ts'o ret = -ESTALE; 47098a363970STheodore Ts'o goto bad_inode; 47108a363970STheodore Ts'o } 47118a363970STheodore Ts'o 4712814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4713814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4714814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 47152dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 47162dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 47178a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47188a363970STheodore Ts'o "iget: bad extra_isize %u " 47198a363970STheodore Ts'o "(inode size %u)", 47202dc8d9e1SEric Biggers ei->i_extra_isize, 4721814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 47226a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4723814525f4SDarrick J. Wong goto bad_inode; 4724814525f4SDarrick J. Wong } 4725814525f4SDarrick J. Wong } else 4726814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4727814525f4SDarrick J. Wong 4728814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 47299aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4730814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4731814525f4SDarrick J. Wong __u32 csum; 4732814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4733814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4734814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4735814525f4SDarrick J. Wong sizeof(inum)); 4736814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4737814525f4SDarrick J. Wong sizeof(gen)); 4738814525f4SDarrick J. Wong } 4739814525f4SDarrick J. Wong 47408016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 47418016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 47428016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 47438016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 47448016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 47456a797d27SDarrick J. Wong ret = -EFSBADCRC; 4746814525f4SDarrick J. Wong goto bad_inode; 4747814525f4SDarrick J. Wong } 4748814525f4SDarrick J. Wong 4749ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 475008cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 475108cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 47520b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4753040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4754040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4755040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4756040cb378SLi Xi else 4757040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4758040cb378SLi Xi 4759ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 476008cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 476108cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4762ac27a0ecSDave Kleikamp } 476308cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 476408cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4765040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4766bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4767ac27a0ecSDave Kleikamp 4768353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 476967cf5b09STao Ma ei->i_inline_off = 0; 4770ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4771ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4772ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4773ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4774ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4775ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4776ac27a0ecSDave Kleikamp */ 4777ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 47785cd74028SBaokun Li if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL || 4779393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4780393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 47815cd74028SBaokun Li /* this inode is deleted or unallocated */ 47825cd74028SBaokun Li if (flags & EXT4_IGET_SPECIAL) { 47835cd74028SBaokun Li ext4_error_inode(inode, function, line, 0, 47845cd74028SBaokun Li "iget: special inode unallocated"); 47855cd74028SBaokun Li ret = -EFSCORRUPTED; 47865cd74028SBaokun Li } else 47871d1fe1eeSDavid Howells ret = -ESTALE; 4788ac27a0ecSDave Kleikamp goto bad_inode; 4789ac27a0ecSDave Kleikamp } 4790ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4791ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4792ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4793393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4794393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4795393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4796ac27a0ecSDave Kleikamp } 4797ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4798043546e4SIra Weiny ext4_set_inode_flags(inode, true); 47990fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 48007973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4801e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4802a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4803a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4804e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 48057e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 48068a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48078a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 48087e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 48097e6e1ef4SDarrick J. Wong goto bad_inode; 48107e6e1ef4SDarrick J. Wong } 481148a34311SJan Kara /* 481248a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 481348a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 481448a34311SJan Kara * checksumming that corrupts checksums so forbid that. 481548a34311SJan Kara */ 481648a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 481748a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 481848a34311SJan Kara ext4_error_inode(inode, function, line, 0, 481948a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 482048a34311SJan Kara ret = -EFSCORRUPTED; 482148a34311SJan Kara goto bad_inode; 482248a34311SJan Kara } 4823ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4824a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4825a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4826a9e7f447SDmitry Monakhov #endif 4827ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4828ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4829a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4830ac27a0ecSDave Kleikamp /* 4831ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4832ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4833ac27a0ecSDave Kleikamp */ 4834617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4835ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4836ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4837aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4838ac27a0ecSDave Kleikamp 4839b436b9beSJan Kara /* 4840b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4841b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4842b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4843b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4844b436b9beSJan Kara * now it is reread from disk. 4845b436b9beSJan Kara */ 4846b436b9beSJan Kara if (journal) { 4847b436b9beSJan Kara transaction_t *transaction; 4848b436b9beSJan Kara tid_t tid; 4849b436b9beSJan Kara 4850a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4851b436b9beSJan Kara if (journal->j_running_transaction) 4852b436b9beSJan Kara transaction = journal->j_running_transaction; 4853b436b9beSJan Kara else 4854b436b9beSJan Kara transaction = journal->j_committing_transaction; 4855b436b9beSJan Kara if (transaction) 4856b436b9beSJan Kara tid = transaction->t_tid; 4857b436b9beSJan Kara else 4858b436b9beSJan Kara tid = journal->j_commit_sequence; 4859a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4860b436b9beSJan Kara ei->i_sync_tid = tid; 4861b436b9beSJan Kara ei->i_datasync_tid = tid; 4862b436b9beSJan Kara } 4863b436b9beSJan Kara 48640040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4865ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4866ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 48672dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4868617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4869617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4870ac27a0ecSDave Kleikamp } else { 4871eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4872eb9b5f01STheodore Ts'o if (ret) 4873eb9b5f01STheodore Ts'o goto bad_inode; 4874ac27a0ecSDave Kleikamp } 4875814525f4SDarrick J. Wong } 4876ac27a0ecSDave Kleikamp 4877ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4878ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4879ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4880ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4881ef7f3835SKalpak Shah 4882ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4883ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4884ee73f9a5SJeff Layton 488525ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 488625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4887ee73f9a5SJeff Layton ivers |= 488825ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 488925ec56b5SJean Noel Cordenner } 4890e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4891c4f65706STheodore Ts'o } 489225ec56b5SJean Noel Cordenner 4893c4b5a614STheodore Ts'o ret = 0; 4894485c26ecSTheodore Ts'o if (ei->i_file_acl && 4895ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 48968a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48978a363970STheodore Ts'o "iget: bad extended attribute block %llu", 489824676da4STheodore Ts'o ei->i_file_acl); 48996a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4900485c26ecSTheodore Ts'o goto bad_inode; 4901f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4902bc716523SLiu Song /* validate the block references in the inode */ 49038016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 49048016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4905fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 49068016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 4907bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4908bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4909bc716523SLiu Song else 49101f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4911fe2c8191SThiemo Nagel } 4912f19d5870STao Ma } 4913567f3e9aSTheodore Ts'o if (ret) 49147a262f7cSAneesh Kumar K.V goto bad_inode; 49157a262f7cSAneesh Kumar K.V 4916ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4917617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4918617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4919617ba13bSMingming Cao ext4_set_aops(inode); 4920ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4921617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4922617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4923ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 49246390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 49256390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 49268a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49278a363970STheodore Ts'o "iget: immutable or append flags " 49288a363970STheodore Ts'o "not allowed on symlinks"); 49296390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 49306390d33bSLuis R. Rodriguez goto bad_inode; 49316390d33bSLuis R. Rodriguez } 4932592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4933a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4934a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 493575e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4936617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4937e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4938e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4939e83c1397SDuane Griffin } else { 4940617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4941ac27a0ecSDave Kleikamp } 4942563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4943563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4944617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4945ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4946ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4947ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4948ac27a0ecSDave Kleikamp else 4949ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4950ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4951393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4952393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4953563bdd61STheodore Ts'o } else { 49546a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 49558a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49568a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4957563bdd61STheodore Ts'o goto bad_inode; 4958ac27a0ecSDave Kleikamp } 49596456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 49606456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49616456ca65STheodore Ts'o "casefold flag without casefold feature"); 4962b3e6bcb9STheodore Ts'o if ((err_str = check_igot_inode(inode, flags)) != NULL) { 4963b3e6bcb9STheodore Ts'o ext4_error_inode(inode, function, line, 0, err_str); 4964b3e6bcb9STheodore Ts'o ret = -EFSCORRUPTED; 496563b1e9bcSBaokun Li goto bad_inode; 496663b1e9bcSBaokun Li } 4967dec214d0STahsin Erdogan 496863b1e9bcSBaokun Li brelse(iloc.bh); 49691d1fe1eeSDavid Howells unlock_new_inode(inode); 49701d1fe1eeSDavid Howells return inode; 4971ac27a0ecSDave Kleikamp 4972ac27a0ecSDave Kleikamp bad_inode: 4973567f3e9aSTheodore Ts'o brelse(iloc.bh); 49741d1fe1eeSDavid Howells iget_failed(inode); 49751d1fe1eeSDavid Howells return ERR_PTR(ret); 4976ac27a0ecSDave Kleikamp } 4977ac27a0ecSDave Kleikamp 49783f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 49793f19b2abSDavid Howells unsigned long orig_ino, 49803f19b2abSDavid Howells unsigned long ino, 49813f19b2abSDavid Howells struct ext4_inode *raw_inode) 4982a26f4992STheodore Ts'o { 49833f19b2abSDavid Howells struct inode *inode; 4984a26f4992STheodore Ts'o 49853f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 49863f19b2abSDavid Howells if (!inode) 49873f19b2abSDavid Howells return; 49883f19b2abSDavid Howells 4989ed296c6cSEric Biggers if (!inode_is_dirtytime_only(inode)) 49903f19b2abSDavid Howells return; 49913f19b2abSDavid Howells 4992a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4993ed296c6cSEric Biggers if (inode_is_dirtytime_only(inode)) { 4994a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4995a26f4992STheodore Ts'o 49965fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 4997a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4998a26f4992STheodore Ts'o 4999a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 50003f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 50013f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 50023f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 50033f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 5004a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 50053f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 50063f19b2abSDavid Howells return; 5007a26f4992STheodore Ts'o } 5008a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5009a26f4992STheodore Ts'o } 5010a26f4992STheodore Ts'o 5011a26f4992STheodore Ts'o /* 5012a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 5013a26f4992STheodore Ts'o * the same inode table block. 5014a26f4992STheodore Ts'o */ 5015a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 5016a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 5017a26f4992STheodore Ts'o { 5018a26f4992STheodore Ts'o unsigned long ino; 5019a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 5020a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 5021a26f4992STheodore Ts'o 50220f0ff9a9STheodore Ts'o /* 50230f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 50240f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 50250f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 50260f0ff9a9STheodore Ts'o */ 50270f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 50283f19b2abSDavid Howells rcu_read_lock(); 5029a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5030a26f4992STheodore Ts'o if (ino == orig_ino) 5031a26f4992STheodore Ts'o continue; 50323f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 50333f19b2abSDavid Howells (struct ext4_inode *)buf); 5034a26f4992STheodore Ts'o } 50353f19b2abSDavid Howells rcu_read_unlock(); 5036a26f4992STheodore Ts'o } 5037a26f4992STheodore Ts'o 5038664bd38bSZhang Yi /* 5039664bd38bSZhang Yi * Post the struct inode info into an on-disk inode location in the 5040664bd38bSZhang Yi * buffer-cache. This gobbles the caller's reference to the 5041664bd38bSZhang Yi * buffer_head in the inode location struct. 5042664bd38bSZhang Yi * 5043664bd38bSZhang Yi * The caller must have write access to iloc->bh. 5044664bd38bSZhang Yi */ 5045664bd38bSZhang Yi static int ext4_do_update_inode(handle_t *handle, 5046664bd38bSZhang Yi struct inode *inode, 5047664bd38bSZhang Yi struct ext4_iloc *iloc) 5048664bd38bSZhang Yi { 5049664bd38bSZhang Yi struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5050664bd38bSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 5051664bd38bSZhang Yi struct buffer_head *bh = iloc->bh; 5052664bd38bSZhang Yi struct super_block *sb = inode->i_sb; 5053664bd38bSZhang Yi int err; 5054664bd38bSZhang Yi int need_datasync = 0, set_large_file = 0; 5055664bd38bSZhang Yi 5056664bd38bSZhang Yi spin_lock(&ei->i_raw_lock); 5057664bd38bSZhang Yi 5058664bd38bSZhang Yi /* 5059664bd38bSZhang Yi * For fields not tracked in the in-memory inode, initialise them 5060664bd38bSZhang Yi * to zero for new inodes. 5061664bd38bSZhang Yi */ 5062664bd38bSZhang Yi if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5063664bd38bSZhang Yi memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5064664bd38bSZhang Yi 5065664bd38bSZhang Yi if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) 5066664bd38bSZhang Yi need_datasync = 1; 5067664bd38bSZhang Yi if (ei->i_disksize > 0x7fffffffULL) { 5068664bd38bSZhang Yi if (!ext4_has_feature_large_file(sb) || 5069664bd38bSZhang Yi EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) 5070664bd38bSZhang Yi set_large_file = 1; 5071664bd38bSZhang Yi } 5072664bd38bSZhang Yi 5073664bd38bSZhang Yi err = ext4_fill_raw_inode(inode, raw_inode); 5074202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 5075baaae979SZhang Yi if (err) { 5076baaae979SZhang Yi EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5077baaae979SZhang Yi goto out_brelse; 5078baaae979SZhang Yi } 5079baaae979SZhang Yi 50801751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5081a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5082a26f4992STheodore Ts'o bh->b_data); 5083202ee5dfSTheodore Ts'o 50840390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 50857d8bd3c7SShijie Luo err = ext4_handle_dirty_metadata(handle, NULL, bh); 50867d8bd3c7SShijie Luo if (err) 5087baaae979SZhang Yi goto out_error; 508819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5089202ee5dfSTheodore Ts'o if (set_large_file) { 50905d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5091188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, 5092188c299eSJan Kara EXT4_SB(sb)->s_sbh, 5093188c299eSJan Kara EXT4_JTR_NONE); 5094202ee5dfSTheodore Ts'o if (err) 5095baaae979SZhang Yi goto out_error; 509605c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 5097e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 509805c2c00fSJan Kara ext4_superblock_csum_set(sb); 509905c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 5100202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5101a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, 5102a3f5cf14SJan Kara EXT4_SB(sb)->s_sbh); 5103202ee5dfSTheodore Ts'o } 5104b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5105baaae979SZhang Yi out_error: 5106baaae979SZhang Yi ext4_std_error(inode->i_sb, err); 5107ac27a0ecSDave Kleikamp out_brelse: 5108ac27a0ecSDave Kleikamp brelse(bh); 5109ac27a0ecSDave Kleikamp return err; 5110ac27a0ecSDave Kleikamp } 5111ac27a0ecSDave Kleikamp 5112ac27a0ecSDave Kleikamp /* 5113617ba13bSMingming Cao * ext4_write_inode() 5114ac27a0ecSDave Kleikamp * 5115ac27a0ecSDave Kleikamp * We are called from a few places: 5116ac27a0ecSDave Kleikamp * 511787f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5118ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51194907cb7bSAnatol Pomozov * transaction to commit. 5120ac27a0ecSDave Kleikamp * 512187f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 512287f7e416STheodore Ts'o * We wait on commit, if told to. 5123ac27a0ecSDave Kleikamp * 512487f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 512587f7e416STheodore Ts'o * We wait on commit, if told to. 5126ac27a0ecSDave Kleikamp * 5127ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5128ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 512987f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 513087f7e416STheodore Ts'o * writeback. 5131ac27a0ecSDave Kleikamp * 5132ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5133ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5134ac27a0ecSDave Kleikamp * which we are interested. 5135ac27a0ecSDave Kleikamp * 5136ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5137ac27a0ecSDave Kleikamp * 5138ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5139ac27a0ecSDave Kleikamp * stuff(); 5140ac27a0ecSDave Kleikamp * inode->i_size = expr; 5141ac27a0ecSDave Kleikamp * 514287f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 514387f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 514487f7e416STheodore Ts'o * superblock's dirty inode list. 5145ac27a0ecSDave Kleikamp */ 5146a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5147ac27a0ecSDave Kleikamp { 514891ac6f43SFrank Mayhar int err; 514991ac6f43SFrank Mayhar 515018f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 515118f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5152ac27a0ecSDave Kleikamp return 0; 5153ac27a0ecSDave Kleikamp 515418f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 515518f2c4fcSTheodore Ts'o return -EIO; 515618f2c4fcSTheodore Ts'o 515791ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5158617ba13bSMingming Cao if (ext4_journal_current_handle()) { 51594978c659SJan Kara ext4_debug("called recursively, non-PF_MEMALLOC!\n"); 5160ac27a0ecSDave Kleikamp dump_stack(); 5161ac27a0ecSDave Kleikamp return -EIO; 5162ac27a0ecSDave Kleikamp } 5163ac27a0ecSDave Kleikamp 516410542c22SJan Kara /* 516510542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 516610542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 516710542c22SJan Kara * written. 516810542c22SJan Kara */ 516910542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5170ac27a0ecSDave Kleikamp return 0; 5171ac27a0ecSDave Kleikamp 5172aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 517318f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 517491ac6f43SFrank Mayhar } else { 517591ac6f43SFrank Mayhar struct ext4_iloc iloc; 517691ac6f43SFrank Mayhar 51778016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 517891ac6f43SFrank Mayhar if (err) 517991ac6f43SFrank Mayhar return err; 518010542c22SJan Kara /* 518110542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 518210542c22SJan Kara * it here separately for each inode. 518310542c22SJan Kara */ 518410542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5185830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5186830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 518754d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5188c398eda0STheodore Ts'o "IO error syncing inode"); 5189830156c7SFrank Mayhar err = -EIO; 5190830156c7SFrank Mayhar } 5191fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 519291ac6f43SFrank Mayhar } 519391ac6f43SFrank Mayhar return err; 5194ac27a0ecSDave Kleikamp } 5195ac27a0ecSDave Kleikamp 5196ac27a0ecSDave Kleikamp /* 5197ccd16945SMatthew Wilcox (Oracle) * In data=journal mode ext4_journalled_invalidate_folio() may fail to invalidate 5198ccd16945SMatthew Wilcox (Oracle) * buffers that are attached to a folio straddling i_size and are undergoing 519953e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 520053e87268SJan Kara */ 520153e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 520253e87268SJan Kara { 520353e87268SJan Kara unsigned offset; 520453e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 520553e87268SJan Kara tid_t commit_tid = 0; 520653e87268SJan Kara int ret; 520753e87268SJan Kara 520809cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 520953e87268SJan Kara /* 5210ccd16945SMatthew Wilcox (Oracle) * If the folio is fully truncated, we don't need to wait for any commit 5211ccd16945SMatthew Wilcox (Oracle) * (and we even should not as __ext4_journalled_invalidate_folio() may 5212ccd16945SMatthew Wilcox (Oracle) * strip all buffers from the folio but keep the folio dirty which can then 52133f079114SJan Kara * confuse e.g. concurrent ext4_writepages() seeing dirty folio without 5214565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5215ccd16945SMatthew Wilcox (Oracle) * the folio remain valid. This is most beneficial for the common case of 5216565333a1Syangerkun * blocksize == PAGESIZE. 521753e87268SJan Kara */ 5218565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 521953e87268SJan Kara return; 522053e87268SJan Kara while (1) { 5221ccd16945SMatthew Wilcox (Oracle) struct folio *folio = filemap_lock_folio(inode->i_mapping, 522209cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 522366dabbb6SChristoph Hellwig if (IS_ERR(folio)) 522453e87268SJan Kara return; 5225ccd16945SMatthew Wilcox (Oracle) ret = __ext4_journalled_invalidate_folio(folio, offset, 5226ccd16945SMatthew Wilcox (Oracle) folio_size(folio) - offset); 5227ccd16945SMatthew Wilcox (Oracle) folio_unlock(folio); 5228ccd16945SMatthew Wilcox (Oracle) folio_put(folio); 522953e87268SJan Kara if (ret != -EBUSY) 523053e87268SJan Kara return; 523153e87268SJan Kara commit_tid = 0; 523253e87268SJan Kara read_lock(&journal->j_state_lock); 523353e87268SJan Kara if (journal->j_committing_transaction) 523453e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 523553e87268SJan Kara read_unlock(&journal->j_state_lock); 523653e87268SJan Kara if (commit_tid) 523753e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 523853e87268SJan Kara } 523953e87268SJan Kara } 524053e87268SJan Kara 524153e87268SJan Kara /* 5242617ba13bSMingming Cao * ext4_setattr() 5243ac27a0ecSDave Kleikamp * 5244ac27a0ecSDave Kleikamp * Called from notify_change. 5245ac27a0ecSDave Kleikamp * 5246ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5247ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5248ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5249ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5250ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5251ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5252ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5253ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5254ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5255ac27a0ecSDave Kleikamp * 5256678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5257678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5258678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5259678aaf48SJan Kara * This way we are sure that all the data written in the previous 5260678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5261678aaf48SJan Kara * writeback). 5262678aaf48SJan Kara * 5263f340b3d9Shongnanli * Called with inode->i_rwsem down. 5264ac27a0ecSDave Kleikamp */ 5265c1632a0fSChristian Brauner int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 5266549c7297SChristian Brauner struct iattr *attr) 5267ac27a0ecSDave Kleikamp { 52682b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5269ac27a0ecSDave Kleikamp int error, rc = 0; 52703d287de3SDmitry Monakhov int orphan = 0; 5271ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5272a642c2c0SJeff Layton bool inc_ivers = true; 5273ac27a0ecSDave Kleikamp 52740db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 52750db1ff22STheodore Ts'o return -EIO; 52760db1ff22STheodore Ts'o 527702b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 527802b016caSTheodore Ts'o return -EPERM; 527902b016caSTheodore Ts'o 528002b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 528102b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 528202b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 528302b016caSTheodore Ts'o return -EPERM; 528402b016caSTheodore Ts'o 5285c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, attr); 5286ac27a0ecSDave Kleikamp if (error) 5287ac27a0ecSDave Kleikamp return error; 5288ac27a0ecSDave Kleikamp 52893ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 52903ce2b8ddSEric Biggers if (error) 52913ce2b8ddSEric Biggers return error; 52923ce2b8ddSEric Biggers 5293c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5294c93d8f88SEric Biggers if (error) 5295c93d8f88SEric Biggers return error; 5296c93d8f88SEric Biggers 5297f861646aSChristian Brauner if (is_quota_modification(idmap, inode, attr)) { 5298a7cdadeeSJan Kara error = dquot_initialize(inode); 5299a7cdadeeSJan Kara if (error) 5300a7cdadeeSJan Kara return error; 5301a7cdadeeSJan Kara } 53022729cfdcSHarshad Shirwadkar 53030dbe12f2SChristian Brauner if (i_uid_needs_update(idmap, attr, inode) || 53040dbe12f2SChristian Brauner i_gid_needs_update(idmap, attr, inode)) { 5305ac27a0ecSDave Kleikamp handle_t *handle; 5306ac27a0ecSDave Kleikamp 5307ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5308ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53099924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53109924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5311194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5312ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5313ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5314ac27a0ecSDave Kleikamp goto err_out; 5315ac27a0ecSDave Kleikamp } 53167a9ca53aSTahsin Erdogan 53177a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53187a9ca53aSTahsin Erdogan * counts xattr inode references. 53197a9ca53aSTahsin Erdogan */ 53207a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5321f861646aSChristian Brauner error = dquot_transfer(idmap, inode, attr); 53227a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53237a9ca53aSTahsin Erdogan 5324ac27a0ecSDave Kleikamp if (error) { 5325617ba13bSMingming Cao ext4_journal_stop(handle); 5326ac27a0ecSDave Kleikamp return error; 5327ac27a0ecSDave Kleikamp } 5328ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5329ac27a0ecSDave Kleikamp * one transaction */ 53300dbe12f2SChristian Brauner i_uid_update(idmap, attr, inode); 53310dbe12f2SChristian Brauner i_gid_update(idmap, attr, inode); 5332617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5333617ba13bSMingming Cao ext4_journal_stop(handle); 5334512c15efSPan Bian if (unlikely(error)) { 53354209ae12SHarshad Shirwadkar return error; 5336ac27a0ecSDave Kleikamp } 5337512c15efSPan Bian } 5338ac27a0ecSDave Kleikamp 53393da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53405208386cSJan Kara handle_t *handle; 53413da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5342f4534c9fSYe Bin loff_t old_disksize; 5343b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5344562c72aaSChristoph Hellwig 534512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5346e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5347e2b46574SEric Sandeen 5348aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 53490c095c7fSTheodore Ts'o return -EFBIG; 5350e2b46574SEric Sandeen } 5351aa75f4d3SHarshad Shirwadkar } 5352aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 53533da40c7bSJosef Bacik return -EINVAL; 5354aa75f4d3SHarshad Shirwadkar } 5355dff6efc3SChristoph Hellwig 5356a642c2c0SJeff Layton if (attr->ia_size == inode->i_size) 5357a642c2c0SJeff Layton inc_ivers = false; 5358dff6efc3SChristoph Hellwig 5359b9c1c267SJan Kara if (shrink) { 5360b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 53615208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 53625208386cSJan Kara attr->ia_size); 53635208386cSJan Kara if (error) 53645208386cSJan Kara goto err_out; 53655208386cSJan Kara } 5366b9c1c267SJan Kara /* 5367b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5368b9c1c267SJan Kara * for dio in flight. 5369b9c1c267SJan Kara */ 5370b9c1c267SJan Kara inode_dio_wait(inode); 5371b9c1c267SJan Kara } 5372b9c1c267SJan Kara 5373d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 5374b9c1c267SJan Kara 5375b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5376b9c1c267SJan Kara if (rc) { 5377d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 5378aa75f4d3SHarshad Shirwadkar goto err_out; 5379b9c1c267SJan Kara } 5380b9c1c267SJan Kara 53813da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 53829924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5383ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5384ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5385b9c1c267SJan Kara goto out_mmap_sem; 5386ac27a0ecSDave Kleikamp } 53873da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5388617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 53893d287de3SDmitry Monakhov orphan = 1; 53903d287de3SDmitry Monakhov } 5391911af577SEryu Guan /* 5392911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5393911af577SEryu Guan * update c/mtime in shrink case below 5394911af577SEryu Guan */ 5395911af577SEryu Guan if (!shrink) { 5396eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5397911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5398911af577SEryu Guan } 5399aa75f4d3SHarshad Shirwadkar 5400aa75f4d3SHarshad Shirwadkar if (shrink) 5401a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, 5402aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5403aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 54049725958bSXin Yin EXT_MAX_BLOCKS - 1); 5405aa75f4d3SHarshad Shirwadkar else 5406aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5407a80f7fcfSHarshad Shirwadkar handle, inode, 5408aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5409aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5410aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5411aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5412aa75f4d3SHarshad Shirwadkar 541390e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5414f4534c9fSYe Bin old_disksize = EXT4_I(inode)->i_disksize; 5415617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5416617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5417ac27a0ecSDave Kleikamp if (!error) 5418ac27a0ecSDave Kleikamp error = rc; 541990e775b7SJan Kara /* 542090e775b7SJan Kara * We have to update i_size under i_data_sem together 542190e775b7SJan Kara * with i_disksize to avoid races with writeback code 542290e775b7SJan Kara * running ext4_wb_update_i_disksize(). 542390e775b7SJan Kara */ 542490e775b7SJan Kara if (!error) 542590e775b7SJan Kara i_size_write(inode, attr->ia_size); 5426f4534c9fSYe Bin else 5427f4534c9fSYe Bin EXT4_I(inode)->i_disksize = old_disksize; 542890e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5429617ba13bSMingming Cao ext4_journal_stop(handle); 5430b9c1c267SJan Kara if (error) 5431b9c1c267SJan Kara goto out_mmap_sem; 543282a25b02SJan Kara if (!shrink) { 5433b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5434b9c1c267SJan Kara inode->i_size); 5435b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 543682a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5437b9c1c267SJan Kara } 5438430657b6SRoss Zwisler } 5439430657b6SRoss Zwisler 544053e87268SJan Kara /* 544153e87268SJan Kara * Truncate pagecache after we've waited for commit 544253e87268SJan Kara * in data=journal mode to make pages freeable. 544353e87268SJan Kara */ 54447caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5445b9c1c267SJan Kara /* 5446b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5447b9c1c267SJan Kara * truncate possible preallocated blocks. 5448b9c1c267SJan Kara */ 5449b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54502c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54512c98eb5eSTheodore Ts'o if (rc) 54522c98eb5eSTheodore Ts'o error = rc; 54532c98eb5eSTheodore Ts'o } 5454b9c1c267SJan Kara out_mmap_sem: 5455d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 54563da40c7bSJosef Bacik } 5457ac27a0ecSDave Kleikamp 54582c98eb5eSTheodore Ts'o if (!error) { 5459a642c2c0SJeff Layton if (inc_ivers) 5460a642c2c0SJeff Layton inode_inc_iversion(inode); 5461c1632a0fSChristian Brauner setattr_copy(idmap, inode, attr); 54621025774cSChristoph Hellwig mark_inode_dirty(inode); 54631025774cSChristoph Hellwig } 54641025774cSChristoph Hellwig 54651025774cSChristoph Hellwig /* 54661025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 54671025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 54681025774cSChristoph Hellwig */ 54693d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5470617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5471ac27a0ecSDave Kleikamp 54722c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 547313e83a49SChristian Brauner rc = posix_acl_chmod(idmap, dentry, inode->i_mode); 5474ac27a0ecSDave Kleikamp 5475ac27a0ecSDave Kleikamp err_out: 5476aa75f4d3SHarshad Shirwadkar if (error) 5477617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5478ac27a0ecSDave Kleikamp if (!error) 5479ac27a0ecSDave Kleikamp error = rc; 5480ac27a0ecSDave Kleikamp return error; 5481ac27a0ecSDave Kleikamp } 5482ac27a0ecSDave Kleikamp 54838434ef1dSEric Biggers u32 ext4_dio_alignment(struct inode *inode) 54848434ef1dSEric Biggers { 54858434ef1dSEric Biggers if (fsverity_active(inode)) 54868434ef1dSEric Biggers return 0; 54878434ef1dSEric Biggers if (ext4_should_journal_data(inode)) 54888434ef1dSEric Biggers return 0; 54898434ef1dSEric Biggers if (ext4_has_inline_data(inode)) 54908434ef1dSEric Biggers return 0; 54918434ef1dSEric Biggers if (IS_ENCRYPTED(inode)) { 54928434ef1dSEric Biggers if (!fscrypt_dio_supported(inode)) 54938434ef1dSEric Biggers return 0; 54948434ef1dSEric Biggers return i_blocksize(inode); 54958434ef1dSEric Biggers } 54968434ef1dSEric Biggers return 1; /* use the iomap defaults */ 54978434ef1dSEric Biggers } 54988434ef1dSEric Biggers 5499b74d24f7SChristian Brauner int ext4_getattr(struct mnt_idmap *idmap, const struct path *path, 5500549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags) 55013e3398a0SMingming Cao { 550299652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 550399652ea5SDavid Howells struct ext4_inode *raw_inode; 550499652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 550599652ea5SDavid Howells unsigned int flags; 55063e3398a0SMingming Cao 5507d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5508d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 550999652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 551099652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 551199652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 551299652ea5SDavid Howells } 551399652ea5SDavid Howells 55148434ef1dSEric Biggers /* 55158434ef1dSEric Biggers * Return the DIO alignment restrictions if requested. We only return 55168434ef1dSEric Biggers * this information when requested, since on encrypted files it might 55178434ef1dSEric Biggers * take a fair bit of work to get if the file wasn't opened recently. 55188434ef1dSEric Biggers */ 55198434ef1dSEric Biggers if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) { 55208434ef1dSEric Biggers u32 dio_align = ext4_dio_alignment(inode); 55218434ef1dSEric Biggers 55228434ef1dSEric Biggers stat->result_mask |= STATX_DIOALIGN; 55238434ef1dSEric Biggers if (dio_align == 1) { 55248434ef1dSEric Biggers struct block_device *bdev = inode->i_sb->s_bdev; 55258434ef1dSEric Biggers 55268434ef1dSEric Biggers /* iomap defaults */ 55278434ef1dSEric Biggers stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; 55288434ef1dSEric Biggers stat->dio_offset_align = bdev_logical_block_size(bdev); 55298434ef1dSEric Biggers } else { 55308434ef1dSEric Biggers stat->dio_mem_align = dio_align; 55318434ef1dSEric Biggers stat->dio_offset_align = dio_align; 55328434ef1dSEric Biggers } 55338434ef1dSEric Biggers } 55348434ef1dSEric Biggers 553599652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 553699652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 553799652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 553899652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 553999652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 554099652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 554199652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 554299652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 554399652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 554499652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 554599652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 55461f607195SEric Biggers if (flags & EXT4_VERITY_FL) 55471f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 554899652ea5SDavid Howells 55493209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 55503209f68bSDavid Howells STATX_ATTR_COMPRESSED | 55513209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 55523209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 55531f607195SEric Biggers STATX_ATTR_NODUMP | 55541f607195SEric Biggers STATX_ATTR_VERITY); 55553209f68bSDavid Howells 5556b74d24f7SChristian Brauner generic_fillattr(idmap, inode, stat); 555799652ea5SDavid Howells return 0; 555899652ea5SDavid Howells } 555999652ea5SDavid Howells 5560b74d24f7SChristian Brauner int ext4_file_getattr(struct mnt_idmap *idmap, 5561549c7297SChristian Brauner const struct path *path, struct kstat *stat, 556299652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 556399652ea5SDavid Howells { 556499652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 556599652ea5SDavid Howells u64 delalloc_blocks; 556699652ea5SDavid Howells 5567b74d24f7SChristian Brauner ext4_getattr(idmap, path, stat, request_mask, query_flags); 55683e3398a0SMingming Cao 55693e3398a0SMingming Cao /* 55709206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 55719206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 55729206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5573d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 55749206c561SAndreas Dilger */ 55759206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 55769206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 55779206c561SAndreas Dilger 55789206c561SAndreas Dilger /* 55793e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 55803e3398a0SMingming Cao * otherwise in the case of system crash before the real block 55813e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 55823e3398a0SMingming Cao * on-disk file blocks. 55833e3398a0SMingming Cao * We always keep i_blocks updated together with real 55843e3398a0SMingming Cao * allocation. But to not confuse with user, stat 55853e3398a0SMingming Cao * will return the blocks that include the delayed allocation 55863e3398a0SMingming Cao * blocks for this file. 55873e3398a0SMingming Cao */ 558896607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 558996607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55908af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55913e3398a0SMingming Cao return 0; 55923e3398a0SMingming Cao } 5593ac27a0ecSDave Kleikamp 5594fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5595fffb2739SJan Kara int pextents) 5596a02908f1SMingming Cao { 559712e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5598fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5599fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5600a02908f1SMingming Cao } 5601ac51d837STheodore Ts'o 5602a02908f1SMingming Cao /* 5603a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5604a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5605a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5606a02908f1SMingming Cao * 5607a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 56084907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5609a02908f1SMingming Cao * they could still across block group boundary. 5610a02908f1SMingming Cao * 5611a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5612a02908f1SMingming Cao */ 5613dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5614fffb2739SJan Kara int pextents) 5615a02908f1SMingming Cao { 56168df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56178df9675fSTheodore Ts'o int gdpblocks; 5618a02908f1SMingming Cao int idxblocks; 56197fc51f92SXU pengfei int ret; 5620a02908f1SMingming Cao 5621a02908f1SMingming Cao /* 5622fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5623fffb2739SJan Kara * to @pextents physical extents? 5624a02908f1SMingming Cao */ 5625fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5626a02908f1SMingming Cao 5627a02908f1SMingming Cao ret = idxblocks; 5628a02908f1SMingming Cao 5629a02908f1SMingming Cao /* 5630a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5631a02908f1SMingming Cao * to account 5632a02908f1SMingming Cao */ 5633fffb2739SJan Kara groups = idxblocks + pextents; 5634a02908f1SMingming Cao gdpblocks = groups; 56358df9675fSTheodore Ts'o if (groups > ngroups) 56368df9675fSTheodore Ts'o groups = ngroups; 5637a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5638a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5639a02908f1SMingming Cao 5640a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5641a02908f1SMingming Cao ret += groups + gdpblocks; 5642a02908f1SMingming Cao 5643a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5644a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5645ac27a0ecSDave Kleikamp 5646ac27a0ecSDave Kleikamp return ret; 5647ac27a0ecSDave Kleikamp } 5648ac27a0ecSDave Kleikamp 5649ac27a0ecSDave Kleikamp /* 565025985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5651f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5652f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5653a02908f1SMingming Cao * 5654525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5655a02908f1SMingming Cao * 5656525f4ed8SMingming Cao * We need to consider the worse case, when 5657a02908f1SMingming Cao * one new block per extent. 5658a02908f1SMingming Cao */ 5659a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5660a02908f1SMingming Cao { 5661a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5662a02908f1SMingming Cao int ret; 5663a02908f1SMingming Cao 5664fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5665a02908f1SMingming Cao 5666a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5667a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5668a02908f1SMingming Cao ret += bpp; 5669a02908f1SMingming Cao return ret; 5670a02908f1SMingming Cao } 5671f3bd1f3fSMingming Cao 5672f3bd1f3fSMingming Cao /* 5673f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5674f3bd1f3fSMingming Cao * 5675f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 567679e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5677f3bd1f3fSMingming Cao * 5678f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5679f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5680f3bd1f3fSMingming Cao */ 5681f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5682f3bd1f3fSMingming Cao { 5683f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5684f3bd1f3fSMingming Cao } 5685f3bd1f3fSMingming Cao 5686a02908f1SMingming Cao /* 5687617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5688ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5689ac27a0ecSDave Kleikamp */ 5690617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5691617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5692ac27a0ecSDave Kleikamp { 5693ac27a0ecSDave Kleikamp int err = 0; 5694ac27a0ecSDave Kleikamp 5695a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5696a6758309SVasily Averin put_bh(iloc->bh); 56970db1ff22STheodore Ts'o return -EIO; 5698a6758309SVasily Averin } 5699a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5700aa75f4d3SHarshad Shirwadkar 5701ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5702ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5703ac27a0ecSDave Kleikamp 5704dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5705830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5706ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5707ac27a0ecSDave Kleikamp return err; 5708ac27a0ecSDave Kleikamp } 5709ac27a0ecSDave Kleikamp 5710ac27a0ecSDave Kleikamp /* 5711ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5712ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5713ac27a0ecSDave Kleikamp */ 5714ac27a0ecSDave Kleikamp 5715ac27a0ecSDave Kleikamp int 5716617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5717617ba13bSMingming Cao struct ext4_iloc *iloc) 5718ac27a0ecSDave Kleikamp { 57190390131bSFrank Mayhar int err; 57200390131bSFrank Mayhar 57210db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 57220db1ff22STheodore Ts'o return -EIO; 57230db1ff22STheodore Ts'o 5724617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5725ac27a0ecSDave Kleikamp if (!err) { 5726ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5727188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, 5728188c299eSJan Kara iloc->bh, EXT4_JTR_NONE); 5729ac27a0ecSDave Kleikamp if (err) { 5730ac27a0ecSDave Kleikamp brelse(iloc->bh); 5731ac27a0ecSDave Kleikamp iloc->bh = NULL; 5732ac27a0ecSDave Kleikamp } 5733ac27a0ecSDave Kleikamp } 5734617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5735ac27a0ecSDave Kleikamp return err; 5736ac27a0ecSDave Kleikamp } 5737ac27a0ecSDave Kleikamp 5738c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5739c03b45b8SMiao Xie unsigned int new_extra_isize, 5740c03b45b8SMiao Xie struct ext4_iloc *iloc, 5741c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5742c03b45b8SMiao Xie { 5743c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5744c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 57454ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 57464ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5747c03b45b8SMiao Xie int error; 5748c03b45b8SMiao Xie 57494ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 57504ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 57514ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 57524ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 57534ea99936STheodore Ts'o ei->i_extra_isize, 57544ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 57554ea99936STheodore Ts'o return -EFSCORRUPTED; 57564ea99936STheodore Ts'o } 57574ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 57584ea99936STheodore Ts'o (new_extra_isize < 4) || 57594ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 57604ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 57614ea99936STheodore Ts'o 5762c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5763c03b45b8SMiao Xie 5764c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5765c03b45b8SMiao Xie 5766c03b45b8SMiao Xie /* No extended attributes present */ 5767c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5768c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5769c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5770c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5771c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5772c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5773c03b45b8SMiao Xie return 0; 5774c03b45b8SMiao Xie } 5775c03b45b8SMiao Xie 57768994d113SJan Kara /* 57778994d113SJan Kara * We may need to allocate external xattr block so we need quotas 57788994d113SJan Kara * initialized. Here we can be called with various locks held so we 57798994d113SJan Kara * cannot affort to initialize quotas ourselves. So just bail. 57808994d113SJan Kara */ 57818994d113SJan Kara if (dquot_initialize_needed(inode)) 57828994d113SJan Kara return -EAGAIN; 57838994d113SJan Kara 5784c03b45b8SMiao Xie /* try to expand with EAs present */ 5785c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5786c03b45b8SMiao Xie raw_inode, handle); 5787c03b45b8SMiao Xie if (error) { 5788c03b45b8SMiao Xie /* 5789c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5790c03b45b8SMiao Xie */ 5791c03b45b8SMiao Xie *no_expand = 1; 5792c03b45b8SMiao Xie } 5793c03b45b8SMiao Xie 5794c03b45b8SMiao Xie return error; 5795c03b45b8SMiao Xie } 5796c03b45b8SMiao Xie 5797ac27a0ecSDave Kleikamp /* 57986dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57996dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 58006dd4ee7cSKalpak Shah */ 5801cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 58021d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 58031d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 58041d03ec98SAneesh Kumar K.V handle_t *handle) 58056dd4ee7cSKalpak Shah { 58063b10fdc6SMiao Xie int no_expand; 58073b10fdc6SMiao Xie int error; 58086dd4ee7cSKalpak Shah 5809cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5810cf0a5e81SMiao Xie return -EOVERFLOW; 5811cf0a5e81SMiao Xie 5812cf0a5e81SMiao Xie /* 5813cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5814cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5815cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5816cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5817cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5818cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5819cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5820cf0a5e81SMiao Xie */ 58216cb367c2SJan Kara if (ext4_journal_extend(handle, 582283448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5823cf0a5e81SMiao Xie return -ENOSPC; 58246dd4ee7cSKalpak Shah 58253b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5826cf0a5e81SMiao Xie return -EBUSY; 58273b10fdc6SMiao Xie 5828c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5829c03b45b8SMiao Xie handle, &no_expand); 58303b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5831c03b45b8SMiao Xie 5832c03b45b8SMiao Xie return error; 58336dd4ee7cSKalpak Shah } 58346dd4ee7cSKalpak Shah 5835c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5836c03b45b8SMiao Xie unsigned int new_extra_isize, 5837c03b45b8SMiao Xie struct ext4_iloc *iloc) 5838c03b45b8SMiao Xie { 5839c03b45b8SMiao Xie handle_t *handle; 5840c03b45b8SMiao Xie int no_expand; 5841c03b45b8SMiao Xie int error, rc; 5842c03b45b8SMiao Xie 5843c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5844c03b45b8SMiao Xie brelse(iloc->bh); 5845c03b45b8SMiao Xie return -EOVERFLOW; 5846c03b45b8SMiao Xie } 5847c03b45b8SMiao Xie 5848c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5849c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5850c03b45b8SMiao Xie if (IS_ERR(handle)) { 5851c03b45b8SMiao Xie error = PTR_ERR(handle); 5852c03b45b8SMiao Xie brelse(iloc->bh); 5853c03b45b8SMiao Xie return error; 5854c03b45b8SMiao Xie } 5855c03b45b8SMiao Xie 5856c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5857c03b45b8SMiao Xie 5858ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5859188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, 5860188c299eSJan Kara EXT4_JTR_NONE); 58613b10fdc6SMiao Xie if (error) { 5862c03b45b8SMiao Xie brelse(iloc->bh); 58637f420d64SDan Carpenter goto out_unlock; 58643b10fdc6SMiao Xie } 5865cf0a5e81SMiao Xie 5866c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5867c03b45b8SMiao Xie handle, &no_expand); 5868c03b45b8SMiao Xie 5869c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5870c03b45b8SMiao Xie if (!error) 5871c03b45b8SMiao Xie error = rc; 5872c03b45b8SMiao Xie 58737f420d64SDan Carpenter out_unlock: 5874c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5875c03b45b8SMiao Xie ext4_journal_stop(handle); 58763b10fdc6SMiao Xie return error; 58776dd4ee7cSKalpak Shah } 58786dd4ee7cSKalpak Shah 58796dd4ee7cSKalpak Shah /* 5880ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5881ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5882ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5883ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5884ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5885ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5886ac27a0ecSDave Kleikamp * 5887ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5888ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5889ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5890ac27a0ecSDave Kleikamp * we start and wait on commits. 5891ac27a0ecSDave Kleikamp */ 58924209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 58934209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5894ac27a0ecSDave Kleikamp { 5895617ba13bSMingming Cao struct ext4_iloc iloc; 58966dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5897cf0a5e81SMiao Xie int err; 5898ac27a0ecSDave Kleikamp 5899ac27a0ecSDave Kleikamp might_sleep(); 59007ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5901617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 59025e1021f2SEryu Guan if (err) 59034209ae12SHarshad Shirwadkar goto out; 5904cf0a5e81SMiao Xie 5905cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5906cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 59076dd4ee7cSKalpak Shah iloc, handle); 5908cf0a5e81SMiao Xie 59094209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 59104209ae12SHarshad Shirwadkar out: 59114209ae12SHarshad Shirwadkar if (unlikely(err)) 59124209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 59134209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 59144209ae12SHarshad Shirwadkar return err; 5915ac27a0ecSDave Kleikamp } 5916ac27a0ecSDave Kleikamp 5917ac27a0ecSDave Kleikamp /* 5918617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5919ac27a0ecSDave Kleikamp * 5920ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5921ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5922ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5923ac27a0ecSDave Kleikamp * 59245dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5925ac27a0ecSDave Kleikamp * are allocated to the file. 5926ac27a0ecSDave Kleikamp * 5927ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5928ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5929ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5930ac27a0ecSDave Kleikamp */ 5931aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5932ac27a0ecSDave Kleikamp { 5933ac27a0ecSDave Kleikamp handle_t *handle; 5934ac27a0ecSDave Kleikamp 59359924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5936ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5937ac27a0ecSDave Kleikamp return; 5938e2728c56SEric Biggers ext4_mark_inode_dirty(handle, inode); 5939e2728c56SEric Biggers ext4_journal_stop(handle); 5940ac27a0ecSDave Kleikamp } 5941ac27a0ecSDave Kleikamp 5942617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5943ac27a0ecSDave Kleikamp { 5944ac27a0ecSDave Kleikamp journal_t *journal; 5945ac27a0ecSDave Kleikamp handle_t *handle; 5946ac27a0ecSDave Kleikamp int err; 594700d873c1SJan Kara int alloc_ctx; 5948ac27a0ecSDave Kleikamp 5949ac27a0ecSDave Kleikamp /* 5950ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5951ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5952ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5953ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5954ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5955ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5956ac27a0ecSDave Kleikamp * nobody is changing anything. 5957ac27a0ecSDave Kleikamp */ 5958ac27a0ecSDave Kleikamp 5959617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59600390131bSFrank Mayhar if (!journal) 59610390131bSFrank Mayhar return 0; 5962d699594dSDave Hansen if (is_journal_aborted(journal)) 5963ac27a0ecSDave Kleikamp return -EROFS; 5964ac27a0ecSDave Kleikamp 596517335dccSDmitry Monakhov /* Wait for all existing dio workers */ 596617335dccSDmitry Monakhov inode_dio_wait(inode); 596717335dccSDmitry Monakhov 59684c546592SDaeho Jeong /* 59694c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 59704c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 59714c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 59724c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 59734c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 59744c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 59754c546592SDaeho Jeong */ 59764c546592SDaeho Jeong if (val) { 5977d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 59784c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 59794c546592SDaeho Jeong if (err < 0) { 5980d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 59814c546592SDaeho Jeong return err; 59824c546592SDaeho Jeong } 59834c546592SDaeho Jeong } 59844c546592SDaeho Jeong 598500d873c1SJan Kara alloc_ctx = ext4_writepages_down_write(inode->i_sb); 5986dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5987ac27a0ecSDave Kleikamp 5988ac27a0ecSDave Kleikamp /* 5989ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5990ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5991ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5992ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5993ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5994ac27a0ecSDave Kleikamp */ 5995ac27a0ecSDave Kleikamp 5996ac27a0ecSDave Kleikamp if (val) 599712e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59985872ddaaSYongqiang Yang else { 599901d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 60004f879ca6SJan Kara if (err < 0) { 60014f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 600200d873c1SJan Kara ext4_writepages_up_write(inode->i_sb, alloc_ctx); 60034f879ca6SJan Kara return err; 60044f879ca6SJan Kara } 600512e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 60065872ddaaSYongqiang Yang } 6007617ba13bSMingming Cao ext4_set_aops(inode); 6008ac27a0ecSDave Kleikamp 6009dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 601000d873c1SJan Kara ext4_writepages_up_write(inode->i_sb, alloc_ctx); 6011c8585c6fSDaeho Jeong 60124c546592SDaeho Jeong if (val) 6013d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 6014ac27a0ecSDave Kleikamp 6015ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6016ac27a0ecSDave Kleikamp 60179924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 6018ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6019ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6020ac27a0ecSDave Kleikamp 6021aa75f4d3SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, 6022e85c81baSXin Yin EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, handle); 6023617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60240390131bSFrank Mayhar ext4_handle_sync(handle); 6025617ba13bSMingming Cao ext4_journal_stop(handle); 6026617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6027ac27a0ecSDave Kleikamp 6028ac27a0ecSDave Kleikamp return err; 6029ac27a0ecSDave Kleikamp } 60302e9ee850SAneesh Kumar K.V 6031188c299eSJan Kara static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, 6032188c299eSJan Kara struct buffer_head *bh) 60332e9ee850SAneesh Kumar K.V { 60342e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60352e9ee850SAneesh Kumar K.V } 60362e9ee850SAneesh Kumar K.V 6037401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 60382e9ee850SAneesh Kumar K.V { 603911bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 60409ea0e45bSMatthew Wilcox struct folio *folio = page_folio(vmf->page); 60412e9ee850SAneesh Kumar K.V loff_t size; 60422e9ee850SAneesh Kumar K.V unsigned long len; 6043401b25aaSSouptick Joarder int err; 6044401b25aaSSouptick Joarder vm_fault_t ret; 60452e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6046496ad9aaSAl Viro struct inode *inode = file_inode(file); 60472e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60489ea7df53SJan Kara handle_t *handle; 60499ea7df53SJan Kara get_block_t *get_block; 60509ea7df53SJan Kara int retries = 0; 60512e9ee850SAneesh Kumar K.V 605202b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 605302b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 605402b016caSTheodore Ts'o 60558e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6056041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6057ea3d7209SJan Kara 6058d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping); 60597b4cc978SEric Biggers 6060401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6061401b25aaSSouptick Joarder if (err) 60627b4cc978SEric Biggers goto out_ret; 60637b4cc978SEric Biggers 606464a9f144SMauricio Faria de Oliveira /* 606564a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 606664a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 606764a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 606864a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 606964a9f144SMauricio Faria de Oliveira */ 607064a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 607164a9f144SMauricio Faria de Oliveira goto retry_alloc; 607264a9f144SMauricio Faria de Oliveira 60739ea7df53SJan Kara /* Delalloc case is easy... */ 60749ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 60759ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 60769ea7df53SJan Kara do { 6077401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 60789ea7df53SJan Kara ext4_da_get_block_prep); 6079401b25aaSSouptick Joarder } while (err == -ENOSPC && 60809ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 60819ea7df53SJan Kara goto out_ret; 60822e9ee850SAneesh Kumar K.V } 60830e499890SDarrick J. Wong 60849ea0e45bSMatthew Wilcox folio_lock(folio); 60859ea7df53SJan Kara size = i_size_read(inode); 60869ea7df53SJan Kara /* Page got truncated from under us? */ 60879ea0e45bSMatthew Wilcox if (folio->mapping != mapping || folio_pos(folio) > size) { 60889ea0e45bSMatthew Wilcox folio_unlock(folio); 60899ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 60909ea7df53SJan Kara goto out; 60910e499890SDarrick J. Wong } 60922e9ee850SAneesh Kumar K.V 60939ea0e45bSMatthew Wilcox len = folio_size(folio); 60949ea0e45bSMatthew Wilcox if (folio_pos(folio) + len > size) 60959ea0e45bSMatthew Wilcox len = size - folio_pos(folio); 6096a827eaffSAneesh Kumar K.V /* 60979ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 60989ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 609964a9f144SMauricio Faria de Oliveira * 610064a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 610164a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6102a827eaffSAneesh Kumar K.V */ 61039ea0e45bSMatthew Wilcox if (folio_buffers(folio)) { 61049ea0e45bSMatthew Wilcox if (!ext4_walk_page_buffers(NULL, inode, folio_buffers(folio), 6105f19d5870STao Ma 0, len, NULL, 6106a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 61079ea7df53SJan Kara /* Wait so that we don't change page under IO */ 61089ea0e45bSMatthew Wilcox folio_wait_stable(folio); 61099ea7df53SJan Kara ret = VM_FAULT_LOCKED; 61109ea7df53SJan Kara goto out; 61112e9ee850SAneesh Kumar K.V } 6112a827eaffSAneesh Kumar K.V } 61139ea0e45bSMatthew Wilcox folio_unlock(folio); 61149ea7df53SJan Kara /* OK, we need to fill the hole... */ 61159ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6116705965bdSJan Kara get_block = ext4_get_block_unwritten; 61179ea7df53SJan Kara else 61189ea7df53SJan Kara get_block = ext4_get_block; 61199ea7df53SJan Kara retry_alloc: 61209924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 61219924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 61229ea7df53SJan Kara if (IS_ERR(handle)) { 6123c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 61249ea7df53SJan Kara goto out; 61259ea7df53SJan Kara } 612664a9f144SMauricio Faria de Oliveira /* 612764a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 612864a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 612964a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 613064a9f144SMauricio Faria de Oliveira */ 613164a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6132401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 613364a9f144SMauricio Faria de Oliveira } else { 61349ea0e45bSMatthew Wilcox folio_lock(folio); 613564a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 613664a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 61379ea0e45bSMatthew Wilcox if (folio->mapping != mapping || folio_pos(folio) > size) { 613864a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6139afb585a9SMauricio Faria de Oliveira goto out_error; 614064a9f144SMauricio Faria de Oliveira } 614164a9f144SMauricio Faria de Oliveira 61429ea0e45bSMatthew Wilcox len = folio_size(folio); 61439ea0e45bSMatthew Wilcox if (folio_pos(folio) + len > size) 61449ea0e45bSMatthew Wilcox len = size - folio_pos(folio); 614564a9f144SMauricio Faria de Oliveira 61469ea0e45bSMatthew Wilcox err = __block_write_begin(&folio->page, 0, len, ext4_get_block); 614764a9f144SMauricio Faria de Oliveira if (!err) { 61489ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 614980be8c5cSRitesh Harjani if (ext4_journal_folio_buffers(handle, folio, len)) 6150afb585a9SMauricio Faria de Oliveira goto out_error; 615164a9f144SMauricio Faria de Oliveira } else { 61529ea0e45bSMatthew Wilcox folio_unlock(folio); 615364a9f144SMauricio Faria de Oliveira } 61549ea7df53SJan Kara } 61559ea7df53SJan Kara ext4_journal_stop(handle); 6156401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 61579ea7df53SJan Kara goto retry_alloc; 61589ea7df53SJan Kara out_ret: 6159401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 61609ea7df53SJan Kara out: 6161d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping); 61628e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 61632e9ee850SAneesh Kumar K.V return ret; 6164afb585a9SMauricio Faria de Oliveira out_error: 61659ea0e45bSMatthew Wilcox folio_unlock(folio); 6166afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6167afb585a9SMauricio Faria de Oliveira goto out; 61682e9ee850SAneesh Kumar K.V } 6169