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); 142dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 143dec214d0STahsin Erdogan int pextents); 14464769240SAlex Tomas 145ac27a0ecSDave Kleikamp /* 146ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 147407cd7fbSTahsin Erdogan * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 148ac27a0ecSDave Kleikamp */ 149f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode) 150ac27a0ecSDave Kleikamp { 151fc82228aSAndi Kleen if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 152fc82228aSAndi Kleen int ea_blocks = EXT4_I(inode)->i_file_acl ? 153fc82228aSAndi Kleen EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 154fc82228aSAndi Kleen 155fc82228aSAndi Kleen if (ext4_has_inline_data(inode)) 156fc82228aSAndi Kleen return 0; 157fc82228aSAndi Kleen 158fc82228aSAndi Kleen return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 159fc82228aSAndi Kleen } 160407cd7fbSTahsin Erdogan return S_ISLNK(inode->i_mode) && inode->i_size && 161407cd7fbSTahsin Erdogan (inode->i_size < EXT4_N_BLOCKS * 4); 162ac27a0ecSDave Kleikamp } 163ac27a0ecSDave Kleikamp 164ac27a0ecSDave Kleikamp /* 165ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 166ac27a0ecSDave Kleikamp */ 1670930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 168ac27a0ecSDave Kleikamp { 169ac27a0ecSDave Kleikamp handle_t *handle; 170bc965ab3STheodore Ts'o int err; 17165db869cSJan Kara /* 17265db869cSJan Kara * Credits for final inode cleanup and freeing: 17365db869cSJan Kara * sb + inode (ext4_orphan_del()), block bitmap, group descriptor 17465db869cSJan Kara * (xattr block freeing), bitmap, group descriptor (inode freeing) 17565db869cSJan Kara */ 17665db869cSJan Kara int extra_credits = 6; 1770421a189STahsin Erdogan struct ext4_xattr_inode_array *ea_inode_array = NULL; 17846e294efSJan Kara bool freeze_protected = false; 179ac27a0ecSDave Kleikamp 1807ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1812581fdc8SJiaying Zhang 1820930fcc1SAl Viro if (inode->i_nlink) { 1832d859db3SJan Kara /* 1842d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1852d859db3SJan Kara * journal. So although mm thinks everything is clean and 1862d859db3SJan Kara * ready for reaping the inode might still have some pages to 1872d859db3SJan Kara * write in the running transaction or waiting to be 1882d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1892d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1902d859db3SJan Kara * cause data loss. Also even if we did not discard these 1912d859db3SJan Kara * buffers, we would have no way to find them after the inode 1922d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1932d859db3SJan Kara * read them before the transaction is checkpointed. So be 1942d859db3SJan Kara * careful and force everything to disk here... We use 1952d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1962d859db3SJan Kara * containing inode's data. 1972d859db3SJan Kara * 1982d859db3SJan Kara * Note that directories do not have this problem because they 1992d859db3SJan Kara * don't use page cache. 2002d859db3SJan Kara */ 2016a7fd522SVegard Nossum if (inode->i_ino != EXT4_JOURNAL_INO && 2026a7fd522SVegard Nossum ext4_should_journal_data(inode) && 2033abb1a0fSJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2043abb1a0fSJan Kara inode->i_data.nrpages) { 2052d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2062d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2072d859db3SJan Kara 208d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2092d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2102d859db3SJan Kara } 21191b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 2125dc23bddSJan Kara 2130930fcc1SAl Viro goto no_delete; 2140930fcc1SAl Viro } 2150930fcc1SAl Viro 216e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 217e2bfb088STheodore Ts'o goto no_delete; 218871a2931SChristoph Hellwig dquot_initialize(inode); 219907f4554SChristoph Hellwig 220678aaf48SJan Kara if (ext4_should_order_data(inode)) 221678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 22291b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 223ac27a0ecSDave Kleikamp 2248e8ad8a5SJan Kara /* 225ceff86fdSJan Kara * For inodes with journalled data, transaction commit could have 226ceff86fdSJan Kara * dirtied the inode. Flush worker is ignoring it because of I_FREEING 227ceff86fdSJan Kara * flag but we still need to remove the inode from the writeback lists. 228ceff86fdSJan Kara */ 229ceff86fdSJan Kara if (!list_empty_careful(&inode->i_io_list)) { 230ceff86fdSJan Kara WARN_ON_ONCE(!ext4_should_journal_data(inode)); 231ceff86fdSJan Kara inode_io_list_del(inode); 232ceff86fdSJan Kara } 233ceff86fdSJan Kara 234ceff86fdSJan Kara /* 2358e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 23646e294efSJan Kara * protection against it. When we are in a running transaction though, 23746e294efSJan Kara * we are already protected against freezing and we cannot grab further 23846e294efSJan Kara * protection due to lock ordering constraints. 2398e8ad8a5SJan Kara */ 24046e294efSJan Kara if (!ext4_journal_current_handle()) { 2418e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 24246e294efSJan Kara freeze_protected = true; 24346e294efSJan Kara } 244e50e5129SAndreas Dilger 24530a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 24630a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 24730a7eb97STahsin Erdogan 24865db869cSJan Kara /* 24965db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 25065db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 25165db869cSJan Kara */ 25230a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 25365db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 254ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 255bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 256ac27a0ecSDave Kleikamp /* 257ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 258ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 259ac27a0ecSDave Kleikamp * cleaned up. 260ac27a0ecSDave Kleikamp */ 261617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 26246e294efSJan Kara if (freeze_protected) 2638e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 264ac27a0ecSDave Kleikamp goto no_delete; 265ac27a0ecSDave Kleikamp } 26630a7eb97STahsin Erdogan 267ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2680390131bSFrank Mayhar ext4_handle_sync(handle); 269407cd7fbSTahsin Erdogan 270407cd7fbSTahsin Erdogan /* 271407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 272407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 273407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 274407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 275407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 276407cd7fbSTahsin Erdogan */ 277407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 278407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 279ac27a0ecSDave Kleikamp inode->i_size = 0; 280bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 281bc965ab3STheodore Ts'o if (err) { 28212062dddSEric Sandeen ext4_warning(inode->i_sb, 283bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 284bc965ab3STheodore Ts'o goto stop_handle; 285bc965ab3STheodore Ts'o } 2862c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2872c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2882c98eb5eSTheodore Ts'o if (err) { 28954d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err, 2902c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2912c98eb5eSTheodore Ts'o inode->i_ino, err); 2922c98eb5eSTheodore Ts'o goto stop_handle; 2932c98eb5eSTheodore Ts'o } 2942c98eb5eSTheodore Ts'o } 295bc965ab3STheodore Ts'o 29630a7eb97STahsin Erdogan /* Remove xattr references. */ 29730a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 29830a7eb97STahsin Erdogan extra_credits); 29930a7eb97STahsin Erdogan if (err) { 30030a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 301bc965ab3STheodore Ts'o stop_handle: 302bc965ab3STheodore Ts'o ext4_journal_stop(handle); 30345388219STheodore Ts'o ext4_orphan_del(NULL, inode); 30446e294efSJan Kara if (freeze_protected) 3058e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 30630a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 307bc965ab3STheodore Ts'o goto no_delete; 308bc965ab3STheodore Ts'o } 309bc965ab3STheodore Ts'o 310ac27a0ecSDave Kleikamp /* 311617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 312ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 313617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 314ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 315617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 316ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 317ac27a0ecSDave Kleikamp */ 318617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3195ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 320ac27a0ecSDave Kleikamp 321ac27a0ecSDave Kleikamp /* 322ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 323ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 324ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 325ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 326ac27a0ecSDave Kleikamp * fails. 327ac27a0ecSDave Kleikamp */ 328617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 329ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3300930fcc1SAl Viro ext4_clear_inode(inode); 331ac27a0ecSDave Kleikamp else 332617ba13bSMingming Cao ext4_free_inode(handle, inode); 333617ba13bSMingming Cao ext4_journal_stop(handle); 33446e294efSJan Kara if (freeze_protected) 3358e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3360421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 337ac27a0ecSDave Kleikamp return; 338ac27a0ecSDave Kleikamp no_delete: 339b21ebf14SHarshad Shirwadkar if (!list_empty(&EXT4_I(inode)->i_fc_list)) 340b21ebf14SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM); 3410930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 342ac27a0ecSDave Kleikamp } 343ac27a0ecSDave Kleikamp 344a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 345a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 34660e58e0fSMingming Cao { 347a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 34860e58e0fSMingming Cao } 349a9e7f447SDmitry Monakhov #endif 3509d0be502STheodore Ts'o 35112219aeaSAneesh Kumar K.V /* 3520637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3530637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3540637c6f4STheodore Ts'o */ 3555f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3565f634d06SAneesh Kumar K.V int used, int quota_claim) 35712219aeaSAneesh Kumar K.V { 35812219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3590637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 36012219aeaSAneesh Kumar K.V 3610637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 362d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3630637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3648de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3651084f252STheodore Ts'o "with only %d reserved data blocks", 3660637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3670637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3680637c6f4STheodore Ts'o WARN_ON(1); 3690637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3706bc6e63fSAneesh Kumar K.V } 37112219aeaSAneesh Kumar K.V 3720637c6f4STheodore Ts'o /* Update per-inode reservations */ 3730637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 37471d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3750637c6f4STheodore Ts'o 376f9505c72Schenyichong spin_unlock(&ei->i_block_reservation_lock); 37760e58e0fSMingming Cao 37872b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 37972b8ab9dSEric Sandeen if (quota_claim) 3807b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38172b8ab9dSEric Sandeen else { 3825f634d06SAneesh Kumar K.V /* 3835f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3845f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 38572b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3865f634d06SAneesh Kumar K.V */ 3877b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3885f634d06SAneesh Kumar K.V } 389d6014301SAneesh Kumar K.V 390d6014301SAneesh Kumar K.V /* 391d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 392d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 393d6014301SAneesh Kumar K.V * inode's preallocations. 394d6014301SAneesh Kumar K.V */ 3950637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 39682dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 39727bc446eSbrookxu ext4_discard_preallocations(inode, 0); 39812219aeaSAneesh Kumar K.V } 39912219aeaSAneesh Kumar K.V 400e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 401c398eda0STheodore Ts'o unsigned int line, 40224676da4STheodore Ts'o struct ext4_map_blocks *map) 4036fd058f7STheodore Ts'o { 404345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 405345c0dbfSTheodore Ts'o (inode->i_ino == 406345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 407345c0dbfSTheodore Ts'o return 0; 408ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 409c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 410bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 41124676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 412bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4136a797d27SDarrick J. Wong return -EFSCORRUPTED; 4146fd058f7STheodore Ts'o } 4156fd058f7STheodore Ts'o return 0; 4166fd058f7STheodore Ts'o } 4176fd058f7STheodore Ts'o 41853085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 41953085facSJan Kara ext4_lblk_t len) 42053085facSJan Kara { 42153085facSJan Kara int ret; 42253085facSJan Kara 42333b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 424a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 42553085facSJan Kara 42653085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 42753085facSJan Kara if (ret > 0) 42853085facSJan Kara ret = 0; 42953085facSJan Kara 43053085facSJan Kara return ret; 43153085facSJan Kara } 43253085facSJan Kara 433e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 434c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 435e29136f8STheodore Ts'o 436921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 437921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 438921f266bSDmitry Monakhov struct inode *inode, 439921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 440921f266bSDmitry Monakhov struct ext4_map_blocks *map, 441921f266bSDmitry Monakhov int flags) 442921f266bSDmitry Monakhov { 443921f266bSDmitry Monakhov int retval; 444921f266bSDmitry Monakhov 445921f266bSDmitry Monakhov map->m_flags = 0; 446921f266bSDmitry Monakhov /* 447921f266bSDmitry Monakhov * There is a race window that the result is not the same. 448921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 449921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 450921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 451921f266bSDmitry Monakhov * could be converted. 452921f266bSDmitry Monakhov */ 453c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 454921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4559e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 456921f266bSDmitry Monakhov } else { 4579e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 458921f266bSDmitry Monakhov } 459921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 460921f266bSDmitry Monakhov 461921f266bSDmitry Monakhov /* 462921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 463921f266bSDmitry Monakhov * tree. So the m_len might not equal. 464921f266bSDmitry Monakhov */ 465921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 466921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 467921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 468bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 469921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 470921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 471921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 472921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 473921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 474921f266bSDmitry Monakhov retval, flags); 475921f266bSDmitry Monakhov } 476921f266bSDmitry Monakhov } 477921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 478921f266bSDmitry Monakhov 47955138e0bSTheodore Ts'o /* 480e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4812b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 482f5ab0d1fSMingming Cao * 483f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 484f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 485f5ab0d1fSMingming Cao * mapped. 486f5ab0d1fSMingming Cao * 487e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 488e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 489f5ab0d1fSMingming Cao * based files 490f5ab0d1fSMingming Cao * 491facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 492facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 493facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 494f5ab0d1fSMingming Cao * 495f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 496facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 497facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 498f5ab0d1fSMingming Cao * 499f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 500f5ab0d1fSMingming Cao */ 501e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 502e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5030e855ac8SAneesh Kumar K.V { 504d100eef2SZheng Liu struct extent_status es; 5050e855ac8SAneesh Kumar K.V int retval; 506b8a86845SLukas Czerner int ret = 0; 507921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 508921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 509921f266bSDmitry Monakhov 510921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 511921f266bSDmitry Monakhov #endif 512f5ab0d1fSMingming Cao 513e35fd660STheodore Ts'o map->m_flags = 0; 51470aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 51570aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 516d100eef2SZheng Liu 517e861b5e9STheodore Ts'o /* 518e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 519e861b5e9STheodore Ts'o */ 520e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 521e861b5e9STheodore Ts'o map->m_len = INT_MAX; 522e861b5e9STheodore Ts'o 5234adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5244adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5256a797d27SDarrick J. Wong return -EFSCORRUPTED; 5264adb6ab3SKazuya Mio 527d100eef2SZheng Liu /* Lookup extent status tree firstly */ 5288016e29fSHarshad Shirwadkar if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 5298016e29fSHarshad Shirwadkar ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 530d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 531d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 532d100eef2SZheng Liu map->m_lblk - es.es_lblk; 533d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 534d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 535d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 536d100eef2SZheng Liu if (retval > map->m_len) 537d100eef2SZheng Liu retval = map->m_len; 538d100eef2SZheng Liu map->m_len = retval; 539d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 540facab4d9SJan Kara map->m_pblk = 0; 541facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 542facab4d9SJan Kara if (retval > map->m_len) 543facab4d9SJan Kara retval = map->m_len; 544facab4d9SJan Kara map->m_len = retval; 545d100eef2SZheng Liu retval = 0; 546d100eef2SZheng Liu } else { 5471e83bc81SArnd Bergmann BUG(); 548d100eef2SZheng Liu } 549921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 550921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 551921f266bSDmitry Monakhov &orig_map, flags); 552921f266bSDmitry Monakhov #endif 553d100eef2SZheng Liu goto found; 554d100eef2SZheng Liu } 555d100eef2SZheng Liu 5564df3d265SAneesh Kumar K.V /* 557b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 558b920c755STheodore Ts'o * file system block. 5594df3d265SAneesh Kumar K.V */ 560c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 56112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5629e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5634df3d265SAneesh Kumar K.V } else { 5649e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5650e855ac8SAneesh Kumar K.V } 566f7fec032SZheng Liu if (retval > 0) { 5673be78c73STheodore Ts'o unsigned int status; 568f7fec032SZheng Liu 56944fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 57044fb851dSZheng Liu ext4_warning(inode->i_sb, 57144fb851dSZheng Liu "ES len assertion failed for inode " 57244fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 57344fb851dSZheng Liu inode->i_ino, retval, map->m_len); 57444fb851dSZheng Liu WARN_ON(1); 575921f266bSDmitry Monakhov } 576921f266bSDmitry Monakhov 577f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 578f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 579f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 580d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 581ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 582f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 583f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 584f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 585f7fec032SZheng Liu map->m_len, map->m_pblk, status); 586f7fec032SZheng Liu if (ret < 0) 587f7fec032SZheng Liu retval = ret; 588f7fec032SZheng Liu } 5894df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 590f5ab0d1fSMingming Cao 591d100eef2SZheng Liu found: 592e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 593b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5946fd058f7STheodore Ts'o if (ret != 0) 5956fd058f7STheodore Ts'o return ret; 5966fd058f7STheodore Ts'o } 5976fd058f7STheodore Ts'o 598f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 599c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6004df3d265SAneesh Kumar K.V return retval; 6014df3d265SAneesh Kumar K.V 6024df3d265SAneesh Kumar K.V /* 603f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 604f5ab0d1fSMingming Cao * 605f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 606df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 607f5ab0d1fSMingming Cao * with buffer head unmapped. 608f5ab0d1fSMingming Cao */ 609e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 610b8a86845SLukas Czerner /* 611b8a86845SLukas Czerner * If we need to convert extent to unwritten 612b8a86845SLukas Czerner * we continue and do the actual work in 613b8a86845SLukas Czerner * ext4_ext_map_blocks() 614b8a86845SLukas Czerner */ 615b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 616f5ab0d1fSMingming Cao return retval; 617f5ab0d1fSMingming Cao 618f5ab0d1fSMingming Cao /* 619a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 620a25a4e1aSZheng Liu * it will be set again. 6212a8964d6SAneesh Kumar K.V */ 622a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6232a8964d6SAneesh Kumar K.V 6242a8964d6SAneesh Kumar K.V /* 625556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 626f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 627d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 628f5ab0d1fSMingming Cao * with create == 1 flag. 6294df3d265SAneesh Kumar K.V */ 630c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 631d2a17637SMingming Cao 632d2a17637SMingming Cao /* 6334df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6344df3d265SAneesh Kumar K.V * could have changed the inode type in between 6354df3d265SAneesh Kumar K.V */ 63612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 637e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6380e855ac8SAneesh Kumar K.V } else { 639e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 640267e4db9SAneesh Kumar K.V 641e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 642267e4db9SAneesh Kumar K.V /* 643267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 644267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 645267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 646267e4db9SAneesh Kumar K.V */ 64719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 648267e4db9SAneesh Kumar K.V } 6492ac3b6e0STheodore Ts'o 650d2a17637SMingming Cao /* 6512ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6525f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6535f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6545f634d06SAneesh Kumar K.V * reserve space here. 655d2a17637SMingming Cao */ 6565f634d06SAneesh Kumar K.V if ((retval > 0) && 6571296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6585f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6595f634d06SAneesh Kumar K.V } 660d2a17637SMingming Cao 661f7fec032SZheng Liu if (retval > 0) { 6623be78c73STheodore Ts'o unsigned int status; 663f7fec032SZheng Liu 66444fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 66544fb851dSZheng Liu ext4_warning(inode->i_sb, 66644fb851dSZheng Liu "ES len assertion failed for inode " 66744fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 66844fb851dSZheng Liu inode->i_ino, retval, map->m_len); 66944fb851dSZheng Liu WARN_ON(1); 670921f266bSDmitry Monakhov } 671921f266bSDmitry Monakhov 672adb23551SZheng Liu /* 673c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 674c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6759b623df6SJan Kara * use them before they are really zeroed. We also have to 6769b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6779b623df6SJan Kara * overwrite zeros with stale data from block device. 678c86d8db3SJan Kara */ 679c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 680c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 681c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 682c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 683c86d8db3SJan Kara map->m_pblk, map->m_len); 684c86d8db3SJan Kara if (ret) { 685c86d8db3SJan Kara retval = ret; 686c86d8db3SJan Kara goto out_sem; 687c86d8db3SJan Kara } 688c86d8db3SJan Kara } 689c86d8db3SJan Kara 690c86d8db3SJan Kara /* 691adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 692adb23551SZheng Liu * extent status tree. 693adb23551SZheng Liu */ 694adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 695bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 696adb23551SZheng Liu if (ext4_es_is_written(&es)) 697c86d8db3SJan Kara goto out_sem; 698adb23551SZheng Liu } 699f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 700f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 701f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 702d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 703ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 704f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 705f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 706f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 707f7fec032SZheng Liu map->m_pblk, status); 708c86d8db3SJan Kara if (ret < 0) { 70951865fdaSZheng Liu retval = ret; 710c86d8db3SJan Kara goto out_sem; 711c86d8db3SJan Kara } 71251865fdaSZheng Liu } 7135356f261SAditya Kali 714c86d8db3SJan Kara out_sem: 7150e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 716e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 717b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7186fd058f7STheodore Ts'o if (ret != 0) 7196fd058f7STheodore Ts'o return ret; 72006bd3c36SJan Kara 72106bd3c36SJan Kara /* 72206bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 72306bd3c36SJan Kara * visible after transaction commit must be on transaction's 72406bd3c36SJan Kara * ordered data list. 72506bd3c36SJan Kara */ 72606bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 72706bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 72806bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 72902749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 73006bd3c36SJan Kara ext4_should_order_data(inode)) { 73173131fbbSRoss Zwisler loff_t start_byte = 73273131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 73373131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 73473131fbbSRoss Zwisler 735ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 73673131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 73773131fbbSRoss Zwisler start_byte, length); 738ee0876bcSJan Kara else 73973131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 74073131fbbSRoss Zwisler start_byte, length); 74106bd3c36SJan Kara if (ret) 74206bd3c36SJan Kara return ret; 74306bd3c36SJan Kara } 744a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 745aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 7466fd058f7STheodore Ts'o } 747ec8c60beSRitesh Harjani 748ec8c60beSRitesh Harjani if (retval < 0) 74970aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7500e855ac8SAneesh Kumar K.V return retval; 7510e855ac8SAneesh Kumar K.V } 7520e855ac8SAneesh Kumar K.V 753ed8ad838SJan Kara /* 754ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 755ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 756ed8ad838SJan Kara */ 757ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 758ed8ad838SJan Kara { 759ed8ad838SJan Kara unsigned long old_state; 760ed8ad838SJan Kara unsigned long new_state; 761ed8ad838SJan Kara 762ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 763ed8ad838SJan Kara 764ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 765ed8ad838SJan Kara if (!bh->b_page) { 766ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 767ed8ad838SJan Kara return; 768ed8ad838SJan Kara } 769ed8ad838SJan Kara /* 770ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 771ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 772ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 773ed8ad838SJan Kara */ 774ed8ad838SJan Kara do { 775ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 776ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 777ed8ad838SJan Kara } while (unlikely( 778ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 779ed8ad838SJan Kara } 780ed8ad838SJan Kara 7812ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7822ed88685STheodore Ts'o struct buffer_head *bh, int flags) 783ac27a0ecSDave Kleikamp { 7842ed88685STheodore Ts'o struct ext4_map_blocks map; 785efe70c29SJan Kara int ret = 0; 786ac27a0ecSDave Kleikamp 78746c7f254STao Ma if (ext4_has_inline_data(inode)) 78846c7f254STao Ma return -ERANGE; 78946c7f254STao Ma 7902ed88685STheodore Ts'o map.m_lblk = iblock; 7912ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7922ed88685STheodore Ts'o 793efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 794efe70c29SJan Kara flags); 795ac27a0ecSDave Kleikamp if (ret > 0) { 7962ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 797ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7982ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 799ac27a0ecSDave Kleikamp ret = 0; 800547edce3SRoss Zwisler } else if (ret == 0) { 801547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 802547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 803ac27a0ecSDave Kleikamp } 804ac27a0ecSDave Kleikamp return ret; 805ac27a0ecSDave Kleikamp } 806ac27a0ecSDave Kleikamp 8072ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 8082ed88685STheodore Ts'o struct buffer_head *bh, int create) 8092ed88685STheodore Ts'o { 8102ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8112ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8122ed88685STheodore Ts'o } 8132ed88685STheodore Ts'o 814ac27a0ecSDave Kleikamp /* 815705965bdSJan Kara * Get block function used when preparing for buffered write if we require 816705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 817705965bdSJan Kara * will be converted to written after the IO is complete. 818705965bdSJan Kara */ 819705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 820705965bdSJan Kara struct buffer_head *bh_result, int create) 821705965bdSJan Kara { 822705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 823705965bdSJan Kara inode->i_ino, create); 824705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 825705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 826705965bdSJan Kara } 827705965bdSJan Kara 828efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 829efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 830efe70c29SJan Kara 831e84dfbe2SJan Kara /* 832ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 833ac27a0ecSDave Kleikamp */ 834617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 835c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 836ac27a0ecSDave Kleikamp { 8372ed88685STheodore Ts'o struct ext4_map_blocks map; 8382ed88685STheodore Ts'o struct buffer_head *bh; 839c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 84010560082STheodore Ts'o int err; 841ac27a0ecSDave Kleikamp 842837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8438016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 844ac27a0ecSDave Kleikamp 8452ed88685STheodore Ts'o map.m_lblk = block; 8462ed88685STheodore Ts'o map.m_len = 1; 847c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8482ed88685STheodore Ts'o 84910560082STheodore Ts'o if (err == 0) 85010560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8512ed88685STheodore Ts'o if (err < 0) 85210560082STheodore Ts'o return ERR_PTR(err); 8532ed88685STheodore Ts'o 8542ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 85510560082STheodore Ts'o if (unlikely(!bh)) 85610560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8572ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 858837c23fbSChunguang Xu ASSERT(create != 0); 859837c23fbSChunguang Xu ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8608016e29fSHarshad Shirwadkar || (handle != NULL)); 861ac27a0ecSDave Kleikamp 862ac27a0ecSDave Kleikamp /* 863ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 864ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 865ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 866617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 867ac27a0ecSDave Kleikamp * problem. 868ac27a0ecSDave Kleikamp */ 869ac27a0ecSDave Kleikamp lock_buffer(bh); 870ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 871188c299eSJan Kara err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 872188c299eSJan Kara EXT4_JTR_NONE); 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 957188c299eSJan Kara int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, 958ac27a0ecSDave Kleikamp struct buffer_head *head, 959ac27a0ecSDave Kleikamp unsigned from, 960ac27a0ecSDave Kleikamp unsigned to, 961ac27a0ecSDave Kleikamp int *partial, 962188c299eSJan Kara int (*fn)(handle_t *handle, struct inode *inode, 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 } 981188c299eSJan Kara err = (*fn)(handle, inode, 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 */ 1012188c299eSJan Kara int do_journal_get_write_access(handle_t *handle, struct inode *inode, 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"); 1031188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1032188c299eSJan Kara EXT4_JTR_NONE); 103356d35a4cSJan Kara if (!ret && dirty) 103456d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 103556d35a4cSJan Kara return ret; 1036ac27a0ecSDave Kleikamp } 1037ac27a0ecSDave Kleikamp 1038643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10392058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10402058f83aSMichael Halcrow get_block_t *get_block) 10412058f83aSMichael Halcrow { 104209cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10432058f83aSMichael Halcrow unsigned to = from + len; 10442058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10452058f83aSMichael Halcrow unsigned block_start, block_end; 10462058f83aSMichael Halcrow sector_t block; 10472058f83aSMichael Halcrow int err = 0; 10482058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10492058f83aSMichael Halcrow unsigned bbits; 10500b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10510b578f35SChandan Rajendra int nr_wait = 0; 10520b578f35SChandan Rajendra int i; 10532058f83aSMichael Halcrow 10542058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 105509cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 105609cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10572058f83aSMichael Halcrow BUG_ON(from > to); 10582058f83aSMichael Halcrow 10592058f83aSMichael Halcrow if (!page_has_buffers(page)) 10602058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10612058f83aSMichael Halcrow head = page_buffers(page); 10622058f83aSMichael Halcrow bbits = ilog2(blocksize); 106309cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10642058f83aSMichael Halcrow 10652058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10662058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10672058f83aSMichael Halcrow block_end = block_start + blocksize; 10682058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10692058f83aSMichael Halcrow if (PageUptodate(page)) { 10702058f83aSMichael Halcrow set_buffer_uptodate(bh); 10712058f83aSMichael Halcrow } 10722058f83aSMichael Halcrow continue; 10732058f83aSMichael Halcrow } 10742058f83aSMichael Halcrow if (buffer_new(bh)) 10752058f83aSMichael Halcrow clear_buffer_new(bh); 10762058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10772058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10782058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10792058f83aSMichael Halcrow if (err) 10802058f83aSMichael Halcrow break; 10812058f83aSMichael Halcrow if (buffer_new(bh)) { 10822058f83aSMichael Halcrow if (PageUptodate(page)) { 10832058f83aSMichael Halcrow clear_buffer_new(bh); 10842058f83aSMichael Halcrow set_buffer_uptodate(bh); 10852058f83aSMichael Halcrow mark_buffer_dirty(bh); 10862058f83aSMichael Halcrow continue; 10872058f83aSMichael Halcrow } 10882058f83aSMichael Halcrow if (block_end > to || block_start < from) 10892058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10902058f83aSMichael Halcrow block_start, from); 10912058f83aSMichael Halcrow continue; 10922058f83aSMichael Halcrow } 10932058f83aSMichael Halcrow } 10942058f83aSMichael Halcrow if (PageUptodate(page)) { 10952058f83aSMichael Halcrow set_buffer_uptodate(bh); 10962058f83aSMichael Halcrow continue; 10972058f83aSMichael Halcrow } 10982058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10992058f83aSMichael Halcrow !buffer_unwritten(bh) && 11002058f83aSMichael Halcrow (block_start < from || block_end > to)) { 11012d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 11020b578f35SChandan Rajendra wait[nr_wait++] = bh; 11032058f83aSMichael Halcrow } 11042058f83aSMichael Halcrow } 11052058f83aSMichael Halcrow /* 11062058f83aSMichael Halcrow * If we issued read requests, let them complete. 11072058f83aSMichael Halcrow */ 11080b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11090b578f35SChandan Rajendra wait_on_buffer(wait[i]); 11100b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 11112058f83aSMichael Halcrow err = -EIO; 11122058f83aSMichael Halcrow } 11137e0785fcSChandan Rajendra if (unlikely(err)) { 11142058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 11154f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11160b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11170b578f35SChandan Rajendra int err2; 11180b578f35SChandan Rajendra 11190b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 11200b578f35SChandan Rajendra bh_offset(wait[i])); 11210b578f35SChandan Rajendra if (err2) { 11220b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11230b578f35SChandan Rajendra err = err2; 11240b578f35SChandan Rajendra } 11250b578f35SChandan Rajendra } 11267e0785fcSChandan Rajendra } 11277e0785fcSChandan Rajendra 11282058f83aSMichael Halcrow return err; 11292058f83aSMichael Halcrow } 11302058f83aSMichael Halcrow #endif 11312058f83aSMichael Halcrow 1132bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1133bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1134bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1135ac27a0ecSDave Kleikamp { 1136bfc1af65SNick Piggin struct inode *inode = mapping->host; 11371938a150SAneesh Kumar K.V int ret, needed_blocks; 1138ac27a0ecSDave Kleikamp handle_t *handle; 1139ac27a0ecSDave Kleikamp int retries = 0; 1140bfc1af65SNick Piggin struct page *page; 1141bfc1af65SNick Piggin pgoff_t index; 1142bfc1af65SNick Piggin unsigned from, to; 1143bfc1af65SNick Piggin 11440db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11450db1ff22STheodore Ts'o return -EIO; 11460db1ff22STheodore Ts'o 11479bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11481938a150SAneesh Kumar K.V /* 11491938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11501938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11511938a150SAneesh Kumar K.V */ 11521938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 115309cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 115409cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1155bfc1af65SNick Piggin to = from + len; 1156ac27a0ecSDave Kleikamp 1157f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1158f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1159f19d5870STao Ma flags, pagep); 1160f19d5870STao Ma if (ret < 0) 116147564bfbSTheodore Ts'o return ret; 116247564bfbSTheodore Ts'o if (ret == 1) 116347564bfbSTheodore Ts'o return 0; 1164f19d5870STao Ma } 1165f19d5870STao Ma 116647564bfbSTheodore Ts'o /* 116747564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 116847564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 116947564bfbSTheodore Ts'o * is being written back. So grab it first before we start 117047564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 117147564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 117247564bfbSTheodore Ts'o */ 117347564bfbSTheodore Ts'o retry_grab: 117454566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 117547564bfbSTheodore Ts'o if (!page) 117647564bfbSTheodore Ts'o return -ENOMEM; 117747564bfbSTheodore Ts'o unlock_page(page); 117847564bfbSTheodore Ts'o 117947564bfbSTheodore Ts'o retry_journal: 11809924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1181ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 118209cbfeafSKirill A. Shutemov put_page(page); 118347564bfbSTheodore Ts'o return PTR_ERR(handle); 1184cf108bcaSJan Kara } 1185f19d5870STao Ma 118647564bfbSTheodore Ts'o lock_page(page); 118747564bfbSTheodore Ts'o if (page->mapping != mapping) { 118847564bfbSTheodore Ts'o /* The page got truncated from under us */ 118947564bfbSTheodore Ts'o unlock_page(page); 119009cbfeafSKirill A. Shutemov put_page(page); 1191cf108bcaSJan Kara ext4_journal_stop(handle); 119247564bfbSTheodore Ts'o goto retry_grab; 1193cf108bcaSJan Kara } 11947afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11957afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1196cf108bcaSJan Kara 1197643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11982058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11992058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1200705965bdSJan Kara ext4_get_block_unwritten); 12012058f83aSMichael Halcrow else 12022058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 12032058f83aSMichael Halcrow ext4_get_block); 12042058f83aSMichael Halcrow #else 1205744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1206705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1207705965bdSJan Kara ext4_get_block_unwritten); 1208744692dcSJiaying Zhang else 12096e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 12102058f83aSMichael Halcrow #endif 1211bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1212188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, 1213188c299eSJan Kara page_buffers(page), from, to, NULL, 1214f19d5870STao Ma do_journal_get_write_access); 1215b46be050SAndrey Savochkin } 1216bfc1af65SNick Piggin 1217bfc1af65SNick Piggin if (ret) { 1218c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1219c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1220c93d8f88SEric Biggers 1221bfc1af65SNick Piggin unlock_page(page); 1222ae4d5372SAneesh Kumar K.V /* 12236e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1224ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1225ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12261938a150SAneesh Kumar K.V * 12271938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12281938a150SAneesh Kumar K.V * truncate finishes 1229ae4d5372SAneesh Kumar K.V */ 1230c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12311938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12321938a150SAneesh Kumar K.V 12331938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1234c93d8f88SEric Biggers if (extended) { 1235b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12361938a150SAneesh Kumar K.V /* 1237ffacfa7aSJan Kara * If truncate failed early the inode might 12381938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12391938a150SAneesh Kumar K.V * make sure the inode is removed from the 12401938a150SAneesh Kumar K.V * orphan list in that case. 12411938a150SAneesh Kumar K.V */ 12421938a150SAneesh Kumar K.V if (inode->i_nlink) 12431938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12441938a150SAneesh Kumar K.V } 1245bfc1af65SNick Piggin 124647564bfbSTheodore Ts'o if (ret == -ENOSPC && 124747564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 124847564bfbSTheodore Ts'o goto retry_journal; 124909cbfeafSKirill A. Shutemov put_page(page); 125047564bfbSTheodore Ts'o return ret; 125147564bfbSTheodore Ts'o } 125247564bfbSTheodore Ts'o *pagep = page; 1253ac27a0ecSDave Kleikamp return ret; 1254ac27a0ecSDave Kleikamp } 1255ac27a0ecSDave Kleikamp 1256bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1257188c299eSJan Kara static int write_end_fn(handle_t *handle, struct inode *inode, 1258188c299eSJan Kara struct buffer_head *bh) 1259ac27a0ecSDave Kleikamp { 126013fca323STheodore Ts'o int ret; 1261ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1262ac27a0ecSDave Kleikamp return 0; 1263ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 126413fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 126513fca323STheodore Ts'o clear_buffer_meta(bh); 126613fca323STheodore Ts'o clear_buffer_prio(bh); 126713fca323STheodore Ts'o return ret; 1268ac27a0ecSDave Kleikamp } 1269ac27a0ecSDave Kleikamp 1270eed4333fSZheng Liu /* 1271eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1272eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1273eed4333fSZheng Liu * 1274eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1275eed4333fSZheng Liu * buffers are managed internally. 1276eed4333fSZheng Liu */ 1277eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1278f8514083SAneesh Kumar K.V struct address_space *mapping, 1279f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1280f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1281f8514083SAneesh Kumar K.V { 1282f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1283eed4333fSZheng Liu struct inode *inode = mapping->host; 12840572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1285eed4333fSZheng Liu int ret = 0, ret2; 1286eed4333fSZheng Liu int i_size_changed = 0; 1287c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1288eed4333fSZheng Liu 1289eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 12906984aef5SZhang Yi 12916984aef5SZhang Yi if (ext4_has_inline_data(inode)) 12926984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 12936984aef5SZhang Yi 12946984aef5SZhang Yi copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1295f8514083SAneesh Kumar K.V /* 12964631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1297f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1298c93d8f88SEric Biggers * 1299c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1300c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1301f8514083SAneesh Kumar K.V */ 1302c93d8f88SEric Biggers if (!verity) 13034631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1304f8514083SAneesh Kumar K.V unlock_page(page); 130509cbfeafSKirill A. Shutemov put_page(page); 1306f8514083SAneesh Kumar K.V 1307c93d8f88SEric Biggers if (old_size < pos && !verity) 13080572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1309f8514083SAneesh Kumar K.V /* 1310f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1311f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1312f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1313f8514083SAneesh Kumar K.V * filesystems. 1314f8514083SAneesh Kumar K.V */ 13156984aef5SZhang Yi if (i_size_changed) 13164209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1317f8514083SAneesh Kumar K.V 1318c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1319f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1320f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1321f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1322f8514083SAneesh Kumar K.V */ 1323f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 132455ce2f64SZhang Yi 1325617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1326ac27a0ecSDave Kleikamp if (!ret) 1327ac27a0ecSDave Kleikamp ret = ret2; 1328bfc1af65SNick Piggin 1329c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1330b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1331f8514083SAneesh Kumar K.V /* 1332ffacfa7aSJan Kara * If truncate failed early the inode might still be 1333f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1334f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1335f8514083SAneesh Kumar K.V */ 1336f8514083SAneesh Kumar K.V if (inode->i_nlink) 1337f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1338f8514083SAneesh Kumar K.V } 1339f8514083SAneesh Kumar K.V 1340bfc1af65SNick Piggin return ret ? ret : copied; 1341ac27a0ecSDave Kleikamp } 1342ac27a0ecSDave Kleikamp 1343b90197b6STheodore Ts'o /* 1344b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1345b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1346b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1347b90197b6STheodore Ts'o */ 13483b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 1349188c299eSJan Kara struct inode *inode, 13503b136499SJan Kara struct page *page, 13513b136499SJan Kara unsigned from, unsigned to) 1352b90197b6STheodore Ts'o { 1353b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1354b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1355b90197b6STheodore Ts'o 1356b90197b6STheodore Ts'o bh = head = page_buffers(page); 1357b90197b6STheodore Ts'o do { 1358b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1359b90197b6STheodore Ts'o if (buffer_new(bh)) { 1360b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1361b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1362b90197b6STheodore Ts'o unsigned start, size; 1363b90197b6STheodore Ts'o 1364b90197b6STheodore Ts'o start = max(from, block_start); 1365b90197b6STheodore Ts'o size = min(to, block_end) - start; 1366b90197b6STheodore Ts'o 1367b90197b6STheodore Ts'o zero_user(page, start, size); 1368188c299eSJan Kara write_end_fn(handle, inode, bh); 1369b90197b6STheodore Ts'o } 1370b90197b6STheodore Ts'o clear_buffer_new(bh); 1371b90197b6STheodore Ts'o } 1372b90197b6STheodore Ts'o } 1373b90197b6STheodore Ts'o block_start = block_end; 1374b90197b6STheodore Ts'o bh = bh->b_this_page; 1375b90197b6STheodore Ts'o } while (bh != head); 1376b90197b6STheodore Ts'o } 1377b90197b6STheodore Ts'o 1378bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1379bfc1af65SNick Piggin struct address_space *mapping, 1380bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1381bfc1af65SNick Piggin struct page *page, void *fsdata) 1382ac27a0ecSDave Kleikamp { 1383617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1384bfc1af65SNick Piggin struct inode *inode = mapping->host; 13850572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1386ac27a0ecSDave Kleikamp int ret = 0, ret2; 1387ac27a0ecSDave Kleikamp int partial = 0; 1388bfc1af65SNick Piggin unsigned from, to; 13894631dbf6SDmitry Monakhov int size_changed = 0; 1390c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1391ac27a0ecSDave Kleikamp 13929bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 139309cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1394bfc1af65SNick Piggin to = from + len; 1395bfc1af65SNick Piggin 1396441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1397441c8508SCurt Wohlgemuth 13986984aef5SZhang Yi if (ext4_has_inline_data(inode)) 13996984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 14006984aef5SZhang Yi 14016984aef5SZhang Yi if (unlikely(copied < len) && !PageUptodate(page)) { 1402bfc1af65SNick Piggin copied = 0; 1403188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, from, to); 14043b136499SJan Kara } else { 14053b136499SJan Kara if (unlikely(copied < len)) 1406188c299eSJan Kara ext4_journalled_zero_new_buffers(handle, inode, page, 14073b136499SJan Kara from + copied, to); 1408188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_buffers(page), 1409188c299eSJan Kara from, from + copied, &partial, 14103b136499SJan Kara write_end_fn); 1411ac27a0ecSDave Kleikamp if (!partial) 1412ac27a0ecSDave Kleikamp SetPageUptodate(page); 14133fdcfb66STao Ma } 1414c93d8f88SEric Biggers if (!verity) 14154631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 141619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14172d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14184631dbf6SDmitry Monakhov unlock_page(page); 141909cbfeafSKirill A. Shutemov put_page(page); 14204631dbf6SDmitry Monakhov 1421c93d8f88SEric Biggers if (old_size < pos && !verity) 14220572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14230572639fSXiaoguang Wang 14246984aef5SZhang Yi if (size_changed) { 1425617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1426ac27a0ecSDave Kleikamp if (!ret) 1427ac27a0ecSDave Kleikamp ret = ret2; 1428ac27a0ecSDave Kleikamp } 1429bfc1af65SNick Piggin 1430c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1431f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1432f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1433f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1434f8514083SAneesh Kumar K.V */ 1435f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1436f8514083SAneesh Kumar K.V 1437617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1438ac27a0ecSDave Kleikamp if (!ret) 1439ac27a0ecSDave Kleikamp ret = ret2; 1440c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1441b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1442f8514083SAneesh Kumar K.V /* 1443ffacfa7aSJan Kara * If truncate failed early the inode might still be 1444f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1445f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1446f8514083SAneesh Kumar K.V */ 1447f8514083SAneesh Kumar K.V if (inode->i_nlink) 1448f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1449f8514083SAneesh Kumar K.V } 1450bfc1af65SNick Piggin 1451bfc1af65SNick Piggin return ret ? ret : copied; 1452ac27a0ecSDave Kleikamp } 1453d2a17637SMingming Cao 14549d0be502STheodore Ts'o /* 1455c27e43a1SEric Whitney * Reserve space for a single cluster 14569d0be502STheodore Ts'o */ 1457c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1458d2a17637SMingming Cao { 1459d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14600637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14615dd4056dSChristoph Hellwig int ret; 1462d2a17637SMingming Cao 146360e58e0fSMingming Cao /* 146472b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 146572b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 146672b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 146760e58e0fSMingming Cao */ 14687b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14695dd4056dSChristoph Hellwig if (ret) 14705dd4056dSChristoph Hellwig return ret; 147103179fe9STheodore Ts'o 147203179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 147371d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 147403179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147503179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1476d2a17637SMingming Cao return -ENOSPC; 1477d2a17637SMingming Cao } 14789d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1479c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14800637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 148139bc680aSDmitry Monakhov 1482d2a17637SMingming Cao return 0; /* success */ 1483d2a17637SMingming Cao } 1484d2a17637SMingming Cao 1485f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1486d2a17637SMingming Cao { 1487d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14880637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1489d2a17637SMingming Cao 1490cd213226SMingming Cao if (!to_free) 1491cd213226SMingming Cao return; /* Nothing to release, exit */ 1492cd213226SMingming Cao 1493d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1494cd213226SMingming Cao 14955a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14960637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1497cd213226SMingming Cao /* 14980637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14990637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 15000637c6f4STheodore Ts'o * function is called from invalidate page, it's 15010637c6f4STheodore Ts'o * harmless to return without any action. 1502cd213226SMingming Cao */ 15038de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15040637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15051084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15060637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15070637c6f4STheodore Ts'o WARN_ON(1); 15080637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15090637c6f4STheodore Ts'o } 15100637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15110637c6f4STheodore Ts'o 151272b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 151357042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1514d2a17637SMingming Cao 1515d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 151660e58e0fSMingming Cao 15177b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1518d2a17637SMingming Cao } 1519d2a17637SMingming Cao 1520ac27a0ecSDave Kleikamp /* 152164769240SAlex Tomas * Delayed allocation stuff 152264769240SAlex Tomas */ 152364769240SAlex Tomas 15244e7ea81dSJan Kara struct mpage_da_data { 15254e7ea81dSJan Kara struct inode *inode; 15264e7ea81dSJan Kara struct writeback_control *wbc; 15276b523df4SJan Kara 15284e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15294e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15304e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 153164769240SAlex Tomas /* 15324e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15334e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15344e7ea81dSJan Kara * is delalloc or unwritten. 153564769240SAlex Tomas */ 15364e7ea81dSJan Kara struct ext4_map_blocks map; 15374e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1538dddbd6acSJan Kara unsigned int do_map:1; 15396b8ed620SJan Kara unsigned int scanned_until_end:1; 15404e7ea81dSJan Kara }; 154164769240SAlex Tomas 15424e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15434e7ea81dSJan Kara bool invalidate) 1544c4a0c46eSAneesh Kumar K.V { 1545c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1546c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1547c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1548c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1549c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15504e7ea81dSJan Kara 15514e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15524e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15534e7ea81dSJan Kara return; 1554c4a0c46eSAneesh Kumar K.V 15556b8ed620SJan Kara mpd->scanned_until_end = 0; 1556c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1557c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15584e7ea81dSJan Kara if (invalidate) { 15594e7ea81dSJan Kara ext4_lblk_t start, last; 156009cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 156109cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 156251865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15634e7ea81dSJan Kara } 156451865fdaSZheng Liu 156586679820SMel Gorman pagevec_init(&pvec); 1566c4a0c46eSAneesh Kumar K.V while (index <= end) { 1567397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1568c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1569c4a0c46eSAneesh Kumar K.V break; 1570c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1571c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15722b85a617SJan Kara 1573c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1574c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15754e7ea81dSJan Kara if (invalidate) { 15764e800c03Swangguang if (page_mapped(page)) 15774e800c03Swangguang clear_page_dirty_for_io(page); 157809cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1579c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15804e7ea81dSJan Kara } 1581c4a0c46eSAneesh Kumar K.V unlock_page(page); 1582c4a0c46eSAneesh Kumar K.V } 15839b1d0998SJan Kara pagevec_release(&pvec); 1584c4a0c46eSAneesh Kumar K.V } 1585c4a0c46eSAneesh Kumar K.V } 1586c4a0c46eSAneesh Kumar K.V 1587df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1588df22291fSAneesh Kumar K.V { 1589df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 159092b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1591f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 159292b97816STheodore Ts'o 159392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15945dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1595f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 159692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 159792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1598f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 159957042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 160092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1601f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16027b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 160392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 160492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1605f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1606df22291fSAneesh Kumar K.V return; 1607df22291fSAneesh Kumar K.V } 1608df22291fSAneesh Kumar K.V 1609188c299eSJan Kara static int ext4_bh_delay_or_unwritten(handle_t *handle, struct inode *inode, 1610188c299eSJan Kara struct buffer_head *bh) 161129fa89d0SAneesh Kumar K.V { 1612c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 161329fa89d0SAneesh Kumar K.V } 161429fa89d0SAneesh Kumar K.V 161564769240SAlex Tomas /* 16160b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16170b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16180b02f4c0SEric Whitney * count or making a pending reservation 16190b02f4c0SEric Whitney * where needed 16200b02f4c0SEric Whitney * 16210b02f4c0SEric Whitney * @inode - file containing the newly added block 16220b02f4c0SEric Whitney * @lblk - logical block to be added 16230b02f4c0SEric Whitney * 16240b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16250b02f4c0SEric Whitney */ 16260b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16270b02f4c0SEric Whitney { 16280b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16290b02f4c0SEric Whitney int ret; 16300b02f4c0SEric Whitney bool allocated = false; 16316fed8395SJeffle Xu bool reserved = false; 16320b02f4c0SEric Whitney 16330b02f4c0SEric Whitney /* 16340b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16350b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16360b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16370b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16380b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16390b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16400b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16410b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16420b02f4c0SEric Whitney * extents status tree doesn't get a match. 16430b02f4c0SEric Whitney */ 16440b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16450b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16460b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16470b02f4c0SEric Whitney goto errout; 16486fed8395SJeffle Xu reserved = true; 16490b02f4c0SEric Whitney } else { /* bigalloc */ 16500b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16510b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16520b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16530b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16540b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16550b02f4c0SEric Whitney if (ret < 0) 16560b02f4c0SEric Whitney goto errout; 16570b02f4c0SEric Whitney if (ret == 0) { 16580b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16590b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16600b02f4c0SEric Whitney goto errout; 16616fed8395SJeffle Xu reserved = true; 16620b02f4c0SEric Whitney } else { 16630b02f4c0SEric Whitney allocated = true; 16640b02f4c0SEric Whitney } 16650b02f4c0SEric Whitney } else { 16660b02f4c0SEric Whitney allocated = true; 16670b02f4c0SEric Whitney } 16680b02f4c0SEric Whitney } 16690b02f4c0SEric Whitney } 16700b02f4c0SEric Whitney 16710b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16726fed8395SJeffle Xu if (ret && reserved) 16736fed8395SJeffle Xu ext4_da_release_space(inode, 1); 16740b02f4c0SEric Whitney 16750b02f4c0SEric Whitney errout: 16760b02f4c0SEric Whitney return ret; 16770b02f4c0SEric Whitney } 16780b02f4c0SEric Whitney 16790b02f4c0SEric Whitney /* 16805356f261SAditya Kali * This function is grabs code from the very beginning of 16815356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16825356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16835356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16845356f261SAditya Kali */ 16855356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16865356f261SAditya Kali struct ext4_map_blocks *map, 16875356f261SAditya Kali struct buffer_head *bh) 16885356f261SAditya Kali { 1689d100eef2SZheng Liu struct extent_status es; 16905356f261SAditya Kali int retval; 16915356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1692921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1693921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1694921f266bSDmitry Monakhov 1695921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1696921f266bSDmitry Monakhov #endif 16975356f261SAditya Kali 16985356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16995356f261SAditya Kali invalid_block = ~0; 17005356f261SAditya Kali 17015356f261SAditya Kali map->m_flags = 0; 170270aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 17035356f261SAditya Kali (unsigned long) map->m_lblk); 1704d100eef2SZheng Liu 1705d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1706bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1707d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1708d100eef2SZheng Liu retval = 0; 1709c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1710d100eef2SZheng Liu goto add_delayed; 1711d100eef2SZheng Liu } 1712d100eef2SZheng Liu 1713d100eef2SZheng Liu /* 17143eda41dfSEric Whitney * Delayed extent could be allocated by fallocate. 17153eda41dfSEric Whitney * So we need to check it. 1716d100eef2SZheng Liu */ 17173eda41dfSEric Whitney if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 17183eda41dfSEric Whitney map_bh(bh, inode->i_sb, invalid_block); 17193eda41dfSEric Whitney set_buffer_new(bh); 17203eda41dfSEric Whitney set_buffer_delay(bh); 1721d100eef2SZheng Liu return 0; 1722d100eef2SZheng Liu } 1723d100eef2SZheng Liu 1724d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1725d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1726d100eef2SZheng Liu if (retval > map->m_len) 1727d100eef2SZheng Liu retval = map->m_len; 1728d100eef2SZheng Liu map->m_len = retval; 1729d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1730d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1731d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1732d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1733d100eef2SZheng Liu else 17341e83bc81SArnd Bergmann BUG(); 1735d100eef2SZheng Liu 1736921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1737921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1738921f266bSDmitry Monakhov #endif 1739d100eef2SZheng Liu return retval; 1740d100eef2SZheng Liu } 1741d100eef2SZheng Liu 17425356f261SAditya Kali /* 17435356f261SAditya Kali * Try to see if we can get the block without requesting a new 17445356f261SAditya Kali * file system block. 17455356f261SAditya Kali */ 1746c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1747cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17489c3569b5STao Ma retval = 0; 1749cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17502f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17515356f261SAditya Kali else 17522f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17535356f261SAditya Kali 1754d100eef2SZheng Liu add_delayed: 17555356f261SAditya Kali if (retval == 0) { 1756f7fec032SZheng Liu int ret; 1757ad431025SEric Whitney 17585356f261SAditya Kali /* 17595356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17605356f261SAditya Kali * is it OK? 17615356f261SAditya Kali */ 17625356f261SAditya Kali 17630b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17640b02f4c0SEric Whitney if (ret != 0) { 1765f7fec032SZheng Liu retval = ret; 176651865fdaSZheng Liu goto out_unlock; 1767f7fec032SZheng Liu } 176851865fdaSZheng Liu 17695356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17705356f261SAditya Kali set_buffer_new(bh); 17715356f261SAditya Kali set_buffer_delay(bh); 1772f7fec032SZheng Liu } else if (retval > 0) { 1773f7fec032SZheng Liu int ret; 17743be78c73STheodore Ts'o unsigned int status; 1775f7fec032SZheng Liu 177644fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 177744fb851dSZheng Liu ext4_warning(inode->i_sb, 177844fb851dSZheng Liu "ES len assertion failed for inode " 177944fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 178044fb851dSZheng Liu inode->i_ino, retval, map->m_len); 178144fb851dSZheng Liu WARN_ON(1); 1782921f266bSDmitry Monakhov } 1783921f266bSDmitry Monakhov 1784f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1785f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1786f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1787f7fec032SZheng Liu map->m_pblk, status); 1788f7fec032SZheng Liu if (ret != 0) 1789f7fec032SZheng Liu retval = ret; 17905356f261SAditya Kali } 17915356f261SAditya Kali 17925356f261SAditya Kali out_unlock: 17935356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17945356f261SAditya Kali 17955356f261SAditya Kali return retval; 17965356f261SAditya Kali } 17975356f261SAditya Kali 17985356f261SAditya Kali /* 1799d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1800b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1801b920c755STheodore Ts'o * reserve space for a single block. 180229fa89d0SAneesh Kumar K.V * 180329fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 180429fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 180529fa89d0SAneesh Kumar K.V * 180629fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 180729fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 180829fa89d0SAneesh Kumar K.V * initialized properly. 180964769240SAlex Tomas */ 18109c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18112ed88685STheodore Ts'o struct buffer_head *bh, int create) 181264769240SAlex Tomas { 18132ed88685STheodore Ts'o struct ext4_map_blocks map; 181464769240SAlex Tomas int ret = 0; 181564769240SAlex Tomas 181664769240SAlex Tomas BUG_ON(create == 0); 18172ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18182ed88685STheodore Ts'o 18192ed88685STheodore Ts'o map.m_lblk = iblock; 18202ed88685STheodore Ts'o map.m_len = 1; 182164769240SAlex Tomas 182264769240SAlex Tomas /* 182364769240SAlex Tomas * first, we need to know whether the block is allocated already 182464769240SAlex Tomas * preallocated blocks are unmapped but should treated 182564769240SAlex Tomas * the same as allocated blocks. 182664769240SAlex Tomas */ 18275356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18285356f261SAditya Kali if (ret <= 0) 18292ed88685STheodore Ts'o return ret; 183064769240SAlex Tomas 18312ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1832ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18332ed88685STheodore Ts'o 18342ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18352ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18362ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18372ed88685STheodore Ts'o * get_block multiple times when we write to the same 18382ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18392ed88685STheodore Ts'o * for partial write. 18402ed88685STheodore Ts'o */ 18412ed88685STheodore Ts'o set_buffer_new(bh); 1842c8205636STheodore Ts'o set_buffer_mapped(bh); 18432ed88685STheodore Ts'o } 18442ed88685STheodore Ts'o return 0; 184564769240SAlex Tomas } 184661628a3fSMingming Cao 1847188c299eSJan Kara static int bget_one(handle_t *handle, struct inode *inode, 1848188c299eSJan Kara struct buffer_head *bh) 184962e086beSAneesh Kumar K.V { 185062e086beSAneesh Kumar K.V get_bh(bh); 185162e086beSAneesh Kumar K.V return 0; 185262e086beSAneesh Kumar K.V } 185362e086beSAneesh Kumar K.V 1854188c299eSJan Kara static int bput_one(handle_t *handle, struct inode *inode, 1855188c299eSJan Kara struct buffer_head *bh) 185662e086beSAneesh Kumar K.V { 185762e086beSAneesh Kumar K.V put_bh(bh); 185862e086beSAneesh Kumar K.V return 0; 185962e086beSAneesh Kumar K.V } 186062e086beSAneesh Kumar K.V 186162e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 186262e086beSAneesh Kumar K.V unsigned int len) 186362e086beSAneesh Kumar K.V { 186462e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 186562e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18663fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 186762e086beSAneesh Kumar K.V handle_t *handle = NULL; 18683fdcfb66STao Ma int ret = 0, err = 0; 18693fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18703fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 187162e086beSAneesh Kumar K.V 1872cb20d518STheodore Ts'o ClearPageChecked(page); 18733fdcfb66STao Ma 18743fdcfb66STao Ma if (inline_data) { 18753fdcfb66STao Ma BUG_ON(page->index != 0); 18763fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18773fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18783fdcfb66STao Ma if (inode_bh == NULL) 18793fdcfb66STao Ma goto out; 18803fdcfb66STao Ma } else { 188162e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18823fdcfb66STao Ma if (!page_bufs) { 18833fdcfb66STao Ma BUG(); 18843fdcfb66STao Ma goto out; 18853fdcfb66STao Ma } 1886188c299eSJan Kara ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 18873fdcfb66STao Ma NULL, bget_one); 18883fdcfb66STao Ma } 1889bdf96838STheodore Ts'o /* 1890bdf96838STheodore Ts'o * We need to release the page lock before we start the 1891bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1892bdf96838STheodore Ts'o * out from under us. 1893bdf96838STheodore Ts'o */ 1894bdf96838STheodore Ts'o get_page(page); 189562e086beSAneesh Kumar K.V unlock_page(page); 189662e086beSAneesh Kumar K.V 18979924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18989924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 189962e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 190062e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1901bdf96838STheodore Ts'o put_page(page); 1902bdf96838STheodore Ts'o goto out_no_pagelock; 1903bdf96838STheodore Ts'o } 1904bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1905bdf96838STheodore Ts'o 1906bdf96838STheodore Ts'o lock_page(page); 1907bdf96838STheodore Ts'o put_page(page); 1908bdf96838STheodore Ts'o if (page->mapping != mapping) { 1909bdf96838STheodore Ts'o /* The page got truncated from under us */ 1910bdf96838STheodore Ts'o ext4_journal_stop(handle); 1911bdf96838STheodore Ts'o ret = 0; 191262e086beSAneesh Kumar K.V goto out; 191362e086beSAneesh Kumar K.V } 191462e086beSAneesh Kumar K.V 19153fdcfb66STao Ma if (inline_data) { 1916362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19173fdcfb66STao Ma } else { 1918188c299eSJan Kara ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1919188c299eSJan Kara NULL, do_journal_get_write_access); 192062e086beSAneesh Kumar K.V 1921188c299eSJan Kara err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1922188c299eSJan Kara NULL, write_end_fn); 19233fdcfb66STao Ma } 192462e086beSAneesh Kumar K.V if (ret == 0) 192562e086beSAneesh Kumar K.V ret = err; 1926b5b18160SJan Kara err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 1927afb585a9SMauricio Faria de Oliveira if (ret == 0) 1928afb585a9SMauricio Faria de Oliveira ret = err; 19292d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 193062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 193162e086beSAneesh Kumar K.V if (!ret) 193262e086beSAneesh Kumar K.V ret = err; 193362e086beSAneesh Kumar K.V 193419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 193562e086beSAneesh Kumar K.V out: 1936bdf96838STheodore Ts'o unlock_page(page); 1937bdf96838STheodore Ts'o out_no_pagelock: 1938c915fb80SZhaolong Zhang if (!inline_data && page_bufs) 1939188c299eSJan Kara ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, 1940c915fb80SZhaolong Zhang NULL, bput_one); 19413fdcfb66STao Ma brelse(inode_bh); 194262e086beSAneesh Kumar K.V return ret; 194362e086beSAneesh Kumar K.V } 194462e086beSAneesh Kumar K.V 194561628a3fSMingming Cao /* 194643ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 194743ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 194843ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 194943ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 195043ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 195143ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 195243ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 195343ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 195443ce1d23SAneesh Kumar K.V * 1955b920c755STheodore Ts'o * This function can get called via... 195620970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1957b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1958f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1959b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 196043ce1d23SAneesh Kumar K.V * 196143ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 196243ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 196343ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 196443ce1d23SAneesh Kumar K.V * truncate(f, 1024); 196543ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 196643ce1d23SAneesh Kumar K.V * a[0] = 'a'; 196743ce1d23SAneesh Kumar K.V * truncate(f, 4096); 196843ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 196990802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 197043ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 197143ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 197243ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 197343ce1d23SAneesh Kumar K.V * buffer_heads mapped. 197443ce1d23SAneesh Kumar K.V * 197543ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 197643ce1d23SAneesh Kumar K.V * unwritten in the page. 197743ce1d23SAneesh Kumar K.V * 197843ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 197943ce1d23SAneesh Kumar K.V * 198043ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 198143ce1d23SAneesh Kumar K.V * ext4_writepage() 198243ce1d23SAneesh Kumar K.V * 198343ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 198443ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 198561628a3fSMingming Cao */ 198643ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 198764769240SAlex Tomas struct writeback_control *wbc) 198864769240SAlex Tomas { 1989f8bec370SJan Kara int ret = 0; 199061628a3fSMingming Cao loff_t size; 1991498e5f24STheodore Ts'o unsigned int len; 1992744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 199361628a3fSMingming Cao struct inode *inode = page->mapping->host; 199436ade451SJan Kara struct ext4_io_submit io_submit; 19951c8349a1SNamjae Jeon bool keep_towrite = false; 199664769240SAlex Tomas 19970db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 1998c2a559bcSyangerkun inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 19990db1ff22STheodore Ts'o unlock_page(page); 20000db1ff22STheodore Ts'o return -EIO; 20010db1ff22STheodore Ts'o } 20020db1ff22STheodore Ts'o 2003a9c667f8SLukas Czerner trace_ext4_writepage(page); 200461628a3fSMingming Cao size = i_size_read(inode); 2005c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2006c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 200709cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 200861628a3fSMingming Cao else 200909cbfeafSKirill A. Shutemov len = PAGE_SIZE; 201061628a3fSMingming Cao 2011f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 201264769240SAlex Tomas /* 2013fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2014fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2015fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2016fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2017fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2018cccd147aSTheodore Ts'o * 2019cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2020cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2021cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2022cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2023cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2024cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2025cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2026cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2027cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 202864769240SAlex Tomas */ 2029188c299eSJan Kara if (ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, NULL, 2030c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 203161628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2032cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 203309cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2034fe386132SJan Kara /* 2035fe386132SJan Kara * For memory cleaning there's no point in writing only 2036fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2037fe386132SJan Kara * from direct reclaim. 2038fe386132SJan Kara */ 2039fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2040fe386132SJan Kara == PF_MEMALLOC); 204161628a3fSMingming Cao unlock_page(page); 204261628a3fSMingming Cao return 0; 204361628a3fSMingming Cao } 20441c8349a1SNamjae Jeon keep_towrite = true; 2045f0e6c985SAneesh Kumar K.V } 204664769240SAlex Tomas 2047cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 204843ce1d23SAneesh Kumar K.V /* 204943ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 205043ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 205143ce1d23SAneesh Kumar K.V */ 20523f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 205343ce1d23SAneesh Kumar K.V 205497a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 205597a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 205697a851edSJan Kara if (!io_submit.io_end) { 205797a851edSJan Kara redirty_page_for_writepage(wbc, page); 205897a851edSJan Kara unlock_page(page); 205997a851edSJan Kara return -ENOMEM; 206097a851edSJan Kara } 2061be993933SLei Chen ret = ext4_bio_write_page(&io_submit, page, len, keep_towrite); 206236ade451SJan Kara ext4_io_submit(&io_submit); 206397a851edSJan Kara /* Drop io_end reference we got from init */ 206497a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 206564769240SAlex Tomas return ret; 206664769240SAlex Tomas } 206764769240SAlex Tomas 20685f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20695f1132b2SJan Kara { 20705f1132b2SJan Kara int len; 2071a056bdaaSJan Kara loff_t size; 20725f1132b2SJan Kara int err; 20735f1132b2SJan Kara 20745f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2075a056bdaaSJan Kara clear_page_dirty_for_io(page); 2076a056bdaaSJan Kara /* 2077a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2078a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2079a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2080a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2081a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2082a056bdaaSJan Kara * written to again until we release page lock. So only after 2083a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2084a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2085a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2086a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2087a056bdaaSJan Kara * after page tables are updated. 2088a056bdaaSJan Kara */ 2089a056bdaaSJan Kara size = i_size_read(mpd->inode); 2090c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2091c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 209209cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20935f1132b2SJan Kara else 209409cbfeafSKirill A. Shutemov len = PAGE_SIZE; 2095be993933SLei Chen err = ext4_bio_write_page(&mpd->io_submit, page, len, false); 20965f1132b2SJan Kara if (!err) 20975f1132b2SJan Kara mpd->wbc->nr_to_write--; 20985f1132b2SJan Kara mpd->first_page++; 20995f1132b2SJan Kara 21005f1132b2SJan Kara return err; 21015f1132b2SJan Kara } 21025f1132b2SJan Kara 21036db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 21044e7ea81dSJan Kara 210561628a3fSMingming Cao /* 2106fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2107fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2108fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 210961628a3fSMingming Cao */ 2110fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2111525f4ed8SMingming Cao 2112525f4ed8SMingming Cao /* 21134e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 21144e7ea81dSJan Kara * 21154e7ea81dSJan Kara * @mpd - extent of blocks 21164e7ea81dSJan Kara * @lblk - logical number of the block in the file 211709930042SJan Kara * @bh - buffer head we want to add to the extent 21184e7ea81dSJan Kara * 211909930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 212009930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 212109930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 212209930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 212309930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 212409930042SJan Kara * added. 21254e7ea81dSJan Kara */ 212609930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 212709930042SJan Kara struct buffer_head *bh) 21284e7ea81dSJan Kara { 21294e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21304e7ea81dSJan Kara 213109930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 213209930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 213309930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 213409930042SJan Kara /* So far no extent to map => we write the buffer right away */ 213509930042SJan Kara if (map->m_len == 0) 213609930042SJan Kara return true; 213709930042SJan Kara return false; 213809930042SJan Kara } 21394e7ea81dSJan Kara 21404e7ea81dSJan Kara /* First block in the extent? */ 21414e7ea81dSJan Kara if (map->m_len == 0) { 2142dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2143dddbd6acSJan Kara if (!mpd->do_map) 2144dddbd6acSJan Kara return false; 21454e7ea81dSJan Kara map->m_lblk = lblk; 21464e7ea81dSJan Kara map->m_len = 1; 214709930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 214809930042SJan Kara return true; 21494e7ea81dSJan Kara } 21504e7ea81dSJan Kara 215109930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 215209930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 215309930042SJan Kara return false; 215409930042SJan Kara 21554e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21564e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 215709930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21584e7ea81dSJan Kara map->m_len++; 215909930042SJan Kara return true; 21604e7ea81dSJan Kara } 216109930042SJan Kara return false; 21624e7ea81dSJan Kara } 21634e7ea81dSJan Kara 21645f1132b2SJan Kara /* 21655f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21665f1132b2SJan Kara * 21675f1132b2SJan Kara * @mpd - extent of blocks for mapping 21685f1132b2SJan Kara * @head - the first buffer in the page 21695f1132b2SJan Kara * @bh - buffer we should start processing from 21705f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21715f1132b2SJan Kara * 21725f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21735f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21745f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21755f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21765f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21775f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21785f1132b2SJan Kara * < 0 in case of error during IO submission. 21795f1132b2SJan Kara */ 21805f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21814e7ea81dSJan Kara struct buffer_head *head, 21824e7ea81dSJan Kara struct buffer_head *bh, 21834e7ea81dSJan Kara ext4_lblk_t lblk) 21844e7ea81dSJan Kara { 21854e7ea81dSJan Kara struct inode *inode = mpd->inode; 21865f1132b2SJan Kara int err; 218793407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21884e7ea81dSJan Kara >> inode->i_blkbits; 21894e7ea81dSJan Kara 2190c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2191c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2192c93d8f88SEric Biggers 21934e7ea81dSJan Kara do { 21944e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21954e7ea81dSJan Kara 219609930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21974e7ea81dSJan Kara /* Found extent to map? */ 21984e7ea81dSJan Kara if (mpd->map.m_len) 21995f1132b2SJan Kara return 0; 2200dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2201dddbd6acSJan Kara if (!mpd->do_map) 2202dddbd6acSJan Kara return 0; 220309930042SJan Kara /* Everything mapped so far and we hit EOF */ 22045f1132b2SJan Kara break; 22054e7ea81dSJan Kara } 22064e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 22075f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 22085f1132b2SJan Kara if (mpd->map.m_len == 0) { 22095f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 22105f1132b2SJan Kara if (err < 0) 22114e7ea81dSJan Kara return err; 22124e7ea81dSJan Kara } 22136b8ed620SJan Kara if (lblk >= blocks) { 22146b8ed620SJan Kara mpd->scanned_until_end = 1; 22156b8ed620SJan Kara return 0; 22166b8ed620SJan Kara } 22176b8ed620SJan Kara return 1; 22185f1132b2SJan Kara } 22194e7ea81dSJan Kara 22204e7ea81dSJan Kara /* 22212943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22222943fdbcSRitesh Harjani * may submit fully mapped page for IO 22232943fdbcSRitesh Harjani * 22242943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22252943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22262943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22272943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22282943fdbcSRitesh Harjani * mapping or not. 22292943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22302943fdbcSRitesh Harjani * state according to new extent state. 22312943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22322943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22332943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22342943fdbcSRitesh Harjani */ 22352943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22362943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22372943fdbcSRitesh Harjani bool *map_bh) 22382943fdbcSRitesh Harjani { 22392943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22402943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22412943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22422943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22432943fdbcSRitesh Harjani int err = 0; 2244c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2245c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2246c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22472943fdbcSRitesh Harjani 22482943fdbcSRitesh Harjani bh = head = page_buffers(page); 22492943fdbcSRitesh Harjani do { 22502943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22512943fdbcSRitesh Harjani continue; 22522943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22532943fdbcSRitesh Harjani /* 22542943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22552943fdbcSRitesh Harjani * Find next buffer in the page to map. 22562943fdbcSRitesh Harjani */ 22572943fdbcSRitesh Harjani mpd->map.m_len = 0; 22582943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2259c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2260c8cc8816SRitesh Harjani io_end_size = 0; 22612943fdbcSRitesh Harjani 22622943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22632943fdbcSRitesh Harjani if (err > 0) 22642943fdbcSRitesh Harjani err = 0; 2265c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2266c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22674d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22684d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22694d06bfb9SRitesh Harjani goto out; 22704d06bfb9SRitesh Harjani } 2271d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2272c8cc8816SRitesh Harjani } 22732943fdbcSRitesh Harjani *map_bh = true; 22742943fdbcSRitesh Harjani goto out; 22752943fdbcSRitesh Harjani } 22762943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22772943fdbcSRitesh Harjani clear_buffer_delay(bh); 22782943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22792943fdbcSRitesh Harjani } 22802943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2281c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22822943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2283c8cc8816SRitesh Harjani 2284c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2285c8cc8816SRitesh Harjani io_end_size = 0; 22862943fdbcSRitesh Harjani *map_bh = false; 22872943fdbcSRitesh Harjani out: 22882943fdbcSRitesh Harjani *m_lblk = lblk; 22892943fdbcSRitesh Harjani *m_pblk = pblock; 22902943fdbcSRitesh Harjani return err; 22912943fdbcSRitesh Harjani } 22922943fdbcSRitesh Harjani 22932943fdbcSRitesh Harjani /* 22944e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22954e7ea81dSJan Kara * submit fully mapped pages for IO 22964e7ea81dSJan Kara * 22974e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22984e7ea81dSJan Kara * 22994e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 23004e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 23014e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2302556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 23034e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 23044e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 23054e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 23064e7ea81dSJan Kara */ 23074e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 23084e7ea81dSJan Kara { 23094e7ea81dSJan Kara struct pagevec pvec; 23104e7ea81dSJan Kara int nr_pages, i; 23114e7ea81dSJan Kara struct inode *inode = mpd->inode; 231209cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 23134e7ea81dSJan Kara pgoff_t start, end; 23144e7ea81dSJan Kara ext4_lblk_t lblk; 23152943fdbcSRitesh Harjani ext4_fsblk_t pblock; 23164e7ea81dSJan Kara int err; 23172943fdbcSRitesh Harjani bool map_bh = false; 23184e7ea81dSJan Kara 23194e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23204e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23214e7ea81dSJan Kara lblk = start << bpp_bits; 23224e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23234e7ea81dSJan Kara 232486679820SMel Gorman pagevec_init(&pvec); 23254e7ea81dSJan Kara while (start <= end) { 23262b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2327397162ffSJan Kara &start, end); 23284e7ea81dSJan Kara if (nr_pages == 0) 23294e7ea81dSJan Kara break; 23304e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23314e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23324e7ea81dSJan Kara 23332943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23342943fdbcSRitesh Harjani &map_bh); 23354e7ea81dSJan Kara /* 23362943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23372943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23382943fdbcSRitesh Harjani * So we return to call further extent mapping. 23394e7ea81dSJan Kara */ 234039c0ae16SJason Yan if (err < 0 || map_bh) 23412943fdbcSRitesh Harjani goto out; 23424e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23434e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23442943fdbcSRitesh Harjani if (err < 0) 23452943fdbcSRitesh Harjani goto out; 23464e7ea81dSJan Kara } 23474e7ea81dSJan Kara pagevec_release(&pvec); 23484e7ea81dSJan Kara } 23494e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23504e7ea81dSJan Kara mpd->map.m_len = 0; 23514e7ea81dSJan Kara mpd->map.m_flags = 0; 23524e7ea81dSJan Kara return 0; 23532943fdbcSRitesh Harjani out: 23542943fdbcSRitesh Harjani pagevec_release(&pvec); 23552943fdbcSRitesh Harjani return err; 23564e7ea81dSJan Kara } 23574e7ea81dSJan Kara 23584e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23594e7ea81dSJan Kara { 23604e7ea81dSJan Kara struct inode *inode = mpd->inode; 23614e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23624e7ea81dSJan Kara int get_blocks_flags; 2363090f32eeSLukas Czerner int err, dioread_nolock; 23644e7ea81dSJan Kara 23654e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23664e7ea81dSJan Kara /* 23674e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2368556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23694e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23704e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23714e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23724e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23734e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23744e7ea81dSJan Kara * possible. 23754e7ea81dSJan Kara * 2376754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2377754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2378754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2379754cfed6STheodore Ts'o * the data was copied into the page cache. 23804e7ea81dSJan Kara */ 23814e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2382ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2383ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2384090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2385090f32eeSLukas Czerner if (dioread_nolock) 23864e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23876db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 23884e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23894e7ea81dSJan Kara 23904e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23914e7ea81dSJan Kara if (err < 0) 23924e7ea81dSJan Kara return err; 2393090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23946b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23956b523df4SJan Kara ext4_handle_valid(handle)) { 23966b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23976b523df4SJan Kara handle->h_rsv_handle = NULL; 23986b523df4SJan Kara } 23993613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 24006b523df4SJan Kara } 24014e7ea81dSJan Kara 24024e7ea81dSJan Kara BUG_ON(map->m_len == 0); 24034e7ea81dSJan Kara return 0; 24044e7ea81dSJan Kara } 24054e7ea81dSJan Kara 24064e7ea81dSJan Kara /* 24074e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 24084e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 24094e7ea81dSJan Kara * 24104e7ea81dSJan Kara * @handle - handle for journal operations 24114e7ea81dSJan Kara * @mpd - extent to map 24127534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 24137534e854SJan Kara * is no hope of writing the data. The caller should discard 24147534e854SJan Kara * dirty pages to avoid infinite loops. 24154e7ea81dSJan Kara * 24164e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 24174e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 24184e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 24194e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24204e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24214e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24224e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24234e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24244e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24254e7ea81dSJan Kara */ 24264e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2427cb530541STheodore Ts'o struct mpage_da_data *mpd, 2428cb530541STheodore Ts'o bool *give_up_on_write) 24294e7ea81dSJan Kara { 24304e7ea81dSJan Kara struct inode *inode = mpd->inode; 24314e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24324e7ea81dSJan Kara int err; 24334e7ea81dSJan Kara loff_t disksize; 24346603120eSDmitry Monakhov int progress = 0; 2435c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24364d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24374e7ea81dSJan Kara 24384d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24394d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24404d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2441c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 244227d7c4edSJan Kara do { 24434e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24444e7ea81dSJan Kara if (err < 0) { 24454e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24464e7ea81dSJan Kara 24470db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24489b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2449cb530541STheodore Ts'o goto invalidate_dirty_pages; 24504e7ea81dSJan Kara /* 2451cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2452cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2453cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24544e7ea81dSJan Kara */ 2455cb530541STheodore Ts'o if ((err == -ENOMEM) || 24566603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24576603120eSDmitry Monakhov if (progress) 24586603120eSDmitry Monakhov goto update_disksize; 2459cb530541STheodore Ts'o return err; 24606603120eSDmitry Monakhov } 24614e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24624e7ea81dSJan Kara "Delayed block allocation failed for " 24634e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24644e7ea81dSJan Kara " max blocks %u with error %d", 24654e7ea81dSJan Kara inode->i_ino, 24664e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2467cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24684e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24694e7ea81dSJan Kara "This should not happen!! Data will " 24704e7ea81dSJan Kara "be lost\n"); 24714e7ea81dSJan Kara if (err == -ENOSPC) 24724e7ea81dSJan Kara ext4_print_free_blocks(inode); 2473cb530541STheodore Ts'o invalidate_dirty_pages: 2474cb530541STheodore Ts'o *give_up_on_write = true; 24754e7ea81dSJan Kara return err; 24764e7ea81dSJan Kara } 24776603120eSDmitry Monakhov progress = 1; 24784e7ea81dSJan Kara /* 24794e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24804e7ea81dSJan Kara * extent to map 24814e7ea81dSJan Kara */ 24824e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24834e7ea81dSJan Kara if (err < 0) 24846603120eSDmitry Monakhov goto update_disksize; 248527d7c4edSJan Kara } while (map->m_len); 24864e7ea81dSJan Kara 24876603120eSDmitry Monakhov update_disksize: 2488622cad13STheodore Ts'o /* 2489622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2490622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2491622cad13STheodore Ts'o */ 249209cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 249335df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 24944e7ea81dSJan Kara int err2; 2495622cad13STheodore Ts'o loff_t i_size; 24964e7ea81dSJan Kara 2497622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2498622cad13STheodore Ts'o i_size = i_size_read(inode); 2499622cad13STheodore Ts'o if (disksize > i_size) 2500622cad13STheodore Ts'o disksize = i_size; 2501622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2502622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2503622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2504b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2505878520acSTheodore Ts'o if (err2) { 250654d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 25074e7ea81dSJan Kara "Failed to mark inode %lu dirty", 25084e7ea81dSJan Kara inode->i_ino); 2509878520acSTheodore Ts'o } 25104e7ea81dSJan Kara if (!err) 25114e7ea81dSJan Kara err = err2; 25124e7ea81dSJan Kara } 25134e7ea81dSJan Kara return err; 25144e7ea81dSJan Kara } 25154e7ea81dSJan Kara 25164e7ea81dSJan Kara /* 2517fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 251820970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2519fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2520fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2521fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2522525f4ed8SMingming Cao */ 2523fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2524fffb2739SJan Kara { 2525fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2526525f4ed8SMingming Cao 2527fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2528fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2529525f4ed8SMingming Cao } 253061628a3fSMingming Cao 25318e48dcfbSTheodore Ts'o /* 25324e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25334e7ea81dSJan Kara * and underlying extent to map 25344e7ea81dSJan Kara * 25354e7ea81dSJan Kara * @mpd - where to look for pages 25364e7ea81dSJan Kara * 25374e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25384e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25394e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25404e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25414e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25424e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25434e7ea81dSJan Kara * 25444e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25454e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25464e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25474e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25488e48dcfbSTheodore Ts'o */ 25494e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25508e48dcfbSTheodore Ts'o { 25514e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25528e48dcfbSTheodore Ts'o struct pagevec pvec; 25534f01b02cSTheodore Ts'o unsigned int nr_pages; 2554aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25554e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25564e7ea81dSJan Kara pgoff_t end = mpd->last_page; 255710bbd235SMatthew Wilcox xa_mark_t tag; 25584e7ea81dSJan Kara int i, err = 0; 25594e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25604e7ea81dSJan Kara ext4_lblk_t lblk; 25614e7ea81dSJan Kara struct buffer_head *head; 25628e48dcfbSTheodore Ts'o 25634e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25645b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25655b41d924SEric Sandeen else 25665b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25675b41d924SEric Sandeen 256886679820SMel Gorman pagevec_init(&pvec); 25694e7ea81dSJan Kara mpd->map.m_len = 0; 25704e7ea81dSJan Kara mpd->next_page = index; 25714f01b02cSTheodore Ts'o while (index <= end) { 2572dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 257367fd707fSJan Kara tag); 25748e48dcfbSTheodore Ts'o if (nr_pages == 0) 25756b8ed620SJan Kara break; 25768e48dcfbSTheodore Ts'o 25778e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25788e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25798e48dcfbSTheodore Ts'o 25808e48dcfbSTheodore Ts'o /* 2581aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2582aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2583aeac589aSMing Lei * keep going because someone may be concurrently 2584aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2585aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2586aeac589aSMing Lei * of the old dirty pages. 2587aeac589aSMing Lei */ 2588aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2589aeac589aSMing Lei goto out; 2590aeac589aSMing Lei 25914e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25924e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25934e7ea81dSJan Kara goto out; 259478aaced3STheodore Ts'o 25958e48dcfbSTheodore Ts'o lock_page(page); 25968e48dcfbSTheodore Ts'o /* 25974e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25984e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25994e7ea81dSJan Kara * means it has been truncated or invalidated), or the 26004e7ea81dSJan Kara * page is already under writeback and we are not doing 26014e7ea81dSJan Kara * a data integrity writeback, skip the page 26028e48dcfbSTheodore Ts'o */ 26034f01b02cSTheodore Ts'o if (!PageDirty(page) || 26044f01b02cSTheodore Ts'o (PageWriteback(page) && 26054e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 26064f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 26078e48dcfbSTheodore Ts'o unlock_page(page); 26088e48dcfbSTheodore Ts'o continue; 26098e48dcfbSTheodore Ts'o } 26108e48dcfbSTheodore Ts'o 26118e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 26128e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 26138e48dcfbSTheodore Ts'o 26144e7ea81dSJan Kara if (mpd->map.m_len == 0) 26158eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 26168eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2617f8bec370SJan Kara /* Add all dirty buffers to mpd */ 26184e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 261909cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 26208eb9e5ceSTheodore Ts'o head = page_buffers(page); 26215f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26225f1132b2SJan Kara if (err <= 0) 26234e7ea81dSJan Kara goto out; 26245f1132b2SJan Kara err = 0; 2625aeac589aSMing Lei left--; 26268e48dcfbSTheodore Ts'o } 26278e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26288e48dcfbSTheodore Ts'o cond_resched(); 26298e48dcfbSTheodore Ts'o } 26306b8ed620SJan Kara mpd->scanned_until_end = 1; 26314f01b02cSTheodore Ts'o return 0; 26328eb9e5ceSTheodore Ts'o out: 26338eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26344e7ea81dSJan Kara return err; 26358e48dcfbSTheodore Ts'o } 26368e48dcfbSTheodore Ts'o 263720970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 263864769240SAlex Tomas struct writeback_control *wbc) 263964769240SAlex Tomas { 26404e7ea81dSJan Kara pgoff_t writeback_index = 0; 26414e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 264222208dedSAneesh Kumar K.V int range_whole = 0; 26434e7ea81dSJan Kara int cycled = 1; 264461628a3fSMingming Cao handle_t *handle = NULL; 2645df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26465e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26476b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26485e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26491bce63d1SShaohua Li struct blk_plug plug; 2650cb530541STheodore Ts'o bool give_up_on_write = false; 265161628a3fSMingming Cao 26520db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26530db1ff22STheodore Ts'o return -EIO; 26540db1ff22STheodore Ts'o 2655bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 265620970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2657ba80b101STheodore Ts'o 265861628a3fSMingming Cao /* 265961628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 266061628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 266161628a3fSMingming Cao * because that could violate lock ordering on umount 266261628a3fSMingming Cao */ 2663a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2664bbf023c7SMing Lei goto out_writepages; 26652a21e37eSTheodore Ts'o 266620970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2667043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2668bbf023c7SMing Lei goto out_writepages; 266920970ba6STheodore Ts'o } 267020970ba6STheodore Ts'o 26712a21e37eSTheodore Ts'o /* 26722a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26732a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26742a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26751751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26762a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 267720970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26782a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26792a21e37eSTheodore Ts'o * the stack trace. 26802a21e37eSTheodore Ts'o */ 26810db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26829b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2683bbf023c7SMing Lei ret = -EROFS; 2684bbf023c7SMing Lei goto out_writepages; 2685bbf023c7SMing Lei } 26862a21e37eSTheodore Ts'o 26874e7ea81dSJan Kara /* 26884e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26894e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26904e7ea81dSJan Kara * we'd better clear the inline data here. 26914e7ea81dSJan Kara */ 26924e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26934e7ea81dSJan Kara /* Just inode will be modified... */ 26944e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26954e7ea81dSJan Kara if (IS_ERR(handle)) { 26964e7ea81dSJan Kara ret = PTR_ERR(handle); 26974e7ea81dSJan Kara goto out_writepages; 26984e7ea81dSJan Kara } 26994e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 27004e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 27014e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 27024e7ea81dSJan Kara ext4_journal_stop(handle); 27034e7ea81dSJan Kara } 27044e7ea81dSJan Kara 27054e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 27064e343231Syangerkun /* 27074e343231Syangerkun * We may need to convert up to one extent per block in 27084e343231Syangerkun * the page and we may dirty the inode. 27094e343231Syangerkun */ 27104e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 27114e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 27124e343231Syangerkun } 27134e343231Syangerkun 271422208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 271522208dedSAneesh Kumar K.V range_whole = 1; 271661628a3fSMingming Cao 27172acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 27184e7ea81dSJan Kara writeback_index = mapping->writeback_index; 27194e7ea81dSJan Kara if (writeback_index) 27202acf2c26SAneesh Kumar K.V cycled = 0; 27214e7ea81dSJan Kara mpd.first_page = writeback_index; 27224e7ea81dSJan Kara mpd.last_page = -1; 27235b41d924SEric Sandeen } else { 272409cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 272509cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27265b41d924SEric Sandeen } 2727a1d6cc56SAneesh Kumar K.V 27284e7ea81dSJan Kara mpd.inode = inode; 27294e7ea81dSJan Kara mpd.wbc = wbc; 27304e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27312acf2c26SAneesh Kumar K.V retry: 27326e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27334e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27341bce63d1SShaohua Li blk_start_plug(&plug); 2735dddbd6acSJan Kara 2736dddbd6acSJan Kara /* 2737dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2738dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2739dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2740dddbd6acSJan Kara * started. 2741dddbd6acSJan Kara */ 2742dddbd6acSJan Kara mpd.do_map = 0; 27436b8ed620SJan Kara mpd.scanned_until_end = 0; 2744dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2745dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2746dddbd6acSJan Kara ret = -ENOMEM; 2747dddbd6acSJan Kara goto unplug; 2748dddbd6acSJan Kara } 2749dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2750a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2751a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2752dddbd6acSJan Kara /* Submit prepared bio */ 2753dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2754dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2755dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2756dddbd6acSJan Kara if (ret < 0) 2757dddbd6acSJan Kara goto unplug; 2758dddbd6acSJan Kara 27596b8ed620SJan Kara while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { 27604e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27614e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27624e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27634e7ea81dSJan Kara ret = -ENOMEM; 27644e7ea81dSJan Kara break; 27654e7ea81dSJan Kara } 2766a1d6cc56SAneesh Kumar K.V 2767a1d6cc56SAneesh Kumar K.V /* 27684e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27694e7ea81dSJan Kara * must always write out whole page (makes a difference when 27704e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27714e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27724e7ea81dSJan Kara * not supported by delalloc. 2773a1d6cc56SAneesh Kumar K.V */ 2774a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2775525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2776a1d6cc56SAneesh Kumar K.V 277761628a3fSMingming Cao /* start a new transaction */ 27786b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27796b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 278061628a3fSMingming Cao if (IS_ERR(handle)) { 278161628a3fSMingming Cao ret = PTR_ERR(handle); 27821693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2783fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2784a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27854e7ea81dSJan Kara /* Release allocated io_end */ 27864e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2787dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27884e7ea81dSJan Kara break; 278961628a3fSMingming Cao } 2790dddbd6acSJan Kara mpd.do_map = 1; 2791f63e6005STheodore Ts'o 27924e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27934e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27946b8ed620SJan Kara if (!ret && mpd.map.m_len) 2795cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2796cb530541STheodore Ts'o &give_up_on_write); 2797646caa9cSJan Kara /* 2798646caa9cSJan Kara * Caution: If the handle is synchronous, 2799646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2800646caa9cSJan Kara * to finish which may depend on writeback of pages to 2801646caa9cSJan Kara * complete or on page lock to be released. In that 2802b483bb77SRandy Dunlap * case, we have to wait until after we have 2803646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2804646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2805646caa9cSJan Kara * to be able to complete) before stopping the handle. 2806646caa9cSJan Kara */ 2807646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 280861628a3fSMingming Cao ext4_journal_stop(handle); 2809646caa9cSJan Kara handle = NULL; 2810dddbd6acSJan Kara mpd.do_map = 0; 2811646caa9cSJan Kara } 28124e7ea81dSJan Kara /* Unlock pages we didn't use */ 2813cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2814a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2815a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2816a297b2fcSXiaoguang Wang 2817646caa9cSJan Kara /* 2818646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2819646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2820646caa9cSJan Kara * we are still holding the transaction as we can 2821646caa9cSJan Kara * release the last reference to io_end which may end 2822646caa9cSJan Kara * up doing unwritten extent conversion. 2823646caa9cSJan Kara */ 2824646caa9cSJan Kara if (handle) { 2825646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2826646caa9cSJan Kara ext4_journal_stop(handle); 2827646caa9cSJan Kara } else 28284e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2829dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2830df22291fSAneesh Kumar K.V 28314e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28324e7ea81dSJan Kara /* 28334e7ea81dSJan Kara * Commit the transaction which would 283422208dedSAneesh Kumar K.V * free blocks released in the transaction 283522208dedSAneesh Kumar K.V * and try again 283622208dedSAneesh Kumar K.V */ 2837df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 283822208dedSAneesh Kumar K.V ret = 0; 28394e7ea81dSJan Kara continue; 28404e7ea81dSJan Kara } 28414e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28424e7ea81dSJan Kara if (ret) 284361628a3fSMingming Cao break; 284461628a3fSMingming Cao } 2845dddbd6acSJan Kara unplug: 28461bce63d1SShaohua Li blk_finish_plug(&plug); 28479c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28482acf2c26SAneesh Kumar K.V cycled = 1; 28494e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28504e7ea81dSJan Kara mpd.first_page = 0; 28512acf2c26SAneesh Kumar K.V goto retry; 28522acf2c26SAneesh Kumar K.V } 285361628a3fSMingming Cao 285422208dedSAneesh Kumar K.V /* Update index */ 285522208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 285622208dedSAneesh Kumar K.V /* 28574e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 285822208dedSAneesh Kumar K.V * mode will write it back later 285922208dedSAneesh Kumar K.V */ 28604e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2861a1d6cc56SAneesh Kumar K.V 286261628a3fSMingming Cao out_writepages: 286320970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28644e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2865bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 286661628a3fSMingming Cao return ret; 286764769240SAlex Tomas } 286864769240SAlex Tomas 28695f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28705f0663bbSDan Williams struct writeback_control *wbc) 28715f0663bbSDan Williams { 28725f0663bbSDan Williams int ret; 28735f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28745f0663bbSDan Williams struct inode *inode = mapping->host; 28755f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28765f0663bbSDan Williams 28775f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28785f0663bbSDan Williams return -EIO; 28795f0663bbSDan Williams 2880bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28815f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28825f0663bbSDan Williams 28833f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28845f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28855f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2886bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28875f0663bbSDan Williams return ret; 28885f0663bbSDan Williams } 28895f0663bbSDan Williams 289079f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 289179f0be8dSAneesh Kumar K.V { 28925c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 289379f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 289479f0be8dSAneesh Kumar K.V 289579f0be8dSAneesh Kumar K.V /* 289679f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 289779f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2898179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 289979f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 290079f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 290179f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 290279f0be8dSAneesh Kumar K.V */ 29035c1ff336SEric Whitney free_clusters = 29045c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 29055c1ff336SEric Whitney dirty_clusters = 29065c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 290700d4e736STheodore Ts'o /* 290800d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 290900d4e736STheodore Ts'o */ 29105c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 291110ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 291200d4e736STheodore Ts'o 29135c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29145c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 291579f0be8dSAneesh Kumar K.V /* 2916c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2917c8afb446SEric Sandeen * or free blocks is less than watermark 291879f0be8dSAneesh Kumar K.V */ 291979f0be8dSAneesh Kumar K.V return 1; 292079f0be8dSAneesh Kumar K.V } 292179f0be8dSAneesh Kumar K.V return 0; 292279f0be8dSAneesh Kumar K.V } 292379f0be8dSAneesh Kumar K.V 292464769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 292564769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 292664769240SAlex Tomas struct page **pagep, void **fsdata) 292764769240SAlex Tomas { 292872b8ab9dSEric Sandeen int ret, retries = 0; 292964769240SAlex Tomas struct page *page; 293064769240SAlex Tomas pgoff_t index; 293164769240SAlex Tomas struct inode *inode = mapping->host; 293264769240SAlex Tomas 29330db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29340db1ff22STheodore Ts'o return -EIO; 29350db1ff22STheodore Ts'o 293609cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 293779f0be8dSAneesh Kumar K.V 2938c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2939c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 294079f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 294179f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 294279f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 294379f0be8dSAneesh Kumar K.V } 294479f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29459bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29469c3569b5STao Ma 29479c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29489c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29499c3569b5STao Ma pos, len, flags, 29509c3569b5STao Ma pagep, fsdata); 29519c3569b5STao Ma if (ret < 0) 295247564bfbSTheodore Ts'o return ret; 295347564bfbSTheodore Ts'o if (ret == 1) 295447564bfbSTheodore Ts'o return 0; 29559c3569b5STao Ma } 29569c3569b5STao Ma 2957cc883236SZhang Yi retry: 295847564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 295947564bfbSTheodore Ts'o if (!page) 296047564bfbSTheodore Ts'o return -ENOMEM; 296147564bfbSTheodore Ts'o 296247564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29637afe5aa5SDmitry Monakhov wait_for_stable_page(page); 296464769240SAlex Tomas 2965643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29662058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 29672058f83aSMichael Halcrow ext4_da_get_block_prep); 29682058f83aSMichael Halcrow #else 29696e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 29702058f83aSMichael Halcrow #endif 297164769240SAlex Tomas if (ret < 0) { 297264769240SAlex Tomas unlock_page(page); 2973cc883236SZhang Yi put_page(page); 2974ae4d5372SAneesh Kumar K.V /* 2975ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2976ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2977cc883236SZhang Yi * i_size_read because we hold inode lock. 2978ae4d5372SAneesh Kumar K.V */ 2979ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2980b9a4207dSJan Kara ext4_truncate_failed_write(inode); 298147564bfbSTheodore Ts'o 298247564bfbSTheodore Ts'o if (ret == -ENOSPC && 298347564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 2984cc883236SZhang Yi goto retry; 298547564bfbSTheodore Ts'o return ret; 298664769240SAlex Tomas } 298764769240SAlex Tomas 298847564bfbSTheodore Ts'o *pagep = page; 298964769240SAlex Tomas return ret; 299064769240SAlex Tomas } 299164769240SAlex Tomas 2992632eaeabSMingming Cao /* 2993632eaeabSMingming Cao * Check if we should update i_disksize 2994632eaeabSMingming Cao * when write to the end of file but not require block allocation 2995632eaeabSMingming Cao */ 2996632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2997632eaeabSMingming Cao unsigned long offset) 2998632eaeabSMingming Cao { 2999632eaeabSMingming Cao struct buffer_head *bh; 3000632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3001632eaeabSMingming Cao unsigned int idx; 3002632eaeabSMingming Cao int i; 3003632eaeabSMingming Cao 3004632eaeabSMingming Cao bh = page_buffers(page); 3005632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3006632eaeabSMingming Cao 3007632eaeabSMingming Cao for (i = 0; i < idx; i++) 3008632eaeabSMingming Cao bh = bh->b_this_page; 3009632eaeabSMingming Cao 301029fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3011632eaeabSMingming Cao return 0; 3012632eaeabSMingming Cao return 1; 3013632eaeabSMingming Cao } 3014632eaeabSMingming Cao 301564769240SAlex Tomas static int ext4_da_write_end(struct file *file, 301664769240SAlex Tomas struct address_space *mapping, 301764769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 301864769240SAlex Tomas struct page *page, void *fsdata) 301964769240SAlex Tomas { 302064769240SAlex Tomas struct inode *inode = mapping->host; 302164769240SAlex Tomas loff_t new_i_size; 3022632eaeabSMingming Cao unsigned long start, end; 302379f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 302479f0be8dSAneesh Kumar K.V 302574d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 302674d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 302779f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3028632eaeabSMingming Cao 30299bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 30309c3569b5STao Ma 30319c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30329c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30339c3569b5STao Ma ext4_has_inline_data(inode)) 30346984aef5SZhang Yi return ext4_write_inline_data_end(inode, pos, len, copied, page); 30359c3569b5STao Ma 303664769240SAlex Tomas start = pos & (PAGE_SIZE - 1); 303764769240SAlex Tomas end = start + copied - 1; 303864769240SAlex Tomas 303964769240SAlex Tomas /* 30404df031ffSZhang Yi * Since we are holding inode lock, we are sure i_disksize <= 30414df031ffSZhang Yi * i_size. We also know that if i_disksize < i_size, there are 30424df031ffSZhang Yi * delalloc writes pending in the range upto i_size. If the end of 30434df031ffSZhang Yi * the current write is <= i_size, there's no need to touch 30444df031ffSZhang Yi * i_disksize since writeback will push i_disksize upto i_size 30454df031ffSZhang Yi * eventually. If the end of the current write is > i_size and 30464df031ffSZhang Yi * inside an allocated block (ext4_da_should_update_i_disksize() 30474df031ffSZhang Yi * check), we need to update i_disksize here as neither 30484df031ffSZhang Yi * ext4_writepage() nor certain ext4_writepages() paths not 30494df031ffSZhang Yi * allocating blocks update i_disksize. 30504df031ffSZhang Yi * 30514df031ffSZhang Yi * Note that we defer inode dirtying to generic_write_end() / 30524df031ffSZhang Yi * ext4_da_write_inline_data_end(). 3053d2a17637SMingming Cao */ 305464769240SAlex Tomas new_i_size = pos + copied; 30556984aef5SZhang Yi if (copied && new_i_size > inode->i_size && 30564df031ffSZhang Yi ext4_da_should_update_i_disksize(page, end)) 305764769240SAlex Tomas ext4_update_i_disksize(inode, new_i_size); 3058ccd2506bSTheodore Ts'o 3059cc883236SZhang Yi return generic_write_end(file, mapping, pos, len, copied, page, fsdata); 3060ac27a0ecSDave Kleikamp } 3061ac27a0ecSDave Kleikamp 3062ccd2506bSTheodore Ts'o /* 3063ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3064ccd2506bSTheodore Ts'o */ 3065ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3066ccd2506bSTheodore Ts'o { 3067ccd2506bSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3068ccd2506bSTheodore Ts'o 306971d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3070ccd2506bSTheodore Ts'o return 0; 3071ccd2506bSTheodore Ts'o 3072ccd2506bSTheodore Ts'o /* 3073ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3074ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3075ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3076ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3077ccd2506bSTheodore Ts'o * would require replicating code paths in: 3078ccd2506bSTheodore Ts'o * 307920970ba6STheodore Ts'o * ext4_writepages() -> 3080ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3081ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3082ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3083ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3084ccd2506bSTheodore Ts'o * 3085ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3086ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3087ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3088ccd2506bSTheodore Ts'o * doing I/O at all. 3089ccd2506bSTheodore Ts'o * 3090ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3091380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3092ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3093ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 309425985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3095ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3096ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3097ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3098ccd2506bSTheodore Ts'o * 3099ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3100ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3101ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3102ccd2506bSTheodore Ts'o */ 3103ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3104ccd2506bSTheodore Ts'o } 3105ac27a0ecSDave Kleikamp 3106ac27a0ecSDave Kleikamp /* 3107ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3108ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3109ac27a0ecSDave Kleikamp * 3110ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3111ac27a0ecSDave Kleikamp * journal. If somebody makes a swapfile on an ext4 data-journaling 3112ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3113ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3114ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3115ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3116ac27a0ecSDave Kleikamp * 3117ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3118ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3119ac27a0ecSDave Kleikamp */ 3120ac27a0ecSDave Kleikamp static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3121ac27a0ecSDave Kleikamp { 3122ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3123ac27a0ecSDave Kleikamp journal_t *journal; 3124ac27a0ecSDave Kleikamp int err; 3125ac27a0ecSDave Kleikamp 312646c7f254STao Ma /* 312746c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 312846c7f254STao Ma */ 312946c7f254STao Ma if (ext4_has_inline_data(inode)) 313046c7f254STao Ma return 0; 313146c7f254STao Ma 3132ac27a0ecSDave Kleikamp if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 3133ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DELALLOC)) { 3134ac27a0ecSDave Kleikamp /* 3135ac27a0ecSDave Kleikamp * With delalloc we want to sync the file 3136ac27a0ecSDave Kleikamp * so that we can make sure we allocate 3137ac27a0ecSDave Kleikamp * blocks for file 3138ac27a0ecSDave Kleikamp */ 3139ac27a0ecSDave Kleikamp filemap_write_and_wait(mapping); 3140ac27a0ecSDave Kleikamp } 3141ac27a0ecSDave Kleikamp 314219f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 314319f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3144ac27a0ecSDave Kleikamp /* 3145ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3146ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3147ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3148ac27a0ecSDave Kleikamp * do we expect this to happen. 3149ac27a0ecSDave Kleikamp * 3150ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3151ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3152ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3153ac27a0ecSDave Kleikamp * will.) 3154ac27a0ecSDave Kleikamp * 3155ac27a0ecSDave Kleikamp * NB. EXT4_STATE_JDATA is not set on files other than 3156ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3157ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3158ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3159ac27a0ecSDave Kleikamp * everything they get. 3160ac27a0ecSDave Kleikamp */ 3161ac27a0ecSDave Kleikamp 316219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3163ac27a0ecSDave Kleikamp journal = EXT4_JOURNAL(inode); 3164ac27a0ecSDave Kleikamp jbd2_journal_lock_updates(journal); 316501d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 3166ac27a0ecSDave Kleikamp jbd2_journal_unlock_updates(journal); 3167ac27a0ecSDave Kleikamp 3168ac27a0ecSDave Kleikamp if (err) 3169ac27a0ecSDave Kleikamp return 0; 3170ac27a0ecSDave Kleikamp } 3171ac27a0ecSDave Kleikamp 3172ac58e4fbSRitesh Harjani return iomap_bmap(mapping, block, &ext4_iomap_ops); 3173ac27a0ecSDave Kleikamp } 3174ac27a0ecSDave Kleikamp 3175617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3176ac27a0ecSDave Kleikamp { 317746c7f254STao Ma int ret = -EAGAIN; 317846c7f254STao Ma struct inode *inode = page->mapping->host; 317946c7f254STao Ma 31800562e0baSJiaying Zhang trace_ext4_readpage(page); 318146c7f254STao Ma 318246c7f254STao Ma if (ext4_has_inline_data(inode)) 318346c7f254STao Ma ret = ext4_readpage_inline(inode, page); 318446c7f254STao Ma 318546c7f254STao Ma if (ret == -EAGAIN) 3186a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 318746c7f254STao Ma 318846c7f254STao Ma return ret; 3189ac27a0ecSDave Kleikamp } 3190ac27a0ecSDave Kleikamp 31916311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3192ac27a0ecSDave Kleikamp { 31936311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 319446c7f254STao Ma 31956311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 319646c7f254STao Ma if (ext4_has_inline_data(inode)) 31976311f91fSMatthew Wilcox (Oracle) return; 319846c7f254STao Ma 3199a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3200ac27a0ecSDave Kleikamp } 3201ac27a0ecSDave Kleikamp 3202d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3203d47992f8SLukas Czerner unsigned int length) 3204ac27a0ecSDave Kleikamp { 3205ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32060562e0baSJiaying Zhang 32074520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32084520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32094520fb3cSJan Kara 3210ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32114520fb3cSJan Kara } 32124520fb3cSJan Kara 321353e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3214ca99fdd2SLukas Czerner unsigned int offset, 3215ca99fdd2SLukas Czerner unsigned int length) 32164520fb3cSJan Kara { 32174520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32184520fb3cSJan Kara 3219ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32204520fb3cSJan Kara 3221744692dcSJiaying Zhang /* 3222ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3223ac27a0ecSDave Kleikamp */ 322409cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3225ac27a0ecSDave Kleikamp ClearPageChecked(page); 3226ac27a0ecSDave Kleikamp 3227ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 322853e87268SJan Kara } 322953e87268SJan Kara 323053e87268SJan Kara /* Wrapper for aops... */ 323153e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3232d47992f8SLukas Czerner unsigned int offset, 3233d47992f8SLukas Czerner unsigned int length) 323453e87268SJan Kara { 3235ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3236ac27a0ecSDave Kleikamp } 3237ac27a0ecSDave Kleikamp 3238617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3239ac27a0ecSDave Kleikamp { 3240617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3241ac27a0ecSDave Kleikamp 32420562e0baSJiaying Zhang trace_ext4_releasepage(page); 32430562e0baSJiaying Zhang 3244e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3245e1c36595SJan Kara if (PageChecked(page)) 3246ac27a0ecSDave Kleikamp return 0; 32470390131bSFrank Mayhar if (journal) 3248529a781eSzhangyi (F) return jbd2_journal_try_to_free_buffers(journal, page); 32490390131bSFrank Mayhar else 32500390131bSFrank Mayhar return try_to_free_buffers(page); 3251ac27a0ecSDave Kleikamp } 3252ac27a0ecSDave Kleikamp 3253b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3254b8a6176cSJan Kara { 3255b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3256b8a6176cSJan Kara 3257aa75f4d3SHarshad Shirwadkar if (journal) { 3258aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3259aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3260d0520df7SAndrea Righi return false; 3261d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 32621ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3263d0520df7SAndrea Righi return true; 3264aa75f4d3SHarshad Shirwadkar } 3265aa75f4d3SHarshad Shirwadkar 3266b8a6176cSJan Kara /* Any metadata buffers to write? */ 3267b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3268b8a6176cSJan Kara return true; 3269b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3270b8a6176cSJan Kara } 3271b8a6176cSJan Kara 3272c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3273c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3274c8fdfe29SMatthew Bobrowski loff_t length) 3275364443cbSJan Kara { 3276c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3277c8fdfe29SMatthew Bobrowski 3278c8fdfe29SMatthew Bobrowski /* 3279c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3280c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3281c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3282c8fdfe29SMatthew Bobrowski */ 3283c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3284c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3285c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3286c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3287c8fdfe29SMatthew Bobrowski 3288c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3289c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3290c8fdfe29SMatthew Bobrowski 3291c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3292c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3293c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3294c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3295c8fdfe29SMatthew Bobrowski 32966386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 32976386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32986386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 32996386722aSRitesh Harjani 3300c8fdfe29SMatthew Bobrowski /* 3301c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3302c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3303c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3304c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3305c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3306c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3307c8fdfe29SMatthew Bobrowski * been set first. 3308c8fdfe29SMatthew Bobrowski */ 3309c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3310c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3311c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3312c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3313c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3314c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3315c8fdfe29SMatthew Bobrowski } else { 3316c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3317c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3318c8fdfe29SMatthew Bobrowski } 3319c8fdfe29SMatthew Bobrowski } 3320c8fdfe29SMatthew Bobrowski 3321f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3322f063db5eSMatthew Bobrowski unsigned int flags) 3323f063db5eSMatthew Bobrowski { 3324f063db5eSMatthew Bobrowski handle_t *handle; 3325378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3326378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3327f063db5eSMatthew Bobrowski 3328f063db5eSMatthew Bobrowski /* 3329f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3330f063db5eSMatthew Bobrowski * once for direct I/O. 3331f063db5eSMatthew Bobrowski */ 3332f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3333f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3334f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3335f063db5eSMatthew Bobrowski 3336f063db5eSMatthew Bobrowski retry: 3337f063db5eSMatthew Bobrowski /* 3338f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3339f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3340f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3341f063db5eSMatthew Bobrowski * fits into the credits as well. 3342f063db5eSMatthew Bobrowski */ 3343f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3344f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3345f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3346f063db5eSMatthew Bobrowski 3347378f32baSMatthew Bobrowski /* 3348378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3349378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3350378f32baSMatthew Bobrowski */ 3351378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3352378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3353378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3354378f32baSMatthew Bobrowski /* 3355378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3356378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3357378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3358378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3359378f32baSMatthew Bobrowski */ 3360d0b040f5SJan Kara else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode)) 3361378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3362378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3363378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3364378f32baSMatthew Bobrowski 3365378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3366378f32baSMatthew Bobrowski 3367378f32baSMatthew Bobrowski /* 3368378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3369378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3370378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3371378f32baSMatthew Bobrowski */ 3372378f32baSMatthew Bobrowski if (!m_flags && !ret) 3373378f32baSMatthew Bobrowski ret = -ENOTBLK; 3374f063db5eSMatthew Bobrowski 3375f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3376f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3377f063db5eSMatthew Bobrowski goto retry; 3378f063db5eSMatthew Bobrowski 3379f063db5eSMatthew Bobrowski return ret; 3380f063db5eSMatthew Bobrowski } 3381f063db5eSMatthew Bobrowski 3382f063db5eSMatthew Bobrowski 3383364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3384c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3385364443cbSJan Kara { 3386364443cbSJan Kara int ret; 338709edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 338809edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3389364443cbSJan Kara 3390bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3391bcd8e91fSTheodore Ts'o return -EINVAL; 33927046ae35SAndreas Gruenbacher 3393364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3394364443cbSJan Kara return -ERANGE; 3395364443cbSJan Kara 339609edf4d3SMatthew Bobrowski /* 339709edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 339809edf4d3SMatthew Bobrowski */ 339909edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 340009edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 340109edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3402364443cbSJan Kara 34039faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 34049faac62dSRitesh Harjani /* 34059faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 34069faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 34079faac62dSRitesh Harjani * the mapping information. This could boost performance 34089faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 34099faac62dSRitesh Harjani */ 34109faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3411545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 34129faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 34139faac62dSRitesh Harjani goto out; 34149faac62dSRitesh Harjani } 34159faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 34169faac62dSRitesh Harjani } else { 34179faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 34189faac62dSRitesh Harjani } 3419f063db5eSMatthew Bobrowski 3420545052e9SChristoph Hellwig if (ret < 0) 3421545052e9SChristoph Hellwig return ret; 34229faac62dSRitesh Harjani out: 3423c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3424545052e9SChristoph Hellwig 3425364443cbSJan Kara return 0; 3426364443cbSJan Kara } 3427364443cbSJan Kara 34288cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34298cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34308cd115bdSJan Kara struct iomap *srcmap) 34318cd115bdSJan Kara { 34328cd115bdSJan Kara int ret; 34338cd115bdSJan Kara 34348cd115bdSJan Kara /* 34358cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34368cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34378cd115bdSJan Kara */ 34388cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34398cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34408cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34418cd115bdSJan Kara return ret; 34428cd115bdSJan Kara } 34438cd115bdSJan Kara 3444776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3445776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3446776722e8SJan Kara { 3447378f32baSMatthew Bobrowski /* 3448378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3449378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3450378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3451378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3452378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3453378f32baSMatthew Bobrowski */ 3454378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3455378f32baSMatthew Bobrowski return -ENOTBLK; 3456378f32baSMatthew Bobrowski 3457776722e8SJan Kara return 0; 3458776722e8SJan Kara } 3459776722e8SJan Kara 34608ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3461364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3462776722e8SJan Kara .iomap_end = ext4_iomap_end, 3463364443cbSJan Kara }; 3464364443cbSJan Kara 34658cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 34668cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 34678cd115bdSJan Kara .iomap_end = ext4_iomap_end, 34688cd115bdSJan Kara }; 34698cd115bdSJan Kara 347009edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 347109edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 347209edf4d3SMatthew Bobrowski { 347309edf4d3SMatthew Bobrowski struct extent_status es; 347409edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 347509edf4d3SMatthew Bobrowski 347609edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 347709edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 347809edf4d3SMatthew Bobrowski 347909edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 348009edf4d3SMatthew Bobrowski return false; 348109edf4d3SMatthew Bobrowski 348209edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 348309edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 348409edf4d3SMatthew Bobrowski return false; 348509edf4d3SMatthew Bobrowski } 348609edf4d3SMatthew Bobrowski 348709edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 348809edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 348909edf4d3SMatthew Bobrowski 349009edf4d3SMatthew Bobrowski return true; 349109edf4d3SMatthew Bobrowski } 349209edf4d3SMatthew Bobrowski 349309edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 349409edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 349509edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 349609edf4d3SMatthew Bobrowski { 349709edf4d3SMatthew Bobrowski int ret; 349809edf4d3SMatthew Bobrowski bool delalloc = false; 349909edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 350009edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 350109edf4d3SMatthew Bobrowski 350209edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 350309edf4d3SMatthew Bobrowski return -EINVAL; 350409edf4d3SMatthew Bobrowski 35057cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35067cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3507ba5843f5SJan Kara if (ret != -EAGAIN) { 3508ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3509ba5843f5SJan Kara ret = -ENOENT; 3510ba5843f5SJan Kara return ret; 3511ba5843f5SJan Kara } 3512ba5843f5SJan Kara } 351312735f88SJan Kara 351409edf4d3SMatthew Bobrowski /* 351509edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 351609edf4d3SMatthew Bobrowski */ 351709edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 351809edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 351909edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 352012735f88SJan Kara 3521b2c57642SRitesh Harjani /* 3522b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3523b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3524b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3525b2c57642SRitesh Harjani * -EIO error. 3526b2c57642SRitesh Harjani */ 3527b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3528b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3529b2c57642SRitesh Harjani 3530b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3531b2c57642SRitesh Harjani map.m_flags = 0; 3532b2c57642SRitesh Harjani goto set_iomap; 3533b2c57642SRitesh Harjani } 3534b2c57642SRitesh Harjani } 3535b2c57642SRitesh Harjani 353612735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3537ba5843f5SJan Kara if (ret < 0) 3538ba5843f5SJan Kara return ret; 3539914f82a3SJan Kara if (ret == 0) 354009edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 354109edf4d3SMatthew Bobrowski 3542b2c57642SRitesh Harjani set_iomap: 354309edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 354409edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 354509edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 354609edf4d3SMatthew Bobrowski 354709edf4d3SMatthew Bobrowski return 0; 3548914f82a3SJan Kara } 3549914f82a3SJan Kara 355009edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 355109edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 355209edf4d3SMatthew Bobrowski }; 35534c0425ffSMingming Cao 3554ac27a0ecSDave Kleikamp /* 3555617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3556ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3557ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3558ac27a0ecSDave Kleikamp * not necessarily locked. 3559ac27a0ecSDave Kleikamp * 3560ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3561ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3562ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3563ac27a0ecSDave Kleikamp * 3564ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3565ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3566ac27a0ecSDave Kleikamp */ 3567617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3568ac27a0ecSDave Kleikamp { 3569ac27a0ecSDave Kleikamp SetPageChecked(page); 3570ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3571ac27a0ecSDave Kleikamp } 3572ac27a0ecSDave Kleikamp 35736dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 35746dcc693bSJan Kara { 35756dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 35766dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 35776dcc693bSJan Kara return __set_page_dirty_buffers(page); 35786dcc693bSJan Kara } 35796dcc693bSJan Kara 35800e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 35810e6895baSRitesh Harjani struct file *file, sector_t *span) 35820e6895baSRitesh Harjani { 35830e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 35840e6895baSRitesh Harjani &ext4_iomap_report_ops); 35850e6895baSRitesh Harjani } 35860e6895baSRitesh Harjani 358774d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3588617ba13bSMingming Cao .readpage = ext4_readpage, 35896311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 359043ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 359120970ba6STheodore Ts'o .writepages = ext4_writepages, 3592bfc1af65SNick Piggin .write_begin = ext4_write_begin, 359374d553aaSTheodore Ts'o .write_end = ext4_write_end, 35946dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3595617ba13bSMingming Cao .bmap = ext4_bmap, 3596617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3597617ba13bSMingming Cao .releasepage = ext4_releasepage, 3598378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3599ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 36008ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3601aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36020e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3603ac27a0ecSDave Kleikamp }; 3604ac27a0ecSDave Kleikamp 3605617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3606617ba13bSMingming Cao .readpage = ext4_readpage, 36076311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 360843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 360920970ba6STheodore Ts'o .writepages = ext4_writepages, 3610bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3611bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3612617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3613617ba13bSMingming Cao .bmap = ext4_bmap, 36144520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3615617ba13bSMingming Cao .releasepage = ext4_releasepage, 3616378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 36178ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3618aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36190e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3620ac27a0ecSDave Kleikamp }; 3621ac27a0ecSDave Kleikamp 362264769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 362364769240SAlex Tomas .readpage = ext4_readpage, 36246311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 362543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 362620970ba6STheodore Ts'o .writepages = ext4_writepages, 362764769240SAlex Tomas .write_begin = ext4_da_write_begin, 362864769240SAlex Tomas .write_end = ext4_da_write_end, 36296dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 363064769240SAlex Tomas .bmap = ext4_bmap, 36318fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 363264769240SAlex Tomas .releasepage = ext4_releasepage, 3633378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 363464769240SAlex Tomas .migratepage = buffer_migrate_page, 36358ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3636aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36370e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 363864769240SAlex Tomas }; 363964769240SAlex Tomas 36405f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36415f0663bbSDan Williams .writepages = ext4_dax_writepages, 36425f0663bbSDan Williams .direct_IO = noop_direct_IO, 3643b82a96c9SMatthew Wilcox (Oracle) .set_page_dirty = __set_page_dirty_no_writeback, 364494dbb631SToshi Kani .bmap = ext4_bmap, 36455f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36460e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 36475f0663bbSDan Williams }; 36485f0663bbSDan Williams 3649617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3650ac27a0ecSDave Kleikamp { 36513d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36523d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36533d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36543d2b1582SLukas Czerner break; 36553d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3656617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 365774d553aaSTheodore Ts'o return; 36583d2b1582SLukas Czerner default: 36593d2b1582SLukas Czerner BUG(); 36603d2b1582SLukas Czerner } 36615f0663bbSDan Williams if (IS_DAX(inode)) 36625f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 36635f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 366474d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 366574d553aaSTheodore Ts'o else 366674d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3667ac27a0ecSDave Kleikamp } 3668ac27a0ecSDave Kleikamp 3669923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3670d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3671d863dc36SLukas Czerner { 367209cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 367309cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3674923ae0ffSRoss Zwisler unsigned blocksize, pos; 3675d863dc36SLukas Czerner ext4_lblk_t iblock; 3676d863dc36SLukas Czerner struct inode *inode = mapping->host; 3677d863dc36SLukas Czerner struct buffer_head *bh; 3678d863dc36SLukas Czerner struct page *page; 3679d863dc36SLukas Czerner int err = 0; 3680d863dc36SLukas Czerner 368109cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3682c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3683d863dc36SLukas Czerner if (!page) 3684d863dc36SLukas Czerner return -ENOMEM; 3685d863dc36SLukas Czerner 3686d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3687d863dc36SLukas Czerner 368809cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3689d863dc36SLukas Czerner 3690d863dc36SLukas Czerner if (!page_has_buffers(page)) 3691d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3692d863dc36SLukas Czerner 3693d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3694d863dc36SLukas Czerner bh = page_buffers(page); 3695d863dc36SLukas Czerner pos = blocksize; 3696d863dc36SLukas Czerner while (offset >= pos) { 3697d863dc36SLukas Czerner bh = bh->b_this_page; 3698d863dc36SLukas Czerner iblock++; 3699d863dc36SLukas Czerner pos += blocksize; 3700d863dc36SLukas Czerner } 3701d863dc36SLukas Czerner if (buffer_freed(bh)) { 3702d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3703d863dc36SLukas Czerner goto unlock; 3704d863dc36SLukas Czerner } 3705d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3706d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3707d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3708d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3709d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3710d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3711d863dc36SLukas Czerner goto unlock; 3712d863dc36SLukas Czerner } 3713d863dc36SLukas Czerner } 3714d863dc36SLukas Czerner 3715d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3716d863dc36SLukas Czerner if (PageUptodate(page)) 3717d863dc36SLukas Czerner set_buffer_uptodate(bh); 3718d863dc36SLukas Czerner 3719d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 37202d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 37212d069c08Szhangyi (F) if (err) 3722d863dc36SLukas Czerner goto unlock; 37234f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3724c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3725a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3726834f1565SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3727834f1565SEric Biggers bh_offset(bh)); 3728834f1565SEric Biggers if (err) { 3729834f1565SEric Biggers clear_buffer_uptodate(bh); 3730834f1565SEric Biggers goto unlock; 3731834f1565SEric Biggers } 3732c9c7429cSMichael Halcrow } 3733d863dc36SLukas Czerner } 3734d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3735d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3736188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 3737188c299eSJan Kara EXT4_JTR_NONE); 3738d863dc36SLukas Czerner if (err) 3739d863dc36SLukas Czerner goto unlock; 3740d863dc36SLukas Czerner } 3741d863dc36SLukas Czerner zero_user(page, offset, length); 3742d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3743d863dc36SLukas Czerner 3744d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3745d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37460713ed0cSLukas Czerner } else { 3747353eefd3Sjon ernst err = 0; 3748d863dc36SLukas Czerner mark_buffer_dirty(bh); 37493957ef53SJan Kara if (ext4_should_order_data(inode)) 375073131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 375173131fbbSRoss Zwisler length); 37520713ed0cSLukas Czerner } 3753d863dc36SLukas Czerner 3754d863dc36SLukas Czerner unlock: 3755d863dc36SLukas Czerner unlock_page(page); 375609cbfeafSKirill A. Shutemov put_page(page); 3757d863dc36SLukas Czerner return err; 3758d863dc36SLukas Czerner } 3759d863dc36SLukas Czerner 376094350ab5SMatthew Wilcox /* 3761923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3762923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3763923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3764923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 37653088e5a5SBhaskar Chowdhury * that corresponds to 'from' 3766923ae0ffSRoss Zwisler */ 3767923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3768923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3769923ae0ffSRoss Zwisler { 3770923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 377109cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3772923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3773923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3774923ae0ffSRoss Zwisler 3775923ae0ffSRoss Zwisler /* 3776923ae0ffSRoss Zwisler * correct length if it does not fall between 3777923ae0ffSRoss Zwisler * 'from' and the end of the block 3778923ae0ffSRoss Zwisler */ 3779923ae0ffSRoss Zwisler if (length > max || length < 0) 3780923ae0ffSRoss Zwisler length = max; 3781923ae0ffSRoss Zwisler 378247e69351SJan Kara if (IS_DAX(inode)) { 378347e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 378447e69351SJan Kara &ext4_iomap_ops); 378547e69351SJan Kara } 3786923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3787923ae0ffSRoss Zwisler } 3788923ae0ffSRoss Zwisler 3789923ae0ffSRoss Zwisler /* 379094350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 379194350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 379294350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 379394350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 379494350ab5SMatthew Wilcox */ 3795c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 379694350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 379794350ab5SMatthew Wilcox { 379809cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 379994350ab5SMatthew Wilcox unsigned length; 380094350ab5SMatthew Wilcox unsigned blocksize; 380194350ab5SMatthew Wilcox struct inode *inode = mapping->host; 380294350ab5SMatthew Wilcox 38030d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3804592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 38050d06863fSTheodore Ts'o return 0; 38060d06863fSTheodore Ts'o 380794350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 380894350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 380994350ab5SMatthew Wilcox 381094350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 381194350ab5SMatthew Wilcox } 381294350ab5SMatthew Wilcox 3813a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3814a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3815a87dd18cSLukas Czerner { 3816a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3817a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3818e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3819a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3820a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3821a87dd18cSLukas Czerner int err = 0; 3822a87dd18cSLukas Czerner 3823e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3824e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3825e1be3a92SLukas Czerner 3826a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3827a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3828a87dd18cSLukas Czerner 3829a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3830e1be3a92SLukas Czerner if (start == end && 3831e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3832a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3833a87dd18cSLukas Czerner lstart, length); 3834a87dd18cSLukas Czerner return err; 3835a87dd18cSLukas Czerner } 3836a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3837e1be3a92SLukas Czerner if (partial_start) { 3838a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3839a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3840a87dd18cSLukas Czerner if (err) 3841a87dd18cSLukas Czerner return err; 3842a87dd18cSLukas Czerner } 3843a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3844e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3845a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3846e1be3a92SLukas Czerner byte_end - partial_end, 3847e1be3a92SLukas Czerner partial_end + 1); 3848a87dd18cSLukas Czerner return err; 3849a87dd18cSLukas Czerner } 3850a87dd18cSLukas Czerner 385191ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 385291ef4cafSDuane Griffin { 385391ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 385491ef4cafSDuane Griffin return 1; 385591ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 385691ef4cafSDuane Griffin return 1; 385791ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 385891ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 385991ef4cafSDuane Griffin return 0; 386091ef4cafSDuane Griffin } 386191ef4cafSDuane Griffin 3862ac27a0ecSDave Kleikamp /* 386301127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 386401127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 386501127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 386601127848SJan Kara * that will never happen after we truncate page cache. 386701127848SJan Kara */ 386801127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 386901127848SJan Kara loff_t len) 387001127848SJan Kara { 387101127848SJan Kara handle_t *handle; 38724209ae12SHarshad Shirwadkar int ret; 38734209ae12SHarshad Shirwadkar 387401127848SJan Kara loff_t size = i_size_read(inode); 387501127848SJan Kara 38765955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 387701127848SJan Kara if (offset > size || offset + len < size) 387801127848SJan Kara return 0; 387901127848SJan Kara 388001127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 388101127848SJan Kara return 0; 388201127848SJan Kara 388301127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 388401127848SJan Kara if (IS_ERR(handle)) 388501127848SJan Kara return PTR_ERR(handle); 388601127848SJan Kara ext4_update_i_disksize(inode, size); 38874209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 388801127848SJan Kara ext4_journal_stop(handle); 388901127848SJan Kara 38904209ae12SHarshad Shirwadkar return ret; 389101127848SJan Kara } 389201127848SJan Kara 3893d4f5258eSJan Kara static void ext4_wait_dax_page(struct inode *inode) 3894430657b6SRoss Zwisler { 3895d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 3896430657b6SRoss Zwisler schedule(); 3897d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 3898430657b6SRoss Zwisler } 3899430657b6SRoss Zwisler 3900430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3901430657b6SRoss Zwisler { 3902430657b6SRoss Zwisler struct page *page; 3903430657b6SRoss Zwisler int error; 3904430657b6SRoss Zwisler 3905d4f5258eSJan Kara if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock))) 3906430657b6SRoss Zwisler return -EINVAL; 3907430657b6SRoss Zwisler 3908430657b6SRoss Zwisler do { 3909430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3910430657b6SRoss Zwisler if (!page) 3911430657b6SRoss Zwisler return 0; 3912430657b6SRoss Zwisler 3913430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3914430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3915430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3916d4f5258eSJan Kara ext4_wait_dax_page(inode)); 3917b1f38217SRoss Zwisler } while (error == 0); 3918430657b6SRoss Zwisler 3919430657b6SRoss Zwisler return error; 3920430657b6SRoss Zwisler } 3921430657b6SRoss Zwisler 392201127848SJan Kara /* 3923cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3924a4bb6b64SAllison Henderson * associated with the given offset and length 3925a4bb6b64SAllison Henderson * 3926a4bb6b64SAllison Henderson * @inode: File inode 3927a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3928a4bb6b64SAllison Henderson * @len: The length of the hole 3929a4bb6b64SAllison Henderson * 39304907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3931a4bb6b64SAllison Henderson */ 3932a4bb6b64SAllison Henderson 3933aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3934a4bb6b64SAllison Henderson { 393526a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 393626a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 393726a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3938a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 393926a4c0c6STheodore Ts'o handle_t *handle; 394026a4c0c6STheodore Ts'o unsigned int credits; 39414209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 394226a4c0c6STheodore Ts'o 3943b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3944aaddea81SZheng Liu 3945c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3946c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3947d4f5258eSJan Kara filemap_invalidate_lock(mapping); 3948c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3949d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 3950c1e8220bSTheodore Ts'o if (ret) 3951c1e8220bSTheodore Ts'o return ret; 3952c1e8220bSTheodore Ts'o } 3953c1e8220bSTheodore Ts'o 395426a4c0c6STheodore Ts'o /* 395526a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 395626a4c0c6STheodore Ts'o * Then release them. 395726a4c0c6STheodore Ts'o */ 3958cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 395926a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 396026a4c0c6STheodore Ts'o offset + length - 1); 396126a4c0c6STheodore Ts'o if (ret) 396226a4c0c6STheodore Ts'o return ret; 396326a4c0c6STheodore Ts'o } 396426a4c0c6STheodore Ts'o 39655955102cSAl Viro inode_lock(inode); 39669ef06cecSLukas Czerner 396726a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 396826a4c0c6STheodore Ts'o if (offset >= inode->i_size) 396926a4c0c6STheodore Ts'o goto out_mutex; 397026a4c0c6STheodore Ts'o 397126a4c0c6STheodore Ts'o /* 397226a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 397326a4c0c6STheodore Ts'o * to end after the page that contains i_size 397426a4c0c6STheodore Ts'o */ 397526a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 397626a4c0c6STheodore Ts'o length = inode->i_size + 397709cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 397826a4c0c6STheodore Ts'o offset; 397926a4c0c6STheodore Ts'o } 398026a4c0c6STheodore Ts'o 3981a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 3982a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 3983a361293fSJan Kara /* 3984a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 3985a361293fSJan Kara * partial block 3986a361293fSJan Kara */ 3987a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 3988a361293fSJan Kara if (ret < 0) 3989a361293fSJan Kara goto out_mutex; 3990a361293fSJan Kara 3991a361293fSJan Kara } 3992a361293fSJan Kara 3993ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 3994ea3d7209SJan Kara inode_dio_wait(inode); 3995ea3d7209SJan Kara 3996ea3d7209SJan Kara /* 3997ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 3998ea3d7209SJan Kara * page cache. 3999ea3d7209SJan Kara */ 4000d4f5258eSJan Kara filemap_invalidate_lock(mapping); 4001430657b6SRoss Zwisler 4002430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4003430657b6SRoss Zwisler if (ret) 4004430657b6SRoss Zwisler goto out_dio; 4005430657b6SRoss Zwisler 4006a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4007a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 400826a4c0c6STheodore Ts'o 4009a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 401001127848SJan Kara if (last_block_offset > first_block_offset) { 401101127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 401201127848SJan Kara if (ret) 401301127848SJan Kara goto out_dio; 4014a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4015a87dd18cSLukas Czerner last_block_offset); 401601127848SJan Kara } 401726a4c0c6STheodore Ts'o 401826a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 401926a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 402026a4c0c6STheodore Ts'o else 402126a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 402226a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 402326a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 402426a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 402526a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 402626a4c0c6STheodore Ts'o goto out_dio; 402726a4c0c6STheodore Ts'o } 402826a4c0c6STheodore Ts'o 4029a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4030a87dd18cSLukas Czerner length); 403126a4c0c6STheodore Ts'o if (ret) 403226a4c0c6STheodore Ts'o goto out_stop; 403326a4c0c6STheodore Ts'o 403426a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 403526a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 403626a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 403726a4c0c6STheodore Ts'o 4038eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4039eee597acSLukas Czerner if (stop_block > first_block) { 404026a4c0c6STheodore Ts'o 404126a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 404227bc446eSbrookxu ext4_discard_preallocations(inode, 0); 404326a4c0c6STheodore Ts'o 404426a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 404526a4c0c6STheodore Ts'o stop_block - first_block); 404626a4c0c6STheodore Ts'o if (ret) { 404726a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 404826a4c0c6STheodore Ts'o goto out_stop; 404926a4c0c6STheodore Ts'o } 405026a4c0c6STheodore Ts'o 405126a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 405226a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 405326a4c0c6STheodore Ts'o stop_block - 1); 405426a4c0c6STheodore Ts'o else 40554f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 405626a4c0c6STheodore Ts'o stop_block); 405726a4c0c6STheodore Ts'o 4058819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4059eee597acSLukas Czerner } 4060a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 406126a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 406226a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4063e251f9bcSMaxim Patlasov 4064eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 40654209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 40664209ae12SHarshad Shirwadkar if (unlikely(ret2)) 40674209ae12SHarshad Shirwadkar ret = ret2; 406867a7d5f5SJan Kara if (ret >= 0) 406967a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 407026a4c0c6STheodore Ts'o out_stop: 407126a4c0c6STheodore Ts'o ext4_journal_stop(handle); 407226a4c0c6STheodore Ts'o out_dio: 4073d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 407426a4c0c6STheodore Ts'o out_mutex: 40755955102cSAl Viro inode_unlock(inode); 407626a4c0c6STheodore Ts'o return ret; 4077a4bb6b64SAllison Henderson } 4078a4bb6b64SAllison Henderson 4079a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4080a361293fSJan Kara { 4081a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4082a361293fSJan Kara struct jbd2_inode *jinode; 4083a361293fSJan Kara 4084a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4085a361293fSJan Kara return 0; 4086a361293fSJan Kara 4087a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4088a361293fSJan Kara spin_lock(&inode->i_lock); 4089a361293fSJan Kara if (!ei->jinode) { 4090a361293fSJan Kara if (!jinode) { 4091a361293fSJan Kara spin_unlock(&inode->i_lock); 4092a361293fSJan Kara return -ENOMEM; 4093a361293fSJan Kara } 4094a361293fSJan Kara ei->jinode = jinode; 4095a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4096a361293fSJan Kara jinode = NULL; 4097a361293fSJan Kara } 4098a361293fSJan Kara spin_unlock(&inode->i_lock); 4099a361293fSJan Kara if (unlikely(jinode != NULL)) 4100a361293fSJan Kara jbd2_free_inode(jinode); 4101a361293fSJan Kara return 0; 4102a361293fSJan Kara } 4103a361293fSJan Kara 4104a4bb6b64SAllison Henderson /* 4105617ba13bSMingming Cao * ext4_truncate() 4106ac27a0ecSDave Kleikamp * 4107617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4108617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4109ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4110ac27a0ecSDave Kleikamp * 411142b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4112ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4113ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4114ac27a0ecSDave Kleikamp * 4115ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4116ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4117ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4118ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4119ac27a0ecSDave Kleikamp * left-to-right works OK too). 4120ac27a0ecSDave Kleikamp * 4121ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4122ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4123ac27a0ecSDave Kleikamp * 4124ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4125617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4126ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4127617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4128617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4129ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4130617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4131ac27a0ecSDave Kleikamp */ 41322c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4133ac27a0ecSDave Kleikamp { 4134819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4135819c4920STheodore Ts'o unsigned int credits; 41364209ae12SHarshad Shirwadkar int err = 0, err2; 4137819c4920STheodore Ts'o handle_t *handle; 4138819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4139819c4920STheodore Ts'o 414019b5ef61STheodore Ts'o /* 414119b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4142e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 414319b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 414419b5ef61STheodore Ts'o */ 414519b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41465955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41470562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41480562e0baSJiaying Zhang 414991ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41509a5d265fSzhengliang goto out_trace; 4151ac27a0ecSDave Kleikamp 41525534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 415319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41547d8f9f7dSTheodore Ts'o 4155aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4156aef1c851STao Ma int has_inline = 1; 4157aef1c851STao Ma 415801daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 41599a5d265fSzhengliang if (err || has_inline) 41609a5d265fSzhengliang goto out_trace; 4161aef1c851STao Ma } 4162aef1c851STao Ma 4163a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4164a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4165a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 41669a5d265fSzhengliang goto out_trace; 4167a361293fSJan Kara } 4168a361293fSJan Kara 4169ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4170819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4171ff9893dcSAmir Goldstein else 4172819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4173819c4920STheodore Ts'o 4174819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 41759a5d265fSzhengliang if (IS_ERR(handle)) { 41769a5d265fSzhengliang err = PTR_ERR(handle); 41779a5d265fSzhengliang goto out_trace; 41789a5d265fSzhengliang } 4179819c4920STheodore Ts'o 4180eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4181eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4182819c4920STheodore Ts'o 4183819c4920STheodore Ts'o /* 4184819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4185819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4186819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4187819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4188819c4920STheodore Ts'o * 4189819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4190819c4920STheodore Ts'o * truncatable state while each transaction commits. 4191819c4920STheodore Ts'o */ 41922c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 41932c98eb5eSTheodore Ts'o if (err) 4194819c4920STheodore Ts'o goto out_stop; 4195819c4920STheodore Ts'o 4196819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4197819c4920STheodore Ts'o 419827bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4199819c4920STheodore Ts'o 4200819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4201d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4202819c4920STheodore Ts'o else 4203819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4204819c4920STheodore Ts'o 4205819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4206d0abb36dSTheodore Ts'o if (err) 4207d0abb36dSTheodore Ts'o goto out_stop; 4208819c4920STheodore Ts'o 4209819c4920STheodore Ts'o if (IS_SYNC(inode)) 4210819c4920STheodore Ts'o ext4_handle_sync(handle); 4211819c4920STheodore Ts'o 4212819c4920STheodore Ts'o out_stop: 4213819c4920STheodore Ts'o /* 4214819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4215819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4216819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 421758d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4218819c4920STheodore Ts'o * orphan info for us. 4219819c4920STheodore Ts'o */ 4220819c4920STheodore Ts'o if (inode->i_nlink) 4221819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4222819c4920STheodore Ts'o 4223eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 42244209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 42254209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 42264209ae12SHarshad Shirwadkar err = err2; 4227819c4920STheodore Ts'o ext4_journal_stop(handle); 4228a86c6181SAlex Tomas 42299a5d265fSzhengliang out_trace: 42300562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42312c98eb5eSTheodore Ts'o return err; 4232ac27a0ecSDave Kleikamp } 4233ac27a0ecSDave Kleikamp 4234*9a1bf32cSZhang Yi static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4235*9a1bf32cSZhang Yi { 4236*9a1bf32cSZhang Yi if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4237*9a1bf32cSZhang Yi return inode_peek_iversion_raw(inode); 4238*9a1bf32cSZhang Yi else 4239*9a1bf32cSZhang Yi return inode_peek_iversion(inode); 4240*9a1bf32cSZhang Yi } 4241*9a1bf32cSZhang Yi 4242*9a1bf32cSZhang Yi static int ext4_inode_blocks_set(struct ext4_inode *raw_inode, 4243*9a1bf32cSZhang Yi struct ext4_inode_info *ei) 4244*9a1bf32cSZhang Yi { 4245*9a1bf32cSZhang Yi struct inode *inode = &(ei->vfs_inode); 4246*9a1bf32cSZhang Yi u64 i_blocks = READ_ONCE(inode->i_blocks); 4247*9a1bf32cSZhang Yi struct super_block *sb = inode->i_sb; 4248*9a1bf32cSZhang Yi 4249*9a1bf32cSZhang Yi if (i_blocks <= ~0U) { 4250*9a1bf32cSZhang Yi /* 4251*9a1bf32cSZhang Yi * i_blocks can be represented in a 32 bit variable 4252*9a1bf32cSZhang Yi * as multiple of 512 bytes 4253*9a1bf32cSZhang Yi */ 4254*9a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 4255*9a1bf32cSZhang Yi raw_inode->i_blocks_high = 0; 4256*9a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4257*9a1bf32cSZhang Yi return 0; 4258*9a1bf32cSZhang Yi } 4259*9a1bf32cSZhang Yi 4260*9a1bf32cSZhang Yi /* 4261*9a1bf32cSZhang Yi * This should never happen since sb->s_maxbytes should not have 4262*9a1bf32cSZhang Yi * allowed this, sb->s_maxbytes was set according to the huge_file 4263*9a1bf32cSZhang Yi * feature in ext4_fill_super(). 4264*9a1bf32cSZhang Yi */ 4265*9a1bf32cSZhang Yi if (!ext4_has_feature_huge_file(sb)) 4266*9a1bf32cSZhang Yi return -EFSCORRUPTED; 4267*9a1bf32cSZhang Yi 4268*9a1bf32cSZhang Yi if (i_blocks <= 0xffffffffffffULL) { 4269*9a1bf32cSZhang Yi /* 4270*9a1bf32cSZhang Yi * i_blocks can be represented in a 48 bit variable 4271*9a1bf32cSZhang Yi * as multiple of 512 bytes 4272*9a1bf32cSZhang Yi */ 4273*9a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 4274*9a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 4275*9a1bf32cSZhang Yi ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4276*9a1bf32cSZhang Yi } else { 4277*9a1bf32cSZhang Yi ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4278*9a1bf32cSZhang Yi /* i_block is stored in file system block size */ 4279*9a1bf32cSZhang Yi i_blocks = i_blocks >> (inode->i_blkbits - 9); 4280*9a1bf32cSZhang Yi raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 4281*9a1bf32cSZhang Yi raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 4282*9a1bf32cSZhang Yi } 4283*9a1bf32cSZhang Yi return 0; 4284*9a1bf32cSZhang Yi } 4285*9a1bf32cSZhang Yi 4286*9a1bf32cSZhang Yi static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode) 4287*9a1bf32cSZhang Yi { 4288*9a1bf32cSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 4289*9a1bf32cSZhang Yi uid_t i_uid; 4290*9a1bf32cSZhang Yi gid_t i_gid; 4291*9a1bf32cSZhang Yi projid_t i_projid; 4292*9a1bf32cSZhang Yi int block; 4293*9a1bf32cSZhang Yi int err; 4294*9a1bf32cSZhang Yi 4295*9a1bf32cSZhang Yi err = ext4_inode_blocks_set(raw_inode, ei); 4296*9a1bf32cSZhang Yi 4297*9a1bf32cSZhang Yi raw_inode->i_mode = cpu_to_le16(inode->i_mode); 4298*9a1bf32cSZhang Yi i_uid = i_uid_read(inode); 4299*9a1bf32cSZhang Yi i_gid = i_gid_read(inode); 4300*9a1bf32cSZhang Yi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 4301*9a1bf32cSZhang Yi if (!(test_opt(inode->i_sb, NO_UID32))) { 4302*9a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 4303*9a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4304*9a1bf32cSZhang Yi /* 4305*9a1bf32cSZhang Yi * Fix up interoperability with old kernels. Otherwise, 4306*9a1bf32cSZhang Yi * old inodes get re-used with the upper 16 bits of the 4307*9a1bf32cSZhang Yi * uid/gid intact. 4308*9a1bf32cSZhang Yi */ 4309*9a1bf32cSZhang Yi if (ei->i_dtime && list_empty(&ei->i_orphan)) { 4310*9a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 4311*9a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 4312*9a1bf32cSZhang Yi } else { 4313*9a1bf32cSZhang Yi raw_inode->i_uid_high = 4314*9a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_uid)); 4315*9a1bf32cSZhang Yi raw_inode->i_gid_high = 4316*9a1bf32cSZhang Yi cpu_to_le16(high_16_bits(i_gid)); 4317*9a1bf32cSZhang Yi } 4318*9a1bf32cSZhang Yi } else { 4319*9a1bf32cSZhang Yi raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 4320*9a1bf32cSZhang Yi raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4321*9a1bf32cSZhang Yi raw_inode->i_uid_high = 0; 4322*9a1bf32cSZhang Yi raw_inode->i_gid_high = 0; 4323*9a1bf32cSZhang Yi } 4324*9a1bf32cSZhang Yi raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4325*9a1bf32cSZhang Yi 4326*9a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4327*9a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4328*9a1bf32cSZhang Yi EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4329*9a1bf32cSZhang Yi EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4330*9a1bf32cSZhang Yi 4331*9a1bf32cSZhang Yi raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4332*9a1bf32cSZhang Yi raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 4333*9a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 4334*9a1bf32cSZhang Yi raw_inode->i_file_acl_high = 4335*9a1bf32cSZhang Yi cpu_to_le16(ei->i_file_acl >> 32); 4336*9a1bf32cSZhang Yi raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4337*9a1bf32cSZhang Yi ext4_isize_set(raw_inode, ei->i_disksize); 4338*9a1bf32cSZhang Yi 4339*9a1bf32cSZhang Yi raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4340*9a1bf32cSZhang Yi if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4341*9a1bf32cSZhang Yi if (old_valid_dev(inode->i_rdev)) { 4342*9a1bf32cSZhang Yi raw_inode->i_block[0] = 4343*9a1bf32cSZhang Yi cpu_to_le32(old_encode_dev(inode->i_rdev)); 4344*9a1bf32cSZhang Yi raw_inode->i_block[1] = 0; 4345*9a1bf32cSZhang Yi } else { 4346*9a1bf32cSZhang Yi raw_inode->i_block[0] = 0; 4347*9a1bf32cSZhang Yi raw_inode->i_block[1] = 4348*9a1bf32cSZhang Yi cpu_to_le32(new_encode_dev(inode->i_rdev)); 4349*9a1bf32cSZhang Yi raw_inode->i_block[2] = 0; 4350*9a1bf32cSZhang Yi } 4351*9a1bf32cSZhang Yi } else if (!ext4_has_inline_data(inode)) { 4352*9a1bf32cSZhang Yi for (block = 0; block < EXT4_N_BLOCKS; block++) 4353*9a1bf32cSZhang Yi raw_inode->i_block[block] = ei->i_data[block]; 4354*9a1bf32cSZhang Yi } 4355*9a1bf32cSZhang Yi 4356*9a1bf32cSZhang Yi if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4357*9a1bf32cSZhang Yi u64 ivers = ext4_inode_peek_iversion(inode); 4358*9a1bf32cSZhang Yi 4359*9a1bf32cSZhang Yi raw_inode->i_disk_version = cpu_to_le32(ivers); 4360*9a1bf32cSZhang Yi if (ei->i_extra_isize) { 4361*9a1bf32cSZhang Yi if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4362*9a1bf32cSZhang Yi raw_inode->i_version_hi = 4363*9a1bf32cSZhang Yi cpu_to_le32(ivers >> 32); 4364*9a1bf32cSZhang Yi raw_inode->i_extra_isize = 4365*9a1bf32cSZhang Yi cpu_to_le16(ei->i_extra_isize); 4366*9a1bf32cSZhang Yi } 4367*9a1bf32cSZhang Yi } 4368*9a1bf32cSZhang Yi 4369*9a1bf32cSZhang Yi if (i_projid != EXT4_DEF_PROJID && 4370*9a1bf32cSZhang Yi !ext4_has_feature_project(inode->i_sb)) 4371*9a1bf32cSZhang Yi err = err ?: -EFSCORRUPTED; 4372*9a1bf32cSZhang Yi 4373*9a1bf32cSZhang Yi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 4374*9a1bf32cSZhang Yi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4375*9a1bf32cSZhang Yi raw_inode->i_projid = cpu_to_le32(i_projid); 4376*9a1bf32cSZhang Yi 4377*9a1bf32cSZhang Yi ext4_inode_csum_set(inode, raw_inode, ei); 4378*9a1bf32cSZhang Yi return err; 4379*9a1bf32cSZhang Yi } 4380*9a1bf32cSZhang Yi 4381ac27a0ecSDave Kleikamp /* 4382617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4383ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4384ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4385ac27a0ecSDave Kleikamp * inode. 4386ac27a0ecSDave Kleikamp */ 43878016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 43888016e29fSHarshad Shirwadkar struct ext4_iloc *iloc, int in_mem, 43898016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4390ac27a0ecSDave Kleikamp { 4391240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4392ac27a0ecSDave Kleikamp struct buffer_head *bh; 4393240799cdSTheodore Ts'o ext4_fsblk_t block; 439402f03c42SLinus Torvalds struct blk_plug plug; 4395240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4396ac27a0ecSDave Kleikamp 43973a06d778SAneesh Kumar K.V iloc->bh = NULL; 43988016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 43998016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 44006a797d27SDarrick J. Wong return -EFSCORRUPTED; 4401ac27a0ecSDave Kleikamp 44028016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4403240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4404240799cdSTheodore Ts'o if (!gdp) 4405240799cdSTheodore Ts'o return -EIO; 4406240799cdSTheodore Ts'o 4407240799cdSTheodore Ts'o /* 4408240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4409240799cdSTheodore Ts'o */ 441000d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 44118016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4412240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4413240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4414240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4415240799cdSTheodore Ts'o 4416240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4417aebf0243SWang Shilong if (unlikely(!bh)) 4418860d21e2STheodore Ts'o return -ENOMEM; 44198e33fadfSZhang Yi if (ext4_buffer_uptodate(bh)) 4420ac27a0ecSDave Kleikamp goto has_buffer; 4421ac27a0ecSDave Kleikamp 44228e33fadfSZhang Yi lock_buffer(bh); 4423f2c77973SZhang Yi if (ext4_buffer_uptodate(bh)) { 4424f2c77973SZhang Yi /* Someone brought it uptodate while we waited */ 4425f2c77973SZhang Yi unlock_buffer(bh); 4426f2c77973SZhang Yi goto has_buffer; 4427f2c77973SZhang Yi } 4428f2c77973SZhang Yi 4429ac27a0ecSDave Kleikamp /* 4430ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4431ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4432ac27a0ecSDave Kleikamp * block. 4433ac27a0ecSDave Kleikamp */ 4434ac27a0ecSDave Kleikamp if (in_mem) { 4435ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4436240799cdSTheodore Ts'o int i, start; 4437ac27a0ecSDave Kleikamp 4438240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4439ac27a0ecSDave Kleikamp 4440ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4441240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4442aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4443ac27a0ecSDave Kleikamp goto make_io; 4444ac27a0ecSDave Kleikamp 4445ac27a0ecSDave Kleikamp /* 4446ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4447ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4448ac27a0ecSDave Kleikamp * of one, so skip it. 4449ac27a0ecSDave Kleikamp */ 4450ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4451ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4452ac27a0ecSDave Kleikamp goto make_io; 4453ac27a0ecSDave Kleikamp } 4454240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4455ac27a0ecSDave Kleikamp if (i == inode_offset) 4456ac27a0ecSDave Kleikamp continue; 4457617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4458ac27a0ecSDave Kleikamp break; 4459ac27a0ecSDave Kleikamp } 4460ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4461240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4462ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4463ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4464ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4465ac27a0ecSDave Kleikamp unlock_buffer(bh); 4466ac27a0ecSDave Kleikamp goto has_buffer; 4467ac27a0ecSDave Kleikamp } 4468ac27a0ecSDave Kleikamp } 4469ac27a0ecSDave Kleikamp 4470ac27a0ecSDave Kleikamp make_io: 4471ac27a0ecSDave Kleikamp /* 4472240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4473240799cdSTheodore Ts'o * blocks from the inode table. 4474240799cdSTheodore Ts'o */ 447502f03c42SLinus Torvalds blk_start_plug(&plug); 4476240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4477240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4478240799cdSTheodore Ts'o unsigned num; 44790d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4480240799cdSTheodore Ts'o 4481240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4482b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 44830d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4484240799cdSTheodore Ts'o if (table > b) 4485240799cdSTheodore Ts'o b = table; 44860d606e2cSTheodore Ts'o end = b + ra_blks; 4487240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4488feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4489560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4490240799cdSTheodore Ts'o table += num / inodes_per_block; 4491240799cdSTheodore Ts'o if (end > table) 4492240799cdSTheodore Ts'o end = table; 4493240799cdSTheodore Ts'o while (b <= end) 44945df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4495240799cdSTheodore Ts'o } 4496240799cdSTheodore Ts'o 4497240799cdSTheodore Ts'o /* 4498ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4499ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4500ac27a0ecSDave Kleikamp * Read the block from disk. 4501ac27a0ecSDave Kleikamp */ 45028016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 45032d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 450402f03c42SLinus Torvalds blk_finish_plug(&plug); 4505ac27a0ecSDave Kleikamp wait_on_buffer(bh); 45060904c9aeSZhang Yi ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4507ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 45088016e29fSHarshad Shirwadkar if (ret_block) 45098016e29fSHarshad Shirwadkar *ret_block = block; 4510ac27a0ecSDave Kleikamp brelse(bh); 4511ac27a0ecSDave Kleikamp return -EIO; 4512ac27a0ecSDave Kleikamp } 4513ac27a0ecSDave Kleikamp has_buffer: 4514ac27a0ecSDave Kleikamp iloc->bh = bh; 4515ac27a0ecSDave Kleikamp return 0; 4516ac27a0ecSDave Kleikamp } 4517ac27a0ecSDave Kleikamp 45188016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 45198016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45208016e29fSHarshad Shirwadkar { 45218016e29fSHarshad Shirwadkar ext4_fsblk_t err_blk; 45228016e29fSHarshad Shirwadkar int ret; 45238016e29fSHarshad Shirwadkar 45248016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 0, 45258016e29fSHarshad Shirwadkar &err_blk); 45268016e29fSHarshad Shirwadkar 45278016e29fSHarshad Shirwadkar if (ret == -EIO) 45288016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 45298016e29fSHarshad Shirwadkar "unable to read itable block"); 45308016e29fSHarshad Shirwadkar 45318016e29fSHarshad Shirwadkar return ret; 45328016e29fSHarshad Shirwadkar } 45338016e29fSHarshad Shirwadkar 4534617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4535ac27a0ecSDave Kleikamp { 45368016e29fSHarshad Shirwadkar ext4_fsblk_t err_blk; 45378016e29fSHarshad Shirwadkar int ret; 45388016e29fSHarshad Shirwadkar 4539ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 45408016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 45418016e29fSHarshad Shirwadkar !ext4_test_inode_state(inode, EXT4_STATE_XATTR), &err_blk); 45428016e29fSHarshad Shirwadkar 45438016e29fSHarshad Shirwadkar if (ret == -EIO) 45448016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 45458016e29fSHarshad Shirwadkar "unable to read itable block"); 45468016e29fSHarshad Shirwadkar 45478016e29fSHarshad Shirwadkar return ret; 45488016e29fSHarshad Shirwadkar } 45498016e29fSHarshad Shirwadkar 45508016e29fSHarshad Shirwadkar 45518016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 45528016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 45538016e29fSHarshad Shirwadkar { 45548016e29fSHarshad Shirwadkar return __ext4_get_inode_loc(sb, ino, iloc, 0, NULL); 4555ac27a0ecSDave Kleikamp } 4556ac27a0ecSDave Kleikamp 4557a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 45586642586bSRoss Zwisler { 4559a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4560a8ab6d38SIra Weiny 45619cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 45626642586bSRoss Zwisler return false; 45636642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 45646642586bSRoss Zwisler return false; 45656642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 45666642586bSRoss Zwisler return false; 45676642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 45686642586bSRoss Zwisler return false; 4569592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 45706642586bSRoss Zwisler return false; 4571c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4572c93d8f88SEric Biggers return false; 4573a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4574a8ab6d38SIra Weiny return false; 4575a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 45766642586bSRoss Zwisler return true; 4577a8ab6d38SIra Weiny 4578b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 45796642586bSRoss Zwisler } 45806642586bSRoss Zwisler 4581043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4582ac27a0ecSDave Kleikamp { 4583617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 458400a1a053STheodore Ts'o unsigned int new_fl = 0; 4585ac27a0ecSDave Kleikamp 4586043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4587043546e4SIra Weiny 4588617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 458900a1a053STheodore Ts'o new_fl |= S_SYNC; 4590617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 459100a1a053STheodore Ts'o new_fl |= S_APPEND; 4592617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 459300a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4594617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 459500a1a053STheodore Ts'o new_fl |= S_NOATIME; 4596617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 459700a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4598043546e4SIra Weiny 4599043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4600043546e4SIra Weiny * here if already set. */ 4601043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4602043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4603923ae0ffSRoss Zwisler new_fl |= S_DAX; 4604043546e4SIra Weiny 46052ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 46062ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4607b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4608b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4609c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4610c93d8f88SEric Biggers new_fl |= S_VERITY; 46115f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 46122ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4613c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4614ac27a0ecSDave Kleikamp } 4615ac27a0ecSDave Kleikamp 46160fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 46170fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 46180fc1b451SAneesh Kumar K.V { 46190fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 46208180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 46218180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 46220fc1b451SAneesh Kumar K.V 4623e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 46240fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 46250fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 46260fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 462707a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 46288180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 46298180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 46308180a562SAneesh Kumar K.V } else { 46310fc1b451SAneesh Kumar K.V return i_blocks; 46328180a562SAneesh Kumar K.V } 46330fc1b451SAneesh Kumar K.V } else { 46340fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 46350fc1b451SAneesh Kumar K.V } 46360fc1b451SAneesh Kumar K.V } 4637ff9ddf7eSJan Kara 4638eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4639152a7b0aSTao Ma struct ext4_inode *raw_inode, 4640152a7b0aSTao Ma struct ext4_inode_info *ei) 4641152a7b0aSTao Ma { 4642152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4643152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4644eb9b5f01STheodore Ts'o 4645290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4646290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4647290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4648152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4649eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4650f19d5870STao Ma } else 4651f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4652eb9b5f01STheodore Ts'o return 0; 4653152a7b0aSTao Ma } 4654152a7b0aSTao Ma 4655040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4656040cb378SLi Xi { 46570b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4658040cb378SLi Xi return -EOPNOTSUPP; 4659040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4660040cb378SLi Xi return 0; 4661040cb378SLi Xi } 4662040cb378SLi Xi 4663e254d1afSEryu Guan /* 4664e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4665e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4666e254d1afSEryu Guan * set. 4667e254d1afSEryu Guan */ 4668e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4669e254d1afSEryu Guan { 4670e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4671e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4672e254d1afSEryu Guan else 4673e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4674e254d1afSEryu Guan } 4675e254d1afSEryu Guan 46768a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 46778a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 46788a363970STheodore Ts'o unsigned int line) 4679ac27a0ecSDave Kleikamp { 4680617ba13bSMingming Cao struct ext4_iloc iloc; 4681617ba13bSMingming Cao struct ext4_inode *raw_inode; 46821d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4683bd2c38cfSJan Kara struct ext4_super_block *es = EXT4_SB(sb)->s_es; 46841d1fe1eeSDavid Howells struct inode *inode; 4685b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 46861d1fe1eeSDavid Howells long ret; 46877e6e1ef4SDarrick J. Wong loff_t size; 4688ac27a0ecSDave Kleikamp int block; 468908cefc7aSEric W. Biederman uid_t i_uid; 469008cefc7aSEric W. Biederman gid_t i_gid; 4691040cb378SLi Xi projid_t i_projid; 4692ac27a0ecSDave Kleikamp 4693191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 4694bd2c38cfSJan Kara ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4695bd2c38cfSJan Kara ino == le32_to_cpu(es->s_usr_quota_inum) || 4696bd2c38cfSJan Kara ino == le32_to_cpu(es->s_grp_quota_inum) || 469702f310fcSJan Kara ino == le32_to_cpu(es->s_prj_quota_inum) || 469802f310fcSJan Kara ino == le32_to_cpu(es->s_orphan_file_inum))) || 46998a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 4700bd2c38cfSJan Kara (ino > le32_to_cpu(es->s_inodes_count))) { 47018a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 47028a363970STheodore Ts'o return ERR_PTR(-ESTALE); 4703014c9caaSJan Kara __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, 47048a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 47058a363970STheodore Ts'o ino, current->comm); 47068a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 47078a363970STheodore Ts'o } 47088a363970STheodore Ts'o 47091d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 47101d1fe1eeSDavid Howells if (!inode) 47111d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 47121d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 47131d1fe1eeSDavid Howells return inode; 47141d1fe1eeSDavid Howells 47151d1fe1eeSDavid Howells ei = EXT4_I(inode); 47167dc57615SPeter Huewe iloc.bh = NULL; 4717ac27a0ecSDave Kleikamp 47188016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 47191d1fe1eeSDavid Howells if (ret < 0) 4720ac27a0ecSDave Kleikamp goto bad_inode; 4721617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4722814525f4SDarrick J. Wong 47238e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 47248a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47258a363970STheodore Ts'o "iget: root inode unallocated"); 47268e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 47278e4b5eaeSTheodore Ts'o goto bad_inode; 47288e4b5eaeSTheodore Ts'o } 47298e4b5eaeSTheodore Ts'o 47308a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 47318a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 47328a363970STheodore Ts'o ret = -ESTALE; 47338a363970STheodore Ts'o goto bad_inode; 47348a363970STheodore Ts'o } 47358a363970STheodore Ts'o 4736814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4737814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4738814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 47392dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 47402dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 47418a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47428a363970STheodore Ts'o "iget: bad extra_isize %u " 47438a363970STheodore Ts'o "(inode size %u)", 47442dc8d9e1SEric Biggers ei->i_extra_isize, 4745814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 47466a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4747814525f4SDarrick J. Wong goto bad_inode; 4748814525f4SDarrick J. Wong } 4749814525f4SDarrick J. Wong } else 4750814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4751814525f4SDarrick J. Wong 4752814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 47539aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4754814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4755814525f4SDarrick J. Wong __u32 csum; 4756814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4757814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4758814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4759814525f4SDarrick J. Wong sizeof(inum)); 4760814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4761814525f4SDarrick J. Wong sizeof(gen)); 4762814525f4SDarrick J. Wong } 4763814525f4SDarrick J. Wong 47648016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 47658016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 47668016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 47678016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 47688016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 47696a797d27SDarrick J. Wong ret = -EFSBADCRC; 4770814525f4SDarrick J. Wong goto bad_inode; 4771814525f4SDarrick J. Wong } 4772814525f4SDarrick J. Wong 4773ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 477408cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 477508cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 47760b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4777040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4778040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4779040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4780040cb378SLi Xi else 4781040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4782040cb378SLi Xi 4783ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 478408cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 478508cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4786ac27a0ecSDave Kleikamp } 478708cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 478808cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4789040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4790bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4791ac27a0ecSDave Kleikamp 4792353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 479367cf5b09STao Ma ei->i_inline_off = 0; 4794ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4795ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4796ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4797ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4798ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4799ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4800ac27a0ecSDave Kleikamp */ 4801ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4802393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4803393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4804393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4805ac27a0ecSDave Kleikamp /* this inode is deleted */ 48061d1fe1eeSDavid Howells ret = -ESTALE; 4807ac27a0ecSDave Kleikamp goto bad_inode; 4808ac27a0ecSDave Kleikamp } 4809ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4810ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4811ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4812393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4813393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4814393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4815ac27a0ecSDave Kleikamp } 4816ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4817043546e4SIra Weiny ext4_set_inode_flags(inode, true); 48180fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 48197973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4820e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4821a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4822a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4823e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 48247e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 48258a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48268a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 48277e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 48287e6e1ef4SDarrick J. Wong goto bad_inode; 48297e6e1ef4SDarrick J. Wong } 483048a34311SJan Kara /* 483148a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 483248a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 483348a34311SJan Kara * checksumming that corrupts checksums so forbid that. 483448a34311SJan Kara */ 483548a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 483648a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 483748a34311SJan Kara ext4_error_inode(inode, function, line, 0, 483848a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 483948a34311SJan Kara ret = -EFSCORRUPTED; 484048a34311SJan Kara goto bad_inode; 484148a34311SJan Kara } 4842ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4843a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4844a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4845a9e7f447SDmitry Monakhov #endif 4846ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4847ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4848a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4849ac27a0ecSDave Kleikamp /* 4850ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4851ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4852ac27a0ecSDave Kleikamp */ 4853617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4854ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4855ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4856aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4857ac27a0ecSDave Kleikamp 4858b436b9beSJan Kara /* 4859b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4860b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4861b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4862b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4863b436b9beSJan Kara * now it is reread from disk. 4864b436b9beSJan Kara */ 4865b436b9beSJan Kara if (journal) { 4866b436b9beSJan Kara transaction_t *transaction; 4867b436b9beSJan Kara tid_t tid; 4868b436b9beSJan Kara 4869a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4870b436b9beSJan Kara if (journal->j_running_transaction) 4871b436b9beSJan Kara transaction = journal->j_running_transaction; 4872b436b9beSJan Kara else 4873b436b9beSJan Kara transaction = journal->j_committing_transaction; 4874b436b9beSJan Kara if (transaction) 4875b436b9beSJan Kara tid = transaction->t_tid; 4876b436b9beSJan Kara else 4877b436b9beSJan Kara tid = journal->j_commit_sequence; 4878a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4879b436b9beSJan Kara ei->i_sync_tid = tid; 4880b436b9beSJan Kara ei->i_datasync_tid = tid; 4881b436b9beSJan Kara } 4882b436b9beSJan Kara 48830040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4884ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4885ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 48862dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4887617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4888617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4889ac27a0ecSDave Kleikamp } else { 4890eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4891eb9b5f01STheodore Ts'o if (ret) 4892eb9b5f01STheodore Ts'o goto bad_inode; 4893ac27a0ecSDave Kleikamp } 4894814525f4SDarrick J. Wong } 4895ac27a0ecSDave Kleikamp 4896ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4897ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4898ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4899ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4900ef7f3835SKalpak Shah 4901ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4902ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4903ee73f9a5SJeff Layton 490425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 490525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4906ee73f9a5SJeff Layton ivers |= 490725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 490825ec56b5SJean Noel Cordenner } 4909e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4910c4f65706STheodore Ts'o } 491125ec56b5SJean Noel Cordenner 4912c4b5a614STheodore Ts'o ret = 0; 4913485c26ecSTheodore Ts'o if (ei->i_file_acl && 4914ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 49158a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49168a363970STheodore Ts'o "iget: bad extended attribute block %llu", 491724676da4STheodore Ts'o ei->i_file_acl); 49186a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4919485c26ecSTheodore Ts'o goto bad_inode; 4920f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4921bc716523SLiu Song /* validate the block references in the inode */ 49228016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 49238016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4924fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 49258016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 4926bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4927bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4928bc716523SLiu Song else 49291f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4930fe2c8191SThiemo Nagel } 4931f19d5870STao Ma } 4932567f3e9aSTheodore Ts'o if (ret) 49337a262f7cSAneesh Kumar K.V goto bad_inode; 49347a262f7cSAneesh Kumar K.V 4935ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4936617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4937617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4938617ba13bSMingming Cao ext4_set_aops(inode); 4939ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4940617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4941617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4942ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 49436390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 49446390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 49458a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49468a363970STheodore Ts'o "iget: immutable or append flags " 49478a363970STheodore Ts'o "not allowed on symlinks"); 49486390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 49496390d33bSLuis R. Rodriguez goto bad_inode; 49506390d33bSLuis R. Rodriguez } 4951592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4952a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4953a7a67e8aSAl Viro ext4_set_aops(inode); 4954a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 495575e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4956617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4957e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4958e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4959e83c1397SDuane Griffin } else { 4960617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4961617ba13bSMingming Cao ext4_set_aops(inode); 4962ac27a0ecSDave Kleikamp } 496321fc61c7SAl Viro inode_nohighmem(inode); 4964563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4965563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4966617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4967ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4968ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4969ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4970ac27a0ecSDave Kleikamp else 4971ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4972ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4973393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4974393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4975563bdd61STheodore Ts'o } else { 49766a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 49778a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49788a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4979563bdd61STheodore Ts'o goto bad_inode; 4980ac27a0ecSDave Kleikamp } 49816456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 49826456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 49836456ca65STheodore Ts'o "casefold flag without casefold feature"); 4984ac27a0ecSDave Kleikamp brelse(iloc.bh); 4985dec214d0STahsin Erdogan 49861d1fe1eeSDavid Howells unlock_new_inode(inode); 49871d1fe1eeSDavid Howells return inode; 4988ac27a0ecSDave Kleikamp 4989ac27a0ecSDave Kleikamp bad_inode: 4990567f3e9aSTheodore Ts'o brelse(iloc.bh); 49911d1fe1eeSDavid Howells iget_failed(inode); 49921d1fe1eeSDavid Howells return ERR_PTR(ret); 4993ac27a0ecSDave Kleikamp } 4994ac27a0ecSDave Kleikamp 49953f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 49963f19b2abSDavid Howells unsigned long orig_ino, 49973f19b2abSDavid Howells unsigned long ino, 49983f19b2abSDavid Howells struct ext4_inode *raw_inode) 4999a26f4992STheodore Ts'o { 50003f19b2abSDavid Howells struct inode *inode; 5001a26f4992STheodore Ts'o 50023f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 50033f19b2abSDavid Howells if (!inode) 50043f19b2abSDavid Howells return; 50053f19b2abSDavid Howells 5006ed296c6cSEric Biggers if (!inode_is_dirtytime_only(inode)) 50073f19b2abSDavid Howells return; 50083f19b2abSDavid Howells 5009a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 5010ed296c6cSEric Biggers if (inode_is_dirtytime_only(inode)) { 5011a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5012a26f4992STheodore Ts'o 50135fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 5014a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5015a26f4992STheodore Ts'o 5016a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 50173f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 50183f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 50193f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 50203f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 5021a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 50223f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 50233f19b2abSDavid Howells return; 5024a26f4992STheodore Ts'o } 5025a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 5026a26f4992STheodore Ts'o } 5027a26f4992STheodore Ts'o 5028a26f4992STheodore Ts'o /* 5029a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 5030a26f4992STheodore Ts'o * the same inode table block. 5031a26f4992STheodore Ts'o */ 5032a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 5033a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 5034a26f4992STheodore Ts'o { 5035a26f4992STheodore Ts'o unsigned long ino; 5036a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 5037a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 5038a26f4992STheodore Ts'o 50390f0ff9a9STheodore Ts'o /* 50400f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 50410f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 50420f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 50430f0ff9a9STheodore Ts'o */ 50440f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 50453f19b2abSDavid Howells rcu_read_lock(); 5046a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5047a26f4992STheodore Ts'o if (ino == orig_ino) 5048a26f4992STheodore Ts'o continue; 50493f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 50503f19b2abSDavid Howells (struct ext4_inode *)buf); 5051a26f4992STheodore Ts'o } 50523f19b2abSDavid Howells rcu_read_unlock(); 5053a26f4992STheodore Ts'o } 5054a26f4992STheodore Ts'o 5055664bd38bSZhang Yi /* 5056664bd38bSZhang Yi * Post the struct inode info into an on-disk inode location in the 5057664bd38bSZhang Yi * buffer-cache. This gobbles the caller's reference to the 5058664bd38bSZhang Yi * buffer_head in the inode location struct. 5059664bd38bSZhang Yi * 5060664bd38bSZhang Yi * The caller must have write access to iloc->bh. 5061664bd38bSZhang Yi */ 5062664bd38bSZhang Yi static int ext4_do_update_inode(handle_t *handle, 5063664bd38bSZhang Yi struct inode *inode, 5064664bd38bSZhang Yi struct ext4_iloc *iloc) 5065664bd38bSZhang Yi { 5066664bd38bSZhang Yi struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5067664bd38bSZhang Yi struct ext4_inode_info *ei = EXT4_I(inode); 5068664bd38bSZhang Yi struct buffer_head *bh = iloc->bh; 5069664bd38bSZhang Yi struct super_block *sb = inode->i_sb; 5070664bd38bSZhang Yi int err; 5071664bd38bSZhang Yi int need_datasync = 0, set_large_file = 0; 5072664bd38bSZhang Yi 5073664bd38bSZhang Yi spin_lock(&ei->i_raw_lock); 5074664bd38bSZhang Yi 5075664bd38bSZhang Yi /* 5076664bd38bSZhang Yi * For fields not tracked in the in-memory inode, initialise them 5077664bd38bSZhang Yi * to zero for new inodes. 5078664bd38bSZhang Yi */ 5079664bd38bSZhang Yi if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5080664bd38bSZhang Yi memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5081664bd38bSZhang Yi 5082664bd38bSZhang Yi if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) 5083664bd38bSZhang Yi need_datasync = 1; 5084664bd38bSZhang Yi if (ei->i_disksize > 0x7fffffffULL) { 5085664bd38bSZhang Yi if (!ext4_has_feature_large_file(sb) || 5086664bd38bSZhang Yi EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV)) 5087664bd38bSZhang Yi set_large_file = 1; 5088664bd38bSZhang Yi } 5089664bd38bSZhang Yi 5090664bd38bSZhang Yi err = ext4_fill_raw_inode(inode, raw_inode); 5091202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 5092baaae979SZhang Yi if (err) { 5093baaae979SZhang Yi EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5094baaae979SZhang Yi goto out_brelse; 5095baaae979SZhang Yi } 5096baaae979SZhang Yi 50971751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5098a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5099a26f4992STheodore Ts'o bh->b_data); 5100202ee5dfSTheodore Ts'o 51010390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 51027d8bd3c7SShijie Luo err = ext4_handle_dirty_metadata(handle, NULL, bh); 51037d8bd3c7SShijie Luo if (err) 5104baaae979SZhang Yi goto out_error; 510519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5106202ee5dfSTheodore Ts'o if (set_large_file) { 51075d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5108188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, 5109188c299eSJan Kara EXT4_SB(sb)->s_sbh, 5110188c299eSJan Kara EXT4_JTR_NONE); 5111202ee5dfSTheodore Ts'o if (err) 5112baaae979SZhang Yi goto out_error; 511305c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 5114e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 511505c2c00fSJan Kara ext4_superblock_csum_set(sb); 511605c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 5117202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5118a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, 5119a3f5cf14SJan Kara EXT4_SB(sb)->s_sbh); 5120202ee5dfSTheodore Ts'o } 5121b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5122baaae979SZhang Yi out_error: 5123baaae979SZhang Yi ext4_std_error(inode->i_sb, err); 5124ac27a0ecSDave Kleikamp out_brelse: 5125ac27a0ecSDave Kleikamp brelse(bh); 5126ac27a0ecSDave Kleikamp return err; 5127ac27a0ecSDave Kleikamp } 5128ac27a0ecSDave Kleikamp 5129ac27a0ecSDave Kleikamp /* 5130617ba13bSMingming Cao * ext4_write_inode() 5131ac27a0ecSDave Kleikamp * 5132ac27a0ecSDave Kleikamp * We are called from a few places: 5133ac27a0ecSDave Kleikamp * 513487f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5135ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51364907cb7bSAnatol Pomozov * transaction to commit. 5137ac27a0ecSDave Kleikamp * 513887f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 513987f7e416STheodore Ts'o * We wait on commit, if told to. 5140ac27a0ecSDave Kleikamp * 514187f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 514287f7e416STheodore Ts'o * We wait on commit, if told to. 5143ac27a0ecSDave Kleikamp * 5144ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5145ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 514687f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 514787f7e416STheodore Ts'o * writeback. 5148ac27a0ecSDave Kleikamp * 5149ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5150ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5151ac27a0ecSDave Kleikamp * which we are interested. 5152ac27a0ecSDave Kleikamp * 5153ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5154ac27a0ecSDave Kleikamp * 5155ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5156ac27a0ecSDave Kleikamp * stuff(); 5157ac27a0ecSDave Kleikamp * inode->i_size = expr; 5158ac27a0ecSDave Kleikamp * 515987f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 516087f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 516187f7e416STheodore Ts'o * superblock's dirty inode list. 5162ac27a0ecSDave Kleikamp */ 5163a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5164ac27a0ecSDave Kleikamp { 516591ac6f43SFrank Mayhar int err; 516691ac6f43SFrank Mayhar 516718f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 516818f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5169ac27a0ecSDave Kleikamp return 0; 5170ac27a0ecSDave Kleikamp 517118f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 517218f2c4fcSTheodore Ts'o return -EIO; 517318f2c4fcSTheodore Ts'o 517491ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5175617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5176b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5177ac27a0ecSDave Kleikamp dump_stack(); 5178ac27a0ecSDave Kleikamp return -EIO; 5179ac27a0ecSDave Kleikamp } 5180ac27a0ecSDave Kleikamp 518110542c22SJan Kara /* 518210542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 518310542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 518410542c22SJan Kara * written. 518510542c22SJan Kara */ 518610542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5187ac27a0ecSDave Kleikamp return 0; 5188ac27a0ecSDave Kleikamp 5189aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 519018f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 519191ac6f43SFrank Mayhar } else { 519291ac6f43SFrank Mayhar struct ext4_iloc iloc; 519391ac6f43SFrank Mayhar 51948016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 519591ac6f43SFrank Mayhar if (err) 519691ac6f43SFrank Mayhar return err; 519710542c22SJan Kara /* 519810542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 519910542c22SJan Kara * it here separately for each inode. 520010542c22SJan Kara */ 520110542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5202830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5203830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 520454d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5205c398eda0STheodore Ts'o "IO error syncing inode"); 5206830156c7SFrank Mayhar err = -EIO; 5207830156c7SFrank Mayhar } 5208fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 520991ac6f43SFrank Mayhar } 521091ac6f43SFrank Mayhar return err; 5211ac27a0ecSDave Kleikamp } 5212ac27a0ecSDave Kleikamp 5213ac27a0ecSDave Kleikamp /* 521453e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 521553e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 521653e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 521753e87268SJan Kara */ 521853e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 521953e87268SJan Kara { 522053e87268SJan Kara struct page *page; 522153e87268SJan Kara unsigned offset; 522253e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 522353e87268SJan Kara tid_t commit_tid = 0; 522453e87268SJan Kara int ret; 522553e87268SJan Kara 522609cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 522753e87268SJan Kara /* 5228565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5229565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5230565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5231565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5232565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5233565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5234565333a1Syangerkun * blocksize == PAGESIZE. 523553e87268SJan Kara */ 5236565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 523753e87268SJan Kara return; 523853e87268SJan Kara while (1) { 523953e87268SJan Kara page = find_lock_page(inode->i_mapping, 524009cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 524153e87268SJan Kara if (!page) 524253e87268SJan Kara return; 5243ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 524409cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 524553e87268SJan Kara unlock_page(page); 524609cbfeafSKirill A. Shutemov put_page(page); 524753e87268SJan Kara if (ret != -EBUSY) 524853e87268SJan Kara return; 524953e87268SJan Kara commit_tid = 0; 525053e87268SJan Kara read_lock(&journal->j_state_lock); 525153e87268SJan Kara if (journal->j_committing_transaction) 525253e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 525353e87268SJan Kara read_unlock(&journal->j_state_lock); 525453e87268SJan Kara if (commit_tid) 525553e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 525653e87268SJan Kara } 525753e87268SJan Kara } 525853e87268SJan Kara 525953e87268SJan Kara /* 5260617ba13bSMingming Cao * ext4_setattr() 5261ac27a0ecSDave Kleikamp * 5262ac27a0ecSDave Kleikamp * Called from notify_change. 5263ac27a0ecSDave Kleikamp * 5264ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5265ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5266ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5267ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5268ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5269ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5270ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5271ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5272ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5273ac27a0ecSDave Kleikamp * 5274678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5275678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5276678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5277678aaf48SJan Kara * This way we are sure that all the data written in the previous 5278678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5279678aaf48SJan Kara * writeback). 5280678aaf48SJan Kara * 5281678aaf48SJan Kara * Called with inode->i_mutex down. 5282ac27a0ecSDave Kleikamp */ 5283549c7297SChristian Brauner int ext4_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 5284549c7297SChristian Brauner struct iattr *attr) 5285ac27a0ecSDave Kleikamp { 52862b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5287ac27a0ecSDave Kleikamp int error, rc = 0; 52883d287de3SDmitry Monakhov int orphan = 0; 5289ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5290ac27a0ecSDave Kleikamp 52910db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 52920db1ff22STheodore Ts'o return -EIO; 52930db1ff22STheodore Ts'o 529402b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 529502b016caSTheodore Ts'o return -EPERM; 529602b016caSTheodore Ts'o 529702b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 529802b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 529902b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 530002b016caSTheodore Ts'o return -EPERM; 530102b016caSTheodore Ts'o 530214f3db55SChristian Brauner error = setattr_prepare(mnt_userns, dentry, attr); 5303ac27a0ecSDave Kleikamp if (error) 5304ac27a0ecSDave Kleikamp return error; 5305ac27a0ecSDave Kleikamp 53063ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 53073ce2b8ddSEric Biggers if (error) 53083ce2b8ddSEric Biggers return error; 53093ce2b8ddSEric Biggers 5310c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5311c93d8f88SEric Biggers if (error) 5312c93d8f88SEric Biggers return error; 5313c93d8f88SEric Biggers 5314a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5315a7cdadeeSJan Kara error = dquot_initialize(inode); 5316a7cdadeeSJan Kara if (error) 5317a7cdadeeSJan Kara return error; 5318a7cdadeeSJan Kara } 5319aa75f4d3SHarshad Shirwadkar ext4_fc_start_update(inode); 532008cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 532108cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5322ac27a0ecSDave Kleikamp handle_t *handle; 5323ac27a0ecSDave Kleikamp 5324ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5325ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53269924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53279924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5328194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5329ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5330ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5331ac27a0ecSDave Kleikamp goto err_out; 5332ac27a0ecSDave Kleikamp } 53337a9ca53aSTahsin Erdogan 53347a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53357a9ca53aSTahsin Erdogan * counts xattr inode references. 53367a9ca53aSTahsin Erdogan */ 53377a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5338b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 53397a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53407a9ca53aSTahsin Erdogan 5341ac27a0ecSDave Kleikamp if (error) { 5342617ba13bSMingming Cao ext4_journal_stop(handle); 5343aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 5344ac27a0ecSDave Kleikamp return error; 5345ac27a0ecSDave Kleikamp } 5346ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5347ac27a0ecSDave Kleikamp * one transaction */ 5348ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5349ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5350ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5351ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5352617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5353617ba13bSMingming Cao ext4_journal_stop(handle); 5354512c15efSPan Bian if (unlikely(error)) { 5355512c15efSPan Bian ext4_fc_stop_update(inode); 53564209ae12SHarshad Shirwadkar return error; 5357ac27a0ecSDave Kleikamp } 5358512c15efSPan Bian } 5359ac27a0ecSDave Kleikamp 53603da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53615208386cSJan Kara handle_t *handle; 53623da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5363b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5364562c72aaSChristoph Hellwig 536512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5366e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5367e2b46574SEric Sandeen 5368aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5369aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 53700c095c7fSTheodore Ts'o return -EFBIG; 5371e2b46574SEric Sandeen } 5372aa75f4d3SHarshad Shirwadkar } 5373aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 5374aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 53753da40c7bSJosef Bacik return -EINVAL; 5376aa75f4d3SHarshad Shirwadkar } 5377dff6efc3SChristoph Hellwig 5378dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5379dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5380dff6efc3SChristoph Hellwig 5381b9c1c267SJan Kara if (shrink) { 5382b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 53835208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 53845208386cSJan Kara attr->ia_size); 53855208386cSJan Kara if (error) 53865208386cSJan Kara goto err_out; 53875208386cSJan Kara } 5388b9c1c267SJan Kara /* 5389b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5390b9c1c267SJan Kara * for dio in flight. 5391b9c1c267SJan Kara */ 5392b9c1c267SJan Kara inode_dio_wait(inode); 5393b9c1c267SJan Kara } 5394b9c1c267SJan Kara 5395d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 5396b9c1c267SJan Kara 5397b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5398b9c1c267SJan Kara if (rc) { 5399d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 5400aa75f4d3SHarshad Shirwadkar goto err_out; 5401b9c1c267SJan Kara } 5402b9c1c267SJan Kara 54033da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 54049924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5405ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5406ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5407b9c1c267SJan Kara goto out_mmap_sem; 5408ac27a0ecSDave Kleikamp } 54093da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5410617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 54113d287de3SDmitry Monakhov orphan = 1; 54123d287de3SDmitry Monakhov } 5413911af577SEryu Guan /* 5414911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5415911af577SEryu Guan * update c/mtime in shrink case below 5416911af577SEryu Guan */ 5417911af577SEryu Guan if (!shrink) { 5418eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5419911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5420911af577SEryu Guan } 5421aa75f4d3SHarshad Shirwadkar 5422aa75f4d3SHarshad Shirwadkar if (shrink) 5423a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, 5424aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5425aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5426aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : 0) >> 5427aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5428aa75f4d3SHarshad Shirwadkar else 5429aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5430a80f7fcfSHarshad Shirwadkar handle, inode, 5431aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5432aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5433aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5434aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5435aa75f4d3SHarshad Shirwadkar 543690e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5437617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5438617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5439ac27a0ecSDave Kleikamp if (!error) 5440ac27a0ecSDave Kleikamp error = rc; 544190e775b7SJan Kara /* 544290e775b7SJan Kara * We have to update i_size under i_data_sem together 544390e775b7SJan Kara * with i_disksize to avoid races with writeback code 544490e775b7SJan Kara * running ext4_wb_update_i_disksize(). 544590e775b7SJan Kara */ 544690e775b7SJan Kara if (!error) 544790e775b7SJan Kara i_size_write(inode, attr->ia_size); 544890e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5449617ba13bSMingming Cao ext4_journal_stop(handle); 5450b9c1c267SJan Kara if (error) 5451b9c1c267SJan Kara goto out_mmap_sem; 545282a25b02SJan Kara if (!shrink) { 5453b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5454b9c1c267SJan Kara inode->i_size); 5455b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 545682a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5457b9c1c267SJan Kara } 5458430657b6SRoss Zwisler } 5459430657b6SRoss Zwisler 546053e87268SJan Kara /* 546153e87268SJan Kara * Truncate pagecache after we've waited for commit 546253e87268SJan Kara * in data=journal mode to make pages freeable. 546353e87268SJan Kara */ 54647caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5465b9c1c267SJan Kara /* 5466b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5467b9c1c267SJan Kara * truncate possible preallocated blocks. 5468b9c1c267SJan Kara */ 5469b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54702c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54712c98eb5eSTheodore Ts'o if (rc) 54722c98eb5eSTheodore Ts'o error = rc; 54732c98eb5eSTheodore Ts'o } 5474b9c1c267SJan Kara out_mmap_sem: 5475d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 54763da40c7bSJosef Bacik } 5477ac27a0ecSDave Kleikamp 54782c98eb5eSTheodore Ts'o if (!error) { 547914f3db55SChristian Brauner setattr_copy(mnt_userns, inode, attr); 54801025774cSChristoph Hellwig mark_inode_dirty(inode); 54811025774cSChristoph Hellwig } 54821025774cSChristoph Hellwig 54831025774cSChristoph Hellwig /* 54841025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 54851025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 54861025774cSChristoph Hellwig */ 54873d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5488617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5489ac27a0ecSDave Kleikamp 54902c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 549114f3db55SChristian Brauner rc = posix_acl_chmod(mnt_userns, inode, inode->i_mode); 5492ac27a0ecSDave Kleikamp 5493ac27a0ecSDave Kleikamp err_out: 5494aa75f4d3SHarshad Shirwadkar if (error) 5495617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5496ac27a0ecSDave Kleikamp if (!error) 5497ac27a0ecSDave Kleikamp error = rc; 5498aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 5499ac27a0ecSDave Kleikamp return error; 5500ac27a0ecSDave Kleikamp } 5501ac27a0ecSDave Kleikamp 5502549c7297SChristian Brauner int ext4_getattr(struct user_namespace *mnt_userns, const struct path *path, 5503549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags) 55043e3398a0SMingming Cao { 550599652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 550699652ea5SDavid Howells struct ext4_inode *raw_inode; 550799652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 550899652ea5SDavid Howells unsigned int flags; 55093e3398a0SMingming Cao 5510d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5511d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 551299652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 551399652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 551499652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 551599652ea5SDavid Howells } 551699652ea5SDavid Howells 551799652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 551899652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 551999652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 552099652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 552199652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 552299652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 552399652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 552499652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 552599652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 552699652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 552799652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 55281f607195SEric Biggers if (flags & EXT4_VERITY_FL) 55291f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 553099652ea5SDavid Howells 55313209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 55323209f68bSDavid Howells STATX_ATTR_COMPRESSED | 55333209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 55343209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 55351f607195SEric Biggers STATX_ATTR_NODUMP | 55361f607195SEric Biggers STATX_ATTR_VERITY); 55373209f68bSDavid Howells 553814f3db55SChristian Brauner generic_fillattr(mnt_userns, inode, stat); 553999652ea5SDavid Howells return 0; 554099652ea5SDavid Howells } 554199652ea5SDavid Howells 5542549c7297SChristian Brauner int ext4_file_getattr(struct user_namespace *mnt_userns, 5543549c7297SChristian Brauner const struct path *path, struct kstat *stat, 554499652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 554599652ea5SDavid Howells { 554699652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 554799652ea5SDavid Howells u64 delalloc_blocks; 554899652ea5SDavid Howells 554914f3db55SChristian Brauner ext4_getattr(mnt_userns, path, stat, request_mask, query_flags); 55503e3398a0SMingming Cao 55513e3398a0SMingming Cao /* 55529206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 55539206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 55549206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5555d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 55569206c561SAndreas Dilger */ 55579206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 55589206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 55599206c561SAndreas Dilger 55609206c561SAndreas Dilger /* 55613e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 55623e3398a0SMingming Cao * otherwise in the case of system crash before the real block 55633e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 55643e3398a0SMingming Cao * on-disk file blocks. 55653e3398a0SMingming Cao * We always keep i_blocks updated together with real 55663e3398a0SMingming Cao * allocation. But to not confuse with user, stat 55673e3398a0SMingming Cao * will return the blocks that include the delayed allocation 55683e3398a0SMingming Cao * blocks for this file. 55693e3398a0SMingming Cao */ 557096607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 557196607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55728af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55733e3398a0SMingming Cao return 0; 55743e3398a0SMingming Cao } 5575ac27a0ecSDave Kleikamp 5576fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5577fffb2739SJan Kara int pextents) 5578a02908f1SMingming Cao { 557912e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5580fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5581fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5582a02908f1SMingming Cao } 5583ac51d837STheodore Ts'o 5584a02908f1SMingming Cao /* 5585a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5586a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5587a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5588a02908f1SMingming Cao * 5589a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 55904907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5591a02908f1SMingming Cao * they could still across block group boundary. 5592a02908f1SMingming Cao * 5593a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5594a02908f1SMingming Cao */ 5595dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5596fffb2739SJan Kara int pextents) 5597a02908f1SMingming Cao { 55988df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 55998df9675fSTheodore Ts'o int gdpblocks; 5600a02908f1SMingming Cao int idxblocks; 5601a02908f1SMingming Cao int ret = 0; 5602a02908f1SMingming Cao 5603a02908f1SMingming Cao /* 5604fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5605fffb2739SJan Kara * to @pextents physical extents? 5606a02908f1SMingming Cao */ 5607fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5608a02908f1SMingming Cao 5609a02908f1SMingming Cao ret = idxblocks; 5610a02908f1SMingming Cao 5611a02908f1SMingming Cao /* 5612a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5613a02908f1SMingming Cao * to account 5614a02908f1SMingming Cao */ 5615fffb2739SJan Kara groups = idxblocks + pextents; 5616a02908f1SMingming Cao gdpblocks = groups; 56178df9675fSTheodore Ts'o if (groups > ngroups) 56188df9675fSTheodore Ts'o groups = ngroups; 5619a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5620a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5621a02908f1SMingming Cao 5622a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5623a02908f1SMingming Cao ret += groups + gdpblocks; 5624a02908f1SMingming Cao 5625a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5626a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5627ac27a0ecSDave Kleikamp 5628ac27a0ecSDave Kleikamp return ret; 5629ac27a0ecSDave Kleikamp } 5630ac27a0ecSDave Kleikamp 5631ac27a0ecSDave Kleikamp /* 563225985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5633f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5634f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5635a02908f1SMingming Cao * 5636525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5637a02908f1SMingming Cao * 5638525f4ed8SMingming Cao * We need to consider the worse case, when 5639a02908f1SMingming Cao * one new block per extent. 5640a02908f1SMingming Cao */ 5641a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5642a02908f1SMingming Cao { 5643a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5644a02908f1SMingming Cao int ret; 5645a02908f1SMingming Cao 5646fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5647a02908f1SMingming Cao 5648a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5649a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5650a02908f1SMingming Cao ret += bpp; 5651a02908f1SMingming Cao return ret; 5652a02908f1SMingming Cao } 5653f3bd1f3fSMingming Cao 5654f3bd1f3fSMingming Cao /* 5655f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5656f3bd1f3fSMingming Cao * 5657f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 565879e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5659f3bd1f3fSMingming Cao * 5660f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5661f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5662f3bd1f3fSMingming Cao */ 5663f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5664f3bd1f3fSMingming Cao { 5665f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5666f3bd1f3fSMingming Cao } 5667f3bd1f3fSMingming Cao 5668a02908f1SMingming Cao /* 5669617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5670ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5671ac27a0ecSDave Kleikamp */ 5672617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5673617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5674ac27a0ecSDave Kleikamp { 5675ac27a0ecSDave Kleikamp int err = 0; 5676ac27a0ecSDave Kleikamp 5677a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5678a6758309SVasily Averin put_bh(iloc->bh); 56790db1ff22STheodore Ts'o return -EIO; 5680a6758309SVasily Averin } 5681a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5682aa75f4d3SHarshad Shirwadkar 5683c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 568425ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 568525ec56b5SJean Noel Cordenner 5686ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5687ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5688ac27a0ecSDave Kleikamp 5689dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5690830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5691ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5692ac27a0ecSDave Kleikamp return err; 5693ac27a0ecSDave Kleikamp } 5694ac27a0ecSDave Kleikamp 5695ac27a0ecSDave Kleikamp /* 5696ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5697ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5698ac27a0ecSDave Kleikamp */ 5699ac27a0ecSDave Kleikamp 5700ac27a0ecSDave Kleikamp int 5701617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5702617ba13bSMingming Cao struct ext4_iloc *iloc) 5703ac27a0ecSDave Kleikamp { 57040390131bSFrank Mayhar int err; 57050390131bSFrank Mayhar 57060db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 57070db1ff22STheodore Ts'o return -EIO; 57080db1ff22STheodore Ts'o 5709617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5710ac27a0ecSDave Kleikamp if (!err) { 5711ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5712188c299eSJan Kara err = ext4_journal_get_write_access(handle, inode->i_sb, 5713188c299eSJan Kara iloc->bh, EXT4_JTR_NONE); 5714ac27a0ecSDave Kleikamp if (err) { 5715ac27a0ecSDave Kleikamp brelse(iloc->bh); 5716ac27a0ecSDave Kleikamp iloc->bh = NULL; 5717ac27a0ecSDave Kleikamp } 5718ac27a0ecSDave Kleikamp } 5719617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5720ac27a0ecSDave Kleikamp return err; 5721ac27a0ecSDave Kleikamp } 5722ac27a0ecSDave Kleikamp 5723c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5724c03b45b8SMiao Xie unsigned int new_extra_isize, 5725c03b45b8SMiao Xie struct ext4_iloc *iloc, 5726c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5727c03b45b8SMiao Xie { 5728c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5729c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 57304ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 57314ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5732c03b45b8SMiao Xie int error; 5733c03b45b8SMiao Xie 57344ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 57354ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 57364ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 57374ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 57384ea99936STheodore Ts'o ei->i_extra_isize, 57394ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 57404ea99936STheodore Ts'o return -EFSCORRUPTED; 57414ea99936STheodore Ts'o } 57424ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 57434ea99936STheodore Ts'o (new_extra_isize < 4) || 57444ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 57454ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 57464ea99936STheodore Ts'o 5747c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5748c03b45b8SMiao Xie 5749c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5750c03b45b8SMiao Xie 5751c03b45b8SMiao Xie /* No extended attributes present */ 5752c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5753c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5754c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5755c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5756c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5757c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5758c03b45b8SMiao Xie return 0; 5759c03b45b8SMiao Xie } 5760c03b45b8SMiao Xie 5761c03b45b8SMiao Xie /* try to expand with EAs present */ 5762c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5763c03b45b8SMiao Xie raw_inode, handle); 5764c03b45b8SMiao Xie if (error) { 5765c03b45b8SMiao Xie /* 5766c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5767c03b45b8SMiao Xie */ 5768c03b45b8SMiao Xie *no_expand = 1; 5769c03b45b8SMiao Xie } 5770c03b45b8SMiao Xie 5771c03b45b8SMiao Xie return error; 5772c03b45b8SMiao Xie } 5773c03b45b8SMiao Xie 5774ac27a0ecSDave Kleikamp /* 57756dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57766dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 57776dd4ee7cSKalpak Shah */ 5778cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 57791d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 57801d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 57811d03ec98SAneesh Kumar K.V handle_t *handle) 57826dd4ee7cSKalpak Shah { 57833b10fdc6SMiao Xie int no_expand; 57843b10fdc6SMiao Xie int error; 57856dd4ee7cSKalpak Shah 5786cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5787cf0a5e81SMiao Xie return -EOVERFLOW; 5788cf0a5e81SMiao Xie 5789cf0a5e81SMiao Xie /* 5790cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5791cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5792cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5793cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5794cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5795cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5796cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5797cf0a5e81SMiao Xie */ 57986cb367c2SJan Kara if (ext4_journal_extend(handle, 579983448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5800cf0a5e81SMiao Xie return -ENOSPC; 58016dd4ee7cSKalpak Shah 58023b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5803cf0a5e81SMiao Xie return -EBUSY; 58043b10fdc6SMiao Xie 5805c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5806c03b45b8SMiao Xie handle, &no_expand); 58073b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5808c03b45b8SMiao Xie 5809c03b45b8SMiao Xie return error; 58106dd4ee7cSKalpak Shah } 58116dd4ee7cSKalpak Shah 5812c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5813c03b45b8SMiao Xie unsigned int new_extra_isize, 5814c03b45b8SMiao Xie struct ext4_iloc *iloc) 5815c03b45b8SMiao Xie { 5816c03b45b8SMiao Xie handle_t *handle; 5817c03b45b8SMiao Xie int no_expand; 5818c03b45b8SMiao Xie int error, rc; 5819c03b45b8SMiao Xie 5820c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5821c03b45b8SMiao Xie brelse(iloc->bh); 5822c03b45b8SMiao Xie return -EOVERFLOW; 5823c03b45b8SMiao Xie } 5824c03b45b8SMiao Xie 5825c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5826c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5827c03b45b8SMiao Xie if (IS_ERR(handle)) { 5828c03b45b8SMiao Xie error = PTR_ERR(handle); 5829c03b45b8SMiao Xie brelse(iloc->bh); 5830c03b45b8SMiao Xie return error; 5831c03b45b8SMiao Xie } 5832c03b45b8SMiao Xie 5833c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5834c03b45b8SMiao Xie 5835ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5836188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, 5837188c299eSJan Kara EXT4_JTR_NONE); 58383b10fdc6SMiao Xie if (error) { 5839c03b45b8SMiao Xie brelse(iloc->bh); 58407f420d64SDan Carpenter goto out_unlock; 58413b10fdc6SMiao Xie } 5842cf0a5e81SMiao Xie 5843c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5844c03b45b8SMiao Xie handle, &no_expand); 5845c03b45b8SMiao Xie 5846c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5847c03b45b8SMiao Xie if (!error) 5848c03b45b8SMiao Xie error = rc; 5849c03b45b8SMiao Xie 58507f420d64SDan Carpenter out_unlock: 5851c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5852c03b45b8SMiao Xie ext4_journal_stop(handle); 58533b10fdc6SMiao Xie return error; 58546dd4ee7cSKalpak Shah } 58556dd4ee7cSKalpak Shah 58566dd4ee7cSKalpak Shah /* 5857ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5858ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5859ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5860ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5861ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5862ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5863ac27a0ecSDave Kleikamp * 5864ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5865ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5866ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5867ac27a0ecSDave Kleikamp * we start and wait on commits. 5868ac27a0ecSDave Kleikamp */ 58694209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 58704209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5871ac27a0ecSDave Kleikamp { 5872617ba13bSMingming Cao struct ext4_iloc iloc; 58736dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5874cf0a5e81SMiao Xie int err; 5875ac27a0ecSDave Kleikamp 5876ac27a0ecSDave Kleikamp might_sleep(); 58777ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5878617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58795e1021f2SEryu Guan if (err) 58804209ae12SHarshad Shirwadkar goto out; 5881cf0a5e81SMiao Xie 5882cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5883cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 58846dd4ee7cSKalpak Shah iloc, handle); 5885cf0a5e81SMiao Xie 58864209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 58874209ae12SHarshad Shirwadkar out: 58884209ae12SHarshad Shirwadkar if (unlikely(err)) 58894209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 58904209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 58914209ae12SHarshad Shirwadkar return err; 5892ac27a0ecSDave Kleikamp } 5893ac27a0ecSDave Kleikamp 5894ac27a0ecSDave Kleikamp /* 5895617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5896ac27a0ecSDave Kleikamp * 5897ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5898ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5899ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5900ac27a0ecSDave Kleikamp * 59015dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5902ac27a0ecSDave Kleikamp * are allocated to the file. 5903ac27a0ecSDave Kleikamp * 5904ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5905ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5906ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5907ac27a0ecSDave Kleikamp */ 5908aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5909ac27a0ecSDave Kleikamp { 5910ac27a0ecSDave Kleikamp handle_t *handle; 5911ac27a0ecSDave Kleikamp 59129924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5913ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5914ac27a0ecSDave Kleikamp return; 5915e2728c56SEric Biggers ext4_mark_inode_dirty(handle, inode); 5916e2728c56SEric Biggers ext4_journal_stop(handle); 5917ac27a0ecSDave Kleikamp } 5918ac27a0ecSDave Kleikamp 5919617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5920ac27a0ecSDave Kleikamp { 5921ac27a0ecSDave Kleikamp journal_t *journal; 5922ac27a0ecSDave Kleikamp handle_t *handle; 5923ac27a0ecSDave Kleikamp int err; 5924c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5925ac27a0ecSDave Kleikamp 5926ac27a0ecSDave Kleikamp /* 5927ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5928ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5929ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5930ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5931ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5932ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5933ac27a0ecSDave Kleikamp * nobody is changing anything. 5934ac27a0ecSDave Kleikamp */ 5935ac27a0ecSDave Kleikamp 5936617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59370390131bSFrank Mayhar if (!journal) 59380390131bSFrank Mayhar return 0; 5939d699594dSDave Hansen if (is_journal_aborted(journal)) 5940ac27a0ecSDave Kleikamp return -EROFS; 5941ac27a0ecSDave Kleikamp 594217335dccSDmitry Monakhov /* Wait for all existing dio workers */ 594317335dccSDmitry Monakhov inode_dio_wait(inode); 594417335dccSDmitry Monakhov 59454c546592SDaeho Jeong /* 59464c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 59474c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 59484c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 59494c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 59504c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 59514c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 59524c546592SDaeho Jeong */ 59534c546592SDaeho Jeong if (val) { 5954d4f5258eSJan Kara filemap_invalidate_lock(inode->i_mapping); 59554c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 59564c546592SDaeho Jeong if (err < 0) { 5957d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 59584c546592SDaeho Jeong return err; 59594c546592SDaeho Jeong } 59604c546592SDaeho Jeong } 59614c546592SDaeho Jeong 5962bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 5963dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5964ac27a0ecSDave Kleikamp 5965ac27a0ecSDave Kleikamp /* 5966ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5967ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5968ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5969ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5970ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5971ac27a0ecSDave Kleikamp */ 5972ac27a0ecSDave Kleikamp 5973ac27a0ecSDave Kleikamp if (val) 597412e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59755872ddaaSYongqiang Yang else { 597601d5d965SLeah Rumancik err = jbd2_journal_flush(journal, 0); 59774f879ca6SJan Kara if (err < 0) { 59784f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 5979bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 59804f879ca6SJan Kara return err; 59814f879ca6SJan Kara } 598212e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59835872ddaaSYongqiang Yang } 5984617ba13bSMingming Cao ext4_set_aops(inode); 5985ac27a0ecSDave Kleikamp 5986dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5987bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 5988c8585c6fSDaeho Jeong 59894c546592SDaeho Jeong if (val) 5990d4f5258eSJan Kara filemap_invalidate_unlock(inode->i_mapping); 5991ac27a0ecSDave Kleikamp 5992ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5993ac27a0ecSDave Kleikamp 59949924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5995ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5996ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5997ac27a0ecSDave Kleikamp 5998aa75f4d3SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, 5999aa75f4d3SHarshad Shirwadkar EXT4_FC_REASON_JOURNAL_FLAG_CHANGE); 6000617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60010390131bSFrank Mayhar ext4_handle_sync(handle); 6002617ba13bSMingming Cao ext4_journal_stop(handle); 6003617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6004ac27a0ecSDave Kleikamp 6005ac27a0ecSDave Kleikamp return err; 6006ac27a0ecSDave Kleikamp } 60072e9ee850SAneesh Kumar K.V 6008188c299eSJan Kara static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, 6009188c299eSJan Kara struct buffer_head *bh) 60102e9ee850SAneesh Kumar K.V { 60112e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60122e9ee850SAneesh Kumar K.V } 60132e9ee850SAneesh Kumar K.V 6014401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 60152e9ee850SAneesh Kumar K.V { 601611bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 6017c2ec175cSNick Piggin struct page *page = vmf->page; 60182e9ee850SAneesh Kumar K.V loff_t size; 60192e9ee850SAneesh Kumar K.V unsigned long len; 6020401b25aaSSouptick Joarder int err; 6021401b25aaSSouptick Joarder vm_fault_t ret; 60222e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6023496ad9aaSAl Viro struct inode *inode = file_inode(file); 60242e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60259ea7df53SJan Kara handle_t *handle; 60269ea7df53SJan Kara get_block_t *get_block; 60279ea7df53SJan Kara int retries = 0; 60282e9ee850SAneesh Kumar K.V 602902b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 603002b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 603102b016caSTheodore Ts'o 60328e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6033041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6034ea3d7209SJan Kara 6035d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping); 60367b4cc978SEric Biggers 6037401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6038401b25aaSSouptick Joarder if (err) 60397b4cc978SEric Biggers goto out_ret; 60407b4cc978SEric Biggers 604164a9f144SMauricio Faria de Oliveira /* 604264a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 604364a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 604464a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 604564a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 604664a9f144SMauricio Faria de Oliveira */ 604764a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 604864a9f144SMauricio Faria de Oliveira goto retry_alloc; 604964a9f144SMauricio Faria de Oliveira 60509ea7df53SJan Kara /* Delalloc case is easy... */ 60519ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 60529ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 60539ea7df53SJan Kara do { 6054401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 60559ea7df53SJan Kara ext4_da_get_block_prep); 6056401b25aaSSouptick Joarder } while (err == -ENOSPC && 60579ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 60589ea7df53SJan Kara goto out_ret; 60592e9ee850SAneesh Kumar K.V } 60600e499890SDarrick J. Wong 60610e499890SDarrick J. Wong lock_page(page); 60629ea7df53SJan Kara size = i_size_read(inode); 60639ea7df53SJan Kara /* Page got truncated from under us? */ 60649ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 60659ea7df53SJan Kara unlock_page(page); 60669ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 60679ea7df53SJan Kara goto out; 60680e499890SDarrick J. Wong } 60692e9ee850SAneesh Kumar K.V 607009cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 607109cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 60722e9ee850SAneesh Kumar K.V else 607309cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6074a827eaffSAneesh Kumar K.V /* 60759ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 60769ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 607764a9f144SMauricio Faria de Oliveira * 607864a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 607964a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6080a827eaffSAneesh Kumar K.V */ 60812e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6082188c299eSJan Kara if (!ext4_walk_page_buffers(NULL, inode, page_buffers(page), 6083f19d5870STao Ma 0, len, NULL, 6084a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 60859ea7df53SJan Kara /* Wait so that we don't change page under IO */ 60861d1d1a76SDarrick J. Wong wait_for_stable_page(page); 60879ea7df53SJan Kara ret = VM_FAULT_LOCKED; 60889ea7df53SJan Kara goto out; 60892e9ee850SAneesh Kumar K.V } 6090a827eaffSAneesh Kumar K.V } 6091a827eaffSAneesh Kumar K.V unlock_page(page); 60929ea7df53SJan Kara /* OK, we need to fill the hole... */ 60939ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6094705965bdSJan Kara get_block = ext4_get_block_unwritten; 60959ea7df53SJan Kara else 60969ea7df53SJan Kara get_block = ext4_get_block; 60979ea7df53SJan Kara retry_alloc: 60989924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 60999924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 61009ea7df53SJan Kara if (IS_ERR(handle)) { 6101c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 61029ea7df53SJan Kara goto out; 61039ea7df53SJan Kara } 610464a9f144SMauricio Faria de Oliveira /* 610564a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 610664a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 610764a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 610864a9f144SMauricio Faria de Oliveira */ 610964a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6110401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 611164a9f144SMauricio Faria de Oliveira } else { 611264a9f144SMauricio Faria de Oliveira lock_page(page); 611364a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 611464a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 611564a9f144SMauricio Faria de Oliveira if (page->mapping != mapping || page_offset(page) > size) { 611664a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6117afb585a9SMauricio Faria de Oliveira goto out_error; 611864a9f144SMauricio Faria de Oliveira } 611964a9f144SMauricio Faria de Oliveira 612064a9f144SMauricio Faria de Oliveira if (page->index == size >> PAGE_SHIFT) 612164a9f144SMauricio Faria de Oliveira len = size & ~PAGE_MASK; 612264a9f144SMauricio Faria de Oliveira else 612364a9f144SMauricio Faria de Oliveira len = PAGE_SIZE; 612464a9f144SMauricio Faria de Oliveira 612564a9f144SMauricio Faria de Oliveira err = __block_write_begin(page, 0, len, ext4_get_block); 612664a9f144SMauricio Faria de Oliveira if (!err) { 61279ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6128188c299eSJan Kara if (ext4_walk_page_buffers(handle, inode, 6129188c299eSJan Kara page_buffers(page), 0, len, NULL, 6130188c299eSJan Kara do_journal_get_write_access)) 6131afb585a9SMauricio Faria de Oliveira goto out_error; 6132188c299eSJan Kara if (ext4_walk_page_buffers(handle, inode, 6133188c299eSJan Kara page_buffers(page), 0, len, NULL, 6134188c299eSJan Kara write_end_fn)) 6135afb585a9SMauricio Faria de Oliveira goto out_error; 6136b5b18160SJan Kara if (ext4_jbd2_inode_add_write(handle, inode, 6137b5b18160SJan Kara page_offset(page), len)) 6138afb585a9SMauricio Faria de Oliveira goto out_error; 61399ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 614064a9f144SMauricio Faria de Oliveira } else { 614164a9f144SMauricio Faria de Oliveira unlock_page(page); 614264a9f144SMauricio Faria de Oliveira } 61439ea7df53SJan Kara } 61449ea7df53SJan Kara ext4_journal_stop(handle); 6145401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 61469ea7df53SJan Kara goto retry_alloc; 61479ea7df53SJan Kara out_ret: 6148401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 61499ea7df53SJan Kara out: 6150d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping); 61518e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 61522e9ee850SAneesh Kumar K.V return ret; 6153afb585a9SMauricio Faria de Oliveira out_error: 6154afb585a9SMauricio Faria de Oliveira unlock_page(page); 6155afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6156afb585a9SMauricio Faria de Oliveira goto out; 61572e9ee850SAneesh Kumar K.V } 6158