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> 23ac27a0ecSDave Kleikamp #include <linux/time.h> 24ac27a0ecSDave Kleikamp #include <linux/highuid.h> 25ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 26c94c2acfSMatthew Wilcox #include <linux/dax.h> 27ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 28ac27a0ecSDave Kleikamp #include <linux/string.h> 29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 30ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3164769240SAlex Tomas #include <linux/pagevec.h> 32ac27a0ecSDave Kleikamp #include <linux/mpage.h> 33e83c1397SDuane Griffin #include <linux/namei.h> 34ac27a0ecSDave Kleikamp #include <linux/uio.h> 35ac27a0ecSDave Kleikamp #include <linux/bio.h> 364c0425ffSMingming Cao #include <linux/workqueue.h> 37744692dcSJiaying Zhang #include <linux/kernel.h> 386db26ffcSAndrew Morton #include <linux/printk.h> 395a0e3ad6STejun Heo #include <linux/slab.h> 4000a1a053STheodore Ts'o #include <linux/bitops.h> 41364443cbSJan Kara #include <linux/iomap.h> 42ae5e165dSJeff Layton #include <linux/iversion.h> 439bffad1eSTheodore Ts'o 443dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 45ac27a0ecSDave Kleikamp #include "xattr.h" 46ac27a0ecSDave Kleikamp #include "acl.h" 479f125d64STheodore Ts'o #include "truncate.h" 48ac27a0ecSDave Kleikamp 499bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 509bffad1eSTheodore Ts'o 51814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 52814525f4SDarrick J. Wong struct ext4_inode_info *ei) 53814525f4SDarrick J. Wong { 54814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55814525f4SDarrick J. Wong __u32 csum; 56b47820edSDaeho Jeong __u16 dummy_csum = 0; 57b47820edSDaeho Jeong int offset = offsetof(struct ext4_inode, i_checksum_lo); 58b47820edSDaeho Jeong unsigned int csum_size = sizeof(dummy_csum); 59814525f4SDarrick J. Wong 60b47820edSDaeho Jeong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); 61b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); 62b47820edSDaeho Jeong offset += csum_size; 63b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 64b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE - offset); 65b47820edSDaeho Jeong 66b47820edSDaeho Jeong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 67b47820edSDaeho Jeong offset = offsetof(struct ext4_inode, i_checksum_hi); 68b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + 69b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE, 70b47820edSDaeho Jeong offset - EXT4_GOOD_OLD_INODE_SIZE); 71b47820edSDaeho Jeong if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 72b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, 73b47820edSDaeho Jeong csum_size); 74b47820edSDaeho Jeong offset += csum_size; 75814525f4SDarrick J. Wong } 7605ac5aa1SDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 7705ac5aa1SDaeho Jeong EXT4_INODE_SIZE(inode->i_sb) - offset); 78b47820edSDaeho Jeong } 79814525f4SDarrick J. Wong 80814525f4SDarrick J. Wong return csum; 81814525f4SDarrick J. Wong } 82814525f4SDarrick J. Wong 83814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 84814525f4SDarrick J. Wong struct ext4_inode_info *ei) 85814525f4SDarrick J. Wong { 86814525f4SDarrick J. Wong __u32 provided, calculated; 87814525f4SDarrick J. Wong 88814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 89814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 909aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 91814525f4SDarrick J. Wong return 1; 92814525f4SDarrick J. Wong 93814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 94814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 95814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 96814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 97814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 98814525f4SDarrick J. Wong else 99814525f4SDarrick J. Wong calculated &= 0xFFFF; 100814525f4SDarrick J. Wong 101814525f4SDarrick J. Wong return provided == calculated; 102814525f4SDarrick J. Wong } 103814525f4SDarrick J. Wong 1048016e29fSHarshad Shirwadkar void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 105814525f4SDarrick J. Wong struct ext4_inode_info *ei) 106814525f4SDarrick J. Wong { 107814525f4SDarrick J. Wong __u32 csum; 108814525f4SDarrick J. Wong 109814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 110814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 1119aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 112814525f4SDarrick J. Wong return; 113814525f4SDarrick J. Wong 114814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 115814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 116814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 117814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 118814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 119814525f4SDarrick J. Wong } 120814525f4SDarrick J. Wong 121678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 122678aaf48SJan Kara loff_t new_size) 123678aaf48SJan Kara { 1247ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1258aefcd55STheodore Ts'o /* 1268aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1278aefcd55STheodore Ts'o * writing, so there's no need to call 1288aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1298aefcd55STheodore Ts'o * outstanding writes we need to flush. 1308aefcd55STheodore Ts'o */ 1318aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1328aefcd55STheodore Ts'o return 0; 1338aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1348aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 135678aaf48SJan Kara new_size); 136678aaf48SJan Kara } 137678aaf48SJan Kara 138d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 139d47992f8SLukas Czerner unsigned int length); 140cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 141cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 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; 178ac27a0ecSDave Kleikamp 1797ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1802581fdc8SJiaying Zhang 1810930fcc1SAl Viro if (inode->i_nlink) { 1822d859db3SJan Kara /* 1832d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1842d859db3SJan Kara * journal. So although mm thinks everything is clean and 1852d859db3SJan Kara * ready for reaping the inode might still have some pages to 1862d859db3SJan Kara * write in the running transaction or waiting to be 1872d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1882d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1892d859db3SJan Kara * cause data loss. Also even if we did not discard these 1902d859db3SJan Kara * buffers, we would have no way to find them after the inode 1912d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1922d859db3SJan Kara * read them before the transaction is checkpointed. So be 1932d859db3SJan Kara * careful and force everything to disk here... We use 1942d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1952d859db3SJan Kara * containing inode's data. 1962d859db3SJan Kara * 1972d859db3SJan Kara * Note that directories do not have this problem because they 1982d859db3SJan Kara * don't use page cache. 1992d859db3SJan Kara */ 2006a7fd522SVegard Nossum if (inode->i_ino != EXT4_JOURNAL_INO && 2016a7fd522SVegard Nossum ext4_should_journal_data(inode) && 2023abb1a0fSJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2033abb1a0fSJan Kara inode->i_data.nrpages) { 2042d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2052d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2062d859db3SJan Kara 207d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2082d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2092d859db3SJan Kara } 21091b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 2115dc23bddSJan Kara 2120930fcc1SAl Viro goto no_delete; 2130930fcc1SAl Viro } 2140930fcc1SAl Viro 215e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 216e2bfb088STheodore Ts'o goto no_delete; 217871a2931SChristoph Hellwig dquot_initialize(inode); 218907f4554SChristoph Hellwig 219678aaf48SJan Kara if (ext4_should_order_data(inode)) 220678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 22191b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 222ac27a0ecSDave Kleikamp 2238e8ad8a5SJan Kara /* 224ceff86fdSJan Kara * For inodes with journalled data, transaction commit could have 225ceff86fdSJan Kara * dirtied the inode. Flush worker is ignoring it because of I_FREEING 226ceff86fdSJan Kara * flag but we still need to remove the inode from the writeback lists. 227ceff86fdSJan Kara */ 228ceff86fdSJan Kara if (!list_empty_careful(&inode->i_io_list)) { 229ceff86fdSJan Kara WARN_ON_ONCE(!ext4_should_journal_data(inode)); 230ceff86fdSJan Kara inode_io_list_del(inode); 231ceff86fdSJan Kara } 232ceff86fdSJan Kara 233ceff86fdSJan Kara /* 2348e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2358e8ad8a5SJan Kara * protection against it 2368e8ad8a5SJan Kara */ 2378e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 238e50e5129SAndreas Dilger 23930a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 24030a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 24130a7eb97STahsin Erdogan 24265db869cSJan Kara /* 24365db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 24465db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 24565db869cSJan Kara */ 24630a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 24765db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 248ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 249bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 250ac27a0ecSDave Kleikamp /* 251ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 252ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 253ac27a0ecSDave Kleikamp * cleaned up. 254ac27a0ecSDave Kleikamp */ 255617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2568e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 257ac27a0ecSDave Kleikamp goto no_delete; 258ac27a0ecSDave Kleikamp } 25930a7eb97STahsin Erdogan 260ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2610390131bSFrank Mayhar ext4_handle_sync(handle); 262407cd7fbSTahsin Erdogan 263407cd7fbSTahsin Erdogan /* 264407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 265407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 266407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 267407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 268407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 269407cd7fbSTahsin Erdogan */ 270407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 271407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 272ac27a0ecSDave Kleikamp inode->i_size = 0; 273bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 274bc965ab3STheodore Ts'o if (err) { 27512062dddSEric Sandeen ext4_warning(inode->i_sb, 276bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 277bc965ab3STheodore Ts'o goto stop_handle; 278bc965ab3STheodore Ts'o } 2792c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2802c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2812c98eb5eSTheodore Ts'o if (err) { 28254d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err, 2832c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2842c98eb5eSTheodore Ts'o inode->i_ino, err); 2852c98eb5eSTheodore Ts'o goto stop_handle; 2862c98eb5eSTheodore Ts'o } 2872c98eb5eSTheodore Ts'o } 288bc965ab3STheodore Ts'o 28930a7eb97STahsin Erdogan /* Remove xattr references. */ 29030a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 29130a7eb97STahsin Erdogan extra_credits); 29230a7eb97STahsin Erdogan if (err) { 29330a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 294bc965ab3STheodore Ts'o stop_handle: 295bc965ab3STheodore Ts'o ext4_journal_stop(handle); 29645388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2978e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 29830a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 299bc965ab3STheodore Ts'o goto no_delete; 300bc965ab3STheodore Ts'o } 301bc965ab3STheodore Ts'o 302ac27a0ecSDave Kleikamp /* 303617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 304ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 305617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 306ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 307617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 308ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 309ac27a0ecSDave Kleikamp */ 310617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3115ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 312ac27a0ecSDave Kleikamp 313ac27a0ecSDave Kleikamp /* 314ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 315ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 316ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 317ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 318ac27a0ecSDave Kleikamp * fails. 319ac27a0ecSDave Kleikamp */ 320617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 321ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3220930fcc1SAl Viro ext4_clear_inode(inode); 323ac27a0ecSDave Kleikamp else 324617ba13bSMingming Cao ext4_free_inode(handle, inode); 325617ba13bSMingming Cao ext4_journal_stop(handle); 3268e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3270421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 328ac27a0ecSDave Kleikamp return; 329ac27a0ecSDave Kleikamp no_delete: 330b21ebf14SHarshad Shirwadkar if (!list_empty(&EXT4_I(inode)->i_fc_list)) 331b21ebf14SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM); 3320930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 333ac27a0ecSDave Kleikamp } 334ac27a0ecSDave Kleikamp 335a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 336a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 33760e58e0fSMingming Cao { 338a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 33960e58e0fSMingming Cao } 340a9e7f447SDmitry Monakhov #endif 3419d0be502STheodore Ts'o 34212219aeaSAneesh Kumar K.V /* 3430637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3440637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3450637c6f4STheodore Ts'o */ 3465f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3475f634d06SAneesh Kumar K.V int used, int quota_claim) 34812219aeaSAneesh Kumar K.V { 34912219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3500637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 35112219aeaSAneesh Kumar K.V 3520637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 353d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3540637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3558de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3561084f252STheodore Ts'o "with only %d reserved data blocks", 3570637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3580637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3590637c6f4STheodore Ts'o WARN_ON(1); 3600637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3616bc6e63fSAneesh Kumar K.V } 36212219aeaSAneesh Kumar K.V 3630637c6f4STheodore Ts'o /* Update per-inode reservations */ 3640637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 36571d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3660637c6f4STheodore Ts'o 36712219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 36860e58e0fSMingming Cao 36972b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 37072b8ab9dSEric Sandeen if (quota_claim) 3717b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 37272b8ab9dSEric Sandeen else { 3735f634d06SAneesh Kumar K.V /* 3745f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3755f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 37672b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3775f634d06SAneesh Kumar K.V */ 3787b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3795f634d06SAneesh Kumar K.V } 380d6014301SAneesh Kumar K.V 381d6014301SAneesh Kumar K.V /* 382d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 383d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 384d6014301SAneesh Kumar K.V * inode's preallocations. 385d6014301SAneesh Kumar K.V */ 3860637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 38782dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 38827bc446eSbrookxu ext4_discard_preallocations(inode, 0); 38912219aeaSAneesh Kumar K.V } 39012219aeaSAneesh Kumar K.V 391e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 392c398eda0STheodore Ts'o unsigned int line, 39324676da4STheodore Ts'o struct ext4_map_blocks *map) 3946fd058f7STheodore Ts'o { 395345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 396345c0dbfSTheodore Ts'o (inode->i_ino == 397345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 398345c0dbfSTheodore Ts'o return 0; 399ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 400c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 401bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 40224676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 403bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4046a797d27SDarrick J. Wong return -EFSCORRUPTED; 4056fd058f7STheodore Ts'o } 4066fd058f7STheodore Ts'o return 0; 4076fd058f7STheodore Ts'o } 4086fd058f7STheodore Ts'o 40953085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 41053085facSJan Kara ext4_lblk_t len) 41153085facSJan Kara { 41253085facSJan Kara int ret; 41353085facSJan Kara 41433b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 415a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 41653085facSJan Kara 41753085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 41853085facSJan Kara if (ret > 0) 41953085facSJan Kara ret = 0; 42053085facSJan Kara 42153085facSJan Kara return ret; 42253085facSJan Kara } 42353085facSJan Kara 424e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 425c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 426e29136f8STheodore Ts'o 427921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 428921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 429921f266bSDmitry Monakhov struct inode *inode, 430921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 431921f266bSDmitry Monakhov struct ext4_map_blocks *map, 432921f266bSDmitry Monakhov int flags) 433921f266bSDmitry Monakhov { 434921f266bSDmitry Monakhov int retval; 435921f266bSDmitry Monakhov 436921f266bSDmitry Monakhov map->m_flags = 0; 437921f266bSDmitry Monakhov /* 438921f266bSDmitry Monakhov * There is a race window that the result is not the same. 439921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 440921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 441921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 442921f266bSDmitry Monakhov * could be converted. 443921f266bSDmitry Monakhov */ 444c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 445921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4469e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 447921f266bSDmitry Monakhov } else { 4489e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 449921f266bSDmitry Monakhov } 450921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 451921f266bSDmitry Monakhov 452921f266bSDmitry Monakhov /* 453921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 454921f266bSDmitry Monakhov * tree. So the m_len might not equal. 455921f266bSDmitry Monakhov */ 456921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 457921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 458921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 459bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 460921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 461921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 462921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 463921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 464921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 465921f266bSDmitry Monakhov retval, flags); 466921f266bSDmitry Monakhov } 467921f266bSDmitry Monakhov } 468921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 469921f266bSDmitry Monakhov 47055138e0bSTheodore Ts'o /* 471e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4722b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 473f5ab0d1fSMingming Cao * 474f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 475f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 476f5ab0d1fSMingming Cao * mapped. 477f5ab0d1fSMingming Cao * 478e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 479e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 480f5ab0d1fSMingming Cao * based files 481f5ab0d1fSMingming Cao * 482facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 483facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 484facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 485f5ab0d1fSMingming Cao * 486f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 487facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 488facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 489f5ab0d1fSMingming Cao * 490f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 491f5ab0d1fSMingming Cao */ 492e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 493e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4940e855ac8SAneesh Kumar K.V { 495d100eef2SZheng Liu struct extent_status es; 4960e855ac8SAneesh Kumar K.V int retval; 497b8a86845SLukas Czerner int ret = 0; 498921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 499921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 500921f266bSDmitry Monakhov 501921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 502921f266bSDmitry Monakhov #endif 503f5ab0d1fSMingming Cao 504e35fd660STheodore Ts'o map->m_flags = 0; 50570aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 50670aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 507d100eef2SZheng Liu 508e861b5e9STheodore Ts'o /* 509e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 510e861b5e9STheodore Ts'o */ 511e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 512e861b5e9STheodore Ts'o map->m_len = INT_MAX; 513e861b5e9STheodore Ts'o 5144adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5154adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5166a797d27SDarrick J. Wong return -EFSCORRUPTED; 5174adb6ab3SKazuya Mio 518d100eef2SZheng Liu /* Lookup extent status tree firstly */ 5198016e29fSHarshad Shirwadkar if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) && 5208016e29fSHarshad Shirwadkar ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 521d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 522d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 523d100eef2SZheng Liu map->m_lblk - es.es_lblk; 524d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 525d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 526d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 527d100eef2SZheng Liu if (retval > map->m_len) 528d100eef2SZheng Liu retval = map->m_len; 529d100eef2SZheng Liu map->m_len = retval; 530d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 531facab4d9SJan Kara map->m_pblk = 0; 532facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 533facab4d9SJan Kara if (retval > map->m_len) 534facab4d9SJan Kara retval = map->m_len; 535facab4d9SJan Kara map->m_len = retval; 536d100eef2SZheng Liu retval = 0; 537d100eef2SZheng Liu } else { 5381e83bc81SArnd Bergmann BUG(); 539d100eef2SZheng Liu } 540921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 541921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 542921f266bSDmitry Monakhov &orig_map, flags); 543921f266bSDmitry Monakhov #endif 544d100eef2SZheng Liu goto found; 545d100eef2SZheng Liu } 546d100eef2SZheng Liu 5474df3d265SAneesh Kumar K.V /* 548b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 549b920c755STheodore Ts'o * file system block. 5504df3d265SAneesh Kumar K.V */ 551c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 55212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5539e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5544df3d265SAneesh Kumar K.V } else { 5559e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5560e855ac8SAneesh Kumar K.V } 557f7fec032SZheng Liu if (retval > 0) { 5583be78c73STheodore Ts'o unsigned int status; 559f7fec032SZheng Liu 56044fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 56144fb851dSZheng Liu ext4_warning(inode->i_sb, 56244fb851dSZheng Liu "ES len assertion failed for inode " 56344fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 56444fb851dSZheng Liu inode->i_ino, retval, map->m_len); 56544fb851dSZheng Liu WARN_ON(1); 566921f266bSDmitry Monakhov } 567921f266bSDmitry Monakhov 568f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 569f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 570f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 571d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 572ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 573f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 574f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 575f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 576f7fec032SZheng Liu map->m_len, map->m_pblk, status); 577f7fec032SZheng Liu if (ret < 0) 578f7fec032SZheng Liu retval = ret; 579f7fec032SZheng Liu } 5804df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 581f5ab0d1fSMingming Cao 582d100eef2SZheng Liu found: 583e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 584b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5856fd058f7STheodore Ts'o if (ret != 0) 5866fd058f7STheodore Ts'o return ret; 5876fd058f7STheodore Ts'o } 5886fd058f7STheodore Ts'o 589f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 590c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5914df3d265SAneesh Kumar K.V return retval; 5924df3d265SAneesh Kumar K.V 5934df3d265SAneesh Kumar K.V /* 594f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 595f5ab0d1fSMingming Cao * 596f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 597df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 598f5ab0d1fSMingming Cao * with buffer head unmapped. 599f5ab0d1fSMingming Cao */ 600e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 601b8a86845SLukas Czerner /* 602b8a86845SLukas Czerner * If we need to convert extent to unwritten 603b8a86845SLukas Czerner * we continue and do the actual work in 604b8a86845SLukas Czerner * ext4_ext_map_blocks() 605b8a86845SLukas Czerner */ 606b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 607f5ab0d1fSMingming Cao return retval; 608f5ab0d1fSMingming Cao 609f5ab0d1fSMingming Cao /* 610a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 611a25a4e1aSZheng Liu * it will be set again. 6122a8964d6SAneesh Kumar K.V */ 613a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6142a8964d6SAneesh Kumar K.V 6152a8964d6SAneesh Kumar K.V /* 616556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 617f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 618d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 619f5ab0d1fSMingming Cao * with create == 1 flag. 6204df3d265SAneesh Kumar K.V */ 621c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 622d2a17637SMingming Cao 623d2a17637SMingming Cao /* 6244df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6254df3d265SAneesh Kumar K.V * could have changed the inode type in between 6264df3d265SAneesh Kumar K.V */ 62712e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 628e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6290e855ac8SAneesh Kumar K.V } else { 630e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 631267e4db9SAneesh Kumar K.V 632e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 633267e4db9SAneesh Kumar K.V /* 634267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 635267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 636267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 637267e4db9SAneesh Kumar K.V */ 63819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 639267e4db9SAneesh Kumar K.V } 6402ac3b6e0STheodore Ts'o 641d2a17637SMingming Cao /* 6422ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6435f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6445f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6455f634d06SAneesh Kumar K.V * reserve space here. 646d2a17637SMingming Cao */ 6475f634d06SAneesh Kumar K.V if ((retval > 0) && 6481296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6495f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6505f634d06SAneesh Kumar K.V } 651d2a17637SMingming Cao 652f7fec032SZheng Liu if (retval > 0) { 6533be78c73STheodore Ts'o unsigned int status; 654f7fec032SZheng Liu 65544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 65644fb851dSZheng Liu ext4_warning(inode->i_sb, 65744fb851dSZheng Liu "ES len assertion failed for inode " 65844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 65944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 66044fb851dSZheng Liu WARN_ON(1); 661921f266bSDmitry Monakhov } 662921f266bSDmitry Monakhov 663adb23551SZheng Liu /* 664c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 665c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6669b623df6SJan Kara * use them before they are really zeroed. We also have to 6679b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6689b623df6SJan Kara * overwrite zeros with stale data from block device. 669c86d8db3SJan Kara */ 670c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 671c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 672c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 673c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 674c86d8db3SJan Kara map->m_pblk, map->m_len); 675c86d8db3SJan Kara if (ret) { 676c86d8db3SJan Kara retval = ret; 677c86d8db3SJan Kara goto out_sem; 678c86d8db3SJan Kara } 679c86d8db3SJan Kara } 680c86d8db3SJan Kara 681c86d8db3SJan Kara /* 682adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 683adb23551SZheng Liu * extent status tree. 684adb23551SZheng Liu */ 685adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 686bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 687adb23551SZheng Liu if (ext4_es_is_written(&es)) 688c86d8db3SJan Kara goto out_sem; 689adb23551SZheng Liu } 690f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 691f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 692f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 693d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 694ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 695f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 696f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 697f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 698f7fec032SZheng Liu map->m_pblk, status); 699c86d8db3SJan Kara if (ret < 0) { 70051865fdaSZheng Liu retval = ret; 701c86d8db3SJan Kara goto out_sem; 702c86d8db3SJan Kara } 70351865fdaSZheng Liu } 7045356f261SAditya Kali 705c86d8db3SJan Kara out_sem: 7060e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 707e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 708b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7096fd058f7STheodore Ts'o if (ret != 0) 7106fd058f7STheodore Ts'o return ret; 71106bd3c36SJan Kara 71206bd3c36SJan Kara /* 71306bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 71406bd3c36SJan Kara * visible after transaction commit must be on transaction's 71506bd3c36SJan Kara * ordered data list. 71606bd3c36SJan Kara */ 71706bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 71806bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 71906bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 72002749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 72106bd3c36SJan Kara ext4_should_order_data(inode)) { 72273131fbbSRoss Zwisler loff_t start_byte = 72373131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 72473131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 72573131fbbSRoss Zwisler 726ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 72773131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 72873131fbbSRoss Zwisler start_byte, length); 729ee0876bcSJan Kara else 73073131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 73173131fbbSRoss Zwisler start_byte, length); 73206bd3c36SJan Kara if (ret) 73306bd3c36SJan Kara return ret; 73406bd3c36SJan Kara } 735a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, map->m_lblk, 736aa75f4d3SHarshad Shirwadkar map->m_lblk + map->m_len - 1); 7376fd058f7STheodore Ts'o } 738ec8c60beSRitesh Harjani 739ec8c60beSRitesh Harjani if (retval < 0) 74070aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7410e855ac8SAneesh Kumar K.V return retval; 7420e855ac8SAneesh Kumar K.V } 7430e855ac8SAneesh Kumar K.V 744ed8ad838SJan Kara /* 745ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 746ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 747ed8ad838SJan Kara */ 748ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 749ed8ad838SJan Kara { 750ed8ad838SJan Kara unsigned long old_state; 751ed8ad838SJan Kara unsigned long new_state; 752ed8ad838SJan Kara 753ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 754ed8ad838SJan Kara 755ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 756ed8ad838SJan Kara if (!bh->b_page) { 757ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 758ed8ad838SJan Kara return; 759ed8ad838SJan Kara } 760ed8ad838SJan Kara /* 761ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 762ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 763ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 764ed8ad838SJan Kara */ 765ed8ad838SJan Kara do { 766ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 767ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 768ed8ad838SJan Kara } while (unlikely( 769ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 770ed8ad838SJan Kara } 771ed8ad838SJan Kara 7722ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7732ed88685STheodore Ts'o struct buffer_head *bh, int flags) 774ac27a0ecSDave Kleikamp { 7752ed88685STheodore Ts'o struct ext4_map_blocks map; 776efe70c29SJan Kara int ret = 0; 777ac27a0ecSDave Kleikamp 77846c7f254STao Ma if (ext4_has_inline_data(inode)) 77946c7f254STao Ma return -ERANGE; 78046c7f254STao Ma 7812ed88685STheodore Ts'o map.m_lblk = iblock; 7822ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7832ed88685STheodore Ts'o 784efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 785efe70c29SJan Kara flags); 786ac27a0ecSDave Kleikamp if (ret > 0) { 7872ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 788ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7892ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 790ac27a0ecSDave Kleikamp ret = 0; 791547edce3SRoss Zwisler } else if (ret == 0) { 792547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 793547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 794ac27a0ecSDave Kleikamp } 795ac27a0ecSDave Kleikamp return ret; 796ac27a0ecSDave Kleikamp } 797ac27a0ecSDave Kleikamp 7982ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7992ed88685STheodore Ts'o struct buffer_head *bh, int create) 8002ed88685STheodore Ts'o { 8012ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8022ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8032ed88685STheodore Ts'o } 8042ed88685STheodore Ts'o 805ac27a0ecSDave Kleikamp /* 806705965bdSJan Kara * Get block function used when preparing for buffered write if we require 807705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 808705965bdSJan Kara * will be converted to written after the IO is complete. 809705965bdSJan Kara */ 810705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 811705965bdSJan Kara struct buffer_head *bh_result, int create) 812705965bdSJan Kara { 813705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 814705965bdSJan Kara inode->i_ino, create); 815705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 816705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 817705965bdSJan Kara } 818705965bdSJan Kara 819efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 820efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 821efe70c29SJan Kara 822e84dfbe2SJan Kara /* 823ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 824ac27a0ecSDave Kleikamp */ 825617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 826c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 827ac27a0ecSDave Kleikamp { 8282ed88685STheodore Ts'o struct ext4_map_blocks map; 8292ed88685STheodore Ts'o struct buffer_head *bh; 830c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 83110560082STheodore Ts'o int err; 832ac27a0ecSDave Kleikamp 8338016e29fSHarshad Shirwadkar J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8348016e29fSHarshad Shirwadkar || handle != NULL || create == 0); 835ac27a0ecSDave Kleikamp 8362ed88685STheodore Ts'o map.m_lblk = block; 8372ed88685STheodore Ts'o map.m_len = 1; 838c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8392ed88685STheodore Ts'o 84010560082STheodore Ts'o if (err == 0) 84110560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8422ed88685STheodore Ts'o if (err < 0) 84310560082STheodore Ts'o return ERR_PTR(err); 8442ed88685STheodore Ts'o 8452ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 84610560082STheodore Ts'o if (unlikely(!bh)) 84710560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8482ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 849ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 8508016e29fSHarshad Shirwadkar J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) 8518016e29fSHarshad Shirwadkar || (handle != NULL)); 852ac27a0ecSDave Kleikamp 853ac27a0ecSDave Kleikamp /* 854ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 855ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 856ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 857617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 858ac27a0ecSDave Kleikamp * problem. 859ac27a0ecSDave Kleikamp */ 860ac27a0ecSDave Kleikamp lock_buffer(bh); 861ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 86210560082STheodore Ts'o err = ext4_journal_get_create_access(handle, bh); 86310560082STheodore Ts'o if (unlikely(err)) { 86410560082STheodore Ts'o unlock_buffer(bh); 86510560082STheodore Ts'o goto errout; 86610560082STheodore Ts'o } 86710560082STheodore Ts'o if (!buffer_uptodate(bh)) { 868ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 869ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 870ac27a0ecSDave Kleikamp } 871ac27a0ecSDave Kleikamp unlock_buffer(bh); 8720390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8730390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 87410560082STheodore Ts'o if (unlikely(err)) 87510560082STheodore Ts'o goto errout; 87610560082STheodore Ts'o } else 877ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 878ac27a0ecSDave Kleikamp return bh; 87910560082STheodore Ts'o errout: 88010560082STheodore Ts'o brelse(bh); 88110560082STheodore Ts'o return ERR_PTR(err); 882ac27a0ecSDave Kleikamp } 883ac27a0ecSDave Kleikamp 884617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 885c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 886ac27a0ecSDave Kleikamp { 887ac27a0ecSDave Kleikamp struct buffer_head *bh; 8882d069c08Szhangyi (F) int ret; 889ac27a0ecSDave Kleikamp 890c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 8911c215028STheodore Ts'o if (IS_ERR(bh)) 892ac27a0ecSDave Kleikamp return bh; 8937963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 894ac27a0ecSDave Kleikamp return bh; 8952d069c08Szhangyi (F) 8962d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 8972d069c08Szhangyi (F) if (ret) { 898ac27a0ecSDave Kleikamp put_bh(bh); 8992d069c08Szhangyi (F) return ERR_PTR(ret); 9002d069c08Szhangyi (F) } 9012d069c08Szhangyi (F) return bh; 902ac27a0ecSDave Kleikamp } 903ac27a0ecSDave Kleikamp 9049699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 9059699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 9069699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 9079699d4f9STahsin Erdogan { 9089699d4f9STahsin Erdogan int i, err; 9099699d4f9STahsin Erdogan 9109699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9119699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9129699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9139699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9149699d4f9STahsin Erdogan bh_count = i; 9159699d4f9STahsin Erdogan goto out_brelse; 9169699d4f9STahsin Erdogan } 9179699d4f9STahsin Erdogan } 9189699d4f9STahsin Erdogan 9199699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9209699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9212d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9222d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9239699d4f9STahsin Erdogan 9249699d4f9STahsin Erdogan if (!wait) 9259699d4f9STahsin Erdogan return 0; 9269699d4f9STahsin Erdogan 9279699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9289699d4f9STahsin Erdogan if (bhs[i]) 9299699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9309699d4f9STahsin Erdogan 9319699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9329699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9339699d4f9STahsin Erdogan err = -EIO; 9349699d4f9STahsin Erdogan goto out_brelse; 9359699d4f9STahsin Erdogan } 9369699d4f9STahsin Erdogan } 9379699d4f9STahsin Erdogan return 0; 9389699d4f9STahsin Erdogan 9399699d4f9STahsin Erdogan out_brelse: 9409699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9419699d4f9STahsin Erdogan brelse(bhs[i]); 9429699d4f9STahsin Erdogan bhs[i] = NULL; 9439699d4f9STahsin Erdogan } 9449699d4f9STahsin Erdogan return err; 9459699d4f9STahsin Erdogan } 9469699d4f9STahsin Erdogan 947f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 948ac27a0ecSDave Kleikamp struct buffer_head *head, 949ac27a0ecSDave Kleikamp unsigned from, 950ac27a0ecSDave Kleikamp unsigned to, 951ac27a0ecSDave Kleikamp int *partial, 952ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 953ac27a0ecSDave Kleikamp struct buffer_head *bh)) 954ac27a0ecSDave Kleikamp { 955ac27a0ecSDave Kleikamp struct buffer_head *bh; 956ac27a0ecSDave Kleikamp unsigned block_start, block_end; 957ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 958ac27a0ecSDave Kleikamp int err, ret = 0; 959ac27a0ecSDave Kleikamp struct buffer_head *next; 960ac27a0ecSDave Kleikamp 961ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 962ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 963de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 964ac27a0ecSDave Kleikamp next = bh->b_this_page; 965ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 966ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 967ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 968ac27a0ecSDave Kleikamp *partial = 1; 969ac27a0ecSDave Kleikamp continue; 970ac27a0ecSDave Kleikamp } 971ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 972ac27a0ecSDave Kleikamp if (!ret) 973ac27a0ecSDave Kleikamp ret = err; 974ac27a0ecSDave Kleikamp } 975ac27a0ecSDave Kleikamp return ret; 976ac27a0ecSDave Kleikamp } 977ac27a0ecSDave Kleikamp 978ac27a0ecSDave Kleikamp /* 979ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 980ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 981617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 982dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 983ac27a0ecSDave Kleikamp * prepare_write() is the right place. 984ac27a0ecSDave Kleikamp * 98536ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 98636ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 98736ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 98836ade451SJan Kara * because the caller may be PF_MEMALLOC. 989ac27a0ecSDave Kleikamp * 990617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 991ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 992ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 993ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 994ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 995ac27a0ecSDave Kleikamp * violation. 996ac27a0ecSDave Kleikamp * 997dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 998ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 999ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1000ac27a0ecSDave Kleikamp * write. 1001ac27a0ecSDave Kleikamp */ 1002f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 1003ac27a0ecSDave Kleikamp struct buffer_head *bh) 1004ac27a0ecSDave Kleikamp { 100556d35a4cSJan Kara int dirty = buffer_dirty(bh); 100656d35a4cSJan Kara int ret; 100756d35a4cSJan Kara 1008ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1009ac27a0ecSDave Kleikamp return 0; 101056d35a4cSJan Kara /* 1011ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 101256d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 101356d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1014ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 101556d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 101656d35a4cSJan Kara * ever write the buffer. 101756d35a4cSJan Kara */ 101856d35a4cSJan Kara if (dirty) 101956d35a4cSJan Kara clear_buffer_dirty(bh); 10205d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 102156d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 102256d35a4cSJan Kara if (!ret && dirty) 102356d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 102456d35a4cSJan Kara return ret; 1025ac27a0ecSDave Kleikamp } 1026ac27a0ecSDave Kleikamp 1027643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10282058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10292058f83aSMichael Halcrow get_block_t *get_block) 10302058f83aSMichael Halcrow { 103109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10322058f83aSMichael Halcrow unsigned to = from + len; 10332058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10342058f83aSMichael Halcrow unsigned block_start, block_end; 10352058f83aSMichael Halcrow sector_t block; 10362058f83aSMichael Halcrow int err = 0; 10372058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10382058f83aSMichael Halcrow unsigned bbits; 10390b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10400b578f35SChandan Rajendra int nr_wait = 0; 10410b578f35SChandan Rajendra int i; 10422058f83aSMichael Halcrow 10432058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 104409cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 104509cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10462058f83aSMichael Halcrow BUG_ON(from > to); 10472058f83aSMichael Halcrow 10482058f83aSMichael Halcrow if (!page_has_buffers(page)) 10492058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10502058f83aSMichael Halcrow head = page_buffers(page); 10512058f83aSMichael Halcrow bbits = ilog2(blocksize); 105209cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10532058f83aSMichael Halcrow 10542058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10552058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10562058f83aSMichael Halcrow block_end = block_start + blocksize; 10572058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10582058f83aSMichael Halcrow if (PageUptodate(page)) { 10592058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10602058f83aSMichael Halcrow set_buffer_uptodate(bh); 10612058f83aSMichael Halcrow } 10622058f83aSMichael Halcrow continue; 10632058f83aSMichael Halcrow } 10642058f83aSMichael Halcrow if (buffer_new(bh)) 10652058f83aSMichael Halcrow clear_buffer_new(bh); 10662058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10672058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10682058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10692058f83aSMichael Halcrow if (err) 10702058f83aSMichael Halcrow break; 10712058f83aSMichael Halcrow if (buffer_new(bh)) { 10722058f83aSMichael Halcrow if (PageUptodate(page)) { 10732058f83aSMichael Halcrow clear_buffer_new(bh); 10742058f83aSMichael Halcrow set_buffer_uptodate(bh); 10752058f83aSMichael Halcrow mark_buffer_dirty(bh); 10762058f83aSMichael Halcrow continue; 10772058f83aSMichael Halcrow } 10782058f83aSMichael Halcrow if (block_end > to || block_start < from) 10792058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10802058f83aSMichael Halcrow block_start, from); 10812058f83aSMichael Halcrow continue; 10822058f83aSMichael Halcrow } 10832058f83aSMichael Halcrow } 10842058f83aSMichael Halcrow if (PageUptodate(page)) { 10852058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10862058f83aSMichael Halcrow set_buffer_uptodate(bh); 10872058f83aSMichael Halcrow continue; 10882058f83aSMichael Halcrow } 10892058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10902058f83aSMichael Halcrow !buffer_unwritten(bh) && 10912058f83aSMichael Halcrow (block_start < from || block_end > to)) { 10922d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 10930b578f35SChandan Rajendra wait[nr_wait++] = bh; 10942058f83aSMichael Halcrow } 10952058f83aSMichael Halcrow } 10962058f83aSMichael Halcrow /* 10972058f83aSMichael Halcrow * If we issued read requests, let them complete. 10982058f83aSMichael Halcrow */ 10990b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11000b578f35SChandan Rajendra wait_on_buffer(wait[i]); 11010b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 11022058f83aSMichael Halcrow err = -EIO; 11032058f83aSMichael Halcrow } 11047e0785fcSChandan Rajendra if (unlikely(err)) { 11052058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 11064f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11070b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11080b578f35SChandan Rajendra int err2; 11090b578f35SChandan Rajendra 11100b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 11110b578f35SChandan Rajendra bh_offset(wait[i])); 11120b578f35SChandan Rajendra if (err2) { 11130b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11140b578f35SChandan Rajendra err = err2; 11150b578f35SChandan Rajendra } 11160b578f35SChandan Rajendra } 11177e0785fcSChandan Rajendra } 11187e0785fcSChandan Rajendra 11192058f83aSMichael Halcrow return err; 11202058f83aSMichael Halcrow } 11212058f83aSMichael Halcrow #endif 11222058f83aSMichael Halcrow 1123bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1124bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1125bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1126ac27a0ecSDave Kleikamp { 1127bfc1af65SNick Piggin struct inode *inode = mapping->host; 11281938a150SAneesh Kumar K.V int ret, needed_blocks; 1129ac27a0ecSDave Kleikamp handle_t *handle; 1130ac27a0ecSDave Kleikamp int retries = 0; 1131bfc1af65SNick Piggin struct page *page; 1132bfc1af65SNick Piggin pgoff_t index; 1133bfc1af65SNick Piggin unsigned from, to; 1134bfc1af65SNick Piggin 11350db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11360db1ff22STheodore Ts'o return -EIO; 11370db1ff22STheodore Ts'o 11389bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11391938a150SAneesh Kumar K.V /* 11401938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11411938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11421938a150SAneesh Kumar K.V */ 11431938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 114409cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 114509cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1146bfc1af65SNick Piggin to = from + len; 1147ac27a0ecSDave Kleikamp 1148f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1149f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1150f19d5870STao Ma flags, pagep); 1151f19d5870STao Ma if (ret < 0) 115247564bfbSTheodore Ts'o return ret; 115347564bfbSTheodore Ts'o if (ret == 1) 115447564bfbSTheodore Ts'o return 0; 1155f19d5870STao Ma } 1156f19d5870STao Ma 115747564bfbSTheodore Ts'o /* 115847564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 115947564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 116047564bfbSTheodore Ts'o * is being written back. So grab it first before we start 116147564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 116247564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 116347564bfbSTheodore Ts'o */ 116447564bfbSTheodore Ts'o retry_grab: 116554566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 116647564bfbSTheodore Ts'o if (!page) 116747564bfbSTheodore Ts'o return -ENOMEM; 116847564bfbSTheodore Ts'o unlock_page(page); 116947564bfbSTheodore Ts'o 117047564bfbSTheodore Ts'o retry_journal: 11719924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1172ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 117309cbfeafSKirill A. Shutemov put_page(page); 117447564bfbSTheodore Ts'o return PTR_ERR(handle); 1175cf108bcaSJan Kara } 1176f19d5870STao Ma 117747564bfbSTheodore Ts'o lock_page(page); 117847564bfbSTheodore Ts'o if (page->mapping != mapping) { 117947564bfbSTheodore Ts'o /* The page got truncated from under us */ 118047564bfbSTheodore Ts'o unlock_page(page); 118109cbfeafSKirill A. Shutemov put_page(page); 1182cf108bcaSJan Kara ext4_journal_stop(handle); 118347564bfbSTheodore Ts'o goto retry_grab; 1184cf108bcaSJan Kara } 11857afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11867afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1187cf108bcaSJan Kara 1188643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11892058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11902058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1191705965bdSJan Kara ext4_get_block_unwritten); 11922058f83aSMichael Halcrow else 11932058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 11942058f83aSMichael Halcrow ext4_get_block); 11952058f83aSMichael Halcrow #else 1196744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1197705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1198705965bdSJan Kara ext4_get_block_unwritten); 1199744692dcSJiaying Zhang else 12006e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 12012058f83aSMichael Halcrow #endif 1202bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1203f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1204f19d5870STao Ma from, to, NULL, 1205f19d5870STao Ma do_journal_get_write_access); 1206b46be050SAndrey Savochkin } 1207bfc1af65SNick Piggin 1208bfc1af65SNick Piggin if (ret) { 1209c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1210c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1211c93d8f88SEric Biggers 1212bfc1af65SNick Piggin unlock_page(page); 1213ae4d5372SAneesh Kumar K.V /* 12146e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1215ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1216ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12171938a150SAneesh Kumar K.V * 12181938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12191938a150SAneesh Kumar K.V * truncate finishes 1220ae4d5372SAneesh Kumar K.V */ 1221c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12221938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12231938a150SAneesh Kumar K.V 12241938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1225c93d8f88SEric Biggers if (extended) { 1226b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12271938a150SAneesh Kumar K.V /* 1228ffacfa7aSJan Kara * If truncate failed early the inode might 12291938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12301938a150SAneesh Kumar K.V * make sure the inode is removed from the 12311938a150SAneesh Kumar K.V * orphan list in that case. 12321938a150SAneesh Kumar K.V */ 12331938a150SAneesh Kumar K.V if (inode->i_nlink) 12341938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12351938a150SAneesh Kumar K.V } 1236bfc1af65SNick Piggin 123747564bfbSTheodore Ts'o if (ret == -ENOSPC && 123847564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 123947564bfbSTheodore Ts'o goto retry_journal; 124009cbfeafSKirill A. Shutemov put_page(page); 124147564bfbSTheodore Ts'o return ret; 124247564bfbSTheodore Ts'o } 124347564bfbSTheodore Ts'o *pagep = page; 1244ac27a0ecSDave Kleikamp return ret; 1245ac27a0ecSDave Kleikamp } 1246ac27a0ecSDave Kleikamp 1247bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1248bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1249ac27a0ecSDave Kleikamp { 125013fca323STheodore Ts'o int ret; 1251ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1252ac27a0ecSDave Kleikamp return 0; 1253ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 125413fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 125513fca323STheodore Ts'o clear_buffer_meta(bh); 125613fca323STheodore Ts'o clear_buffer_prio(bh); 125713fca323STheodore Ts'o return ret; 1258ac27a0ecSDave Kleikamp } 1259ac27a0ecSDave Kleikamp 1260eed4333fSZheng Liu /* 1261eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1262eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1263eed4333fSZheng Liu * 1264eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1265eed4333fSZheng Liu * buffers are managed internally. 1266eed4333fSZheng Liu */ 1267eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1268f8514083SAneesh Kumar K.V struct address_space *mapping, 1269f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1270f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1271f8514083SAneesh Kumar K.V { 1272f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1273eed4333fSZheng Liu struct inode *inode = mapping->host; 12740572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1275eed4333fSZheng Liu int ret = 0, ret2; 1276eed4333fSZheng Liu int i_size_changed = 0; 1277362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1278c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1279eed4333fSZheng Liu 1280eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1281362eca70STheodore Ts'o if (inline_data) { 128242c832deSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 1283f19d5870STao Ma copied, page); 1284eb5efbcbSTheodore Ts'o if (ret < 0) { 1285eb5efbcbSTheodore Ts'o unlock_page(page); 1286eb5efbcbSTheodore Ts'o put_page(page); 128742c832deSTheodore Ts'o goto errout; 1288eb5efbcbSTheodore Ts'o } 128942c832deSTheodore Ts'o copied = ret; 129042c832deSTheodore Ts'o } else 1291f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1292f19d5870STao Ma len, copied, page, fsdata); 1293f8514083SAneesh Kumar K.V /* 12944631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1295f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1296c93d8f88SEric Biggers * 1297c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1298c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1299f8514083SAneesh Kumar K.V */ 1300c93d8f88SEric Biggers if (!verity) 13014631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1302f8514083SAneesh Kumar K.V unlock_page(page); 130309cbfeafSKirill A. Shutemov put_page(page); 1304f8514083SAneesh Kumar K.V 1305c93d8f88SEric Biggers if (old_size < pos && !verity) 13060572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1307f8514083SAneesh Kumar K.V /* 1308f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1309f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1310f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1311f8514083SAneesh Kumar K.V * filesystems. 1312f8514083SAneesh Kumar K.V */ 1313362eca70STheodore Ts'o if (i_size_changed || inline_data) 13144209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1315f8514083SAneesh Kumar K.V 1316c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1317f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1318f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1319f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1320f8514083SAneesh Kumar K.V */ 1321f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 132274d553aaSTheodore Ts'o errout: 1323617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1324ac27a0ecSDave Kleikamp if (!ret) 1325ac27a0ecSDave Kleikamp ret = ret2; 1326bfc1af65SNick Piggin 1327c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1328b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1329f8514083SAneesh Kumar K.V /* 1330ffacfa7aSJan Kara * If truncate failed early the inode might still be 1331f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1332f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1333f8514083SAneesh Kumar K.V */ 1334f8514083SAneesh Kumar K.V if (inode->i_nlink) 1335f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1336f8514083SAneesh Kumar K.V } 1337f8514083SAneesh Kumar K.V 1338bfc1af65SNick Piggin return ret ? ret : copied; 1339ac27a0ecSDave Kleikamp } 1340ac27a0ecSDave Kleikamp 1341b90197b6STheodore Ts'o /* 1342b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1343b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1344b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1345b90197b6STheodore Ts'o */ 13463b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 13473b136499SJan Kara struct page *page, 13483b136499SJan Kara unsigned from, unsigned to) 1349b90197b6STheodore Ts'o { 1350b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1351b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1352b90197b6STheodore Ts'o 1353b90197b6STheodore Ts'o bh = head = page_buffers(page); 1354b90197b6STheodore Ts'o do { 1355b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1356b90197b6STheodore Ts'o if (buffer_new(bh)) { 1357b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1358b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1359b90197b6STheodore Ts'o unsigned start, size; 1360b90197b6STheodore Ts'o 1361b90197b6STheodore Ts'o start = max(from, block_start); 1362b90197b6STheodore Ts'o size = min(to, block_end) - start; 1363b90197b6STheodore Ts'o 1364b90197b6STheodore Ts'o zero_user(page, start, size); 13653b136499SJan Kara write_end_fn(handle, bh); 1366b90197b6STheodore Ts'o } 1367b90197b6STheodore Ts'o clear_buffer_new(bh); 1368b90197b6STheodore Ts'o } 1369b90197b6STheodore Ts'o } 1370b90197b6STheodore Ts'o block_start = block_end; 1371b90197b6STheodore Ts'o bh = bh->b_this_page; 1372b90197b6STheodore Ts'o } while (bh != head); 1373b90197b6STheodore Ts'o } 1374b90197b6STheodore Ts'o 1375bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1376bfc1af65SNick Piggin struct address_space *mapping, 1377bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1378bfc1af65SNick Piggin struct page *page, void *fsdata) 1379ac27a0ecSDave Kleikamp { 1380617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1381bfc1af65SNick Piggin struct inode *inode = mapping->host; 13820572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1383ac27a0ecSDave Kleikamp int ret = 0, ret2; 1384ac27a0ecSDave Kleikamp int partial = 0; 1385bfc1af65SNick Piggin unsigned from, to; 13864631dbf6SDmitry Monakhov int size_changed = 0; 1387362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1388c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1389ac27a0ecSDave Kleikamp 13909bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 139109cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1392bfc1af65SNick Piggin to = from + len; 1393bfc1af65SNick Piggin 1394441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1395441c8508SCurt Wohlgemuth 1396362eca70STheodore Ts'o if (inline_data) { 1397eb5efbcbSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 13983fdcfb66STao Ma copied, page); 1399eb5efbcbSTheodore Ts'o if (ret < 0) { 1400eb5efbcbSTheodore Ts'o unlock_page(page); 1401eb5efbcbSTheodore Ts'o put_page(page); 1402eb5efbcbSTheodore Ts'o goto errout; 1403eb5efbcbSTheodore Ts'o } 1404eb5efbcbSTheodore Ts'o copied = ret; 1405eb5efbcbSTheodore Ts'o } else if (unlikely(copied < len) && !PageUptodate(page)) { 1406bfc1af65SNick Piggin copied = 0; 14073b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, from, to); 14083b136499SJan Kara } else { 14093b136499SJan Kara if (unlikely(copied < len)) 14103b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, 14113b136499SJan Kara from + copied, to); 1412f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 14133b136499SJan Kara from + copied, &partial, 14143b136499SJan Kara write_end_fn); 1415ac27a0ecSDave Kleikamp if (!partial) 1416ac27a0ecSDave Kleikamp SetPageUptodate(page); 14173fdcfb66STao Ma } 1418c93d8f88SEric Biggers if (!verity) 14194631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 142019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14212d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14224631dbf6SDmitry Monakhov unlock_page(page); 142309cbfeafSKirill A. Shutemov put_page(page); 14244631dbf6SDmitry Monakhov 1425c93d8f88SEric Biggers if (old_size < pos && !verity) 14260572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14270572639fSXiaoguang Wang 1428362eca70STheodore Ts'o if (size_changed || inline_data) { 1429617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1430ac27a0ecSDave Kleikamp if (!ret) 1431ac27a0ecSDave Kleikamp ret = ret2; 1432ac27a0ecSDave Kleikamp } 1433bfc1af65SNick Piggin 1434c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1435f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1436f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1437f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1438f8514083SAneesh Kumar K.V */ 1439f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1440f8514083SAneesh Kumar K.V 1441eb5efbcbSTheodore Ts'o errout: 1442617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1443ac27a0ecSDave Kleikamp if (!ret) 1444ac27a0ecSDave Kleikamp ret = ret2; 1445c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1446b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1447f8514083SAneesh Kumar K.V /* 1448ffacfa7aSJan Kara * If truncate failed early the inode might still be 1449f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1450f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1451f8514083SAneesh Kumar K.V */ 1452f8514083SAneesh Kumar K.V if (inode->i_nlink) 1453f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1454f8514083SAneesh Kumar K.V } 1455bfc1af65SNick Piggin 1456bfc1af65SNick Piggin return ret ? ret : copied; 1457ac27a0ecSDave Kleikamp } 1458d2a17637SMingming Cao 14599d0be502STheodore Ts'o /* 1460c27e43a1SEric Whitney * Reserve space for a single cluster 14619d0be502STheodore Ts'o */ 1462c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1463d2a17637SMingming Cao { 1464d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14650637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14665dd4056dSChristoph Hellwig int ret; 1467d2a17637SMingming Cao 146860e58e0fSMingming Cao /* 146972b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 147072b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 147172b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 147260e58e0fSMingming Cao */ 14737b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14745dd4056dSChristoph Hellwig if (ret) 14755dd4056dSChristoph Hellwig return ret; 147603179fe9STheodore Ts'o 147703179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 147871d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 147903179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 148003179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1481d2a17637SMingming Cao return -ENOSPC; 1482d2a17637SMingming Cao } 14839d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1484c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14850637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 148639bc680aSDmitry Monakhov 1487d2a17637SMingming Cao return 0; /* success */ 1488d2a17637SMingming Cao } 1489d2a17637SMingming Cao 1490f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1491d2a17637SMingming Cao { 1492d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14930637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1494d2a17637SMingming Cao 1495cd213226SMingming Cao if (!to_free) 1496cd213226SMingming Cao return; /* Nothing to release, exit */ 1497cd213226SMingming Cao 1498d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1499cd213226SMingming Cao 15005a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 15010637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1502cd213226SMingming Cao /* 15030637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 15040637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 15050637c6f4STheodore Ts'o * function is called from invalidate page, it's 15060637c6f4STheodore Ts'o * harmless to return without any action. 1507cd213226SMingming Cao */ 15088de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15090637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15101084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15110637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15120637c6f4STheodore Ts'o WARN_ON(1); 15130637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15140637c6f4STheodore Ts'o } 15150637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15160637c6f4STheodore Ts'o 151772b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 151857042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1519d2a17637SMingming Cao 1520d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 152160e58e0fSMingming Cao 15227b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1523d2a17637SMingming Cao } 1524d2a17637SMingming Cao 1525ac27a0ecSDave Kleikamp /* 152664769240SAlex Tomas * Delayed allocation stuff 152764769240SAlex Tomas */ 152864769240SAlex Tomas 15294e7ea81dSJan Kara struct mpage_da_data { 15304e7ea81dSJan Kara struct inode *inode; 15314e7ea81dSJan Kara struct writeback_control *wbc; 15326b523df4SJan Kara 15334e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15344e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15354e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 153664769240SAlex Tomas /* 15374e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15384e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15394e7ea81dSJan Kara * is delalloc or unwritten. 154064769240SAlex Tomas */ 15414e7ea81dSJan Kara struct ext4_map_blocks map; 15424e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1543dddbd6acSJan Kara unsigned int do_map:1; 15446b8ed620SJan Kara unsigned int scanned_until_end:1; 15454e7ea81dSJan Kara }; 154664769240SAlex Tomas 15474e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15484e7ea81dSJan Kara bool invalidate) 1549c4a0c46eSAneesh Kumar K.V { 1550c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1551c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1552c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1553c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1554c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15554e7ea81dSJan Kara 15564e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15574e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15584e7ea81dSJan Kara return; 1559c4a0c46eSAneesh Kumar K.V 15606b8ed620SJan Kara mpd->scanned_until_end = 0; 1561c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1562c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15634e7ea81dSJan Kara if (invalidate) { 15644e7ea81dSJan Kara ext4_lblk_t start, last; 156509cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 156609cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 156751865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15684e7ea81dSJan Kara } 156951865fdaSZheng Liu 157086679820SMel Gorman pagevec_init(&pvec); 1571c4a0c46eSAneesh Kumar K.V while (index <= end) { 1572397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1573c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1574c4a0c46eSAneesh Kumar K.V break; 1575c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1576c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15772b85a617SJan Kara 1578c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1579c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15804e7ea81dSJan Kara if (invalidate) { 15814e800c03Swangguang if (page_mapped(page)) 15824e800c03Swangguang clear_page_dirty_for_io(page); 158309cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1584c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15854e7ea81dSJan Kara } 1586c4a0c46eSAneesh Kumar K.V unlock_page(page); 1587c4a0c46eSAneesh Kumar K.V } 15889b1d0998SJan Kara pagevec_release(&pvec); 1589c4a0c46eSAneesh Kumar K.V } 1590c4a0c46eSAneesh Kumar K.V } 1591c4a0c46eSAneesh Kumar K.V 1592df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1593df22291fSAneesh Kumar K.V { 1594df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 159592b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1596f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 159792b97816STheodore Ts'o 159892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15995dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1600f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 160192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 160292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1603f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 160457042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 160592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1606f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16077b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 160892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 160992b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1610f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1611df22291fSAneesh Kumar K.V return; 1612df22291fSAneesh Kumar K.V } 1613df22291fSAneesh Kumar K.V 1614c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 161529fa89d0SAneesh Kumar K.V { 1616c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 161729fa89d0SAneesh Kumar K.V } 161829fa89d0SAneesh Kumar K.V 161964769240SAlex Tomas /* 16200b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16210b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16220b02f4c0SEric Whitney * count or making a pending reservation 16230b02f4c0SEric Whitney * where needed 16240b02f4c0SEric Whitney * 16250b02f4c0SEric Whitney * @inode - file containing the newly added block 16260b02f4c0SEric Whitney * @lblk - logical block to be added 16270b02f4c0SEric Whitney * 16280b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16290b02f4c0SEric Whitney */ 16300b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16310b02f4c0SEric Whitney { 16320b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16330b02f4c0SEric Whitney int ret; 16340b02f4c0SEric Whitney bool allocated = false; 16350b02f4c0SEric Whitney 16360b02f4c0SEric Whitney /* 16370b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16380b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16390b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16400b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16410b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16420b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16430b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16440b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16450b02f4c0SEric Whitney * extents status tree doesn't get a match. 16460b02f4c0SEric Whitney */ 16470b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16480b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16490b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16500b02f4c0SEric Whitney goto errout; 16510b02f4c0SEric Whitney } else { /* bigalloc */ 16520b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16530b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16540b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16550b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16560b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16570b02f4c0SEric Whitney if (ret < 0) 16580b02f4c0SEric Whitney goto errout; 16590b02f4c0SEric Whitney if (ret == 0) { 16600b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16610b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16620b02f4c0SEric Whitney goto errout; 16630b02f4c0SEric Whitney } else { 16640b02f4c0SEric Whitney allocated = true; 16650b02f4c0SEric Whitney } 16660b02f4c0SEric Whitney } else { 16670b02f4c0SEric Whitney allocated = true; 16680b02f4c0SEric Whitney } 16690b02f4c0SEric Whitney } 16700b02f4c0SEric Whitney } 16710b02f4c0SEric Whitney 16720b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16730b02f4c0SEric Whitney 16740b02f4c0SEric Whitney errout: 16750b02f4c0SEric Whitney return ret; 16760b02f4c0SEric Whitney } 16770b02f4c0SEric Whitney 16780b02f4c0SEric Whitney /* 16795356f261SAditya Kali * This function is grabs code from the very beginning of 16805356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16815356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16825356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16835356f261SAditya Kali */ 16845356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16855356f261SAditya Kali struct ext4_map_blocks *map, 16865356f261SAditya Kali struct buffer_head *bh) 16875356f261SAditya Kali { 1688d100eef2SZheng Liu struct extent_status es; 16895356f261SAditya Kali int retval; 16905356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1691921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1692921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1693921f266bSDmitry Monakhov 1694921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1695921f266bSDmitry Monakhov #endif 16965356f261SAditya Kali 16975356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16985356f261SAditya Kali invalid_block = ~0; 16995356f261SAditya Kali 17005356f261SAditya Kali map->m_flags = 0; 170170aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 17025356f261SAditya Kali (unsigned long) map->m_lblk); 1703d100eef2SZheng Liu 1704d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1705bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1706d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1707d100eef2SZheng Liu retval = 0; 1708c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1709d100eef2SZheng Liu goto add_delayed; 1710d100eef2SZheng Liu } 1711d100eef2SZheng Liu 1712d100eef2SZheng Liu /* 1713d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1714d100eef2SZheng Liu * So we need to check it. 1715d100eef2SZheng Liu */ 1716d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1717d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1718d100eef2SZheng Liu set_buffer_new(bh); 1719d100eef2SZheng Liu set_buffer_delay(bh); 1720d100eef2SZheng Liu return 0; 1721d100eef2SZheng Liu } 1722d100eef2SZheng Liu 1723d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1724d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1725d100eef2SZheng Liu if (retval > map->m_len) 1726d100eef2SZheng Liu retval = map->m_len; 1727d100eef2SZheng Liu map->m_len = retval; 1728d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1729d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1730d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1731d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1732d100eef2SZheng Liu else 17331e83bc81SArnd Bergmann BUG(); 1734d100eef2SZheng Liu 1735921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1736921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1737921f266bSDmitry Monakhov #endif 1738d100eef2SZheng Liu return retval; 1739d100eef2SZheng Liu } 1740d100eef2SZheng Liu 17415356f261SAditya Kali /* 17425356f261SAditya Kali * Try to see if we can get the block without requesting a new 17435356f261SAditya Kali * file system block. 17445356f261SAditya Kali */ 1745c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1746cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17479c3569b5STao Ma retval = 0; 1748cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17492f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17505356f261SAditya Kali else 17512f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17525356f261SAditya Kali 1753d100eef2SZheng Liu add_delayed: 17545356f261SAditya Kali if (retval == 0) { 1755f7fec032SZheng Liu int ret; 1756ad431025SEric Whitney 17575356f261SAditya Kali /* 17585356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17595356f261SAditya Kali * is it OK? 17605356f261SAditya Kali */ 17615356f261SAditya Kali 17620b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17630b02f4c0SEric Whitney if (ret != 0) { 1764f7fec032SZheng Liu retval = ret; 176551865fdaSZheng Liu goto out_unlock; 1766f7fec032SZheng Liu } 176751865fdaSZheng Liu 17685356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17695356f261SAditya Kali set_buffer_new(bh); 17705356f261SAditya Kali set_buffer_delay(bh); 1771f7fec032SZheng Liu } else if (retval > 0) { 1772f7fec032SZheng Liu int ret; 17733be78c73STheodore Ts'o unsigned int status; 1774f7fec032SZheng Liu 177544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 177644fb851dSZheng Liu ext4_warning(inode->i_sb, 177744fb851dSZheng Liu "ES len assertion failed for inode " 177844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 177944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 178044fb851dSZheng Liu WARN_ON(1); 1781921f266bSDmitry Monakhov } 1782921f266bSDmitry Monakhov 1783f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1784f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1785f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1786f7fec032SZheng Liu map->m_pblk, status); 1787f7fec032SZheng Liu if (ret != 0) 1788f7fec032SZheng Liu retval = ret; 17895356f261SAditya Kali } 17905356f261SAditya Kali 17915356f261SAditya Kali out_unlock: 17925356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17935356f261SAditya Kali 17945356f261SAditya Kali return retval; 17955356f261SAditya Kali } 17965356f261SAditya Kali 17975356f261SAditya Kali /* 1798d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1799b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1800b920c755STheodore Ts'o * reserve space for a single block. 180129fa89d0SAneesh Kumar K.V * 180229fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 180329fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 180429fa89d0SAneesh Kumar K.V * 180529fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 180629fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 180729fa89d0SAneesh Kumar K.V * initialized properly. 180864769240SAlex Tomas */ 18099c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18102ed88685STheodore Ts'o struct buffer_head *bh, int create) 181164769240SAlex Tomas { 18122ed88685STheodore Ts'o struct ext4_map_blocks map; 181364769240SAlex Tomas int ret = 0; 181464769240SAlex Tomas 181564769240SAlex Tomas BUG_ON(create == 0); 18162ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18172ed88685STheodore Ts'o 18182ed88685STheodore Ts'o map.m_lblk = iblock; 18192ed88685STheodore Ts'o map.m_len = 1; 182064769240SAlex Tomas 182164769240SAlex Tomas /* 182264769240SAlex Tomas * first, we need to know whether the block is allocated already 182364769240SAlex Tomas * preallocated blocks are unmapped but should treated 182464769240SAlex Tomas * the same as allocated blocks. 182564769240SAlex Tomas */ 18265356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18275356f261SAditya Kali if (ret <= 0) 18282ed88685STheodore Ts'o return ret; 182964769240SAlex Tomas 18302ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1831ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18322ed88685STheodore Ts'o 18332ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18342ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18352ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18362ed88685STheodore Ts'o * get_block multiple times when we write to the same 18372ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18382ed88685STheodore Ts'o * for partial write. 18392ed88685STheodore Ts'o */ 18402ed88685STheodore Ts'o set_buffer_new(bh); 1841c8205636STheodore Ts'o set_buffer_mapped(bh); 18422ed88685STheodore Ts'o } 18432ed88685STheodore Ts'o return 0; 184464769240SAlex Tomas } 184561628a3fSMingming Cao 184662e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 184762e086beSAneesh Kumar K.V { 184862e086beSAneesh Kumar K.V get_bh(bh); 184962e086beSAneesh Kumar K.V return 0; 185062e086beSAneesh Kumar K.V } 185162e086beSAneesh Kumar K.V 185262e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 185362e086beSAneesh Kumar K.V { 185462e086beSAneesh Kumar K.V put_bh(bh); 185562e086beSAneesh Kumar K.V return 0; 185662e086beSAneesh Kumar K.V } 185762e086beSAneesh Kumar K.V 185862e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 185962e086beSAneesh Kumar K.V unsigned int len) 186062e086beSAneesh Kumar K.V { 186162e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 186262e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18633fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 186462e086beSAneesh Kumar K.V handle_t *handle = NULL; 18653fdcfb66STao Ma int ret = 0, err = 0; 18663fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18673fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 186862e086beSAneesh Kumar K.V 1869cb20d518STheodore Ts'o ClearPageChecked(page); 18703fdcfb66STao Ma 18713fdcfb66STao Ma if (inline_data) { 18723fdcfb66STao Ma BUG_ON(page->index != 0); 18733fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18743fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18753fdcfb66STao Ma if (inode_bh == NULL) 18763fdcfb66STao Ma goto out; 18773fdcfb66STao Ma } else { 187862e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18793fdcfb66STao Ma if (!page_bufs) { 18803fdcfb66STao Ma BUG(); 18813fdcfb66STao Ma goto out; 18823fdcfb66STao Ma } 18833fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18843fdcfb66STao Ma NULL, bget_one); 18853fdcfb66STao Ma } 1886bdf96838STheodore Ts'o /* 1887bdf96838STheodore Ts'o * We need to release the page lock before we start the 1888bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1889bdf96838STheodore Ts'o * out from under us. 1890bdf96838STheodore Ts'o */ 1891bdf96838STheodore Ts'o get_page(page); 189262e086beSAneesh Kumar K.V unlock_page(page); 189362e086beSAneesh Kumar K.V 18949924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18959924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 189662e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 189762e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1898bdf96838STheodore Ts'o put_page(page); 1899bdf96838STheodore Ts'o goto out_no_pagelock; 1900bdf96838STheodore Ts'o } 1901bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1902bdf96838STheodore Ts'o 1903bdf96838STheodore Ts'o lock_page(page); 1904bdf96838STheodore Ts'o put_page(page); 1905bdf96838STheodore Ts'o if (page->mapping != mapping) { 1906bdf96838STheodore Ts'o /* The page got truncated from under us */ 1907bdf96838STheodore Ts'o ext4_journal_stop(handle); 1908bdf96838STheodore Ts'o ret = 0; 190962e086beSAneesh Kumar K.V goto out; 191062e086beSAneesh Kumar K.V } 191162e086beSAneesh Kumar K.V 19123fdcfb66STao Ma if (inline_data) { 1913362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19143fdcfb66STao Ma } else { 1915f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 191662e086beSAneesh Kumar K.V do_journal_get_write_access); 191762e086beSAneesh Kumar K.V 1918f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 191962e086beSAneesh Kumar K.V write_end_fn); 19203fdcfb66STao Ma } 192162e086beSAneesh Kumar K.V if (ret == 0) 192262e086beSAneesh Kumar K.V ret = err; 1923b5b18160SJan Kara err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len); 1924afb585a9SMauricio Faria de Oliveira if (ret == 0) 1925afb585a9SMauricio Faria de Oliveira ret = err; 19262d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 192762e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 192862e086beSAneesh Kumar K.V if (!ret) 192962e086beSAneesh Kumar K.V ret = err; 193062e086beSAneesh Kumar K.V 19313fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19328c9367fdSTheodore Ts'o ext4_walk_page_buffers(NULL, page_bufs, 0, len, 19333fdcfb66STao Ma NULL, bput_one); 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: 19383fdcfb66STao Ma brelse(inode_bh); 193962e086beSAneesh Kumar K.V return ret; 194062e086beSAneesh Kumar K.V } 194162e086beSAneesh Kumar K.V 194261628a3fSMingming Cao /* 194343ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 194443ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 194543ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 194643ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 194743ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 194843ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 194943ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 195043ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 195143ce1d23SAneesh Kumar K.V * 1952b920c755STheodore Ts'o * This function can get called via... 195320970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1954b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1955f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1956b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 195743ce1d23SAneesh Kumar K.V * 195843ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 195943ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 196043ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 196143ce1d23SAneesh Kumar K.V * truncate(f, 1024); 196243ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 196343ce1d23SAneesh Kumar K.V * a[0] = 'a'; 196443ce1d23SAneesh Kumar K.V * truncate(f, 4096); 196543ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 196690802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 196743ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 196843ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 196943ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 197043ce1d23SAneesh Kumar K.V * buffer_heads mapped. 197143ce1d23SAneesh Kumar K.V * 197243ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 197343ce1d23SAneesh Kumar K.V * unwritten in the page. 197443ce1d23SAneesh Kumar K.V * 197543ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 197643ce1d23SAneesh Kumar K.V * 197743ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 197843ce1d23SAneesh Kumar K.V * ext4_writepage() 197943ce1d23SAneesh Kumar K.V * 198043ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 198143ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 198261628a3fSMingming Cao */ 198343ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 198464769240SAlex Tomas struct writeback_control *wbc) 198564769240SAlex Tomas { 1986f8bec370SJan Kara int ret = 0; 198761628a3fSMingming Cao loff_t size; 1988498e5f24STheodore Ts'o unsigned int len; 1989744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 199061628a3fSMingming Cao struct inode *inode = page->mapping->host; 199136ade451SJan Kara struct ext4_io_submit io_submit; 19921c8349a1SNamjae Jeon bool keep_towrite = false; 199364769240SAlex Tomas 19940db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 1995c2a559bcSyangerkun inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 19960db1ff22STheodore Ts'o unlock_page(page); 19970db1ff22STheodore Ts'o return -EIO; 19980db1ff22STheodore Ts'o } 19990db1ff22STheodore Ts'o 2000a9c667f8SLukas Czerner trace_ext4_writepage(page); 200161628a3fSMingming Cao size = i_size_read(inode); 2002c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2003c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 200409cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 200561628a3fSMingming Cao else 200609cbfeafSKirill A. Shutemov len = PAGE_SIZE; 200761628a3fSMingming Cao 2008f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 200964769240SAlex Tomas /* 2010fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2011fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2012fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2013fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2014fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2015cccd147aSTheodore Ts'o * 2016cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2017cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2018cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2019cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2020cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2021cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2022cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2023cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2024cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 202564769240SAlex Tomas */ 2026f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2027c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 202861628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2029cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 203009cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2031fe386132SJan Kara /* 2032fe386132SJan Kara * For memory cleaning there's no point in writing only 2033fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2034fe386132SJan Kara * from direct reclaim. 2035fe386132SJan Kara */ 2036fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2037fe386132SJan Kara == PF_MEMALLOC); 203861628a3fSMingming Cao unlock_page(page); 203961628a3fSMingming Cao return 0; 204061628a3fSMingming Cao } 20411c8349a1SNamjae Jeon keep_towrite = true; 2042f0e6c985SAneesh Kumar K.V } 204364769240SAlex Tomas 2044cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 204543ce1d23SAneesh Kumar K.V /* 204643ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 204743ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 204843ce1d23SAneesh Kumar K.V */ 20493f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 205043ce1d23SAneesh Kumar K.V 205197a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 205297a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 205397a851edSJan Kara if (!io_submit.io_end) { 205497a851edSJan Kara redirty_page_for_writepage(wbc, page); 205597a851edSJan Kara unlock_page(page); 205697a851edSJan Kara return -ENOMEM; 205797a851edSJan Kara } 20581c8349a1SNamjae Jeon ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite); 205936ade451SJan Kara ext4_io_submit(&io_submit); 206097a851edSJan Kara /* Drop io_end reference we got from init */ 206197a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 206264769240SAlex Tomas return ret; 206364769240SAlex Tomas } 206464769240SAlex Tomas 20655f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20665f1132b2SJan Kara { 20675f1132b2SJan Kara int len; 2068a056bdaaSJan Kara loff_t size; 20695f1132b2SJan Kara int err; 20705f1132b2SJan Kara 20715f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2072a056bdaaSJan Kara clear_page_dirty_for_io(page); 2073a056bdaaSJan Kara /* 2074a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2075a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2076a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2077a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2078a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2079a056bdaaSJan Kara * written to again until we release page lock. So only after 2080a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2081a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2082a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2083a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2084a056bdaaSJan Kara * after page tables are updated. 2085a056bdaaSJan Kara */ 2086a056bdaaSJan Kara size = i_size_read(mpd->inode); 2087c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2088c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 208909cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20905f1132b2SJan Kara else 209109cbfeafSKirill A. Shutemov len = PAGE_SIZE; 20921c8349a1SNamjae Jeon err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false); 20935f1132b2SJan Kara if (!err) 20945f1132b2SJan Kara mpd->wbc->nr_to_write--; 20955f1132b2SJan Kara mpd->first_page++; 20965f1132b2SJan Kara 20975f1132b2SJan Kara return err; 20985f1132b2SJan Kara } 20995f1132b2SJan Kara 21006db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 21014e7ea81dSJan Kara 210261628a3fSMingming Cao /* 2103fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2104fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2105fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 210661628a3fSMingming Cao */ 2107fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2108525f4ed8SMingming Cao 2109525f4ed8SMingming Cao /* 21104e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 21114e7ea81dSJan Kara * 21124e7ea81dSJan Kara * @mpd - extent of blocks 21134e7ea81dSJan Kara * @lblk - logical number of the block in the file 211409930042SJan Kara * @bh - buffer head we want to add to the extent 21154e7ea81dSJan Kara * 211609930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 211709930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 211809930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 211909930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 212009930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 212109930042SJan Kara * added. 21224e7ea81dSJan Kara */ 212309930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 212409930042SJan Kara struct buffer_head *bh) 21254e7ea81dSJan Kara { 21264e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21274e7ea81dSJan Kara 212809930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 212909930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 213009930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 213109930042SJan Kara /* So far no extent to map => we write the buffer right away */ 213209930042SJan Kara if (map->m_len == 0) 213309930042SJan Kara return true; 213409930042SJan Kara return false; 213509930042SJan Kara } 21364e7ea81dSJan Kara 21374e7ea81dSJan Kara /* First block in the extent? */ 21384e7ea81dSJan Kara if (map->m_len == 0) { 2139dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2140dddbd6acSJan Kara if (!mpd->do_map) 2141dddbd6acSJan Kara return false; 21424e7ea81dSJan Kara map->m_lblk = lblk; 21434e7ea81dSJan Kara map->m_len = 1; 214409930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 214509930042SJan Kara return true; 21464e7ea81dSJan Kara } 21474e7ea81dSJan Kara 214809930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 214909930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 215009930042SJan Kara return false; 215109930042SJan Kara 21524e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21534e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 215409930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21554e7ea81dSJan Kara map->m_len++; 215609930042SJan Kara return true; 21574e7ea81dSJan Kara } 215809930042SJan Kara return false; 21594e7ea81dSJan Kara } 21604e7ea81dSJan Kara 21615f1132b2SJan Kara /* 21625f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21635f1132b2SJan Kara * 21645f1132b2SJan Kara * @mpd - extent of blocks for mapping 21655f1132b2SJan Kara * @head - the first buffer in the page 21665f1132b2SJan Kara * @bh - buffer we should start processing from 21675f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21685f1132b2SJan Kara * 21695f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21705f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21715f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21725f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21735f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21745f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21755f1132b2SJan Kara * < 0 in case of error during IO submission. 21765f1132b2SJan Kara */ 21775f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21784e7ea81dSJan Kara struct buffer_head *head, 21794e7ea81dSJan Kara struct buffer_head *bh, 21804e7ea81dSJan Kara ext4_lblk_t lblk) 21814e7ea81dSJan Kara { 21824e7ea81dSJan Kara struct inode *inode = mpd->inode; 21835f1132b2SJan Kara int err; 218493407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21854e7ea81dSJan Kara >> inode->i_blkbits; 21864e7ea81dSJan Kara 2187c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2188c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2189c93d8f88SEric Biggers 21904e7ea81dSJan Kara do { 21914e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21924e7ea81dSJan Kara 219309930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21944e7ea81dSJan Kara /* Found extent to map? */ 21954e7ea81dSJan Kara if (mpd->map.m_len) 21965f1132b2SJan Kara return 0; 2197dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2198dddbd6acSJan Kara if (!mpd->do_map) 2199dddbd6acSJan Kara return 0; 220009930042SJan Kara /* Everything mapped so far and we hit EOF */ 22015f1132b2SJan Kara break; 22024e7ea81dSJan Kara } 22034e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 22045f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 22055f1132b2SJan Kara if (mpd->map.m_len == 0) { 22065f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 22075f1132b2SJan Kara if (err < 0) 22084e7ea81dSJan Kara return err; 22094e7ea81dSJan Kara } 22106b8ed620SJan Kara if (lblk >= blocks) { 22116b8ed620SJan Kara mpd->scanned_until_end = 1; 22126b8ed620SJan Kara return 0; 22136b8ed620SJan Kara } 22146b8ed620SJan Kara return 1; 22155f1132b2SJan Kara } 22164e7ea81dSJan Kara 22174e7ea81dSJan Kara /* 22182943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22192943fdbcSRitesh Harjani * may submit fully mapped page for IO 22202943fdbcSRitesh Harjani * 22212943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22222943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22232943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22242943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22252943fdbcSRitesh Harjani * mapping or not. 22262943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22272943fdbcSRitesh Harjani * state according to new extent state. 22282943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22292943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22302943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22312943fdbcSRitesh Harjani */ 22322943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22332943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22342943fdbcSRitesh Harjani bool *map_bh) 22352943fdbcSRitesh Harjani { 22362943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22372943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22382943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22392943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22402943fdbcSRitesh Harjani int err = 0; 2241c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2242c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2243c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22442943fdbcSRitesh Harjani 22452943fdbcSRitesh Harjani bh = head = page_buffers(page); 22462943fdbcSRitesh Harjani do { 22472943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22482943fdbcSRitesh Harjani continue; 22492943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22502943fdbcSRitesh Harjani /* 22512943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22522943fdbcSRitesh Harjani * Find next buffer in the page to map. 22532943fdbcSRitesh Harjani */ 22542943fdbcSRitesh Harjani mpd->map.m_len = 0; 22552943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2256c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2257c8cc8816SRitesh Harjani io_end_size = 0; 22582943fdbcSRitesh Harjani 22592943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22602943fdbcSRitesh Harjani if (err > 0) 22612943fdbcSRitesh Harjani err = 0; 2262c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2263c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22644d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22654d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22664d06bfb9SRitesh Harjani goto out; 22674d06bfb9SRitesh Harjani } 2268d1e18b88SRitesh Harjani io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits; 2269c8cc8816SRitesh Harjani } 22702943fdbcSRitesh Harjani *map_bh = true; 22712943fdbcSRitesh Harjani goto out; 22722943fdbcSRitesh Harjani } 22732943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22742943fdbcSRitesh Harjani clear_buffer_delay(bh); 22752943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22762943fdbcSRitesh Harjani } 22772943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2278c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22792943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2280c8cc8816SRitesh Harjani 2281c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2282c8cc8816SRitesh Harjani io_end_size = 0; 22832943fdbcSRitesh Harjani *map_bh = false; 22842943fdbcSRitesh Harjani out: 22852943fdbcSRitesh Harjani *m_lblk = lblk; 22862943fdbcSRitesh Harjani *m_pblk = pblock; 22872943fdbcSRitesh Harjani return err; 22882943fdbcSRitesh Harjani } 22892943fdbcSRitesh Harjani 22902943fdbcSRitesh Harjani /* 22914e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22924e7ea81dSJan Kara * submit fully mapped pages for IO 22934e7ea81dSJan Kara * 22944e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22954e7ea81dSJan Kara * 22964e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22974e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22984e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2299556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 23004e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 23014e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 23024e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 23034e7ea81dSJan Kara */ 23044e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 23054e7ea81dSJan Kara { 23064e7ea81dSJan Kara struct pagevec pvec; 23074e7ea81dSJan Kara int nr_pages, i; 23084e7ea81dSJan Kara struct inode *inode = mpd->inode; 230909cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 23104e7ea81dSJan Kara pgoff_t start, end; 23114e7ea81dSJan Kara ext4_lblk_t lblk; 23122943fdbcSRitesh Harjani ext4_fsblk_t pblock; 23134e7ea81dSJan Kara int err; 23142943fdbcSRitesh Harjani bool map_bh = false; 23154e7ea81dSJan Kara 23164e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23174e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23184e7ea81dSJan Kara lblk = start << bpp_bits; 23194e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23204e7ea81dSJan Kara 232186679820SMel Gorman pagevec_init(&pvec); 23224e7ea81dSJan Kara while (start <= end) { 23232b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2324397162ffSJan Kara &start, end); 23254e7ea81dSJan Kara if (nr_pages == 0) 23264e7ea81dSJan Kara break; 23274e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23284e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23294e7ea81dSJan Kara 23302943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23312943fdbcSRitesh Harjani &map_bh); 23324e7ea81dSJan Kara /* 23332943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23342943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23352943fdbcSRitesh Harjani * So we return to call further extent mapping. 23364e7ea81dSJan Kara */ 233739c0ae16SJason Yan if (err < 0 || map_bh) 23382943fdbcSRitesh Harjani goto out; 23394e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23404e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23412943fdbcSRitesh Harjani if (err < 0) 23422943fdbcSRitesh Harjani goto out; 23434e7ea81dSJan Kara } 23444e7ea81dSJan Kara pagevec_release(&pvec); 23454e7ea81dSJan Kara } 23464e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23474e7ea81dSJan Kara mpd->map.m_len = 0; 23484e7ea81dSJan Kara mpd->map.m_flags = 0; 23494e7ea81dSJan Kara return 0; 23502943fdbcSRitesh Harjani out: 23512943fdbcSRitesh Harjani pagevec_release(&pvec); 23522943fdbcSRitesh Harjani return err; 23534e7ea81dSJan Kara } 23544e7ea81dSJan Kara 23554e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23564e7ea81dSJan Kara { 23574e7ea81dSJan Kara struct inode *inode = mpd->inode; 23584e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23594e7ea81dSJan Kara int get_blocks_flags; 2360090f32eeSLukas Czerner int err, dioread_nolock; 23614e7ea81dSJan Kara 23624e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23634e7ea81dSJan Kara /* 23644e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2365556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23664e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23674e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23684e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23694e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23704e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23714e7ea81dSJan Kara * possible. 23724e7ea81dSJan Kara * 2373754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2374754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2375754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2376754cfed6STheodore Ts'o * the data was copied into the page cache. 23774e7ea81dSJan Kara */ 23784e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2379ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2380ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2381090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2382090f32eeSLukas Czerner if (dioread_nolock) 23834e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23846db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 23854e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23864e7ea81dSJan Kara 23874e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23884e7ea81dSJan Kara if (err < 0) 23894e7ea81dSJan Kara return err; 2390090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23916b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23926b523df4SJan Kara ext4_handle_valid(handle)) { 23936b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23946b523df4SJan Kara handle->h_rsv_handle = NULL; 23956b523df4SJan Kara } 23963613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23976b523df4SJan Kara } 23984e7ea81dSJan Kara 23994e7ea81dSJan Kara BUG_ON(map->m_len == 0); 24004e7ea81dSJan Kara return 0; 24014e7ea81dSJan Kara } 24024e7ea81dSJan Kara 24034e7ea81dSJan Kara /* 24044e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 24054e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 24064e7ea81dSJan Kara * 24074e7ea81dSJan Kara * @handle - handle for journal operations 24084e7ea81dSJan Kara * @mpd - extent to map 24097534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 24107534e854SJan Kara * is no hope of writing the data. The caller should discard 24117534e854SJan Kara * dirty pages to avoid infinite loops. 24124e7ea81dSJan Kara * 24134e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 24144e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 24154e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 24164e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24174e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24184e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24194e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24204e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24214e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24224e7ea81dSJan Kara */ 24234e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2424cb530541STheodore Ts'o struct mpage_da_data *mpd, 2425cb530541STheodore Ts'o bool *give_up_on_write) 24264e7ea81dSJan Kara { 24274e7ea81dSJan Kara struct inode *inode = mpd->inode; 24284e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24294e7ea81dSJan Kara int err; 24304e7ea81dSJan Kara loff_t disksize; 24316603120eSDmitry Monakhov int progress = 0; 2432c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24334d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24344e7ea81dSJan Kara 24354d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24364d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24374d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2438c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 243927d7c4edSJan Kara do { 24404e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24414e7ea81dSJan Kara if (err < 0) { 24424e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24434e7ea81dSJan Kara 24440db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 2445*9b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) 2446cb530541STheodore Ts'o goto invalidate_dirty_pages; 24474e7ea81dSJan Kara /* 2448cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2449cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2450cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24514e7ea81dSJan Kara */ 2452cb530541STheodore Ts'o if ((err == -ENOMEM) || 24536603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24546603120eSDmitry Monakhov if (progress) 24556603120eSDmitry Monakhov goto update_disksize; 2456cb530541STheodore Ts'o return err; 24576603120eSDmitry Monakhov } 24584e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24594e7ea81dSJan Kara "Delayed block allocation failed for " 24604e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24614e7ea81dSJan Kara " max blocks %u with error %d", 24624e7ea81dSJan Kara inode->i_ino, 24634e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2464cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24654e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24664e7ea81dSJan Kara "This should not happen!! Data will " 24674e7ea81dSJan Kara "be lost\n"); 24684e7ea81dSJan Kara if (err == -ENOSPC) 24694e7ea81dSJan Kara ext4_print_free_blocks(inode); 2470cb530541STheodore Ts'o invalidate_dirty_pages: 2471cb530541STheodore Ts'o *give_up_on_write = true; 24724e7ea81dSJan Kara return err; 24734e7ea81dSJan Kara } 24746603120eSDmitry Monakhov progress = 1; 24754e7ea81dSJan Kara /* 24764e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24774e7ea81dSJan Kara * extent to map 24784e7ea81dSJan Kara */ 24794e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24804e7ea81dSJan Kara if (err < 0) 24816603120eSDmitry Monakhov goto update_disksize; 248227d7c4edSJan Kara } while (map->m_len); 24834e7ea81dSJan Kara 24846603120eSDmitry Monakhov update_disksize: 2485622cad13STheodore Ts'o /* 2486622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2487622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2488622cad13STheodore Ts'o */ 248909cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 249035df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 24914e7ea81dSJan Kara int err2; 2492622cad13STheodore Ts'o loff_t i_size; 24934e7ea81dSJan Kara 2494622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2495622cad13STheodore Ts'o i_size = i_size_read(inode); 2496622cad13STheodore Ts'o if (disksize > i_size) 2497622cad13STheodore Ts'o disksize = i_size; 2498622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2499622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2500622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2501b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2502878520acSTheodore Ts'o if (err2) { 250354d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 25044e7ea81dSJan Kara "Failed to mark inode %lu dirty", 25054e7ea81dSJan Kara inode->i_ino); 2506878520acSTheodore Ts'o } 25074e7ea81dSJan Kara if (!err) 25084e7ea81dSJan Kara err = err2; 25094e7ea81dSJan Kara } 25104e7ea81dSJan Kara return err; 25114e7ea81dSJan Kara } 25124e7ea81dSJan Kara 25134e7ea81dSJan Kara /* 2514fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 251520970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2516fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2517fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2518fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2519525f4ed8SMingming Cao */ 2520fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2521fffb2739SJan Kara { 2522fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2523525f4ed8SMingming Cao 2524fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2525fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2526525f4ed8SMingming Cao } 252761628a3fSMingming Cao 25288e48dcfbSTheodore Ts'o /* 25294e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25304e7ea81dSJan Kara * and underlying extent to map 25314e7ea81dSJan Kara * 25324e7ea81dSJan Kara * @mpd - where to look for pages 25334e7ea81dSJan Kara * 25344e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25354e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25364e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25374e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25384e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25394e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25404e7ea81dSJan Kara * 25414e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25424e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25434e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25444e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25458e48dcfbSTheodore Ts'o */ 25464e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25478e48dcfbSTheodore Ts'o { 25484e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25498e48dcfbSTheodore Ts'o struct pagevec pvec; 25504f01b02cSTheodore Ts'o unsigned int nr_pages; 2551aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25524e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25534e7ea81dSJan Kara pgoff_t end = mpd->last_page; 255410bbd235SMatthew Wilcox xa_mark_t tag; 25554e7ea81dSJan Kara int i, err = 0; 25564e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25574e7ea81dSJan Kara ext4_lblk_t lblk; 25584e7ea81dSJan Kara struct buffer_head *head; 25598e48dcfbSTheodore Ts'o 25604e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25615b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25625b41d924SEric Sandeen else 25635b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25645b41d924SEric Sandeen 256586679820SMel Gorman pagevec_init(&pvec); 25664e7ea81dSJan Kara mpd->map.m_len = 0; 25674e7ea81dSJan Kara mpd->next_page = index; 25684f01b02cSTheodore Ts'o while (index <= end) { 2569dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 257067fd707fSJan Kara tag); 25718e48dcfbSTheodore Ts'o if (nr_pages == 0) 25726b8ed620SJan Kara break; 25738e48dcfbSTheodore Ts'o 25748e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25758e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25768e48dcfbSTheodore Ts'o 25778e48dcfbSTheodore Ts'o /* 2578aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2579aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2580aeac589aSMing Lei * keep going because someone may be concurrently 2581aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2582aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2583aeac589aSMing Lei * of the old dirty pages. 2584aeac589aSMing Lei */ 2585aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2586aeac589aSMing Lei goto out; 2587aeac589aSMing Lei 25884e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25894e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25904e7ea81dSJan Kara goto out; 259178aaced3STheodore Ts'o 25928e48dcfbSTheodore Ts'o lock_page(page); 25938e48dcfbSTheodore Ts'o /* 25944e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25954e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25964e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25974e7ea81dSJan Kara * page is already under writeback and we are not doing 25984e7ea81dSJan Kara * a data integrity writeback, skip the page 25998e48dcfbSTheodore Ts'o */ 26004f01b02cSTheodore Ts'o if (!PageDirty(page) || 26014f01b02cSTheodore Ts'o (PageWriteback(page) && 26024e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 26034f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 26048e48dcfbSTheodore Ts'o unlock_page(page); 26058e48dcfbSTheodore Ts'o continue; 26068e48dcfbSTheodore Ts'o } 26078e48dcfbSTheodore Ts'o 26088e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 26098e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 26108e48dcfbSTheodore Ts'o 26114e7ea81dSJan Kara if (mpd->map.m_len == 0) 26128eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 26138eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2614f8bec370SJan Kara /* Add all dirty buffers to mpd */ 26154e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 261609cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 26178eb9e5ceSTheodore Ts'o head = page_buffers(page); 26185f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26195f1132b2SJan Kara if (err <= 0) 26204e7ea81dSJan Kara goto out; 26215f1132b2SJan Kara err = 0; 2622aeac589aSMing Lei left--; 26238e48dcfbSTheodore Ts'o } 26248e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26258e48dcfbSTheodore Ts'o cond_resched(); 26268e48dcfbSTheodore Ts'o } 26276b8ed620SJan Kara mpd->scanned_until_end = 1; 26284f01b02cSTheodore Ts'o return 0; 26298eb9e5ceSTheodore Ts'o out: 26308eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26314e7ea81dSJan Kara return err; 26328e48dcfbSTheodore Ts'o } 26338e48dcfbSTheodore Ts'o 263420970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 263564769240SAlex Tomas struct writeback_control *wbc) 263664769240SAlex Tomas { 26374e7ea81dSJan Kara pgoff_t writeback_index = 0; 26384e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 263922208dedSAneesh Kumar K.V int range_whole = 0; 26404e7ea81dSJan Kara int cycled = 1; 264161628a3fSMingming Cao handle_t *handle = NULL; 2642df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26435e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26446b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26455e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26461bce63d1SShaohua Li struct blk_plug plug; 2647cb530541STheodore Ts'o bool give_up_on_write = false; 264861628a3fSMingming Cao 26490db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26500db1ff22STheodore Ts'o return -EIO; 26510db1ff22STheodore Ts'o 2652bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 265320970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2654ba80b101STheodore Ts'o 265561628a3fSMingming Cao /* 265661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 265761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 265861628a3fSMingming Cao * because that could violate lock ordering on umount 265961628a3fSMingming Cao */ 2660a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2661bbf023c7SMing Lei goto out_writepages; 26622a21e37eSTheodore Ts'o 266320970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2664043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2665bbf023c7SMing Lei goto out_writepages; 266620970ba6STheodore Ts'o } 266720970ba6STheodore Ts'o 26682a21e37eSTheodore Ts'o /* 26692a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26702a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26712a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26721751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26732a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 267420970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26752a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26762a21e37eSTheodore Ts'o * the stack trace. 26772a21e37eSTheodore Ts'o */ 26780db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 2679*9b5f6c9bSHarshad Shirwadkar ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) { 2680bbf023c7SMing Lei ret = -EROFS; 2681bbf023c7SMing Lei goto out_writepages; 2682bbf023c7SMing Lei } 26832a21e37eSTheodore Ts'o 26844e7ea81dSJan Kara /* 26854e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26864e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26874e7ea81dSJan Kara * we'd better clear the inline data here. 26884e7ea81dSJan Kara */ 26894e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26904e7ea81dSJan Kara /* Just inode will be modified... */ 26914e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26924e7ea81dSJan Kara if (IS_ERR(handle)) { 26934e7ea81dSJan Kara ret = PTR_ERR(handle); 26944e7ea81dSJan Kara goto out_writepages; 26954e7ea81dSJan Kara } 26964e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26974e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26984e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26994e7ea81dSJan Kara ext4_journal_stop(handle); 27004e7ea81dSJan Kara } 27014e7ea81dSJan Kara 27024e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 27034e343231Syangerkun /* 27044e343231Syangerkun * We may need to convert up to one extent per block in 27054e343231Syangerkun * the page and we may dirty the inode. 27064e343231Syangerkun */ 27074e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 27084e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 27094e343231Syangerkun } 27104e343231Syangerkun 271122208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 271222208dedSAneesh Kumar K.V range_whole = 1; 271361628a3fSMingming Cao 27142acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 27154e7ea81dSJan Kara writeback_index = mapping->writeback_index; 27164e7ea81dSJan Kara if (writeback_index) 27172acf2c26SAneesh Kumar K.V cycled = 0; 27184e7ea81dSJan Kara mpd.first_page = writeback_index; 27194e7ea81dSJan Kara mpd.last_page = -1; 27205b41d924SEric Sandeen } else { 272109cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 272209cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27235b41d924SEric Sandeen } 2724a1d6cc56SAneesh Kumar K.V 27254e7ea81dSJan Kara mpd.inode = inode; 27264e7ea81dSJan Kara mpd.wbc = wbc; 27274e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27282acf2c26SAneesh Kumar K.V retry: 27296e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27304e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27311bce63d1SShaohua Li blk_start_plug(&plug); 2732dddbd6acSJan Kara 2733dddbd6acSJan Kara /* 2734dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2735dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2736dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2737dddbd6acSJan Kara * started. 2738dddbd6acSJan Kara */ 2739dddbd6acSJan Kara mpd.do_map = 0; 27406b8ed620SJan Kara mpd.scanned_until_end = 0; 2741dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2742dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2743dddbd6acSJan Kara ret = -ENOMEM; 2744dddbd6acSJan Kara goto unplug; 2745dddbd6acSJan Kara } 2746dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2747a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2748a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2749dddbd6acSJan Kara /* Submit prepared bio */ 2750dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2751dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2752dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2753dddbd6acSJan Kara if (ret < 0) 2754dddbd6acSJan Kara goto unplug; 2755dddbd6acSJan Kara 27566b8ed620SJan Kara while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { 27574e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27584e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27594e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27604e7ea81dSJan Kara ret = -ENOMEM; 27614e7ea81dSJan Kara break; 27624e7ea81dSJan Kara } 2763a1d6cc56SAneesh Kumar K.V 2764a1d6cc56SAneesh Kumar K.V /* 27654e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27664e7ea81dSJan Kara * must always write out whole page (makes a difference when 27674e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27684e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27694e7ea81dSJan Kara * not supported by delalloc. 2770a1d6cc56SAneesh Kumar K.V */ 2771a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2772525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2773a1d6cc56SAneesh Kumar K.V 277461628a3fSMingming Cao /* start a new transaction */ 27756b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27766b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 277761628a3fSMingming Cao if (IS_ERR(handle)) { 277861628a3fSMingming Cao ret = PTR_ERR(handle); 27791693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2780fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2781a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27824e7ea81dSJan Kara /* Release allocated io_end */ 27834e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2784dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27854e7ea81dSJan Kara break; 278661628a3fSMingming Cao } 2787dddbd6acSJan Kara mpd.do_map = 1; 2788f63e6005STheodore Ts'o 27894e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27904e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27916b8ed620SJan Kara if (!ret && mpd.map.m_len) 2792cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2793cb530541STheodore Ts'o &give_up_on_write); 2794646caa9cSJan Kara /* 2795646caa9cSJan Kara * Caution: If the handle is synchronous, 2796646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2797646caa9cSJan Kara * to finish which may depend on writeback of pages to 2798646caa9cSJan Kara * complete or on page lock to be released. In that 2799b483bb77SRandy Dunlap * case, we have to wait until after we have 2800646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2801646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2802646caa9cSJan Kara * to be able to complete) before stopping the handle. 2803646caa9cSJan Kara */ 2804646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 280561628a3fSMingming Cao ext4_journal_stop(handle); 2806646caa9cSJan Kara handle = NULL; 2807dddbd6acSJan Kara mpd.do_map = 0; 2808646caa9cSJan Kara } 28094e7ea81dSJan Kara /* Unlock pages we didn't use */ 2810cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2811a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2812a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2813a297b2fcSXiaoguang Wang 2814646caa9cSJan Kara /* 2815646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2816646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2817646caa9cSJan Kara * we are still holding the transaction as we can 2818646caa9cSJan Kara * release the last reference to io_end which may end 2819646caa9cSJan Kara * up doing unwritten extent conversion. 2820646caa9cSJan Kara */ 2821646caa9cSJan Kara if (handle) { 2822646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2823646caa9cSJan Kara ext4_journal_stop(handle); 2824646caa9cSJan Kara } else 28254e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2826dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2827df22291fSAneesh Kumar K.V 28284e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28294e7ea81dSJan Kara /* 28304e7ea81dSJan Kara * Commit the transaction which would 283122208dedSAneesh Kumar K.V * free blocks released in the transaction 283222208dedSAneesh Kumar K.V * and try again 283322208dedSAneesh Kumar K.V */ 2834df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 283522208dedSAneesh Kumar K.V ret = 0; 28364e7ea81dSJan Kara continue; 28374e7ea81dSJan Kara } 28384e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28394e7ea81dSJan Kara if (ret) 284061628a3fSMingming Cao break; 284161628a3fSMingming Cao } 2842dddbd6acSJan Kara unplug: 28431bce63d1SShaohua Li blk_finish_plug(&plug); 28449c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28452acf2c26SAneesh Kumar K.V cycled = 1; 28464e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28474e7ea81dSJan Kara mpd.first_page = 0; 28482acf2c26SAneesh Kumar K.V goto retry; 28492acf2c26SAneesh Kumar K.V } 285061628a3fSMingming Cao 285122208dedSAneesh Kumar K.V /* Update index */ 285222208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 285322208dedSAneesh Kumar K.V /* 28544e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 285522208dedSAneesh Kumar K.V * mode will write it back later 285622208dedSAneesh Kumar K.V */ 28574e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2858a1d6cc56SAneesh Kumar K.V 285961628a3fSMingming Cao out_writepages: 286020970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28614e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2862bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 286361628a3fSMingming Cao return ret; 286464769240SAlex Tomas } 286564769240SAlex Tomas 28665f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28675f0663bbSDan Williams struct writeback_control *wbc) 28685f0663bbSDan Williams { 28695f0663bbSDan Williams int ret; 28705f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28715f0663bbSDan Williams struct inode *inode = mapping->host; 28725f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28735f0663bbSDan Williams 28745f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28755f0663bbSDan Williams return -EIO; 28765f0663bbSDan Williams 2877bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28785f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28795f0663bbSDan Williams 28803f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28815f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28825f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2883bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28845f0663bbSDan Williams return ret; 28855f0663bbSDan Williams } 28865f0663bbSDan Williams 288779f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 288879f0be8dSAneesh Kumar K.V { 28895c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 289079f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 289179f0be8dSAneesh Kumar K.V 289279f0be8dSAneesh Kumar K.V /* 289379f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 289479f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2895179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 289679f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 289779f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 289879f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 289979f0be8dSAneesh Kumar K.V */ 29005c1ff336SEric Whitney free_clusters = 29015c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 29025c1ff336SEric Whitney dirty_clusters = 29035c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 290400d4e736STheodore Ts'o /* 290500d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 290600d4e736STheodore Ts'o */ 29075c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 290810ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 290900d4e736STheodore Ts'o 29105c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29115c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 291279f0be8dSAneesh Kumar K.V /* 2913c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2914c8afb446SEric Sandeen * or free blocks is less than watermark 291579f0be8dSAneesh Kumar K.V */ 291679f0be8dSAneesh Kumar K.V return 1; 291779f0be8dSAneesh Kumar K.V } 291879f0be8dSAneesh Kumar K.V return 0; 291979f0be8dSAneesh Kumar K.V } 292079f0be8dSAneesh Kumar K.V 29210ff8947fSEric Sandeen /* We always reserve for an inode update; the superblock could be there too */ 29220ff8947fSEric Sandeen static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) 29230ff8947fSEric Sandeen { 2924e2b911c5SDarrick J. Wong if (likely(ext4_has_feature_large_file(inode->i_sb))) 29250ff8947fSEric Sandeen return 1; 29260ff8947fSEric Sandeen 29270ff8947fSEric Sandeen if (pos + len <= 0x7fffffffULL) 29280ff8947fSEric Sandeen return 1; 29290ff8947fSEric Sandeen 29300ff8947fSEric Sandeen /* We might need to update the superblock to set LARGE_FILE */ 29310ff8947fSEric Sandeen return 2; 29320ff8947fSEric Sandeen } 29330ff8947fSEric Sandeen 293464769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 293564769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 293664769240SAlex Tomas struct page **pagep, void **fsdata) 293764769240SAlex Tomas { 293872b8ab9dSEric Sandeen int ret, retries = 0; 293964769240SAlex Tomas struct page *page; 294064769240SAlex Tomas pgoff_t index; 294164769240SAlex Tomas struct inode *inode = mapping->host; 294264769240SAlex Tomas handle_t *handle; 294364769240SAlex Tomas 29440db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29450db1ff22STheodore Ts'o return -EIO; 29460db1ff22STheodore Ts'o 294709cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 294879f0be8dSAneesh Kumar K.V 2949c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2950c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 295179f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 295279f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 295379f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 295479f0be8dSAneesh Kumar K.V } 295579f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29569bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29579c3569b5STao Ma 29589c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29599c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29609c3569b5STao Ma pos, len, flags, 29619c3569b5STao Ma pagep, fsdata); 29629c3569b5STao Ma if (ret < 0) 296347564bfbSTheodore Ts'o return ret; 296447564bfbSTheodore Ts'o if (ret == 1) 296547564bfbSTheodore Ts'o return 0; 29669c3569b5STao Ma } 29679c3569b5STao Ma 296847564bfbSTheodore Ts'o /* 296947564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 297047564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 297147564bfbSTheodore Ts'o * is being written back. So grab it first before we start 297247564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 297347564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 297447564bfbSTheodore Ts'o */ 297547564bfbSTheodore Ts'o retry_grab: 297647564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 297747564bfbSTheodore Ts'o if (!page) 297847564bfbSTheodore Ts'o return -ENOMEM; 297947564bfbSTheodore Ts'o unlock_page(page); 298047564bfbSTheodore Ts'o 298164769240SAlex Tomas /* 298264769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 298364769240SAlex Tomas * if there is delayed block allocation. But we still need 298464769240SAlex Tomas * to journalling the i_disksize update if writes to the end 298564769240SAlex Tomas * of file which has an already mapped buffer. 298664769240SAlex Tomas */ 298747564bfbSTheodore Ts'o retry_journal: 29880ff8947fSEric Sandeen handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 29890ff8947fSEric Sandeen ext4_da_write_credits(inode, pos, len)); 299064769240SAlex Tomas if (IS_ERR(handle)) { 299109cbfeafSKirill A. Shutemov put_page(page); 299247564bfbSTheodore Ts'o return PTR_ERR(handle); 299364769240SAlex Tomas } 299464769240SAlex Tomas 299547564bfbSTheodore Ts'o lock_page(page); 299647564bfbSTheodore Ts'o if (page->mapping != mapping) { 299747564bfbSTheodore Ts'o /* The page got truncated from under us */ 299847564bfbSTheodore Ts'o unlock_page(page); 299909cbfeafSKirill A. Shutemov put_page(page); 3000d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 300147564bfbSTheodore Ts'o goto retry_grab; 3002d5a0d4f7SEric Sandeen } 300347564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 30047afe5aa5SDmitry Monakhov wait_for_stable_page(page); 300564769240SAlex Tomas 3006643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 30072058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 30082058f83aSMichael Halcrow ext4_da_get_block_prep); 30092058f83aSMichael Halcrow #else 30106e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 30112058f83aSMichael Halcrow #endif 301264769240SAlex Tomas if (ret < 0) { 301364769240SAlex Tomas unlock_page(page); 301464769240SAlex Tomas ext4_journal_stop(handle); 3015ae4d5372SAneesh Kumar K.V /* 3016ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3017ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3018ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3019ae4d5372SAneesh Kumar K.V */ 3020ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3021b9a4207dSJan Kara ext4_truncate_failed_write(inode); 302247564bfbSTheodore Ts'o 302347564bfbSTheodore Ts'o if (ret == -ENOSPC && 302447564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 302547564bfbSTheodore Ts'o goto retry_journal; 302647564bfbSTheodore Ts'o 302709cbfeafSKirill A. Shutemov put_page(page); 302847564bfbSTheodore Ts'o return ret; 302964769240SAlex Tomas } 303064769240SAlex Tomas 303147564bfbSTheodore Ts'o *pagep = page; 303264769240SAlex Tomas return ret; 303364769240SAlex Tomas } 303464769240SAlex Tomas 3035632eaeabSMingming Cao /* 3036632eaeabSMingming Cao * Check if we should update i_disksize 3037632eaeabSMingming Cao * when write to the end of file but not require block allocation 3038632eaeabSMingming Cao */ 3039632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3040632eaeabSMingming Cao unsigned long offset) 3041632eaeabSMingming Cao { 3042632eaeabSMingming Cao struct buffer_head *bh; 3043632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3044632eaeabSMingming Cao unsigned int idx; 3045632eaeabSMingming Cao int i; 3046632eaeabSMingming Cao 3047632eaeabSMingming Cao bh = page_buffers(page); 3048632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3049632eaeabSMingming Cao 3050632eaeabSMingming Cao for (i = 0; i < idx; i++) 3051632eaeabSMingming Cao bh = bh->b_this_page; 3052632eaeabSMingming Cao 305329fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3054632eaeabSMingming Cao return 0; 3055632eaeabSMingming Cao return 1; 3056632eaeabSMingming Cao } 3057632eaeabSMingming Cao 305864769240SAlex Tomas static int ext4_da_write_end(struct file *file, 305964769240SAlex Tomas struct address_space *mapping, 306064769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 306164769240SAlex Tomas struct page *page, void *fsdata) 306264769240SAlex Tomas { 306364769240SAlex Tomas struct inode *inode = mapping->host; 306464769240SAlex Tomas int ret = 0, ret2; 306564769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 306664769240SAlex Tomas loff_t new_i_size; 3067632eaeabSMingming Cao unsigned long start, end; 306879f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 306979f0be8dSAneesh Kumar K.V 307074d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 307174d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 307279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3073632eaeabSMingming Cao 30749bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 307509cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 3076632eaeabSMingming Cao end = start + copied - 1; 307764769240SAlex Tomas 307864769240SAlex Tomas /* 307964769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 308064769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 308164769240SAlex Tomas * into that. 308264769240SAlex Tomas */ 308364769240SAlex Tomas new_i_size = pos + copied; 3084ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 30859c3569b5STao Ma if (ext4_has_inline_data(inode) || 30869c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 3087ee124d27SDmitry Monakhov ext4_update_i_disksize(inode, new_i_size); 3088cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3089cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3090cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3091cf17fea6SAneesh Kumar K.V */ 30924209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 3093632eaeabSMingming Cao } 3094632eaeabSMingming Cao } 30959c3569b5STao Ma 30969c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30979c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30989c3569b5STao Ma ext4_has_inline_data(inode)) 30999c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 31009c3569b5STao Ma page); 31019c3569b5STao Ma else 310264769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 310364769240SAlex Tomas page, fsdata); 31049c3569b5STao Ma 310564769240SAlex Tomas copied = ret2; 310664769240SAlex Tomas if (ret2 < 0) 310764769240SAlex Tomas ret = ret2; 310864769240SAlex Tomas ret2 = ext4_journal_stop(handle); 31094209ae12SHarshad Shirwadkar if (unlikely(ret2 && !ret)) 311064769240SAlex Tomas ret = ret2; 311164769240SAlex Tomas 311264769240SAlex Tomas return ret ? ret : copied; 311364769240SAlex Tomas } 311464769240SAlex Tomas 3115ccd2506bSTheodore Ts'o /* 3116ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3117ccd2506bSTheodore Ts'o */ 3118ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3119ccd2506bSTheodore Ts'o { 3120fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3121fb40ba0dSTheodore Ts'o 312271d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3123ccd2506bSTheodore Ts'o return 0; 3124ccd2506bSTheodore Ts'o 3125ccd2506bSTheodore Ts'o /* 3126ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3127ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3128ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3129ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3130ccd2506bSTheodore Ts'o * would require replicating code paths in: 3131ccd2506bSTheodore Ts'o * 313220970ba6STheodore Ts'o * ext4_writepages() -> 3133ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3134ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3135ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3136ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3137ccd2506bSTheodore Ts'o * 3138ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3139ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3140ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3141ccd2506bSTheodore Ts'o * doing I/O at all. 3142ccd2506bSTheodore Ts'o * 3143ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3144380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3145ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3146ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 314725985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3148ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3149ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3150ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3151ccd2506bSTheodore Ts'o * 3152ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3153ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3154ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3155ccd2506bSTheodore Ts'o */ 3156ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3157ccd2506bSTheodore Ts'o } 315864769240SAlex Tomas 315964769240SAlex Tomas /* 3160ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3161ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3162ac27a0ecSDave Kleikamp * 3163ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3164617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3165ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3166ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3167ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3168ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3169ac27a0ecSDave Kleikamp * 3170ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3171ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3172ac27a0ecSDave Kleikamp */ 3173617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3174ac27a0ecSDave Kleikamp { 3175ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3176ac27a0ecSDave Kleikamp journal_t *journal; 3177ac27a0ecSDave Kleikamp int err; 3178ac27a0ecSDave Kleikamp 317946c7f254STao Ma /* 318046c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 318146c7f254STao Ma */ 318246c7f254STao Ma if (ext4_has_inline_data(inode)) 318346c7f254STao Ma return 0; 318446c7f254STao Ma 318564769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 318664769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 318764769240SAlex Tomas /* 318864769240SAlex Tomas * With delalloc we want to sync the file 318964769240SAlex Tomas * so that we can make sure we allocate 319064769240SAlex Tomas * blocks for file 319164769240SAlex Tomas */ 319264769240SAlex Tomas filemap_write_and_wait(mapping); 319364769240SAlex Tomas } 319464769240SAlex Tomas 319519f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 319619f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3197ac27a0ecSDave Kleikamp /* 3198ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3199ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3200ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3201ac27a0ecSDave Kleikamp * do we expect this to happen. 3202ac27a0ecSDave Kleikamp * 3203ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3204ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3205ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3206ac27a0ecSDave Kleikamp * will.) 3207ac27a0ecSDave Kleikamp * 3208617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3209ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3210ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3211ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3212ac27a0ecSDave Kleikamp * everything they get. 3213ac27a0ecSDave Kleikamp */ 3214ac27a0ecSDave Kleikamp 321519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3216617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3217dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3218dab291afSMingming Cao err = jbd2_journal_flush(journal); 3219dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3220ac27a0ecSDave Kleikamp 3221ac27a0ecSDave Kleikamp if (err) 3222ac27a0ecSDave Kleikamp return 0; 3223ac27a0ecSDave Kleikamp } 3224ac27a0ecSDave Kleikamp 3225ac58e4fbSRitesh Harjani return iomap_bmap(mapping, block, &ext4_iomap_ops); 3226ac27a0ecSDave Kleikamp } 3227ac27a0ecSDave Kleikamp 3228617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3229ac27a0ecSDave Kleikamp { 323046c7f254STao Ma int ret = -EAGAIN; 323146c7f254STao Ma struct inode *inode = page->mapping->host; 323246c7f254STao Ma 32330562e0baSJiaying Zhang trace_ext4_readpage(page); 323446c7f254STao Ma 323546c7f254STao Ma if (ext4_has_inline_data(inode)) 323646c7f254STao Ma ret = ext4_readpage_inline(inode, page); 323746c7f254STao Ma 323846c7f254STao Ma if (ret == -EAGAIN) 3239a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 324046c7f254STao Ma 324146c7f254STao Ma return ret; 3242ac27a0ecSDave Kleikamp } 3243ac27a0ecSDave Kleikamp 32446311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3245ac27a0ecSDave Kleikamp { 32466311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 324746c7f254STao Ma 32486311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 324946c7f254STao Ma if (ext4_has_inline_data(inode)) 32506311f91fSMatthew Wilcox (Oracle) return; 325146c7f254STao Ma 3252a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3253ac27a0ecSDave Kleikamp } 3254ac27a0ecSDave Kleikamp 3255d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3256d47992f8SLukas Czerner unsigned int length) 3257ac27a0ecSDave Kleikamp { 3258ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32590562e0baSJiaying Zhang 32604520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32614520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32624520fb3cSJan Kara 3263ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32644520fb3cSJan Kara } 32654520fb3cSJan Kara 326653e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3267ca99fdd2SLukas Czerner unsigned int offset, 3268ca99fdd2SLukas Czerner unsigned int length) 32694520fb3cSJan Kara { 32704520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32714520fb3cSJan Kara 3272ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32734520fb3cSJan Kara 3274744692dcSJiaying Zhang /* 3275ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3276ac27a0ecSDave Kleikamp */ 327709cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3278ac27a0ecSDave Kleikamp ClearPageChecked(page); 3279ac27a0ecSDave Kleikamp 3280ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 328153e87268SJan Kara } 328253e87268SJan Kara 328353e87268SJan Kara /* Wrapper for aops... */ 328453e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3285d47992f8SLukas Czerner unsigned int offset, 3286d47992f8SLukas Czerner unsigned int length) 328753e87268SJan Kara { 3288ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3289ac27a0ecSDave Kleikamp } 3290ac27a0ecSDave Kleikamp 3291617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3292ac27a0ecSDave Kleikamp { 3293617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3294ac27a0ecSDave Kleikamp 32950562e0baSJiaying Zhang trace_ext4_releasepage(page); 32960562e0baSJiaying Zhang 3297e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3298e1c36595SJan Kara if (PageChecked(page)) 3299ac27a0ecSDave Kleikamp return 0; 33000390131bSFrank Mayhar if (journal) 3301529a781eSzhangyi (F) return jbd2_journal_try_to_free_buffers(journal, page); 33020390131bSFrank Mayhar else 33030390131bSFrank Mayhar return try_to_free_buffers(page); 3304ac27a0ecSDave Kleikamp } 3305ac27a0ecSDave Kleikamp 3306b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3307b8a6176cSJan Kara { 3308b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3309b8a6176cSJan Kara 3310aa75f4d3SHarshad Shirwadkar if (journal) { 3311aa75f4d3SHarshad Shirwadkar if (jbd2_transaction_committed(journal, 3312aa75f4d3SHarshad Shirwadkar EXT4_I(inode)->i_datasync_tid)) 3313d0520df7SAndrea Righi return false; 3314d0520df7SAndrea Righi if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT)) 33151ceecb53SHarshad Shirwadkar return !list_empty(&EXT4_I(inode)->i_fc_list); 3316d0520df7SAndrea Righi return true; 3317aa75f4d3SHarshad Shirwadkar } 3318aa75f4d3SHarshad Shirwadkar 3319b8a6176cSJan Kara /* Any metadata buffers to write? */ 3320b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3321b8a6176cSJan Kara return true; 3322b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3323b8a6176cSJan Kara } 3324b8a6176cSJan Kara 3325c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3326c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3327c8fdfe29SMatthew Bobrowski loff_t length) 3328364443cbSJan Kara { 3329c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3330c8fdfe29SMatthew Bobrowski 3331c8fdfe29SMatthew Bobrowski /* 3332c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3333c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3334c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3335c8fdfe29SMatthew Bobrowski */ 3336c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3337c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3338c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3339c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3340c8fdfe29SMatthew Bobrowski 3341c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3342c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3343c8fdfe29SMatthew Bobrowski 3344c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3345c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3346c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3347c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3348c8fdfe29SMatthew Bobrowski 33496386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 33506386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 33516386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 33526386722aSRitesh Harjani 3353c8fdfe29SMatthew Bobrowski /* 3354c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3355c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3356c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3357c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3358c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3359c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3360c8fdfe29SMatthew Bobrowski * been set first. 3361c8fdfe29SMatthew Bobrowski */ 3362c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3363c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3364c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3365c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3366c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3367c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3368c8fdfe29SMatthew Bobrowski } else { 3369c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3370c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3371c8fdfe29SMatthew Bobrowski } 3372c8fdfe29SMatthew Bobrowski } 3373c8fdfe29SMatthew Bobrowski 3374f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3375f063db5eSMatthew Bobrowski unsigned int flags) 3376f063db5eSMatthew Bobrowski { 3377f063db5eSMatthew Bobrowski handle_t *handle; 3378378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3379378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3380f063db5eSMatthew Bobrowski 3381f063db5eSMatthew Bobrowski /* 3382f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3383f063db5eSMatthew Bobrowski * once for direct I/O. 3384f063db5eSMatthew Bobrowski */ 3385f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3386f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3387f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3388f063db5eSMatthew Bobrowski 3389f063db5eSMatthew Bobrowski retry: 3390f063db5eSMatthew Bobrowski /* 3391f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3392f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3393f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3394f063db5eSMatthew Bobrowski * fits into the credits as well. 3395f063db5eSMatthew Bobrowski */ 3396f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3397f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3398f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3399f063db5eSMatthew Bobrowski 3400378f32baSMatthew Bobrowski /* 3401378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3402378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3403378f32baSMatthew Bobrowski */ 3404378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3405378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3406378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3407378f32baSMatthew Bobrowski /* 3408378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3409378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3410378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3411378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3412378f32baSMatthew Bobrowski */ 3413378f32baSMatthew Bobrowski else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode)) 3414378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3415378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3416378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3417378f32baSMatthew Bobrowski 3418378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3419378f32baSMatthew Bobrowski 3420378f32baSMatthew Bobrowski /* 3421378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3422378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3423378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3424378f32baSMatthew Bobrowski */ 3425378f32baSMatthew Bobrowski if (!m_flags && !ret) 3426378f32baSMatthew Bobrowski ret = -ENOTBLK; 3427f063db5eSMatthew Bobrowski 3428f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3429f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3430f063db5eSMatthew Bobrowski goto retry; 3431f063db5eSMatthew Bobrowski 3432f063db5eSMatthew Bobrowski return ret; 3433f063db5eSMatthew Bobrowski } 3434f063db5eSMatthew Bobrowski 3435f063db5eSMatthew Bobrowski 3436364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3437c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3438364443cbSJan Kara { 3439364443cbSJan Kara int ret; 344009edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 344109edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3442364443cbSJan Kara 3443bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3444bcd8e91fSTheodore Ts'o return -EINVAL; 34457046ae35SAndreas Gruenbacher 3446364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3447364443cbSJan Kara return -ERANGE; 3448364443cbSJan Kara 344909edf4d3SMatthew Bobrowski /* 345009edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 345109edf4d3SMatthew Bobrowski */ 345209edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 345309edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 345409edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3455364443cbSJan Kara 34569faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 34579faac62dSRitesh Harjani /* 34589faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 34599faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 34609faac62dSRitesh Harjani * the mapping information. This could boost performance 34619faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 34629faac62dSRitesh Harjani */ 34639faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3464545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 34659faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 34669faac62dSRitesh Harjani goto out; 34679faac62dSRitesh Harjani } 34689faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 34699faac62dSRitesh Harjani } else { 34709faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 34719faac62dSRitesh Harjani } 3472f063db5eSMatthew Bobrowski 3473545052e9SChristoph Hellwig if (ret < 0) 3474545052e9SChristoph Hellwig return ret; 34759faac62dSRitesh Harjani out: 3476c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3477545052e9SChristoph Hellwig 3478364443cbSJan Kara return 0; 3479364443cbSJan Kara } 3480364443cbSJan Kara 34818cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34828cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34838cd115bdSJan Kara struct iomap *srcmap) 34848cd115bdSJan Kara { 34858cd115bdSJan Kara int ret; 34868cd115bdSJan Kara 34878cd115bdSJan Kara /* 34888cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34898cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34908cd115bdSJan Kara */ 34918cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34928cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34938cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34948cd115bdSJan Kara return ret; 34958cd115bdSJan Kara } 34968cd115bdSJan Kara 3497776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3498776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3499776722e8SJan Kara { 3500378f32baSMatthew Bobrowski /* 3501378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3502378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3503378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3504378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3505378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3506378f32baSMatthew Bobrowski */ 3507378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3508378f32baSMatthew Bobrowski return -ENOTBLK; 3509378f32baSMatthew Bobrowski 3510776722e8SJan Kara return 0; 3511776722e8SJan Kara } 3512776722e8SJan Kara 35138ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3514364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3515776722e8SJan Kara .iomap_end = ext4_iomap_end, 3516364443cbSJan Kara }; 3517364443cbSJan Kara 35188cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 35198cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 35208cd115bdSJan Kara .iomap_end = ext4_iomap_end, 35218cd115bdSJan Kara }; 35228cd115bdSJan Kara 352309edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 352409edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 352509edf4d3SMatthew Bobrowski { 352609edf4d3SMatthew Bobrowski struct extent_status es; 352709edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 352809edf4d3SMatthew Bobrowski 352909edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 353009edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 353109edf4d3SMatthew Bobrowski 353209edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 353309edf4d3SMatthew Bobrowski return false; 353409edf4d3SMatthew Bobrowski 353509edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 353609edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 353709edf4d3SMatthew Bobrowski return false; 353809edf4d3SMatthew Bobrowski } 353909edf4d3SMatthew Bobrowski 354009edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 354109edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 354209edf4d3SMatthew Bobrowski 354309edf4d3SMatthew Bobrowski return true; 354409edf4d3SMatthew Bobrowski } 354509edf4d3SMatthew Bobrowski 354609edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 354709edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 354809edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 354909edf4d3SMatthew Bobrowski { 355009edf4d3SMatthew Bobrowski int ret; 355109edf4d3SMatthew Bobrowski bool delalloc = false; 355209edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 355309edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 355409edf4d3SMatthew Bobrowski 355509edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 355609edf4d3SMatthew Bobrowski return -EINVAL; 355709edf4d3SMatthew Bobrowski 35587cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35597cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3560ba5843f5SJan Kara if (ret != -EAGAIN) { 3561ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3562ba5843f5SJan Kara ret = -ENOENT; 3563ba5843f5SJan Kara return ret; 3564ba5843f5SJan Kara } 3565ba5843f5SJan Kara } 356612735f88SJan Kara 356709edf4d3SMatthew Bobrowski /* 356809edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 356909edf4d3SMatthew Bobrowski */ 357009edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 357109edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 357209edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 357312735f88SJan Kara 3574b2c57642SRitesh Harjani /* 3575b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3576b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3577b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3578b2c57642SRitesh Harjani * -EIO error. 3579b2c57642SRitesh Harjani */ 3580b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3581b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3582b2c57642SRitesh Harjani 3583b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3584b2c57642SRitesh Harjani map.m_flags = 0; 3585b2c57642SRitesh Harjani goto set_iomap; 3586b2c57642SRitesh Harjani } 3587b2c57642SRitesh Harjani } 3588b2c57642SRitesh Harjani 358912735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3590ba5843f5SJan Kara if (ret < 0) 3591ba5843f5SJan Kara return ret; 3592914f82a3SJan Kara if (ret == 0) 359309edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 359409edf4d3SMatthew Bobrowski 3595b2c57642SRitesh Harjani set_iomap: 359609edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 359709edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 359809edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 359909edf4d3SMatthew Bobrowski 360009edf4d3SMatthew Bobrowski return 0; 3601914f82a3SJan Kara } 3602914f82a3SJan Kara 360309edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 360409edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 360509edf4d3SMatthew Bobrowski }; 36064c0425ffSMingming Cao 3607ac27a0ecSDave Kleikamp /* 3608617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3609ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3610ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3611ac27a0ecSDave Kleikamp * not necessarily locked. 3612ac27a0ecSDave Kleikamp * 3613ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3614ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3615ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3616ac27a0ecSDave Kleikamp * 3617ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3618ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3619ac27a0ecSDave Kleikamp */ 3620617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3621ac27a0ecSDave Kleikamp { 3622ac27a0ecSDave Kleikamp SetPageChecked(page); 3623ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3624ac27a0ecSDave Kleikamp } 3625ac27a0ecSDave Kleikamp 36266dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 36276dcc693bSJan Kara { 36286dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 36296dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 36306dcc693bSJan Kara return __set_page_dirty_buffers(page); 36316dcc693bSJan Kara } 36326dcc693bSJan Kara 36330e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 36340e6895baSRitesh Harjani struct file *file, sector_t *span) 36350e6895baSRitesh Harjani { 36360e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 36370e6895baSRitesh Harjani &ext4_iomap_report_ops); 36380e6895baSRitesh Harjani } 36390e6895baSRitesh Harjani 364074d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3641617ba13bSMingming Cao .readpage = ext4_readpage, 36426311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 364343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 364420970ba6STheodore Ts'o .writepages = ext4_writepages, 3645bfc1af65SNick Piggin .write_begin = ext4_write_begin, 364674d553aaSTheodore Ts'o .write_end = ext4_write_end, 36476dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3648617ba13bSMingming Cao .bmap = ext4_bmap, 3649617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3650617ba13bSMingming Cao .releasepage = ext4_releasepage, 3651378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3652ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 36538ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3654aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36550e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3656ac27a0ecSDave Kleikamp }; 3657ac27a0ecSDave Kleikamp 3658617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3659617ba13bSMingming Cao .readpage = ext4_readpage, 36606311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 366143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 366220970ba6STheodore Ts'o .writepages = ext4_writepages, 3663bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3664bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3665617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3666617ba13bSMingming Cao .bmap = ext4_bmap, 36674520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3668617ba13bSMingming Cao .releasepage = ext4_releasepage, 3669378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 36708ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3671aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36720e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3673ac27a0ecSDave Kleikamp }; 3674ac27a0ecSDave Kleikamp 367564769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 367664769240SAlex Tomas .readpage = ext4_readpage, 36776311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 367843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 367920970ba6STheodore Ts'o .writepages = ext4_writepages, 368064769240SAlex Tomas .write_begin = ext4_da_write_begin, 368164769240SAlex Tomas .write_end = ext4_da_write_end, 36826dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 368364769240SAlex Tomas .bmap = ext4_bmap, 36848fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 368564769240SAlex Tomas .releasepage = ext4_releasepage, 3686378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 368764769240SAlex Tomas .migratepage = buffer_migrate_page, 36888ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3689aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36900e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 369164769240SAlex Tomas }; 369264769240SAlex Tomas 36935f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36945f0663bbSDan Williams .writepages = ext4_dax_writepages, 36955f0663bbSDan Williams .direct_IO = noop_direct_IO, 36965f0663bbSDan Williams .set_page_dirty = noop_set_page_dirty, 369794dbb631SToshi Kani .bmap = ext4_bmap, 36985f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36990e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 37005f0663bbSDan Williams }; 37015f0663bbSDan Williams 3702617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3703ac27a0ecSDave Kleikamp { 37043d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 37053d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 37063d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 37073d2b1582SLukas Czerner break; 37083d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3709617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 371074d553aaSTheodore Ts'o return; 37113d2b1582SLukas Czerner default: 37123d2b1582SLukas Czerner BUG(); 37133d2b1582SLukas Czerner } 37145f0663bbSDan Williams if (IS_DAX(inode)) 37155f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 37165f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 371774d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 371874d553aaSTheodore Ts'o else 371974d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3720ac27a0ecSDave Kleikamp } 3721ac27a0ecSDave Kleikamp 3722923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3723d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3724d863dc36SLukas Czerner { 372509cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 372609cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3727923ae0ffSRoss Zwisler unsigned blocksize, pos; 3728d863dc36SLukas Czerner ext4_lblk_t iblock; 3729d863dc36SLukas Czerner struct inode *inode = mapping->host; 3730d863dc36SLukas Czerner struct buffer_head *bh; 3731d863dc36SLukas Czerner struct page *page; 3732d863dc36SLukas Czerner int err = 0; 3733d863dc36SLukas Czerner 373409cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3735c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3736d863dc36SLukas Czerner if (!page) 3737d863dc36SLukas Czerner return -ENOMEM; 3738d863dc36SLukas Czerner 3739d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3740d863dc36SLukas Czerner 374109cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3742d863dc36SLukas Czerner 3743d863dc36SLukas Czerner if (!page_has_buffers(page)) 3744d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3745d863dc36SLukas Czerner 3746d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3747d863dc36SLukas Czerner bh = page_buffers(page); 3748d863dc36SLukas Czerner pos = blocksize; 3749d863dc36SLukas Czerner while (offset >= pos) { 3750d863dc36SLukas Czerner bh = bh->b_this_page; 3751d863dc36SLukas Czerner iblock++; 3752d863dc36SLukas Czerner pos += blocksize; 3753d863dc36SLukas Czerner } 3754d863dc36SLukas Czerner if (buffer_freed(bh)) { 3755d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3756d863dc36SLukas Czerner goto unlock; 3757d863dc36SLukas Czerner } 3758d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3759d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3760d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3761d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3762d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3763d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3764d863dc36SLukas Czerner goto unlock; 3765d863dc36SLukas Czerner } 3766d863dc36SLukas Czerner } 3767d863dc36SLukas Czerner 3768d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3769d863dc36SLukas Czerner if (PageUptodate(page)) 3770d863dc36SLukas Czerner set_buffer_uptodate(bh); 3771d863dc36SLukas Czerner 3772d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 37732d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 37742d069c08Szhangyi (F) if (err) 3775d863dc36SLukas Czerner goto unlock; 37764f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3777c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3778a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3779834f1565SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3780834f1565SEric Biggers bh_offset(bh)); 3781834f1565SEric Biggers if (err) { 3782834f1565SEric Biggers clear_buffer_uptodate(bh); 3783834f1565SEric Biggers goto unlock; 3784834f1565SEric Biggers } 3785c9c7429cSMichael Halcrow } 3786d863dc36SLukas Czerner } 3787d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3788d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3789d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3790d863dc36SLukas Czerner if (err) 3791d863dc36SLukas Czerner goto unlock; 3792d863dc36SLukas Czerner } 3793d863dc36SLukas Czerner zero_user(page, offset, length); 3794d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3795d863dc36SLukas Czerner 3796d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3797d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37980713ed0cSLukas Czerner } else { 3799353eefd3Sjon ernst err = 0; 3800d863dc36SLukas Czerner mark_buffer_dirty(bh); 38013957ef53SJan Kara if (ext4_should_order_data(inode)) 380273131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 380373131fbbSRoss Zwisler length); 38040713ed0cSLukas Czerner } 3805d863dc36SLukas Czerner 3806d863dc36SLukas Czerner unlock: 3807d863dc36SLukas Czerner unlock_page(page); 380809cbfeafSKirill A. Shutemov put_page(page); 3809d863dc36SLukas Czerner return err; 3810d863dc36SLukas Czerner } 3811d863dc36SLukas Czerner 381294350ab5SMatthew Wilcox /* 3813923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3814923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3815923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3816923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 3817923ae0ffSRoss Zwisler * that cooresponds to 'from' 3818923ae0ffSRoss Zwisler */ 3819923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3820923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3821923ae0ffSRoss Zwisler { 3822923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 382309cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3824923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3825923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3826923ae0ffSRoss Zwisler 3827923ae0ffSRoss Zwisler /* 3828923ae0ffSRoss Zwisler * correct length if it does not fall between 3829923ae0ffSRoss Zwisler * 'from' and the end of the block 3830923ae0ffSRoss Zwisler */ 3831923ae0ffSRoss Zwisler if (length > max || length < 0) 3832923ae0ffSRoss Zwisler length = max; 3833923ae0ffSRoss Zwisler 383447e69351SJan Kara if (IS_DAX(inode)) { 383547e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 383647e69351SJan Kara &ext4_iomap_ops); 383747e69351SJan Kara } 3838923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3839923ae0ffSRoss Zwisler } 3840923ae0ffSRoss Zwisler 3841923ae0ffSRoss Zwisler /* 384294350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 384394350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 384494350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 384594350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 384694350ab5SMatthew Wilcox */ 3847c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 384894350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 384994350ab5SMatthew Wilcox { 385009cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 385194350ab5SMatthew Wilcox unsigned length; 385294350ab5SMatthew Wilcox unsigned blocksize; 385394350ab5SMatthew Wilcox struct inode *inode = mapping->host; 385494350ab5SMatthew Wilcox 38550d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3856592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 38570d06863fSTheodore Ts'o return 0; 38580d06863fSTheodore Ts'o 385994350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 386094350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 386194350ab5SMatthew Wilcox 386294350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 386394350ab5SMatthew Wilcox } 386494350ab5SMatthew Wilcox 3865a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3866a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3867a87dd18cSLukas Czerner { 3868a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3869a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3870e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3871a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3872a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3873a87dd18cSLukas Czerner int err = 0; 3874a87dd18cSLukas Czerner 3875e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3876e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3877e1be3a92SLukas Czerner 3878a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3879a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3880a87dd18cSLukas Czerner 3881a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3882e1be3a92SLukas Czerner if (start == end && 3883e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3884a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3885a87dd18cSLukas Czerner lstart, length); 3886a87dd18cSLukas Czerner return err; 3887a87dd18cSLukas Czerner } 3888a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3889e1be3a92SLukas Czerner if (partial_start) { 3890a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3891a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3892a87dd18cSLukas Czerner if (err) 3893a87dd18cSLukas Czerner return err; 3894a87dd18cSLukas Czerner } 3895a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3896e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3897a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3898e1be3a92SLukas Czerner byte_end - partial_end, 3899e1be3a92SLukas Czerner partial_end + 1); 3900a87dd18cSLukas Czerner return err; 3901a87dd18cSLukas Czerner } 3902a87dd18cSLukas Czerner 390391ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 390491ef4cafSDuane Griffin { 390591ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 390691ef4cafSDuane Griffin return 1; 390791ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 390891ef4cafSDuane Griffin return 1; 390991ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 391091ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 391191ef4cafSDuane Griffin return 0; 391291ef4cafSDuane Griffin } 391391ef4cafSDuane Griffin 3914ac27a0ecSDave Kleikamp /* 391501127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 391601127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 391701127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 391801127848SJan Kara * that will never happen after we truncate page cache. 391901127848SJan Kara */ 392001127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 392101127848SJan Kara loff_t len) 392201127848SJan Kara { 392301127848SJan Kara handle_t *handle; 39244209ae12SHarshad Shirwadkar int ret; 39254209ae12SHarshad Shirwadkar 392601127848SJan Kara loff_t size = i_size_read(inode); 392701127848SJan Kara 39285955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 392901127848SJan Kara if (offset > size || offset + len < size) 393001127848SJan Kara return 0; 393101127848SJan Kara 393201127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 393301127848SJan Kara return 0; 393401127848SJan Kara 393501127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 393601127848SJan Kara if (IS_ERR(handle)) 393701127848SJan Kara return PTR_ERR(handle); 393801127848SJan Kara ext4_update_i_disksize(inode, size); 39394209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 394001127848SJan Kara ext4_journal_stop(handle); 394101127848SJan Kara 39424209ae12SHarshad Shirwadkar return ret; 394301127848SJan Kara } 394401127848SJan Kara 3945b1f38217SRoss Zwisler static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3946430657b6SRoss Zwisler { 3947430657b6SRoss Zwisler up_write(&ei->i_mmap_sem); 3948430657b6SRoss Zwisler schedule(); 3949430657b6SRoss Zwisler down_write(&ei->i_mmap_sem); 3950430657b6SRoss Zwisler } 3951430657b6SRoss Zwisler 3952430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3953430657b6SRoss Zwisler { 3954430657b6SRoss Zwisler struct ext4_inode_info *ei = EXT4_I(inode); 3955430657b6SRoss Zwisler struct page *page; 3956430657b6SRoss Zwisler int error; 3957430657b6SRoss Zwisler 3958430657b6SRoss Zwisler if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3959430657b6SRoss Zwisler return -EINVAL; 3960430657b6SRoss Zwisler 3961430657b6SRoss Zwisler do { 3962430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3963430657b6SRoss Zwisler if (!page) 3964430657b6SRoss Zwisler return 0; 3965430657b6SRoss Zwisler 3966430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3967430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3968430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3969b1f38217SRoss Zwisler ext4_wait_dax_page(ei)); 3970b1f38217SRoss Zwisler } while (error == 0); 3971430657b6SRoss Zwisler 3972430657b6SRoss Zwisler return error; 3973430657b6SRoss Zwisler } 3974430657b6SRoss Zwisler 397501127848SJan Kara /* 3976cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3977a4bb6b64SAllison Henderson * associated with the given offset and length 3978a4bb6b64SAllison Henderson * 3979a4bb6b64SAllison Henderson * @inode: File inode 3980a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3981a4bb6b64SAllison Henderson * @len: The length of the hole 3982a4bb6b64SAllison Henderson * 39834907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3984a4bb6b64SAllison Henderson */ 3985a4bb6b64SAllison Henderson 3986aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3987a4bb6b64SAllison Henderson { 398826a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 398926a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 399026a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3991a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 399226a4c0c6STheodore Ts'o handle_t *handle; 399326a4c0c6STheodore Ts'o unsigned int credits; 39944209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 399526a4c0c6STheodore Ts'o 3996b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3997aaddea81SZheng Liu 3998c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3999c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 4000c1e8220bSTheodore Ts'o down_write(&EXT4_I(inode)->i_mmap_sem); 4001c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 4002c1e8220bSTheodore Ts'o up_write(&EXT4_I(inode)->i_mmap_sem); 4003c1e8220bSTheodore Ts'o if (ret) 4004c1e8220bSTheodore Ts'o return ret; 4005c1e8220bSTheodore Ts'o } 4006c1e8220bSTheodore Ts'o 400726a4c0c6STheodore Ts'o /* 400826a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 400926a4c0c6STheodore Ts'o * Then release them. 401026a4c0c6STheodore Ts'o */ 4011cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 401226a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 401326a4c0c6STheodore Ts'o offset + length - 1); 401426a4c0c6STheodore Ts'o if (ret) 401526a4c0c6STheodore Ts'o return ret; 401626a4c0c6STheodore Ts'o } 401726a4c0c6STheodore Ts'o 40185955102cSAl Viro inode_lock(inode); 40199ef06cecSLukas Czerner 402026a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 402126a4c0c6STheodore Ts'o if (offset >= inode->i_size) 402226a4c0c6STheodore Ts'o goto out_mutex; 402326a4c0c6STheodore Ts'o 402426a4c0c6STheodore Ts'o /* 402526a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 402626a4c0c6STheodore Ts'o * to end after the page that contains i_size 402726a4c0c6STheodore Ts'o */ 402826a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 402926a4c0c6STheodore Ts'o length = inode->i_size + 403009cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 403126a4c0c6STheodore Ts'o offset; 403226a4c0c6STheodore Ts'o } 403326a4c0c6STheodore Ts'o 4034a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 4035a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 4036a361293fSJan Kara /* 4037a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 4038a361293fSJan Kara * partial block 4039a361293fSJan Kara */ 4040a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 4041a361293fSJan Kara if (ret < 0) 4042a361293fSJan Kara goto out_mutex; 4043a361293fSJan Kara 4044a361293fSJan Kara } 4045a361293fSJan Kara 4046ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 4047ea3d7209SJan Kara inode_dio_wait(inode); 4048ea3d7209SJan Kara 4049ea3d7209SJan Kara /* 4050ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 4051ea3d7209SJan Kara * page cache. 4052ea3d7209SJan Kara */ 4053ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 4054430657b6SRoss Zwisler 4055430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4056430657b6SRoss Zwisler if (ret) 4057430657b6SRoss Zwisler goto out_dio; 4058430657b6SRoss Zwisler 4059a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4060a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 406126a4c0c6STheodore Ts'o 4062a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 406301127848SJan Kara if (last_block_offset > first_block_offset) { 406401127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 406501127848SJan Kara if (ret) 406601127848SJan Kara goto out_dio; 4067a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4068a87dd18cSLukas Czerner last_block_offset); 406901127848SJan Kara } 407026a4c0c6STheodore Ts'o 407126a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 407226a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 407326a4c0c6STheodore Ts'o else 407426a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 407526a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 407626a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 407726a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 407826a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 407926a4c0c6STheodore Ts'o goto out_dio; 408026a4c0c6STheodore Ts'o } 408126a4c0c6STheodore Ts'o 4082a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4083a87dd18cSLukas Czerner length); 408426a4c0c6STheodore Ts'o if (ret) 408526a4c0c6STheodore Ts'o goto out_stop; 408626a4c0c6STheodore Ts'o 408726a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 408826a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 408926a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 409026a4c0c6STheodore Ts'o 4091eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4092eee597acSLukas Czerner if (stop_block > first_block) { 409326a4c0c6STheodore Ts'o 409426a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 409527bc446eSbrookxu ext4_discard_preallocations(inode, 0); 409626a4c0c6STheodore Ts'o 409726a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 409826a4c0c6STheodore Ts'o stop_block - first_block); 409926a4c0c6STheodore Ts'o if (ret) { 410026a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 410126a4c0c6STheodore Ts'o goto out_stop; 410226a4c0c6STheodore Ts'o } 410326a4c0c6STheodore Ts'o 410426a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 410526a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 410626a4c0c6STheodore Ts'o stop_block - 1); 410726a4c0c6STheodore Ts'o else 41084f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 410926a4c0c6STheodore Ts'o stop_block); 411026a4c0c6STheodore Ts'o 4111819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4112eee597acSLukas Czerner } 4113a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, first_block, stop_block); 411426a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 411526a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4116e251f9bcSMaxim Patlasov 4117eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 41184209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 41194209ae12SHarshad Shirwadkar if (unlikely(ret2)) 41204209ae12SHarshad Shirwadkar ret = ret2; 412167a7d5f5SJan Kara if (ret >= 0) 412267a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 412326a4c0c6STheodore Ts'o out_stop: 412426a4c0c6STheodore Ts'o ext4_journal_stop(handle); 412526a4c0c6STheodore Ts'o out_dio: 4126ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 412726a4c0c6STheodore Ts'o out_mutex: 41285955102cSAl Viro inode_unlock(inode); 412926a4c0c6STheodore Ts'o return ret; 4130a4bb6b64SAllison Henderson } 4131a4bb6b64SAllison Henderson 4132a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4133a361293fSJan Kara { 4134a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4135a361293fSJan Kara struct jbd2_inode *jinode; 4136a361293fSJan Kara 4137a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4138a361293fSJan Kara return 0; 4139a361293fSJan Kara 4140a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4141a361293fSJan Kara spin_lock(&inode->i_lock); 4142a361293fSJan Kara if (!ei->jinode) { 4143a361293fSJan Kara if (!jinode) { 4144a361293fSJan Kara spin_unlock(&inode->i_lock); 4145a361293fSJan Kara return -ENOMEM; 4146a361293fSJan Kara } 4147a361293fSJan Kara ei->jinode = jinode; 4148a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4149a361293fSJan Kara jinode = NULL; 4150a361293fSJan Kara } 4151a361293fSJan Kara spin_unlock(&inode->i_lock); 4152a361293fSJan Kara if (unlikely(jinode != NULL)) 4153a361293fSJan Kara jbd2_free_inode(jinode); 4154a361293fSJan Kara return 0; 4155a361293fSJan Kara } 4156a361293fSJan Kara 4157a4bb6b64SAllison Henderson /* 4158617ba13bSMingming Cao * ext4_truncate() 4159ac27a0ecSDave Kleikamp * 4160617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4161617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4162ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4163ac27a0ecSDave Kleikamp * 416442b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4165ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4166ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4167ac27a0ecSDave Kleikamp * 4168ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4169ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4170ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4171ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4172ac27a0ecSDave Kleikamp * left-to-right works OK too). 4173ac27a0ecSDave Kleikamp * 4174ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4175ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4176ac27a0ecSDave Kleikamp * 4177ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4178617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4179ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4180617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4181617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4182ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4183617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4184ac27a0ecSDave Kleikamp */ 41852c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4186ac27a0ecSDave Kleikamp { 4187819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4188819c4920STheodore Ts'o unsigned int credits; 41894209ae12SHarshad Shirwadkar int err = 0, err2; 4190819c4920STheodore Ts'o handle_t *handle; 4191819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4192819c4920STheodore Ts'o 419319b5ef61STheodore Ts'o /* 419419b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4195e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 419619b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 419719b5ef61STheodore Ts'o */ 419819b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41995955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 42000562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 42010562e0baSJiaying Zhang 420291ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 42039a5d265fSzhengliang goto out_trace; 4204ac27a0ecSDave Kleikamp 42055534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 420619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 42077d8f9f7dSTheodore Ts'o 4208aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4209aef1c851STao Ma int has_inline = 1; 4210aef1c851STao Ma 421101daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 42129a5d265fSzhengliang if (err || has_inline) 42139a5d265fSzhengliang goto out_trace; 4214aef1c851STao Ma } 4215aef1c851STao Ma 4216a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4217a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4218a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 42199a5d265fSzhengliang goto out_trace; 4220a361293fSJan Kara } 4221a361293fSJan Kara 4222ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4223819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4224ff9893dcSAmir Goldstein else 4225819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4226819c4920STheodore Ts'o 4227819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 42289a5d265fSzhengliang if (IS_ERR(handle)) { 42299a5d265fSzhengliang err = PTR_ERR(handle); 42309a5d265fSzhengliang goto out_trace; 42319a5d265fSzhengliang } 4232819c4920STheodore Ts'o 4233eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4234eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4235819c4920STheodore Ts'o 4236819c4920STheodore Ts'o /* 4237819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4238819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4239819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4240819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4241819c4920STheodore Ts'o * 4242819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4243819c4920STheodore Ts'o * truncatable state while each transaction commits. 4244819c4920STheodore Ts'o */ 42452c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 42462c98eb5eSTheodore Ts'o if (err) 4247819c4920STheodore Ts'o goto out_stop; 4248819c4920STheodore Ts'o 4249819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4250819c4920STheodore Ts'o 425127bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4252819c4920STheodore Ts'o 4253819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4254d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4255819c4920STheodore Ts'o else 4256819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4257819c4920STheodore Ts'o 4258819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4259d0abb36dSTheodore Ts'o if (err) 4260d0abb36dSTheodore Ts'o goto out_stop; 4261819c4920STheodore Ts'o 4262819c4920STheodore Ts'o if (IS_SYNC(inode)) 4263819c4920STheodore Ts'o ext4_handle_sync(handle); 4264819c4920STheodore Ts'o 4265819c4920STheodore Ts'o out_stop: 4266819c4920STheodore Ts'o /* 4267819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4268819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4269819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 427058d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4271819c4920STheodore Ts'o * orphan info for us. 4272819c4920STheodore Ts'o */ 4273819c4920STheodore Ts'o if (inode->i_nlink) 4274819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4275819c4920STheodore Ts'o 4276eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 42774209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 42784209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 42794209ae12SHarshad Shirwadkar err = err2; 4280819c4920STheodore Ts'o ext4_journal_stop(handle); 4281a86c6181SAlex Tomas 42829a5d265fSzhengliang out_trace: 42830562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42842c98eb5eSTheodore Ts'o return err; 4285ac27a0ecSDave Kleikamp } 4286ac27a0ecSDave Kleikamp 4287ac27a0ecSDave Kleikamp /* 4288617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4289ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4290ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4291ac27a0ecSDave Kleikamp * inode. 4292ac27a0ecSDave Kleikamp */ 42938016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, 42948016e29fSHarshad Shirwadkar struct ext4_iloc *iloc, int in_mem, 42958016e29fSHarshad Shirwadkar ext4_fsblk_t *ret_block) 4296ac27a0ecSDave Kleikamp { 4297240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4298ac27a0ecSDave Kleikamp struct buffer_head *bh; 4299240799cdSTheodore Ts'o ext4_fsblk_t block; 430002f03c42SLinus Torvalds struct blk_plug plug; 4301240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4302ac27a0ecSDave Kleikamp 43033a06d778SAneesh Kumar K.V iloc->bh = NULL; 43048016e29fSHarshad Shirwadkar if (ino < EXT4_ROOT_INO || 43058016e29fSHarshad Shirwadkar ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 43066a797d27SDarrick J. Wong return -EFSCORRUPTED; 4307ac27a0ecSDave Kleikamp 43088016e29fSHarshad Shirwadkar iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 4309240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4310240799cdSTheodore Ts'o if (!gdp) 4311240799cdSTheodore Ts'o return -EIO; 4312240799cdSTheodore Ts'o 4313240799cdSTheodore Ts'o /* 4314240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4315240799cdSTheodore Ts'o */ 431600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 43178016e29fSHarshad Shirwadkar inode_offset = ((ino - 1) % 4318240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4319240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4320240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4321240799cdSTheodore Ts'o 4322240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4323aebf0243SWang Shilong if (unlikely(!bh)) 4324860d21e2STheodore Ts'o return -ENOMEM; 432546f870d6STheodore Ts'o if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO)) 432646f870d6STheodore Ts'o goto simulate_eio; 4327ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4328ac27a0ecSDave Kleikamp lock_buffer(bh); 43299c83a923SHidehiro Kawai 433060c776e5Szhangyi (F) if (ext4_buffer_uptodate(bh)) { 4331ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4332ac27a0ecSDave Kleikamp unlock_buffer(bh); 4333ac27a0ecSDave Kleikamp goto has_buffer; 4334ac27a0ecSDave Kleikamp } 4335ac27a0ecSDave Kleikamp 4336ac27a0ecSDave Kleikamp /* 4337ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4338ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4339ac27a0ecSDave Kleikamp * block. 4340ac27a0ecSDave Kleikamp */ 4341ac27a0ecSDave Kleikamp if (in_mem) { 4342ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4343240799cdSTheodore Ts'o int i, start; 4344ac27a0ecSDave Kleikamp 4345240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4346ac27a0ecSDave Kleikamp 4347ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4348240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4349aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4350ac27a0ecSDave Kleikamp goto make_io; 4351ac27a0ecSDave Kleikamp 4352ac27a0ecSDave Kleikamp /* 4353ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4354ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4355ac27a0ecSDave Kleikamp * of one, so skip it. 4356ac27a0ecSDave Kleikamp */ 4357ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4358ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4359ac27a0ecSDave Kleikamp goto make_io; 4360ac27a0ecSDave Kleikamp } 4361240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4362ac27a0ecSDave Kleikamp if (i == inode_offset) 4363ac27a0ecSDave Kleikamp continue; 4364617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4365ac27a0ecSDave Kleikamp break; 4366ac27a0ecSDave Kleikamp } 4367ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4368240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4369ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4370ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4371ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4372ac27a0ecSDave Kleikamp unlock_buffer(bh); 4373ac27a0ecSDave Kleikamp goto has_buffer; 4374ac27a0ecSDave Kleikamp } 4375ac27a0ecSDave Kleikamp } 4376ac27a0ecSDave Kleikamp 4377ac27a0ecSDave Kleikamp make_io: 4378ac27a0ecSDave Kleikamp /* 4379240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4380240799cdSTheodore Ts'o * blocks from the inode table. 4381240799cdSTheodore Ts'o */ 438202f03c42SLinus Torvalds blk_start_plug(&plug); 4383240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4384240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4385240799cdSTheodore Ts'o unsigned num; 43860d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4387240799cdSTheodore Ts'o 4388240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4389b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 43900d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4391240799cdSTheodore Ts'o if (table > b) 4392240799cdSTheodore Ts'o b = table; 43930d606e2cSTheodore Ts'o end = b + ra_blks; 4394240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4395feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4396560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4397240799cdSTheodore Ts'o table += num / inodes_per_block; 4398240799cdSTheodore Ts'o if (end > table) 4399240799cdSTheodore Ts'o end = table; 4400240799cdSTheodore Ts'o while (b <= end) 44015df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, b++); 4402240799cdSTheodore Ts'o } 4403240799cdSTheodore Ts'o 4404240799cdSTheodore Ts'o /* 4405ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4406ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4407ac27a0ecSDave Kleikamp * Read the block from disk. 4408ac27a0ecSDave Kleikamp */ 44098016e29fSHarshad Shirwadkar trace_ext4_load_inode(sb, ino); 44102d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 441102f03c42SLinus Torvalds blk_finish_plug(&plug); 4412ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4413ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 441446f870d6STheodore Ts'o simulate_eio: 44158016e29fSHarshad Shirwadkar if (ret_block) 44168016e29fSHarshad Shirwadkar *ret_block = block; 4417ac27a0ecSDave Kleikamp brelse(bh); 4418ac27a0ecSDave Kleikamp return -EIO; 4419ac27a0ecSDave Kleikamp } 4420ac27a0ecSDave Kleikamp } 4421ac27a0ecSDave Kleikamp has_buffer: 4422ac27a0ecSDave Kleikamp iloc->bh = bh; 4423ac27a0ecSDave Kleikamp return 0; 4424ac27a0ecSDave Kleikamp } 4425ac27a0ecSDave Kleikamp 44268016e29fSHarshad Shirwadkar static int __ext4_get_inode_loc_noinmem(struct inode *inode, 44278016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 44288016e29fSHarshad Shirwadkar { 44298016e29fSHarshad Shirwadkar ext4_fsblk_t err_blk; 44308016e29fSHarshad Shirwadkar int ret; 44318016e29fSHarshad Shirwadkar 44328016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 0, 44338016e29fSHarshad Shirwadkar &err_blk); 44348016e29fSHarshad Shirwadkar 44358016e29fSHarshad Shirwadkar if (ret == -EIO) 44368016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 44378016e29fSHarshad Shirwadkar "unable to read itable block"); 44388016e29fSHarshad Shirwadkar 44398016e29fSHarshad Shirwadkar return ret; 44408016e29fSHarshad Shirwadkar } 44418016e29fSHarshad Shirwadkar 4442617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4443ac27a0ecSDave Kleikamp { 44448016e29fSHarshad Shirwadkar ext4_fsblk_t err_blk; 44458016e29fSHarshad Shirwadkar int ret; 44468016e29fSHarshad Shirwadkar 4447ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 44488016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 44498016e29fSHarshad Shirwadkar !ext4_test_inode_state(inode, EXT4_STATE_XATTR), &err_blk); 44508016e29fSHarshad Shirwadkar 44518016e29fSHarshad Shirwadkar if (ret == -EIO) 44528016e29fSHarshad Shirwadkar ext4_error_inode_block(inode, err_blk, EIO, 44538016e29fSHarshad Shirwadkar "unable to read itable block"); 44548016e29fSHarshad Shirwadkar 44558016e29fSHarshad Shirwadkar return ret; 44568016e29fSHarshad Shirwadkar } 44578016e29fSHarshad Shirwadkar 44588016e29fSHarshad Shirwadkar 44598016e29fSHarshad Shirwadkar int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino, 44608016e29fSHarshad Shirwadkar struct ext4_iloc *iloc) 44618016e29fSHarshad Shirwadkar { 44628016e29fSHarshad Shirwadkar return __ext4_get_inode_loc(sb, ino, iloc, 0, NULL); 4463ac27a0ecSDave Kleikamp } 4464ac27a0ecSDave Kleikamp 4465a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 44666642586bSRoss Zwisler { 4467a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4468a8ab6d38SIra Weiny 44699cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 44706642586bSRoss Zwisler return false; 44716642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 44726642586bSRoss Zwisler return false; 44736642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 44746642586bSRoss Zwisler return false; 44756642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 44766642586bSRoss Zwisler return false; 4477592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 44786642586bSRoss Zwisler return false; 4479c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4480c93d8f88SEric Biggers return false; 4481a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4482a8ab6d38SIra Weiny return false; 4483a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 44846642586bSRoss Zwisler return true; 4485a8ab6d38SIra Weiny 4486b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 44876642586bSRoss Zwisler } 44886642586bSRoss Zwisler 4489043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4490ac27a0ecSDave Kleikamp { 4491617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 449200a1a053STheodore Ts'o unsigned int new_fl = 0; 4493ac27a0ecSDave Kleikamp 4494043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4495043546e4SIra Weiny 4496617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 449700a1a053STheodore Ts'o new_fl |= S_SYNC; 4498617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 449900a1a053STheodore Ts'o new_fl |= S_APPEND; 4500617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 450100a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4502617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 450300a1a053STheodore Ts'o new_fl |= S_NOATIME; 4504617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 450500a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4506043546e4SIra Weiny 4507043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4508043546e4SIra Weiny * here if already set. */ 4509043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4510043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4511923ae0ffSRoss Zwisler new_fl |= S_DAX; 4512043546e4SIra Weiny 45132ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 45142ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4515b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4516b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4517c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4518c93d8f88SEric Biggers new_fl |= S_VERITY; 45195f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 45202ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4521c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4522ac27a0ecSDave Kleikamp } 4523ac27a0ecSDave Kleikamp 45240fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 45250fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 45260fc1b451SAneesh Kumar K.V { 45270fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 45288180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 45298180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 45300fc1b451SAneesh Kumar K.V 4531e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 45320fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 45330fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 45340fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 453507a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 45368180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 45378180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 45388180a562SAneesh Kumar K.V } else { 45390fc1b451SAneesh Kumar K.V return i_blocks; 45408180a562SAneesh Kumar K.V } 45410fc1b451SAneesh Kumar K.V } else { 45420fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 45430fc1b451SAneesh Kumar K.V } 45440fc1b451SAneesh Kumar K.V } 4545ff9ddf7eSJan Kara 4546eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4547152a7b0aSTao Ma struct ext4_inode *raw_inode, 4548152a7b0aSTao Ma struct ext4_inode_info *ei) 4549152a7b0aSTao Ma { 4550152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4551152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4552eb9b5f01STheodore Ts'o 4553290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4554290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4555290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4556152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4557eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4558f19d5870STao Ma } else 4559f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4560eb9b5f01STheodore Ts'o return 0; 4561152a7b0aSTao Ma } 4562152a7b0aSTao Ma 4563040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4564040cb378SLi Xi { 45650b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4566040cb378SLi Xi return -EOPNOTSUPP; 4567040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4568040cb378SLi Xi return 0; 4569040cb378SLi Xi } 4570040cb378SLi Xi 4571e254d1afSEryu Guan /* 4572e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4573e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4574e254d1afSEryu Guan * set. 4575e254d1afSEryu Guan */ 4576e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4577e254d1afSEryu Guan { 4578e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4579e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4580e254d1afSEryu Guan else 4581e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4582e254d1afSEryu Guan } 4583e254d1afSEryu Guan static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4584e254d1afSEryu Guan { 4585e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4586e254d1afSEryu Guan return inode_peek_iversion_raw(inode); 4587e254d1afSEryu Guan else 4588e254d1afSEryu Guan return inode_peek_iversion(inode); 4589e254d1afSEryu Guan } 4590e254d1afSEryu Guan 45918a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 45928a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 45938a363970STheodore Ts'o unsigned int line) 4594ac27a0ecSDave Kleikamp { 4595617ba13bSMingming Cao struct ext4_iloc iloc; 4596617ba13bSMingming Cao struct ext4_inode *raw_inode; 45971d1fe1eeSDavid Howells struct ext4_inode_info *ei; 45981d1fe1eeSDavid Howells struct inode *inode; 4599b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 46001d1fe1eeSDavid Howells long ret; 46017e6e1ef4SDarrick J. Wong loff_t size; 4602ac27a0ecSDave Kleikamp int block; 460308cefc7aSEric W. Biederman uid_t i_uid; 460408cefc7aSEric W. Biederman gid_t i_gid; 4605040cb378SLi Xi projid_t i_projid; 4606ac27a0ecSDave Kleikamp 4607191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 46088a363970STheodore Ts'o (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 46098a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 46108a363970STheodore Ts'o (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 46118a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 46128a363970STheodore Ts'o return ERR_PTR(-ESTALE); 461354d3adbcSTheodore Ts'o __ext4_error(sb, function, line, EFSCORRUPTED, 0, 46148a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 46158a363970STheodore Ts'o ino, current->comm); 46168a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 46178a363970STheodore Ts'o } 46188a363970STheodore Ts'o 46191d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 46201d1fe1eeSDavid Howells if (!inode) 46211d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 46221d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 46231d1fe1eeSDavid Howells return inode; 46241d1fe1eeSDavid Howells 46251d1fe1eeSDavid Howells ei = EXT4_I(inode); 46267dc57615SPeter Huewe iloc.bh = NULL; 4627ac27a0ecSDave Kleikamp 46288016e29fSHarshad Shirwadkar ret = __ext4_get_inode_loc_noinmem(inode, &iloc); 46291d1fe1eeSDavid Howells if (ret < 0) 4630ac27a0ecSDave Kleikamp goto bad_inode; 4631617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4632814525f4SDarrick J. Wong 46338e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 46348a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46358a363970STheodore Ts'o "iget: root inode unallocated"); 46368e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 46378e4b5eaeSTheodore Ts'o goto bad_inode; 46388e4b5eaeSTheodore Ts'o } 46398e4b5eaeSTheodore Ts'o 46408a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 46418a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 46428a363970STheodore Ts'o ret = -ESTALE; 46438a363970STheodore Ts'o goto bad_inode; 46448a363970STheodore Ts'o } 46458a363970STheodore Ts'o 4646814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4647814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4648814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 46492dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 46502dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 46518a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46528a363970STheodore Ts'o "iget: bad extra_isize %u " 46538a363970STheodore Ts'o "(inode size %u)", 46542dc8d9e1SEric Biggers ei->i_extra_isize, 4655814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 46566a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4657814525f4SDarrick J. Wong goto bad_inode; 4658814525f4SDarrick J. Wong } 4659814525f4SDarrick J. Wong } else 4660814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4661814525f4SDarrick J. Wong 4662814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 46639aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4664814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4665814525f4SDarrick J. Wong __u32 csum; 4666814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4667814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4668814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4669814525f4SDarrick J. Wong sizeof(inum)); 4670814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4671814525f4SDarrick J. Wong sizeof(gen)); 4672814525f4SDarrick J. Wong } 4673814525f4SDarrick J. Wong 46748016e29fSHarshad Shirwadkar if ((!ext4_inode_csum_verify(inode, raw_inode, ei) || 46758016e29fSHarshad Shirwadkar ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) && 46768016e29fSHarshad Shirwadkar (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) { 46778016e29fSHarshad Shirwadkar ext4_error_inode_err(inode, function, line, 0, 46788016e29fSHarshad Shirwadkar EFSBADCRC, "iget: checksum invalid"); 46796a797d27SDarrick J. Wong ret = -EFSBADCRC; 4680814525f4SDarrick J. Wong goto bad_inode; 4681814525f4SDarrick J. Wong } 4682814525f4SDarrick J. Wong 4683ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 468408cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 468508cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 46860b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4687040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4688040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4689040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4690040cb378SLi Xi else 4691040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4692040cb378SLi Xi 4693ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 469408cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 469508cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4696ac27a0ecSDave Kleikamp } 469708cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 469808cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4699040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4700bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4701ac27a0ecSDave Kleikamp 4702353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 470367cf5b09STao Ma ei->i_inline_off = 0; 4704ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4705ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4706ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4707ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4708ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4709ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4710ac27a0ecSDave Kleikamp */ 4711ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4712393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4713393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4714393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4715ac27a0ecSDave Kleikamp /* this inode is deleted */ 47161d1fe1eeSDavid Howells ret = -ESTALE; 4717ac27a0ecSDave Kleikamp goto bad_inode; 4718ac27a0ecSDave Kleikamp } 4719ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4720ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4721ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4722393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4723393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4724393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4725ac27a0ecSDave Kleikamp } 4726ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4727043546e4SIra Weiny ext4_set_inode_flags(inode, true); 47280fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 47297973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4730e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4731a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4732a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4733e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 47347e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 47358a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47368a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 47377e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 47387e6e1ef4SDarrick J. Wong goto bad_inode; 47397e6e1ef4SDarrick J. Wong } 474048a34311SJan Kara /* 474148a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 474248a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 474348a34311SJan Kara * checksumming that corrupts checksums so forbid that. 474448a34311SJan Kara */ 474548a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 474648a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 474748a34311SJan Kara ext4_error_inode(inode, function, line, 0, 474848a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 474948a34311SJan Kara ret = -EFSCORRUPTED; 475048a34311SJan Kara goto bad_inode; 475148a34311SJan Kara } 4752ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4753a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4754a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4755a9e7f447SDmitry Monakhov #endif 4756ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4757ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4758a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4759ac27a0ecSDave Kleikamp /* 4760ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4761ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4762ac27a0ecSDave Kleikamp */ 4763617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4764ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4765ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4766aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 4767ac27a0ecSDave Kleikamp 4768b436b9beSJan Kara /* 4769b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4770b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4771b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4772b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4773b436b9beSJan Kara * now it is reread from disk. 4774b436b9beSJan Kara */ 4775b436b9beSJan Kara if (journal) { 4776b436b9beSJan Kara transaction_t *transaction; 4777b436b9beSJan Kara tid_t tid; 4778b436b9beSJan Kara 4779a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4780b436b9beSJan Kara if (journal->j_running_transaction) 4781b436b9beSJan Kara transaction = journal->j_running_transaction; 4782b436b9beSJan Kara else 4783b436b9beSJan Kara transaction = journal->j_committing_transaction; 4784b436b9beSJan Kara if (transaction) 4785b436b9beSJan Kara tid = transaction->t_tid; 4786b436b9beSJan Kara else 4787b436b9beSJan Kara tid = journal->j_commit_sequence; 4788a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4789b436b9beSJan Kara ei->i_sync_tid = tid; 4790b436b9beSJan Kara ei->i_datasync_tid = tid; 4791b436b9beSJan Kara } 4792b436b9beSJan Kara 47930040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4794ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4795ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 47962dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4797617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4798617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4799ac27a0ecSDave Kleikamp } else { 4800eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4801eb9b5f01STheodore Ts'o if (ret) 4802eb9b5f01STheodore Ts'o goto bad_inode; 4803ac27a0ecSDave Kleikamp } 4804814525f4SDarrick J. Wong } 4805ac27a0ecSDave Kleikamp 4806ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4807ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4808ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4809ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4810ef7f3835SKalpak Shah 4811ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4812ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4813ee73f9a5SJeff Layton 481425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 481525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4816ee73f9a5SJeff Layton ivers |= 481725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 481825ec56b5SJean Noel Cordenner } 4819e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4820c4f65706STheodore Ts'o } 482125ec56b5SJean Noel Cordenner 4822c4b5a614STheodore Ts'o ret = 0; 4823485c26ecSTheodore Ts'o if (ei->i_file_acl && 4824ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 48258a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48268a363970STheodore Ts'o "iget: bad extended attribute block %llu", 482724676da4STheodore Ts'o ei->i_file_acl); 48286a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4829485c26ecSTheodore Ts'o goto bad_inode; 4830f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4831bc716523SLiu Song /* validate the block references in the inode */ 48328016e29fSHarshad Shirwadkar if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) && 48338016e29fSHarshad Shirwadkar (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4834fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 48358016e29fSHarshad Shirwadkar !ext4_inode_is_fast_symlink(inode)))) { 4836bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4837bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4838bc716523SLiu Song else 48391f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4840fe2c8191SThiemo Nagel } 4841f19d5870STao Ma } 4842567f3e9aSTheodore Ts'o if (ret) 48437a262f7cSAneesh Kumar K.V goto bad_inode; 48447a262f7cSAneesh Kumar K.V 4845ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4846617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4847617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4848617ba13bSMingming Cao ext4_set_aops(inode); 4849ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4850617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4851617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4852ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 48536390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 48546390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 48558a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48568a363970STheodore Ts'o "iget: immutable or append flags " 48578a363970STheodore Ts'o "not allowed on symlinks"); 48586390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 48596390d33bSLuis R. Rodriguez goto bad_inode; 48606390d33bSLuis R. Rodriguez } 4861592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4862a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4863a7a67e8aSAl Viro ext4_set_aops(inode); 4864a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 486575e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4866617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4867e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4868e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4869e83c1397SDuane Griffin } else { 4870617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4871617ba13bSMingming Cao ext4_set_aops(inode); 4872ac27a0ecSDave Kleikamp } 487321fc61c7SAl Viro inode_nohighmem(inode); 4874563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4875563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4876617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4877ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4878ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4879ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4880ac27a0ecSDave Kleikamp else 4881ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4882ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4883393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4884393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4885563bdd61STheodore Ts'o } else { 48866a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 48878a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48888a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4889563bdd61STheodore Ts'o goto bad_inode; 4890ac27a0ecSDave Kleikamp } 48916456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 48926456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48936456ca65STheodore Ts'o "casefold flag without casefold feature"); 4894ac27a0ecSDave Kleikamp brelse(iloc.bh); 4895dec214d0STahsin Erdogan 48961d1fe1eeSDavid Howells unlock_new_inode(inode); 48971d1fe1eeSDavid Howells return inode; 4898ac27a0ecSDave Kleikamp 4899ac27a0ecSDave Kleikamp bad_inode: 4900567f3e9aSTheodore Ts'o brelse(iloc.bh); 49011d1fe1eeSDavid Howells iget_failed(inode); 49021d1fe1eeSDavid Howells return ERR_PTR(ret); 4903ac27a0ecSDave Kleikamp } 4904ac27a0ecSDave Kleikamp 49050fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 49060fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 49070fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 49080fc1b451SAneesh Kumar K.V { 49090fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 491028936b62SQian Cai u64 i_blocks = READ_ONCE(inode->i_blocks); 49110fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 49120fc1b451SAneesh Kumar K.V 49130fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 49140fc1b451SAneesh Kumar K.V /* 49154907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 49160fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49170fc1b451SAneesh Kumar K.V */ 49188180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49190fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 492084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4921f287a1a5STheodore Ts'o return 0; 4922f287a1a5STheodore Ts'o } 4923e2b911c5SDarrick J. Wong if (!ext4_has_feature_huge_file(sb)) 4924f287a1a5STheodore Ts'o return -EFBIG; 4925f287a1a5STheodore Ts'o 4926f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 49270fc1b451SAneesh Kumar K.V /* 49280fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 49290fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49300fc1b451SAneesh Kumar K.V */ 49318180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49320fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 493384a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 49340fc1b451SAneesh Kumar K.V } else { 493584a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 49368180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 49378180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 49388180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49398180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 49400fc1b451SAneesh Kumar K.V } 4941f287a1a5STheodore Ts'o return 0; 49420fc1b451SAneesh Kumar K.V } 49430fc1b451SAneesh Kumar K.V 49443f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 49453f19b2abSDavid Howells unsigned long orig_ino, 49463f19b2abSDavid Howells unsigned long ino, 49473f19b2abSDavid Howells struct ext4_inode *raw_inode) 4948a26f4992STheodore Ts'o { 49493f19b2abSDavid Howells struct inode *inode; 4950a26f4992STheodore Ts'o 49513f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 49523f19b2abSDavid Howells if (!inode) 49533f19b2abSDavid Howells return; 49543f19b2abSDavid Howells 49553f19b2abSDavid Howells if ((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 49560e11f644SChristoph Hellwig I_DIRTY_INODE)) || 4957a26f4992STheodore Ts'o ((inode->i_state & I_DIRTY_TIME) == 0)) 49583f19b2abSDavid Howells return; 49593f19b2abSDavid Howells 4960a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4961a26f4992STheodore Ts'o if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 49620e11f644SChristoph Hellwig I_DIRTY_INODE)) == 0) && 4963a26f4992STheodore Ts'o (inode->i_state & I_DIRTY_TIME)) { 4964a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4965a26f4992STheodore Ts'o 49665fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 4967a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4968a26f4992STheodore Ts'o 4969a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 49703f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 49713f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 49723f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 49733f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 4974a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 49753f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 49763f19b2abSDavid Howells return; 4977a26f4992STheodore Ts'o } 4978a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4979a26f4992STheodore Ts'o } 4980a26f4992STheodore Ts'o 4981a26f4992STheodore Ts'o /* 4982a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 4983a26f4992STheodore Ts'o * the same inode table block. 4984a26f4992STheodore Ts'o */ 4985a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 4986a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 4987a26f4992STheodore Ts'o { 4988a26f4992STheodore Ts'o unsigned long ino; 4989a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4990a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 4991a26f4992STheodore Ts'o 49920f0ff9a9STheodore Ts'o /* 49930f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 49940f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 49950f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 49960f0ff9a9STheodore Ts'o */ 49970f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 49983f19b2abSDavid Howells rcu_read_lock(); 4999a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 5000a26f4992STheodore Ts'o if (ino == orig_ino) 5001a26f4992STheodore Ts'o continue; 50023f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 50033f19b2abSDavid Howells (struct ext4_inode *)buf); 5004a26f4992STheodore Ts'o } 50053f19b2abSDavid Howells rcu_read_unlock(); 5006a26f4992STheodore Ts'o } 5007a26f4992STheodore Ts'o 5008ac27a0ecSDave Kleikamp /* 5009ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 5010ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 5011ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 5012ac27a0ecSDave Kleikamp * 5013ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 5014ac27a0ecSDave Kleikamp */ 5015617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 5016ac27a0ecSDave Kleikamp struct inode *inode, 5017830156c7SFrank Mayhar struct ext4_iloc *iloc) 5018ac27a0ecSDave Kleikamp { 5019617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5020617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 5021ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 5022202ee5dfSTheodore Ts'o struct super_block *sb = inode->i_sb; 5023ac27a0ecSDave Kleikamp int err = 0, rc, block; 5024202ee5dfSTheodore Ts'o int need_datasync = 0, set_large_file = 0; 502508cefc7aSEric W. Biederman uid_t i_uid; 502608cefc7aSEric W. Biederman gid_t i_gid; 5027040cb378SLi Xi projid_t i_projid; 5028ac27a0ecSDave Kleikamp 5029202ee5dfSTheodore Ts'o spin_lock(&ei->i_raw_lock); 5030202ee5dfSTheodore Ts'o 5031202ee5dfSTheodore Ts'o /* For fields not tracked in the in-memory inode, 5032ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 503319f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5034617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5035ac27a0ecSDave Kleikamp 503613221811SLuo Meng err = ext4_inode_blocks_set(handle, raw_inode, ei); 503713221811SLuo Meng if (err) { 503813221811SLuo Meng spin_unlock(&ei->i_raw_lock); 503913221811SLuo Meng goto out_brelse; 504013221811SLuo Meng } 504113221811SLuo Meng 5042ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 504308cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 504408cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 5045040cb378SLi Xi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 5046ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 504708cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 504808cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 5049ac27a0ecSDave Kleikamp /* 5050ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5051ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5052ac27a0ecSDave Kleikamp */ 505393e3b4e6SDaeho Jeong if (ei->i_dtime && list_empty(&ei->i_orphan)) { 505493e3b4e6SDaeho Jeong raw_inode->i_uid_high = 0; 505593e3b4e6SDaeho Jeong raw_inode->i_gid_high = 0; 505693e3b4e6SDaeho Jeong } else { 5057ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 505808cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 5059ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 506008cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 5061ac27a0ecSDave Kleikamp } 5062ac27a0ecSDave Kleikamp } else { 506308cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 506408cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 5065ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5066ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5067ac27a0ecSDave Kleikamp } 5068ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5069ef7f3835SKalpak Shah 5070ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5071ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5072ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5073ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5074ef7f3835SKalpak Shah 5075ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 5076353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 5077ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 5078a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5079a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 50807973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5081dce8e237SQiujun Huang if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) { 5082a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5083b71fc079SJan Kara need_datasync = 1; 5084b71fc079SJan Kara } 5085ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5086e2b911c5SDarrick J. Wong if (!ext4_has_feature_large_file(sb) || 5087617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5088202ee5dfSTheodore Ts'o cpu_to_le32(EXT4_GOOD_OLD_REV)) 5089202ee5dfSTheodore Ts'o set_large_file = 1; 5090ac27a0ecSDave Kleikamp } 5091ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5092ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5093ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5094ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5095ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5096ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5097ac27a0ecSDave Kleikamp } else { 5098ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5099ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5100ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5101ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5102ac27a0ecSDave Kleikamp } 5103f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 5104de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5105ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5106f19d5870STao Ma } 5107ac27a0ecSDave Kleikamp 5108ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 5109e254d1afSEryu Guan u64 ivers = ext4_inode_peek_iversion(inode); 5110ee73f9a5SJeff Layton 5111ee73f9a5SJeff Layton raw_inode->i_disk_version = cpu_to_le32(ivers); 511225ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 511325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 511425ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 5115ee73f9a5SJeff Layton cpu_to_le32(ivers >> 32); 5116c4f65706STheodore Ts'o raw_inode->i_extra_isize = 5117c4f65706STheodore Ts'o cpu_to_le16(ei->i_extra_isize); 5118c4f65706STheodore Ts'o } 511925ec56b5SJean Noel Cordenner } 5120040cb378SLi Xi 51210b7b7779SKaho Ng BUG_ON(!ext4_has_feature_project(inode->i_sb) && 5122040cb378SLi Xi i_projid != EXT4_DEF_PROJID); 5123040cb378SLi Xi 5124040cb378SLi Xi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 5125040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 5126040cb378SLi Xi raw_inode->i_projid = cpu_to_le32(i_projid); 5127040cb378SLi Xi 5128814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 5129202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 51301751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5131a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5132a26f4992STheodore Ts'o bh->b_data); 5133202ee5dfSTheodore Ts'o 51340390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 513573b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 5136ac27a0ecSDave Kleikamp if (!err) 5137ac27a0ecSDave Kleikamp err = rc; 513819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5139202ee5dfSTheodore Ts'o if (set_large_file) { 51405d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5141202ee5dfSTheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5142202ee5dfSTheodore Ts'o if (err) 5143202ee5dfSTheodore Ts'o goto out_brelse; 5144e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 5145202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5146202ee5dfSTheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 5147202ee5dfSTheodore Ts'o } 5148b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5149ac27a0ecSDave Kleikamp out_brelse: 5150ac27a0ecSDave Kleikamp brelse(bh); 5151617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5152ac27a0ecSDave Kleikamp return err; 5153ac27a0ecSDave Kleikamp } 5154ac27a0ecSDave Kleikamp 5155ac27a0ecSDave Kleikamp /* 5156617ba13bSMingming Cao * ext4_write_inode() 5157ac27a0ecSDave Kleikamp * 5158ac27a0ecSDave Kleikamp * We are called from a few places: 5159ac27a0ecSDave Kleikamp * 516087f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5161ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51624907cb7bSAnatol Pomozov * transaction to commit. 5163ac27a0ecSDave Kleikamp * 516487f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 516587f7e416STheodore Ts'o * We wait on commit, if told to. 5166ac27a0ecSDave Kleikamp * 516787f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 516887f7e416STheodore Ts'o * We wait on commit, if told to. 5169ac27a0ecSDave Kleikamp * 5170ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5171ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 517287f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 517387f7e416STheodore Ts'o * writeback. 5174ac27a0ecSDave Kleikamp * 5175ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5176ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5177ac27a0ecSDave Kleikamp * which we are interested. 5178ac27a0ecSDave Kleikamp * 5179ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5180ac27a0ecSDave Kleikamp * 5181ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5182ac27a0ecSDave Kleikamp * stuff(); 5183ac27a0ecSDave Kleikamp * inode->i_size = expr; 5184ac27a0ecSDave Kleikamp * 518587f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 518687f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 518787f7e416STheodore Ts'o * superblock's dirty inode list. 5188ac27a0ecSDave Kleikamp */ 5189a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5190ac27a0ecSDave Kleikamp { 519191ac6f43SFrank Mayhar int err; 519291ac6f43SFrank Mayhar 519318f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 519418f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5195ac27a0ecSDave Kleikamp return 0; 5196ac27a0ecSDave Kleikamp 519718f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 519818f2c4fcSTheodore Ts'o return -EIO; 519918f2c4fcSTheodore Ts'o 520091ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5201617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5202b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5203ac27a0ecSDave Kleikamp dump_stack(); 5204ac27a0ecSDave Kleikamp return -EIO; 5205ac27a0ecSDave Kleikamp } 5206ac27a0ecSDave Kleikamp 520710542c22SJan Kara /* 520810542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 520910542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 521010542c22SJan Kara * written. 521110542c22SJan Kara */ 521210542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5213ac27a0ecSDave Kleikamp return 0; 5214ac27a0ecSDave Kleikamp 5215aa75f4d3SHarshad Shirwadkar err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 521618f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 521791ac6f43SFrank Mayhar } else { 521891ac6f43SFrank Mayhar struct ext4_iloc iloc; 521991ac6f43SFrank Mayhar 52208016e29fSHarshad Shirwadkar err = __ext4_get_inode_loc_noinmem(inode, &iloc); 522191ac6f43SFrank Mayhar if (err) 522291ac6f43SFrank Mayhar return err; 522310542c22SJan Kara /* 522410542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 522510542c22SJan Kara * it here separately for each inode. 522610542c22SJan Kara */ 522710542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5228830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5229830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 523054d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5231c398eda0STheodore Ts'o "IO error syncing inode"); 5232830156c7SFrank Mayhar err = -EIO; 5233830156c7SFrank Mayhar } 5234fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 523591ac6f43SFrank Mayhar } 523691ac6f43SFrank Mayhar return err; 5237ac27a0ecSDave Kleikamp } 5238ac27a0ecSDave Kleikamp 5239ac27a0ecSDave Kleikamp /* 524053e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 524153e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 524253e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 524353e87268SJan Kara */ 524453e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 524553e87268SJan Kara { 524653e87268SJan Kara struct page *page; 524753e87268SJan Kara unsigned offset; 524853e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 524953e87268SJan Kara tid_t commit_tid = 0; 525053e87268SJan Kara int ret; 525153e87268SJan Kara 525209cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 525353e87268SJan Kara /* 5254565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5255565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5256565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5257565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5258565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5259565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5260565333a1Syangerkun * blocksize == PAGESIZE. 526153e87268SJan Kara */ 5262565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 526353e87268SJan Kara return; 526453e87268SJan Kara while (1) { 526553e87268SJan Kara page = find_lock_page(inode->i_mapping, 526609cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 526753e87268SJan Kara if (!page) 526853e87268SJan Kara return; 5269ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 527009cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 527153e87268SJan Kara unlock_page(page); 527209cbfeafSKirill A. Shutemov put_page(page); 527353e87268SJan Kara if (ret != -EBUSY) 527453e87268SJan Kara return; 527553e87268SJan Kara commit_tid = 0; 527653e87268SJan Kara read_lock(&journal->j_state_lock); 527753e87268SJan Kara if (journal->j_committing_transaction) 527853e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 527953e87268SJan Kara read_unlock(&journal->j_state_lock); 528053e87268SJan Kara if (commit_tid) 528153e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 528253e87268SJan Kara } 528353e87268SJan Kara } 528453e87268SJan Kara 528553e87268SJan Kara /* 5286617ba13bSMingming Cao * ext4_setattr() 5287ac27a0ecSDave Kleikamp * 5288ac27a0ecSDave Kleikamp * Called from notify_change. 5289ac27a0ecSDave Kleikamp * 5290ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5291ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5292ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5293ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5294ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5295ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5296ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5297ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5298ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5299ac27a0ecSDave Kleikamp * 5300678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5301678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5302678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5303678aaf48SJan Kara * This way we are sure that all the data written in the previous 5304678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5305678aaf48SJan Kara * writeback). 5306678aaf48SJan Kara * 5307678aaf48SJan Kara * Called with inode->i_mutex down. 5308ac27a0ecSDave Kleikamp */ 5309617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5310ac27a0ecSDave Kleikamp { 53112b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5312ac27a0ecSDave Kleikamp int error, rc = 0; 53133d287de3SDmitry Monakhov int orphan = 0; 5314ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5315ac27a0ecSDave Kleikamp 53160db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 53170db1ff22STheodore Ts'o return -EIO; 53180db1ff22STheodore Ts'o 531902b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 532002b016caSTheodore Ts'o return -EPERM; 532102b016caSTheodore Ts'o 532202b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 532302b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 532402b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 532502b016caSTheodore Ts'o return -EPERM; 532602b016caSTheodore Ts'o 532731051c85SJan Kara error = setattr_prepare(dentry, attr); 5328ac27a0ecSDave Kleikamp if (error) 5329ac27a0ecSDave Kleikamp return error; 5330ac27a0ecSDave Kleikamp 53313ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 53323ce2b8ddSEric Biggers if (error) 53333ce2b8ddSEric Biggers return error; 53343ce2b8ddSEric Biggers 5335c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5336c93d8f88SEric Biggers if (error) 5337c93d8f88SEric Biggers return error; 5338c93d8f88SEric Biggers 5339a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5340a7cdadeeSJan Kara error = dquot_initialize(inode); 5341a7cdadeeSJan Kara if (error) 5342a7cdadeeSJan Kara return error; 5343a7cdadeeSJan Kara } 5344aa75f4d3SHarshad Shirwadkar ext4_fc_start_update(inode); 534508cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 534608cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5347ac27a0ecSDave Kleikamp handle_t *handle; 5348ac27a0ecSDave Kleikamp 5349ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5350ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53519924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53529924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5353194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5354ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5355ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5356ac27a0ecSDave Kleikamp goto err_out; 5357ac27a0ecSDave Kleikamp } 53587a9ca53aSTahsin Erdogan 53597a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53607a9ca53aSTahsin Erdogan * counts xattr inode references. 53617a9ca53aSTahsin Erdogan */ 53627a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5363b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 53647a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53657a9ca53aSTahsin Erdogan 5366ac27a0ecSDave Kleikamp if (error) { 5367617ba13bSMingming Cao ext4_journal_stop(handle); 5368aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 5369ac27a0ecSDave Kleikamp return error; 5370ac27a0ecSDave Kleikamp } 5371ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5372ac27a0ecSDave Kleikamp * one transaction */ 5373ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5374ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5375ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5376ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5377617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5378617ba13bSMingming Cao ext4_journal_stop(handle); 53794209ae12SHarshad Shirwadkar if (unlikely(error)) 53804209ae12SHarshad Shirwadkar return error; 5381ac27a0ecSDave Kleikamp } 5382ac27a0ecSDave Kleikamp 53833da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53845208386cSJan Kara handle_t *handle; 53853da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5386b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5387562c72aaSChristoph Hellwig 538812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5389e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5390e2b46574SEric Sandeen 5391aa75f4d3SHarshad Shirwadkar if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5392aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 53930c095c7fSTheodore Ts'o return -EFBIG; 5394e2b46574SEric Sandeen } 5395aa75f4d3SHarshad Shirwadkar } 5396aa75f4d3SHarshad Shirwadkar if (!S_ISREG(inode->i_mode)) { 5397aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 53983da40c7bSJosef Bacik return -EINVAL; 5399aa75f4d3SHarshad Shirwadkar } 5400dff6efc3SChristoph Hellwig 5401dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5402dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5403dff6efc3SChristoph Hellwig 5404b9c1c267SJan Kara if (shrink) { 5405b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 54065208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 54075208386cSJan Kara attr->ia_size); 54085208386cSJan Kara if (error) 54095208386cSJan Kara goto err_out; 54105208386cSJan Kara } 5411b9c1c267SJan Kara /* 5412b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5413b9c1c267SJan Kara * for dio in flight. 5414b9c1c267SJan Kara */ 5415b9c1c267SJan Kara inode_dio_wait(inode); 5416b9c1c267SJan Kara } 5417b9c1c267SJan Kara 5418b9c1c267SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5419b9c1c267SJan Kara 5420b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5421b9c1c267SJan Kara if (rc) { 5422b9c1c267SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 5423aa75f4d3SHarshad Shirwadkar goto err_out; 5424b9c1c267SJan Kara } 5425b9c1c267SJan Kara 54263da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 54279924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5428ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5429ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5430b9c1c267SJan Kara goto out_mmap_sem; 5431ac27a0ecSDave Kleikamp } 54323da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5433617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 54343d287de3SDmitry Monakhov orphan = 1; 54353d287de3SDmitry Monakhov } 5436911af577SEryu Guan /* 5437911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5438911af577SEryu Guan * update c/mtime in shrink case below 5439911af577SEryu Guan */ 5440911af577SEryu Guan if (!shrink) { 5441eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5442911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5443911af577SEryu Guan } 5444aa75f4d3SHarshad Shirwadkar 5445aa75f4d3SHarshad Shirwadkar if (shrink) 5446a80f7fcfSHarshad Shirwadkar ext4_fc_track_range(handle, inode, 5447aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5448aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5449aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : 0) >> 5450aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5451aa75f4d3SHarshad Shirwadkar else 5452aa75f4d3SHarshad Shirwadkar ext4_fc_track_range( 5453a80f7fcfSHarshad Shirwadkar handle, inode, 5454aa75f4d3SHarshad Shirwadkar (oldsize > 0 ? oldsize - 1 : oldsize) >> 5455aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits, 5456aa75f4d3SHarshad Shirwadkar (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >> 5457aa75f4d3SHarshad Shirwadkar inode->i_sb->s_blocksize_bits); 5458aa75f4d3SHarshad Shirwadkar 545990e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5460617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5461617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5462ac27a0ecSDave Kleikamp if (!error) 5463ac27a0ecSDave Kleikamp error = rc; 546490e775b7SJan Kara /* 546590e775b7SJan Kara * We have to update i_size under i_data_sem together 546690e775b7SJan Kara * with i_disksize to avoid races with writeback code 546790e775b7SJan Kara * running ext4_wb_update_i_disksize(). 546890e775b7SJan Kara */ 546990e775b7SJan Kara if (!error) 547090e775b7SJan Kara i_size_write(inode, attr->ia_size); 547190e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5472617ba13bSMingming Cao ext4_journal_stop(handle); 5473b9c1c267SJan Kara if (error) 5474b9c1c267SJan Kara goto out_mmap_sem; 547582a25b02SJan Kara if (!shrink) { 5476b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5477b9c1c267SJan Kara inode->i_size); 5478b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 547982a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5480b9c1c267SJan Kara } 5481430657b6SRoss Zwisler } 5482430657b6SRoss Zwisler 548353e87268SJan Kara /* 548453e87268SJan Kara * Truncate pagecache after we've waited for commit 548553e87268SJan Kara * in data=journal mode to make pages freeable. 548653e87268SJan Kara */ 54877caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5488b9c1c267SJan Kara /* 5489b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5490b9c1c267SJan Kara * truncate possible preallocated blocks. 5491b9c1c267SJan Kara */ 5492b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54932c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54942c98eb5eSTheodore Ts'o if (rc) 54952c98eb5eSTheodore Ts'o error = rc; 54962c98eb5eSTheodore Ts'o } 5497b9c1c267SJan Kara out_mmap_sem: 5498ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 54993da40c7bSJosef Bacik } 5500ac27a0ecSDave Kleikamp 55012c98eb5eSTheodore Ts'o if (!error) { 55021025774cSChristoph Hellwig setattr_copy(inode, attr); 55031025774cSChristoph Hellwig mark_inode_dirty(inode); 55041025774cSChristoph Hellwig } 55051025774cSChristoph Hellwig 55061025774cSChristoph Hellwig /* 55071025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 55081025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 55091025774cSChristoph Hellwig */ 55103d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5511617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5512ac27a0ecSDave Kleikamp 55132c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 551464e178a7SChristoph Hellwig rc = posix_acl_chmod(inode, inode->i_mode); 5515ac27a0ecSDave Kleikamp 5516ac27a0ecSDave Kleikamp err_out: 5517aa75f4d3SHarshad Shirwadkar if (error) 5518617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5519ac27a0ecSDave Kleikamp if (!error) 5520ac27a0ecSDave Kleikamp error = rc; 5521aa75f4d3SHarshad Shirwadkar ext4_fc_stop_update(inode); 5522ac27a0ecSDave Kleikamp return error; 5523ac27a0ecSDave Kleikamp } 5524ac27a0ecSDave Kleikamp 5525a528d35eSDavid Howells int ext4_getattr(const struct path *path, struct kstat *stat, 5526a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 55273e3398a0SMingming Cao { 552899652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 552999652ea5SDavid Howells struct ext4_inode *raw_inode; 553099652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 553199652ea5SDavid Howells unsigned int flags; 55323e3398a0SMingming Cao 5533d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5534d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 553599652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 553699652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 553799652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 553899652ea5SDavid Howells } 553999652ea5SDavid Howells 554099652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 554199652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 554299652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 554399652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 554499652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 554599652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 554699652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 554799652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 554899652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 554999652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 555099652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 55511f607195SEric Biggers if (flags & EXT4_VERITY_FL) 55521f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 555399652ea5SDavid Howells 55543209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 55553209f68bSDavid Howells STATX_ATTR_COMPRESSED | 55563209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 55573209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 55581f607195SEric Biggers STATX_ATTR_NODUMP | 55591f607195SEric Biggers STATX_ATTR_VERITY); 55603209f68bSDavid Howells 55613e3398a0SMingming Cao generic_fillattr(inode, stat); 556299652ea5SDavid Howells return 0; 556399652ea5SDavid Howells } 556499652ea5SDavid Howells 556599652ea5SDavid Howells int ext4_file_getattr(const struct path *path, struct kstat *stat, 556699652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 556799652ea5SDavid Howells { 556899652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 556999652ea5SDavid Howells u64 delalloc_blocks; 557099652ea5SDavid Howells 557199652ea5SDavid Howells ext4_getattr(path, stat, request_mask, query_flags); 55723e3398a0SMingming Cao 55733e3398a0SMingming Cao /* 55749206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 55759206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 55769206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5577d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 55789206c561SAndreas Dilger */ 55799206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 55809206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 55819206c561SAndreas Dilger 55829206c561SAndreas Dilger /* 55833e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 55843e3398a0SMingming Cao * otherwise in the case of system crash before the real block 55853e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 55863e3398a0SMingming Cao * on-disk file blocks. 55873e3398a0SMingming Cao * We always keep i_blocks updated together with real 55883e3398a0SMingming Cao * allocation. But to not confuse with user, stat 55893e3398a0SMingming Cao * will return the blocks that include the delayed allocation 55903e3398a0SMingming Cao * blocks for this file. 55913e3398a0SMingming Cao */ 559296607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 559396607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55948af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55953e3398a0SMingming Cao return 0; 55963e3398a0SMingming Cao } 5597ac27a0ecSDave Kleikamp 5598fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5599fffb2739SJan Kara int pextents) 5600a02908f1SMingming Cao { 560112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5602fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5603fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5604a02908f1SMingming Cao } 5605ac51d837STheodore Ts'o 5606a02908f1SMingming Cao /* 5607a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5608a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5609a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5610a02908f1SMingming Cao * 5611a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 56124907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5613a02908f1SMingming Cao * they could still across block group boundary. 5614a02908f1SMingming Cao * 5615a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5616a02908f1SMingming Cao */ 5617dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5618fffb2739SJan Kara int pextents) 5619a02908f1SMingming Cao { 56208df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56218df9675fSTheodore Ts'o int gdpblocks; 5622a02908f1SMingming Cao int idxblocks; 5623a02908f1SMingming Cao int ret = 0; 5624a02908f1SMingming Cao 5625a02908f1SMingming Cao /* 5626fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5627fffb2739SJan Kara * to @pextents physical extents? 5628a02908f1SMingming Cao */ 5629fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5630a02908f1SMingming Cao 5631a02908f1SMingming Cao ret = idxblocks; 5632a02908f1SMingming Cao 5633a02908f1SMingming Cao /* 5634a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5635a02908f1SMingming Cao * to account 5636a02908f1SMingming Cao */ 5637fffb2739SJan Kara groups = idxblocks + pextents; 5638a02908f1SMingming Cao gdpblocks = groups; 56398df9675fSTheodore Ts'o if (groups > ngroups) 56408df9675fSTheodore Ts'o groups = ngroups; 5641a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5642a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5643a02908f1SMingming Cao 5644a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5645a02908f1SMingming Cao ret += groups + gdpblocks; 5646a02908f1SMingming Cao 5647a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5648a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5649ac27a0ecSDave Kleikamp 5650ac27a0ecSDave Kleikamp return ret; 5651ac27a0ecSDave Kleikamp } 5652ac27a0ecSDave Kleikamp 5653ac27a0ecSDave Kleikamp /* 565425985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5655f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5656f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5657a02908f1SMingming Cao * 5658525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5659a02908f1SMingming Cao * 5660525f4ed8SMingming Cao * We need to consider the worse case, when 5661a02908f1SMingming Cao * one new block per extent. 5662a02908f1SMingming Cao */ 5663a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5664a02908f1SMingming Cao { 5665a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5666a02908f1SMingming Cao int ret; 5667a02908f1SMingming Cao 5668fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5669a02908f1SMingming Cao 5670a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5671a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5672a02908f1SMingming Cao ret += bpp; 5673a02908f1SMingming Cao return ret; 5674a02908f1SMingming Cao } 5675f3bd1f3fSMingming Cao 5676f3bd1f3fSMingming Cao /* 5677f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5678f3bd1f3fSMingming Cao * 5679f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 568079e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5681f3bd1f3fSMingming Cao * 5682f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5683f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5684f3bd1f3fSMingming Cao */ 5685f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5686f3bd1f3fSMingming Cao { 5687f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5688f3bd1f3fSMingming Cao } 5689f3bd1f3fSMingming Cao 5690a02908f1SMingming Cao /* 5691617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5692ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5693ac27a0ecSDave Kleikamp */ 5694617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5695617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5696ac27a0ecSDave Kleikamp { 5697ac27a0ecSDave Kleikamp int err = 0; 5698ac27a0ecSDave Kleikamp 5699a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5700a6758309SVasily Averin put_bh(iloc->bh); 57010db1ff22STheodore Ts'o return -EIO; 5702a6758309SVasily Averin } 5703a80f7fcfSHarshad Shirwadkar ext4_fc_track_inode(handle, inode); 5704aa75f4d3SHarshad Shirwadkar 5705c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 570625ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 570725ec56b5SJean Noel Cordenner 5708ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5709ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5710ac27a0ecSDave Kleikamp 5711dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5712830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5713ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5714ac27a0ecSDave Kleikamp return err; 5715ac27a0ecSDave Kleikamp } 5716ac27a0ecSDave Kleikamp 5717ac27a0ecSDave Kleikamp /* 5718ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5719ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5720ac27a0ecSDave Kleikamp */ 5721ac27a0ecSDave Kleikamp 5722ac27a0ecSDave Kleikamp int 5723617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5724617ba13bSMingming Cao struct ext4_iloc *iloc) 5725ac27a0ecSDave Kleikamp { 57260390131bSFrank Mayhar int err; 57270390131bSFrank Mayhar 57280db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 57290db1ff22STheodore Ts'o return -EIO; 57300db1ff22STheodore Ts'o 5731617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5732ac27a0ecSDave Kleikamp if (!err) { 5733ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5734617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5735ac27a0ecSDave Kleikamp if (err) { 5736ac27a0ecSDave Kleikamp brelse(iloc->bh); 5737ac27a0ecSDave Kleikamp iloc->bh = NULL; 5738ac27a0ecSDave Kleikamp } 5739ac27a0ecSDave Kleikamp } 5740617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5741ac27a0ecSDave Kleikamp return err; 5742ac27a0ecSDave Kleikamp } 5743ac27a0ecSDave Kleikamp 5744c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5745c03b45b8SMiao Xie unsigned int new_extra_isize, 5746c03b45b8SMiao Xie struct ext4_iloc *iloc, 5747c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5748c03b45b8SMiao Xie { 5749c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5750c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 57514ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 57524ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5753c03b45b8SMiao Xie int error; 5754c03b45b8SMiao Xie 57554ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 57564ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 57574ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 57584ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 57594ea99936STheodore Ts'o ei->i_extra_isize, 57604ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 57614ea99936STheodore Ts'o return -EFSCORRUPTED; 57624ea99936STheodore Ts'o } 57634ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 57644ea99936STheodore Ts'o (new_extra_isize < 4) || 57654ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 57664ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 57674ea99936STheodore Ts'o 5768c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5769c03b45b8SMiao Xie 5770c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5771c03b45b8SMiao Xie 5772c03b45b8SMiao Xie /* No extended attributes present */ 5773c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5774c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5775c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5776c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5777c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5778c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5779c03b45b8SMiao Xie return 0; 5780c03b45b8SMiao Xie } 5781c03b45b8SMiao Xie 5782c03b45b8SMiao Xie /* try to expand with EAs present */ 5783c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5784c03b45b8SMiao Xie raw_inode, handle); 5785c03b45b8SMiao Xie if (error) { 5786c03b45b8SMiao Xie /* 5787c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5788c03b45b8SMiao Xie */ 5789c03b45b8SMiao Xie *no_expand = 1; 5790c03b45b8SMiao Xie } 5791c03b45b8SMiao Xie 5792c03b45b8SMiao Xie return error; 5793c03b45b8SMiao Xie } 5794c03b45b8SMiao Xie 5795ac27a0ecSDave Kleikamp /* 57966dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57976dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 57986dd4ee7cSKalpak Shah */ 5799cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 58001d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 58011d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 58021d03ec98SAneesh Kumar K.V handle_t *handle) 58036dd4ee7cSKalpak Shah { 58043b10fdc6SMiao Xie int no_expand; 58053b10fdc6SMiao Xie int error; 58066dd4ee7cSKalpak Shah 5807cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5808cf0a5e81SMiao Xie return -EOVERFLOW; 5809cf0a5e81SMiao Xie 5810cf0a5e81SMiao Xie /* 5811cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5812cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5813cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5814cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5815cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5816cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5817cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5818cf0a5e81SMiao Xie */ 58196cb367c2SJan Kara if (ext4_journal_extend(handle, 582083448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5821cf0a5e81SMiao Xie return -ENOSPC; 58226dd4ee7cSKalpak Shah 58233b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5824cf0a5e81SMiao Xie return -EBUSY; 58253b10fdc6SMiao Xie 5826c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5827c03b45b8SMiao Xie handle, &no_expand); 58283b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5829c03b45b8SMiao Xie 5830c03b45b8SMiao Xie return error; 58316dd4ee7cSKalpak Shah } 58326dd4ee7cSKalpak Shah 5833c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5834c03b45b8SMiao Xie unsigned int new_extra_isize, 5835c03b45b8SMiao Xie struct ext4_iloc *iloc) 5836c03b45b8SMiao Xie { 5837c03b45b8SMiao Xie handle_t *handle; 5838c03b45b8SMiao Xie int no_expand; 5839c03b45b8SMiao Xie int error, rc; 5840c03b45b8SMiao Xie 5841c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5842c03b45b8SMiao Xie brelse(iloc->bh); 5843c03b45b8SMiao Xie return -EOVERFLOW; 5844c03b45b8SMiao Xie } 5845c03b45b8SMiao Xie 5846c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5847c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5848c03b45b8SMiao Xie if (IS_ERR(handle)) { 5849c03b45b8SMiao Xie error = PTR_ERR(handle); 5850c03b45b8SMiao Xie brelse(iloc->bh); 5851c03b45b8SMiao Xie return error; 5852c03b45b8SMiao Xie } 5853c03b45b8SMiao Xie 5854c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5855c03b45b8SMiao Xie 5856ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5857c03b45b8SMiao Xie error = ext4_journal_get_write_access(handle, iloc->bh); 58583b10fdc6SMiao Xie if (error) { 5859c03b45b8SMiao Xie brelse(iloc->bh); 58607f420d64SDan Carpenter goto out_unlock; 58613b10fdc6SMiao Xie } 5862cf0a5e81SMiao Xie 5863c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5864c03b45b8SMiao Xie handle, &no_expand); 5865c03b45b8SMiao Xie 5866c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5867c03b45b8SMiao Xie if (!error) 5868c03b45b8SMiao Xie error = rc; 5869c03b45b8SMiao Xie 58707f420d64SDan Carpenter out_unlock: 5871c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5872c03b45b8SMiao Xie ext4_journal_stop(handle); 58733b10fdc6SMiao Xie return error; 58746dd4ee7cSKalpak Shah } 58756dd4ee7cSKalpak Shah 58766dd4ee7cSKalpak Shah /* 5877ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5878ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5879ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5880ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5881ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5882ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5883ac27a0ecSDave Kleikamp * 5884ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5885ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5886ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5887ac27a0ecSDave Kleikamp * we start and wait on commits. 5888ac27a0ecSDave Kleikamp */ 58894209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 58904209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5891ac27a0ecSDave Kleikamp { 5892617ba13bSMingming Cao struct ext4_iloc iloc; 58936dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5894cf0a5e81SMiao Xie int err; 5895ac27a0ecSDave Kleikamp 5896ac27a0ecSDave Kleikamp might_sleep(); 58977ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5898617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58995e1021f2SEryu Guan if (err) 59004209ae12SHarshad Shirwadkar goto out; 5901cf0a5e81SMiao Xie 5902cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5903cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 59046dd4ee7cSKalpak Shah iloc, handle); 5905cf0a5e81SMiao Xie 59064209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 59074209ae12SHarshad Shirwadkar out: 59084209ae12SHarshad Shirwadkar if (unlikely(err)) 59094209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 59104209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 59114209ae12SHarshad Shirwadkar return err; 5912ac27a0ecSDave Kleikamp } 5913ac27a0ecSDave Kleikamp 5914ac27a0ecSDave Kleikamp /* 5915617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5916ac27a0ecSDave Kleikamp * 5917ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5918ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5919ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5920ac27a0ecSDave Kleikamp * 59215dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5922ac27a0ecSDave Kleikamp * are allocated to the file. 5923ac27a0ecSDave Kleikamp * 5924ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5925ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5926ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 59270ae45f63STheodore Ts'o * 59280ae45f63STheodore Ts'o * If only the I_DIRTY_TIME flag is set, we can skip everything. If 59290ae45f63STheodore Ts'o * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need 59300ae45f63STheodore Ts'o * to copy into the on-disk inode structure are the timestamp files. 5931ac27a0ecSDave Kleikamp */ 5932aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5933ac27a0ecSDave Kleikamp { 5934ac27a0ecSDave Kleikamp handle_t *handle; 5935ac27a0ecSDave Kleikamp 59360ae45f63STheodore Ts'o if (flags == I_DIRTY_TIME) 59370ae45f63STheodore Ts'o return; 59389924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5939ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5940ac27a0ecSDave Kleikamp goto out; 5941f3dc272fSCurt Wohlgemuth 5942617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5943f3dc272fSCurt Wohlgemuth 5944617ba13bSMingming Cao ext4_journal_stop(handle); 5945ac27a0ecSDave Kleikamp out: 5946ac27a0ecSDave Kleikamp return; 5947ac27a0ecSDave Kleikamp } 5948ac27a0ecSDave Kleikamp 5949617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5950ac27a0ecSDave Kleikamp { 5951ac27a0ecSDave Kleikamp journal_t *journal; 5952ac27a0ecSDave Kleikamp handle_t *handle; 5953ac27a0ecSDave Kleikamp int err; 5954c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5955ac27a0ecSDave Kleikamp 5956ac27a0ecSDave Kleikamp /* 5957ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5958ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5959ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5960ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5961ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5962ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5963ac27a0ecSDave Kleikamp * nobody is changing anything. 5964ac27a0ecSDave Kleikamp */ 5965ac27a0ecSDave Kleikamp 5966617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59670390131bSFrank Mayhar if (!journal) 59680390131bSFrank Mayhar return 0; 5969d699594dSDave Hansen if (is_journal_aborted(journal)) 5970ac27a0ecSDave Kleikamp return -EROFS; 5971ac27a0ecSDave Kleikamp 597217335dccSDmitry Monakhov /* Wait for all existing dio workers */ 597317335dccSDmitry Monakhov inode_dio_wait(inode); 597417335dccSDmitry Monakhov 59754c546592SDaeho Jeong /* 59764c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 59774c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 59784c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 59794c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 59804c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 59814c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 59824c546592SDaeho Jeong */ 59834c546592SDaeho Jeong if (val) { 59844c546592SDaeho Jeong down_write(&EXT4_I(inode)->i_mmap_sem); 59854c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 59864c546592SDaeho Jeong if (err < 0) { 59874c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 59884c546592SDaeho Jeong return err; 59894c546592SDaeho Jeong } 59904c546592SDaeho Jeong } 59914c546592SDaeho Jeong 5992bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 5993dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5994ac27a0ecSDave Kleikamp 5995ac27a0ecSDave Kleikamp /* 5996ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5997ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5998ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5999ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 6000ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 6001ac27a0ecSDave Kleikamp */ 6002ac27a0ecSDave Kleikamp 6003ac27a0ecSDave Kleikamp if (val) 600412e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 60055872ddaaSYongqiang Yang else { 60064f879ca6SJan Kara err = jbd2_journal_flush(journal); 60074f879ca6SJan Kara if (err < 0) { 60084f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 6009bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 60104f879ca6SJan Kara return err; 60114f879ca6SJan Kara } 601212e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 60135872ddaaSYongqiang Yang } 6014617ba13bSMingming Cao ext4_set_aops(inode); 6015ac27a0ecSDave Kleikamp 6016dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 6017bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 6018c8585c6fSDaeho Jeong 60194c546592SDaeho Jeong if (val) 60204c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 6021ac27a0ecSDave Kleikamp 6022ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6023ac27a0ecSDave Kleikamp 60249924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 6025ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6026ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6027ac27a0ecSDave Kleikamp 6028aa75f4d3SHarshad Shirwadkar ext4_fc_mark_ineligible(inode->i_sb, 6029aa75f4d3SHarshad Shirwadkar EXT4_FC_REASON_JOURNAL_FLAG_CHANGE); 6030617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60310390131bSFrank Mayhar ext4_handle_sync(handle); 6032617ba13bSMingming Cao ext4_journal_stop(handle); 6033617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6034ac27a0ecSDave Kleikamp 6035ac27a0ecSDave Kleikamp return err; 6036ac27a0ecSDave Kleikamp } 60372e9ee850SAneesh Kumar K.V 60382e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 60392e9ee850SAneesh Kumar K.V { 60402e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60412e9ee850SAneesh Kumar K.V } 60422e9ee850SAneesh Kumar K.V 6043401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 60442e9ee850SAneesh Kumar K.V { 604511bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 6046c2ec175cSNick Piggin struct page *page = vmf->page; 60472e9ee850SAneesh Kumar K.V loff_t size; 60482e9ee850SAneesh Kumar K.V unsigned long len; 6049401b25aaSSouptick Joarder int err; 6050401b25aaSSouptick Joarder vm_fault_t ret; 60512e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 6052496ad9aaSAl Viro struct inode *inode = file_inode(file); 60532e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60549ea7df53SJan Kara handle_t *handle; 60559ea7df53SJan Kara get_block_t *get_block; 60569ea7df53SJan Kara int retries = 0; 60572e9ee850SAneesh Kumar K.V 605802b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 605902b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 606002b016caSTheodore Ts'o 60618e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 6062041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 6063ea3d7209SJan Kara 6064ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 60657b4cc978SEric Biggers 6066401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 6067401b25aaSSouptick Joarder if (err) 60687b4cc978SEric Biggers goto out_ret; 60697b4cc978SEric Biggers 607064a9f144SMauricio Faria de Oliveira /* 607164a9f144SMauricio Faria de Oliveira * On data journalling we skip straight to the transaction handle: 607264a9f144SMauricio Faria de Oliveira * there's no delalloc; page truncated will be checked later; the 607364a9f144SMauricio Faria de Oliveira * early return w/ all buffers mapped (calculates size/len) can't 607464a9f144SMauricio Faria de Oliveira * be used; and there's no dioread_nolock, so only ext4_get_block. 607564a9f144SMauricio Faria de Oliveira */ 607664a9f144SMauricio Faria de Oliveira if (ext4_should_journal_data(inode)) 607764a9f144SMauricio Faria de Oliveira goto retry_alloc; 607864a9f144SMauricio Faria de Oliveira 60799ea7df53SJan Kara /* Delalloc case is easy... */ 60809ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 60819ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 60829ea7df53SJan Kara do { 6083401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 60849ea7df53SJan Kara ext4_da_get_block_prep); 6085401b25aaSSouptick Joarder } while (err == -ENOSPC && 60869ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 60879ea7df53SJan Kara goto out_ret; 60882e9ee850SAneesh Kumar K.V } 60890e499890SDarrick J. Wong 60900e499890SDarrick J. Wong lock_page(page); 60919ea7df53SJan Kara size = i_size_read(inode); 60929ea7df53SJan Kara /* Page got truncated from under us? */ 60939ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 60949ea7df53SJan Kara unlock_page(page); 60959ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 60969ea7df53SJan Kara goto out; 60970e499890SDarrick J. Wong } 60982e9ee850SAneesh Kumar K.V 609909cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 610009cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 61012e9ee850SAneesh Kumar K.V else 610209cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6103a827eaffSAneesh Kumar K.V /* 61049ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 61059ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 610664a9f144SMauricio Faria de Oliveira * 610764a9f144SMauricio Faria de Oliveira * This cannot be done for data journalling, as we have to add the 610864a9f144SMauricio Faria de Oliveira * inode to the transaction's list to writeprotect pages on commit. 6109a827eaffSAneesh Kumar K.V */ 61102e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6111f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 6112f19d5870STao Ma 0, len, NULL, 6113a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 61149ea7df53SJan Kara /* Wait so that we don't change page under IO */ 61151d1d1a76SDarrick J. Wong wait_for_stable_page(page); 61169ea7df53SJan Kara ret = VM_FAULT_LOCKED; 61179ea7df53SJan Kara goto out; 61182e9ee850SAneesh Kumar K.V } 6119a827eaffSAneesh Kumar K.V } 6120a827eaffSAneesh Kumar K.V unlock_page(page); 61219ea7df53SJan Kara /* OK, we need to fill the hole... */ 61229ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6123705965bdSJan Kara get_block = ext4_get_block_unwritten; 61249ea7df53SJan Kara else 61259ea7df53SJan Kara get_block = ext4_get_block; 61269ea7df53SJan Kara retry_alloc: 61279924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 61289924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 61299ea7df53SJan Kara if (IS_ERR(handle)) { 6130c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 61319ea7df53SJan Kara goto out; 61329ea7df53SJan Kara } 613364a9f144SMauricio Faria de Oliveira /* 613464a9f144SMauricio Faria de Oliveira * Data journalling can't use block_page_mkwrite() because it 613564a9f144SMauricio Faria de Oliveira * will set_buffer_dirty() before do_journal_get_write_access() 613664a9f144SMauricio Faria de Oliveira * thus might hit warning messages for dirty metadata buffers. 613764a9f144SMauricio Faria de Oliveira */ 613864a9f144SMauricio Faria de Oliveira if (!ext4_should_journal_data(inode)) { 6139401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 614064a9f144SMauricio Faria de Oliveira } else { 614164a9f144SMauricio Faria de Oliveira lock_page(page); 614264a9f144SMauricio Faria de Oliveira size = i_size_read(inode); 614364a9f144SMauricio Faria de Oliveira /* Page got truncated from under us? */ 614464a9f144SMauricio Faria de Oliveira if (page->mapping != mapping || page_offset(page) > size) { 614564a9f144SMauricio Faria de Oliveira ret = VM_FAULT_NOPAGE; 6146afb585a9SMauricio Faria de Oliveira goto out_error; 614764a9f144SMauricio Faria de Oliveira } 614864a9f144SMauricio Faria de Oliveira 614964a9f144SMauricio Faria de Oliveira if (page->index == size >> PAGE_SHIFT) 615064a9f144SMauricio Faria de Oliveira len = size & ~PAGE_MASK; 615164a9f144SMauricio Faria de Oliveira else 615264a9f144SMauricio Faria de Oliveira len = PAGE_SIZE; 615364a9f144SMauricio Faria de Oliveira 615464a9f144SMauricio Faria de Oliveira err = __block_write_begin(page, 0, len, ext4_get_block); 615564a9f144SMauricio Faria de Oliveira if (!err) { 61569ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6157afb585a9SMauricio Faria de Oliveira if (ext4_walk_page_buffers(handle, page_buffers(page), 6158afb585a9SMauricio Faria de Oliveira 0, len, NULL, do_journal_get_write_access)) 6159afb585a9SMauricio Faria de Oliveira goto out_error; 6160afb585a9SMauricio Faria de Oliveira if (ext4_walk_page_buffers(handle, page_buffers(page), 6161afb585a9SMauricio Faria de Oliveira 0, len, NULL, write_end_fn)) 6162afb585a9SMauricio Faria de Oliveira goto out_error; 6163b5b18160SJan Kara if (ext4_jbd2_inode_add_write(handle, inode, 6164b5b18160SJan Kara page_offset(page), len)) 6165afb585a9SMauricio Faria de Oliveira goto out_error; 61669ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 616764a9f144SMauricio Faria de Oliveira } else { 616864a9f144SMauricio Faria de Oliveira unlock_page(page); 616964a9f144SMauricio Faria de Oliveira } 61709ea7df53SJan Kara } 61719ea7df53SJan Kara ext4_journal_stop(handle); 6172401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 61739ea7df53SJan Kara goto retry_alloc; 61749ea7df53SJan Kara out_ret: 6175401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 61769ea7df53SJan Kara out: 6177ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 61788e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 61792e9ee850SAneesh Kumar K.V return ret; 6180afb585a9SMauricio Faria de Oliveira out_error: 6181afb585a9SMauricio Faria de Oliveira unlock_page(page); 6182afb585a9SMauricio Faria de Oliveira ext4_journal_stop(handle); 6183afb585a9SMauricio Faria de Oliveira goto out; 61842e9ee850SAneesh Kumar K.V } 6185ea3d7209SJan Kara 6186401b25aaSSouptick Joarder vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 6187ea3d7209SJan Kara { 618811bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file); 6189401b25aaSSouptick Joarder vm_fault_t ret; 6190ea3d7209SJan Kara 6191ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 6192401b25aaSSouptick Joarder ret = filemap_fault(vmf); 6193ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 6194ea3d7209SJan Kara 6195401b25aaSSouptick Joarder return ret; 6196ea3d7209SJan Kara } 6197