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) { 1822d859db3SJan Kara /* 1832d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1842d859db3SJan Kara * journal. So although mm thinks everything is clean and 1852d859db3SJan Kara * ready for reaping the inode might still have some pages to 1862d859db3SJan Kara * write in the running transaction or waiting to be 187ccd16945SMatthew Wilcox (Oracle) * checkpointed. Thus calling jbd2_journal_invalidate_folio() 1882d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1892d859db3SJan Kara * cause data loss. Also even if we did not discard these 1902d859db3SJan Kara * buffers, we would have no way to find them after the inode 1912d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1922d859db3SJan Kara * read them before the transaction is checkpointed. So be 1932d859db3SJan Kara * careful and force everything to disk here... We use 1942d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1952d859db3SJan Kara * containing inode's data. 1962d859db3SJan Kara * 1972d859db3SJan Kara * Note that directories do not have this problem because they 1982d859db3SJan Kara * don't use page cache. 1992d859db3SJan Kara */ 2006a7fd522SVegard Nossum if (inode->i_ino != EXT4_JOURNAL_INO && 2016a7fd522SVegard Nossum ext4_should_journal_data(inode) && 2026493792dSZhang Yi S_ISREG(inode->i_mode) && inode->i_data.nrpages) { 2032d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2042d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2052d859db3SJan Kara 206d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2072d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2082d859db3SJan Kara } 20991b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 2105dc23bddSJan Kara 2110930fcc1SAl Viro goto no_delete; 2120930fcc1SAl Viro } 2130930fcc1SAl Viro 214e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 215e2bfb088STheodore Ts'o goto no_delete; 216871a2931SChristoph Hellwig dquot_initialize(inode); 217907f4554SChristoph Hellwig 218678aaf48SJan Kara if (ext4_should_order_data(inode)) 219678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 22091b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 221ac27a0ecSDave Kleikamp 2228e8ad8a5SJan Kara /* 223ceff86fdSJan Kara * For inodes with journalled data, transaction commit could have 224bc12ac98SZhang Yi * dirtied the inode. And for inodes with dioread_nolock, unwritten 225bc12ac98SZhang Yi * extents converting worker could merge extents and also have dirtied 226bc12ac98SZhang Yi * the inode. Flush worker is ignoring it because of I_FREEING flag but 227bc12ac98SZhang Yi * we still need to remove the inode from the writeback lists. 228ceff86fdSJan Kara */ 229bc12ac98SZhang Yi if (!list_empty_careful(&inode->i_io_list)) 230ceff86fdSJan Kara inode_io_list_del(inode); 231ceff86fdSJan Kara 232ceff86fdSJan Kara /* 2338e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 23446e294efSJan Kara * protection against it. When we are in a running transaction though, 23546e294efSJan Kara * we are already protected against freezing and we cannot grab further 23646e294efSJan Kara * protection due to lock ordering constraints. 2378e8ad8a5SJan Kara */ 23846e294efSJan Kara if (!ext4_journal_current_handle()) { 2398e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 24046e294efSJan Kara freeze_protected = true; 24146e294efSJan Kara } 242e50e5129SAndreas Dilger 24330a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 24430a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 24530a7eb97STahsin Erdogan 24665db869cSJan Kara /* 24765db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 24865db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 24965db869cSJan Kara */ 25030a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 25165db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 252ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 253bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 254ac27a0ecSDave Kleikamp /* 255ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 256ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 257ac27a0ecSDave Kleikamp * cleaned up. 258ac27a0ecSDave Kleikamp */ 259617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 26046e294efSJan Kara if (freeze_protected) 2618e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 262ac27a0ecSDave Kleikamp goto no_delete; 263ac27a0ecSDave Kleikamp } 26430a7eb97STahsin Erdogan 265ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2660390131bSFrank Mayhar ext4_handle_sync(handle); 267407cd7fbSTahsin Erdogan 268407cd7fbSTahsin Erdogan /* 269407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 270407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 271407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 272407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 273407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 274407cd7fbSTahsin Erdogan */ 275407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 276407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 277ac27a0ecSDave Kleikamp inode->i_size = 0; 278bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 279bc965ab3STheodore Ts'o if (err) { 28012062dddSEric Sandeen ext4_warning(inode->i_sb, 281bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 282bc965ab3STheodore Ts'o goto stop_handle; 283bc965ab3STheodore Ts'o } 2842c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2852c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2862c98eb5eSTheodore Ts'o if (err) { 28754d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err, 2882c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2892c98eb5eSTheodore Ts'o inode->i_ino, err); 2902c98eb5eSTheodore Ts'o goto stop_handle; 2912c98eb5eSTheodore Ts'o } 2922c98eb5eSTheodore Ts'o } 293bc965ab3STheodore Ts'o 29430a7eb97STahsin Erdogan /* Remove xattr references. */ 29530a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 29630a7eb97STahsin Erdogan extra_credits); 29730a7eb97STahsin Erdogan if (err) { 29830a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 299bc965ab3STheodore Ts'o stop_handle: 300bc965ab3STheodore Ts'o ext4_journal_stop(handle); 30145388219STheodore Ts'o ext4_orphan_del(NULL, inode); 30246e294efSJan Kara if (freeze_protected) 3038e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 30430a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 305bc965ab3STheodore Ts'o goto no_delete; 306bc965ab3STheodore Ts'o } 307bc965ab3STheodore Ts'o 308ac27a0ecSDave Kleikamp /* 309617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 310ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 311617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 312ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 313617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 314ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 315ac27a0ecSDave Kleikamp */ 316617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3175ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 318ac27a0ecSDave Kleikamp 319ac27a0ecSDave Kleikamp /* 320ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 321ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 322ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 323ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 324ac27a0ecSDave Kleikamp * fails. 325ac27a0ecSDave Kleikamp */ 326617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 327ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3280930fcc1SAl Viro ext4_clear_inode(inode); 329ac27a0ecSDave Kleikamp else 330617ba13bSMingming Cao ext4_free_inode(handle, inode); 331617ba13bSMingming Cao ext4_journal_stop(handle); 33246e294efSJan Kara if (freeze_protected) 3338e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3340421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 335ac27a0ecSDave Kleikamp return; 336ac27a0ecSDave Kleikamp no_delete: 337318cdc82SZhang Yi /* 338318cdc82SZhang Yi * Check out some where else accidentally dirty the evicting inode, 339318cdc82SZhang Yi * which may probably cause inode use-after-free issues later. 340318cdc82SZhang Yi */ 341318cdc82SZhang Yi WARN_ON_ONCE(!list_empty_careful(&inode->i_io_list)); 342318cdc82SZhang Yi 343b21ebf14SHarshad Shirwadkar if (!list_empty(&EXT4_I(inode)->i_fc_list)) 344e85c81baSXin Yin ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM, NULL); 3450930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 346ac27a0ecSDave Kleikamp } 347ac27a0ecSDave Kleikamp 348a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 349a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 35060e58e0fSMingming Cao { 351a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 35260e58e0fSMingming Cao } 353a9e7f447SDmitry Monakhov #endif 3549d0be502STheodore Ts'o 35512219aeaSAneesh Kumar K.V /* 3560637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3570637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3580637c6f4STheodore Ts'o */ 3595f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3605f634d06SAneesh Kumar K.V int used, int quota_claim) 36112219aeaSAneesh Kumar K.V { 36212219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3630637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 36412219aeaSAneesh Kumar K.V 3650637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 366d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3670637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3688de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3691084f252STheodore Ts'o "with only %d reserved data blocks", 3700637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3710637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3720637c6f4STheodore Ts'o WARN_ON(1); 3730637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3746bc6e63fSAneesh Kumar K.V } 37512219aeaSAneesh Kumar K.V 3760637c6f4STheodore Ts'o /* Update per-inode reservations */ 3770637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 37871d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3790637c6f4STheodore Ts'o 380f9505c72Schenyichong spin_unlock(&ei->i_block_reservation_lock); 38160e58e0fSMingming Cao 38272b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38372b8ab9dSEric Sandeen if (quota_claim) 3847b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38572b8ab9dSEric Sandeen else { 3865f634d06SAneesh Kumar K.V /* 3875f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3885f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 38972b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3905f634d06SAneesh Kumar K.V */ 3917b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3925f634d06SAneesh Kumar K.V } 393d6014301SAneesh Kumar K.V 394d6014301SAneesh Kumar K.V /* 395d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 396d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 397d6014301SAneesh Kumar K.V * inode's preallocations. 398d6014301SAneesh Kumar K.V */ 3990637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 40082dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 40127bc446eSbrookxu ext4_discard_preallocations(inode, 0); 40212219aeaSAneesh Kumar K.V } 40312219aeaSAneesh Kumar K.V 404e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 405c398eda0STheodore Ts'o unsigned int line, 40624676da4STheodore Ts'o struct ext4_map_blocks *map) 4076fd058f7STheodore Ts'o { 408345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 409345c0dbfSTheodore Ts'o (inode->i_ino == 410345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 411345c0dbfSTheodore Ts'o return 0; 412ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 413c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 414bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 41524676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 416bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4176a797d27SDarrick J. Wong return -EFSCORRUPTED; 4186fd058f7STheodore Ts'o } 4196fd058f7STheodore Ts'o return 0; 4206fd058f7STheodore Ts'o } 4216fd058f7STheodore Ts'o 42253085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 42353085facSJan Kara ext4_lblk_t len) 42453085facSJan Kara { 42553085facSJan Kara int ret; 42653085facSJan Kara 42733b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 428a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 42953085facSJan Kara 43053085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 43153085facSJan Kara if (ret > 0) 43253085facSJan Kara ret = 0; 43353085facSJan Kara 43453085facSJan Kara return ret; 43553085facSJan Kara } 43653085facSJan Kara 437e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 438c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 439e29136f8STheodore Ts'o 440921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 441921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 442921f266bSDmitry Monakhov struct inode *inode, 443921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 444921f266bSDmitry Monakhov struct ext4_map_blocks *map, 445921f266bSDmitry Monakhov int flags) 446921f266bSDmitry Monakhov { 447921f266bSDmitry Monakhov int retval; 448921f266bSDmitry Monakhov 449921f266bSDmitry Monakhov map->m_flags = 0; 450921f266bSDmitry Monakhov /* 451921f266bSDmitry Monakhov * There is a race window that the result is not the same. 452921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 453921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 454921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 455921f266bSDmitry Monakhov * could be converted. 456921f266bSDmitry Monakhov */ 457c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 458921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4599e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 460921f266bSDmitry Monakhov } else { 4619e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 462921f266bSDmitry Monakhov } 463921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 464921f266bSDmitry Monakhov 465921f266bSDmitry Monakhov /* 466921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 467921f266bSDmitry Monakhov * tree. So the m_len might not equal. 468921f266bSDmitry Monakhov */ 469921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 470921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 471921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 472bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 473921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 474921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 475921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 476921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 477921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 478921f266bSDmitry Monakhov retval, flags); 479921f266bSDmitry Monakhov } 480921f266bSDmitry Monakhov } 481921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 482921f266bSDmitry Monakhov 48355138e0bSTheodore Ts'o /* 484e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4852b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 486f5ab0d1fSMingming Cao * 487f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 488f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 489f5ab0d1fSMingming Cao * mapped. 490f5ab0d1fSMingming Cao * 491e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 492e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 493f5ab0d1fSMingming Cao * based files 494f5ab0d1fSMingming Cao * 495facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 496facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 497facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 498f5ab0d1fSMingming Cao * 499f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 500facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 501facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 502f5ab0d1fSMingming Cao * 503f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 504f5ab0d1fSMingming Cao */ 505e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 506e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5070e855ac8SAneesh Kumar K.V { 508d100eef2SZheng Liu struct extent_status es; 5090e855ac8SAneesh Kumar K.V int retval; 510b8a86845SLukas Czerner int ret = 0; 511921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 512921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 513921f266bSDmitry Monakhov 514921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 515921f266bSDmitry Monakhov #endif 516f5ab0d1fSMingming Cao 517e35fd660STheodore Ts'o map->m_flags = 0; 51870aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 51970aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 520d100eef2SZheng Liu 521e861b5e9STheodore Ts'o /* 522e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 523e861b5e9STheodore Ts'o */ 524e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 525e861b5e9STheodore Ts'o map->m_len = INT_MAX; 526e861b5e9STheodore Ts'o 5274adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5284adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5296a797d27SDarrick J. Wong return -EFSCORRUPTED; 5304adb6ab3SKazuya Mio 531d100eef2SZheng Liu /* Lookup extent status tree firstly */ 5328016e29fSHarshad Shirwadkar if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 5338016e29fSHarshad Shirwadkar ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 534d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 535d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 536d100eef2SZheng Liu map->m_lblk - es.es_lblk; 537d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 538d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 539d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 540d100eef2SZheng Liu if (retval > map->m_len) 541d100eef2SZheng Liu retval = map->m_len; 542d100eef2SZheng Liu map->m_len = retval; 543d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 544facab4d9SJan Kara map->m_pblk = 0; 545facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 546facab4d9SJan Kara if (retval > map->m_len) 547facab4d9SJan Kara retval = map->m_len; 548facab4d9SJan Kara map->m_len = retval; 549d100eef2SZheng Liu retval = 0; 550d100eef2SZheng Liu } else { 5511e83bc81SArnd Bergmann BUG(); 552d100eef2SZheng Liu } 5539558cf14SZhang Yi 5549558cf14SZhang Yi if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) 5559558cf14SZhang Yi return retval; 556921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 557921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 558921f266bSDmitry Monakhov &orig_map, flags); 559921f266bSDmitry Monakhov #endif 560d100eef2SZheng Liu goto found; 561d100eef2SZheng Liu } 5629558cf14SZhang Yi /* 5639558cf14SZhang Yi * In the query cache no-wait mode, nothing we can do more if we 5649558cf14SZhang Yi * cannot find extent in the cache. 5659558cf14SZhang Yi */ 5669558cf14SZhang Yi if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) 5679558cf14SZhang Yi return 0; 568d100eef2SZheng Liu 5694df3d265SAneesh Kumar K.V /* 570b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 571b920c755STheodore Ts'o * file system block. 5724df3d265SAneesh Kumar K.V */ 573c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 57412e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5759e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5764df3d265SAneesh Kumar K.V } else { 5779e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5780e855ac8SAneesh Kumar K.V } 579f7fec032SZheng Liu if (retval > 0) { 5803be78c73STheodore Ts'o unsigned int status; 581f7fec032SZheng Liu 58244fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 58344fb851dSZheng Liu ext4_warning(inode->i_sb, 58444fb851dSZheng Liu "ES len assertion failed for inode " 58544fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 58644fb851dSZheng Liu inode->i_ino, retval, map->m_len); 58744fb851dSZheng Liu WARN_ON(1); 588921f266bSDmitry Monakhov } 589921f266bSDmitry Monakhov 590f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 591f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 592f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 593d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 594ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 595f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 596f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 597f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 598f7fec032SZheng Liu map->m_len, map->m_pblk, status); 599f7fec032SZheng Liu if (ret < 0) 600f7fec032SZheng Liu retval = ret; 601f7fec032SZheng Liu } 6024df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 603f5ab0d1fSMingming Cao 604d100eef2SZheng Liu found: 605e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 606b8a86845SLukas Czerner ret = check_block_validity(inode, map); 6076fd058f7STheodore Ts'o if (ret != 0) 6086fd058f7STheodore Ts'o return ret; 6096fd058f7STheodore Ts'o } 6106fd058f7STheodore Ts'o 611f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 612c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6134df3d265SAneesh Kumar K.V return retval; 6144df3d265SAneesh Kumar K.V 6154df3d265SAneesh Kumar K.V /* 616f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 617f5ab0d1fSMingming Cao * 618f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 619df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 620f5ab0d1fSMingming Cao * with buffer head unmapped. 621f5ab0d1fSMingming Cao */ 622e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 623b8a86845SLukas Czerner /* 624b8a86845SLukas Czerner * If we need to convert extent to unwritten 625b8a86845SLukas Czerner * we continue and do the actual work in 626b8a86845SLukas Czerner * ext4_ext_map_blocks() 627b8a86845SLukas Czerner */ 628b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 629f5ab0d1fSMingming Cao return retval; 630f5ab0d1fSMingming Cao 631f5ab0d1fSMingming Cao /* 632a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 633a25a4e1aSZheng Liu * it will be set again. 6342a8964d6SAneesh Kumar K.V */ 635a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6362a8964d6SAneesh Kumar K.V 6372a8964d6SAneesh Kumar K.V /* 638556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 639f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 640d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 641f5ab0d1fSMingming Cao * with create == 1 flag. 6424df3d265SAneesh Kumar K.V */ 643c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 644d2a17637SMingming Cao 645d2a17637SMingming Cao /* 6464df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6474df3d265SAneesh Kumar K.V * could have changed the inode type in between 6484df3d265SAneesh Kumar K.V */ 64912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 650e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6510e855ac8SAneesh Kumar K.V } else { 652e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 653267e4db9SAneesh Kumar K.V 654e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 655267e4db9SAneesh Kumar K.V /* 656267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 657267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 658267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 659267e4db9SAneesh Kumar K.V */ 66019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 661267e4db9SAneesh Kumar K.V } 6622ac3b6e0STheodore Ts'o 663d2a17637SMingming Cao /* 6642ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6655f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6665f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6675f634d06SAneesh Kumar K.V * reserve space here. 668d2a17637SMingming Cao */ 6695f634d06SAneesh Kumar K.V if ((retval > 0) && 6701296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6715f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6725f634d06SAneesh Kumar K.V } 673d2a17637SMingming Cao 674f7fec032SZheng Liu if (retval > 0) { 6753be78c73STheodore Ts'o unsigned int status; 676f7fec032SZheng Liu 67744fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 67844fb851dSZheng Liu ext4_warning(inode->i_sb, 67944fb851dSZheng Liu "ES len assertion failed for inode " 68044fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 68144fb851dSZheng Liu inode->i_ino, retval, map->m_len); 68244fb851dSZheng Liu WARN_ON(1); 683921f266bSDmitry Monakhov } 684921f266bSDmitry Monakhov 685adb23551SZheng Liu /* 686c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 687c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6889b623df6SJan Kara * use them before they are really zeroed. We also have to 6899b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6909b623df6SJan Kara * overwrite zeros with stale data from block device. 691c86d8db3SJan Kara */ 692c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 693c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 694c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 695c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 696c86d8db3SJan Kara map->m_pblk, map->m_len); 697c86d8db3SJan Kara if (ret) { 698c86d8db3SJan Kara retval = ret; 699c86d8db3SJan Kara goto out_sem; 700c86d8db3SJan Kara } 701c86d8db3SJan Kara } 702c86d8db3SJan Kara 703c86d8db3SJan Kara /* 704adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 705adb23551SZheng Liu * extent status tree. 706adb23551SZheng Liu */ 707adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 708bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 709adb23551SZheng Liu if (ext4_es_is_written(&es)) 710c86d8db3SJan Kara goto out_sem; 711adb23551SZheng Liu } 712f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 713f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 714f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 715d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 716ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 717f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 718f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 719f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 720f7fec032SZheng Liu map->m_pblk, status); 721c86d8db3SJan Kara if (ret < 0) { 72251865fdaSZheng Liu retval = ret; 723c86d8db3SJan Kara goto out_sem; 724c86d8db3SJan Kara } 72551865fdaSZheng Liu } 7265356f261SAditya Kali 727c86d8db3SJan Kara out_sem: 7280e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 729e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 730b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7316fd058f7STheodore Ts'o if (ret != 0) 7326fd058f7STheodore Ts'o return ret; 73306bd3c36SJan Kara 73406bd3c36SJan Kara /* 73506bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 73606bd3c36SJan Kara * visible after transaction commit must be on transaction's 73706bd3c36SJan Kara * ordered data list. 73806bd3c36SJan Kara */ 73906bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 74006bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 74106bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 74202749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 74306bd3c36SJan Kara ext4_should_order_data(inode)) { 74473131fbbSRoss Zwisler loff_t start_byte = 74573131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 74673131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 74773131fbbSRoss Zwisler 748ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 74973131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 75073131fbbSRoss Zwisler start_byte, length); 751ee0876bcSJan Kara else 75273131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 75373131fbbSRoss Zwisler start_byte, length); 75406bd3c36SJan Kara if (ret) 75506bd3c36SJan Kara return ret; 75606bd3c36SJan Kara } 7575e4d0ebaSXin Yin } 7585e4d0ebaSXin Yin if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || 7595e4d0ebaSXin Yin map->m_flags & EXT4_MAP_MAPPED)) 760a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 761aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 762ec8c60beSRitesh Harjani if (retval < 0) 76370aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7640e855ac8SAneesh Kumar K.V return retval; 7650e855ac8SAneesh Kumar K.V } 7660e855ac8SAneesh Kumar K.V 767ed8ad838SJan Kara /* 768ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 769ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 770ed8ad838SJan Kara */ 771ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 772ed8ad838SJan Kara { 773ed8ad838SJan Kara unsigned long old_state; 774ed8ad838SJan Kara unsigned long new_state; 775ed8ad838SJan Kara 776ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 777ed8ad838SJan Kara 778ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 779ed8ad838SJan Kara if (!bh->b_page) { 780ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 781ed8ad838SJan Kara return; 782ed8ad838SJan Kara } 783ed8ad838SJan Kara /* 784ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 785ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 786ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 787ed8ad838SJan Kara */ 788ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 7893ee2a3e7SUros Bizjak do { 790ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 7913ee2a3e7SUros Bizjak } while (unlikely(!try_cmpxchg(&bh->b_state, &old_state, new_state))); 792ed8ad838SJan Kara } 793ed8ad838SJan Kara 7942ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7952ed88685STheodore Ts'o struct buffer_head *bh, int flags) 796ac27a0ecSDave Kleikamp { 7972ed88685STheodore Ts'o struct ext4_map_blocks map; 798efe70c29SJan Kara int ret = 0; 799ac27a0ecSDave Kleikamp 80046c7f254STao Ma if (ext4_has_inline_data(inode)) 80146c7f254STao Ma return -ERANGE; 80246c7f254STao Ma 8032ed88685STheodore Ts'o map.m_lblk = iblock; 8042ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 8052ed88685STheodore Ts'o 806efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 807efe70c29SJan Kara flags); 808ac27a0ecSDave Kleikamp if (ret > 0) { 8092ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 810ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 8112ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 812ac27a0ecSDave Kleikamp ret = 0; 813547edce3SRoss Zwisler } else if (ret == 0) { 814547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 815547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 816ac27a0ecSDave Kleikamp } 817ac27a0ecSDave Kleikamp return ret; 818ac27a0ecSDave Kleikamp } 819ac27a0ecSDave Kleikamp 8202ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8212ed88685STheodore Ts'o struct buffer_head *bh, int create) 8222ed88685STheodore Ts'o { 8232ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8242ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8252ed88685STheodore Ts'o } 8262ed88685STheodore Ts'o 827ac27a0ecSDave Kleikamp /* 828705965bdSJan Kara * Get block function used when preparing for buffered write if we require 829705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 830705965bdSJan Kara * will be converted to written after the IO is complete. 831705965bdSJan Kara */ 832705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 833705965bdSJan Kara struct buffer_head *bh_result, int create) 834705965bdSJan Kara { 835705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 836705965bdSJan Kara inode->i_ino, create); 837705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 8388d5459c1SJan Kara EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); 839705965bdSJan Kara } 840705965bdSJan Kara 841efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 842efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 843efe70c29SJan Kara 844e84dfbe2SJan Kara /* 845ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 846ac27a0ecSDave Kleikamp */ 847617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 848c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 849ac27a0ecSDave Kleikamp { 8502ed88685STheodore Ts'o struct ext4_map_blocks map; 8512ed88685STheodore Ts'o struct buffer_head *bh; 852c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 8539558cf14SZhang Yi bool nowait = map_flags & EXT4_GET_BLOCKS_CACHED_NOWAIT; 85410560082STheodore Ts'o int err; 855ac27a0ecSDave Kleikamp 856837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8578016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 8589558cf14SZhang Yi ASSERT(create == 0 || !nowait); 859ac27a0ecSDave Kleikamp 8602ed88685STheodore Ts'o map.m_lblk = block; 8612ed88685STheodore Ts'o map.m_len = 1; 862c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8632ed88685STheodore Ts'o 86410560082STheodore Ts'o if (err == 0) 86510560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8662ed88685STheodore Ts'o if (err < 0) 86710560082STheodore Ts'o return ERR_PTR(err); 8682ed88685STheodore Ts'o 8699558cf14SZhang Yi if (nowait) 8709558cf14SZhang Yi return sb_find_get_block(inode->i_sb, map.m_pblk); 8719558cf14SZhang Yi 8722ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 87310560082STheodore Ts'o if (unlikely(!bh)) 87410560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8752ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 876837c23fbSChunguang Xu ASSERT(create != 0); 877837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8788016e29fSHarshad Shirwadkar || (handle != NULL)); 879ac27a0ecSDave Kleikamp 880ac27a0ecSDave Kleikamp /* 881ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 882ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 883ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 884617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 885ac27a0ecSDave Kleikamp * problem. 886ac27a0ecSDave Kleikamp */ 887ac27a0ecSDave Kleikamp lock_buffer(bh); 888ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 889188c299eSJan Kara err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 890188c299eSJan Kara EXT4_JTR_NONE); 89110560082STheodore Ts'o if (unlikely(err)) { 89210560082STheodore Ts'o unlock_buffer(bh); 89310560082STheodore Ts'o goto errout; 89410560082STheodore Ts'o } 89510560082STheodore Ts'o if (!buffer_uptodate(bh)) { 896ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 897ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 898ac27a0ecSDave Kleikamp } 899ac27a0ecSDave Kleikamp unlock_buffer(bh); 9000390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 9010390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 90210560082STheodore Ts'o if (unlikely(err)) 90310560082STheodore Ts'o goto errout; 90410560082STheodore Ts'o } else 905ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 906ac27a0ecSDave Kleikamp return bh; 90710560082STheodore Ts'o errout: 90810560082STheodore Ts'o brelse(bh); 90910560082STheodore Ts'o return ERR_PTR(err); 910ac27a0ecSDave Kleikamp } 911ac27a0ecSDave Kleikamp 912617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 913c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 914ac27a0ecSDave Kleikamp { 915ac27a0ecSDave Kleikamp struct buffer_head *bh; 9162d069c08Szhangyi (F) int ret; 917ac27a0ecSDave Kleikamp 918c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 9191c215028STheodore Ts'o if (IS_ERR(bh)) 920ac27a0ecSDave Kleikamp return bh; 9217963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 922ac27a0ecSDave Kleikamp return bh; 9232d069c08Szhangyi (F) 9242d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 9252d069c08Szhangyi (F) if (ret) { 926ac27a0ecSDave Kleikamp put_bh(bh); 9272d069c08Szhangyi (F) return ERR_PTR(ret); 9282d069c08Szhangyi (F) } 9292d069c08Szhangyi (F) return bh; 930ac27a0ecSDave Kleikamp } 931ac27a0ecSDave Kleikamp 9329699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 9339699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 9349699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 9359699d4f9STahsin Erdogan { 9369699d4f9STahsin Erdogan int i, err; 9379699d4f9STahsin Erdogan 9389699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9399699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9409699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9419699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9429699d4f9STahsin Erdogan bh_count = i; 9439699d4f9STahsin Erdogan goto out_brelse; 9449699d4f9STahsin Erdogan } 9459699d4f9STahsin Erdogan } 9469699d4f9STahsin Erdogan 9479699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9489699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9492d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9502d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9519699d4f9STahsin Erdogan 9529699d4f9STahsin Erdogan if (!wait) 9539699d4f9STahsin Erdogan return 0; 9549699d4f9STahsin Erdogan 9559699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9569699d4f9STahsin Erdogan if (bhs[i]) 9579699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9589699d4f9STahsin Erdogan 9599699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9609699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9619699d4f9STahsin Erdogan err = -EIO; 9629699d4f9STahsin Erdogan goto out_brelse; 9639699d4f9STahsin Erdogan } 9649699d4f9STahsin Erdogan } 9659699d4f9STahsin Erdogan return 0; 9669699d4f9STahsin Erdogan 9679699d4f9STahsin Erdogan out_brelse: 9689699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9699699d4f9STahsin Erdogan brelse(bhs[i]); 9709699d4f9STahsin Erdogan bhs[i] = NULL; 9719699d4f9STahsin Erdogan } 9729699d4f9STahsin Erdogan return err; 9739699d4f9STahsin Erdogan } 9749699d4f9STahsin Erdogan 975188c299eSJan Kara int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, 976ac27a0ecSDave Kleikamp struct buffer_head *head, 977ac27a0ecSDave Kleikamp unsigned from, 978ac27a0ecSDave Kleikamp unsigned to, 979ac27a0ecSDave Kleikamp int *partial, 980188c299eSJan Kara int (*fn)(handle_t *handle, struct inode *inode, 981ac27a0ecSDave Kleikamp struct buffer_head *bh)) 982ac27a0ecSDave Kleikamp { 983ac27a0ecSDave Kleikamp struct buffer_head *bh; 984ac27a0ecSDave Kleikamp unsigned block_start, block_end; 985ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 986ac27a0ecSDave Kleikamp int err, ret = 0; 987ac27a0ecSDave Kleikamp struct buffer_head *next; 988ac27a0ecSDave Kleikamp 989ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 990ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 991de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 992ac27a0ecSDave Kleikamp next = bh->b_this_page; 993ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 994ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 995ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 996ac27a0ecSDave Kleikamp *partial = 1; 997ac27a0ecSDave Kleikamp continue; 998ac27a0ecSDave Kleikamp } 999188c299eSJan Kara err = (*fn)(handle, inode, bh); 1000ac27a0ecSDave Kleikamp if (!ret) 1001ac27a0ecSDave Kleikamp ret = err; 1002ac27a0ecSDave Kleikamp } 1003ac27a0ecSDave Kleikamp return ret; 1004ac27a0ecSDave Kleikamp } 1005ac27a0ecSDave Kleikamp 1006188c299eSJan Kara int do_journal_get_write_access(handle_t *handle, struct inode *inode, 1007ac27a0ecSDave Kleikamp struct buffer_head *bh) 1008ac27a0ecSDave Kleikamp { 100956d35a4cSJan Kara int dirty = buffer_dirty(bh); 101056d35a4cSJan Kara int ret; 101156d35a4cSJan Kara 1012ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1013ac27a0ecSDave Kleikamp return 0; 101456d35a4cSJan Kara /* 1015ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 101656d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 101756d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1018ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 101956d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 102056d35a4cSJan Kara * ever write the buffer. 102156d35a4cSJan Kara */ 102256d35a4cSJan Kara if (dirty) 102356d35a4cSJan Kara clear_buffer_dirty(bh); 10245d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 1025188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1026188c299eSJan Kara EXT4_JTR_NONE); 102756d35a4cSJan Kara if (!ret && dirty) 102856d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 102956d35a4cSJan Kara return ret; 1030ac27a0ecSDave Kleikamp } 1031ac27a0ecSDave Kleikamp 1032643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10332058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10342058f83aSMichael Halcrow get_block_t *get_block) 10352058f83aSMichael Halcrow { 103609cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10372058f83aSMichael Halcrow unsigned to = from + len; 10382058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10392058f83aSMichael Halcrow unsigned block_start, block_end; 10402058f83aSMichael Halcrow sector_t block; 10412058f83aSMichael Halcrow int err = 0; 10422058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10432058f83aSMichael Halcrow unsigned bbits; 10440b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10450b578f35SChandan Rajendra int nr_wait = 0; 10460b578f35SChandan Rajendra int i; 10472058f83aSMichael Halcrow 10482058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 104909cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 105009cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10512058f83aSMichael Halcrow BUG_ON(from > to); 10522058f83aSMichael Halcrow 10532058f83aSMichael Halcrow if (!page_has_buffers(page)) 10542058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10552058f83aSMichael Halcrow head = page_buffers(page); 10562058f83aSMichael Halcrow bbits = ilog2(blocksize); 105709cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10582058f83aSMichael Halcrow 10592058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10602058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10612058f83aSMichael Halcrow block_end = block_start + blocksize; 10622058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10632058f83aSMichael Halcrow if (PageUptodate(page)) { 10642058f83aSMichael Halcrow set_buffer_uptodate(bh); 10652058f83aSMichael Halcrow } 10662058f83aSMichael Halcrow continue; 10672058f83aSMichael Halcrow } 10682058f83aSMichael Halcrow if (buffer_new(bh)) 10692058f83aSMichael Halcrow clear_buffer_new(bh); 10702058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10712058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10722058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10732058f83aSMichael Halcrow if (err) 10742058f83aSMichael Halcrow break; 10752058f83aSMichael Halcrow if (buffer_new(bh)) { 10762058f83aSMichael Halcrow if (PageUptodate(page)) { 10772058f83aSMichael Halcrow clear_buffer_new(bh); 10782058f83aSMichael Halcrow set_buffer_uptodate(bh); 10792058f83aSMichael Halcrow mark_buffer_dirty(bh); 10802058f83aSMichael Halcrow continue; 10812058f83aSMichael Halcrow } 10822058f83aSMichael Halcrow if (block_end > to || block_start < from) 10832058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10842058f83aSMichael Halcrow block_start, from); 10852058f83aSMichael Halcrow continue; 10862058f83aSMichael Halcrow } 10872058f83aSMichael Halcrow } 10882058f83aSMichael Halcrow if (PageUptodate(page)) { 10892058f83aSMichael Halcrow set_buffer_uptodate(bh); 10902058f83aSMichael Halcrow continue; 10912058f83aSMichael Halcrow } 10922058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10932058f83aSMichael Halcrow !buffer_unwritten(bh) && 10942058f83aSMichael Halcrow (block_start < from || block_end > to)) { 10952d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 10960b578f35SChandan Rajendra wait[nr_wait++] = bh; 10972058f83aSMichael Halcrow } 10982058f83aSMichael Halcrow } 10992058f83aSMichael Halcrow /* 11002058f83aSMichael Halcrow * If we issued read requests, let them complete. 11012058f83aSMichael Halcrow */ 11020b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11030b578f35SChandan Rajendra wait_on_buffer(wait[i]); 11040b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 11052058f83aSMichael Halcrow err = -EIO; 11062058f83aSMichael Halcrow } 11077e0785fcSChandan Rajendra if (unlikely(err)) { 11082058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 11094f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11100b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11110b578f35SChandan Rajendra int err2; 11120b578f35SChandan Rajendra 111351e4e315SEric Biggers err2 = fscrypt_decrypt_pagecache_blocks(page_folio(page), 111451e4e315SEric Biggers blocksize, 11150b578f35SChandan Rajendra bh_offset(wait[i])); 11160b578f35SChandan Rajendra if (err2) { 11170b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11180b578f35SChandan Rajendra err = err2; 11190b578f35SChandan Rajendra } 11200b578f35SChandan Rajendra } 11217e0785fcSChandan Rajendra } 11227e0785fcSChandan Rajendra 11232058f83aSMichael Halcrow return err; 11242058f83aSMichael Halcrow } 11252058f83aSMichael Halcrow #endif 11262058f83aSMichael Halcrow 11279462f770SJan Kara /* 11289462f770SJan Kara * To preserve ordering, it is essential that the hole instantiation and 11299462f770SJan Kara * the data write be encapsulated in a single transaction. We cannot 11309462f770SJan Kara * close off a transaction and start a new one between the ext4_get_block() 11319462f770SJan Kara * and the ext4_write_end(). So doing the jbd2_journal_start at the start of 11329462f770SJan Kara * ext4_write_begin() is the right place. 11339462f770SJan Kara */ 1134bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 11359d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 1136bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1137ac27a0ecSDave Kleikamp { 1138bfc1af65SNick Piggin struct inode *inode = mapping->host; 11391938a150SAneesh Kumar K.V int ret, needed_blocks; 1140ac27a0ecSDave Kleikamp handle_t *handle; 1141ac27a0ecSDave Kleikamp int retries = 0; 1142bfc1af65SNick Piggin struct page *page; 1143bfc1af65SNick Piggin pgoff_t index; 1144bfc1af65SNick Piggin unsigned from, to; 1145bfc1af65SNick Piggin 11460db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11470db1ff22STheodore Ts'o return -EIO; 11480db1ff22STheodore Ts'o 11499d6b0cd7SMatthew Wilcox (Oracle) trace_ext4_write_begin(inode, pos, len); 11501938a150SAneesh Kumar K.V /* 11511938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11521938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11531938a150SAneesh Kumar K.V */ 11541938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 115509cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 115609cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1157bfc1af65SNick Piggin to = from + len; 1158ac27a0ecSDave Kleikamp 1159f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1160f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1161832ee62dSMatthew Wilcox (Oracle) pagep); 1162f19d5870STao Ma if (ret < 0) 116347564bfbSTheodore Ts'o return ret; 116447564bfbSTheodore Ts'o if (ret == 1) 116547564bfbSTheodore Ts'o return 0; 1166f19d5870STao Ma } 1167f19d5870STao Ma 116847564bfbSTheodore Ts'o /* 116947564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 117047564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 117147564bfbSTheodore Ts'o * is being written back. So grab it first before we start 117247564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 117347564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 117447564bfbSTheodore Ts'o */ 117547564bfbSTheodore Ts'o retry_grab: 1176b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 117747564bfbSTheodore Ts'o if (!page) 117847564bfbSTheodore Ts'o return -ENOMEM; 1179d1052d23SJinke Han /* 1180d1052d23SJinke Han * The same as page allocation, we prealloc buffer heads before 1181d1052d23SJinke Han * starting the handle. 1182d1052d23SJinke Han */ 1183d1052d23SJinke Han if (!page_has_buffers(page)) 1184d1052d23SJinke Han create_empty_buffers(page, inode->i_sb->s_blocksize, 0); 1185d1052d23SJinke Han 118647564bfbSTheodore Ts'o unlock_page(page); 118747564bfbSTheodore Ts'o 118847564bfbSTheodore Ts'o retry_journal: 11899924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 11907479d2b9SAndrew Morton if (IS_ERR(handle)) { 119109cbfeafSKirill A. Shutemov put_page(page); 119247564bfbSTheodore Ts'o return PTR_ERR(handle); 1193cf108bcaSJan Kara } 1194f19d5870STao Ma 119547564bfbSTheodore Ts'o lock_page(page); 119647564bfbSTheodore Ts'o if (page->mapping != mapping) { 119747564bfbSTheodore Ts'o /* The page got truncated from under us */ 119847564bfbSTheodore Ts'o unlock_page(page); 119909cbfeafSKirill A. Shutemov put_page(page); 1200cf108bcaSJan Kara ext4_journal_stop(handle); 120147564bfbSTheodore Ts'o goto retry_grab; 1202cf108bcaSJan Kara } 12037afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 12047afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1205cf108bcaSJan Kara 1206643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 12072058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 12082058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1209705965bdSJan Kara ext4_get_block_unwritten); 12102058f83aSMichael Halcrow else 12112058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 12122058f83aSMichael Halcrow ext4_get_block); 12132058f83aSMichael Halcrow #else 1214744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1215705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1216705965bdSJan Kara ext4_get_block_unwritten); 1217744692dcSJiaying Zhang else 12186e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 12192058f83aSMichael Halcrow #endif 1220bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1221188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, 1222188c299eSJan Kara page_buffers(page), from, to, NULL, 1223f19d5870STao Ma do_journal_get_write_access); 1224b46be050SAndrey Savochkin } 1225bfc1af65SNick Piggin 1226bfc1af65SNick Piggin if (ret) { 1227c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1228c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1229c93d8f88SEric Biggers 1230bfc1af65SNick Piggin unlock_page(page); 1231ae4d5372SAneesh Kumar K.V /* 12326e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1233ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1234f340b3d9Shongnanli * i_size_read because we hold i_rwsem. 12351938a150SAneesh Kumar K.V * 12361938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12371938a150SAneesh Kumar K.V * truncate finishes 1238ae4d5372SAneesh Kumar K.V */ 1239c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12401938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12411938a150SAneesh Kumar K.V 12421938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1243c93d8f88SEric Biggers if (extended) { 1244b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12451938a150SAneesh Kumar K.V /* 1246ffacfa7aSJan Kara * If truncate failed early the inode might 12471938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12481938a150SAneesh Kumar K.V * make sure the inode is removed from the 12491938a150SAneesh Kumar K.V * orphan list in that case. 12501938a150SAneesh Kumar K.V */ 12511938a150SAneesh Kumar K.V if (inode->i_nlink) 12521938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12531938a150SAneesh Kumar K.V } 1254bfc1af65SNick Piggin 125547564bfbSTheodore Ts'o if (ret == -ENOSPC && 125647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 125747564bfbSTheodore Ts'o goto retry_journal; 125809cbfeafSKirill A. Shutemov put_page(page); 125947564bfbSTheodore Ts'o return ret; 126047564bfbSTheodore Ts'o } 126147564bfbSTheodore Ts'o *pagep = page; 1262ac27a0ecSDave Kleikamp return ret; 1263ac27a0ecSDave Kleikamp } 1264ac27a0ecSDave Kleikamp 1265bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1266188c299eSJan Kara static int write_end_fn(handle_t *handle, struct inode *inode, 1267188c299eSJan Kara struct buffer_head *bh) 1268ac27a0ecSDave Kleikamp { 126913fca323STheodore Ts'o int ret; 1270ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1271ac27a0ecSDave Kleikamp return 0; 1272ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 127313fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 127413fca323STheodore Ts'o clear_buffer_meta(bh); 127513fca323STheodore Ts'o clear_buffer_prio(bh); 127613fca323STheodore Ts'o return ret; 1277ac27a0ecSDave Kleikamp } 1278ac27a0ecSDave Kleikamp 1279eed4333fSZheng Liu /* 1280eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1281eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1282eed4333fSZheng Liu * 1283eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1284eed4333fSZheng Liu * buffers are managed internally. 1285eed4333fSZheng Liu */ 1286eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1287f8514083SAneesh Kumar K.V struct address_space *mapping, 1288f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1289f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1290f8514083SAneesh Kumar K.V { 1291f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1292eed4333fSZheng Liu struct inode *inode = mapping->host; 12930572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1294eed4333fSZheng Liu int ret = 0, ret2; 1295eed4333fSZheng Liu int i_size_changed = 0; 1296c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1297eed4333fSZheng Liu 1298eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 12996984aef5SZhang Yi 13005c099c4fSYe Bin if (ext4_has_inline_data(inode) && 13015c099c4fSYe Bin ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) 13026984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 13036984aef5SZhang Yi 13046984aef5SZhang Yi copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1305f8514083SAneesh Kumar K.V /* 13064631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1307f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1308c93d8f88SEric Biggers * 1309c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1310c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1311f8514083SAneesh Kumar K.V */ 1312c93d8f88SEric Biggers if (!verity) 13134631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1314f8514083SAneesh Kumar K.V unlock_page(page); 131509cbfeafSKirill A. Shutemov put_page(page); 1316f8514083SAneesh Kumar K.V 1317c93d8f88SEric Biggers if (old_size < pos && !verity) 13180572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1319f8514083SAneesh Kumar K.V /* 1320f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1321f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1322f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1323f8514083SAneesh Kumar K.V * filesystems. 1324f8514083SAneesh Kumar K.V */ 13256984aef5SZhang Yi if (i_size_changed) 13264209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1327f8514083SAneesh Kumar K.V 1328c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1329f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1330f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1331f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1332f8514083SAneesh Kumar K.V */ 1333f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 133455ce2f64SZhang Yi 1335617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1336ac27a0ecSDave Kleikamp if (!ret) 1337ac27a0ecSDave Kleikamp ret = ret2; 1338bfc1af65SNick Piggin 1339c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1340b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1341f8514083SAneesh Kumar K.V /* 1342ffacfa7aSJan Kara * If truncate failed early the inode might still be 1343f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1344f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1345f8514083SAneesh Kumar K.V */ 1346f8514083SAneesh Kumar K.V if (inode->i_nlink) 1347f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1348f8514083SAneesh Kumar K.V } 1349f8514083SAneesh Kumar K.V 1350bfc1af65SNick Piggin return ret ? ret : copied; 1351ac27a0ecSDave Kleikamp } 1352ac27a0ecSDave Kleikamp 1353b90197b6STheodore Ts'o /* 1354b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1355b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1356b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1357b90197b6STheodore Ts'o */ 13583b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 1359188c299eSJan Kara struct inode *inode, 13603b136499SJan Kara struct page *page, 13613b136499SJan Kara unsigned from, unsigned to) 1362b90197b6STheodore Ts'o { 1363b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1364b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1365b90197b6STheodore Ts'o 1366b90197b6STheodore Ts'o bh = head = page_buffers(page); 1367b90197b6STheodore Ts'o do { 1368b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1369b90197b6STheodore Ts'o if (buffer_new(bh)) { 1370b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1371b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1372b90197b6STheodore Ts'o unsigned start, size; 1373b90197b6STheodore Ts'o 1374b90197b6STheodore Ts'o start = max(from, block_start); 1375b90197b6STheodore Ts'o size = min(to, block_end) - start; 1376b90197b6STheodore Ts'o 1377b90197b6STheodore Ts'o zero_user(page, start, size); 1378188c299eSJan Kara write_end_fn(handle, inode, bh); 1379b90197b6STheodore Ts'o } 1380b90197b6STheodore Ts'o clear_buffer_new(bh); 1381b90197b6STheodore Ts'o } 1382b90197b6STheodore Ts'o } 1383b90197b6STheodore Ts'o block_start = block_end; 1384b90197b6STheodore Ts'o bh = bh->b_this_page; 1385b90197b6STheodore Ts'o } while (bh != head); 1386b90197b6STheodore Ts'o } 1387b90197b6STheodore Ts'o 1388bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1389bfc1af65SNick Piggin struct address_space *mapping, 1390bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1391bfc1af65SNick Piggin struct page *page, void *fsdata) 1392ac27a0ecSDave Kleikamp { 1393617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1394bfc1af65SNick Piggin struct inode *inode = mapping->host; 13950572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1396ac27a0ecSDave Kleikamp int ret = 0, ret2; 1397ac27a0ecSDave Kleikamp int partial = 0; 1398bfc1af65SNick Piggin unsigned from, to; 13994631dbf6SDmitry Monakhov int size_changed = 0; 1400c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1401ac27a0ecSDave Kleikamp 14029bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 140309cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1404bfc1af65SNick Piggin to = from + len; 1405bfc1af65SNick Piggin 1406441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1407441c8508SCurt Wohlgemuth 14086984aef5SZhang Yi if (ext4_has_inline_data(inode)) 14096984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 14106984aef5SZhang Yi 14116984aef5SZhang Yi if (unlikely(copied < len) && !PageUptodate(page)) { 1412bfc1af65SNick Piggin copied = 0; 1413188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, from, to); 14143b136499SJan Kara } else { 14153b136499SJan Kara if (unlikely(copied < len)) 1416188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, 14173b136499SJan Kara from + copied, to); 1418188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_buffers(page), 1419188c299eSJan Kara from, from + copied, &partial, 14203b136499SJan Kara write_end_fn); 1421ac27a0ecSDave Kleikamp if (!partial) 1422ac27a0ecSDave Kleikamp SetPageUptodate(page); 14233fdcfb66STao Ma } 1424c93d8f88SEric Biggers if (!verity) 14254631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 142619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14272d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14284631dbf6SDmitry Monakhov unlock_page(page); 142909cbfeafSKirill A. Shutemov put_page(page); 14304631dbf6SDmitry Monakhov 1431c93d8f88SEric Biggers if (old_size < pos && !verity) 14320572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14330572639fSXiaoguang Wang 14346984aef5SZhang Yi if (size_changed) { 1435617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1436ac27a0ecSDave Kleikamp if (!ret) 1437ac27a0ecSDave Kleikamp ret = ret2; 1438ac27a0ecSDave Kleikamp } 1439bfc1af65SNick Piggin 1440c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1441f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1442f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1443f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1444f8514083SAneesh Kumar K.V */ 1445f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1446f8514083SAneesh Kumar K.V 1447617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1448ac27a0ecSDave Kleikamp if (!ret) 1449ac27a0ecSDave Kleikamp ret = ret2; 1450c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1451b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1452f8514083SAneesh Kumar K.V /* 1453ffacfa7aSJan Kara * If truncate failed early the inode might still be 1454f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1455f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1456f8514083SAneesh Kumar K.V */ 1457f8514083SAneesh Kumar K.V if (inode->i_nlink) 1458f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1459f8514083SAneesh Kumar K.V } 1460bfc1af65SNick Piggin 1461bfc1af65SNick Piggin return ret ? ret : copied; 1462ac27a0ecSDave Kleikamp } 1463d2a17637SMingming Cao 14649d0be502STheodore Ts'o /* 1465c27e43a1SEric Whitney * Reserve space for a single cluster 14669d0be502STheodore Ts'o */ 1467c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1468d2a17637SMingming Cao { 1469d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14700637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14715dd4056dSChristoph Hellwig int ret; 1472d2a17637SMingming Cao 147360e58e0fSMingming Cao /* 147472b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 147572b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 147672b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 147760e58e0fSMingming Cao */ 14787b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14795dd4056dSChristoph Hellwig if (ret) 14805dd4056dSChristoph Hellwig return ret; 148103179fe9STheodore Ts'o 148203179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 148371d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 148403179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 148503179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1486d2a17637SMingming Cao return -ENOSPC; 1487d2a17637SMingming Cao } 14889d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1489c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14900637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 149139bc680aSDmitry Monakhov 1492d2a17637SMingming Cao return 0; /* success */ 1493d2a17637SMingming Cao } 1494d2a17637SMingming Cao 1495f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1496d2a17637SMingming Cao { 1497d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14980637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1499d2a17637SMingming Cao 1500cd213226SMingming Cao if (!to_free) 1501cd213226SMingming Cao return; /* Nothing to release, exit */ 1502cd213226SMingming Cao 1503d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1504cd213226SMingming Cao 15055a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 15060637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1507cd213226SMingming Cao /* 15080637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 15090637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 15100637c6f4STheodore Ts'o * function is called from invalidate page, it's 15110637c6f4STheodore Ts'o * harmless to return without any action. 1512cd213226SMingming Cao */ 15138de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15140637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15151084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15160637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15170637c6f4STheodore Ts'o WARN_ON(1); 15180637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15190637c6f4STheodore Ts'o } 15200637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15210637c6f4STheodore Ts'o 152272b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 152357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1524d2a17637SMingming Cao 1525d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 152660e58e0fSMingming Cao 15277b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1528d2a17637SMingming Cao } 1529d2a17637SMingming Cao 1530ac27a0ecSDave Kleikamp /* 153164769240SAlex Tomas * Delayed allocation stuff 153264769240SAlex Tomas */ 153364769240SAlex Tomas 15344e7ea81dSJan Kara struct mpage_da_data { 153515648d59SJan Kara /* These are input fields for ext4_do_writepages() */ 15364e7ea81dSJan Kara struct inode *inode; 15374e7ea81dSJan Kara struct writeback_control *wbc; 153815648d59SJan Kara unsigned int can_map:1; /* Can writepages call map blocks? */ 15396b523df4SJan Kara 154015648d59SJan Kara /* These are internal state of ext4_do_writepages() */ 15414e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15424e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15434e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 154464769240SAlex Tomas /* 15454e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15464e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15474e7ea81dSJan Kara * is delalloc or unwritten. 154864769240SAlex Tomas */ 15494e7ea81dSJan Kara struct ext4_map_blocks map; 15504e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1551dddbd6acSJan Kara unsigned int do_map:1; 15526b8ed620SJan Kara unsigned int scanned_until_end:1; 15534e7ea81dSJan Kara }; 155464769240SAlex Tomas 15554e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15564e7ea81dSJan Kara bool invalidate) 1557c4a0c46eSAneesh Kumar K.V { 1558fb5a5be0SMatthew Wilcox (Oracle) unsigned nr, i; 1559c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1560fb5a5be0SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1561c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1562c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15634e7ea81dSJan Kara 15644e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15654e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15664e7ea81dSJan Kara return; 1567c4a0c46eSAneesh Kumar K.V 15686b8ed620SJan Kara mpd->scanned_until_end = 0; 1569c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1570c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15714e7ea81dSJan Kara if (invalidate) { 15724e7ea81dSJan Kara ext4_lblk_t start, last; 157309cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 157409cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 15757f0d8e1dSEric Whitney 15767f0d8e1dSEric Whitney /* 15777f0d8e1dSEric Whitney * avoid racing with extent status tree scans made by 15787f0d8e1dSEric Whitney * ext4_insert_delayed_block() 15797f0d8e1dSEric Whitney */ 15807f0d8e1dSEric Whitney down_write(&EXT4_I(inode)->i_data_sem); 158151865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15827f0d8e1dSEric Whitney up_write(&EXT4_I(inode)->i_data_sem); 15834e7ea81dSJan Kara } 158451865fdaSZheng Liu 1585fb5a5be0SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1586c4a0c46eSAneesh Kumar K.V while (index <= end) { 1587fb5a5be0SMatthew Wilcox (Oracle) nr = filemap_get_folios(mapping, &index, end, &fbatch); 1588fb5a5be0SMatthew Wilcox (Oracle) if (nr == 0) 1589c4a0c46eSAneesh Kumar K.V break; 1590fb5a5be0SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) { 1591fb5a5be0SMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 15922b85a617SJan Kara 1593fb5a5be0SMatthew Wilcox (Oracle) if (folio->index < mpd->first_page) 1594fb5a5be0SMatthew Wilcox (Oracle) continue; 1595fb5a5be0SMatthew Wilcox (Oracle) if (folio->index + folio_nr_pages(folio) - 1 > end) 1596fb5a5be0SMatthew Wilcox (Oracle) continue; 15977ba13abbSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 15987ba13abbSMatthew Wilcox (Oracle) BUG_ON(folio_test_writeback(folio)); 15994e7ea81dSJan Kara if (invalidate) { 16007ba13abbSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 16017ba13abbSMatthew Wilcox (Oracle) folio_clear_dirty_for_io(folio); 16027ba13abbSMatthew Wilcox (Oracle) block_invalidate_folio(folio, 0, 16037ba13abbSMatthew Wilcox (Oracle) folio_size(folio)); 16047ba13abbSMatthew Wilcox (Oracle) folio_clear_uptodate(folio); 16054e7ea81dSJan Kara } 16067ba13abbSMatthew Wilcox (Oracle) folio_unlock(folio); 1607c4a0c46eSAneesh Kumar K.V } 1608fb5a5be0SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1609c4a0c46eSAneesh Kumar K.V } 1610c4a0c46eSAneesh Kumar K.V } 1611c4a0c46eSAneesh Kumar K.V 1612df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1613df22291fSAneesh Kumar K.V { 1614df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 161592b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1616f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 161792b97816STheodore Ts'o 161892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16195dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1620f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 162192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 162292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1623f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 162457042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 162592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1626f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16277b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 162892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 162992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1630f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1631df22291fSAneesh Kumar K.V return; 1632df22291fSAneesh Kumar K.V } 1633df22291fSAneesh Kumar K.V 163464769240SAlex Tomas /* 16350b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16360b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16370b02f4c0SEric Whitney * count or making a pending reservation 16380b02f4c0SEric Whitney * where needed 16390b02f4c0SEric Whitney * 16400b02f4c0SEric Whitney * @inode - file containing the newly added block 16410b02f4c0SEric Whitney * @lblk - logical block to be added 16420b02f4c0SEric Whitney * 16430b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16440b02f4c0SEric Whitney */ 16450b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16460b02f4c0SEric Whitney { 16470b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16480b02f4c0SEric Whitney int ret; 16490b02f4c0SEric Whitney bool allocated = false; 16506fed8395SJeffle Xu bool reserved = false; 16510b02f4c0SEric Whitney 16520b02f4c0SEric Whitney /* 16530b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16540b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16550b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16560b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16570b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16580b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16590b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16600b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16610b02f4c0SEric Whitney * extents status tree doesn't get a match. 16620b02f4c0SEric Whitney */ 16630b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16640b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16650b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16660b02f4c0SEric Whitney goto errout; 16676fed8395SJeffle Xu reserved = true; 16680b02f4c0SEric Whitney } else { /* bigalloc */ 16690b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16700b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16710b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16720b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16730b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16740b02f4c0SEric Whitney if (ret < 0) 16750b02f4c0SEric Whitney goto errout; 16760b02f4c0SEric Whitney if (ret == 0) { 16770b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16780b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16790b02f4c0SEric Whitney goto errout; 16806fed8395SJeffle Xu reserved = true; 16810b02f4c0SEric Whitney } else { 16820b02f4c0SEric Whitney allocated = true; 16830b02f4c0SEric Whitney } 16840b02f4c0SEric Whitney } else { 16850b02f4c0SEric Whitney allocated = true; 16860b02f4c0SEric Whitney } 16870b02f4c0SEric Whitney } 16880b02f4c0SEric Whitney } 16890b02f4c0SEric Whitney 16900b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16916fed8395SJeffle Xu if (ret && reserved) 16926fed8395SJeffle Xu ext4_da_release_space(inode, 1); 16930b02f4c0SEric Whitney 16940b02f4c0SEric Whitney errout: 16950b02f4c0SEric Whitney return ret; 16960b02f4c0SEric Whitney } 16970b02f4c0SEric Whitney 16980b02f4c0SEric Whitney /* 16995356f261SAditya Kali * This function is grabs code from the very beginning of 17005356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17015356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17025356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17035356f261SAditya Kali */ 17045356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17055356f261SAditya Kali struct ext4_map_blocks *map, 17065356f261SAditya Kali struct buffer_head *bh) 17075356f261SAditya Kali { 1708d100eef2SZheng Liu struct extent_status es; 17095356f261SAditya Kali int retval; 17105356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1711921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1712921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1713921f266bSDmitry Monakhov 1714921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1715921f266bSDmitry Monakhov #endif 17165356f261SAditya Kali 17175356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17185356f261SAditya Kali invalid_block = ~0; 17195356f261SAditya Kali 17205356f261SAditya Kali map->m_flags = 0; 172170aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 17225356f261SAditya Kali (unsigned long) map->m_lblk); 1723d100eef2SZheng Liu 1724d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1725bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1726d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1727d100eef2SZheng Liu retval = 0; 1728c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1729d100eef2SZheng Liu goto add_delayed; 1730d100eef2SZheng Liu } 1731d100eef2SZheng Liu 1732d100eef2SZheng Liu /* 17333eda41dfSEric Whitney * Delayed extent could be allocated by fallocate. 17343eda41dfSEric Whitney * So we need to check it. 1735d100eef2SZheng Liu */ 17363eda41dfSEric Whitney if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 17373eda41dfSEric Whitney map_bh(bh, inode->i_sb, invalid_block); 17383eda41dfSEric Whitney set_buffer_new(bh); 17393eda41dfSEric Whitney set_buffer_delay(bh); 1740d100eef2SZheng Liu return 0; 1741d100eef2SZheng Liu } 1742d100eef2SZheng Liu 1743d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1744d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1745d100eef2SZheng Liu if (retval > map->m_len) 1746d100eef2SZheng Liu retval = map->m_len; 1747d100eef2SZheng Liu map->m_len = retval; 1748d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1749d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1750d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1751d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1752d100eef2SZheng Liu else 17531e83bc81SArnd Bergmann BUG(); 1754d100eef2SZheng Liu 1755921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1756921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1757921f266bSDmitry Monakhov #endif 1758d100eef2SZheng Liu return retval; 1759d100eef2SZheng Liu } 1760d100eef2SZheng Liu 17615356f261SAditya Kali /* 17625356f261SAditya Kali * Try to see if we can get the block without requesting a new 17635356f261SAditya Kali * file system block. 17645356f261SAditya Kali */ 1765c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1766cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17679c3569b5STao Ma retval = 0; 1768cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17692f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17705356f261SAditya Kali else 17712f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17725356f261SAditya Kali 1773d100eef2SZheng Liu add_delayed: 17745356f261SAditya Kali if (retval == 0) { 1775f7fec032SZheng Liu int ret; 1776ad431025SEric Whitney 17775356f261SAditya Kali /* 17785356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17795356f261SAditya Kali * is it OK? 17805356f261SAditya Kali */ 17815356f261SAditya Kali 17820b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17830b02f4c0SEric Whitney if (ret != 0) { 1784f7fec032SZheng Liu retval = ret; 178551865fdaSZheng Liu goto out_unlock; 1786f7fec032SZheng Liu } 178751865fdaSZheng Liu 17885356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17895356f261SAditya Kali set_buffer_new(bh); 17905356f261SAditya Kali set_buffer_delay(bh); 1791f7fec032SZheng Liu } else if (retval > 0) { 1792f7fec032SZheng Liu int ret; 17933be78c73STheodore Ts'o unsigned int status; 1794f7fec032SZheng Liu 179544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 179644fb851dSZheng Liu ext4_warning(inode->i_sb, 179744fb851dSZheng Liu "ES len assertion failed for inode " 179844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 179944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 180044fb851dSZheng Liu WARN_ON(1); 1801921f266bSDmitry Monakhov } 1802921f266bSDmitry Monakhov 1803f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1804f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1805f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1806f7fec032SZheng Liu map->m_pblk, status); 1807f7fec032SZheng Liu if (ret != 0) 1808f7fec032SZheng Liu retval = ret; 18095356f261SAditya Kali } 18105356f261SAditya Kali 18115356f261SAditya Kali out_unlock: 18125356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18135356f261SAditya Kali 18145356f261SAditya Kali return retval; 18155356f261SAditya Kali } 18165356f261SAditya Kali 18175356f261SAditya Kali /* 1818d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1819b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1820b920c755STheodore Ts'o * reserve space for a single block. 182129fa89d0SAneesh Kumar K.V * 182229fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 182329fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 182429fa89d0SAneesh Kumar K.V * 182529fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 182629fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 182729fa89d0SAneesh Kumar K.V * initialized properly. 182864769240SAlex Tomas */ 18299c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18302ed88685STheodore Ts'o struct buffer_head *bh, int create) 183164769240SAlex Tomas { 18322ed88685STheodore Ts'o struct ext4_map_blocks map; 183364769240SAlex Tomas int ret = 0; 183464769240SAlex Tomas 183564769240SAlex Tomas BUG_ON(create == 0); 18362ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18372ed88685STheodore Ts'o 18382ed88685STheodore Ts'o map.m_lblk = iblock; 18392ed88685STheodore Ts'o map.m_len = 1; 184064769240SAlex Tomas 184164769240SAlex Tomas /* 184264769240SAlex Tomas * first, we need to know whether the block is allocated already 184364769240SAlex Tomas * preallocated blocks are unmapped but should treated 184464769240SAlex Tomas * the same as allocated blocks. 184564769240SAlex Tomas */ 18465356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18475356f261SAditya Kali if (ret <= 0) 18482ed88685STheodore Ts'o return ret; 184964769240SAlex Tomas 18502ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1851ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18522ed88685STheodore Ts'o 18532ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18542ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18552ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18562ed88685STheodore Ts'o * get_block multiple times when we write to the same 18572ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18582ed88685STheodore Ts'o * for partial write. 18592ed88685STheodore Ts'o */ 18602ed88685STheodore Ts'o set_buffer_new(bh); 1861c8205636STheodore Ts'o set_buffer_mapped(bh); 18622ed88685STheodore Ts'o } 18632ed88685STheodore Ts'o return 0; 186464769240SAlex Tomas } 186561628a3fSMingming Cao 1866*33483b3bSMatthew Wilcox static void mpage_folio_done(struct mpage_da_data *mpd, struct folio *folio) 1867eaf2ca10SJan Kara { 1868*33483b3bSMatthew Wilcox mpd->first_page += folio_nr_pages(folio); 1869*33483b3bSMatthew Wilcox folio_unlock(folio); 1870eaf2ca10SJan Kara } 1871eaf2ca10SJan Kara 187281a0d3e1SMatthew Wilcox static int mpage_submit_folio(struct mpage_da_data *mpd, struct folio *folio) 18735f1132b2SJan Kara { 187481a0d3e1SMatthew Wilcox size_t len; 1875a056bdaaSJan Kara loff_t size; 18765f1132b2SJan Kara int err; 18775f1132b2SJan Kara 187881a0d3e1SMatthew Wilcox BUG_ON(folio->index != mpd->first_page); 187981a0d3e1SMatthew Wilcox folio_clear_dirty_for_io(folio); 1880a056bdaaSJan Kara /* 1881a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 1882a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 1883a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 188481a0d3e1SMatthew Wilcox * data through mmap while writeback runs. folio_clear_dirty_for_io() 1885a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 188681a0d3e1SMatthew Wilcox * written to again until we release folio lock. So only after 188781a0d3e1SMatthew Wilcox * folio_clear_dirty_for_io() we are safe to sample i_size for 1888a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 1889a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 189081a0d3e1SMatthew Wilcox * folio_clear_dirty_for_io() to make sure i_size is really sampled only 1891a056bdaaSJan Kara * after page tables are updated. 1892a056bdaaSJan Kara */ 1893a056bdaaSJan Kara size = i_size_read(mpd->inode); 189481a0d3e1SMatthew Wilcox len = folio_size(folio); 189581a0d3e1SMatthew Wilcox if (folio_pos(folio) + len > size && 1896c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 189709cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 189881a0d3e1SMatthew Wilcox err = ext4_bio_write_page(&mpd->io_submit, &folio->page, len); 18995f1132b2SJan Kara if (!err) 19005f1132b2SJan Kara mpd->wbc->nr_to_write--; 19015f1132b2SJan Kara 19025f1132b2SJan Kara return err; 19035f1132b2SJan Kara } 19045f1132b2SJan Kara 19056db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 19064e7ea81dSJan Kara 190761628a3fSMingming Cao /* 1908fffb2739SJan Kara * mballoc gives us at most this number of blocks... 1909fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 1910fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 191161628a3fSMingming Cao */ 1912fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 1913525f4ed8SMingming Cao 1914525f4ed8SMingming Cao /* 19154e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 19164e7ea81dSJan Kara * 19174e7ea81dSJan Kara * @mpd - extent of blocks 19184e7ea81dSJan Kara * @lblk - logical number of the block in the file 191909930042SJan Kara * @bh - buffer head we want to add to the extent 19204e7ea81dSJan Kara * 192109930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 192209930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 192309930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 192409930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 192509930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 192609930042SJan Kara * added. 19274e7ea81dSJan Kara */ 192809930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 192909930042SJan Kara struct buffer_head *bh) 19304e7ea81dSJan Kara { 19314e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 19324e7ea81dSJan Kara 193309930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 193409930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 193509930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 193609930042SJan Kara /* So far no extent to map => we write the buffer right away */ 193709930042SJan Kara if (map->m_len == 0) 193809930042SJan Kara return true; 193909930042SJan Kara return false; 194009930042SJan Kara } 19414e7ea81dSJan Kara 19424e7ea81dSJan Kara /* First block in the extent? */ 19434e7ea81dSJan Kara if (map->m_len == 0) { 1944dddbd6acSJan Kara /* We cannot map unless handle is started... */ 1945dddbd6acSJan Kara if (!mpd->do_map) 1946dddbd6acSJan Kara return false; 19474e7ea81dSJan Kara map->m_lblk = lblk; 19484e7ea81dSJan Kara map->m_len = 1; 194909930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 195009930042SJan Kara return true; 19514e7ea81dSJan Kara } 19524e7ea81dSJan Kara 195309930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 195409930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 195509930042SJan Kara return false; 195609930042SJan Kara 19574e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 19584e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 195909930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 19604e7ea81dSJan Kara map->m_len++; 196109930042SJan Kara return true; 19624e7ea81dSJan Kara } 196309930042SJan Kara return false; 19644e7ea81dSJan Kara } 19654e7ea81dSJan Kara 19665f1132b2SJan Kara /* 19675f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 19685f1132b2SJan Kara * 19695f1132b2SJan Kara * @mpd - extent of blocks for mapping 19705f1132b2SJan Kara * @head - the first buffer in the page 19715f1132b2SJan Kara * @bh - buffer we should start processing from 19725f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 19735f1132b2SJan Kara * 19745f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 19755f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 19765f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 19775f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 19785f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 19795f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 19805f1132b2SJan Kara * < 0 in case of error during IO submission. 19815f1132b2SJan Kara */ 19825f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 19834e7ea81dSJan Kara struct buffer_head *head, 19844e7ea81dSJan Kara struct buffer_head *bh, 19854e7ea81dSJan Kara ext4_lblk_t lblk) 19864e7ea81dSJan Kara { 19874e7ea81dSJan Kara struct inode *inode = mpd->inode; 19885f1132b2SJan Kara int err; 198993407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 19904e7ea81dSJan Kara >> inode->i_blkbits; 19914e7ea81dSJan Kara 1992c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 1993c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 1994c93d8f88SEric Biggers 19954e7ea81dSJan Kara do { 19964e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 19974e7ea81dSJan Kara 199809930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 19994e7ea81dSJan Kara /* Found extent to map? */ 20004e7ea81dSJan Kara if (mpd->map.m_len) 20015f1132b2SJan Kara return 0; 2002dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2003dddbd6acSJan Kara if (!mpd->do_map) 2004dddbd6acSJan Kara return 0; 200509930042SJan Kara /* Everything mapped so far and we hit EOF */ 20065f1132b2SJan Kara break; 20074e7ea81dSJan Kara } 20084e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 20095f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 20105f1132b2SJan Kara if (mpd->map.m_len == 0) { 201181a0d3e1SMatthew Wilcox err = mpage_submit_folio(mpd, head->b_folio); 20125f1132b2SJan Kara if (err < 0) 20134e7ea81dSJan Kara return err; 2014*33483b3bSMatthew Wilcox mpage_folio_done(mpd, head->b_folio); 20154e7ea81dSJan Kara } 20166b8ed620SJan Kara if (lblk >= blocks) { 20176b8ed620SJan Kara mpd->scanned_until_end = 1; 20186b8ed620SJan Kara return 0; 20196b8ed620SJan Kara } 20206b8ed620SJan Kara return 1; 20215f1132b2SJan Kara } 20224e7ea81dSJan Kara 20234e7ea81dSJan Kara /* 20244da2f6e3SMatthew Wilcox * mpage_process_folio - update folio buffers corresponding to changed extent 20254da2f6e3SMatthew Wilcox * and may submit fully mapped page for IO 20264da2f6e3SMatthew Wilcox * @mpd: description of extent to map, on return next extent to map 20274da2f6e3SMatthew Wilcox * @folio: Contains these buffers. 20284da2f6e3SMatthew Wilcox * @m_lblk: logical block mapping. 20294da2f6e3SMatthew Wilcox * @m_pblk: corresponding physical mapping. 20304da2f6e3SMatthew Wilcox * @map_bh: determines on return whether this page requires any further 20312943fdbcSRitesh Harjani * mapping or not. 20324da2f6e3SMatthew Wilcox * 20334da2f6e3SMatthew Wilcox * Scan given folio buffers corresponding to changed extent and update buffer 20342943fdbcSRitesh Harjani * state according to new extent state. 20352943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 20364da2f6e3SMatthew Wilcox * If the given folio is not fully mapped, we update @mpd to the next extent in 20374da2f6e3SMatthew Wilcox * the given folio that needs mapping & return @map_bh as true. 20382943fdbcSRitesh Harjani */ 20394da2f6e3SMatthew Wilcox static int mpage_process_folio(struct mpage_da_data *mpd, struct folio *folio, 20402943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 20412943fdbcSRitesh Harjani bool *map_bh) 20422943fdbcSRitesh Harjani { 20432943fdbcSRitesh Harjani struct buffer_head *head, *bh; 20442943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 20452943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 20462943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 20472943fdbcSRitesh Harjani int err = 0; 2048c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2049c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2050c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 20512943fdbcSRitesh Harjani 20524da2f6e3SMatthew Wilcox bh = head = folio_buffers(folio); 20532943fdbcSRitesh Harjani do { 20542943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 20552943fdbcSRitesh Harjani continue; 20562943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 20572943fdbcSRitesh Harjani /* 20582943fdbcSRitesh Harjani * Buffer after end of mapped extent. 20594da2f6e3SMatthew Wilcox * Find next buffer in the folio to map. 20602943fdbcSRitesh Harjani */ 20612943fdbcSRitesh Harjani mpd->map.m_len = 0; 20622943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2063c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 20642943fdbcSRitesh Harjani 20652943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 20662943fdbcSRitesh Harjani if (err > 0) 20672943fdbcSRitesh Harjani err = 0; 2068c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2069c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 20704d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 20714d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 20724d06bfb9SRitesh Harjani goto out; 20734d06bfb9SRitesh Harjani } 2074d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2075c8cc8816SRitesh Harjani } 20762943fdbcSRitesh Harjani *map_bh = true; 20772943fdbcSRitesh Harjani goto out; 20782943fdbcSRitesh Harjani } 20792943fdbcSRitesh Harjani if (buffer_delay(bh)) { 20802943fdbcSRitesh Harjani clear_buffer_delay(bh); 20812943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 20822943fdbcSRitesh Harjani } 20832943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2084c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 20852943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2086c8cc8816SRitesh Harjani 2087c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 20882943fdbcSRitesh Harjani *map_bh = false; 20892943fdbcSRitesh Harjani out: 20902943fdbcSRitesh Harjani *m_lblk = lblk; 20912943fdbcSRitesh Harjani *m_pblk = pblock; 20922943fdbcSRitesh Harjani return err; 20932943fdbcSRitesh Harjani } 20942943fdbcSRitesh Harjani 20952943fdbcSRitesh Harjani /* 20964e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 20974e7ea81dSJan Kara * submit fully mapped pages for IO 20984e7ea81dSJan Kara * 20994e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 21004e7ea81dSJan Kara * 21014e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 21024e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 21034e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2104556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 21054e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 21064e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 21074e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 21084e7ea81dSJan Kara */ 21094e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 21104e7ea81dSJan Kara { 21117530d093SMatthew Wilcox (Oracle) struct folio_batch fbatch; 21127530d093SMatthew Wilcox (Oracle) unsigned nr, i; 21134e7ea81dSJan Kara struct inode *inode = mpd->inode; 211409cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 21154e7ea81dSJan Kara pgoff_t start, end; 21164e7ea81dSJan Kara ext4_lblk_t lblk; 21172943fdbcSRitesh Harjani ext4_fsblk_t pblock; 21184e7ea81dSJan Kara int err; 21192943fdbcSRitesh Harjani bool map_bh = false; 21204e7ea81dSJan Kara 21214e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 21224e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 21234e7ea81dSJan Kara lblk = start << bpp_bits; 21244e7ea81dSJan Kara pblock = mpd->map.m_pblk; 21254e7ea81dSJan Kara 21267530d093SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 21274e7ea81dSJan Kara while (start <= end) { 21287530d093SMatthew Wilcox (Oracle) nr = filemap_get_folios(inode->i_mapping, &start, end, &fbatch); 21297530d093SMatthew Wilcox (Oracle) if (nr == 0) 21304e7ea81dSJan Kara break; 21317530d093SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) { 21324da2f6e3SMatthew Wilcox struct folio *folio = fbatch.folios[i]; 21334e7ea81dSJan Kara 21344da2f6e3SMatthew Wilcox err = mpage_process_folio(mpd, folio, &lblk, &pblock, 21352943fdbcSRitesh Harjani &map_bh); 21364e7ea81dSJan Kara /* 21372943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 21382943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 21392943fdbcSRitesh Harjani * So we return to call further extent mapping. 21404e7ea81dSJan Kara */ 214139c0ae16SJason Yan if (err < 0 || map_bh) 21422943fdbcSRitesh Harjani goto out; 21434e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 214481a0d3e1SMatthew Wilcox err = mpage_submit_folio(mpd, folio); 21452943fdbcSRitesh Harjani if (err < 0) 21462943fdbcSRitesh Harjani goto out; 2147*33483b3bSMatthew Wilcox mpage_folio_done(mpd, folio); 21484e7ea81dSJan Kara } 21497530d093SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 21504e7ea81dSJan Kara } 21514e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 21524e7ea81dSJan Kara mpd->map.m_len = 0; 21534e7ea81dSJan Kara mpd->map.m_flags = 0; 21544e7ea81dSJan Kara return 0; 21552943fdbcSRitesh Harjani out: 21567530d093SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 21572943fdbcSRitesh Harjani return err; 21584e7ea81dSJan Kara } 21594e7ea81dSJan Kara 21604e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 21614e7ea81dSJan Kara { 21624e7ea81dSJan Kara struct inode *inode = mpd->inode; 21634e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21644e7ea81dSJan Kara int get_blocks_flags; 2165090f32eeSLukas Czerner int err, dioread_nolock; 21664e7ea81dSJan Kara 21674e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 21684e7ea81dSJan Kara /* 21694e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2170556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 21714e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 21724e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 21734e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 21744e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 21754e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 21764e7ea81dSJan Kara * possible. 21774e7ea81dSJan Kara * 2178754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2179754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2180754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2181754cfed6STheodore Ts'o * the data was copied into the page cache. 21824e7ea81dSJan Kara */ 21834e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2184ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2185ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2186090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2187090f32eeSLukas Czerner if (dioread_nolock) 21884e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 21896db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 21904e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 21914e7ea81dSJan Kara 21924e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 21934e7ea81dSJan Kara if (err < 0) 21944e7ea81dSJan Kara return err; 2195090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 21966b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 21976b523df4SJan Kara ext4_handle_valid(handle)) { 21986b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 21996b523df4SJan Kara handle->h_rsv_handle = NULL; 22006b523df4SJan Kara } 22013613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 22026b523df4SJan Kara } 22034e7ea81dSJan Kara 22044e7ea81dSJan Kara BUG_ON(map->m_len == 0); 22054e7ea81dSJan Kara return 0; 22064e7ea81dSJan Kara } 22074e7ea81dSJan Kara 22084e7ea81dSJan Kara /* 22094e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 22104e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 22114e7ea81dSJan Kara * 22124e7ea81dSJan Kara * @handle - handle for journal operations 22134e7ea81dSJan Kara * @mpd - extent to map 22147534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 22157534e854SJan Kara * is no hope of writing the data. The caller should discard 22167534e854SJan Kara * dirty pages to avoid infinite loops. 22174e7ea81dSJan Kara * 22184e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 22194e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 22204e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 22214e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 22224e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 22234e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 22244e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 22254e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 22264e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 22274e7ea81dSJan Kara */ 22284e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2229cb530541STheodore Ts'o struct mpage_da_data *mpd, 2230cb530541STheodore Ts'o bool *give_up_on_write) 22314e7ea81dSJan Kara { 22324e7ea81dSJan Kara struct inode *inode = mpd->inode; 22334e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 22344e7ea81dSJan Kara int err; 22354e7ea81dSJan Kara loff_t disksize; 22366603120eSDmitry Monakhov int progress = 0; 2237c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22384d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 22394e7ea81dSJan Kara 22404d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22414d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 22424d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2243c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 224427d7c4edSJan Kara do { 22454e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 22464e7ea81dSJan Kara if (err < 0) { 22474e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 22484e7ea81dSJan Kara 22490db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 22509b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2251cb530541STheodore Ts'o goto invalidate_dirty_pages; 22524e7ea81dSJan Kara /* 2253cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2254cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2255cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 22564e7ea81dSJan Kara */ 2257cb530541STheodore Ts'o if ((err == -ENOMEM) || 22586603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 22596603120eSDmitry Monakhov if (progress) 22606603120eSDmitry Monakhov goto update_disksize; 2261cb530541STheodore Ts'o return err; 22626603120eSDmitry Monakhov } 22634e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 22644e7ea81dSJan Kara "Delayed block allocation failed for " 22654e7ea81dSJan Kara "inode %lu at logical offset %llu with" 22664e7ea81dSJan Kara " max blocks %u with error %d", 22674e7ea81dSJan Kara inode->i_ino, 22684e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2269cb530541STheodore Ts'o (unsigned)map->m_len, -err); 22704e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 22714e7ea81dSJan Kara "This should not happen!! Data will " 22724e7ea81dSJan Kara "be lost\n"); 22734e7ea81dSJan Kara if (err == -ENOSPC) 22744e7ea81dSJan Kara ext4_print_free_blocks(inode); 2275cb530541STheodore Ts'o invalidate_dirty_pages: 2276cb530541STheodore Ts'o *give_up_on_write = true; 22774e7ea81dSJan Kara return err; 22784e7ea81dSJan Kara } 22796603120eSDmitry Monakhov progress = 1; 22804e7ea81dSJan Kara /* 22814e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 22824e7ea81dSJan Kara * extent to map 22834e7ea81dSJan Kara */ 22844e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 22854e7ea81dSJan Kara if (err < 0) 22866603120eSDmitry Monakhov goto update_disksize; 228727d7c4edSJan Kara } while (map->m_len); 22884e7ea81dSJan Kara 22896603120eSDmitry Monakhov update_disksize: 2290622cad13STheodore Ts'o /* 2291622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2292622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2293622cad13STheodore Ts'o */ 229409cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 229535df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 22964e7ea81dSJan Kara int err2; 2297622cad13STheodore Ts'o loff_t i_size; 22984e7ea81dSJan Kara 2299622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2300622cad13STheodore Ts'o i_size = i_size_read(inode); 2301622cad13STheodore Ts'o if (disksize > i_size) 2302622cad13STheodore Ts'o disksize = i_size; 2303622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2304622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2305622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2306b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2307878520acSTheodore Ts'o if (err2) { 230854d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 23094e7ea81dSJan Kara "Failed to mark inode %lu dirty", 23104e7ea81dSJan Kara inode->i_ino); 2311878520acSTheodore Ts'o } 23124e7ea81dSJan Kara if (!err) 23134e7ea81dSJan Kara err = err2; 23144e7ea81dSJan Kara } 23154e7ea81dSJan Kara return err; 23164e7ea81dSJan Kara } 23174e7ea81dSJan Kara 23184e7ea81dSJan Kara /* 2319fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 232020970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2321fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2322fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2323fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2324525f4ed8SMingming Cao */ 2325fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2326fffb2739SJan Kara { 2327fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2328525f4ed8SMingming Cao 2329fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2330fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2331525f4ed8SMingming Cao } 233261628a3fSMingming Cao 2333de0039f6SJan Kara /* Return true if the page needs to be written as part of transaction commit */ 2334de0039f6SJan Kara static bool ext4_page_nomap_can_writeout(struct page *page) 2335de0039f6SJan Kara { 2336de0039f6SJan Kara struct buffer_head *bh, *head; 2337de0039f6SJan Kara 2338de0039f6SJan Kara bh = head = page_buffers(page); 2339de0039f6SJan Kara do { 2340de0039f6SJan Kara if (buffer_dirty(bh) && buffer_mapped(bh) && !buffer_delay(bh)) 2341de0039f6SJan Kara return true; 2342de0039f6SJan Kara } while ((bh = bh->b_this_page) != head); 2343de0039f6SJan Kara return false; 2344de0039f6SJan Kara } 2345de0039f6SJan Kara 23463f079114SJan Kara static int ext4_journal_page_buffers(handle_t *handle, struct page *page, 23473f079114SJan Kara int len) 23483f079114SJan Kara { 23493f079114SJan Kara struct buffer_head *page_bufs = page_buffers(page); 23503f079114SJan Kara struct inode *inode = page->mapping->host; 23513f079114SJan Kara int ret, err; 23523f079114SJan Kara 23533f079114SJan Kara ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 23543f079114SJan Kara NULL, do_journal_get_write_access); 23553f079114SJan Kara err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 23563f079114SJan Kara NULL, write_end_fn); 23573f079114SJan Kara if (ret == 0) 23583f079114SJan Kara ret = err; 23593f079114SJan Kara err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 23603f079114SJan Kara if (ret == 0) 23613f079114SJan Kara ret = err; 23623f079114SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 23633f079114SJan Kara 23643f079114SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 23653f079114SJan Kara 23663f079114SJan Kara return ret; 23673f079114SJan Kara } 23683f079114SJan Kara 23693f079114SJan Kara static int mpage_journal_page_buffers(handle_t *handle, 23703f079114SJan Kara struct mpage_da_data *mpd, 23713f079114SJan Kara struct page *page) 23723f079114SJan Kara { 23733f079114SJan Kara struct inode *inode = mpd->inode; 23743f079114SJan Kara loff_t size = i_size_read(inode); 23753f079114SJan Kara int len; 23763f079114SJan Kara 23773f079114SJan Kara ClearPageChecked(page); 23783f079114SJan Kara clear_page_dirty_for_io(page); 23793f079114SJan Kara mpd->wbc->nr_to_write--; 23803f079114SJan Kara 23813f079114SJan Kara if (page->index == size >> PAGE_SHIFT && 23823f079114SJan Kara !ext4_verity_in_progress(inode)) 23833f079114SJan Kara len = size & ~PAGE_MASK; 23843f079114SJan Kara else 23853f079114SJan Kara len = PAGE_SIZE; 23863f079114SJan Kara 23873f079114SJan Kara return ext4_journal_page_buffers(handle, page, len); 23883f079114SJan Kara } 23893f079114SJan Kara 23908e48dcfbSTheodore Ts'o /* 23914e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 2392de0039f6SJan Kara * needing mapping, submit mapped pages 23934e7ea81dSJan Kara * 23944e7ea81dSJan Kara * @mpd - where to look for pages 23954e7ea81dSJan Kara * 23964e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 2397de0039f6SJan Kara * IO immediately. If we cannot map blocks, we submit just already mapped 2398de0039f6SJan Kara * buffers in the page for IO and keep page dirty. When we can map blocks and 2399de0039f6SJan Kara * we find a page which isn't mapped we start accumulating extent of buffers 2400de0039f6SJan Kara * underlying these pages that needs mapping (formed by either delayed or 2401de0039f6SJan Kara * unwritten buffers). We also lock the pages containing these buffers. The 2402de0039f6SJan Kara * extent found is returned in @mpd structure (starting at mpd->lblk with 2403de0039f6SJan Kara * length mpd->len blocks). 24044e7ea81dSJan Kara * 24054e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 24064e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 24074e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 24084e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 24098e48dcfbSTheodore Ts'o */ 24104e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 24118e48dcfbSTheodore Ts'o { 24124e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 2413e6c28a26SJan Kara struct super_block *sb = mpd->inode->i_sb; 241450ead253SVishal Moola (Oracle) struct folio_batch fbatch; 241550ead253SVishal Moola (Oracle) unsigned int nr_folios; 24164e7ea81dSJan Kara pgoff_t index = mpd->first_page; 24174e7ea81dSJan Kara pgoff_t end = mpd->last_page; 241810bbd235SMatthew Wilcox xa_mark_t tag; 24194e7ea81dSJan Kara int i, err = 0; 24204e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 24214e7ea81dSJan Kara ext4_lblk_t lblk; 24224e7ea81dSJan Kara struct buffer_head *head; 24233f079114SJan Kara handle_t *handle = NULL; 24243f079114SJan Kara int bpp = ext4_journal_blocks_per_page(mpd->inode); 24258e48dcfbSTheodore Ts'o 24264e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 24275b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 24285b41d924SEric Sandeen else 24295b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 24303f079114SJan Kara 2431e6c28a26SJan Kara mpd->map.m_len = 0; 2432e6c28a26SJan Kara mpd->next_page = index; 2433e6c28a26SJan Kara /* 2434e6c28a26SJan Kara * Start a transaction for writeback of journalled data. We don't start 243598ccceeeSTheodore Ts'o * the transaction if the filesystem is frozen. In that case we 2436e6c28a26SJan Kara * should not have any dirty data to write anymore but possibly there 2437e6c28a26SJan Kara * are stray page dirty bits left by the checkpointing code so this 2438e6c28a26SJan Kara * loop clears them. 2439e6c28a26SJan Kara */ 2440e6c28a26SJan Kara if (ext4_should_journal_data(mpd->inode) && 2441e6c28a26SJan Kara sb->s_writers.frozen < SB_FREEZE_FS) { 24423f079114SJan Kara handle = ext4_journal_start(mpd->inode, EXT4_HT_WRITE_PAGE, 24433f079114SJan Kara bpp); 24443f079114SJan Kara if (IS_ERR(handle)) 24453f079114SJan Kara return PTR_ERR(handle); 24463f079114SJan Kara } 244750ead253SVishal Moola (Oracle) folio_batch_init(&fbatch); 24484f01b02cSTheodore Ts'o while (index <= end) { 244950ead253SVishal Moola (Oracle) nr_folios = filemap_get_folios_tag(mapping, &index, end, 245050ead253SVishal Moola (Oracle) tag, &fbatch); 245150ead253SVishal Moola (Oracle) if (nr_folios == 0) 24526b8ed620SJan Kara break; 24538e48dcfbSTheodore Ts'o 245450ead253SVishal Moola (Oracle) for (i = 0; i < nr_folios; i++) { 245550ead253SVishal Moola (Oracle) struct folio *folio = fbatch.folios[i]; 24568e48dcfbSTheodore Ts'o 24578e48dcfbSTheodore Ts'o /* 2458aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2459aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2460aeac589aSMing Lei * keep going because someone may be concurrently 2461aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2462aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2463aeac589aSMing Lei * of the old dirty pages. 2464aeac589aSMing Lei */ 2465c8e8e16dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_NONE && 2466c8e8e16dSJan Kara mpd->wbc->nr_to_write <= 2467c8e8e16dSJan Kara mpd->map.m_len >> (PAGE_SHIFT - blkbits)) 2468aeac589aSMing Lei goto out; 2469aeac589aSMing Lei 24704e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 247150ead253SVishal Moola (Oracle) if (mpd->map.m_len > 0 && mpd->next_page != folio->index) 24724e7ea81dSJan Kara goto out; 247378aaced3STheodore Ts'o 24743f079114SJan Kara if (handle) { 24753f079114SJan Kara err = ext4_journal_ensure_credits(handle, bpp, 24763f079114SJan Kara 0); 24773f079114SJan Kara if (err < 0) 24783f079114SJan Kara goto out; 24793f079114SJan Kara } 24803f079114SJan Kara 248150ead253SVishal Moola (Oracle) folio_lock(folio); 24828e48dcfbSTheodore Ts'o /* 24834e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 24844e7ea81dSJan Kara * longer corresponds to inode we are writing (which 24854e7ea81dSJan Kara * means it has been truncated or invalidated), or the 24864e7ea81dSJan Kara * page is already under writeback and we are not doing 24874e7ea81dSJan Kara * a data integrity writeback, skip the page 24888e48dcfbSTheodore Ts'o */ 248950ead253SVishal Moola (Oracle) if (!folio_test_dirty(folio) || 249050ead253SVishal Moola (Oracle) (folio_test_writeback(folio) && 24914e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 249250ead253SVishal Moola (Oracle) unlikely(folio->mapping != mapping)) { 249350ead253SVishal Moola (Oracle) folio_unlock(folio); 24948e48dcfbSTheodore Ts'o continue; 24958e48dcfbSTheodore Ts'o } 24968e48dcfbSTheodore Ts'o 249750ead253SVishal Moola (Oracle) folio_wait_writeback(folio); 249850ead253SVishal Moola (Oracle) BUG_ON(folio_test_writeback(folio)); 24998e48dcfbSTheodore Ts'o 2500cc509574STheodore Ts'o /* 2501cc509574STheodore Ts'o * Should never happen but for buggy code in 2502cc509574STheodore Ts'o * other subsystems that call 2503cc509574STheodore Ts'o * set_page_dirty() without properly warning 2504cc509574STheodore Ts'o * the file system first. See [1] for more 2505cc509574STheodore Ts'o * information. 2506cc509574STheodore Ts'o * 2507cc509574STheodore Ts'o * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz 2508cc509574STheodore Ts'o */ 250950ead253SVishal Moola (Oracle) if (!folio_buffers(folio)) { 251050ead253SVishal Moola (Oracle) ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", folio->index); 251150ead253SVishal Moola (Oracle) folio_clear_dirty(folio); 251250ead253SVishal Moola (Oracle) folio_unlock(folio); 2513cc509574STheodore Ts'o continue; 2514cc509574STheodore Ts'o } 2515cc509574STheodore Ts'o 25164e7ea81dSJan Kara if (mpd->map.m_len == 0) 251750ead253SVishal Moola (Oracle) mpd->first_page = folio->index; 251850ead253SVishal Moola (Oracle) mpd->next_page = folio->index + folio_nr_pages(folio); 2519de0039f6SJan Kara /* 25203f079114SJan Kara * Writeout when we cannot modify metadata is simple. 25213f079114SJan Kara * Just submit the page. For data=journal mode we 25223f079114SJan Kara * first handle writeout of the page for checkpoint and 25233f079114SJan Kara * only after that handle delayed page dirtying. This 25243f079114SJan Kara * is crutial so that forcing a transaction commit and 25253f079114SJan Kara * then calling filemap_write_and_wait() guarantees 25263f079114SJan Kara * current state of data is in its final location. Such 25273f079114SJan Kara * sequence is used for example by insert/collapse 25283f079114SJan Kara * range operations before discarding the page cache. 2529de0039f6SJan Kara */ 2530de0039f6SJan Kara if (!mpd->can_map) { 2531d8be7607SJan Kara if (ext4_page_nomap_can_writeout(&folio->page)) { 2532e6c28a26SJan Kara WARN_ON_ONCE(sb->s_writers.frozen == 2533e6c28a26SJan Kara SB_FREEZE_COMPLETE); 253481a0d3e1SMatthew Wilcox err = mpage_submit_folio(mpd, folio); 2535de0039f6SJan Kara if (err < 0) 2536de0039f6SJan Kara goto out; 2537d8be7607SJan Kara } 25383f079114SJan Kara /* Pending dirtying of journalled data? */ 253981a0d3e1SMatthew Wilcox if (folio_test_checked(folio)) { 2540e6c28a26SJan Kara WARN_ON_ONCE(sb->s_writers.frozen >= 2541e6c28a26SJan Kara SB_FREEZE_FS); 25423f079114SJan Kara err = mpage_journal_page_buffers(handle, 25433f079114SJan Kara mpd, &folio->page); 25443f079114SJan Kara if (err < 0) 25453f079114SJan Kara goto out; 25463f079114SJan Kara } 2547*33483b3bSMatthew Wilcox mpage_folio_done(mpd, folio); 2548de0039f6SJan Kara } else { 2549f8bec370SJan Kara /* Add all dirty buffers to mpd */ 255050ead253SVishal Moola (Oracle) lblk = ((ext4_lblk_t)folio->index) << 255109cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 255250ead253SVishal Moola (Oracle) head = folio_buffers(folio); 2553de0039f6SJan Kara err = mpage_process_page_bufs(mpd, head, head, 2554de0039f6SJan Kara lblk); 25555f1132b2SJan Kara if (err <= 0) 25564e7ea81dSJan Kara goto out; 25575f1132b2SJan Kara err = 0; 2558de0039f6SJan Kara } 25598e48dcfbSTheodore Ts'o } 256050ead253SVishal Moola (Oracle) folio_batch_release(&fbatch); 25618e48dcfbSTheodore Ts'o cond_resched(); 25628e48dcfbSTheodore Ts'o } 25636b8ed620SJan Kara mpd->scanned_until_end = 1; 25643f079114SJan Kara if (handle) 25653f079114SJan Kara ext4_journal_stop(handle); 25664f01b02cSTheodore Ts'o return 0; 25678eb9e5ceSTheodore Ts'o out: 256850ead253SVishal Moola (Oracle) folio_batch_release(&fbatch); 25693f079114SJan Kara if (handle) 25703f079114SJan Kara ext4_journal_stop(handle); 25714e7ea81dSJan Kara return err; 25728e48dcfbSTheodore Ts'o } 25738e48dcfbSTheodore Ts'o 257415648d59SJan Kara static int ext4_do_writepages(struct mpage_da_data *mpd) 257564769240SAlex Tomas { 257615648d59SJan Kara struct writeback_control *wbc = mpd->wbc; 25774e7ea81dSJan Kara pgoff_t writeback_index = 0; 25784e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 257922208dedSAneesh Kumar K.V int range_whole = 0; 25804e7ea81dSJan Kara int cycled = 1; 258161628a3fSMingming Cao handle_t *handle = NULL; 258215648d59SJan Kara struct inode *inode = mpd->inode; 258315648d59SJan Kara struct address_space *mapping = inode->i_mapping; 25846b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 25855e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 25861bce63d1SShaohua Li struct blk_plug plug; 2587cb530541STheodore Ts'o bool give_up_on_write = false; 258861628a3fSMingming Cao 258920970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2590ba80b101STheodore Ts'o 259161628a3fSMingming Cao /* 259261628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 259361628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 259461628a3fSMingming Cao * because that could violate lock ordering on umount 259561628a3fSMingming Cao */ 2596a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2597bbf023c7SMing Lei goto out_writepages; 25982a21e37eSTheodore Ts'o 25992a21e37eSTheodore Ts'o /* 26002a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26012a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26022a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26031751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26042a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 260520970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26062a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26072a21e37eSTheodore Ts'o * the stack trace. 26082a21e37eSTheodore Ts'o */ 26090db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26109b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2611bbf023c7SMing Lei ret = -EROFS; 2612bbf023c7SMing Lei goto out_writepages; 2613bbf023c7SMing Lei } 26142a21e37eSTheodore Ts'o 26154e7ea81dSJan Kara /* 26164e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26174e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26184e7ea81dSJan Kara * we'd better clear the inline data here. 26194e7ea81dSJan Kara */ 26204e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26214e7ea81dSJan Kara /* Just inode will be modified... */ 26224e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26234e7ea81dSJan Kara if (IS_ERR(handle)) { 26244e7ea81dSJan Kara ret = PTR_ERR(handle); 26254e7ea81dSJan Kara goto out_writepages; 26264e7ea81dSJan Kara } 26274e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26284e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26294e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26304e7ea81dSJan Kara ext4_journal_stop(handle); 26314e7ea81dSJan Kara } 26324e7ea81dSJan Kara 26333f079114SJan Kara /* 26343f079114SJan Kara * data=journal mode does not do delalloc so we just need to writeout / 26353f079114SJan Kara * journal already mapped buffers 26363f079114SJan Kara */ 26373f079114SJan Kara if (ext4_should_journal_data(inode)) 26383f079114SJan Kara mpd->can_map = 0; 26393f079114SJan Kara 26404e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26414e343231Syangerkun /* 26424e343231Syangerkun * We may need to convert up to one extent per block in 26434e343231Syangerkun * the page and we may dirty the inode. 26444e343231Syangerkun */ 26454e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 26464e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 26474e343231Syangerkun } 26484e343231Syangerkun 264922208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 265022208dedSAneesh Kumar K.V range_whole = 1; 265161628a3fSMingming Cao 26522acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 26534e7ea81dSJan Kara writeback_index = mapping->writeback_index; 26544e7ea81dSJan Kara if (writeback_index) 26552acf2c26SAneesh Kumar K.V cycled = 0; 265615648d59SJan Kara mpd->first_page = writeback_index; 265715648d59SJan Kara mpd->last_page = -1; 26585b41d924SEric Sandeen } else { 265915648d59SJan Kara mpd->first_page = wbc->range_start >> PAGE_SHIFT; 266015648d59SJan Kara mpd->last_page = wbc->range_end >> PAGE_SHIFT; 26615b41d924SEric Sandeen } 2662a1d6cc56SAneesh Kumar K.V 266315648d59SJan Kara ext4_io_submit_init(&mpd->io_submit, wbc); 26642acf2c26SAneesh Kumar K.V retry: 26656e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 266615648d59SJan Kara tag_pages_for_writeback(mapping, mpd->first_page, 266715648d59SJan Kara mpd->last_page); 26681bce63d1SShaohua Li blk_start_plug(&plug); 2669dddbd6acSJan Kara 2670dddbd6acSJan Kara /* 2671dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2672dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2673dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2674dddbd6acSJan Kara * started. 2675dddbd6acSJan Kara */ 267615648d59SJan Kara mpd->do_map = 0; 267715648d59SJan Kara mpd->scanned_until_end = 0; 267815648d59SJan Kara mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 267915648d59SJan Kara if (!mpd->io_submit.io_end) { 2680dddbd6acSJan Kara ret = -ENOMEM; 2681dddbd6acSJan Kara goto unplug; 2682dddbd6acSJan Kara } 268315648d59SJan Kara ret = mpage_prepare_extent_to_map(mpd); 2684a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 268515648d59SJan Kara mpage_release_unused_pages(mpd, false); 2686dddbd6acSJan Kara /* Submit prepared bio */ 268715648d59SJan Kara ext4_io_submit(&mpd->io_submit); 268815648d59SJan Kara ext4_put_io_end_defer(mpd->io_submit.io_end); 268915648d59SJan Kara mpd->io_submit.io_end = NULL; 2690dddbd6acSJan Kara if (ret < 0) 2691dddbd6acSJan Kara goto unplug; 2692dddbd6acSJan Kara 269315648d59SJan Kara while (!mpd->scanned_until_end && wbc->nr_to_write > 0) { 26944e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 269515648d59SJan Kara mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 269615648d59SJan Kara if (!mpd->io_submit.io_end) { 26974e7ea81dSJan Kara ret = -ENOMEM; 26984e7ea81dSJan Kara break; 26994e7ea81dSJan Kara } 2700a1d6cc56SAneesh Kumar K.V 2701de0039f6SJan Kara WARN_ON_ONCE(!mpd->can_map); 2702a1d6cc56SAneesh Kumar K.V /* 27034e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27044e7ea81dSJan Kara * must always write out whole page (makes a difference when 27054e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27064e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27074e7ea81dSJan Kara * not supported by delalloc. 2708a1d6cc56SAneesh Kumar K.V */ 2709a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2710525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2711a1d6cc56SAneesh Kumar K.V 271261628a3fSMingming Cao /* start a new transaction */ 27136b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27146b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 271561628a3fSMingming Cao if (IS_ERR(handle)) { 271661628a3fSMingming Cao ret = PTR_ERR(handle); 27171693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2718fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2719a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27204e7ea81dSJan Kara /* Release allocated io_end */ 272115648d59SJan Kara ext4_put_io_end(mpd->io_submit.io_end); 272215648d59SJan Kara mpd->io_submit.io_end = NULL; 27234e7ea81dSJan Kara break; 272461628a3fSMingming Cao } 272515648d59SJan Kara mpd->do_map = 1; 2726f63e6005STheodore Ts'o 272715648d59SJan Kara trace_ext4_da_write_pages(inode, mpd->first_page, wbc); 272815648d59SJan Kara ret = mpage_prepare_extent_to_map(mpd); 272915648d59SJan Kara if (!ret && mpd->map.m_len) 273015648d59SJan Kara ret = mpage_map_and_submit_extent(handle, mpd, 2731cb530541STheodore Ts'o &give_up_on_write); 2732646caa9cSJan Kara /* 2733646caa9cSJan Kara * Caution: If the handle is synchronous, 2734646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2735646caa9cSJan Kara * to finish which may depend on writeback of pages to 2736646caa9cSJan Kara * complete or on page lock to be released. In that 2737b483bb77SRandy Dunlap * case, we have to wait until after we have 2738646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2739646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2740646caa9cSJan Kara * to be able to complete) before stopping the handle. 2741646caa9cSJan Kara */ 2742646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 274361628a3fSMingming Cao ext4_journal_stop(handle); 2744646caa9cSJan Kara handle = NULL; 274515648d59SJan Kara mpd->do_map = 0; 2746646caa9cSJan Kara } 27474e7ea81dSJan Kara /* Unlock pages we didn't use */ 274815648d59SJan Kara mpage_release_unused_pages(mpd, give_up_on_write); 2749a297b2fcSXiaoguang Wang /* Submit prepared bio */ 275015648d59SJan Kara ext4_io_submit(&mpd->io_submit); 2751a297b2fcSXiaoguang Wang 2752646caa9cSJan Kara /* 2753646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2754646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2755646caa9cSJan Kara * we are still holding the transaction as we can 2756646caa9cSJan Kara * release the last reference to io_end which may end 2757646caa9cSJan Kara * up doing unwritten extent conversion. 2758646caa9cSJan Kara */ 2759646caa9cSJan Kara if (handle) { 276015648d59SJan Kara ext4_put_io_end_defer(mpd->io_submit.io_end); 2761646caa9cSJan Kara ext4_journal_stop(handle); 2762646caa9cSJan Kara } else 276315648d59SJan Kara ext4_put_io_end(mpd->io_submit.io_end); 276415648d59SJan Kara mpd->io_submit.io_end = NULL; 2765df22291fSAneesh Kumar K.V 27664e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 27674e7ea81dSJan Kara /* 27684e7ea81dSJan Kara * Commit the transaction which would 276922208dedSAneesh Kumar K.V * free blocks released in the transaction 277022208dedSAneesh Kumar K.V * and try again 277122208dedSAneesh Kumar K.V */ 2772df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 277322208dedSAneesh Kumar K.V ret = 0; 27744e7ea81dSJan Kara continue; 27754e7ea81dSJan Kara } 27764e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 27774e7ea81dSJan Kara if (ret) 277861628a3fSMingming Cao break; 277961628a3fSMingming Cao } 2780dddbd6acSJan Kara unplug: 27811bce63d1SShaohua Li blk_finish_plug(&plug); 27829c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 27832acf2c26SAneesh Kumar K.V cycled = 1; 278415648d59SJan Kara mpd->last_page = writeback_index - 1; 278515648d59SJan Kara mpd->first_page = 0; 27862acf2c26SAneesh Kumar K.V goto retry; 27872acf2c26SAneesh Kumar K.V } 278861628a3fSMingming Cao 278922208dedSAneesh Kumar K.V /* Update index */ 279022208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 279122208dedSAneesh Kumar K.V /* 27924e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 279322208dedSAneesh Kumar K.V * mode will write it back later 279422208dedSAneesh Kumar K.V */ 279515648d59SJan Kara mapping->writeback_index = mpd->first_page; 2796a1d6cc56SAneesh Kumar K.V 279761628a3fSMingming Cao out_writepages: 279820970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 27994e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 280061628a3fSMingming Cao return ret; 280164769240SAlex Tomas } 280264769240SAlex Tomas 280315648d59SJan Kara static int ext4_writepages(struct address_space *mapping, 280415648d59SJan Kara struct writeback_control *wbc) 280515648d59SJan Kara { 280629bc9ceaSJan Kara struct super_block *sb = mapping->host->i_sb; 280715648d59SJan Kara struct mpage_da_data mpd = { 280815648d59SJan Kara .inode = mapping->host, 280915648d59SJan Kara .wbc = wbc, 281015648d59SJan Kara .can_map = 1, 281115648d59SJan Kara }; 281229bc9ceaSJan Kara int ret; 281315648d59SJan Kara 281429bc9ceaSJan Kara if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 281529bc9ceaSJan Kara return -EIO; 281629bc9ceaSJan Kara 281729bc9ceaSJan Kara percpu_down_read(&EXT4_SB(sb)->s_writepages_rwsem); 281829bc9ceaSJan Kara ret = ext4_do_writepages(&mpd); 281929bc9ceaSJan Kara percpu_up_read(&EXT4_SB(sb)->s_writepages_rwsem); 282029bc9ceaSJan Kara 282129bc9ceaSJan Kara return ret; 282215648d59SJan Kara } 282315648d59SJan Kara 282459205c8dSJan Kara int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode) 282559205c8dSJan Kara { 282659205c8dSJan Kara struct writeback_control wbc = { 282759205c8dSJan Kara .sync_mode = WB_SYNC_ALL, 282859205c8dSJan Kara .nr_to_write = LONG_MAX, 282959205c8dSJan Kara .range_start = jinode->i_dirty_start, 283059205c8dSJan Kara .range_end = jinode->i_dirty_end, 283159205c8dSJan Kara }; 283259205c8dSJan Kara struct mpage_da_data mpd = { 283359205c8dSJan Kara .inode = jinode->i_vfs_inode, 283459205c8dSJan Kara .wbc = &wbc, 283559205c8dSJan Kara .can_map = 0, 283659205c8dSJan Kara }; 283759205c8dSJan Kara return ext4_do_writepages(&mpd); 283859205c8dSJan Kara } 283959205c8dSJan Kara 28405f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28415f0663bbSDan Williams struct writeback_control *wbc) 28425f0663bbSDan Williams { 28435f0663bbSDan Williams int ret; 28445f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28455f0663bbSDan Williams struct inode *inode = mapping->host; 28465f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28475f0663bbSDan Williams 28485f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28495f0663bbSDan Williams return -EIO; 28505f0663bbSDan Williams 2851bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28525f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28535f0663bbSDan Williams 28543f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28555f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28565f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2857bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28585f0663bbSDan Williams return ret; 28595f0663bbSDan Williams } 28605f0663bbSDan Williams 286179f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 286279f0be8dSAneesh Kumar K.V { 28635c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 286479f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 286579f0be8dSAneesh Kumar K.V 286679f0be8dSAneesh Kumar K.V /* 286779f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 286879f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2869179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 287079f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 287179f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 287279f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 287379f0be8dSAneesh Kumar K.V */ 28745c1ff336SEric Whitney free_clusters = 28755c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28765c1ff336SEric Whitney dirty_clusters = 28775c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 287800d4e736STheodore Ts'o /* 287900d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 288000d4e736STheodore Ts'o */ 28815c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 288210ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 288300d4e736STheodore Ts'o 28845c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 28855c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 288679f0be8dSAneesh Kumar K.V /* 2887c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2888c8afb446SEric Sandeen * or free blocks is less than watermark 288979f0be8dSAneesh Kumar K.V */ 289079f0be8dSAneesh Kumar K.V return 1; 289179f0be8dSAneesh Kumar K.V } 289279f0be8dSAneesh Kumar K.V return 0; 289379f0be8dSAneesh Kumar K.V } 289479f0be8dSAneesh Kumar K.V 289564769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 28969d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 289764769240SAlex Tomas struct page **pagep, void **fsdata) 289864769240SAlex Tomas { 289972b8ab9dSEric Sandeen int ret, retries = 0; 290064769240SAlex Tomas struct page *page; 290164769240SAlex Tomas pgoff_t index; 290264769240SAlex Tomas struct inode *inode = mapping->host; 290364769240SAlex Tomas 29040db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29050db1ff22STheodore Ts'o return -EIO; 29060db1ff22STheodore Ts'o 290709cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 290879f0be8dSAneesh Kumar K.V 29096493792dSZhang Yi if (ext4_nonda_switch(inode->i_sb) || ext4_verity_in_progress(inode)) { 291079f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 291179f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 29129d6b0cd7SMatthew Wilcox (Oracle) len, pagep, fsdata); 291379f0be8dSAneesh Kumar K.V } 291479f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29159d6b0cd7SMatthew Wilcox (Oracle) trace_ext4_da_write_begin(inode, pos, len); 29169c3569b5STao Ma 29179c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 291836d116e9SMatthew Wilcox (Oracle) ret = ext4_da_write_inline_data_begin(mapping, inode, pos, len, 29199c3569b5STao Ma pagep, fsdata); 29209c3569b5STao Ma if (ret < 0) 292147564bfbSTheodore Ts'o return ret; 292247564bfbSTheodore Ts'o if (ret == 1) 292347564bfbSTheodore Ts'o return 0; 29249c3569b5STao Ma } 29259c3569b5STao Ma 2926cc883236SZhang Yi retry: 2927b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 292847564bfbSTheodore Ts'o if (!page) 292947564bfbSTheodore Ts'o return -ENOMEM; 293047564bfbSTheodore Ts'o 293147564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29327afe5aa5SDmitry Monakhov wait_for_stable_page(page); 293364769240SAlex Tomas 2934643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29352058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 29362058f83aSMichael Halcrow ext4_da_get_block_prep); 29372058f83aSMichael Halcrow #else 29386e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 29392058f83aSMichael Halcrow #endif 294064769240SAlex Tomas if (ret < 0) { 294164769240SAlex Tomas unlock_page(page); 2942cc883236SZhang Yi put_page(page); 2943ae4d5372SAneesh Kumar K.V /* 2944ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2945ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2946cc883236SZhang Yi * i_size_read because we hold inode lock. 2947ae4d5372SAneesh Kumar K.V */ 2948ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2949b9a4207dSJan Kara ext4_truncate_failed_write(inode); 295047564bfbSTheodore Ts'o 295147564bfbSTheodore Ts'o if (ret == -ENOSPC && 295247564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 2953cc883236SZhang Yi goto retry; 295447564bfbSTheodore Ts'o return ret; 295564769240SAlex Tomas } 295664769240SAlex Tomas 295747564bfbSTheodore Ts'o *pagep = page; 295864769240SAlex Tomas return ret; 295964769240SAlex Tomas } 296064769240SAlex Tomas 2961632eaeabSMingming Cao /* 2962632eaeabSMingming Cao * Check if we should update i_disksize 2963632eaeabSMingming Cao * when write to the end of file but not require block allocation 2964632eaeabSMingming Cao */ 2965632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2966632eaeabSMingming Cao unsigned long offset) 2967632eaeabSMingming Cao { 2968632eaeabSMingming Cao struct buffer_head *bh; 2969632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2970632eaeabSMingming Cao unsigned int idx; 2971632eaeabSMingming Cao int i; 2972632eaeabSMingming Cao 2973632eaeabSMingming Cao bh = page_buffers(page); 2974632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2975632eaeabSMingming Cao 2976632eaeabSMingming Cao for (i = 0; i < idx; i++) 2977632eaeabSMingming Cao bh = bh->b_this_page; 2978632eaeabSMingming Cao 297929fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2980632eaeabSMingming Cao return 0; 2981632eaeabSMingming Cao return 1; 2982632eaeabSMingming Cao } 2983632eaeabSMingming Cao 298464769240SAlex Tomas static int ext4_da_write_end(struct file *file, 298564769240SAlex Tomas struct address_space *mapping, 298664769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 298764769240SAlex Tomas struct page *page, void *fsdata) 298864769240SAlex Tomas { 298964769240SAlex Tomas struct inode *inode = mapping->host; 299064769240SAlex Tomas loff_t new_i_size; 2991632eaeabSMingming Cao unsigned long start, end; 299279f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 299379f0be8dSAneesh Kumar K.V 299474d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 299574d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 299679f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 2997632eaeabSMingming Cao 29989bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 29999c3569b5STao Ma 30009c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30019c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30029c3569b5STao Ma ext4_has_inline_data(inode)) 30036984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 30049c3569b5STao Ma 300564769240SAlex Tomas start = pos & (PAGE_SIZE - 1); 300664769240SAlex Tomas end = start + copied - 1; 300764769240SAlex Tomas 300864769240SAlex Tomas /* 30094df031ffSZhang Yi * Since we are holding inode lock, we are sure i_disksize <= 30104df031ffSZhang Yi * i_size. We also know that if i_disksize < i_size, there are 30114df031ffSZhang Yi * delalloc writes pending in the range upto i_size. If the end of 30124df031ffSZhang Yi * the current write is <= i_size, there's no need to touch 30134df031ffSZhang Yi * i_disksize since writeback will push i_disksize upto i_size 30144df031ffSZhang Yi * eventually. If the end of the current write is > i_size and 30154df031ffSZhang Yi * inside an allocated block (ext4_da_should_update_i_disksize() 30163f079114SJan Kara * check), we need to update i_disksize here as certain 30173f079114SJan Kara * ext4_writepages() paths not allocating blocks update i_disksize. 30184df031ffSZhang Yi * 30194df031ffSZhang Yi * Note that we defer inode dirtying to generic_write_end() / 30204df031ffSZhang Yi * ext4_da_write_inline_data_end(). 3021d2a17637SMingming Cao */ 302264769240SAlex Tomas new_i_size = pos + copied; 30236984aef5SZhang Yi if (copied && new_i_size > inode->i_size && 30244df031ffSZhang Yi ext4_da_should_update_i_disksize(page, end)) 302564769240SAlex Tomas ext4_update_i_disksize(inode, new_i_size); 3026ccd2506bSTheodore Ts'o 3027cc883236SZhang Yi return generic_write_end(file, mapping, pos, len, copied, page, fsdata); 3028ac27a0ecSDave Kleikamp } 3029ac27a0ecSDave Kleikamp 3030ccd2506bSTheodore Ts'o /* 3031ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3032ccd2506bSTheodore Ts'o */ 3033ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3034ccd2506bSTheodore Ts'o { 3035ccd2506bSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3036ccd2506bSTheodore Ts'o 303771d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3038ccd2506bSTheodore Ts'o return 0; 3039ccd2506bSTheodore Ts'o 3040ccd2506bSTheodore Ts'o /* 3041ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3042ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3043ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3044ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3045ccd2506bSTheodore Ts'o * would require replicating code paths in: 3046ccd2506bSTheodore Ts'o * 304720970ba6STheodore Ts'o * ext4_writepages() -> 3048ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3049ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3050ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3051ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3052ccd2506bSTheodore Ts'o * 3053ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3054ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3055ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3056ccd2506bSTheodore Ts'o * doing I/O at all. 3057ccd2506bSTheodore Ts'o * 3058ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3059380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3060ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3061ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 306225985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3063ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3064ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3065ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3066ccd2506bSTheodore Ts'o * 3067ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3068ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3069ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3070ccd2506bSTheodore Ts'o */ 3071ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3072ccd2506bSTheodore Ts'o } 3073ac27a0ecSDave Kleikamp 3074ac27a0ecSDave Kleikamp /* 3075ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3076ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3077ac27a0ecSDave Kleikamp * 3078ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3079ac27a0ecSDave Kleikamp * journal. If somebody makes a swapfile on an ext4 data-journaling 3080ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3081ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3082ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3083ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3084ac27a0ecSDave Kleikamp * 3085ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3086ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3087ac27a0ecSDave Kleikamp */ 3088ac27a0ecSDave Kleikamp static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3089ac27a0ecSDave Kleikamp { 3090ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3091ac27a0ecSDave Kleikamp journal_t *journal; 309251ae846cSYe Bin sector_t ret = 0; 3093ac27a0ecSDave Kleikamp int err; 3094ac27a0ecSDave Kleikamp 309551ae846cSYe Bin inode_lock_shared(inode); 309646c7f254STao Ma /* 309746c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 309846c7f254STao Ma */ 309946c7f254STao Ma if (ext4_has_inline_data(inode)) 310051ae846cSYe Bin goto out; 310146c7f254STao Ma 3102ac27a0ecSDave Kleikamp if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 3103ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DELALLOC)) { 3104ac27a0ecSDave Kleikamp /* 3105ac27a0ecSDave Kleikamp * With delalloc we want to sync the file 3106ac27a0ecSDave Kleikamp * so that we can make sure we allocate 3107ac27a0ecSDave Kleikamp * blocks for file 3108ac27a0ecSDave Kleikamp */ 3109ac27a0ecSDave Kleikamp filemap_write_and_wait(mapping); 3110ac27a0ecSDave Kleikamp } 3111ac27a0ecSDave Kleikamp 311219f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 311319f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3114ac27a0ecSDave Kleikamp /* 3115ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3116ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3117ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3118ac27a0ecSDave Kleikamp * do we expect this to happen. 3119ac27a0ecSDave Kleikamp * 3120ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3121ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3122ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3123ac27a0ecSDave Kleikamp * will.) 3124ac27a0ecSDave Kleikamp * 3125ac27a0ecSDave Kleikamp * NB. EXT4_STATE_JDATA is not set on files other than 3126ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3127ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3128ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3129ac27a0ecSDave Kleikamp * everything they get. 3130ac27a0ecSDave Kleikamp */ 3131ac27a0ecSDave Kleikamp 313219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3133ac27a0ecSDave Kleikamp journal = EXT4_JOURNAL(inode); 3134ac27a0ecSDave Kleikamp jbd2_journal_lock_updates(journal); 313501d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 3136ac27a0ecSDave Kleikamp jbd2_journal_unlock_updates(journal); 3137ac27a0ecSDave Kleikamp 3138ac27a0ecSDave Kleikamp if (err) 313951ae846cSYe Bin goto out; 3140ac27a0ecSDave Kleikamp } 3141ac27a0ecSDave Kleikamp 314251ae846cSYe Bin ret = iomap_bmap(mapping, block, &ext4_iomap_ops); 314351ae846cSYe Bin 314451ae846cSYe Bin out: 314551ae846cSYe Bin inode_unlock_shared(inode); 314651ae846cSYe Bin return ret; 3147ac27a0ecSDave Kleikamp } 3148ac27a0ecSDave Kleikamp 3149fe5ddf6bSMatthew Wilcox (Oracle) static int ext4_read_folio(struct file *file, struct folio *folio) 3150ac27a0ecSDave Kleikamp { 3151fe5ddf6bSMatthew Wilcox (Oracle) struct page *page = &folio->page; 315246c7f254STao Ma int ret = -EAGAIN; 315346c7f254STao Ma struct inode *inode = page->mapping->host; 315446c7f254STao Ma 31550562e0baSJiaying Zhang trace_ext4_readpage(page); 315646c7f254STao Ma 315746c7f254STao Ma if (ext4_has_inline_data(inode)) 315846c7f254STao Ma ret = ext4_readpage_inline(inode, page); 315946c7f254STao Ma 316046c7f254STao Ma if (ret == -EAGAIN) 3161a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 316246c7f254STao Ma 316346c7f254STao Ma return ret; 3164ac27a0ecSDave Kleikamp } 3165ac27a0ecSDave Kleikamp 31666311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3167ac27a0ecSDave Kleikamp { 31686311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 316946c7f254STao Ma 31706311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 317146c7f254STao Ma if (ext4_has_inline_data(inode)) 31726311f91fSMatthew Wilcox (Oracle) return; 317346c7f254STao Ma 3174a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3175ac27a0ecSDave Kleikamp } 3176ac27a0ecSDave Kleikamp 31777ba13abbSMatthew Wilcox (Oracle) static void ext4_invalidate_folio(struct folio *folio, size_t offset, 31787ba13abbSMatthew Wilcox (Oracle) size_t length) 3179ac27a0ecSDave Kleikamp { 3180ccd16945SMatthew Wilcox (Oracle) trace_ext4_invalidate_folio(folio, offset, length); 31810562e0baSJiaying Zhang 31824520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 31837ba13abbSMatthew Wilcox (Oracle) WARN_ON(folio_buffers(folio) && buffer_jbd(folio_buffers(folio))); 31844520fb3cSJan Kara 31857ba13abbSMatthew Wilcox (Oracle) block_invalidate_folio(folio, offset, length); 31864520fb3cSJan Kara } 31874520fb3cSJan Kara 3188ccd16945SMatthew Wilcox (Oracle) static int __ext4_journalled_invalidate_folio(struct folio *folio, 3189ccd16945SMatthew Wilcox (Oracle) size_t offset, size_t length) 31904520fb3cSJan Kara { 3191ccd16945SMatthew Wilcox (Oracle) journal_t *journal = EXT4_JOURNAL(folio->mapping->host); 31924520fb3cSJan Kara 3193ccd16945SMatthew Wilcox (Oracle) trace_ext4_journalled_invalidate_folio(folio, offset, length); 31944520fb3cSJan Kara 3195744692dcSJiaying Zhang /* 3196ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3197ac27a0ecSDave Kleikamp */ 3198ccd16945SMatthew Wilcox (Oracle) if (offset == 0 && length == folio_size(folio)) 3199ccd16945SMatthew Wilcox (Oracle) folio_clear_checked(folio); 3200ac27a0ecSDave Kleikamp 3201ccd16945SMatthew Wilcox (Oracle) return jbd2_journal_invalidate_folio(journal, folio, offset, length); 320253e87268SJan Kara } 320353e87268SJan Kara 320453e87268SJan Kara /* Wrapper for aops... */ 3205ccd16945SMatthew Wilcox (Oracle) static void ext4_journalled_invalidate_folio(struct folio *folio, 3206ccd16945SMatthew Wilcox (Oracle) size_t offset, 3207ccd16945SMatthew Wilcox (Oracle) size_t length) 320853e87268SJan Kara { 3209ccd16945SMatthew Wilcox (Oracle) WARN_ON(__ext4_journalled_invalidate_folio(folio, offset, length) < 0); 3210ac27a0ecSDave Kleikamp } 3211ac27a0ecSDave Kleikamp 32123c402f15SMatthew Wilcox (Oracle) static bool ext4_release_folio(struct folio *folio, gfp_t wait) 3213ac27a0ecSDave Kleikamp { 32143c402f15SMatthew Wilcox (Oracle) journal_t *journal = EXT4_JOURNAL(folio->mapping->host); 3215ac27a0ecSDave Kleikamp 32163c402f15SMatthew Wilcox (Oracle) trace_ext4_releasepage(&folio->page); 32170562e0baSJiaying Zhang 3218e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 32193c402f15SMatthew Wilcox (Oracle) if (folio_test_checked(folio)) 32203c402f15SMatthew Wilcox (Oracle) return false; 32210390131bSFrank Mayhar if (journal) 3222c56a6eb0SMatthew Wilcox (Oracle) return jbd2_journal_try_to_free_buffers(journal, folio); 32230390131bSFrank Mayhar else 322468189fefSMatthew Wilcox (Oracle) return try_to_free_buffers(folio); 3225ac27a0ecSDave Kleikamp } 3226ac27a0ecSDave Kleikamp 3227b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3228b8a6176cSJan Kara { 3229b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3230b8a6176cSJan Kara 3231aa75f4d3SHarshad Shirwadkar if (journal) { 3232aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3233aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3234d0520df7SAndrea Righi return false; 3235d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 32361ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3237d0520df7SAndrea Righi return true; 3238aa75f4d3SHarshad Shirwadkar } 3239aa75f4d3SHarshad Shirwadkar 3240b8a6176cSJan Kara /* Any metadata buffers to write? */ 3241b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3242b8a6176cSJan Kara return true; 3243b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3244b8a6176cSJan Kara } 3245b8a6176cSJan Kara 3246c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3247c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3248de205114SChristoph Hellwig loff_t length, unsigned int flags) 3249364443cbSJan Kara { 3250c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3251c8fdfe29SMatthew Bobrowski 3252c8fdfe29SMatthew Bobrowski /* 3253c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3254c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3255c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3256c8fdfe29SMatthew Bobrowski */ 3257c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3258c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3259c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3260c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3261c8fdfe29SMatthew Bobrowski 3262c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3263c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3264c8fdfe29SMatthew Bobrowski 3265de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3266c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3267de205114SChristoph Hellwig else 3268de205114SChristoph Hellwig iomap->bdev = inode->i_sb->s_bdev; 3269c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3270c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3271c8fdfe29SMatthew Bobrowski 32726386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 32736386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32746386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 32756386722aSRitesh Harjani 3276c8fdfe29SMatthew Bobrowski /* 3277c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3278c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3279c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3280c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3281c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3282c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3283c8fdfe29SMatthew Bobrowski * been set first. 3284c8fdfe29SMatthew Bobrowski */ 3285c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3286c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3287c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3288de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3289de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3290c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3291c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3292c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3293de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3294de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3295c8fdfe29SMatthew Bobrowski } else { 3296c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3297c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3298c8fdfe29SMatthew Bobrowski } 3299c8fdfe29SMatthew Bobrowski } 3300c8fdfe29SMatthew Bobrowski 3301f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3302f063db5eSMatthew Bobrowski unsigned int flags) 3303f063db5eSMatthew Bobrowski { 3304f063db5eSMatthew Bobrowski handle_t *handle; 3305378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3306378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3307f063db5eSMatthew Bobrowski 3308f063db5eSMatthew Bobrowski /* 3309f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3310f063db5eSMatthew Bobrowski * once for direct I/O. 3311f063db5eSMatthew Bobrowski */ 3312f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3313f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3314f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3315f063db5eSMatthew Bobrowski 3316f063db5eSMatthew Bobrowski retry: 3317f063db5eSMatthew Bobrowski /* 3318f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3319f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3320f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3321f063db5eSMatthew Bobrowski * fits into the credits as well. 3322f063db5eSMatthew Bobrowski */ 3323f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3324f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3325f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3326f063db5eSMatthew Bobrowski 3327378f32baSMatthew Bobrowski /* 3328378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3329378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3330378f32baSMatthew Bobrowski */ 3331952da063SChristoph Hellwig WARN_ON(!(flags & (IOMAP_DAX | IOMAP_DIRECT))); 3332952da063SChristoph Hellwig if (flags & IOMAP_DAX) 3333378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3334378f32baSMatthew Bobrowski /* 3335378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3336378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3337378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3338378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3339378f32baSMatthew Bobrowski */ 3340d0b040f5SJan Kara else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3341378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3342378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3343378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3344378f32baSMatthew Bobrowski 3345378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3346378f32baSMatthew Bobrowski 3347378f32baSMatthew Bobrowski /* 3348378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3349378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3350378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3351378f32baSMatthew Bobrowski */ 3352378f32baSMatthew Bobrowski if (!m_flags && !ret) 3353378f32baSMatthew Bobrowski ret = -ENOTBLK; 3354f063db5eSMatthew Bobrowski 3355f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3356f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3357f063db5eSMatthew Bobrowski goto retry; 3358f063db5eSMatthew Bobrowski 3359f063db5eSMatthew Bobrowski return ret; 3360f063db5eSMatthew Bobrowski } 3361f063db5eSMatthew Bobrowski 3362f063db5eSMatthew Bobrowski 3363364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3364c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3365364443cbSJan Kara { 3366364443cbSJan Kara int ret; 336709edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 336809edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3369364443cbSJan Kara 3370bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3371bcd8e91fSTheodore Ts'o return -EINVAL; 33727046ae35SAndreas Gruenbacher 3373364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3374364443cbSJan Kara return -ERANGE; 3375364443cbSJan Kara 337609edf4d3SMatthew Bobrowski /* 337709edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 337809edf4d3SMatthew Bobrowski */ 337909edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 338009edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 338109edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3382364443cbSJan Kara 33839faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 33849faac62dSRitesh Harjani /* 33859faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 33869faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 33879faac62dSRitesh Harjani * the mapping information. This could boost performance 33889faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 33899faac62dSRitesh Harjani */ 33909faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3391545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 33929faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 33939faac62dSRitesh Harjani goto out; 33949faac62dSRitesh Harjani } 33959faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 33969faac62dSRitesh Harjani } else { 33979faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 33989faac62dSRitesh Harjani } 3399f063db5eSMatthew Bobrowski 3400545052e9SChristoph Hellwig if (ret < 0) 3401545052e9SChristoph Hellwig return ret; 34029faac62dSRitesh Harjani out: 340338ea50daSEric Biggers /* 340438ea50daSEric Biggers * When inline encryption is enabled, sometimes I/O to an encrypted file 340538ea50daSEric Biggers * has to be broken up to guarantee DUN contiguity. Handle this by 340638ea50daSEric Biggers * limiting the length of the mapping returned. 340738ea50daSEric Biggers */ 340838ea50daSEric Biggers map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); 340938ea50daSEric Biggers 3410de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 3411545052e9SChristoph Hellwig 3412364443cbSJan Kara return 0; 3413364443cbSJan Kara } 3414364443cbSJan Kara 34158cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34168cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34178cd115bdSJan Kara struct iomap *srcmap) 34188cd115bdSJan Kara { 34198cd115bdSJan Kara int ret; 34208cd115bdSJan Kara 34218cd115bdSJan Kara /* 34228cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34238cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34248cd115bdSJan Kara */ 34258cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34268cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34278cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34288cd115bdSJan Kara return ret; 34298cd115bdSJan Kara } 34308cd115bdSJan Kara 3431776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3432776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3433776722e8SJan Kara { 3434378f32baSMatthew Bobrowski /* 3435378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3436378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3437378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3438378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3439378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3440378f32baSMatthew Bobrowski */ 3441378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3442378f32baSMatthew Bobrowski return -ENOTBLK; 3443378f32baSMatthew Bobrowski 3444776722e8SJan Kara return 0; 3445776722e8SJan Kara } 3446776722e8SJan Kara 34478ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3448364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3449776722e8SJan Kara .iomap_end = ext4_iomap_end, 3450364443cbSJan Kara }; 3451364443cbSJan Kara 34528cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 34538cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 34548cd115bdSJan Kara .iomap_end = ext4_iomap_end, 34558cd115bdSJan Kara }; 34568cd115bdSJan Kara 345709edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 345809edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 345909edf4d3SMatthew Bobrowski { 346009edf4d3SMatthew Bobrowski struct extent_status es; 346109edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 346209edf4d3SMatthew Bobrowski 346309edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 346409edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 346509edf4d3SMatthew Bobrowski 346609edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 346709edf4d3SMatthew Bobrowski return false; 346809edf4d3SMatthew Bobrowski 346909edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 347009edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 347109edf4d3SMatthew Bobrowski return false; 347209edf4d3SMatthew Bobrowski } 347309edf4d3SMatthew Bobrowski 347409edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 347509edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 347609edf4d3SMatthew Bobrowski 347709edf4d3SMatthew Bobrowski return true; 347809edf4d3SMatthew Bobrowski } 347909edf4d3SMatthew Bobrowski 348009edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 348109edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 348209edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 348309edf4d3SMatthew Bobrowski { 348409edf4d3SMatthew Bobrowski int ret; 348509edf4d3SMatthew Bobrowski bool delalloc = false; 348609edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 348709edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 348809edf4d3SMatthew Bobrowski 348909edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 349009edf4d3SMatthew Bobrowski return -EINVAL; 349109edf4d3SMatthew Bobrowski 34927cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 34937cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3494ba5843f5SJan Kara if (ret != -EAGAIN) { 3495ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3496ba5843f5SJan Kara ret = -ENOENT; 3497ba5843f5SJan Kara return ret; 3498ba5843f5SJan Kara } 3499ba5843f5SJan Kara } 350012735f88SJan Kara 350109edf4d3SMatthew Bobrowski /* 350209edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 350309edf4d3SMatthew Bobrowski */ 350409edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 350509edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 350609edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 350712735f88SJan Kara 3508b2c57642SRitesh Harjani /* 3509b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3510b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3511b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3512b2c57642SRitesh Harjani * -EIO error. 3513b2c57642SRitesh Harjani */ 3514b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3515b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3516b2c57642SRitesh Harjani 3517b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3518b2c57642SRitesh Harjani map.m_flags = 0; 3519b2c57642SRitesh Harjani goto set_iomap; 3520b2c57642SRitesh Harjani } 3521b2c57642SRitesh Harjani } 3522b2c57642SRitesh Harjani 352312735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3524ba5843f5SJan Kara if (ret < 0) 3525ba5843f5SJan Kara return ret; 3526914f82a3SJan Kara if (ret == 0) 352709edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 352809edf4d3SMatthew Bobrowski 3529b2c57642SRitesh Harjani set_iomap: 3530de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 353109edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 353209edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 353309edf4d3SMatthew Bobrowski 353409edf4d3SMatthew Bobrowski return 0; 3535914f82a3SJan Kara } 3536914f82a3SJan Kara 353709edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 353809edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 353909edf4d3SMatthew Bobrowski }; 35404c0425ffSMingming Cao 3541ac27a0ecSDave Kleikamp /* 35423f5d3063SJan Kara * For data=journal mode, folio should be marked dirty only when it was 35433f5d3063SJan Kara * writeably mapped. When that happens, it was already attached to the 35443f5d3063SJan Kara * transaction and marked as jbddirty (we take care of this in 35453f5d3063SJan Kara * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings 35463f5d3063SJan Kara * so we should have nothing to do here, except for the case when someone 35473f5d3063SJan Kara * had the page pinned and dirtied the page through this pin (e.g. by doing 35483f5d3063SJan Kara * direct IO to it). In that case we'd need to attach buffers here to the 35493f5d3063SJan Kara * transaction but we cannot due to lock ordering. We cannot just dirty the 35503f5d3063SJan Kara * folio and leave attached buffers clean, because the buffers' dirty state is 35513f5d3063SJan Kara * "definitive". We cannot just set the buffers dirty or jbddirty because all 35523f5d3063SJan Kara * the journalling code will explode. So what we do is to mark the folio 35533f5d3063SJan Kara * "pending dirty" and next time ext4_writepages() is called, attach buffers 35543f5d3063SJan Kara * to the transaction appropriately. 3555ac27a0ecSDave Kleikamp */ 3556187c82cbSMatthew Wilcox (Oracle) static bool ext4_journalled_dirty_folio(struct address_space *mapping, 3557187c82cbSMatthew Wilcox (Oracle) struct folio *folio) 3558ac27a0ecSDave Kleikamp { 35590f252336SMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_buffers(folio)); 35603f5d3063SJan Kara if (folio_maybe_dma_pinned(folio)) 3561187c82cbSMatthew Wilcox (Oracle) folio_set_checked(folio); 3562187c82cbSMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 3563ac27a0ecSDave Kleikamp } 3564ac27a0ecSDave Kleikamp 3565e621900aSMatthew Wilcox (Oracle) static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio) 35666dcc693bSJan Kara { 3567e621900aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio)); 3568e621900aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_buffers(folio)); 3569e621900aSMatthew Wilcox (Oracle) return block_dirty_folio(mapping, folio); 35706dcc693bSJan Kara } 35716dcc693bSJan Kara 35720e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 35730e6895baSRitesh Harjani struct file *file, sector_t *span) 35740e6895baSRitesh Harjani { 35750e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 35760e6895baSRitesh Harjani &ext4_iomap_report_ops); 35770e6895baSRitesh Harjani } 35780e6895baSRitesh Harjani 357974d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3580fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 35816311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 358220970ba6STheodore Ts'o .writepages = ext4_writepages, 3583bfc1af65SNick Piggin .write_begin = ext4_write_begin, 358474d553aaSTheodore Ts'o .write_end = ext4_write_end, 3585e621900aSMatthew Wilcox (Oracle) .dirty_folio = ext4_dirty_folio, 3586617ba13bSMingming Cao .bmap = ext4_bmap, 35877ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = ext4_invalidate_folio, 35883c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3589378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 359067235182SMatthew Wilcox (Oracle) .migrate_folio = buffer_migrate_folio, 35918ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3592aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 35930e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3594ac27a0ecSDave Kleikamp }; 3595ac27a0ecSDave Kleikamp 3596617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3597fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 35986311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 359920970ba6STheodore Ts'o .writepages = ext4_writepages, 3600bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3601bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3602187c82cbSMatthew Wilcox (Oracle) .dirty_folio = ext4_journalled_dirty_folio, 3603617ba13bSMingming Cao .bmap = ext4_bmap, 3604ccd16945SMatthew Wilcox (Oracle) .invalidate_folio = ext4_journalled_invalidate_folio, 36053c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3606378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3607dae99960SJan Kara .migrate_folio = buffer_migrate_folio_norefs, 36088ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3609aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36100e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3611ac27a0ecSDave Kleikamp }; 3612ac27a0ecSDave Kleikamp 361364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 3614fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 36156311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 361620970ba6STheodore Ts'o .writepages = ext4_writepages, 361764769240SAlex Tomas .write_begin = ext4_da_write_begin, 361864769240SAlex Tomas .write_end = ext4_da_write_end, 3619e621900aSMatthew Wilcox (Oracle) .dirty_folio = ext4_dirty_folio, 362064769240SAlex Tomas .bmap = ext4_bmap, 36217ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = ext4_invalidate_folio, 36223c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3623378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 362467235182SMatthew Wilcox (Oracle) .migrate_folio = buffer_migrate_folio, 36258ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3626aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36270e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 362864769240SAlex Tomas }; 362964769240SAlex Tomas 36305f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36315f0663bbSDan Williams .writepages = ext4_dax_writepages, 36325f0663bbSDan Williams .direct_IO = noop_direct_IO, 363346de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 363494dbb631SToshi Kani .bmap = ext4_bmap, 36350e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 36365f0663bbSDan Williams }; 36375f0663bbSDan Williams 3638617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3639ac27a0ecSDave Kleikamp { 36403d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36413d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36423d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36433d2b1582SLukas Czerner break; 36443d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3645617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 364674d553aaSTheodore Ts'o return; 36473d2b1582SLukas Czerner default: 36483d2b1582SLukas Czerner BUG(); 36493d2b1582SLukas Czerner } 36505f0663bbSDan Williams if (IS_DAX(inode)) 36515f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 36525f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 365374d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 365474d553aaSTheodore Ts'o else 365574d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3656ac27a0ecSDave Kleikamp } 3657ac27a0ecSDave Kleikamp 3658923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3659d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3660d863dc36SLukas Czerner { 366109cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 366209cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3663923ae0ffSRoss Zwisler unsigned blocksize, pos; 3664d863dc36SLukas Czerner ext4_lblk_t iblock; 3665d863dc36SLukas Czerner struct inode *inode = mapping->host; 3666d863dc36SLukas Czerner struct buffer_head *bh; 3667d863dc36SLukas Czerner struct page *page; 3668d863dc36SLukas Czerner int err = 0; 3669d863dc36SLukas Czerner 367009cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3671c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3672d863dc36SLukas Czerner if (!page) 3673d863dc36SLukas Czerner return -ENOMEM; 3674d863dc36SLukas Czerner 3675d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3676d863dc36SLukas Czerner 367709cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3678d863dc36SLukas Czerner 3679d863dc36SLukas Czerner if (!page_has_buffers(page)) 3680d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3681d863dc36SLukas Czerner 3682d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3683d863dc36SLukas Czerner bh = page_buffers(page); 3684d863dc36SLukas Czerner pos = blocksize; 3685d863dc36SLukas Czerner while (offset >= pos) { 3686d863dc36SLukas Czerner bh = bh->b_this_page; 3687d863dc36SLukas Czerner iblock++; 3688d863dc36SLukas Czerner pos += blocksize; 3689d863dc36SLukas Czerner } 3690d863dc36SLukas Czerner if (buffer_freed(bh)) { 3691d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3692d863dc36SLukas Czerner goto unlock; 3693d863dc36SLukas Czerner } 3694d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3695d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3696d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3697d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3698d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3699d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3700d863dc36SLukas Czerner goto unlock; 3701d863dc36SLukas Czerner } 3702d863dc36SLukas Czerner } 3703d863dc36SLukas Czerner 3704d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3705d863dc36SLukas Czerner if (PageUptodate(page)) 3706d863dc36SLukas Czerner set_buffer_uptodate(bh); 3707d863dc36SLukas Czerner 3708d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 37092d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 37102d069c08Szhangyi (F) if (err) 3711d863dc36SLukas Czerner goto unlock; 37124f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3713c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3714a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 371551e4e315SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page_folio(page), 371651e4e315SEric Biggers blocksize, 3717834f1565SEric Biggers bh_offset(bh)); 3718834f1565SEric Biggers if (err) { 3719834f1565SEric Biggers clear_buffer_uptodate(bh); 3720834f1565SEric Biggers goto unlock; 3721834f1565SEric Biggers } 3722c9c7429cSMichael Halcrow } 3723d863dc36SLukas Czerner } 3724d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3725d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3726188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 3727188c299eSJan Kara EXT4_JTR_NONE); 3728d863dc36SLukas Czerner if (err) 3729d863dc36SLukas Czerner goto unlock; 3730d863dc36SLukas Czerner } 3731d863dc36SLukas Czerner zero_user(page, offset, length); 3732d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3733d863dc36SLukas Czerner 3734d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3735d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37360713ed0cSLukas Czerner } else { 3737353eefd3Sjon ernst err = 0; 3738d863dc36SLukas Czerner mark_buffer_dirty(bh); 37393957ef53SJan Kara if (ext4_should_order_data(inode)) 374073131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 374173131fbbSRoss Zwisler length); 37420713ed0cSLukas Czerner } 3743d863dc36SLukas Czerner 3744d863dc36SLukas Czerner unlock: 3745d863dc36SLukas Czerner unlock_page(page); 374609cbfeafSKirill A. Shutemov put_page(page); 3747d863dc36SLukas Czerner return err; 3748d863dc36SLukas Czerner } 3749d863dc36SLukas Czerner 375094350ab5SMatthew Wilcox /* 3751923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3752923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3753923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3754923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 37553088e5a5SBhaskar Chowdhury * that corresponds to 'from' 3756923ae0ffSRoss Zwisler */ 3757923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3758923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3759923ae0ffSRoss Zwisler { 3760923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 376109cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3762923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3763923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3764923ae0ffSRoss Zwisler 3765923ae0ffSRoss Zwisler /* 3766923ae0ffSRoss Zwisler * correct length if it does not fall between 3767923ae0ffSRoss Zwisler * 'from' and the end of the block 3768923ae0ffSRoss Zwisler */ 3769923ae0ffSRoss Zwisler if (length > max || length < 0) 3770923ae0ffSRoss Zwisler length = max; 3771923ae0ffSRoss Zwisler 377247e69351SJan Kara if (IS_DAX(inode)) { 3773c6f40468SChristoph Hellwig return dax_zero_range(inode, from, length, NULL, 377447e69351SJan Kara &ext4_iomap_ops); 377547e69351SJan Kara } 3776923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3777923ae0ffSRoss Zwisler } 3778923ae0ffSRoss Zwisler 3779923ae0ffSRoss Zwisler /* 378094350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 378194350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 378294350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 378394350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 378494350ab5SMatthew Wilcox */ 3785c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 378694350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 378794350ab5SMatthew Wilcox { 378809cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 378994350ab5SMatthew Wilcox unsigned length; 379094350ab5SMatthew Wilcox unsigned blocksize; 379194350ab5SMatthew Wilcox struct inode *inode = mapping->host; 379294350ab5SMatthew Wilcox 37930d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3794592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 37950d06863fSTheodore Ts'o return 0; 37960d06863fSTheodore Ts'o 379794350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 379894350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 379994350ab5SMatthew Wilcox 380094350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 380194350ab5SMatthew Wilcox } 380294350ab5SMatthew Wilcox 3803a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3804a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3805a87dd18cSLukas Czerner { 3806a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3807a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3808e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3809a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3810a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3811a87dd18cSLukas Czerner int err = 0; 3812a87dd18cSLukas Czerner 3813e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3814e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3815e1be3a92SLukas Czerner 3816a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3817a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3818a87dd18cSLukas Czerner 3819a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3820e1be3a92SLukas Czerner if (start == end && 3821e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3822a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3823a87dd18cSLukas Czerner lstart, length); 3824a87dd18cSLukas Czerner return err; 3825a87dd18cSLukas Czerner } 3826a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3827e1be3a92SLukas Czerner if (partial_start) { 3828a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3829a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3830a87dd18cSLukas Czerner if (err) 3831a87dd18cSLukas Czerner return err; 3832a87dd18cSLukas Czerner } 3833a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3834e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3835a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3836e1be3a92SLukas Czerner byte_end - partial_end, 3837e1be3a92SLukas Czerner partial_end + 1); 3838a87dd18cSLukas Czerner return err; 3839a87dd18cSLukas Czerner } 3840a87dd18cSLukas Czerner 384191ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 384291ef4cafSDuane Griffin { 384391ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 384491ef4cafSDuane Griffin return 1; 384591ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 384691ef4cafSDuane Griffin return 1; 384791ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 384891ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 384991ef4cafSDuane Griffin return 0; 385091ef4cafSDuane Griffin } 385191ef4cafSDuane Griffin 3852ac27a0ecSDave Kleikamp /* 385301127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 385401127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 385501127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 385601127848SJan Kara * that will never happen after we truncate page cache. 385701127848SJan Kara */ 385801127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 385901127848SJan Kara loff_t len) 386001127848SJan Kara { 386101127848SJan Kara handle_t *handle; 38624209ae12SHarshad Shirwadkar int ret; 38634209ae12SHarshad Shirwadkar 386401127848SJan Kara loff_t size = i_size_read(inode); 386501127848SJan Kara 38665955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 386701127848SJan Kara if (offset > size || offset + len < size) 386801127848SJan Kara return 0; 386901127848SJan Kara 387001127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 387101127848SJan Kara return 0; 387201127848SJan Kara 387301127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 387401127848SJan Kara if (IS_ERR(handle)) 387501127848SJan Kara return PTR_ERR(handle); 387601127848SJan Kara ext4_update_i_disksize(inode, size); 38774209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 387801127848SJan Kara ext4_journal_stop(handle); 387901127848SJan Kara 38804209ae12SHarshad Shirwadkar return ret; 388101127848SJan Kara } 388201127848SJan Kara 3883d4f5258eSJan Kara static void ext4_wait_dax_page(struct inode *inode) 3884430657b6SRoss Zwisler { 3885d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 3886430657b6SRoss Zwisler schedule(); 3887d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 3888430657b6SRoss Zwisler } 3889430657b6SRoss Zwisler 3890430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3891430657b6SRoss Zwisler { 3892430657b6SRoss Zwisler struct page *page; 3893430657b6SRoss Zwisler int error; 3894430657b6SRoss Zwisler 3895d4f5258eSJan Kara if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock))) 3896430657b6SRoss Zwisler return -EINVAL; 3897430657b6SRoss Zwisler 3898430657b6SRoss Zwisler do { 3899430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3900430657b6SRoss Zwisler if (!page) 3901430657b6SRoss Zwisler return 0; 3902430657b6SRoss Zwisler 3903430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3904430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3905430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3906d4f5258eSJan Kara ext4_wait_dax_page(inode)); 3907b1f38217SRoss Zwisler } while (error == 0); 3908430657b6SRoss Zwisler 3909430657b6SRoss Zwisler return error; 3910430657b6SRoss Zwisler } 3911430657b6SRoss Zwisler 391201127848SJan Kara /* 3913cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3914a4bb6b64SAllison Henderson * associated with the given offset and length 3915a4bb6b64SAllison Henderson * 3916a4bb6b64SAllison Henderson * @inode: File inode 3917a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3918a4bb6b64SAllison Henderson * @len: The length of the hole 3919a4bb6b64SAllison Henderson * 39204907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3921a4bb6b64SAllison Henderson */ 3922a4bb6b64SAllison Henderson 3923ad5cd4f4SDarrick J. Wong int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3924a4bb6b64SAllison Henderson { 3925ad5cd4f4SDarrick J. Wong struct inode *inode = file_inode(file); 392626a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 392726a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 392826a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 39292da37622STadeusz Struk loff_t first_block_offset, last_block_offset, max_length; 39302da37622STadeusz Struk struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 393126a4c0c6STheodore Ts'o handle_t *handle; 393226a4c0c6STheodore Ts'o unsigned int credits; 39334209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 393426a4c0c6STheodore Ts'o 3935b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3936aaddea81SZheng Liu 393726a4c0c6STheodore Ts'o /* 393826a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 393926a4c0c6STheodore Ts'o * Then release them. 394026a4c0c6STheodore Ts'o */ 3941cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 394226a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 394326a4c0c6STheodore Ts'o offset + length - 1); 394426a4c0c6STheodore Ts'o if (ret) 394526a4c0c6STheodore Ts'o return ret; 394626a4c0c6STheodore Ts'o } 394726a4c0c6STheodore Ts'o 39485955102cSAl Viro inode_lock(inode); 39499ef06cecSLukas Czerner 395026a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 395126a4c0c6STheodore Ts'o if (offset >= inode->i_size) 395226a4c0c6STheodore Ts'o goto out_mutex; 395326a4c0c6STheodore Ts'o 395426a4c0c6STheodore Ts'o /* 395526a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 395626a4c0c6STheodore Ts'o * to end after the page that contains i_size 395726a4c0c6STheodore Ts'o */ 395826a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 395926a4c0c6STheodore Ts'o length = inode->i_size + 396009cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 396126a4c0c6STheodore Ts'o offset; 396226a4c0c6STheodore Ts'o } 396326a4c0c6STheodore Ts'o 39642da37622STadeusz Struk /* 39652da37622STadeusz Struk * For punch hole the length + offset needs to be within one block 39662da37622STadeusz Struk * before last range. Adjust the length if it goes beyond that limit. 39672da37622STadeusz Struk */ 39682da37622STadeusz Struk max_length = sbi->s_bitmap_maxbytes - inode->i_sb->s_blocksize; 39692da37622STadeusz Struk if (offset + length > max_length) 39702da37622STadeusz Struk length = max_length - offset; 39712da37622STadeusz Struk 3972a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 3973a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 3974a361293fSJan Kara /* 3975a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 3976a361293fSJan Kara * partial block 3977a361293fSJan Kara */ 3978a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 3979a361293fSJan Kara if (ret < 0) 3980a361293fSJan Kara goto out_mutex; 3981a361293fSJan Kara 3982a361293fSJan Kara } 3983a361293fSJan Kara 3984f340b3d9Shongnanli /* Wait all existing dio workers, newcomers will block on i_rwsem */ 3985ea3d7209SJan Kara inode_dio_wait(inode); 3986ea3d7209SJan Kara 3987ad5cd4f4SDarrick J. Wong ret = file_modified(file); 3988ad5cd4f4SDarrick J. Wong if (ret) 3989ad5cd4f4SDarrick J. Wong goto out_mutex; 3990ad5cd4f4SDarrick J. Wong 3991ea3d7209SJan Kara /* 3992ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 3993ea3d7209SJan Kara * page cache. 3994ea3d7209SJan Kara */ 3995d4f5258eSJan Kara filemap_invalidate_lock(mapping); 3996430657b6SRoss Zwisler 3997430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 3998430657b6SRoss Zwisler if (ret) 3999430657b6SRoss Zwisler goto out_dio; 4000430657b6SRoss Zwisler 4001a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4002a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 400326a4c0c6STheodore Ts'o 4004a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 400501127848SJan Kara if (last_block_offset > first_block_offset) { 400601127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 400701127848SJan Kara if (ret) 400801127848SJan Kara goto out_dio; 4009a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4010a87dd18cSLukas Czerner last_block_offset); 401101127848SJan Kara } 401226a4c0c6STheodore Ts'o 401326a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 401426a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 401526a4c0c6STheodore Ts'o else 401626a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 401726a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 401826a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 401926a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 402026a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 402126a4c0c6STheodore Ts'o goto out_dio; 402226a4c0c6STheodore Ts'o } 402326a4c0c6STheodore Ts'o 4024a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4025a87dd18cSLukas Czerner length); 402626a4c0c6STheodore Ts'o if (ret) 402726a4c0c6STheodore Ts'o goto out_stop; 402826a4c0c6STheodore Ts'o 402926a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 403026a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 403126a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 403226a4c0c6STheodore Ts'o 4033eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4034eee597acSLukas Czerner if (stop_block > first_block) { 403526a4c0c6STheodore Ts'o 403626a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 403727bc446eSbrookxu ext4_discard_preallocations(inode, 0); 403826a4c0c6STheodore Ts'o 403926a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 404026a4c0c6STheodore Ts'o stop_block - first_block); 404126a4c0c6STheodore Ts'o if (ret) { 404226a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 404326a4c0c6STheodore Ts'o goto out_stop; 404426a4c0c6STheodore Ts'o } 404526a4c0c6STheodore Ts'o 404626a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 404726a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 404826a4c0c6STheodore Ts'o stop_block - 1); 404926a4c0c6STheodore Ts'o else 40504f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 405126a4c0c6STheodore Ts'o stop_block); 405226a4c0c6STheodore Ts'o 4053819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4054eee597acSLukas Czerner } 4055a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 405626a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 405726a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4058e251f9bcSMaxim Patlasov 4059eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 40604209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 40614209ae12SHarshad Shirwadkar if (unlikely(ret2)) 40624209ae12SHarshad Shirwadkar ret = ret2; 406367a7d5f5SJan Kara if (ret >= 0) 406467a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 406526a4c0c6STheodore Ts'o out_stop: 406626a4c0c6STheodore Ts'o ext4_journal_stop(handle); 406726a4c0c6STheodore Ts'o out_dio: 4068d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 406926a4c0c6STheodore Ts'o out_mutex: 40705955102cSAl Viro inode_unlock(inode); 407126a4c0c6STheodore Ts'o return ret; 4072a4bb6b64SAllison Henderson } 4073a4bb6b64SAllison Henderson 4074a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4075a361293fSJan Kara { 4076a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4077a361293fSJan Kara struct jbd2_inode *jinode; 4078a361293fSJan Kara 4079a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4080a361293fSJan Kara return 0; 4081a361293fSJan Kara 4082a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4083a361293fSJan Kara spin_lock(&inode->i_lock); 4084a361293fSJan Kara if (!ei->jinode) { 4085a361293fSJan Kara if (!jinode) { 4086a361293fSJan Kara spin_unlock(&inode->i_lock); 4087a361293fSJan Kara return -ENOMEM; 4088a361293fSJan Kara } 4089a361293fSJan Kara ei->jinode = jinode; 4090a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4091a361293fSJan Kara jinode = NULL; 4092a361293fSJan Kara } 4093a361293fSJan Kara spin_unlock(&inode->i_lock); 4094a361293fSJan Kara if (unlikely(jinode != NULL)) 4095a361293fSJan Kara jbd2_free_inode(jinode); 4096a361293fSJan Kara return 0; 4097a361293fSJan Kara } 4098a361293fSJan Kara 4099a4bb6b64SAllison Henderson /* 4100617ba13bSMingming Cao * ext4_truncate() 4101ac27a0ecSDave Kleikamp * 4102617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4103617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4104ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4105ac27a0ecSDave Kleikamp * 410642b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4107ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4108ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4109ac27a0ecSDave Kleikamp * 4110ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4111ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4112ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4113ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4114ac27a0ecSDave Kleikamp * left-to-right works OK too). 4115ac27a0ecSDave Kleikamp * 4116ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4117ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4118ac27a0ecSDave Kleikamp * 4119ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4120617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4121ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4122617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4123617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4124ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4125617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4126ac27a0ecSDave Kleikamp */ 41272c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4128ac27a0ecSDave Kleikamp { 4129819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4130819c4920STheodore Ts'o unsigned int credits; 41314209ae12SHarshad Shirwadkar int err = 0, err2; 4132819c4920STheodore Ts'o handle_t *handle; 4133819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4134819c4920STheodore Ts'o 413519b5ef61STheodore Ts'o /* 413619b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4137e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 4138f340b3d9Shongnanli * have i_rwsem locked because it's not necessary. 413919b5ef61STheodore Ts'o */ 414019b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41415955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41420562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41430562e0baSJiaying Zhang 414491ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41459a5d265fSzhengliang goto out_trace; 4146ac27a0ecSDave Kleikamp 41475534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 414819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41497d8f9f7dSTheodore Ts'o 4150aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4151aef1c851STao Ma int has_inline = 1; 4152aef1c851STao Ma 415301daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 41549a5d265fSzhengliang if (err || has_inline) 41559a5d265fSzhengliang goto out_trace; 4156aef1c851STao Ma } 4157aef1c851STao Ma 4158a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4159a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4160a71248b1SBaokun Li err = ext4_inode_attach_jinode(inode); 4161a71248b1SBaokun Li if (err) 41629a5d265fSzhengliang goto out_trace; 4163a361293fSJan Kara } 4164a361293fSJan Kara 4165ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4166819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4167ff9893dcSAmir Goldstein else 4168819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4169819c4920STheodore Ts'o 4170819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 41719a5d265fSzhengliang if (IS_ERR(handle)) { 41729a5d265fSzhengliang err = PTR_ERR(handle); 41739a5d265fSzhengliang goto out_trace; 41749a5d265fSzhengliang } 4175819c4920STheodore Ts'o 4176eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4177eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4178819c4920STheodore Ts'o 4179819c4920STheodore Ts'o /* 4180819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4181819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4182819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4183819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4184819c4920STheodore Ts'o * 4185819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4186819c4920STheodore Ts'o * truncatable state while each transaction commits. 4187819c4920STheodore Ts'o */ 41882c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 41892c98eb5eSTheodore Ts'o if (err) 4190819c4920STheodore Ts'o goto out_stop; 4191819c4920STheodore Ts'o 4192819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4193819c4920STheodore Ts'o 419427bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4195819c4920STheodore Ts'o 4196819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4197d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4198819c4920STheodore Ts'o else 4199819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4200819c4920STheodore Ts'o 4201819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4202d0abb36dSTheodore Ts'o if (err) 4203d0abb36dSTheodore Ts'o goto out_stop; 4204819c4920STheodore Ts'o 4205819c4920STheodore Ts'o if (IS_SYNC(inode)) 4206819c4920STheodore Ts'o ext4_handle_sync(handle); 4207819c4920STheodore Ts'o 4208819c4920STheodore Ts'o out_stop: 4209819c4920STheodore Ts'o /* 4210819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4211819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4212819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 421358d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4214819c4920STheodore Ts'o * orphan info for us. 4215819c4920STheodore Ts'o */ 4216819c4920STheodore Ts'o if (inode->i_nlink) 4217819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4218819c4920STheodore Ts'o 4219eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 42204209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 42214209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 42224209ae12SHarshad Shirwadkar err = err2; 4223819c4920STheodore Ts'o ext4_journal_stop(handle); 4224a86c6181SAlex Tomas 42259a5d265fSzhengliang out_trace: 42260562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42272c98eb5eSTheodore Ts'o return err; 4228ac27a0ecSDave Kleikamp } 4229ac27a0ecSDave Kleikamp 42309a1bf32cSZhang Yi static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 42319a1bf32cSZhang Yi { 42329a1bf32cSZhang Yi if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 42339a1bf32cSZhang Yi return inode_peek_iversion_raw(inode); 42349a1bf32cSZhang Yi else 42359a1bf32cSZhang Yi return inode_peek_iversion(inode); 42369a1bf32cSZhang Yi } 42379a1bf32cSZhang Yi 42389a1bf32cSZhang Yi static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, 42399a1bf32cSZhang Yi struct ext4_inode_info *ei) 42409a1bf32cSZhang Yi { 42419a1bf32cSZhang Yi struct inode *inode = &(ei->vfs_inode); 42429a1bf32cSZhang Yi u64 i_blocks = READ_ONCE(inode->i_blocks); 42439a1bf32cSZhang Yi struct super_block *sb = inode->i_sb; 42449a1bf32cSZhang Yi 42459a1bf32cSZhang Yi if (i_blocks <= ~0U) { 42469a1bf32cSZhang Yi /* 42479a1bf32cSZhang Yi * i_blocks can be represented in a 32 bit variable 42489a1bf32cSZhang Yi * as multiple of 512 bytes 42499a1bf32cSZhang Yi */ 42509a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42519a1bf32cSZhang Yi raw_inode->i_blocks_high = 0; 42529a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42539a1bf32cSZhang Yi return 0; 42549a1bf32cSZhang Yi } 42559a1bf32cSZhang Yi 42569a1bf32cSZhang Yi /* 42579a1bf32cSZhang Yi * This should never happen since sb->s_maxbytes should not have 42589a1bf32cSZhang Yi * allowed this, sb->s_maxbytes was set according to the huge_file 42599a1bf32cSZhang Yi * feature in ext4_fill_super(). 42609a1bf32cSZhang Yi */ 42619a1bf32cSZhang Yi if (!ext4_has_feature_huge_file(sb)) 42629a1bf32cSZhang Yi return -EFSCORRUPTED; 42639a1bf32cSZhang Yi 42649a1bf32cSZhang Yi if (i_blocks <= 0xffffffffffffULL) { 42659a1bf32cSZhang Yi /* 42669a1bf32cSZhang Yi * i_blocks can be represented in a 48 bit variable 42679a1bf32cSZhang Yi * as multiple of 512 bytes 42689a1bf32cSZhang Yi */ 42699a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42709a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42719a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42729a1bf32cSZhang Yi } else { 42739a1bf32cSZhang Yi ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42749a1bf32cSZhang Yi /* i_block is stored in file system block size */ 42759a1bf32cSZhang Yi i_blocks = i_blocks >> (inode->i_blkbits - 9); 42769a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42779a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42789a1bf32cSZhang Yi } 42799a1bf32cSZhang Yi return 0; 42809a1bf32cSZhang Yi } 42819a1bf32cSZhang Yi 42829a1bf32cSZhang Yi static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) 42839a1bf32cSZhang Yi { 42849a1bf32cSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 42859a1bf32cSZhang Yi uid_t i_uid; 42869a1bf32cSZhang Yi gid_t i_gid; 42879a1bf32cSZhang Yi projid_t i_projid; 42889a1bf32cSZhang Yi int block; 42899a1bf32cSZhang Yi int err; 42909a1bf32cSZhang Yi 42919a1bf32cSZhang Yi err = ext4_inode_blocks_set(raw_inode, ei); 42929a1bf32cSZhang Yi 42939a1bf32cSZhang Yi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 42949a1bf32cSZhang Yi i_uid = i_uid_read(inode); 42959a1bf32cSZhang Yi i_gid = i_gid_read(inode); 42969a1bf32cSZhang Yi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 42979a1bf32cSZhang Yi if (!(test_opt(inode->i_sb, NO_UID32))) { 42989a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 42999a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 43009a1bf32cSZhang Yi /* 43019a1bf32cSZhang Yi * Fix up interoperability with old kernels. Otherwise, 43029a1bf32cSZhang Yi * old inodes get re-used with the upper 16 bits of the 43039a1bf32cSZhang Yi * uid/gid intact. 43049a1bf32cSZhang Yi */ 43059a1bf32cSZhang Yi if (ei->i_dtime && list_empty(&ei->i_orphan)) { 43069a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 43079a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 43089a1bf32cSZhang Yi } else { 43099a1bf32cSZhang Yi raw_inode->i_uid_high = 43109a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_uid)); 43119a1bf32cSZhang Yi raw_inode->i_gid_high = 43129a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_gid)); 43139a1bf32cSZhang Yi } 43149a1bf32cSZhang Yi } else { 43159a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 43169a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 43179a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 43189a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 43199a1bf32cSZhang Yi } 43209a1bf32cSZhang Yi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 43219a1bf32cSZhang Yi 43229a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 43239a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 43249a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 43259a1bf32cSZhang Yi EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 43269a1bf32cSZhang Yi 43279a1bf32cSZhang Yi raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 43289a1bf32cSZhang Yi raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 43299a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 43309a1bf32cSZhang Yi raw_inode->i_file_acl_high = 43319a1bf32cSZhang Yi cpu_to_le16(ei->i_file_acl >> 32); 43329a1bf32cSZhang Yi raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 43339a1bf32cSZhang Yi ext4_isize_set(raw_inode, ei->i_disksize); 43349a1bf32cSZhang Yi 43359a1bf32cSZhang Yi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 43369a1bf32cSZhang Yi if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 43379a1bf32cSZhang Yi if (old_valid_dev(inode->i_rdev)) { 43389a1bf32cSZhang Yi raw_inode->i_block[0] = 43399a1bf32cSZhang Yi cpu_to_le32(old_encode_dev(inode->i_rdev)); 43409a1bf32cSZhang Yi raw_inode->i_block[1] = 0; 43419a1bf32cSZhang Yi } else { 43429a1bf32cSZhang Yi raw_inode->i_block[0] = 0; 43439a1bf32cSZhang Yi raw_inode->i_block[1] = 43449a1bf32cSZhang Yi cpu_to_le32(new_encode_dev(inode->i_rdev)); 43459a1bf32cSZhang Yi raw_inode->i_block[2] = 0; 43469a1bf32cSZhang Yi } 43479a1bf32cSZhang Yi } else if (!ext4_has_inline_data(inode)) { 43489a1bf32cSZhang Yi for (block = 0; block < EXT4_N_BLOCKS; block++) 43499a1bf32cSZhang Yi raw_inode->i_block[block] = ei->i_data[block]; 43509a1bf32cSZhang Yi } 43519a1bf32cSZhang Yi 43529a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 43539a1bf32cSZhang Yi u64 ivers = ext4_inode_peek_iversion(inode); 43549a1bf32cSZhang Yi 43559a1bf32cSZhang Yi raw_inode->i_disk_version = cpu_to_le32(ivers); 43569a1bf32cSZhang Yi if (ei->i_extra_isize) { 43579a1bf32cSZhang Yi if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 43589a1bf32cSZhang Yi raw_inode->i_version_hi = 43599a1bf32cSZhang Yi cpu_to_le32(ivers >> 32); 43609a1bf32cSZhang Yi raw_inode->i_extra_isize = 43619a1bf32cSZhang Yi cpu_to_le16(ei->i_extra_isize); 43629a1bf32cSZhang Yi } 43639a1bf32cSZhang Yi } 43649a1bf32cSZhang Yi 43659a1bf32cSZhang Yi if (i_projid != EXT4_DEF_PROJID && 43669a1bf32cSZhang Yi !ext4_has_feature_project(inode->i_sb)) 43679a1bf32cSZhang Yi err = err ?: -EFSCORRUPTED; 43689a1bf32cSZhang Yi 43699a1bf32cSZhang Yi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 43709a1bf32cSZhang Yi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 43719a1bf32cSZhang Yi raw_inode->i_projid = cpu_to_le32(i_projid); 43729a1bf32cSZhang Yi 43739a1bf32cSZhang Yi ext4_inode_csum_set(inode, raw_inode, ei); 43749a1bf32cSZhang Yi return err; 43759a1bf32cSZhang Yi } 43769a1bf32cSZhang Yi 4377ac27a0ecSDave Kleikamp /* 4378617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4379de01f484SZhang Yi * underlying buffer_head on success. If we pass 'inode' and it does not 4380de01f484SZhang Yi * have in-inode xattr, we have all inode data in memory that is needed 4381de01f484SZhang Yi * to recreate the on-disk version of this inode. 4382ac27a0ecSDave Kleikamp */ 43838016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 4384de01f484SZhang Yi struct inode *inode, struct ext4_iloc *iloc, 43858016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4386ac27a0ecSDave Kleikamp { 4387240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4388ac27a0ecSDave Kleikamp struct buffer_head *bh; 4389240799cdSTheodore Ts'o ext4_fsblk_t block; 439002f03c42SLinus Torvalds struct blk_plug plug; 4391240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4392ac27a0ecSDave Kleikamp 43933a06d778SAneesh Kumar K.V iloc->bh = NULL; 43948016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 43958016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 43966a797d27SDarrick J. Wong return -EFSCORRUPTED; 4397ac27a0ecSDave Kleikamp 43988016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4399240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4400240799cdSTheodore Ts'o if (!gdp) 4401240799cdSTheodore Ts'o return -EIO; 4402240799cdSTheodore Ts'o 4403240799cdSTheodore Ts'o /* 4404240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4405240799cdSTheodore Ts'o */ 440600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 44078016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4408240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4409240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4410240799cdSTheodore Ts'o 4411eee22187SBaokun Li block = ext4_inode_table(sb, gdp); 4412eee22187SBaokun Li if ((block <= le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) || 4413eee22187SBaokun Li (block >= ext4_blocks_count(EXT4_SB(sb)->s_es))) { 4414eee22187SBaokun Li ext4_error(sb, "Invalid inode table block %llu in " 4415eee22187SBaokun Li "block_group %u", block, iloc->block_group); 4416eee22187SBaokun Li return -EFSCORRUPTED; 4417eee22187SBaokun Li } 4418eee22187SBaokun Li block += (inode_offset / inodes_per_block); 4419eee22187SBaokun Li 4420240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4421aebf0243SWang Shilong if (unlikely(!bh)) 4422860d21e2STheodore Ts'o return -ENOMEM; 44238e33fadfSZhang Yi if (ext4_buffer_uptodate(bh)) 4424ac27a0ecSDave Kleikamp goto has_buffer; 4425ac27a0ecSDave Kleikamp 44268e33fadfSZhang Yi lock_buffer(bh); 4427f2c77973SZhang Yi if (ext4_buffer_uptodate(bh)) { 4428f2c77973SZhang Yi /* Someone brought it uptodate while we waited */ 4429f2c77973SZhang Yi unlock_buffer(bh); 4430f2c77973SZhang Yi goto has_buffer; 4431f2c77973SZhang Yi } 4432f2c77973SZhang Yi 4433ac27a0ecSDave Kleikamp /* 4434ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4435ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4436ac27a0ecSDave Kleikamp * block. 4437ac27a0ecSDave Kleikamp */ 4438de01f484SZhang Yi if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 4439ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4440240799cdSTheodore Ts'o int i, start; 4441ac27a0ecSDave Kleikamp 4442240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4443ac27a0ecSDave Kleikamp 4444ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4445240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4446aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4447ac27a0ecSDave Kleikamp goto make_io; 4448ac27a0ecSDave Kleikamp 4449ac27a0ecSDave Kleikamp /* 4450ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4451ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4452ac27a0ecSDave Kleikamp * of one, so skip it. 4453ac27a0ecSDave Kleikamp */ 4454ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4455ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4456ac27a0ecSDave Kleikamp goto make_io; 4457ac27a0ecSDave Kleikamp } 4458240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4459ac27a0ecSDave Kleikamp if (i == inode_offset) 4460ac27a0ecSDave Kleikamp continue; 4461617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4462ac27a0ecSDave Kleikamp break; 4463ac27a0ecSDave Kleikamp } 4464ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4465240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4466de01f484SZhang Yi struct ext4_inode *raw_inode = 4467de01f484SZhang Yi (struct ext4_inode *) (bh->b_data + iloc->offset); 4468de01f484SZhang Yi 4469ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4470ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4471de01f484SZhang Yi if (!ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4472de01f484SZhang Yi ext4_fill_raw_inode(inode, raw_inode); 4473ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4474ac27a0ecSDave Kleikamp unlock_buffer(bh); 4475ac27a0ecSDave Kleikamp goto has_buffer; 4476ac27a0ecSDave Kleikamp } 4477ac27a0ecSDave Kleikamp } 4478ac27a0ecSDave Kleikamp 4479ac27a0ecSDave Kleikamp make_io: 4480ac27a0ecSDave Kleikamp /* 4481240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4482240799cdSTheodore Ts'o * blocks from the inode table. 4483240799cdSTheodore Ts'o */ 448402f03c42SLinus Torvalds blk_start_plug(&plug); 4485240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4486240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4487240799cdSTheodore Ts'o unsigned num; 44880d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4489240799cdSTheodore Ts'o 4490240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4491b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 44920d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4493240799cdSTheodore Ts'o if (table > b) 4494240799cdSTheodore Ts'o b = table; 44950d606e2cSTheodore Ts'o end = b + ra_blks; 4496240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4497feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4498560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4499240799cdSTheodore Ts'o table += num / inodes_per_block; 4500240799cdSTheodore Ts'o if (end > table) 4501240799cdSTheodore Ts'o end = table; 4502240799cdSTheodore Ts'o while (b <= end) 45035df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4504240799cdSTheodore Ts'o } 4505240799cdSTheodore Ts'o 4506240799cdSTheodore Ts'o /* 4507ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4508ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4509ac27a0ecSDave Kleikamp * Read the block from disk. 4510ac27a0ecSDave Kleikamp */ 45118016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 45122d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 451302f03c42SLinus Torvalds blk_finish_plug(&plug); 4514ac27a0ecSDave Kleikamp wait_on_buffer(bh); 45150904c9aeSZhang Yi ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4516ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 45178016e29fSHarshad Shirwadkar if (ret_block) 45188016e29fSHarshad Shirwadkar *ret_block = block; 4519ac27a0ecSDave Kleikamp brelse(bh); 4520ac27a0ecSDave Kleikamp return -EIO; 4521ac27a0ecSDave Kleikamp } 4522ac27a0ecSDave Kleikamp has_buffer: 4523ac27a0ecSDave Kleikamp iloc->bh = bh; 4524ac27a0ecSDave Kleikamp return 0; 4525ac27a0ecSDave Kleikamp } 4526ac27a0ecSDave Kleikamp 45278016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 45288016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45298016e29fSHarshad Shirwadkar { 4530c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 45318016e29fSHarshad Shirwadkar int ret; 45328016e29fSHarshad Shirwadkar 4533de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc, 45348016e29fSHarshad Shirwadkar &err_blk); 45358016e29fSHarshad Shirwadkar 45368016e29fSHarshad Shirwadkar if (ret == -EIO) 45378016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 45388016e29fSHarshad Shirwadkar "unable to read itable block"); 45398016e29fSHarshad Shirwadkar 45408016e29fSHarshad Shirwadkar return ret; 45418016e29fSHarshad Shirwadkar } 45428016e29fSHarshad Shirwadkar 4543617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4544ac27a0ecSDave Kleikamp { 4545c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 45468016e29fSHarshad Shirwadkar int ret; 45478016e29fSHarshad Shirwadkar 4548de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc, 4549de01f484SZhang Yi &err_blk); 45508016e29fSHarshad Shirwadkar 45518016e29fSHarshad Shirwadkar if (ret == -EIO) 45528016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 45538016e29fSHarshad Shirwadkar "unable to read itable block"); 45548016e29fSHarshad Shirwadkar 45558016e29fSHarshad Shirwadkar return ret; 45568016e29fSHarshad Shirwadkar } 45578016e29fSHarshad Shirwadkar 45588016e29fSHarshad Shirwadkar 45598016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 45608016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45618016e29fSHarshad Shirwadkar { 4562de01f484SZhang Yi return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL); 4563ac27a0ecSDave Kleikamp } 4564ac27a0ecSDave Kleikamp 4565a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 45666642586bSRoss Zwisler { 4567a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4568a8ab6d38SIra Weiny 45699cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 45706642586bSRoss Zwisler return false; 45716642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 45726642586bSRoss Zwisler return false; 45736642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 45746642586bSRoss Zwisler return false; 45756642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 45766642586bSRoss Zwisler return false; 4577592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 45786642586bSRoss Zwisler return false; 4579c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4580c93d8f88SEric Biggers return false; 4581a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4582a8ab6d38SIra Weiny return false; 4583a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 45846642586bSRoss Zwisler return true; 4585a8ab6d38SIra Weiny 4586b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 45876642586bSRoss Zwisler } 45886642586bSRoss Zwisler 4589043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4590ac27a0ecSDave Kleikamp { 4591617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 459200a1a053STheodore Ts'o unsigned int new_fl = 0; 4593ac27a0ecSDave Kleikamp 4594043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4595043546e4SIra Weiny 4596617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 459700a1a053STheodore Ts'o new_fl |= S_SYNC; 4598617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 459900a1a053STheodore Ts'o new_fl |= S_APPEND; 4600617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 460100a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4602617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 460300a1a053STheodore Ts'o new_fl |= S_NOATIME; 4604617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 460500a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4606043546e4SIra Weiny 4607043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4608043546e4SIra Weiny * here if already set. */ 4609043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4610043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4611923ae0ffSRoss Zwisler new_fl |= S_DAX; 4612043546e4SIra Weiny 46132ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 46142ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4615b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4616b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4617c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4618c93d8f88SEric Biggers new_fl |= S_VERITY; 46195f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 46202ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4621c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4622ac27a0ecSDave Kleikamp } 4623ac27a0ecSDave Kleikamp 46240fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 46250fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 46260fc1b451SAneesh Kumar K.V { 46270fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 46288180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 46298180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 46300fc1b451SAneesh Kumar K.V 4631e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 46320fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 46330fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 46340fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 463507a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 46368180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 46378180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 46388180a562SAneesh Kumar K.V } else { 46390fc1b451SAneesh Kumar K.V return i_blocks; 46408180a562SAneesh Kumar K.V } 46410fc1b451SAneesh Kumar K.V } else { 46420fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 46430fc1b451SAneesh Kumar K.V } 46440fc1b451SAneesh Kumar K.V } 4645ff9ddf7eSJan Kara 4646eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4647152a7b0aSTao Ma struct ext4_inode *raw_inode, 4648152a7b0aSTao Ma struct ext4_inode_info *ei) 4649152a7b0aSTao Ma { 4650152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4651152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4652eb9b5f01STheodore Ts'o 4653fd7e672eSBaokun Li if (EXT4_INODE_HAS_XATTR_SPACE(inode) && 4654290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 46551dcdce59SYe Bin int err; 46561dcdce59SYe Bin 4657152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 46581dcdce59SYe Bin err = ext4_find_inline_data_nolock(inode); 46591dcdce59SYe Bin if (!err && ext4_has_inline_data(inode)) 46601dcdce59SYe Bin ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 46611dcdce59SYe Bin return err; 4662f19d5870STao Ma } else 4663f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4664eb9b5f01STheodore Ts'o return 0; 4665152a7b0aSTao Ma } 4666152a7b0aSTao Ma 4667040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4668040cb378SLi Xi { 46690b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4670040cb378SLi Xi return -EOPNOTSUPP; 4671040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4672040cb378SLi Xi return 0; 4673040cb378SLi Xi } 4674040cb378SLi Xi 4675e254d1afSEryu Guan /* 4676e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4677e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4678e254d1afSEryu Guan * set. 4679e254d1afSEryu Guan */ 4680e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4681e254d1afSEryu Guan { 4682e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4683e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4684e254d1afSEryu Guan else 4685e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4686e254d1afSEryu Guan } 4687e254d1afSEryu Guan 46888a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 46898a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 46908a363970STheodore Ts'o unsigned int line) 4691ac27a0ecSDave Kleikamp { 4692617ba13bSMingming Cao struct ext4_iloc iloc; 4693617ba13bSMingming Cao struct ext4_inode *raw_inode; 46941d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4695bd2c38cfSJan Kara struct ext4_super_block *es = EXT4_SB(sb)->s_es; 46961d1fe1eeSDavid Howells struct inode *inode; 4697b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 46981d1fe1eeSDavid Howells long ret; 46997e6e1ef4SDarrick J. Wong loff_t size; 4700ac27a0ecSDave Kleikamp int block; 470108cefc7aSEric W. Biederman uid_t i_uid; 470208cefc7aSEric W. Biederman gid_t i_gid; 4703040cb378SLi Xi projid_t i_projid; 4704ac27a0ecSDave Kleikamp 4705191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 4706bd2c38cfSJan Kara ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4707bd2c38cfSJan Kara ino == le32_to_cpu(es->s_usr_quota_inum) || 4708bd2c38cfSJan Kara ino == le32_to_cpu(es->s_grp_quota_inum) || 470902f310fcSJan Kara ino == le32_to_cpu(es->s_prj_quota_inum) || 471002f310fcSJan Kara ino == le32_to_cpu(es->s_orphan_file_inum))) || 47118a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 4712bd2c38cfSJan Kara (ino > le32_to_cpu(es->s_inodes_count))) { 47138a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 47148a363970STheodore Ts'o return ERR_PTR(-ESTALE); 4715014c9caaSJan Kara __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 47168a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 47178a363970STheodore Ts'o ino, current->comm); 47188a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 47198a363970STheodore Ts'o } 47208a363970STheodore Ts'o 47211d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 47221d1fe1eeSDavid Howells if (!inode) 47231d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 47241d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 47251d1fe1eeSDavid Howells return inode; 47261d1fe1eeSDavid Howells 47271d1fe1eeSDavid Howells ei = EXT4_I(inode); 47287dc57615SPeter Huewe iloc.bh = NULL; 4729ac27a0ecSDave Kleikamp 47308016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 47311d1fe1eeSDavid Howells if (ret < 0) 4732ac27a0ecSDave Kleikamp goto bad_inode; 4733617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4734814525f4SDarrick J. Wong 47358a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 47368a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 47378a363970STheodore Ts'o ret = -ESTALE; 47388a363970STheodore Ts'o goto bad_inode; 47398a363970STheodore Ts'o } 47408a363970STheodore Ts'o 4741814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4742814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4743814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 47442dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 47452dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 47468a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47478a363970STheodore Ts'o "iget: bad extra_isize %u " 47488a363970STheodore Ts'o "(inode size %u)", 47492dc8d9e1SEric Biggers ei->i_extra_isize, 4750814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 47516a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4752814525f4SDarrick J. Wong goto bad_inode; 4753814525f4SDarrick J. Wong } 4754814525f4SDarrick J. Wong } else 4755814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4756814525f4SDarrick J. Wong 4757814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 47589aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4759814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4760814525f4SDarrick J. Wong __u32 csum; 4761814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4762814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4763814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4764814525f4SDarrick J. Wong sizeof(inum)); 4765814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4766814525f4SDarrick J. Wong sizeof(gen)); 4767814525f4SDarrick J. Wong } 4768814525f4SDarrick J. Wong 47698016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 47708016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 47718016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 47728016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 47738016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 47746a797d27SDarrick J. Wong ret = -EFSBADCRC; 4775814525f4SDarrick J. Wong goto bad_inode; 4776814525f4SDarrick J. Wong } 4777814525f4SDarrick J. Wong 4778ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 477908cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 478008cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 47810b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4782040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4783040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4784040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4785040cb378SLi Xi else 4786040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4787040cb378SLi Xi 4788ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 478908cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 479008cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4791ac27a0ecSDave Kleikamp } 479208cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 479308cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4794040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4795bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4796ac27a0ecSDave Kleikamp 4797353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 479867cf5b09STao Ma ei->i_inline_off = 0; 4799ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4800ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4801ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4802ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4803ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4804ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4805ac27a0ecSDave Kleikamp */ 4806ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 48075cd74028SBaokun Li if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL || 4808393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4809393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 48105cd74028SBaokun Li /* this inode is deleted or unallocated */ 48115cd74028SBaokun Li if (flags & EXT4_IGET_SPECIAL) { 48125cd74028SBaokun Li ext4_error_inode(inode, function, line, 0, 48135cd74028SBaokun Li "iget: special inode unallocated"); 48145cd74028SBaokun Li ret = -EFSCORRUPTED; 48155cd74028SBaokun Li } else 48161d1fe1eeSDavid Howells ret = -ESTALE; 4817ac27a0ecSDave Kleikamp goto bad_inode; 4818ac27a0ecSDave Kleikamp } 4819ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4820ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4821ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4822393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4823393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4824393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4825ac27a0ecSDave Kleikamp } 4826ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4827043546e4SIra Weiny ext4_set_inode_flags(inode, true); 48280fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 48297973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4830e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4831a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4832a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4833e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 48347e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 48358a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48368a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 48377e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 48387e6e1ef4SDarrick J. Wong goto bad_inode; 48397e6e1ef4SDarrick J. Wong } 484048a34311SJan Kara /* 484148a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 484248a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 484348a34311SJan Kara * checksumming that corrupts checksums so forbid that. 484448a34311SJan Kara */ 484548a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 484648a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 484748a34311SJan Kara ext4_error_inode(inode, function, line, 0, 484848a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 484948a34311SJan Kara ret = -EFSCORRUPTED; 485048a34311SJan Kara goto bad_inode; 485148a34311SJan Kara } 4852ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4853a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4854a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4855a9e7f447SDmitry Monakhov #endif 4856ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4857ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4858a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4859ac27a0ecSDave Kleikamp /* 4860ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4861ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4862ac27a0ecSDave Kleikamp */ 4863617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4864ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4865ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4866aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4867ac27a0ecSDave Kleikamp 4868b436b9beSJan Kara /* 4869b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4870b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4871b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4872b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4873b436b9beSJan Kara * now it is reread from disk. 4874b436b9beSJan Kara */ 4875b436b9beSJan Kara if (journal) { 4876b436b9beSJan Kara transaction_t *transaction; 4877b436b9beSJan Kara tid_t tid; 4878b436b9beSJan Kara 4879a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4880b436b9beSJan Kara if (journal->j_running_transaction) 4881b436b9beSJan Kara transaction = journal->j_running_transaction; 4882b436b9beSJan Kara else 4883b436b9beSJan Kara transaction = journal->j_committing_transaction; 4884b436b9beSJan Kara if (transaction) 4885b436b9beSJan Kara tid = transaction->t_tid; 4886b436b9beSJan Kara else 4887b436b9beSJan Kara tid = journal->j_commit_sequence; 4888a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4889b436b9beSJan Kara ei->i_sync_tid = tid; 4890b436b9beSJan Kara ei->i_datasync_tid = tid; 4891b436b9beSJan Kara } 4892b436b9beSJan Kara 48930040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4894ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4895ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 48962dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4897617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4898617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4899ac27a0ecSDave Kleikamp } else { 4900eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4901eb9b5f01STheodore Ts'o if (ret) 4902eb9b5f01STheodore Ts'o goto bad_inode; 4903ac27a0ecSDave Kleikamp } 4904814525f4SDarrick J. Wong } 4905ac27a0ecSDave Kleikamp 4906ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4907ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4908ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4909ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4910ef7f3835SKalpak Shah 4911ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4912ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4913ee73f9a5SJeff Layton 491425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 491525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4916ee73f9a5SJeff Layton ivers |= 491725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 491825ec56b5SJean Noel Cordenner } 4919e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4920c4f65706STheodore Ts'o } 492125ec56b5SJean Noel Cordenner 4922c4b5a614STheodore Ts'o ret = 0; 4923485c26ecSTheodore Ts'o if (ei->i_file_acl && 4924ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 49258a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49268a363970STheodore Ts'o "iget: bad extended attribute block %llu", 492724676da4STheodore Ts'o ei->i_file_acl); 49286a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4929485c26ecSTheodore Ts'o goto bad_inode; 4930f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4931bc716523SLiu Song /* validate the block references in the inode */ 49328016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 49338016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4934fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 49358016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 4936bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4937bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4938bc716523SLiu Song else 49391f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4940fe2c8191SThiemo Nagel } 4941f19d5870STao Ma } 4942567f3e9aSTheodore Ts'o if (ret) 49437a262f7cSAneesh Kumar K.V goto bad_inode; 49447a262f7cSAneesh Kumar K.V 4945ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4946617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4947617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4948617ba13bSMingming Cao ext4_set_aops(inode); 4949ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4950617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4951617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4952ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 49536390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 49546390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 49558a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49568a363970STheodore Ts'o "iget: immutable or append flags " 49578a363970STheodore Ts'o "not allowed on symlinks"); 49586390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 49596390d33bSLuis R. Rodriguez goto bad_inode; 49606390d33bSLuis R. Rodriguez } 4961592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4962a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4963a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 496475e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4965617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4966e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4967e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4968e83c1397SDuane Griffin } else { 4969617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4970ac27a0ecSDave Kleikamp } 4971563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4972563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4973617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4974ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4975ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4976ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4977ac27a0ecSDave Kleikamp else 4978ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4979ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4980393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4981393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4982563bdd61STheodore Ts'o } else { 49836a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 49848a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49858a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4986563bdd61STheodore Ts'o goto bad_inode; 4987ac27a0ecSDave Kleikamp } 49886456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 49896456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49906456ca65STheodore Ts'o "casefold flag without casefold feature"); 499163b1e9bcSBaokun Li if (is_bad_inode(inode) && !(flags & EXT4_IGET_BAD)) { 499263b1e9bcSBaokun Li ext4_error_inode(inode, function, line, 0, 499363b1e9bcSBaokun Li "bad inode without EXT4_IGET_BAD flag"); 499463b1e9bcSBaokun Li ret = -EUCLEAN; 499563b1e9bcSBaokun Li goto bad_inode; 499663b1e9bcSBaokun Li } 4997dec214d0STahsin Erdogan 499863b1e9bcSBaokun Li brelse(iloc.bh); 49991d1fe1eeSDavid Howells unlock_new_inode(inode); 50001d1fe1eeSDavid Howells return inode; 5001ac27a0ecSDave Kleikamp 5002ac27a0ecSDave Kleikamp bad_inode: 5003567f3e9aSTheodore Ts'o brelse(iloc.bh); 50041d1fe1eeSDavid Howells iget_failed(inode); 50051d1fe1eeSDavid Howells return ERR_PTR(ret); 5006ac27a0ecSDave Kleikamp } 5007ac27a0ecSDave Kleikamp 50083f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 50093f19b2abSDavid Howells unsigned long orig_ino, 50103f19b2abSDavid Howells unsigned long ino, 50113f19b2abSDavid Howells struct ext4_inode *raw_inode) 5012a26f4992STheodore Ts'o { 50133f19b2abSDavid Howells struct inode *inode; 5014a26f4992STheodore Ts'o 50153f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 50163f19b2abSDavid Howells if (!inode) 50173f19b2abSDavid Howells return; 50183f19b2abSDavid Howells 5019ed296c6cSEric Biggers if (!inode_is_dirtytime_only(inode)) 50203f19b2abSDavid Howells return; 50213f19b2abSDavid Howells 5022a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 5023ed296c6cSEric Biggers if (inode_is_dirtytime_only(inode)) { 5024a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5025a26f4992STheodore Ts'o 50265fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 5027a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5028a26f4992STheodore Ts'o 5029a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 50303f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 50313f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 50323f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 50333f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 5034a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 50353f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 50363f19b2abSDavid Howells return; 5037a26f4992STheodore Ts'o } 5038a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5039a26f4992STheodore Ts'o } 5040a26f4992STheodore Ts'o 5041a26f4992STheodore Ts'o /* 5042a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 5043a26f4992STheodore Ts'o * the same inode table block. 5044a26f4992STheodore Ts'o */ 5045a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 5046a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 5047a26f4992STheodore Ts'o { 5048a26f4992STheodore Ts'o unsigned long ino; 5049a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 5050a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 5051a26f4992STheodore Ts'o 50520f0ff9a9STheodore Ts'o /* 50530f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 50540f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 50550f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 50560f0ff9a9STheodore Ts'o */ 50570f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 50583f19b2abSDavid Howells rcu_read_lock(); 5059a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5060a26f4992STheodore Ts'o if (ino == orig_ino) 5061a26f4992STheodore Ts'o continue; 50623f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 50633f19b2abSDavid Howells (struct ext4_inode *)buf); 5064a26f4992STheodore Ts'o } 50653f19b2abSDavid Howells rcu_read_unlock(); 5066a26f4992STheodore Ts'o } 5067a26f4992STheodore Ts'o 5068664bd38bSZhang Yi /* 5069664bd38bSZhang Yi * Post the struct inode info into an on-disk inode location in the 5070664bd38bSZhang Yi * buffer-cache. This gobbles the caller's reference to the 5071664bd38bSZhang Yi * buffer_head in the inode location struct. 5072664bd38bSZhang Yi * 5073664bd38bSZhang Yi * The caller must have write access to iloc->bh. 5074664bd38bSZhang Yi */ 5075664bd38bSZhang Yi static int ext4_do_update_inode(handle_t *handle, 5076664bd38bSZhang Yi struct inode *inode, 5077664bd38bSZhang Yi struct ext4_iloc *iloc) 5078664bd38bSZhang Yi { 5079664bd38bSZhang Yi struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5080664bd38bSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 5081664bd38bSZhang Yi struct buffer_head *bh = iloc->bh; 5082664bd38bSZhang Yi struct super_block *sb = inode->i_sb; 5083664bd38bSZhang Yi int err; 5084664bd38bSZhang Yi int need_datasync = 0, set_large_file = 0; 5085664bd38bSZhang Yi 5086664bd38bSZhang Yi spin_lock(&ei->i_raw_lock); 5087664bd38bSZhang Yi 5088664bd38bSZhang Yi /* 5089664bd38bSZhang Yi * For fields not tracked in the in-memory inode, initialise them 5090664bd38bSZhang Yi * to zero for new inodes. 5091664bd38bSZhang Yi */ 5092664bd38bSZhang Yi if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5093664bd38bSZhang Yi memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5094664bd38bSZhang Yi 5095664bd38bSZhang Yi if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) 5096664bd38bSZhang Yi need_datasync = 1; 5097664bd38bSZhang Yi if (ei->i_disksize > 0x7fffffffULL) { 5098664bd38bSZhang Yi if (!ext4_has_feature_large_file(sb) || 5099664bd38bSZhang Yi EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) 5100664bd38bSZhang Yi set_large_file = 1; 5101664bd38bSZhang Yi } 5102664bd38bSZhang Yi 5103664bd38bSZhang Yi err = ext4_fill_raw_inode(inode, raw_inode); 5104202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 5105baaae979SZhang Yi if (err) { 5106baaae979SZhang Yi EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5107baaae979SZhang Yi goto out_brelse; 5108baaae979SZhang Yi } 5109baaae979SZhang Yi 51101751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5111a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5112a26f4992STheodore Ts'o bh->b_data); 5113202ee5dfSTheodore Ts'o 51140390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 51157d8bd3c7SShijie Luo err = ext4_handle_dirty_metadata(handle, NULL, bh); 51167d8bd3c7SShijie Luo if (err) 5117baaae979SZhang Yi goto out_error; 511819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5119202ee5dfSTheodore Ts'o if (set_large_file) { 51205d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5121188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, 5122188c299eSJan Kara EXT4_SB(sb)->s_sbh, 5123188c299eSJan Kara EXT4_JTR_NONE); 5124202ee5dfSTheodore Ts'o if (err) 5125baaae979SZhang Yi goto out_error; 512605c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 5127e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 512805c2c00fSJan Kara ext4_superblock_csum_set(sb); 512905c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 5130202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5131a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, 5132a3f5cf14SJan Kara EXT4_SB(sb)->s_sbh); 5133202ee5dfSTheodore Ts'o } 5134b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5135baaae979SZhang Yi out_error: 5136baaae979SZhang Yi ext4_std_error(inode->i_sb, err); 5137ac27a0ecSDave Kleikamp out_brelse: 5138ac27a0ecSDave Kleikamp brelse(bh); 5139ac27a0ecSDave Kleikamp return err; 5140ac27a0ecSDave Kleikamp } 5141ac27a0ecSDave Kleikamp 5142ac27a0ecSDave Kleikamp /* 5143617ba13bSMingming Cao * ext4_write_inode() 5144ac27a0ecSDave Kleikamp * 5145ac27a0ecSDave Kleikamp * We are called from a few places: 5146ac27a0ecSDave Kleikamp * 514787f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5148ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51494907cb7bSAnatol Pomozov * transaction to commit. 5150ac27a0ecSDave Kleikamp * 515187f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 515287f7e416STheodore Ts'o * We wait on commit, if told to. 5153ac27a0ecSDave Kleikamp * 515487f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 515587f7e416STheodore Ts'o * We wait on commit, if told to. 5156ac27a0ecSDave Kleikamp * 5157ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5158ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 515987f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 516087f7e416STheodore Ts'o * writeback. 5161ac27a0ecSDave Kleikamp * 5162ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5163ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5164ac27a0ecSDave Kleikamp * which we are interested. 5165ac27a0ecSDave Kleikamp * 5166ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5167ac27a0ecSDave Kleikamp * 5168ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5169ac27a0ecSDave Kleikamp * stuff(); 5170ac27a0ecSDave Kleikamp * inode->i_size = expr; 5171ac27a0ecSDave Kleikamp * 517287f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 517387f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 517487f7e416STheodore Ts'o * superblock's dirty inode list. 5175ac27a0ecSDave Kleikamp */ 5176a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5177ac27a0ecSDave Kleikamp { 517891ac6f43SFrank Mayhar int err; 517991ac6f43SFrank Mayhar 518018f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 518118f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5182ac27a0ecSDave Kleikamp return 0; 5183ac27a0ecSDave Kleikamp 518418f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 518518f2c4fcSTheodore Ts'o return -EIO; 518618f2c4fcSTheodore Ts'o 518791ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5188617ba13bSMingming Cao if (ext4_journal_current_handle()) { 51894978c659SJan Kara ext4_debug("called recursively, non-PF_MEMALLOC!\n"); 5190ac27a0ecSDave Kleikamp dump_stack(); 5191ac27a0ecSDave Kleikamp return -EIO; 5192ac27a0ecSDave Kleikamp } 5193ac27a0ecSDave Kleikamp 519410542c22SJan Kara /* 519510542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 519610542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 519710542c22SJan Kara * written. 519810542c22SJan Kara */ 519910542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5200ac27a0ecSDave Kleikamp return 0; 5201ac27a0ecSDave Kleikamp 5202aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 520318f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 520491ac6f43SFrank Mayhar } else { 520591ac6f43SFrank Mayhar struct ext4_iloc iloc; 520691ac6f43SFrank Mayhar 52078016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 520891ac6f43SFrank Mayhar if (err) 520991ac6f43SFrank Mayhar return err; 521010542c22SJan Kara /* 521110542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 521210542c22SJan Kara * it here separately for each inode. 521310542c22SJan Kara */ 521410542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5215830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5216830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 521754d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5218c398eda0STheodore Ts'o "IO error syncing inode"); 5219830156c7SFrank Mayhar err = -EIO; 5220830156c7SFrank Mayhar } 5221fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 522291ac6f43SFrank Mayhar } 522391ac6f43SFrank Mayhar return err; 5224ac27a0ecSDave Kleikamp } 5225ac27a0ecSDave Kleikamp 5226ac27a0ecSDave Kleikamp /* 5227ccd16945SMatthew Wilcox (Oracle) * In data=journal mode ext4_journalled_invalidate_folio() may fail to invalidate 5228ccd16945SMatthew Wilcox (Oracle) * buffers that are attached to a folio straddling i_size and are undergoing 522953e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 523053e87268SJan Kara */ 523153e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 523253e87268SJan Kara { 523353e87268SJan Kara unsigned offset; 523453e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 523553e87268SJan Kara tid_t commit_tid = 0; 523653e87268SJan Kara int ret; 523753e87268SJan Kara 523809cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 523953e87268SJan Kara /* 5240ccd16945SMatthew Wilcox (Oracle) * If the folio is fully truncated, we don't need to wait for any commit 5241ccd16945SMatthew Wilcox (Oracle) * (and we even should not as __ext4_journalled_invalidate_folio() may 5242ccd16945SMatthew Wilcox (Oracle) * strip all buffers from the folio but keep the folio dirty which can then 52433f079114SJan Kara * confuse e.g. concurrent ext4_writepages() seeing dirty folio without 5244565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5245ccd16945SMatthew Wilcox (Oracle) * the folio remain valid. This is most beneficial for the common case of 5246565333a1Syangerkun * blocksize == PAGESIZE. 524753e87268SJan Kara */ 5248565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 524953e87268SJan Kara return; 525053e87268SJan Kara while (1) { 5251ccd16945SMatthew Wilcox (Oracle) struct folio *folio = filemap_lock_folio(inode->i_mapping, 525209cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 5253ccd16945SMatthew Wilcox (Oracle) if (!folio) 525453e87268SJan Kara return; 5255ccd16945SMatthew Wilcox (Oracle) ret = __ext4_journalled_invalidate_folio(folio, offset, 5256ccd16945SMatthew Wilcox (Oracle) folio_size(folio) - offset); 5257ccd16945SMatthew Wilcox (Oracle) folio_unlock(folio); 5258ccd16945SMatthew Wilcox (Oracle) folio_put(folio); 525953e87268SJan Kara if (ret != -EBUSY) 526053e87268SJan Kara return; 526153e87268SJan Kara commit_tid = 0; 526253e87268SJan Kara read_lock(&journal->j_state_lock); 526353e87268SJan Kara if (journal->j_committing_transaction) 526453e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 526553e87268SJan Kara read_unlock(&journal->j_state_lock); 526653e87268SJan Kara if (commit_tid) 526753e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 526853e87268SJan Kara } 526953e87268SJan Kara } 527053e87268SJan Kara 527153e87268SJan Kara /* 5272617ba13bSMingming Cao * ext4_setattr() 5273ac27a0ecSDave Kleikamp * 5274ac27a0ecSDave Kleikamp * Called from notify_change. 5275ac27a0ecSDave Kleikamp * 5276ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5277ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5278ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5279ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5280ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5281ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5282ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5283ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5284ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5285ac27a0ecSDave Kleikamp * 5286678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5287678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5288678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5289678aaf48SJan Kara * This way we are sure that all the data written in the previous 5290678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5291678aaf48SJan Kara * writeback). 5292678aaf48SJan Kara * 5293f340b3d9Shongnanli * Called with inode->i_rwsem down. 5294ac27a0ecSDave Kleikamp */ 5295c1632a0fSChristian Brauner int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 5296549c7297SChristian Brauner struct iattr *attr) 5297ac27a0ecSDave Kleikamp { 52982b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5299ac27a0ecSDave Kleikamp int error, rc = 0; 53003d287de3SDmitry Monakhov int orphan = 0; 5301ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5302a642c2c0SJeff Layton bool inc_ivers = true; 5303ac27a0ecSDave Kleikamp 53040db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 53050db1ff22STheodore Ts'o return -EIO; 53060db1ff22STheodore Ts'o 530702b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 530802b016caSTheodore Ts'o return -EPERM; 530902b016caSTheodore Ts'o 531002b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 531102b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 531202b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 531302b016caSTheodore Ts'o return -EPERM; 531402b016caSTheodore Ts'o 5315c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, attr); 5316ac27a0ecSDave Kleikamp if (error) 5317ac27a0ecSDave Kleikamp return error; 5318ac27a0ecSDave Kleikamp 53193ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 53203ce2b8ddSEric Biggers if (error) 53213ce2b8ddSEric Biggers return error; 53223ce2b8ddSEric Biggers 5323c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5324c93d8f88SEric Biggers if (error) 5325c93d8f88SEric Biggers return error; 5326c93d8f88SEric Biggers 5327f861646aSChristian Brauner if (is_quota_modification(idmap, inode, attr)) { 5328a7cdadeeSJan Kara error = dquot_initialize(inode); 5329a7cdadeeSJan Kara if (error) 5330a7cdadeeSJan Kara return error; 5331a7cdadeeSJan Kara } 53322729cfdcSHarshad Shirwadkar 53330dbe12f2SChristian Brauner if (i_uid_needs_update(idmap, attr, inode) || 53340dbe12f2SChristian Brauner i_gid_needs_update(idmap, attr, inode)) { 5335ac27a0ecSDave Kleikamp handle_t *handle; 5336ac27a0ecSDave Kleikamp 5337ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5338ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53399924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53409924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5341194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5342ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5343ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5344ac27a0ecSDave Kleikamp goto err_out; 5345ac27a0ecSDave Kleikamp } 53467a9ca53aSTahsin Erdogan 53477a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53487a9ca53aSTahsin Erdogan * counts xattr inode references. 53497a9ca53aSTahsin Erdogan */ 53507a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5351f861646aSChristian Brauner error = dquot_transfer(idmap, inode, attr); 53527a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53537a9ca53aSTahsin Erdogan 5354ac27a0ecSDave Kleikamp if (error) { 5355617ba13bSMingming Cao ext4_journal_stop(handle); 5356ac27a0ecSDave Kleikamp return error; 5357ac27a0ecSDave Kleikamp } 5358ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5359ac27a0ecSDave Kleikamp * one transaction */ 53600dbe12f2SChristian Brauner i_uid_update(idmap, attr, inode); 53610dbe12f2SChristian Brauner i_gid_update(idmap, attr, inode); 5362617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5363617ba13bSMingming Cao ext4_journal_stop(handle); 5364512c15efSPan Bian if (unlikely(error)) { 53654209ae12SHarshad Shirwadkar return error; 5366ac27a0ecSDave Kleikamp } 5367512c15efSPan Bian } 5368ac27a0ecSDave Kleikamp 53693da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53705208386cSJan Kara handle_t *handle; 53713da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5372f4534c9fSYe Bin loff_t old_disksize; 5373b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5374562c72aaSChristoph Hellwig 537512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5376e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5377e2b46574SEric Sandeen 5378aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 53790c095c7fSTheodore Ts'o return -EFBIG; 5380e2b46574SEric Sandeen } 5381aa75f4d3SHarshad Shirwadkar } 5382aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 53833da40c7bSJosef Bacik return -EINVAL; 5384aa75f4d3SHarshad Shirwadkar } 5385dff6efc3SChristoph Hellwig 5386a642c2c0SJeff Layton if (attr->ia_size == inode->i_size) 5387a642c2c0SJeff Layton inc_ivers = false; 5388dff6efc3SChristoph Hellwig 5389b9c1c267SJan Kara if (shrink) { 5390b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 53915208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 53925208386cSJan Kara attr->ia_size); 53935208386cSJan Kara if (error) 53945208386cSJan Kara goto err_out; 53955208386cSJan Kara } 5396b9c1c267SJan Kara /* 5397b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5398b9c1c267SJan Kara * for dio in flight. 5399b9c1c267SJan Kara */ 5400b9c1c267SJan Kara inode_dio_wait(inode); 5401b9c1c267SJan Kara } 5402b9c1c267SJan Kara 5403d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 5404b9c1c267SJan Kara 5405b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5406b9c1c267SJan Kara if (rc) { 5407d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 5408aa75f4d3SHarshad Shirwadkar goto err_out; 5409b9c1c267SJan Kara } 5410b9c1c267SJan Kara 54113da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 54129924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5413ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5414ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5415b9c1c267SJan Kara goto out_mmap_sem; 5416ac27a0ecSDave Kleikamp } 54173da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5418617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 54193d287de3SDmitry Monakhov orphan = 1; 54203d287de3SDmitry Monakhov } 5421911af577SEryu Guan /* 5422911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5423911af577SEryu Guan * update c/mtime in shrink case below 5424911af577SEryu Guan */ 5425911af577SEryu Guan if (!shrink) { 5426eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5427911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5428911af577SEryu Guan } 5429aa75f4d3SHarshad Shirwadkar 5430aa75f4d3SHarshad Shirwadkar if (shrink) 5431a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, 5432aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5433aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 54349725958bSXin Yin EXT_MAX_BLOCKS - 1); 5435aa75f4d3SHarshad Shirwadkar else 5436aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5437a80f7fcfSHarshad Shirwadkar handle, inode, 5438aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5439aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5440aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5441aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5442aa75f4d3SHarshad Shirwadkar 544390e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5444f4534c9fSYe Bin old_disksize = EXT4_I(inode)->i_disksize; 5445617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5446617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5447ac27a0ecSDave Kleikamp if (!error) 5448ac27a0ecSDave Kleikamp error = rc; 544990e775b7SJan Kara /* 545090e775b7SJan Kara * We have to update i_size under i_data_sem together 545190e775b7SJan Kara * with i_disksize to avoid races with writeback code 545290e775b7SJan Kara * running ext4_wb_update_i_disksize(). 545390e775b7SJan Kara */ 545490e775b7SJan Kara if (!error) 545590e775b7SJan Kara i_size_write(inode, attr->ia_size); 5456f4534c9fSYe Bin else 5457f4534c9fSYe Bin EXT4_I(inode)->i_disksize = old_disksize; 545890e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5459617ba13bSMingming Cao ext4_journal_stop(handle); 5460b9c1c267SJan Kara if (error) 5461b9c1c267SJan Kara goto out_mmap_sem; 546282a25b02SJan Kara if (!shrink) { 5463b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5464b9c1c267SJan Kara inode->i_size); 5465b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 546682a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5467b9c1c267SJan Kara } 5468430657b6SRoss Zwisler } 5469430657b6SRoss Zwisler 547053e87268SJan Kara /* 547153e87268SJan Kara * Truncate pagecache after we've waited for commit 547253e87268SJan Kara * in data=journal mode to make pages freeable. 547353e87268SJan Kara */ 54747caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5475b9c1c267SJan Kara /* 5476b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5477b9c1c267SJan Kara * truncate possible preallocated blocks. 5478b9c1c267SJan Kara */ 5479b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54802c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54812c98eb5eSTheodore Ts'o if (rc) 54822c98eb5eSTheodore Ts'o error = rc; 54832c98eb5eSTheodore Ts'o } 5484b9c1c267SJan Kara out_mmap_sem: 5485d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 54863da40c7bSJosef Bacik } 5487ac27a0ecSDave Kleikamp 54882c98eb5eSTheodore Ts'o if (!error) { 5489a642c2c0SJeff Layton if (inc_ivers) 5490a642c2c0SJeff Layton inode_inc_iversion(inode); 5491c1632a0fSChristian Brauner setattr_copy(idmap, inode, attr); 54921025774cSChristoph Hellwig mark_inode_dirty(inode); 54931025774cSChristoph Hellwig } 54941025774cSChristoph Hellwig 54951025774cSChristoph Hellwig /* 54961025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 54971025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 54981025774cSChristoph Hellwig */ 54993d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5500617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5501ac27a0ecSDave Kleikamp 55022c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 550313e83a49SChristian Brauner rc = posix_acl_chmod(idmap, dentry, inode->i_mode); 5504ac27a0ecSDave Kleikamp 5505ac27a0ecSDave Kleikamp err_out: 5506aa75f4d3SHarshad Shirwadkar if (error) 5507617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5508ac27a0ecSDave Kleikamp if (!error) 5509ac27a0ecSDave Kleikamp error = rc; 5510ac27a0ecSDave Kleikamp return error; 5511ac27a0ecSDave Kleikamp } 5512ac27a0ecSDave Kleikamp 55138434ef1dSEric Biggers u32 ext4_dio_alignment(struct inode *inode) 55148434ef1dSEric Biggers { 55158434ef1dSEric Biggers if (fsverity_active(inode)) 55168434ef1dSEric Biggers return 0; 55178434ef1dSEric Biggers if (ext4_should_journal_data(inode)) 55188434ef1dSEric Biggers return 0; 55198434ef1dSEric Biggers if (ext4_has_inline_data(inode)) 55208434ef1dSEric Biggers return 0; 55218434ef1dSEric Biggers if (IS_ENCRYPTED(inode)) { 55228434ef1dSEric Biggers if (!fscrypt_dio_supported(inode)) 55238434ef1dSEric Biggers return 0; 55248434ef1dSEric Biggers return i_blocksize(inode); 55258434ef1dSEric Biggers } 55268434ef1dSEric Biggers return 1; /* use the iomap defaults */ 55278434ef1dSEric Biggers } 55288434ef1dSEric Biggers 5529b74d24f7SChristian Brauner int ext4_getattr(struct mnt_idmap *idmap, const struct path *path, 5530549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags) 55313e3398a0SMingming Cao { 553299652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 553399652ea5SDavid Howells struct ext4_inode *raw_inode; 553499652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 553599652ea5SDavid Howells unsigned int flags; 55363e3398a0SMingming Cao 5537d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5538d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 553999652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 554099652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 554199652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 554299652ea5SDavid Howells } 554399652ea5SDavid Howells 55448434ef1dSEric Biggers /* 55458434ef1dSEric Biggers * Return the DIO alignment restrictions if requested. We only return 55468434ef1dSEric Biggers * this information when requested, since on encrypted files it might 55478434ef1dSEric Biggers * take a fair bit of work to get if the file wasn't opened recently. 55488434ef1dSEric Biggers */ 55498434ef1dSEric Biggers if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) { 55508434ef1dSEric Biggers u32 dio_align = ext4_dio_alignment(inode); 55518434ef1dSEric Biggers 55528434ef1dSEric Biggers stat->result_mask |= STATX_DIOALIGN; 55538434ef1dSEric Biggers if (dio_align == 1) { 55548434ef1dSEric Biggers struct block_device *bdev = inode->i_sb->s_bdev; 55558434ef1dSEric Biggers 55568434ef1dSEric Biggers /* iomap defaults */ 55578434ef1dSEric Biggers stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; 55588434ef1dSEric Biggers stat->dio_offset_align = bdev_logical_block_size(bdev); 55598434ef1dSEric Biggers } else { 55608434ef1dSEric Biggers stat->dio_mem_align = dio_align; 55618434ef1dSEric Biggers stat->dio_offset_align = dio_align; 55628434ef1dSEric Biggers } 55638434ef1dSEric Biggers } 55648434ef1dSEric Biggers 556599652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 556699652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 556799652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 556899652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 556999652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 557099652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 557199652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 557299652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 557399652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 557499652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 557599652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 55761f607195SEric Biggers if (flags & EXT4_VERITY_FL) 55771f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 557899652ea5SDavid Howells 55793209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 55803209f68bSDavid Howells STATX_ATTR_COMPRESSED | 55813209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 55823209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 55831f607195SEric Biggers STATX_ATTR_NODUMP | 55841f607195SEric Biggers STATX_ATTR_VERITY); 55853209f68bSDavid Howells 5586b74d24f7SChristian Brauner generic_fillattr(idmap, inode, stat); 558799652ea5SDavid Howells return 0; 558899652ea5SDavid Howells } 558999652ea5SDavid Howells 5590b74d24f7SChristian Brauner int ext4_file_getattr(struct mnt_idmap *idmap, 5591549c7297SChristian Brauner const struct path *path, struct kstat *stat, 559299652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 559399652ea5SDavid Howells { 559499652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 559599652ea5SDavid Howells u64 delalloc_blocks; 559699652ea5SDavid Howells 5597b74d24f7SChristian Brauner ext4_getattr(idmap, path, stat, request_mask, query_flags); 55983e3398a0SMingming Cao 55993e3398a0SMingming Cao /* 56009206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 56019206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 56029206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5603d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 56049206c561SAndreas Dilger */ 56059206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 56069206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 56079206c561SAndreas Dilger 56089206c561SAndreas Dilger /* 56093e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 56103e3398a0SMingming Cao * otherwise in the case of system crash before the real block 56113e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 56123e3398a0SMingming Cao * on-disk file blocks. 56133e3398a0SMingming Cao * We always keep i_blocks updated together with real 56143e3398a0SMingming Cao * allocation. But to not confuse with user, stat 56153e3398a0SMingming Cao * will return the blocks that include the delayed allocation 56163e3398a0SMingming Cao * blocks for this file. 56173e3398a0SMingming Cao */ 561896607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 561996607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 56208af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 56213e3398a0SMingming Cao return 0; 56223e3398a0SMingming Cao } 5623ac27a0ecSDave Kleikamp 5624fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5625fffb2739SJan Kara int pextents) 5626a02908f1SMingming Cao { 562712e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5628fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5629fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5630a02908f1SMingming Cao } 5631ac51d837STheodore Ts'o 5632a02908f1SMingming Cao /* 5633a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5634a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5635a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5636a02908f1SMingming Cao * 5637a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 56384907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5639a02908f1SMingming Cao * they could still across block group boundary. 5640a02908f1SMingming Cao * 5641a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5642a02908f1SMingming Cao */ 5643dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5644fffb2739SJan Kara int pextents) 5645a02908f1SMingming Cao { 56468df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56478df9675fSTheodore Ts'o int gdpblocks; 5648a02908f1SMingming Cao int idxblocks; 56497fc51f92SXU pengfei int ret; 5650a02908f1SMingming Cao 5651a02908f1SMingming Cao /* 5652fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5653fffb2739SJan Kara * to @pextents physical extents? 5654a02908f1SMingming Cao */ 5655fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5656a02908f1SMingming Cao 5657a02908f1SMingming Cao ret = idxblocks; 5658a02908f1SMingming Cao 5659a02908f1SMingming Cao /* 5660a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5661a02908f1SMingming Cao * to account 5662a02908f1SMingming Cao */ 5663fffb2739SJan Kara groups = idxblocks + pextents; 5664a02908f1SMingming Cao gdpblocks = groups; 56658df9675fSTheodore Ts'o if (groups > ngroups) 56668df9675fSTheodore Ts'o groups = ngroups; 5667a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5668a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5669a02908f1SMingming Cao 5670a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5671a02908f1SMingming Cao ret += groups + gdpblocks; 5672a02908f1SMingming Cao 5673a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5674a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5675ac27a0ecSDave Kleikamp 5676ac27a0ecSDave Kleikamp return ret; 5677ac27a0ecSDave Kleikamp } 5678ac27a0ecSDave Kleikamp 5679ac27a0ecSDave Kleikamp /* 568025985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5681f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5682f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5683a02908f1SMingming Cao * 5684525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5685a02908f1SMingming Cao * 5686525f4ed8SMingming Cao * We need to consider the worse case, when 5687a02908f1SMingming Cao * one new block per extent. 5688a02908f1SMingming Cao */ 5689a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5690a02908f1SMingming Cao { 5691a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5692a02908f1SMingming Cao int ret; 5693a02908f1SMingming Cao 5694fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5695a02908f1SMingming Cao 5696a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5697a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5698a02908f1SMingming Cao ret += bpp; 5699a02908f1SMingming Cao return ret; 5700a02908f1SMingming Cao } 5701f3bd1f3fSMingming Cao 5702f3bd1f3fSMingming Cao /* 5703f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5704f3bd1f3fSMingming Cao * 5705f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 570679e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5707f3bd1f3fSMingming Cao * 5708f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5709f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5710f3bd1f3fSMingming Cao */ 5711f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5712f3bd1f3fSMingming Cao { 5713f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5714f3bd1f3fSMingming Cao } 5715f3bd1f3fSMingming Cao 5716a02908f1SMingming Cao /* 5717617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5718ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5719ac27a0ecSDave Kleikamp */ 5720617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5721617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5722ac27a0ecSDave Kleikamp { 5723ac27a0ecSDave Kleikamp int err = 0; 5724ac27a0ecSDave Kleikamp 5725a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5726a6758309SVasily Averin put_bh(iloc->bh); 57270db1ff22STheodore Ts'o return -EIO; 5728a6758309SVasily Averin } 5729a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5730aa75f4d3SHarshad Shirwadkar 5731ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5732ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5733ac27a0ecSDave Kleikamp 5734dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5735830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5736ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5737ac27a0ecSDave Kleikamp return err; 5738ac27a0ecSDave Kleikamp } 5739ac27a0ecSDave Kleikamp 5740ac27a0ecSDave Kleikamp /* 5741ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5742ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5743ac27a0ecSDave Kleikamp */ 5744ac27a0ecSDave Kleikamp 5745ac27a0ecSDave Kleikamp int 5746617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5747617ba13bSMingming Cao struct ext4_iloc *iloc) 5748ac27a0ecSDave Kleikamp { 57490390131bSFrank Mayhar int err; 57500390131bSFrank Mayhar 57510db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 57520db1ff22STheodore Ts'o return -EIO; 57530db1ff22STheodore Ts'o 5754617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5755ac27a0ecSDave Kleikamp if (!err) { 5756ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5757188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, 5758188c299eSJan Kara iloc->bh, EXT4_JTR_NONE); 5759ac27a0ecSDave Kleikamp if (err) { 5760ac27a0ecSDave Kleikamp brelse(iloc->bh); 5761ac27a0ecSDave Kleikamp iloc->bh = NULL; 5762ac27a0ecSDave Kleikamp } 5763ac27a0ecSDave Kleikamp } 5764617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5765ac27a0ecSDave Kleikamp return err; 5766ac27a0ecSDave Kleikamp } 5767ac27a0ecSDave Kleikamp 5768c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5769c03b45b8SMiao Xie unsigned int new_extra_isize, 5770c03b45b8SMiao Xie struct ext4_iloc *iloc, 5771c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5772c03b45b8SMiao Xie { 5773c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5774c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 57754ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 57764ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5777c03b45b8SMiao Xie int error; 5778c03b45b8SMiao Xie 57794ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 57804ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 57814ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 57824ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 57834ea99936STheodore Ts'o ei->i_extra_isize, 57844ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 57854ea99936STheodore Ts'o return -EFSCORRUPTED; 57864ea99936STheodore Ts'o } 57874ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 57884ea99936STheodore Ts'o (new_extra_isize < 4) || 57894ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 57904ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 57914ea99936STheodore Ts'o 5792c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5793c03b45b8SMiao Xie 5794c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5795c03b45b8SMiao Xie 5796c03b45b8SMiao Xie /* No extended attributes present */ 5797c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5798c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5799c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5800c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5801c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5802c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5803c03b45b8SMiao Xie return 0; 5804c03b45b8SMiao Xie } 5805c03b45b8SMiao Xie 58068994d113SJan Kara /* 58078994d113SJan Kara * We may need to allocate external xattr block so we need quotas 58088994d113SJan Kara * initialized. Here we can be called with various locks held so we 58098994d113SJan Kara * cannot affort to initialize quotas ourselves. So just bail. 58108994d113SJan Kara */ 58118994d113SJan Kara if (dquot_initialize_needed(inode)) 58128994d113SJan Kara return -EAGAIN; 58138994d113SJan Kara 5814c03b45b8SMiao Xie /* try to expand with EAs present */ 5815c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5816c03b45b8SMiao Xie raw_inode, handle); 5817c03b45b8SMiao Xie if (error) { 5818c03b45b8SMiao Xie /* 5819c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5820c03b45b8SMiao Xie */ 5821c03b45b8SMiao Xie *no_expand = 1; 5822c03b45b8SMiao Xie } 5823c03b45b8SMiao Xie 5824c03b45b8SMiao Xie return error; 5825c03b45b8SMiao Xie } 5826c03b45b8SMiao Xie 5827ac27a0ecSDave Kleikamp /* 58286dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 58296dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 58306dd4ee7cSKalpak Shah */ 5831cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 58321d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 58331d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 58341d03ec98SAneesh Kumar K.V handle_t *handle) 58356dd4ee7cSKalpak Shah { 58363b10fdc6SMiao Xie int no_expand; 58373b10fdc6SMiao Xie int error; 58386dd4ee7cSKalpak Shah 5839cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5840cf0a5e81SMiao Xie return -EOVERFLOW; 5841cf0a5e81SMiao Xie 5842cf0a5e81SMiao Xie /* 5843cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5844cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5845cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5846cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5847cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5848cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5849cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5850cf0a5e81SMiao Xie */ 58516cb367c2SJan Kara if (ext4_journal_extend(handle, 585283448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5853cf0a5e81SMiao Xie return -ENOSPC; 58546dd4ee7cSKalpak Shah 58553b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5856cf0a5e81SMiao Xie return -EBUSY; 58573b10fdc6SMiao Xie 5858c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5859c03b45b8SMiao Xie handle, &no_expand); 58603b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5861c03b45b8SMiao Xie 5862c03b45b8SMiao Xie return error; 58636dd4ee7cSKalpak Shah } 58646dd4ee7cSKalpak Shah 5865c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5866c03b45b8SMiao Xie unsigned int new_extra_isize, 5867c03b45b8SMiao Xie struct ext4_iloc *iloc) 5868c03b45b8SMiao Xie { 5869c03b45b8SMiao Xie handle_t *handle; 5870c03b45b8SMiao Xie int no_expand; 5871c03b45b8SMiao Xie int error, rc; 5872c03b45b8SMiao Xie 5873c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5874c03b45b8SMiao Xie brelse(iloc->bh); 5875c03b45b8SMiao Xie return -EOVERFLOW; 5876c03b45b8SMiao Xie } 5877c03b45b8SMiao Xie 5878c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5879c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5880c03b45b8SMiao Xie if (IS_ERR(handle)) { 5881c03b45b8SMiao Xie error = PTR_ERR(handle); 5882c03b45b8SMiao Xie brelse(iloc->bh); 5883c03b45b8SMiao Xie return error; 5884c03b45b8SMiao Xie } 5885c03b45b8SMiao Xie 5886c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5887c03b45b8SMiao Xie 5888ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5889188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, 5890188c299eSJan Kara EXT4_JTR_NONE); 58913b10fdc6SMiao Xie if (error) { 5892c03b45b8SMiao Xie brelse(iloc->bh); 58937f420d64SDan Carpenter goto out_unlock; 58943b10fdc6SMiao Xie } 5895cf0a5e81SMiao Xie 5896c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5897c03b45b8SMiao Xie handle, &no_expand); 5898c03b45b8SMiao Xie 5899c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5900c03b45b8SMiao Xie if (!error) 5901c03b45b8SMiao Xie error = rc; 5902c03b45b8SMiao Xie 59037f420d64SDan Carpenter out_unlock: 5904c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5905c03b45b8SMiao Xie ext4_journal_stop(handle); 59063b10fdc6SMiao Xie return error; 59076dd4ee7cSKalpak Shah } 59086dd4ee7cSKalpak Shah 59096dd4ee7cSKalpak Shah /* 5910ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5911ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5912ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5913ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5914ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5915ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5916ac27a0ecSDave Kleikamp * 5917ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5918ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5919ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5920ac27a0ecSDave Kleikamp * we start and wait on commits. 5921ac27a0ecSDave Kleikamp */ 59224209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 59234209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5924ac27a0ecSDave Kleikamp { 5925617ba13bSMingming Cao struct ext4_iloc iloc; 59266dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5927cf0a5e81SMiao Xie int err; 5928ac27a0ecSDave Kleikamp 5929ac27a0ecSDave Kleikamp might_sleep(); 59307ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5931617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 59325e1021f2SEryu Guan if (err) 59334209ae12SHarshad Shirwadkar goto out; 5934cf0a5e81SMiao Xie 5935cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5936cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 59376dd4ee7cSKalpak Shah iloc, handle); 5938cf0a5e81SMiao Xie 59394209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 59404209ae12SHarshad Shirwadkar out: 59414209ae12SHarshad Shirwadkar if (unlikely(err)) 59424209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 59434209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 59444209ae12SHarshad Shirwadkar return err; 5945ac27a0ecSDave Kleikamp } 5946ac27a0ecSDave Kleikamp 5947ac27a0ecSDave Kleikamp /* 5948617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5949ac27a0ecSDave Kleikamp * 5950ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5951ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5952ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5953ac27a0ecSDave Kleikamp * 59545dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5955ac27a0ecSDave Kleikamp * are allocated to the file. 5956ac27a0ecSDave Kleikamp * 5957ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5958ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5959ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5960ac27a0ecSDave Kleikamp */ 5961aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5962ac27a0ecSDave Kleikamp { 5963ac27a0ecSDave Kleikamp handle_t *handle; 5964ac27a0ecSDave Kleikamp 59659924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5966ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5967ac27a0ecSDave Kleikamp return; 5968e2728c56SEric Biggers ext4_mark_inode_dirty(handle, inode); 5969e2728c56SEric Biggers ext4_journal_stop(handle); 5970ac27a0ecSDave Kleikamp } 5971ac27a0ecSDave Kleikamp 5972617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5973ac27a0ecSDave Kleikamp { 5974ac27a0ecSDave Kleikamp journal_t *journal; 5975ac27a0ecSDave Kleikamp handle_t *handle; 5976ac27a0ecSDave Kleikamp int err; 5977c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5978ac27a0ecSDave Kleikamp 5979ac27a0ecSDave Kleikamp /* 5980ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5981ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5982ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5983ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5984ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5985ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5986ac27a0ecSDave Kleikamp * nobody is changing anything. 5987ac27a0ecSDave Kleikamp */ 5988ac27a0ecSDave Kleikamp 5989617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59900390131bSFrank Mayhar if (!journal) 59910390131bSFrank Mayhar return 0; 5992d699594dSDave Hansen if (is_journal_aborted(journal)) 5993ac27a0ecSDave Kleikamp return -EROFS; 5994ac27a0ecSDave Kleikamp 599517335dccSDmitry Monakhov /* Wait for all existing dio workers */ 599617335dccSDmitry Monakhov inode_dio_wait(inode); 599717335dccSDmitry Monakhov 59984c546592SDaeho Jeong /* 59994c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 60004c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 60014c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 60024c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 60034c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 60044c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 60054c546592SDaeho Jeong */ 60064c546592SDaeho Jeong if (val) { 6007d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 60084c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 60094c546592SDaeho Jeong if (err < 0) { 6010d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 60114c546592SDaeho Jeong return err; 60124c546592SDaeho Jeong } 60134c546592SDaeho Jeong } 60144c546592SDaeho Jeong 6015bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 6016dab291afSMingming Cao jbd2_journal_lock_updates(journal); 6017ac27a0ecSDave Kleikamp 6018ac27a0ecSDave Kleikamp /* 6019ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 6020ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 6021ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 6022ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 6023ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 6024ac27a0ecSDave Kleikamp */ 6025ac27a0ecSDave Kleikamp 6026ac27a0ecSDave Kleikamp if (val) 602712e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 60285872ddaaSYongqiang Yang else { 602901d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 60304f879ca6SJan Kara if (err < 0) { 60314f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 6032bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 60334f879ca6SJan Kara return err; 60344f879ca6SJan Kara } 603512e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 60365872ddaaSYongqiang Yang } 6037617ba13bSMingming Cao ext4_set_aops(inode); 6038ac27a0ecSDave Kleikamp 6039dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 6040bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 6041c8585c6fSDaeho Jeong 60424c546592SDaeho Jeong if (val) 6043d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 6044ac27a0ecSDave Kleikamp 6045ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6046ac27a0ecSDave Kleikamp 60479924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 6048ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6049ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6050ac27a0ecSDave Kleikamp 6051aa75f4d3SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, 6052e85c81baSXin Yin EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, handle); 6053617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60540390131bSFrank Mayhar ext4_handle_sync(handle); 6055617ba13bSMingming Cao ext4_journal_stop(handle); 6056617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6057ac27a0ecSDave Kleikamp 6058ac27a0ecSDave Kleikamp return err; 6059ac27a0ecSDave Kleikamp } 60602e9ee850SAneesh Kumar K.V 6061188c299eSJan Kara static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, 6062188c299eSJan Kara struct buffer_head *bh) 60632e9ee850SAneesh Kumar K.V { 60642e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60652e9ee850SAneesh Kumar K.V } 60662e9ee850SAneesh Kumar K.V 6067401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 60682e9ee850SAneesh Kumar K.V { 606911bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 6070c2ec175cSNick Piggin struct page *page = vmf->page; 60712e9ee850SAneesh Kumar K.V loff_t size; 60722e9ee850SAneesh Kumar K.V unsigned long len; 6073401b25aaSSouptick Joarder int err; 6074401b25aaSSouptick Joarder vm_fault_t ret; 60752e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6076496ad9aaSAl Viro struct inode *inode = file_inode(file); 60772e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60789ea7df53SJan Kara handle_t *handle; 60799ea7df53SJan Kara get_block_t *get_block; 60809ea7df53SJan Kara int retries = 0; 60812e9ee850SAneesh Kumar K.V 608202b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 608302b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 608402b016caSTheodore Ts'o 60858e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6086041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6087ea3d7209SJan Kara 6088d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping); 60897b4cc978SEric Biggers 6090401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6091401b25aaSSouptick Joarder if (err) 60927b4cc978SEric Biggers goto out_ret; 60937b4cc978SEric Biggers 609464a9f144SMauricio Faria de Oliveira /* 609564a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 609664a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 609764a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 609864a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 609964a9f144SMauricio Faria de Oliveira */ 610064a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 610164a9f144SMauricio Faria de Oliveira goto retry_alloc; 610264a9f144SMauricio Faria de Oliveira 61039ea7df53SJan Kara /* Delalloc case is easy... */ 61049ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 61059ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 61069ea7df53SJan Kara do { 6107401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 61089ea7df53SJan Kara ext4_da_get_block_prep); 6109401b25aaSSouptick Joarder } while (err == -ENOSPC && 61109ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 61119ea7df53SJan Kara goto out_ret; 61122e9ee850SAneesh Kumar K.V } 61130e499890SDarrick J. Wong 61140e499890SDarrick J. Wong lock_page(page); 61159ea7df53SJan Kara size = i_size_read(inode); 61169ea7df53SJan Kara /* Page got truncated from under us? */ 61179ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 61189ea7df53SJan Kara unlock_page(page); 61199ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 61209ea7df53SJan Kara goto out; 61210e499890SDarrick J. Wong } 61222e9ee850SAneesh Kumar K.V 612309cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 612409cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 61252e9ee850SAneesh Kumar K.V else 612609cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6127a827eaffSAneesh Kumar K.V /* 61289ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 61299ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 613064a9f144SMauricio Faria de Oliveira * 613164a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 613264a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6133a827eaffSAneesh Kumar K.V */ 61342e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6135188c299eSJan Kara if (!ext4_walk_page_buffers(NULL, inode, page_buffers(page), 6136f19d5870STao Ma 0, len, NULL, 6137a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 61389ea7df53SJan Kara /* Wait so that we don't change page under IO */ 61391d1d1a76SDarrick J. Wong wait_for_stable_page(page); 61409ea7df53SJan Kara ret = VM_FAULT_LOCKED; 61419ea7df53SJan Kara goto out; 61422e9ee850SAneesh Kumar K.V } 6143a827eaffSAneesh Kumar K.V } 6144a827eaffSAneesh Kumar K.V unlock_page(page); 61459ea7df53SJan Kara /* OK, we need to fill the hole... */ 61469ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6147705965bdSJan Kara get_block = ext4_get_block_unwritten; 61489ea7df53SJan Kara else 61499ea7df53SJan Kara get_block = ext4_get_block; 61509ea7df53SJan Kara retry_alloc: 61519924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 61529924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 61539ea7df53SJan Kara if (IS_ERR(handle)) { 6154c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 61559ea7df53SJan Kara goto out; 61569ea7df53SJan Kara } 615764a9f144SMauricio Faria de Oliveira /* 615864a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 615964a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 616064a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 616164a9f144SMauricio Faria de Oliveira */ 616264a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6163401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 616464a9f144SMauricio Faria de Oliveira } else { 616564a9f144SMauricio Faria de Oliveira lock_page(page); 616664a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 616764a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 616864a9f144SMauricio Faria de Oliveira if (page->mapping != mapping || page_offset(page) > size) { 616964a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6170afb585a9SMauricio Faria de Oliveira goto out_error; 617164a9f144SMauricio Faria de Oliveira } 617264a9f144SMauricio Faria de Oliveira 617364a9f144SMauricio Faria de Oliveira if (page->index == size >> PAGE_SHIFT) 617464a9f144SMauricio Faria de Oliveira len = size & ~PAGE_MASK; 617564a9f144SMauricio Faria de Oliveira else 617664a9f144SMauricio Faria de Oliveira len = PAGE_SIZE; 617764a9f144SMauricio Faria de Oliveira 617864a9f144SMauricio Faria de Oliveira err = __block_write_begin(page, 0, len, ext4_get_block); 617964a9f144SMauricio Faria de Oliveira if (!err) { 61809ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 61813f079114SJan Kara if (ext4_journal_page_buffers(handle, page, len)) 6182afb585a9SMauricio Faria de Oliveira goto out_error; 618364a9f144SMauricio Faria de Oliveira } else { 618464a9f144SMauricio Faria de Oliveira unlock_page(page); 618564a9f144SMauricio Faria de Oliveira } 61869ea7df53SJan Kara } 61879ea7df53SJan Kara ext4_journal_stop(handle); 6188401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 61899ea7df53SJan Kara goto retry_alloc; 61909ea7df53SJan Kara out_ret: 6191401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 61929ea7df53SJan Kara out: 6193d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping); 61948e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 61952e9ee850SAneesh Kumar K.V return ret; 6196afb585a9SMauricio Faria de Oliveira out_error: 6197afb585a9SMauricio Faria de Oliveira unlock_page(page); 6198afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6199afb585a9SMauricio Faria de Oliveira goto out; 62002e9ee850SAneesh Kumar K.V } 6201