1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2ac27a0ecSDave Kleikamp /* 3617ba13bSMingming Cao * linux/fs/ext4/inode.c 4ac27a0ecSDave Kleikamp * 5ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 6ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 7ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 8ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 9ac27a0ecSDave Kleikamp * 10ac27a0ecSDave Kleikamp * from 11ac27a0ecSDave Kleikamp * 12ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 13ac27a0ecSDave Kleikamp * 14ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 15ac27a0ecSDave Kleikamp * 16ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek 17ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 18ac27a0ecSDave Kleikamp * 19617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 20ac27a0ecSDave Kleikamp */ 21ac27a0ecSDave Kleikamp 22ac27a0ecSDave Kleikamp #include <linux/fs.h> 2314f3db55SChristian Brauner #include <linux/mount.h> 24ac27a0ecSDave Kleikamp #include <linux/time.h> 25ac27a0ecSDave Kleikamp #include <linux/highuid.h> 26ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 27c94c2acfSMatthew Wilcox #include <linux/dax.h> 28ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 29ac27a0ecSDave Kleikamp #include <linux/string.h> 30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 31ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3264769240SAlex Tomas #include <linux/pagevec.h> 33ac27a0ecSDave Kleikamp #include <linux/mpage.h> 34e83c1397SDuane Griffin #include <linux/namei.h> 35ac27a0ecSDave Kleikamp #include <linux/uio.h> 36ac27a0ecSDave Kleikamp #include <linux/bio.h> 374c0425ffSMingming Cao #include <linux/workqueue.h> 38744692dcSJiaying Zhang #include <linux/kernel.h> 396db26ffcSAndrew Morton #include <linux/printk.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 4100a1a053STheodore Ts'o #include <linux/bitops.h> 42364443cbSJan Kara #include <linux/iomap.h> 43ae5e165dSJeff Layton #include <linux/iversion.h> 449bffad1eSTheodore Ts'o 453dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 46ac27a0ecSDave Kleikamp #include "xattr.h" 47ac27a0ecSDave Kleikamp #include "acl.h" 489f125d64STheodore Ts'o #include "truncate.h" 49ac27a0ecSDave Kleikamp 509bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 519bffad1eSTheodore Ts'o 52814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 53814525f4SDarrick J. Wong struct ext4_inode_info *ei) 54814525f4SDarrick J. Wong { 55814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 56814525f4SDarrick J. Wong __u32 csum; 57b47820edSDaeho Jeong __u16 dummy_csum = 0; 58b47820edSDaeho Jeong int offset = offsetof(struct ext4_inode, i_checksum_lo); 59b47820edSDaeho Jeong unsigned int csum_size = sizeof(dummy_csum); 60814525f4SDarrick J. Wong 61b47820edSDaeho Jeong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); 62b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); 63b47820edSDaeho Jeong offset += csum_size; 64b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 65b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE - offset); 66b47820edSDaeho Jeong 67b47820edSDaeho Jeong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 68b47820edSDaeho Jeong offset = offsetof(struct ext4_inode, i_checksum_hi); 69b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + 70b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE, 71b47820edSDaeho Jeong offset - EXT4_GOOD_OLD_INODE_SIZE); 72b47820edSDaeho Jeong if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 73b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, 74b47820edSDaeho Jeong csum_size); 75b47820edSDaeho Jeong offset += csum_size; 76814525f4SDarrick J. Wong } 7705ac5aa1SDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 7805ac5aa1SDaeho Jeong EXT4_INODE_SIZE(inode->i_sb) - offset); 79b47820edSDaeho Jeong } 80814525f4SDarrick J. Wong 81814525f4SDarrick J. Wong return csum; 82814525f4SDarrick J. Wong } 83814525f4SDarrick J. Wong 84814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 85814525f4SDarrick J. Wong struct ext4_inode_info *ei) 86814525f4SDarrick J. Wong { 87814525f4SDarrick J. Wong __u32 provided, calculated; 88814525f4SDarrick J. Wong 89814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 90814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 919aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 92814525f4SDarrick J. Wong return 1; 93814525f4SDarrick J. Wong 94814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 95814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 96814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 97814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 98814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 99814525f4SDarrick J. Wong else 100814525f4SDarrick J. Wong calculated &= 0xFFFF; 101814525f4SDarrick J. Wong 102814525f4SDarrick J. Wong return provided == calculated; 103814525f4SDarrick J. Wong } 104814525f4SDarrick J. Wong 1058016e29fSHarshad Shirwadkar void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 106814525f4SDarrick J. Wong struct ext4_inode_info *ei) 107814525f4SDarrick J. Wong { 108814525f4SDarrick J. Wong __u32 csum; 109814525f4SDarrick J. Wong 110814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 111814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 1129aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 113814525f4SDarrick J. Wong return; 114814525f4SDarrick J. Wong 115814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 116814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 117814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 118814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 119814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 120814525f4SDarrick J. Wong } 121814525f4SDarrick J. Wong 122678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 123678aaf48SJan Kara loff_t new_size) 124678aaf48SJan Kara { 1257ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1268aefcd55STheodore Ts'o /* 1278aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1288aefcd55STheodore Ts'o * writing, so there's no need to call 1298aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1308aefcd55STheodore Ts'o * outstanding writes we need to flush. 1318aefcd55STheodore Ts'o */ 1328aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1338aefcd55STheodore Ts'o return 0; 1348aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1358aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 136678aaf48SJan Kara new_size); 137678aaf48SJan Kara } 138678aaf48SJan Kara 139d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 140d47992f8SLukas Czerner unsigned int length); 141cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 142cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 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)) 341b21ebf14SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM); 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 } 745a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 746aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 7476fd058f7STheodore Ts'o } 748ec8c60beSRitesh Harjani 749ec8c60beSRitesh Harjani if (retval < 0) 75070aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7510e855ac8SAneesh Kumar K.V return retval; 7520e855ac8SAneesh Kumar K.V } 7530e855ac8SAneesh Kumar K.V 754ed8ad838SJan Kara /* 755ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 756ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 757ed8ad838SJan Kara */ 758ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 759ed8ad838SJan Kara { 760ed8ad838SJan Kara unsigned long old_state; 761ed8ad838SJan Kara unsigned long new_state; 762ed8ad838SJan Kara 763ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 764ed8ad838SJan Kara 765ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 766ed8ad838SJan Kara if (!bh->b_page) { 767ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 768ed8ad838SJan Kara return; 769ed8ad838SJan Kara } 770ed8ad838SJan Kara /* 771ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 772ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 773ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 774ed8ad838SJan Kara */ 775ed8ad838SJan Kara do { 776ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 777ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 778ed8ad838SJan Kara } while (unlikely( 779ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 780ed8ad838SJan Kara } 781ed8ad838SJan Kara 7822ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7832ed88685STheodore Ts'o struct buffer_head *bh, int flags) 784ac27a0ecSDave Kleikamp { 7852ed88685STheodore Ts'o struct ext4_map_blocks map; 786efe70c29SJan Kara int ret = 0; 787ac27a0ecSDave Kleikamp 78846c7f254STao Ma if (ext4_has_inline_data(inode)) 78946c7f254STao Ma return -ERANGE; 79046c7f254STao Ma 7912ed88685STheodore Ts'o map.m_lblk = iblock; 7922ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7932ed88685STheodore Ts'o 794efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 795efe70c29SJan Kara flags); 796ac27a0ecSDave Kleikamp if (ret > 0) { 7972ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 798ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7992ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 800ac27a0ecSDave Kleikamp ret = 0; 801547edce3SRoss Zwisler } else if (ret == 0) { 802547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 803547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 804ac27a0ecSDave Kleikamp } 805ac27a0ecSDave Kleikamp return ret; 806ac27a0ecSDave Kleikamp } 807ac27a0ecSDave Kleikamp 8082ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8092ed88685STheodore Ts'o struct buffer_head *bh, int create) 8102ed88685STheodore Ts'o { 8112ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8122ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8132ed88685STheodore Ts'o } 8142ed88685STheodore Ts'o 815ac27a0ecSDave Kleikamp /* 816705965bdSJan Kara * Get block function used when preparing for buffered write if we require 817705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 818705965bdSJan Kara * will be converted to written after the IO is complete. 819705965bdSJan Kara */ 820705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 821705965bdSJan Kara struct buffer_head *bh_result, int create) 822705965bdSJan Kara { 823705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 824705965bdSJan Kara inode->i_ino, create); 825705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 826705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 827705965bdSJan Kara } 828705965bdSJan Kara 829efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 830efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 831efe70c29SJan Kara 832e84dfbe2SJan Kara /* 833ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 834ac27a0ecSDave Kleikamp */ 835617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 836c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 837ac27a0ecSDave Kleikamp { 8382ed88685STheodore Ts'o struct ext4_map_blocks map; 8392ed88685STheodore Ts'o struct buffer_head *bh; 840c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 84110560082STheodore Ts'o int err; 842ac27a0ecSDave Kleikamp 843837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8448016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 845ac27a0ecSDave Kleikamp 8462ed88685STheodore Ts'o map.m_lblk = block; 8472ed88685STheodore Ts'o map.m_len = 1; 848c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8492ed88685STheodore Ts'o 85010560082STheodore Ts'o if (err == 0) 85110560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8522ed88685STheodore Ts'o if (err < 0) 85310560082STheodore Ts'o return ERR_PTR(err); 8542ed88685STheodore Ts'o 8552ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 85610560082STheodore Ts'o if (unlikely(!bh)) 85710560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8582ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 859837c23fbSChunguang Xu ASSERT(create != 0); 860837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8618016e29fSHarshad Shirwadkar || (handle != NULL)); 862ac27a0ecSDave Kleikamp 863ac27a0ecSDave Kleikamp /* 864ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 865ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 866ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 867617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 868ac27a0ecSDave Kleikamp * problem. 869ac27a0ecSDave Kleikamp */ 870ac27a0ecSDave Kleikamp lock_buffer(bh); 871ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 87210560082STheodore Ts'o err = ext4_journal_get_create_access(handle, bh); 87310560082STheodore Ts'o if (unlikely(err)) { 87410560082STheodore Ts'o unlock_buffer(bh); 87510560082STheodore Ts'o goto errout; 87610560082STheodore Ts'o } 87710560082STheodore Ts'o if (!buffer_uptodate(bh)) { 878ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 879ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 880ac27a0ecSDave Kleikamp } 881ac27a0ecSDave Kleikamp unlock_buffer(bh); 8820390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8830390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 88410560082STheodore Ts'o if (unlikely(err)) 88510560082STheodore Ts'o goto errout; 88610560082STheodore Ts'o } else 887ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 888ac27a0ecSDave Kleikamp return bh; 88910560082STheodore Ts'o errout: 89010560082STheodore Ts'o brelse(bh); 89110560082STheodore Ts'o return ERR_PTR(err); 892ac27a0ecSDave Kleikamp } 893ac27a0ecSDave Kleikamp 894617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 895c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 896ac27a0ecSDave Kleikamp { 897ac27a0ecSDave Kleikamp struct buffer_head *bh; 8982d069c08Szhangyi (F) int ret; 899ac27a0ecSDave Kleikamp 900c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 9011c215028STheodore Ts'o if (IS_ERR(bh)) 902ac27a0ecSDave Kleikamp return bh; 9037963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 904ac27a0ecSDave Kleikamp return bh; 9052d069c08Szhangyi (F) 9062d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 9072d069c08Szhangyi (F) if (ret) { 908ac27a0ecSDave Kleikamp put_bh(bh); 9092d069c08Szhangyi (F) return ERR_PTR(ret); 9102d069c08Szhangyi (F) } 9112d069c08Szhangyi (F) return bh; 912ac27a0ecSDave Kleikamp } 913ac27a0ecSDave Kleikamp 9149699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 9159699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 9169699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 9179699d4f9STahsin Erdogan { 9189699d4f9STahsin Erdogan int i, err; 9199699d4f9STahsin Erdogan 9209699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9219699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9229699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9239699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9249699d4f9STahsin Erdogan bh_count = i; 9259699d4f9STahsin Erdogan goto out_brelse; 9269699d4f9STahsin Erdogan } 9279699d4f9STahsin Erdogan } 9289699d4f9STahsin Erdogan 9299699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9309699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9312d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9322d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9339699d4f9STahsin Erdogan 9349699d4f9STahsin Erdogan if (!wait) 9359699d4f9STahsin Erdogan return 0; 9369699d4f9STahsin Erdogan 9379699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9389699d4f9STahsin Erdogan if (bhs[i]) 9399699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9409699d4f9STahsin Erdogan 9419699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9429699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9439699d4f9STahsin Erdogan err = -EIO; 9449699d4f9STahsin Erdogan goto out_brelse; 9459699d4f9STahsin Erdogan } 9469699d4f9STahsin Erdogan } 9479699d4f9STahsin Erdogan return 0; 9489699d4f9STahsin Erdogan 9499699d4f9STahsin Erdogan out_brelse: 9509699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9519699d4f9STahsin Erdogan brelse(bhs[i]); 9529699d4f9STahsin Erdogan bhs[i] = NULL; 9539699d4f9STahsin Erdogan } 9549699d4f9STahsin Erdogan return err; 9559699d4f9STahsin Erdogan } 9569699d4f9STahsin Erdogan 957f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 958ac27a0ecSDave Kleikamp struct buffer_head *head, 959ac27a0ecSDave Kleikamp unsigned from, 960ac27a0ecSDave Kleikamp unsigned to, 961ac27a0ecSDave Kleikamp int *partial, 962ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 963ac27a0ecSDave Kleikamp struct buffer_head *bh)) 964ac27a0ecSDave Kleikamp { 965ac27a0ecSDave Kleikamp struct buffer_head *bh; 966ac27a0ecSDave Kleikamp unsigned block_start, block_end; 967ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 968ac27a0ecSDave Kleikamp int err, ret = 0; 969ac27a0ecSDave Kleikamp struct buffer_head *next; 970ac27a0ecSDave Kleikamp 971ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 972ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 973de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 974ac27a0ecSDave Kleikamp next = bh->b_this_page; 975ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 976ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 977ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 978ac27a0ecSDave Kleikamp *partial = 1; 979ac27a0ecSDave Kleikamp continue; 980ac27a0ecSDave Kleikamp } 981ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 982ac27a0ecSDave Kleikamp if (!ret) 983ac27a0ecSDave Kleikamp ret = err; 984ac27a0ecSDave Kleikamp } 985ac27a0ecSDave Kleikamp return ret; 986ac27a0ecSDave Kleikamp } 987ac27a0ecSDave Kleikamp 988ac27a0ecSDave Kleikamp /* 989ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 990ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 991617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 992dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 993ac27a0ecSDave Kleikamp * prepare_write() is the right place. 994ac27a0ecSDave Kleikamp * 99536ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 99636ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 99736ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 99836ade451SJan Kara * because the caller may be PF_MEMALLOC. 999ac27a0ecSDave Kleikamp * 1000617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1001ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1002ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1003ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1004ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1005ac27a0ecSDave Kleikamp * violation. 1006ac27a0ecSDave Kleikamp * 1007dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1008ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1009ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1010ac27a0ecSDave Kleikamp * write. 1011ac27a0ecSDave Kleikamp */ 1012f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 1013ac27a0ecSDave Kleikamp struct buffer_head *bh) 1014ac27a0ecSDave Kleikamp { 101556d35a4cSJan Kara int dirty = buffer_dirty(bh); 101656d35a4cSJan Kara int ret; 101756d35a4cSJan Kara 1018ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1019ac27a0ecSDave Kleikamp return 0; 102056d35a4cSJan Kara /* 1021ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 102256d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 102356d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1024ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 102556d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 102656d35a4cSJan Kara * ever write the buffer. 102756d35a4cSJan Kara */ 102856d35a4cSJan Kara if (dirty) 102956d35a4cSJan Kara clear_buffer_dirty(bh); 10305d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 103156d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 103256d35a4cSJan Kara if (!ret && dirty) 103356d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 103456d35a4cSJan Kara return ret; 1035ac27a0ecSDave Kleikamp } 1036ac27a0ecSDave Kleikamp 1037643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10382058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10392058f83aSMichael Halcrow get_block_t *get_block) 10402058f83aSMichael Halcrow { 104109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10422058f83aSMichael Halcrow unsigned to = from + len; 10432058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10442058f83aSMichael Halcrow unsigned block_start, block_end; 10452058f83aSMichael Halcrow sector_t block; 10462058f83aSMichael Halcrow int err = 0; 10472058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10482058f83aSMichael Halcrow unsigned bbits; 10490b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10500b578f35SChandan Rajendra int nr_wait = 0; 10510b578f35SChandan Rajendra int i; 10522058f83aSMichael Halcrow 10532058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 105409cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 105509cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10562058f83aSMichael Halcrow BUG_ON(from > to); 10572058f83aSMichael Halcrow 10582058f83aSMichael Halcrow if (!page_has_buffers(page)) 10592058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10602058f83aSMichael Halcrow head = page_buffers(page); 10612058f83aSMichael Halcrow bbits = ilog2(blocksize); 106209cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10632058f83aSMichael Halcrow 10642058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10652058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10662058f83aSMichael Halcrow block_end = block_start + blocksize; 10672058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10682058f83aSMichael Halcrow if (PageUptodate(page)) { 10692058f83aSMichael Halcrow set_buffer_uptodate(bh); 10702058f83aSMichael Halcrow } 10712058f83aSMichael Halcrow continue; 10722058f83aSMichael Halcrow } 10732058f83aSMichael Halcrow if (buffer_new(bh)) 10742058f83aSMichael Halcrow clear_buffer_new(bh); 10752058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10762058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10772058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10782058f83aSMichael Halcrow if (err) 10792058f83aSMichael Halcrow break; 10802058f83aSMichael Halcrow if (buffer_new(bh)) { 10812058f83aSMichael Halcrow if (PageUptodate(page)) { 10822058f83aSMichael Halcrow clear_buffer_new(bh); 10832058f83aSMichael Halcrow set_buffer_uptodate(bh); 10842058f83aSMichael Halcrow mark_buffer_dirty(bh); 10852058f83aSMichael Halcrow continue; 10862058f83aSMichael Halcrow } 10872058f83aSMichael Halcrow if (block_end > to || block_start < from) 10882058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10892058f83aSMichael Halcrow block_start, from); 10902058f83aSMichael Halcrow continue; 10912058f83aSMichael Halcrow } 10922058f83aSMichael Halcrow } 10932058f83aSMichael Halcrow if (PageUptodate(page)) { 10942058f83aSMichael Halcrow set_buffer_uptodate(bh); 10952058f83aSMichael Halcrow continue; 10962058f83aSMichael Halcrow } 10972058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10982058f83aSMichael Halcrow !buffer_unwritten(bh) && 10992058f83aSMichael Halcrow (block_start < from || block_end > to)) { 11002d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 11010b578f35SChandan Rajendra wait[nr_wait++] = bh; 11022058f83aSMichael Halcrow } 11032058f83aSMichael Halcrow } 11042058f83aSMichael Halcrow /* 11052058f83aSMichael Halcrow * If we issued read requests, let them complete. 11062058f83aSMichael Halcrow */ 11070b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11080b578f35SChandan Rajendra wait_on_buffer(wait[i]); 11090b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 11102058f83aSMichael Halcrow err = -EIO; 11112058f83aSMichael Halcrow } 11127e0785fcSChandan Rajendra if (unlikely(err)) { 11132058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 11144f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11150b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11160b578f35SChandan Rajendra int err2; 11170b578f35SChandan Rajendra 11180b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 11190b578f35SChandan Rajendra bh_offset(wait[i])); 11200b578f35SChandan Rajendra if (err2) { 11210b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11220b578f35SChandan Rajendra err = err2; 11230b578f35SChandan Rajendra } 11240b578f35SChandan Rajendra } 11257e0785fcSChandan Rajendra } 11267e0785fcSChandan Rajendra 11272058f83aSMichael Halcrow return err; 11282058f83aSMichael Halcrow } 11292058f83aSMichael Halcrow #endif 11302058f83aSMichael Halcrow 1131bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1132bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1133bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1134ac27a0ecSDave Kleikamp { 1135bfc1af65SNick Piggin struct inode *inode = mapping->host; 11361938a150SAneesh Kumar K.V int ret, needed_blocks; 1137ac27a0ecSDave Kleikamp handle_t *handle; 1138ac27a0ecSDave Kleikamp int retries = 0; 1139bfc1af65SNick Piggin struct page *page; 1140bfc1af65SNick Piggin pgoff_t index; 1141bfc1af65SNick Piggin unsigned from, to; 1142bfc1af65SNick Piggin 11430db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11440db1ff22STheodore Ts'o return -EIO; 11450db1ff22STheodore Ts'o 11469bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11471938a150SAneesh Kumar K.V /* 11481938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11491938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11501938a150SAneesh Kumar K.V */ 11511938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 115209cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 115309cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1154bfc1af65SNick Piggin to = from + len; 1155ac27a0ecSDave Kleikamp 1156f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1157f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1158f19d5870STao Ma flags, pagep); 1159f19d5870STao Ma if (ret < 0) 116047564bfbSTheodore Ts'o return ret; 116147564bfbSTheodore Ts'o if (ret == 1) 116247564bfbSTheodore Ts'o return 0; 1163f19d5870STao Ma } 1164f19d5870STao Ma 116547564bfbSTheodore Ts'o /* 116647564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 116747564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 116847564bfbSTheodore Ts'o * is being written back. So grab it first before we start 116947564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 117047564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 117147564bfbSTheodore Ts'o */ 117247564bfbSTheodore Ts'o retry_grab: 117354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 117447564bfbSTheodore Ts'o if (!page) 117547564bfbSTheodore Ts'o return -ENOMEM; 117647564bfbSTheodore Ts'o unlock_page(page); 117747564bfbSTheodore Ts'o 117847564bfbSTheodore Ts'o retry_journal: 11799924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1180ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 118109cbfeafSKirill A. Shutemov put_page(page); 118247564bfbSTheodore Ts'o return PTR_ERR(handle); 1183cf108bcaSJan Kara } 1184f19d5870STao Ma 118547564bfbSTheodore Ts'o lock_page(page); 118647564bfbSTheodore Ts'o if (page->mapping != mapping) { 118747564bfbSTheodore Ts'o /* The page got truncated from under us */ 118847564bfbSTheodore Ts'o unlock_page(page); 118909cbfeafSKirill A. Shutemov put_page(page); 1190cf108bcaSJan Kara ext4_journal_stop(handle); 119147564bfbSTheodore Ts'o goto retry_grab; 1192cf108bcaSJan Kara } 11937afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11947afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1195cf108bcaSJan Kara 1196643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11972058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11982058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1199705965bdSJan Kara ext4_get_block_unwritten); 12002058f83aSMichael Halcrow else 12012058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 12022058f83aSMichael Halcrow ext4_get_block); 12032058f83aSMichael Halcrow #else 1204744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1205705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1206705965bdSJan Kara ext4_get_block_unwritten); 1207744692dcSJiaying Zhang else 12086e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 12092058f83aSMichael Halcrow #endif 1210bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1211f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1212f19d5870STao Ma from, to, NULL, 1213f19d5870STao Ma do_journal_get_write_access); 1214b46be050SAndrey Savochkin } 1215bfc1af65SNick Piggin 1216bfc1af65SNick Piggin if (ret) { 1217c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1218c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1219c93d8f88SEric Biggers 1220bfc1af65SNick Piggin unlock_page(page); 1221ae4d5372SAneesh Kumar K.V /* 12226e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1223ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1224ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12251938a150SAneesh Kumar K.V * 12261938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12271938a150SAneesh Kumar K.V * truncate finishes 1228ae4d5372SAneesh Kumar K.V */ 1229c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12301938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12311938a150SAneesh Kumar K.V 12321938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1233c93d8f88SEric Biggers if (extended) { 1234b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12351938a150SAneesh Kumar K.V /* 1236ffacfa7aSJan Kara * If truncate failed early the inode might 12371938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12381938a150SAneesh Kumar K.V * make sure the inode is removed from the 12391938a150SAneesh Kumar K.V * orphan list in that case. 12401938a150SAneesh Kumar K.V */ 12411938a150SAneesh Kumar K.V if (inode->i_nlink) 12421938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12431938a150SAneesh Kumar K.V } 1244bfc1af65SNick Piggin 124547564bfbSTheodore Ts'o if (ret == -ENOSPC && 124647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 124747564bfbSTheodore Ts'o goto retry_journal; 124809cbfeafSKirill A. Shutemov put_page(page); 124947564bfbSTheodore Ts'o return ret; 125047564bfbSTheodore Ts'o } 125147564bfbSTheodore Ts'o *pagep = page; 1252ac27a0ecSDave Kleikamp return ret; 1253ac27a0ecSDave Kleikamp } 1254ac27a0ecSDave Kleikamp 1255bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1256bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1257ac27a0ecSDave Kleikamp { 125813fca323STheodore Ts'o int ret; 1259ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1260ac27a0ecSDave Kleikamp return 0; 1261ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 126213fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 126313fca323STheodore Ts'o clear_buffer_meta(bh); 126413fca323STheodore Ts'o clear_buffer_prio(bh); 126513fca323STheodore Ts'o return ret; 1266ac27a0ecSDave Kleikamp } 1267ac27a0ecSDave Kleikamp 1268eed4333fSZheng Liu /* 1269eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1270eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1271eed4333fSZheng Liu * 1272eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1273eed4333fSZheng Liu * buffers are managed internally. 1274eed4333fSZheng Liu */ 1275eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1276f8514083SAneesh Kumar K.V struct address_space *mapping, 1277f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1278f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1279f8514083SAneesh Kumar K.V { 1280f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1281eed4333fSZheng Liu struct inode *inode = mapping->host; 12820572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1283eed4333fSZheng Liu int ret = 0, ret2; 1284eed4333fSZheng Liu int i_size_changed = 0; 1285c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1286eed4333fSZheng Liu 1287eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1288*6984aef5SZhang Yi 1289*6984aef5SZhang Yi if (ext4_has_inline_data(inode)) 1290*6984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 1291*6984aef5SZhang Yi 1292*6984aef5SZhang Yi copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1293f8514083SAneesh Kumar K.V /* 12944631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1295f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1296c93d8f88SEric Biggers * 1297c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1298c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1299f8514083SAneesh Kumar K.V */ 1300c93d8f88SEric Biggers if (!verity) 13014631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1302f8514083SAneesh Kumar K.V unlock_page(page); 130309cbfeafSKirill A. Shutemov put_page(page); 1304f8514083SAneesh Kumar K.V 1305c93d8f88SEric Biggers if (old_size < pos && !verity) 13060572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1307f8514083SAneesh Kumar K.V /* 1308f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1309f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1310f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1311f8514083SAneesh Kumar K.V * filesystems. 1312f8514083SAneesh Kumar K.V */ 1313*6984aef5SZhang Yi if (i_size_changed) 13144209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1315f8514083SAneesh Kumar K.V 1316c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1317f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1318f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1319f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1320f8514083SAneesh Kumar K.V */ 1321f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 132255ce2f64SZhang Yi 1323617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1324ac27a0ecSDave Kleikamp if (!ret) 1325ac27a0ecSDave Kleikamp ret = ret2; 1326bfc1af65SNick Piggin 1327c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1328b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1329f8514083SAneesh Kumar K.V /* 1330ffacfa7aSJan Kara * If truncate failed early the inode might still be 1331f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1332f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1333f8514083SAneesh Kumar K.V */ 1334f8514083SAneesh Kumar K.V if (inode->i_nlink) 1335f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1336f8514083SAneesh Kumar K.V } 1337f8514083SAneesh Kumar K.V 1338bfc1af65SNick Piggin return ret ? ret : copied; 1339ac27a0ecSDave Kleikamp } 1340ac27a0ecSDave Kleikamp 1341b90197b6STheodore Ts'o /* 1342b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1343b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1344b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1345b90197b6STheodore Ts'o */ 13463b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 13473b136499SJan Kara struct page *page, 13483b136499SJan Kara unsigned from, unsigned to) 1349b90197b6STheodore Ts'o { 1350b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1351b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1352b90197b6STheodore Ts'o 1353b90197b6STheodore Ts'o bh = head = page_buffers(page); 1354b90197b6STheodore Ts'o do { 1355b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1356b90197b6STheodore Ts'o if (buffer_new(bh)) { 1357b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1358b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1359b90197b6STheodore Ts'o unsigned start, size; 1360b90197b6STheodore Ts'o 1361b90197b6STheodore Ts'o start = max(from, block_start); 1362b90197b6STheodore Ts'o size = min(to, block_end) - start; 1363b90197b6STheodore Ts'o 1364b90197b6STheodore Ts'o zero_user(page, start, size); 13653b136499SJan Kara write_end_fn(handle, bh); 1366b90197b6STheodore Ts'o } 1367b90197b6STheodore Ts'o clear_buffer_new(bh); 1368b90197b6STheodore Ts'o } 1369b90197b6STheodore Ts'o } 1370b90197b6STheodore Ts'o block_start = block_end; 1371b90197b6STheodore Ts'o bh = bh->b_this_page; 1372b90197b6STheodore Ts'o } while (bh != head); 1373b90197b6STheodore Ts'o } 1374b90197b6STheodore Ts'o 1375bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1376bfc1af65SNick Piggin struct address_space *mapping, 1377bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1378bfc1af65SNick Piggin struct page *page, void *fsdata) 1379ac27a0ecSDave Kleikamp { 1380617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1381bfc1af65SNick Piggin struct inode *inode = mapping->host; 13820572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1383ac27a0ecSDave Kleikamp int ret = 0, ret2; 1384ac27a0ecSDave Kleikamp int partial = 0; 1385bfc1af65SNick Piggin unsigned from, to; 13864631dbf6SDmitry Monakhov int size_changed = 0; 1387c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1388ac27a0ecSDave Kleikamp 13899bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 139009cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1391bfc1af65SNick Piggin to = from + len; 1392bfc1af65SNick Piggin 1393441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1394441c8508SCurt Wohlgemuth 1395*6984aef5SZhang Yi if (ext4_has_inline_data(inode)) 1396*6984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 1397*6984aef5SZhang Yi 1398*6984aef5SZhang Yi if (unlikely(copied < len) && !PageUptodate(page)) { 1399bfc1af65SNick Piggin copied = 0; 14003b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, from, to); 14013b136499SJan Kara } else { 14023b136499SJan Kara if (unlikely(copied < len)) 14033b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, 14043b136499SJan Kara from + copied, to); 1405f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 14063b136499SJan Kara from + copied, &partial, 14073b136499SJan Kara write_end_fn); 1408ac27a0ecSDave Kleikamp if (!partial) 1409ac27a0ecSDave Kleikamp SetPageUptodate(page); 14103fdcfb66STao Ma } 1411c93d8f88SEric Biggers if (!verity) 14124631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 141319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14142d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14154631dbf6SDmitry Monakhov unlock_page(page); 141609cbfeafSKirill A. Shutemov put_page(page); 14174631dbf6SDmitry Monakhov 1418c93d8f88SEric Biggers if (old_size < pos && !verity) 14190572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14200572639fSXiaoguang Wang 1421*6984aef5SZhang Yi if (size_changed) { 1422617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1423ac27a0ecSDave Kleikamp if (!ret) 1424ac27a0ecSDave Kleikamp ret = ret2; 1425ac27a0ecSDave Kleikamp } 1426bfc1af65SNick Piggin 1427c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1428f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1429f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1430f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1431f8514083SAneesh Kumar K.V */ 1432f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1433f8514083SAneesh Kumar K.V 1434617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1435ac27a0ecSDave Kleikamp if (!ret) 1436ac27a0ecSDave Kleikamp ret = ret2; 1437c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1438b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1439f8514083SAneesh Kumar K.V /* 1440ffacfa7aSJan Kara * If truncate failed early the inode might still be 1441f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1442f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1443f8514083SAneesh Kumar K.V */ 1444f8514083SAneesh Kumar K.V if (inode->i_nlink) 1445f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1446f8514083SAneesh Kumar K.V } 1447bfc1af65SNick Piggin 1448bfc1af65SNick Piggin return ret ? ret : copied; 1449ac27a0ecSDave Kleikamp } 1450d2a17637SMingming Cao 14519d0be502STheodore Ts'o /* 1452c27e43a1SEric Whitney * Reserve space for a single cluster 14539d0be502STheodore Ts'o */ 1454c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1455d2a17637SMingming Cao { 1456d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14570637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14585dd4056dSChristoph Hellwig int ret; 1459d2a17637SMingming Cao 146060e58e0fSMingming Cao /* 146172b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 146272b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 146372b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 146460e58e0fSMingming Cao */ 14657b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14665dd4056dSChristoph Hellwig if (ret) 14675dd4056dSChristoph Hellwig return ret; 146803179fe9STheodore Ts'o 146903179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 147071d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 147103179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147203179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1473d2a17637SMingming Cao return -ENOSPC; 1474d2a17637SMingming Cao } 14759d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1476c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14770637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147839bc680aSDmitry Monakhov 1479d2a17637SMingming Cao return 0; /* success */ 1480d2a17637SMingming Cao } 1481d2a17637SMingming Cao 1482f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1483d2a17637SMingming Cao { 1484d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14850637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1486d2a17637SMingming Cao 1487cd213226SMingming Cao if (!to_free) 1488cd213226SMingming Cao return; /* Nothing to release, exit */ 1489cd213226SMingming Cao 1490d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1491cd213226SMingming Cao 14925a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14930637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1494cd213226SMingming Cao /* 14950637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14960637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14970637c6f4STheodore Ts'o * function is called from invalidate page, it's 14980637c6f4STheodore Ts'o * harmless to return without any action. 1499cd213226SMingming Cao */ 15008de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15010637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15021084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15030637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15040637c6f4STheodore Ts'o WARN_ON(1); 15050637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15060637c6f4STheodore Ts'o } 15070637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15080637c6f4STheodore Ts'o 150972b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 151057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1511d2a17637SMingming Cao 1512d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 151360e58e0fSMingming Cao 15147b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1515d2a17637SMingming Cao } 1516d2a17637SMingming Cao 1517ac27a0ecSDave Kleikamp /* 151864769240SAlex Tomas * Delayed allocation stuff 151964769240SAlex Tomas */ 152064769240SAlex Tomas 15214e7ea81dSJan Kara struct mpage_da_data { 15224e7ea81dSJan Kara struct inode *inode; 15234e7ea81dSJan Kara struct writeback_control *wbc; 15246b523df4SJan Kara 15254e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15264e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15274e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 152864769240SAlex Tomas /* 15294e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15304e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15314e7ea81dSJan Kara * is delalloc or unwritten. 153264769240SAlex Tomas */ 15334e7ea81dSJan Kara struct ext4_map_blocks map; 15344e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1535dddbd6acSJan Kara unsigned int do_map:1; 15366b8ed620SJan Kara unsigned int scanned_until_end:1; 15374e7ea81dSJan Kara }; 153864769240SAlex Tomas 15394e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15404e7ea81dSJan Kara bool invalidate) 1541c4a0c46eSAneesh Kumar K.V { 1542c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1543c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1544c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1545c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1546c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15474e7ea81dSJan Kara 15484e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15494e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15504e7ea81dSJan Kara return; 1551c4a0c46eSAneesh Kumar K.V 15526b8ed620SJan Kara mpd->scanned_until_end = 0; 1553c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1554c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15554e7ea81dSJan Kara if (invalidate) { 15564e7ea81dSJan Kara ext4_lblk_t start, last; 155709cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 155809cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 155951865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15604e7ea81dSJan Kara } 156151865fdaSZheng Liu 156286679820SMel Gorman pagevec_init(&pvec); 1563c4a0c46eSAneesh Kumar K.V while (index <= end) { 1564397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1565c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1566c4a0c46eSAneesh Kumar K.V break; 1567c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1568c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15692b85a617SJan Kara 1570c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1571c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15724e7ea81dSJan Kara if (invalidate) { 15734e800c03Swangguang if (page_mapped(page)) 15744e800c03Swangguang clear_page_dirty_for_io(page); 157509cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1576c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15774e7ea81dSJan Kara } 1578c4a0c46eSAneesh Kumar K.V unlock_page(page); 1579c4a0c46eSAneesh Kumar K.V } 15809b1d0998SJan Kara pagevec_release(&pvec); 1581c4a0c46eSAneesh Kumar K.V } 1582c4a0c46eSAneesh Kumar K.V } 1583c4a0c46eSAneesh Kumar K.V 1584df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1585df22291fSAneesh Kumar K.V { 1586df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 158792b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1588f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 158992b97816STheodore Ts'o 159092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15915dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1592f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 159392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 159492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1595f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 159657042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 159792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1598f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 15997b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 160092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 160192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1602f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1603df22291fSAneesh Kumar K.V return; 1604df22291fSAneesh Kumar K.V } 1605df22291fSAneesh Kumar K.V 1606c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 160729fa89d0SAneesh Kumar K.V { 1608c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 160929fa89d0SAneesh Kumar K.V } 161029fa89d0SAneesh Kumar K.V 161164769240SAlex Tomas /* 16120b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16130b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16140b02f4c0SEric Whitney * count or making a pending reservation 16150b02f4c0SEric Whitney * where needed 16160b02f4c0SEric Whitney * 16170b02f4c0SEric Whitney * @inode - file containing the newly added block 16180b02f4c0SEric Whitney * @lblk - logical block to be added 16190b02f4c0SEric Whitney * 16200b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16210b02f4c0SEric Whitney */ 16220b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16230b02f4c0SEric Whitney { 16240b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16250b02f4c0SEric Whitney int ret; 16260b02f4c0SEric Whitney bool allocated = false; 16270b02f4c0SEric Whitney 16280b02f4c0SEric Whitney /* 16290b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16300b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16310b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16320b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16330b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16340b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16350b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16360b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16370b02f4c0SEric Whitney * extents status tree doesn't get a match. 16380b02f4c0SEric Whitney */ 16390b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16400b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16410b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16420b02f4c0SEric Whitney goto errout; 16430b02f4c0SEric Whitney } else { /* bigalloc */ 16440b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16450b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16460b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16470b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16480b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16490b02f4c0SEric Whitney if (ret < 0) 16500b02f4c0SEric Whitney goto errout; 16510b02f4c0SEric Whitney if (ret == 0) { 16520b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16530b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16540b02f4c0SEric Whitney goto errout; 16550b02f4c0SEric Whitney } else { 16560b02f4c0SEric Whitney allocated = true; 16570b02f4c0SEric Whitney } 16580b02f4c0SEric Whitney } else { 16590b02f4c0SEric Whitney allocated = true; 16600b02f4c0SEric Whitney } 16610b02f4c0SEric Whitney } 16620b02f4c0SEric Whitney } 16630b02f4c0SEric Whitney 16640b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16650b02f4c0SEric Whitney 16660b02f4c0SEric Whitney errout: 16670b02f4c0SEric Whitney return ret; 16680b02f4c0SEric Whitney } 16690b02f4c0SEric Whitney 16700b02f4c0SEric Whitney /* 16715356f261SAditya Kali * This function is grabs code from the very beginning of 16725356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16735356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16745356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16755356f261SAditya Kali */ 16765356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16775356f261SAditya Kali struct ext4_map_blocks *map, 16785356f261SAditya Kali struct buffer_head *bh) 16795356f261SAditya Kali { 1680d100eef2SZheng Liu struct extent_status es; 16815356f261SAditya Kali int retval; 16825356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1683921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1684921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1685921f266bSDmitry Monakhov 1686921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1687921f266bSDmitry Monakhov #endif 16885356f261SAditya Kali 16895356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16905356f261SAditya Kali invalid_block = ~0; 16915356f261SAditya Kali 16925356f261SAditya Kali map->m_flags = 0; 169370aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 16945356f261SAditya Kali (unsigned long) map->m_lblk); 1695d100eef2SZheng Liu 1696d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1697bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1698d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1699d100eef2SZheng Liu retval = 0; 1700c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1701d100eef2SZheng Liu goto add_delayed; 1702d100eef2SZheng Liu } 1703d100eef2SZheng Liu 1704d100eef2SZheng Liu /* 1705d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1706d100eef2SZheng Liu * So we need to check it. 1707d100eef2SZheng Liu */ 1708d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1709d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1710d100eef2SZheng Liu set_buffer_new(bh); 1711d100eef2SZheng Liu set_buffer_delay(bh); 1712d100eef2SZheng Liu return 0; 1713d100eef2SZheng Liu } 1714d100eef2SZheng Liu 1715d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1716d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1717d100eef2SZheng Liu if (retval > map->m_len) 1718d100eef2SZheng Liu retval = map->m_len; 1719d100eef2SZheng Liu map->m_len = retval; 1720d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1721d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1722d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1723d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1724d100eef2SZheng Liu else 17251e83bc81SArnd Bergmann BUG(); 1726d100eef2SZheng Liu 1727921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1728921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1729921f266bSDmitry Monakhov #endif 1730d100eef2SZheng Liu return retval; 1731d100eef2SZheng Liu } 1732d100eef2SZheng Liu 17335356f261SAditya Kali /* 17345356f261SAditya Kali * Try to see if we can get the block without requesting a new 17355356f261SAditya Kali * file system block. 17365356f261SAditya Kali */ 1737c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1738cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17399c3569b5STao Ma retval = 0; 1740cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17412f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17425356f261SAditya Kali else 17432f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17445356f261SAditya Kali 1745d100eef2SZheng Liu add_delayed: 17465356f261SAditya Kali if (retval == 0) { 1747f7fec032SZheng Liu int ret; 1748ad431025SEric Whitney 17495356f261SAditya Kali /* 17505356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17515356f261SAditya Kali * is it OK? 17525356f261SAditya Kali */ 17535356f261SAditya Kali 17540b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17550b02f4c0SEric Whitney if (ret != 0) { 1756f7fec032SZheng Liu retval = ret; 175751865fdaSZheng Liu goto out_unlock; 1758f7fec032SZheng Liu } 175951865fdaSZheng Liu 17605356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17615356f261SAditya Kali set_buffer_new(bh); 17625356f261SAditya Kali set_buffer_delay(bh); 1763f7fec032SZheng Liu } else if (retval > 0) { 1764f7fec032SZheng Liu int ret; 17653be78c73STheodore Ts'o unsigned int status; 1766f7fec032SZheng Liu 176744fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 176844fb851dSZheng Liu ext4_warning(inode->i_sb, 176944fb851dSZheng Liu "ES len assertion failed for inode " 177044fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 177144fb851dSZheng Liu inode->i_ino, retval, map->m_len); 177244fb851dSZheng Liu WARN_ON(1); 1773921f266bSDmitry Monakhov } 1774921f266bSDmitry Monakhov 1775f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1776f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1777f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1778f7fec032SZheng Liu map->m_pblk, status); 1779f7fec032SZheng Liu if (ret != 0) 1780f7fec032SZheng Liu retval = ret; 17815356f261SAditya Kali } 17825356f261SAditya Kali 17835356f261SAditya Kali out_unlock: 17845356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17855356f261SAditya Kali 17865356f261SAditya Kali return retval; 17875356f261SAditya Kali } 17885356f261SAditya Kali 17895356f261SAditya Kali /* 1790d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1791b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1792b920c755STheodore Ts'o * reserve space for a single block. 179329fa89d0SAneesh Kumar K.V * 179429fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 179529fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 179629fa89d0SAneesh Kumar K.V * 179729fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 179829fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 179929fa89d0SAneesh Kumar K.V * initialized properly. 180064769240SAlex Tomas */ 18019c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18022ed88685STheodore Ts'o struct buffer_head *bh, int create) 180364769240SAlex Tomas { 18042ed88685STheodore Ts'o struct ext4_map_blocks map; 180564769240SAlex Tomas int ret = 0; 180664769240SAlex Tomas 180764769240SAlex Tomas BUG_ON(create == 0); 18082ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18092ed88685STheodore Ts'o 18102ed88685STheodore Ts'o map.m_lblk = iblock; 18112ed88685STheodore Ts'o map.m_len = 1; 181264769240SAlex Tomas 181364769240SAlex Tomas /* 181464769240SAlex Tomas * first, we need to know whether the block is allocated already 181564769240SAlex Tomas * preallocated blocks are unmapped but should treated 181664769240SAlex Tomas * the same as allocated blocks. 181764769240SAlex Tomas */ 18185356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18195356f261SAditya Kali if (ret <= 0) 18202ed88685STheodore Ts'o return ret; 182164769240SAlex Tomas 18222ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1823ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18242ed88685STheodore Ts'o 18252ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18262ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18272ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18282ed88685STheodore Ts'o * get_block multiple times when we write to the same 18292ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18302ed88685STheodore Ts'o * for partial write. 18312ed88685STheodore Ts'o */ 18322ed88685STheodore Ts'o set_buffer_new(bh); 1833c8205636STheodore Ts'o set_buffer_mapped(bh); 18342ed88685STheodore Ts'o } 18352ed88685STheodore Ts'o return 0; 183664769240SAlex Tomas } 183761628a3fSMingming Cao 183862e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 183962e086beSAneesh Kumar K.V { 184062e086beSAneesh Kumar K.V get_bh(bh); 184162e086beSAneesh Kumar K.V return 0; 184262e086beSAneesh Kumar K.V } 184362e086beSAneesh Kumar K.V 184462e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 184562e086beSAneesh Kumar K.V { 184662e086beSAneesh Kumar K.V put_bh(bh); 184762e086beSAneesh Kumar K.V return 0; 184862e086beSAneesh Kumar K.V } 184962e086beSAneesh Kumar K.V 185062e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 185162e086beSAneesh Kumar K.V unsigned int len) 185262e086beSAneesh Kumar K.V { 185362e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 185462e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18553fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 185662e086beSAneesh Kumar K.V handle_t *handle = NULL; 18573fdcfb66STao Ma int ret = 0, err = 0; 18583fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18593fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 186062e086beSAneesh Kumar K.V 1861cb20d518STheodore Ts'o ClearPageChecked(page); 18623fdcfb66STao Ma 18633fdcfb66STao Ma if (inline_data) { 18643fdcfb66STao Ma BUG_ON(page->index != 0); 18653fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18663fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18673fdcfb66STao Ma if (inode_bh == NULL) 18683fdcfb66STao Ma goto out; 18693fdcfb66STao Ma } else { 187062e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18713fdcfb66STao Ma if (!page_bufs) { 18723fdcfb66STao Ma BUG(); 18733fdcfb66STao Ma goto out; 18743fdcfb66STao Ma } 18753fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18763fdcfb66STao Ma NULL, bget_one); 18773fdcfb66STao Ma } 1878bdf96838STheodore Ts'o /* 1879bdf96838STheodore Ts'o * We need to release the page lock before we start the 1880bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1881bdf96838STheodore Ts'o * out from under us. 1882bdf96838STheodore Ts'o */ 1883bdf96838STheodore Ts'o get_page(page); 188462e086beSAneesh Kumar K.V unlock_page(page); 188562e086beSAneesh Kumar K.V 18869924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18879924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 188862e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 188962e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1890bdf96838STheodore Ts'o put_page(page); 1891bdf96838STheodore Ts'o goto out_no_pagelock; 1892bdf96838STheodore Ts'o } 1893bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1894bdf96838STheodore Ts'o 1895bdf96838STheodore Ts'o lock_page(page); 1896bdf96838STheodore Ts'o put_page(page); 1897bdf96838STheodore Ts'o if (page->mapping != mapping) { 1898bdf96838STheodore Ts'o /* The page got truncated from under us */ 1899bdf96838STheodore Ts'o ext4_journal_stop(handle); 1900bdf96838STheodore Ts'o ret = 0; 190162e086beSAneesh Kumar K.V goto out; 190262e086beSAneesh Kumar K.V } 190362e086beSAneesh Kumar K.V 19043fdcfb66STao Ma if (inline_data) { 1905362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19063fdcfb66STao Ma } else { 1907f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190862e086beSAneesh Kumar K.V do_journal_get_write_access); 190962e086beSAneesh Kumar K.V 1910f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 191162e086beSAneesh Kumar K.V write_end_fn); 19123fdcfb66STao Ma } 191362e086beSAneesh Kumar K.V if (ret == 0) 191462e086beSAneesh Kumar K.V ret = err; 1915b5b18160SJan Kara err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 1916afb585a9SMauricio Faria de Oliveira if (ret == 0) 1917afb585a9SMauricio Faria de Oliveira ret = err; 19182d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 191962e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 192062e086beSAneesh Kumar K.V if (!ret) 192162e086beSAneesh Kumar K.V ret = err; 192262e086beSAneesh Kumar K.V 192319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 192462e086beSAneesh Kumar K.V out: 1925bdf96838STheodore Ts'o unlock_page(page); 1926bdf96838STheodore Ts'o out_no_pagelock: 1927c915fb80SZhaolong Zhang if (!inline_data && page_bufs) 1928c915fb80SZhaolong Zhang ext4_walk_page_buffers(NULL, page_bufs, 0, len, 1929c915fb80SZhaolong Zhang NULL, bput_one); 19303fdcfb66STao Ma brelse(inode_bh); 193162e086beSAneesh Kumar K.V return ret; 193262e086beSAneesh Kumar K.V } 193362e086beSAneesh Kumar K.V 193461628a3fSMingming Cao /* 193543ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 193643ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 193743ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 193843ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193943ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 194043ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 194143ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 194243ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 194343ce1d23SAneesh Kumar K.V * 1944b920c755STheodore Ts'o * This function can get called via... 194520970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1946b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1947f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1948b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194943ce1d23SAneesh Kumar K.V * 195043ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 195143ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 195243ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 195343ce1d23SAneesh Kumar K.V * truncate(f, 1024); 195443ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195543ce1d23SAneesh Kumar K.V * a[0] = 'a'; 195643ce1d23SAneesh Kumar K.V * truncate(f, 4096); 195743ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 195890802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195943ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 196043ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 196143ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 196243ce1d23SAneesh Kumar K.V * buffer_heads mapped. 196343ce1d23SAneesh Kumar K.V * 196443ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196543ce1d23SAneesh Kumar K.V * unwritten in the page. 196643ce1d23SAneesh Kumar K.V * 196743ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 196843ce1d23SAneesh Kumar K.V * 196943ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 197043ce1d23SAneesh Kumar K.V * ext4_writepage() 197143ce1d23SAneesh Kumar K.V * 197243ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 197343ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 197461628a3fSMingming Cao */ 197543ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 197664769240SAlex Tomas struct writeback_control *wbc) 197764769240SAlex Tomas { 1978f8bec370SJan Kara int ret = 0; 197961628a3fSMingming Cao loff_t size; 1980498e5f24STheodore Ts'o unsigned int len; 1981744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 198261628a3fSMingming Cao struct inode *inode = page->mapping->host; 198336ade451SJan Kara struct ext4_io_submit io_submit; 19841c8349a1SNamjae Jeon bool keep_towrite = false; 198564769240SAlex Tomas 19860db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 1987c2a559bcSyangerkun inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 19880db1ff22STheodore Ts'o unlock_page(page); 19890db1ff22STheodore Ts'o return -EIO; 19900db1ff22STheodore Ts'o } 19910db1ff22STheodore Ts'o 1992a9c667f8SLukas Czerner trace_ext4_writepage(page); 199361628a3fSMingming Cao size = i_size_read(inode); 1994c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 1995c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 199609cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 199761628a3fSMingming Cao else 199809cbfeafSKirill A. Shutemov len = PAGE_SIZE; 199961628a3fSMingming Cao 2000f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 200164769240SAlex Tomas /* 2002fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2003fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2004fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2005fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2006fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2007cccd147aSTheodore Ts'o * 2008cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2009cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2010cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2011cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2012cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2013cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2014cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2015cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2016cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 201764769240SAlex Tomas */ 2018f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2019c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 202061628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2021cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 202209cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2023fe386132SJan Kara /* 2024fe386132SJan Kara * For memory cleaning there's no point in writing only 2025fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2026fe386132SJan Kara * from direct reclaim. 2027fe386132SJan Kara */ 2028fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2029fe386132SJan Kara == PF_MEMALLOC); 203061628a3fSMingming Cao unlock_page(page); 203161628a3fSMingming Cao return 0; 203261628a3fSMingming Cao } 20331c8349a1SNamjae Jeon keep_towrite = true; 2034f0e6c985SAneesh Kumar K.V } 203564769240SAlex Tomas 2036cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 203743ce1d23SAneesh Kumar K.V /* 203843ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 203943ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 204043ce1d23SAneesh Kumar K.V */ 20413f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 204243ce1d23SAneesh Kumar K.V 204397a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 204497a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 204597a851edSJan Kara if (!io_submit.io_end) { 204697a851edSJan Kara redirty_page_for_writepage(wbc, page); 204797a851edSJan Kara unlock_page(page); 204897a851edSJan Kara return -ENOMEM; 204997a851edSJan Kara } 2050be993933SLei Chen ret = ext4_bio_write_page(&io_submit, page, len, keep_towrite); 205136ade451SJan Kara ext4_io_submit(&io_submit); 205297a851edSJan Kara /* Drop io_end reference we got from init */ 205397a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 205464769240SAlex Tomas return ret; 205564769240SAlex Tomas } 205664769240SAlex Tomas 20575f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20585f1132b2SJan Kara { 20595f1132b2SJan Kara int len; 2060a056bdaaSJan Kara loff_t size; 20615f1132b2SJan Kara int err; 20625f1132b2SJan Kara 20635f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2064a056bdaaSJan Kara clear_page_dirty_for_io(page); 2065a056bdaaSJan Kara /* 2066a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2067a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2068a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2069a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2070a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2071a056bdaaSJan Kara * written to again until we release page lock. So only after 2072a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2073a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2074a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2075a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2076a056bdaaSJan Kara * after page tables are updated. 2077a056bdaaSJan Kara */ 2078a056bdaaSJan Kara size = i_size_read(mpd->inode); 2079c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2080c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 208109cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20825f1132b2SJan Kara else 208309cbfeafSKirill A. Shutemov len = PAGE_SIZE; 2084be993933SLei Chen err = ext4_bio_write_page(&mpd->io_submit, page, len, false); 20855f1132b2SJan Kara if (!err) 20865f1132b2SJan Kara mpd->wbc->nr_to_write--; 20875f1132b2SJan Kara mpd->first_page++; 20885f1132b2SJan Kara 20895f1132b2SJan Kara return err; 20905f1132b2SJan Kara } 20915f1132b2SJan Kara 20926db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 20934e7ea81dSJan Kara 209461628a3fSMingming Cao /* 2095fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2096fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2097fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 209861628a3fSMingming Cao */ 2099fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2100525f4ed8SMingming Cao 2101525f4ed8SMingming Cao /* 21024e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 21034e7ea81dSJan Kara * 21044e7ea81dSJan Kara * @mpd - extent of blocks 21054e7ea81dSJan Kara * @lblk - logical number of the block in the file 210609930042SJan Kara * @bh - buffer head we want to add to the extent 21074e7ea81dSJan Kara * 210809930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 210909930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 211009930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 211109930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 211209930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 211309930042SJan Kara * added. 21144e7ea81dSJan Kara */ 211509930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 211609930042SJan Kara struct buffer_head *bh) 21174e7ea81dSJan Kara { 21184e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21194e7ea81dSJan Kara 212009930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 212109930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 212209930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 212309930042SJan Kara /* So far no extent to map => we write the buffer right away */ 212409930042SJan Kara if (map->m_len == 0) 212509930042SJan Kara return true; 212609930042SJan Kara return false; 212709930042SJan Kara } 21284e7ea81dSJan Kara 21294e7ea81dSJan Kara /* First block in the extent? */ 21304e7ea81dSJan Kara if (map->m_len == 0) { 2131dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2132dddbd6acSJan Kara if (!mpd->do_map) 2133dddbd6acSJan Kara return false; 21344e7ea81dSJan Kara map->m_lblk = lblk; 21354e7ea81dSJan Kara map->m_len = 1; 213609930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 213709930042SJan Kara return true; 21384e7ea81dSJan Kara } 21394e7ea81dSJan Kara 214009930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 214109930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 214209930042SJan Kara return false; 214309930042SJan Kara 21444e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21454e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 214609930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21474e7ea81dSJan Kara map->m_len++; 214809930042SJan Kara return true; 21494e7ea81dSJan Kara } 215009930042SJan Kara return false; 21514e7ea81dSJan Kara } 21524e7ea81dSJan Kara 21535f1132b2SJan Kara /* 21545f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21555f1132b2SJan Kara * 21565f1132b2SJan Kara * @mpd - extent of blocks for mapping 21575f1132b2SJan Kara * @head - the first buffer in the page 21585f1132b2SJan Kara * @bh - buffer we should start processing from 21595f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21605f1132b2SJan Kara * 21615f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21625f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21635f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21645f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21655f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21665f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21675f1132b2SJan Kara * < 0 in case of error during IO submission. 21685f1132b2SJan Kara */ 21695f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21704e7ea81dSJan Kara struct buffer_head *head, 21714e7ea81dSJan Kara struct buffer_head *bh, 21724e7ea81dSJan Kara ext4_lblk_t lblk) 21734e7ea81dSJan Kara { 21744e7ea81dSJan Kara struct inode *inode = mpd->inode; 21755f1132b2SJan Kara int err; 217693407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21774e7ea81dSJan Kara >> inode->i_blkbits; 21784e7ea81dSJan Kara 2179c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2180c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2181c93d8f88SEric Biggers 21824e7ea81dSJan Kara do { 21834e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21844e7ea81dSJan Kara 218509930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21864e7ea81dSJan Kara /* Found extent to map? */ 21874e7ea81dSJan Kara if (mpd->map.m_len) 21885f1132b2SJan Kara return 0; 2189dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2190dddbd6acSJan Kara if (!mpd->do_map) 2191dddbd6acSJan Kara return 0; 219209930042SJan Kara /* Everything mapped so far and we hit EOF */ 21935f1132b2SJan Kara break; 21944e7ea81dSJan Kara } 21954e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 21965f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 21975f1132b2SJan Kara if (mpd->map.m_len == 0) { 21985f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 21995f1132b2SJan Kara if (err < 0) 22004e7ea81dSJan Kara return err; 22014e7ea81dSJan Kara } 22026b8ed620SJan Kara if (lblk >= blocks) { 22036b8ed620SJan Kara mpd->scanned_until_end = 1; 22046b8ed620SJan Kara return 0; 22056b8ed620SJan Kara } 22066b8ed620SJan Kara return 1; 22075f1132b2SJan Kara } 22084e7ea81dSJan Kara 22094e7ea81dSJan Kara /* 22102943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22112943fdbcSRitesh Harjani * may submit fully mapped page for IO 22122943fdbcSRitesh Harjani * 22132943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22142943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22152943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22162943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22172943fdbcSRitesh Harjani * mapping or not. 22182943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22192943fdbcSRitesh Harjani * state according to new extent state. 22202943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22212943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22222943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22232943fdbcSRitesh Harjani */ 22242943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22252943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22262943fdbcSRitesh Harjani bool *map_bh) 22272943fdbcSRitesh Harjani { 22282943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22292943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22302943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22312943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22322943fdbcSRitesh Harjani int err = 0; 2233c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2234c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2235c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22362943fdbcSRitesh Harjani 22372943fdbcSRitesh Harjani bh = head = page_buffers(page); 22382943fdbcSRitesh Harjani do { 22392943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22402943fdbcSRitesh Harjani continue; 22412943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22422943fdbcSRitesh Harjani /* 22432943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22442943fdbcSRitesh Harjani * Find next buffer in the page to map. 22452943fdbcSRitesh Harjani */ 22462943fdbcSRitesh Harjani mpd->map.m_len = 0; 22472943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2248c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2249c8cc8816SRitesh Harjani io_end_size = 0; 22502943fdbcSRitesh Harjani 22512943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22522943fdbcSRitesh Harjani if (err > 0) 22532943fdbcSRitesh Harjani err = 0; 2254c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2255c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22564d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22574d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22584d06bfb9SRitesh Harjani goto out; 22594d06bfb9SRitesh Harjani } 2260d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2261c8cc8816SRitesh Harjani } 22622943fdbcSRitesh Harjani *map_bh = true; 22632943fdbcSRitesh Harjani goto out; 22642943fdbcSRitesh Harjani } 22652943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22662943fdbcSRitesh Harjani clear_buffer_delay(bh); 22672943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22682943fdbcSRitesh Harjani } 22692943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2270c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22712943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2272c8cc8816SRitesh Harjani 2273c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2274c8cc8816SRitesh Harjani io_end_size = 0; 22752943fdbcSRitesh Harjani *map_bh = false; 22762943fdbcSRitesh Harjani out: 22772943fdbcSRitesh Harjani *m_lblk = lblk; 22782943fdbcSRitesh Harjani *m_pblk = pblock; 22792943fdbcSRitesh Harjani return err; 22802943fdbcSRitesh Harjani } 22812943fdbcSRitesh Harjani 22822943fdbcSRitesh Harjani /* 22834e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22844e7ea81dSJan Kara * submit fully mapped pages for IO 22854e7ea81dSJan Kara * 22864e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22874e7ea81dSJan Kara * 22884e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22894e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22904e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2291556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 22924e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 22934e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 22944e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 22954e7ea81dSJan Kara */ 22964e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 22974e7ea81dSJan Kara { 22984e7ea81dSJan Kara struct pagevec pvec; 22994e7ea81dSJan Kara int nr_pages, i; 23004e7ea81dSJan Kara struct inode *inode = mpd->inode; 230109cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 23024e7ea81dSJan Kara pgoff_t start, end; 23034e7ea81dSJan Kara ext4_lblk_t lblk; 23042943fdbcSRitesh Harjani ext4_fsblk_t pblock; 23054e7ea81dSJan Kara int err; 23062943fdbcSRitesh Harjani bool map_bh = false; 23074e7ea81dSJan Kara 23084e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23094e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23104e7ea81dSJan Kara lblk = start << bpp_bits; 23114e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23124e7ea81dSJan Kara 231386679820SMel Gorman pagevec_init(&pvec); 23144e7ea81dSJan Kara while (start <= end) { 23152b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2316397162ffSJan Kara &start, end); 23174e7ea81dSJan Kara if (nr_pages == 0) 23184e7ea81dSJan Kara break; 23194e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23204e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23214e7ea81dSJan Kara 23222943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23232943fdbcSRitesh Harjani &map_bh); 23244e7ea81dSJan Kara /* 23252943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23262943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23272943fdbcSRitesh Harjani * So we return to call further extent mapping. 23284e7ea81dSJan Kara */ 232939c0ae16SJason Yan if (err < 0 || map_bh) 23302943fdbcSRitesh Harjani goto out; 23314e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23324e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23332943fdbcSRitesh Harjani if (err < 0) 23342943fdbcSRitesh Harjani goto out; 23354e7ea81dSJan Kara } 23364e7ea81dSJan Kara pagevec_release(&pvec); 23374e7ea81dSJan Kara } 23384e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23394e7ea81dSJan Kara mpd->map.m_len = 0; 23404e7ea81dSJan Kara mpd->map.m_flags = 0; 23414e7ea81dSJan Kara return 0; 23422943fdbcSRitesh Harjani out: 23432943fdbcSRitesh Harjani pagevec_release(&pvec); 23442943fdbcSRitesh Harjani return err; 23454e7ea81dSJan Kara } 23464e7ea81dSJan Kara 23474e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23484e7ea81dSJan Kara { 23494e7ea81dSJan Kara struct inode *inode = mpd->inode; 23504e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23514e7ea81dSJan Kara int get_blocks_flags; 2352090f32eeSLukas Czerner int err, dioread_nolock; 23534e7ea81dSJan Kara 23544e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23554e7ea81dSJan Kara /* 23564e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2357556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23584e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23594e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23604e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23614e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23624e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23634e7ea81dSJan Kara * possible. 23644e7ea81dSJan Kara * 2365754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2366754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2367754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2368754cfed6STheodore Ts'o * the data was copied into the page cache. 23694e7ea81dSJan Kara */ 23704e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2371ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2372ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2373090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2374090f32eeSLukas Czerner if (dioread_nolock) 23754e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23766db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 23774e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23784e7ea81dSJan Kara 23794e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23804e7ea81dSJan Kara if (err < 0) 23814e7ea81dSJan Kara return err; 2382090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23836b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23846b523df4SJan Kara ext4_handle_valid(handle)) { 23856b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23866b523df4SJan Kara handle->h_rsv_handle = NULL; 23876b523df4SJan Kara } 23883613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23896b523df4SJan Kara } 23904e7ea81dSJan Kara 23914e7ea81dSJan Kara BUG_ON(map->m_len == 0); 23924e7ea81dSJan Kara return 0; 23934e7ea81dSJan Kara } 23944e7ea81dSJan Kara 23954e7ea81dSJan Kara /* 23964e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 23974e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 23984e7ea81dSJan Kara * 23994e7ea81dSJan Kara * @handle - handle for journal operations 24004e7ea81dSJan Kara * @mpd - extent to map 24017534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 24027534e854SJan Kara * is no hope of writing the data. The caller should discard 24037534e854SJan Kara * dirty pages to avoid infinite loops. 24044e7ea81dSJan Kara * 24054e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 24064e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 24074e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 24084e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24094e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24104e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24114e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24124e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24134e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24144e7ea81dSJan Kara */ 24154e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2416cb530541STheodore Ts'o struct mpage_da_data *mpd, 2417cb530541STheodore Ts'o bool *give_up_on_write) 24184e7ea81dSJan Kara { 24194e7ea81dSJan Kara struct inode *inode = mpd->inode; 24204e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24214e7ea81dSJan Kara int err; 24224e7ea81dSJan Kara loff_t disksize; 24236603120eSDmitry Monakhov int progress = 0; 2424c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24254d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24264e7ea81dSJan Kara 24274d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24284d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24294d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2430c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 243127d7c4edSJan Kara do { 24324e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24334e7ea81dSJan Kara if (err < 0) { 24344e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24354e7ea81dSJan Kara 24360db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24379b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2438cb530541STheodore Ts'o goto invalidate_dirty_pages; 24394e7ea81dSJan Kara /* 2440cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2441cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2442cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24434e7ea81dSJan Kara */ 2444cb530541STheodore Ts'o if ((err == -ENOMEM) || 24456603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24466603120eSDmitry Monakhov if (progress) 24476603120eSDmitry Monakhov goto update_disksize; 2448cb530541STheodore Ts'o return err; 24496603120eSDmitry Monakhov } 24504e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24514e7ea81dSJan Kara "Delayed block allocation failed for " 24524e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24534e7ea81dSJan Kara " max blocks %u with error %d", 24544e7ea81dSJan Kara inode->i_ino, 24554e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2456cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24574e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24584e7ea81dSJan Kara "This should not happen!! Data will " 24594e7ea81dSJan Kara "be lost\n"); 24604e7ea81dSJan Kara if (err == -ENOSPC) 24614e7ea81dSJan Kara ext4_print_free_blocks(inode); 2462cb530541STheodore Ts'o invalidate_dirty_pages: 2463cb530541STheodore Ts'o *give_up_on_write = true; 24644e7ea81dSJan Kara return err; 24654e7ea81dSJan Kara } 24666603120eSDmitry Monakhov progress = 1; 24674e7ea81dSJan Kara /* 24684e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24694e7ea81dSJan Kara * extent to map 24704e7ea81dSJan Kara */ 24714e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24724e7ea81dSJan Kara if (err < 0) 24736603120eSDmitry Monakhov goto update_disksize; 247427d7c4edSJan Kara } while (map->m_len); 24754e7ea81dSJan Kara 24766603120eSDmitry Monakhov update_disksize: 2477622cad13STheodore Ts'o /* 2478622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2479622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2480622cad13STheodore Ts'o */ 248109cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 248235df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 24834e7ea81dSJan Kara int err2; 2484622cad13STheodore Ts'o loff_t i_size; 24854e7ea81dSJan Kara 2486622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2487622cad13STheodore Ts'o i_size = i_size_read(inode); 2488622cad13STheodore Ts'o if (disksize > i_size) 2489622cad13STheodore Ts'o disksize = i_size; 2490622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2491622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2492622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2493b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2494878520acSTheodore Ts'o if (err2) { 249554d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 24964e7ea81dSJan Kara "Failed to mark inode %lu dirty", 24974e7ea81dSJan Kara inode->i_ino); 2498878520acSTheodore Ts'o } 24994e7ea81dSJan Kara if (!err) 25004e7ea81dSJan Kara err = err2; 25014e7ea81dSJan Kara } 25024e7ea81dSJan Kara return err; 25034e7ea81dSJan Kara } 25044e7ea81dSJan Kara 25054e7ea81dSJan Kara /* 2506fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 250720970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2508fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2509fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2510fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2511525f4ed8SMingming Cao */ 2512fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2513fffb2739SJan Kara { 2514fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2515525f4ed8SMingming Cao 2516fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2517fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2518525f4ed8SMingming Cao } 251961628a3fSMingming Cao 25208e48dcfbSTheodore Ts'o /* 25214e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25224e7ea81dSJan Kara * and underlying extent to map 25234e7ea81dSJan Kara * 25244e7ea81dSJan Kara * @mpd - where to look for pages 25254e7ea81dSJan Kara * 25264e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25274e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25284e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25294e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25304e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25314e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25324e7ea81dSJan Kara * 25334e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25344e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25354e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25364e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25378e48dcfbSTheodore Ts'o */ 25384e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25398e48dcfbSTheodore Ts'o { 25404e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25418e48dcfbSTheodore Ts'o struct pagevec pvec; 25424f01b02cSTheodore Ts'o unsigned int nr_pages; 2543aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25444e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25454e7ea81dSJan Kara pgoff_t end = mpd->last_page; 254610bbd235SMatthew Wilcox xa_mark_t tag; 25474e7ea81dSJan Kara int i, err = 0; 25484e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25494e7ea81dSJan Kara ext4_lblk_t lblk; 25504e7ea81dSJan Kara struct buffer_head *head; 25518e48dcfbSTheodore Ts'o 25524e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25535b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25545b41d924SEric Sandeen else 25555b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25565b41d924SEric Sandeen 255786679820SMel Gorman pagevec_init(&pvec); 25584e7ea81dSJan Kara mpd->map.m_len = 0; 25594e7ea81dSJan Kara mpd->next_page = index; 25604f01b02cSTheodore Ts'o while (index <= end) { 2561dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 256267fd707fSJan Kara tag); 25638e48dcfbSTheodore Ts'o if (nr_pages == 0) 25646b8ed620SJan Kara break; 25658e48dcfbSTheodore Ts'o 25668e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25678e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25688e48dcfbSTheodore Ts'o 25698e48dcfbSTheodore Ts'o /* 2570aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2571aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2572aeac589aSMing Lei * keep going because someone may be concurrently 2573aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2574aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2575aeac589aSMing Lei * of the old dirty pages. 2576aeac589aSMing Lei */ 2577aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2578aeac589aSMing Lei goto out; 2579aeac589aSMing Lei 25804e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25814e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25824e7ea81dSJan Kara goto out; 258378aaced3STheodore Ts'o 25848e48dcfbSTheodore Ts'o lock_page(page); 25858e48dcfbSTheodore Ts'o /* 25864e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25874e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25884e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25894e7ea81dSJan Kara * page is already under writeback and we are not doing 25904e7ea81dSJan Kara * a data integrity writeback, skip the page 25918e48dcfbSTheodore Ts'o */ 25924f01b02cSTheodore Ts'o if (!PageDirty(page) || 25934f01b02cSTheodore Ts'o (PageWriteback(page) && 25944e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 25954f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 25968e48dcfbSTheodore Ts'o unlock_page(page); 25978e48dcfbSTheodore Ts'o continue; 25988e48dcfbSTheodore Ts'o } 25998e48dcfbSTheodore Ts'o 26008e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 26018e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 26028e48dcfbSTheodore Ts'o 26034e7ea81dSJan Kara if (mpd->map.m_len == 0) 26048eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 26058eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2606f8bec370SJan Kara /* Add all dirty buffers to mpd */ 26074e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 260809cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 26098eb9e5ceSTheodore Ts'o head = page_buffers(page); 26105f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26115f1132b2SJan Kara if (err <= 0) 26124e7ea81dSJan Kara goto out; 26135f1132b2SJan Kara err = 0; 2614aeac589aSMing Lei left--; 26158e48dcfbSTheodore Ts'o } 26168e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26178e48dcfbSTheodore Ts'o cond_resched(); 26188e48dcfbSTheodore Ts'o } 26196b8ed620SJan Kara mpd->scanned_until_end = 1; 26204f01b02cSTheodore Ts'o return 0; 26218eb9e5ceSTheodore Ts'o out: 26228eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26234e7ea81dSJan Kara return err; 26248e48dcfbSTheodore Ts'o } 26258e48dcfbSTheodore Ts'o 262620970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 262764769240SAlex Tomas struct writeback_control *wbc) 262864769240SAlex Tomas { 26294e7ea81dSJan Kara pgoff_t writeback_index = 0; 26304e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 263122208dedSAneesh Kumar K.V int range_whole = 0; 26324e7ea81dSJan Kara int cycled = 1; 263361628a3fSMingming Cao handle_t *handle = NULL; 2634df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26355e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26366b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26375e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26381bce63d1SShaohua Li struct blk_plug plug; 2639cb530541STheodore Ts'o bool give_up_on_write = false; 264061628a3fSMingming Cao 26410db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26420db1ff22STheodore Ts'o return -EIO; 26430db1ff22STheodore Ts'o 2644bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 264520970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2646ba80b101STheodore Ts'o 264761628a3fSMingming Cao /* 264861628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 264961628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 265061628a3fSMingming Cao * because that could violate lock ordering on umount 265161628a3fSMingming Cao */ 2652a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2653bbf023c7SMing Lei goto out_writepages; 26542a21e37eSTheodore Ts'o 265520970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2656043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2657bbf023c7SMing Lei goto out_writepages; 265820970ba6STheodore Ts'o } 265920970ba6STheodore Ts'o 26602a21e37eSTheodore Ts'o /* 26612a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26622a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26632a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26641751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26652a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 266620970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26672a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26682a21e37eSTheodore Ts'o * the stack trace. 26692a21e37eSTheodore Ts'o */ 26700db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26719b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2672bbf023c7SMing Lei ret = -EROFS; 2673bbf023c7SMing Lei goto out_writepages; 2674bbf023c7SMing Lei } 26752a21e37eSTheodore Ts'o 26764e7ea81dSJan Kara /* 26774e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26784e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26794e7ea81dSJan Kara * we'd better clear the inline data here. 26804e7ea81dSJan Kara */ 26814e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26824e7ea81dSJan Kara /* Just inode will be modified... */ 26834e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26844e7ea81dSJan Kara if (IS_ERR(handle)) { 26854e7ea81dSJan Kara ret = PTR_ERR(handle); 26864e7ea81dSJan Kara goto out_writepages; 26874e7ea81dSJan Kara } 26884e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26894e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26904e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26914e7ea81dSJan Kara ext4_journal_stop(handle); 26924e7ea81dSJan Kara } 26934e7ea81dSJan Kara 26944e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26954e343231Syangerkun /* 26964e343231Syangerkun * We may need to convert up to one extent per block in 26974e343231Syangerkun * the page and we may dirty the inode. 26984e343231Syangerkun */ 26994e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 27004e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 27014e343231Syangerkun } 27024e343231Syangerkun 270322208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 270422208dedSAneesh Kumar K.V range_whole = 1; 270561628a3fSMingming Cao 27062acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 27074e7ea81dSJan Kara writeback_index = mapping->writeback_index; 27084e7ea81dSJan Kara if (writeback_index) 27092acf2c26SAneesh Kumar K.V cycled = 0; 27104e7ea81dSJan Kara mpd.first_page = writeback_index; 27114e7ea81dSJan Kara mpd.last_page = -1; 27125b41d924SEric Sandeen } else { 271309cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 271409cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27155b41d924SEric Sandeen } 2716a1d6cc56SAneesh Kumar K.V 27174e7ea81dSJan Kara mpd.inode = inode; 27184e7ea81dSJan Kara mpd.wbc = wbc; 27194e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27202acf2c26SAneesh Kumar K.V retry: 27216e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27224e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27231bce63d1SShaohua Li blk_start_plug(&plug); 2724dddbd6acSJan Kara 2725dddbd6acSJan Kara /* 2726dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2727dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2728dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2729dddbd6acSJan Kara * started. 2730dddbd6acSJan Kara */ 2731dddbd6acSJan Kara mpd.do_map = 0; 27326b8ed620SJan Kara mpd.scanned_until_end = 0; 2733dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2734dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2735dddbd6acSJan Kara ret = -ENOMEM; 2736dddbd6acSJan Kara goto unplug; 2737dddbd6acSJan Kara } 2738dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2739a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2740a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2741dddbd6acSJan Kara /* Submit prepared bio */ 2742dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2743dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2744dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2745dddbd6acSJan Kara if (ret < 0) 2746dddbd6acSJan Kara goto unplug; 2747dddbd6acSJan Kara 27486b8ed620SJan Kara while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { 27494e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27504e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27514e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27524e7ea81dSJan Kara ret = -ENOMEM; 27534e7ea81dSJan Kara break; 27544e7ea81dSJan Kara } 2755a1d6cc56SAneesh Kumar K.V 2756a1d6cc56SAneesh Kumar K.V /* 27574e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27584e7ea81dSJan Kara * must always write out whole page (makes a difference when 27594e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27604e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27614e7ea81dSJan Kara * not supported by delalloc. 2762a1d6cc56SAneesh Kumar K.V */ 2763a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2764525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2765a1d6cc56SAneesh Kumar K.V 276661628a3fSMingming Cao /* start a new transaction */ 27676b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27686b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 276961628a3fSMingming Cao if (IS_ERR(handle)) { 277061628a3fSMingming Cao ret = PTR_ERR(handle); 27711693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2772fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2773a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27744e7ea81dSJan Kara /* Release allocated io_end */ 27754e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2776dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27774e7ea81dSJan Kara break; 277861628a3fSMingming Cao } 2779dddbd6acSJan Kara mpd.do_map = 1; 2780f63e6005STheodore Ts'o 27814e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27824e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27836b8ed620SJan Kara if (!ret && mpd.map.m_len) 2784cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2785cb530541STheodore Ts'o &give_up_on_write); 2786646caa9cSJan Kara /* 2787646caa9cSJan Kara * Caution: If the handle is synchronous, 2788646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2789646caa9cSJan Kara * to finish which may depend on writeback of pages to 2790646caa9cSJan Kara * complete or on page lock to be released. In that 2791b483bb77SRandy Dunlap * case, we have to wait until after we have 2792646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2793646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2794646caa9cSJan Kara * to be able to complete) before stopping the handle. 2795646caa9cSJan Kara */ 2796646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 279761628a3fSMingming Cao ext4_journal_stop(handle); 2798646caa9cSJan Kara handle = NULL; 2799dddbd6acSJan Kara mpd.do_map = 0; 2800646caa9cSJan Kara } 28014e7ea81dSJan Kara /* Unlock pages we didn't use */ 2802cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2803a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2804a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2805a297b2fcSXiaoguang Wang 2806646caa9cSJan Kara /* 2807646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2808646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2809646caa9cSJan Kara * we are still holding the transaction as we can 2810646caa9cSJan Kara * release the last reference to io_end which may end 2811646caa9cSJan Kara * up doing unwritten extent conversion. 2812646caa9cSJan Kara */ 2813646caa9cSJan Kara if (handle) { 2814646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2815646caa9cSJan Kara ext4_journal_stop(handle); 2816646caa9cSJan Kara } else 28174e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2818dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2819df22291fSAneesh Kumar K.V 28204e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28214e7ea81dSJan Kara /* 28224e7ea81dSJan Kara * Commit the transaction which would 282322208dedSAneesh Kumar K.V * free blocks released in the transaction 282422208dedSAneesh Kumar K.V * and try again 282522208dedSAneesh Kumar K.V */ 2826df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 282722208dedSAneesh Kumar K.V ret = 0; 28284e7ea81dSJan Kara continue; 28294e7ea81dSJan Kara } 28304e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28314e7ea81dSJan Kara if (ret) 283261628a3fSMingming Cao break; 283361628a3fSMingming Cao } 2834dddbd6acSJan Kara unplug: 28351bce63d1SShaohua Li blk_finish_plug(&plug); 28369c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28372acf2c26SAneesh Kumar K.V cycled = 1; 28384e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28394e7ea81dSJan Kara mpd.first_page = 0; 28402acf2c26SAneesh Kumar K.V goto retry; 28412acf2c26SAneesh Kumar K.V } 284261628a3fSMingming Cao 284322208dedSAneesh Kumar K.V /* Update index */ 284422208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 284522208dedSAneesh Kumar K.V /* 28464e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 284722208dedSAneesh Kumar K.V * mode will write it back later 284822208dedSAneesh Kumar K.V */ 28494e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2850a1d6cc56SAneesh Kumar K.V 285161628a3fSMingming Cao out_writepages: 285220970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28534e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2854bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 285561628a3fSMingming Cao return ret; 285664769240SAlex Tomas } 285764769240SAlex Tomas 28585f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28595f0663bbSDan Williams struct writeback_control *wbc) 28605f0663bbSDan Williams { 28615f0663bbSDan Williams int ret; 28625f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28635f0663bbSDan Williams struct inode *inode = mapping->host; 28645f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28655f0663bbSDan Williams 28665f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28675f0663bbSDan Williams return -EIO; 28685f0663bbSDan Williams 2869bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28705f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28715f0663bbSDan Williams 28723f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28735f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28745f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2875bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28765f0663bbSDan Williams return ret; 28775f0663bbSDan Williams } 28785f0663bbSDan Williams 287979f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 288079f0be8dSAneesh Kumar K.V { 28815c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 288279f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 288379f0be8dSAneesh Kumar K.V 288479f0be8dSAneesh Kumar K.V /* 288579f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 288679f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2887179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 288879f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 288979f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 289079f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 289179f0be8dSAneesh Kumar K.V */ 28925c1ff336SEric Whitney free_clusters = 28935c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28945c1ff336SEric Whitney dirty_clusters = 28955c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 289600d4e736STheodore Ts'o /* 289700d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 289800d4e736STheodore Ts'o */ 28995c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 290010ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 290100d4e736STheodore Ts'o 29025c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29035c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 290479f0be8dSAneesh Kumar K.V /* 2905c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2906c8afb446SEric Sandeen * or free blocks is less than watermark 290779f0be8dSAneesh Kumar K.V */ 290879f0be8dSAneesh Kumar K.V return 1; 290979f0be8dSAneesh Kumar K.V } 291079f0be8dSAneesh Kumar K.V return 0; 291179f0be8dSAneesh Kumar K.V } 291279f0be8dSAneesh Kumar K.V 29130ff8947fSEric Sandeen /* We always reserve for an inode update; the superblock could be there too */ 29140ff8947fSEric Sandeen static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) 29150ff8947fSEric Sandeen { 2916e2b911c5SDarrick J. Wong if (likely(ext4_has_feature_large_file(inode->i_sb))) 29170ff8947fSEric Sandeen return 1; 29180ff8947fSEric Sandeen 29190ff8947fSEric Sandeen if (pos + len <= 0x7fffffffULL) 29200ff8947fSEric Sandeen return 1; 29210ff8947fSEric Sandeen 29220ff8947fSEric Sandeen /* We might need to update the superblock to set LARGE_FILE */ 29230ff8947fSEric Sandeen return 2; 29240ff8947fSEric Sandeen } 29250ff8947fSEric Sandeen 292664769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 292764769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 292864769240SAlex Tomas struct page **pagep, void **fsdata) 292964769240SAlex Tomas { 293072b8ab9dSEric Sandeen int ret, retries = 0; 293164769240SAlex Tomas struct page *page; 293264769240SAlex Tomas pgoff_t index; 293364769240SAlex Tomas struct inode *inode = mapping->host; 293464769240SAlex Tomas handle_t *handle; 293564769240SAlex Tomas 29360db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29370db1ff22STheodore Ts'o return -EIO; 29380db1ff22STheodore Ts'o 293909cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 294079f0be8dSAneesh Kumar K.V 2941c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2942c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 294379f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 294479f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 294579f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 294679f0be8dSAneesh Kumar K.V } 294779f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29489bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29499c3569b5STao Ma 29509c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29519c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29529c3569b5STao Ma pos, len, flags, 29539c3569b5STao Ma pagep, fsdata); 29549c3569b5STao Ma if (ret < 0) 295547564bfbSTheodore Ts'o return ret; 295647564bfbSTheodore Ts'o if (ret == 1) 295747564bfbSTheodore Ts'o return 0; 29589c3569b5STao Ma } 29599c3569b5STao Ma 296047564bfbSTheodore Ts'o /* 296147564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 296247564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 296347564bfbSTheodore Ts'o * is being written back. So grab it first before we start 296447564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 296547564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 296647564bfbSTheodore Ts'o */ 296747564bfbSTheodore Ts'o retry_grab: 296847564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 296947564bfbSTheodore Ts'o if (!page) 297047564bfbSTheodore Ts'o return -ENOMEM; 297147564bfbSTheodore Ts'o unlock_page(page); 297247564bfbSTheodore Ts'o 297364769240SAlex Tomas /* 297464769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 297564769240SAlex Tomas * if there is delayed block allocation. But we still need 297664769240SAlex Tomas * to journalling the i_disksize update if writes to the end 297764769240SAlex Tomas * of file which has an already mapped buffer. 297864769240SAlex Tomas */ 297947564bfbSTheodore Ts'o retry_journal: 29800ff8947fSEric Sandeen handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 29810ff8947fSEric Sandeen ext4_da_write_credits(inode, pos, len)); 298264769240SAlex Tomas if (IS_ERR(handle)) { 298309cbfeafSKirill A. Shutemov put_page(page); 298447564bfbSTheodore Ts'o return PTR_ERR(handle); 298564769240SAlex Tomas } 298664769240SAlex Tomas 298747564bfbSTheodore Ts'o lock_page(page); 298847564bfbSTheodore Ts'o if (page->mapping != mapping) { 298947564bfbSTheodore Ts'o /* The page got truncated from under us */ 299047564bfbSTheodore Ts'o unlock_page(page); 299109cbfeafSKirill A. Shutemov put_page(page); 2992d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 299347564bfbSTheodore Ts'o goto retry_grab; 2994d5a0d4f7SEric Sandeen } 299547564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29967afe5aa5SDmitry Monakhov wait_for_stable_page(page); 299764769240SAlex Tomas 2998643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29992058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 30002058f83aSMichael Halcrow ext4_da_get_block_prep); 30012058f83aSMichael Halcrow #else 30026e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 30032058f83aSMichael Halcrow #endif 300464769240SAlex Tomas if (ret < 0) { 300564769240SAlex Tomas unlock_page(page); 300664769240SAlex Tomas ext4_journal_stop(handle); 3007ae4d5372SAneesh Kumar K.V /* 3008ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3009ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3010ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3011ae4d5372SAneesh Kumar K.V */ 3012ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3013b9a4207dSJan Kara ext4_truncate_failed_write(inode); 301447564bfbSTheodore Ts'o 301547564bfbSTheodore Ts'o if (ret == -ENOSPC && 301647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 301747564bfbSTheodore Ts'o goto retry_journal; 301847564bfbSTheodore Ts'o 301909cbfeafSKirill A. Shutemov put_page(page); 302047564bfbSTheodore Ts'o return ret; 302164769240SAlex Tomas } 302264769240SAlex Tomas 302347564bfbSTheodore Ts'o *pagep = page; 302464769240SAlex Tomas return ret; 302564769240SAlex Tomas } 302664769240SAlex Tomas 3027632eaeabSMingming Cao /* 3028632eaeabSMingming Cao * Check if we should update i_disksize 3029632eaeabSMingming Cao * when write to the end of file but not require block allocation 3030632eaeabSMingming Cao */ 3031632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3032632eaeabSMingming Cao unsigned long offset) 3033632eaeabSMingming Cao { 3034632eaeabSMingming Cao struct buffer_head *bh; 3035632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3036632eaeabSMingming Cao unsigned int idx; 3037632eaeabSMingming Cao int i; 3038632eaeabSMingming Cao 3039632eaeabSMingming Cao bh = page_buffers(page); 3040632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3041632eaeabSMingming Cao 3042632eaeabSMingming Cao for (i = 0; i < idx; i++) 3043632eaeabSMingming Cao bh = bh->b_this_page; 3044632eaeabSMingming Cao 304529fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3046632eaeabSMingming Cao return 0; 3047632eaeabSMingming Cao return 1; 3048632eaeabSMingming Cao } 3049632eaeabSMingming Cao 305064769240SAlex Tomas static int ext4_da_write_end(struct file *file, 305164769240SAlex Tomas struct address_space *mapping, 305264769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 305364769240SAlex Tomas struct page *page, void *fsdata) 305464769240SAlex Tomas { 305564769240SAlex Tomas struct inode *inode = mapping->host; 3056*6984aef5SZhang Yi int ret; 305764769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 305864769240SAlex Tomas loff_t new_i_size; 3059632eaeabSMingming Cao unsigned long start, end; 306079f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 306179f0be8dSAneesh Kumar K.V 306274d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 306374d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 306479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3065632eaeabSMingming Cao 30669bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 3067*6984aef5SZhang Yi 3068*6984aef5SZhang Yi if (write_mode != CONVERT_INLINE_DATA && 3069*6984aef5SZhang Yi ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 3070*6984aef5SZhang Yi ext4_has_inline_data(inode)) 3071*6984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 3072*6984aef5SZhang Yi 307309cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 3074632eaeabSMingming Cao end = start + copied - 1; 307564769240SAlex Tomas 307664769240SAlex Tomas /* 30774df031ffSZhang Yi * Since we are holding inode lock, we are sure i_disksize <= 30784df031ffSZhang Yi * i_size. We also know that if i_disksize < i_size, there are 30794df031ffSZhang Yi * delalloc writes pending in the range upto i_size. If the end of 30804df031ffSZhang Yi * the current write is <= i_size, there's no need to touch 30814df031ffSZhang Yi * i_disksize since writeback will push i_disksize upto i_size 30824df031ffSZhang Yi * eventually. If the end of the current write is > i_size and 30834df031ffSZhang Yi * inside an allocated block (ext4_da_should_update_i_disksize() 30844df031ffSZhang Yi * check), we need to update i_disksize here as neither 30854df031ffSZhang Yi * ext4_writepage() nor certain ext4_writepages() paths not 30864df031ffSZhang Yi * allocating blocks update i_disksize. 30874df031ffSZhang Yi * 30884df031ffSZhang Yi * Note that we defer inode dirtying to generic_write_end() / 30894df031ffSZhang Yi * ext4_da_write_inline_data_end(). 309064769240SAlex Tomas */ 309164769240SAlex Tomas new_i_size = pos + copied; 3092*6984aef5SZhang Yi if (copied && new_i_size > inode->i_size && 30934df031ffSZhang Yi ext4_da_should_update_i_disksize(page, end)) 3094ee124d27SDmitry Monakhov ext4_update_i_disksize(inode, new_i_size); 30959c3569b5STao Ma 3096*6984aef5SZhang Yi copied = generic_write_end(file, mapping, pos, len, copied, page, fsdata); 3097*6984aef5SZhang Yi ret = ext4_journal_stop(handle); 309864769240SAlex Tomas return ret ? ret : copied; 309964769240SAlex Tomas } 310064769240SAlex Tomas 3101ccd2506bSTheodore Ts'o /* 3102ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3103ccd2506bSTheodore Ts'o */ 3104ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3105ccd2506bSTheodore Ts'o { 3106fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3107fb40ba0dSTheodore Ts'o 310871d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3109ccd2506bSTheodore Ts'o return 0; 3110ccd2506bSTheodore Ts'o 3111ccd2506bSTheodore Ts'o /* 3112ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3113ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3114ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3115ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3116ccd2506bSTheodore Ts'o * would require replicating code paths in: 3117ccd2506bSTheodore Ts'o * 311820970ba6STheodore Ts'o * ext4_writepages() -> 3119ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3120ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3121ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3122ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3123ccd2506bSTheodore Ts'o * 3124ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3125ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3126ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3127ccd2506bSTheodore Ts'o * doing I/O at all. 3128ccd2506bSTheodore Ts'o * 3129ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3130380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3131ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3132ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 313325985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3134ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3135ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3136ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3137ccd2506bSTheodore Ts'o * 3138ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3139ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3140ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3141ccd2506bSTheodore Ts'o */ 3142ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3143ccd2506bSTheodore Ts'o } 314464769240SAlex Tomas 314564769240SAlex Tomas /* 3146ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3147ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3148ac27a0ecSDave Kleikamp * 3149ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3150617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3151ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3152ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3153ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3154ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3155ac27a0ecSDave Kleikamp * 3156ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3157ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3158ac27a0ecSDave Kleikamp */ 3159617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3160ac27a0ecSDave Kleikamp { 3161ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3162ac27a0ecSDave Kleikamp journal_t *journal; 3163ac27a0ecSDave Kleikamp int err; 3164ac27a0ecSDave Kleikamp 316546c7f254STao Ma /* 316646c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 316746c7f254STao Ma */ 316846c7f254STao Ma if (ext4_has_inline_data(inode)) 316946c7f254STao Ma return 0; 317046c7f254STao Ma 317164769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 317264769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 317364769240SAlex Tomas /* 317464769240SAlex Tomas * With delalloc we want to sync the file 317564769240SAlex Tomas * so that we can make sure we allocate 317664769240SAlex Tomas * blocks for file 317764769240SAlex Tomas */ 317864769240SAlex Tomas filemap_write_and_wait(mapping); 317964769240SAlex Tomas } 318064769240SAlex Tomas 318119f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 318219f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3183ac27a0ecSDave Kleikamp /* 3184ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3185ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3186ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3187ac27a0ecSDave Kleikamp * do we expect this to happen. 3188ac27a0ecSDave Kleikamp * 3189ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3190ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3191ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3192ac27a0ecSDave Kleikamp * will.) 3193ac27a0ecSDave Kleikamp * 3194617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3195ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3196ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3197ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3198ac27a0ecSDave Kleikamp * everything they get. 3199ac27a0ecSDave Kleikamp */ 3200ac27a0ecSDave Kleikamp 320119f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3202617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3203dab291afSMingming Cao jbd2_journal_lock_updates(journal); 320401d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 3205dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3206ac27a0ecSDave Kleikamp 3207ac27a0ecSDave Kleikamp if (err) 3208ac27a0ecSDave Kleikamp return 0; 3209ac27a0ecSDave Kleikamp } 3210ac27a0ecSDave Kleikamp 3211ac58e4fbSRitesh Harjani return iomap_bmap(mapping, block, &ext4_iomap_ops); 3212ac27a0ecSDave Kleikamp } 3213ac27a0ecSDave Kleikamp 3214617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3215ac27a0ecSDave Kleikamp { 321646c7f254STao Ma int ret = -EAGAIN; 321746c7f254STao Ma struct inode *inode = page->mapping->host; 321846c7f254STao Ma 32190562e0baSJiaying Zhang trace_ext4_readpage(page); 322046c7f254STao Ma 322146c7f254STao Ma if (ext4_has_inline_data(inode)) 322246c7f254STao Ma ret = ext4_readpage_inline(inode, page); 322346c7f254STao Ma 322446c7f254STao Ma if (ret == -EAGAIN) 3225a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 322646c7f254STao Ma 322746c7f254STao Ma return ret; 3228ac27a0ecSDave Kleikamp } 3229ac27a0ecSDave Kleikamp 32306311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3231ac27a0ecSDave Kleikamp { 32326311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 323346c7f254STao Ma 32346311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 323546c7f254STao Ma if (ext4_has_inline_data(inode)) 32366311f91fSMatthew Wilcox (Oracle) return; 323746c7f254STao Ma 3238a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3239ac27a0ecSDave Kleikamp } 3240ac27a0ecSDave Kleikamp 3241d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3242d47992f8SLukas Czerner unsigned int length) 3243ac27a0ecSDave Kleikamp { 3244ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32450562e0baSJiaying Zhang 32464520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32474520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32484520fb3cSJan Kara 3249ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32504520fb3cSJan Kara } 32514520fb3cSJan Kara 325253e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3253ca99fdd2SLukas Czerner unsigned int offset, 3254ca99fdd2SLukas Czerner unsigned int length) 32554520fb3cSJan Kara { 32564520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32574520fb3cSJan Kara 3258ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32594520fb3cSJan Kara 3260744692dcSJiaying Zhang /* 3261ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3262ac27a0ecSDave Kleikamp */ 326309cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3264ac27a0ecSDave Kleikamp ClearPageChecked(page); 3265ac27a0ecSDave Kleikamp 3266ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 326753e87268SJan Kara } 326853e87268SJan Kara 326953e87268SJan Kara /* Wrapper for aops... */ 327053e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3271d47992f8SLukas Czerner unsigned int offset, 3272d47992f8SLukas Czerner unsigned int length) 327353e87268SJan Kara { 3274ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3275ac27a0ecSDave Kleikamp } 3276ac27a0ecSDave Kleikamp 3277617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3278ac27a0ecSDave Kleikamp { 3279617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3280ac27a0ecSDave Kleikamp 32810562e0baSJiaying Zhang trace_ext4_releasepage(page); 32820562e0baSJiaying Zhang 3283e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3284e1c36595SJan Kara if (PageChecked(page)) 3285ac27a0ecSDave Kleikamp return 0; 32860390131bSFrank Mayhar if (journal) 3287529a781eSzhangyi (F) return jbd2_journal_try_to_free_buffers(journal, page); 32880390131bSFrank Mayhar else 32890390131bSFrank Mayhar return try_to_free_buffers(page); 3290ac27a0ecSDave Kleikamp } 3291ac27a0ecSDave Kleikamp 3292b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3293b8a6176cSJan Kara { 3294b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3295b8a6176cSJan Kara 3296aa75f4d3SHarshad Shirwadkar if (journal) { 3297aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3298aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3299d0520df7SAndrea Righi return false; 3300d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 33011ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3302d0520df7SAndrea Righi return true; 3303aa75f4d3SHarshad Shirwadkar } 3304aa75f4d3SHarshad Shirwadkar 3305b8a6176cSJan Kara /* Any metadata buffers to write? */ 3306b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3307b8a6176cSJan Kara return true; 3308b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3309b8a6176cSJan Kara } 3310b8a6176cSJan Kara 3311c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3312c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3313c8fdfe29SMatthew Bobrowski loff_t length) 3314364443cbSJan Kara { 3315c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3316c8fdfe29SMatthew Bobrowski 3317c8fdfe29SMatthew Bobrowski /* 3318c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3319c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3320c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3321c8fdfe29SMatthew Bobrowski */ 3322c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3323c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3324c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3325c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3326c8fdfe29SMatthew Bobrowski 3327c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3328c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3329c8fdfe29SMatthew Bobrowski 3330c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3331c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3332c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3333c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3334c8fdfe29SMatthew Bobrowski 33356386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 33366386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 33376386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 33386386722aSRitesh Harjani 3339c8fdfe29SMatthew Bobrowski /* 3340c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3341c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3342c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3343c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3344c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3345c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3346c8fdfe29SMatthew Bobrowski * been set first. 3347c8fdfe29SMatthew Bobrowski */ 3348c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3349c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3350c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3351c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3352c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3353c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3354c8fdfe29SMatthew Bobrowski } else { 3355c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3356c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3357c8fdfe29SMatthew Bobrowski } 3358c8fdfe29SMatthew Bobrowski } 3359c8fdfe29SMatthew Bobrowski 3360f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3361f063db5eSMatthew Bobrowski unsigned int flags) 3362f063db5eSMatthew Bobrowski { 3363f063db5eSMatthew Bobrowski handle_t *handle; 3364378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3365378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3366f063db5eSMatthew Bobrowski 3367f063db5eSMatthew Bobrowski /* 3368f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3369f063db5eSMatthew Bobrowski * once for direct I/O. 3370f063db5eSMatthew Bobrowski */ 3371f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3372f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3373f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3374f063db5eSMatthew Bobrowski 3375f063db5eSMatthew Bobrowski retry: 3376f063db5eSMatthew Bobrowski /* 3377f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3378f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3379f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3380f063db5eSMatthew Bobrowski * fits into the credits as well. 3381f063db5eSMatthew Bobrowski */ 3382f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3383f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3384f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3385f063db5eSMatthew Bobrowski 3386378f32baSMatthew Bobrowski /* 3387378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3388378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3389378f32baSMatthew Bobrowski */ 3390378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3391378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3392378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3393378f32baSMatthew Bobrowski /* 3394378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3395378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3396378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3397378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3398378f32baSMatthew Bobrowski */ 3399d0b040f5SJan Kara else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3400378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3401378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3402378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3403378f32baSMatthew Bobrowski 3404378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3405378f32baSMatthew Bobrowski 3406378f32baSMatthew Bobrowski /* 3407378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3408378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3409378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3410378f32baSMatthew Bobrowski */ 3411378f32baSMatthew Bobrowski if (!m_flags && !ret) 3412378f32baSMatthew Bobrowski ret = -ENOTBLK; 3413f063db5eSMatthew Bobrowski 3414f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3415f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3416f063db5eSMatthew Bobrowski goto retry; 3417f063db5eSMatthew Bobrowski 3418f063db5eSMatthew Bobrowski return ret; 3419f063db5eSMatthew Bobrowski } 3420f063db5eSMatthew Bobrowski 3421f063db5eSMatthew Bobrowski 3422364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3423c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3424364443cbSJan Kara { 3425364443cbSJan Kara int ret; 342609edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 342709edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3428364443cbSJan Kara 3429bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3430bcd8e91fSTheodore Ts'o return -EINVAL; 34317046ae35SAndreas Gruenbacher 3432364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3433364443cbSJan Kara return -ERANGE; 3434364443cbSJan Kara 343509edf4d3SMatthew Bobrowski /* 343609edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 343709edf4d3SMatthew Bobrowski */ 343809edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 343909edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 344009edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3441364443cbSJan Kara 34429faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 34439faac62dSRitesh Harjani /* 34449faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 34459faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 34469faac62dSRitesh Harjani * the mapping information. This could boost performance 34479faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 34489faac62dSRitesh Harjani */ 34499faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3450545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 34519faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 34529faac62dSRitesh Harjani goto out; 34539faac62dSRitesh Harjani } 34549faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 34559faac62dSRitesh Harjani } else { 34569faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 34579faac62dSRitesh Harjani } 3458f063db5eSMatthew Bobrowski 3459545052e9SChristoph Hellwig if (ret < 0) 3460545052e9SChristoph Hellwig return ret; 34619faac62dSRitesh Harjani out: 3462c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3463545052e9SChristoph Hellwig 3464364443cbSJan Kara return 0; 3465364443cbSJan Kara } 3466364443cbSJan Kara 34678cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34688cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34698cd115bdSJan Kara struct iomap *srcmap) 34708cd115bdSJan Kara { 34718cd115bdSJan Kara int ret; 34728cd115bdSJan Kara 34738cd115bdSJan Kara /* 34748cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34758cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34768cd115bdSJan Kara */ 34778cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34788cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34798cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34808cd115bdSJan Kara return ret; 34818cd115bdSJan Kara } 34828cd115bdSJan Kara 3483776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3484776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3485776722e8SJan Kara { 3486378f32baSMatthew Bobrowski /* 3487378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3488378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3489378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3490378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3491378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3492378f32baSMatthew Bobrowski */ 3493378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3494378f32baSMatthew Bobrowski return -ENOTBLK; 3495378f32baSMatthew Bobrowski 3496776722e8SJan Kara return 0; 3497776722e8SJan Kara } 3498776722e8SJan Kara 34998ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3500364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3501776722e8SJan Kara .iomap_end = ext4_iomap_end, 3502364443cbSJan Kara }; 3503364443cbSJan Kara 35048cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 35058cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 35068cd115bdSJan Kara .iomap_end = ext4_iomap_end, 35078cd115bdSJan Kara }; 35088cd115bdSJan Kara 350909edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 351009edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 351109edf4d3SMatthew Bobrowski { 351209edf4d3SMatthew Bobrowski struct extent_status es; 351309edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 351409edf4d3SMatthew Bobrowski 351509edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 351609edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 351709edf4d3SMatthew Bobrowski 351809edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 351909edf4d3SMatthew Bobrowski return false; 352009edf4d3SMatthew Bobrowski 352109edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 352209edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 352309edf4d3SMatthew Bobrowski return false; 352409edf4d3SMatthew Bobrowski } 352509edf4d3SMatthew Bobrowski 352609edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 352709edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 352809edf4d3SMatthew Bobrowski 352909edf4d3SMatthew Bobrowski return true; 353009edf4d3SMatthew Bobrowski } 353109edf4d3SMatthew Bobrowski 353209edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 353309edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 353409edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 353509edf4d3SMatthew Bobrowski { 353609edf4d3SMatthew Bobrowski int ret; 353709edf4d3SMatthew Bobrowski bool delalloc = false; 353809edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 353909edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 354009edf4d3SMatthew Bobrowski 354109edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 354209edf4d3SMatthew Bobrowski return -EINVAL; 354309edf4d3SMatthew Bobrowski 35447cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35457cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3546ba5843f5SJan Kara if (ret != -EAGAIN) { 3547ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3548ba5843f5SJan Kara ret = -ENOENT; 3549ba5843f5SJan Kara return ret; 3550ba5843f5SJan Kara } 3551ba5843f5SJan Kara } 355212735f88SJan Kara 355309edf4d3SMatthew Bobrowski /* 355409edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 355509edf4d3SMatthew Bobrowski */ 355609edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 355709edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 355809edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 355912735f88SJan Kara 3560b2c57642SRitesh Harjani /* 3561b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3562b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3563b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3564b2c57642SRitesh Harjani * -EIO error. 3565b2c57642SRitesh Harjani */ 3566b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3567b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3568b2c57642SRitesh Harjani 3569b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3570b2c57642SRitesh Harjani map.m_flags = 0; 3571b2c57642SRitesh Harjani goto set_iomap; 3572b2c57642SRitesh Harjani } 3573b2c57642SRitesh Harjani } 3574b2c57642SRitesh Harjani 357512735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3576ba5843f5SJan Kara if (ret < 0) 3577ba5843f5SJan Kara return ret; 3578914f82a3SJan Kara if (ret == 0) 357909edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 358009edf4d3SMatthew Bobrowski 3581b2c57642SRitesh Harjani set_iomap: 358209edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 358309edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 358409edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 358509edf4d3SMatthew Bobrowski 358609edf4d3SMatthew Bobrowski return 0; 3587914f82a3SJan Kara } 3588914f82a3SJan Kara 358909edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 359009edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 359109edf4d3SMatthew Bobrowski }; 35924c0425ffSMingming Cao 3593ac27a0ecSDave Kleikamp /* 3594617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3595ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3596ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3597ac27a0ecSDave Kleikamp * not necessarily locked. 3598ac27a0ecSDave Kleikamp * 3599ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3600ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3601ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3602ac27a0ecSDave Kleikamp * 3603ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3604ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3605ac27a0ecSDave Kleikamp */ 3606617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3607ac27a0ecSDave Kleikamp { 3608ac27a0ecSDave Kleikamp SetPageChecked(page); 3609ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3610ac27a0ecSDave Kleikamp } 3611ac27a0ecSDave Kleikamp 36126dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 36136dcc693bSJan Kara { 36146dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 36156dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 36166dcc693bSJan Kara return __set_page_dirty_buffers(page); 36176dcc693bSJan Kara } 36186dcc693bSJan Kara 36190e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 36200e6895baSRitesh Harjani struct file *file, sector_t *span) 36210e6895baSRitesh Harjani { 36220e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 36230e6895baSRitesh Harjani &ext4_iomap_report_ops); 36240e6895baSRitesh Harjani } 36250e6895baSRitesh Harjani 362674d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3627617ba13bSMingming Cao .readpage = ext4_readpage, 36286311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 362943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 363020970ba6STheodore Ts'o .writepages = ext4_writepages, 3631bfc1af65SNick Piggin .write_begin = ext4_write_begin, 363274d553aaSTheodore Ts'o .write_end = ext4_write_end, 36336dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3634617ba13bSMingming Cao .bmap = ext4_bmap, 3635617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3636617ba13bSMingming Cao .releasepage = ext4_releasepage, 3637378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3638ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 36398ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3640aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36410e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3642ac27a0ecSDave Kleikamp }; 3643ac27a0ecSDave Kleikamp 3644617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3645617ba13bSMingming Cao .readpage = ext4_readpage, 36466311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 364743ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 364820970ba6STheodore Ts'o .writepages = ext4_writepages, 3649bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3650bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3651617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3652617ba13bSMingming Cao .bmap = ext4_bmap, 36534520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3654617ba13bSMingming Cao .releasepage = ext4_releasepage, 3655378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 36568ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3657aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36580e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3659ac27a0ecSDave Kleikamp }; 3660ac27a0ecSDave Kleikamp 366164769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 366264769240SAlex Tomas .readpage = ext4_readpage, 36636311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 366443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 366520970ba6STheodore Ts'o .writepages = ext4_writepages, 366664769240SAlex Tomas .write_begin = ext4_da_write_begin, 366764769240SAlex Tomas .write_end = ext4_da_write_end, 36686dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 366964769240SAlex Tomas .bmap = ext4_bmap, 36708fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 367164769240SAlex Tomas .releasepage = ext4_releasepage, 3672378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 367364769240SAlex Tomas .migratepage = buffer_migrate_page, 36748ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3675aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36760e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 367764769240SAlex Tomas }; 367864769240SAlex Tomas 36795f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36805f0663bbSDan Williams .writepages = ext4_dax_writepages, 36815f0663bbSDan Williams .direct_IO = noop_direct_IO, 3682b82a96c9SMatthew Wilcox (Oracle) .set_page_dirty = __set_page_dirty_no_writeback, 368394dbb631SToshi Kani .bmap = ext4_bmap, 36845f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36850e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 36865f0663bbSDan Williams }; 36875f0663bbSDan Williams 3688617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3689ac27a0ecSDave Kleikamp { 36903d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36913d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36923d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36933d2b1582SLukas Czerner break; 36943d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3695617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 369674d553aaSTheodore Ts'o return; 36973d2b1582SLukas Czerner default: 36983d2b1582SLukas Czerner BUG(); 36993d2b1582SLukas Czerner } 37005f0663bbSDan Williams if (IS_DAX(inode)) 37015f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 37025f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 370374d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 370474d553aaSTheodore Ts'o else 370574d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3706ac27a0ecSDave Kleikamp } 3707ac27a0ecSDave Kleikamp 3708923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3709d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3710d863dc36SLukas Czerner { 371109cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 371209cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3713923ae0ffSRoss Zwisler unsigned blocksize, pos; 3714d863dc36SLukas Czerner ext4_lblk_t iblock; 3715d863dc36SLukas Czerner struct inode *inode = mapping->host; 3716d863dc36SLukas Czerner struct buffer_head *bh; 3717d863dc36SLukas Czerner struct page *page; 3718d863dc36SLukas Czerner int err = 0; 3719d863dc36SLukas Czerner 372009cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3721c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3722d863dc36SLukas Czerner if (!page) 3723d863dc36SLukas Czerner return -ENOMEM; 3724d863dc36SLukas Czerner 3725d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3726d863dc36SLukas Czerner 372709cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3728d863dc36SLukas Czerner 3729d863dc36SLukas Czerner if (!page_has_buffers(page)) 3730d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3731d863dc36SLukas Czerner 3732d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3733d863dc36SLukas Czerner bh = page_buffers(page); 3734d863dc36SLukas Czerner pos = blocksize; 3735d863dc36SLukas Czerner while (offset >= pos) { 3736d863dc36SLukas Czerner bh = bh->b_this_page; 3737d863dc36SLukas Czerner iblock++; 3738d863dc36SLukas Czerner pos += blocksize; 3739d863dc36SLukas Czerner } 3740d863dc36SLukas Czerner if (buffer_freed(bh)) { 3741d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3742d863dc36SLukas Czerner goto unlock; 3743d863dc36SLukas Czerner } 3744d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3745d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3746d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3747d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3748d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3749d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3750d863dc36SLukas Czerner goto unlock; 3751d863dc36SLukas Czerner } 3752d863dc36SLukas Czerner } 3753d863dc36SLukas Czerner 3754d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3755d863dc36SLukas Czerner if (PageUptodate(page)) 3756d863dc36SLukas Czerner set_buffer_uptodate(bh); 3757d863dc36SLukas Czerner 3758d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 37592d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 37602d069c08Szhangyi (F) if (err) 3761d863dc36SLukas Czerner goto unlock; 37624f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3763c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3764a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3765834f1565SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3766834f1565SEric Biggers bh_offset(bh)); 3767834f1565SEric Biggers if (err) { 3768834f1565SEric Biggers clear_buffer_uptodate(bh); 3769834f1565SEric Biggers goto unlock; 3770834f1565SEric Biggers } 3771c9c7429cSMichael Halcrow } 3772d863dc36SLukas Czerner } 3773d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3774d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3775d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3776d863dc36SLukas Czerner if (err) 3777d863dc36SLukas Czerner goto unlock; 3778d863dc36SLukas Czerner } 3779d863dc36SLukas Czerner zero_user(page, offset, length); 3780d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3781d863dc36SLukas Czerner 3782d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3783d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37840713ed0cSLukas Czerner } else { 3785353eefd3Sjon ernst err = 0; 3786d863dc36SLukas Czerner mark_buffer_dirty(bh); 37873957ef53SJan Kara if (ext4_should_order_data(inode)) 378873131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 378973131fbbSRoss Zwisler length); 37900713ed0cSLukas Czerner } 3791d863dc36SLukas Czerner 3792d863dc36SLukas Czerner unlock: 3793d863dc36SLukas Czerner unlock_page(page); 379409cbfeafSKirill A. Shutemov put_page(page); 3795d863dc36SLukas Czerner return err; 3796d863dc36SLukas Czerner } 3797d863dc36SLukas Czerner 379894350ab5SMatthew Wilcox /* 3799923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3800923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3801923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3802923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 38033088e5a5SBhaskar Chowdhury * that corresponds to 'from' 3804923ae0ffSRoss Zwisler */ 3805923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3806923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3807923ae0ffSRoss Zwisler { 3808923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 380909cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3810923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3811923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3812923ae0ffSRoss Zwisler 3813923ae0ffSRoss Zwisler /* 3814923ae0ffSRoss Zwisler * correct length if it does not fall between 3815923ae0ffSRoss Zwisler * 'from' and the end of the block 3816923ae0ffSRoss Zwisler */ 3817923ae0ffSRoss Zwisler if (length > max || length < 0) 3818923ae0ffSRoss Zwisler length = max; 3819923ae0ffSRoss Zwisler 382047e69351SJan Kara if (IS_DAX(inode)) { 382147e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 382247e69351SJan Kara &ext4_iomap_ops); 382347e69351SJan Kara } 3824923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3825923ae0ffSRoss Zwisler } 3826923ae0ffSRoss Zwisler 3827923ae0ffSRoss Zwisler /* 382894350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 382994350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 383094350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 383194350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 383294350ab5SMatthew Wilcox */ 3833c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 383494350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 383594350ab5SMatthew Wilcox { 383609cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 383794350ab5SMatthew Wilcox unsigned length; 383894350ab5SMatthew Wilcox unsigned blocksize; 383994350ab5SMatthew Wilcox struct inode *inode = mapping->host; 384094350ab5SMatthew Wilcox 38410d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3842592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 38430d06863fSTheodore Ts'o return 0; 38440d06863fSTheodore Ts'o 384594350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 384694350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 384794350ab5SMatthew Wilcox 384894350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 384994350ab5SMatthew Wilcox } 385094350ab5SMatthew Wilcox 3851a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3852a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3853a87dd18cSLukas Czerner { 3854a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3855a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3856e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3857a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3858a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3859a87dd18cSLukas Czerner int err = 0; 3860a87dd18cSLukas Czerner 3861e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3862e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3863e1be3a92SLukas Czerner 3864a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3865a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3866a87dd18cSLukas Czerner 3867a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3868e1be3a92SLukas Czerner if (start == end && 3869e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3870a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3871a87dd18cSLukas Czerner lstart, length); 3872a87dd18cSLukas Czerner return err; 3873a87dd18cSLukas Czerner } 3874a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3875e1be3a92SLukas Czerner if (partial_start) { 3876a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3877a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3878a87dd18cSLukas Czerner if (err) 3879a87dd18cSLukas Czerner return err; 3880a87dd18cSLukas Czerner } 3881a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3882e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3883a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3884e1be3a92SLukas Czerner byte_end - partial_end, 3885e1be3a92SLukas Czerner partial_end + 1); 3886a87dd18cSLukas Czerner return err; 3887a87dd18cSLukas Czerner } 3888a87dd18cSLukas Czerner 388991ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 389091ef4cafSDuane Griffin { 389191ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 389291ef4cafSDuane Griffin return 1; 389391ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 389491ef4cafSDuane Griffin return 1; 389591ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 389691ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 389791ef4cafSDuane Griffin return 0; 389891ef4cafSDuane Griffin } 389991ef4cafSDuane Griffin 3900ac27a0ecSDave Kleikamp /* 390101127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 390201127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 390301127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 390401127848SJan Kara * that will never happen after we truncate page cache. 390501127848SJan Kara */ 390601127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 390701127848SJan Kara loff_t len) 390801127848SJan Kara { 390901127848SJan Kara handle_t *handle; 39104209ae12SHarshad Shirwadkar int ret; 39114209ae12SHarshad Shirwadkar 391201127848SJan Kara loff_t size = i_size_read(inode); 391301127848SJan Kara 39145955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 391501127848SJan Kara if (offset > size || offset + len < size) 391601127848SJan Kara return 0; 391701127848SJan Kara 391801127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 391901127848SJan Kara return 0; 392001127848SJan Kara 392101127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 392201127848SJan Kara if (IS_ERR(handle)) 392301127848SJan Kara return PTR_ERR(handle); 392401127848SJan Kara ext4_update_i_disksize(inode, size); 39254209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 392601127848SJan Kara ext4_journal_stop(handle); 392701127848SJan Kara 39284209ae12SHarshad Shirwadkar return ret; 392901127848SJan Kara } 393001127848SJan Kara 3931b1f38217SRoss Zwisler static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3932430657b6SRoss Zwisler { 3933430657b6SRoss Zwisler up_write(&ei->i_mmap_sem); 3934430657b6SRoss Zwisler schedule(); 3935430657b6SRoss Zwisler down_write(&ei->i_mmap_sem); 3936430657b6SRoss Zwisler } 3937430657b6SRoss Zwisler 3938430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3939430657b6SRoss Zwisler { 3940430657b6SRoss Zwisler struct ext4_inode_info *ei = EXT4_I(inode); 3941430657b6SRoss Zwisler struct page *page; 3942430657b6SRoss Zwisler int error; 3943430657b6SRoss Zwisler 3944430657b6SRoss Zwisler if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3945430657b6SRoss Zwisler return -EINVAL; 3946430657b6SRoss Zwisler 3947430657b6SRoss Zwisler do { 3948430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3949430657b6SRoss Zwisler if (!page) 3950430657b6SRoss Zwisler return 0; 3951430657b6SRoss Zwisler 3952430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3953430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3954430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3955b1f38217SRoss Zwisler ext4_wait_dax_page(ei)); 3956b1f38217SRoss Zwisler } while (error == 0); 3957430657b6SRoss Zwisler 3958430657b6SRoss Zwisler return error; 3959430657b6SRoss Zwisler } 3960430657b6SRoss Zwisler 396101127848SJan Kara /* 3962cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3963a4bb6b64SAllison Henderson * associated with the given offset and length 3964a4bb6b64SAllison Henderson * 3965a4bb6b64SAllison Henderson * @inode: File inode 3966a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3967a4bb6b64SAllison Henderson * @len: The length of the hole 3968a4bb6b64SAllison Henderson * 39694907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3970a4bb6b64SAllison Henderson */ 3971a4bb6b64SAllison Henderson 3972aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3973a4bb6b64SAllison Henderson { 397426a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 397526a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 397626a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3977a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 397826a4c0c6STheodore Ts'o handle_t *handle; 397926a4c0c6STheodore Ts'o unsigned int credits; 39804209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 398126a4c0c6STheodore Ts'o 3982b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3983aaddea81SZheng Liu 3984c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3985c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3986c1e8220bSTheodore Ts'o down_write(&EXT4_I(inode)->i_mmap_sem); 3987c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3988c1e8220bSTheodore Ts'o up_write(&EXT4_I(inode)->i_mmap_sem); 3989c1e8220bSTheodore Ts'o if (ret) 3990c1e8220bSTheodore Ts'o return ret; 3991c1e8220bSTheodore Ts'o } 3992c1e8220bSTheodore Ts'o 399326a4c0c6STheodore Ts'o /* 399426a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 399526a4c0c6STheodore Ts'o * Then release them. 399626a4c0c6STheodore Ts'o */ 3997cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 399826a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 399926a4c0c6STheodore Ts'o offset + length - 1); 400026a4c0c6STheodore Ts'o if (ret) 400126a4c0c6STheodore Ts'o return ret; 400226a4c0c6STheodore Ts'o } 400326a4c0c6STheodore Ts'o 40045955102cSAl Viro inode_lock(inode); 40059ef06cecSLukas Czerner 400626a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 400726a4c0c6STheodore Ts'o if (offset >= inode->i_size) 400826a4c0c6STheodore Ts'o goto out_mutex; 400926a4c0c6STheodore Ts'o 401026a4c0c6STheodore Ts'o /* 401126a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 401226a4c0c6STheodore Ts'o * to end after the page that contains i_size 401326a4c0c6STheodore Ts'o */ 401426a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 401526a4c0c6STheodore Ts'o length = inode->i_size + 401609cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 401726a4c0c6STheodore Ts'o offset; 401826a4c0c6STheodore Ts'o } 401926a4c0c6STheodore Ts'o 4020a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 4021a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 4022a361293fSJan Kara /* 4023a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 4024a361293fSJan Kara * partial block 4025a361293fSJan Kara */ 4026a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 4027a361293fSJan Kara if (ret < 0) 4028a361293fSJan Kara goto out_mutex; 4029a361293fSJan Kara 4030a361293fSJan Kara } 4031a361293fSJan Kara 4032ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 4033ea3d7209SJan Kara inode_dio_wait(inode); 4034ea3d7209SJan Kara 4035ea3d7209SJan Kara /* 4036ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 4037ea3d7209SJan Kara * page cache. 4038ea3d7209SJan Kara */ 4039ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 4040430657b6SRoss Zwisler 4041430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4042430657b6SRoss Zwisler if (ret) 4043430657b6SRoss Zwisler goto out_dio; 4044430657b6SRoss Zwisler 4045a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4046a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 404726a4c0c6STheodore Ts'o 4048a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 404901127848SJan Kara if (last_block_offset > first_block_offset) { 405001127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 405101127848SJan Kara if (ret) 405201127848SJan Kara goto out_dio; 4053a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4054a87dd18cSLukas Czerner last_block_offset); 405501127848SJan Kara } 405626a4c0c6STheodore Ts'o 405726a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 405826a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 405926a4c0c6STheodore Ts'o else 406026a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 406126a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 406226a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 406326a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 406426a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 406526a4c0c6STheodore Ts'o goto out_dio; 406626a4c0c6STheodore Ts'o } 406726a4c0c6STheodore Ts'o 4068a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4069a87dd18cSLukas Czerner length); 407026a4c0c6STheodore Ts'o if (ret) 407126a4c0c6STheodore Ts'o goto out_stop; 407226a4c0c6STheodore Ts'o 407326a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 407426a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 407526a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 407626a4c0c6STheodore Ts'o 4077eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4078eee597acSLukas Czerner if (stop_block > first_block) { 407926a4c0c6STheodore Ts'o 408026a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 408127bc446eSbrookxu ext4_discard_preallocations(inode, 0); 408226a4c0c6STheodore Ts'o 408326a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 408426a4c0c6STheodore Ts'o stop_block - first_block); 408526a4c0c6STheodore Ts'o if (ret) { 408626a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 408726a4c0c6STheodore Ts'o goto out_stop; 408826a4c0c6STheodore Ts'o } 408926a4c0c6STheodore Ts'o 409026a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 409126a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 409226a4c0c6STheodore Ts'o stop_block - 1); 409326a4c0c6STheodore Ts'o else 40944f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 409526a4c0c6STheodore Ts'o stop_block); 409626a4c0c6STheodore Ts'o 4097819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4098eee597acSLukas Czerner } 4099a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 410026a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 410126a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4102e251f9bcSMaxim Patlasov 4103eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 41044209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 41054209ae12SHarshad Shirwadkar if (unlikely(ret2)) 41064209ae12SHarshad Shirwadkar ret = ret2; 410767a7d5f5SJan Kara if (ret >= 0) 410867a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 410926a4c0c6STheodore Ts'o out_stop: 411026a4c0c6STheodore Ts'o ext4_journal_stop(handle); 411126a4c0c6STheodore Ts'o out_dio: 4112ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 411326a4c0c6STheodore Ts'o out_mutex: 41145955102cSAl Viro inode_unlock(inode); 411526a4c0c6STheodore Ts'o return ret; 4116a4bb6b64SAllison Henderson } 4117a4bb6b64SAllison Henderson 4118a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4119a361293fSJan Kara { 4120a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4121a361293fSJan Kara struct jbd2_inode *jinode; 4122a361293fSJan Kara 4123a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4124a361293fSJan Kara return 0; 4125a361293fSJan Kara 4126a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4127a361293fSJan Kara spin_lock(&inode->i_lock); 4128a361293fSJan Kara if (!ei->jinode) { 4129a361293fSJan Kara if (!jinode) { 4130a361293fSJan Kara spin_unlock(&inode->i_lock); 4131a361293fSJan Kara return -ENOMEM; 4132a361293fSJan Kara } 4133a361293fSJan Kara ei->jinode = jinode; 4134a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4135a361293fSJan Kara jinode = NULL; 4136a361293fSJan Kara } 4137a361293fSJan Kara spin_unlock(&inode->i_lock); 4138a361293fSJan Kara if (unlikely(jinode != NULL)) 4139a361293fSJan Kara jbd2_free_inode(jinode); 4140a361293fSJan Kara return 0; 4141a361293fSJan Kara } 4142a361293fSJan Kara 4143a4bb6b64SAllison Henderson /* 4144617ba13bSMingming Cao * ext4_truncate() 4145ac27a0ecSDave Kleikamp * 4146617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4147617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4148ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4149ac27a0ecSDave Kleikamp * 415042b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4151ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4152ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4153ac27a0ecSDave Kleikamp * 4154ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4155ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4156ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4157ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4158ac27a0ecSDave Kleikamp * left-to-right works OK too). 4159ac27a0ecSDave Kleikamp * 4160ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4161ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4162ac27a0ecSDave Kleikamp * 4163ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4164617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4165ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4166617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4167617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4168ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4169617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4170ac27a0ecSDave Kleikamp */ 41712c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4172ac27a0ecSDave Kleikamp { 4173819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4174819c4920STheodore Ts'o unsigned int credits; 41754209ae12SHarshad Shirwadkar int err = 0, err2; 4176819c4920STheodore Ts'o handle_t *handle; 4177819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4178819c4920STheodore Ts'o 417919b5ef61STheodore Ts'o /* 418019b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4181e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 418219b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 418319b5ef61STheodore Ts'o */ 418419b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41855955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41860562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41870562e0baSJiaying Zhang 418891ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41899a5d265fSzhengliang goto out_trace; 4190ac27a0ecSDave Kleikamp 41915534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 419219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41937d8f9f7dSTheodore Ts'o 4194aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4195aef1c851STao Ma int has_inline = 1; 4196aef1c851STao Ma 419701daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 41989a5d265fSzhengliang if (err || has_inline) 41999a5d265fSzhengliang goto out_trace; 4200aef1c851STao Ma } 4201aef1c851STao Ma 4202a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4203a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4204a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 42059a5d265fSzhengliang goto out_trace; 4206a361293fSJan Kara } 4207a361293fSJan Kara 4208ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4209819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4210ff9893dcSAmir Goldstein else 4211819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4212819c4920STheodore Ts'o 4213819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 42149a5d265fSzhengliang if (IS_ERR(handle)) { 42159a5d265fSzhengliang err = PTR_ERR(handle); 42169a5d265fSzhengliang goto out_trace; 42179a5d265fSzhengliang } 4218819c4920STheodore Ts'o 4219eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4220eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4221819c4920STheodore Ts'o 4222819c4920STheodore Ts'o /* 4223819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4224819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4225819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4226819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4227819c4920STheodore Ts'o * 4228819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4229819c4920STheodore Ts'o * truncatable state while each transaction commits. 4230819c4920STheodore Ts'o */ 42312c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 42322c98eb5eSTheodore Ts'o if (err) 4233819c4920STheodore Ts'o goto out_stop; 4234819c4920STheodore Ts'o 4235819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4236819c4920STheodore Ts'o 423727bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4238819c4920STheodore Ts'o 4239819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4240d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4241819c4920STheodore Ts'o else 4242819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4243819c4920STheodore Ts'o 4244819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4245d0abb36dSTheodore Ts'o if (err) 4246d0abb36dSTheodore Ts'o goto out_stop; 4247819c4920STheodore Ts'o 4248819c4920STheodore Ts'o if (IS_SYNC(inode)) 4249819c4920STheodore Ts'o ext4_handle_sync(handle); 4250819c4920STheodore Ts'o 4251819c4920STheodore Ts'o out_stop: 4252819c4920STheodore Ts'o /* 4253819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4254819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4255819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 425658d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4257819c4920STheodore Ts'o * orphan info for us. 4258819c4920STheodore Ts'o */ 4259819c4920STheodore Ts'o if (inode->i_nlink) 4260819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4261819c4920STheodore Ts'o 4262eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 42634209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 42644209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 42654209ae12SHarshad Shirwadkar err = err2; 4266819c4920STheodore Ts'o ext4_journal_stop(handle); 4267a86c6181SAlex Tomas 42689a5d265fSzhengliang out_trace: 42690562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42702c98eb5eSTheodore Ts'o return err; 4271ac27a0ecSDave Kleikamp } 4272ac27a0ecSDave Kleikamp 4273ac27a0ecSDave Kleikamp /* 4274617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4275ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4276ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4277ac27a0ecSDave Kleikamp * inode. 4278ac27a0ecSDave Kleikamp */ 42798016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 42808016e29fSHarshad Shirwadkar struct ext4_iloc *iloc, int in_mem, 42818016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4282ac27a0ecSDave Kleikamp { 4283240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4284ac27a0ecSDave Kleikamp struct buffer_head *bh; 4285240799cdSTheodore Ts'o ext4_fsblk_t block; 428602f03c42SLinus Torvalds struct blk_plug plug; 4287240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4288ac27a0ecSDave Kleikamp 42893a06d778SAneesh Kumar K.V iloc->bh = NULL; 42908016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 42918016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 42926a797d27SDarrick J. Wong return -EFSCORRUPTED; 4293ac27a0ecSDave Kleikamp 42948016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4295240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4296240799cdSTheodore Ts'o if (!gdp) 4297240799cdSTheodore Ts'o return -EIO; 4298240799cdSTheodore Ts'o 4299240799cdSTheodore Ts'o /* 4300240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4301240799cdSTheodore Ts'o */ 430200d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 43038016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4304240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4305240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4306240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4307240799cdSTheodore Ts'o 4308240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4309aebf0243SWang Shilong if (unlikely(!bh)) 4310860d21e2STheodore Ts'o return -ENOMEM; 431146f870d6STheodore Ts'o if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO)) 431246f870d6STheodore Ts'o goto simulate_eio; 4313ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4314ac27a0ecSDave Kleikamp lock_buffer(bh); 43159c83a923SHidehiro Kawai 431660c776e5Szhangyi (F) if (ext4_buffer_uptodate(bh)) { 4317ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4318ac27a0ecSDave Kleikamp unlock_buffer(bh); 4319ac27a0ecSDave Kleikamp goto has_buffer; 4320ac27a0ecSDave Kleikamp } 4321ac27a0ecSDave Kleikamp 4322ac27a0ecSDave Kleikamp /* 4323ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4324ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4325ac27a0ecSDave Kleikamp * block. 4326ac27a0ecSDave Kleikamp */ 4327ac27a0ecSDave Kleikamp if (in_mem) { 4328ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4329240799cdSTheodore Ts'o int i, start; 4330ac27a0ecSDave Kleikamp 4331240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4332ac27a0ecSDave Kleikamp 4333ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4334240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4335aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4336ac27a0ecSDave Kleikamp goto make_io; 4337ac27a0ecSDave Kleikamp 4338ac27a0ecSDave Kleikamp /* 4339ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4340ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4341ac27a0ecSDave Kleikamp * of one, so skip it. 4342ac27a0ecSDave Kleikamp */ 4343ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4344ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4345ac27a0ecSDave Kleikamp goto make_io; 4346ac27a0ecSDave Kleikamp } 4347240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4348ac27a0ecSDave Kleikamp if (i == inode_offset) 4349ac27a0ecSDave Kleikamp continue; 4350617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4351ac27a0ecSDave Kleikamp break; 4352ac27a0ecSDave Kleikamp } 4353ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4354240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4355ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4356ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4357ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4358ac27a0ecSDave Kleikamp unlock_buffer(bh); 4359ac27a0ecSDave Kleikamp goto has_buffer; 4360ac27a0ecSDave Kleikamp } 4361ac27a0ecSDave Kleikamp } 4362ac27a0ecSDave Kleikamp 4363ac27a0ecSDave Kleikamp make_io: 4364ac27a0ecSDave Kleikamp /* 4365240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4366240799cdSTheodore Ts'o * blocks from the inode table. 4367240799cdSTheodore Ts'o */ 436802f03c42SLinus Torvalds blk_start_plug(&plug); 4369240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4370240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4371240799cdSTheodore Ts'o unsigned num; 43720d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4373240799cdSTheodore Ts'o 4374240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4375b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 43760d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4377240799cdSTheodore Ts'o if (table > b) 4378240799cdSTheodore Ts'o b = table; 43790d606e2cSTheodore Ts'o end = b + ra_blks; 4380240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4381feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4382560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4383240799cdSTheodore Ts'o table += num / inodes_per_block; 4384240799cdSTheodore Ts'o if (end > table) 4385240799cdSTheodore Ts'o end = table; 4386240799cdSTheodore Ts'o while (b <= end) 43875df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4388240799cdSTheodore Ts'o } 4389240799cdSTheodore Ts'o 4390240799cdSTheodore Ts'o /* 4391ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4392ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4393ac27a0ecSDave Kleikamp * Read the block from disk. 4394ac27a0ecSDave Kleikamp */ 43958016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 43962d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 439702f03c42SLinus Torvalds blk_finish_plug(&plug); 4398ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4399ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 440046f870d6STheodore Ts'o simulate_eio: 44018016e29fSHarshad Shirwadkar if (ret_block) 44028016e29fSHarshad Shirwadkar *ret_block = block; 4403ac27a0ecSDave Kleikamp brelse(bh); 4404ac27a0ecSDave Kleikamp return -EIO; 4405ac27a0ecSDave Kleikamp } 4406ac27a0ecSDave Kleikamp } 4407ac27a0ecSDave Kleikamp has_buffer: 4408ac27a0ecSDave Kleikamp iloc->bh = bh; 4409ac27a0ecSDave Kleikamp return 0; 4410ac27a0ecSDave Kleikamp } 4411ac27a0ecSDave Kleikamp 44128016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 44138016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 44148016e29fSHarshad Shirwadkar { 44158016e29fSHarshad Shirwadkar ext4_fsblk_t err_blk; 44168016e29fSHarshad Shirwadkar int ret; 44178016e29fSHarshad Shirwadkar 44188016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 0, 44198016e29fSHarshad Shirwadkar &err_blk); 44208016e29fSHarshad Shirwadkar 44218016e29fSHarshad Shirwadkar if (ret == -EIO) 44228016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 44238016e29fSHarshad Shirwadkar "unable to read itable block"); 44248016e29fSHarshad Shirwadkar 44258016e29fSHarshad Shirwadkar return ret; 44268016e29fSHarshad Shirwadkar } 44278016e29fSHarshad Shirwadkar 4428617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4429ac27a0ecSDave Kleikamp { 44308016e29fSHarshad Shirwadkar ext4_fsblk_t err_blk; 44318016e29fSHarshad Shirwadkar int ret; 44328016e29fSHarshad Shirwadkar 4433ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 44348016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 44358016e29fSHarshad Shirwadkar !ext4_test_inode_state(inode, EXT4_STATE_XATTR), &err_blk); 44368016e29fSHarshad Shirwadkar 44378016e29fSHarshad Shirwadkar if (ret == -EIO) 44388016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 44398016e29fSHarshad Shirwadkar "unable to read itable block"); 44408016e29fSHarshad Shirwadkar 44418016e29fSHarshad Shirwadkar return ret; 44428016e29fSHarshad Shirwadkar } 44438016e29fSHarshad Shirwadkar 44448016e29fSHarshad Shirwadkar 44458016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 44468016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 44478016e29fSHarshad Shirwadkar { 44488016e29fSHarshad Shirwadkar return __ext4_get_inode_loc(sb, ino, iloc, 0, NULL); 4449ac27a0ecSDave Kleikamp } 4450ac27a0ecSDave Kleikamp 4451a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 44526642586bSRoss Zwisler { 4453a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4454a8ab6d38SIra Weiny 44559cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 44566642586bSRoss Zwisler return false; 44576642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 44586642586bSRoss Zwisler return false; 44596642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 44606642586bSRoss Zwisler return false; 44616642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 44626642586bSRoss Zwisler return false; 4463592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 44646642586bSRoss Zwisler return false; 4465c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4466c93d8f88SEric Biggers return false; 4467a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4468a8ab6d38SIra Weiny return false; 4469a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 44706642586bSRoss Zwisler return true; 4471a8ab6d38SIra Weiny 4472b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 44736642586bSRoss Zwisler } 44746642586bSRoss Zwisler 4475043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4476ac27a0ecSDave Kleikamp { 4477617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 447800a1a053STheodore Ts'o unsigned int new_fl = 0; 4479ac27a0ecSDave Kleikamp 4480043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4481043546e4SIra Weiny 4482617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 448300a1a053STheodore Ts'o new_fl |= S_SYNC; 4484617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 448500a1a053STheodore Ts'o new_fl |= S_APPEND; 4486617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 448700a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4488617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 448900a1a053STheodore Ts'o new_fl |= S_NOATIME; 4490617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 449100a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4492043546e4SIra Weiny 4493043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4494043546e4SIra Weiny * here if already set. */ 4495043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4496043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4497923ae0ffSRoss Zwisler new_fl |= S_DAX; 4498043546e4SIra Weiny 44992ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 45002ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4501b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4502b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4503c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4504c93d8f88SEric Biggers new_fl |= S_VERITY; 45055f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 45062ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4507c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4508ac27a0ecSDave Kleikamp } 4509ac27a0ecSDave Kleikamp 45100fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 45110fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 45120fc1b451SAneesh Kumar K.V { 45130fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 45148180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 45158180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 45160fc1b451SAneesh Kumar K.V 4517e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 45180fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 45190fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 45200fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 452107a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 45228180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 45238180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 45248180a562SAneesh Kumar K.V } else { 45250fc1b451SAneesh Kumar K.V return i_blocks; 45268180a562SAneesh Kumar K.V } 45270fc1b451SAneesh Kumar K.V } else { 45280fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 45290fc1b451SAneesh Kumar K.V } 45300fc1b451SAneesh Kumar K.V } 4531ff9ddf7eSJan Kara 4532eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4533152a7b0aSTao Ma struct ext4_inode *raw_inode, 4534152a7b0aSTao Ma struct ext4_inode_info *ei) 4535152a7b0aSTao Ma { 4536152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4537152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4538eb9b5f01STheodore Ts'o 4539290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4540290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4541290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4542152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4543eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4544f19d5870STao Ma } else 4545f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4546eb9b5f01STheodore Ts'o return 0; 4547152a7b0aSTao Ma } 4548152a7b0aSTao Ma 4549040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4550040cb378SLi Xi { 45510b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4552040cb378SLi Xi return -EOPNOTSUPP; 4553040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4554040cb378SLi Xi return 0; 4555040cb378SLi Xi } 4556040cb378SLi Xi 4557e254d1afSEryu Guan /* 4558e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4559e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4560e254d1afSEryu Guan * set. 4561e254d1afSEryu Guan */ 4562e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4563e254d1afSEryu Guan { 4564e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4565e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4566e254d1afSEryu Guan else 4567e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4568e254d1afSEryu Guan } 4569e254d1afSEryu Guan static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4570e254d1afSEryu Guan { 4571e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4572e254d1afSEryu Guan return inode_peek_iversion_raw(inode); 4573e254d1afSEryu Guan else 4574e254d1afSEryu Guan return inode_peek_iversion(inode); 4575e254d1afSEryu Guan } 4576e254d1afSEryu Guan 45778a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 45788a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 45798a363970STheodore Ts'o unsigned int line) 4580ac27a0ecSDave Kleikamp { 4581617ba13bSMingming Cao struct ext4_iloc iloc; 4582617ba13bSMingming Cao struct ext4_inode *raw_inode; 45831d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4584bd2c38cfSJan Kara struct ext4_super_block *es = EXT4_SB(sb)->s_es; 45851d1fe1eeSDavid Howells struct inode *inode; 4586b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 45871d1fe1eeSDavid Howells long ret; 45887e6e1ef4SDarrick J. Wong loff_t size; 4589ac27a0ecSDave Kleikamp int block; 459008cefc7aSEric W. Biederman uid_t i_uid; 459108cefc7aSEric W. Biederman gid_t i_gid; 4592040cb378SLi Xi projid_t i_projid; 4593ac27a0ecSDave Kleikamp 4594191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 4595bd2c38cfSJan Kara ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4596bd2c38cfSJan Kara ino == le32_to_cpu(es->s_usr_quota_inum) || 4597bd2c38cfSJan Kara ino == le32_to_cpu(es->s_grp_quota_inum) || 4598bd2c38cfSJan Kara ino == le32_to_cpu(es->s_prj_quota_inum))) || 45998a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 4600bd2c38cfSJan Kara (ino > le32_to_cpu(es->s_inodes_count))) { 46018a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 46028a363970STheodore Ts'o return ERR_PTR(-ESTALE); 4603014c9caaSJan Kara __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 46048a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 46058a363970STheodore Ts'o ino, current->comm); 46068a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 46078a363970STheodore Ts'o } 46088a363970STheodore Ts'o 46091d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 46101d1fe1eeSDavid Howells if (!inode) 46111d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 46121d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 46131d1fe1eeSDavid Howells return inode; 46141d1fe1eeSDavid Howells 46151d1fe1eeSDavid Howells ei = EXT4_I(inode); 46167dc57615SPeter Huewe iloc.bh = NULL; 4617ac27a0ecSDave Kleikamp 46188016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 46191d1fe1eeSDavid Howells if (ret < 0) 4620ac27a0ecSDave Kleikamp goto bad_inode; 4621617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4622814525f4SDarrick J. Wong 46238e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 46248a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46258a363970STheodore Ts'o "iget: root inode unallocated"); 46268e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 46278e4b5eaeSTheodore Ts'o goto bad_inode; 46288e4b5eaeSTheodore Ts'o } 46298e4b5eaeSTheodore Ts'o 46308a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 46318a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 46328a363970STheodore Ts'o ret = -ESTALE; 46338a363970STheodore Ts'o goto bad_inode; 46348a363970STheodore Ts'o } 46358a363970STheodore Ts'o 4636814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4637814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4638814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 46392dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 46402dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 46418a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46428a363970STheodore Ts'o "iget: bad extra_isize %u " 46438a363970STheodore Ts'o "(inode size %u)", 46442dc8d9e1SEric Biggers ei->i_extra_isize, 4645814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 46466a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4647814525f4SDarrick J. Wong goto bad_inode; 4648814525f4SDarrick J. Wong } 4649814525f4SDarrick J. Wong } else 4650814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4651814525f4SDarrick J. Wong 4652814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 46539aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4654814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4655814525f4SDarrick J. Wong __u32 csum; 4656814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4657814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4658814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4659814525f4SDarrick J. Wong sizeof(inum)); 4660814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4661814525f4SDarrick J. Wong sizeof(gen)); 4662814525f4SDarrick J. Wong } 4663814525f4SDarrick J. Wong 46648016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 46658016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 46668016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 46678016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 46688016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 46696a797d27SDarrick J. Wong ret = -EFSBADCRC; 4670814525f4SDarrick J. Wong goto bad_inode; 4671814525f4SDarrick J. Wong } 4672814525f4SDarrick J. Wong 4673ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 467408cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 467508cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 46760b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4677040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4678040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4679040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4680040cb378SLi Xi else 4681040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4682040cb378SLi Xi 4683ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 468408cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 468508cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4686ac27a0ecSDave Kleikamp } 468708cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 468808cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4689040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4690bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4691ac27a0ecSDave Kleikamp 4692353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 469367cf5b09STao Ma ei->i_inline_off = 0; 4694ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4695ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4696ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4697ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4698ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4699ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4700ac27a0ecSDave Kleikamp */ 4701ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4702393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4703393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4704393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4705ac27a0ecSDave Kleikamp /* this inode is deleted */ 47061d1fe1eeSDavid Howells ret = -ESTALE; 4707ac27a0ecSDave Kleikamp goto bad_inode; 4708ac27a0ecSDave Kleikamp } 4709ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4710ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4711ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4712393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4713393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4714393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4715ac27a0ecSDave Kleikamp } 4716ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4717043546e4SIra Weiny ext4_set_inode_flags(inode, true); 47180fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 47197973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4720e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4721a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4722a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4723e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 47247e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 47258a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47268a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 47277e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 47287e6e1ef4SDarrick J. Wong goto bad_inode; 47297e6e1ef4SDarrick J. Wong } 473048a34311SJan Kara /* 473148a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 473248a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 473348a34311SJan Kara * checksumming that corrupts checksums so forbid that. 473448a34311SJan Kara */ 473548a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 473648a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 473748a34311SJan Kara ext4_error_inode(inode, function, line, 0, 473848a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 473948a34311SJan Kara ret = -EFSCORRUPTED; 474048a34311SJan Kara goto bad_inode; 474148a34311SJan Kara } 4742ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4743a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4744a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4745a9e7f447SDmitry Monakhov #endif 4746ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4747ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4748a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4749ac27a0ecSDave Kleikamp /* 4750ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4751ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4752ac27a0ecSDave Kleikamp */ 4753617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4754ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4755ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4756aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4757ac27a0ecSDave Kleikamp 4758b436b9beSJan Kara /* 4759b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4760b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4761b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4762b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4763b436b9beSJan Kara * now it is reread from disk. 4764b436b9beSJan Kara */ 4765b436b9beSJan Kara if (journal) { 4766b436b9beSJan Kara transaction_t *transaction; 4767b436b9beSJan Kara tid_t tid; 4768b436b9beSJan Kara 4769a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4770b436b9beSJan Kara if (journal->j_running_transaction) 4771b436b9beSJan Kara transaction = journal->j_running_transaction; 4772b436b9beSJan Kara else 4773b436b9beSJan Kara transaction = journal->j_committing_transaction; 4774b436b9beSJan Kara if (transaction) 4775b436b9beSJan Kara tid = transaction->t_tid; 4776b436b9beSJan Kara else 4777b436b9beSJan Kara tid = journal->j_commit_sequence; 4778a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4779b436b9beSJan Kara ei->i_sync_tid = tid; 4780b436b9beSJan Kara ei->i_datasync_tid = tid; 4781b436b9beSJan Kara } 4782b436b9beSJan Kara 47830040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4784ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4785ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 47862dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4787617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4788617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4789ac27a0ecSDave Kleikamp } else { 4790eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4791eb9b5f01STheodore Ts'o if (ret) 4792eb9b5f01STheodore Ts'o goto bad_inode; 4793ac27a0ecSDave Kleikamp } 4794814525f4SDarrick J. Wong } 4795ac27a0ecSDave Kleikamp 4796ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4797ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4798ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4799ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4800ef7f3835SKalpak Shah 4801ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4802ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4803ee73f9a5SJeff Layton 480425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 480525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4806ee73f9a5SJeff Layton ivers |= 480725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 480825ec56b5SJean Noel Cordenner } 4809e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4810c4f65706STheodore Ts'o } 481125ec56b5SJean Noel Cordenner 4812c4b5a614STheodore Ts'o ret = 0; 4813485c26ecSTheodore Ts'o if (ei->i_file_acl && 4814ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 48158a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48168a363970STheodore Ts'o "iget: bad extended attribute block %llu", 481724676da4STheodore Ts'o ei->i_file_acl); 48186a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4819485c26ecSTheodore Ts'o goto bad_inode; 4820f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4821bc716523SLiu Song /* validate the block references in the inode */ 48228016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 48238016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4824fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 48258016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 4826bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4827bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4828bc716523SLiu Song else 48291f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4830fe2c8191SThiemo Nagel } 4831f19d5870STao Ma } 4832567f3e9aSTheodore Ts'o if (ret) 48337a262f7cSAneesh Kumar K.V goto bad_inode; 48347a262f7cSAneesh Kumar K.V 4835ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4836617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4837617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4838617ba13bSMingming Cao ext4_set_aops(inode); 4839ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4840617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4841617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4842ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 48436390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 48446390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 48458a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48468a363970STheodore Ts'o "iget: immutable or append flags " 48478a363970STheodore Ts'o "not allowed on symlinks"); 48486390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 48496390d33bSLuis R. Rodriguez goto bad_inode; 48506390d33bSLuis R. Rodriguez } 4851592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4852a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4853a7a67e8aSAl Viro ext4_set_aops(inode); 4854a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 485575e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4856617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4857e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4858e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4859e83c1397SDuane Griffin } else { 4860617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4861617ba13bSMingming Cao ext4_set_aops(inode); 4862ac27a0ecSDave Kleikamp } 486321fc61c7SAl Viro inode_nohighmem(inode); 4864563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4865563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4866617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4867ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4868ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4869ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4870ac27a0ecSDave Kleikamp else 4871ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4872ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4873393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4874393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4875563bdd61STheodore Ts'o } else { 48766a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 48778a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48788a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4879563bdd61STheodore Ts'o goto bad_inode; 4880ac27a0ecSDave Kleikamp } 48816456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 48826456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48836456ca65STheodore Ts'o "casefold flag without casefold feature"); 4884ac27a0ecSDave Kleikamp brelse(iloc.bh); 4885dec214d0STahsin Erdogan 48861d1fe1eeSDavid Howells unlock_new_inode(inode); 48871d1fe1eeSDavid Howells return inode; 4888ac27a0ecSDave Kleikamp 4889ac27a0ecSDave Kleikamp bad_inode: 4890567f3e9aSTheodore Ts'o brelse(iloc.bh); 48911d1fe1eeSDavid Howells iget_failed(inode); 48921d1fe1eeSDavid Howells return ERR_PTR(ret); 4893ac27a0ecSDave Kleikamp } 4894ac27a0ecSDave Kleikamp 48950fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 48960fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 48970fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 48980fc1b451SAneesh Kumar K.V { 48990fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 490028936b62SQian Cai u64 i_blocks = READ_ONCE(inode->i_blocks); 49010fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 49020fc1b451SAneesh Kumar K.V 49030fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 49040fc1b451SAneesh Kumar K.V /* 49054907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 49060fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49070fc1b451SAneesh Kumar K.V */ 49088180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49090fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 491084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4911f287a1a5STheodore Ts'o return 0; 4912f287a1a5STheodore Ts'o } 4913e2b911c5SDarrick J. Wong if (!ext4_has_feature_huge_file(sb)) 4914f287a1a5STheodore Ts'o return -EFBIG; 4915f287a1a5STheodore Ts'o 4916f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 49170fc1b451SAneesh Kumar K.V /* 49180fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 49190fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49200fc1b451SAneesh Kumar K.V */ 49218180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49220fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 492384a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 49240fc1b451SAneesh Kumar K.V } else { 492584a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 49268180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 49278180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 49288180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49298180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 49300fc1b451SAneesh Kumar K.V } 4931f287a1a5STheodore Ts'o return 0; 49320fc1b451SAneesh Kumar K.V } 49330fc1b451SAneesh Kumar K.V 49343f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 49353f19b2abSDavid Howells unsigned long orig_ino, 49363f19b2abSDavid Howells unsigned long ino, 49373f19b2abSDavid Howells struct ext4_inode *raw_inode) 4938a26f4992STheodore Ts'o { 49393f19b2abSDavid Howells struct inode *inode; 4940a26f4992STheodore Ts'o 49413f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 49423f19b2abSDavid Howells if (!inode) 49433f19b2abSDavid Howells return; 49443f19b2abSDavid Howells 4945ed296c6cSEric Biggers if (!inode_is_dirtytime_only(inode)) 49463f19b2abSDavid Howells return; 49473f19b2abSDavid Howells 4948a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4949ed296c6cSEric Biggers if (inode_is_dirtytime_only(inode)) { 4950a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4951a26f4992STheodore Ts'o 49525fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 4953a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4954a26f4992STheodore Ts'o 4955a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 49563f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 49573f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 49583f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 49593f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 4960a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 49613f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 49623f19b2abSDavid Howells return; 4963a26f4992STheodore Ts'o } 4964a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4965a26f4992STheodore Ts'o } 4966a26f4992STheodore Ts'o 4967a26f4992STheodore Ts'o /* 4968a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 4969a26f4992STheodore Ts'o * the same inode table block. 4970a26f4992STheodore Ts'o */ 4971a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 4972a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 4973a26f4992STheodore Ts'o { 4974a26f4992STheodore Ts'o unsigned long ino; 4975a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4976a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 4977a26f4992STheodore Ts'o 49780f0ff9a9STheodore Ts'o /* 49790f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 49800f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 49810f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 49820f0ff9a9STheodore Ts'o */ 49830f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 49843f19b2abSDavid Howells rcu_read_lock(); 4985a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 4986a26f4992STheodore Ts'o if (ino == orig_ino) 4987a26f4992STheodore Ts'o continue; 49883f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 49893f19b2abSDavid Howells (struct ext4_inode *)buf); 4990a26f4992STheodore Ts'o } 49913f19b2abSDavid Howells rcu_read_unlock(); 4992a26f4992STheodore Ts'o } 4993a26f4992STheodore Ts'o 4994ac27a0ecSDave Kleikamp /* 4995ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4996ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4997ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4998ac27a0ecSDave Kleikamp * 4999ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 5000ac27a0ecSDave Kleikamp */ 5001617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 5002ac27a0ecSDave Kleikamp struct inode *inode, 5003830156c7SFrank Mayhar struct ext4_iloc *iloc) 5004ac27a0ecSDave Kleikamp { 5005617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5006617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 5007ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 5008202ee5dfSTheodore Ts'o struct super_block *sb = inode->i_sb; 50097d8bd3c7SShijie Luo int err = 0, block; 5010202ee5dfSTheodore Ts'o int need_datasync = 0, set_large_file = 0; 501108cefc7aSEric W. Biederman uid_t i_uid; 501208cefc7aSEric W. Biederman gid_t i_gid; 5013040cb378SLi Xi projid_t i_projid; 5014ac27a0ecSDave Kleikamp 5015202ee5dfSTheodore Ts'o spin_lock(&ei->i_raw_lock); 5016202ee5dfSTheodore Ts'o 5017202ee5dfSTheodore Ts'o /* For fields not tracked in the in-memory inode, 5018ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 501919f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5020617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5021ac27a0ecSDave Kleikamp 502213221811SLuo Meng err = ext4_inode_blocks_set(handle, raw_inode, ei); 502313221811SLuo Meng if (err) { 502413221811SLuo Meng spin_unlock(&ei->i_raw_lock); 502513221811SLuo Meng goto out_brelse; 502613221811SLuo Meng } 502713221811SLuo Meng 5028ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 502908cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 503008cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 5031040cb378SLi Xi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 5032ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 503308cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 503408cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 5035ac27a0ecSDave Kleikamp /* 5036ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5037ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5038ac27a0ecSDave Kleikamp */ 503993e3b4e6SDaeho Jeong if (ei->i_dtime && list_empty(&ei->i_orphan)) { 504093e3b4e6SDaeho Jeong raw_inode->i_uid_high = 0; 504193e3b4e6SDaeho Jeong raw_inode->i_gid_high = 0; 504293e3b4e6SDaeho Jeong } else { 5043ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 504408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 5045ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 504608cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 5047ac27a0ecSDave Kleikamp } 5048ac27a0ecSDave Kleikamp } else { 504908cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 505008cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 5051ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5052ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5053ac27a0ecSDave Kleikamp } 5054ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5055ef7f3835SKalpak Shah 5056ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5057ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5058ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5059ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5060ef7f3835SKalpak Shah 5061ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 5062353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 5063ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 5064a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5065a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 50667973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5067dce8e237SQiujun Huang if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) { 5068a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5069b71fc079SJan Kara need_datasync = 1; 5070b71fc079SJan Kara } 5071ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5072e2b911c5SDarrick J. Wong if (!ext4_has_feature_large_file(sb) || 5073617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5074202ee5dfSTheodore Ts'o cpu_to_le32(EXT4_GOOD_OLD_REV)) 5075202ee5dfSTheodore Ts'o set_large_file = 1; 5076ac27a0ecSDave Kleikamp } 5077ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5078ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5079ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5080ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5081ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5082ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5083ac27a0ecSDave Kleikamp } else { 5084ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5085ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5086ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5087ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5088ac27a0ecSDave Kleikamp } 5089f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 5090de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5091ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5092f19d5870STao Ma } 5093ac27a0ecSDave Kleikamp 5094ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 5095e254d1afSEryu Guan u64 ivers = ext4_inode_peek_iversion(inode); 5096ee73f9a5SJeff Layton 5097ee73f9a5SJeff Layton raw_inode->i_disk_version = cpu_to_le32(ivers); 509825ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 509925ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 510025ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 5101ee73f9a5SJeff Layton cpu_to_le32(ivers >> 32); 5102c4f65706STheodore Ts'o raw_inode->i_extra_isize = 5103c4f65706STheodore Ts'o cpu_to_le16(ei->i_extra_isize); 5104c4f65706STheodore Ts'o } 510525ec56b5SJean Noel Cordenner } 5106040cb378SLi Xi 51070b7b7779SKaho Ng BUG_ON(!ext4_has_feature_project(inode->i_sb) && 5108040cb378SLi Xi i_projid != EXT4_DEF_PROJID); 5109040cb378SLi Xi 5110040cb378SLi Xi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 5111040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 5112040cb378SLi Xi raw_inode->i_projid = cpu_to_le32(i_projid); 5113040cb378SLi Xi 5114814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 5115202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 51161751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5117a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5118a26f4992STheodore Ts'o bh->b_data); 5119202ee5dfSTheodore Ts'o 51200390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 51217d8bd3c7SShijie Luo err = ext4_handle_dirty_metadata(handle, NULL, bh); 51227d8bd3c7SShijie Luo if (err) 51237d8bd3c7SShijie Luo goto out_brelse; 512419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5125202ee5dfSTheodore Ts'o if (set_large_file) { 51265d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5127202ee5dfSTheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5128202ee5dfSTheodore Ts'o if (err) 5129202ee5dfSTheodore Ts'o goto out_brelse; 513005c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 5131e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 513205c2c00fSJan Kara ext4_superblock_csum_set(sb); 513305c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 5134202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5135a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, 5136a3f5cf14SJan Kara EXT4_SB(sb)->s_sbh); 5137202ee5dfSTheodore Ts'o } 5138b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5139ac27a0ecSDave Kleikamp out_brelse: 5140ac27a0ecSDave Kleikamp brelse(bh); 5141617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5142ac27a0ecSDave Kleikamp return err; 5143ac27a0ecSDave Kleikamp } 5144ac27a0ecSDave Kleikamp 5145ac27a0ecSDave Kleikamp /* 5146617ba13bSMingming Cao * ext4_write_inode() 5147ac27a0ecSDave Kleikamp * 5148ac27a0ecSDave Kleikamp * We are called from a few places: 5149ac27a0ecSDave Kleikamp * 515087f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5151ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51524907cb7bSAnatol Pomozov * transaction to commit. 5153ac27a0ecSDave Kleikamp * 515487f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 515587f7e416STheodore Ts'o * We wait on commit, if told to. 5156ac27a0ecSDave Kleikamp * 515787f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 515887f7e416STheodore Ts'o * We wait on commit, if told to. 5159ac27a0ecSDave Kleikamp * 5160ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5161ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 516287f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 516387f7e416STheodore Ts'o * writeback. 5164ac27a0ecSDave Kleikamp * 5165ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5166ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5167ac27a0ecSDave Kleikamp * which we are interested. 5168ac27a0ecSDave Kleikamp * 5169ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5170ac27a0ecSDave Kleikamp * 5171ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5172ac27a0ecSDave Kleikamp * stuff(); 5173ac27a0ecSDave Kleikamp * inode->i_size = expr; 5174ac27a0ecSDave Kleikamp * 517587f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 517687f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 517787f7e416STheodore Ts'o * superblock's dirty inode list. 5178ac27a0ecSDave Kleikamp */ 5179a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5180ac27a0ecSDave Kleikamp { 518191ac6f43SFrank Mayhar int err; 518291ac6f43SFrank Mayhar 518318f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 518418f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5185ac27a0ecSDave Kleikamp return 0; 5186ac27a0ecSDave Kleikamp 518718f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 518818f2c4fcSTheodore Ts'o return -EIO; 518918f2c4fcSTheodore Ts'o 519091ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5191617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5192b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5193ac27a0ecSDave Kleikamp dump_stack(); 5194ac27a0ecSDave Kleikamp return -EIO; 5195ac27a0ecSDave Kleikamp } 5196ac27a0ecSDave Kleikamp 519710542c22SJan Kara /* 519810542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 519910542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 520010542c22SJan Kara * written. 520110542c22SJan Kara */ 520210542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5203ac27a0ecSDave Kleikamp return 0; 5204ac27a0ecSDave Kleikamp 5205aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 520618f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 520791ac6f43SFrank Mayhar } else { 520891ac6f43SFrank Mayhar struct ext4_iloc iloc; 520991ac6f43SFrank Mayhar 52108016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 521191ac6f43SFrank Mayhar if (err) 521291ac6f43SFrank Mayhar return err; 521310542c22SJan Kara /* 521410542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 521510542c22SJan Kara * it here separately for each inode. 521610542c22SJan Kara */ 521710542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5218830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5219830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 522054d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5221c398eda0STheodore Ts'o "IO error syncing inode"); 5222830156c7SFrank Mayhar err = -EIO; 5223830156c7SFrank Mayhar } 5224fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 522591ac6f43SFrank Mayhar } 522691ac6f43SFrank Mayhar return err; 5227ac27a0ecSDave Kleikamp } 5228ac27a0ecSDave Kleikamp 5229ac27a0ecSDave Kleikamp /* 523053e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 523153e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 523253e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 523353e87268SJan Kara */ 523453e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 523553e87268SJan Kara { 523653e87268SJan Kara struct page *page; 523753e87268SJan Kara unsigned offset; 523853e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 523953e87268SJan Kara tid_t commit_tid = 0; 524053e87268SJan Kara int ret; 524153e87268SJan Kara 524209cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 524353e87268SJan Kara /* 5244565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5245565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5246565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5247565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5248565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5249565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5250565333a1Syangerkun * blocksize == PAGESIZE. 525153e87268SJan Kara */ 5252565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 525353e87268SJan Kara return; 525453e87268SJan Kara while (1) { 525553e87268SJan Kara page = find_lock_page(inode->i_mapping, 525609cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 525753e87268SJan Kara if (!page) 525853e87268SJan Kara return; 5259ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 526009cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 526153e87268SJan Kara unlock_page(page); 526209cbfeafSKirill A. Shutemov put_page(page); 526353e87268SJan Kara if (ret != -EBUSY) 526453e87268SJan Kara return; 526553e87268SJan Kara commit_tid = 0; 526653e87268SJan Kara read_lock(&journal->j_state_lock); 526753e87268SJan Kara if (journal->j_committing_transaction) 526853e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 526953e87268SJan Kara read_unlock(&journal->j_state_lock); 527053e87268SJan Kara if (commit_tid) 527153e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 527253e87268SJan Kara } 527353e87268SJan Kara } 527453e87268SJan Kara 527553e87268SJan Kara /* 5276617ba13bSMingming Cao * ext4_setattr() 5277ac27a0ecSDave Kleikamp * 5278ac27a0ecSDave Kleikamp * Called from notify_change. 5279ac27a0ecSDave Kleikamp * 5280ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5281ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5282ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5283ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5284ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5285ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5286ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5287ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5288ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5289ac27a0ecSDave Kleikamp * 5290678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5291678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5292678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5293678aaf48SJan Kara * This way we are sure that all the data written in the previous 5294678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5295678aaf48SJan Kara * writeback). 5296678aaf48SJan Kara * 5297678aaf48SJan Kara * Called with inode->i_mutex down. 5298ac27a0ecSDave Kleikamp */ 5299549c7297SChristian Brauner int ext4_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 5300549c7297SChristian Brauner struct iattr *attr) 5301ac27a0ecSDave Kleikamp { 53022b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5303ac27a0ecSDave Kleikamp int error, rc = 0; 53043d287de3SDmitry Monakhov int orphan = 0; 5305ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5306ac27a0ecSDave Kleikamp 53070db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 53080db1ff22STheodore Ts'o return -EIO; 53090db1ff22STheodore Ts'o 531002b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 531102b016caSTheodore Ts'o return -EPERM; 531202b016caSTheodore Ts'o 531302b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 531402b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 531502b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 531602b016caSTheodore Ts'o return -EPERM; 531702b016caSTheodore Ts'o 531814f3db55SChristian Brauner error = setattr_prepare(mnt_userns, dentry, attr); 5319ac27a0ecSDave Kleikamp if (error) 5320ac27a0ecSDave Kleikamp return error; 5321ac27a0ecSDave Kleikamp 53223ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 53233ce2b8ddSEric Biggers if (error) 53243ce2b8ddSEric Biggers return error; 53253ce2b8ddSEric Biggers 5326c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5327c93d8f88SEric Biggers if (error) 5328c93d8f88SEric Biggers return error; 5329c93d8f88SEric Biggers 5330a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5331a7cdadeeSJan Kara error = dquot_initialize(inode); 5332a7cdadeeSJan Kara if (error) 5333a7cdadeeSJan Kara return error; 5334a7cdadeeSJan Kara } 5335aa75f4d3SHarshad Shirwadkar ext4_fc_start_update(inode); 533608cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 533708cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5338ac27a0ecSDave Kleikamp handle_t *handle; 5339ac27a0ecSDave Kleikamp 5340ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5341ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53429924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53439924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5344194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5345ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5346ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5347ac27a0ecSDave Kleikamp goto err_out; 5348ac27a0ecSDave Kleikamp } 53497a9ca53aSTahsin Erdogan 53507a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53517a9ca53aSTahsin Erdogan * counts xattr inode references. 53527a9ca53aSTahsin Erdogan */ 53537a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5354b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 53557a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53567a9ca53aSTahsin Erdogan 5357ac27a0ecSDave Kleikamp if (error) { 5358617ba13bSMingming Cao ext4_journal_stop(handle); 5359aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 5360ac27a0ecSDave Kleikamp return error; 5361ac27a0ecSDave Kleikamp } 5362ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5363ac27a0ecSDave Kleikamp * one transaction */ 5364ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5365ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5366ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5367ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5368617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5369617ba13bSMingming Cao ext4_journal_stop(handle); 5370512c15efSPan Bian if (unlikely(error)) { 5371512c15efSPan Bian ext4_fc_stop_update(inode); 53724209ae12SHarshad Shirwadkar return error; 5373ac27a0ecSDave Kleikamp } 5374512c15efSPan Bian } 5375ac27a0ecSDave Kleikamp 53763da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53775208386cSJan Kara handle_t *handle; 53783da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5379b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5380562c72aaSChristoph Hellwig 538112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5382e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5383e2b46574SEric Sandeen 5384aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5385aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 53860c095c7fSTheodore Ts'o return -EFBIG; 5387e2b46574SEric Sandeen } 5388aa75f4d3SHarshad Shirwadkar } 5389aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 5390aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 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 5411b9c1c267SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5412b9c1c267SJan Kara 5413b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5414b9c1c267SJan Kara if (rc) { 5415b9c1c267SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 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, 5442aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : 0) >> 5443aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5444aa75f4d3SHarshad Shirwadkar else 5445aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5446a80f7fcfSHarshad Shirwadkar handle, inode, 5447aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5448aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5449aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5450aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5451aa75f4d3SHarshad Shirwadkar 545290e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5453617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5454617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5455ac27a0ecSDave Kleikamp if (!error) 5456ac27a0ecSDave Kleikamp error = rc; 545790e775b7SJan Kara /* 545890e775b7SJan Kara * We have to update i_size under i_data_sem together 545990e775b7SJan Kara * with i_disksize to avoid races with writeback code 546090e775b7SJan Kara * running ext4_wb_update_i_disksize(). 546190e775b7SJan Kara */ 546290e775b7SJan Kara if (!error) 546390e775b7SJan Kara i_size_write(inode, attr->ia_size); 546490e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5465617ba13bSMingming Cao ext4_journal_stop(handle); 5466b9c1c267SJan Kara if (error) 5467b9c1c267SJan Kara goto out_mmap_sem; 546882a25b02SJan Kara if (!shrink) { 5469b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5470b9c1c267SJan Kara inode->i_size); 5471b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 547282a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5473b9c1c267SJan Kara } 5474430657b6SRoss Zwisler } 5475430657b6SRoss Zwisler 547653e87268SJan Kara /* 547753e87268SJan Kara * Truncate pagecache after we've waited for commit 547853e87268SJan Kara * in data=journal mode to make pages freeable. 547953e87268SJan Kara */ 54807caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5481b9c1c267SJan Kara /* 5482b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5483b9c1c267SJan Kara * truncate possible preallocated blocks. 5484b9c1c267SJan Kara */ 5485b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54862c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54872c98eb5eSTheodore Ts'o if (rc) 54882c98eb5eSTheodore Ts'o error = rc; 54892c98eb5eSTheodore Ts'o } 5490b9c1c267SJan Kara out_mmap_sem: 5491ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 54923da40c7bSJosef Bacik } 5493ac27a0ecSDave Kleikamp 54942c98eb5eSTheodore Ts'o if (!error) { 549514f3db55SChristian Brauner setattr_copy(mnt_userns, inode, attr); 54961025774cSChristoph Hellwig mark_inode_dirty(inode); 54971025774cSChristoph Hellwig } 54981025774cSChristoph Hellwig 54991025774cSChristoph Hellwig /* 55001025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 55011025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 55021025774cSChristoph Hellwig */ 55033d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5504617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5505ac27a0ecSDave Kleikamp 55062c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 550714f3db55SChristian Brauner rc = posix_acl_chmod(mnt_userns, inode, inode->i_mode); 5508ac27a0ecSDave Kleikamp 5509ac27a0ecSDave Kleikamp err_out: 5510aa75f4d3SHarshad Shirwadkar if (error) 5511617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5512ac27a0ecSDave Kleikamp if (!error) 5513ac27a0ecSDave Kleikamp error = rc; 5514aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 5515ac27a0ecSDave Kleikamp return error; 5516ac27a0ecSDave Kleikamp } 5517ac27a0ecSDave Kleikamp 5518549c7297SChristian Brauner int ext4_getattr(struct user_namespace *mnt_userns, const struct path *path, 5519549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags) 55203e3398a0SMingming Cao { 552199652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 552299652ea5SDavid Howells struct ext4_inode *raw_inode; 552399652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 552499652ea5SDavid Howells unsigned int flags; 55253e3398a0SMingming Cao 5526d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5527d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 552899652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 552999652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 553099652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 553199652ea5SDavid Howells } 553299652ea5SDavid Howells 553399652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 553499652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 553599652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 553699652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 553799652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 553899652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 553999652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 554099652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 554199652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 554299652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 554399652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 55441f607195SEric Biggers if (flags & EXT4_VERITY_FL) 55451f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 554699652ea5SDavid Howells 55473209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 55483209f68bSDavid Howells STATX_ATTR_COMPRESSED | 55493209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 55503209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 55511f607195SEric Biggers STATX_ATTR_NODUMP | 55521f607195SEric Biggers STATX_ATTR_VERITY); 55533209f68bSDavid Howells 555414f3db55SChristian Brauner generic_fillattr(mnt_userns, inode, stat); 555599652ea5SDavid Howells return 0; 555699652ea5SDavid Howells } 555799652ea5SDavid Howells 5558549c7297SChristian Brauner int ext4_file_getattr(struct user_namespace *mnt_userns, 5559549c7297SChristian Brauner const struct path *path, struct kstat *stat, 556099652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 556199652ea5SDavid Howells { 556299652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 556399652ea5SDavid Howells u64 delalloc_blocks; 556499652ea5SDavid Howells 556514f3db55SChristian Brauner ext4_getattr(mnt_userns, path, stat, request_mask, query_flags); 55663e3398a0SMingming Cao 55673e3398a0SMingming Cao /* 55689206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 55699206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 55709206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5571d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 55729206c561SAndreas Dilger */ 55739206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 55749206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 55759206c561SAndreas Dilger 55769206c561SAndreas Dilger /* 55773e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 55783e3398a0SMingming Cao * otherwise in the case of system crash before the real block 55793e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 55803e3398a0SMingming Cao * on-disk file blocks. 55813e3398a0SMingming Cao * We always keep i_blocks updated together with real 55823e3398a0SMingming Cao * allocation. But to not confuse with user, stat 55833e3398a0SMingming Cao * will return the blocks that include the delayed allocation 55843e3398a0SMingming Cao * blocks for this file. 55853e3398a0SMingming Cao */ 558696607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 558796607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55888af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55893e3398a0SMingming Cao return 0; 55903e3398a0SMingming Cao } 5591ac27a0ecSDave Kleikamp 5592fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5593fffb2739SJan Kara int pextents) 5594a02908f1SMingming Cao { 559512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5596fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5597fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5598a02908f1SMingming Cao } 5599ac51d837STheodore Ts'o 5600a02908f1SMingming Cao /* 5601a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5602a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5603a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5604a02908f1SMingming Cao * 5605a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 56064907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5607a02908f1SMingming Cao * they could still across block group boundary. 5608a02908f1SMingming Cao * 5609a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5610a02908f1SMingming Cao */ 5611dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5612fffb2739SJan Kara int pextents) 5613a02908f1SMingming Cao { 56148df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56158df9675fSTheodore Ts'o int gdpblocks; 5616a02908f1SMingming Cao int idxblocks; 5617a02908f1SMingming Cao int ret = 0; 5618a02908f1SMingming Cao 5619a02908f1SMingming Cao /* 5620fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5621fffb2739SJan Kara * to @pextents physical extents? 5622a02908f1SMingming Cao */ 5623fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5624a02908f1SMingming Cao 5625a02908f1SMingming Cao ret = idxblocks; 5626a02908f1SMingming Cao 5627a02908f1SMingming Cao /* 5628a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5629a02908f1SMingming Cao * to account 5630a02908f1SMingming Cao */ 5631fffb2739SJan Kara groups = idxblocks + pextents; 5632a02908f1SMingming Cao gdpblocks = groups; 56338df9675fSTheodore Ts'o if (groups > ngroups) 56348df9675fSTheodore Ts'o groups = ngroups; 5635a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5636a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5637a02908f1SMingming Cao 5638a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5639a02908f1SMingming Cao ret += groups + gdpblocks; 5640a02908f1SMingming Cao 5641a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5642a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5643ac27a0ecSDave Kleikamp 5644ac27a0ecSDave Kleikamp return ret; 5645ac27a0ecSDave Kleikamp } 5646ac27a0ecSDave Kleikamp 5647ac27a0ecSDave Kleikamp /* 564825985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5649f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5650f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5651a02908f1SMingming Cao * 5652525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5653a02908f1SMingming Cao * 5654525f4ed8SMingming Cao * We need to consider the worse case, when 5655a02908f1SMingming Cao * one new block per extent. 5656a02908f1SMingming Cao */ 5657a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5658a02908f1SMingming Cao { 5659a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5660a02908f1SMingming Cao int ret; 5661a02908f1SMingming Cao 5662fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5663a02908f1SMingming Cao 5664a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5665a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5666a02908f1SMingming Cao ret += bpp; 5667a02908f1SMingming Cao return ret; 5668a02908f1SMingming Cao } 5669f3bd1f3fSMingming Cao 5670f3bd1f3fSMingming Cao /* 5671f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5672f3bd1f3fSMingming Cao * 5673f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 567479e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5675f3bd1f3fSMingming Cao * 5676f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5677f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5678f3bd1f3fSMingming Cao */ 5679f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5680f3bd1f3fSMingming Cao { 5681f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5682f3bd1f3fSMingming Cao } 5683f3bd1f3fSMingming Cao 5684a02908f1SMingming Cao /* 5685617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5686ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5687ac27a0ecSDave Kleikamp */ 5688617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5689617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5690ac27a0ecSDave Kleikamp { 5691ac27a0ecSDave Kleikamp int err = 0; 5692ac27a0ecSDave Kleikamp 5693a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5694a6758309SVasily Averin put_bh(iloc->bh); 56950db1ff22STheodore Ts'o return -EIO; 5696a6758309SVasily Averin } 5697a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5698aa75f4d3SHarshad Shirwadkar 5699c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 570025ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 570125ec56b5SJean Noel Cordenner 5702ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5703ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5704ac27a0ecSDave Kleikamp 5705dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5706830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5707ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5708ac27a0ecSDave Kleikamp return err; 5709ac27a0ecSDave Kleikamp } 5710ac27a0ecSDave Kleikamp 5711ac27a0ecSDave Kleikamp /* 5712ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5713ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5714ac27a0ecSDave Kleikamp */ 5715ac27a0ecSDave Kleikamp 5716ac27a0ecSDave Kleikamp int 5717617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5718617ba13bSMingming Cao struct ext4_iloc *iloc) 5719ac27a0ecSDave Kleikamp { 57200390131bSFrank Mayhar int err; 57210390131bSFrank Mayhar 57220db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 57230db1ff22STheodore Ts'o return -EIO; 57240db1ff22STheodore Ts'o 5725617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5726ac27a0ecSDave Kleikamp if (!err) { 5727ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5728617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5729ac27a0ecSDave Kleikamp if (err) { 5730ac27a0ecSDave Kleikamp brelse(iloc->bh); 5731ac27a0ecSDave Kleikamp iloc->bh = NULL; 5732ac27a0ecSDave Kleikamp } 5733ac27a0ecSDave Kleikamp } 5734617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5735ac27a0ecSDave Kleikamp return err; 5736ac27a0ecSDave Kleikamp } 5737ac27a0ecSDave Kleikamp 5738c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5739c03b45b8SMiao Xie unsigned int new_extra_isize, 5740c03b45b8SMiao Xie struct ext4_iloc *iloc, 5741c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5742c03b45b8SMiao Xie { 5743c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5744c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 57454ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 57464ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5747c03b45b8SMiao Xie int error; 5748c03b45b8SMiao Xie 57494ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 57504ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 57514ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 57524ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 57534ea99936STheodore Ts'o ei->i_extra_isize, 57544ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 57554ea99936STheodore Ts'o return -EFSCORRUPTED; 57564ea99936STheodore Ts'o } 57574ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 57584ea99936STheodore Ts'o (new_extra_isize < 4) || 57594ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 57604ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 57614ea99936STheodore Ts'o 5762c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5763c03b45b8SMiao Xie 5764c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5765c03b45b8SMiao Xie 5766c03b45b8SMiao Xie /* No extended attributes present */ 5767c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5768c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5769c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5770c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5771c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5772c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5773c03b45b8SMiao Xie return 0; 5774c03b45b8SMiao Xie } 5775c03b45b8SMiao Xie 5776c03b45b8SMiao Xie /* try to expand with EAs present */ 5777c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5778c03b45b8SMiao Xie raw_inode, handle); 5779c03b45b8SMiao Xie if (error) { 5780c03b45b8SMiao Xie /* 5781c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5782c03b45b8SMiao Xie */ 5783c03b45b8SMiao Xie *no_expand = 1; 5784c03b45b8SMiao Xie } 5785c03b45b8SMiao Xie 5786c03b45b8SMiao Xie return error; 5787c03b45b8SMiao Xie } 5788c03b45b8SMiao Xie 5789ac27a0ecSDave Kleikamp /* 57906dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57916dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 57926dd4ee7cSKalpak Shah */ 5793cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 57941d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 57951d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 57961d03ec98SAneesh Kumar K.V handle_t *handle) 57976dd4ee7cSKalpak Shah { 57983b10fdc6SMiao Xie int no_expand; 57993b10fdc6SMiao Xie int error; 58006dd4ee7cSKalpak Shah 5801cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5802cf0a5e81SMiao Xie return -EOVERFLOW; 5803cf0a5e81SMiao Xie 5804cf0a5e81SMiao Xie /* 5805cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5806cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5807cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5808cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5809cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5810cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5811cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5812cf0a5e81SMiao Xie */ 58136cb367c2SJan Kara if (ext4_journal_extend(handle, 581483448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5815cf0a5e81SMiao Xie return -ENOSPC; 58166dd4ee7cSKalpak Shah 58173b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5818cf0a5e81SMiao Xie return -EBUSY; 58193b10fdc6SMiao Xie 5820c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5821c03b45b8SMiao Xie handle, &no_expand); 58223b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5823c03b45b8SMiao Xie 5824c03b45b8SMiao Xie return error; 58256dd4ee7cSKalpak Shah } 58266dd4ee7cSKalpak Shah 5827c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5828c03b45b8SMiao Xie unsigned int new_extra_isize, 5829c03b45b8SMiao Xie struct ext4_iloc *iloc) 5830c03b45b8SMiao Xie { 5831c03b45b8SMiao Xie handle_t *handle; 5832c03b45b8SMiao Xie int no_expand; 5833c03b45b8SMiao Xie int error, rc; 5834c03b45b8SMiao Xie 5835c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5836c03b45b8SMiao Xie brelse(iloc->bh); 5837c03b45b8SMiao Xie return -EOVERFLOW; 5838c03b45b8SMiao Xie } 5839c03b45b8SMiao Xie 5840c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5841c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5842c03b45b8SMiao Xie if (IS_ERR(handle)) { 5843c03b45b8SMiao Xie error = PTR_ERR(handle); 5844c03b45b8SMiao Xie brelse(iloc->bh); 5845c03b45b8SMiao Xie return error; 5846c03b45b8SMiao Xie } 5847c03b45b8SMiao Xie 5848c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5849c03b45b8SMiao Xie 5850ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5851c03b45b8SMiao Xie error = ext4_journal_get_write_access(handle, iloc->bh); 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) { 59684c546592SDaeho Jeong down_write(&EXT4_I(inode)->i_mmap_sem); 59694c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 59704c546592SDaeho Jeong if (err < 0) { 59714c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 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) 60044c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 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, 6013aa75f4d3SHarshad Shirwadkar EXT4_FC_REASON_JOURNAL_FLAG_CHANGE); 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 60222e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 60232e9ee850SAneesh Kumar K.V { 60242e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60252e9ee850SAneesh Kumar K.V } 60262e9ee850SAneesh Kumar K.V 6027401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 60282e9ee850SAneesh Kumar K.V { 602911bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 6030c2ec175cSNick Piggin struct page *page = vmf->page; 60312e9ee850SAneesh Kumar K.V loff_t size; 60322e9ee850SAneesh Kumar K.V unsigned long len; 6033401b25aaSSouptick Joarder int err; 6034401b25aaSSouptick Joarder vm_fault_t ret; 60352e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6036496ad9aaSAl Viro struct inode *inode = file_inode(file); 60372e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60389ea7df53SJan Kara handle_t *handle; 60399ea7df53SJan Kara get_block_t *get_block; 60409ea7df53SJan Kara int retries = 0; 60412e9ee850SAneesh Kumar K.V 604202b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 604302b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 604402b016caSTheodore Ts'o 60458e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6046041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6047ea3d7209SJan Kara 6048ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 60497b4cc978SEric Biggers 6050401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6051401b25aaSSouptick Joarder if (err) 60527b4cc978SEric Biggers goto out_ret; 60537b4cc978SEric Biggers 605464a9f144SMauricio Faria de Oliveira /* 605564a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 605664a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 605764a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 605864a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 605964a9f144SMauricio Faria de Oliveira */ 606064a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 606164a9f144SMauricio Faria de Oliveira goto retry_alloc; 606264a9f144SMauricio Faria de Oliveira 60639ea7df53SJan Kara /* Delalloc case is easy... */ 60649ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 60659ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 60669ea7df53SJan Kara do { 6067401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 60689ea7df53SJan Kara ext4_da_get_block_prep); 6069401b25aaSSouptick Joarder } while (err == -ENOSPC && 60709ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 60719ea7df53SJan Kara goto out_ret; 60722e9ee850SAneesh Kumar K.V } 60730e499890SDarrick J. Wong 60740e499890SDarrick J. Wong lock_page(page); 60759ea7df53SJan Kara size = i_size_read(inode); 60769ea7df53SJan Kara /* Page got truncated from under us? */ 60779ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 60789ea7df53SJan Kara unlock_page(page); 60799ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 60809ea7df53SJan Kara goto out; 60810e499890SDarrick J. Wong } 60822e9ee850SAneesh Kumar K.V 608309cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 608409cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 60852e9ee850SAneesh Kumar K.V else 608609cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6087a827eaffSAneesh Kumar K.V /* 60889ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 60899ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 609064a9f144SMauricio Faria de Oliveira * 609164a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 609264a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6093a827eaffSAneesh Kumar K.V */ 60942e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6095f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 6096f19d5870STao Ma 0, len, NULL, 6097a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 60989ea7df53SJan Kara /* Wait so that we don't change page under IO */ 60991d1d1a76SDarrick J. Wong wait_for_stable_page(page); 61009ea7df53SJan Kara ret = VM_FAULT_LOCKED; 61019ea7df53SJan Kara goto out; 61022e9ee850SAneesh Kumar K.V } 6103a827eaffSAneesh Kumar K.V } 6104a827eaffSAneesh Kumar K.V unlock_page(page); 61059ea7df53SJan Kara /* OK, we need to fill the hole... */ 61069ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6107705965bdSJan Kara get_block = ext4_get_block_unwritten; 61089ea7df53SJan Kara else 61099ea7df53SJan Kara get_block = ext4_get_block; 61109ea7df53SJan Kara retry_alloc: 61119924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 61129924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 61139ea7df53SJan Kara if (IS_ERR(handle)) { 6114c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 61159ea7df53SJan Kara goto out; 61169ea7df53SJan Kara } 611764a9f144SMauricio Faria de Oliveira /* 611864a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 611964a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 612064a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 612164a9f144SMauricio Faria de Oliveira */ 612264a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6123401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 612464a9f144SMauricio Faria de Oliveira } else { 612564a9f144SMauricio Faria de Oliveira lock_page(page); 612664a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 612764a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 612864a9f144SMauricio Faria de Oliveira if (page->mapping != mapping || page_offset(page) > size) { 612964a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6130afb585a9SMauricio Faria de Oliveira goto out_error; 613164a9f144SMauricio Faria de Oliveira } 613264a9f144SMauricio Faria de Oliveira 613364a9f144SMauricio Faria de Oliveira if (page->index == size >> PAGE_SHIFT) 613464a9f144SMauricio Faria de Oliveira len = size & ~PAGE_MASK; 613564a9f144SMauricio Faria de Oliveira else 613664a9f144SMauricio Faria de Oliveira len = PAGE_SIZE; 613764a9f144SMauricio Faria de Oliveira 613864a9f144SMauricio Faria de Oliveira err = __block_write_begin(page, 0, len, ext4_get_block); 613964a9f144SMauricio Faria de Oliveira if (!err) { 61409ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6141afb585a9SMauricio Faria de Oliveira if (ext4_walk_page_buffers(handle, page_buffers(page), 6142afb585a9SMauricio Faria de Oliveira 0, len, NULL, do_journal_get_write_access)) 6143afb585a9SMauricio Faria de Oliveira goto out_error; 6144afb585a9SMauricio Faria de Oliveira if (ext4_walk_page_buffers(handle, page_buffers(page), 6145afb585a9SMauricio Faria de Oliveira 0, len, NULL, write_end_fn)) 6146afb585a9SMauricio Faria de Oliveira goto out_error; 6147b5b18160SJan Kara if (ext4_jbd2_inode_add_write(handle, inode, 6148b5b18160SJan Kara page_offset(page), len)) 6149afb585a9SMauricio Faria de Oliveira goto out_error; 61509ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 615164a9f144SMauricio Faria de Oliveira } else { 615264a9f144SMauricio Faria de Oliveira unlock_page(page); 615364a9f144SMauricio Faria de Oliveira } 61549ea7df53SJan Kara } 61559ea7df53SJan Kara ext4_journal_stop(handle); 6156401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 61579ea7df53SJan Kara goto retry_alloc; 61589ea7df53SJan Kara out_ret: 6159401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 61609ea7df53SJan Kara out: 6161ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 61628e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 61632e9ee850SAneesh Kumar K.V return ret; 6164afb585a9SMauricio Faria de Oliveira out_error: 6165afb585a9SMauricio Faria de Oliveira unlock_page(page); 6166afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6167afb585a9SMauricio Faria de Oliveira goto out; 61682e9ee850SAneesh Kumar K.V } 6169ea3d7209SJan Kara 6170401b25aaSSouptick Joarder vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 6171ea3d7209SJan Kara { 617211bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file); 6173401b25aaSSouptick Joarder vm_fault_t ret; 6174ea3d7209SJan Kara 6175ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 6176401b25aaSSouptick Joarder ret = filemap_fault(vmf); 6177ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 6178ea3d7209SJan Kara 6179401b25aaSSouptick Joarder return ret; 6180ea3d7209SJan Kara } 6181