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> 44c6f40468SChristoph Hellwig #include <linux/dax.h> 459bffad1eSTheodore Ts'o 463dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 47ac27a0ecSDave Kleikamp #include "xattr.h" 48ac27a0ecSDave Kleikamp #include "acl.h" 499f125d64STheodore Ts'o #include "truncate.h" 50ac27a0ecSDave Kleikamp 519bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 529bffad1eSTheodore Ts'o 53814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 54814525f4SDarrick J. Wong struct ext4_inode_info *ei) 55814525f4SDarrick J. Wong { 56814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 57814525f4SDarrick J. Wong __u32 csum; 58b47820edSDaeho Jeong __u16 dummy_csum = 0; 59b47820edSDaeho Jeong int offset = offsetof(struct ext4_inode, i_checksum_lo); 60b47820edSDaeho Jeong unsigned int csum_size = sizeof(dummy_csum); 61814525f4SDarrick J. Wong 62b47820edSDaeho Jeong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); 63b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); 64b47820edSDaeho Jeong offset += csum_size; 65b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 66b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE - offset); 67b47820edSDaeho Jeong 68b47820edSDaeho Jeong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 69b47820edSDaeho Jeong offset = offsetof(struct ext4_inode, i_checksum_hi); 70b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + 71b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE, 72b47820edSDaeho Jeong offset - EXT4_GOOD_OLD_INODE_SIZE); 73b47820edSDaeho Jeong if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 74b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, 75b47820edSDaeho Jeong csum_size); 76b47820edSDaeho Jeong offset += csum_size; 77814525f4SDarrick J. Wong } 7805ac5aa1SDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 7905ac5aa1SDaeho Jeong EXT4_INODE_SIZE(inode->i_sb) - offset); 80b47820edSDaeho Jeong } 81814525f4SDarrick J. Wong 82814525f4SDarrick J. Wong return csum; 83814525f4SDarrick J. Wong } 84814525f4SDarrick J. Wong 85814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 86814525f4SDarrick J. Wong struct ext4_inode_info *ei) 87814525f4SDarrick J. Wong { 88814525f4SDarrick J. Wong __u32 provided, calculated; 89814525f4SDarrick J. Wong 90814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 91814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 929aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 93814525f4SDarrick J. Wong return 1; 94814525f4SDarrick J. Wong 95814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 96814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 97814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 98814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 99814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 100814525f4SDarrick J. Wong else 101814525f4SDarrick J. Wong calculated &= 0xFFFF; 102814525f4SDarrick J. Wong 103814525f4SDarrick J. Wong return provided == calculated; 104814525f4SDarrick J. Wong } 105814525f4SDarrick J. Wong 1068016e29fSHarshad Shirwadkar void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 107814525f4SDarrick J. Wong struct ext4_inode_info *ei) 108814525f4SDarrick J. Wong { 109814525f4SDarrick J. Wong __u32 csum; 110814525f4SDarrick J. Wong 111814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 112814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 1139aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 114814525f4SDarrick J. Wong return; 115814525f4SDarrick J. Wong 116814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 117814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 118814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 119814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 120814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 121814525f4SDarrick J. Wong } 122814525f4SDarrick J. Wong 123678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 124678aaf48SJan Kara loff_t new_size) 125678aaf48SJan Kara { 1267ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1278aefcd55STheodore Ts'o /* 1288aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1298aefcd55STheodore Ts'o * writing, so there's no need to call 1308aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1318aefcd55STheodore Ts'o * outstanding writes we need to flush. 1328aefcd55STheodore Ts'o */ 1338aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1348aefcd55STheodore Ts'o return 0; 1358aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1368aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 137678aaf48SJan Kara new_size); 138678aaf48SJan Kara } 139678aaf48SJan Kara 140d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 141d47992f8SLukas Czerner unsigned int length); 142cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 143dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 144dec214d0STahsin Erdogan int pextents); 14564769240SAlex Tomas 146ac27a0ecSDave Kleikamp /* 147ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 148407cd7fbSTahsin Erdogan * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 149ac27a0ecSDave Kleikamp */ 150f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode) 151ac27a0ecSDave Kleikamp { 152fc82228aSAndi Kleen if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 153fc82228aSAndi Kleen int ea_blocks = EXT4_I(inode)->i_file_acl ? 154fc82228aSAndi Kleen EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 155fc82228aSAndi Kleen 156fc82228aSAndi Kleen if (ext4_has_inline_data(inode)) 157fc82228aSAndi Kleen return 0; 158fc82228aSAndi Kleen 159fc82228aSAndi Kleen return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 160fc82228aSAndi Kleen } 161407cd7fbSTahsin Erdogan return S_ISLNK(inode->i_mode) && inode->i_size && 162407cd7fbSTahsin Erdogan (inode->i_size < EXT4_N_BLOCKS * 4); 163ac27a0ecSDave Kleikamp } 164ac27a0ecSDave Kleikamp 165ac27a0ecSDave Kleikamp /* 166ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 167ac27a0ecSDave Kleikamp */ 1680930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 169ac27a0ecSDave Kleikamp { 170ac27a0ecSDave Kleikamp handle_t *handle; 171bc965ab3STheodore Ts'o int err; 17265db869cSJan Kara /* 17365db869cSJan Kara * Credits for final inode cleanup and freeing: 17465db869cSJan Kara * sb + inode (ext4_orphan_del()), block bitmap, group descriptor 17565db869cSJan Kara * (xattr block freeing), bitmap, group descriptor (inode freeing) 17665db869cSJan Kara */ 17765db869cSJan Kara int extra_credits = 6; 1780421a189STahsin Erdogan struct ext4_xattr_inode_array *ea_inode_array = NULL; 17946e294efSJan Kara bool freeze_protected = false; 180ac27a0ecSDave Kleikamp 1817ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1822581fdc8SJiaying Zhang 1830930fcc1SAl Viro if (inode->i_nlink) { 1842d859db3SJan Kara /* 1852d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1862d859db3SJan Kara * journal. So although mm thinks everything is clean and 1872d859db3SJan Kara * ready for reaping the inode might still have some pages to 1882d859db3SJan Kara * write in the running transaction or waiting to be 1892d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1902d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1912d859db3SJan Kara * cause data loss. Also even if we did not discard these 1922d859db3SJan Kara * buffers, we would have no way to find them after the inode 1932d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1942d859db3SJan Kara * read them before the transaction is checkpointed. So be 1952d859db3SJan Kara * careful and force everything to disk here... We use 1962d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1972d859db3SJan Kara * containing inode's data. 1982d859db3SJan Kara * 1992d859db3SJan Kara * Note that directories do not have this problem because they 2002d859db3SJan Kara * don't use page cache. 2012d859db3SJan Kara */ 2026a7fd522SVegard Nossum if (inode->i_ino != EXT4_JOURNAL_INO && 2036a7fd522SVegard Nossum ext4_should_journal_data(inode) && 2043abb1a0fSJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2053abb1a0fSJan Kara inode->i_data.nrpages) { 2062d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2072d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2082d859db3SJan Kara 209d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2102d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2112d859db3SJan Kara } 21291b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 2135dc23bddSJan Kara 2140930fcc1SAl Viro goto no_delete; 2150930fcc1SAl Viro } 2160930fcc1SAl Viro 217e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 218e2bfb088STheodore Ts'o goto no_delete; 219871a2931SChristoph Hellwig dquot_initialize(inode); 220907f4554SChristoph Hellwig 221678aaf48SJan Kara if (ext4_should_order_data(inode)) 222678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 22391b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 224ac27a0ecSDave Kleikamp 2258e8ad8a5SJan Kara /* 226ceff86fdSJan Kara * For inodes with journalled data, transaction commit could have 227ceff86fdSJan Kara * dirtied the inode. Flush worker is ignoring it because of I_FREEING 228ceff86fdSJan Kara * flag but we still need to remove the inode from the writeback lists. 229ceff86fdSJan Kara */ 230ceff86fdSJan Kara if (!list_empty_careful(&inode->i_io_list)) { 231ceff86fdSJan Kara WARN_ON_ONCE(!ext4_should_journal_data(inode)); 232ceff86fdSJan Kara inode_io_list_del(inode); 233ceff86fdSJan Kara } 234ceff86fdSJan Kara 235ceff86fdSJan Kara /* 2368e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 23746e294efSJan Kara * protection against it. When we are in a running transaction though, 23846e294efSJan Kara * we are already protected against freezing and we cannot grab further 23946e294efSJan Kara * protection due to lock ordering constraints. 2408e8ad8a5SJan Kara */ 24146e294efSJan Kara if (!ext4_journal_current_handle()) { 2428e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 24346e294efSJan Kara freeze_protected = true; 24446e294efSJan Kara } 245e50e5129SAndreas Dilger 24630a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 24730a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 24830a7eb97STahsin Erdogan 24965db869cSJan Kara /* 25065db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 25165db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 25265db869cSJan Kara */ 25330a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 25465db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 255ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 256bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 257ac27a0ecSDave Kleikamp /* 258ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 259ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 260ac27a0ecSDave Kleikamp * cleaned up. 261ac27a0ecSDave Kleikamp */ 262617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 26346e294efSJan Kara if (freeze_protected) 2648e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 265ac27a0ecSDave Kleikamp goto no_delete; 266ac27a0ecSDave Kleikamp } 26730a7eb97STahsin Erdogan 268ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2690390131bSFrank Mayhar ext4_handle_sync(handle); 270407cd7fbSTahsin Erdogan 271407cd7fbSTahsin Erdogan /* 272407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 273407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 274407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 275407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 276407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 277407cd7fbSTahsin Erdogan */ 278407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 279407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 280ac27a0ecSDave Kleikamp inode->i_size = 0; 281bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 282bc965ab3STheodore Ts'o if (err) { 28312062dddSEric Sandeen ext4_warning(inode->i_sb, 284bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 285bc965ab3STheodore Ts'o goto stop_handle; 286bc965ab3STheodore Ts'o } 2872c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2882c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2892c98eb5eSTheodore Ts'o if (err) { 29054d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err, 2912c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2922c98eb5eSTheodore Ts'o inode->i_ino, err); 2932c98eb5eSTheodore Ts'o goto stop_handle; 2942c98eb5eSTheodore Ts'o } 2952c98eb5eSTheodore Ts'o } 296bc965ab3STheodore Ts'o 29730a7eb97STahsin Erdogan /* Remove xattr references. */ 29830a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 29930a7eb97STahsin Erdogan extra_credits); 30030a7eb97STahsin Erdogan if (err) { 30130a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 302bc965ab3STheodore Ts'o stop_handle: 303bc965ab3STheodore Ts'o ext4_journal_stop(handle); 30445388219STheodore Ts'o ext4_orphan_del(NULL, inode); 30546e294efSJan Kara if (freeze_protected) 3068e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 30730a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 308bc965ab3STheodore Ts'o goto no_delete; 309bc965ab3STheodore Ts'o } 310bc965ab3STheodore Ts'o 311ac27a0ecSDave Kleikamp /* 312617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 313ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 314617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 315ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 316617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 317ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 318ac27a0ecSDave Kleikamp */ 319617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3205ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 321ac27a0ecSDave Kleikamp 322ac27a0ecSDave Kleikamp /* 323ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 324ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 325ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 326ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 327ac27a0ecSDave Kleikamp * fails. 328ac27a0ecSDave Kleikamp */ 329617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 330ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3310930fcc1SAl Viro ext4_clear_inode(inode); 332ac27a0ecSDave Kleikamp else 333617ba13bSMingming Cao ext4_free_inode(handle, inode); 334617ba13bSMingming Cao ext4_journal_stop(handle); 33546e294efSJan Kara if (freeze_protected) 3368e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3370421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 338ac27a0ecSDave Kleikamp return; 339ac27a0ecSDave Kleikamp no_delete: 340b21ebf14SHarshad Shirwadkar if (!list_empty(&EXT4_I(inode)->i_fc_list)) 341e85c81baSXin Yin ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM, NULL); 3420930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 343ac27a0ecSDave Kleikamp } 344ac27a0ecSDave Kleikamp 345a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 346a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 34760e58e0fSMingming Cao { 348a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 34960e58e0fSMingming Cao } 350a9e7f447SDmitry Monakhov #endif 3519d0be502STheodore Ts'o 35212219aeaSAneesh Kumar K.V /* 3530637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3540637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3550637c6f4STheodore Ts'o */ 3565f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3575f634d06SAneesh Kumar K.V int used, int quota_claim) 35812219aeaSAneesh Kumar K.V { 35912219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3600637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 36112219aeaSAneesh Kumar K.V 3620637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 363d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3640637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3658de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3661084f252STheodore Ts'o "with only %d reserved data blocks", 3670637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3680637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3690637c6f4STheodore Ts'o WARN_ON(1); 3700637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3716bc6e63fSAneesh Kumar K.V } 37212219aeaSAneesh Kumar K.V 3730637c6f4STheodore Ts'o /* Update per-inode reservations */ 3740637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 37571d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3760637c6f4STheodore Ts'o 377f9505c72Schenyichong spin_unlock(&ei->i_block_reservation_lock); 37860e58e0fSMingming Cao 37972b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38072b8ab9dSEric Sandeen if (quota_claim) 3817b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38272b8ab9dSEric Sandeen else { 3835f634d06SAneesh Kumar K.V /* 3845f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3855f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 38672b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3875f634d06SAneesh Kumar K.V */ 3887b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3895f634d06SAneesh Kumar K.V } 390d6014301SAneesh Kumar K.V 391d6014301SAneesh Kumar K.V /* 392d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 393d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 394d6014301SAneesh Kumar K.V * inode's preallocations. 395d6014301SAneesh Kumar K.V */ 3960637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 39782dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 39827bc446eSbrookxu ext4_discard_preallocations(inode, 0); 39912219aeaSAneesh Kumar K.V } 40012219aeaSAneesh Kumar K.V 401e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 402c398eda0STheodore Ts'o unsigned int line, 40324676da4STheodore Ts'o struct ext4_map_blocks *map) 4046fd058f7STheodore Ts'o { 405345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 406345c0dbfSTheodore Ts'o (inode->i_ino == 407345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 408345c0dbfSTheodore Ts'o return 0; 409ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 410c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 411bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 41224676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 413bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4146a797d27SDarrick J. Wong return -EFSCORRUPTED; 4156fd058f7STheodore Ts'o } 4166fd058f7STheodore Ts'o return 0; 4176fd058f7STheodore Ts'o } 4186fd058f7STheodore Ts'o 41953085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 42053085facSJan Kara ext4_lblk_t len) 42153085facSJan Kara { 42253085facSJan Kara int ret; 42353085facSJan Kara 42433b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 425a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 42653085facSJan Kara 42753085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 42853085facSJan Kara if (ret > 0) 42953085facSJan Kara ret = 0; 43053085facSJan Kara 43153085facSJan Kara return ret; 43253085facSJan Kara } 43353085facSJan Kara 434e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 435c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 436e29136f8STheodore Ts'o 437921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 438921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 439921f266bSDmitry Monakhov struct inode *inode, 440921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 441921f266bSDmitry Monakhov struct ext4_map_blocks *map, 442921f266bSDmitry Monakhov int flags) 443921f266bSDmitry Monakhov { 444921f266bSDmitry Monakhov int retval; 445921f266bSDmitry Monakhov 446921f266bSDmitry Monakhov map->m_flags = 0; 447921f266bSDmitry Monakhov /* 448921f266bSDmitry Monakhov * There is a race window that the result is not the same. 449921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 450921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 451921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 452921f266bSDmitry Monakhov * could be converted. 453921f266bSDmitry Monakhov */ 454c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 455921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4569e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 457921f266bSDmitry Monakhov } else { 4589e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 459921f266bSDmitry Monakhov } 460921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 461921f266bSDmitry Monakhov 462921f266bSDmitry Monakhov /* 463921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 464921f266bSDmitry Monakhov * tree. So the m_len might not equal. 465921f266bSDmitry Monakhov */ 466921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 467921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 468921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 469bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 470921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 471921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 472921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 473921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 474921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 475921f266bSDmitry Monakhov retval, flags); 476921f266bSDmitry Monakhov } 477921f266bSDmitry Monakhov } 478921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 479921f266bSDmitry Monakhov 48055138e0bSTheodore Ts'o /* 481e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4822b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 483f5ab0d1fSMingming Cao * 484f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 485f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 486f5ab0d1fSMingming Cao * mapped. 487f5ab0d1fSMingming Cao * 488e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 489e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 490f5ab0d1fSMingming Cao * based files 491f5ab0d1fSMingming Cao * 492facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 493facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 494facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 495f5ab0d1fSMingming Cao * 496f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 497facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 498facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 499f5ab0d1fSMingming Cao * 500f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 501f5ab0d1fSMingming Cao */ 502e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 503e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5040e855ac8SAneesh Kumar K.V { 505d100eef2SZheng Liu struct extent_status es; 5060e855ac8SAneesh Kumar K.V int retval; 507b8a86845SLukas Czerner int ret = 0; 508921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 509921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 510921f266bSDmitry Monakhov 511921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 512921f266bSDmitry Monakhov #endif 513f5ab0d1fSMingming Cao 514e35fd660STheodore Ts'o map->m_flags = 0; 51570aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 51670aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 517d100eef2SZheng Liu 518e861b5e9STheodore Ts'o /* 519e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 520e861b5e9STheodore Ts'o */ 521e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 522e861b5e9STheodore Ts'o map->m_len = INT_MAX; 523e861b5e9STheodore Ts'o 5244adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5254adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5266a797d27SDarrick J. Wong return -EFSCORRUPTED; 5274adb6ab3SKazuya Mio 528d100eef2SZheng Liu /* Lookup extent status tree firstly */ 5298016e29fSHarshad Shirwadkar if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 5308016e29fSHarshad Shirwadkar ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 531d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 532d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 533d100eef2SZheng Liu map->m_lblk - es.es_lblk; 534d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 535d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 536d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 537d100eef2SZheng Liu if (retval > map->m_len) 538d100eef2SZheng Liu retval = map->m_len; 539d100eef2SZheng Liu map->m_len = retval; 540d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 541facab4d9SJan Kara map->m_pblk = 0; 542facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 543facab4d9SJan Kara if (retval > map->m_len) 544facab4d9SJan Kara retval = map->m_len; 545facab4d9SJan Kara map->m_len = retval; 546d100eef2SZheng Liu retval = 0; 547d100eef2SZheng Liu } else { 5481e83bc81SArnd Bergmann BUG(); 549d100eef2SZheng Liu } 550921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 551921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 552921f266bSDmitry Monakhov &orig_map, flags); 553921f266bSDmitry Monakhov #endif 554d100eef2SZheng Liu goto found; 555d100eef2SZheng Liu } 556d100eef2SZheng Liu 5574df3d265SAneesh Kumar K.V /* 558b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 559b920c755STheodore Ts'o * file system block. 5604df3d265SAneesh Kumar K.V */ 561c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 56212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5639e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5644df3d265SAneesh Kumar K.V } else { 5659e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5660e855ac8SAneesh Kumar K.V } 567f7fec032SZheng Liu if (retval > 0) { 5683be78c73STheodore Ts'o unsigned int status; 569f7fec032SZheng Liu 57044fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 57144fb851dSZheng Liu ext4_warning(inode->i_sb, 57244fb851dSZheng Liu "ES len assertion failed for inode " 57344fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 57444fb851dSZheng Liu inode->i_ino, retval, map->m_len); 57544fb851dSZheng Liu WARN_ON(1); 576921f266bSDmitry Monakhov } 577921f266bSDmitry Monakhov 578f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 579f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 580f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 581d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 582ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 583f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 584f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 585f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 586f7fec032SZheng Liu map->m_len, map->m_pblk, status); 587f7fec032SZheng Liu if (ret < 0) 588f7fec032SZheng Liu retval = ret; 589f7fec032SZheng Liu } 5904df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 591f5ab0d1fSMingming Cao 592d100eef2SZheng Liu found: 593e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 594b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5956fd058f7STheodore Ts'o if (ret != 0) 5966fd058f7STheodore Ts'o return ret; 5976fd058f7STheodore Ts'o } 5986fd058f7STheodore Ts'o 599f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 600c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6014df3d265SAneesh Kumar K.V return retval; 6024df3d265SAneesh Kumar K.V 6034df3d265SAneesh Kumar K.V /* 604f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 605f5ab0d1fSMingming Cao * 606f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 607df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 608f5ab0d1fSMingming Cao * with buffer head unmapped. 609f5ab0d1fSMingming Cao */ 610e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 611b8a86845SLukas Czerner /* 612b8a86845SLukas Czerner * If we need to convert extent to unwritten 613b8a86845SLukas Czerner * we continue and do the actual work in 614b8a86845SLukas Czerner * ext4_ext_map_blocks() 615b8a86845SLukas Czerner */ 616b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 617f5ab0d1fSMingming Cao return retval; 618f5ab0d1fSMingming Cao 619f5ab0d1fSMingming Cao /* 620a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 621a25a4e1aSZheng Liu * it will be set again. 6222a8964d6SAneesh Kumar K.V */ 623a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6242a8964d6SAneesh Kumar K.V 6252a8964d6SAneesh Kumar K.V /* 626556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 627f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 628d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 629f5ab0d1fSMingming Cao * with create == 1 flag. 6304df3d265SAneesh Kumar K.V */ 631c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 632d2a17637SMingming Cao 633d2a17637SMingming Cao /* 6344df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6354df3d265SAneesh Kumar K.V * could have changed the inode type in between 6364df3d265SAneesh Kumar K.V */ 63712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 638e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6390e855ac8SAneesh Kumar K.V } else { 640e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 641267e4db9SAneesh Kumar K.V 642e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 643267e4db9SAneesh Kumar K.V /* 644267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 645267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 646267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 647267e4db9SAneesh Kumar K.V */ 64819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 649267e4db9SAneesh Kumar K.V } 6502ac3b6e0STheodore Ts'o 651d2a17637SMingming Cao /* 6522ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6535f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6545f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6555f634d06SAneesh Kumar K.V * reserve space here. 656d2a17637SMingming Cao */ 6575f634d06SAneesh Kumar K.V if ((retval > 0) && 6581296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6595f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6605f634d06SAneesh Kumar K.V } 661d2a17637SMingming Cao 662f7fec032SZheng Liu if (retval > 0) { 6633be78c73STheodore Ts'o unsigned int status; 664f7fec032SZheng Liu 66544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 66644fb851dSZheng Liu ext4_warning(inode->i_sb, 66744fb851dSZheng Liu "ES len assertion failed for inode " 66844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 66944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 67044fb851dSZheng Liu WARN_ON(1); 671921f266bSDmitry Monakhov } 672921f266bSDmitry Monakhov 673adb23551SZheng Liu /* 674c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 675c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6769b623df6SJan Kara * use them before they are really zeroed. We also have to 6779b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6789b623df6SJan Kara * overwrite zeros with stale data from block device. 679c86d8db3SJan Kara */ 680c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 681c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 682c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 683c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 684c86d8db3SJan Kara map->m_pblk, map->m_len); 685c86d8db3SJan Kara if (ret) { 686c86d8db3SJan Kara retval = ret; 687c86d8db3SJan Kara goto out_sem; 688c86d8db3SJan Kara } 689c86d8db3SJan Kara } 690c86d8db3SJan Kara 691c86d8db3SJan Kara /* 692adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 693adb23551SZheng Liu * extent status tree. 694adb23551SZheng Liu */ 695adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 696bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 697adb23551SZheng Liu if (ext4_es_is_written(&es)) 698c86d8db3SJan Kara goto out_sem; 699adb23551SZheng Liu } 700f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 701f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 702f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 703d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 704ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 705f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 706f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 707f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 708f7fec032SZheng Liu map->m_pblk, status); 709c86d8db3SJan Kara if (ret < 0) { 71051865fdaSZheng Liu retval = ret; 711c86d8db3SJan Kara goto out_sem; 712c86d8db3SJan Kara } 71351865fdaSZheng Liu } 7145356f261SAditya Kali 715c86d8db3SJan Kara out_sem: 7160e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 717e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 718b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7196fd058f7STheodore Ts'o if (ret != 0) 7206fd058f7STheodore Ts'o return ret; 72106bd3c36SJan Kara 72206bd3c36SJan Kara /* 72306bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 72406bd3c36SJan Kara * visible after transaction commit must be on transaction's 72506bd3c36SJan Kara * ordered data list. 72606bd3c36SJan Kara */ 72706bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 72806bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 72906bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 73002749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 73106bd3c36SJan Kara ext4_should_order_data(inode)) { 73273131fbbSRoss Zwisler loff_t start_byte = 73373131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 73473131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 73573131fbbSRoss Zwisler 736ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 73773131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 73873131fbbSRoss Zwisler start_byte, length); 739ee0876bcSJan Kara else 74073131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 74173131fbbSRoss Zwisler start_byte, length); 74206bd3c36SJan Kara if (ret) 74306bd3c36SJan Kara return ret; 74406bd3c36SJan Kara } 7455e4d0ebaSXin Yin } 7465e4d0ebaSXin Yin if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN || 7475e4d0ebaSXin Yin map->m_flags & EXT4_MAP_MAPPED)) 748a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 749aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 750ec8c60beSRitesh Harjani if (retval < 0) 75170aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7520e855ac8SAneesh Kumar K.V return retval; 7530e855ac8SAneesh Kumar K.V } 7540e855ac8SAneesh Kumar K.V 755ed8ad838SJan Kara /* 756ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 757ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 758ed8ad838SJan Kara */ 759ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 760ed8ad838SJan Kara { 761ed8ad838SJan Kara unsigned long old_state; 762ed8ad838SJan Kara unsigned long new_state; 763ed8ad838SJan Kara 764ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 765ed8ad838SJan Kara 766ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 767ed8ad838SJan Kara if (!bh->b_page) { 768ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 769ed8ad838SJan Kara return; 770ed8ad838SJan Kara } 771ed8ad838SJan Kara /* 772ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 773ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 774ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 775ed8ad838SJan Kara */ 776ed8ad838SJan Kara do { 777ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 778ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 779ed8ad838SJan Kara } while (unlikely( 780ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 781ed8ad838SJan Kara } 782ed8ad838SJan Kara 7832ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7842ed88685STheodore Ts'o struct buffer_head *bh, int flags) 785ac27a0ecSDave Kleikamp { 7862ed88685STheodore Ts'o struct ext4_map_blocks map; 787efe70c29SJan Kara int ret = 0; 788ac27a0ecSDave Kleikamp 78946c7f254STao Ma if (ext4_has_inline_data(inode)) 79046c7f254STao Ma return -ERANGE; 79146c7f254STao Ma 7922ed88685STheodore Ts'o map.m_lblk = iblock; 7932ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7942ed88685STheodore Ts'o 795efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 796efe70c29SJan Kara flags); 797ac27a0ecSDave Kleikamp if (ret > 0) { 7982ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 799ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 8002ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 801ac27a0ecSDave Kleikamp ret = 0; 802547edce3SRoss Zwisler } else if (ret == 0) { 803547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 804547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 805ac27a0ecSDave Kleikamp } 806ac27a0ecSDave Kleikamp return ret; 807ac27a0ecSDave Kleikamp } 808ac27a0ecSDave Kleikamp 8092ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8102ed88685STheodore Ts'o struct buffer_head *bh, int create) 8112ed88685STheodore Ts'o { 8122ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8132ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8142ed88685STheodore Ts'o } 8152ed88685STheodore Ts'o 816ac27a0ecSDave Kleikamp /* 817705965bdSJan Kara * Get block function used when preparing for buffered write if we require 818705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 819705965bdSJan Kara * will be converted to written after the IO is complete. 820705965bdSJan Kara */ 821705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 822705965bdSJan Kara struct buffer_head *bh_result, int create) 823705965bdSJan Kara { 824705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 825705965bdSJan Kara inode->i_ino, create); 826705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 827705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 828705965bdSJan Kara } 829705965bdSJan Kara 830efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 831efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 832efe70c29SJan Kara 833e84dfbe2SJan Kara /* 834ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 835ac27a0ecSDave Kleikamp */ 836617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 837c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 838ac27a0ecSDave Kleikamp { 8392ed88685STheodore Ts'o struct ext4_map_blocks map; 8402ed88685STheodore Ts'o struct buffer_head *bh; 841c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 84210560082STheodore Ts'o int err; 843ac27a0ecSDave Kleikamp 844837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8458016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 846ac27a0ecSDave Kleikamp 8472ed88685STheodore Ts'o map.m_lblk = block; 8482ed88685STheodore Ts'o map.m_len = 1; 849c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8502ed88685STheodore Ts'o 85110560082STheodore Ts'o if (err == 0) 85210560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8532ed88685STheodore Ts'o if (err < 0) 85410560082STheodore Ts'o return ERR_PTR(err); 8552ed88685STheodore Ts'o 8562ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 85710560082STheodore Ts'o if (unlikely(!bh)) 85810560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8592ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 860837c23fbSChunguang Xu ASSERT(create != 0); 861837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8628016e29fSHarshad Shirwadkar || (handle != NULL)); 863ac27a0ecSDave Kleikamp 864ac27a0ecSDave Kleikamp /* 865ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 866ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 867ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 868617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 869ac27a0ecSDave Kleikamp * problem. 870ac27a0ecSDave Kleikamp */ 871ac27a0ecSDave Kleikamp lock_buffer(bh); 872ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 873188c299eSJan Kara err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 874188c299eSJan Kara EXT4_JTR_NONE); 87510560082STheodore Ts'o if (unlikely(err)) { 87610560082STheodore Ts'o unlock_buffer(bh); 87710560082STheodore Ts'o goto errout; 87810560082STheodore Ts'o } 87910560082STheodore Ts'o if (!buffer_uptodate(bh)) { 880ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 881ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 882ac27a0ecSDave Kleikamp } 883ac27a0ecSDave Kleikamp unlock_buffer(bh); 8840390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8850390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 88610560082STheodore Ts'o if (unlikely(err)) 88710560082STheodore Ts'o goto errout; 88810560082STheodore Ts'o } else 889ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 890ac27a0ecSDave Kleikamp return bh; 89110560082STheodore Ts'o errout: 89210560082STheodore Ts'o brelse(bh); 89310560082STheodore Ts'o return ERR_PTR(err); 894ac27a0ecSDave Kleikamp } 895ac27a0ecSDave Kleikamp 896617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 897c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 898ac27a0ecSDave Kleikamp { 899ac27a0ecSDave Kleikamp struct buffer_head *bh; 9002d069c08Szhangyi (F) int ret; 901ac27a0ecSDave Kleikamp 902c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 9031c215028STheodore Ts'o if (IS_ERR(bh)) 904ac27a0ecSDave Kleikamp return bh; 9057963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 906ac27a0ecSDave Kleikamp return bh; 9072d069c08Szhangyi (F) 9082d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 9092d069c08Szhangyi (F) if (ret) { 910ac27a0ecSDave Kleikamp put_bh(bh); 9112d069c08Szhangyi (F) return ERR_PTR(ret); 9122d069c08Szhangyi (F) } 9132d069c08Szhangyi (F) return bh; 914ac27a0ecSDave Kleikamp } 915ac27a0ecSDave Kleikamp 9169699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 9179699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 9189699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 9199699d4f9STahsin Erdogan { 9209699d4f9STahsin Erdogan int i, err; 9219699d4f9STahsin Erdogan 9229699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9239699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9249699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9259699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9269699d4f9STahsin Erdogan bh_count = i; 9279699d4f9STahsin Erdogan goto out_brelse; 9289699d4f9STahsin Erdogan } 9299699d4f9STahsin Erdogan } 9309699d4f9STahsin Erdogan 9319699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9329699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9332d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9342d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9359699d4f9STahsin Erdogan 9369699d4f9STahsin Erdogan if (!wait) 9379699d4f9STahsin Erdogan return 0; 9389699d4f9STahsin Erdogan 9399699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9409699d4f9STahsin Erdogan if (bhs[i]) 9419699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9429699d4f9STahsin Erdogan 9439699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9449699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9459699d4f9STahsin Erdogan err = -EIO; 9469699d4f9STahsin Erdogan goto out_brelse; 9479699d4f9STahsin Erdogan } 9489699d4f9STahsin Erdogan } 9499699d4f9STahsin Erdogan return 0; 9509699d4f9STahsin Erdogan 9519699d4f9STahsin Erdogan out_brelse: 9529699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9539699d4f9STahsin Erdogan brelse(bhs[i]); 9549699d4f9STahsin Erdogan bhs[i] = NULL; 9559699d4f9STahsin Erdogan } 9569699d4f9STahsin Erdogan return err; 9579699d4f9STahsin Erdogan } 9589699d4f9STahsin Erdogan 959188c299eSJan Kara int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, 960ac27a0ecSDave Kleikamp struct buffer_head *head, 961ac27a0ecSDave Kleikamp unsigned from, 962ac27a0ecSDave Kleikamp unsigned to, 963ac27a0ecSDave Kleikamp int *partial, 964188c299eSJan Kara int (*fn)(handle_t *handle, struct inode *inode, 965ac27a0ecSDave Kleikamp struct buffer_head *bh)) 966ac27a0ecSDave Kleikamp { 967ac27a0ecSDave Kleikamp struct buffer_head *bh; 968ac27a0ecSDave Kleikamp unsigned block_start, block_end; 969ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 970ac27a0ecSDave Kleikamp int err, ret = 0; 971ac27a0ecSDave Kleikamp struct buffer_head *next; 972ac27a0ecSDave Kleikamp 973ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 974ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 975de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 976ac27a0ecSDave Kleikamp next = bh->b_this_page; 977ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 978ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 979ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 980ac27a0ecSDave Kleikamp *partial = 1; 981ac27a0ecSDave Kleikamp continue; 982ac27a0ecSDave Kleikamp } 983188c299eSJan Kara err = (*fn)(handle, inode, bh); 984ac27a0ecSDave Kleikamp if (!ret) 985ac27a0ecSDave Kleikamp ret = err; 986ac27a0ecSDave Kleikamp } 987ac27a0ecSDave Kleikamp return ret; 988ac27a0ecSDave Kleikamp } 989ac27a0ecSDave Kleikamp 990ac27a0ecSDave Kleikamp /* 991ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 992ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 993617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 994dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 995ac27a0ecSDave Kleikamp * prepare_write() is the right place. 996ac27a0ecSDave Kleikamp * 99736ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 99836ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 99936ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 100036ade451SJan Kara * because the caller may be PF_MEMALLOC. 1001ac27a0ecSDave Kleikamp * 1002617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1003ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1004ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1005ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1006ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1007ac27a0ecSDave Kleikamp * violation. 1008ac27a0ecSDave Kleikamp * 1009dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1010ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1011ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1012ac27a0ecSDave Kleikamp * write. 1013ac27a0ecSDave Kleikamp */ 1014188c299eSJan Kara int do_journal_get_write_access(handle_t *handle, struct inode *inode, 1015ac27a0ecSDave Kleikamp struct buffer_head *bh) 1016ac27a0ecSDave Kleikamp { 101756d35a4cSJan Kara int dirty = buffer_dirty(bh); 101856d35a4cSJan Kara int ret; 101956d35a4cSJan Kara 1020ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1021ac27a0ecSDave Kleikamp return 0; 102256d35a4cSJan Kara /* 1023ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 102456d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 102556d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1026ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 102756d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 102856d35a4cSJan Kara * ever write the buffer. 102956d35a4cSJan Kara */ 103056d35a4cSJan Kara if (dirty) 103156d35a4cSJan Kara clear_buffer_dirty(bh); 10325d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 1033188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1034188c299eSJan Kara EXT4_JTR_NONE); 103556d35a4cSJan Kara if (!ret && dirty) 103656d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 103756d35a4cSJan Kara return ret; 1038ac27a0ecSDave Kleikamp } 1039ac27a0ecSDave Kleikamp 1040643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10412058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10422058f83aSMichael Halcrow get_block_t *get_block) 10432058f83aSMichael Halcrow { 104409cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10452058f83aSMichael Halcrow unsigned to = from + len; 10462058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10472058f83aSMichael Halcrow unsigned block_start, block_end; 10482058f83aSMichael Halcrow sector_t block; 10492058f83aSMichael Halcrow int err = 0; 10502058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10512058f83aSMichael Halcrow unsigned bbits; 10520b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10530b578f35SChandan Rajendra int nr_wait = 0; 10540b578f35SChandan Rajendra int i; 10552058f83aSMichael Halcrow 10562058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 105709cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 105809cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10592058f83aSMichael Halcrow BUG_ON(from > to); 10602058f83aSMichael Halcrow 10612058f83aSMichael Halcrow if (!page_has_buffers(page)) 10622058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10632058f83aSMichael Halcrow head = page_buffers(page); 10642058f83aSMichael Halcrow bbits = ilog2(blocksize); 106509cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10662058f83aSMichael Halcrow 10672058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10682058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10692058f83aSMichael Halcrow block_end = block_start + blocksize; 10702058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10712058f83aSMichael Halcrow if (PageUptodate(page)) { 10722058f83aSMichael Halcrow set_buffer_uptodate(bh); 10732058f83aSMichael Halcrow } 10742058f83aSMichael Halcrow continue; 10752058f83aSMichael Halcrow } 10762058f83aSMichael Halcrow if (buffer_new(bh)) 10772058f83aSMichael Halcrow clear_buffer_new(bh); 10782058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10792058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10802058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10812058f83aSMichael Halcrow if (err) 10822058f83aSMichael Halcrow break; 10832058f83aSMichael Halcrow if (buffer_new(bh)) { 10842058f83aSMichael Halcrow if (PageUptodate(page)) { 10852058f83aSMichael Halcrow clear_buffer_new(bh); 10862058f83aSMichael Halcrow set_buffer_uptodate(bh); 10872058f83aSMichael Halcrow mark_buffer_dirty(bh); 10882058f83aSMichael Halcrow continue; 10892058f83aSMichael Halcrow } 10902058f83aSMichael Halcrow if (block_end > to || block_start < from) 10912058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10922058f83aSMichael Halcrow block_start, from); 10932058f83aSMichael Halcrow continue; 10942058f83aSMichael Halcrow } 10952058f83aSMichael Halcrow } 10962058f83aSMichael Halcrow if (PageUptodate(page)) { 10972058f83aSMichael Halcrow set_buffer_uptodate(bh); 10982058f83aSMichael Halcrow continue; 10992058f83aSMichael Halcrow } 11002058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 11012058f83aSMichael Halcrow !buffer_unwritten(bh) && 11022058f83aSMichael Halcrow (block_start < from || block_end > to)) { 11032d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 11040b578f35SChandan Rajendra wait[nr_wait++] = bh; 11052058f83aSMichael Halcrow } 11062058f83aSMichael Halcrow } 11072058f83aSMichael Halcrow /* 11082058f83aSMichael Halcrow * If we issued read requests, let them complete. 11092058f83aSMichael Halcrow */ 11100b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11110b578f35SChandan Rajendra wait_on_buffer(wait[i]); 11120b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 11132058f83aSMichael Halcrow err = -EIO; 11142058f83aSMichael Halcrow } 11157e0785fcSChandan Rajendra if (unlikely(err)) { 11162058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 11174f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11180b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11190b578f35SChandan Rajendra int err2; 11200b578f35SChandan Rajendra 11210b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 11220b578f35SChandan Rajendra bh_offset(wait[i])); 11230b578f35SChandan Rajendra if (err2) { 11240b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11250b578f35SChandan Rajendra err = err2; 11260b578f35SChandan Rajendra } 11270b578f35SChandan Rajendra } 11287e0785fcSChandan Rajendra } 11297e0785fcSChandan Rajendra 11302058f83aSMichael Halcrow return err; 11312058f83aSMichael Halcrow } 11322058f83aSMichael Halcrow #endif 11332058f83aSMichael Halcrow 1134bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1135bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1136bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1137ac27a0ecSDave Kleikamp { 1138bfc1af65SNick Piggin struct inode *inode = mapping->host; 11391938a150SAneesh Kumar K.V int ret, needed_blocks; 1140ac27a0ecSDave Kleikamp handle_t *handle; 1141ac27a0ecSDave Kleikamp int retries = 0; 1142bfc1af65SNick Piggin struct page *page; 1143bfc1af65SNick Piggin pgoff_t index; 1144bfc1af65SNick Piggin unsigned from, to; 1145bfc1af65SNick Piggin 11460db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11470db1ff22STheodore Ts'o return -EIO; 11480db1ff22STheodore Ts'o 11499bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11501938a150SAneesh Kumar K.V /* 11511938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11521938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11531938a150SAneesh Kumar K.V */ 11541938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 115509cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 115609cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1157bfc1af65SNick Piggin to = from + len; 1158ac27a0ecSDave Kleikamp 1159f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1160f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1161f19d5870STao Ma flags, pagep); 1162f19d5870STao Ma if (ret < 0) 116347564bfbSTheodore Ts'o return ret; 116447564bfbSTheodore Ts'o if (ret == 1) 116547564bfbSTheodore Ts'o return 0; 1166f19d5870STao Ma } 1167f19d5870STao Ma 116847564bfbSTheodore Ts'o /* 116947564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 117047564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 117147564bfbSTheodore Ts'o * is being written back. So grab it first before we start 117247564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 117347564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 117447564bfbSTheodore Ts'o */ 117547564bfbSTheodore Ts'o retry_grab: 117654566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 117747564bfbSTheodore Ts'o if (!page) 117847564bfbSTheodore Ts'o return -ENOMEM; 117947564bfbSTheodore Ts'o unlock_page(page); 118047564bfbSTheodore Ts'o 118147564bfbSTheodore Ts'o retry_journal: 11829924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1183ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 118409cbfeafSKirill A. Shutemov put_page(page); 118547564bfbSTheodore Ts'o return PTR_ERR(handle); 1186cf108bcaSJan Kara } 1187f19d5870STao Ma 118847564bfbSTheodore Ts'o lock_page(page); 118947564bfbSTheodore Ts'o if (page->mapping != mapping) { 119047564bfbSTheodore Ts'o /* The page got truncated from under us */ 119147564bfbSTheodore Ts'o unlock_page(page); 119209cbfeafSKirill A. Shutemov put_page(page); 1193cf108bcaSJan Kara ext4_journal_stop(handle); 119447564bfbSTheodore Ts'o goto retry_grab; 1195cf108bcaSJan Kara } 11967afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11977afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1198cf108bcaSJan Kara 1199643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 12002058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 12012058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1202705965bdSJan Kara ext4_get_block_unwritten); 12032058f83aSMichael Halcrow else 12042058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 12052058f83aSMichael Halcrow ext4_get_block); 12062058f83aSMichael Halcrow #else 1207744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1208705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1209705965bdSJan Kara ext4_get_block_unwritten); 1210744692dcSJiaying Zhang else 12116e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 12122058f83aSMichael Halcrow #endif 1213bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1214188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, 1215188c299eSJan Kara page_buffers(page), from, to, NULL, 1216f19d5870STao Ma do_journal_get_write_access); 1217b46be050SAndrey Savochkin } 1218bfc1af65SNick Piggin 1219bfc1af65SNick Piggin if (ret) { 1220c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1221c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1222c93d8f88SEric Biggers 1223bfc1af65SNick Piggin unlock_page(page); 1224ae4d5372SAneesh Kumar K.V /* 12256e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1226ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1227f340b3d9Shongnanli * i_size_read because we hold i_rwsem. 12281938a150SAneesh Kumar K.V * 12291938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12301938a150SAneesh Kumar K.V * truncate finishes 1231ae4d5372SAneesh Kumar K.V */ 1232c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12331938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12341938a150SAneesh Kumar K.V 12351938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1236c93d8f88SEric Biggers if (extended) { 1237b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12381938a150SAneesh Kumar K.V /* 1239ffacfa7aSJan Kara * If truncate failed early the inode might 12401938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12411938a150SAneesh Kumar K.V * make sure the inode is removed from the 12421938a150SAneesh Kumar K.V * orphan list in that case. 12431938a150SAneesh Kumar K.V */ 12441938a150SAneesh Kumar K.V if (inode->i_nlink) 12451938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12461938a150SAneesh Kumar K.V } 1247bfc1af65SNick Piggin 124847564bfbSTheodore Ts'o if (ret == -ENOSPC && 124947564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 125047564bfbSTheodore Ts'o goto retry_journal; 125109cbfeafSKirill A. Shutemov put_page(page); 125247564bfbSTheodore Ts'o return ret; 125347564bfbSTheodore Ts'o } 125447564bfbSTheodore Ts'o *pagep = page; 1255ac27a0ecSDave Kleikamp return ret; 1256ac27a0ecSDave Kleikamp } 1257ac27a0ecSDave Kleikamp 1258bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1259188c299eSJan Kara static int write_end_fn(handle_t *handle, struct inode *inode, 1260188c299eSJan Kara struct buffer_head *bh) 1261ac27a0ecSDave Kleikamp { 126213fca323STheodore Ts'o int ret; 1263ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1264ac27a0ecSDave Kleikamp return 0; 1265ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 126613fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 126713fca323STheodore Ts'o clear_buffer_meta(bh); 126813fca323STheodore Ts'o clear_buffer_prio(bh); 126913fca323STheodore Ts'o return ret; 1270ac27a0ecSDave Kleikamp } 1271ac27a0ecSDave Kleikamp 1272eed4333fSZheng Liu /* 1273eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1274eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1275eed4333fSZheng Liu * 1276eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1277eed4333fSZheng Liu * buffers are managed internally. 1278eed4333fSZheng Liu */ 1279eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1280f8514083SAneesh Kumar K.V struct address_space *mapping, 1281f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1282f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1283f8514083SAneesh Kumar K.V { 1284f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1285eed4333fSZheng Liu struct inode *inode = mapping->host; 12860572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1287eed4333fSZheng Liu int ret = 0, ret2; 1288eed4333fSZheng Liu int i_size_changed = 0; 1289c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1290eed4333fSZheng Liu 1291eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 12926984aef5SZhang Yi 12936984aef5SZhang Yi if (ext4_has_inline_data(inode)) 12946984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 12956984aef5SZhang Yi 12966984aef5SZhang Yi copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1297f8514083SAneesh Kumar K.V /* 12984631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1299f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1300c93d8f88SEric Biggers * 1301c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1302c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1303f8514083SAneesh Kumar K.V */ 1304c93d8f88SEric Biggers if (!verity) 13054631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1306f8514083SAneesh Kumar K.V unlock_page(page); 130709cbfeafSKirill A. Shutemov put_page(page); 1308f8514083SAneesh Kumar K.V 1309c93d8f88SEric Biggers if (old_size < pos && !verity) 13100572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1311f8514083SAneesh Kumar K.V /* 1312f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1313f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1314f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1315f8514083SAneesh Kumar K.V * filesystems. 1316f8514083SAneesh Kumar K.V */ 13176984aef5SZhang Yi if (i_size_changed) 13184209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1319f8514083SAneesh Kumar K.V 1320c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1321f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1322f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1323f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1324f8514083SAneesh Kumar K.V */ 1325f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 132655ce2f64SZhang Yi 1327617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1328ac27a0ecSDave Kleikamp if (!ret) 1329ac27a0ecSDave Kleikamp ret = ret2; 1330bfc1af65SNick Piggin 1331c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1332b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1333f8514083SAneesh Kumar K.V /* 1334ffacfa7aSJan Kara * If truncate failed early the inode might still be 1335f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1336f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1337f8514083SAneesh Kumar K.V */ 1338f8514083SAneesh Kumar K.V if (inode->i_nlink) 1339f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1340f8514083SAneesh Kumar K.V } 1341f8514083SAneesh Kumar K.V 1342bfc1af65SNick Piggin return ret ? ret : copied; 1343ac27a0ecSDave Kleikamp } 1344ac27a0ecSDave Kleikamp 1345b90197b6STheodore Ts'o /* 1346b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1347b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1348b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1349b90197b6STheodore Ts'o */ 13503b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 1351188c299eSJan Kara struct inode *inode, 13523b136499SJan Kara struct page *page, 13533b136499SJan Kara unsigned from, unsigned to) 1354b90197b6STheodore Ts'o { 1355b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1356b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1357b90197b6STheodore Ts'o 1358b90197b6STheodore Ts'o bh = head = page_buffers(page); 1359b90197b6STheodore Ts'o do { 1360b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1361b90197b6STheodore Ts'o if (buffer_new(bh)) { 1362b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1363b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1364b90197b6STheodore Ts'o unsigned start, size; 1365b90197b6STheodore Ts'o 1366b90197b6STheodore Ts'o start = max(from, block_start); 1367b90197b6STheodore Ts'o size = min(to, block_end) - start; 1368b90197b6STheodore Ts'o 1369b90197b6STheodore Ts'o zero_user(page, start, size); 1370188c299eSJan Kara write_end_fn(handle, inode, bh); 1371b90197b6STheodore Ts'o } 1372b90197b6STheodore Ts'o clear_buffer_new(bh); 1373b90197b6STheodore Ts'o } 1374b90197b6STheodore Ts'o } 1375b90197b6STheodore Ts'o block_start = block_end; 1376b90197b6STheodore Ts'o bh = bh->b_this_page; 1377b90197b6STheodore Ts'o } while (bh != head); 1378b90197b6STheodore Ts'o } 1379b90197b6STheodore Ts'o 1380bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1381bfc1af65SNick Piggin struct address_space *mapping, 1382bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1383bfc1af65SNick Piggin struct page *page, void *fsdata) 1384ac27a0ecSDave Kleikamp { 1385617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1386bfc1af65SNick Piggin struct inode *inode = mapping->host; 13870572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1388ac27a0ecSDave Kleikamp int ret = 0, ret2; 1389ac27a0ecSDave Kleikamp int partial = 0; 1390bfc1af65SNick Piggin unsigned from, to; 13914631dbf6SDmitry Monakhov int size_changed = 0; 1392c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1393ac27a0ecSDave Kleikamp 13949bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 139509cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1396bfc1af65SNick Piggin to = from + len; 1397bfc1af65SNick Piggin 1398441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1399441c8508SCurt Wohlgemuth 14006984aef5SZhang Yi if (ext4_has_inline_data(inode)) 14016984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 14026984aef5SZhang Yi 14036984aef5SZhang Yi if (unlikely(copied < len) && !PageUptodate(page)) { 1404bfc1af65SNick Piggin copied = 0; 1405188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, from, to); 14063b136499SJan Kara } else { 14073b136499SJan Kara if (unlikely(copied < len)) 1408188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, 14093b136499SJan Kara from + copied, to); 1410188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_buffers(page), 1411188c299eSJan Kara from, from + copied, &partial, 14123b136499SJan Kara write_end_fn); 1413ac27a0ecSDave Kleikamp if (!partial) 1414ac27a0ecSDave Kleikamp SetPageUptodate(page); 14153fdcfb66STao Ma } 1416c93d8f88SEric Biggers if (!verity) 14174631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 141819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14192d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14204631dbf6SDmitry Monakhov unlock_page(page); 142109cbfeafSKirill A. Shutemov put_page(page); 14224631dbf6SDmitry Monakhov 1423c93d8f88SEric Biggers if (old_size < pos && !verity) 14240572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14250572639fSXiaoguang Wang 14266984aef5SZhang Yi if (size_changed) { 1427617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1428ac27a0ecSDave Kleikamp if (!ret) 1429ac27a0ecSDave Kleikamp ret = ret2; 1430ac27a0ecSDave Kleikamp } 1431bfc1af65SNick Piggin 1432c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1433f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1434f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1435f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1436f8514083SAneesh Kumar K.V */ 1437f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1438f8514083SAneesh Kumar K.V 1439617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1440ac27a0ecSDave Kleikamp if (!ret) 1441ac27a0ecSDave Kleikamp ret = ret2; 1442c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1443b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1444f8514083SAneesh Kumar K.V /* 1445ffacfa7aSJan Kara * If truncate failed early the inode might still be 1446f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1447f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1448f8514083SAneesh Kumar K.V */ 1449f8514083SAneesh Kumar K.V if (inode->i_nlink) 1450f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1451f8514083SAneesh Kumar K.V } 1452bfc1af65SNick Piggin 1453bfc1af65SNick Piggin return ret ? ret : copied; 1454ac27a0ecSDave Kleikamp } 1455d2a17637SMingming Cao 14569d0be502STheodore Ts'o /* 1457c27e43a1SEric Whitney * Reserve space for a single cluster 14589d0be502STheodore Ts'o */ 1459c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1460d2a17637SMingming Cao { 1461d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14620637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14635dd4056dSChristoph Hellwig int ret; 1464d2a17637SMingming Cao 146560e58e0fSMingming Cao /* 146672b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 146772b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 146872b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 146960e58e0fSMingming Cao */ 14707b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14715dd4056dSChristoph Hellwig if (ret) 14725dd4056dSChristoph Hellwig return ret; 147303179fe9STheodore Ts'o 147403179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 147571d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 147603179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147703179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1478d2a17637SMingming Cao return -ENOSPC; 1479d2a17637SMingming Cao } 14809d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1481c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14820637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 148339bc680aSDmitry Monakhov 1484d2a17637SMingming Cao return 0; /* success */ 1485d2a17637SMingming Cao } 1486d2a17637SMingming Cao 1487f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1488d2a17637SMingming Cao { 1489d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14900637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1491d2a17637SMingming Cao 1492cd213226SMingming Cao if (!to_free) 1493cd213226SMingming Cao return; /* Nothing to release, exit */ 1494cd213226SMingming Cao 1495d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1496cd213226SMingming Cao 14975a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14980637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1499cd213226SMingming Cao /* 15000637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 15010637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 15020637c6f4STheodore Ts'o * function is called from invalidate page, it's 15030637c6f4STheodore Ts'o * harmless to return without any action. 1504cd213226SMingming Cao */ 15058de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15060637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15071084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15080637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15090637c6f4STheodore Ts'o WARN_ON(1); 15100637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15110637c6f4STheodore Ts'o } 15120637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15130637c6f4STheodore Ts'o 151472b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 151557042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1516d2a17637SMingming Cao 1517d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 151860e58e0fSMingming Cao 15197b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1520d2a17637SMingming Cao } 1521d2a17637SMingming Cao 1522ac27a0ecSDave Kleikamp /* 152364769240SAlex Tomas * Delayed allocation stuff 152464769240SAlex Tomas */ 152564769240SAlex Tomas 15264e7ea81dSJan Kara struct mpage_da_data { 15274e7ea81dSJan Kara struct inode *inode; 15284e7ea81dSJan Kara struct writeback_control *wbc; 15296b523df4SJan Kara 15304e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15314e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15324e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 153364769240SAlex Tomas /* 15344e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15354e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15364e7ea81dSJan Kara * is delalloc or unwritten. 153764769240SAlex Tomas */ 15384e7ea81dSJan Kara struct ext4_map_blocks map; 15394e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1540dddbd6acSJan Kara unsigned int do_map:1; 15416b8ed620SJan Kara unsigned int scanned_until_end:1; 15424e7ea81dSJan Kara }; 154364769240SAlex Tomas 15444e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15454e7ea81dSJan Kara bool invalidate) 1546c4a0c46eSAneesh Kumar K.V { 1547c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1548c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1549c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1550c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1551c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15524e7ea81dSJan Kara 15534e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15544e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15554e7ea81dSJan Kara return; 1556c4a0c46eSAneesh Kumar K.V 15576b8ed620SJan Kara mpd->scanned_until_end = 0; 1558c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1559c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15604e7ea81dSJan Kara if (invalidate) { 15614e7ea81dSJan Kara ext4_lblk_t start, last; 156209cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 156309cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 156451865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15654e7ea81dSJan Kara } 156651865fdaSZheng Liu 156786679820SMel Gorman pagevec_init(&pvec); 1568c4a0c46eSAneesh Kumar K.V while (index <= end) { 1569397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1570c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1571c4a0c46eSAneesh Kumar K.V break; 1572c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1573c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15742b85a617SJan Kara 1575c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1576c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15774e7ea81dSJan Kara if (invalidate) { 15784e800c03Swangguang if (page_mapped(page)) 15794e800c03Swangguang clear_page_dirty_for_io(page); 158009cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1581c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15824e7ea81dSJan Kara } 1583c4a0c46eSAneesh Kumar K.V unlock_page(page); 1584c4a0c46eSAneesh Kumar K.V } 15859b1d0998SJan Kara pagevec_release(&pvec); 1586c4a0c46eSAneesh Kumar K.V } 1587c4a0c46eSAneesh Kumar K.V } 1588c4a0c46eSAneesh Kumar K.V 1589df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1590df22291fSAneesh Kumar K.V { 1591df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 159292b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1593f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 159492b97816STheodore Ts'o 159592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15965dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1597f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 159892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 159992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1600f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 160157042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 160292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1603f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16047b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 160592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 160692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1607f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1608df22291fSAneesh Kumar K.V return; 1609df22291fSAneesh Kumar K.V } 1610df22291fSAneesh Kumar K.V 1611188c299eSJan Kara static int ext4_bh_delay_or_unwritten(handle_t *handle, struct inode *inode, 1612188c299eSJan Kara struct buffer_head *bh) 161329fa89d0SAneesh Kumar K.V { 1614c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 161529fa89d0SAneesh Kumar K.V } 161629fa89d0SAneesh Kumar K.V 161764769240SAlex Tomas /* 16180b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16190b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16200b02f4c0SEric Whitney * count or making a pending reservation 16210b02f4c0SEric Whitney * where needed 16220b02f4c0SEric Whitney * 16230b02f4c0SEric Whitney * @inode - file containing the newly added block 16240b02f4c0SEric Whitney * @lblk - logical block to be added 16250b02f4c0SEric Whitney * 16260b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16270b02f4c0SEric Whitney */ 16280b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16290b02f4c0SEric Whitney { 16300b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16310b02f4c0SEric Whitney int ret; 16320b02f4c0SEric Whitney bool allocated = false; 16336fed8395SJeffle Xu bool reserved = false; 16340b02f4c0SEric Whitney 16350b02f4c0SEric Whitney /* 16360b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16370b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16380b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16390b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16400b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16410b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16420b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16430b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16440b02f4c0SEric Whitney * extents status tree doesn't get a match. 16450b02f4c0SEric Whitney */ 16460b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16470b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16480b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16490b02f4c0SEric Whitney goto errout; 16506fed8395SJeffle Xu reserved = true; 16510b02f4c0SEric Whitney } else { /* bigalloc */ 16520b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16530b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16540b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16550b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16560b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16570b02f4c0SEric Whitney if (ret < 0) 16580b02f4c0SEric Whitney goto errout; 16590b02f4c0SEric Whitney if (ret == 0) { 16600b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16610b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16620b02f4c0SEric Whitney goto errout; 16636fed8395SJeffle Xu reserved = true; 16640b02f4c0SEric Whitney } else { 16650b02f4c0SEric Whitney allocated = true; 16660b02f4c0SEric Whitney } 16670b02f4c0SEric Whitney } else { 16680b02f4c0SEric Whitney allocated = true; 16690b02f4c0SEric Whitney } 16700b02f4c0SEric Whitney } 16710b02f4c0SEric Whitney } 16720b02f4c0SEric Whitney 16730b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16746fed8395SJeffle Xu if (ret && reserved) 16756fed8395SJeffle Xu ext4_da_release_space(inode, 1); 16760b02f4c0SEric Whitney 16770b02f4c0SEric Whitney errout: 16780b02f4c0SEric Whitney return ret; 16790b02f4c0SEric Whitney } 16800b02f4c0SEric Whitney 16810b02f4c0SEric Whitney /* 16825356f261SAditya Kali * This function is grabs code from the very beginning of 16835356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16845356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16855356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16865356f261SAditya Kali */ 16875356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16885356f261SAditya Kali struct ext4_map_blocks *map, 16895356f261SAditya Kali struct buffer_head *bh) 16905356f261SAditya Kali { 1691d100eef2SZheng Liu struct extent_status es; 16925356f261SAditya Kali int retval; 16935356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1694921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1695921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1696921f266bSDmitry Monakhov 1697921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1698921f266bSDmitry Monakhov #endif 16995356f261SAditya Kali 17005356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 17015356f261SAditya Kali invalid_block = ~0; 17025356f261SAditya Kali 17035356f261SAditya Kali map->m_flags = 0; 170470aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 17055356f261SAditya Kali (unsigned long) map->m_lblk); 1706d100eef2SZheng Liu 1707d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1708bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1709d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1710d100eef2SZheng Liu retval = 0; 1711c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1712d100eef2SZheng Liu goto add_delayed; 1713d100eef2SZheng Liu } 1714d100eef2SZheng Liu 1715d100eef2SZheng Liu /* 17163eda41dfSEric Whitney * Delayed extent could be allocated by fallocate. 17173eda41dfSEric Whitney * So we need to check it. 1718d100eef2SZheng Liu */ 17193eda41dfSEric Whitney if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 17203eda41dfSEric Whitney map_bh(bh, inode->i_sb, invalid_block); 17213eda41dfSEric Whitney set_buffer_new(bh); 17223eda41dfSEric Whitney set_buffer_delay(bh); 1723d100eef2SZheng Liu return 0; 1724d100eef2SZheng Liu } 1725d100eef2SZheng Liu 1726d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1727d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1728d100eef2SZheng Liu if (retval > map->m_len) 1729d100eef2SZheng Liu retval = map->m_len; 1730d100eef2SZheng Liu map->m_len = retval; 1731d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1732d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1733d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1734d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1735d100eef2SZheng Liu else 17361e83bc81SArnd Bergmann BUG(); 1737d100eef2SZheng Liu 1738921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1739921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1740921f266bSDmitry Monakhov #endif 1741d100eef2SZheng Liu return retval; 1742d100eef2SZheng Liu } 1743d100eef2SZheng Liu 17445356f261SAditya Kali /* 17455356f261SAditya Kali * Try to see if we can get the block without requesting a new 17465356f261SAditya Kali * file system block. 17475356f261SAditya Kali */ 1748c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1749cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17509c3569b5STao Ma retval = 0; 1751cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17522f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17535356f261SAditya Kali else 17542f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17555356f261SAditya Kali 1756d100eef2SZheng Liu add_delayed: 17575356f261SAditya Kali if (retval == 0) { 1758f7fec032SZheng Liu int ret; 1759ad431025SEric Whitney 17605356f261SAditya Kali /* 17615356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17625356f261SAditya Kali * is it OK? 17635356f261SAditya Kali */ 17645356f261SAditya Kali 17650b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17660b02f4c0SEric Whitney if (ret != 0) { 1767f7fec032SZheng Liu retval = ret; 176851865fdaSZheng Liu goto out_unlock; 1769f7fec032SZheng Liu } 177051865fdaSZheng Liu 17715356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17725356f261SAditya Kali set_buffer_new(bh); 17735356f261SAditya Kali set_buffer_delay(bh); 1774f7fec032SZheng Liu } else if (retval > 0) { 1775f7fec032SZheng Liu int ret; 17763be78c73STheodore Ts'o unsigned int status; 1777f7fec032SZheng Liu 177844fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 177944fb851dSZheng Liu ext4_warning(inode->i_sb, 178044fb851dSZheng Liu "ES len assertion failed for inode " 178144fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 178244fb851dSZheng Liu inode->i_ino, retval, map->m_len); 178344fb851dSZheng Liu WARN_ON(1); 1784921f266bSDmitry Monakhov } 1785921f266bSDmitry Monakhov 1786f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1787f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1788f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1789f7fec032SZheng Liu map->m_pblk, status); 1790f7fec032SZheng Liu if (ret != 0) 1791f7fec032SZheng Liu retval = ret; 17925356f261SAditya Kali } 17935356f261SAditya Kali 17945356f261SAditya Kali out_unlock: 17955356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17965356f261SAditya Kali 17975356f261SAditya Kali return retval; 17985356f261SAditya Kali } 17995356f261SAditya Kali 18005356f261SAditya Kali /* 1801d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1802b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1803b920c755STheodore Ts'o * reserve space for a single block. 180429fa89d0SAneesh Kumar K.V * 180529fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 180629fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 180729fa89d0SAneesh Kumar K.V * 180829fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 180929fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 181029fa89d0SAneesh Kumar K.V * initialized properly. 181164769240SAlex Tomas */ 18129c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18132ed88685STheodore Ts'o struct buffer_head *bh, int create) 181464769240SAlex Tomas { 18152ed88685STheodore Ts'o struct ext4_map_blocks map; 181664769240SAlex Tomas int ret = 0; 181764769240SAlex Tomas 181864769240SAlex Tomas BUG_ON(create == 0); 18192ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18202ed88685STheodore Ts'o 18212ed88685STheodore Ts'o map.m_lblk = iblock; 18222ed88685STheodore Ts'o map.m_len = 1; 182364769240SAlex Tomas 182464769240SAlex Tomas /* 182564769240SAlex Tomas * first, we need to know whether the block is allocated already 182664769240SAlex Tomas * preallocated blocks are unmapped but should treated 182764769240SAlex Tomas * the same as allocated blocks. 182864769240SAlex Tomas */ 18295356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18305356f261SAditya Kali if (ret <= 0) 18312ed88685STheodore Ts'o return ret; 183264769240SAlex Tomas 18332ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1834ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18352ed88685STheodore Ts'o 18362ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18372ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18382ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18392ed88685STheodore Ts'o * get_block multiple times when we write to the same 18402ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18412ed88685STheodore Ts'o * for partial write. 18422ed88685STheodore Ts'o */ 18432ed88685STheodore Ts'o set_buffer_new(bh); 1844c8205636STheodore Ts'o set_buffer_mapped(bh); 18452ed88685STheodore Ts'o } 18462ed88685STheodore Ts'o return 0; 184764769240SAlex Tomas } 184861628a3fSMingming Cao 184962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 185062e086beSAneesh Kumar K.V unsigned int len) 185162e086beSAneesh Kumar K.V { 185262e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 185362e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 185462e086beSAneesh Kumar K.V handle_t *handle = NULL; 18553fdcfb66STao Ma int ret = 0, err = 0; 18563fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18573fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 18585c48a7dfSZhang Yi loff_t size; 185962e086beSAneesh Kumar K.V 1860cb20d518STheodore Ts'o ClearPageChecked(page); 18613fdcfb66STao Ma 18623fdcfb66STao Ma if (inline_data) { 18633fdcfb66STao Ma BUG_ON(page->index != 0); 18643fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18653fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18663fdcfb66STao Ma if (inode_bh == NULL) 18673fdcfb66STao Ma goto out; 18683fdcfb66STao Ma } 1869bdf96838STheodore Ts'o /* 1870bdf96838STheodore Ts'o * We need to release the page lock before we start the 1871bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1872bdf96838STheodore Ts'o * out from under us. 1873bdf96838STheodore Ts'o */ 1874bdf96838STheodore Ts'o get_page(page); 187562e086beSAneesh Kumar K.V unlock_page(page); 187662e086beSAneesh Kumar K.V 18779924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18789924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 187962e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 188062e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1881bdf96838STheodore Ts'o put_page(page); 1882bdf96838STheodore Ts'o goto out_no_pagelock; 1883bdf96838STheodore Ts'o } 1884bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1885bdf96838STheodore Ts'o 1886bdf96838STheodore Ts'o lock_page(page); 1887bdf96838STheodore Ts'o put_page(page); 18885c48a7dfSZhang Yi size = i_size_read(inode); 18895c48a7dfSZhang Yi if (page->mapping != mapping || page_offset(page) > size) { 1890bdf96838STheodore Ts'o /* The page got truncated from under us */ 1891bdf96838STheodore Ts'o ext4_journal_stop(handle); 1892bdf96838STheodore Ts'o ret = 0; 189362e086beSAneesh Kumar K.V goto out; 189462e086beSAneesh Kumar K.V } 189562e086beSAneesh Kumar K.V 18963fdcfb66STao Ma if (inline_data) { 1897362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 18983fdcfb66STao Ma } else { 18995c48a7dfSZhang Yi struct buffer_head *page_bufs = page_buffers(page); 19005c48a7dfSZhang Yi 19015c48a7dfSZhang Yi if (page->index == size >> PAGE_SHIFT) 19025c48a7dfSZhang Yi len = size & ~PAGE_MASK; 19035c48a7dfSZhang Yi else 19045c48a7dfSZhang Yi len = PAGE_SIZE; 19055c48a7dfSZhang Yi 1906188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1907188c299eSJan Kara NULL, do_journal_get_write_access); 190862e086beSAneesh Kumar K.V 1909188c299eSJan Kara err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1910188c299eSJan Kara NULL, write_end_fn); 19113fdcfb66STao Ma } 191262e086beSAneesh Kumar K.V if (ret == 0) 191362e086beSAneesh Kumar K.V ret = err; 1914b5b18160SJan Kara err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 1915afb585a9SMauricio Faria de Oliveira if (ret == 0) 1916afb585a9SMauricio Faria de Oliveira ret = err; 19172d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 191862e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 191962e086beSAneesh Kumar K.V if (!ret) 192062e086beSAneesh Kumar K.V ret = err; 192162e086beSAneesh Kumar K.V 192219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 192362e086beSAneesh Kumar K.V out: 1924bdf96838STheodore Ts'o unlock_page(page); 1925bdf96838STheodore Ts'o out_no_pagelock: 19263fdcfb66STao Ma brelse(inode_bh); 192762e086beSAneesh Kumar K.V return ret; 192862e086beSAneesh Kumar K.V } 192962e086beSAneesh Kumar K.V 193061628a3fSMingming Cao /* 193143ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 193243ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 193343ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 193443ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193543ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 193643ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 193743ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 193843ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 193943ce1d23SAneesh Kumar K.V * 1940b920c755STheodore Ts'o * This function can get called via... 194120970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1942b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1943f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1944b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194543ce1d23SAneesh Kumar K.V * 194643ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 194743ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 194843ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 194943ce1d23SAneesh Kumar K.V * truncate(f, 1024); 195043ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195143ce1d23SAneesh Kumar K.V * a[0] = 'a'; 195243ce1d23SAneesh Kumar K.V * truncate(f, 4096); 195343ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 195490802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195543ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 195643ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 195743ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 195843ce1d23SAneesh Kumar K.V * buffer_heads mapped. 195943ce1d23SAneesh Kumar K.V * 196043ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196143ce1d23SAneesh Kumar K.V * unwritten in the page. 196243ce1d23SAneesh Kumar K.V * 196343ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 196443ce1d23SAneesh Kumar K.V * 196543ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 196643ce1d23SAneesh Kumar K.V * ext4_writepage() 196743ce1d23SAneesh Kumar K.V * 196843ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 196943ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 197061628a3fSMingming Cao */ 197143ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 197264769240SAlex Tomas struct writeback_control *wbc) 197364769240SAlex Tomas { 1974f8bec370SJan Kara int ret = 0; 197561628a3fSMingming Cao loff_t size; 1976498e5f24STheodore Ts'o unsigned int len; 1977744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 197861628a3fSMingming Cao struct inode *inode = page->mapping->host; 197936ade451SJan Kara struct ext4_io_submit io_submit; 19801c8349a1SNamjae Jeon bool keep_towrite = false; 198164769240SAlex Tomas 19820db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 1983c2a559bcSyangerkun inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 19840db1ff22STheodore Ts'o unlock_page(page); 19850db1ff22STheodore Ts'o return -EIO; 19860db1ff22STheodore Ts'o } 19870db1ff22STheodore Ts'o 1988a9c667f8SLukas Czerner trace_ext4_writepage(page); 198961628a3fSMingming Cao size = i_size_read(inode); 1990c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 1991c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 199209cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 199361628a3fSMingming Cao else 199409cbfeafSKirill A. Shutemov len = PAGE_SIZE; 199561628a3fSMingming Cao 1996cc509574STheodore Ts'o /* Should never happen but for bugs in other kernel subsystems */ 1997cc509574STheodore Ts'o if (!page_has_buffers(page)) { 1998cc509574STheodore Ts'o ext4_warning_inode(inode, 1999cc509574STheodore Ts'o "page %lu does not have buffers attached", page->index); 2000cc509574STheodore Ts'o ClearPageDirty(page); 2001cc509574STheodore Ts'o unlock_page(page); 2002cc509574STheodore Ts'o return 0; 2003cc509574STheodore Ts'o } 2004cc509574STheodore Ts'o 2005f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 200664769240SAlex Tomas /* 2007fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2008fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2009fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2010fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2011fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2012cccd147aSTheodore Ts'o * 2013cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2014cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2015cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2016cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2017cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2018cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2019cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2020cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2021cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 202264769240SAlex Tomas */ 2023188c299eSJan Kara if (ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, NULL, 2024c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 202561628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2026cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 202709cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2028fe386132SJan Kara /* 2029fe386132SJan Kara * For memory cleaning there's no point in writing only 2030fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2031fe386132SJan Kara * from direct reclaim. 2032fe386132SJan Kara */ 2033fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2034fe386132SJan Kara == PF_MEMALLOC); 203561628a3fSMingming Cao unlock_page(page); 203661628a3fSMingming Cao return 0; 203761628a3fSMingming Cao } 20381c8349a1SNamjae Jeon keep_towrite = true; 2039f0e6c985SAneesh Kumar K.V } 204064769240SAlex Tomas 2041cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 204243ce1d23SAneesh Kumar K.V /* 204343ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 204443ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 204543ce1d23SAneesh Kumar K.V */ 20463f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 204743ce1d23SAneesh Kumar K.V 204897a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 204997a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 205097a851edSJan Kara if (!io_submit.io_end) { 205197a851edSJan Kara redirty_page_for_writepage(wbc, page); 205297a851edSJan Kara unlock_page(page); 205397a851edSJan Kara return -ENOMEM; 205497a851edSJan Kara } 2055be993933SLei Chen ret = ext4_bio_write_page(&io_submit, page, len, keep_towrite); 205636ade451SJan Kara ext4_io_submit(&io_submit); 205797a851edSJan Kara /* Drop io_end reference we got from init */ 205897a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 205964769240SAlex Tomas return ret; 206064769240SAlex Tomas } 206164769240SAlex Tomas 20625f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20635f1132b2SJan Kara { 20645f1132b2SJan Kara int len; 2065a056bdaaSJan Kara loff_t size; 20665f1132b2SJan Kara int err; 20675f1132b2SJan Kara 20685f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2069a056bdaaSJan Kara clear_page_dirty_for_io(page); 2070a056bdaaSJan Kara /* 2071a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2072a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2073a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2074a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2075a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2076a056bdaaSJan Kara * written to again until we release page lock. So only after 2077a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2078a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2079a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2080a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2081a056bdaaSJan Kara * after page tables are updated. 2082a056bdaaSJan Kara */ 2083a056bdaaSJan Kara size = i_size_read(mpd->inode); 2084c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2085c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 208609cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20875f1132b2SJan Kara else 208809cbfeafSKirill A. Shutemov len = PAGE_SIZE; 2089be993933SLei Chen err = ext4_bio_write_page(&mpd->io_submit, page, len, false); 20905f1132b2SJan Kara if (!err) 20915f1132b2SJan Kara mpd->wbc->nr_to_write--; 20925f1132b2SJan Kara mpd->first_page++; 20935f1132b2SJan Kara 20945f1132b2SJan Kara return err; 20955f1132b2SJan Kara } 20965f1132b2SJan Kara 20976db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 20984e7ea81dSJan Kara 209961628a3fSMingming Cao /* 2100fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2101fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2102fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 210361628a3fSMingming Cao */ 2104fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2105525f4ed8SMingming Cao 2106525f4ed8SMingming Cao /* 21074e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 21084e7ea81dSJan Kara * 21094e7ea81dSJan Kara * @mpd - extent of blocks 21104e7ea81dSJan Kara * @lblk - logical number of the block in the file 211109930042SJan Kara * @bh - buffer head we want to add to the extent 21124e7ea81dSJan Kara * 211309930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 211409930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 211509930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 211609930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 211709930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 211809930042SJan Kara * added. 21194e7ea81dSJan Kara */ 212009930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 212109930042SJan Kara struct buffer_head *bh) 21224e7ea81dSJan Kara { 21234e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21244e7ea81dSJan Kara 212509930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 212609930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 212709930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 212809930042SJan Kara /* So far no extent to map => we write the buffer right away */ 212909930042SJan Kara if (map->m_len == 0) 213009930042SJan Kara return true; 213109930042SJan Kara return false; 213209930042SJan Kara } 21334e7ea81dSJan Kara 21344e7ea81dSJan Kara /* First block in the extent? */ 21354e7ea81dSJan Kara if (map->m_len == 0) { 2136dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2137dddbd6acSJan Kara if (!mpd->do_map) 2138dddbd6acSJan Kara return false; 21394e7ea81dSJan Kara map->m_lblk = lblk; 21404e7ea81dSJan Kara map->m_len = 1; 214109930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 214209930042SJan Kara return true; 21434e7ea81dSJan Kara } 21444e7ea81dSJan Kara 214509930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 214609930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 214709930042SJan Kara return false; 214809930042SJan Kara 21494e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21504e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 215109930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21524e7ea81dSJan Kara map->m_len++; 215309930042SJan Kara return true; 21544e7ea81dSJan Kara } 215509930042SJan Kara return false; 21564e7ea81dSJan Kara } 21574e7ea81dSJan Kara 21585f1132b2SJan Kara /* 21595f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21605f1132b2SJan Kara * 21615f1132b2SJan Kara * @mpd - extent of blocks for mapping 21625f1132b2SJan Kara * @head - the first buffer in the page 21635f1132b2SJan Kara * @bh - buffer we should start processing from 21645f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21655f1132b2SJan Kara * 21665f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21675f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21685f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21695f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21705f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21715f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21725f1132b2SJan Kara * < 0 in case of error during IO submission. 21735f1132b2SJan Kara */ 21745f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21754e7ea81dSJan Kara struct buffer_head *head, 21764e7ea81dSJan Kara struct buffer_head *bh, 21774e7ea81dSJan Kara ext4_lblk_t lblk) 21784e7ea81dSJan Kara { 21794e7ea81dSJan Kara struct inode *inode = mpd->inode; 21805f1132b2SJan Kara int err; 218193407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21824e7ea81dSJan Kara >> inode->i_blkbits; 21834e7ea81dSJan Kara 2184c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2185c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2186c93d8f88SEric Biggers 21874e7ea81dSJan Kara do { 21884e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21894e7ea81dSJan Kara 219009930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21914e7ea81dSJan Kara /* Found extent to map? */ 21924e7ea81dSJan Kara if (mpd->map.m_len) 21935f1132b2SJan Kara return 0; 2194dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2195dddbd6acSJan Kara if (!mpd->do_map) 2196dddbd6acSJan Kara return 0; 219709930042SJan Kara /* Everything mapped so far and we hit EOF */ 21985f1132b2SJan Kara break; 21994e7ea81dSJan Kara } 22004e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 22015f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 22025f1132b2SJan Kara if (mpd->map.m_len == 0) { 22035f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 22045f1132b2SJan Kara if (err < 0) 22054e7ea81dSJan Kara return err; 22064e7ea81dSJan Kara } 22076b8ed620SJan Kara if (lblk >= blocks) { 22086b8ed620SJan Kara mpd->scanned_until_end = 1; 22096b8ed620SJan Kara return 0; 22106b8ed620SJan Kara } 22116b8ed620SJan Kara return 1; 22125f1132b2SJan Kara } 22134e7ea81dSJan Kara 22144e7ea81dSJan Kara /* 22152943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22162943fdbcSRitesh Harjani * may submit fully mapped page for IO 22172943fdbcSRitesh Harjani * 22182943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22192943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22202943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22212943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22222943fdbcSRitesh Harjani * mapping or not. 22232943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22242943fdbcSRitesh Harjani * state according to new extent state. 22252943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22262943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22272943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22282943fdbcSRitesh Harjani */ 22292943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22302943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22312943fdbcSRitesh Harjani bool *map_bh) 22322943fdbcSRitesh Harjani { 22332943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22342943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22352943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22362943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22372943fdbcSRitesh Harjani int err = 0; 2238c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2239c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2240c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22412943fdbcSRitesh Harjani 22422943fdbcSRitesh Harjani bh = head = page_buffers(page); 22432943fdbcSRitesh Harjani do { 22442943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22452943fdbcSRitesh Harjani continue; 22462943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22472943fdbcSRitesh Harjani /* 22482943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22492943fdbcSRitesh Harjani * Find next buffer in the page to map. 22502943fdbcSRitesh Harjani */ 22512943fdbcSRitesh Harjani mpd->map.m_len = 0; 22522943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2253c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 22542943fdbcSRitesh Harjani 22552943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22562943fdbcSRitesh Harjani if (err > 0) 22572943fdbcSRitesh Harjani err = 0; 2258c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2259c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22604d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22614d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22624d06bfb9SRitesh Harjani goto out; 22634d06bfb9SRitesh Harjani } 2264d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2265c8cc8816SRitesh Harjani } 22662943fdbcSRitesh Harjani *map_bh = true; 22672943fdbcSRitesh Harjani goto out; 22682943fdbcSRitesh Harjani } 22692943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22702943fdbcSRitesh Harjani clear_buffer_delay(bh); 22712943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22722943fdbcSRitesh Harjani } 22732943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2274c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22752943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2276c8cc8816SRitesh Harjani 2277c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 22782943fdbcSRitesh Harjani *map_bh = false; 22792943fdbcSRitesh Harjani out: 22802943fdbcSRitesh Harjani *m_lblk = lblk; 22812943fdbcSRitesh Harjani *m_pblk = pblock; 22822943fdbcSRitesh Harjani return err; 22832943fdbcSRitesh Harjani } 22842943fdbcSRitesh Harjani 22852943fdbcSRitesh Harjani /* 22864e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22874e7ea81dSJan Kara * submit fully mapped pages for IO 22884e7ea81dSJan Kara * 22894e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22904e7ea81dSJan Kara * 22914e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22924e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22934e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2294556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 22954e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 22964e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 22974e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 22984e7ea81dSJan Kara */ 22994e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 23004e7ea81dSJan Kara { 23014e7ea81dSJan Kara struct pagevec pvec; 23024e7ea81dSJan Kara int nr_pages, i; 23034e7ea81dSJan Kara struct inode *inode = mpd->inode; 230409cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 23054e7ea81dSJan Kara pgoff_t start, end; 23064e7ea81dSJan Kara ext4_lblk_t lblk; 23072943fdbcSRitesh Harjani ext4_fsblk_t pblock; 23084e7ea81dSJan Kara int err; 23092943fdbcSRitesh Harjani bool map_bh = false; 23104e7ea81dSJan Kara 23114e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23124e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23134e7ea81dSJan Kara lblk = start << bpp_bits; 23144e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23154e7ea81dSJan Kara 231686679820SMel Gorman pagevec_init(&pvec); 23174e7ea81dSJan Kara while (start <= end) { 23182b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2319397162ffSJan Kara &start, end); 23204e7ea81dSJan Kara if (nr_pages == 0) 23214e7ea81dSJan Kara break; 23224e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23234e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23244e7ea81dSJan Kara 23252943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23262943fdbcSRitesh Harjani &map_bh); 23274e7ea81dSJan Kara /* 23282943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23292943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23302943fdbcSRitesh Harjani * So we return to call further extent mapping. 23314e7ea81dSJan Kara */ 233239c0ae16SJason Yan if (err < 0 || map_bh) 23332943fdbcSRitesh Harjani goto out; 23344e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23354e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23362943fdbcSRitesh Harjani if (err < 0) 23372943fdbcSRitesh Harjani goto out; 23384e7ea81dSJan Kara } 23394e7ea81dSJan Kara pagevec_release(&pvec); 23404e7ea81dSJan Kara } 23414e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23424e7ea81dSJan Kara mpd->map.m_len = 0; 23434e7ea81dSJan Kara mpd->map.m_flags = 0; 23444e7ea81dSJan Kara return 0; 23452943fdbcSRitesh Harjani out: 23462943fdbcSRitesh Harjani pagevec_release(&pvec); 23472943fdbcSRitesh Harjani return err; 23484e7ea81dSJan Kara } 23494e7ea81dSJan Kara 23504e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23514e7ea81dSJan Kara { 23524e7ea81dSJan Kara struct inode *inode = mpd->inode; 23534e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23544e7ea81dSJan Kara int get_blocks_flags; 2355090f32eeSLukas Czerner int err, dioread_nolock; 23564e7ea81dSJan Kara 23574e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23584e7ea81dSJan Kara /* 23594e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2360556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23614e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23624e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23634e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23644e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23654e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23664e7ea81dSJan Kara * possible. 23674e7ea81dSJan Kara * 2368754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2369754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2370754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2371754cfed6STheodore Ts'o * the data was copied into the page cache. 23724e7ea81dSJan Kara */ 23734e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2374ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2375ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2376090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2377090f32eeSLukas Czerner if (dioread_nolock) 23784e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23796db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 23804e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23814e7ea81dSJan Kara 23824e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23834e7ea81dSJan Kara if (err < 0) 23844e7ea81dSJan Kara return err; 2385090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23866b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23876b523df4SJan Kara ext4_handle_valid(handle)) { 23886b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23896b523df4SJan Kara handle->h_rsv_handle = NULL; 23906b523df4SJan Kara } 23913613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23926b523df4SJan Kara } 23934e7ea81dSJan Kara 23944e7ea81dSJan Kara BUG_ON(map->m_len == 0); 23954e7ea81dSJan Kara return 0; 23964e7ea81dSJan Kara } 23974e7ea81dSJan Kara 23984e7ea81dSJan Kara /* 23994e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 24004e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 24014e7ea81dSJan Kara * 24024e7ea81dSJan Kara * @handle - handle for journal operations 24034e7ea81dSJan Kara * @mpd - extent to map 24047534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 24057534e854SJan Kara * is no hope of writing the data. The caller should discard 24067534e854SJan Kara * dirty pages to avoid infinite loops. 24074e7ea81dSJan Kara * 24084e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 24094e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 24104e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 24114e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24124e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24134e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24144e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24154e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24164e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24174e7ea81dSJan Kara */ 24184e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2419cb530541STheodore Ts'o struct mpage_da_data *mpd, 2420cb530541STheodore Ts'o bool *give_up_on_write) 24214e7ea81dSJan Kara { 24224e7ea81dSJan Kara struct inode *inode = mpd->inode; 24234e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24244e7ea81dSJan Kara int err; 24254e7ea81dSJan Kara loff_t disksize; 24266603120eSDmitry Monakhov int progress = 0; 2427c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24284d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24294e7ea81dSJan Kara 24304d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24314d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24324d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2433c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 243427d7c4edSJan Kara do { 24354e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24364e7ea81dSJan Kara if (err < 0) { 24374e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24384e7ea81dSJan Kara 24390db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24409b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2441cb530541STheodore Ts'o goto invalidate_dirty_pages; 24424e7ea81dSJan Kara /* 2443cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2444cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2445cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24464e7ea81dSJan Kara */ 2447cb530541STheodore Ts'o if ((err == -ENOMEM) || 24486603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24496603120eSDmitry Monakhov if (progress) 24506603120eSDmitry Monakhov goto update_disksize; 2451cb530541STheodore Ts'o return err; 24526603120eSDmitry Monakhov } 24534e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24544e7ea81dSJan Kara "Delayed block allocation failed for " 24554e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24564e7ea81dSJan Kara " max blocks %u with error %d", 24574e7ea81dSJan Kara inode->i_ino, 24584e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2459cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24604e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24614e7ea81dSJan Kara "This should not happen!! Data will " 24624e7ea81dSJan Kara "be lost\n"); 24634e7ea81dSJan Kara if (err == -ENOSPC) 24644e7ea81dSJan Kara ext4_print_free_blocks(inode); 2465cb530541STheodore Ts'o invalidate_dirty_pages: 2466cb530541STheodore Ts'o *give_up_on_write = true; 24674e7ea81dSJan Kara return err; 24684e7ea81dSJan Kara } 24696603120eSDmitry Monakhov progress = 1; 24704e7ea81dSJan Kara /* 24714e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24724e7ea81dSJan Kara * extent to map 24734e7ea81dSJan Kara */ 24744e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24754e7ea81dSJan Kara if (err < 0) 24766603120eSDmitry Monakhov goto update_disksize; 247727d7c4edSJan Kara } while (map->m_len); 24784e7ea81dSJan Kara 24796603120eSDmitry Monakhov update_disksize: 2480622cad13STheodore Ts'o /* 2481622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2482622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2483622cad13STheodore Ts'o */ 248409cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 248535df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 24864e7ea81dSJan Kara int err2; 2487622cad13STheodore Ts'o loff_t i_size; 24884e7ea81dSJan Kara 2489622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2490622cad13STheodore Ts'o i_size = i_size_read(inode); 2491622cad13STheodore Ts'o if (disksize > i_size) 2492622cad13STheodore Ts'o disksize = i_size; 2493622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2494622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2495622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2496b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2497878520acSTheodore Ts'o if (err2) { 249854d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 24994e7ea81dSJan Kara "Failed to mark inode %lu dirty", 25004e7ea81dSJan Kara inode->i_ino); 2501878520acSTheodore Ts'o } 25024e7ea81dSJan Kara if (!err) 25034e7ea81dSJan Kara err = err2; 25044e7ea81dSJan Kara } 25054e7ea81dSJan Kara return err; 25064e7ea81dSJan Kara } 25074e7ea81dSJan Kara 25084e7ea81dSJan Kara /* 2509fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 251020970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2511fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2512fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2513fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2514525f4ed8SMingming Cao */ 2515fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2516fffb2739SJan Kara { 2517fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2518525f4ed8SMingming Cao 2519fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2520fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2521525f4ed8SMingming Cao } 252261628a3fSMingming Cao 25238e48dcfbSTheodore Ts'o /* 25244e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25254e7ea81dSJan Kara * and underlying extent to map 25264e7ea81dSJan Kara * 25274e7ea81dSJan Kara * @mpd - where to look for pages 25284e7ea81dSJan Kara * 25294e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25304e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25314e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25324e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25334e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25344e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25354e7ea81dSJan Kara * 25364e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25374e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25384e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25394e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25408e48dcfbSTheodore Ts'o */ 25414e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25428e48dcfbSTheodore Ts'o { 25434e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25448e48dcfbSTheodore Ts'o struct pagevec pvec; 25454f01b02cSTheodore Ts'o unsigned int nr_pages; 2546aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25474e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25484e7ea81dSJan Kara pgoff_t end = mpd->last_page; 254910bbd235SMatthew Wilcox xa_mark_t tag; 25504e7ea81dSJan Kara int i, err = 0; 25514e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25524e7ea81dSJan Kara ext4_lblk_t lblk; 25534e7ea81dSJan Kara struct buffer_head *head; 25548e48dcfbSTheodore Ts'o 25554e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25565b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25575b41d924SEric Sandeen else 25585b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25595b41d924SEric Sandeen 256086679820SMel Gorman pagevec_init(&pvec); 25614e7ea81dSJan Kara mpd->map.m_len = 0; 25624e7ea81dSJan Kara mpd->next_page = index; 25634f01b02cSTheodore Ts'o while (index <= end) { 2564dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 256567fd707fSJan Kara tag); 25668e48dcfbSTheodore Ts'o if (nr_pages == 0) 25676b8ed620SJan Kara break; 25688e48dcfbSTheodore Ts'o 25698e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25708e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25718e48dcfbSTheodore Ts'o 25728e48dcfbSTheodore Ts'o /* 2573aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2574aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2575aeac589aSMing Lei * keep going because someone may be concurrently 2576aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2577aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2578aeac589aSMing Lei * of the old dirty pages. 2579aeac589aSMing Lei */ 2580aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2581aeac589aSMing Lei goto out; 2582aeac589aSMing Lei 25834e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25844e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25854e7ea81dSJan Kara goto out; 258678aaced3STheodore Ts'o 25878e48dcfbSTheodore Ts'o lock_page(page); 25888e48dcfbSTheodore Ts'o /* 25894e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25904e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25914e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25924e7ea81dSJan Kara * page is already under writeback and we are not doing 25934e7ea81dSJan Kara * a data integrity writeback, skip the page 25948e48dcfbSTheodore Ts'o */ 25954f01b02cSTheodore Ts'o if (!PageDirty(page) || 25964f01b02cSTheodore Ts'o (PageWriteback(page) && 25974e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 25984f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 25998e48dcfbSTheodore Ts'o unlock_page(page); 26008e48dcfbSTheodore Ts'o continue; 26018e48dcfbSTheodore Ts'o } 26028e48dcfbSTheodore Ts'o 26038e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 26048e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 26058e48dcfbSTheodore Ts'o 2606cc509574STheodore Ts'o /* 2607cc509574STheodore Ts'o * Should never happen but for buggy code in 2608cc509574STheodore Ts'o * other subsystems that call 2609cc509574STheodore Ts'o * set_page_dirty() without properly warning 2610cc509574STheodore Ts'o * the file system first. See [1] for more 2611cc509574STheodore Ts'o * information. 2612cc509574STheodore Ts'o * 2613cc509574STheodore Ts'o * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz 2614cc509574STheodore Ts'o */ 2615cc509574STheodore Ts'o if (!page_has_buffers(page)) { 2616cc509574STheodore Ts'o ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", page->index); 2617cc509574STheodore Ts'o ClearPageDirty(page); 2618cc509574STheodore Ts'o unlock_page(page); 2619cc509574STheodore Ts'o continue; 2620cc509574STheodore Ts'o } 2621cc509574STheodore Ts'o 26224e7ea81dSJan Kara if (mpd->map.m_len == 0) 26238eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 26248eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2625f8bec370SJan Kara /* Add all dirty buffers to mpd */ 26264e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 262709cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 26288eb9e5ceSTheodore Ts'o head = page_buffers(page); 26295f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26305f1132b2SJan Kara if (err <= 0) 26314e7ea81dSJan Kara goto out; 26325f1132b2SJan Kara err = 0; 2633aeac589aSMing Lei left--; 26348e48dcfbSTheodore Ts'o } 26358e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26368e48dcfbSTheodore Ts'o cond_resched(); 26378e48dcfbSTheodore Ts'o } 26386b8ed620SJan Kara mpd->scanned_until_end = 1; 26394f01b02cSTheodore Ts'o return 0; 26408eb9e5ceSTheodore Ts'o out: 26418eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26424e7ea81dSJan Kara return err; 26438e48dcfbSTheodore Ts'o } 26448e48dcfbSTheodore Ts'o 264520970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 264664769240SAlex Tomas struct writeback_control *wbc) 264764769240SAlex Tomas { 26484e7ea81dSJan Kara pgoff_t writeback_index = 0; 26494e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 265022208dedSAneesh Kumar K.V int range_whole = 0; 26514e7ea81dSJan Kara int cycled = 1; 265261628a3fSMingming Cao handle_t *handle = NULL; 2653df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26545e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26556b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26565e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26571bce63d1SShaohua Li struct blk_plug plug; 2658cb530541STheodore Ts'o bool give_up_on_write = false; 265961628a3fSMingming Cao 26600db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26610db1ff22STheodore Ts'o return -EIO; 26620db1ff22STheodore Ts'o 2663bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 266420970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2665ba80b101STheodore Ts'o 266661628a3fSMingming Cao /* 266761628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 266861628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 266961628a3fSMingming Cao * because that could violate lock ordering on umount 267061628a3fSMingming Cao */ 2671a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2672bbf023c7SMing Lei goto out_writepages; 26732a21e37eSTheodore Ts'o 267420970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2675043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2676bbf023c7SMing Lei goto out_writepages; 267720970ba6STheodore Ts'o } 267820970ba6STheodore Ts'o 26792a21e37eSTheodore Ts'o /* 26802a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26812a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26822a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26831751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26842a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 268520970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26862a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26872a21e37eSTheodore Ts'o * the stack trace. 26882a21e37eSTheodore Ts'o */ 26890db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26909b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2691bbf023c7SMing Lei ret = -EROFS; 2692bbf023c7SMing Lei goto out_writepages; 2693bbf023c7SMing Lei } 26942a21e37eSTheodore Ts'o 26954e7ea81dSJan Kara /* 26964e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26974e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26984e7ea81dSJan Kara * we'd better clear the inline data here. 26994e7ea81dSJan Kara */ 27004e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 27014e7ea81dSJan Kara /* Just inode will be modified... */ 27024e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 27034e7ea81dSJan Kara if (IS_ERR(handle)) { 27044e7ea81dSJan Kara ret = PTR_ERR(handle); 27054e7ea81dSJan Kara goto out_writepages; 27064e7ea81dSJan Kara } 27074e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 27084e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 27094e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 27104e7ea81dSJan Kara ext4_journal_stop(handle); 27114e7ea81dSJan Kara } 27124e7ea81dSJan Kara 27134e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 27144e343231Syangerkun /* 27154e343231Syangerkun * We may need to convert up to one extent per block in 27164e343231Syangerkun * the page and we may dirty the inode. 27174e343231Syangerkun */ 27184e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 27194e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 27204e343231Syangerkun } 27214e343231Syangerkun 272222208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 272322208dedSAneesh Kumar K.V range_whole = 1; 272461628a3fSMingming Cao 27252acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 27264e7ea81dSJan Kara writeback_index = mapping->writeback_index; 27274e7ea81dSJan Kara if (writeback_index) 27282acf2c26SAneesh Kumar K.V cycled = 0; 27294e7ea81dSJan Kara mpd.first_page = writeback_index; 27304e7ea81dSJan Kara mpd.last_page = -1; 27315b41d924SEric Sandeen } else { 273209cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 273309cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27345b41d924SEric Sandeen } 2735a1d6cc56SAneesh Kumar K.V 27364e7ea81dSJan Kara mpd.inode = inode; 27374e7ea81dSJan Kara mpd.wbc = wbc; 27384e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27392acf2c26SAneesh Kumar K.V retry: 27406e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27414e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27421bce63d1SShaohua Li blk_start_plug(&plug); 2743dddbd6acSJan Kara 2744dddbd6acSJan Kara /* 2745dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2746dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2747dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2748dddbd6acSJan Kara * started. 2749dddbd6acSJan Kara */ 2750dddbd6acSJan Kara mpd.do_map = 0; 27516b8ed620SJan Kara mpd.scanned_until_end = 0; 2752dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2753dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2754dddbd6acSJan Kara ret = -ENOMEM; 2755dddbd6acSJan Kara goto unplug; 2756dddbd6acSJan Kara } 2757dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2758a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2759a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2760dddbd6acSJan Kara /* Submit prepared bio */ 2761dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2762dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2763dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2764dddbd6acSJan Kara if (ret < 0) 2765dddbd6acSJan Kara goto unplug; 2766dddbd6acSJan Kara 27676b8ed620SJan Kara while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { 27684e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27694e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27704e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27714e7ea81dSJan Kara ret = -ENOMEM; 27724e7ea81dSJan Kara break; 27734e7ea81dSJan Kara } 2774a1d6cc56SAneesh Kumar K.V 2775a1d6cc56SAneesh Kumar K.V /* 27764e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27774e7ea81dSJan Kara * must always write out whole page (makes a difference when 27784e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27794e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27804e7ea81dSJan Kara * not supported by delalloc. 2781a1d6cc56SAneesh Kumar K.V */ 2782a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2783525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2784a1d6cc56SAneesh Kumar K.V 278561628a3fSMingming Cao /* start a new transaction */ 27866b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27876b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 278861628a3fSMingming Cao if (IS_ERR(handle)) { 278961628a3fSMingming Cao ret = PTR_ERR(handle); 27901693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2791fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2792a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27934e7ea81dSJan Kara /* Release allocated io_end */ 27944e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2795dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27964e7ea81dSJan Kara break; 279761628a3fSMingming Cao } 2798dddbd6acSJan Kara mpd.do_map = 1; 2799f63e6005STheodore Ts'o 28004e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 28014e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 28026b8ed620SJan Kara if (!ret && mpd.map.m_len) 2803cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2804cb530541STheodore Ts'o &give_up_on_write); 2805646caa9cSJan Kara /* 2806646caa9cSJan Kara * Caution: If the handle is synchronous, 2807646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2808646caa9cSJan Kara * to finish which may depend on writeback of pages to 2809646caa9cSJan Kara * complete or on page lock to be released. In that 2810b483bb77SRandy Dunlap * case, we have to wait until after we have 2811646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2812646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2813646caa9cSJan Kara * to be able to complete) before stopping the handle. 2814646caa9cSJan Kara */ 2815646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 281661628a3fSMingming Cao ext4_journal_stop(handle); 2817646caa9cSJan Kara handle = NULL; 2818dddbd6acSJan Kara mpd.do_map = 0; 2819646caa9cSJan Kara } 28204e7ea81dSJan Kara /* Unlock pages we didn't use */ 2821cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2822a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2823a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2824a297b2fcSXiaoguang Wang 2825646caa9cSJan Kara /* 2826646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2827646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2828646caa9cSJan Kara * we are still holding the transaction as we can 2829646caa9cSJan Kara * release the last reference to io_end which may end 2830646caa9cSJan Kara * up doing unwritten extent conversion. 2831646caa9cSJan Kara */ 2832646caa9cSJan Kara if (handle) { 2833646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2834646caa9cSJan Kara ext4_journal_stop(handle); 2835646caa9cSJan Kara } else 28364e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2837dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2838df22291fSAneesh Kumar K.V 28394e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28404e7ea81dSJan Kara /* 28414e7ea81dSJan Kara * Commit the transaction which would 284222208dedSAneesh Kumar K.V * free blocks released in the transaction 284322208dedSAneesh Kumar K.V * and try again 284422208dedSAneesh Kumar K.V */ 2845df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 284622208dedSAneesh Kumar K.V ret = 0; 28474e7ea81dSJan Kara continue; 28484e7ea81dSJan Kara } 28494e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28504e7ea81dSJan Kara if (ret) 285161628a3fSMingming Cao break; 285261628a3fSMingming Cao } 2853dddbd6acSJan Kara unplug: 28541bce63d1SShaohua Li blk_finish_plug(&plug); 28559c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28562acf2c26SAneesh Kumar K.V cycled = 1; 28574e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28584e7ea81dSJan Kara mpd.first_page = 0; 28592acf2c26SAneesh Kumar K.V goto retry; 28602acf2c26SAneesh Kumar K.V } 286161628a3fSMingming Cao 286222208dedSAneesh Kumar K.V /* Update index */ 286322208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 286422208dedSAneesh Kumar K.V /* 28654e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 286622208dedSAneesh Kumar K.V * mode will write it back later 286722208dedSAneesh Kumar K.V */ 28684e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2869a1d6cc56SAneesh Kumar K.V 287061628a3fSMingming Cao out_writepages: 287120970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28724e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2873bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 287461628a3fSMingming Cao return ret; 287564769240SAlex Tomas } 287664769240SAlex Tomas 28775f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28785f0663bbSDan Williams struct writeback_control *wbc) 28795f0663bbSDan Williams { 28805f0663bbSDan Williams int ret; 28815f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28825f0663bbSDan Williams struct inode *inode = mapping->host; 28835f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28845f0663bbSDan Williams 28855f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28865f0663bbSDan Williams return -EIO; 28875f0663bbSDan Williams 2888bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28895f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28905f0663bbSDan Williams 28913f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28925f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28935f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2894bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28955f0663bbSDan Williams return ret; 28965f0663bbSDan Williams } 28975f0663bbSDan Williams 289879f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 289979f0be8dSAneesh Kumar K.V { 29005c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 290179f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 290279f0be8dSAneesh Kumar K.V 290379f0be8dSAneesh Kumar K.V /* 290479f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 290579f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2906179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 290779f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 290879f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 290979f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 291079f0be8dSAneesh Kumar K.V */ 29115c1ff336SEric Whitney free_clusters = 29125c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 29135c1ff336SEric Whitney dirty_clusters = 29145c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 291500d4e736STheodore Ts'o /* 291600d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 291700d4e736STheodore Ts'o */ 29185c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 291910ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 292000d4e736STheodore Ts'o 29215c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29225c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 292379f0be8dSAneesh Kumar K.V /* 2924c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2925c8afb446SEric Sandeen * or free blocks is less than watermark 292679f0be8dSAneesh Kumar K.V */ 292779f0be8dSAneesh Kumar K.V return 1; 292879f0be8dSAneesh Kumar K.V } 292979f0be8dSAneesh Kumar K.V return 0; 293079f0be8dSAneesh Kumar K.V } 293179f0be8dSAneesh Kumar K.V 293264769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 293364769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 293464769240SAlex Tomas struct page **pagep, void **fsdata) 293564769240SAlex Tomas { 293672b8ab9dSEric Sandeen int ret, retries = 0; 293764769240SAlex Tomas struct page *page; 293864769240SAlex Tomas pgoff_t index; 293964769240SAlex Tomas struct inode *inode = mapping->host; 294064769240SAlex Tomas 29410db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29420db1ff22STheodore Ts'o return -EIO; 29430db1ff22STheodore Ts'o 294409cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 294579f0be8dSAneesh Kumar K.V 2946c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2947c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 294879f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 294979f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 295079f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 295179f0be8dSAneesh Kumar K.V } 295279f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29539bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29549c3569b5STao Ma 29559c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29569c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29579c3569b5STao Ma pos, len, flags, 29589c3569b5STao Ma pagep, fsdata); 29599c3569b5STao Ma if (ret < 0) 296047564bfbSTheodore Ts'o return ret; 296147564bfbSTheodore Ts'o if (ret == 1) 296247564bfbSTheodore Ts'o return 0; 29639c3569b5STao Ma } 29649c3569b5STao Ma 2965cc883236SZhang Yi retry: 296647564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 296747564bfbSTheodore Ts'o if (!page) 296847564bfbSTheodore Ts'o return -ENOMEM; 296947564bfbSTheodore Ts'o 297047564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29717afe5aa5SDmitry Monakhov wait_for_stable_page(page); 297264769240SAlex Tomas 2973643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29742058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 29752058f83aSMichael Halcrow ext4_da_get_block_prep); 29762058f83aSMichael Halcrow #else 29776e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 29782058f83aSMichael Halcrow #endif 297964769240SAlex Tomas if (ret < 0) { 298064769240SAlex Tomas unlock_page(page); 2981cc883236SZhang Yi put_page(page); 2982ae4d5372SAneesh Kumar K.V /* 2983ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2984ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2985cc883236SZhang Yi * i_size_read because we hold inode lock. 2986ae4d5372SAneesh Kumar K.V */ 2987ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2988b9a4207dSJan Kara ext4_truncate_failed_write(inode); 298947564bfbSTheodore Ts'o 299047564bfbSTheodore Ts'o if (ret == -ENOSPC && 299147564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 2992cc883236SZhang Yi goto retry; 299347564bfbSTheodore Ts'o return ret; 299464769240SAlex Tomas } 299564769240SAlex Tomas 299647564bfbSTheodore Ts'o *pagep = page; 299764769240SAlex Tomas return ret; 299864769240SAlex Tomas } 299964769240SAlex Tomas 3000632eaeabSMingming Cao /* 3001632eaeabSMingming Cao * Check if we should update i_disksize 3002632eaeabSMingming Cao * when write to the end of file but not require block allocation 3003632eaeabSMingming Cao */ 3004632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3005632eaeabSMingming Cao unsigned long offset) 3006632eaeabSMingming Cao { 3007632eaeabSMingming Cao struct buffer_head *bh; 3008632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3009632eaeabSMingming Cao unsigned int idx; 3010632eaeabSMingming Cao int i; 3011632eaeabSMingming Cao 3012632eaeabSMingming Cao bh = page_buffers(page); 3013632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3014632eaeabSMingming Cao 3015632eaeabSMingming Cao for (i = 0; i < idx; i++) 3016632eaeabSMingming Cao bh = bh->b_this_page; 3017632eaeabSMingming Cao 301829fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3019632eaeabSMingming Cao return 0; 3020632eaeabSMingming Cao return 1; 3021632eaeabSMingming Cao } 3022632eaeabSMingming Cao 302364769240SAlex Tomas static int ext4_da_write_end(struct file *file, 302464769240SAlex Tomas struct address_space *mapping, 302564769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 302664769240SAlex Tomas struct page *page, void *fsdata) 302764769240SAlex Tomas { 302864769240SAlex Tomas struct inode *inode = mapping->host; 302964769240SAlex Tomas loff_t new_i_size; 3030632eaeabSMingming Cao unsigned long start, end; 303179f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 303279f0be8dSAneesh Kumar K.V 303374d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 303474d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 303579f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3036632eaeabSMingming Cao 30379bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 30389c3569b5STao Ma 30399c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30409c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30419c3569b5STao Ma ext4_has_inline_data(inode)) 30426984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 30439c3569b5STao Ma 304464769240SAlex Tomas start = pos & (PAGE_SIZE - 1); 304564769240SAlex Tomas end = start + copied - 1; 304664769240SAlex Tomas 304764769240SAlex Tomas /* 30484df031ffSZhang Yi * Since we are holding inode lock, we are sure i_disksize <= 30494df031ffSZhang Yi * i_size. We also know that if i_disksize < i_size, there are 30504df031ffSZhang Yi * delalloc writes pending in the range upto i_size. If the end of 30514df031ffSZhang Yi * the current write is <= i_size, there's no need to touch 30524df031ffSZhang Yi * i_disksize since writeback will push i_disksize upto i_size 30534df031ffSZhang Yi * eventually. If the end of the current write is > i_size and 30544df031ffSZhang Yi * inside an allocated block (ext4_da_should_update_i_disksize() 30554df031ffSZhang Yi * check), we need to update i_disksize here as neither 30564df031ffSZhang Yi * ext4_writepage() nor certain ext4_writepages() paths not 30574df031ffSZhang Yi * allocating blocks update i_disksize. 30584df031ffSZhang Yi * 30594df031ffSZhang Yi * Note that we defer inode dirtying to generic_write_end() / 30604df031ffSZhang Yi * ext4_da_write_inline_data_end(). 3061d2a17637SMingming Cao */ 306264769240SAlex Tomas new_i_size = pos + copied; 30636984aef5SZhang Yi if (copied && new_i_size > inode->i_size && 30644df031ffSZhang Yi ext4_da_should_update_i_disksize(page, end)) 306564769240SAlex Tomas ext4_update_i_disksize(inode, new_i_size); 3066ccd2506bSTheodore Ts'o 3067cc883236SZhang Yi return generic_write_end(file, mapping, pos, len, copied, page, fsdata); 3068ac27a0ecSDave Kleikamp } 3069ac27a0ecSDave Kleikamp 3070ccd2506bSTheodore Ts'o /* 3071ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3072ccd2506bSTheodore Ts'o */ 3073ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3074ccd2506bSTheodore Ts'o { 3075ccd2506bSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3076ccd2506bSTheodore Ts'o 307771d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3078ccd2506bSTheodore Ts'o return 0; 3079ccd2506bSTheodore Ts'o 3080ccd2506bSTheodore Ts'o /* 3081ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3082ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3083ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3084ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3085ccd2506bSTheodore Ts'o * would require replicating code paths in: 3086ccd2506bSTheodore Ts'o * 308720970ba6STheodore Ts'o * ext4_writepages() -> 3088ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3089ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3090ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3091ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3092ccd2506bSTheodore Ts'o * 3093ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3094ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3095ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3096ccd2506bSTheodore Ts'o * doing I/O at all. 3097ccd2506bSTheodore Ts'o * 3098ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3099380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3100ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3101ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 310225985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3103ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3104ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3105ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3106ccd2506bSTheodore Ts'o * 3107ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3108ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3109ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3110ccd2506bSTheodore Ts'o */ 3111ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3112ccd2506bSTheodore Ts'o } 3113ac27a0ecSDave Kleikamp 3114ac27a0ecSDave Kleikamp /* 3115ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3116ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3117ac27a0ecSDave Kleikamp * 3118ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3119ac27a0ecSDave Kleikamp * journal. If somebody makes a swapfile on an ext4 data-journaling 3120ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3121ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3122ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3123ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3124ac27a0ecSDave Kleikamp * 3125ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3126ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3127ac27a0ecSDave Kleikamp */ 3128ac27a0ecSDave Kleikamp static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3129ac27a0ecSDave Kleikamp { 3130ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3131ac27a0ecSDave Kleikamp journal_t *journal; 3132ac27a0ecSDave Kleikamp int err; 3133ac27a0ecSDave Kleikamp 313446c7f254STao Ma /* 313546c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 313646c7f254STao Ma */ 313746c7f254STao Ma if (ext4_has_inline_data(inode)) 313846c7f254STao Ma return 0; 313946c7f254STao Ma 3140ac27a0ecSDave Kleikamp if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 3141ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DELALLOC)) { 3142ac27a0ecSDave Kleikamp /* 3143ac27a0ecSDave Kleikamp * With delalloc we want to sync the file 3144ac27a0ecSDave Kleikamp * so that we can make sure we allocate 3145ac27a0ecSDave Kleikamp * blocks for file 3146ac27a0ecSDave Kleikamp */ 3147ac27a0ecSDave Kleikamp filemap_write_and_wait(mapping); 3148ac27a0ecSDave Kleikamp } 3149ac27a0ecSDave Kleikamp 315019f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 315119f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3152ac27a0ecSDave Kleikamp /* 3153ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3154ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3155ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3156ac27a0ecSDave Kleikamp * do we expect this to happen. 3157ac27a0ecSDave Kleikamp * 3158ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3159ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3160ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3161ac27a0ecSDave Kleikamp * will.) 3162ac27a0ecSDave Kleikamp * 3163ac27a0ecSDave Kleikamp * NB. EXT4_STATE_JDATA is not set on files other than 3164ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3165ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3166ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3167ac27a0ecSDave Kleikamp * everything they get. 3168ac27a0ecSDave Kleikamp */ 3169ac27a0ecSDave Kleikamp 317019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3171ac27a0ecSDave Kleikamp journal = EXT4_JOURNAL(inode); 3172ac27a0ecSDave Kleikamp jbd2_journal_lock_updates(journal); 317301d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 3174ac27a0ecSDave Kleikamp jbd2_journal_unlock_updates(journal); 3175ac27a0ecSDave Kleikamp 3176ac27a0ecSDave Kleikamp if (err) 3177ac27a0ecSDave Kleikamp return 0; 3178ac27a0ecSDave Kleikamp } 3179ac27a0ecSDave Kleikamp 3180ac58e4fbSRitesh Harjani return iomap_bmap(mapping, block, &ext4_iomap_ops); 3181ac27a0ecSDave Kleikamp } 3182ac27a0ecSDave Kleikamp 3183617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3184ac27a0ecSDave Kleikamp { 318546c7f254STao Ma int ret = -EAGAIN; 318646c7f254STao Ma struct inode *inode = page->mapping->host; 318746c7f254STao Ma 31880562e0baSJiaying Zhang trace_ext4_readpage(page); 318946c7f254STao Ma 319046c7f254STao Ma if (ext4_has_inline_data(inode)) 319146c7f254STao Ma ret = ext4_readpage_inline(inode, page); 319246c7f254STao Ma 319346c7f254STao Ma if (ret == -EAGAIN) 3194a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 319546c7f254STao Ma 319646c7f254STao Ma return ret; 3197ac27a0ecSDave Kleikamp } 3198ac27a0ecSDave Kleikamp 31996311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3200ac27a0ecSDave Kleikamp { 32016311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 320246c7f254STao Ma 32036311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 320446c7f254STao Ma if (ext4_has_inline_data(inode)) 32056311f91fSMatthew Wilcox (Oracle) return; 320646c7f254STao Ma 3207a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3208ac27a0ecSDave Kleikamp } 3209ac27a0ecSDave Kleikamp 3210d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3211d47992f8SLukas Czerner unsigned int length) 3212ac27a0ecSDave Kleikamp { 3213ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32140562e0baSJiaying Zhang 32154520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32164520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32174520fb3cSJan Kara 3218ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32194520fb3cSJan Kara } 32204520fb3cSJan Kara 322153e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3222ca99fdd2SLukas Czerner unsigned int offset, 3223ca99fdd2SLukas Czerner unsigned int length) 32244520fb3cSJan Kara { 32254520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32264520fb3cSJan Kara 3227ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32284520fb3cSJan Kara 3229744692dcSJiaying Zhang /* 3230ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3231ac27a0ecSDave Kleikamp */ 323209cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3233ac27a0ecSDave Kleikamp ClearPageChecked(page); 3234ac27a0ecSDave Kleikamp 3235ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 323653e87268SJan Kara } 323753e87268SJan Kara 323853e87268SJan Kara /* Wrapper for aops... */ 323953e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3240d47992f8SLukas Czerner unsigned int offset, 3241d47992f8SLukas Czerner unsigned int length) 324253e87268SJan Kara { 3243ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3244ac27a0ecSDave Kleikamp } 3245ac27a0ecSDave Kleikamp 3246617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3247ac27a0ecSDave Kleikamp { 3248617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3249ac27a0ecSDave Kleikamp 32500562e0baSJiaying Zhang trace_ext4_releasepage(page); 32510562e0baSJiaying Zhang 3252e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3253e1c36595SJan Kara if (PageChecked(page)) 3254ac27a0ecSDave Kleikamp return 0; 32550390131bSFrank Mayhar if (journal) 3256529a781eSzhangyi (F) return jbd2_journal_try_to_free_buffers(journal, page); 32570390131bSFrank Mayhar else 32580390131bSFrank Mayhar return try_to_free_buffers(page); 3259ac27a0ecSDave Kleikamp } 3260ac27a0ecSDave Kleikamp 3261b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3262b8a6176cSJan Kara { 3263b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3264b8a6176cSJan Kara 3265aa75f4d3SHarshad Shirwadkar if (journal) { 3266aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3267aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3268d0520df7SAndrea Righi return false; 3269d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 32701ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3271d0520df7SAndrea Righi return true; 3272aa75f4d3SHarshad Shirwadkar } 3273aa75f4d3SHarshad Shirwadkar 3274b8a6176cSJan Kara /* Any metadata buffers to write? */ 3275b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3276b8a6176cSJan Kara return true; 3277b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3278b8a6176cSJan Kara } 3279b8a6176cSJan Kara 3280c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3281c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3282de205114SChristoph Hellwig loff_t length, unsigned int flags) 3283364443cbSJan Kara { 3284c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3285c8fdfe29SMatthew Bobrowski 3286c8fdfe29SMatthew Bobrowski /* 3287c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3288c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3289c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3290c8fdfe29SMatthew Bobrowski */ 3291c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3292c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3293c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3294c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3295c8fdfe29SMatthew Bobrowski 3296c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3297c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3298c8fdfe29SMatthew Bobrowski 3299de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3300c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3301de205114SChristoph Hellwig else 3302de205114SChristoph Hellwig iomap->bdev = inode->i_sb->s_bdev; 3303c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3304c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3305c8fdfe29SMatthew Bobrowski 33066386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 33076386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 33086386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 33096386722aSRitesh Harjani 3310c8fdfe29SMatthew Bobrowski /* 3311c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3312c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3313c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3314c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3315c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3316c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3317c8fdfe29SMatthew Bobrowski * been set first. 3318c8fdfe29SMatthew Bobrowski */ 3319c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3320c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3321c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3322de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3323de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3324c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3325c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3326c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3327de205114SChristoph Hellwig if (flags & IOMAP_DAX) 3328de205114SChristoph Hellwig iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off; 3329c8fdfe29SMatthew Bobrowski } else { 3330c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3331c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3332c8fdfe29SMatthew Bobrowski } 3333c8fdfe29SMatthew Bobrowski } 3334c8fdfe29SMatthew Bobrowski 3335f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3336f063db5eSMatthew Bobrowski unsigned int flags) 3337f063db5eSMatthew Bobrowski { 3338f063db5eSMatthew Bobrowski handle_t *handle; 3339378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3340378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3341f063db5eSMatthew Bobrowski 3342f063db5eSMatthew Bobrowski /* 3343f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3344f063db5eSMatthew Bobrowski * once for direct I/O. 3345f063db5eSMatthew Bobrowski */ 3346f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3347f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3348f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3349f063db5eSMatthew Bobrowski 3350f063db5eSMatthew Bobrowski retry: 3351f063db5eSMatthew Bobrowski /* 3352f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3353f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3354f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3355f063db5eSMatthew Bobrowski * fits into the credits as well. 3356f063db5eSMatthew Bobrowski */ 3357f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3358f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3359f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3360f063db5eSMatthew Bobrowski 3361378f32baSMatthew Bobrowski /* 3362378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3363378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3364378f32baSMatthew Bobrowski */ 3365952da063SChristoph Hellwig WARN_ON(!(flags & (IOMAP_DAX | IOMAP_DIRECT))); 3366952da063SChristoph Hellwig if (flags & IOMAP_DAX) 3367378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3368378f32baSMatthew Bobrowski /* 3369378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3370378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3371378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3372378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3373378f32baSMatthew Bobrowski */ 3374d0b040f5SJan Kara else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3375378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3376378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3377378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3378378f32baSMatthew Bobrowski 3379378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3380378f32baSMatthew Bobrowski 3381378f32baSMatthew Bobrowski /* 3382378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3383378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3384378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3385378f32baSMatthew Bobrowski */ 3386378f32baSMatthew Bobrowski if (!m_flags && !ret) 3387378f32baSMatthew Bobrowski ret = -ENOTBLK; 3388f063db5eSMatthew Bobrowski 3389f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3390f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3391f063db5eSMatthew Bobrowski goto retry; 3392f063db5eSMatthew Bobrowski 3393f063db5eSMatthew Bobrowski return ret; 3394f063db5eSMatthew Bobrowski } 3395f063db5eSMatthew Bobrowski 3396f063db5eSMatthew Bobrowski 3397364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3398c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3399364443cbSJan Kara { 3400364443cbSJan Kara int ret; 340109edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 340209edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3403364443cbSJan Kara 3404bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3405bcd8e91fSTheodore Ts'o return -EINVAL; 34067046ae35SAndreas Gruenbacher 3407364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3408364443cbSJan Kara return -ERANGE; 3409364443cbSJan Kara 341009edf4d3SMatthew Bobrowski /* 341109edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 341209edf4d3SMatthew Bobrowski */ 341309edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 341409edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 341509edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3416364443cbSJan Kara 34179faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 34189faac62dSRitesh Harjani /* 34199faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 34209faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 34219faac62dSRitesh Harjani * the mapping information. This could boost performance 34229faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 34239faac62dSRitesh Harjani */ 34249faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3425545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 34269faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 34279faac62dSRitesh Harjani goto out; 34289faac62dSRitesh Harjani } 34299faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 34309faac62dSRitesh Harjani } else { 34319faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 34329faac62dSRitesh Harjani } 3433f063db5eSMatthew Bobrowski 3434545052e9SChristoph Hellwig if (ret < 0) 3435545052e9SChristoph Hellwig return ret; 34369faac62dSRitesh Harjani out: 3437de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 3438545052e9SChristoph Hellwig 3439364443cbSJan Kara return 0; 3440364443cbSJan Kara } 3441364443cbSJan Kara 34428cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34438cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34448cd115bdSJan Kara struct iomap *srcmap) 34458cd115bdSJan Kara { 34468cd115bdSJan Kara int ret; 34478cd115bdSJan Kara 34488cd115bdSJan Kara /* 34498cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34508cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34518cd115bdSJan Kara */ 34528cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34538cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34548cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34558cd115bdSJan Kara return ret; 34568cd115bdSJan Kara } 34578cd115bdSJan Kara 3458776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3459776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3460776722e8SJan Kara { 3461378f32baSMatthew Bobrowski /* 3462378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3463378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3464378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3465378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3466378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3467378f32baSMatthew Bobrowski */ 3468378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3469378f32baSMatthew Bobrowski return -ENOTBLK; 3470378f32baSMatthew Bobrowski 3471776722e8SJan Kara return 0; 3472776722e8SJan Kara } 3473776722e8SJan Kara 34748ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3475364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3476776722e8SJan Kara .iomap_end = ext4_iomap_end, 3477364443cbSJan Kara }; 3478364443cbSJan Kara 34798cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 34808cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 34818cd115bdSJan Kara .iomap_end = ext4_iomap_end, 34828cd115bdSJan Kara }; 34838cd115bdSJan Kara 348409edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 348509edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 348609edf4d3SMatthew Bobrowski { 348709edf4d3SMatthew Bobrowski struct extent_status es; 348809edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 348909edf4d3SMatthew Bobrowski 349009edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 349109edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 349209edf4d3SMatthew Bobrowski 349309edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 349409edf4d3SMatthew Bobrowski return false; 349509edf4d3SMatthew Bobrowski 349609edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 349709edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 349809edf4d3SMatthew Bobrowski return false; 349909edf4d3SMatthew Bobrowski } 350009edf4d3SMatthew Bobrowski 350109edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 350209edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 350309edf4d3SMatthew Bobrowski 350409edf4d3SMatthew Bobrowski return true; 350509edf4d3SMatthew Bobrowski } 350609edf4d3SMatthew Bobrowski 350709edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 350809edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 350909edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 351009edf4d3SMatthew Bobrowski { 351109edf4d3SMatthew Bobrowski int ret; 351209edf4d3SMatthew Bobrowski bool delalloc = false; 351309edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 351409edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 351509edf4d3SMatthew Bobrowski 351609edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 351709edf4d3SMatthew Bobrowski return -EINVAL; 351809edf4d3SMatthew Bobrowski 35197cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35207cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3521ba5843f5SJan Kara if (ret != -EAGAIN) { 3522ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3523ba5843f5SJan Kara ret = -ENOENT; 3524ba5843f5SJan Kara return ret; 3525ba5843f5SJan Kara } 3526ba5843f5SJan Kara } 352712735f88SJan Kara 352809edf4d3SMatthew Bobrowski /* 352909edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 353009edf4d3SMatthew Bobrowski */ 353109edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 353209edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 353309edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 353412735f88SJan Kara 3535b2c57642SRitesh Harjani /* 3536b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3537b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3538b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3539b2c57642SRitesh Harjani * -EIO error. 3540b2c57642SRitesh Harjani */ 3541b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3542b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3543b2c57642SRitesh Harjani 3544b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3545b2c57642SRitesh Harjani map.m_flags = 0; 3546b2c57642SRitesh Harjani goto set_iomap; 3547b2c57642SRitesh Harjani } 3548b2c57642SRitesh Harjani } 3549b2c57642SRitesh Harjani 355012735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3551ba5843f5SJan Kara if (ret < 0) 3552ba5843f5SJan Kara return ret; 3553914f82a3SJan Kara if (ret == 0) 355409edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 355509edf4d3SMatthew Bobrowski 3556b2c57642SRitesh Harjani set_iomap: 3557de205114SChristoph Hellwig ext4_set_iomap(inode, iomap, &map, offset, length, flags); 355809edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 355909edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 356009edf4d3SMatthew Bobrowski 356109edf4d3SMatthew Bobrowski return 0; 3562914f82a3SJan Kara } 3563914f82a3SJan Kara 356409edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 356509edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 356609edf4d3SMatthew Bobrowski }; 35674c0425ffSMingming Cao 3568ac27a0ecSDave Kleikamp /* 3569*2bb8dd40SJan Kara * Whenever the page is being dirtied, corresponding buffers should already be 3570*2bb8dd40SJan Kara * attached to the transaction (we take care of this in ext4_page_mkwrite() and 3571*2bb8dd40SJan Kara * ext4_write_begin()). However we cannot move buffers to dirty transaction 3572*2bb8dd40SJan Kara * lists here because ->set_page_dirty is called under VFS locks and the page 3573*2bb8dd40SJan Kara * is not necessarily locked. 3574ac27a0ecSDave Kleikamp * 3575ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3576ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3577ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3578ac27a0ecSDave Kleikamp * 3579ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3580ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3581ac27a0ecSDave Kleikamp */ 3582617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3583ac27a0ecSDave Kleikamp { 3584*2bb8dd40SJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 3585ac27a0ecSDave Kleikamp SetPageChecked(page); 3586ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3587ac27a0ecSDave Kleikamp } 3588ac27a0ecSDave Kleikamp 35896dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 35906dcc693bSJan Kara { 35916dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 35926dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 35936dcc693bSJan Kara return __set_page_dirty_buffers(page); 35946dcc693bSJan Kara } 35956dcc693bSJan Kara 35960e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 35970e6895baSRitesh Harjani struct file *file, sector_t *span) 35980e6895baSRitesh Harjani { 35990e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 36000e6895baSRitesh Harjani &ext4_iomap_report_ops); 36010e6895baSRitesh Harjani } 36020e6895baSRitesh Harjani 360374d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3604617ba13bSMingming Cao .readpage = ext4_readpage, 36056311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 360643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 360720970ba6STheodore Ts'o .writepages = ext4_writepages, 3608bfc1af65SNick Piggin .write_begin = ext4_write_begin, 360974d553aaSTheodore Ts'o .write_end = ext4_write_end, 36106dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3611617ba13bSMingming Cao .bmap = ext4_bmap, 3612617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3613617ba13bSMingming Cao .releasepage = ext4_releasepage, 3614378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3615ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 36168ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3617aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36180e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3619ac27a0ecSDave Kleikamp }; 3620ac27a0ecSDave Kleikamp 3621617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3622617ba13bSMingming Cao .readpage = ext4_readpage, 36236311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 362443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 362520970ba6STheodore Ts'o .writepages = ext4_writepages, 3626bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3627bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3628617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3629617ba13bSMingming Cao .bmap = ext4_bmap, 36304520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3631617ba13bSMingming Cao .releasepage = ext4_releasepage, 3632378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 36338ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3634aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36350e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3636ac27a0ecSDave Kleikamp }; 3637ac27a0ecSDave Kleikamp 363864769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 363964769240SAlex Tomas .readpage = ext4_readpage, 36406311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 364143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 364220970ba6STheodore Ts'o .writepages = ext4_writepages, 364364769240SAlex Tomas .write_begin = ext4_da_write_begin, 364464769240SAlex Tomas .write_end = ext4_da_write_end, 36456dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 364664769240SAlex Tomas .bmap = ext4_bmap, 36478fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 364864769240SAlex Tomas .releasepage = ext4_releasepage, 3649378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 365064769240SAlex Tomas .migratepage = buffer_migrate_page, 36518ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3652aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36530e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 365464769240SAlex Tomas }; 365564769240SAlex Tomas 36565f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36575f0663bbSDan Williams .writepages = ext4_dax_writepages, 36585f0663bbSDan Williams .direct_IO = noop_direct_IO, 3659b82a96c9SMatthew Wilcox (Oracle) .set_page_dirty = __set_page_dirty_no_writeback, 366094dbb631SToshi Kani .bmap = ext4_bmap, 36615f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36620e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 36635f0663bbSDan Williams }; 36645f0663bbSDan Williams 3665617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3666ac27a0ecSDave Kleikamp { 36673d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36683d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36693d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36703d2b1582SLukas Czerner break; 36713d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3672617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 367374d553aaSTheodore Ts'o return; 36743d2b1582SLukas Czerner default: 36753d2b1582SLukas Czerner BUG(); 36763d2b1582SLukas Czerner } 36775f0663bbSDan Williams if (IS_DAX(inode)) 36785f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 36795f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 368074d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 368174d553aaSTheodore Ts'o else 368274d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3683ac27a0ecSDave Kleikamp } 3684ac27a0ecSDave Kleikamp 3685923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3686d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3687d863dc36SLukas Czerner { 368809cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 368909cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3690923ae0ffSRoss Zwisler unsigned blocksize, pos; 3691d863dc36SLukas Czerner ext4_lblk_t iblock; 3692d863dc36SLukas Czerner struct inode *inode = mapping->host; 3693d863dc36SLukas Czerner struct buffer_head *bh; 3694d863dc36SLukas Czerner struct page *page; 3695d863dc36SLukas Czerner int err = 0; 3696d863dc36SLukas Czerner 369709cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3698c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3699d863dc36SLukas Czerner if (!page) 3700d863dc36SLukas Czerner return -ENOMEM; 3701d863dc36SLukas Czerner 3702d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3703d863dc36SLukas Czerner 370409cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3705d863dc36SLukas Czerner 3706d863dc36SLukas Czerner if (!page_has_buffers(page)) 3707d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3708d863dc36SLukas Czerner 3709d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3710d863dc36SLukas Czerner bh = page_buffers(page); 3711d863dc36SLukas Czerner pos = blocksize; 3712d863dc36SLukas Czerner while (offset >= pos) { 3713d863dc36SLukas Czerner bh = bh->b_this_page; 3714d863dc36SLukas Czerner iblock++; 3715d863dc36SLukas Czerner pos += blocksize; 3716d863dc36SLukas Czerner } 3717d863dc36SLukas Czerner if (buffer_freed(bh)) { 3718d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3719d863dc36SLukas Czerner goto unlock; 3720d863dc36SLukas Czerner } 3721d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3722d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3723d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3724d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3725d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3726d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3727d863dc36SLukas Czerner goto unlock; 3728d863dc36SLukas Czerner } 3729d863dc36SLukas Czerner } 3730d863dc36SLukas Czerner 3731d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3732d863dc36SLukas Czerner if (PageUptodate(page)) 3733d863dc36SLukas Czerner set_buffer_uptodate(bh); 3734d863dc36SLukas Czerner 3735d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 37362d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 37372d069c08Szhangyi (F) if (err) 3738d863dc36SLukas Czerner goto unlock; 37394f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3740c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3741a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3742834f1565SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3743834f1565SEric Biggers bh_offset(bh)); 3744834f1565SEric Biggers if (err) { 3745834f1565SEric Biggers clear_buffer_uptodate(bh); 3746834f1565SEric Biggers goto unlock; 3747834f1565SEric Biggers } 3748c9c7429cSMichael Halcrow } 3749d863dc36SLukas Czerner } 3750d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3751d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3752188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 3753188c299eSJan Kara EXT4_JTR_NONE); 3754d863dc36SLukas Czerner if (err) 3755d863dc36SLukas Czerner goto unlock; 3756d863dc36SLukas Czerner } 3757d863dc36SLukas Czerner zero_user(page, offset, length); 3758d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3759d863dc36SLukas Czerner 3760d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3761d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37620713ed0cSLukas Czerner } else { 3763353eefd3Sjon ernst err = 0; 3764d863dc36SLukas Czerner mark_buffer_dirty(bh); 37653957ef53SJan Kara if (ext4_should_order_data(inode)) 376673131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 376773131fbbSRoss Zwisler length); 37680713ed0cSLukas Czerner } 3769d863dc36SLukas Czerner 3770d863dc36SLukas Czerner unlock: 3771d863dc36SLukas Czerner unlock_page(page); 377209cbfeafSKirill A. Shutemov put_page(page); 3773d863dc36SLukas Czerner return err; 3774d863dc36SLukas Czerner } 3775d863dc36SLukas Czerner 377694350ab5SMatthew Wilcox /* 3777923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3778923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3779923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3780923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 37813088e5a5SBhaskar Chowdhury * that corresponds to 'from' 3782923ae0ffSRoss Zwisler */ 3783923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3784923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3785923ae0ffSRoss Zwisler { 3786923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 378709cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3788923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3789923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3790923ae0ffSRoss Zwisler 3791923ae0ffSRoss Zwisler /* 3792923ae0ffSRoss Zwisler * correct length if it does not fall between 3793923ae0ffSRoss Zwisler * 'from' and the end of the block 3794923ae0ffSRoss Zwisler */ 3795923ae0ffSRoss Zwisler if (length > max || length < 0) 3796923ae0ffSRoss Zwisler length = max; 3797923ae0ffSRoss Zwisler 379847e69351SJan Kara if (IS_DAX(inode)) { 3799c6f40468SChristoph Hellwig return dax_zero_range(inode, from, length, NULL, 380047e69351SJan Kara &ext4_iomap_ops); 380147e69351SJan Kara } 3802923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3803923ae0ffSRoss Zwisler } 3804923ae0ffSRoss Zwisler 3805923ae0ffSRoss Zwisler /* 380694350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 380794350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 380894350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 380994350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 381094350ab5SMatthew Wilcox */ 3811c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 381294350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 381394350ab5SMatthew Wilcox { 381409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 381594350ab5SMatthew Wilcox unsigned length; 381694350ab5SMatthew Wilcox unsigned blocksize; 381794350ab5SMatthew Wilcox struct inode *inode = mapping->host; 381894350ab5SMatthew Wilcox 38190d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3820592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 38210d06863fSTheodore Ts'o return 0; 38220d06863fSTheodore Ts'o 382394350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 382494350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 382594350ab5SMatthew Wilcox 382694350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 382794350ab5SMatthew Wilcox } 382894350ab5SMatthew Wilcox 3829a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3830a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3831a87dd18cSLukas Czerner { 3832a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3833a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3834e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3835a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3836a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3837a87dd18cSLukas Czerner int err = 0; 3838a87dd18cSLukas Czerner 3839e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3840e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3841e1be3a92SLukas Czerner 3842a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3843a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3844a87dd18cSLukas Czerner 3845a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3846e1be3a92SLukas Czerner if (start == end && 3847e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3848a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3849a87dd18cSLukas Czerner lstart, length); 3850a87dd18cSLukas Czerner return err; 3851a87dd18cSLukas Czerner } 3852a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3853e1be3a92SLukas Czerner if (partial_start) { 3854a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3855a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3856a87dd18cSLukas Czerner if (err) 3857a87dd18cSLukas Czerner return err; 3858a87dd18cSLukas Czerner } 3859a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3860e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3861a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3862e1be3a92SLukas Czerner byte_end - partial_end, 3863e1be3a92SLukas Czerner partial_end + 1); 3864a87dd18cSLukas Czerner return err; 3865a87dd18cSLukas Czerner } 3866a87dd18cSLukas Czerner 386791ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 386891ef4cafSDuane Griffin { 386991ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 387091ef4cafSDuane Griffin return 1; 387191ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 387291ef4cafSDuane Griffin return 1; 387391ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 387491ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 387591ef4cafSDuane Griffin return 0; 387691ef4cafSDuane Griffin } 387791ef4cafSDuane Griffin 3878ac27a0ecSDave Kleikamp /* 387901127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 388001127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 388101127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 388201127848SJan Kara * that will never happen after we truncate page cache. 388301127848SJan Kara */ 388401127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 388501127848SJan Kara loff_t len) 388601127848SJan Kara { 388701127848SJan Kara handle_t *handle; 38884209ae12SHarshad Shirwadkar int ret; 38894209ae12SHarshad Shirwadkar 389001127848SJan Kara loff_t size = i_size_read(inode); 389101127848SJan Kara 38925955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 389301127848SJan Kara if (offset > size || offset + len < size) 389401127848SJan Kara return 0; 389501127848SJan Kara 389601127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 389701127848SJan Kara return 0; 389801127848SJan Kara 389901127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 390001127848SJan Kara if (IS_ERR(handle)) 390101127848SJan Kara return PTR_ERR(handle); 390201127848SJan Kara ext4_update_i_disksize(inode, size); 39034209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 390401127848SJan Kara ext4_journal_stop(handle); 390501127848SJan Kara 39064209ae12SHarshad Shirwadkar return ret; 390701127848SJan Kara } 390801127848SJan Kara 3909d4f5258eSJan Kara static void ext4_wait_dax_page(struct inode *inode) 3910430657b6SRoss Zwisler { 3911d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 3912430657b6SRoss Zwisler schedule(); 3913d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 3914430657b6SRoss Zwisler } 3915430657b6SRoss Zwisler 3916430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3917430657b6SRoss Zwisler { 3918430657b6SRoss Zwisler struct page *page; 3919430657b6SRoss Zwisler int error; 3920430657b6SRoss Zwisler 3921d4f5258eSJan Kara if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock))) 3922430657b6SRoss Zwisler return -EINVAL; 3923430657b6SRoss Zwisler 3924430657b6SRoss Zwisler do { 3925430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3926430657b6SRoss Zwisler if (!page) 3927430657b6SRoss Zwisler return 0; 3928430657b6SRoss Zwisler 3929430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3930430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3931430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3932d4f5258eSJan Kara ext4_wait_dax_page(inode)); 3933b1f38217SRoss Zwisler } while (error == 0); 3934430657b6SRoss Zwisler 3935430657b6SRoss Zwisler return error; 3936430657b6SRoss Zwisler } 3937430657b6SRoss Zwisler 393801127848SJan Kara /* 3939cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3940a4bb6b64SAllison Henderson * associated with the given offset and length 3941a4bb6b64SAllison Henderson * 3942a4bb6b64SAllison Henderson * @inode: File inode 3943a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3944a4bb6b64SAllison Henderson * @len: The length of the hole 3945a4bb6b64SAllison Henderson * 39464907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3947a4bb6b64SAllison Henderson */ 3948a4bb6b64SAllison Henderson 3949aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3950a4bb6b64SAllison Henderson { 395126a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 395226a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 395326a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3954a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 395526a4c0c6STheodore Ts'o handle_t *handle; 395626a4c0c6STheodore Ts'o unsigned int credits; 39574209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 395826a4c0c6STheodore Ts'o 3959b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3960aaddea81SZheng Liu 3961c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3962c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3963d4f5258eSJan Kara filemap_invalidate_lock(mapping); 3964c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3965d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 3966c1e8220bSTheodore Ts'o if (ret) 3967c1e8220bSTheodore Ts'o return ret; 3968c1e8220bSTheodore Ts'o } 3969c1e8220bSTheodore Ts'o 397026a4c0c6STheodore Ts'o /* 397126a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 397226a4c0c6STheodore Ts'o * Then release them. 397326a4c0c6STheodore Ts'o */ 3974cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 397526a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 397626a4c0c6STheodore Ts'o offset + length - 1); 397726a4c0c6STheodore Ts'o if (ret) 397826a4c0c6STheodore Ts'o return ret; 397926a4c0c6STheodore Ts'o } 398026a4c0c6STheodore Ts'o 39815955102cSAl Viro inode_lock(inode); 39829ef06cecSLukas Czerner 398326a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 398426a4c0c6STheodore Ts'o if (offset >= inode->i_size) 398526a4c0c6STheodore Ts'o goto out_mutex; 398626a4c0c6STheodore Ts'o 398726a4c0c6STheodore Ts'o /* 398826a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 398926a4c0c6STheodore Ts'o * to end after the page that contains i_size 399026a4c0c6STheodore Ts'o */ 399126a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 399226a4c0c6STheodore Ts'o length = inode->i_size + 399309cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 399426a4c0c6STheodore Ts'o offset; 399526a4c0c6STheodore Ts'o } 399626a4c0c6STheodore Ts'o 3997a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 3998a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 3999a361293fSJan Kara /* 4000a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 4001a361293fSJan Kara * partial block 4002a361293fSJan Kara */ 4003a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 4004a361293fSJan Kara if (ret < 0) 4005a361293fSJan Kara goto out_mutex; 4006a361293fSJan Kara 4007a361293fSJan Kara } 4008a361293fSJan Kara 4009f340b3d9Shongnanli /* Wait all existing dio workers, newcomers will block on i_rwsem */ 4010ea3d7209SJan Kara inode_dio_wait(inode); 4011ea3d7209SJan Kara 4012ea3d7209SJan Kara /* 4013ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 4014ea3d7209SJan Kara * page cache. 4015ea3d7209SJan Kara */ 4016d4f5258eSJan Kara filemap_invalidate_lock(mapping); 4017430657b6SRoss Zwisler 4018430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4019430657b6SRoss Zwisler if (ret) 4020430657b6SRoss Zwisler goto out_dio; 4021430657b6SRoss Zwisler 4022a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4023a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 402426a4c0c6STheodore Ts'o 4025a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 402601127848SJan Kara if (last_block_offset > first_block_offset) { 402701127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 402801127848SJan Kara if (ret) 402901127848SJan Kara goto out_dio; 4030a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4031a87dd18cSLukas Czerner last_block_offset); 403201127848SJan Kara } 403326a4c0c6STheodore Ts'o 403426a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 403526a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 403626a4c0c6STheodore Ts'o else 403726a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 403826a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 403926a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 404026a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 404126a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 404226a4c0c6STheodore Ts'o goto out_dio; 404326a4c0c6STheodore Ts'o } 404426a4c0c6STheodore Ts'o 4045a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4046a87dd18cSLukas Czerner length); 404726a4c0c6STheodore Ts'o if (ret) 404826a4c0c6STheodore Ts'o goto out_stop; 404926a4c0c6STheodore Ts'o 405026a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 405126a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 405226a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 405326a4c0c6STheodore Ts'o 4054eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4055eee597acSLukas Czerner if (stop_block > first_block) { 405626a4c0c6STheodore Ts'o 405726a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 405827bc446eSbrookxu ext4_discard_preallocations(inode, 0); 405926a4c0c6STheodore Ts'o 406026a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 406126a4c0c6STheodore Ts'o stop_block - first_block); 406226a4c0c6STheodore Ts'o if (ret) { 406326a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 406426a4c0c6STheodore Ts'o goto out_stop; 406526a4c0c6STheodore Ts'o } 406626a4c0c6STheodore Ts'o 406726a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 406826a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 406926a4c0c6STheodore Ts'o stop_block - 1); 407026a4c0c6STheodore Ts'o else 40714f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 407226a4c0c6STheodore Ts'o stop_block); 407326a4c0c6STheodore Ts'o 4074819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4075eee597acSLukas Czerner } 4076a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 407726a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 407826a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4079e251f9bcSMaxim Patlasov 4080eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 40814209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 40824209ae12SHarshad Shirwadkar if (unlikely(ret2)) 40834209ae12SHarshad Shirwadkar ret = ret2; 408467a7d5f5SJan Kara if (ret >= 0) 408567a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 408626a4c0c6STheodore Ts'o out_stop: 408726a4c0c6STheodore Ts'o ext4_journal_stop(handle); 408826a4c0c6STheodore Ts'o out_dio: 4089d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 409026a4c0c6STheodore Ts'o out_mutex: 40915955102cSAl Viro inode_unlock(inode); 409226a4c0c6STheodore Ts'o return ret; 4093a4bb6b64SAllison Henderson } 4094a4bb6b64SAllison Henderson 4095a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4096a361293fSJan Kara { 4097a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4098a361293fSJan Kara struct jbd2_inode *jinode; 4099a361293fSJan Kara 4100a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4101a361293fSJan Kara return 0; 4102a361293fSJan Kara 4103a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4104a361293fSJan Kara spin_lock(&inode->i_lock); 4105a361293fSJan Kara if (!ei->jinode) { 4106a361293fSJan Kara if (!jinode) { 4107a361293fSJan Kara spin_unlock(&inode->i_lock); 4108a361293fSJan Kara return -ENOMEM; 4109a361293fSJan Kara } 4110a361293fSJan Kara ei->jinode = jinode; 4111a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4112a361293fSJan Kara jinode = NULL; 4113a361293fSJan Kara } 4114a361293fSJan Kara spin_unlock(&inode->i_lock); 4115a361293fSJan Kara if (unlikely(jinode != NULL)) 4116a361293fSJan Kara jbd2_free_inode(jinode); 4117a361293fSJan Kara return 0; 4118a361293fSJan Kara } 4119a361293fSJan Kara 4120a4bb6b64SAllison Henderson /* 4121617ba13bSMingming Cao * ext4_truncate() 4122ac27a0ecSDave Kleikamp * 4123617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4124617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4125ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4126ac27a0ecSDave Kleikamp * 412742b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4128ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4129ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4130ac27a0ecSDave Kleikamp * 4131ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4132ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4133ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4134ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4135ac27a0ecSDave Kleikamp * left-to-right works OK too). 4136ac27a0ecSDave Kleikamp * 4137ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4138ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4139ac27a0ecSDave Kleikamp * 4140ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4141617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4142ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4143617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4144617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4145ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4146617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4147ac27a0ecSDave Kleikamp */ 41482c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4149ac27a0ecSDave Kleikamp { 4150819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4151819c4920STheodore Ts'o unsigned int credits; 41524209ae12SHarshad Shirwadkar int err = 0, err2; 4153819c4920STheodore Ts'o handle_t *handle; 4154819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4155819c4920STheodore Ts'o 415619b5ef61STheodore Ts'o /* 415719b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4158e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 4159f340b3d9Shongnanli * have i_rwsem locked because it's not necessary. 416019b5ef61STheodore Ts'o */ 416119b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41625955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41630562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41640562e0baSJiaying Zhang 416591ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41669a5d265fSzhengliang goto out_trace; 4167ac27a0ecSDave Kleikamp 41685534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 416919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41707d8f9f7dSTheodore Ts'o 4171aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4172aef1c851STao Ma int has_inline = 1; 4173aef1c851STao Ma 417401daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 41759a5d265fSzhengliang if (err || has_inline) 41769a5d265fSzhengliang goto out_trace; 4177aef1c851STao Ma } 4178aef1c851STao Ma 4179a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4180a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4181a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 41829a5d265fSzhengliang goto out_trace; 4183a361293fSJan Kara } 4184a361293fSJan Kara 4185ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4186819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4187ff9893dcSAmir Goldstein else 4188819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4189819c4920STheodore Ts'o 4190819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 41919a5d265fSzhengliang if (IS_ERR(handle)) { 41929a5d265fSzhengliang err = PTR_ERR(handle); 41939a5d265fSzhengliang goto out_trace; 41949a5d265fSzhengliang } 4195819c4920STheodore Ts'o 4196eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4197eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4198819c4920STheodore Ts'o 4199819c4920STheodore Ts'o /* 4200819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4201819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4202819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4203819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4204819c4920STheodore Ts'o * 4205819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4206819c4920STheodore Ts'o * truncatable state while each transaction commits. 4207819c4920STheodore Ts'o */ 42082c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 42092c98eb5eSTheodore Ts'o if (err) 4210819c4920STheodore Ts'o goto out_stop; 4211819c4920STheodore Ts'o 4212819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4213819c4920STheodore Ts'o 421427bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4215819c4920STheodore Ts'o 4216819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4217d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4218819c4920STheodore Ts'o else 4219819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4220819c4920STheodore Ts'o 4221819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4222d0abb36dSTheodore Ts'o if (err) 4223d0abb36dSTheodore Ts'o goto out_stop; 4224819c4920STheodore Ts'o 4225819c4920STheodore Ts'o if (IS_SYNC(inode)) 4226819c4920STheodore Ts'o ext4_handle_sync(handle); 4227819c4920STheodore Ts'o 4228819c4920STheodore Ts'o out_stop: 4229819c4920STheodore Ts'o /* 4230819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4231819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4232819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 423358d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4234819c4920STheodore Ts'o * orphan info for us. 4235819c4920STheodore Ts'o */ 4236819c4920STheodore Ts'o if (inode->i_nlink) 4237819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4238819c4920STheodore Ts'o 4239eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 42404209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 42414209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 42424209ae12SHarshad Shirwadkar err = err2; 4243819c4920STheodore Ts'o ext4_journal_stop(handle); 4244a86c6181SAlex Tomas 42459a5d265fSzhengliang out_trace: 42460562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42472c98eb5eSTheodore Ts'o return err; 4248ac27a0ecSDave Kleikamp } 4249ac27a0ecSDave Kleikamp 42509a1bf32cSZhang Yi static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 42519a1bf32cSZhang Yi { 42529a1bf32cSZhang Yi if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 42539a1bf32cSZhang Yi return inode_peek_iversion_raw(inode); 42549a1bf32cSZhang Yi else 42559a1bf32cSZhang Yi return inode_peek_iversion(inode); 42569a1bf32cSZhang Yi } 42579a1bf32cSZhang Yi 42589a1bf32cSZhang Yi static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, 42599a1bf32cSZhang Yi struct ext4_inode_info *ei) 42609a1bf32cSZhang Yi { 42619a1bf32cSZhang Yi struct inode *inode = &(ei->vfs_inode); 42629a1bf32cSZhang Yi u64 i_blocks = READ_ONCE(inode->i_blocks); 42639a1bf32cSZhang Yi struct super_block *sb = inode->i_sb; 42649a1bf32cSZhang Yi 42659a1bf32cSZhang Yi if (i_blocks <= ~0U) { 42669a1bf32cSZhang Yi /* 42679a1bf32cSZhang Yi * i_blocks can be represented in a 32 bit variable 42689a1bf32cSZhang Yi * as multiple of 512 bytes 42699a1bf32cSZhang Yi */ 42709a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42719a1bf32cSZhang Yi raw_inode->i_blocks_high = 0; 42729a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42739a1bf32cSZhang Yi return 0; 42749a1bf32cSZhang Yi } 42759a1bf32cSZhang Yi 42769a1bf32cSZhang Yi /* 42779a1bf32cSZhang Yi * This should never happen since sb->s_maxbytes should not have 42789a1bf32cSZhang Yi * allowed this, sb->s_maxbytes was set according to the huge_file 42799a1bf32cSZhang Yi * feature in ext4_fill_super(). 42809a1bf32cSZhang Yi */ 42819a1bf32cSZhang Yi if (!ext4_has_feature_huge_file(sb)) 42829a1bf32cSZhang Yi return -EFSCORRUPTED; 42839a1bf32cSZhang Yi 42849a1bf32cSZhang Yi if (i_blocks <= 0xffffffffffffULL) { 42859a1bf32cSZhang Yi /* 42869a1bf32cSZhang Yi * i_blocks can be represented in a 48 bit variable 42879a1bf32cSZhang Yi * as multiple of 512 bytes 42889a1bf32cSZhang Yi */ 42899a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42909a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42919a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42929a1bf32cSZhang Yi } else { 42939a1bf32cSZhang Yi ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42949a1bf32cSZhang Yi /* i_block is stored in file system block size */ 42959a1bf32cSZhang Yi i_blocks = i_blocks >> (inode->i_blkbits - 9); 42969a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42979a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42989a1bf32cSZhang Yi } 42999a1bf32cSZhang Yi return 0; 43009a1bf32cSZhang Yi } 43019a1bf32cSZhang Yi 43029a1bf32cSZhang Yi static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) 43039a1bf32cSZhang Yi { 43049a1bf32cSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 43059a1bf32cSZhang Yi uid_t i_uid; 43069a1bf32cSZhang Yi gid_t i_gid; 43079a1bf32cSZhang Yi projid_t i_projid; 43089a1bf32cSZhang Yi int block; 43099a1bf32cSZhang Yi int err; 43109a1bf32cSZhang Yi 43119a1bf32cSZhang Yi err = ext4_inode_blocks_set(raw_inode, ei); 43129a1bf32cSZhang Yi 43139a1bf32cSZhang Yi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 43149a1bf32cSZhang Yi i_uid = i_uid_read(inode); 43159a1bf32cSZhang Yi i_gid = i_gid_read(inode); 43169a1bf32cSZhang Yi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 43179a1bf32cSZhang Yi if (!(test_opt(inode->i_sb, NO_UID32))) { 43189a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 43199a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 43209a1bf32cSZhang Yi /* 43219a1bf32cSZhang Yi * Fix up interoperability with old kernels. Otherwise, 43229a1bf32cSZhang Yi * old inodes get re-used with the upper 16 bits of the 43239a1bf32cSZhang Yi * uid/gid intact. 43249a1bf32cSZhang Yi */ 43259a1bf32cSZhang Yi if (ei->i_dtime && list_empty(&ei->i_orphan)) { 43269a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 43279a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 43289a1bf32cSZhang Yi } else { 43299a1bf32cSZhang Yi raw_inode->i_uid_high = 43309a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_uid)); 43319a1bf32cSZhang Yi raw_inode->i_gid_high = 43329a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_gid)); 43339a1bf32cSZhang Yi } 43349a1bf32cSZhang Yi } else { 43359a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 43369a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 43379a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 43389a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 43399a1bf32cSZhang Yi } 43409a1bf32cSZhang Yi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 43419a1bf32cSZhang Yi 43429a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 43439a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 43449a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 43459a1bf32cSZhang Yi EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 43469a1bf32cSZhang Yi 43479a1bf32cSZhang Yi raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 43489a1bf32cSZhang Yi raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 43499a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 43509a1bf32cSZhang Yi raw_inode->i_file_acl_high = 43519a1bf32cSZhang Yi cpu_to_le16(ei->i_file_acl >> 32); 43529a1bf32cSZhang Yi raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 43539a1bf32cSZhang Yi ext4_isize_set(raw_inode, ei->i_disksize); 43549a1bf32cSZhang Yi 43559a1bf32cSZhang Yi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 43569a1bf32cSZhang Yi if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 43579a1bf32cSZhang Yi if (old_valid_dev(inode->i_rdev)) { 43589a1bf32cSZhang Yi raw_inode->i_block[0] = 43599a1bf32cSZhang Yi cpu_to_le32(old_encode_dev(inode->i_rdev)); 43609a1bf32cSZhang Yi raw_inode->i_block[1] = 0; 43619a1bf32cSZhang Yi } else { 43629a1bf32cSZhang Yi raw_inode->i_block[0] = 0; 43639a1bf32cSZhang Yi raw_inode->i_block[1] = 43649a1bf32cSZhang Yi cpu_to_le32(new_encode_dev(inode->i_rdev)); 43659a1bf32cSZhang Yi raw_inode->i_block[2] = 0; 43669a1bf32cSZhang Yi } 43679a1bf32cSZhang Yi } else if (!ext4_has_inline_data(inode)) { 43689a1bf32cSZhang Yi for (block = 0; block < EXT4_N_BLOCKS; block++) 43699a1bf32cSZhang Yi raw_inode->i_block[block] = ei->i_data[block]; 43709a1bf32cSZhang Yi } 43719a1bf32cSZhang Yi 43729a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 43739a1bf32cSZhang Yi u64 ivers = ext4_inode_peek_iversion(inode); 43749a1bf32cSZhang Yi 43759a1bf32cSZhang Yi raw_inode->i_disk_version = cpu_to_le32(ivers); 43769a1bf32cSZhang Yi if (ei->i_extra_isize) { 43779a1bf32cSZhang Yi if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 43789a1bf32cSZhang Yi raw_inode->i_version_hi = 43799a1bf32cSZhang Yi cpu_to_le32(ivers >> 32); 43809a1bf32cSZhang Yi raw_inode->i_extra_isize = 43819a1bf32cSZhang Yi cpu_to_le16(ei->i_extra_isize); 43829a1bf32cSZhang Yi } 43839a1bf32cSZhang Yi } 43849a1bf32cSZhang Yi 43859a1bf32cSZhang Yi if (i_projid != EXT4_DEF_PROJID && 43869a1bf32cSZhang Yi !ext4_has_feature_project(inode->i_sb)) 43879a1bf32cSZhang Yi err = err ?: -EFSCORRUPTED; 43889a1bf32cSZhang Yi 43899a1bf32cSZhang Yi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 43909a1bf32cSZhang Yi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 43919a1bf32cSZhang Yi raw_inode->i_projid = cpu_to_le32(i_projid); 43929a1bf32cSZhang Yi 43939a1bf32cSZhang Yi ext4_inode_csum_set(inode, raw_inode, ei); 43949a1bf32cSZhang Yi return err; 43959a1bf32cSZhang Yi } 43969a1bf32cSZhang Yi 4397ac27a0ecSDave Kleikamp /* 4398617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4399de01f484SZhang Yi * underlying buffer_head on success. If we pass 'inode' and it does not 4400de01f484SZhang Yi * have in-inode xattr, we have all inode data in memory that is needed 4401de01f484SZhang Yi * to recreate the on-disk version of this inode. 4402ac27a0ecSDave Kleikamp */ 44038016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 4404de01f484SZhang Yi struct inode *inode, struct ext4_iloc *iloc, 44058016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4406ac27a0ecSDave Kleikamp { 4407240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4408ac27a0ecSDave Kleikamp struct buffer_head *bh; 4409240799cdSTheodore Ts'o ext4_fsblk_t block; 441002f03c42SLinus Torvalds struct blk_plug plug; 4411240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4412ac27a0ecSDave Kleikamp 44133a06d778SAneesh Kumar K.V iloc->bh = NULL; 44148016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 44158016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 44166a797d27SDarrick J. Wong return -EFSCORRUPTED; 4417ac27a0ecSDave Kleikamp 44188016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4419240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4420240799cdSTheodore Ts'o if (!gdp) 4421240799cdSTheodore Ts'o return -EIO; 4422240799cdSTheodore Ts'o 4423240799cdSTheodore Ts'o /* 4424240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4425240799cdSTheodore Ts'o */ 442600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 44278016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4428240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4429240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4430240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4431240799cdSTheodore Ts'o 4432240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4433aebf0243SWang Shilong if (unlikely(!bh)) 4434860d21e2STheodore Ts'o return -ENOMEM; 44358e33fadfSZhang Yi if (ext4_buffer_uptodate(bh)) 4436ac27a0ecSDave Kleikamp goto has_buffer; 4437ac27a0ecSDave Kleikamp 44388e33fadfSZhang Yi lock_buffer(bh); 4439f2c77973SZhang Yi if (ext4_buffer_uptodate(bh)) { 4440f2c77973SZhang Yi /* Someone brought it uptodate while we waited */ 4441f2c77973SZhang Yi unlock_buffer(bh); 4442f2c77973SZhang Yi goto has_buffer; 4443f2c77973SZhang Yi } 4444f2c77973SZhang Yi 4445ac27a0ecSDave Kleikamp /* 4446ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4447ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4448ac27a0ecSDave Kleikamp * block. 4449ac27a0ecSDave Kleikamp */ 4450de01f484SZhang Yi if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 4451ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4452240799cdSTheodore Ts'o int i, start; 4453ac27a0ecSDave Kleikamp 4454240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4455ac27a0ecSDave Kleikamp 4456ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4457240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4458aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4459ac27a0ecSDave Kleikamp goto make_io; 4460ac27a0ecSDave Kleikamp 4461ac27a0ecSDave Kleikamp /* 4462ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4463ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4464ac27a0ecSDave Kleikamp * of one, so skip it. 4465ac27a0ecSDave Kleikamp */ 4466ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4467ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4468ac27a0ecSDave Kleikamp goto make_io; 4469ac27a0ecSDave Kleikamp } 4470240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4471ac27a0ecSDave Kleikamp if (i == inode_offset) 4472ac27a0ecSDave Kleikamp continue; 4473617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4474ac27a0ecSDave Kleikamp break; 4475ac27a0ecSDave Kleikamp } 4476ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4477240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4478de01f484SZhang Yi struct ext4_inode *raw_inode = 4479de01f484SZhang Yi (struct ext4_inode *) (bh->b_data + iloc->offset); 4480de01f484SZhang Yi 4481ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4482ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4483de01f484SZhang Yi if (!ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4484de01f484SZhang Yi ext4_fill_raw_inode(inode, raw_inode); 4485ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4486ac27a0ecSDave Kleikamp unlock_buffer(bh); 4487ac27a0ecSDave Kleikamp goto has_buffer; 4488ac27a0ecSDave Kleikamp } 4489ac27a0ecSDave Kleikamp } 4490ac27a0ecSDave Kleikamp 4491ac27a0ecSDave Kleikamp make_io: 4492ac27a0ecSDave Kleikamp /* 4493240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4494240799cdSTheodore Ts'o * blocks from the inode table. 4495240799cdSTheodore Ts'o */ 449602f03c42SLinus Torvalds blk_start_plug(&plug); 4497240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4498240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4499240799cdSTheodore Ts'o unsigned num; 45000d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4501240799cdSTheodore Ts'o 4502240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4503b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 45040d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4505240799cdSTheodore Ts'o if (table > b) 4506240799cdSTheodore Ts'o b = table; 45070d606e2cSTheodore Ts'o end = b + ra_blks; 4508240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4509feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4510560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4511240799cdSTheodore Ts'o table += num / inodes_per_block; 4512240799cdSTheodore Ts'o if (end > table) 4513240799cdSTheodore Ts'o end = table; 4514240799cdSTheodore Ts'o while (b <= end) 45155df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4516240799cdSTheodore Ts'o } 4517240799cdSTheodore Ts'o 4518240799cdSTheodore Ts'o /* 4519ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4520ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4521ac27a0ecSDave Kleikamp * Read the block from disk. 4522ac27a0ecSDave Kleikamp */ 45238016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 45242d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 452502f03c42SLinus Torvalds blk_finish_plug(&plug); 4526ac27a0ecSDave Kleikamp wait_on_buffer(bh); 45270904c9aeSZhang Yi ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4528ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 45298016e29fSHarshad Shirwadkar if (ret_block) 45308016e29fSHarshad Shirwadkar *ret_block = block; 4531ac27a0ecSDave Kleikamp brelse(bh); 4532ac27a0ecSDave Kleikamp return -EIO; 4533ac27a0ecSDave Kleikamp } 4534ac27a0ecSDave Kleikamp has_buffer: 4535ac27a0ecSDave Kleikamp iloc->bh = bh; 4536ac27a0ecSDave Kleikamp return 0; 4537ac27a0ecSDave Kleikamp } 4538ac27a0ecSDave Kleikamp 45398016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 45408016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45418016e29fSHarshad Shirwadkar { 4542c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 45438016e29fSHarshad Shirwadkar int ret; 45448016e29fSHarshad Shirwadkar 4545de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc, 45468016e29fSHarshad Shirwadkar &err_blk); 45478016e29fSHarshad Shirwadkar 45488016e29fSHarshad Shirwadkar if (ret == -EIO) 45498016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 45508016e29fSHarshad Shirwadkar "unable to read itable block"); 45518016e29fSHarshad Shirwadkar 45528016e29fSHarshad Shirwadkar return ret; 45538016e29fSHarshad Shirwadkar } 45548016e29fSHarshad Shirwadkar 4555617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4556ac27a0ecSDave Kleikamp { 4557c27c29c6SHarshad Shirwadkar ext4_fsblk_t err_blk = 0; 45588016e29fSHarshad Shirwadkar int ret; 45598016e29fSHarshad Shirwadkar 4560de01f484SZhang Yi ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc, 4561de01f484SZhang Yi &err_blk); 45628016e29fSHarshad Shirwadkar 45638016e29fSHarshad Shirwadkar if (ret == -EIO) 45648016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 45658016e29fSHarshad Shirwadkar "unable to read itable block"); 45668016e29fSHarshad Shirwadkar 45678016e29fSHarshad Shirwadkar return ret; 45688016e29fSHarshad Shirwadkar } 45698016e29fSHarshad Shirwadkar 45708016e29fSHarshad Shirwadkar 45718016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 45728016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45738016e29fSHarshad Shirwadkar { 4574de01f484SZhang Yi return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL); 4575ac27a0ecSDave Kleikamp } 4576ac27a0ecSDave Kleikamp 4577a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 45786642586bSRoss Zwisler { 4579a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4580a8ab6d38SIra Weiny 45819cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 45826642586bSRoss Zwisler return false; 45836642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 45846642586bSRoss Zwisler return false; 45856642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 45866642586bSRoss Zwisler return false; 45876642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 45886642586bSRoss Zwisler return false; 4589592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 45906642586bSRoss Zwisler return false; 4591c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4592c93d8f88SEric Biggers return false; 4593a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4594a8ab6d38SIra Weiny return false; 4595a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 45966642586bSRoss Zwisler return true; 4597a8ab6d38SIra Weiny 4598b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 45996642586bSRoss Zwisler } 46006642586bSRoss Zwisler 4601043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4602ac27a0ecSDave Kleikamp { 4603617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 460400a1a053STheodore Ts'o unsigned int new_fl = 0; 4605ac27a0ecSDave Kleikamp 4606043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4607043546e4SIra Weiny 4608617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 460900a1a053STheodore Ts'o new_fl |= S_SYNC; 4610617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 461100a1a053STheodore Ts'o new_fl |= S_APPEND; 4612617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 461300a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4614617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 461500a1a053STheodore Ts'o new_fl |= S_NOATIME; 4616617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 461700a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4618043546e4SIra Weiny 4619043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4620043546e4SIra Weiny * here if already set. */ 4621043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4622043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4623923ae0ffSRoss Zwisler new_fl |= S_DAX; 4624043546e4SIra Weiny 46252ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 46262ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4627b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4628b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4629c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4630c93d8f88SEric Biggers new_fl |= S_VERITY; 46315f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 46322ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4633c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4634ac27a0ecSDave Kleikamp } 4635ac27a0ecSDave Kleikamp 46360fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 46370fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 46380fc1b451SAneesh Kumar K.V { 46390fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 46408180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 46418180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 46420fc1b451SAneesh Kumar K.V 4643e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 46440fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 46450fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 46460fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 464707a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 46488180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 46498180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 46508180a562SAneesh Kumar K.V } else { 46510fc1b451SAneesh Kumar K.V return i_blocks; 46528180a562SAneesh Kumar K.V } 46530fc1b451SAneesh Kumar K.V } else { 46540fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 46550fc1b451SAneesh Kumar K.V } 46560fc1b451SAneesh Kumar K.V } 4657ff9ddf7eSJan Kara 4658eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4659152a7b0aSTao Ma struct ext4_inode *raw_inode, 4660152a7b0aSTao Ma struct ext4_inode_info *ei) 4661152a7b0aSTao Ma { 4662152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4663152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4664eb9b5f01STheodore Ts'o 4665290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4666290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4667290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4668152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4669eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4670f19d5870STao Ma } else 4671f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4672eb9b5f01STheodore Ts'o return 0; 4673152a7b0aSTao Ma } 4674152a7b0aSTao Ma 4675040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4676040cb378SLi Xi { 46770b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4678040cb378SLi Xi return -EOPNOTSUPP; 4679040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4680040cb378SLi Xi return 0; 4681040cb378SLi Xi } 4682040cb378SLi Xi 4683e254d1afSEryu Guan /* 4684e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4685e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4686e254d1afSEryu Guan * set. 4687e254d1afSEryu Guan */ 4688e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4689e254d1afSEryu Guan { 4690e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4691e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4692e254d1afSEryu Guan else 4693e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4694e254d1afSEryu Guan } 4695e254d1afSEryu Guan 46968a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 46978a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 46988a363970STheodore Ts'o unsigned int line) 4699ac27a0ecSDave Kleikamp { 4700617ba13bSMingming Cao struct ext4_iloc iloc; 4701617ba13bSMingming Cao struct ext4_inode *raw_inode; 47021d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4703bd2c38cfSJan Kara struct ext4_super_block *es = EXT4_SB(sb)->s_es; 47041d1fe1eeSDavid Howells struct inode *inode; 4705b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 47061d1fe1eeSDavid Howells long ret; 47077e6e1ef4SDarrick J. Wong loff_t size; 4708ac27a0ecSDave Kleikamp int block; 470908cefc7aSEric W. Biederman uid_t i_uid; 471008cefc7aSEric W. Biederman gid_t i_gid; 4711040cb378SLi Xi projid_t i_projid; 4712ac27a0ecSDave Kleikamp 4713191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 4714bd2c38cfSJan Kara ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4715bd2c38cfSJan Kara ino == le32_to_cpu(es->s_usr_quota_inum) || 4716bd2c38cfSJan Kara ino == le32_to_cpu(es->s_grp_quota_inum) || 471702f310fcSJan Kara ino == le32_to_cpu(es->s_prj_quota_inum) || 471802f310fcSJan Kara ino == le32_to_cpu(es->s_orphan_file_inum))) || 47198a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 4720bd2c38cfSJan Kara (ino > le32_to_cpu(es->s_inodes_count))) { 47218a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 47228a363970STheodore Ts'o return ERR_PTR(-ESTALE); 4723014c9caaSJan Kara __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 47248a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 47258a363970STheodore Ts'o ino, current->comm); 47268a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 47278a363970STheodore Ts'o } 47288a363970STheodore Ts'o 47291d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 47301d1fe1eeSDavid Howells if (!inode) 47311d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 47321d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 47331d1fe1eeSDavid Howells return inode; 47341d1fe1eeSDavid Howells 47351d1fe1eeSDavid Howells ei = EXT4_I(inode); 47367dc57615SPeter Huewe iloc.bh = NULL; 4737ac27a0ecSDave Kleikamp 47388016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 47391d1fe1eeSDavid Howells if (ret < 0) 4740ac27a0ecSDave Kleikamp goto bad_inode; 4741617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4742814525f4SDarrick J. Wong 47438e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 47448a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47458a363970STheodore Ts'o "iget: root inode unallocated"); 47468e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 47478e4b5eaeSTheodore Ts'o goto bad_inode; 47488e4b5eaeSTheodore Ts'o } 47498e4b5eaeSTheodore Ts'o 47508a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 47518a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 47528a363970STheodore Ts'o ret = -ESTALE; 47538a363970STheodore Ts'o goto bad_inode; 47548a363970STheodore Ts'o } 47558a363970STheodore Ts'o 4756814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4757814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4758814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 47592dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 47602dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 47618a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47628a363970STheodore Ts'o "iget: bad extra_isize %u " 47638a363970STheodore Ts'o "(inode size %u)", 47642dc8d9e1SEric Biggers ei->i_extra_isize, 4765814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 47666a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4767814525f4SDarrick J. Wong goto bad_inode; 4768814525f4SDarrick J. Wong } 4769814525f4SDarrick J. Wong } else 4770814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4771814525f4SDarrick J. Wong 4772814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 47739aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4774814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4775814525f4SDarrick J. Wong __u32 csum; 4776814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4777814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4778814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4779814525f4SDarrick J. Wong sizeof(inum)); 4780814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4781814525f4SDarrick J. Wong sizeof(gen)); 4782814525f4SDarrick J. Wong } 4783814525f4SDarrick J. Wong 47848016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 47858016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 47868016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 47878016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 47888016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 47896a797d27SDarrick J. Wong ret = -EFSBADCRC; 4790814525f4SDarrick J. Wong goto bad_inode; 4791814525f4SDarrick J. Wong } 4792814525f4SDarrick J. Wong 4793ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 479408cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 479508cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 47960b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4797040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4798040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4799040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4800040cb378SLi Xi else 4801040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4802040cb378SLi Xi 4803ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 480408cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 480508cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4806ac27a0ecSDave Kleikamp } 480708cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 480808cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4809040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4810bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4811ac27a0ecSDave Kleikamp 4812353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 481367cf5b09STao Ma ei->i_inline_off = 0; 4814ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4815ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4816ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4817ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4818ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4819ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4820ac27a0ecSDave Kleikamp */ 4821ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4822393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4823393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4824393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4825ac27a0ecSDave Kleikamp /* this inode is deleted */ 48261d1fe1eeSDavid Howells ret = -ESTALE; 4827ac27a0ecSDave Kleikamp goto bad_inode; 4828ac27a0ecSDave Kleikamp } 4829ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4830ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4831ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4832393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4833393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4834393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4835ac27a0ecSDave Kleikamp } 4836ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4837043546e4SIra Weiny ext4_set_inode_flags(inode, true); 48380fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 48397973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4840e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4841a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4842a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4843e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 48447e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 48458a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48468a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 48477e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 48487e6e1ef4SDarrick J. Wong goto bad_inode; 48497e6e1ef4SDarrick J. Wong } 485048a34311SJan Kara /* 485148a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 485248a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 485348a34311SJan Kara * checksumming that corrupts checksums so forbid that. 485448a34311SJan Kara */ 485548a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 485648a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 485748a34311SJan Kara ext4_error_inode(inode, function, line, 0, 485848a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 485948a34311SJan Kara ret = -EFSCORRUPTED; 486048a34311SJan Kara goto bad_inode; 486148a34311SJan Kara } 4862ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4863a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4864a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4865a9e7f447SDmitry Monakhov #endif 4866ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4867ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4868a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4869ac27a0ecSDave Kleikamp /* 4870ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4871ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4872ac27a0ecSDave Kleikamp */ 4873617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4874ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4875ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4876aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4877ac27a0ecSDave Kleikamp 4878b436b9beSJan Kara /* 4879b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4880b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4881b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4882b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4883b436b9beSJan Kara * now it is reread from disk. 4884b436b9beSJan Kara */ 4885b436b9beSJan Kara if (journal) { 4886b436b9beSJan Kara transaction_t *transaction; 4887b436b9beSJan Kara tid_t tid; 4888b436b9beSJan Kara 4889a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4890b436b9beSJan Kara if (journal->j_running_transaction) 4891b436b9beSJan Kara transaction = journal->j_running_transaction; 4892b436b9beSJan Kara else 4893b436b9beSJan Kara transaction = journal->j_committing_transaction; 4894b436b9beSJan Kara if (transaction) 4895b436b9beSJan Kara tid = transaction->t_tid; 4896b436b9beSJan Kara else 4897b436b9beSJan Kara tid = journal->j_commit_sequence; 4898a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4899b436b9beSJan Kara ei->i_sync_tid = tid; 4900b436b9beSJan Kara ei->i_datasync_tid = tid; 4901b436b9beSJan Kara } 4902b436b9beSJan Kara 49030040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4904ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4905ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 49062dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4907617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4908617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4909ac27a0ecSDave Kleikamp } else { 4910eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4911eb9b5f01STheodore Ts'o if (ret) 4912eb9b5f01STheodore Ts'o goto bad_inode; 4913ac27a0ecSDave Kleikamp } 4914814525f4SDarrick J. Wong } 4915ac27a0ecSDave Kleikamp 4916ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4917ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4918ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4919ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4920ef7f3835SKalpak Shah 4921ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4922ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4923ee73f9a5SJeff Layton 492425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 492525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4926ee73f9a5SJeff Layton ivers |= 492725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 492825ec56b5SJean Noel Cordenner } 4929e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4930c4f65706STheodore Ts'o } 493125ec56b5SJean Noel Cordenner 4932c4b5a614STheodore Ts'o ret = 0; 4933485c26ecSTheodore Ts'o if (ei->i_file_acl && 4934ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 49358a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49368a363970STheodore Ts'o "iget: bad extended attribute block %llu", 493724676da4STheodore Ts'o ei->i_file_acl); 49386a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4939485c26ecSTheodore Ts'o goto bad_inode; 4940f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4941bc716523SLiu Song /* validate the block references in the inode */ 49428016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 49438016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4944fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 49458016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 4946bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4947bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4948bc716523SLiu Song else 49491f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4950fe2c8191SThiemo Nagel } 4951f19d5870STao Ma } 4952567f3e9aSTheodore Ts'o if (ret) 49537a262f7cSAneesh Kumar K.V goto bad_inode; 49547a262f7cSAneesh Kumar K.V 4955ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4956617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4957617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4958617ba13bSMingming Cao ext4_set_aops(inode); 4959ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4960617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4961617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4962ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 49636390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 49646390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 49658a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49668a363970STheodore Ts'o "iget: immutable or append flags " 49678a363970STheodore Ts'o "not allowed on symlinks"); 49686390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 49696390d33bSLuis R. Rodriguez goto bad_inode; 49706390d33bSLuis R. Rodriguez } 4971592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4972a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4973a7a67e8aSAl Viro ext4_set_aops(inode); 4974a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 497575e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4976617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4977e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4978e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4979e83c1397SDuane Griffin } else { 4980617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4981617ba13bSMingming Cao ext4_set_aops(inode); 4982ac27a0ecSDave Kleikamp } 498321fc61c7SAl Viro inode_nohighmem(inode); 4984563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4985563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4986617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4987ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4988ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4989ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4990ac27a0ecSDave Kleikamp else 4991ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4992ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4993393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4994393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4995563bdd61STheodore Ts'o } else { 49966a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 49978a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49988a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4999563bdd61STheodore Ts'o goto bad_inode; 5000ac27a0ecSDave Kleikamp } 50016456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 50026456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 50036456ca65STheodore Ts'o "casefold flag without casefold feature"); 5004ac27a0ecSDave Kleikamp brelse(iloc.bh); 5005dec214d0STahsin Erdogan 50061d1fe1eeSDavid Howells unlock_new_inode(inode); 50071d1fe1eeSDavid Howells return inode; 5008ac27a0ecSDave Kleikamp 5009ac27a0ecSDave Kleikamp bad_inode: 5010567f3e9aSTheodore Ts'o brelse(iloc.bh); 50111d1fe1eeSDavid Howells iget_failed(inode); 50121d1fe1eeSDavid Howells return ERR_PTR(ret); 5013ac27a0ecSDave Kleikamp } 5014ac27a0ecSDave Kleikamp 50153f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 50163f19b2abSDavid Howells unsigned long orig_ino, 50173f19b2abSDavid Howells unsigned long ino, 50183f19b2abSDavid Howells struct ext4_inode *raw_inode) 5019a26f4992STheodore Ts'o { 50203f19b2abSDavid Howells struct inode *inode; 5021a26f4992STheodore Ts'o 50223f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 50233f19b2abSDavid Howells if (!inode) 50243f19b2abSDavid Howells return; 50253f19b2abSDavid Howells 5026ed296c6cSEric Biggers if (!inode_is_dirtytime_only(inode)) 50273f19b2abSDavid Howells return; 50283f19b2abSDavid Howells 5029a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 5030ed296c6cSEric Biggers if (inode_is_dirtytime_only(inode)) { 5031a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5032a26f4992STheodore Ts'o 50335fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 5034a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5035a26f4992STheodore Ts'o 5036a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 50373f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 50383f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 50393f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 50403f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 5041a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 50423f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 50433f19b2abSDavid Howells return; 5044a26f4992STheodore Ts'o } 5045a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5046a26f4992STheodore Ts'o } 5047a26f4992STheodore Ts'o 5048a26f4992STheodore Ts'o /* 5049a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 5050a26f4992STheodore Ts'o * the same inode table block. 5051a26f4992STheodore Ts'o */ 5052a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 5053a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 5054a26f4992STheodore Ts'o { 5055a26f4992STheodore Ts'o unsigned long ino; 5056a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 5057a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 5058a26f4992STheodore Ts'o 50590f0ff9a9STheodore Ts'o /* 50600f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 50610f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 50620f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 50630f0ff9a9STheodore Ts'o */ 50640f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 50653f19b2abSDavid Howells rcu_read_lock(); 5066a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5067a26f4992STheodore Ts'o if (ino == orig_ino) 5068a26f4992STheodore Ts'o continue; 50693f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 50703f19b2abSDavid Howells (struct ext4_inode *)buf); 5071a26f4992STheodore Ts'o } 50723f19b2abSDavid Howells rcu_read_unlock(); 5073a26f4992STheodore Ts'o } 5074a26f4992STheodore Ts'o 5075664bd38bSZhang Yi /* 5076664bd38bSZhang Yi * Post the struct inode info into an on-disk inode location in the 5077664bd38bSZhang Yi * buffer-cache. This gobbles the caller's reference to the 5078664bd38bSZhang Yi * buffer_head in the inode location struct. 5079664bd38bSZhang Yi * 5080664bd38bSZhang Yi * The caller must have write access to iloc->bh. 5081664bd38bSZhang Yi */ 5082664bd38bSZhang Yi static int ext4_do_update_inode(handle_t *handle, 5083664bd38bSZhang Yi struct inode *inode, 5084664bd38bSZhang Yi struct ext4_iloc *iloc) 5085664bd38bSZhang Yi { 5086664bd38bSZhang Yi struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5087664bd38bSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 5088664bd38bSZhang Yi struct buffer_head *bh = iloc->bh; 5089664bd38bSZhang Yi struct super_block *sb = inode->i_sb; 5090664bd38bSZhang Yi int err; 5091664bd38bSZhang Yi int need_datasync = 0, set_large_file = 0; 5092664bd38bSZhang Yi 5093664bd38bSZhang Yi spin_lock(&ei->i_raw_lock); 5094664bd38bSZhang Yi 5095664bd38bSZhang Yi /* 5096664bd38bSZhang Yi * For fields not tracked in the in-memory inode, initialise them 5097664bd38bSZhang Yi * to zero for new inodes. 5098664bd38bSZhang Yi */ 5099664bd38bSZhang Yi if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5100664bd38bSZhang Yi memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5101664bd38bSZhang Yi 5102664bd38bSZhang Yi if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) 5103664bd38bSZhang Yi need_datasync = 1; 5104664bd38bSZhang Yi if (ei->i_disksize > 0x7fffffffULL) { 5105664bd38bSZhang Yi if (!ext4_has_feature_large_file(sb) || 5106664bd38bSZhang Yi EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) 5107664bd38bSZhang Yi set_large_file = 1; 5108664bd38bSZhang Yi } 5109664bd38bSZhang Yi 5110664bd38bSZhang Yi err = ext4_fill_raw_inode(inode, raw_inode); 5111202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 5112baaae979SZhang Yi if (err) { 5113baaae979SZhang Yi EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5114baaae979SZhang Yi goto out_brelse; 5115baaae979SZhang Yi } 5116baaae979SZhang Yi 51171751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5118a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5119a26f4992STheodore Ts'o bh->b_data); 5120202ee5dfSTheodore Ts'o 51210390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 51227d8bd3c7SShijie Luo err = ext4_handle_dirty_metadata(handle, NULL, bh); 51237d8bd3c7SShijie Luo if (err) 5124baaae979SZhang Yi goto out_error; 512519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5126202ee5dfSTheodore Ts'o if (set_large_file) { 51275d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5128188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, 5129188c299eSJan Kara EXT4_SB(sb)->s_sbh, 5130188c299eSJan Kara EXT4_JTR_NONE); 5131202ee5dfSTheodore Ts'o if (err) 5132baaae979SZhang Yi goto out_error; 513305c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 5134e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 513505c2c00fSJan Kara ext4_superblock_csum_set(sb); 513605c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 5137202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5138a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, 5139a3f5cf14SJan Kara EXT4_SB(sb)->s_sbh); 5140202ee5dfSTheodore Ts'o } 5141b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5142baaae979SZhang Yi out_error: 5143baaae979SZhang Yi ext4_std_error(inode->i_sb, err); 5144ac27a0ecSDave Kleikamp out_brelse: 5145ac27a0ecSDave Kleikamp brelse(bh); 5146ac27a0ecSDave Kleikamp return err; 5147ac27a0ecSDave Kleikamp } 5148ac27a0ecSDave Kleikamp 5149ac27a0ecSDave Kleikamp /* 5150617ba13bSMingming Cao * ext4_write_inode() 5151ac27a0ecSDave Kleikamp * 5152ac27a0ecSDave Kleikamp * We are called from a few places: 5153ac27a0ecSDave Kleikamp * 515487f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5155ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51564907cb7bSAnatol Pomozov * transaction to commit. 5157ac27a0ecSDave Kleikamp * 515887f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 515987f7e416STheodore Ts'o * We wait on commit, if told to. 5160ac27a0ecSDave Kleikamp * 516187f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 516287f7e416STheodore Ts'o * We wait on commit, if told to. 5163ac27a0ecSDave Kleikamp * 5164ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5165ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 516687f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 516787f7e416STheodore Ts'o * writeback. 5168ac27a0ecSDave Kleikamp * 5169ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5170ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5171ac27a0ecSDave Kleikamp * which we are interested. 5172ac27a0ecSDave Kleikamp * 5173ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5174ac27a0ecSDave Kleikamp * 5175ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5176ac27a0ecSDave Kleikamp * stuff(); 5177ac27a0ecSDave Kleikamp * inode->i_size = expr; 5178ac27a0ecSDave Kleikamp * 517987f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 518087f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 518187f7e416STheodore Ts'o * superblock's dirty inode list. 5182ac27a0ecSDave Kleikamp */ 5183a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5184ac27a0ecSDave Kleikamp { 518591ac6f43SFrank Mayhar int err; 518691ac6f43SFrank Mayhar 518718f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 518818f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5189ac27a0ecSDave Kleikamp return 0; 5190ac27a0ecSDave Kleikamp 519118f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 519218f2c4fcSTheodore Ts'o return -EIO; 519318f2c4fcSTheodore Ts'o 519491ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5195617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5196b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5197ac27a0ecSDave Kleikamp dump_stack(); 5198ac27a0ecSDave Kleikamp return -EIO; 5199ac27a0ecSDave Kleikamp } 5200ac27a0ecSDave Kleikamp 520110542c22SJan Kara /* 520210542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 520310542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 520410542c22SJan Kara * written. 520510542c22SJan Kara */ 520610542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5207ac27a0ecSDave Kleikamp return 0; 5208ac27a0ecSDave Kleikamp 5209aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 521018f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 521191ac6f43SFrank Mayhar } else { 521291ac6f43SFrank Mayhar struct ext4_iloc iloc; 521391ac6f43SFrank Mayhar 52148016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 521591ac6f43SFrank Mayhar if (err) 521691ac6f43SFrank Mayhar return err; 521710542c22SJan Kara /* 521810542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 521910542c22SJan Kara * it here separately for each inode. 522010542c22SJan Kara */ 522110542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5222830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5223830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 522454d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5225c398eda0STheodore Ts'o "IO error syncing inode"); 5226830156c7SFrank Mayhar err = -EIO; 5227830156c7SFrank Mayhar } 5228fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 522991ac6f43SFrank Mayhar } 523091ac6f43SFrank Mayhar return err; 5231ac27a0ecSDave Kleikamp } 5232ac27a0ecSDave Kleikamp 5233ac27a0ecSDave Kleikamp /* 523453e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 523553e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 523653e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 523753e87268SJan Kara */ 523853e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 523953e87268SJan Kara { 524053e87268SJan Kara struct page *page; 524153e87268SJan Kara unsigned offset; 524253e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 524353e87268SJan Kara tid_t commit_tid = 0; 524453e87268SJan Kara int ret; 524553e87268SJan Kara 524609cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 524753e87268SJan Kara /* 5248565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5249565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5250565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5251565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5252565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5253565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5254565333a1Syangerkun * blocksize == PAGESIZE. 525553e87268SJan Kara */ 5256565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 525753e87268SJan Kara return; 525853e87268SJan Kara while (1) { 525953e87268SJan Kara page = find_lock_page(inode->i_mapping, 526009cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 526153e87268SJan Kara if (!page) 526253e87268SJan Kara return; 5263ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 526409cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 526553e87268SJan Kara unlock_page(page); 526609cbfeafSKirill A. Shutemov put_page(page); 526753e87268SJan Kara if (ret != -EBUSY) 526853e87268SJan Kara return; 526953e87268SJan Kara commit_tid = 0; 527053e87268SJan Kara read_lock(&journal->j_state_lock); 527153e87268SJan Kara if (journal->j_committing_transaction) 527253e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 527353e87268SJan Kara read_unlock(&journal->j_state_lock); 527453e87268SJan Kara if (commit_tid) 527553e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 527653e87268SJan Kara } 527753e87268SJan Kara } 527853e87268SJan Kara 527953e87268SJan Kara /* 5280617ba13bSMingming Cao * ext4_setattr() 5281ac27a0ecSDave Kleikamp * 5282ac27a0ecSDave Kleikamp * Called from notify_change. 5283ac27a0ecSDave Kleikamp * 5284ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5285ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5286ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5287ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5288ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5289ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5290ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5291ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5292ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5293ac27a0ecSDave Kleikamp * 5294678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5295678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5296678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5297678aaf48SJan Kara * This way we are sure that all the data written in the previous 5298678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5299678aaf48SJan Kara * writeback). 5300678aaf48SJan Kara * 5301f340b3d9Shongnanli * Called with inode->i_rwsem down. 5302ac27a0ecSDave Kleikamp */ 5303549c7297SChristian Brauner int ext4_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 5304549c7297SChristian Brauner struct iattr *attr) 5305ac27a0ecSDave Kleikamp { 53062b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5307ac27a0ecSDave Kleikamp int error, rc = 0; 53083d287de3SDmitry Monakhov int orphan = 0; 5309ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5310ac27a0ecSDave Kleikamp 53110db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 53120db1ff22STheodore Ts'o return -EIO; 53130db1ff22STheodore Ts'o 531402b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 531502b016caSTheodore Ts'o return -EPERM; 531602b016caSTheodore Ts'o 531702b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 531802b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 531902b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 532002b016caSTheodore Ts'o return -EPERM; 532102b016caSTheodore Ts'o 532214f3db55SChristian Brauner error = setattr_prepare(mnt_userns, dentry, attr); 5323ac27a0ecSDave Kleikamp if (error) 5324ac27a0ecSDave Kleikamp return error; 5325ac27a0ecSDave Kleikamp 53263ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 53273ce2b8ddSEric Biggers if (error) 53283ce2b8ddSEric Biggers return error; 53293ce2b8ddSEric Biggers 5330c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5331c93d8f88SEric Biggers if (error) 5332c93d8f88SEric Biggers return error; 5333c93d8f88SEric Biggers 5334a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5335a7cdadeeSJan Kara error = dquot_initialize(inode); 5336a7cdadeeSJan Kara if (error) 5337a7cdadeeSJan Kara return error; 5338a7cdadeeSJan Kara } 53392729cfdcSHarshad Shirwadkar 534008cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 534108cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5342ac27a0ecSDave Kleikamp handle_t *handle; 5343ac27a0ecSDave Kleikamp 5344ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5345ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53469924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53479924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5348194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5349ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5350ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5351ac27a0ecSDave Kleikamp goto err_out; 5352ac27a0ecSDave Kleikamp } 53537a9ca53aSTahsin Erdogan 53547a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53557a9ca53aSTahsin Erdogan * counts xattr inode references. 53567a9ca53aSTahsin Erdogan */ 53577a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5358b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 53597a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53607a9ca53aSTahsin Erdogan 5361ac27a0ecSDave Kleikamp if (error) { 5362617ba13bSMingming Cao ext4_journal_stop(handle); 5363ac27a0ecSDave Kleikamp return error; 5364ac27a0ecSDave Kleikamp } 5365ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5366ac27a0ecSDave Kleikamp * one transaction */ 5367ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5368ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5369ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5370ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5371617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5372617ba13bSMingming Cao ext4_journal_stop(handle); 5373512c15efSPan Bian if (unlikely(error)) { 53744209ae12SHarshad Shirwadkar return error; 5375ac27a0ecSDave Kleikamp } 5376512c15efSPan Bian } 5377ac27a0ecSDave Kleikamp 53783da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53795208386cSJan Kara handle_t *handle; 53803da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5381b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5382562c72aaSChristoph Hellwig 538312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5384e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5385e2b46574SEric Sandeen 5386aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 53870c095c7fSTheodore Ts'o return -EFBIG; 5388e2b46574SEric Sandeen } 5389aa75f4d3SHarshad Shirwadkar } 5390aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 53913da40c7bSJosef Bacik return -EINVAL; 5392aa75f4d3SHarshad Shirwadkar } 5393dff6efc3SChristoph Hellwig 5394dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5395dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5396dff6efc3SChristoph Hellwig 5397b9c1c267SJan Kara if (shrink) { 5398b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 53995208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 54005208386cSJan Kara attr->ia_size); 54015208386cSJan Kara if (error) 54025208386cSJan Kara goto err_out; 54035208386cSJan Kara } 5404b9c1c267SJan Kara /* 5405b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5406b9c1c267SJan Kara * for dio in flight. 5407b9c1c267SJan Kara */ 5408b9c1c267SJan Kara inode_dio_wait(inode); 5409b9c1c267SJan Kara } 5410b9c1c267SJan Kara 5411d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 5412b9c1c267SJan Kara 5413b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5414b9c1c267SJan Kara if (rc) { 5415d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 5416aa75f4d3SHarshad Shirwadkar goto err_out; 5417b9c1c267SJan Kara } 5418b9c1c267SJan Kara 54193da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 54209924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5421ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5422ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5423b9c1c267SJan Kara goto out_mmap_sem; 5424ac27a0ecSDave Kleikamp } 54253da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5426617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 54273d287de3SDmitry Monakhov orphan = 1; 54283d287de3SDmitry Monakhov } 5429911af577SEryu Guan /* 5430911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5431911af577SEryu Guan * update c/mtime in shrink case below 5432911af577SEryu Guan */ 5433911af577SEryu Guan if (!shrink) { 5434eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5435911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5436911af577SEryu Guan } 5437aa75f4d3SHarshad Shirwadkar 5438aa75f4d3SHarshad Shirwadkar if (shrink) 5439a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, 5440aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5441aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 54429725958bSXin Yin EXT_MAX_BLOCKS - 1); 5443aa75f4d3SHarshad Shirwadkar else 5444aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5445a80f7fcfSHarshad Shirwadkar handle, inode, 5446aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5447aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5448aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5449aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5450aa75f4d3SHarshad Shirwadkar 545190e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5452617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5453617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5454ac27a0ecSDave Kleikamp if (!error) 5455ac27a0ecSDave Kleikamp error = rc; 545690e775b7SJan Kara /* 545790e775b7SJan Kara * We have to update i_size under i_data_sem together 545890e775b7SJan Kara * with i_disksize to avoid races with writeback code 545990e775b7SJan Kara * running ext4_wb_update_i_disksize(). 546090e775b7SJan Kara */ 546190e775b7SJan Kara if (!error) 546290e775b7SJan Kara i_size_write(inode, attr->ia_size); 546390e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5464617ba13bSMingming Cao ext4_journal_stop(handle); 5465b9c1c267SJan Kara if (error) 5466b9c1c267SJan Kara goto out_mmap_sem; 546782a25b02SJan Kara if (!shrink) { 5468b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5469b9c1c267SJan Kara inode->i_size); 5470b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 547182a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5472b9c1c267SJan Kara } 5473430657b6SRoss Zwisler } 5474430657b6SRoss Zwisler 547553e87268SJan Kara /* 547653e87268SJan Kara * Truncate pagecache after we've waited for commit 547753e87268SJan Kara * in data=journal mode to make pages freeable. 547853e87268SJan Kara */ 54797caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5480b9c1c267SJan Kara /* 5481b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5482b9c1c267SJan Kara * truncate possible preallocated blocks. 5483b9c1c267SJan Kara */ 5484b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54852c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54862c98eb5eSTheodore Ts'o if (rc) 54872c98eb5eSTheodore Ts'o error = rc; 54882c98eb5eSTheodore Ts'o } 5489b9c1c267SJan Kara out_mmap_sem: 5490d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 54913da40c7bSJosef Bacik } 5492ac27a0ecSDave Kleikamp 54932c98eb5eSTheodore Ts'o if (!error) { 549414f3db55SChristian Brauner setattr_copy(mnt_userns, inode, attr); 54951025774cSChristoph Hellwig mark_inode_dirty(inode); 54961025774cSChristoph Hellwig } 54971025774cSChristoph Hellwig 54981025774cSChristoph Hellwig /* 54991025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 55001025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 55011025774cSChristoph Hellwig */ 55023d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5503617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5504ac27a0ecSDave Kleikamp 55052c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 550614f3db55SChristian Brauner rc = posix_acl_chmod(mnt_userns, inode, inode->i_mode); 5507ac27a0ecSDave Kleikamp 5508ac27a0ecSDave Kleikamp err_out: 5509aa75f4d3SHarshad Shirwadkar if (error) 5510617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5511ac27a0ecSDave Kleikamp if (!error) 5512ac27a0ecSDave Kleikamp error = rc; 5513ac27a0ecSDave Kleikamp return error; 5514ac27a0ecSDave Kleikamp } 5515ac27a0ecSDave Kleikamp 5516549c7297SChristian Brauner int ext4_getattr(struct user_namespace *mnt_userns, const struct path *path, 5517549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags) 55183e3398a0SMingming Cao { 551999652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 552099652ea5SDavid Howells struct ext4_inode *raw_inode; 552199652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 552299652ea5SDavid Howells unsigned int flags; 55233e3398a0SMingming Cao 5524d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5525d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 552699652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 552799652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 552899652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 552999652ea5SDavid Howells } 553099652ea5SDavid Howells 553199652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 553299652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 553399652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 553499652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 553599652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 553699652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 553799652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 553899652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 553999652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 554099652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 554199652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 55421f607195SEric Biggers if (flags & EXT4_VERITY_FL) 55431f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 554499652ea5SDavid Howells 55453209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 55463209f68bSDavid Howells STATX_ATTR_COMPRESSED | 55473209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 55483209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 55491f607195SEric Biggers STATX_ATTR_NODUMP | 55501f607195SEric Biggers STATX_ATTR_VERITY); 55513209f68bSDavid Howells 555214f3db55SChristian Brauner generic_fillattr(mnt_userns, inode, stat); 555399652ea5SDavid Howells return 0; 555499652ea5SDavid Howells } 555599652ea5SDavid Howells 5556549c7297SChristian Brauner int ext4_file_getattr(struct user_namespace *mnt_userns, 5557549c7297SChristian Brauner const struct path *path, struct kstat *stat, 555899652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 555999652ea5SDavid Howells { 556099652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 556199652ea5SDavid Howells u64 delalloc_blocks; 556299652ea5SDavid Howells 556314f3db55SChristian Brauner ext4_getattr(mnt_userns, path, stat, request_mask, query_flags); 55643e3398a0SMingming Cao 55653e3398a0SMingming Cao /* 55669206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 55679206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 55689206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5569d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 55709206c561SAndreas Dilger */ 55719206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 55729206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 55739206c561SAndreas Dilger 55749206c561SAndreas Dilger /* 55753e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 55763e3398a0SMingming Cao * otherwise in the case of system crash before the real block 55773e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 55783e3398a0SMingming Cao * on-disk file blocks. 55793e3398a0SMingming Cao * We always keep i_blocks updated together with real 55803e3398a0SMingming Cao * allocation. But to not confuse with user, stat 55813e3398a0SMingming Cao * will return the blocks that include the delayed allocation 55823e3398a0SMingming Cao * blocks for this file. 55833e3398a0SMingming Cao */ 558496607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 558596607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55868af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55873e3398a0SMingming Cao return 0; 55883e3398a0SMingming Cao } 5589ac27a0ecSDave Kleikamp 5590fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5591fffb2739SJan Kara int pextents) 5592a02908f1SMingming Cao { 559312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5594fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5595fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5596a02908f1SMingming Cao } 5597ac51d837STheodore Ts'o 5598a02908f1SMingming Cao /* 5599a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5600a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5601a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5602a02908f1SMingming Cao * 5603a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 56044907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5605a02908f1SMingming Cao * they could still across block group boundary. 5606a02908f1SMingming Cao * 5607a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5608a02908f1SMingming Cao */ 5609dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5610fffb2739SJan Kara int pextents) 5611a02908f1SMingming Cao { 56128df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56138df9675fSTheodore Ts'o int gdpblocks; 5614a02908f1SMingming Cao int idxblocks; 5615a02908f1SMingming Cao int ret = 0; 5616a02908f1SMingming Cao 5617a02908f1SMingming Cao /* 5618fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5619fffb2739SJan Kara * to @pextents physical extents? 5620a02908f1SMingming Cao */ 5621fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5622a02908f1SMingming Cao 5623a02908f1SMingming Cao ret = idxblocks; 5624a02908f1SMingming Cao 5625a02908f1SMingming Cao /* 5626a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5627a02908f1SMingming Cao * to account 5628a02908f1SMingming Cao */ 5629fffb2739SJan Kara groups = idxblocks + pextents; 5630a02908f1SMingming Cao gdpblocks = groups; 56318df9675fSTheodore Ts'o if (groups > ngroups) 56328df9675fSTheodore Ts'o groups = ngroups; 5633a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5634a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5635a02908f1SMingming Cao 5636a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5637a02908f1SMingming Cao ret += groups + gdpblocks; 5638a02908f1SMingming Cao 5639a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5640a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5641ac27a0ecSDave Kleikamp 5642ac27a0ecSDave Kleikamp return ret; 5643ac27a0ecSDave Kleikamp } 5644ac27a0ecSDave Kleikamp 5645ac27a0ecSDave Kleikamp /* 564625985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5647f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5648f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5649a02908f1SMingming Cao * 5650525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5651a02908f1SMingming Cao * 5652525f4ed8SMingming Cao * We need to consider the worse case, when 5653a02908f1SMingming Cao * one new block per extent. 5654a02908f1SMingming Cao */ 5655a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5656a02908f1SMingming Cao { 5657a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5658a02908f1SMingming Cao int ret; 5659a02908f1SMingming Cao 5660fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5661a02908f1SMingming Cao 5662a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5663a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5664a02908f1SMingming Cao ret += bpp; 5665a02908f1SMingming Cao return ret; 5666a02908f1SMingming Cao } 5667f3bd1f3fSMingming Cao 5668f3bd1f3fSMingming Cao /* 5669f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5670f3bd1f3fSMingming Cao * 5671f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 567279e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5673f3bd1f3fSMingming Cao * 5674f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5675f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5676f3bd1f3fSMingming Cao */ 5677f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5678f3bd1f3fSMingming Cao { 5679f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5680f3bd1f3fSMingming Cao } 5681f3bd1f3fSMingming Cao 5682a02908f1SMingming Cao /* 5683617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5684ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5685ac27a0ecSDave Kleikamp */ 5686617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5687617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5688ac27a0ecSDave Kleikamp { 5689ac27a0ecSDave Kleikamp int err = 0; 5690ac27a0ecSDave Kleikamp 5691a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5692a6758309SVasily Averin put_bh(iloc->bh); 56930db1ff22STheodore Ts'o return -EIO; 5694a6758309SVasily Averin } 5695a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5696aa75f4d3SHarshad Shirwadkar 5697c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 569825ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 569925ec56b5SJean Noel Cordenner 5700ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5701ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5702ac27a0ecSDave Kleikamp 5703dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5704830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5705ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5706ac27a0ecSDave Kleikamp return err; 5707ac27a0ecSDave Kleikamp } 5708ac27a0ecSDave Kleikamp 5709ac27a0ecSDave Kleikamp /* 5710ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5711ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5712ac27a0ecSDave Kleikamp */ 5713ac27a0ecSDave Kleikamp 5714ac27a0ecSDave Kleikamp int 5715617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5716617ba13bSMingming Cao struct ext4_iloc *iloc) 5717ac27a0ecSDave Kleikamp { 57180390131bSFrank Mayhar int err; 57190390131bSFrank Mayhar 57200db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 57210db1ff22STheodore Ts'o return -EIO; 57220db1ff22STheodore Ts'o 5723617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5724ac27a0ecSDave Kleikamp if (!err) { 5725ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5726188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, 5727188c299eSJan Kara iloc->bh, EXT4_JTR_NONE); 5728ac27a0ecSDave Kleikamp if (err) { 5729ac27a0ecSDave Kleikamp brelse(iloc->bh); 5730ac27a0ecSDave Kleikamp iloc->bh = NULL; 5731ac27a0ecSDave Kleikamp } 5732ac27a0ecSDave Kleikamp } 5733617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5734ac27a0ecSDave Kleikamp return err; 5735ac27a0ecSDave Kleikamp } 5736ac27a0ecSDave Kleikamp 5737c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5738c03b45b8SMiao Xie unsigned int new_extra_isize, 5739c03b45b8SMiao Xie struct ext4_iloc *iloc, 5740c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5741c03b45b8SMiao Xie { 5742c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5743c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 57444ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 57454ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5746c03b45b8SMiao Xie int error; 5747c03b45b8SMiao Xie 57484ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 57494ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 57504ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 57514ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 57524ea99936STheodore Ts'o ei->i_extra_isize, 57534ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 57544ea99936STheodore Ts'o return -EFSCORRUPTED; 57554ea99936STheodore Ts'o } 57564ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 57574ea99936STheodore Ts'o (new_extra_isize < 4) || 57584ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 57594ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 57604ea99936STheodore Ts'o 5761c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5762c03b45b8SMiao Xie 5763c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5764c03b45b8SMiao Xie 5765c03b45b8SMiao Xie /* No extended attributes present */ 5766c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5767c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5768c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5769c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5770c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5771c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5772c03b45b8SMiao Xie return 0; 5773c03b45b8SMiao Xie } 5774c03b45b8SMiao Xie 5775c03b45b8SMiao Xie /* try to expand with EAs present */ 5776c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5777c03b45b8SMiao Xie raw_inode, handle); 5778c03b45b8SMiao Xie if (error) { 5779c03b45b8SMiao Xie /* 5780c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5781c03b45b8SMiao Xie */ 5782c03b45b8SMiao Xie *no_expand = 1; 5783c03b45b8SMiao Xie } 5784c03b45b8SMiao Xie 5785c03b45b8SMiao Xie return error; 5786c03b45b8SMiao Xie } 5787c03b45b8SMiao Xie 5788ac27a0ecSDave Kleikamp /* 57896dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57906dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 57916dd4ee7cSKalpak Shah */ 5792cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 57931d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 57941d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 57951d03ec98SAneesh Kumar K.V handle_t *handle) 57966dd4ee7cSKalpak Shah { 57973b10fdc6SMiao Xie int no_expand; 57983b10fdc6SMiao Xie int error; 57996dd4ee7cSKalpak Shah 5800cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5801cf0a5e81SMiao Xie return -EOVERFLOW; 5802cf0a5e81SMiao Xie 5803cf0a5e81SMiao Xie /* 5804cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5805cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5806cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5807cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5808cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5809cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5810cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5811cf0a5e81SMiao Xie */ 58126cb367c2SJan Kara if (ext4_journal_extend(handle, 581383448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5814cf0a5e81SMiao Xie return -ENOSPC; 58156dd4ee7cSKalpak Shah 58163b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5817cf0a5e81SMiao Xie return -EBUSY; 58183b10fdc6SMiao Xie 5819c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5820c03b45b8SMiao Xie handle, &no_expand); 58213b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5822c03b45b8SMiao Xie 5823c03b45b8SMiao Xie return error; 58246dd4ee7cSKalpak Shah } 58256dd4ee7cSKalpak Shah 5826c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5827c03b45b8SMiao Xie unsigned int new_extra_isize, 5828c03b45b8SMiao Xie struct ext4_iloc *iloc) 5829c03b45b8SMiao Xie { 5830c03b45b8SMiao Xie handle_t *handle; 5831c03b45b8SMiao Xie int no_expand; 5832c03b45b8SMiao Xie int error, rc; 5833c03b45b8SMiao Xie 5834c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5835c03b45b8SMiao Xie brelse(iloc->bh); 5836c03b45b8SMiao Xie return -EOVERFLOW; 5837c03b45b8SMiao Xie } 5838c03b45b8SMiao Xie 5839c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5840c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5841c03b45b8SMiao Xie if (IS_ERR(handle)) { 5842c03b45b8SMiao Xie error = PTR_ERR(handle); 5843c03b45b8SMiao Xie brelse(iloc->bh); 5844c03b45b8SMiao Xie return error; 5845c03b45b8SMiao Xie } 5846c03b45b8SMiao Xie 5847c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5848c03b45b8SMiao Xie 5849ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5850188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, 5851188c299eSJan Kara EXT4_JTR_NONE); 58523b10fdc6SMiao Xie if (error) { 5853c03b45b8SMiao Xie brelse(iloc->bh); 58547f420d64SDan Carpenter goto out_unlock; 58553b10fdc6SMiao Xie } 5856cf0a5e81SMiao Xie 5857c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5858c03b45b8SMiao Xie handle, &no_expand); 5859c03b45b8SMiao Xie 5860c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5861c03b45b8SMiao Xie if (!error) 5862c03b45b8SMiao Xie error = rc; 5863c03b45b8SMiao Xie 58647f420d64SDan Carpenter out_unlock: 5865c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5866c03b45b8SMiao Xie ext4_journal_stop(handle); 58673b10fdc6SMiao Xie return error; 58686dd4ee7cSKalpak Shah } 58696dd4ee7cSKalpak Shah 58706dd4ee7cSKalpak Shah /* 5871ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5872ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5873ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5874ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5875ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5876ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5877ac27a0ecSDave Kleikamp * 5878ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5879ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5880ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5881ac27a0ecSDave Kleikamp * we start and wait on commits. 5882ac27a0ecSDave Kleikamp */ 58834209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 58844209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5885ac27a0ecSDave Kleikamp { 5886617ba13bSMingming Cao struct ext4_iloc iloc; 58876dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5888cf0a5e81SMiao Xie int err; 5889ac27a0ecSDave Kleikamp 5890ac27a0ecSDave Kleikamp might_sleep(); 58917ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5892617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58935e1021f2SEryu Guan if (err) 58944209ae12SHarshad Shirwadkar goto out; 5895cf0a5e81SMiao Xie 5896cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5897cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 58986dd4ee7cSKalpak Shah iloc, handle); 5899cf0a5e81SMiao Xie 59004209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 59014209ae12SHarshad Shirwadkar out: 59024209ae12SHarshad Shirwadkar if (unlikely(err)) 59034209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 59044209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 59054209ae12SHarshad Shirwadkar return err; 5906ac27a0ecSDave Kleikamp } 5907ac27a0ecSDave Kleikamp 5908ac27a0ecSDave Kleikamp /* 5909617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5910ac27a0ecSDave Kleikamp * 5911ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5912ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5913ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5914ac27a0ecSDave Kleikamp * 59155dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5916ac27a0ecSDave Kleikamp * are allocated to the file. 5917ac27a0ecSDave Kleikamp * 5918ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5919ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5920ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5921ac27a0ecSDave Kleikamp */ 5922aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5923ac27a0ecSDave Kleikamp { 5924ac27a0ecSDave Kleikamp handle_t *handle; 5925ac27a0ecSDave Kleikamp 59269924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5927ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5928ac27a0ecSDave Kleikamp return; 5929e2728c56SEric Biggers ext4_mark_inode_dirty(handle, inode); 5930e2728c56SEric Biggers ext4_journal_stop(handle); 5931ac27a0ecSDave Kleikamp } 5932ac27a0ecSDave Kleikamp 5933617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5934ac27a0ecSDave Kleikamp { 5935ac27a0ecSDave Kleikamp journal_t *journal; 5936ac27a0ecSDave Kleikamp handle_t *handle; 5937ac27a0ecSDave Kleikamp int err; 5938c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5939ac27a0ecSDave Kleikamp 5940ac27a0ecSDave Kleikamp /* 5941ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5942ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5943ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5944ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5945ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5946ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5947ac27a0ecSDave Kleikamp * nobody is changing anything. 5948ac27a0ecSDave Kleikamp */ 5949ac27a0ecSDave Kleikamp 5950617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59510390131bSFrank Mayhar if (!journal) 59520390131bSFrank Mayhar return 0; 5953d699594dSDave Hansen if (is_journal_aborted(journal)) 5954ac27a0ecSDave Kleikamp return -EROFS; 5955ac27a0ecSDave Kleikamp 595617335dccSDmitry Monakhov /* Wait for all existing dio workers */ 595717335dccSDmitry Monakhov inode_dio_wait(inode); 595817335dccSDmitry Monakhov 59594c546592SDaeho Jeong /* 59604c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 59614c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 59624c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 59634c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 59644c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 59654c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 59664c546592SDaeho Jeong */ 59674c546592SDaeho Jeong if (val) { 5968d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 59694c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 59704c546592SDaeho Jeong if (err < 0) { 5971d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 59724c546592SDaeho Jeong return err; 59734c546592SDaeho Jeong } 59744c546592SDaeho Jeong } 59754c546592SDaeho Jeong 5976bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 5977dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5978ac27a0ecSDave Kleikamp 5979ac27a0ecSDave Kleikamp /* 5980ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5981ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5982ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5983ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5984ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5985ac27a0ecSDave Kleikamp */ 5986ac27a0ecSDave Kleikamp 5987ac27a0ecSDave Kleikamp if (val) 598812e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59895872ddaaSYongqiang Yang else { 599001d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 59914f879ca6SJan Kara if (err < 0) { 59924f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 5993bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 59944f879ca6SJan Kara return err; 59954f879ca6SJan Kara } 599612e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59975872ddaaSYongqiang Yang } 5998617ba13bSMingming Cao ext4_set_aops(inode); 5999ac27a0ecSDave Kleikamp 6000dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 6001bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 6002c8585c6fSDaeho Jeong 60034c546592SDaeho Jeong if (val) 6004d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 6005ac27a0ecSDave Kleikamp 6006ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6007ac27a0ecSDave Kleikamp 60089924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 6009ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6010ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6011ac27a0ecSDave Kleikamp 6012aa75f4d3SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, 6013e85c81baSXin Yin EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, handle); 6014617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60150390131bSFrank Mayhar ext4_handle_sync(handle); 6016617ba13bSMingming Cao ext4_journal_stop(handle); 6017617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6018ac27a0ecSDave Kleikamp 6019ac27a0ecSDave Kleikamp return err; 6020ac27a0ecSDave Kleikamp } 60212e9ee850SAneesh Kumar K.V 6022188c299eSJan Kara static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, 6023188c299eSJan Kara struct buffer_head *bh) 60242e9ee850SAneesh Kumar K.V { 60252e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60262e9ee850SAneesh Kumar K.V } 60272e9ee850SAneesh Kumar K.V 6028401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 60292e9ee850SAneesh Kumar K.V { 603011bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 6031c2ec175cSNick Piggin struct page *page = vmf->page; 60322e9ee850SAneesh Kumar K.V loff_t size; 60332e9ee850SAneesh Kumar K.V unsigned long len; 6034401b25aaSSouptick Joarder int err; 6035401b25aaSSouptick Joarder vm_fault_t ret; 60362e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6037496ad9aaSAl Viro struct inode *inode = file_inode(file); 60382e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60399ea7df53SJan Kara handle_t *handle; 60409ea7df53SJan Kara get_block_t *get_block; 60419ea7df53SJan Kara int retries = 0; 60422e9ee850SAneesh Kumar K.V 604302b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 604402b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 604502b016caSTheodore Ts'o 60468e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6047041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6048ea3d7209SJan Kara 6049d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping); 60507b4cc978SEric Biggers 6051401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6052401b25aaSSouptick Joarder if (err) 60537b4cc978SEric Biggers goto out_ret; 60547b4cc978SEric Biggers 605564a9f144SMauricio Faria de Oliveira /* 605664a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 605764a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 605864a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 605964a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 606064a9f144SMauricio Faria de Oliveira */ 606164a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 606264a9f144SMauricio Faria de Oliveira goto retry_alloc; 606364a9f144SMauricio Faria de Oliveira 60649ea7df53SJan Kara /* Delalloc case is easy... */ 60659ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 60669ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 60679ea7df53SJan Kara do { 6068401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 60699ea7df53SJan Kara ext4_da_get_block_prep); 6070401b25aaSSouptick Joarder } while (err == -ENOSPC && 60719ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 60729ea7df53SJan Kara goto out_ret; 60732e9ee850SAneesh Kumar K.V } 60740e499890SDarrick J. Wong 60750e499890SDarrick J. Wong lock_page(page); 60769ea7df53SJan Kara size = i_size_read(inode); 60779ea7df53SJan Kara /* Page got truncated from under us? */ 60789ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 60799ea7df53SJan Kara unlock_page(page); 60809ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 60819ea7df53SJan Kara goto out; 60820e499890SDarrick J. Wong } 60832e9ee850SAneesh Kumar K.V 608409cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 608509cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 60862e9ee850SAneesh Kumar K.V else 608709cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6088a827eaffSAneesh Kumar K.V /* 60899ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 60909ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 609164a9f144SMauricio Faria de Oliveira * 609264a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 609364a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6094a827eaffSAneesh Kumar K.V */ 60952e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6096188c299eSJan Kara if (!ext4_walk_page_buffers(NULL, inode, page_buffers(page), 6097f19d5870STao Ma 0, len, NULL, 6098a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 60999ea7df53SJan Kara /* Wait so that we don't change page under IO */ 61001d1d1a76SDarrick J. Wong wait_for_stable_page(page); 61019ea7df53SJan Kara ret = VM_FAULT_LOCKED; 61029ea7df53SJan Kara goto out; 61032e9ee850SAneesh Kumar K.V } 6104a827eaffSAneesh Kumar K.V } 6105a827eaffSAneesh Kumar K.V unlock_page(page); 61069ea7df53SJan Kara /* OK, we need to fill the hole... */ 61079ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6108705965bdSJan Kara get_block = ext4_get_block_unwritten; 61099ea7df53SJan Kara else 61109ea7df53SJan Kara get_block = ext4_get_block; 61119ea7df53SJan Kara retry_alloc: 61129924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 61139924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 61149ea7df53SJan Kara if (IS_ERR(handle)) { 6115c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 61169ea7df53SJan Kara goto out; 61179ea7df53SJan Kara } 611864a9f144SMauricio Faria de Oliveira /* 611964a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 612064a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 612164a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 612264a9f144SMauricio Faria de Oliveira */ 612364a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6124401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 612564a9f144SMauricio Faria de Oliveira } else { 612664a9f144SMauricio Faria de Oliveira lock_page(page); 612764a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 612864a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 612964a9f144SMauricio Faria de Oliveira if (page->mapping != mapping || page_offset(page) > size) { 613064a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6131afb585a9SMauricio Faria de Oliveira goto out_error; 613264a9f144SMauricio Faria de Oliveira } 613364a9f144SMauricio Faria de Oliveira 613464a9f144SMauricio Faria de Oliveira if (page->index == size >> PAGE_SHIFT) 613564a9f144SMauricio Faria de Oliveira len = size & ~PAGE_MASK; 613664a9f144SMauricio Faria de Oliveira else 613764a9f144SMauricio Faria de Oliveira len = PAGE_SIZE; 613864a9f144SMauricio Faria de Oliveira 613964a9f144SMauricio Faria de Oliveira err = __block_write_begin(page, 0, len, ext4_get_block); 614064a9f144SMauricio Faria de Oliveira if (!err) { 61419ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6142188c299eSJan Kara if (ext4_walk_page_buffers(handle, inode, 6143188c299eSJan Kara page_buffers(page), 0, len, NULL, 6144188c299eSJan Kara do_journal_get_write_access)) 6145afb585a9SMauricio Faria de Oliveira goto out_error; 6146188c299eSJan Kara if (ext4_walk_page_buffers(handle, inode, 6147188c299eSJan Kara page_buffers(page), 0, len, NULL, 6148188c299eSJan Kara write_end_fn)) 6149afb585a9SMauricio Faria de Oliveira goto out_error; 6150b5b18160SJan Kara if (ext4_jbd2_inode_add_write(handle, inode, 6151b5b18160SJan Kara page_offset(page), len)) 6152afb585a9SMauricio Faria de Oliveira goto out_error; 61539ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 615464a9f144SMauricio Faria de Oliveira } else { 615564a9f144SMauricio Faria de Oliveira unlock_page(page); 615664a9f144SMauricio Faria de Oliveira } 61579ea7df53SJan Kara } 61589ea7df53SJan Kara ext4_journal_stop(handle); 6159401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 61609ea7df53SJan Kara goto retry_alloc; 61619ea7df53SJan Kara out_ret: 6162401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 61639ea7df53SJan Kara out: 6164d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping); 61658e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 61662e9ee850SAneesh Kumar K.V return ret; 6167afb585a9SMauricio Faria de Oliveira out_error: 6168afb585a9SMauricio Faria de Oliveira unlock_page(page); 6169afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6170afb585a9SMauricio Faria de Oliveira goto out; 61712e9ee850SAneesh Kumar K.V } 6172