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 139cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 140dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 141dec214d0STahsin Erdogan int pextents); 14264769240SAlex Tomas 143ac27a0ecSDave Kleikamp /* 144ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 145407cd7fbSTahsin Erdogan * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 146ac27a0ecSDave Kleikamp */ 147f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode) 148ac27a0ecSDave Kleikamp { 149fc82228aSAndi Kleen if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 150fc82228aSAndi Kleen int ea_blocks = EXT4_I(inode)->i_file_acl ? 151fc82228aSAndi Kleen EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 152fc82228aSAndi Kleen 153fc82228aSAndi Kleen if (ext4_has_inline_data(inode)) 154fc82228aSAndi Kleen return 0; 155fc82228aSAndi Kleen 156fc82228aSAndi Kleen return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 157fc82228aSAndi Kleen } 158407cd7fbSTahsin Erdogan return S_ISLNK(inode->i_mode) && inode->i_size && 159407cd7fbSTahsin Erdogan (inode->i_size < EXT4_N_BLOCKS * 4); 160ac27a0ecSDave Kleikamp } 161ac27a0ecSDave Kleikamp 162ac27a0ecSDave Kleikamp /* 163ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 164ac27a0ecSDave Kleikamp */ 1650930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 166ac27a0ecSDave Kleikamp { 167ac27a0ecSDave Kleikamp handle_t *handle; 168bc965ab3STheodore Ts'o int err; 16965db869cSJan Kara /* 17065db869cSJan Kara * Credits for final inode cleanup and freeing: 17165db869cSJan Kara * sb + inode (ext4_orphan_del()), block bitmap, group descriptor 17265db869cSJan Kara * (xattr block freeing), bitmap, group descriptor (inode freeing) 17365db869cSJan Kara */ 17465db869cSJan Kara int extra_credits = 6; 1750421a189STahsin Erdogan struct ext4_xattr_inode_array *ea_inode_array = NULL; 17646e294efSJan Kara bool freeze_protected = false; 177ac27a0ecSDave Kleikamp 1787ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1792581fdc8SJiaying Zhang 1806bc0d63dSJan Kara if (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL) 1816bc0d63dSJan Kara ext4_evict_ea_inode(inode); 1820930fcc1SAl Viro if (inode->i_nlink) { 1832d859db3SJan Kara /* 1842d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1852d859db3SJan Kara * journal. So although mm thinks everything is clean and 1862d859db3SJan Kara * ready for reaping the inode might still have some pages to 1872d859db3SJan Kara * write in the running transaction or waiting to be 188ccd16945SMatthew Wilcox (Oracle) * checkpointed. Thus calling jbd2_journal_invalidate_folio() 1892d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1902d859db3SJan Kara * cause data loss. Also even if we did not discard these 1912d859db3SJan Kara * buffers, we would have no way to find them after the inode 1922d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1932d859db3SJan Kara * read them before the transaction is checkpointed. So be 1942d859db3SJan Kara * careful and force everything to disk here... We use 1952d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1962d859db3SJan Kara * containing inode's data. 1972d859db3SJan Kara * 1982d859db3SJan Kara * Note that directories do not have this problem because they 1992d859db3SJan Kara * don't use page cache. 2002d859db3SJan Kara */ 2016a7fd522SVegard Nossum if (inode->i_ino != EXT4_JOURNAL_INO && 2026a7fd522SVegard Nossum ext4_should_journal_data(inode) && 2036493792dSZhang Yi S_ISREG(inode->i_mode) && inode->i_data.nrpages) { 2042d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2052d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2062d859db3SJan Kara 207d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2082d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2092d859db3SJan Kara } 21091b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 2115dc23bddSJan Kara 2120930fcc1SAl Viro goto no_delete; 2130930fcc1SAl Viro } 2140930fcc1SAl Viro 215e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 216e2bfb088STheodore Ts'o goto no_delete; 217871a2931SChristoph Hellwig dquot_initialize(inode); 218907f4554SChristoph Hellwig 219678aaf48SJan Kara if (ext4_should_order_data(inode)) 220678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 22191b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 222ac27a0ecSDave Kleikamp 2238e8ad8a5SJan Kara /* 224ceff86fdSJan Kara * For inodes with journalled data, transaction commit could have 225bc12ac98SZhang Yi * dirtied the inode. And for inodes with dioread_nolock, unwritten 226bc12ac98SZhang Yi * extents converting worker could merge extents and also have dirtied 227bc12ac98SZhang Yi * the inode. Flush worker is ignoring it because of I_FREEING flag but 228bc12ac98SZhang Yi * we still need to remove the inode from the writeback lists. 229ceff86fdSJan Kara */ 230bc12ac98SZhang Yi if (!list_empty_careful(&inode->i_io_list)) 231ceff86fdSJan Kara inode_io_list_del(inode); 232ceff86fdSJan Kara 233ceff86fdSJan Kara /* 2348e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 23546e294efSJan Kara * protection against it. When we are in a running transaction though, 23646e294efSJan Kara * we are already protected against freezing and we cannot grab further 23746e294efSJan Kara * protection due to lock ordering constraints. 2388e8ad8a5SJan Kara */ 23946e294efSJan Kara if (!ext4_journal_current_handle()) { 2408e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 24146e294efSJan Kara freeze_protected = true; 24246e294efSJan Kara } 243e50e5129SAndreas Dilger 24430a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 24530a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 24630a7eb97STahsin Erdogan 24765db869cSJan Kara /* 24865db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 24965db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 25065db869cSJan Kara */ 25130a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 25265db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 253ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 254bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 255ac27a0ecSDave Kleikamp /* 256ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 257ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 258ac27a0ecSDave Kleikamp * cleaned up. 259ac27a0ecSDave Kleikamp */ 260617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 26146e294efSJan Kara if (freeze_protected) 2628e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 263ac27a0ecSDave Kleikamp goto no_delete; 264ac27a0ecSDave Kleikamp } 26530a7eb97STahsin Erdogan 266ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2670390131bSFrank Mayhar ext4_handle_sync(handle); 268407cd7fbSTahsin Erdogan 269407cd7fbSTahsin Erdogan /* 270407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 271407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 272407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 273407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 274407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 275407cd7fbSTahsin Erdogan */ 276407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 277407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 278ac27a0ecSDave Kleikamp inode->i_size = 0; 279bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 280bc965ab3STheodore Ts'o if (err) { 28112062dddSEric Sandeen ext4_warning(inode->i_sb, 282bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 283bc965ab3STheodore Ts'o goto stop_handle; 284bc965ab3STheodore Ts'o } 2852c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2862c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2872c98eb5eSTheodore Ts'o if (err) { 28854d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err, 2892c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2902c98eb5eSTheodore Ts'o inode->i_ino, err); 2912c98eb5eSTheodore Ts'o goto stop_handle; 2922c98eb5eSTheodore Ts'o } 2932c98eb5eSTheodore Ts'o } 294bc965ab3STheodore Ts'o 29530a7eb97STahsin Erdogan /* Remove xattr references. */ 29630a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 29730a7eb97STahsin Erdogan extra_credits); 29830a7eb97STahsin Erdogan if (err) { 29930a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 300bc965ab3STheodore Ts'o stop_handle: 301bc965ab3STheodore Ts'o ext4_journal_stop(handle); 30245388219STheodore Ts'o ext4_orphan_del(NULL, inode); 30346e294efSJan Kara if (freeze_protected) 3048e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 30530a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 306bc965ab3STheodore Ts'o goto no_delete; 307bc965ab3STheodore Ts'o } 308bc965ab3STheodore Ts'o 309ac27a0ecSDave Kleikamp /* 310617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 311ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 312617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 313ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 314617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 315ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 316ac27a0ecSDave Kleikamp */ 317617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3185ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 319ac27a0ecSDave Kleikamp 320ac27a0ecSDave Kleikamp /* 321ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 322ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 323ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 324ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 325ac27a0ecSDave Kleikamp * fails. 326ac27a0ecSDave Kleikamp */ 327617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 328ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3290930fcc1SAl Viro ext4_clear_inode(inode); 330ac27a0ecSDave Kleikamp else 331617ba13bSMingming Cao ext4_free_inode(handle, inode); 332617ba13bSMingming Cao ext4_journal_stop(handle); 33346e294efSJan Kara if (freeze_protected) 3348e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3350421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 336ac27a0ecSDave Kleikamp return; 337ac27a0ecSDave Kleikamp no_delete: 338318cdc82SZhang Yi /* 339318cdc82SZhang Yi * Check out some where else accidentally dirty the evicting inode, 340318cdc82SZhang Yi * which may probably cause inode use-after-free issues later. 341318cdc82SZhang Yi */ 342318cdc82SZhang Yi WARN_ON_ONCE(!list_empty_careful(&inode->i_io_list)); 343318cdc82SZhang Yi 344b21ebf14SHarshad Shirwadkar if (!list_empty(&EXT4_I(inode)->i_fc_list)) 345e85c81baSXin Yin ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM, NULL); 3460930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 347ac27a0ecSDave Kleikamp } 348ac27a0ecSDave Kleikamp 349a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 350a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 35160e58e0fSMingming Cao { 352a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 35360e58e0fSMingming Cao } 354a9e7f447SDmitry Monakhov #endif 3559d0be502STheodore Ts'o 35612219aeaSAneesh Kumar K.V /* 3570637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3580637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3590637c6f4STheodore Ts'o */ 3605f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3615f634d06SAneesh Kumar K.V int used, int quota_claim) 36212219aeaSAneesh Kumar K.V { 36312219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3640637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 36512219aeaSAneesh Kumar K.V 3660637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 367d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3680637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3698de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3701084f252STheodore Ts'o "with only %d reserved data blocks", 3710637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3720637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3730637c6f4STheodore Ts'o WARN_ON(1); 3740637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3756bc6e63fSAneesh Kumar K.V } 37612219aeaSAneesh Kumar K.V 3770637c6f4STheodore Ts'o /* Update per-inode reservations */ 3780637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 37971d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3800637c6f4STheodore Ts'o 381f9505c72Schenyichong spin_unlock(&ei->i_block_reservation_lock); 38260e58e0fSMingming Cao 38372b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38472b8ab9dSEric Sandeen if (quota_claim) 3857b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38672b8ab9dSEric Sandeen else { 3875f634d06SAneesh Kumar K.V /* 3885f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3895f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39072b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3915f634d06SAneesh Kumar K.V */ 3927b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3935f634d06SAneesh Kumar K.V } 394d6014301SAneesh Kumar K.V 395d6014301SAneesh Kumar K.V /* 396d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 397d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 398d6014301SAneesh Kumar K.V * inode's preallocations. 399d6014301SAneesh Kumar K.V */ 4000637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 40182dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 40227bc446eSbrookxu ext4_discard_preallocations(inode, 0); 40312219aeaSAneesh Kumar K.V } 40412219aeaSAneesh Kumar K.V 405e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 406c398eda0STheodore Ts'o unsigned int line, 40724676da4STheodore Ts'o struct ext4_map_blocks *map) 4086fd058f7STheodore Ts'o { 409345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 410345c0dbfSTheodore Ts'o (inode->i_ino == 411345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 412345c0dbfSTheodore Ts'o return 0; 413ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 414c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 415bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 41624676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 417bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4186a797d27SDarrick J. Wong return -EFSCORRUPTED; 4196fd058f7STheodore Ts'o } 4206fd058f7STheodore Ts'o return 0; 4216fd058f7STheodore Ts'o } 4226fd058f7STheodore Ts'o 42353085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 42453085facSJan Kara ext4_lblk_t len) 42553085facSJan Kara { 42653085facSJan Kara int ret; 42753085facSJan Kara 42833b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 429a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 43053085facSJan Kara 43153085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 43253085facSJan Kara if (ret > 0) 43353085facSJan Kara ret = 0; 43453085facSJan Kara 43553085facSJan Kara return ret; 43653085facSJan Kara } 43753085facSJan Kara 438e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 439c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 440e29136f8STheodore Ts'o 441921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 442921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 443921f266bSDmitry Monakhov struct inode *inode, 444921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 445921f266bSDmitry Monakhov struct ext4_map_blocks *map, 446921f266bSDmitry Monakhov int flags) 447921f266bSDmitry Monakhov { 448921f266bSDmitry Monakhov int retval; 449921f266bSDmitry Monakhov 450921f266bSDmitry Monakhov map->m_flags = 0; 451921f266bSDmitry Monakhov /* 452921f266bSDmitry Monakhov * There is a race window that the result is not the same. 453921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 454921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 455921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 456921f266bSDmitry Monakhov * could be converted. 457921f266bSDmitry Monakhov */ 458c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 459921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4609e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 461921f266bSDmitry Monakhov } else { 4629e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 463921f266bSDmitry Monakhov } 464921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 465921f266bSDmitry Monakhov 466921f266bSDmitry Monakhov /* 467921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 468921f266bSDmitry Monakhov * tree. So the m_len might not equal. 469921f266bSDmitry Monakhov */ 470921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 471921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 472921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 473bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 474921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 475921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 476921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 477921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 478921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 479921f266bSDmitry Monakhov retval, flags); 480921f266bSDmitry Monakhov } 481921f266bSDmitry Monakhov } 482921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 483921f266bSDmitry Monakhov 48455138e0bSTheodore Ts'o /* 485e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4862b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 487f5ab0d1fSMingming Cao * 488f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 489f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 490f5ab0d1fSMingming Cao * mapped. 491f5ab0d1fSMingming Cao * 492e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 493e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 494f5ab0d1fSMingming Cao * based files 495f5ab0d1fSMingming Cao * 496facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 497facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 498facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 499f5ab0d1fSMingming Cao * 500f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 501facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 502facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 503f5ab0d1fSMingming Cao * 504f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 505f5ab0d1fSMingming Cao */ 506e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 507e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5080e855ac8SAneesh Kumar K.V { 509d100eef2SZheng Liu struct extent_status es; 5100e855ac8SAneesh Kumar K.V int retval; 511b8a86845SLukas Czerner int ret = 0; 512921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 513921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 514921f266bSDmitry Monakhov 515921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 516921f266bSDmitry Monakhov #endif 517f5ab0d1fSMingming Cao 518e35fd660STheodore Ts'o map->m_flags = 0; 51970aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 52070aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 521d100eef2SZheng Liu 522e861b5e9STheodore Ts'o /* 523e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 524e861b5e9STheodore Ts'o */ 525e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 526e861b5e9STheodore Ts'o map->m_len = INT_MAX; 527e861b5e9STheodore Ts'o 5284adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5294adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5306a797d27SDarrick J. Wong return -EFSCORRUPTED; 5314adb6ab3SKazuya Mio 532d100eef2SZheng Liu /* Lookup extent status tree firstly */ 5338016e29fSHarshad Shirwadkar if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 5348016e29fSHarshad Shirwadkar ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 535d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 536d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 537d100eef2SZheng Liu map->m_lblk - es.es_lblk; 538d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 539d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 540d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 541d100eef2SZheng Liu if (retval > map->m_len) 542d100eef2SZheng Liu retval = map->m_len; 543d100eef2SZheng Liu map->m_len = retval; 544d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 545facab4d9SJan Kara map->m_pblk = 0; 546facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 547facab4d9SJan Kara if (retval > map->m_len) 548facab4d9SJan Kara retval = map->m_len; 549facab4d9SJan Kara map->m_len = retval; 550d100eef2SZheng Liu retval = 0; 551d100eef2SZheng Liu } else { 5521e83bc81SArnd Bergmann BUG(); 553d100eef2SZheng Liu } 5549558cf14SZhang Yi 5559558cf14SZhang Yi if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) 5569558cf14SZhang Yi return retval; 557921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 558921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 559921f266bSDmitry Monakhov &orig_map, flags); 560921f266bSDmitry Monakhov #endif 561d100eef2SZheng Liu goto found; 562d100eef2SZheng Liu } 5639558cf14SZhang Yi /* 5649558cf14SZhang Yi * In the query cache no-wait mode, nothing we can do more if we 5659558cf14SZhang Yi * cannot find extent in the cache. 5669558cf14SZhang Yi */ 5679558cf14SZhang Yi if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT) 5689558cf14SZhang Yi return 0; 569d100eef2SZheng Liu 5704df3d265SAneesh Kumar K.V /* 571b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 572b920c755STheodore Ts'o * file system block. 5734df3d265SAneesh Kumar K.V */ 574c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 57512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5769e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5774df3d265SAneesh Kumar K.V } else { 5789e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5790e855ac8SAneesh Kumar K.V } 580f7fec032SZheng Liu if (retval > 0) { 5813be78c73STheodore Ts'o unsigned int status; 582f7fec032SZheng Liu 58344fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 58444fb851dSZheng Liu ext4_warning(inode->i_sb, 58544fb851dSZheng Liu "ES len assertion failed for inode " 58644fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 58744fb851dSZheng Liu inode->i_ino, retval, map->m_len); 58844fb851dSZheng Liu WARN_ON(1); 589921f266bSDmitry Monakhov } 590921f266bSDmitry Monakhov 591f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 592f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 593f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 594d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 595ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 596f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 597f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 598f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 599f7fec032SZheng Liu map->m_len, map->m_pblk, status); 600f7fec032SZheng Liu if (ret < 0) 601f7fec032SZheng Liu retval = ret; 602f7fec032SZheng Liu } 6034df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 604f5ab0d1fSMingming Cao 605d100eef2SZheng Liu found: 606e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 607b8a86845SLukas Czerner ret = check_block_validity(inode, map); 6086fd058f7STheodore Ts'o if (ret != 0) 6096fd058f7STheodore Ts'o return ret; 6106fd058f7STheodore Ts'o } 6116fd058f7STheodore Ts'o 612f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 613c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6144df3d265SAneesh Kumar K.V return retval; 6154df3d265SAneesh Kumar K.V 6164df3d265SAneesh Kumar K.V /* 617f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 618f5ab0d1fSMingming Cao * 619f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 620df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 621f5ab0d1fSMingming Cao * with buffer head unmapped. 622f5ab0d1fSMingming Cao */ 623e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 624b8a86845SLukas Czerner /* 625b8a86845SLukas Czerner * If we need to convert extent to unwritten 626b8a86845SLukas Czerner * we continue and do the actual work in 627b8a86845SLukas Czerner * ext4_ext_map_blocks() 628b8a86845SLukas Czerner */ 629b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 630f5ab0d1fSMingming Cao return retval; 631f5ab0d1fSMingming Cao 632f5ab0d1fSMingming Cao /* 633a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 634a25a4e1aSZheng Liu * it will be set again. 6352a8964d6SAneesh Kumar K.V */ 636a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6372a8964d6SAneesh Kumar K.V 6382a8964d6SAneesh Kumar K.V /* 639556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 640f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 641d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 642f5ab0d1fSMingming Cao * with create == 1 flag. 6434df3d265SAneesh Kumar K.V */ 644c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 645d2a17637SMingming Cao 646d2a17637SMingming Cao /* 6474df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6484df3d265SAneesh Kumar K.V * could have changed the inode type in between 6494df3d265SAneesh Kumar K.V */ 65012e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 651e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6520e855ac8SAneesh Kumar K.V } else { 653e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 654267e4db9SAneesh Kumar K.V 655e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 656267e4db9SAneesh Kumar K.V /* 657267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 658267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 659267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 660267e4db9SAneesh Kumar K.V */ 66119f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 662267e4db9SAneesh Kumar K.V } 6632ac3b6e0STheodore Ts'o 664d2a17637SMingming Cao /* 6652ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6665f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6675f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6685f634d06SAneesh Kumar K.V * reserve space here. 669d2a17637SMingming Cao */ 6705f634d06SAneesh Kumar K.V if ((retval > 0) && 6711296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6725f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6735f634d06SAneesh Kumar K.V } 674d2a17637SMingming Cao 675f7fec032SZheng Liu if (retval > 0) { 6763be78c73STheodore Ts'o unsigned int status; 677f7fec032SZheng Liu 67844fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 67944fb851dSZheng Liu ext4_warning(inode->i_sb, 68044fb851dSZheng Liu "ES len assertion failed for inode " 68144fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 68244fb851dSZheng Liu inode->i_ino, retval, map->m_len); 68344fb851dSZheng Liu WARN_ON(1); 684921f266bSDmitry Monakhov } 685921f266bSDmitry Monakhov 686adb23551SZheng Liu /* 687c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 688c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6899b623df6SJan Kara * use them before they are really zeroed. We also have to 6909b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6919b623df6SJan Kara * overwrite zeros with stale data from block device. 692c86d8db3SJan Kara */ 693c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 694c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 695c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 696c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 697c86d8db3SJan Kara map->m_pblk, map->m_len); 698c86d8db3SJan Kara if (ret) { 699c86d8db3SJan Kara retval = ret; 700c86d8db3SJan Kara goto out_sem; 701c86d8db3SJan Kara } 702c86d8db3SJan Kara } 703c86d8db3SJan Kara 704c86d8db3SJan Kara /* 705adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 706adb23551SZheng Liu * extent status tree. 707adb23551SZheng Liu */ 708adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 709bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 710adb23551SZheng Liu if (ext4_es_is_written(&es)) 711c86d8db3SJan Kara goto out_sem; 712adb23551SZheng Liu } 713f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 714f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 715f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 716d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 717ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 718f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 719f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 720f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 721f7fec032SZheng Liu map->m_pblk, status); 722c86d8db3SJan Kara if (ret < 0) { 72351865fdaSZheng Liu retval = ret; 724c86d8db3SJan Kara goto out_sem; 725c86d8db3SJan Kara } 72651865fdaSZheng Liu } 7275356f261SAditya Kali 728c86d8db3SJan Kara out_sem: 7290e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 730e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 731b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7326fd058f7STheodore Ts'o if (ret != 0) 7336fd058f7STheodore Ts'o return ret; 73406bd3c36SJan Kara 73506bd3c36SJan Kara /* 73606bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 73706bd3c36SJan Kara * visible after transaction commit must be on transaction's 73806bd3c36SJan Kara * ordered data list. 73906bd3c36SJan Kara */ 74006bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 74106bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 74206bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 74302749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 74406bd3c36SJan Kara ext4_should_order_data(inode)) { 74573131fbbSRoss Zwisler loff_t start_byte = 74673131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 74773131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 74873131fbbSRoss Zwisler 749ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 75073131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 75173131fbbSRoss Zwisler start_byte, length); 752ee0876bcSJan Kara else 75373131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 75473131fbbSRoss Zwisler start_byte, length); 75506bd3c36SJan Kara if (ret) 75606bd3c36SJan Kara return ret; 75706bd3c36SJan Kara } 7585e4d0ebaSXin Yin } 7595e4d0ebaSXin Yin if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || 7605e4d0ebaSXin Yin map->m_flags & EXT4_MAP_MAPPED)) 761a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 762aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 763ec8c60beSRitesh Harjani if (retval < 0) 76470aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7650e855ac8SAneesh Kumar K.V return retval; 7660e855ac8SAneesh Kumar K.V } 7670e855ac8SAneesh Kumar K.V 768ed8ad838SJan Kara /* 769ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 770ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 771ed8ad838SJan Kara */ 772ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 773ed8ad838SJan Kara { 774ed8ad838SJan Kara unsigned long old_state; 775ed8ad838SJan Kara unsigned long new_state; 776ed8ad838SJan Kara 777ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 778ed8ad838SJan Kara 779ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 780ed8ad838SJan Kara if (!bh->b_page) { 781ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 782ed8ad838SJan Kara return; 783ed8ad838SJan Kara } 784ed8ad838SJan Kara /* 785ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 786ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 787ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 788ed8ad838SJan Kara */ 789ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 7903ee2a3e7SUros Bizjak do { 791ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 7923ee2a3e7SUros Bizjak } while (unlikely(!try_cmpxchg(&bh->b_state, &old_state, new_state))); 793ed8ad838SJan Kara } 794ed8ad838SJan Kara 7952ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7962ed88685STheodore Ts'o struct buffer_head *bh, int flags) 797ac27a0ecSDave Kleikamp { 7982ed88685STheodore Ts'o struct ext4_map_blocks map; 799efe70c29SJan Kara int ret = 0; 800ac27a0ecSDave Kleikamp 80146c7f254STao Ma if (ext4_has_inline_data(inode)) 80246c7f254STao Ma return -ERANGE; 80346c7f254STao Ma 8042ed88685STheodore Ts'o map.m_lblk = iblock; 8052ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 8062ed88685STheodore Ts'o 807efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 808efe70c29SJan Kara flags); 809ac27a0ecSDave Kleikamp if (ret > 0) { 8102ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 811ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 8122ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 813ac27a0ecSDave Kleikamp ret = 0; 814547edce3SRoss Zwisler } else if (ret == 0) { 815547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 816547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 817ac27a0ecSDave Kleikamp } 818ac27a0ecSDave Kleikamp return ret; 819ac27a0ecSDave Kleikamp } 820ac27a0ecSDave Kleikamp 8212ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8222ed88685STheodore Ts'o struct buffer_head *bh, int create) 8232ed88685STheodore Ts'o { 8242ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8252ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8262ed88685STheodore Ts'o } 8272ed88685STheodore Ts'o 828ac27a0ecSDave Kleikamp /* 829705965bdSJan Kara * Get block function used when preparing for buffered write if we require 830705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 831705965bdSJan Kara * will be converted to written after the IO is complete. 832705965bdSJan Kara */ 833705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 834705965bdSJan Kara struct buffer_head *bh_result, int create) 835705965bdSJan Kara { 836705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 837705965bdSJan Kara inode->i_ino, create); 838705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 8398d5459c1SJan Kara EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); 840705965bdSJan Kara } 841705965bdSJan Kara 842efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 843efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 844efe70c29SJan Kara 845e84dfbe2SJan Kara /* 846ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 847ac27a0ecSDave Kleikamp */ 848617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 849c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 850ac27a0ecSDave Kleikamp { 8512ed88685STheodore Ts'o struct ext4_map_blocks map; 8522ed88685STheodore Ts'o struct buffer_head *bh; 853c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 8549558cf14SZhang Yi bool nowait = map_flags & EXT4_GET_BLOCKS_CACHED_NOWAIT; 85510560082STheodore Ts'o int err; 856ac27a0ecSDave Kleikamp 857837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8588016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 8599558cf14SZhang Yi ASSERT(create == 0 || !nowait); 860ac27a0ecSDave Kleikamp 8612ed88685STheodore Ts'o map.m_lblk = block; 8622ed88685STheodore Ts'o map.m_len = 1; 863c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8642ed88685STheodore Ts'o 86510560082STheodore Ts'o if (err == 0) 86610560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8672ed88685STheodore Ts'o if (err < 0) 86810560082STheodore Ts'o return ERR_PTR(err); 8692ed88685STheodore Ts'o 8709558cf14SZhang Yi if (nowait) 8719558cf14SZhang Yi return sb_find_get_block(inode->i_sb, map.m_pblk); 8729558cf14SZhang Yi 8732ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 87410560082STheodore Ts'o if (unlikely(!bh)) 87510560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8762ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 877837c23fbSChunguang Xu ASSERT(create != 0); 878837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8798016e29fSHarshad Shirwadkar || (handle != NULL)); 880ac27a0ecSDave Kleikamp 881ac27a0ecSDave Kleikamp /* 882ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 883ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 884ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 885617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 886ac27a0ecSDave Kleikamp * problem. 887ac27a0ecSDave Kleikamp */ 888ac27a0ecSDave Kleikamp lock_buffer(bh); 889ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 890188c299eSJan Kara err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 891188c299eSJan Kara EXT4_JTR_NONE); 89210560082STheodore Ts'o if (unlikely(err)) { 89310560082STheodore Ts'o unlock_buffer(bh); 89410560082STheodore Ts'o goto errout; 89510560082STheodore Ts'o } 89610560082STheodore Ts'o if (!buffer_uptodate(bh)) { 897ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 898ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 899ac27a0ecSDave Kleikamp } 900ac27a0ecSDave Kleikamp unlock_buffer(bh); 9010390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 9020390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 90310560082STheodore Ts'o if (unlikely(err)) 90410560082STheodore Ts'o goto errout; 90510560082STheodore Ts'o } else 906ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 907ac27a0ecSDave Kleikamp return bh; 90810560082STheodore Ts'o errout: 90910560082STheodore Ts'o brelse(bh); 91010560082STheodore Ts'o return ERR_PTR(err); 911ac27a0ecSDave Kleikamp } 912ac27a0ecSDave Kleikamp 913617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 914c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 915ac27a0ecSDave Kleikamp { 916ac27a0ecSDave Kleikamp struct buffer_head *bh; 9172d069c08Szhangyi (F) int ret; 918ac27a0ecSDave Kleikamp 919c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 9201c215028STheodore Ts'o if (IS_ERR(bh)) 921ac27a0ecSDave Kleikamp return bh; 9227963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 923ac27a0ecSDave Kleikamp return bh; 9242d069c08Szhangyi (F) 9252d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 9262d069c08Szhangyi (F) if (ret) { 927ac27a0ecSDave Kleikamp put_bh(bh); 9282d069c08Szhangyi (F) return ERR_PTR(ret); 9292d069c08Szhangyi (F) } 9302d069c08Szhangyi (F) return bh; 931ac27a0ecSDave Kleikamp } 932ac27a0ecSDave Kleikamp 9339699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 9349699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 9359699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 9369699d4f9STahsin Erdogan { 9379699d4f9STahsin Erdogan int i, err; 9389699d4f9STahsin Erdogan 9399699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9409699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9419699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9429699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9439699d4f9STahsin Erdogan bh_count = i; 9449699d4f9STahsin Erdogan goto out_brelse; 9459699d4f9STahsin Erdogan } 9469699d4f9STahsin Erdogan } 9479699d4f9STahsin Erdogan 9489699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9499699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9502d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9512d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9529699d4f9STahsin Erdogan 9539699d4f9STahsin Erdogan if (!wait) 9549699d4f9STahsin Erdogan return 0; 9559699d4f9STahsin Erdogan 9569699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9579699d4f9STahsin Erdogan if (bhs[i]) 9589699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9599699d4f9STahsin Erdogan 9609699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9619699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9629699d4f9STahsin Erdogan err = -EIO; 9639699d4f9STahsin Erdogan goto out_brelse; 9649699d4f9STahsin Erdogan } 9659699d4f9STahsin Erdogan } 9669699d4f9STahsin Erdogan return 0; 9679699d4f9STahsin Erdogan 9689699d4f9STahsin Erdogan out_brelse: 9699699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9709699d4f9STahsin Erdogan brelse(bhs[i]); 9719699d4f9STahsin Erdogan bhs[i] = NULL; 9729699d4f9STahsin Erdogan } 9739699d4f9STahsin Erdogan return err; 9749699d4f9STahsin Erdogan } 9759699d4f9STahsin Erdogan 976188c299eSJan Kara int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, 977ac27a0ecSDave Kleikamp struct buffer_head *head, 978ac27a0ecSDave Kleikamp unsigned from, 979ac27a0ecSDave Kleikamp unsigned to, 980ac27a0ecSDave Kleikamp int *partial, 981188c299eSJan Kara int (*fn)(handle_t *handle, struct inode *inode, 982ac27a0ecSDave Kleikamp struct buffer_head *bh)) 983ac27a0ecSDave Kleikamp { 984ac27a0ecSDave Kleikamp struct buffer_head *bh; 985ac27a0ecSDave Kleikamp unsigned block_start, block_end; 986ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 987ac27a0ecSDave Kleikamp int err, ret = 0; 988ac27a0ecSDave Kleikamp struct buffer_head *next; 989ac27a0ecSDave Kleikamp 990ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 991ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 992de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 993ac27a0ecSDave Kleikamp next = bh->b_this_page; 994ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 995ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 996ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 997ac27a0ecSDave Kleikamp *partial = 1; 998ac27a0ecSDave Kleikamp continue; 999ac27a0ecSDave Kleikamp } 1000188c299eSJan Kara err = (*fn)(handle, inode, bh); 1001ac27a0ecSDave Kleikamp if (!ret) 1002ac27a0ecSDave Kleikamp ret = err; 1003ac27a0ecSDave Kleikamp } 1004ac27a0ecSDave Kleikamp return ret; 1005ac27a0ecSDave Kleikamp } 1006ac27a0ecSDave Kleikamp 1007188c299eSJan Kara int do_journal_get_write_access(handle_t *handle, struct inode *inode, 1008ac27a0ecSDave Kleikamp struct buffer_head *bh) 1009ac27a0ecSDave Kleikamp { 101056d35a4cSJan Kara int dirty = buffer_dirty(bh); 101156d35a4cSJan Kara int ret; 101256d35a4cSJan Kara 1013ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1014ac27a0ecSDave Kleikamp return 0; 101556d35a4cSJan Kara /* 1016ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 101756d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 101856d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1019ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 102056d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 102156d35a4cSJan Kara * ever write the buffer. 102256d35a4cSJan Kara */ 102356d35a4cSJan Kara if (dirty) 102456d35a4cSJan Kara clear_buffer_dirty(bh); 10255d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 1026188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1027188c299eSJan Kara EXT4_JTR_NONE); 102856d35a4cSJan Kara if (!ret && dirty) 102956d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 103056d35a4cSJan Kara return ret; 1031ac27a0ecSDave Kleikamp } 1032ac27a0ecSDave Kleikamp 1033643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10342058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10352058f83aSMichael Halcrow get_block_t *get_block) 10362058f83aSMichael Halcrow { 103709cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10382058f83aSMichael Halcrow unsigned to = from + len; 10392058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10402058f83aSMichael Halcrow unsigned block_start, block_end; 10412058f83aSMichael Halcrow sector_t block; 10422058f83aSMichael Halcrow int err = 0; 10432058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10442058f83aSMichael Halcrow unsigned bbits; 10450b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10460b578f35SChandan Rajendra int nr_wait = 0; 10470b578f35SChandan Rajendra int i; 10482058f83aSMichael Halcrow 10492058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 105009cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 105109cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10522058f83aSMichael Halcrow BUG_ON(from > to); 10532058f83aSMichael Halcrow 10542058f83aSMichael Halcrow if (!page_has_buffers(page)) 10552058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10562058f83aSMichael Halcrow head = page_buffers(page); 10572058f83aSMichael Halcrow bbits = ilog2(blocksize); 105809cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10592058f83aSMichael Halcrow 10602058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10612058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10622058f83aSMichael Halcrow block_end = block_start + blocksize; 10632058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10642058f83aSMichael Halcrow if (PageUptodate(page)) { 10652058f83aSMichael Halcrow set_buffer_uptodate(bh); 10662058f83aSMichael Halcrow } 10672058f83aSMichael Halcrow continue; 10682058f83aSMichael Halcrow } 10692058f83aSMichael Halcrow if (buffer_new(bh)) 10702058f83aSMichael Halcrow clear_buffer_new(bh); 10712058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10722058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10732058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10742058f83aSMichael Halcrow if (err) 10752058f83aSMichael Halcrow break; 10762058f83aSMichael Halcrow if (buffer_new(bh)) { 10772058f83aSMichael Halcrow if (PageUptodate(page)) { 10782058f83aSMichael Halcrow clear_buffer_new(bh); 10792058f83aSMichael Halcrow set_buffer_uptodate(bh); 10802058f83aSMichael Halcrow mark_buffer_dirty(bh); 10812058f83aSMichael Halcrow continue; 10822058f83aSMichael Halcrow } 10832058f83aSMichael Halcrow if (block_end > to || block_start < from) 10842058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10852058f83aSMichael Halcrow block_start, from); 10862058f83aSMichael Halcrow continue; 10872058f83aSMichael Halcrow } 10882058f83aSMichael Halcrow } 10892058f83aSMichael Halcrow if (PageUptodate(page)) { 10902058f83aSMichael Halcrow set_buffer_uptodate(bh); 10912058f83aSMichael Halcrow continue; 10922058f83aSMichael Halcrow } 10932058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10942058f83aSMichael Halcrow !buffer_unwritten(bh) && 10952058f83aSMichael Halcrow (block_start < from || block_end > to)) { 10962d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 10970b578f35SChandan Rajendra wait[nr_wait++] = bh; 10982058f83aSMichael Halcrow } 10992058f83aSMichael Halcrow } 11002058f83aSMichael Halcrow /* 11012058f83aSMichael Halcrow * If we issued read requests, let them complete. 11022058f83aSMichael Halcrow */ 11030b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11040b578f35SChandan Rajendra wait_on_buffer(wait[i]); 11050b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 11062058f83aSMichael Halcrow err = -EIO; 11072058f83aSMichael Halcrow } 11087e0785fcSChandan Rajendra if (unlikely(err)) { 11092058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 11104f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11110b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11120b578f35SChandan Rajendra int err2; 11130b578f35SChandan Rajendra 111451e4e315SEric Biggers err2 = fscrypt_decrypt_pagecache_blocks(page_folio(page), 111551e4e315SEric Biggers blocksize, 11160b578f35SChandan Rajendra bh_offset(wait[i])); 11170b578f35SChandan Rajendra if (err2) { 11180b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11190b578f35SChandan Rajendra err = err2; 11200b578f35SChandan Rajendra } 11210b578f35SChandan Rajendra } 11227e0785fcSChandan Rajendra } 11237e0785fcSChandan Rajendra 11242058f83aSMichael Halcrow return err; 11252058f83aSMichael Halcrow } 11262058f83aSMichael Halcrow #endif 11272058f83aSMichael Halcrow 11289462f770SJan Kara /* 11299462f770SJan Kara * To preserve ordering, it is essential that the hole instantiation and 11309462f770SJan Kara * the data write be encapsulated in a single transaction. We cannot 11319462f770SJan Kara * close off a transaction and start a new one between the ext4_get_block() 11329462f770SJan Kara * and the ext4_write_end(). So doing the jbd2_journal_start at the start of 11339462f770SJan Kara * ext4_write_begin() is the right place. 11349462f770SJan Kara */ 1135bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 11369d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 1137bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1138ac27a0ecSDave Kleikamp { 1139bfc1af65SNick Piggin struct inode *inode = mapping->host; 11401938a150SAneesh Kumar K.V int ret, needed_blocks; 1141ac27a0ecSDave Kleikamp handle_t *handle; 1142ac27a0ecSDave Kleikamp int retries = 0; 1143bfc1af65SNick Piggin struct page *page; 1144bfc1af65SNick Piggin pgoff_t index; 1145bfc1af65SNick Piggin unsigned from, to; 1146bfc1af65SNick Piggin 11470db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11480db1ff22STheodore Ts'o return -EIO; 11490db1ff22STheodore Ts'o 11509d6b0cd7SMatthew Wilcox (Oracle) trace_ext4_write_begin(inode, pos, len); 11511938a150SAneesh Kumar K.V /* 11521938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11531938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11541938a150SAneesh Kumar K.V */ 11551938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 115609cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 115709cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1158bfc1af65SNick Piggin to = from + len; 1159ac27a0ecSDave Kleikamp 1160f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1161f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1162832ee62dSMatthew Wilcox (Oracle) pagep); 1163f19d5870STao Ma if (ret < 0) 116447564bfbSTheodore Ts'o return ret; 116547564bfbSTheodore Ts'o if (ret == 1) 116647564bfbSTheodore Ts'o return 0; 1167f19d5870STao Ma } 1168f19d5870STao Ma 116947564bfbSTheodore Ts'o /* 117047564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 117147564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 117247564bfbSTheodore Ts'o * is being written back. So grab it first before we start 117347564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 117447564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 117547564bfbSTheodore Ts'o */ 117647564bfbSTheodore Ts'o retry_grab: 1177b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 117847564bfbSTheodore Ts'o if (!page) 117947564bfbSTheodore Ts'o return -ENOMEM; 1180d1052d23SJinke Han /* 1181d1052d23SJinke Han * The same as page allocation, we prealloc buffer heads before 1182d1052d23SJinke Han * starting the handle. 1183d1052d23SJinke Han */ 1184d1052d23SJinke Han if (!page_has_buffers(page)) 1185d1052d23SJinke Han create_empty_buffers(page, inode->i_sb->s_blocksize, 0); 1186d1052d23SJinke Han 118747564bfbSTheodore Ts'o unlock_page(page); 118847564bfbSTheodore Ts'o 118947564bfbSTheodore Ts'o retry_journal: 11909924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 11917479d2b9SAndrew Morton if (IS_ERR(handle)) { 119209cbfeafSKirill A. Shutemov put_page(page); 119347564bfbSTheodore Ts'o return PTR_ERR(handle); 1194cf108bcaSJan Kara } 1195f19d5870STao Ma 119647564bfbSTheodore Ts'o lock_page(page); 119747564bfbSTheodore Ts'o if (page->mapping != mapping) { 119847564bfbSTheodore Ts'o /* The page got truncated from under us */ 119947564bfbSTheodore Ts'o unlock_page(page); 120009cbfeafSKirill A. Shutemov put_page(page); 1201cf108bcaSJan Kara ext4_journal_stop(handle); 120247564bfbSTheodore Ts'o goto retry_grab; 1203cf108bcaSJan Kara } 12047afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 12057afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1206cf108bcaSJan Kara 1207643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 12082058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 12092058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1210705965bdSJan Kara ext4_get_block_unwritten); 12112058f83aSMichael Halcrow else 12122058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 12132058f83aSMichael Halcrow ext4_get_block); 12142058f83aSMichael Halcrow #else 1215744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1216705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1217705965bdSJan Kara ext4_get_block_unwritten); 1218744692dcSJiaying Zhang else 12196e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 12202058f83aSMichael Halcrow #endif 1221bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1222188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, 1223188c299eSJan Kara page_buffers(page), from, to, NULL, 1224f19d5870STao Ma do_journal_get_write_access); 1225b46be050SAndrey Savochkin } 1226bfc1af65SNick Piggin 1227bfc1af65SNick Piggin if (ret) { 1228c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1229c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1230c93d8f88SEric Biggers 1231bfc1af65SNick Piggin unlock_page(page); 1232ae4d5372SAneesh Kumar K.V /* 12336e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1234ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1235f340b3d9Shongnanli * i_size_read because we hold i_rwsem. 12361938a150SAneesh Kumar K.V * 12371938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12381938a150SAneesh Kumar K.V * truncate finishes 1239ae4d5372SAneesh Kumar K.V */ 1240c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12411938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12421938a150SAneesh Kumar K.V 12431938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1244c93d8f88SEric Biggers if (extended) { 1245b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12461938a150SAneesh Kumar K.V /* 1247ffacfa7aSJan Kara * If truncate failed early the inode might 12481938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12491938a150SAneesh Kumar K.V * make sure the inode is removed from the 12501938a150SAneesh Kumar K.V * orphan list in that case. 12511938a150SAneesh Kumar K.V */ 12521938a150SAneesh Kumar K.V if (inode->i_nlink) 12531938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12541938a150SAneesh Kumar K.V } 1255bfc1af65SNick Piggin 125647564bfbSTheodore Ts'o if (ret == -ENOSPC && 125747564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 125847564bfbSTheodore Ts'o goto retry_journal; 125909cbfeafSKirill A. Shutemov put_page(page); 126047564bfbSTheodore Ts'o return ret; 126147564bfbSTheodore Ts'o } 126247564bfbSTheodore Ts'o *pagep = page; 1263ac27a0ecSDave Kleikamp return ret; 1264ac27a0ecSDave Kleikamp } 1265ac27a0ecSDave Kleikamp 1266bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1267188c299eSJan Kara static int write_end_fn(handle_t *handle, struct inode *inode, 1268188c299eSJan Kara struct buffer_head *bh) 1269ac27a0ecSDave Kleikamp { 127013fca323STheodore Ts'o int ret; 1271ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1272ac27a0ecSDave Kleikamp return 0; 1273ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 127413fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 127513fca323STheodore Ts'o clear_buffer_meta(bh); 127613fca323STheodore Ts'o clear_buffer_prio(bh); 127713fca323STheodore Ts'o return ret; 1278ac27a0ecSDave Kleikamp } 1279ac27a0ecSDave Kleikamp 1280eed4333fSZheng Liu /* 1281eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1282eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1283eed4333fSZheng Liu * 1284eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1285eed4333fSZheng Liu * buffers are managed internally. 1286eed4333fSZheng Liu */ 1287eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1288f8514083SAneesh Kumar K.V struct address_space *mapping, 1289f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1290f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1291f8514083SAneesh Kumar K.V { 1292f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1293eed4333fSZheng Liu struct inode *inode = mapping->host; 12940572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1295eed4333fSZheng Liu int ret = 0, ret2; 1296eed4333fSZheng Liu int i_size_changed = 0; 1297c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1298eed4333fSZheng Liu 1299eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 13006984aef5SZhang Yi 13015c099c4fSYe Bin if (ext4_has_inline_data(inode) && 13025c099c4fSYe Bin ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) 13036984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 13046984aef5SZhang Yi 13056984aef5SZhang Yi copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1306f8514083SAneesh Kumar K.V /* 13074631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1308f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1309c93d8f88SEric Biggers * 1310c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1311c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1312f8514083SAneesh Kumar K.V */ 1313c93d8f88SEric Biggers if (!verity) 13144631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1315f8514083SAneesh Kumar K.V unlock_page(page); 131609cbfeafSKirill A. Shutemov put_page(page); 1317f8514083SAneesh Kumar K.V 1318c93d8f88SEric Biggers if (old_size < pos && !verity) 13190572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1320f8514083SAneesh Kumar K.V /* 1321f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1322f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1323f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1324f8514083SAneesh Kumar K.V * filesystems. 1325f8514083SAneesh Kumar K.V */ 13266984aef5SZhang Yi if (i_size_changed) 13274209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1328f8514083SAneesh Kumar K.V 1329c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1330f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1331f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1332f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1333f8514083SAneesh Kumar K.V */ 1334f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 133555ce2f64SZhang Yi 1336617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1337ac27a0ecSDave Kleikamp if (!ret) 1338ac27a0ecSDave Kleikamp ret = ret2; 1339bfc1af65SNick Piggin 1340c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1341b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1342f8514083SAneesh Kumar K.V /* 1343ffacfa7aSJan Kara * If truncate failed early the inode might still be 1344f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1345f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1346f8514083SAneesh Kumar K.V */ 1347f8514083SAneesh Kumar K.V if (inode->i_nlink) 1348f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1349f8514083SAneesh Kumar K.V } 1350f8514083SAneesh Kumar K.V 1351bfc1af65SNick Piggin return ret ? ret : copied; 1352ac27a0ecSDave Kleikamp } 1353ac27a0ecSDave Kleikamp 1354b90197b6STheodore Ts'o /* 1355b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1356b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1357b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1358b90197b6STheodore Ts'o */ 13593b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 1360188c299eSJan Kara struct inode *inode, 13613b136499SJan Kara struct page *page, 13623b136499SJan Kara unsigned from, unsigned to) 1363b90197b6STheodore Ts'o { 1364b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1365b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1366b90197b6STheodore Ts'o 1367b90197b6STheodore Ts'o bh = head = page_buffers(page); 1368b90197b6STheodore Ts'o do { 1369b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1370b90197b6STheodore Ts'o if (buffer_new(bh)) { 1371b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1372b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1373b90197b6STheodore Ts'o unsigned start, size; 1374b90197b6STheodore Ts'o 1375b90197b6STheodore Ts'o start = max(from, block_start); 1376b90197b6STheodore Ts'o size = min(to, block_end) - start; 1377b90197b6STheodore Ts'o 1378b90197b6STheodore Ts'o zero_user(page, start, size); 1379188c299eSJan Kara write_end_fn(handle, inode, bh); 1380b90197b6STheodore Ts'o } 1381b90197b6STheodore Ts'o clear_buffer_new(bh); 1382b90197b6STheodore Ts'o } 1383b90197b6STheodore Ts'o } 1384b90197b6STheodore Ts'o block_start = block_end; 1385b90197b6STheodore Ts'o bh = bh->b_this_page; 1386b90197b6STheodore Ts'o } while (bh != head); 1387b90197b6STheodore Ts'o } 1388b90197b6STheodore Ts'o 1389bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1390bfc1af65SNick Piggin struct address_space *mapping, 1391bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1392bfc1af65SNick Piggin struct page *page, void *fsdata) 1393ac27a0ecSDave Kleikamp { 1394617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1395bfc1af65SNick Piggin struct inode *inode = mapping->host; 13960572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1397ac27a0ecSDave Kleikamp int ret = 0, ret2; 1398ac27a0ecSDave Kleikamp int partial = 0; 1399bfc1af65SNick Piggin unsigned from, to; 14004631dbf6SDmitry Monakhov int size_changed = 0; 1401c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1402ac27a0ecSDave Kleikamp 14039bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 140409cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1405bfc1af65SNick Piggin to = from + len; 1406bfc1af65SNick Piggin 1407441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1408441c8508SCurt Wohlgemuth 14096984aef5SZhang Yi if (ext4_has_inline_data(inode)) 14106984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 14116984aef5SZhang Yi 14126984aef5SZhang Yi if (unlikely(copied < len) && !PageUptodate(page)) { 1413bfc1af65SNick Piggin copied = 0; 1414188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, from, to); 14153b136499SJan Kara } else { 14163b136499SJan Kara if (unlikely(copied < len)) 1417188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, 14183b136499SJan Kara from + copied, to); 1419188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_buffers(page), 1420188c299eSJan Kara from, from + copied, &partial, 14213b136499SJan Kara write_end_fn); 1422ac27a0ecSDave Kleikamp if (!partial) 1423ac27a0ecSDave Kleikamp SetPageUptodate(page); 14243fdcfb66STao Ma } 1425c93d8f88SEric Biggers if (!verity) 14264631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 142719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14282d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14294631dbf6SDmitry Monakhov unlock_page(page); 143009cbfeafSKirill A. Shutemov put_page(page); 14314631dbf6SDmitry Monakhov 1432c93d8f88SEric Biggers if (old_size < pos && !verity) 14330572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14340572639fSXiaoguang Wang 14356984aef5SZhang Yi if (size_changed) { 1436617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1437ac27a0ecSDave Kleikamp if (!ret) 1438ac27a0ecSDave Kleikamp ret = ret2; 1439ac27a0ecSDave Kleikamp } 1440bfc1af65SNick Piggin 1441c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1442f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1443f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1444f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1445f8514083SAneesh Kumar K.V */ 1446f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1447f8514083SAneesh Kumar K.V 1448617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1449ac27a0ecSDave Kleikamp if (!ret) 1450ac27a0ecSDave Kleikamp ret = ret2; 1451c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1452b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1453f8514083SAneesh Kumar K.V /* 1454ffacfa7aSJan Kara * If truncate failed early the inode might still be 1455f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1456f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1457f8514083SAneesh Kumar K.V */ 1458f8514083SAneesh Kumar K.V if (inode->i_nlink) 1459f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1460f8514083SAneesh Kumar K.V } 1461bfc1af65SNick Piggin 1462bfc1af65SNick Piggin return ret ? ret : copied; 1463ac27a0ecSDave Kleikamp } 1464d2a17637SMingming Cao 14659d0be502STheodore Ts'o /* 1466c27e43a1SEric Whitney * Reserve space for a single cluster 14679d0be502STheodore Ts'o */ 1468c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1469d2a17637SMingming Cao { 1470d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14710637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14725dd4056dSChristoph Hellwig int ret; 1473d2a17637SMingming Cao 147460e58e0fSMingming Cao /* 147572b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 147672b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 147772b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 147860e58e0fSMingming Cao */ 14797b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14805dd4056dSChristoph Hellwig if (ret) 14815dd4056dSChristoph Hellwig return ret; 148203179fe9STheodore Ts'o 148303179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 148471d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 148503179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 148603179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1487d2a17637SMingming Cao return -ENOSPC; 1488d2a17637SMingming Cao } 14899d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1490c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14910637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 149239bc680aSDmitry Monakhov 1493d2a17637SMingming Cao return 0; /* success */ 1494d2a17637SMingming Cao } 1495d2a17637SMingming Cao 1496f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1497d2a17637SMingming Cao { 1498d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14990637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1500d2a17637SMingming Cao 1501cd213226SMingming Cao if (!to_free) 1502cd213226SMingming Cao return; /* Nothing to release, exit */ 1503cd213226SMingming Cao 1504d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1505cd213226SMingming Cao 15065a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 15070637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1508cd213226SMingming Cao /* 15090637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 15100637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 15110637c6f4STheodore Ts'o * function is called from invalidate page, it's 15120637c6f4STheodore Ts'o * harmless to return without any action. 1513cd213226SMingming Cao */ 15148de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15150637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15161084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15170637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15180637c6f4STheodore Ts'o WARN_ON(1); 15190637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15200637c6f4STheodore Ts'o } 15210637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15220637c6f4STheodore Ts'o 152372b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 152457042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1525d2a17637SMingming Cao 1526d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 152760e58e0fSMingming Cao 15287b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1529d2a17637SMingming Cao } 1530d2a17637SMingming Cao 1531ac27a0ecSDave Kleikamp /* 153264769240SAlex Tomas * Delayed allocation stuff 153364769240SAlex Tomas */ 153464769240SAlex Tomas 15354e7ea81dSJan Kara struct mpage_da_data { 153615648d59SJan Kara /* These are input fields for ext4_do_writepages() */ 15374e7ea81dSJan Kara struct inode *inode; 15384e7ea81dSJan Kara struct writeback_control *wbc; 153915648d59SJan Kara unsigned int can_map:1; /* Can writepages call map blocks? */ 15406b523df4SJan Kara 154115648d59SJan Kara /* These are internal state of ext4_do_writepages() */ 15424e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15434e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15444e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 154564769240SAlex Tomas /* 15464e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15474e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15484e7ea81dSJan Kara * is delalloc or unwritten. 154964769240SAlex Tomas */ 15504e7ea81dSJan Kara struct ext4_map_blocks map; 15514e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1552dddbd6acSJan Kara unsigned int do_map:1; 15536b8ed620SJan Kara unsigned int scanned_until_end:1; 15544e7ea81dSJan Kara }; 155564769240SAlex Tomas 15564e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15574e7ea81dSJan Kara bool invalidate) 1558c4a0c46eSAneesh Kumar K.V { 1559fb5a5be0SMatthew Wilcox (Oracle) unsigned nr, i; 1560c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1561fb5a5be0SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1562c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1563c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15644e7ea81dSJan Kara 15654e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15664e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15674e7ea81dSJan Kara return; 1568c4a0c46eSAneesh Kumar K.V 15696b8ed620SJan Kara mpd->scanned_until_end = 0; 1570c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1571c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15724e7ea81dSJan Kara if (invalidate) { 15734e7ea81dSJan Kara ext4_lblk_t start, last; 157409cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 157509cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 15767f0d8e1dSEric Whitney 15777f0d8e1dSEric Whitney /* 15787f0d8e1dSEric Whitney * avoid racing with extent status tree scans made by 15797f0d8e1dSEric Whitney * ext4_insert_delayed_block() 15807f0d8e1dSEric Whitney */ 15817f0d8e1dSEric Whitney down_write(&EXT4_I(inode)->i_data_sem); 158251865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15837f0d8e1dSEric Whitney up_write(&EXT4_I(inode)->i_data_sem); 15844e7ea81dSJan Kara } 158551865fdaSZheng Liu 1586fb5a5be0SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1587c4a0c46eSAneesh Kumar K.V while (index <= end) { 1588fb5a5be0SMatthew Wilcox (Oracle) nr = filemap_get_folios(mapping, &index, end, &fbatch); 1589fb5a5be0SMatthew Wilcox (Oracle) if (nr == 0) 1590c4a0c46eSAneesh Kumar K.V break; 1591fb5a5be0SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) { 1592fb5a5be0SMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 15932b85a617SJan Kara 1594fb5a5be0SMatthew Wilcox (Oracle) if (folio->index < mpd->first_page) 1595fb5a5be0SMatthew Wilcox (Oracle) continue; 1596fb5a5be0SMatthew Wilcox (Oracle) if (folio->index + folio_nr_pages(folio) - 1 > end) 1597fb5a5be0SMatthew Wilcox (Oracle) continue; 15987ba13abbSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 15997ba13abbSMatthew Wilcox (Oracle) BUG_ON(folio_test_writeback(folio)); 16004e7ea81dSJan Kara if (invalidate) { 16017ba13abbSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 16027ba13abbSMatthew Wilcox (Oracle) folio_clear_dirty_for_io(folio); 16037ba13abbSMatthew Wilcox (Oracle) block_invalidate_folio(folio, 0, 16047ba13abbSMatthew Wilcox (Oracle) folio_size(folio)); 16057ba13abbSMatthew Wilcox (Oracle) folio_clear_uptodate(folio); 16064e7ea81dSJan Kara } 16077ba13abbSMatthew Wilcox (Oracle) folio_unlock(folio); 1608c4a0c46eSAneesh Kumar K.V } 1609fb5a5be0SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1610c4a0c46eSAneesh Kumar K.V } 1611c4a0c46eSAneesh Kumar K.V } 1612c4a0c46eSAneesh Kumar K.V 1613df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1614df22291fSAneesh Kumar K.V { 1615df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 161692b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1617f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 161892b97816STheodore Ts'o 161992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16205dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1621f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 162292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 162392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1624f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 162557042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 162692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1627f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16287b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 162992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 163092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1631f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1632df22291fSAneesh Kumar K.V return; 1633df22291fSAneesh Kumar K.V } 1634df22291fSAneesh Kumar K.V 1635188c299eSJan Kara static int ext4_bh_delay_or_unwritten(handle_t *handle, struct inode *inode, 1636188c299eSJan Kara struct buffer_head *bh) 163729fa89d0SAneesh Kumar K.V { 1638c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 163929fa89d0SAneesh Kumar K.V } 164029fa89d0SAneesh Kumar K.V 164164769240SAlex Tomas /* 16420b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16430b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16440b02f4c0SEric Whitney * count or making a pending reservation 16450b02f4c0SEric Whitney * where needed 16460b02f4c0SEric Whitney * 16470b02f4c0SEric Whitney * @inode - file containing the newly added block 16480b02f4c0SEric Whitney * @lblk - logical block to be added 16490b02f4c0SEric Whitney * 16500b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16510b02f4c0SEric Whitney */ 16520b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16530b02f4c0SEric Whitney { 16540b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16550b02f4c0SEric Whitney int ret; 16560b02f4c0SEric Whitney bool allocated = false; 16576fed8395SJeffle Xu bool reserved = false; 16580b02f4c0SEric Whitney 16590b02f4c0SEric Whitney /* 16600b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16610b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16620b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16630b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16640b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16650b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16660b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16670b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16680b02f4c0SEric Whitney * extents status tree doesn't get a match. 16690b02f4c0SEric Whitney */ 16700b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16710b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16720b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16730b02f4c0SEric Whitney goto errout; 16746fed8395SJeffle Xu reserved = true; 16750b02f4c0SEric Whitney } else { /* bigalloc */ 16760b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16770b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16780b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16790b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16800b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16810b02f4c0SEric Whitney if (ret < 0) 16820b02f4c0SEric Whitney goto errout; 16830b02f4c0SEric Whitney if (ret == 0) { 16840b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16850b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16860b02f4c0SEric Whitney goto errout; 16876fed8395SJeffle Xu reserved = true; 16880b02f4c0SEric Whitney } else { 16890b02f4c0SEric Whitney allocated = true; 16900b02f4c0SEric Whitney } 16910b02f4c0SEric Whitney } else { 16920b02f4c0SEric Whitney allocated = true; 16930b02f4c0SEric Whitney } 16940b02f4c0SEric Whitney } 16950b02f4c0SEric Whitney } 16960b02f4c0SEric Whitney 16970b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16986fed8395SJeffle Xu if (ret && reserved) 16996fed8395SJeffle Xu ext4_da_release_space(inode, 1); 17000b02f4c0SEric Whitney 17010b02f4c0SEric Whitney errout: 17020b02f4c0SEric Whitney return ret; 17030b02f4c0SEric Whitney } 17040b02f4c0SEric Whitney 17050b02f4c0SEric Whitney /* 17065356f261SAditya Kali * This function is grabs code from the very beginning of 17075356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 17085356f261SAditya Kali * time. This function looks up the requested blocks and sets the 17095356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 17105356f261SAditya Kali */ 17115356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 17125356f261SAditya Kali struct ext4_map_blocks *map, 17135356f261SAditya Kali struct buffer_head *bh) 17145356f261SAditya Kali { 1715d100eef2SZheng Liu struct extent_status es; 17165356f261SAditya Kali int retval; 17175356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1718921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1719921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1720921f266bSDmitry Monakhov 1721921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1722921f266bSDmitry Monakhov #endif 17235356f261SAditya Kali 17245356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17255356f261SAditya Kali invalid_block = ~0; 17265356f261SAditya Kali 17275356f261SAditya Kali map->m_flags = 0; 172870aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 17295356f261SAditya Kali (unsigned long) map->m_lblk); 1730d100eef2SZheng Liu 1731d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1732bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1733d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1734d100eef2SZheng Liu retval = 0; 1735c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1736d100eef2SZheng Liu goto add_delayed; 1737d100eef2SZheng Liu } 1738d100eef2SZheng Liu 1739d100eef2SZheng Liu /* 17403eda41dfSEric Whitney * Delayed extent could be allocated by fallocate. 17413eda41dfSEric Whitney * So we need to check it. 1742d100eef2SZheng Liu */ 17433eda41dfSEric Whitney if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 17443eda41dfSEric Whitney map_bh(bh, inode->i_sb, invalid_block); 17453eda41dfSEric Whitney set_buffer_new(bh); 17463eda41dfSEric Whitney set_buffer_delay(bh); 1747d100eef2SZheng Liu return 0; 1748d100eef2SZheng Liu } 1749d100eef2SZheng Liu 1750d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1751d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1752d100eef2SZheng Liu if (retval > map->m_len) 1753d100eef2SZheng Liu retval = map->m_len; 1754d100eef2SZheng Liu map->m_len = retval; 1755d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1756d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1757d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1758d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1759d100eef2SZheng Liu else 17601e83bc81SArnd Bergmann BUG(); 1761d100eef2SZheng Liu 1762921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1763921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1764921f266bSDmitry Monakhov #endif 1765d100eef2SZheng Liu return retval; 1766d100eef2SZheng Liu } 1767d100eef2SZheng Liu 17685356f261SAditya Kali /* 17695356f261SAditya Kali * Try to see if we can get the block without requesting a new 17705356f261SAditya Kali * file system block. 17715356f261SAditya Kali */ 1772c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1773cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17749c3569b5STao Ma retval = 0; 1775cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17762f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17775356f261SAditya Kali else 17782f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17795356f261SAditya Kali 1780d100eef2SZheng Liu add_delayed: 17815356f261SAditya Kali if (retval == 0) { 1782f7fec032SZheng Liu int ret; 1783ad431025SEric Whitney 17845356f261SAditya Kali /* 17855356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17865356f261SAditya Kali * is it OK? 17875356f261SAditya Kali */ 17885356f261SAditya Kali 17890b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17900b02f4c0SEric Whitney if (ret != 0) { 1791f7fec032SZheng Liu retval = ret; 179251865fdaSZheng Liu goto out_unlock; 1793f7fec032SZheng Liu } 179451865fdaSZheng Liu 17955356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17965356f261SAditya Kali set_buffer_new(bh); 17975356f261SAditya Kali set_buffer_delay(bh); 1798f7fec032SZheng Liu } else if (retval > 0) { 1799f7fec032SZheng Liu int ret; 18003be78c73STheodore Ts'o unsigned int status; 1801f7fec032SZheng Liu 180244fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 180344fb851dSZheng Liu ext4_warning(inode->i_sb, 180444fb851dSZheng Liu "ES len assertion failed for inode " 180544fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 180644fb851dSZheng Liu inode->i_ino, retval, map->m_len); 180744fb851dSZheng Liu WARN_ON(1); 1808921f266bSDmitry Monakhov } 1809921f266bSDmitry Monakhov 1810f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1811f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1812f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1813f7fec032SZheng Liu map->m_pblk, status); 1814f7fec032SZheng Liu if (ret != 0) 1815f7fec032SZheng Liu retval = ret; 18165356f261SAditya Kali } 18175356f261SAditya Kali 18185356f261SAditya Kali out_unlock: 18195356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 18205356f261SAditya Kali 18215356f261SAditya Kali return retval; 18225356f261SAditya Kali } 18235356f261SAditya Kali 18245356f261SAditya Kali /* 1825d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1826b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1827b920c755STheodore Ts'o * reserve space for a single block. 182829fa89d0SAneesh Kumar K.V * 182929fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 183029fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 183129fa89d0SAneesh Kumar K.V * 183229fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 183329fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 183429fa89d0SAneesh Kumar K.V * initialized properly. 183564769240SAlex Tomas */ 18369c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18372ed88685STheodore Ts'o struct buffer_head *bh, int create) 183864769240SAlex Tomas { 18392ed88685STheodore Ts'o struct ext4_map_blocks map; 184064769240SAlex Tomas int ret = 0; 184164769240SAlex Tomas 184264769240SAlex Tomas BUG_ON(create == 0); 18432ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18442ed88685STheodore Ts'o 18452ed88685STheodore Ts'o map.m_lblk = iblock; 18462ed88685STheodore Ts'o map.m_len = 1; 184764769240SAlex Tomas 184864769240SAlex Tomas /* 184964769240SAlex Tomas * first, we need to know whether the block is allocated already 185064769240SAlex Tomas * preallocated blocks are unmapped but should treated 185164769240SAlex Tomas * the same as allocated blocks. 185264769240SAlex Tomas */ 18535356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18545356f261SAditya Kali if (ret <= 0) 18552ed88685STheodore Ts'o return ret; 185664769240SAlex Tomas 18572ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1858ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18592ed88685STheodore Ts'o 18602ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18612ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18622ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18632ed88685STheodore Ts'o * get_block multiple times when we write to the same 18642ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18652ed88685STheodore Ts'o * for partial write. 18662ed88685STheodore Ts'o */ 18672ed88685STheodore Ts'o set_buffer_new(bh); 1868c8205636STheodore Ts'o set_buffer_mapped(bh); 18692ed88685STheodore Ts'o } 18702ed88685STheodore Ts'o return 0; 187164769240SAlex Tomas } 187261628a3fSMingming Cao 187362e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 187462e086beSAneesh Kumar K.V unsigned int len) 187562e086beSAneesh Kumar K.V { 187662e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 187762e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 187862e086beSAneesh Kumar K.V handle_t *handle = NULL; 18793fdcfb66STao Ma int ret = 0, err = 0; 18803fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18813fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 18825c48a7dfSZhang Yi loff_t size; 188362e086beSAneesh Kumar K.V 1884cb20d518STheodore Ts'o ClearPageChecked(page); 18853fdcfb66STao Ma 18863fdcfb66STao Ma if (inline_data) { 18873fdcfb66STao Ma BUG_ON(page->index != 0); 18883fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18893fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18903fdcfb66STao Ma if (inode_bh == NULL) 18913fdcfb66STao Ma goto out; 18923fdcfb66STao Ma } 1893bdf96838STheodore Ts'o /* 1894bdf96838STheodore Ts'o * We need to release the page lock before we start the 1895bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1896bdf96838STheodore Ts'o * out from under us. 1897bdf96838STheodore Ts'o */ 1898bdf96838STheodore Ts'o get_page(page); 189962e086beSAneesh Kumar K.V unlock_page(page); 190062e086beSAneesh Kumar K.V 19019924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 19029924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 190362e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 190462e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1905bdf96838STheodore Ts'o put_page(page); 1906bdf96838STheodore Ts'o goto out_no_pagelock; 1907bdf96838STheodore Ts'o } 1908bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1909bdf96838STheodore Ts'o 1910bdf96838STheodore Ts'o lock_page(page); 1911bdf96838STheodore Ts'o put_page(page); 19125c48a7dfSZhang Yi size = i_size_read(inode); 19135c48a7dfSZhang Yi if (page->mapping != mapping || page_offset(page) > size) { 1914bdf96838STheodore Ts'o /* The page got truncated from under us */ 1915bdf96838STheodore Ts'o ext4_journal_stop(handle); 1916bdf96838STheodore Ts'o ret = 0; 191762e086beSAneesh Kumar K.V goto out; 191862e086beSAneesh Kumar K.V } 191962e086beSAneesh Kumar K.V 19203fdcfb66STao Ma if (inline_data) { 1921362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19223fdcfb66STao Ma } else { 19235c48a7dfSZhang Yi struct buffer_head *page_bufs = page_buffers(page); 19245c48a7dfSZhang Yi 19255c48a7dfSZhang Yi if (page->index == size >> PAGE_SHIFT) 19265c48a7dfSZhang Yi len = size & ~PAGE_MASK; 19275c48a7dfSZhang Yi else 19285c48a7dfSZhang Yi len = PAGE_SIZE; 19295c48a7dfSZhang Yi 1930188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1931188c299eSJan Kara NULL, do_journal_get_write_access); 193262e086beSAneesh Kumar K.V 1933188c299eSJan Kara err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1934188c299eSJan Kara NULL, write_end_fn); 19353fdcfb66STao Ma } 193662e086beSAneesh Kumar K.V if (ret == 0) 193762e086beSAneesh Kumar K.V ret = err; 1938b5b18160SJan Kara err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 1939afb585a9SMauricio Faria de Oliveira if (ret == 0) 1940afb585a9SMauricio Faria de Oliveira ret = err; 19412d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 194262e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 194362e086beSAneesh Kumar K.V if (!ret) 194462e086beSAneesh Kumar K.V ret = err; 194562e086beSAneesh Kumar K.V 194619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 194762e086beSAneesh Kumar K.V out: 1948bdf96838STheodore Ts'o unlock_page(page); 1949bdf96838STheodore Ts'o out_no_pagelock: 19503fdcfb66STao Ma brelse(inode_bh); 195162e086beSAneesh Kumar K.V return ret; 195262e086beSAneesh Kumar K.V } 195362e086beSAneesh Kumar K.V 195461628a3fSMingming Cao /* 195543ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 195643ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 195743ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 195843ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 195943ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 196043ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 196143ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 196243ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 196343ce1d23SAneesh Kumar K.V * 1964b920c755STheodore Ts'o * This function can get called via... 196520970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1966b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1967f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1968b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 196943ce1d23SAneesh Kumar K.V * 197043ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 197143ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 197243ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 197343ce1d23SAneesh Kumar K.V * truncate(f, 1024); 197443ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 197543ce1d23SAneesh Kumar K.V * a[0] = 'a'; 197643ce1d23SAneesh Kumar K.V * truncate(f, 4096); 197743ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 197890802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 197943ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 198043ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 198143ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 198243ce1d23SAneesh Kumar K.V * buffer_heads mapped. 198343ce1d23SAneesh Kumar K.V * 198443ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 198543ce1d23SAneesh Kumar K.V * unwritten in the page. 198643ce1d23SAneesh Kumar K.V * 198743ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 198843ce1d23SAneesh Kumar K.V * 198943ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 199043ce1d23SAneesh Kumar K.V * ext4_writepage() 199143ce1d23SAneesh Kumar K.V * 199243ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 199343ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 199461628a3fSMingming Cao */ 199543ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 199664769240SAlex Tomas struct writeback_control *wbc) 199764769240SAlex Tomas { 1998020df9baSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 1999f8bec370SJan Kara int ret = 0; 200061628a3fSMingming Cao loff_t size; 2001498e5f24STheodore Ts'o unsigned int len; 2002744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 200361628a3fSMingming Cao struct inode *inode = page->mapping->host; 200436ade451SJan Kara struct ext4_io_submit io_submit; 200564769240SAlex Tomas 20060db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 2007020df9baSMatthew Wilcox (Oracle) folio_invalidate(folio, 0, folio_size(folio)); 2008020df9baSMatthew Wilcox (Oracle) folio_unlock(folio); 20090db1ff22STheodore Ts'o return -EIO; 20100db1ff22STheodore Ts'o } 20110db1ff22STheodore Ts'o 2012a9c667f8SLukas Czerner trace_ext4_writepage(page); 201361628a3fSMingming Cao size = i_size_read(inode); 2014c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2015c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 201609cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 201761628a3fSMingming Cao else 201809cbfeafSKirill A. Shutemov len = PAGE_SIZE; 201961628a3fSMingming Cao 2020cc509574STheodore Ts'o /* Should never happen but for bugs in other kernel subsystems */ 2021cc509574STheodore Ts'o if (!page_has_buffers(page)) { 2022cc509574STheodore Ts'o ext4_warning_inode(inode, 2023cc509574STheodore Ts'o "page %lu does not have buffers attached", page->index); 2024cc509574STheodore Ts'o ClearPageDirty(page); 2025cc509574STheodore Ts'o unlock_page(page); 2026cc509574STheodore Ts'o return 0; 2027cc509574STheodore Ts'o } 2028cc509574STheodore Ts'o 2029f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 203064769240SAlex Tomas /* 2031fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2032fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2033fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2034fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2035fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2036cccd147aSTheodore Ts'o * 2037cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2038cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2039cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2040cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2041cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2042cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2043cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2044cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2045cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 204664769240SAlex Tomas */ 2047188c299eSJan Kara if (ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, NULL, 2048c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 204961628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2050cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 205109cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2052fe386132SJan Kara /* 2053fe386132SJan Kara * For memory cleaning there's no point in writing only 2054fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2055fe386132SJan Kara * from direct reclaim. 2056fe386132SJan Kara */ 2057fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2058fe386132SJan Kara == PF_MEMALLOC); 205961628a3fSMingming Cao unlock_page(page); 206061628a3fSMingming Cao return 0; 206161628a3fSMingming Cao } 2062f0e6c985SAneesh Kumar K.V } 206364769240SAlex Tomas 2064cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 206543ce1d23SAneesh Kumar K.V /* 206643ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 206743ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 206843ce1d23SAneesh Kumar K.V */ 20693f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 207043ce1d23SAneesh Kumar K.V 207197a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 207297a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 207397a851edSJan Kara if (!io_submit.io_end) { 207497a851edSJan Kara redirty_page_for_writepage(wbc, page); 207597a851edSJan Kara unlock_page(page); 207697a851edSJan Kara return -ENOMEM; 207797a851edSJan Kara } 2078dff4ac75SJan Kara ret = ext4_bio_write_page(&io_submit, page, len); 207936ade451SJan Kara ext4_io_submit(&io_submit); 208097a851edSJan Kara /* Drop io_end reference we got from init */ 208197a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 208264769240SAlex Tomas return ret; 208364769240SAlex Tomas } 208464769240SAlex Tomas 20855f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20865f1132b2SJan Kara { 20875f1132b2SJan Kara int len; 2088a056bdaaSJan Kara loff_t size; 20895f1132b2SJan Kara int err; 20905f1132b2SJan Kara 20915f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2092a056bdaaSJan Kara clear_page_dirty_for_io(page); 2093a056bdaaSJan Kara /* 2094a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2095a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2096a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2097a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2098a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2099a056bdaaSJan Kara * written to again until we release page lock. So only after 2100a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2101a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2102a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2103a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2104a056bdaaSJan Kara * after page tables are updated. 2105a056bdaaSJan Kara */ 2106a056bdaaSJan Kara size = i_size_read(mpd->inode); 2107c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2108c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 210909cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 21105f1132b2SJan Kara else 211109cbfeafSKirill A. Shutemov len = PAGE_SIZE; 2112dff4ac75SJan Kara err = ext4_bio_write_page(&mpd->io_submit, page, len); 21135f1132b2SJan Kara if (!err) 21145f1132b2SJan Kara mpd->wbc->nr_to_write--; 21155f1132b2SJan Kara mpd->first_page++; 21165f1132b2SJan Kara 21175f1132b2SJan Kara return err; 21185f1132b2SJan Kara } 21195f1132b2SJan Kara 21206db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 21214e7ea81dSJan Kara 212261628a3fSMingming Cao /* 2123fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2124fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2125fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 212661628a3fSMingming Cao */ 2127fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2128525f4ed8SMingming Cao 2129525f4ed8SMingming Cao /* 21304e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 21314e7ea81dSJan Kara * 21324e7ea81dSJan Kara * @mpd - extent of blocks 21334e7ea81dSJan Kara * @lblk - logical number of the block in the file 213409930042SJan Kara * @bh - buffer head we want to add to the extent 21354e7ea81dSJan Kara * 213609930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 213709930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 213809930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 213909930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 214009930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 214109930042SJan Kara * added. 21424e7ea81dSJan Kara */ 214309930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 214409930042SJan Kara struct buffer_head *bh) 21454e7ea81dSJan Kara { 21464e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21474e7ea81dSJan Kara 214809930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 214909930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 215009930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 215109930042SJan Kara /* So far no extent to map => we write the buffer right away */ 215209930042SJan Kara if (map->m_len == 0) 215309930042SJan Kara return true; 215409930042SJan Kara return false; 215509930042SJan Kara } 21564e7ea81dSJan Kara 21574e7ea81dSJan Kara /* First block in the extent? */ 21584e7ea81dSJan Kara if (map->m_len == 0) { 2159dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2160dddbd6acSJan Kara if (!mpd->do_map) 2161dddbd6acSJan Kara return false; 21624e7ea81dSJan Kara map->m_lblk = lblk; 21634e7ea81dSJan Kara map->m_len = 1; 216409930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 216509930042SJan Kara return true; 21664e7ea81dSJan Kara } 21674e7ea81dSJan Kara 216809930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 216909930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 217009930042SJan Kara return false; 217109930042SJan Kara 21724e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21734e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 217409930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21754e7ea81dSJan Kara map->m_len++; 217609930042SJan Kara return true; 21774e7ea81dSJan Kara } 217809930042SJan Kara return false; 21794e7ea81dSJan Kara } 21804e7ea81dSJan Kara 21815f1132b2SJan Kara /* 21825f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21835f1132b2SJan Kara * 21845f1132b2SJan Kara * @mpd - extent of blocks for mapping 21855f1132b2SJan Kara * @head - the first buffer in the page 21865f1132b2SJan Kara * @bh - buffer we should start processing from 21875f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21885f1132b2SJan Kara * 21895f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21905f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21915f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21925f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21935f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21945f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21955f1132b2SJan Kara * < 0 in case of error during IO submission. 21965f1132b2SJan Kara */ 21975f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21984e7ea81dSJan Kara struct buffer_head *head, 21994e7ea81dSJan Kara struct buffer_head *bh, 22004e7ea81dSJan Kara ext4_lblk_t lblk) 22014e7ea81dSJan Kara { 22024e7ea81dSJan Kara struct inode *inode = mpd->inode; 22035f1132b2SJan Kara int err; 220493407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 22054e7ea81dSJan Kara >> inode->i_blkbits; 22064e7ea81dSJan Kara 2207c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2208c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2209c93d8f88SEric Biggers 22104e7ea81dSJan Kara do { 22114e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 22124e7ea81dSJan Kara 221309930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 22144e7ea81dSJan Kara /* Found extent to map? */ 22154e7ea81dSJan Kara if (mpd->map.m_len) 22165f1132b2SJan Kara return 0; 2217dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2218dddbd6acSJan Kara if (!mpd->do_map) 2219dddbd6acSJan Kara return 0; 222009930042SJan Kara /* Everything mapped so far and we hit EOF */ 22215f1132b2SJan Kara break; 22224e7ea81dSJan Kara } 22234e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 22245f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 22255f1132b2SJan Kara if (mpd->map.m_len == 0) { 22265f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 22275f1132b2SJan Kara if (err < 0) 22284e7ea81dSJan Kara return err; 22294e7ea81dSJan Kara } 22306b8ed620SJan Kara if (lblk >= blocks) { 22316b8ed620SJan Kara mpd->scanned_until_end = 1; 22326b8ed620SJan Kara return 0; 22336b8ed620SJan Kara } 22346b8ed620SJan Kara return 1; 22355f1132b2SJan Kara } 22364e7ea81dSJan Kara 22374e7ea81dSJan Kara /* 22382943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22392943fdbcSRitesh Harjani * may submit fully mapped page for IO 22402943fdbcSRitesh Harjani * 22412943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22422943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22432943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22442943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22452943fdbcSRitesh Harjani * mapping or not. 22462943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22472943fdbcSRitesh Harjani * state according to new extent state. 22482943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22492943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22502943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22512943fdbcSRitesh Harjani */ 22522943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22532943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22542943fdbcSRitesh Harjani bool *map_bh) 22552943fdbcSRitesh Harjani { 22562943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22572943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22582943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22592943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22602943fdbcSRitesh Harjani int err = 0; 2261c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2262c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2263c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22642943fdbcSRitesh Harjani 22652943fdbcSRitesh Harjani bh = head = page_buffers(page); 22662943fdbcSRitesh Harjani do { 22672943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22682943fdbcSRitesh Harjani continue; 22692943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22702943fdbcSRitesh Harjani /* 22712943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22722943fdbcSRitesh Harjani * Find next buffer in the page to map. 22732943fdbcSRitesh Harjani */ 22742943fdbcSRitesh Harjani mpd->map.m_len = 0; 22752943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2276c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 22772943fdbcSRitesh Harjani 22782943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22792943fdbcSRitesh Harjani if (err > 0) 22802943fdbcSRitesh Harjani err = 0; 2281c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2282c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22834d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22844d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22854d06bfb9SRitesh Harjani goto out; 22864d06bfb9SRitesh Harjani } 2287d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2288c8cc8816SRitesh Harjani } 22892943fdbcSRitesh Harjani *map_bh = true; 22902943fdbcSRitesh Harjani goto out; 22912943fdbcSRitesh Harjani } 22922943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22932943fdbcSRitesh Harjani clear_buffer_delay(bh); 22942943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22952943fdbcSRitesh Harjani } 22962943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2297c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22982943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2299c8cc8816SRitesh Harjani 2300c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 23012943fdbcSRitesh Harjani *map_bh = false; 23022943fdbcSRitesh Harjani out: 23032943fdbcSRitesh Harjani *m_lblk = lblk; 23042943fdbcSRitesh Harjani *m_pblk = pblock; 23052943fdbcSRitesh Harjani return err; 23062943fdbcSRitesh Harjani } 23072943fdbcSRitesh Harjani 23082943fdbcSRitesh Harjani /* 23094e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 23104e7ea81dSJan Kara * submit fully mapped pages for IO 23114e7ea81dSJan Kara * 23124e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 23134e7ea81dSJan Kara * 23144e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 23154e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 23164e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2317556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 23184e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 23194e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 23204e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 23214e7ea81dSJan Kara */ 23224e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 23234e7ea81dSJan Kara { 23247530d093SMatthew Wilcox (Oracle) struct folio_batch fbatch; 23257530d093SMatthew Wilcox (Oracle) unsigned nr, i; 23264e7ea81dSJan Kara struct inode *inode = mpd->inode; 232709cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 23284e7ea81dSJan Kara pgoff_t start, end; 23294e7ea81dSJan Kara ext4_lblk_t lblk; 23302943fdbcSRitesh Harjani ext4_fsblk_t pblock; 23314e7ea81dSJan Kara int err; 23322943fdbcSRitesh Harjani bool map_bh = false; 23334e7ea81dSJan Kara 23344e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23354e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23364e7ea81dSJan Kara lblk = start << bpp_bits; 23374e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23384e7ea81dSJan Kara 23397530d093SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 23404e7ea81dSJan Kara while (start <= end) { 23417530d093SMatthew Wilcox (Oracle) nr = filemap_get_folios(inode->i_mapping, &start, end, &fbatch); 23427530d093SMatthew Wilcox (Oracle) if (nr == 0) 23434e7ea81dSJan Kara break; 23447530d093SMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) { 23457530d093SMatthew Wilcox (Oracle) struct page *page = &fbatch.folios[i]->page; 23464e7ea81dSJan Kara 23472943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23482943fdbcSRitesh Harjani &map_bh); 23494e7ea81dSJan Kara /* 23502943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23512943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23522943fdbcSRitesh Harjani * So we return to call further extent mapping. 23534e7ea81dSJan Kara */ 235439c0ae16SJason Yan if (err < 0 || map_bh) 23552943fdbcSRitesh Harjani goto out; 23564e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23574e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23582943fdbcSRitesh Harjani if (err < 0) 23592943fdbcSRitesh Harjani goto out; 23604e7ea81dSJan Kara } 23617530d093SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 23624e7ea81dSJan Kara } 23634e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23644e7ea81dSJan Kara mpd->map.m_len = 0; 23654e7ea81dSJan Kara mpd->map.m_flags = 0; 23664e7ea81dSJan Kara return 0; 23672943fdbcSRitesh Harjani out: 23687530d093SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 23692943fdbcSRitesh Harjani return err; 23704e7ea81dSJan Kara } 23714e7ea81dSJan Kara 23724e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23734e7ea81dSJan Kara { 23744e7ea81dSJan Kara struct inode *inode = mpd->inode; 23754e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23764e7ea81dSJan Kara int get_blocks_flags; 2377090f32eeSLukas Czerner int err, dioread_nolock; 23784e7ea81dSJan Kara 23794e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23804e7ea81dSJan Kara /* 23814e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2382556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23834e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23844e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23854e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23864e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23874e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23884e7ea81dSJan Kara * possible. 23894e7ea81dSJan Kara * 2390754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2391754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2392754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2393754cfed6STheodore Ts'o * the data was copied into the page cache. 23944e7ea81dSJan Kara */ 23954e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2396ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2397ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2398090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2399090f32eeSLukas Czerner if (dioread_nolock) 24004e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 24016db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 24024e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 24034e7ea81dSJan Kara 24044e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 24054e7ea81dSJan Kara if (err < 0) 24064e7ea81dSJan Kara return err; 2407090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 24086b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 24096b523df4SJan Kara ext4_handle_valid(handle)) { 24106b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 24116b523df4SJan Kara handle->h_rsv_handle = NULL; 24126b523df4SJan Kara } 24133613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 24146b523df4SJan Kara } 24154e7ea81dSJan Kara 24164e7ea81dSJan Kara BUG_ON(map->m_len == 0); 24174e7ea81dSJan Kara return 0; 24184e7ea81dSJan Kara } 24194e7ea81dSJan Kara 24204e7ea81dSJan Kara /* 24214e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 24224e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 24234e7ea81dSJan Kara * 24244e7ea81dSJan Kara * @handle - handle for journal operations 24254e7ea81dSJan Kara * @mpd - extent to map 24267534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 24277534e854SJan Kara * is no hope of writing the data. The caller should discard 24287534e854SJan Kara * dirty pages to avoid infinite loops. 24294e7ea81dSJan Kara * 24304e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 24314e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 24324e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 24334e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24344e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24354e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24364e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24374e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24384e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24394e7ea81dSJan Kara */ 24404e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2441cb530541STheodore Ts'o struct mpage_da_data *mpd, 2442cb530541STheodore Ts'o bool *give_up_on_write) 24434e7ea81dSJan Kara { 24444e7ea81dSJan Kara struct inode *inode = mpd->inode; 24454e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24464e7ea81dSJan Kara int err; 24474e7ea81dSJan Kara loff_t disksize; 24486603120eSDmitry Monakhov int progress = 0; 2449c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24504d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24514e7ea81dSJan Kara 24524d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24534d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24544d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2455c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 245627d7c4edSJan Kara do { 24574e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24584e7ea81dSJan Kara if (err < 0) { 24594e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24604e7ea81dSJan Kara 24610db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24629b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2463cb530541STheodore Ts'o goto invalidate_dirty_pages; 24644e7ea81dSJan Kara /* 2465cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2466cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2467cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24684e7ea81dSJan Kara */ 2469cb530541STheodore Ts'o if ((err == -ENOMEM) || 24706603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24716603120eSDmitry Monakhov if (progress) 24726603120eSDmitry Monakhov goto update_disksize; 2473cb530541STheodore Ts'o return err; 24746603120eSDmitry Monakhov } 24754e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24764e7ea81dSJan Kara "Delayed block allocation failed for " 24774e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24784e7ea81dSJan Kara " max blocks %u with error %d", 24794e7ea81dSJan Kara inode->i_ino, 24804e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2481cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24824e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24834e7ea81dSJan Kara "This should not happen!! Data will " 24844e7ea81dSJan Kara "be lost\n"); 24854e7ea81dSJan Kara if (err == -ENOSPC) 24864e7ea81dSJan Kara ext4_print_free_blocks(inode); 2487cb530541STheodore Ts'o invalidate_dirty_pages: 2488cb530541STheodore Ts'o *give_up_on_write = true; 24894e7ea81dSJan Kara return err; 24904e7ea81dSJan Kara } 24916603120eSDmitry Monakhov progress = 1; 24924e7ea81dSJan Kara /* 24934e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24944e7ea81dSJan Kara * extent to map 24954e7ea81dSJan Kara */ 24964e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24974e7ea81dSJan Kara if (err < 0) 24986603120eSDmitry Monakhov goto update_disksize; 249927d7c4edSJan Kara } while (map->m_len); 25004e7ea81dSJan Kara 25016603120eSDmitry Monakhov update_disksize: 2502622cad13STheodore Ts'o /* 2503622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2504622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2505622cad13STheodore Ts'o */ 250609cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 250735df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 25084e7ea81dSJan Kara int err2; 2509622cad13STheodore Ts'o loff_t i_size; 25104e7ea81dSJan Kara 2511622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2512622cad13STheodore Ts'o i_size = i_size_read(inode); 2513622cad13STheodore Ts'o if (disksize > i_size) 2514622cad13STheodore Ts'o disksize = i_size; 2515622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2516622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2517622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2518b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2519878520acSTheodore Ts'o if (err2) { 252054d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 25214e7ea81dSJan Kara "Failed to mark inode %lu dirty", 25224e7ea81dSJan Kara inode->i_ino); 2523878520acSTheodore Ts'o } 25244e7ea81dSJan Kara if (!err) 25254e7ea81dSJan Kara err = err2; 25264e7ea81dSJan Kara } 25274e7ea81dSJan Kara return err; 25284e7ea81dSJan Kara } 25294e7ea81dSJan Kara 25304e7ea81dSJan Kara /* 2531fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 253220970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2533fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2534fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2535fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2536525f4ed8SMingming Cao */ 2537fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2538fffb2739SJan Kara { 2539fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2540525f4ed8SMingming Cao 2541fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2542fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2543525f4ed8SMingming Cao } 254461628a3fSMingming Cao 2545de0039f6SJan Kara /* Return true if the page needs to be written as part of transaction commit */ 2546de0039f6SJan Kara static bool ext4_page_nomap_can_writeout(struct page *page) 2547de0039f6SJan Kara { 2548de0039f6SJan Kara struct buffer_head *bh, *head; 2549de0039f6SJan Kara 2550de0039f6SJan Kara bh = head = page_buffers(page); 2551de0039f6SJan Kara do { 2552de0039f6SJan Kara if (buffer_dirty(bh) && buffer_mapped(bh) && !buffer_delay(bh)) 2553de0039f6SJan Kara return true; 2554de0039f6SJan Kara } while ((bh = bh->b_this_page) != head); 2555de0039f6SJan Kara return false; 2556de0039f6SJan Kara } 2557de0039f6SJan Kara 25588e48dcfbSTheodore Ts'o /* 25594e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 2560de0039f6SJan Kara * needing mapping, submit mapped pages 25614e7ea81dSJan Kara * 25624e7ea81dSJan Kara * @mpd - where to look for pages 25634e7ea81dSJan Kara * 25644e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 2565de0039f6SJan Kara * IO immediately. If we cannot map blocks, we submit just already mapped 2566de0039f6SJan Kara * buffers in the page for IO and keep page dirty. When we can map blocks and 2567de0039f6SJan Kara * we find a page which isn't mapped we start accumulating extent of buffers 2568de0039f6SJan Kara * underlying these pages that needs mapping (formed by either delayed or 2569de0039f6SJan Kara * unwritten buffers). We also lock the pages containing these buffers. The 2570de0039f6SJan Kara * extent found is returned in @mpd structure (starting at mpd->lblk with 2571de0039f6SJan Kara * length mpd->len blocks). 25724e7ea81dSJan Kara * 25734e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25744e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25754e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25764e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25778e48dcfbSTheodore Ts'o */ 25784e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25798e48dcfbSTheodore Ts'o { 25804e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 258150ead253SVishal Moola (Oracle) struct folio_batch fbatch; 258250ead253SVishal Moola (Oracle) unsigned int nr_folios; 25834e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25844e7ea81dSJan Kara pgoff_t end = mpd->last_page; 258510bbd235SMatthew Wilcox xa_mark_t tag; 25864e7ea81dSJan Kara int i, err = 0; 25874e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25884e7ea81dSJan Kara ext4_lblk_t lblk; 25894e7ea81dSJan Kara struct buffer_head *head; 25908e48dcfbSTheodore Ts'o 25914e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25925b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25935b41d924SEric Sandeen else 25945b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 259550ead253SVishal Moola (Oracle) folio_batch_init(&fbatch); 25964e7ea81dSJan Kara mpd->map.m_len = 0; 25974e7ea81dSJan Kara mpd->next_page = index; 25984f01b02cSTheodore Ts'o while (index <= end) { 259950ead253SVishal Moola (Oracle) nr_folios = filemap_get_folios_tag(mapping, &index, end, 260050ead253SVishal Moola (Oracle) tag, &fbatch); 260150ead253SVishal Moola (Oracle) if (nr_folios == 0) 26026b8ed620SJan Kara break; 26038e48dcfbSTheodore Ts'o 260450ead253SVishal Moola (Oracle) for (i = 0; i < nr_folios; i++) { 260550ead253SVishal Moola (Oracle) struct folio *folio = fbatch.folios[i]; 26068e48dcfbSTheodore Ts'o 26078e48dcfbSTheodore Ts'o /* 2608aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2609aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2610aeac589aSMing Lei * keep going because someone may be concurrently 2611aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2612aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2613aeac589aSMing Lei * of the old dirty pages. 2614aeac589aSMing Lei */ 2615*c8e8e16dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_NONE && 2616*c8e8e16dSJan Kara mpd->wbc->nr_to_write <= 2617*c8e8e16dSJan Kara mpd->map.m_len >> (PAGE_SHIFT - blkbits)) 2618aeac589aSMing Lei goto out; 2619aeac589aSMing Lei 26204e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 262150ead253SVishal Moola (Oracle) if (mpd->map.m_len > 0 && mpd->next_page != folio->index) 26224e7ea81dSJan Kara goto out; 262378aaced3STheodore Ts'o 262450ead253SVishal Moola (Oracle) folio_lock(folio); 26258e48dcfbSTheodore Ts'o /* 26264e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 26274e7ea81dSJan Kara * longer corresponds to inode we are writing (which 26284e7ea81dSJan Kara * means it has been truncated or invalidated), or the 26294e7ea81dSJan Kara * page is already under writeback and we are not doing 26304e7ea81dSJan Kara * a data integrity writeback, skip the page 26318e48dcfbSTheodore Ts'o */ 263250ead253SVishal Moola (Oracle) if (!folio_test_dirty(folio) || 263350ead253SVishal Moola (Oracle) (folio_test_writeback(folio) && 26344e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 263550ead253SVishal Moola (Oracle) unlikely(folio->mapping != mapping)) { 263650ead253SVishal Moola (Oracle) folio_unlock(folio); 26378e48dcfbSTheodore Ts'o continue; 26388e48dcfbSTheodore Ts'o } 26398e48dcfbSTheodore Ts'o 264050ead253SVishal Moola (Oracle) folio_wait_writeback(folio); 264150ead253SVishal Moola (Oracle) BUG_ON(folio_test_writeback(folio)); 26428e48dcfbSTheodore Ts'o 2643cc509574STheodore Ts'o /* 2644cc509574STheodore Ts'o * Should never happen but for buggy code in 2645cc509574STheodore Ts'o * other subsystems that call 2646cc509574STheodore Ts'o * set_page_dirty() without properly warning 2647cc509574STheodore Ts'o * the file system first. See [1] for more 2648cc509574STheodore Ts'o * information. 2649cc509574STheodore Ts'o * 2650cc509574STheodore Ts'o * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz 2651cc509574STheodore Ts'o */ 265250ead253SVishal Moola (Oracle) if (!folio_buffers(folio)) { 265350ead253SVishal Moola (Oracle) ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", folio->index); 265450ead253SVishal Moola (Oracle) folio_clear_dirty(folio); 265550ead253SVishal Moola (Oracle) folio_unlock(folio); 2656cc509574STheodore Ts'o continue; 2657cc509574STheodore Ts'o } 2658cc509574STheodore Ts'o 26594e7ea81dSJan Kara if (mpd->map.m_len == 0) 266050ead253SVishal Moola (Oracle) mpd->first_page = folio->index; 266150ead253SVishal Moola (Oracle) mpd->next_page = folio->index + folio_nr_pages(folio); 2662de0039f6SJan Kara /* 2663de0039f6SJan Kara * Writeout for transaction commit where we cannot 2664de0039f6SJan Kara * modify metadata is simple. Just submit the page. 2665de0039f6SJan Kara */ 2666de0039f6SJan Kara if (!mpd->can_map) { 266750ead253SVishal Moola (Oracle) if (ext4_page_nomap_can_writeout(&folio->page)) { 266850ead253SVishal Moola (Oracle) err = mpage_submit_page(mpd, &folio->page); 2669de0039f6SJan Kara if (err < 0) 2670de0039f6SJan Kara goto out; 2671de0039f6SJan Kara } else { 267250ead253SVishal Moola (Oracle) folio_unlock(folio); 267350ead253SVishal Moola (Oracle) mpd->first_page += folio_nr_pages(folio); 2674de0039f6SJan Kara } 2675de0039f6SJan Kara } else { 2676f8bec370SJan Kara /* Add all dirty buffers to mpd */ 267750ead253SVishal Moola (Oracle) lblk = ((ext4_lblk_t)folio->index) << 267809cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 267950ead253SVishal Moola (Oracle) head = folio_buffers(folio); 2680de0039f6SJan Kara err = mpage_process_page_bufs(mpd, head, head, 2681de0039f6SJan Kara lblk); 26825f1132b2SJan Kara if (err <= 0) 26834e7ea81dSJan Kara goto out; 26845f1132b2SJan Kara err = 0; 2685de0039f6SJan Kara } 26868e48dcfbSTheodore Ts'o } 268750ead253SVishal Moola (Oracle) folio_batch_release(&fbatch); 26888e48dcfbSTheodore Ts'o cond_resched(); 26898e48dcfbSTheodore Ts'o } 26906b8ed620SJan Kara mpd->scanned_until_end = 1; 26914f01b02cSTheodore Ts'o return 0; 26928eb9e5ceSTheodore Ts'o out: 269350ead253SVishal Moola (Oracle) folio_batch_release(&fbatch); 26944e7ea81dSJan Kara return err; 26958e48dcfbSTheodore Ts'o } 26968e48dcfbSTheodore Ts'o 2697d585bdbeSMatthew Wilcox (Oracle) static int ext4_writepage_cb(struct folio *folio, struct writeback_control *wbc, 269849977f97SJan Kara void *data) 269964769240SAlex Tomas { 2700d585bdbeSMatthew Wilcox (Oracle) return ext4_writepage(&folio->page, wbc); 270149977f97SJan Kara } 270249977f97SJan Kara 270315648d59SJan Kara static int ext4_do_writepages(struct mpage_da_data *mpd) 270464769240SAlex Tomas { 270515648d59SJan Kara struct writeback_control *wbc = mpd->wbc; 27064e7ea81dSJan Kara pgoff_t writeback_index = 0; 27074e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 270822208dedSAneesh Kumar K.V int range_whole = 0; 27094e7ea81dSJan Kara int cycled = 1; 271061628a3fSMingming Cao handle_t *handle = NULL; 271115648d59SJan Kara struct inode *inode = mpd->inode; 271215648d59SJan Kara struct address_space *mapping = inode->i_mapping; 27136b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 27145e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 27151bce63d1SShaohua Li struct blk_plug plug; 2716cb530541STheodore Ts'o bool give_up_on_write = false; 271761628a3fSMingming Cao 271820970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2719ba80b101STheodore Ts'o 272061628a3fSMingming Cao /* 272161628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 272261628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 272361628a3fSMingming Cao * because that could violate lock ordering on umount 272461628a3fSMingming Cao */ 2725a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2726bbf023c7SMing Lei goto out_writepages; 27272a21e37eSTheodore Ts'o 272820970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 272949977f97SJan Kara blk_start_plug(&plug); 273049977f97SJan Kara ret = write_cache_pages(mapping, wbc, ext4_writepage_cb, NULL); 273149977f97SJan Kara blk_finish_plug(&plug); 2732bbf023c7SMing Lei goto out_writepages; 273320970ba6STheodore Ts'o } 273420970ba6STheodore Ts'o 27352a21e37eSTheodore Ts'o /* 27362a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 27372a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 27382a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 27391751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 27402a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 274120970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 27422a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 27432a21e37eSTheodore Ts'o * the stack trace. 27442a21e37eSTheodore Ts'o */ 27450db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 27469b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2747bbf023c7SMing Lei ret = -EROFS; 2748bbf023c7SMing Lei goto out_writepages; 2749bbf023c7SMing Lei } 27502a21e37eSTheodore Ts'o 27514e7ea81dSJan Kara /* 27524e7ea81dSJan Kara * If we have inline data and arrive here, it means that 27534e7ea81dSJan Kara * we will soon create the block for the 1st page, so 27544e7ea81dSJan Kara * we'd better clear the inline data here. 27554e7ea81dSJan Kara */ 27564e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 27574e7ea81dSJan Kara /* Just inode will be modified... */ 27584e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 27594e7ea81dSJan Kara if (IS_ERR(handle)) { 27604e7ea81dSJan Kara ret = PTR_ERR(handle); 27614e7ea81dSJan Kara goto out_writepages; 27624e7ea81dSJan Kara } 27634e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 27644e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 27654e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 27664e7ea81dSJan Kara ext4_journal_stop(handle); 27674e7ea81dSJan Kara } 27684e7ea81dSJan Kara 27694e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 27704e343231Syangerkun /* 27714e343231Syangerkun * We may need to convert up to one extent per block in 27724e343231Syangerkun * the page and we may dirty the inode. 27734e343231Syangerkun */ 27744e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 27754e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 27764e343231Syangerkun } 27774e343231Syangerkun 277822208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 277922208dedSAneesh Kumar K.V range_whole = 1; 278061628a3fSMingming Cao 27812acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 27824e7ea81dSJan Kara writeback_index = mapping->writeback_index; 27834e7ea81dSJan Kara if (writeback_index) 27842acf2c26SAneesh Kumar K.V cycled = 0; 278515648d59SJan Kara mpd->first_page = writeback_index; 278615648d59SJan Kara mpd->last_page = -1; 27875b41d924SEric Sandeen } else { 278815648d59SJan Kara mpd->first_page = wbc->range_start >> PAGE_SHIFT; 278915648d59SJan Kara mpd->last_page = wbc->range_end >> PAGE_SHIFT; 27905b41d924SEric Sandeen } 2791a1d6cc56SAneesh Kumar K.V 279215648d59SJan Kara ext4_io_submit_init(&mpd->io_submit, wbc); 27932acf2c26SAneesh Kumar K.V retry: 27946e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 279515648d59SJan Kara tag_pages_for_writeback(mapping, mpd->first_page, 279615648d59SJan Kara mpd->last_page); 27971bce63d1SShaohua Li blk_start_plug(&plug); 2798dddbd6acSJan Kara 2799dddbd6acSJan Kara /* 2800dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2801dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2802dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2803dddbd6acSJan Kara * started. 2804dddbd6acSJan Kara */ 280515648d59SJan Kara mpd->do_map = 0; 280615648d59SJan Kara mpd->scanned_until_end = 0; 280715648d59SJan Kara mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 280815648d59SJan Kara if (!mpd->io_submit.io_end) { 2809dddbd6acSJan Kara ret = -ENOMEM; 2810dddbd6acSJan Kara goto unplug; 2811dddbd6acSJan Kara } 281215648d59SJan Kara ret = mpage_prepare_extent_to_map(mpd); 2813a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 281415648d59SJan Kara mpage_release_unused_pages(mpd, false); 2815dddbd6acSJan Kara /* Submit prepared bio */ 281615648d59SJan Kara ext4_io_submit(&mpd->io_submit); 281715648d59SJan Kara ext4_put_io_end_defer(mpd->io_submit.io_end); 281815648d59SJan Kara mpd->io_submit.io_end = NULL; 2819dddbd6acSJan Kara if (ret < 0) 2820dddbd6acSJan Kara goto unplug; 2821dddbd6acSJan Kara 282215648d59SJan Kara while (!mpd->scanned_until_end && wbc->nr_to_write > 0) { 28234e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 282415648d59SJan Kara mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 282515648d59SJan Kara if (!mpd->io_submit.io_end) { 28264e7ea81dSJan Kara ret = -ENOMEM; 28274e7ea81dSJan Kara break; 28284e7ea81dSJan Kara } 2829a1d6cc56SAneesh Kumar K.V 2830de0039f6SJan Kara WARN_ON_ONCE(!mpd->can_map); 2831a1d6cc56SAneesh Kumar K.V /* 28324e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 28334e7ea81dSJan Kara * must always write out whole page (makes a difference when 28344e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 28354e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 28364e7ea81dSJan Kara * not supported by delalloc. 2837a1d6cc56SAneesh Kumar K.V */ 2838a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2839525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2840a1d6cc56SAneesh Kumar K.V 284161628a3fSMingming Cao /* start a new transaction */ 28426b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 28436b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 284461628a3fSMingming Cao if (IS_ERR(handle)) { 284561628a3fSMingming Cao ret = PTR_ERR(handle); 28461693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2847fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2848a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 28494e7ea81dSJan Kara /* Release allocated io_end */ 285015648d59SJan Kara ext4_put_io_end(mpd->io_submit.io_end); 285115648d59SJan Kara mpd->io_submit.io_end = NULL; 28524e7ea81dSJan Kara break; 285361628a3fSMingming Cao } 285415648d59SJan Kara mpd->do_map = 1; 2855f63e6005STheodore Ts'o 285615648d59SJan Kara trace_ext4_da_write_pages(inode, mpd->first_page, wbc); 285715648d59SJan Kara ret = mpage_prepare_extent_to_map(mpd); 285815648d59SJan Kara if (!ret && mpd->map.m_len) 285915648d59SJan Kara ret = mpage_map_and_submit_extent(handle, mpd, 2860cb530541STheodore Ts'o &give_up_on_write); 2861646caa9cSJan Kara /* 2862646caa9cSJan Kara * Caution: If the handle is synchronous, 2863646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2864646caa9cSJan Kara * to finish which may depend on writeback of pages to 2865646caa9cSJan Kara * complete or on page lock to be released. In that 2866b483bb77SRandy Dunlap * case, we have to wait until after we have 2867646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2868646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2869646caa9cSJan Kara * to be able to complete) before stopping the handle. 2870646caa9cSJan Kara */ 2871646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 287261628a3fSMingming Cao ext4_journal_stop(handle); 2873646caa9cSJan Kara handle = NULL; 287415648d59SJan Kara mpd->do_map = 0; 2875646caa9cSJan Kara } 28764e7ea81dSJan Kara /* Unlock pages we didn't use */ 287715648d59SJan Kara mpage_release_unused_pages(mpd, give_up_on_write); 2878a297b2fcSXiaoguang Wang /* Submit prepared bio */ 287915648d59SJan Kara ext4_io_submit(&mpd->io_submit); 2880a297b2fcSXiaoguang Wang 2881646caa9cSJan Kara /* 2882646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2883646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2884646caa9cSJan Kara * we are still holding the transaction as we can 2885646caa9cSJan Kara * release the last reference to io_end which may end 2886646caa9cSJan Kara * up doing unwritten extent conversion. 2887646caa9cSJan Kara */ 2888646caa9cSJan Kara if (handle) { 288915648d59SJan Kara ext4_put_io_end_defer(mpd->io_submit.io_end); 2890646caa9cSJan Kara ext4_journal_stop(handle); 2891646caa9cSJan Kara } else 289215648d59SJan Kara ext4_put_io_end(mpd->io_submit.io_end); 289315648d59SJan Kara mpd->io_submit.io_end = NULL; 2894df22291fSAneesh Kumar K.V 28954e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28964e7ea81dSJan Kara /* 28974e7ea81dSJan Kara * Commit the transaction which would 289822208dedSAneesh Kumar K.V * free blocks released in the transaction 289922208dedSAneesh Kumar K.V * and try again 290022208dedSAneesh Kumar K.V */ 2901df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 290222208dedSAneesh Kumar K.V ret = 0; 29034e7ea81dSJan Kara continue; 29044e7ea81dSJan Kara } 29054e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 29064e7ea81dSJan Kara if (ret) 290761628a3fSMingming Cao break; 290861628a3fSMingming Cao } 2909dddbd6acSJan Kara unplug: 29101bce63d1SShaohua Li blk_finish_plug(&plug); 29119c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 29122acf2c26SAneesh Kumar K.V cycled = 1; 291315648d59SJan Kara mpd->last_page = writeback_index - 1; 291415648d59SJan Kara mpd->first_page = 0; 29152acf2c26SAneesh Kumar K.V goto retry; 29162acf2c26SAneesh Kumar K.V } 291761628a3fSMingming Cao 291822208dedSAneesh Kumar K.V /* Update index */ 291922208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 292022208dedSAneesh Kumar K.V /* 29214e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 292222208dedSAneesh Kumar K.V * mode will write it back later 292322208dedSAneesh Kumar K.V */ 292415648d59SJan Kara mapping->writeback_index = mpd->first_page; 2925a1d6cc56SAneesh Kumar K.V 292661628a3fSMingming Cao out_writepages: 292720970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 29284e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 292961628a3fSMingming Cao return ret; 293064769240SAlex Tomas } 293164769240SAlex Tomas 293215648d59SJan Kara static int ext4_writepages(struct address_space *mapping, 293315648d59SJan Kara struct writeback_control *wbc) 293415648d59SJan Kara { 293529bc9ceaSJan Kara struct super_block *sb = mapping->host->i_sb; 293615648d59SJan Kara struct mpage_da_data mpd = { 293715648d59SJan Kara .inode = mapping->host, 293815648d59SJan Kara .wbc = wbc, 293915648d59SJan Kara .can_map = 1, 294015648d59SJan Kara }; 294129bc9ceaSJan Kara int ret; 294215648d59SJan Kara 294329bc9ceaSJan Kara if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 294429bc9ceaSJan Kara return -EIO; 294529bc9ceaSJan Kara 294629bc9ceaSJan Kara percpu_down_read(&EXT4_SB(sb)->s_writepages_rwsem); 294729bc9ceaSJan Kara ret = ext4_do_writepages(&mpd); 294829bc9ceaSJan Kara percpu_up_read(&EXT4_SB(sb)->s_writepages_rwsem); 294929bc9ceaSJan Kara 295029bc9ceaSJan Kara return ret; 295115648d59SJan Kara } 295215648d59SJan Kara 295359205c8dSJan Kara int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode) 295459205c8dSJan Kara { 295559205c8dSJan Kara struct writeback_control wbc = { 295659205c8dSJan Kara .sync_mode = WB_SYNC_ALL, 295759205c8dSJan Kara .nr_to_write = LONG_MAX, 295859205c8dSJan Kara .range_start = jinode->i_dirty_start, 295959205c8dSJan Kara .range_end = jinode->i_dirty_end, 296059205c8dSJan Kara }; 296159205c8dSJan Kara struct mpage_da_data mpd = { 296259205c8dSJan Kara .inode = jinode->i_vfs_inode, 296359205c8dSJan Kara .wbc = &wbc, 296459205c8dSJan Kara .can_map = 0, 296559205c8dSJan Kara }; 296659205c8dSJan Kara return ext4_do_writepages(&mpd); 296759205c8dSJan Kara } 296859205c8dSJan Kara 29695f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 29705f0663bbSDan Williams struct writeback_control *wbc) 29715f0663bbSDan Williams { 29725f0663bbSDan Williams int ret; 29735f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 29745f0663bbSDan Williams struct inode *inode = mapping->host; 29755f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 29765f0663bbSDan Williams 29775f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29785f0663bbSDan Williams return -EIO; 29795f0663bbSDan Williams 2980bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 29815f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 29825f0663bbSDan Williams 29833f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 29845f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 29855f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2986bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 29875f0663bbSDan Williams return ret; 29885f0663bbSDan Williams } 29895f0663bbSDan Williams 299079f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 299179f0be8dSAneesh Kumar K.V { 29925c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 299379f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 299479f0be8dSAneesh Kumar K.V 299579f0be8dSAneesh Kumar K.V /* 299679f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 299779f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2998179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 299979f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 300079f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 300179f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 300279f0be8dSAneesh Kumar K.V */ 30035c1ff336SEric Whitney free_clusters = 30045c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 30055c1ff336SEric Whitney dirty_clusters = 30065c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 300700d4e736STheodore Ts'o /* 300800d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 300900d4e736STheodore Ts'o */ 30105c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 301110ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 301200d4e736STheodore Ts'o 30135c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 30145c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 301579f0be8dSAneesh Kumar K.V /* 3016c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 3017c8afb446SEric Sandeen * or free blocks is less than watermark 301879f0be8dSAneesh Kumar K.V */ 301979f0be8dSAneesh Kumar K.V return 1; 302079f0be8dSAneesh Kumar K.V } 302179f0be8dSAneesh Kumar K.V return 0; 302279f0be8dSAneesh Kumar K.V } 302379f0be8dSAneesh Kumar K.V 302464769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 30259d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 302664769240SAlex Tomas struct page **pagep, void **fsdata) 302764769240SAlex Tomas { 302872b8ab9dSEric Sandeen int ret, retries = 0; 302964769240SAlex Tomas struct page *page; 303064769240SAlex Tomas pgoff_t index; 303164769240SAlex Tomas struct inode *inode = mapping->host; 303264769240SAlex Tomas 30330db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 30340db1ff22STheodore Ts'o return -EIO; 30350db1ff22STheodore Ts'o 303609cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 303779f0be8dSAneesh Kumar K.V 30386493792dSZhang Yi if (ext4_nonda_switch(inode->i_sb) || ext4_verity_in_progress(inode)) { 303979f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 304079f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 30419d6b0cd7SMatthew Wilcox (Oracle) len, pagep, fsdata); 304279f0be8dSAneesh Kumar K.V } 304379f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 30449d6b0cd7SMatthew Wilcox (Oracle) trace_ext4_da_write_begin(inode, pos, len); 30459c3569b5STao Ma 30469c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 304736d116e9SMatthew Wilcox (Oracle) ret = ext4_da_write_inline_data_begin(mapping, inode, pos, len, 30489c3569b5STao Ma pagep, fsdata); 30499c3569b5STao Ma if (ret < 0) 305047564bfbSTheodore Ts'o return ret; 305147564bfbSTheodore Ts'o if (ret == 1) 305247564bfbSTheodore Ts'o return 0; 30539c3569b5STao Ma } 30549c3569b5STao Ma 3055cc883236SZhang Yi retry: 3056b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 305747564bfbSTheodore Ts'o if (!page) 305847564bfbSTheodore Ts'o return -ENOMEM; 305947564bfbSTheodore Ts'o 306047564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 30617afe5aa5SDmitry Monakhov wait_for_stable_page(page); 306264769240SAlex Tomas 3063643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 30642058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 30652058f83aSMichael Halcrow ext4_da_get_block_prep); 30662058f83aSMichael Halcrow #else 30676e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 30682058f83aSMichael Halcrow #endif 306964769240SAlex Tomas if (ret < 0) { 307064769240SAlex Tomas unlock_page(page); 3071cc883236SZhang Yi put_page(page); 3072ae4d5372SAneesh Kumar K.V /* 3073ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3074ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3075cc883236SZhang Yi * i_size_read because we hold inode lock. 3076ae4d5372SAneesh Kumar K.V */ 3077ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3078b9a4207dSJan Kara ext4_truncate_failed_write(inode); 307947564bfbSTheodore Ts'o 308047564bfbSTheodore Ts'o if (ret == -ENOSPC && 308147564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 3082cc883236SZhang Yi goto retry; 308347564bfbSTheodore Ts'o return ret; 308464769240SAlex Tomas } 308564769240SAlex Tomas 308647564bfbSTheodore Ts'o *pagep = page; 308764769240SAlex Tomas return ret; 308864769240SAlex Tomas } 308964769240SAlex Tomas 3090632eaeabSMingming Cao /* 3091632eaeabSMingming Cao * Check if we should update i_disksize 3092632eaeabSMingming Cao * when write to the end of file but not require block allocation 3093632eaeabSMingming Cao */ 3094632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3095632eaeabSMingming Cao unsigned long offset) 3096632eaeabSMingming Cao { 3097632eaeabSMingming Cao struct buffer_head *bh; 3098632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3099632eaeabSMingming Cao unsigned int idx; 3100632eaeabSMingming Cao int i; 3101632eaeabSMingming Cao 3102632eaeabSMingming Cao bh = page_buffers(page); 3103632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3104632eaeabSMingming Cao 3105632eaeabSMingming Cao for (i = 0; i < idx; i++) 3106632eaeabSMingming Cao bh = bh->b_this_page; 3107632eaeabSMingming Cao 310829fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3109632eaeabSMingming Cao return 0; 3110632eaeabSMingming Cao return 1; 3111632eaeabSMingming Cao } 3112632eaeabSMingming Cao 311364769240SAlex Tomas static int ext4_da_write_end(struct file *file, 311464769240SAlex Tomas struct address_space *mapping, 311564769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 311664769240SAlex Tomas struct page *page, void *fsdata) 311764769240SAlex Tomas { 311864769240SAlex Tomas struct inode *inode = mapping->host; 311964769240SAlex Tomas loff_t new_i_size; 3120632eaeabSMingming Cao unsigned long start, end; 312179f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 312279f0be8dSAneesh Kumar K.V 312374d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 312474d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 312579f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3126632eaeabSMingming Cao 31279bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 31289c3569b5STao Ma 31299c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 31309c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 31319c3569b5STao Ma ext4_has_inline_data(inode)) 31326984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 31339c3569b5STao Ma 313464769240SAlex Tomas start = pos & (PAGE_SIZE - 1); 313564769240SAlex Tomas end = start + copied - 1; 313664769240SAlex Tomas 313764769240SAlex Tomas /* 31384df031ffSZhang Yi * Since we are holding inode lock, we are sure i_disksize <= 31394df031ffSZhang Yi * i_size. We also know that if i_disksize < i_size, there are 31404df031ffSZhang Yi * delalloc writes pending in the range upto i_size. If the end of 31414df031ffSZhang Yi * the current write is <= i_size, there's no need to touch 31424df031ffSZhang Yi * i_disksize since writeback will push i_disksize upto i_size 31434df031ffSZhang Yi * eventually. If the end of the current write is > i_size and 31444df031ffSZhang Yi * inside an allocated block (ext4_da_should_update_i_disksize() 31454df031ffSZhang Yi * check), we need to update i_disksize here as neither 31464df031ffSZhang Yi * ext4_writepage() nor certain ext4_writepages() paths not 31474df031ffSZhang Yi * allocating blocks update i_disksize. 31484df031ffSZhang Yi * 31494df031ffSZhang Yi * Note that we defer inode dirtying to generic_write_end() / 31504df031ffSZhang Yi * ext4_da_write_inline_data_end(). 3151d2a17637SMingming Cao */ 315264769240SAlex Tomas new_i_size = pos + copied; 31536984aef5SZhang Yi if (copied && new_i_size > inode->i_size && 31544df031ffSZhang Yi ext4_da_should_update_i_disksize(page, end)) 315564769240SAlex Tomas ext4_update_i_disksize(inode, new_i_size); 3156ccd2506bSTheodore Ts'o 3157cc883236SZhang Yi return generic_write_end(file, mapping, pos, len, copied, page, fsdata); 3158ac27a0ecSDave Kleikamp } 3159ac27a0ecSDave Kleikamp 3160ccd2506bSTheodore Ts'o /* 3161ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3162ccd2506bSTheodore Ts'o */ 3163ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3164ccd2506bSTheodore Ts'o { 3165ccd2506bSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3166ccd2506bSTheodore Ts'o 316771d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3168ccd2506bSTheodore Ts'o return 0; 3169ccd2506bSTheodore Ts'o 3170ccd2506bSTheodore Ts'o /* 3171ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3172ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3173ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3174ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3175ccd2506bSTheodore Ts'o * would require replicating code paths in: 3176ccd2506bSTheodore Ts'o * 317720970ba6STheodore Ts'o * ext4_writepages() -> 3178ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3179ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3180ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3181ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3182ccd2506bSTheodore Ts'o * 3183ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3184ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3185ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3186ccd2506bSTheodore Ts'o * doing I/O at all. 3187ccd2506bSTheodore Ts'o * 3188ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3189380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3190ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3191ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 319225985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3193ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3194ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3195ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3196ccd2506bSTheodore Ts'o * 3197ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3198ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3199ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3200ccd2506bSTheodore Ts'o */ 3201ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3202ccd2506bSTheodore Ts'o } 3203ac27a0ecSDave Kleikamp 3204ac27a0ecSDave Kleikamp /* 3205ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3206ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3207ac27a0ecSDave Kleikamp * 3208ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3209ac27a0ecSDave Kleikamp * journal. If somebody makes a swapfile on an ext4 data-journaling 3210ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3211ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3212ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3213ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3214ac27a0ecSDave Kleikamp * 3215ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3216ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3217ac27a0ecSDave Kleikamp */ 3218ac27a0ecSDave Kleikamp static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3219ac27a0ecSDave Kleikamp { 3220ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3221ac27a0ecSDave Kleikamp journal_t *journal; 322251ae846cSYe Bin sector_t ret = 0; 3223ac27a0ecSDave Kleikamp int err; 3224ac27a0ecSDave Kleikamp 322551ae846cSYe Bin inode_lock_shared(inode); 322646c7f254STao Ma /* 322746c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 322846c7f254STao Ma */ 322946c7f254STao Ma if (ext4_has_inline_data(inode)) 323051ae846cSYe Bin goto out; 323146c7f254STao Ma 3232ac27a0ecSDave Kleikamp if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 3233ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DELALLOC)) { 3234ac27a0ecSDave Kleikamp /* 3235ac27a0ecSDave Kleikamp * With delalloc we want to sync the file 3236ac27a0ecSDave Kleikamp * so that we can make sure we allocate 3237ac27a0ecSDave Kleikamp * blocks for file 3238ac27a0ecSDave Kleikamp */ 3239ac27a0ecSDave Kleikamp filemap_write_and_wait(mapping); 3240ac27a0ecSDave Kleikamp } 3241ac27a0ecSDave Kleikamp 324219f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 324319f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3244ac27a0ecSDave Kleikamp /* 3245ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3246ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3247ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3248ac27a0ecSDave Kleikamp * do we expect this to happen. 3249ac27a0ecSDave Kleikamp * 3250ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3251ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3252ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3253ac27a0ecSDave Kleikamp * will.) 3254ac27a0ecSDave Kleikamp * 3255ac27a0ecSDave Kleikamp * NB. EXT4_STATE_JDATA is not set on files other than 3256ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3257ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3258ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3259ac27a0ecSDave Kleikamp * everything they get. 3260ac27a0ecSDave Kleikamp */ 3261ac27a0ecSDave Kleikamp 326219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3263ac27a0ecSDave Kleikamp journal = EXT4_JOURNAL(inode); 3264ac27a0ecSDave Kleikamp jbd2_journal_lock_updates(journal); 326501d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 3266ac27a0ecSDave Kleikamp jbd2_journal_unlock_updates(journal); 3267ac27a0ecSDave Kleikamp 3268ac27a0ecSDave Kleikamp if (err) 326951ae846cSYe Bin goto out; 3270ac27a0ecSDave Kleikamp } 3271ac27a0ecSDave Kleikamp 327251ae846cSYe Bin ret = iomap_bmap(mapping, block, &ext4_iomap_ops); 327351ae846cSYe Bin 327451ae846cSYe Bin out: 327551ae846cSYe Bin inode_unlock_shared(inode); 327651ae846cSYe Bin return ret; 3277ac27a0ecSDave Kleikamp } 3278ac27a0ecSDave Kleikamp 3279fe5ddf6bSMatthew Wilcox (Oracle) static int ext4_read_folio(struct file *file, struct folio *folio) 3280ac27a0ecSDave Kleikamp { 3281fe5ddf6bSMatthew Wilcox (Oracle) struct page *page = &folio->page; 328246c7f254STao Ma int ret = -EAGAIN; 328346c7f254STao Ma struct inode *inode = page->mapping->host; 328446c7f254STao Ma 32850562e0baSJiaying Zhang trace_ext4_readpage(page); 328646c7f254STao Ma 328746c7f254STao Ma if (ext4_has_inline_data(inode)) 328846c7f254STao Ma ret = ext4_readpage_inline(inode, page); 328946c7f254STao Ma 329046c7f254STao Ma if (ret == -EAGAIN) 3291a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 329246c7f254STao Ma 329346c7f254STao Ma return ret; 3294ac27a0ecSDave Kleikamp } 3295ac27a0ecSDave Kleikamp 32966311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3297ac27a0ecSDave Kleikamp { 32986311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 329946c7f254STao Ma 33006311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 330146c7f254STao Ma if (ext4_has_inline_data(inode)) 33026311f91fSMatthew Wilcox (Oracle) return; 330346c7f254STao Ma 3304a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3305ac27a0ecSDave Kleikamp } 3306ac27a0ecSDave Kleikamp 33077ba13abbSMatthew Wilcox (Oracle) static void ext4_invalidate_folio(struct folio *folio, size_t offset, 33087ba13abbSMatthew Wilcox (Oracle) size_t length) 3309ac27a0ecSDave Kleikamp { 3310ccd16945SMatthew Wilcox (Oracle) trace_ext4_invalidate_folio(folio, offset, length); 33110562e0baSJiaying Zhang 33124520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 33137ba13abbSMatthew Wilcox (Oracle) WARN_ON(folio_buffers(folio) && buffer_jbd(folio_buffers(folio))); 33144520fb3cSJan Kara 33157ba13abbSMatthew Wilcox (Oracle) block_invalidate_folio(folio, offset, length); 33164520fb3cSJan Kara } 33174520fb3cSJan Kara 3318ccd16945SMatthew Wilcox (Oracle) static int __ext4_journalled_invalidate_folio(struct folio *folio, 3319ccd16945SMatthew Wilcox (Oracle) size_t offset, size_t length) 33204520fb3cSJan Kara { 3321ccd16945SMatthew Wilcox (Oracle) journal_t *journal = EXT4_JOURNAL(folio->mapping->host); 33224520fb3cSJan Kara 3323ccd16945SMatthew Wilcox (Oracle) trace_ext4_journalled_invalidate_folio(folio, offset, length); 33244520fb3cSJan Kara 3325744692dcSJiaying Zhang /* 3326ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3327ac27a0ecSDave Kleikamp */ 3328ccd16945SMatthew Wilcox (Oracle) if (offset == 0 && length == folio_size(folio)) 3329ccd16945SMatthew Wilcox (Oracle) folio_clear_checked(folio); 3330ac27a0ecSDave Kleikamp 3331ccd16945SMatthew Wilcox (Oracle) return jbd2_journal_invalidate_folio(journal, folio, offset, length); 333253e87268SJan Kara } 333353e87268SJan Kara 333453e87268SJan Kara /* Wrapper for aops... */ 3335ccd16945SMatthew Wilcox (Oracle) static void ext4_journalled_invalidate_folio(struct folio *folio, 3336ccd16945SMatthew Wilcox (Oracle) size_t offset, 3337ccd16945SMatthew Wilcox (Oracle) size_t length) 333853e87268SJan Kara { 3339ccd16945SMatthew Wilcox (Oracle) WARN_ON(__ext4_journalled_invalidate_folio(folio, offset, length) < 0); 3340ac27a0ecSDave Kleikamp } 3341ac27a0ecSDave Kleikamp 33423c402f15SMatthew Wilcox (Oracle) static bool ext4_release_folio(struct folio *folio, gfp_t wait) 3343ac27a0ecSDave Kleikamp { 33443c402f15SMatthew Wilcox (Oracle) journal_t *journal = EXT4_JOURNAL(folio->mapping->host); 3345ac27a0ecSDave Kleikamp 33463c402f15SMatthew Wilcox (Oracle) trace_ext4_releasepage(&folio->page); 33470562e0baSJiaying Zhang 3348e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 33493c402f15SMatthew Wilcox (Oracle) if (folio_test_checked(folio)) 33503c402f15SMatthew Wilcox (Oracle) return false; 33510390131bSFrank Mayhar if (journal) 3352c56a6eb0SMatthew Wilcox (Oracle) return jbd2_journal_try_to_free_buffers(journal, folio); 33530390131bSFrank Mayhar else 335468189fefSMatthew Wilcox (Oracle) return try_to_free_buffers(folio); 3355ac27a0ecSDave Kleikamp } 3356ac27a0ecSDave Kleikamp 3357b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3358b8a6176cSJan Kara { 3359b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3360b8a6176cSJan Kara 3361aa75f4d3SHarshad Shirwadkar if (journal) { 3362aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3363aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3364d0520df7SAndrea Righi return false; 3365d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 33661ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3367d0520df7SAndrea Righi return true; 3368aa75f4d3SHarshad Shirwadkar } 3369aa75f4d3SHarshad Shirwadkar 3370b8a6176cSJan Kara /* Any metadata buffers to write? */ 3371b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3372b8a6176cSJan Kara return true; 3373b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3374b8a6176cSJan Kara } 3375b8a6176cSJan Kara 3376c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3377c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3378de205114SChristoph Hellwig loff_t length, unsigned int flags) 3379364443cbSJan Kara { 3380c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3381c8fdfe29SMatthew Bobrowski 3382c8fdfe29SMatthew Bobrowski /* 3383c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3384c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3385c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3386c8fdfe29SMatthew Bobrowski */ 3387c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3388c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3389c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3390c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3391c8fdfe29SMatthew Bobrowski 3392c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3393c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3394c8fdfe29SMatthew Bobrowski 3395de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3396c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3397de205114SChristoph Hellwig else 3398de205114SChristoph Hellwig iomap->bdev = inode->i_sb->s_bdev; 3399c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3400c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3401c8fdfe29SMatthew Bobrowski 34026386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 34036386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 34046386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 34056386722aSRitesh Harjani 3406c8fdfe29SMatthew Bobrowski /* 3407c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3408c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3409c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3410c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3411c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3412c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3413c8fdfe29SMatthew Bobrowski * been set first. 3414c8fdfe29SMatthew Bobrowski */ 3415c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3416c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3417c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3418de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3419de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3420c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3421c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3422c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3423de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3424de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3425c8fdfe29SMatthew Bobrowski } else { 3426c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3427c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3428c8fdfe29SMatthew Bobrowski } 3429c8fdfe29SMatthew Bobrowski } 3430c8fdfe29SMatthew Bobrowski 3431f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3432f063db5eSMatthew Bobrowski unsigned int flags) 3433f063db5eSMatthew Bobrowski { 3434f063db5eSMatthew Bobrowski handle_t *handle; 3435378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3436378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3437f063db5eSMatthew Bobrowski 3438f063db5eSMatthew Bobrowski /* 3439f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3440f063db5eSMatthew Bobrowski * once for direct I/O. 3441f063db5eSMatthew Bobrowski */ 3442f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3443f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3444f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3445f063db5eSMatthew Bobrowski 3446f063db5eSMatthew Bobrowski retry: 3447f063db5eSMatthew Bobrowski /* 3448f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3449f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3450f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3451f063db5eSMatthew Bobrowski * fits into the credits as well. 3452f063db5eSMatthew Bobrowski */ 3453f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3454f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3455f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3456f063db5eSMatthew Bobrowski 3457378f32baSMatthew Bobrowski /* 3458378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3459378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3460378f32baSMatthew Bobrowski */ 3461952da063SChristoph Hellwig WARN_ON(!(flags & (IOMAP_DAX | IOMAP_DIRECT))); 3462952da063SChristoph Hellwig if (flags & IOMAP_DAX) 3463378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3464378f32baSMatthew Bobrowski /* 3465378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3466378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3467378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3468378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3469378f32baSMatthew Bobrowski */ 3470d0b040f5SJan Kara else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3471378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3472378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3473378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3474378f32baSMatthew Bobrowski 3475378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3476378f32baSMatthew Bobrowski 3477378f32baSMatthew Bobrowski /* 3478378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3479378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3480378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3481378f32baSMatthew Bobrowski */ 3482378f32baSMatthew Bobrowski if (!m_flags && !ret) 3483378f32baSMatthew Bobrowski ret = -ENOTBLK; 3484f063db5eSMatthew Bobrowski 3485f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3486f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3487f063db5eSMatthew Bobrowski goto retry; 3488f063db5eSMatthew Bobrowski 3489f063db5eSMatthew Bobrowski return ret; 3490f063db5eSMatthew Bobrowski } 3491f063db5eSMatthew Bobrowski 3492f063db5eSMatthew Bobrowski 3493364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3494c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3495364443cbSJan Kara { 3496364443cbSJan Kara int ret; 349709edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 349809edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3499364443cbSJan Kara 3500bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3501bcd8e91fSTheodore Ts'o return -EINVAL; 35027046ae35SAndreas Gruenbacher 3503364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3504364443cbSJan Kara return -ERANGE; 3505364443cbSJan Kara 350609edf4d3SMatthew Bobrowski /* 350709edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 350809edf4d3SMatthew Bobrowski */ 350909edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 351009edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 351109edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3512364443cbSJan Kara 35139faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 35149faac62dSRitesh Harjani /* 35159faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 35169faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 35179faac62dSRitesh Harjani * the mapping information. This could boost performance 35189faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 35199faac62dSRitesh Harjani */ 35209faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3521545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 35229faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 35239faac62dSRitesh Harjani goto out; 35249faac62dSRitesh Harjani } 35259faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 35269faac62dSRitesh Harjani } else { 35279faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 35289faac62dSRitesh Harjani } 3529f063db5eSMatthew Bobrowski 3530545052e9SChristoph Hellwig if (ret < 0) 3531545052e9SChristoph Hellwig return ret; 35329faac62dSRitesh Harjani out: 353338ea50daSEric Biggers /* 353438ea50daSEric Biggers * When inline encryption is enabled, sometimes I/O to an encrypted file 353538ea50daSEric Biggers * has to be broken up to guarantee DUN contiguity. Handle this by 353638ea50daSEric Biggers * limiting the length of the mapping returned. 353738ea50daSEric Biggers */ 353838ea50daSEric Biggers map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); 353938ea50daSEric Biggers 3540de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 3541545052e9SChristoph Hellwig 3542364443cbSJan Kara return 0; 3543364443cbSJan Kara } 3544364443cbSJan Kara 35458cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 35468cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 35478cd115bdSJan Kara struct iomap *srcmap) 35488cd115bdSJan Kara { 35498cd115bdSJan Kara int ret; 35508cd115bdSJan Kara 35518cd115bdSJan Kara /* 35528cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 35538cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 35548cd115bdSJan Kara */ 35558cd115bdSJan Kara flags &= ~IOMAP_WRITE; 35568cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 35578cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 35588cd115bdSJan Kara return ret; 35598cd115bdSJan Kara } 35608cd115bdSJan Kara 3561776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3562776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3563776722e8SJan Kara { 3564378f32baSMatthew Bobrowski /* 3565378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3566378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3567378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3568378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3569378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3570378f32baSMatthew Bobrowski */ 3571378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3572378f32baSMatthew Bobrowski return -ENOTBLK; 3573378f32baSMatthew Bobrowski 3574776722e8SJan Kara return 0; 3575776722e8SJan Kara } 3576776722e8SJan Kara 35778ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3578364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3579776722e8SJan Kara .iomap_end = ext4_iomap_end, 3580364443cbSJan Kara }; 3581364443cbSJan Kara 35828cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 35838cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 35848cd115bdSJan Kara .iomap_end = ext4_iomap_end, 35858cd115bdSJan Kara }; 35868cd115bdSJan Kara 358709edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 358809edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 358909edf4d3SMatthew Bobrowski { 359009edf4d3SMatthew Bobrowski struct extent_status es; 359109edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 359209edf4d3SMatthew Bobrowski 359309edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 359409edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 359509edf4d3SMatthew Bobrowski 359609edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 359709edf4d3SMatthew Bobrowski return false; 359809edf4d3SMatthew Bobrowski 359909edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 360009edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 360109edf4d3SMatthew Bobrowski return false; 360209edf4d3SMatthew Bobrowski } 360309edf4d3SMatthew Bobrowski 360409edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 360509edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 360609edf4d3SMatthew Bobrowski 360709edf4d3SMatthew Bobrowski return true; 360809edf4d3SMatthew Bobrowski } 360909edf4d3SMatthew Bobrowski 361009edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 361109edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 361209edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 361309edf4d3SMatthew Bobrowski { 361409edf4d3SMatthew Bobrowski int ret; 361509edf4d3SMatthew Bobrowski bool delalloc = false; 361609edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 361709edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 361809edf4d3SMatthew Bobrowski 361909edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 362009edf4d3SMatthew Bobrowski return -EINVAL; 362109edf4d3SMatthew Bobrowski 36227cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 36237cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3624ba5843f5SJan Kara if (ret != -EAGAIN) { 3625ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3626ba5843f5SJan Kara ret = -ENOENT; 3627ba5843f5SJan Kara return ret; 3628ba5843f5SJan Kara } 3629ba5843f5SJan Kara } 363012735f88SJan Kara 363109edf4d3SMatthew Bobrowski /* 363209edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 363309edf4d3SMatthew Bobrowski */ 363409edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 363509edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 363609edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 363712735f88SJan Kara 3638b2c57642SRitesh Harjani /* 3639b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3640b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3641b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3642b2c57642SRitesh Harjani * -EIO error. 3643b2c57642SRitesh Harjani */ 3644b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3645b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3646b2c57642SRitesh Harjani 3647b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3648b2c57642SRitesh Harjani map.m_flags = 0; 3649b2c57642SRitesh Harjani goto set_iomap; 3650b2c57642SRitesh Harjani } 3651b2c57642SRitesh Harjani } 3652b2c57642SRitesh Harjani 365312735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3654ba5843f5SJan Kara if (ret < 0) 3655ba5843f5SJan Kara return ret; 3656914f82a3SJan Kara if (ret == 0) 365709edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 365809edf4d3SMatthew Bobrowski 3659b2c57642SRitesh Harjani set_iomap: 3660de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 366109edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 366209edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 366309edf4d3SMatthew Bobrowski 366409edf4d3SMatthew Bobrowski return 0; 3665914f82a3SJan Kara } 3666914f82a3SJan Kara 366709edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 366809edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 366909edf4d3SMatthew Bobrowski }; 36704c0425ffSMingming Cao 3671ac27a0ecSDave Kleikamp /* 36726b1f86f8SLinus Torvalds * Whenever the folio is being dirtied, corresponding buffers should already 36736b1f86f8SLinus Torvalds * be attached to the transaction (we take care of this in ext4_page_mkwrite() 36746b1f86f8SLinus Torvalds * and ext4_write_begin()). However we cannot move buffers to dirty transaction 36756b1f86f8SLinus Torvalds * lists here because ->dirty_folio is called under VFS locks and the folio 36762bb8dd40SJan Kara * is not necessarily locked. 3677ac27a0ecSDave Kleikamp * 3678187c82cbSMatthew Wilcox (Oracle) * We cannot just dirty the folio and leave attached buffers clean, because the 3679ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3680ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3681ac27a0ecSDave Kleikamp * 3682187c82cbSMatthew Wilcox (Oracle) * So what we do is to mark the folio "pending dirty" and next time writepage 3683ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3684ac27a0ecSDave Kleikamp */ 3685187c82cbSMatthew Wilcox (Oracle) static bool ext4_journalled_dirty_folio(struct address_space *mapping, 3686187c82cbSMatthew Wilcox (Oracle) struct folio *folio) 3687ac27a0ecSDave Kleikamp { 36880f252336SMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_buffers(folio)); 3689187c82cbSMatthew Wilcox (Oracle) folio_set_checked(folio); 3690187c82cbSMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 3691ac27a0ecSDave Kleikamp } 3692ac27a0ecSDave Kleikamp 3693e621900aSMatthew Wilcox (Oracle) static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio) 36946dcc693bSJan Kara { 3695e621900aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio)); 3696e621900aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_buffers(folio)); 3697e621900aSMatthew Wilcox (Oracle) return block_dirty_folio(mapping, folio); 36986dcc693bSJan Kara } 36996dcc693bSJan Kara 37000e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 37010e6895baSRitesh Harjani struct file *file, sector_t *span) 37020e6895baSRitesh Harjani { 37030e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 37040e6895baSRitesh Harjani &ext4_iomap_report_ops); 37050e6895baSRitesh Harjani } 37060e6895baSRitesh Harjani 370774d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3708fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 37096311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 371020970ba6STheodore Ts'o .writepages = ext4_writepages, 3711bfc1af65SNick Piggin .write_begin = ext4_write_begin, 371274d553aaSTheodore Ts'o .write_end = ext4_write_end, 3713e621900aSMatthew Wilcox (Oracle) .dirty_folio = ext4_dirty_folio, 3714617ba13bSMingming Cao .bmap = ext4_bmap, 37157ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = ext4_invalidate_folio, 37163c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3717378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 371867235182SMatthew Wilcox (Oracle) .migrate_folio = buffer_migrate_folio, 37198ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3720aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 37210e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3722ac27a0ecSDave Kleikamp }; 3723ac27a0ecSDave Kleikamp 3724617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3725fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 37266311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 372720970ba6STheodore Ts'o .writepages = ext4_writepages, 3728bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3729bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3730187c82cbSMatthew Wilcox (Oracle) .dirty_folio = ext4_journalled_dirty_folio, 3731617ba13bSMingming Cao .bmap = ext4_bmap, 3732ccd16945SMatthew Wilcox (Oracle) .invalidate_folio = ext4_journalled_invalidate_folio, 37333c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3734378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3735dae99960SJan Kara .migrate_folio = buffer_migrate_folio_norefs, 37368ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3737aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 37380e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3739ac27a0ecSDave Kleikamp }; 3740ac27a0ecSDave Kleikamp 374164769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 3742fe5ddf6bSMatthew Wilcox (Oracle) .read_folio = ext4_read_folio, 37436311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 374420970ba6STheodore Ts'o .writepages = ext4_writepages, 374564769240SAlex Tomas .write_begin = ext4_da_write_begin, 374664769240SAlex Tomas .write_end = ext4_da_write_end, 3747e621900aSMatthew Wilcox (Oracle) .dirty_folio = ext4_dirty_folio, 374864769240SAlex Tomas .bmap = ext4_bmap, 37497ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = ext4_invalidate_folio, 37503c402f15SMatthew Wilcox (Oracle) .release_folio = ext4_release_folio, 3751378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 375267235182SMatthew Wilcox (Oracle) .migrate_folio = buffer_migrate_folio, 37538ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3754aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 37550e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 375664769240SAlex Tomas }; 375764769240SAlex Tomas 37585f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 37595f0663bbSDan Williams .writepages = ext4_dax_writepages, 37605f0663bbSDan Williams .direct_IO = noop_direct_IO, 376146de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 376294dbb631SToshi Kani .bmap = ext4_bmap, 37630e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 37645f0663bbSDan Williams }; 37655f0663bbSDan Williams 3766617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3767ac27a0ecSDave Kleikamp { 37683d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 37693d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 37703d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 37713d2b1582SLukas Czerner break; 37723d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3773617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 377474d553aaSTheodore Ts'o return; 37753d2b1582SLukas Czerner default: 37763d2b1582SLukas Czerner BUG(); 37773d2b1582SLukas Czerner } 37785f0663bbSDan Williams if (IS_DAX(inode)) 37795f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 37805f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 378174d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 378274d553aaSTheodore Ts'o else 378374d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3784ac27a0ecSDave Kleikamp } 3785ac27a0ecSDave Kleikamp 3786923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3787d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3788d863dc36SLukas Czerner { 378909cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 379009cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3791923ae0ffSRoss Zwisler unsigned blocksize, pos; 3792d863dc36SLukas Czerner ext4_lblk_t iblock; 3793d863dc36SLukas Czerner struct inode *inode = mapping->host; 3794d863dc36SLukas Czerner struct buffer_head *bh; 3795d863dc36SLukas Czerner struct page *page; 3796d863dc36SLukas Czerner int err = 0; 3797d863dc36SLukas Czerner 379809cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3799c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3800d863dc36SLukas Czerner if (!page) 3801d863dc36SLukas Czerner return -ENOMEM; 3802d863dc36SLukas Czerner 3803d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3804d863dc36SLukas Czerner 380509cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3806d863dc36SLukas Czerner 3807d863dc36SLukas Czerner if (!page_has_buffers(page)) 3808d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3809d863dc36SLukas Czerner 3810d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3811d863dc36SLukas Czerner bh = page_buffers(page); 3812d863dc36SLukas Czerner pos = blocksize; 3813d863dc36SLukas Czerner while (offset >= pos) { 3814d863dc36SLukas Czerner bh = bh->b_this_page; 3815d863dc36SLukas Czerner iblock++; 3816d863dc36SLukas Czerner pos += blocksize; 3817d863dc36SLukas Czerner } 3818d863dc36SLukas Czerner if (buffer_freed(bh)) { 3819d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3820d863dc36SLukas Czerner goto unlock; 3821d863dc36SLukas Czerner } 3822d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3823d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3824d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3825d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3826d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3827d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3828d863dc36SLukas Czerner goto unlock; 3829d863dc36SLukas Czerner } 3830d863dc36SLukas Czerner } 3831d863dc36SLukas Czerner 3832d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3833d863dc36SLukas Czerner if (PageUptodate(page)) 3834d863dc36SLukas Czerner set_buffer_uptodate(bh); 3835d863dc36SLukas Czerner 3836d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 38372d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 38382d069c08Szhangyi (F) if (err) 3839d863dc36SLukas Czerner goto unlock; 38404f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3841c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3842a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 384351e4e315SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page_folio(page), 384451e4e315SEric Biggers blocksize, 3845834f1565SEric Biggers bh_offset(bh)); 3846834f1565SEric Biggers if (err) { 3847834f1565SEric Biggers clear_buffer_uptodate(bh); 3848834f1565SEric Biggers goto unlock; 3849834f1565SEric Biggers } 3850c9c7429cSMichael Halcrow } 3851d863dc36SLukas Czerner } 3852d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3853d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3854188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 3855188c299eSJan Kara EXT4_JTR_NONE); 3856d863dc36SLukas Czerner if (err) 3857d863dc36SLukas Czerner goto unlock; 3858d863dc36SLukas Czerner } 3859d863dc36SLukas Czerner zero_user(page, offset, length); 3860d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3861d863dc36SLukas Czerner 3862d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3863d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 38640713ed0cSLukas Czerner } else { 3865353eefd3Sjon ernst err = 0; 3866d863dc36SLukas Czerner mark_buffer_dirty(bh); 38673957ef53SJan Kara if (ext4_should_order_data(inode)) 386873131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 386973131fbbSRoss Zwisler length); 38700713ed0cSLukas Czerner } 3871d863dc36SLukas Czerner 3872d863dc36SLukas Czerner unlock: 3873d863dc36SLukas Czerner unlock_page(page); 387409cbfeafSKirill A. Shutemov put_page(page); 3875d863dc36SLukas Czerner return err; 3876d863dc36SLukas Czerner } 3877d863dc36SLukas Czerner 387894350ab5SMatthew Wilcox /* 3879923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3880923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3881923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3882923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 38833088e5a5SBhaskar Chowdhury * that corresponds to 'from' 3884923ae0ffSRoss Zwisler */ 3885923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3886923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3887923ae0ffSRoss Zwisler { 3888923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 388909cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3890923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3891923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3892923ae0ffSRoss Zwisler 3893923ae0ffSRoss Zwisler /* 3894923ae0ffSRoss Zwisler * correct length if it does not fall between 3895923ae0ffSRoss Zwisler * 'from' and the end of the block 3896923ae0ffSRoss Zwisler */ 3897923ae0ffSRoss Zwisler if (length > max || length < 0) 3898923ae0ffSRoss Zwisler length = max; 3899923ae0ffSRoss Zwisler 390047e69351SJan Kara if (IS_DAX(inode)) { 3901c6f40468SChristoph Hellwig return dax_zero_range(inode, from, length, NULL, 390247e69351SJan Kara &ext4_iomap_ops); 390347e69351SJan Kara } 3904923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3905923ae0ffSRoss Zwisler } 3906923ae0ffSRoss Zwisler 3907923ae0ffSRoss Zwisler /* 390894350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 390994350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 391094350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 391194350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 391294350ab5SMatthew Wilcox */ 3913c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 391494350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 391594350ab5SMatthew Wilcox { 391609cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 391794350ab5SMatthew Wilcox unsigned length; 391894350ab5SMatthew Wilcox unsigned blocksize; 391994350ab5SMatthew Wilcox struct inode *inode = mapping->host; 392094350ab5SMatthew Wilcox 39210d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3922592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 39230d06863fSTheodore Ts'o return 0; 39240d06863fSTheodore Ts'o 392594350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 392694350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 392794350ab5SMatthew Wilcox 392894350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 392994350ab5SMatthew Wilcox } 393094350ab5SMatthew Wilcox 3931a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3932a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3933a87dd18cSLukas Czerner { 3934a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3935a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3936e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3937a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3938a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3939a87dd18cSLukas Czerner int err = 0; 3940a87dd18cSLukas Czerner 3941e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3942e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3943e1be3a92SLukas Czerner 3944a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3945a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3946a87dd18cSLukas Czerner 3947a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3948e1be3a92SLukas Czerner if (start == end && 3949e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3950a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3951a87dd18cSLukas Czerner lstart, length); 3952a87dd18cSLukas Czerner return err; 3953a87dd18cSLukas Czerner } 3954a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3955e1be3a92SLukas Czerner if (partial_start) { 3956a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3957a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3958a87dd18cSLukas Czerner if (err) 3959a87dd18cSLukas Czerner return err; 3960a87dd18cSLukas Czerner } 3961a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3962e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3963a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3964e1be3a92SLukas Czerner byte_end - partial_end, 3965e1be3a92SLukas Czerner partial_end + 1); 3966a87dd18cSLukas Czerner return err; 3967a87dd18cSLukas Czerner } 3968a87dd18cSLukas Czerner 396991ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 397091ef4cafSDuane Griffin { 397191ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 397291ef4cafSDuane Griffin return 1; 397391ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 397491ef4cafSDuane Griffin return 1; 397591ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 397691ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 397791ef4cafSDuane Griffin return 0; 397891ef4cafSDuane Griffin } 397991ef4cafSDuane Griffin 3980ac27a0ecSDave Kleikamp /* 398101127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 398201127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 398301127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 398401127848SJan Kara * that will never happen after we truncate page cache. 398501127848SJan Kara */ 398601127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 398701127848SJan Kara loff_t len) 398801127848SJan Kara { 398901127848SJan Kara handle_t *handle; 39904209ae12SHarshad Shirwadkar int ret; 39914209ae12SHarshad Shirwadkar 399201127848SJan Kara loff_t size = i_size_read(inode); 399301127848SJan Kara 39945955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 399501127848SJan Kara if (offset > size || offset + len < size) 399601127848SJan Kara return 0; 399701127848SJan Kara 399801127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 399901127848SJan Kara return 0; 400001127848SJan Kara 400101127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 400201127848SJan Kara if (IS_ERR(handle)) 400301127848SJan Kara return PTR_ERR(handle); 400401127848SJan Kara ext4_update_i_disksize(inode, size); 40054209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 400601127848SJan Kara ext4_journal_stop(handle); 400701127848SJan Kara 40084209ae12SHarshad Shirwadkar return ret; 400901127848SJan Kara } 401001127848SJan Kara 4011d4f5258eSJan Kara static void ext4_wait_dax_page(struct inode *inode) 4012430657b6SRoss Zwisler { 4013d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 4014430657b6SRoss Zwisler schedule(); 4015d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 4016430657b6SRoss Zwisler } 4017430657b6SRoss Zwisler 4018430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 4019430657b6SRoss Zwisler { 4020430657b6SRoss Zwisler struct page *page; 4021430657b6SRoss Zwisler int error; 4022430657b6SRoss Zwisler 4023d4f5258eSJan Kara if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock))) 4024430657b6SRoss Zwisler return -EINVAL; 4025430657b6SRoss Zwisler 4026430657b6SRoss Zwisler do { 4027430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 4028430657b6SRoss Zwisler if (!page) 4029430657b6SRoss Zwisler return 0; 4030430657b6SRoss Zwisler 4031430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 4032430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 4033430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 4034d4f5258eSJan Kara ext4_wait_dax_page(inode)); 4035b1f38217SRoss Zwisler } while (error == 0); 4036430657b6SRoss Zwisler 4037430657b6SRoss Zwisler return error; 4038430657b6SRoss Zwisler } 4039430657b6SRoss Zwisler 404001127848SJan Kara /* 4041cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 4042a4bb6b64SAllison Henderson * associated with the given offset and length 4043a4bb6b64SAllison Henderson * 4044a4bb6b64SAllison Henderson * @inode: File inode 4045a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 4046a4bb6b64SAllison Henderson * @len: The length of the hole 4047a4bb6b64SAllison Henderson * 40484907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 4049a4bb6b64SAllison Henderson */ 4050a4bb6b64SAllison Henderson 4051ad5cd4f4SDarrick J. Wong int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 4052a4bb6b64SAllison Henderson { 4053ad5cd4f4SDarrick J. Wong struct inode *inode = file_inode(file); 405426a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 405526a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 405626a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 40572da37622STadeusz Struk loff_t first_block_offset, last_block_offset, max_length; 40582da37622STadeusz Struk struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 405926a4c0c6STheodore Ts'o handle_t *handle; 406026a4c0c6STheodore Ts'o unsigned int credits; 40614209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 406226a4c0c6STheodore Ts'o 4063b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 4064aaddea81SZheng Liu 406526a4c0c6STheodore Ts'o /* 406626a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 406726a4c0c6STheodore Ts'o * Then release them. 406826a4c0c6STheodore Ts'o */ 4069cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 407026a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 407126a4c0c6STheodore Ts'o offset + length - 1); 407226a4c0c6STheodore Ts'o if (ret) 407326a4c0c6STheodore Ts'o return ret; 407426a4c0c6STheodore Ts'o } 407526a4c0c6STheodore Ts'o 40765955102cSAl Viro inode_lock(inode); 40779ef06cecSLukas Czerner 407826a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 407926a4c0c6STheodore Ts'o if (offset >= inode->i_size) 408026a4c0c6STheodore Ts'o goto out_mutex; 408126a4c0c6STheodore Ts'o 408226a4c0c6STheodore Ts'o /* 408326a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 408426a4c0c6STheodore Ts'o * to end after the page that contains i_size 408526a4c0c6STheodore Ts'o */ 408626a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 408726a4c0c6STheodore Ts'o length = inode->i_size + 408809cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 408926a4c0c6STheodore Ts'o offset; 409026a4c0c6STheodore Ts'o } 409126a4c0c6STheodore Ts'o 40922da37622STadeusz Struk /* 40932da37622STadeusz Struk * For punch hole the length + offset needs to be within one block 40942da37622STadeusz Struk * before last range. Adjust the length if it goes beyond that limit. 40952da37622STadeusz Struk */ 40962da37622STadeusz Struk max_length = sbi->s_bitmap_maxbytes - inode->i_sb->s_blocksize; 40972da37622STadeusz Struk if (offset + length > max_length) 40982da37622STadeusz Struk length = max_length - offset; 40992da37622STadeusz Struk 4100a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 4101a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 4102a361293fSJan Kara /* 4103a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 4104a361293fSJan Kara * partial block 4105a361293fSJan Kara */ 4106a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 4107a361293fSJan Kara if (ret < 0) 4108a361293fSJan Kara goto out_mutex; 4109a361293fSJan Kara 4110a361293fSJan Kara } 4111a361293fSJan Kara 4112f340b3d9Shongnanli /* Wait all existing dio workers, newcomers will block on i_rwsem */ 4113ea3d7209SJan Kara inode_dio_wait(inode); 4114ea3d7209SJan Kara 4115ad5cd4f4SDarrick J. Wong ret = file_modified(file); 4116ad5cd4f4SDarrick J. Wong if (ret) 4117ad5cd4f4SDarrick J. Wong goto out_mutex; 4118ad5cd4f4SDarrick J. Wong 4119ea3d7209SJan Kara /* 4120ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 4121ea3d7209SJan Kara * page cache. 4122ea3d7209SJan Kara */ 4123d4f5258eSJan Kara filemap_invalidate_lock(mapping); 4124430657b6SRoss Zwisler 4125430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4126430657b6SRoss Zwisler if (ret) 4127430657b6SRoss Zwisler goto out_dio; 4128430657b6SRoss Zwisler 4129a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4130a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 413126a4c0c6STheodore Ts'o 4132a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 413301127848SJan Kara if (last_block_offset > first_block_offset) { 413401127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 413501127848SJan Kara if (ret) 413601127848SJan Kara goto out_dio; 4137a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4138a87dd18cSLukas Czerner last_block_offset); 413901127848SJan Kara } 414026a4c0c6STheodore Ts'o 414126a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 414226a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 414326a4c0c6STheodore Ts'o else 414426a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 414526a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 414626a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 414726a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 414826a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 414926a4c0c6STheodore Ts'o goto out_dio; 415026a4c0c6STheodore Ts'o } 415126a4c0c6STheodore Ts'o 4152a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4153a87dd18cSLukas Czerner length); 415426a4c0c6STheodore Ts'o if (ret) 415526a4c0c6STheodore Ts'o goto out_stop; 415626a4c0c6STheodore Ts'o 415726a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 415826a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 415926a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 416026a4c0c6STheodore Ts'o 4161eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4162eee597acSLukas Czerner if (stop_block > first_block) { 416326a4c0c6STheodore Ts'o 416426a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 416527bc446eSbrookxu ext4_discard_preallocations(inode, 0); 416626a4c0c6STheodore Ts'o 416726a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 416826a4c0c6STheodore Ts'o stop_block - first_block); 416926a4c0c6STheodore Ts'o if (ret) { 417026a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 417126a4c0c6STheodore Ts'o goto out_stop; 417226a4c0c6STheodore Ts'o } 417326a4c0c6STheodore Ts'o 417426a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 417526a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 417626a4c0c6STheodore Ts'o stop_block - 1); 417726a4c0c6STheodore Ts'o else 41784f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 417926a4c0c6STheodore Ts'o stop_block); 418026a4c0c6STheodore Ts'o 4181819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4182eee597acSLukas Czerner } 4183a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 418426a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 418526a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4186e251f9bcSMaxim Patlasov 4187eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 41884209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 41894209ae12SHarshad Shirwadkar if (unlikely(ret2)) 41904209ae12SHarshad Shirwadkar ret = ret2; 419167a7d5f5SJan Kara if (ret >= 0) 419267a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 419326a4c0c6STheodore Ts'o out_stop: 419426a4c0c6STheodore Ts'o ext4_journal_stop(handle); 419526a4c0c6STheodore Ts'o out_dio: 4196d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 419726a4c0c6STheodore Ts'o out_mutex: 41985955102cSAl Viro inode_unlock(inode); 419926a4c0c6STheodore Ts'o return ret; 4200a4bb6b64SAllison Henderson } 4201a4bb6b64SAllison Henderson 4202a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4203a361293fSJan Kara { 4204a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4205a361293fSJan Kara struct jbd2_inode *jinode; 4206a361293fSJan Kara 4207a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4208a361293fSJan Kara return 0; 4209a361293fSJan Kara 4210a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4211a361293fSJan Kara spin_lock(&inode->i_lock); 4212a361293fSJan Kara if (!ei->jinode) { 4213a361293fSJan Kara if (!jinode) { 4214a361293fSJan Kara spin_unlock(&inode->i_lock); 4215a361293fSJan Kara return -ENOMEM; 4216a361293fSJan Kara } 4217a361293fSJan Kara ei->jinode = jinode; 4218a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4219a361293fSJan Kara jinode = NULL; 4220a361293fSJan Kara } 4221a361293fSJan Kara spin_unlock(&inode->i_lock); 4222a361293fSJan Kara if (unlikely(jinode != NULL)) 4223a361293fSJan Kara jbd2_free_inode(jinode); 4224a361293fSJan Kara return 0; 4225a361293fSJan Kara } 4226a361293fSJan Kara 4227a4bb6b64SAllison Henderson /* 4228617ba13bSMingming Cao * ext4_truncate() 4229ac27a0ecSDave Kleikamp * 4230617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4231617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4232ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4233ac27a0ecSDave Kleikamp * 423442b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4235ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4236ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4237ac27a0ecSDave Kleikamp * 4238ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4239ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4240ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4241ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4242ac27a0ecSDave Kleikamp * left-to-right works OK too). 4243ac27a0ecSDave Kleikamp * 4244ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4245ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4246ac27a0ecSDave Kleikamp * 4247ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4248617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4249ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4250617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4251617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4252ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4253617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4254ac27a0ecSDave Kleikamp */ 42552c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4256ac27a0ecSDave Kleikamp { 4257819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4258819c4920STheodore Ts'o unsigned int credits; 42594209ae12SHarshad Shirwadkar int err = 0, err2; 4260819c4920STheodore Ts'o handle_t *handle; 4261819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4262819c4920STheodore Ts'o 426319b5ef61STheodore Ts'o /* 426419b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4265e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 4266f340b3d9Shongnanli * have i_rwsem locked because it's not necessary. 426719b5ef61STheodore Ts'o */ 426819b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 42695955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 42700562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 42710562e0baSJiaying Zhang 427291ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 42739a5d265fSzhengliang goto out_trace; 4274ac27a0ecSDave Kleikamp 42755534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 427619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 42777d8f9f7dSTheodore Ts'o 4278aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4279aef1c851STao Ma int has_inline = 1; 4280aef1c851STao Ma 428101daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 42829a5d265fSzhengliang if (err || has_inline) 42839a5d265fSzhengliang goto out_trace; 4284aef1c851STao Ma } 4285aef1c851STao Ma 4286a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4287a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4288a71248b1SBaokun Li err = ext4_inode_attach_jinode(inode); 4289a71248b1SBaokun Li if (err) 42909a5d265fSzhengliang goto out_trace; 4291a361293fSJan Kara } 4292a361293fSJan Kara 4293ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4294819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4295ff9893dcSAmir Goldstein else 4296819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4297819c4920STheodore Ts'o 4298819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 42999a5d265fSzhengliang if (IS_ERR(handle)) { 43009a5d265fSzhengliang err = PTR_ERR(handle); 43019a5d265fSzhengliang goto out_trace; 43029a5d265fSzhengliang } 4303819c4920STheodore Ts'o 4304eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4305eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4306819c4920STheodore Ts'o 4307819c4920STheodore Ts'o /* 4308819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4309819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4310819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4311819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4312819c4920STheodore Ts'o * 4313819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4314819c4920STheodore Ts'o * truncatable state while each transaction commits. 4315819c4920STheodore Ts'o */ 43162c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 43172c98eb5eSTheodore Ts'o if (err) 4318819c4920STheodore Ts'o goto out_stop; 4319819c4920STheodore Ts'o 4320819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4321819c4920STheodore Ts'o 432227bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4323819c4920STheodore Ts'o 4324819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4325d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4326819c4920STheodore Ts'o else 4327819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4328819c4920STheodore Ts'o 4329819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4330d0abb36dSTheodore Ts'o if (err) 4331d0abb36dSTheodore Ts'o goto out_stop; 4332819c4920STheodore Ts'o 4333819c4920STheodore Ts'o if (IS_SYNC(inode)) 4334819c4920STheodore Ts'o ext4_handle_sync(handle); 4335819c4920STheodore Ts'o 4336819c4920STheodore Ts'o out_stop: 4337819c4920STheodore Ts'o /* 4338819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4339819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4340819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 434158d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4342819c4920STheodore Ts'o * orphan info for us. 4343819c4920STheodore Ts'o */ 4344819c4920STheodore Ts'o if (inode->i_nlink) 4345819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4346819c4920STheodore Ts'o 4347eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 43484209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 43494209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 43504209ae12SHarshad Shirwadkar err = err2; 4351819c4920STheodore Ts'o ext4_journal_stop(handle); 4352a86c6181SAlex Tomas 43539a5d265fSzhengliang out_trace: 43540562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 43552c98eb5eSTheodore Ts'o return err; 4356ac27a0ecSDave Kleikamp } 4357ac27a0ecSDave Kleikamp 43589a1bf32cSZhang Yi static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 43599a1bf32cSZhang Yi { 43609a1bf32cSZhang Yi if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 43619a1bf32cSZhang Yi return inode_peek_iversion_raw(inode); 43629a1bf32cSZhang Yi else 43639a1bf32cSZhang Yi return inode_peek_iversion(inode); 43649a1bf32cSZhang Yi } 43659a1bf32cSZhang Yi 43669a1bf32cSZhang Yi static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, 43679a1bf32cSZhang Yi struct ext4_inode_info *ei) 43689a1bf32cSZhang Yi { 43699a1bf32cSZhang Yi struct inode *inode = &(ei->vfs_inode); 43709a1bf32cSZhang Yi u64 i_blocks = READ_ONCE(inode->i_blocks); 43719a1bf32cSZhang Yi struct super_block *sb = inode->i_sb; 43729a1bf32cSZhang Yi 43739a1bf32cSZhang Yi if (i_blocks <= ~0U) { 43749a1bf32cSZhang Yi /* 43759a1bf32cSZhang Yi * i_blocks can be represented in a 32 bit variable 43769a1bf32cSZhang Yi * as multiple of 512 bytes 43779a1bf32cSZhang Yi */ 43789a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 43799a1bf32cSZhang Yi raw_inode->i_blocks_high = 0; 43809a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 43819a1bf32cSZhang Yi return 0; 43829a1bf32cSZhang Yi } 43839a1bf32cSZhang Yi 43849a1bf32cSZhang Yi /* 43859a1bf32cSZhang Yi * This should never happen since sb->s_maxbytes should not have 43869a1bf32cSZhang Yi * allowed this, sb->s_maxbytes was set according to the huge_file 43879a1bf32cSZhang Yi * feature in ext4_fill_super(). 43889a1bf32cSZhang Yi */ 43899a1bf32cSZhang Yi if (!ext4_has_feature_huge_file(sb)) 43909a1bf32cSZhang Yi return -EFSCORRUPTED; 43919a1bf32cSZhang Yi 43929a1bf32cSZhang Yi if (i_blocks <= 0xffffffffffffULL) { 43939a1bf32cSZhang Yi /* 43949a1bf32cSZhang Yi * i_blocks can be represented in a 48 bit variable 43959a1bf32cSZhang Yi * as multiple of 512 bytes 43969a1bf32cSZhang Yi */ 43979a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 43989a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 43999a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 44009a1bf32cSZhang Yi } else { 44019a1bf32cSZhang Yi ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 44029a1bf32cSZhang Yi /* i_block is stored in file system block size */ 44039a1bf32cSZhang Yi i_blocks = i_blocks >> (inode->i_blkbits - 9); 44049a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 44059a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 44069a1bf32cSZhang Yi } 44079a1bf32cSZhang Yi return 0; 44089a1bf32cSZhang Yi } 44099a1bf32cSZhang Yi 44109a1bf32cSZhang Yi static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) 44119a1bf32cSZhang Yi { 44129a1bf32cSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 44139a1bf32cSZhang Yi uid_t i_uid; 44149a1bf32cSZhang Yi gid_t i_gid; 44159a1bf32cSZhang Yi projid_t i_projid; 44169a1bf32cSZhang Yi int block; 44179a1bf32cSZhang Yi int err; 44189a1bf32cSZhang Yi 44199a1bf32cSZhang Yi err = ext4_inode_blocks_set(raw_inode, ei); 44209a1bf32cSZhang Yi 44219a1bf32cSZhang Yi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 44229a1bf32cSZhang Yi i_uid = i_uid_read(inode); 44239a1bf32cSZhang Yi i_gid = i_gid_read(inode); 44249a1bf32cSZhang Yi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 44259a1bf32cSZhang Yi if (!(test_opt(inode->i_sb, NO_UID32))) { 44269a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 44279a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 44289a1bf32cSZhang Yi /* 44299a1bf32cSZhang Yi * Fix up interoperability with old kernels. Otherwise, 44309a1bf32cSZhang Yi * old inodes get re-used with the upper 16 bits of the 44319a1bf32cSZhang Yi * uid/gid intact. 44329a1bf32cSZhang Yi */ 44339a1bf32cSZhang Yi if (ei->i_dtime && list_empty(&ei->i_orphan)) { 44349a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 44359a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 44369a1bf32cSZhang Yi } else { 44379a1bf32cSZhang Yi raw_inode->i_uid_high = 44389a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_uid)); 44399a1bf32cSZhang Yi raw_inode->i_gid_high = 44409a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_gid)); 44419a1bf32cSZhang Yi } 44429a1bf32cSZhang Yi } else { 44439a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 44449a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 44459a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 44469a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 44479a1bf32cSZhang Yi } 44489a1bf32cSZhang Yi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 44499a1bf32cSZhang Yi 44509a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 44519a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 44529a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 44539a1bf32cSZhang Yi EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 44549a1bf32cSZhang Yi 44559a1bf32cSZhang Yi raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 44569a1bf32cSZhang Yi raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 44579a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 44589a1bf32cSZhang Yi raw_inode->i_file_acl_high = 44599a1bf32cSZhang Yi cpu_to_le16(ei->i_file_acl >> 32); 44609a1bf32cSZhang Yi raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 44619a1bf32cSZhang Yi ext4_isize_set(raw_inode, ei->i_disksize); 44629a1bf32cSZhang Yi 44639a1bf32cSZhang Yi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 44649a1bf32cSZhang Yi if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 44659a1bf32cSZhang Yi if (old_valid_dev(inode->i_rdev)) { 44669a1bf32cSZhang Yi raw_inode->i_block[0] = 44679a1bf32cSZhang Yi cpu_to_le32(old_encode_dev(inode->i_rdev)); 44689a1bf32cSZhang Yi raw_inode->i_block[1] = 0; 44699a1bf32cSZhang Yi } else { 44709a1bf32cSZhang Yi raw_inode->i_block[0] = 0; 44719a1bf32cSZhang Yi raw_inode->i_block[1] = 44729a1bf32cSZhang Yi cpu_to_le32(new_encode_dev(inode->i_rdev)); 44739a1bf32cSZhang Yi raw_inode->i_block[2] = 0; 44749a1bf32cSZhang Yi } 44759a1bf32cSZhang Yi } else if (!ext4_has_inline_data(inode)) { 44769a1bf32cSZhang Yi for (block = 0; block < EXT4_N_BLOCKS; block++) 44779a1bf32cSZhang Yi raw_inode->i_block[block] = ei->i_data[block]; 44789a1bf32cSZhang Yi } 44799a1bf32cSZhang Yi 44809a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 44819a1bf32cSZhang Yi u64 ivers = ext4_inode_peek_iversion(inode); 44829a1bf32cSZhang Yi 44839a1bf32cSZhang Yi raw_inode->i_disk_version = cpu_to_le32(ivers); 44849a1bf32cSZhang Yi if (ei->i_extra_isize) { 44859a1bf32cSZhang Yi if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 44869a1bf32cSZhang Yi raw_inode->i_version_hi = 44879a1bf32cSZhang Yi cpu_to_le32(ivers >> 32); 44889a1bf32cSZhang Yi raw_inode->i_extra_isize = 44899a1bf32cSZhang Yi cpu_to_le16(ei->i_extra_isize); 44909a1bf32cSZhang Yi } 44919a1bf32cSZhang Yi } 44929a1bf32cSZhang Yi 44939a1bf32cSZhang Yi if (i_projid != EXT4_DEF_PROJID && 44949a1bf32cSZhang Yi !ext4_has_feature_project(inode->i_sb)) 44959a1bf32cSZhang Yi err = err ?: -EFSCORRUPTED; 44969a1bf32cSZhang Yi 44979a1bf32cSZhang Yi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 44989a1bf32cSZhang Yi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 44999a1bf32cSZhang Yi raw_inode->i_projid = cpu_to_le32(i_projid); 45009a1bf32cSZhang Yi 45019a1bf32cSZhang Yi ext4_inode_csum_set(inode, raw_inode, ei); 45029a1bf32cSZhang Yi return err; 45039a1bf32cSZhang Yi } 45049a1bf32cSZhang Yi 4505ac27a0ecSDave Kleikamp /* 4506617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4507de01f484SZhang Yi * underlying buffer_head on success. If we pass 'inode' and it does not 4508de01f484SZhang Yi * have in-inode xattr, we have all inode data in memory that is needed 4509de01f484SZhang Yi * to recreate the on-disk version of this inode. 4510ac27a0ecSDave Kleikamp */ 45118016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 4512de01f484SZhang Yi struct inode *inode, struct ext4_iloc *iloc, 45138016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4514ac27a0ecSDave Kleikamp { 4515240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4516ac27a0ecSDave Kleikamp struct buffer_head *bh; 4517240799cdSTheodore Ts'o ext4_fsblk_t block; 451802f03c42SLinus Torvalds struct blk_plug plug; 4519240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4520ac27a0ecSDave Kleikamp 45213a06d778SAneesh Kumar K.V iloc->bh = NULL; 45228016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 45238016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 45246a797d27SDarrick J. Wong return -EFSCORRUPTED; 4525ac27a0ecSDave Kleikamp 45268016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4527240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4528240799cdSTheodore Ts'o if (!gdp) 4529240799cdSTheodore Ts'o return -EIO; 4530240799cdSTheodore Ts'o 4531240799cdSTheodore Ts'o /* 4532240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4533240799cdSTheodore Ts'o */ 453400d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 45358016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4536240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4537240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4538240799cdSTheodore Ts'o 4539eee22187SBaokun Li block = ext4_inode_table(sb, gdp); 4540eee22187SBaokun Li if ((block <= le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) || 4541eee22187SBaokun Li (block >= ext4_blocks_count(EXT4_SB(sb)->s_es))) { 4542eee22187SBaokun Li ext4_error(sb, "Invalid inode table block %llu in " 4543eee22187SBaokun Li "block_group %u", block, iloc->block_group); 4544eee22187SBaokun Li return -EFSCORRUPTED; 4545eee22187SBaokun Li } 4546eee22187SBaokun Li block += (inode_offset / inodes_per_block); 4547eee22187SBaokun Li 4548240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4549aebf0243SWang Shilong if (unlikely(!bh)) 4550860d21e2STheodore Ts'o return -ENOMEM; 45518e33fadfSZhang Yi if (ext4_buffer_uptodate(bh)) 4552ac27a0ecSDave Kleikamp goto has_buffer; 4553ac27a0ecSDave Kleikamp 45548e33fadfSZhang Yi lock_buffer(bh); 4555f2c77973SZhang Yi if (ext4_buffer_uptodate(bh)) { 4556f2c77973SZhang Yi /* Someone brought it uptodate while we waited */ 4557f2c77973SZhang Yi unlock_buffer(bh); 4558f2c77973SZhang Yi goto has_buffer; 4559f2c77973SZhang Yi } 4560f2c77973SZhang Yi 4561ac27a0ecSDave Kleikamp /* 4562ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4563ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4564ac27a0ecSDave Kleikamp * block. 4565ac27a0ecSDave Kleikamp */ 4566de01f484SZhang Yi if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 4567ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4568240799cdSTheodore Ts'o int i, start; 4569ac27a0ecSDave Kleikamp 4570240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4571ac27a0ecSDave Kleikamp 4572ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4573240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4574aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4575ac27a0ecSDave Kleikamp goto make_io; 4576ac27a0ecSDave Kleikamp 4577ac27a0ecSDave Kleikamp /* 4578ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4579ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4580ac27a0ecSDave Kleikamp * of one, so skip it. 4581ac27a0ecSDave Kleikamp */ 4582ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4583ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4584ac27a0ecSDave Kleikamp goto make_io; 4585ac27a0ecSDave Kleikamp } 4586240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4587ac27a0ecSDave Kleikamp if (i == inode_offset) 4588ac27a0ecSDave Kleikamp continue; 4589617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4590ac27a0ecSDave Kleikamp break; 4591ac27a0ecSDave Kleikamp } 4592ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4593240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4594de01f484SZhang Yi struct ext4_inode *raw_inode = 4595de01f484SZhang Yi (struct ext4_inode *) (bh->b_data + iloc->offset); 4596de01f484SZhang Yi 4597ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4598ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4599de01f484SZhang Yi if (!ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4600de01f484SZhang Yi ext4_fill_raw_inode(inode, raw_inode); 4601ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4602ac27a0ecSDave Kleikamp unlock_buffer(bh); 4603ac27a0ecSDave Kleikamp goto has_buffer; 4604ac27a0ecSDave Kleikamp } 4605ac27a0ecSDave Kleikamp } 4606ac27a0ecSDave Kleikamp 4607ac27a0ecSDave Kleikamp make_io: 4608ac27a0ecSDave Kleikamp /* 4609240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4610240799cdSTheodore Ts'o * blocks from the inode table. 4611240799cdSTheodore Ts'o */ 461202f03c42SLinus Torvalds blk_start_plug(&plug); 4613240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4614240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4615240799cdSTheodore Ts'o unsigned num; 46160d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4617240799cdSTheodore Ts'o 4618240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4619b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 46200d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4621240799cdSTheodore Ts'o if (table > b) 4622240799cdSTheodore Ts'o b = table; 46230d606e2cSTheodore Ts'o end = b + ra_blks; 4624240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4625feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4626560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4627240799cdSTheodore Ts'o table += num / inodes_per_block; 4628240799cdSTheodore Ts'o if (end > table) 4629240799cdSTheodore Ts'o end = table; 4630240799cdSTheodore Ts'o while (b <= end) 46315df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4632240799cdSTheodore Ts'o } 4633240799cdSTheodore Ts'o 4634240799cdSTheodore Ts'o /* 4635ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4636ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4637ac27a0ecSDave Kleikamp * Read the block from disk. 4638ac27a0ecSDave Kleikamp */ 46398016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 46402d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 464102f03c42SLinus Torvalds blk_finish_plug(&plug); 4642ac27a0ecSDave Kleikamp wait_on_buffer(bh); 46430904c9aeSZhang Yi ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4644ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 46458016e29fSHarshad Shirwadkar if (ret_block) 46468016e29fSHarshad Shirwadkar *ret_block = block; 4647ac27a0ecSDave Kleikamp brelse(bh); 4648ac27a0ecSDave Kleikamp return -EIO; 4649ac27a0ecSDave Kleikamp } 4650ac27a0ecSDave Kleikamp has_buffer: 4651ac27a0ecSDave Kleikamp iloc->bh = bh; 4652ac27a0ecSDave Kleikamp return 0; 4653ac27a0ecSDave Kleikamp } 4654ac27a0ecSDave Kleikamp 46558016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 46568016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 46578016e29fSHarshad Shirwadkar { 4658c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 46598016e29fSHarshad Shirwadkar int ret; 46608016e29fSHarshad Shirwadkar 4661de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc, 46628016e29fSHarshad Shirwadkar &err_blk); 46638016e29fSHarshad Shirwadkar 46648016e29fSHarshad Shirwadkar if (ret == -EIO) 46658016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 46668016e29fSHarshad Shirwadkar "unable to read itable block"); 46678016e29fSHarshad Shirwadkar 46688016e29fSHarshad Shirwadkar return ret; 46698016e29fSHarshad Shirwadkar } 46708016e29fSHarshad Shirwadkar 4671617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4672ac27a0ecSDave Kleikamp { 4673c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 46748016e29fSHarshad Shirwadkar int ret; 46758016e29fSHarshad Shirwadkar 4676de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc, 4677de01f484SZhang Yi &err_blk); 46788016e29fSHarshad Shirwadkar 46798016e29fSHarshad Shirwadkar if (ret == -EIO) 46808016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 46818016e29fSHarshad Shirwadkar "unable to read itable block"); 46828016e29fSHarshad Shirwadkar 46838016e29fSHarshad Shirwadkar return ret; 46848016e29fSHarshad Shirwadkar } 46858016e29fSHarshad Shirwadkar 46868016e29fSHarshad Shirwadkar 46878016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 46888016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 46898016e29fSHarshad Shirwadkar { 4690de01f484SZhang Yi return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL); 4691ac27a0ecSDave Kleikamp } 4692ac27a0ecSDave Kleikamp 4693a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 46946642586bSRoss Zwisler { 4695a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4696a8ab6d38SIra Weiny 46979cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 46986642586bSRoss Zwisler return false; 46996642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 47006642586bSRoss Zwisler return false; 47016642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 47026642586bSRoss Zwisler return false; 47036642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 47046642586bSRoss Zwisler return false; 4705592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 47066642586bSRoss Zwisler return false; 4707c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4708c93d8f88SEric Biggers return false; 4709a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4710a8ab6d38SIra Weiny return false; 4711a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 47126642586bSRoss Zwisler return true; 4713a8ab6d38SIra Weiny 4714b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 47156642586bSRoss Zwisler } 47166642586bSRoss Zwisler 4717043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4718ac27a0ecSDave Kleikamp { 4719617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 472000a1a053STheodore Ts'o unsigned int new_fl = 0; 4721ac27a0ecSDave Kleikamp 4722043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4723043546e4SIra Weiny 4724617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 472500a1a053STheodore Ts'o new_fl |= S_SYNC; 4726617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 472700a1a053STheodore Ts'o new_fl |= S_APPEND; 4728617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 472900a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4730617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 473100a1a053STheodore Ts'o new_fl |= S_NOATIME; 4732617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 473300a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4734043546e4SIra Weiny 4735043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4736043546e4SIra Weiny * here if already set. */ 4737043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4738043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4739923ae0ffSRoss Zwisler new_fl |= S_DAX; 4740043546e4SIra Weiny 47412ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 47422ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4743b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4744b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4745c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4746c93d8f88SEric Biggers new_fl |= S_VERITY; 47475f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 47482ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4749c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4750ac27a0ecSDave Kleikamp } 4751ac27a0ecSDave Kleikamp 47520fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 47530fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 47540fc1b451SAneesh Kumar K.V { 47550fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 47568180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 47578180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 47580fc1b451SAneesh Kumar K.V 4759e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 47600fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 47610fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 47620fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 476307a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 47648180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 47658180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 47668180a562SAneesh Kumar K.V } else { 47670fc1b451SAneesh Kumar K.V return i_blocks; 47688180a562SAneesh Kumar K.V } 47690fc1b451SAneesh Kumar K.V } else { 47700fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 47710fc1b451SAneesh Kumar K.V } 47720fc1b451SAneesh Kumar K.V } 4773ff9ddf7eSJan Kara 4774eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4775152a7b0aSTao Ma struct ext4_inode *raw_inode, 4776152a7b0aSTao Ma struct ext4_inode_info *ei) 4777152a7b0aSTao Ma { 4778152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4779152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4780eb9b5f01STheodore Ts'o 4781fd7e672eSBaokun Li if (EXT4_INODE_HAS_XATTR_SPACE(inode) && 4782290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 47831dcdce59SYe Bin int err; 47841dcdce59SYe Bin 4785152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 47861dcdce59SYe Bin err = ext4_find_inline_data_nolock(inode); 47871dcdce59SYe Bin if (!err && ext4_has_inline_data(inode)) 47881dcdce59SYe Bin ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 47891dcdce59SYe Bin return err; 4790f19d5870STao Ma } else 4791f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4792eb9b5f01STheodore Ts'o return 0; 4793152a7b0aSTao Ma } 4794152a7b0aSTao Ma 4795040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4796040cb378SLi Xi { 47970b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4798040cb378SLi Xi return -EOPNOTSUPP; 4799040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4800040cb378SLi Xi return 0; 4801040cb378SLi Xi } 4802040cb378SLi Xi 4803e254d1afSEryu Guan /* 4804e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4805e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4806e254d1afSEryu Guan * set. 4807e254d1afSEryu Guan */ 4808e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4809e254d1afSEryu Guan { 4810e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4811e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4812e254d1afSEryu Guan else 4813e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4814e254d1afSEryu Guan } 4815e254d1afSEryu Guan 48168a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 48178a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 48188a363970STheodore Ts'o unsigned int line) 4819ac27a0ecSDave Kleikamp { 4820617ba13bSMingming Cao struct ext4_iloc iloc; 4821617ba13bSMingming Cao struct ext4_inode *raw_inode; 48221d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4823bd2c38cfSJan Kara struct ext4_super_block *es = EXT4_SB(sb)->s_es; 48241d1fe1eeSDavid Howells struct inode *inode; 4825b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 48261d1fe1eeSDavid Howells long ret; 48277e6e1ef4SDarrick J. Wong loff_t size; 4828ac27a0ecSDave Kleikamp int block; 482908cefc7aSEric W. Biederman uid_t i_uid; 483008cefc7aSEric W. Biederman gid_t i_gid; 4831040cb378SLi Xi projid_t i_projid; 4832ac27a0ecSDave Kleikamp 4833191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 4834bd2c38cfSJan Kara ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4835bd2c38cfSJan Kara ino == le32_to_cpu(es->s_usr_quota_inum) || 4836bd2c38cfSJan Kara ino == le32_to_cpu(es->s_grp_quota_inum) || 483702f310fcSJan Kara ino == le32_to_cpu(es->s_prj_quota_inum) || 483802f310fcSJan Kara ino == le32_to_cpu(es->s_orphan_file_inum))) || 48398a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 4840bd2c38cfSJan Kara (ino > le32_to_cpu(es->s_inodes_count))) { 48418a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 48428a363970STheodore Ts'o return ERR_PTR(-ESTALE); 4843014c9caaSJan Kara __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 48448a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 48458a363970STheodore Ts'o ino, current->comm); 48468a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 48478a363970STheodore Ts'o } 48488a363970STheodore Ts'o 48491d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 48501d1fe1eeSDavid Howells if (!inode) 48511d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 48521d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 48531d1fe1eeSDavid Howells return inode; 48541d1fe1eeSDavid Howells 48551d1fe1eeSDavid Howells ei = EXT4_I(inode); 48567dc57615SPeter Huewe iloc.bh = NULL; 4857ac27a0ecSDave Kleikamp 48588016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 48591d1fe1eeSDavid Howells if (ret < 0) 4860ac27a0ecSDave Kleikamp goto bad_inode; 4861617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4862814525f4SDarrick J. Wong 48638a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 48648a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 48658a363970STheodore Ts'o ret = -ESTALE; 48668a363970STheodore Ts'o goto bad_inode; 48678a363970STheodore Ts'o } 48688a363970STheodore Ts'o 4869814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4870814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4871814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 48722dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 48732dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 48748a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48758a363970STheodore Ts'o "iget: bad extra_isize %u " 48768a363970STheodore Ts'o "(inode size %u)", 48772dc8d9e1SEric Biggers ei->i_extra_isize, 4878814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 48796a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4880814525f4SDarrick J. Wong goto bad_inode; 4881814525f4SDarrick J. Wong } 4882814525f4SDarrick J. Wong } else 4883814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4884814525f4SDarrick J. Wong 4885814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 48869aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4887814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4888814525f4SDarrick J. Wong __u32 csum; 4889814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4890814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4891814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4892814525f4SDarrick J. Wong sizeof(inum)); 4893814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4894814525f4SDarrick J. Wong sizeof(gen)); 4895814525f4SDarrick J. Wong } 4896814525f4SDarrick J. Wong 48978016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 48988016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 48998016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 49008016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 49018016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 49026a797d27SDarrick J. Wong ret = -EFSBADCRC; 4903814525f4SDarrick J. Wong goto bad_inode; 4904814525f4SDarrick J. Wong } 4905814525f4SDarrick J. Wong 4906ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 490708cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 490808cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 49090b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4910040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4911040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4912040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4913040cb378SLi Xi else 4914040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4915040cb378SLi Xi 4916ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 491708cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 491808cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4919ac27a0ecSDave Kleikamp } 492008cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 492108cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4922040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4923bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4924ac27a0ecSDave Kleikamp 4925353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 492667cf5b09STao Ma ei->i_inline_off = 0; 4927ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4928ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4929ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4930ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4931ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4932ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4933ac27a0ecSDave Kleikamp */ 4934ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 49355cd74028SBaokun Li if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL || 4936393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4937393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 49385cd74028SBaokun Li /* this inode is deleted or unallocated */ 49395cd74028SBaokun Li if (flags & EXT4_IGET_SPECIAL) { 49405cd74028SBaokun Li ext4_error_inode(inode, function, line, 0, 49415cd74028SBaokun Li "iget: special inode unallocated"); 49425cd74028SBaokun Li ret = -EFSCORRUPTED; 49435cd74028SBaokun Li } else 49441d1fe1eeSDavid Howells ret = -ESTALE; 4945ac27a0ecSDave Kleikamp goto bad_inode; 4946ac27a0ecSDave Kleikamp } 4947ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4948ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4949ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4950393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4951393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4952393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4953ac27a0ecSDave Kleikamp } 4954ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4955043546e4SIra Weiny ext4_set_inode_flags(inode, true); 49560fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 49577973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4958e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4959a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4960a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4961e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 49627e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 49638a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49648a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 49657e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 49667e6e1ef4SDarrick J. Wong goto bad_inode; 49677e6e1ef4SDarrick J. Wong } 496848a34311SJan Kara /* 496948a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 497048a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 497148a34311SJan Kara * checksumming that corrupts checksums so forbid that. 497248a34311SJan Kara */ 497348a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 497448a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 497548a34311SJan Kara ext4_error_inode(inode, function, line, 0, 497648a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 497748a34311SJan Kara ret = -EFSCORRUPTED; 497848a34311SJan Kara goto bad_inode; 497948a34311SJan Kara } 4980ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4981a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4982a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4983a9e7f447SDmitry Monakhov #endif 4984ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4985ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4986a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4987ac27a0ecSDave Kleikamp /* 4988ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4989ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4990ac27a0ecSDave Kleikamp */ 4991617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4992ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4993ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4994aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4995ac27a0ecSDave Kleikamp 4996b436b9beSJan Kara /* 4997b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4998b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4999b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 5000b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 5001b436b9beSJan Kara * now it is reread from disk. 5002b436b9beSJan Kara */ 5003b436b9beSJan Kara if (journal) { 5004b436b9beSJan Kara transaction_t *transaction; 5005b436b9beSJan Kara tid_t tid; 5006b436b9beSJan Kara 5007a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 5008b436b9beSJan Kara if (journal->j_running_transaction) 5009b436b9beSJan Kara transaction = journal->j_running_transaction; 5010b436b9beSJan Kara else 5011b436b9beSJan Kara transaction = journal->j_committing_transaction; 5012b436b9beSJan Kara if (transaction) 5013b436b9beSJan Kara tid = transaction->t_tid; 5014b436b9beSJan Kara else 5015b436b9beSJan Kara tid = journal->j_commit_sequence; 5016a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 5017b436b9beSJan Kara ei->i_sync_tid = tid; 5018b436b9beSJan Kara ei->i_datasync_tid = tid; 5019b436b9beSJan Kara } 5020b436b9beSJan Kara 50210040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 5022ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 5023ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 50242dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 5025617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 5026617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 5027ac27a0ecSDave Kleikamp } else { 5028eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 5029eb9b5f01STheodore Ts'o if (ret) 5030eb9b5f01STheodore Ts'o goto bad_inode; 5031ac27a0ecSDave Kleikamp } 5032814525f4SDarrick J. Wong } 5033ac27a0ecSDave Kleikamp 5034ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 5035ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 5036ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 5037ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 5038ef7f3835SKalpak Shah 5039ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 5040ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 5041ee73f9a5SJeff Layton 504225ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 504325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 5044ee73f9a5SJeff Layton ivers |= 504525ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 504625ec56b5SJean Noel Cordenner } 5047e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 5048c4f65706STheodore Ts'o } 504925ec56b5SJean Noel Cordenner 5050c4b5a614STheodore Ts'o ret = 0; 5051485c26ecSTheodore Ts'o if (ei->i_file_acl && 5052ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 50538a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 50548a363970STheodore Ts'o "iget: bad extended attribute block %llu", 505524676da4STheodore Ts'o ei->i_file_acl); 50566a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 5057485c26ecSTheodore Ts'o goto bad_inode; 5058f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 5059bc716523SLiu Song /* validate the block references in the inode */ 50608016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 50618016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 5062fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 50638016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 5064bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5065bc716523SLiu Song ret = ext4_ext_check_inode(inode); 5066bc716523SLiu Song else 50671f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 5068fe2c8191SThiemo Nagel } 5069f19d5870STao Ma } 5070567f3e9aSTheodore Ts'o if (ret) 50717a262f7cSAneesh Kumar K.V goto bad_inode; 50727a262f7cSAneesh Kumar K.V 5073ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 5074617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 5075617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 5076617ba13bSMingming Cao ext4_set_aops(inode); 5077ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 5078617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 5079617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 5080ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 50816390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 50826390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 50838a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 50848a363970STheodore Ts'o "iget: immutable or append flags " 50858a363970STheodore Ts'o "not allowed on symlinks"); 50866390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 50876390d33bSLuis R. Rodriguez goto bad_inode; 50886390d33bSLuis R. Rodriguez } 5089592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 5090a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 5091a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 509275e7566bSAl Viro inode->i_link = (char *)ei->i_data; 5093617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 5094e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 5095e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 5096e83c1397SDuane Griffin } else { 5097617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 5098ac27a0ecSDave Kleikamp } 5099563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 5100563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 5101617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 5102ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 5103ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 5104ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 5105ac27a0ecSDave Kleikamp else 5106ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 5107ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 5108393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 5109393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 5110563bdd61STheodore Ts'o } else { 51116a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 51128a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 51138a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 5114563bdd61STheodore Ts'o goto bad_inode; 5115ac27a0ecSDave Kleikamp } 51166456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 51176456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 51186456ca65STheodore Ts'o "casefold flag without casefold feature"); 511963b1e9bcSBaokun Li if (is_bad_inode(inode) && !(flags & EXT4_IGET_BAD)) { 512063b1e9bcSBaokun Li ext4_error_inode(inode, function, line, 0, 512163b1e9bcSBaokun Li "bad inode without EXT4_IGET_BAD flag"); 512263b1e9bcSBaokun Li ret = -EUCLEAN; 512363b1e9bcSBaokun Li goto bad_inode; 512463b1e9bcSBaokun Li } 5125dec214d0STahsin Erdogan 512663b1e9bcSBaokun Li brelse(iloc.bh); 51271d1fe1eeSDavid Howells unlock_new_inode(inode); 51281d1fe1eeSDavid Howells return inode; 5129ac27a0ecSDave Kleikamp 5130ac27a0ecSDave Kleikamp bad_inode: 5131567f3e9aSTheodore Ts'o brelse(iloc.bh); 51321d1fe1eeSDavid Howells iget_failed(inode); 51331d1fe1eeSDavid Howells return ERR_PTR(ret); 5134ac27a0ecSDave Kleikamp } 5135ac27a0ecSDave Kleikamp 51363f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 51373f19b2abSDavid Howells unsigned long orig_ino, 51383f19b2abSDavid Howells unsigned long ino, 51393f19b2abSDavid Howells struct ext4_inode *raw_inode) 5140a26f4992STheodore Ts'o { 51413f19b2abSDavid Howells struct inode *inode; 5142a26f4992STheodore Ts'o 51433f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 51443f19b2abSDavid Howells if (!inode) 51453f19b2abSDavid Howells return; 51463f19b2abSDavid Howells 5147ed296c6cSEric Biggers if (!inode_is_dirtytime_only(inode)) 51483f19b2abSDavid Howells return; 51493f19b2abSDavid Howells 5150a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 5151ed296c6cSEric Biggers if (inode_is_dirtytime_only(inode)) { 5152a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5153a26f4992STheodore Ts'o 51545fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 5155a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5156a26f4992STheodore Ts'o 5157a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 51583f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 51593f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 51603f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 51613f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 5162a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 51633f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 51643f19b2abSDavid Howells return; 5165a26f4992STheodore Ts'o } 5166a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5167a26f4992STheodore Ts'o } 5168a26f4992STheodore Ts'o 5169a26f4992STheodore Ts'o /* 5170a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 5171a26f4992STheodore Ts'o * the same inode table block. 5172a26f4992STheodore Ts'o */ 5173a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 5174a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 5175a26f4992STheodore Ts'o { 5176a26f4992STheodore Ts'o unsigned long ino; 5177a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 5178a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 5179a26f4992STheodore Ts'o 51800f0ff9a9STheodore Ts'o /* 51810f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 51820f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 51830f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 51840f0ff9a9STheodore Ts'o */ 51850f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 51863f19b2abSDavid Howells rcu_read_lock(); 5187a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5188a26f4992STheodore Ts'o if (ino == orig_ino) 5189a26f4992STheodore Ts'o continue; 51903f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 51913f19b2abSDavid Howells (struct ext4_inode *)buf); 5192a26f4992STheodore Ts'o } 51933f19b2abSDavid Howells rcu_read_unlock(); 5194a26f4992STheodore Ts'o } 5195a26f4992STheodore Ts'o 5196664bd38bSZhang Yi /* 5197664bd38bSZhang Yi * Post the struct inode info into an on-disk inode location in the 5198664bd38bSZhang Yi * buffer-cache. This gobbles the caller's reference to the 5199664bd38bSZhang Yi * buffer_head in the inode location struct. 5200664bd38bSZhang Yi * 5201664bd38bSZhang Yi * The caller must have write access to iloc->bh. 5202664bd38bSZhang Yi */ 5203664bd38bSZhang Yi static int ext4_do_update_inode(handle_t *handle, 5204664bd38bSZhang Yi struct inode *inode, 5205664bd38bSZhang Yi struct ext4_iloc *iloc) 5206664bd38bSZhang Yi { 5207664bd38bSZhang Yi struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5208664bd38bSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 5209664bd38bSZhang Yi struct buffer_head *bh = iloc->bh; 5210664bd38bSZhang Yi struct super_block *sb = inode->i_sb; 5211664bd38bSZhang Yi int err; 5212664bd38bSZhang Yi int need_datasync = 0, set_large_file = 0; 5213664bd38bSZhang Yi 5214664bd38bSZhang Yi spin_lock(&ei->i_raw_lock); 5215664bd38bSZhang Yi 5216664bd38bSZhang Yi /* 5217664bd38bSZhang Yi * For fields not tracked in the in-memory inode, initialise them 5218664bd38bSZhang Yi * to zero for new inodes. 5219664bd38bSZhang Yi */ 5220664bd38bSZhang Yi if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5221664bd38bSZhang Yi memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5222664bd38bSZhang Yi 5223664bd38bSZhang Yi if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) 5224664bd38bSZhang Yi need_datasync = 1; 5225664bd38bSZhang Yi if (ei->i_disksize > 0x7fffffffULL) { 5226664bd38bSZhang Yi if (!ext4_has_feature_large_file(sb) || 5227664bd38bSZhang Yi EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) 5228664bd38bSZhang Yi set_large_file = 1; 5229664bd38bSZhang Yi } 5230664bd38bSZhang Yi 5231664bd38bSZhang Yi err = ext4_fill_raw_inode(inode, raw_inode); 5232202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 5233baaae979SZhang Yi if (err) { 5234baaae979SZhang Yi EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5235baaae979SZhang Yi goto out_brelse; 5236baaae979SZhang Yi } 5237baaae979SZhang Yi 52381751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5239a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5240a26f4992STheodore Ts'o bh->b_data); 5241202ee5dfSTheodore Ts'o 52420390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 52437d8bd3c7SShijie Luo err = ext4_handle_dirty_metadata(handle, NULL, bh); 52447d8bd3c7SShijie Luo if (err) 5245baaae979SZhang Yi goto out_error; 524619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5247202ee5dfSTheodore Ts'o if (set_large_file) { 52485d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5249188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, 5250188c299eSJan Kara EXT4_SB(sb)->s_sbh, 5251188c299eSJan Kara EXT4_JTR_NONE); 5252202ee5dfSTheodore Ts'o if (err) 5253baaae979SZhang Yi goto out_error; 525405c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 5255e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 525605c2c00fSJan Kara ext4_superblock_csum_set(sb); 525705c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 5258202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5259a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, 5260a3f5cf14SJan Kara EXT4_SB(sb)->s_sbh); 5261202ee5dfSTheodore Ts'o } 5262b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5263baaae979SZhang Yi out_error: 5264baaae979SZhang Yi ext4_std_error(inode->i_sb, err); 5265ac27a0ecSDave Kleikamp out_brelse: 5266ac27a0ecSDave Kleikamp brelse(bh); 5267ac27a0ecSDave Kleikamp return err; 5268ac27a0ecSDave Kleikamp } 5269ac27a0ecSDave Kleikamp 5270ac27a0ecSDave Kleikamp /* 5271617ba13bSMingming Cao * ext4_write_inode() 5272ac27a0ecSDave Kleikamp * 5273ac27a0ecSDave Kleikamp * We are called from a few places: 5274ac27a0ecSDave Kleikamp * 527587f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5276ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 52774907cb7bSAnatol Pomozov * transaction to commit. 5278ac27a0ecSDave Kleikamp * 527987f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 528087f7e416STheodore Ts'o * We wait on commit, if told to. 5281ac27a0ecSDave Kleikamp * 528287f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 528387f7e416STheodore Ts'o * We wait on commit, if told to. 5284ac27a0ecSDave Kleikamp * 5285ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5286ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 528787f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 528887f7e416STheodore Ts'o * writeback. 5289ac27a0ecSDave Kleikamp * 5290ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5291ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5292ac27a0ecSDave Kleikamp * which we are interested. 5293ac27a0ecSDave Kleikamp * 5294ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5295ac27a0ecSDave Kleikamp * 5296ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5297ac27a0ecSDave Kleikamp * stuff(); 5298ac27a0ecSDave Kleikamp * inode->i_size = expr; 5299ac27a0ecSDave Kleikamp * 530087f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 530187f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 530287f7e416STheodore Ts'o * superblock's dirty inode list. 5303ac27a0ecSDave Kleikamp */ 5304a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5305ac27a0ecSDave Kleikamp { 530691ac6f43SFrank Mayhar int err; 530791ac6f43SFrank Mayhar 530818f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 530918f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5310ac27a0ecSDave Kleikamp return 0; 5311ac27a0ecSDave Kleikamp 531218f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 531318f2c4fcSTheodore Ts'o return -EIO; 531418f2c4fcSTheodore Ts'o 531591ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5316617ba13bSMingming Cao if (ext4_journal_current_handle()) { 53174978c659SJan Kara ext4_debug("called recursively, non-PF_MEMALLOC!\n"); 5318ac27a0ecSDave Kleikamp dump_stack(); 5319ac27a0ecSDave Kleikamp return -EIO; 5320ac27a0ecSDave Kleikamp } 5321ac27a0ecSDave Kleikamp 532210542c22SJan Kara /* 532310542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 532410542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 532510542c22SJan Kara * written. 532610542c22SJan Kara */ 532710542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5328ac27a0ecSDave Kleikamp return 0; 5329ac27a0ecSDave Kleikamp 5330aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 533118f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 533291ac6f43SFrank Mayhar } else { 533391ac6f43SFrank Mayhar struct ext4_iloc iloc; 533491ac6f43SFrank Mayhar 53358016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 533691ac6f43SFrank Mayhar if (err) 533791ac6f43SFrank Mayhar return err; 533810542c22SJan Kara /* 533910542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 534010542c22SJan Kara * it here separately for each inode. 534110542c22SJan Kara */ 534210542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5343830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5344830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 534554d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5346c398eda0STheodore Ts'o "IO error syncing inode"); 5347830156c7SFrank Mayhar err = -EIO; 5348830156c7SFrank Mayhar } 5349fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 535091ac6f43SFrank Mayhar } 535191ac6f43SFrank Mayhar return err; 5352ac27a0ecSDave Kleikamp } 5353ac27a0ecSDave Kleikamp 5354ac27a0ecSDave Kleikamp /* 5355ccd16945SMatthew Wilcox (Oracle) * In data=journal mode ext4_journalled_invalidate_folio() may fail to invalidate 5356ccd16945SMatthew Wilcox (Oracle) * buffers that are attached to a folio straddling i_size and are undergoing 535753e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 535853e87268SJan Kara */ 535953e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 536053e87268SJan Kara { 536153e87268SJan Kara unsigned offset; 536253e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 536353e87268SJan Kara tid_t commit_tid = 0; 536453e87268SJan Kara int ret; 536553e87268SJan Kara 536609cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 536753e87268SJan Kara /* 5368ccd16945SMatthew Wilcox (Oracle) * If the folio is fully truncated, we don't need to wait for any commit 5369ccd16945SMatthew Wilcox (Oracle) * (and we even should not as __ext4_journalled_invalidate_folio() may 5370ccd16945SMatthew Wilcox (Oracle) * strip all buffers from the folio but keep the folio dirty which can then 5371ccd16945SMatthew Wilcox (Oracle) * confuse e.g. concurrent ext4_writepage() seeing dirty folio without 5372565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5373ccd16945SMatthew Wilcox (Oracle) * the folio remain valid. This is most beneficial for the common case of 5374565333a1Syangerkun * blocksize == PAGESIZE. 537553e87268SJan Kara */ 5376565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 537753e87268SJan Kara return; 537853e87268SJan Kara while (1) { 5379ccd16945SMatthew Wilcox (Oracle) struct folio *folio = filemap_lock_folio(inode->i_mapping, 538009cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 5381ccd16945SMatthew Wilcox (Oracle) if (!folio) 538253e87268SJan Kara return; 5383ccd16945SMatthew Wilcox (Oracle) ret = __ext4_journalled_invalidate_folio(folio, offset, 5384ccd16945SMatthew Wilcox (Oracle) folio_size(folio) - offset); 5385ccd16945SMatthew Wilcox (Oracle) folio_unlock(folio); 5386ccd16945SMatthew Wilcox (Oracle) folio_put(folio); 538753e87268SJan Kara if (ret != -EBUSY) 538853e87268SJan Kara return; 538953e87268SJan Kara commit_tid = 0; 539053e87268SJan Kara read_lock(&journal->j_state_lock); 539153e87268SJan Kara if (journal->j_committing_transaction) 539253e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 539353e87268SJan Kara read_unlock(&journal->j_state_lock); 539453e87268SJan Kara if (commit_tid) 539553e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 539653e87268SJan Kara } 539753e87268SJan Kara } 539853e87268SJan Kara 539953e87268SJan Kara /* 5400617ba13bSMingming Cao * ext4_setattr() 5401ac27a0ecSDave Kleikamp * 5402ac27a0ecSDave Kleikamp * Called from notify_change. 5403ac27a0ecSDave Kleikamp * 5404ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5405ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5406ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5407ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5408ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5409ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5410ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5411ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5412ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5413ac27a0ecSDave Kleikamp * 5414678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5415678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5416678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5417678aaf48SJan Kara * This way we are sure that all the data written in the previous 5418678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5419678aaf48SJan Kara * writeback). 5420678aaf48SJan Kara * 5421f340b3d9Shongnanli * Called with inode->i_rwsem down. 5422ac27a0ecSDave Kleikamp */ 5423c1632a0fSChristian Brauner int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 5424549c7297SChristian Brauner struct iattr *attr) 5425ac27a0ecSDave Kleikamp { 54262b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5427ac27a0ecSDave Kleikamp int error, rc = 0; 54283d287de3SDmitry Monakhov int orphan = 0; 5429ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5430a642c2c0SJeff Layton bool inc_ivers = true; 5431ac27a0ecSDave Kleikamp 54320db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 54330db1ff22STheodore Ts'o return -EIO; 54340db1ff22STheodore Ts'o 543502b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 543602b016caSTheodore Ts'o return -EPERM; 543702b016caSTheodore Ts'o 543802b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 543902b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 544002b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 544102b016caSTheodore Ts'o return -EPERM; 544202b016caSTheodore Ts'o 5443c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, attr); 5444ac27a0ecSDave Kleikamp if (error) 5445ac27a0ecSDave Kleikamp return error; 5446ac27a0ecSDave Kleikamp 54473ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 54483ce2b8ddSEric Biggers if (error) 54493ce2b8ddSEric Biggers return error; 54503ce2b8ddSEric Biggers 5451c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5452c93d8f88SEric Biggers if (error) 5453c93d8f88SEric Biggers return error; 5454c93d8f88SEric Biggers 5455f861646aSChristian Brauner if (is_quota_modification(idmap, inode, attr)) { 5456a7cdadeeSJan Kara error = dquot_initialize(inode); 5457a7cdadeeSJan Kara if (error) 5458a7cdadeeSJan Kara return error; 5459a7cdadeeSJan Kara } 54602729cfdcSHarshad Shirwadkar 54610dbe12f2SChristian Brauner if (i_uid_needs_update(idmap, attr, inode) || 54620dbe12f2SChristian Brauner i_gid_needs_update(idmap, attr, inode)) { 5463ac27a0ecSDave Kleikamp handle_t *handle; 5464ac27a0ecSDave Kleikamp 5465ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5466ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 54679924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 54689924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5469194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5470ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5471ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5472ac27a0ecSDave Kleikamp goto err_out; 5473ac27a0ecSDave Kleikamp } 54747a9ca53aSTahsin Erdogan 54757a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 54767a9ca53aSTahsin Erdogan * counts xattr inode references. 54777a9ca53aSTahsin Erdogan */ 54787a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5479f861646aSChristian Brauner error = dquot_transfer(idmap, inode, attr); 54807a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 54817a9ca53aSTahsin Erdogan 5482ac27a0ecSDave Kleikamp if (error) { 5483617ba13bSMingming Cao ext4_journal_stop(handle); 5484ac27a0ecSDave Kleikamp return error; 5485ac27a0ecSDave Kleikamp } 5486ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5487ac27a0ecSDave Kleikamp * one transaction */ 54880dbe12f2SChristian Brauner i_uid_update(idmap, attr, inode); 54890dbe12f2SChristian Brauner i_gid_update(idmap, attr, inode); 5490617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5491617ba13bSMingming Cao ext4_journal_stop(handle); 5492512c15efSPan Bian if (unlikely(error)) { 54934209ae12SHarshad Shirwadkar return error; 5494ac27a0ecSDave Kleikamp } 5495512c15efSPan Bian } 5496ac27a0ecSDave Kleikamp 54973da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 54985208386cSJan Kara handle_t *handle; 54993da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5500f4534c9fSYe Bin loff_t old_disksize; 5501b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5502562c72aaSChristoph Hellwig 550312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5504e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5505e2b46574SEric Sandeen 5506aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 55070c095c7fSTheodore Ts'o return -EFBIG; 5508e2b46574SEric Sandeen } 5509aa75f4d3SHarshad Shirwadkar } 5510aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 55113da40c7bSJosef Bacik return -EINVAL; 5512aa75f4d3SHarshad Shirwadkar } 5513dff6efc3SChristoph Hellwig 5514a642c2c0SJeff Layton if (attr->ia_size == inode->i_size) 5515a642c2c0SJeff Layton inc_ivers = false; 5516dff6efc3SChristoph Hellwig 5517b9c1c267SJan Kara if (shrink) { 5518b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 55195208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 55205208386cSJan Kara attr->ia_size); 55215208386cSJan Kara if (error) 55225208386cSJan Kara goto err_out; 55235208386cSJan Kara } 5524b9c1c267SJan Kara /* 5525b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5526b9c1c267SJan Kara * for dio in flight. 5527b9c1c267SJan Kara */ 5528b9c1c267SJan Kara inode_dio_wait(inode); 5529b9c1c267SJan Kara } 5530b9c1c267SJan Kara 5531d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 5532b9c1c267SJan Kara 5533b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5534b9c1c267SJan Kara if (rc) { 5535d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 5536aa75f4d3SHarshad Shirwadkar goto err_out; 5537b9c1c267SJan Kara } 5538b9c1c267SJan Kara 55393da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 55409924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5541ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5542ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5543b9c1c267SJan Kara goto out_mmap_sem; 5544ac27a0ecSDave Kleikamp } 55453da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5546617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 55473d287de3SDmitry Monakhov orphan = 1; 55483d287de3SDmitry Monakhov } 5549911af577SEryu Guan /* 5550911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5551911af577SEryu Guan * update c/mtime in shrink case below 5552911af577SEryu Guan */ 5553911af577SEryu Guan if (!shrink) { 5554eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5555911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5556911af577SEryu Guan } 5557aa75f4d3SHarshad Shirwadkar 5558aa75f4d3SHarshad Shirwadkar if (shrink) 5559a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, 5560aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5561aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 55629725958bSXin Yin EXT_MAX_BLOCKS - 1); 5563aa75f4d3SHarshad Shirwadkar else 5564aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5565a80f7fcfSHarshad Shirwadkar handle, inode, 5566aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5567aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5568aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5569aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5570aa75f4d3SHarshad Shirwadkar 557190e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5572f4534c9fSYe Bin old_disksize = EXT4_I(inode)->i_disksize; 5573617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5574617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5575ac27a0ecSDave Kleikamp if (!error) 5576ac27a0ecSDave Kleikamp error = rc; 557790e775b7SJan Kara /* 557890e775b7SJan Kara * We have to update i_size under i_data_sem together 557990e775b7SJan Kara * with i_disksize to avoid races with writeback code 558090e775b7SJan Kara * running ext4_wb_update_i_disksize(). 558190e775b7SJan Kara */ 558290e775b7SJan Kara if (!error) 558390e775b7SJan Kara i_size_write(inode, attr->ia_size); 5584f4534c9fSYe Bin else 5585f4534c9fSYe Bin EXT4_I(inode)->i_disksize = old_disksize; 558690e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5587617ba13bSMingming Cao ext4_journal_stop(handle); 5588b9c1c267SJan Kara if (error) 5589b9c1c267SJan Kara goto out_mmap_sem; 559082a25b02SJan Kara if (!shrink) { 5591b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5592b9c1c267SJan Kara inode->i_size); 5593b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 559482a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5595b9c1c267SJan Kara } 5596430657b6SRoss Zwisler } 5597430657b6SRoss Zwisler 559853e87268SJan Kara /* 559953e87268SJan Kara * Truncate pagecache after we've waited for commit 560053e87268SJan Kara * in data=journal mode to make pages freeable. 560153e87268SJan Kara */ 56027caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5603b9c1c267SJan Kara /* 5604b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5605b9c1c267SJan Kara * truncate possible preallocated blocks. 5606b9c1c267SJan Kara */ 5607b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 56082c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 56092c98eb5eSTheodore Ts'o if (rc) 56102c98eb5eSTheodore Ts'o error = rc; 56112c98eb5eSTheodore Ts'o } 5612b9c1c267SJan Kara out_mmap_sem: 5613d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 56143da40c7bSJosef Bacik } 5615ac27a0ecSDave Kleikamp 56162c98eb5eSTheodore Ts'o if (!error) { 5617a642c2c0SJeff Layton if (inc_ivers) 5618a642c2c0SJeff Layton inode_inc_iversion(inode); 5619c1632a0fSChristian Brauner setattr_copy(idmap, inode, attr); 56201025774cSChristoph Hellwig mark_inode_dirty(inode); 56211025774cSChristoph Hellwig } 56221025774cSChristoph Hellwig 56231025774cSChristoph Hellwig /* 56241025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 56251025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 56261025774cSChristoph Hellwig */ 56273d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5628617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5629ac27a0ecSDave Kleikamp 56302c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 563113e83a49SChristian Brauner rc = posix_acl_chmod(idmap, dentry, inode->i_mode); 5632ac27a0ecSDave Kleikamp 5633ac27a0ecSDave Kleikamp err_out: 5634aa75f4d3SHarshad Shirwadkar if (error) 5635617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5636ac27a0ecSDave Kleikamp if (!error) 5637ac27a0ecSDave Kleikamp error = rc; 5638ac27a0ecSDave Kleikamp return error; 5639ac27a0ecSDave Kleikamp } 5640ac27a0ecSDave Kleikamp 56418434ef1dSEric Biggers u32 ext4_dio_alignment(struct inode *inode) 56428434ef1dSEric Biggers { 56438434ef1dSEric Biggers if (fsverity_active(inode)) 56448434ef1dSEric Biggers return 0; 56458434ef1dSEric Biggers if (ext4_should_journal_data(inode)) 56468434ef1dSEric Biggers return 0; 56478434ef1dSEric Biggers if (ext4_has_inline_data(inode)) 56488434ef1dSEric Biggers return 0; 56498434ef1dSEric Biggers if (IS_ENCRYPTED(inode)) { 56508434ef1dSEric Biggers if (!fscrypt_dio_supported(inode)) 56518434ef1dSEric Biggers return 0; 56528434ef1dSEric Biggers return i_blocksize(inode); 56538434ef1dSEric Biggers } 56548434ef1dSEric Biggers return 1; /* use the iomap defaults */ 56558434ef1dSEric Biggers } 56568434ef1dSEric Biggers 5657b74d24f7SChristian Brauner int ext4_getattr(struct mnt_idmap *idmap, const struct path *path, 5658549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags) 56593e3398a0SMingming Cao { 566099652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 566199652ea5SDavid Howells struct ext4_inode *raw_inode; 566299652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 566399652ea5SDavid Howells unsigned int flags; 56643e3398a0SMingming Cao 5665d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5666d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 566799652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 566899652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 566999652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 567099652ea5SDavid Howells } 567199652ea5SDavid Howells 56728434ef1dSEric Biggers /* 56738434ef1dSEric Biggers * Return the DIO alignment restrictions if requested. We only return 56748434ef1dSEric Biggers * this information when requested, since on encrypted files it might 56758434ef1dSEric Biggers * take a fair bit of work to get if the file wasn't opened recently. 56768434ef1dSEric Biggers */ 56778434ef1dSEric Biggers if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) { 56788434ef1dSEric Biggers u32 dio_align = ext4_dio_alignment(inode); 56798434ef1dSEric Biggers 56808434ef1dSEric Biggers stat->result_mask |= STATX_DIOALIGN; 56818434ef1dSEric Biggers if (dio_align == 1) { 56828434ef1dSEric Biggers struct block_device *bdev = inode->i_sb->s_bdev; 56838434ef1dSEric Biggers 56848434ef1dSEric Biggers /* iomap defaults */ 56858434ef1dSEric Biggers stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; 56868434ef1dSEric Biggers stat->dio_offset_align = bdev_logical_block_size(bdev); 56878434ef1dSEric Biggers } else { 56888434ef1dSEric Biggers stat->dio_mem_align = dio_align; 56898434ef1dSEric Biggers stat->dio_offset_align = dio_align; 56908434ef1dSEric Biggers } 56918434ef1dSEric Biggers } 56928434ef1dSEric Biggers 569399652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 569499652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 569599652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 569699652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 569799652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 569899652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 569999652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 570099652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 570199652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 570299652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 570399652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 57041f607195SEric Biggers if (flags & EXT4_VERITY_FL) 57051f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 570699652ea5SDavid Howells 57073209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 57083209f68bSDavid Howells STATX_ATTR_COMPRESSED | 57093209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 57103209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 57111f607195SEric Biggers STATX_ATTR_NODUMP | 57121f607195SEric Biggers STATX_ATTR_VERITY); 57133209f68bSDavid Howells 5714b74d24f7SChristian Brauner generic_fillattr(idmap, inode, stat); 571599652ea5SDavid Howells return 0; 571699652ea5SDavid Howells } 571799652ea5SDavid Howells 5718b74d24f7SChristian Brauner int ext4_file_getattr(struct mnt_idmap *idmap, 5719549c7297SChristian Brauner const struct path *path, struct kstat *stat, 572099652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 572199652ea5SDavid Howells { 572299652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 572399652ea5SDavid Howells u64 delalloc_blocks; 572499652ea5SDavid Howells 5725b74d24f7SChristian Brauner ext4_getattr(idmap, path, stat, request_mask, query_flags); 57263e3398a0SMingming Cao 57273e3398a0SMingming Cao /* 57289206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 57299206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 57309206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5731d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 57329206c561SAndreas Dilger */ 57339206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 57349206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 57359206c561SAndreas Dilger 57369206c561SAndreas Dilger /* 57373e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 57383e3398a0SMingming Cao * otherwise in the case of system crash before the real block 57393e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 57403e3398a0SMingming Cao * on-disk file blocks. 57413e3398a0SMingming Cao * We always keep i_blocks updated together with real 57423e3398a0SMingming Cao * allocation. But to not confuse with user, stat 57433e3398a0SMingming Cao * will return the blocks that include the delayed allocation 57443e3398a0SMingming Cao * blocks for this file. 57453e3398a0SMingming Cao */ 574696607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 574796607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 57488af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 57493e3398a0SMingming Cao return 0; 57503e3398a0SMingming Cao } 5751ac27a0ecSDave Kleikamp 5752fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5753fffb2739SJan Kara int pextents) 5754a02908f1SMingming Cao { 575512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5756fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5757fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5758a02908f1SMingming Cao } 5759ac51d837STheodore Ts'o 5760a02908f1SMingming Cao /* 5761a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5762a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5763a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5764a02908f1SMingming Cao * 5765a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 57664907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5767a02908f1SMingming Cao * they could still across block group boundary. 5768a02908f1SMingming Cao * 5769a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5770a02908f1SMingming Cao */ 5771dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5772fffb2739SJan Kara int pextents) 5773a02908f1SMingming Cao { 57748df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 57758df9675fSTheodore Ts'o int gdpblocks; 5776a02908f1SMingming Cao int idxblocks; 57777fc51f92SXU pengfei int ret; 5778a02908f1SMingming Cao 5779a02908f1SMingming Cao /* 5780fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5781fffb2739SJan Kara * to @pextents physical extents? 5782a02908f1SMingming Cao */ 5783fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5784a02908f1SMingming Cao 5785a02908f1SMingming Cao ret = idxblocks; 5786a02908f1SMingming Cao 5787a02908f1SMingming Cao /* 5788a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5789a02908f1SMingming Cao * to account 5790a02908f1SMingming Cao */ 5791fffb2739SJan Kara groups = idxblocks + pextents; 5792a02908f1SMingming Cao gdpblocks = groups; 57938df9675fSTheodore Ts'o if (groups > ngroups) 57948df9675fSTheodore Ts'o groups = ngroups; 5795a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5796a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5797a02908f1SMingming Cao 5798a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5799a02908f1SMingming Cao ret += groups + gdpblocks; 5800a02908f1SMingming Cao 5801a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5802a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5803ac27a0ecSDave Kleikamp 5804ac27a0ecSDave Kleikamp return ret; 5805ac27a0ecSDave Kleikamp } 5806ac27a0ecSDave Kleikamp 5807ac27a0ecSDave Kleikamp /* 580825985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5809f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5810f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5811a02908f1SMingming Cao * 5812525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5813a02908f1SMingming Cao * 5814525f4ed8SMingming Cao * We need to consider the worse case, when 5815a02908f1SMingming Cao * one new block per extent. 5816a02908f1SMingming Cao */ 5817a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5818a02908f1SMingming Cao { 5819a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5820a02908f1SMingming Cao int ret; 5821a02908f1SMingming Cao 5822fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5823a02908f1SMingming Cao 5824a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5825a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5826a02908f1SMingming Cao ret += bpp; 5827a02908f1SMingming Cao return ret; 5828a02908f1SMingming Cao } 5829f3bd1f3fSMingming Cao 5830f3bd1f3fSMingming Cao /* 5831f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5832f3bd1f3fSMingming Cao * 5833f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 583479e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5835f3bd1f3fSMingming Cao * 5836f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5837f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5838f3bd1f3fSMingming Cao */ 5839f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5840f3bd1f3fSMingming Cao { 5841f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5842f3bd1f3fSMingming Cao } 5843f3bd1f3fSMingming Cao 5844a02908f1SMingming Cao /* 5845617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5846ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5847ac27a0ecSDave Kleikamp */ 5848617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5849617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5850ac27a0ecSDave Kleikamp { 5851ac27a0ecSDave Kleikamp int err = 0; 5852ac27a0ecSDave Kleikamp 5853a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5854a6758309SVasily Averin put_bh(iloc->bh); 58550db1ff22STheodore Ts'o return -EIO; 5856a6758309SVasily Averin } 5857a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5858aa75f4d3SHarshad Shirwadkar 5859ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5860ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5861ac27a0ecSDave Kleikamp 5862dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5863830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5864ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5865ac27a0ecSDave Kleikamp return err; 5866ac27a0ecSDave Kleikamp } 5867ac27a0ecSDave Kleikamp 5868ac27a0ecSDave Kleikamp /* 5869ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5870ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5871ac27a0ecSDave Kleikamp */ 5872ac27a0ecSDave Kleikamp 5873ac27a0ecSDave Kleikamp int 5874617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5875617ba13bSMingming Cao struct ext4_iloc *iloc) 5876ac27a0ecSDave Kleikamp { 58770390131bSFrank Mayhar int err; 58780390131bSFrank Mayhar 58790db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 58800db1ff22STheodore Ts'o return -EIO; 58810db1ff22STheodore Ts'o 5882617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5883ac27a0ecSDave Kleikamp if (!err) { 5884ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5885188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, 5886188c299eSJan Kara iloc->bh, EXT4_JTR_NONE); 5887ac27a0ecSDave Kleikamp if (err) { 5888ac27a0ecSDave Kleikamp brelse(iloc->bh); 5889ac27a0ecSDave Kleikamp iloc->bh = NULL; 5890ac27a0ecSDave Kleikamp } 5891ac27a0ecSDave Kleikamp } 5892617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5893ac27a0ecSDave Kleikamp return err; 5894ac27a0ecSDave Kleikamp } 5895ac27a0ecSDave Kleikamp 5896c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5897c03b45b8SMiao Xie unsigned int new_extra_isize, 5898c03b45b8SMiao Xie struct ext4_iloc *iloc, 5899c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5900c03b45b8SMiao Xie { 5901c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5902c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 59034ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 59044ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5905c03b45b8SMiao Xie int error; 5906c03b45b8SMiao Xie 59074ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 59084ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 59094ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 59104ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 59114ea99936STheodore Ts'o ei->i_extra_isize, 59124ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 59134ea99936STheodore Ts'o return -EFSCORRUPTED; 59144ea99936STheodore Ts'o } 59154ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 59164ea99936STheodore Ts'o (new_extra_isize < 4) || 59174ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 59184ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 59194ea99936STheodore Ts'o 5920c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5921c03b45b8SMiao Xie 5922c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5923c03b45b8SMiao Xie 5924c03b45b8SMiao Xie /* No extended attributes present */ 5925c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5926c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5927c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5928c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5929c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5930c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5931c03b45b8SMiao Xie return 0; 5932c03b45b8SMiao Xie } 5933c03b45b8SMiao Xie 59348994d113SJan Kara /* 59358994d113SJan Kara * We may need to allocate external xattr block so we need quotas 59368994d113SJan Kara * initialized. Here we can be called with various locks held so we 59378994d113SJan Kara * cannot affort to initialize quotas ourselves. So just bail. 59388994d113SJan Kara */ 59398994d113SJan Kara if (dquot_initialize_needed(inode)) 59408994d113SJan Kara return -EAGAIN; 59418994d113SJan Kara 5942c03b45b8SMiao Xie /* try to expand with EAs present */ 5943c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5944c03b45b8SMiao Xie raw_inode, handle); 5945c03b45b8SMiao Xie if (error) { 5946c03b45b8SMiao Xie /* 5947c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5948c03b45b8SMiao Xie */ 5949c03b45b8SMiao Xie *no_expand = 1; 5950c03b45b8SMiao Xie } 5951c03b45b8SMiao Xie 5952c03b45b8SMiao Xie return error; 5953c03b45b8SMiao Xie } 5954c03b45b8SMiao Xie 5955ac27a0ecSDave Kleikamp /* 59566dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 59576dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 59586dd4ee7cSKalpak Shah */ 5959cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 59601d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 59611d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 59621d03ec98SAneesh Kumar K.V handle_t *handle) 59636dd4ee7cSKalpak Shah { 59643b10fdc6SMiao Xie int no_expand; 59653b10fdc6SMiao Xie int error; 59666dd4ee7cSKalpak Shah 5967cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5968cf0a5e81SMiao Xie return -EOVERFLOW; 5969cf0a5e81SMiao Xie 5970cf0a5e81SMiao Xie /* 5971cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5972cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5973cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5974cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5975cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5976cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5977cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5978cf0a5e81SMiao Xie */ 59796cb367c2SJan Kara if (ext4_journal_extend(handle, 598083448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5981cf0a5e81SMiao Xie return -ENOSPC; 59826dd4ee7cSKalpak Shah 59833b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5984cf0a5e81SMiao Xie return -EBUSY; 59853b10fdc6SMiao Xie 5986c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5987c03b45b8SMiao Xie handle, &no_expand); 59883b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5989c03b45b8SMiao Xie 5990c03b45b8SMiao Xie return error; 59916dd4ee7cSKalpak Shah } 59926dd4ee7cSKalpak Shah 5993c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5994c03b45b8SMiao Xie unsigned int new_extra_isize, 5995c03b45b8SMiao Xie struct ext4_iloc *iloc) 5996c03b45b8SMiao Xie { 5997c03b45b8SMiao Xie handle_t *handle; 5998c03b45b8SMiao Xie int no_expand; 5999c03b45b8SMiao Xie int error, rc; 6000c03b45b8SMiao Xie 6001c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 6002c03b45b8SMiao Xie brelse(iloc->bh); 6003c03b45b8SMiao Xie return -EOVERFLOW; 6004c03b45b8SMiao Xie } 6005c03b45b8SMiao Xie 6006c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 6007c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 6008c03b45b8SMiao Xie if (IS_ERR(handle)) { 6009c03b45b8SMiao Xie error = PTR_ERR(handle); 6010c03b45b8SMiao Xie brelse(iloc->bh); 6011c03b45b8SMiao Xie return error; 6012c03b45b8SMiao Xie } 6013c03b45b8SMiao Xie 6014c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 6015c03b45b8SMiao Xie 6016ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 6017188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, 6018188c299eSJan Kara EXT4_JTR_NONE); 60193b10fdc6SMiao Xie if (error) { 6020c03b45b8SMiao Xie brelse(iloc->bh); 60217f420d64SDan Carpenter goto out_unlock; 60223b10fdc6SMiao Xie } 6023cf0a5e81SMiao Xie 6024c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 6025c03b45b8SMiao Xie handle, &no_expand); 6026c03b45b8SMiao Xie 6027c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 6028c03b45b8SMiao Xie if (!error) 6029c03b45b8SMiao Xie error = rc; 6030c03b45b8SMiao Xie 60317f420d64SDan Carpenter out_unlock: 6032c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 6033c03b45b8SMiao Xie ext4_journal_stop(handle); 60343b10fdc6SMiao Xie return error; 60356dd4ee7cSKalpak Shah } 60366dd4ee7cSKalpak Shah 60376dd4ee7cSKalpak Shah /* 6038ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 6039ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 6040ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 6041ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 6042ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 6043ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 6044ac27a0ecSDave Kleikamp * 6045ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 6046ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 6047ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 6048ac27a0ecSDave Kleikamp * we start and wait on commits. 6049ac27a0ecSDave Kleikamp */ 60504209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 60514209ae12SHarshad Shirwadkar const char *func, unsigned int line) 6052ac27a0ecSDave Kleikamp { 6053617ba13bSMingming Cao struct ext4_iloc iloc; 60546dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 6055cf0a5e81SMiao Xie int err; 6056ac27a0ecSDave Kleikamp 6057ac27a0ecSDave Kleikamp might_sleep(); 60587ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 6059617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 60605e1021f2SEryu Guan if (err) 60614209ae12SHarshad Shirwadkar goto out; 6062cf0a5e81SMiao Xie 6063cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 6064cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 60656dd4ee7cSKalpak Shah iloc, handle); 6066cf0a5e81SMiao Xie 60674209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 60684209ae12SHarshad Shirwadkar out: 60694209ae12SHarshad Shirwadkar if (unlikely(err)) 60704209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 60714209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 60724209ae12SHarshad Shirwadkar return err; 6073ac27a0ecSDave Kleikamp } 6074ac27a0ecSDave Kleikamp 6075ac27a0ecSDave Kleikamp /* 6076617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 6077ac27a0ecSDave Kleikamp * 6078ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 6079ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 6080ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 6081ac27a0ecSDave Kleikamp * 60825dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 6083ac27a0ecSDave Kleikamp * are allocated to the file. 6084ac27a0ecSDave Kleikamp * 6085ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 6086ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 6087ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 6088ac27a0ecSDave Kleikamp */ 6089aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 6090ac27a0ecSDave Kleikamp { 6091ac27a0ecSDave Kleikamp handle_t *handle; 6092ac27a0ecSDave Kleikamp 60939924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 6094ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6095ac27a0ecSDave Kleikamp return; 6096e2728c56SEric Biggers ext4_mark_inode_dirty(handle, inode); 6097e2728c56SEric Biggers ext4_journal_stop(handle); 6098ac27a0ecSDave Kleikamp } 6099ac27a0ecSDave Kleikamp 6100617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 6101ac27a0ecSDave Kleikamp { 6102ac27a0ecSDave Kleikamp journal_t *journal; 6103ac27a0ecSDave Kleikamp handle_t *handle; 6104ac27a0ecSDave Kleikamp int err; 6105c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 6106ac27a0ecSDave Kleikamp 6107ac27a0ecSDave Kleikamp /* 6108ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 6109ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 6110ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 6111ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 6112ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 6113ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 6114ac27a0ecSDave Kleikamp * nobody is changing anything. 6115ac27a0ecSDave Kleikamp */ 6116ac27a0ecSDave Kleikamp 6117617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 61180390131bSFrank Mayhar if (!journal) 61190390131bSFrank Mayhar return 0; 6120d699594dSDave Hansen if (is_journal_aborted(journal)) 6121ac27a0ecSDave Kleikamp return -EROFS; 6122ac27a0ecSDave Kleikamp 612317335dccSDmitry Monakhov /* Wait for all existing dio workers */ 612417335dccSDmitry Monakhov inode_dio_wait(inode); 612517335dccSDmitry Monakhov 61264c546592SDaeho Jeong /* 61274c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 61284c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 61294c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 61304c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 61314c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 61324c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 61334c546592SDaeho Jeong */ 61344c546592SDaeho Jeong if (val) { 6135d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 61364c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 61374c546592SDaeho Jeong if (err < 0) { 6138d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 61394c546592SDaeho Jeong return err; 61404c546592SDaeho Jeong } 61414c546592SDaeho Jeong } 61424c546592SDaeho Jeong 6143bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 6144dab291afSMingming Cao jbd2_journal_lock_updates(journal); 6145ac27a0ecSDave Kleikamp 6146ac27a0ecSDave Kleikamp /* 6147ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 6148ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 6149ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 6150ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 6151ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 6152ac27a0ecSDave Kleikamp */ 6153ac27a0ecSDave Kleikamp 6154ac27a0ecSDave Kleikamp if (val) 615512e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 61565872ddaaSYongqiang Yang else { 615701d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 61584f879ca6SJan Kara if (err < 0) { 61594f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 6160bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 61614f879ca6SJan Kara return err; 61624f879ca6SJan Kara } 616312e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 61645872ddaaSYongqiang Yang } 6165617ba13bSMingming Cao ext4_set_aops(inode); 6166ac27a0ecSDave Kleikamp 6167dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 6168bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 6169c8585c6fSDaeho Jeong 61704c546592SDaeho Jeong if (val) 6171d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 6172ac27a0ecSDave Kleikamp 6173ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6174ac27a0ecSDave Kleikamp 61759924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 6176ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6177ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6178ac27a0ecSDave Kleikamp 6179aa75f4d3SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, 6180e85c81baSXin Yin EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, handle); 6181617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 61820390131bSFrank Mayhar ext4_handle_sync(handle); 6183617ba13bSMingming Cao ext4_journal_stop(handle); 6184617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6185ac27a0ecSDave Kleikamp 6186ac27a0ecSDave Kleikamp return err; 6187ac27a0ecSDave Kleikamp } 61882e9ee850SAneesh Kumar K.V 6189188c299eSJan Kara static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, 6190188c299eSJan Kara struct buffer_head *bh) 61912e9ee850SAneesh Kumar K.V { 61922e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 61932e9ee850SAneesh Kumar K.V } 61942e9ee850SAneesh Kumar K.V 6195401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 61962e9ee850SAneesh Kumar K.V { 619711bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 6198c2ec175cSNick Piggin struct page *page = vmf->page; 61992e9ee850SAneesh Kumar K.V loff_t size; 62002e9ee850SAneesh Kumar K.V unsigned long len; 6201401b25aaSSouptick Joarder int err; 6202401b25aaSSouptick Joarder vm_fault_t ret; 62032e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6204496ad9aaSAl Viro struct inode *inode = file_inode(file); 62052e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 62069ea7df53SJan Kara handle_t *handle; 62079ea7df53SJan Kara get_block_t *get_block; 62089ea7df53SJan Kara int retries = 0; 62092e9ee850SAneesh Kumar K.V 621002b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 621102b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 621202b016caSTheodore Ts'o 62138e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6214041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6215ea3d7209SJan Kara 6216d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping); 62177b4cc978SEric Biggers 6218401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6219401b25aaSSouptick Joarder if (err) 62207b4cc978SEric Biggers goto out_ret; 62217b4cc978SEric Biggers 622264a9f144SMauricio Faria de Oliveira /* 622364a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 622464a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 622564a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 622664a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 622764a9f144SMauricio Faria de Oliveira */ 622864a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 622964a9f144SMauricio Faria de Oliveira goto retry_alloc; 623064a9f144SMauricio Faria de Oliveira 62319ea7df53SJan Kara /* Delalloc case is easy... */ 62329ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 62339ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 62349ea7df53SJan Kara do { 6235401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 62369ea7df53SJan Kara ext4_da_get_block_prep); 6237401b25aaSSouptick Joarder } while (err == -ENOSPC && 62389ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 62399ea7df53SJan Kara goto out_ret; 62402e9ee850SAneesh Kumar K.V } 62410e499890SDarrick J. Wong 62420e499890SDarrick J. Wong lock_page(page); 62439ea7df53SJan Kara size = i_size_read(inode); 62449ea7df53SJan Kara /* Page got truncated from under us? */ 62459ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 62469ea7df53SJan Kara unlock_page(page); 62479ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 62489ea7df53SJan Kara goto out; 62490e499890SDarrick J. Wong } 62502e9ee850SAneesh Kumar K.V 625109cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 625209cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 62532e9ee850SAneesh Kumar K.V else 625409cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6255a827eaffSAneesh Kumar K.V /* 62569ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 62579ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 625864a9f144SMauricio Faria de Oliveira * 625964a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 626064a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6261a827eaffSAneesh Kumar K.V */ 62622e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6263188c299eSJan Kara if (!ext4_walk_page_buffers(NULL, inode, page_buffers(page), 6264f19d5870STao Ma 0, len, NULL, 6265a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 62669ea7df53SJan Kara /* Wait so that we don't change page under IO */ 62671d1d1a76SDarrick J. Wong wait_for_stable_page(page); 62689ea7df53SJan Kara ret = VM_FAULT_LOCKED; 62699ea7df53SJan Kara goto out; 62702e9ee850SAneesh Kumar K.V } 6271a827eaffSAneesh Kumar K.V } 6272a827eaffSAneesh Kumar K.V unlock_page(page); 62739ea7df53SJan Kara /* OK, we need to fill the hole... */ 62749ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6275705965bdSJan Kara get_block = ext4_get_block_unwritten; 62769ea7df53SJan Kara else 62779ea7df53SJan Kara get_block = ext4_get_block; 62789ea7df53SJan Kara retry_alloc: 62799924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 62809924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 62819ea7df53SJan Kara if (IS_ERR(handle)) { 6282c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 62839ea7df53SJan Kara goto out; 62849ea7df53SJan Kara } 628564a9f144SMauricio Faria de Oliveira /* 628664a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 628764a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 628864a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 628964a9f144SMauricio Faria de Oliveira */ 629064a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6291401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 629264a9f144SMauricio Faria de Oliveira } else { 629364a9f144SMauricio Faria de Oliveira lock_page(page); 629464a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 629564a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 629664a9f144SMauricio Faria de Oliveira if (page->mapping != mapping || page_offset(page) > size) { 629764a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6298afb585a9SMauricio Faria de Oliveira goto out_error; 629964a9f144SMauricio Faria de Oliveira } 630064a9f144SMauricio Faria de Oliveira 630164a9f144SMauricio Faria de Oliveira if (page->index == size >> PAGE_SHIFT) 630264a9f144SMauricio Faria de Oliveira len = size & ~PAGE_MASK; 630364a9f144SMauricio Faria de Oliveira else 630464a9f144SMauricio Faria de Oliveira len = PAGE_SIZE; 630564a9f144SMauricio Faria de Oliveira 630664a9f144SMauricio Faria de Oliveira err = __block_write_begin(page, 0, len, ext4_get_block); 630764a9f144SMauricio Faria de Oliveira if (!err) { 63089ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6309188c299eSJan Kara if (ext4_walk_page_buffers(handle, inode, 6310188c299eSJan Kara page_buffers(page), 0, len, NULL, 6311188c299eSJan Kara do_journal_get_write_access)) 6312afb585a9SMauricio Faria de Oliveira goto out_error; 6313188c299eSJan Kara if (ext4_walk_page_buffers(handle, inode, 6314188c299eSJan Kara page_buffers(page), 0, len, NULL, 6315188c299eSJan Kara write_end_fn)) 6316afb585a9SMauricio Faria de Oliveira goto out_error; 6317b5b18160SJan Kara if (ext4_jbd2_inode_add_write(handle, inode, 6318b5b18160SJan Kara page_offset(page), len)) 6319afb585a9SMauricio Faria de Oliveira goto out_error; 63209ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 632164a9f144SMauricio Faria de Oliveira } else { 632264a9f144SMauricio Faria de Oliveira unlock_page(page); 632364a9f144SMauricio Faria de Oliveira } 63249ea7df53SJan Kara } 63259ea7df53SJan Kara ext4_journal_stop(handle); 6326401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 63279ea7df53SJan Kara goto retry_alloc; 63289ea7df53SJan Kara out_ret: 6329401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 63309ea7df53SJan Kara out: 6331d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping); 63328e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 63332e9ee850SAneesh Kumar K.V return ret; 6334afb585a9SMauricio Faria de Oliveira out_error: 6335afb585a9SMauricio Faria de Oliveira unlock_page(page); 6336afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6337afb585a9SMauricio Faria de Oliveira goto out; 63382e9ee850SAneesh Kumar K.V } 6339