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 104814525f4SDarrick J. Wong static 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: 3300930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 331ac27a0ecSDave Kleikamp } 332ac27a0ecSDave Kleikamp 333a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 334a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 33560e58e0fSMingming Cao { 336a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 33760e58e0fSMingming Cao } 338a9e7f447SDmitry Monakhov #endif 3399d0be502STheodore Ts'o 34012219aeaSAneesh Kumar K.V /* 3410637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3420637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3430637c6f4STheodore Ts'o */ 3445f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3455f634d06SAneesh Kumar K.V int used, int quota_claim) 34612219aeaSAneesh Kumar K.V { 34712219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3480637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34912219aeaSAneesh Kumar K.V 3500637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 351d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3520637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3538de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3541084f252STheodore Ts'o "with only %d reserved data blocks", 3550637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3560637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3570637c6f4STheodore Ts'o WARN_ON(1); 3580637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3596bc6e63fSAneesh Kumar K.V } 36012219aeaSAneesh Kumar K.V 3610637c6f4STheodore Ts'o /* Update per-inode reservations */ 3620637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 36371d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3640637c6f4STheodore Ts'o 36512219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 36660e58e0fSMingming Cao 36772b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 36872b8ab9dSEric Sandeen if (quota_claim) 3697b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 37072b8ab9dSEric Sandeen else { 3715f634d06SAneesh Kumar K.V /* 3725f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3735f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 37472b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3755f634d06SAneesh Kumar K.V */ 3767b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3775f634d06SAneesh Kumar K.V } 378d6014301SAneesh Kumar K.V 379d6014301SAneesh Kumar K.V /* 380d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 381d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 382d6014301SAneesh Kumar K.V * inode's preallocations. 383d6014301SAneesh Kumar K.V */ 3840637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 38582dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 38627bc446eSbrookxu ext4_discard_preallocations(inode, 0); 38712219aeaSAneesh Kumar K.V } 38812219aeaSAneesh Kumar K.V 389e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 390c398eda0STheodore Ts'o unsigned int line, 39124676da4STheodore Ts'o struct ext4_map_blocks *map) 3926fd058f7STheodore Ts'o { 393345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 394345c0dbfSTheodore Ts'o (inode->i_ino == 395345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 396345c0dbfSTheodore Ts'o return 0; 397ce9f24ccSJan Kara if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) { 398c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 399bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 40024676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 401bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4026a797d27SDarrick J. Wong return -EFSCORRUPTED; 4036fd058f7STheodore Ts'o } 4046fd058f7STheodore Ts'o return 0; 4056fd058f7STheodore Ts'o } 4066fd058f7STheodore Ts'o 40753085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 40853085facSJan Kara ext4_lblk_t len) 40953085facSJan Kara { 41053085facSJan Kara int ret; 41153085facSJan Kara 41233b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 413a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 41453085facSJan Kara 41553085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 41653085facSJan Kara if (ret > 0) 41753085facSJan Kara ret = 0; 41853085facSJan Kara 41953085facSJan Kara return ret; 42053085facSJan Kara } 42153085facSJan Kara 422e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 423c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 424e29136f8STheodore Ts'o 425921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 426921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 427921f266bSDmitry Monakhov struct inode *inode, 428921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 429921f266bSDmitry Monakhov struct ext4_map_blocks *map, 430921f266bSDmitry Monakhov int flags) 431921f266bSDmitry Monakhov { 432921f266bSDmitry Monakhov int retval; 433921f266bSDmitry Monakhov 434921f266bSDmitry Monakhov map->m_flags = 0; 435921f266bSDmitry Monakhov /* 436921f266bSDmitry Monakhov * There is a race window that the result is not the same. 437921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 438921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 439921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 440921f266bSDmitry Monakhov * could be converted. 441921f266bSDmitry Monakhov */ 442c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 443921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4449e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 445921f266bSDmitry Monakhov } else { 4469e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 447921f266bSDmitry Monakhov } 448921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 449921f266bSDmitry Monakhov 450921f266bSDmitry Monakhov /* 451921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 452921f266bSDmitry Monakhov * tree. So the m_len might not equal. 453921f266bSDmitry Monakhov */ 454921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 455921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 456921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 457bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 458921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 459921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 460921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 461921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 462921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 463921f266bSDmitry Monakhov retval, flags); 464921f266bSDmitry Monakhov } 465921f266bSDmitry Monakhov } 466921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 467921f266bSDmitry Monakhov 46855138e0bSTheodore Ts'o /* 469e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4702b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 471f5ab0d1fSMingming Cao * 472f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 473f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 474f5ab0d1fSMingming Cao * mapped. 475f5ab0d1fSMingming Cao * 476e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 477e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 478f5ab0d1fSMingming Cao * based files 479f5ab0d1fSMingming Cao * 480facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 481facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 482facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 483f5ab0d1fSMingming Cao * 484f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 485facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 486facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 487f5ab0d1fSMingming Cao * 488f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 489f5ab0d1fSMingming Cao */ 490e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 491e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4920e855ac8SAneesh Kumar K.V { 493d100eef2SZheng Liu struct extent_status es; 4940e855ac8SAneesh Kumar K.V int retval; 495b8a86845SLukas Czerner int ret = 0; 496921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 497921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 498921f266bSDmitry Monakhov 499921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 500921f266bSDmitry Monakhov #endif 501f5ab0d1fSMingming Cao 502e35fd660STheodore Ts'o map->m_flags = 0; 50370aa1554SRitesh Harjani ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n", 50470aa1554SRitesh Harjani flags, map->m_len, (unsigned long) map->m_lblk); 505d100eef2SZheng Liu 506e861b5e9STheodore Ts'o /* 507e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 508e861b5e9STheodore Ts'o */ 509e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 510e861b5e9STheodore Ts'o map->m_len = INT_MAX; 511e861b5e9STheodore Ts'o 5124adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5134adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5146a797d27SDarrick J. Wong return -EFSCORRUPTED; 5154adb6ab3SKazuya Mio 516d100eef2SZheng Liu /* Lookup extent status tree firstly */ 517bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 518d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 519d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 520d100eef2SZheng Liu map->m_lblk - es.es_lblk; 521d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 522d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 523d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 524d100eef2SZheng Liu if (retval > map->m_len) 525d100eef2SZheng Liu retval = map->m_len; 526d100eef2SZheng Liu map->m_len = retval; 527d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 528facab4d9SJan Kara map->m_pblk = 0; 529facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 530facab4d9SJan Kara if (retval > map->m_len) 531facab4d9SJan Kara retval = map->m_len; 532facab4d9SJan Kara map->m_len = retval; 533d100eef2SZheng Liu retval = 0; 534d100eef2SZheng Liu } else { 5351e83bc81SArnd Bergmann BUG(); 536d100eef2SZheng Liu } 537921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 538921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 539921f266bSDmitry Monakhov &orig_map, flags); 540921f266bSDmitry Monakhov #endif 541d100eef2SZheng Liu goto found; 542d100eef2SZheng Liu } 543d100eef2SZheng Liu 5444df3d265SAneesh Kumar K.V /* 545b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 546b920c755STheodore Ts'o * file system block. 5474df3d265SAneesh Kumar K.V */ 548c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 54912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5509e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5514df3d265SAneesh Kumar K.V } else { 5529e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5530e855ac8SAneesh Kumar K.V } 554f7fec032SZheng Liu if (retval > 0) { 5553be78c73STheodore Ts'o unsigned int status; 556f7fec032SZheng Liu 55744fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 55844fb851dSZheng Liu ext4_warning(inode->i_sb, 55944fb851dSZheng Liu "ES len assertion failed for inode " 56044fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 56144fb851dSZheng Liu inode->i_ino, retval, map->m_len); 56244fb851dSZheng Liu WARN_ON(1); 563921f266bSDmitry Monakhov } 564921f266bSDmitry Monakhov 565f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 566f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 567f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 568d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 569ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 570f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 571f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 572f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 573f7fec032SZheng Liu map->m_len, map->m_pblk, status); 574f7fec032SZheng Liu if (ret < 0) 575f7fec032SZheng Liu retval = ret; 576f7fec032SZheng Liu } 5774df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 578f5ab0d1fSMingming Cao 579d100eef2SZheng Liu found: 580e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 581b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5826fd058f7STheodore Ts'o if (ret != 0) 5836fd058f7STheodore Ts'o return ret; 5846fd058f7STheodore Ts'o } 5856fd058f7STheodore Ts'o 586f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 587c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5884df3d265SAneesh Kumar K.V return retval; 5894df3d265SAneesh Kumar K.V 5904df3d265SAneesh Kumar K.V /* 591f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 592f5ab0d1fSMingming Cao * 593f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 594df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 595f5ab0d1fSMingming Cao * with buffer head unmapped. 596f5ab0d1fSMingming Cao */ 597e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 598b8a86845SLukas Czerner /* 599b8a86845SLukas Czerner * If we need to convert extent to unwritten 600b8a86845SLukas Czerner * we continue and do the actual work in 601b8a86845SLukas Czerner * ext4_ext_map_blocks() 602b8a86845SLukas Czerner */ 603b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 604f5ab0d1fSMingming Cao return retval; 605f5ab0d1fSMingming Cao 606f5ab0d1fSMingming Cao /* 607a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 608a25a4e1aSZheng Liu * it will be set again. 6092a8964d6SAneesh Kumar K.V */ 610a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6112a8964d6SAneesh Kumar K.V 6122a8964d6SAneesh Kumar K.V /* 613556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 614f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 615d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 616f5ab0d1fSMingming Cao * with create == 1 flag. 6174df3d265SAneesh Kumar K.V */ 618c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 619d2a17637SMingming Cao 620d2a17637SMingming Cao /* 6214df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6224df3d265SAneesh Kumar K.V * could have changed the inode type in between 6234df3d265SAneesh Kumar K.V */ 62412e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 625e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6260e855ac8SAneesh Kumar K.V } else { 627e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 628267e4db9SAneesh Kumar K.V 629e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 630267e4db9SAneesh Kumar K.V /* 631267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 632267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 633267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 634267e4db9SAneesh Kumar K.V */ 63519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 636267e4db9SAneesh Kumar K.V } 6372ac3b6e0STheodore Ts'o 638d2a17637SMingming Cao /* 6392ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6405f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6415f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6425f634d06SAneesh Kumar K.V * reserve space here. 643d2a17637SMingming Cao */ 6445f634d06SAneesh Kumar K.V if ((retval > 0) && 6451296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6465f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6475f634d06SAneesh Kumar K.V } 648d2a17637SMingming Cao 649f7fec032SZheng Liu if (retval > 0) { 6503be78c73STheodore Ts'o unsigned int status; 651f7fec032SZheng Liu 65244fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 65344fb851dSZheng Liu ext4_warning(inode->i_sb, 65444fb851dSZheng Liu "ES len assertion failed for inode " 65544fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 65644fb851dSZheng Liu inode->i_ino, retval, map->m_len); 65744fb851dSZheng Liu WARN_ON(1); 658921f266bSDmitry Monakhov } 659921f266bSDmitry Monakhov 660adb23551SZheng Liu /* 661c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 662c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6639b623df6SJan Kara * use them before they are really zeroed. We also have to 6649b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6659b623df6SJan Kara * overwrite zeros with stale data from block device. 666c86d8db3SJan Kara */ 667c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 668c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 669c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 670c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 671c86d8db3SJan Kara map->m_pblk, map->m_len); 672c86d8db3SJan Kara if (ret) { 673c86d8db3SJan Kara retval = ret; 674c86d8db3SJan Kara goto out_sem; 675c86d8db3SJan Kara } 676c86d8db3SJan Kara } 677c86d8db3SJan Kara 678c86d8db3SJan Kara /* 679adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 680adb23551SZheng Liu * extent status tree. 681adb23551SZheng Liu */ 682adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 683bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 684adb23551SZheng Liu if (ext4_es_is_written(&es)) 685c86d8db3SJan Kara goto out_sem; 686adb23551SZheng Liu } 687f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 688f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 689f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 690d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 691ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 692f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 693f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 694f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 695f7fec032SZheng Liu map->m_pblk, status); 696c86d8db3SJan Kara if (ret < 0) { 69751865fdaSZheng Liu retval = ret; 698c86d8db3SJan Kara goto out_sem; 699c86d8db3SJan Kara } 70051865fdaSZheng Liu } 7015356f261SAditya Kali 702c86d8db3SJan Kara out_sem: 7030e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 704e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 705b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7066fd058f7STheodore Ts'o if (ret != 0) 7076fd058f7STheodore Ts'o return ret; 70806bd3c36SJan Kara 70906bd3c36SJan Kara /* 71006bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 71106bd3c36SJan Kara * visible after transaction commit must be on transaction's 71206bd3c36SJan Kara * ordered data list. 71306bd3c36SJan Kara */ 71406bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 71506bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 71606bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 71702749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 71806bd3c36SJan Kara ext4_should_order_data(inode)) { 71973131fbbSRoss Zwisler loff_t start_byte = 72073131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 72173131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 72273131fbbSRoss Zwisler 723ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 72473131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 72573131fbbSRoss Zwisler start_byte, length); 726ee0876bcSJan Kara else 72773131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 72873131fbbSRoss Zwisler start_byte, length); 72906bd3c36SJan Kara if (ret) 73006bd3c36SJan Kara return ret; 73106bd3c36SJan Kara } 7326fd058f7STheodore Ts'o } 733ec8c60beSRitesh Harjani 734ec8c60beSRitesh Harjani if (retval < 0) 73570aa1554SRitesh Harjani ext_debug(inode, "failed with err %d\n", retval); 7360e855ac8SAneesh Kumar K.V return retval; 7370e855ac8SAneesh Kumar K.V } 7380e855ac8SAneesh Kumar K.V 739ed8ad838SJan Kara /* 740ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 741ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 742ed8ad838SJan Kara */ 743ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 744ed8ad838SJan Kara { 745ed8ad838SJan Kara unsigned long old_state; 746ed8ad838SJan Kara unsigned long new_state; 747ed8ad838SJan Kara 748ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 749ed8ad838SJan Kara 750ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 751ed8ad838SJan Kara if (!bh->b_page) { 752ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 753ed8ad838SJan Kara return; 754ed8ad838SJan Kara } 755ed8ad838SJan Kara /* 756ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 757ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 758ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 759ed8ad838SJan Kara */ 760ed8ad838SJan Kara do { 761ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 762ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 763ed8ad838SJan Kara } while (unlikely( 764ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 765ed8ad838SJan Kara } 766ed8ad838SJan Kara 7672ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7682ed88685STheodore Ts'o struct buffer_head *bh, int flags) 769ac27a0ecSDave Kleikamp { 7702ed88685STheodore Ts'o struct ext4_map_blocks map; 771efe70c29SJan Kara int ret = 0; 772ac27a0ecSDave Kleikamp 77346c7f254STao Ma if (ext4_has_inline_data(inode)) 77446c7f254STao Ma return -ERANGE; 77546c7f254STao Ma 7762ed88685STheodore Ts'o map.m_lblk = iblock; 7772ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7782ed88685STheodore Ts'o 779efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 780efe70c29SJan Kara flags); 781ac27a0ecSDave Kleikamp if (ret > 0) { 7822ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 783ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7842ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 785ac27a0ecSDave Kleikamp ret = 0; 786547edce3SRoss Zwisler } else if (ret == 0) { 787547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 788547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 789ac27a0ecSDave Kleikamp } 790ac27a0ecSDave Kleikamp return ret; 791ac27a0ecSDave Kleikamp } 792ac27a0ecSDave Kleikamp 7932ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7942ed88685STheodore Ts'o struct buffer_head *bh, int create) 7952ed88685STheodore Ts'o { 7962ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7972ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7982ed88685STheodore Ts'o } 7992ed88685STheodore Ts'o 800ac27a0ecSDave Kleikamp /* 801705965bdSJan Kara * Get block function used when preparing for buffered write if we require 802705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 803705965bdSJan Kara * will be converted to written after the IO is complete. 804705965bdSJan Kara */ 805705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 806705965bdSJan Kara struct buffer_head *bh_result, int create) 807705965bdSJan Kara { 808705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 809705965bdSJan Kara inode->i_ino, create); 810705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 811705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 812705965bdSJan Kara } 813705965bdSJan Kara 814efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 815efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 816efe70c29SJan Kara 817e84dfbe2SJan Kara /* 818ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 819ac27a0ecSDave Kleikamp */ 820617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 821c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 822ac27a0ecSDave Kleikamp { 8232ed88685STheodore Ts'o struct ext4_map_blocks map; 8242ed88685STheodore Ts'o struct buffer_head *bh; 825c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 82610560082STheodore Ts'o int err; 827ac27a0ecSDave Kleikamp 828ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 829ac27a0ecSDave Kleikamp 8302ed88685STheodore Ts'o map.m_lblk = block; 8312ed88685STheodore Ts'o map.m_len = 1; 832c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8332ed88685STheodore Ts'o 83410560082STheodore Ts'o if (err == 0) 83510560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8362ed88685STheodore Ts'o if (err < 0) 83710560082STheodore Ts'o return ERR_PTR(err); 8382ed88685STheodore Ts'o 8392ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 84010560082STheodore Ts'o if (unlikely(!bh)) 84110560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8422ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 843ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 844ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 845ac27a0ecSDave Kleikamp 846ac27a0ecSDave Kleikamp /* 847ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 848ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 849ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 850617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 851ac27a0ecSDave Kleikamp * problem. 852ac27a0ecSDave Kleikamp */ 853ac27a0ecSDave Kleikamp lock_buffer(bh); 854ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 85510560082STheodore Ts'o err = ext4_journal_get_create_access(handle, bh); 85610560082STheodore Ts'o if (unlikely(err)) { 85710560082STheodore Ts'o unlock_buffer(bh); 85810560082STheodore Ts'o goto errout; 85910560082STheodore Ts'o } 86010560082STheodore Ts'o if (!buffer_uptodate(bh)) { 861ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 862ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 863ac27a0ecSDave Kleikamp } 864ac27a0ecSDave Kleikamp unlock_buffer(bh); 8650390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8660390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 86710560082STheodore Ts'o if (unlikely(err)) 86810560082STheodore Ts'o goto errout; 86910560082STheodore Ts'o } else 870ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 871ac27a0ecSDave Kleikamp return bh; 87210560082STheodore Ts'o errout: 87310560082STheodore Ts'o brelse(bh); 87410560082STheodore Ts'o return ERR_PTR(err); 875ac27a0ecSDave Kleikamp } 876ac27a0ecSDave Kleikamp 877617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 878c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 879ac27a0ecSDave Kleikamp { 880ac27a0ecSDave Kleikamp struct buffer_head *bh; 881*2d069c08Szhangyi (F) int ret; 882ac27a0ecSDave Kleikamp 883c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 8841c215028STheodore Ts'o if (IS_ERR(bh)) 885ac27a0ecSDave Kleikamp return bh; 8867963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 887ac27a0ecSDave Kleikamp return bh; 888*2d069c08Szhangyi (F) 889*2d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true); 890*2d069c08Szhangyi (F) if (ret) { 891ac27a0ecSDave Kleikamp put_bh(bh); 892*2d069c08Szhangyi (F) return ERR_PTR(ret); 893*2d069c08Szhangyi (F) } 894*2d069c08Szhangyi (F) return bh; 895ac27a0ecSDave Kleikamp } 896ac27a0ecSDave Kleikamp 8979699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 8989699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 8999699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 9009699d4f9STahsin Erdogan { 9019699d4f9STahsin Erdogan int i, err; 9029699d4f9STahsin Erdogan 9039699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9049699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9059699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9069699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9079699d4f9STahsin Erdogan bh_count = i; 9089699d4f9STahsin Erdogan goto out_brelse; 9099699d4f9STahsin Erdogan } 9109699d4f9STahsin Erdogan } 9119699d4f9STahsin Erdogan 9129699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9139699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 914*2d069c08Szhangyi (F) if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 915*2d069c08Szhangyi (F) ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false); 9169699d4f9STahsin Erdogan 9179699d4f9STahsin Erdogan if (!wait) 9189699d4f9STahsin Erdogan return 0; 9199699d4f9STahsin Erdogan 9209699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9219699d4f9STahsin Erdogan if (bhs[i]) 9229699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9239699d4f9STahsin Erdogan 9249699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9259699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9269699d4f9STahsin Erdogan err = -EIO; 9279699d4f9STahsin Erdogan goto out_brelse; 9289699d4f9STahsin Erdogan } 9299699d4f9STahsin Erdogan } 9309699d4f9STahsin Erdogan return 0; 9319699d4f9STahsin Erdogan 9329699d4f9STahsin Erdogan out_brelse: 9339699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9349699d4f9STahsin Erdogan brelse(bhs[i]); 9359699d4f9STahsin Erdogan bhs[i] = NULL; 9369699d4f9STahsin Erdogan } 9379699d4f9STahsin Erdogan return err; 9389699d4f9STahsin Erdogan } 9399699d4f9STahsin Erdogan 940f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 941ac27a0ecSDave Kleikamp struct buffer_head *head, 942ac27a0ecSDave Kleikamp unsigned from, 943ac27a0ecSDave Kleikamp unsigned to, 944ac27a0ecSDave Kleikamp int *partial, 945ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 946ac27a0ecSDave Kleikamp struct buffer_head *bh)) 947ac27a0ecSDave Kleikamp { 948ac27a0ecSDave Kleikamp struct buffer_head *bh; 949ac27a0ecSDave Kleikamp unsigned block_start, block_end; 950ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 951ac27a0ecSDave Kleikamp int err, ret = 0; 952ac27a0ecSDave Kleikamp struct buffer_head *next; 953ac27a0ecSDave Kleikamp 954ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 955ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 956de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 957ac27a0ecSDave Kleikamp next = bh->b_this_page; 958ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 959ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 960ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 961ac27a0ecSDave Kleikamp *partial = 1; 962ac27a0ecSDave Kleikamp continue; 963ac27a0ecSDave Kleikamp } 964ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 965ac27a0ecSDave Kleikamp if (!ret) 966ac27a0ecSDave Kleikamp ret = err; 967ac27a0ecSDave Kleikamp } 968ac27a0ecSDave Kleikamp return ret; 969ac27a0ecSDave Kleikamp } 970ac27a0ecSDave Kleikamp 971ac27a0ecSDave Kleikamp /* 972ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 973ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 974617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 975dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 976ac27a0ecSDave Kleikamp * prepare_write() is the right place. 977ac27a0ecSDave Kleikamp * 97836ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 97936ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 98036ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 98136ade451SJan Kara * because the caller may be PF_MEMALLOC. 982ac27a0ecSDave Kleikamp * 983617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 984ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 985ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 986ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 987ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 988ac27a0ecSDave Kleikamp * violation. 989ac27a0ecSDave Kleikamp * 990dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 991ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 992ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 993ac27a0ecSDave Kleikamp * write. 994ac27a0ecSDave Kleikamp */ 995f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 996ac27a0ecSDave Kleikamp struct buffer_head *bh) 997ac27a0ecSDave Kleikamp { 99856d35a4cSJan Kara int dirty = buffer_dirty(bh); 99956d35a4cSJan Kara int ret; 100056d35a4cSJan Kara 1001ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1002ac27a0ecSDave Kleikamp return 0; 100356d35a4cSJan Kara /* 1004ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 100556d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 100656d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1007ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 100856d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 100956d35a4cSJan Kara * ever write the buffer. 101056d35a4cSJan Kara */ 101156d35a4cSJan Kara if (dirty) 101256d35a4cSJan Kara clear_buffer_dirty(bh); 10135d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 101456d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 101556d35a4cSJan Kara if (!ret && dirty) 101656d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 101756d35a4cSJan Kara return ret; 1018ac27a0ecSDave Kleikamp } 1019ac27a0ecSDave Kleikamp 1020643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10212058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10222058f83aSMichael Halcrow get_block_t *get_block) 10232058f83aSMichael Halcrow { 102409cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10252058f83aSMichael Halcrow unsigned to = from + len; 10262058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10272058f83aSMichael Halcrow unsigned block_start, block_end; 10282058f83aSMichael Halcrow sector_t block; 10292058f83aSMichael Halcrow int err = 0; 10302058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10312058f83aSMichael Halcrow unsigned bbits; 10320b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10330b578f35SChandan Rajendra int nr_wait = 0; 10340b578f35SChandan Rajendra int i; 10352058f83aSMichael Halcrow 10362058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 103709cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 103809cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10392058f83aSMichael Halcrow BUG_ON(from > to); 10402058f83aSMichael Halcrow 10412058f83aSMichael Halcrow if (!page_has_buffers(page)) 10422058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10432058f83aSMichael Halcrow head = page_buffers(page); 10442058f83aSMichael Halcrow bbits = ilog2(blocksize); 104509cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10462058f83aSMichael Halcrow 10472058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10482058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10492058f83aSMichael Halcrow block_end = block_start + blocksize; 10502058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10512058f83aSMichael Halcrow if (PageUptodate(page)) { 10522058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10532058f83aSMichael Halcrow set_buffer_uptodate(bh); 10542058f83aSMichael Halcrow } 10552058f83aSMichael Halcrow continue; 10562058f83aSMichael Halcrow } 10572058f83aSMichael Halcrow if (buffer_new(bh)) 10582058f83aSMichael Halcrow clear_buffer_new(bh); 10592058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10602058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10612058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10622058f83aSMichael Halcrow if (err) 10632058f83aSMichael Halcrow break; 10642058f83aSMichael Halcrow if (buffer_new(bh)) { 10652058f83aSMichael Halcrow if (PageUptodate(page)) { 10662058f83aSMichael Halcrow clear_buffer_new(bh); 10672058f83aSMichael Halcrow set_buffer_uptodate(bh); 10682058f83aSMichael Halcrow mark_buffer_dirty(bh); 10692058f83aSMichael Halcrow continue; 10702058f83aSMichael Halcrow } 10712058f83aSMichael Halcrow if (block_end > to || block_start < from) 10722058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10732058f83aSMichael Halcrow block_start, from); 10742058f83aSMichael Halcrow continue; 10752058f83aSMichael Halcrow } 10762058f83aSMichael Halcrow } 10772058f83aSMichael Halcrow if (PageUptodate(page)) { 10782058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10792058f83aSMichael Halcrow set_buffer_uptodate(bh); 10802058f83aSMichael Halcrow continue; 10812058f83aSMichael Halcrow } 10822058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10832058f83aSMichael Halcrow !buffer_unwritten(bh) && 10842058f83aSMichael Halcrow (block_start < from || block_end > to)) { 1085*2d069c08Szhangyi (F) ext4_read_bh_lock(bh, 0, false); 10860b578f35SChandan Rajendra wait[nr_wait++] = bh; 10872058f83aSMichael Halcrow } 10882058f83aSMichael Halcrow } 10892058f83aSMichael Halcrow /* 10902058f83aSMichael Halcrow * If we issued read requests, let them complete. 10912058f83aSMichael Halcrow */ 10920b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10930b578f35SChandan Rajendra wait_on_buffer(wait[i]); 10940b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 10952058f83aSMichael Halcrow err = -EIO; 10962058f83aSMichael Halcrow } 10977e0785fcSChandan Rajendra if (unlikely(err)) { 10982058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 10994f74d15fSEric Biggers } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 11000b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 11010b578f35SChandan Rajendra int err2; 11020b578f35SChandan Rajendra 11030b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 11040b578f35SChandan Rajendra bh_offset(wait[i])); 11050b578f35SChandan Rajendra if (err2) { 11060b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11070b578f35SChandan Rajendra err = err2; 11080b578f35SChandan Rajendra } 11090b578f35SChandan Rajendra } 11107e0785fcSChandan Rajendra } 11117e0785fcSChandan Rajendra 11122058f83aSMichael Halcrow return err; 11132058f83aSMichael Halcrow } 11142058f83aSMichael Halcrow #endif 11152058f83aSMichael Halcrow 1116bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1117bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1118bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1119ac27a0ecSDave Kleikamp { 1120bfc1af65SNick Piggin struct inode *inode = mapping->host; 11211938a150SAneesh Kumar K.V int ret, needed_blocks; 1122ac27a0ecSDave Kleikamp handle_t *handle; 1123ac27a0ecSDave Kleikamp int retries = 0; 1124bfc1af65SNick Piggin struct page *page; 1125bfc1af65SNick Piggin pgoff_t index; 1126bfc1af65SNick Piggin unsigned from, to; 1127bfc1af65SNick Piggin 11280db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11290db1ff22STheodore Ts'o return -EIO; 11300db1ff22STheodore Ts'o 11319bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11321938a150SAneesh Kumar K.V /* 11331938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11341938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11351938a150SAneesh Kumar K.V */ 11361938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 113709cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 113809cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1139bfc1af65SNick Piggin to = from + len; 1140ac27a0ecSDave Kleikamp 1141f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1142f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1143f19d5870STao Ma flags, pagep); 1144f19d5870STao Ma if (ret < 0) 114547564bfbSTheodore Ts'o return ret; 114647564bfbSTheodore Ts'o if (ret == 1) 114747564bfbSTheodore Ts'o return 0; 1148f19d5870STao Ma } 1149f19d5870STao Ma 115047564bfbSTheodore Ts'o /* 115147564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 115247564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 115347564bfbSTheodore Ts'o * is being written back. So grab it first before we start 115447564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 115547564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 115647564bfbSTheodore Ts'o */ 115747564bfbSTheodore Ts'o retry_grab: 115854566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 115947564bfbSTheodore Ts'o if (!page) 116047564bfbSTheodore Ts'o return -ENOMEM; 116147564bfbSTheodore Ts'o unlock_page(page); 116247564bfbSTheodore Ts'o 116347564bfbSTheodore Ts'o retry_journal: 11649924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1165ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 116609cbfeafSKirill A. Shutemov put_page(page); 116747564bfbSTheodore Ts'o return PTR_ERR(handle); 1168cf108bcaSJan Kara } 1169f19d5870STao Ma 117047564bfbSTheodore Ts'o lock_page(page); 117147564bfbSTheodore Ts'o if (page->mapping != mapping) { 117247564bfbSTheodore Ts'o /* The page got truncated from under us */ 117347564bfbSTheodore Ts'o unlock_page(page); 117409cbfeafSKirill A. Shutemov put_page(page); 1175cf108bcaSJan Kara ext4_journal_stop(handle); 117647564bfbSTheodore Ts'o goto retry_grab; 1177cf108bcaSJan Kara } 11787afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11797afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1180cf108bcaSJan Kara 1181643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11822058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11832058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1184705965bdSJan Kara ext4_get_block_unwritten); 11852058f83aSMichael Halcrow else 11862058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 11872058f83aSMichael Halcrow ext4_get_block); 11882058f83aSMichael Halcrow #else 1189744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1190705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1191705965bdSJan Kara ext4_get_block_unwritten); 1192744692dcSJiaying Zhang else 11936e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 11942058f83aSMichael Halcrow #endif 1195bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1196f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1197f19d5870STao Ma from, to, NULL, 1198f19d5870STao Ma do_journal_get_write_access); 1199b46be050SAndrey Savochkin } 1200bfc1af65SNick Piggin 1201bfc1af65SNick Piggin if (ret) { 1202c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1203c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1204c93d8f88SEric Biggers 1205bfc1af65SNick Piggin unlock_page(page); 1206ae4d5372SAneesh Kumar K.V /* 12076e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1208ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1209ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12101938a150SAneesh Kumar K.V * 12111938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12121938a150SAneesh Kumar K.V * truncate finishes 1213ae4d5372SAneesh Kumar K.V */ 1214c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12151938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12161938a150SAneesh Kumar K.V 12171938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1218c93d8f88SEric Biggers if (extended) { 1219b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12201938a150SAneesh Kumar K.V /* 1221ffacfa7aSJan Kara * If truncate failed early the inode might 12221938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12231938a150SAneesh Kumar K.V * make sure the inode is removed from the 12241938a150SAneesh Kumar K.V * orphan list in that case. 12251938a150SAneesh Kumar K.V */ 12261938a150SAneesh Kumar K.V if (inode->i_nlink) 12271938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12281938a150SAneesh Kumar K.V } 1229bfc1af65SNick Piggin 123047564bfbSTheodore Ts'o if (ret == -ENOSPC && 123147564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 123247564bfbSTheodore Ts'o goto retry_journal; 123309cbfeafSKirill A. Shutemov put_page(page); 123447564bfbSTheodore Ts'o return ret; 123547564bfbSTheodore Ts'o } 123647564bfbSTheodore Ts'o *pagep = page; 1237ac27a0ecSDave Kleikamp return ret; 1238ac27a0ecSDave Kleikamp } 1239ac27a0ecSDave Kleikamp 1240bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1241bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1242ac27a0ecSDave Kleikamp { 124313fca323STheodore Ts'o int ret; 1244ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1245ac27a0ecSDave Kleikamp return 0; 1246ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 124713fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 124813fca323STheodore Ts'o clear_buffer_meta(bh); 124913fca323STheodore Ts'o clear_buffer_prio(bh); 125013fca323STheodore Ts'o return ret; 1251ac27a0ecSDave Kleikamp } 1252ac27a0ecSDave Kleikamp 1253eed4333fSZheng Liu /* 1254eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1255eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1256eed4333fSZheng Liu * 1257eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1258eed4333fSZheng Liu * buffers are managed internally. 1259eed4333fSZheng Liu */ 1260eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1261f8514083SAneesh Kumar K.V struct address_space *mapping, 1262f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1263f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1264f8514083SAneesh Kumar K.V { 1265f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1266eed4333fSZheng Liu struct inode *inode = mapping->host; 12670572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1268eed4333fSZheng Liu int ret = 0, ret2; 1269eed4333fSZheng Liu int i_size_changed = 0; 1270362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1271c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1272eed4333fSZheng Liu 1273eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1274362eca70STheodore Ts'o if (inline_data) { 127542c832deSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 1276f19d5870STao Ma copied, page); 1277eb5efbcbSTheodore Ts'o if (ret < 0) { 1278eb5efbcbSTheodore Ts'o unlock_page(page); 1279eb5efbcbSTheodore Ts'o put_page(page); 128042c832deSTheodore Ts'o goto errout; 1281eb5efbcbSTheodore Ts'o } 128242c832deSTheodore Ts'o copied = ret; 128342c832deSTheodore Ts'o } else 1284f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1285f19d5870STao Ma len, copied, page, fsdata); 1286f8514083SAneesh Kumar K.V /* 12874631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1288f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1289c93d8f88SEric Biggers * 1290c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1291c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1292f8514083SAneesh Kumar K.V */ 1293c93d8f88SEric Biggers if (!verity) 12944631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1295f8514083SAneesh Kumar K.V unlock_page(page); 129609cbfeafSKirill A. Shutemov put_page(page); 1297f8514083SAneesh Kumar K.V 1298c93d8f88SEric Biggers if (old_size < pos && !verity) 12990572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1300f8514083SAneesh Kumar K.V /* 1301f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1302f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1303f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1304f8514083SAneesh Kumar K.V * filesystems. 1305f8514083SAneesh Kumar K.V */ 1306362eca70STheodore Ts'o if (i_size_changed || inline_data) 13074209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1308f8514083SAneesh Kumar K.V 1309c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1310f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1311f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1312f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1313f8514083SAneesh Kumar K.V */ 1314f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 131574d553aaSTheodore Ts'o errout: 1316617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1317ac27a0ecSDave Kleikamp if (!ret) 1318ac27a0ecSDave Kleikamp ret = ret2; 1319bfc1af65SNick Piggin 1320c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1321b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1322f8514083SAneesh Kumar K.V /* 1323ffacfa7aSJan Kara * If truncate failed early the inode might still be 1324f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1325f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1326f8514083SAneesh Kumar K.V */ 1327f8514083SAneesh Kumar K.V if (inode->i_nlink) 1328f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1329f8514083SAneesh Kumar K.V } 1330f8514083SAneesh Kumar K.V 1331bfc1af65SNick Piggin return ret ? ret : copied; 1332ac27a0ecSDave Kleikamp } 1333ac27a0ecSDave Kleikamp 1334b90197b6STheodore Ts'o /* 1335b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1336b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1337b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1338b90197b6STheodore Ts'o */ 13393b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 13403b136499SJan Kara struct page *page, 13413b136499SJan Kara unsigned from, unsigned to) 1342b90197b6STheodore Ts'o { 1343b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1344b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1345b90197b6STheodore Ts'o 1346b90197b6STheodore Ts'o bh = head = page_buffers(page); 1347b90197b6STheodore Ts'o do { 1348b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1349b90197b6STheodore Ts'o if (buffer_new(bh)) { 1350b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1351b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1352b90197b6STheodore Ts'o unsigned start, size; 1353b90197b6STheodore Ts'o 1354b90197b6STheodore Ts'o start = max(from, block_start); 1355b90197b6STheodore Ts'o size = min(to, block_end) - start; 1356b90197b6STheodore Ts'o 1357b90197b6STheodore Ts'o zero_user(page, start, size); 13583b136499SJan Kara write_end_fn(handle, bh); 1359b90197b6STheodore Ts'o } 1360b90197b6STheodore Ts'o clear_buffer_new(bh); 1361b90197b6STheodore Ts'o } 1362b90197b6STheodore Ts'o } 1363b90197b6STheodore Ts'o block_start = block_end; 1364b90197b6STheodore Ts'o bh = bh->b_this_page; 1365b90197b6STheodore Ts'o } while (bh != head); 1366b90197b6STheodore Ts'o } 1367b90197b6STheodore Ts'o 1368bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1369bfc1af65SNick Piggin struct address_space *mapping, 1370bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1371bfc1af65SNick Piggin struct page *page, void *fsdata) 1372ac27a0ecSDave Kleikamp { 1373617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1374bfc1af65SNick Piggin struct inode *inode = mapping->host; 13750572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1376ac27a0ecSDave Kleikamp int ret = 0, ret2; 1377ac27a0ecSDave Kleikamp int partial = 0; 1378bfc1af65SNick Piggin unsigned from, to; 13794631dbf6SDmitry Monakhov int size_changed = 0; 1380362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1381c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1382ac27a0ecSDave Kleikamp 13839bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 138409cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1385bfc1af65SNick Piggin to = from + len; 1386bfc1af65SNick Piggin 1387441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1388441c8508SCurt Wohlgemuth 1389362eca70STheodore Ts'o if (inline_data) { 1390eb5efbcbSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 13913fdcfb66STao Ma copied, page); 1392eb5efbcbSTheodore Ts'o if (ret < 0) { 1393eb5efbcbSTheodore Ts'o unlock_page(page); 1394eb5efbcbSTheodore Ts'o put_page(page); 1395eb5efbcbSTheodore Ts'o goto errout; 1396eb5efbcbSTheodore Ts'o } 1397eb5efbcbSTheodore Ts'o copied = ret; 1398eb5efbcbSTheodore Ts'o } else if (unlikely(copied < len) && !PageUptodate(page)) { 1399bfc1af65SNick Piggin copied = 0; 14003b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, from, to); 14013b136499SJan Kara } else { 14023b136499SJan Kara if (unlikely(copied < len)) 14033b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, 14043b136499SJan Kara from + copied, to); 1405f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 14063b136499SJan Kara from + copied, &partial, 14073b136499SJan Kara write_end_fn); 1408ac27a0ecSDave Kleikamp if (!partial) 1409ac27a0ecSDave Kleikamp SetPageUptodate(page); 14103fdcfb66STao Ma } 1411c93d8f88SEric Biggers if (!verity) 14124631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 141319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14142d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14154631dbf6SDmitry Monakhov unlock_page(page); 141609cbfeafSKirill A. Shutemov put_page(page); 14174631dbf6SDmitry Monakhov 1418c93d8f88SEric Biggers if (old_size < pos && !verity) 14190572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14200572639fSXiaoguang Wang 1421362eca70STheodore Ts'o if (size_changed || inline_data) { 1422617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1423ac27a0ecSDave Kleikamp if (!ret) 1424ac27a0ecSDave Kleikamp ret = ret2; 1425ac27a0ecSDave Kleikamp } 1426bfc1af65SNick Piggin 1427c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1428f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1429f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1430f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1431f8514083SAneesh Kumar K.V */ 1432f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1433f8514083SAneesh Kumar K.V 1434eb5efbcbSTheodore Ts'o errout: 1435617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1436ac27a0ecSDave Kleikamp if (!ret) 1437ac27a0ecSDave Kleikamp ret = ret2; 1438c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1439b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1440f8514083SAneesh Kumar K.V /* 1441ffacfa7aSJan Kara * If truncate failed early the inode might still be 1442f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1443f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1444f8514083SAneesh Kumar K.V */ 1445f8514083SAneesh Kumar K.V if (inode->i_nlink) 1446f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1447f8514083SAneesh Kumar K.V } 1448bfc1af65SNick Piggin 1449bfc1af65SNick Piggin return ret ? ret : copied; 1450ac27a0ecSDave Kleikamp } 1451d2a17637SMingming Cao 14529d0be502STheodore Ts'o /* 1453c27e43a1SEric Whitney * Reserve space for a single cluster 14549d0be502STheodore Ts'o */ 1455c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1456d2a17637SMingming Cao { 1457d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14580637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14595dd4056dSChristoph Hellwig int ret; 1460d2a17637SMingming Cao 146160e58e0fSMingming Cao /* 146272b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 146372b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 146472b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 146560e58e0fSMingming Cao */ 14667b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14675dd4056dSChristoph Hellwig if (ret) 14685dd4056dSChristoph Hellwig return ret; 146903179fe9STheodore Ts'o 147003179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 147171d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 147203179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147303179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1474d2a17637SMingming Cao return -ENOSPC; 1475d2a17637SMingming Cao } 14769d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1477c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14780637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147939bc680aSDmitry Monakhov 1480d2a17637SMingming Cao return 0; /* success */ 1481d2a17637SMingming Cao } 1482d2a17637SMingming Cao 1483f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1484d2a17637SMingming Cao { 1485d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14860637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1487d2a17637SMingming Cao 1488cd213226SMingming Cao if (!to_free) 1489cd213226SMingming Cao return; /* Nothing to release, exit */ 1490cd213226SMingming Cao 1491d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1492cd213226SMingming Cao 14935a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14940637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1495cd213226SMingming Cao /* 14960637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14970637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14980637c6f4STheodore Ts'o * function is called from invalidate page, it's 14990637c6f4STheodore Ts'o * harmless to return without any action. 1500cd213226SMingming Cao */ 15018de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15020637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15031084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15040637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15050637c6f4STheodore Ts'o WARN_ON(1); 15060637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15070637c6f4STheodore Ts'o } 15080637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15090637c6f4STheodore Ts'o 151072b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 151157042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1512d2a17637SMingming Cao 1513d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 151460e58e0fSMingming Cao 15157b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1516d2a17637SMingming Cao } 1517d2a17637SMingming Cao 1518ac27a0ecSDave Kleikamp /* 151964769240SAlex Tomas * Delayed allocation stuff 152064769240SAlex Tomas */ 152164769240SAlex Tomas 15224e7ea81dSJan Kara struct mpage_da_data { 15234e7ea81dSJan Kara struct inode *inode; 15244e7ea81dSJan Kara struct writeback_control *wbc; 15256b523df4SJan Kara 15264e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15274e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15284e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 152964769240SAlex Tomas /* 15304e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15314e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15324e7ea81dSJan Kara * is delalloc or unwritten. 153364769240SAlex Tomas */ 15344e7ea81dSJan Kara struct ext4_map_blocks map; 15354e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1536dddbd6acSJan Kara unsigned int do_map:1; 15376b8ed620SJan Kara unsigned int scanned_until_end:1; 15384e7ea81dSJan Kara }; 153964769240SAlex Tomas 15404e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15414e7ea81dSJan Kara bool invalidate) 1542c4a0c46eSAneesh Kumar K.V { 1543c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1544c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1545c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1546c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1547c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15484e7ea81dSJan Kara 15494e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15504e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15514e7ea81dSJan Kara return; 1552c4a0c46eSAneesh Kumar K.V 15536b8ed620SJan Kara mpd->scanned_until_end = 0; 1554c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1555c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15564e7ea81dSJan Kara if (invalidate) { 15574e7ea81dSJan Kara ext4_lblk_t start, last; 155809cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 155909cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 156051865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15614e7ea81dSJan Kara } 156251865fdaSZheng Liu 156386679820SMel Gorman pagevec_init(&pvec); 1564c4a0c46eSAneesh Kumar K.V while (index <= end) { 1565397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1566c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1567c4a0c46eSAneesh Kumar K.V break; 1568c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1569c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15702b85a617SJan Kara 1571c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1572c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15734e7ea81dSJan Kara if (invalidate) { 15744e800c03Swangguang if (page_mapped(page)) 15754e800c03Swangguang clear_page_dirty_for_io(page); 157609cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1577c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15784e7ea81dSJan Kara } 1579c4a0c46eSAneesh Kumar K.V unlock_page(page); 1580c4a0c46eSAneesh Kumar K.V } 15819b1d0998SJan Kara pagevec_release(&pvec); 1582c4a0c46eSAneesh Kumar K.V } 1583c4a0c46eSAneesh Kumar K.V } 1584c4a0c46eSAneesh Kumar K.V 1585df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1586df22291fSAneesh Kumar K.V { 1587df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 158892b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1589f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 159092b97816STheodore Ts'o 159192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15925dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1593f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 159492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 159592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1596f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 159757042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 159892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1599f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16007b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 160192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 160292b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1603f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1604df22291fSAneesh Kumar K.V return; 1605df22291fSAneesh Kumar K.V } 1606df22291fSAneesh Kumar K.V 1607c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 160829fa89d0SAneesh Kumar K.V { 1609c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 161029fa89d0SAneesh Kumar K.V } 161129fa89d0SAneesh Kumar K.V 161264769240SAlex Tomas /* 16130b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16140b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16150b02f4c0SEric Whitney * count or making a pending reservation 16160b02f4c0SEric Whitney * where needed 16170b02f4c0SEric Whitney * 16180b02f4c0SEric Whitney * @inode - file containing the newly added block 16190b02f4c0SEric Whitney * @lblk - logical block to be added 16200b02f4c0SEric Whitney * 16210b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16220b02f4c0SEric Whitney */ 16230b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16240b02f4c0SEric Whitney { 16250b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16260b02f4c0SEric Whitney int ret; 16270b02f4c0SEric Whitney bool allocated = false; 16280b02f4c0SEric Whitney 16290b02f4c0SEric Whitney /* 16300b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16310b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16320b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16330b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16340b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16350b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16360b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16370b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16380b02f4c0SEric Whitney * extents status tree doesn't get a match. 16390b02f4c0SEric Whitney */ 16400b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16410b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16420b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16430b02f4c0SEric Whitney goto errout; 16440b02f4c0SEric Whitney } else { /* bigalloc */ 16450b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16460b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16470b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16480b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16490b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16500b02f4c0SEric Whitney if (ret < 0) 16510b02f4c0SEric Whitney goto errout; 16520b02f4c0SEric Whitney if (ret == 0) { 16530b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16540b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16550b02f4c0SEric Whitney goto errout; 16560b02f4c0SEric Whitney } else { 16570b02f4c0SEric Whitney allocated = true; 16580b02f4c0SEric Whitney } 16590b02f4c0SEric Whitney } else { 16600b02f4c0SEric Whitney allocated = true; 16610b02f4c0SEric Whitney } 16620b02f4c0SEric Whitney } 16630b02f4c0SEric Whitney } 16640b02f4c0SEric Whitney 16650b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16660b02f4c0SEric Whitney 16670b02f4c0SEric Whitney errout: 16680b02f4c0SEric Whitney return ret; 16690b02f4c0SEric Whitney } 16700b02f4c0SEric Whitney 16710b02f4c0SEric Whitney /* 16725356f261SAditya Kali * This function is grabs code from the very beginning of 16735356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16745356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16755356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16765356f261SAditya Kali */ 16775356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16785356f261SAditya Kali struct ext4_map_blocks *map, 16795356f261SAditya Kali struct buffer_head *bh) 16805356f261SAditya Kali { 1681d100eef2SZheng Liu struct extent_status es; 16825356f261SAditya Kali int retval; 16835356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1684921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1685921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1686921f266bSDmitry Monakhov 1687921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1688921f266bSDmitry Monakhov #endif 16895356f261SAditya Kali 16905356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16915356f261SAditya Kali invalid_block = ~0; 16925356f261SAditya Kali 16935356f261SAditya Kali map->m_flags = 0; 169470aa1554SRitesh Harjani ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, 16955356f261SAditya Kali (unsigned long) map->m_lblk); 1696d100eef2SZheng Liu 1697d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1698bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1699d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1700d100eef2SZheng Liu retval = 0; 1701c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1702d100eef2SZheng Liu goto add_delayed; 1703d100eef2SZheng Liu } 1704d100eef2SZheng Liu 1705d100eef2SZheng Liu /* 1706d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1707d100eef2SZheng Liu * So we need to check it. 1708d100eef2SZheng Liu */ 1709d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1710d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1711d100eef2SZheng Liu set_buffer_new(bh); 1712d100eef2SZheng Liu set_buffer_delay(bh); 1713d100eef2SZheng Liu return 0; 1714d100eef2SZheng Liu } 1715d100eef2SZheng Liu 1716d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1717d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1718d100eef2SZheng Liu if (retval > map->m_len) 1719d100eef2SZheng Liu retval = map->m_len; 1720d100eef2SZheng Liu map->m_len = retval; 1721d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1722d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1723d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1724d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1725d100eef2SZheng Liu else 17261e83bc81SArnd Bergmann BUG(); 1727d100eef2SZheng Liu 1728921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1729921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1730921f266bSDmitry Monakhov #endif 1731d100eef2SZheng Liu return retval; 1732d100eef2SZheng Liu } 1733d100eef2SZheng Liu 17345356f261SAditya Kali /* 17355356f261SAditya Kali * Try to see if we can get the block without requesting a new 17365356f261SAditya Kali * file system block. 17375356f261SAditya Kali */ 1738c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1739cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17409c3569b5STao Ma retval = 0; 1741cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17422f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17435356f261SAditya Kali else 17442f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17455356f261SAditya Kali 1746d100eef2SZheng Liu add_delayed: 17475356f261SAditya Kali if (retval == 0) { 1748f7fec032SZheng Liu int ret; 1749ad431025SEric Whitney 17505356f261SAditya Kali /* 17515356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17525356f261SAditya Kali * is it OK? 17535356f261SAditya Kali */ 17545356f261SAditya Kali 17550b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17560b02f4c0SEric Whitney if (ret != 0) { 1757f7fec032SZheng Liu retval = ret; 175851865fdaSZheng Liu goto out_unlock; 1759f7fec032SZheng Liu } 176051865fdaSZheng Liu 17615356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17625356f261SAditya Kali set_buffer_new(bh); 17635356f261SAditya Kali set_buffer_delay(bh); 1764f7fec032SZheng Liu } else if (retval > 0) { 1765f7fec032SZheng Liu int ret; 17663be78c73STheodore Ts'o unsigned int status; 1767f7fec032SZheng Liu 176844fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 176944fb851dSZheng Liu ext4_warning(inode->i_sb, 177044fb851dSZheng Liu "ES len assertion failed for inode " 177144fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 177244fb851dSZheng Liu inode->i_ino, retval, map->m_len); 177344fb851dSZheng Liu WARN_ON(1); 1774921f266bSDmitry Monakhov } 1775921f266bSDmitry Monakhov 1776f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1777f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1778f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1779f7fec032SZheng Liu map->m_pblk, status); 1780f7fec032SZheng Liu if (ret != 0) 1781f7fec032SZheng Liu retval = ret; 17825356f261SAditya Kali } 17835356f261SAditya Kali 17845356f261SAditya Kali out_unlock: 17855356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17865356f261SAditya Kali 17875356f261SAditya Kali return retval; 17885356f261SAditya Kali } 17895356f261SAditya Kali 17905356f261SAditya Kali /* 1791d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1792b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1793b920c755STheodore Ts'o * reserve space for a single block. 179429fa89d0SAneesh Kumar K.V * 179529fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 179629fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 179729fa89d0SAneesh Kumar K.V * 179829fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 179929fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 180029fa89d0SAneesh Kumar K.V * initialized properly. 180164769240SAlex Tomas */ 18029c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18032ed88685STheodore Ts'o struct buffer_head *bh, int create) 180464769240SAlex Tomas { 18052ed88685STheodore Ts'o struct ext4_map_blocks map; 180664769240SAlex Tomas int ret = 0; 180764769240SAlex Tomas 180864769240SAlex Tomas BUG_ON(create == 0); 18092ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18102ed88685STheodore Ts'o 18112ed88685STheodore Ts'o map.m_lblk = iblock; 18122ed88685STheodore Ts'o map.m_len = 1; 181364769240SAlex Tomas 181464769240SAlex Tomas /* 181564769240SAlex Tomas * first, we need to know whether the block is allocated already 181664769240SAlex Tomas * preallocated blocks are unmapped but should treated 181764769240SAlex Tomas * the same as allocated blocks. 181864769240SAlex Tomas */ 18195356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18205356f261SAditya Kali if (ret <= 0) 18212ed88685STheodore Ts'o return ret; 182264769240SAlex Tomas 18232ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1824ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18252ed88685STheodore Ts'o 18262ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18272ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18282ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18292ed88685STheodore Ts'o * get_block multiple times when we write to the same 18302ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18312ed88685STheodore Ts'o * for partial write. 18322ed88685STheodore Ts'o */ 18332ed88685STheodore Ts'o set_buffer_new(bh); 1834c8205636STheodore Ts'o set_buffer_mapped(bh); 18352ed88685STheodore Ts'o } 18362ed88685STheodore Ts'o return 0; 183764769240SAlex Tomas } 183861628a3fSMingming Cao 183962e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 184062e086beSAneesh Kumar K.V { 184162e086beSAneesh Kumar K.V get_bh(bh); 184262e086beSAneesh Kumar K.V return 0; 184362e086beSAneesh Kumar K.V } 184462e086beSAneesh Kumar K.V 184562e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 184662e086beSAneesh Kumar K.V { 184762e086beSAneesh Kumar K.V put_bh(bh); 184862e086beSAneesh Kumar K.V return 0; 184962e086beSAneesh Kumar K.V } 185062e086beSAneesh Kumar K.V 185162e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 185262e086beSAneesh Kumar K.V unsigned int len) 185362e086beSAneesh Kumar K.V { 185462e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 185562e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18563fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 185762e086beSAneesh Kumar K.V handle_t *handle = NULL; 18583fdcfb66STao Ma int ret = 0, err = 0; 18593fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18603fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 186162e086beSAneesh Kumar K.V 1862cb20d518STheodore Ts'o ClearPageChecked(page); 18633fdcfb66STao Ma 18643fdcfb66STao Ma if (inline_data) { 18653fdcfb66STao Ma BUG_ON(page->index != 0); 18663fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18673fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18683fdcfb66STao Ma if (inode_bh == NULL) 18693fdcfb66STao Ma goto out; 18703fdcfb66STao Ma } else { 187162e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18723fdcfb66STao Ma if (!page_bufs) { 18733fdcfb66STao Ma BUG(); 18743fdcfb66STao Ma goto out; 18753fdcfb66STao Ma } 18763fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18773fdcfb66STao Ma NULL, bget_one); 18783fdcfb66STao Ma } 1879bdf96838STheodore Ts'o /* 1880bdf96838STheodore Ts'o * We need to release the page lock before we start the 1881bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1882bdf96838STheodore Ts'o * out from under us. 1883bdf96838STheodore Ts'o */ 1884bdf96838STheodore Ts'o get_page(page); 188562e086beSAneesh Kumar K.V unlock_page(page); 188662e086beSAneesh Kumar K.V 18879924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18889924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 188962e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 189062e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1891bdf96838STheodore Ts'o put_page(page); 1892bdf96838STheodore Ts'o goto out_no_pagelock; 1893bdf96838STheodore Ts'o } 1894bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1895bdf96838STheodore Ts'o 1896bdf96838STheodore Ts'o lock_page(page); 1897bdf96838STheodore Ts'o put_page(page); 1898bdf96838STheodore Ts'o if (page->mapping != mapping) { 1899bdf96838STheodore Ts'o /* The page got truncated from under us */ 1900bdf96838STheodore Ts'o ext4_journal_stop(handle); 1901bdf96838STheodore Ts'o ret = 0; 190262e086beSAneesh Kumar K.V goto out; 190362e086beSAneesh Kumar K.V } 190462e086beSAneesh Kumar K.V 19053fdcfb66STao Ma if (inline_data) { 1906362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19073fdcfb66STao Ma } else { 1908f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190962e086beSAneesh Kumar K.V do_journal_get_write_access); 191062e086beSAneesh Kumar K.V 1911f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 191262e086beSAneesh Kumar K.V write_end_fn); 19133fdcfb66STao Ma } 191462e086beSAneesh Kumar K.V if (ret == 0) 191562e086beSAneesh Kumar K.V ret = err; 19162d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 191762e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 191862e086beSAneesh Kumar K.V if (!ret) 191962e086beSAneesh Kumar K.V ret = err; 192062e086beSAneesh Kumar K.V 19213fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19228c9367fdSTheodore Ts'o ext4_walk_page_buffers(NULL, page_bufs, 0, len, 19233fdcfb66STao Ma NULL, bput_one); 192419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 192562e086beSAneesh Kumar K.V out: 1926bdf96838STheodore Ts'o unlock_page(page); 1927bdf96838STheodore Ts'o out_no_pagelock: 19283fdcfb66STao Ma brelse(inode_bh); 192962e086beSAneesh Kumar K.V return ret; 193062e086beSAneesh Kumar K.V } 193162e086beSAneesh Kumar K.V 193261628a3fSMingming Cao /* 193343ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 193443ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 193543ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 193643ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193743ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 193843ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 193943ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 194043ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 194143ce1d23SAneesh Kumar K.V * 1942b920c755STheodore Ts'o * This function can get called via... 194320970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1944b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1945f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1946b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194743ce1d23SAneesh Kumar K.V * 194843ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 194943ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 195043ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 195143ce1d23SAneesh Kumar K.V * truncate(f, 1024); 195243ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195343ce1d23SAneesh Kumar K.V * a[0] = 'a'; 195443ce1d23SAneesh Kumar K.V * truncate(f, 4096); 195543ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 195690802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195743ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 195843ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 195943ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 196043ce1d23SAneesh Kumar K.V * buffer_heads mapped. 196143ce1d23SAneesh Kumar K.V * 196243ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196343ce1d23SAneesh Kumar K.V * unwritten in the page. 196443ce1d23SAneesh Kumar K.V * 196543ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 196643ce1d23SAneesh Kumar K.V * 196743ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 196843ce1d23SAneesh Kumar K.V * ext4_writepage() 196943ce1d23SAneesh Kumar K.V * 197043ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 197143ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 197261628a3fSMingming Cao */ 197343ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 197464769240SAlex Tomas struct writeback_control *wbc) 197564769240SAlex Tomas { 1976f8bec370SJan Kara int ret = 0; 197761628a3fSMingming Cao loff_t size; 1978498e5f24STheodore Ts'o unsigned int len; 1979744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 198061628a3fSMingming Cao struct inode *inode = page->mapping->host; 198136ade451SJan Kara struct ext4_io_submit io_submit; 19821c8349a1SNamjae Jeon bool keep_towrite = false; 198364769240SAlex Tomas 19840db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 1985c2a559bcSyangerkun inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 19860db1ff22STheodore Ts'o unlock_page(page); 19870db1ff22STheodore Ts'o return -EIO; 19880db1ff22STheodore Ts'o } 19890db1ff22STheodore Ts'o 1990a9c667f8SLukas Czerner trace_ext4_writepage(page); 199161628a3fSMingming Cao size = i_size_read(inode); 1992c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 1993c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 199409cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 199561628a3fSMingming Cao else 199609cbfeafSKirill A. Shutemov len = PAGE_SIZE; 199761628a3fSMingming Cao 1998f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 199964769240SAlex Tomas /* 2000fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2001fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2002fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2003fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2004fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2005cccd147aSTheodore Ts'o * 2006cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2007cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2008cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2009cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2010cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2011cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2012cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2013cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2014cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 201564769240SAlex Tomas */ 2016f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2017c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 201861628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2019cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 202009cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2021fe386132SJan Kara /* 2022fe386132SJan Kara * For memory cleaning there's no point in writing only 2023fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2024fe386132SJan Kara * from direct reclaim. 2025fe386132SJan Kara */ 2026fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2027fe386132SJan Kara == PF_MEMALLOC); 202861628a3fSMingming Cao unlock_page(page); 202961628a3fSMingming Cao return 0; 203061628a3fSMingming Cao } 20311c8349a1SNamjae Jeon keep_towrite = true; 2032f0e6c985SAneesh Kumar K.V } 203364769240SAlex Tomas 2034cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 203543ce1d23SAneesh Kumar K.V /* 203643ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 203743ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 203843ce1d23SAneesh Kumar K.V */ 20393f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 204043ce1d23SAneesh Kumar K.V 204197a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 204297a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 204397a851edSJan Kara if (!io_submit.io_end) { 204497a851edSJan Kara redirty_page_for_writepage(wbc, page); 204597a851edSJan Kara unlock_page(page); 204697a851edSJan Kara return -ENOMEM; 204797a851edSJan Kara } 20481c8349a1SNamjae Jeon ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite); 204936ade451SJan Kara ext4_io_submit(&io_submit); 205097a851edSJan Kara /* Drop io_end reference we got from init */ 205197a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 205264769240SAlex Tomas return ret; 205364769240SAlex Tomas } 205464769240SAlex Tomas 20555f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20565f1132b2SJan Kara { 20575f1132b2SJan Kara int len; 2058a056bdaaSJan Kara loff_t size; 20595f1132b2SJan Kara int err; 20605f1132b2SJan Kara 20615f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2062a056bdaaSJan Kara clear_page_dirty_for_io(page); 2063a056bdaaSJan Kara /* 2064a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2065a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2066a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2067a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2068a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2069a056bdaaSJan Kara * written to again until we release page lock. So only after 2070a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2071a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2072a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2073a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2074a056bdaaSJan Kara * after page tables are updated. 2075a056bdaaSJan Kara */ 2076a056bdaaSJan Kara size = i_size_read(mpd->inode); 2077c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2078c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 207909cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20805f1132b2SJan Kara else 208109cbfeafSKirill A. Shutemov len = PAGE_SIZE; 20821c8349a1SNamjae Jeon err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false); 20835f1132b2SJan Kara if (!err) 20845f1132b2SJan Kara mpd->wbc->nr_to_write--; 20855f1132b2SJan Kara mpd->first_page++; 20865f1132b2SJan Kara 20875f1132b2SJan Kara return err; 20885f1132b2SJan Kara } 20895f1132b2SJan Kara 20906db07461SRitesh Harjani #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay)) 20914e7ea81dSJan Kara 209261628a3fSMingming Cao /* 2093fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2094fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2095fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 209661628a3fSMingming Cao */ 2097fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2098525f4ed8SMingming Cao 2099525f4ed8SMingming Cao /* 21004e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 21014e7ea81dSJan Kara * 21024e7ea81dSJan Kara * @mpd - extent of blocks 21034e7ea81dSJan Kara * @lblk - logical number of the block in the file 210409930042SJan Kara * @bh - buffer head we want to add to the extent 21054e7ea81dSJan Kara * 210609930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 210709930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 210809930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 210909930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 211009930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 211109930042SJan Kara * added. 21124e7ea81dSJan Kara */ 211309930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 211409930042SJan Kara struct buffer_head *bh) 21154e7ea81dSJan Kara { 21164e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21174e7ea81dSJan Kara 211809930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 211909930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 212009930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 212109930042SJan Kara /* So far no extent to map => we write the buffer right away */ 212209930042SJan Kara if (map->m_len == 0) 212309930042SJan Kara return true; 212409930042SJan Kara return false; 212509930042SJan Kara } 21264e7ea81dSJan Kara 21274e7ea81dSJan Kara /* First block in the extent? */ 21284e7ea81dSJan Kara if (map->m_len == 0) { 2129dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2130dddbd6acSJan Kara if (!mpd->do_map) 2131dddbd6acSJan Kara return false; 21324e7ea81dSJan Kara map->m_lblk = lblk; 21334e7ea81dSJan Kara map->m_len = 1; 213409930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 213509930042SJan Kara return true; 21364e7ea81dSJan Kara } 21374e7ea81dSJan Kara 213809930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 213909930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 214009930042SJan Kara return false; 214109930042SJan Kara 21424e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21434e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 214409930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21454e7ea81dSJan Kara map->m_len++; 214609930042SJan Kara return true; 21474e7ea81dSJan Kara } 214809930042SJan Kara return false; 21494e7ea81dSJan Kara } 21504e7ea81dSJan Kara 21515f1132b2SJan Kara /* 21525f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21535f1132b2SJan Kara * 21545f1132b2SJan Kara * @mpd - extent of blocks for mapping 21555f1132b2SJan Kara * @head - the first buffer in the page 21565f1132b2SJan Kara * @bh - buffer we should start processing from 21575f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21585f1132b2SJan Kara * 21595f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21605f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21615f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21625f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21635f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21645f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21655f1132b2SJan Kara * < 0 in case of error during IO submission. 21665f1132b2SJan Kara */ 21675f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21684e7ea81dSJan Kara struct buffer_head *head, 21694e7ea81dSJan Kara struct buffer_head *bh, 21704e7ea81dSJan Kara ext4_lblk_t lblk) 21714e7ea81dSJan Kara { 21724e7ea81dSJan Kara struct inode *inode = mpd->inode; 21735f1132b2SJan Kara int err; 217493407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21754e7ea81dSJan Kara >> inode->i_blkbits; 21764e7ea81dSJan Kara 2177c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2178c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2179c93d8f88SEric Biggers 21804e7ea81dSJan Kara do { 21814e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21824e7ea81dSJan Kara 218309930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21844e7ea81dSJan Kara /* Found extent to map? */ 21854e7ea81dSJan Kara if (mpd->map.m_len) 21865f1132b2SJan Kara return 0; 2187dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2188dddbd6acSJan Kara if (!mpd->do_map) 2189dddbd6acSJan Kara return 0; 219009930042SJan Kara /* Everything mapped so far and we hit EOF */ 21915f1132b2SJan Kara break; 21924e7ea81dSJan Kara } 21934e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 21945f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 21955f1132b2SJan Kara if (mpd->map.m_len == 0) { 21965f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 21975f1132b2SJan Kara if (err < 0) 21984e7ea81dSJan Kara return err; 21994e7ea81dSJan Kara } 22006b8ed620SJan Kara if (lblk >= blocks) { 22016b8ed620SJan Kara mpd->scanned_until_end = 1; 22026b8ed620SJan Kara return 0; 22036b8ed620SJan Kara } 22046b8ed620SJan Kara return 1; 22055f1132b2SJan Kara } 22064e7ea81dSJan Kara 22074e7ea81dSJan Kara /* 22082943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22092943fdbcSRitesh Harjani * may submit fully mapped page for IO 22102943fdbcSRitesh Harjani * 22112943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22122943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22132943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22142943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22152943fdbcSRitesh Harjani * mapping or not. 22162943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22172943fdbcSRitesh Harjani * state according to new extent state. 22182943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22192943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22202943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22212943fdbcSRitesh Harjani */ 22222943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22232943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22242943fdbcSRitesh Harjani bool *map_bh) 22252943fdbcSRitesh Harjani { 22262943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22272943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22282943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22292943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22302943fdbcSRitesh Harjani int err = 0; 2231c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2232c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2233c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22342943fdbcSRitesh Harjani 22352943fdbcSRitesh Harjani bh = head = page_buffers(page); 22362943fdbcSRitesh Harjani do { 22372943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22382943fdbcSRitesh Harjani continue; 22392943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22402943fdbcSRitesh Harjani /* 22412943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22422943fdbcSRitesh Harjani * Find next buffer in the page to map. 22432943fdbcSRitesh Harjani */ 22442943fdbcSRitesh Harjani mpd->map.m_len = 0; 22452943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2246c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2247c8cc8816SRitesh Harjani io_end_size = 0; 22482943fdbcSRitesh Harjani 22492943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22502943fdbcSRitesh Harjani if (err > 0) 22512943fdbcSRitesh Harjani err = 0; 2252c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2253c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22544d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22554d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22564d06bfb9SRitesh Harjani goto out; 22574d06bfb9SRitesh Harjani } 2258c8cc8816SRitesh Harjani io_end_vec->offset = mpd->map.m_lblk << blkbits; 2259c8cc8816SRitesh Harjani } 22602943fdbcSRitesh Harjani *map_bh = true; 22612943fdbcSRitesh Harjani goto out; 22622943fdbcSRitesh Harjani } 22632943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22642943fdbcSRitesh Harjani clear_buffer_delay(bh); 22652943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22662943fdbcSRitesh Harjani } 22672943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2268c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22692943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2270c8cc8816SRitesh Harjani 2271c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2272c8cc8816SRitesh Harjani io_end_size = 0; 22732943fdbcSRitesh Harjani *map_bh = false; 22742943fdbcSRitesh Harjani out: 22752943fdbcSRitesh Harjani *m_lblk = lblk; 22762943fdbcSRitesh Harjani *m_pblk = pblock; 22772943fdbcSRitesh Harjani return err; 22782943fdbcSRitesh Harjani } 22792943fdbcSRitesh Harjani 22802943fdbcSRitesh Harjani /* 22814e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22824e7ea81dSJan Kara * submit fully mapped pages for IO 22834e7ea81dSJan Kara * 22844e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22854e7ea81dSJan Kara * 22864e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22874e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22884e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2289556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 22904e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 22914e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 22924e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 22934e7ea81dSJan Kara */ 22944e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 22954e7ea81dSJan Kara { 22964e7ea81dSJan Kara struct pagevec pvec; 22974e7ea81dSJan Kara int nr_pages, i; 22984e7ea81dSJan Kara struct inode *inode = mpd->inode; 229909cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 23004e7ea81dSJan Kara pgoff_t start, end; 23014e7ea81dSJan Kara ext4_lblk_t lblk; 23022943fdbcSRitesh Harjani ext4_fsblk_t pblock; 23034e7ea81dSJan Kara int err; 23042943fdbcSRitesh Harjani bool map_bh = false; 23054e7ea81dSJan Kara 23064e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23074e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23084e7ea81dSJan Kara lblk = start << bpp_bits; 23094e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23104e7ea81dSJan Kara 231186679820SMel Gorman pagevec_init(&pvec); 23124e7ea81dSJan Kara while (start <= end) { 23132b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2314397162ffSJan Kara &start, end); 23154e7ea81dSJan Kara if (nr_pages == 0) 23164e7ea81dSJan Kara break; 23174e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23184e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23194e7ea81dSJan Kara 23202943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23212943fdbcSRitesh Harjani &map_bh); 23224e7ea81dSJan Kara /* 23232943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23242943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23252943fdbcSRitesh Harjani * So we return to call further extent mapping. 23264e7ea81dSJan Kara */ 232739c0ae16SJason Yan if (err < 0 || map_bh) 23282943fdbcSRitesh Harjani goto out; 23294e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23304e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23312943fdbcSRitesh Harjani if (err < 0) 23322943fdbcSRitesh Harjani goto out; 23334e7ea81dSJan Kara } 23344e7ea81dSJan Kara pagevec_release(&pvec); 23354e7ea81dSJan Kara } 23364e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23374e7ea81dSJan Kara mpd->map.m_len = 0; 23384e7ea81dSJan Kara mpd->map.m_flags = 0; 23394e7ea81dSJan Kara return 0; 23402943fdbcSRitesh Harjani out: 23412943fdbcSRitesh Harjani pagevec_release(&pvec); 23422943fdbcSRitesh Harjani return err; 23434e7ea81dSJan Kara } 23444e7ea81dSJan Kara 23454e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23464e7ea81dSJan Kara { 23474e7ea81dSJan Kara struct inode *inode = mpd->inode; 23484e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23494e7ea81dSJan Kara int get_blocks_flags; 2350090f32eeSLukas Czerner int err, dioread_nolock; 23514e7ea81dSJan Kara 23524e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23534e7ea81dSJan Kara /* 23544e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2355556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23564e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23574e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23584e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23594e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23604e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23614e7ea81dSJan Kara * possible. 23624e7ea81dSJan Kara * 2363754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2364754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2365754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2366754cfed6STheodore Ts'o * the data was copied into the page cache. 23674e7ea81dSJan Kara */ 23684e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2369ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2370ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2371090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2372090f32eeSLukas Czerner if (dioread_nolock) 23734e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23746db07461SRitesh Harjani if (map->m_flags & BIT(BH_Delay)) 23754e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23764e7ea81dSJan Kara 23774e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23784e7ea81dSJan Kara if (err < 0) 23794e7ea81dSJan Kara return err; 2380090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23816b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23826b523df4SJan Kara ext4_handle_valid(handle)) { 23836b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23846b523df4SJan Kara handle->h_rsv_handle = NULL; 23856b523df4SJan Kara } 23863613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23876b523df4SJan Kara } 23884e7ea81dSJan Kara 23894e7ea81dSJan Kara BUG_ON(map->m_len == 0); 23904e7ea81dSJan Kara return 0; 23914e7ea81dSJan Kara } 23924e7ea81dSJan Kara 23934e7ea81dSJan Kara /* 23944e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 23954e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 23964e7ea81dSJan Kara * 23974e7ea81dSJan Kara * @handle - handle for journal operations 23984e7ea81dSJan Kara * @mpd - extent to map 23997534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 24007534e854SJan Kara * is no hope of writing the data. The caller should discard 24017534e854SJan Kara * dirty pages to avoid infinite loops. 24024e7ea81dSJan Kara * 24034e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 24044e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 24054e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 24064e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24074e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24084e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24094e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24104e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24114e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24124e7ea81dSJan Kara */ 24134e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2414cb530541STheodore Ts'o struct mpage_da_data *mpd, 2415cb530541STheodore Ts'o bool *give_up_on_write) 24164e7ea81dSJan Kara { 24174e7ea81dSJan Kara struct inode *inode = mpd->inode; 24184e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24194e7ea81dSJan Kara int err; 24204e7ea81dSJan Kara loff_t disksize; 24216603120eSDmitry Monakhov int progress = 0; 2422c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24234d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24244e7ea81dSJan Kara 24254d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24264d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24274d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2428c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 242927d7c4edSJan Kara do { 24304e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24314e7ea81dSJan Kara if (err < 0) { 24324e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24334e7ea81dSJan Kara 24340db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24350db1ff22STheodore Ts'o EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED) 2436cb530541STheodore Ts'o goto invalidate_dirty_pages; 24374e7ea81dSJan Kara /* 2438cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2439cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2440cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24414e7ea81dSJan Kara */ 2442cb530541STheodore Ts'o if ((err == -ENOMEM) || 24436603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24446603120eSDmitry Monakhov if (progress) 24456603120eSDmitry Monakhov goto update_disksize; 2446cb530541STheodore Ts'o return err; 24476603120eSDmitry Monakhov } 24484e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24494e7ea81dSJan Kara "Delayed block allocation failed for " 24504e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24514e7ea81dSJan Kara " max blocks %u with error %d", 24524e7ea81dSJan Kara inode->i_ino, 24534e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2454cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24554e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24564e7ea81dSJan Kara "This should not happen!! Data will " 24574e7ea81dSJan Kara "be lost\n"); 24584e7ea81dSJan Kara if (err == -ENOSPC) 24594e7ea81dSJan Kara ext4_print_free_blocks(inode); 2460cb530541STheodore Ts'o invalidate_dirty_pages: 2461cb530541STheodore Ts'o *give_up_on_write = true; 24624e7ea81dSJan Kara return err; 24634e7ea81dSJan Kara } 24646603120eSDmitry Monakhov progress = 1; 24654e7ea81dSJan Kara /* 24664e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24674e7ea81dSJan Kara * extent to map 24684e7ea81dSJan Kara */ 24694e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24704e7ea81dSJan Kara if (err < 0) 24716603120eSDmitry Monakhov goto update_disksize; 247227d7c4edSJan Kara } while (map->m_len); 24734e7ea81dSJan Kara 24746603120eSDmitry Monakhov update_disksize: 2475622cad13STheodore Ts'o /* 2476622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2477622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2478622cad13STheodore Ts'o */ 247909cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 248035df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 24814e7ea81dSJan Kara int err2; 2482622cad13STheodore Ts'o loff_t i_size; 24834e7ea81dSJan Kara 2484622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2485622cad13STheodore Ts'o i_size = i_size_read(inode); 2486622cad13STheodore Ts'o if (disksize > i_size) 2487622cad13STheodore Ts'o disksize = i_size; 2488622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2489622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2490622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2491b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2492878520acSTheodore Ts'o if (err2) { 249354d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 24944e7ea81dSJan Kara "Failed to mark inode %lu dirty", 24954e7ea81dSJan Kara inode->i_ino); 2496878520acSTheodore Ts'o } 24974e7ea81dSJan Kara if (!err) 24984e7ea81dSJan Kara err = err2; 24994e7ea81dSJan Kara } 25004e7ea81dSJan Kara return err; 25014e7ea81dSJan Kara } 25024e7ea81dSJan Kara 25034e7ea81dSJan Kara /* 2504fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 250520970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2506fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2507fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2508fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2509525f4ed8SMingming Cao */ 2510fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2511fffb2739SJan Kara { 2512fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2513525f4ed8SMingming Cao 2514fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2515fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2516525f4ed8SMingming Cao } 251761628a3fSMingming Cao 25188e48dcfbSTheodore Ts'o /* 25194e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25204e7ea81dSJan Kara * and underlying extent to map 25214e7ea81dSJan Kara * 25224e7ea81dSJan Kara * @mpd - where to look for pages 25234e7ea81dSJan Kara * 25244e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25254e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25264e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25274e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25284e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25294e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25304e7ea81dSJan Kara * 25314e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25324e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25334e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25344e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25358e48dcfbSTheodore Ts'o */ 25364e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25378e48dcfbSTheodore Ts'o { 25384e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25398e48dcfbSTheodore Ts'o struct pagevec pvec; 25404f01b02cSTheodore Ts'o unsigned int nr_pages; 2541aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25424e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25434e7ea81dSJan Kara pgoff_t end = mpd->last_page; 254410bbd235SMatthew Wilcox xa_mark_t tag; 25454e7ea81dSJan Kara int i, err = 0; 25464e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25474e7ea81dSJan Kara ext4_lblk_t lblk; 25484e7ea81dSJan Kara struct buffer_head *head; 25498e48dcfbSTheodore Ts'o 25504e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25515b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25525b41d924SEric Sandeen else 25535b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25545b41d924SEric Sandeen 255586679820SMel Gorman pagevec_init(&pvec); 25564e7ea81dSJan Kara mpd->map.m_len = 0; 25574e7ea81dSJan Kara mpd->next_page = index; 25584f01b02cSTheodore Ts'o while (index <= end) { 2559dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 256067fd707fSJan Kara tag); 25618e48dcfbSTheodore Ts'o if (nr_pages == 0) 25626b8ed620SJan Kara break; 25638e48dcfbSTheodore Ts'o 25648e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25658e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25668e48dcfbSTheodore Ts'o 25678e48dcfbSTheodore Ts'o /* 2568aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2569aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2570aeac589aSMing Lei * keep going because someone may be concurrently 2571aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2572aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2573aeac589aSMing Lei * of the old dirty pages. 2574aeac589aSMing Lei */ 2575aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2576aeac589aSMing Lei goto out; 2577aeac589aSMing Lei 25784e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25794e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25804e7ea81dSJan Kara goto out; 258178aaced3STheodore Ts'o 25828e48dcfbSTheodore Ts'o lock_page(page); 25838e48dcfbSTheodore Ts'o /* 25844e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25854e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25864e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25874e7ea81dSJan Kara * page is already under writeback and we are not doing 25884e7ea81dSJan Kara * a data integrity writeback, skip the page 25898e48dcfbSTheodore Ts'o */ 25904f01b02cSTheodore Ts'o if (!PageDirty(page) || 25914f01b02cSTheodore Ts'o (PageWriteback(page) && 25924e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 25934f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 25948e48dcfbSTheodore Ts'o unlock_page(page); 25958e48dcfbSTheodore Ts'o continue; 25968e48dcfbSTheodore Ts'o } 25978e48dcfbSTheodore Ts'o 25988e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 25998e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 26008e48dcfbSTheodore Ts'o 26014e7ea81dSJan Kara if (mpd->map.m_len == 0) 26028eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 26038eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2604f8bec370SJan Kara /* Add all dirty buffers to mpd */ 26054e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 260609cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 26078eb9e5ceSTheodore Ts'o head = page_buffers(page); 26085f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26095f1132b2SJan Kara if (err <= 0) 26104e7ea81dSJan Kara goto out; 26115f1132b2SJan Kara err = 0; 2612aeac589aSMing Lei left--; 26138e48dcfbSTheodore Ts'o } 26148e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26158e48dcfbSTheodore Ts'o cond_resched(); 26168e48dcfbSTheodore Ts'o } 26176b8ed620SJan Kara mpd->scanned_until_end = 1; 26184f01b02cSTheodore Ts'o return 0; 26198eb9e5ceSTheodore Ts'o out: 26208eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26214e7ea81dSJan Kara return err; 26228e48dcfbSTheodore Ts'o } 26238e48dcfbSTheodore Ts'o 262420970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 262564769240SAlex Tomas struct writeback_control *wbc) 262664769240SAlex Tomas { 26274e7ea81dSJan Kara pgoff_t writeback_index = 0; 26284e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 262922208dedSAneesh Kumar K.V int range_whole = 0; 26304e7ea81dSJan Kara int cycled = 1; 263161628a3fSMingming Cao handle_t *handle = NULL; 2632df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26335e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26346b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26355e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26361bce63d1SShaohua Li struct blk_plug plug; 2637cb530541STheodore Ts'o bool give_up_on_write = false; 263861628a3fSMingming Cao 26390db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26400db1ff22STheodore Ts'o return -EIO; 26410db1ff22STheodore Ts'o 2642bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 264320970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2644ba80b101STheodore Ts'o 264561628a3fSMingming Cao /* 264661628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 264761628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 264861628a3fSMingming Cao * because that could violate lock ordering on umount 264961628a3fSMingming Cao */ 2650a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2651bbf023c7SMing Lei goto out_writepages; 26522a21e37eSTheodore Ts'o 265320970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2654043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2655bbf023c7SMing Lei goto out_writepages; 265620970ba6STheodore Ts'o } 265720970ba6STheodore Ts'o 26582a21e37eSTheodore Ts'o /* 26592a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26602a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26612a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26621751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26632a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 266420970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26652a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26662a21e37eSTheodore Ts'o * the stack trace. 26672a21e37eSTheodore Ts'o */ 26680db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26690db1ff22STheodore Ts'o sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) { 2670bbf023c7SMing Lei ret = -EROFS; 2671bbf023c7SMing Lei goto out_writepages; 2672bbf023c7SMing Lei } 26732a21e37eSTheodore Ts'o 26744e7ea81dSJan Kara /* 26754e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26764e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26774e7ea81dSJan Kara * we'd better clear the inline data here. 26784e7ea81dSJan Kara */ 26794e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26804e7ea81dSJan Kara /* Just inode will be modified... */ 26814e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26824e7ea81dSJan Kara if (IS_ERR(handle)) { 26834e7ea81dSJan Kara ret = PTR_ERR(handle); 26844e7ea81dSJan Kara goto out_writepages; 26854e7ea81dSJan Kara } 26864e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26874e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26884e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26894e7ea81dSJan Kara ext4_journal_stop(handle); 26904e7ea81dSJan Kara } 26914e7ea81dSJan Kara 26924e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26934e343231Syangerkun /* 26944e343231Syangerkun * We may need to convert up to one extent per block in 26954e343231Syangerkun * the page and we may dirty the inode. 26964e343231Syangerkun */ 26974e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 26984e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 26994e343231Syangerkun } 27004e343231Syangerkun 270122208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 270222208dedSAneesh Kumar K.V range_whole = 1; 270361628a3fSMingming Cao 27042acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 27054e7ea81dSJan Kara writeback_index = mapping->writeback_index; 27064e7ea81dSJan Kara if (writeback_index) 27072acf2c26SAneesh Kumar K.V cycled = 0; 27084e7ea81dSJan Kara mpd.first_page = writeback_index; 27094e7ea81dSJan Kara mpd.last_page = -1; 27105b41d924SEric Sandeen } else { 271109cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 271209cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27135b41d924SEric Sandeen } 2714a1d6cc56SAneesh Kumar K.V 27154e7ea81dSJan Kara mpd.inode = inode; 27164e7ea81dSJan Kara mpd.wbc = wbc; 27174e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27182acf2c26SAneesh Kumar K.V retry: 27196e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27204e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27211bce63d1SShaohua Li blk_start_plug(&plug); 2722dddbd6acSJan Kara 2723dddbd6acSJan Kara /* 2724dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2725dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2726dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2727dddbd6acSJan Kara * started. 2728dddbd6acSJan Kara */ 2729dddbd6acSJan Kara mpd.do_map = 0; 27306b8ed620SJan Kara mpd.scanned_until_end = 0; 2731dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2732dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2733dddbd6acSJan Kara ret = -ENOMEM; 2734dddbd6acSJan Kara goto unplug; 2735dddbd6acSJan Kara } 2736dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2737a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2738a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2739dddbd6acSJan Kara /* Submit prepared bio */ 2740dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2741dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2742dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2743dddbd6acSJan Kara if (ret < 0) 2744dddbd6acSJan Kara goto unplug; 2745dddbd6acSJan Kara 27466b8ed620SJan Kara while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { 27474e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27484e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27494e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27504e7ea81dSJan Kara ret = -ENOMEM; 27514e7ea81dSJan Kara break; 27524e7ea81dSJan Kara } 2753a1d6cc56SAneesh Kumar K.V 2754a1d6cc56SAneesh Kumar K.V /* 27554e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27564e7ea81dSJan Kara * must always write out whole page (makes a difference when 27574e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27584e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27594e7ea81dSJan Kara * not supported by delalloc. 2760a1d6cc56SAneesh Kumar K.V */ 2761a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2762525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2763a1d6cc56SAneesh Kumar K.V 276461628a3fSMingming Cao /* start a new transaction */ 27656b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27666b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 276761628a3fSMingming Cao if (IS_ERR(handle)) { 276861628a3fSMingming Cao ret = PTR_ERR(handle); 27691693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2770fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2771a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27724e7ea81dSJan Kara /* Release allocated io_end */ 27734e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2774dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27754e7ea81dSJan Kara break; 277661628a3fSMingming Cao } 2777dddbd6acSJan Kara mpd.do_map = 1; 2778f63e6005STheodore Ts'o 27794e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27804e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27816b8ed620SJan Kara if (!ret && mpd.map.m_len) 2782cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2783cb530541STheodore Ts'o &give_up_on_write); 2784646caa9cSJan Kara /* 2785646caa9cSJan Kara * Caution: If the handle is synchronous, 2786646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2787646caa9cSJan Kara * to finish which may depend on writeback of pages to 2788646caa9cSJan Kara * complete or on page lock to be released. In that 2789b483bb77SRandy Dunlap * case, we have to wait until after we have 2790646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2791646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2792646caa9cSJan Kara * to be able to complete) before stopping the handle. 2793646caa9cSJan Kara */ 2794646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 279561628a3fSMingming Cao ext4_journal_stop(handle); 2796646caa9cSJan Kara handle = NULL; 2797dddbd6acSJan Kara mpd.do_map = 0; 2798646caa9cSJan Kara } 27994e7ea81dSJan Kara /* Unlock pages we didn't use */ 2800cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2801a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2802a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2803a297b2fcSXiaoguang Wang 2804646caa9cSJan Kara /* 2805646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2806646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2807646caa9cSJan Kara * we are still holding the transaction as we can 2808646caa9cSJan Kara * release the last reference to io_end which may end 2809646caa9cSJan Kara * up doing unwritten extent conversion. 2810646caa9cSJan Kara */ 2811646caa9cSJan Kara if (handle) { 2812646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2813646caa9cSJan Kara ext4_journal_stop(handle); 2814646caa9cSJan Kara } else 28154e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2816dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2817df22291fSAneesh Kumar K.V 28184e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28194e7ea81dSJan Kara /* 28204e7ea81dSJan Kara * Commit the transaction which would 282122208dedSAneesh Kumar K.V * free blocks released in the transaction 282222208dedSAneesh Kumar K.V * and try again 282322208dedSAneesh Kumar K.V */ 2824df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 282522208dedSAneesh Kumar K.V ret = 0; 28264e7ea81dSJan Kara continue; 28274e7ea81dSJan Kara } 28284e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28294e7ea81dSJan Kara if (ret) 283061628a3fSMingming Cao break; 283161628a3fSMingming Cao } 2832dddbd6acSJan Kara unplug: 28331bce63d1SShaohua Li blk_finish_plug(&plug); 28349c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28352acf2c26SAneesh Kumar K.V cycled = 1; 28364e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28374e7ea81dSJan Kara mpd.first_page = 0; 28382acf2c26SAneesh Kumar K.V goto retry; 28392acf2c26SAneesh Kumar K.V } 284061628a3fSMingming Cao 284122208dedSAneesh Kumar K.V /* Update index */ 284222208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 284322208dedSAneesh Kumar K.V /* 28444e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 284522208dedSAneesh Kumar K.V * mode will write it back later 284622208dedSAneesh Kumar K.V */ 28474e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2848a1d6cc56SAneesh Kumar K.V 284961628a3fSMingming Cao out_writepages: 285020970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28514e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2852bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 285361628a3fSMingming Cao return ret; 285464769240SAlex Tomas } 285564769240SAlex Tomas 28565f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28575f0663bbSDan Williams struct writeback_control *wbc) 28585f0663bbSDan Williams { 28595f0663bbSDan Williams int ret; 28605f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28615f0663bbSDan Williams struct inode *inode = mapping->host; 28625f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28635f0663bbSDan Williams 28645f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28655f0663bbSDan Williams return -EIO; 28665f0663bbSDan Williams 2867bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28685f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28695f0663bbSDan Williams 28703f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28715f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28725f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2873bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28745f0663bbSDan Williams return ret; 28755f0663bbSDan Williams } 28765f0663bbSDan Williams 287779f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 287879f0be8dSAneesh Kumar K.V { 28795c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 288079f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 288179f0be8dSAneesh Kumar K.V 288279f0be8dSAneesh Kumar K.V /* 288379f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 288479f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2885179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 288679f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 288779f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 288879f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 288979f0be8dSAneesh Kumar K.V */ 28905c1ff336SEric Whitney free_clusters = 28915c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28925c1ff336SEric Whitney dirty_clusters = 28935c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 289400d4e736STheodore Ts'o /* 289500d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 289600d4e736STheodore Ts'o */ 28975c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 289810ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 289900d4e736STheodore Ts'o 29005c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29015c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 290279f0be8dSAneesh Kumar K.V /* 2903c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2904c8afb446SEric Sandeen * or free blocks is less than watermark 290579f0be8dSAneesh Kumar K.V */ 290679f0be8dSAneesh Kumar K.V return 1; 290779f0be8dSAneesh Kumar K.V } 290879f0be8dSAneesh Kumar K.V return 0; 290979f0be8dSAneesh Kumar K.V } 291079f0be8dSAneesh Kumar K.V 29110ff8947fSEric Sandeen /* We always reserve for an inode update; the superblock could be there too */ 29120ff8947fSEric Sandeen static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) 29130ff8947fSEric Sandeen { 2914e2b911c5SDarrick J. Wong if (likely(ext4_has_feature_large_file(inode->i_sb))) 29150ff8947fSEric Sandeen return 1; 29160ff8947fSEric Sandeen 29170ff8947fSEric Sandeen if (pos + len <= 0x7fffffffULL) 29180ff8947fSEric Sandeen return 1; 29190ff8947fSEric Sandeen 29200ff8947fSEric Sandeen /* We might need to update the superblock to set LARGE_FILE */ 29210ff8947fSEric Sandeen return 2; 29220ff8947fSEric Sandeen } 29230ff8947fSEric Sandeen 292464769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 292564769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 292664769240SAlex Tomas struct page **pagep, void **fsdata) 292764769240SAlex Tomas { 292872b8ab9dSEric Sandeen int ret, retries = 0; 292964769240SAlex Tomas struct page *page; 293064769240SAlex Tomas pgoff_t index; 293164769240SAlex Tomas struct inode *inode = mapping->host; 293264769240SAlex Tomas handle_t *handle; 293364769240SAlex Tomas 29340db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29350db1ff22STheodore Ts'o return -EIO; 29360db1ff22STheodore Ts'o 293709cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 293879f0be8dSAneesh Kumar K.V 2939c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2940c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 294179f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 294279f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 294379f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 294479f0be8dSAneesh Kumar K.V } 294579f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29469bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29479c3569b5STao Ma 29489c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29499c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29509c3569b5STao Ma pos, len, flags, 29519c3569b5STao Ma pagep, fsdata); 29529c3569b5STao Ma if (ret < 0) 295347564bfbSTheodore Ts'o return ret; 295447564bfbSTheodore Ts'o if (ret == 1) 295547564bfbSTheodore Ts'o return 0; 29569c3569b5STao Ma } 29579c3569b5STao Ma 295847564bfbSTheodore Ts'o /* 295947564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 296047564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 296147564bfbSTheodore Ts'o * is being written back. So grab it first before we start 296247564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 296347564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 296447564bfbSTheodore Ts'o */ 296547564bfbSTheodore Ts'o retry_grab: 296647564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 296747564bfbSTheodore Ts'o if (!page) 296847564bfbSTheodore Ts'o return -ENOMEM; 296947564bfbSTheodore Ts'o unlock_page(page); 297047564bfbSTheodore Ts'o 297164769240SAlex Tomas /* 297264769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 297364769240SAlex Tomas * if there is delayed block allocation. But we still need 297464769240SAlex Tomas * to journalling the i_disksize update if writes to the end 297564769240SAlex Tomas * of file which has an already mapped buffer. 297664769240SAlex Tomas */ 297747564bfbSTheodore Ts'o retry_journal: 29780ff8947fSEric Sandeen handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 29790ff8947fSEric Sandeen ext4_da_write_credits(inode, pos, len)); 298064769240SAlex Tomas if (IS_ERR(handle)) { 298109cbfeafSKirill A. Shutemov put_page(page); 298247564bfbSTheodore Ts'o return PTR_ERR(handle); 298364769240SAlex Tomas } 298464769240SAlex Tomas 298547564bfbSTheodore Ts'o lock_page(page); 298647564bfbSTheodore Ts'o if (page->mapping != mapping) { 298747564bfbSTheodore Ts'o /* The page got truncated from under us */ 298847564bfbSTheodore Ts'o unlock_page(page); 298909cbfeafSKirill A. Shutemov put_page(page); 2990d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 299147564bfbSTheodore Ts'o goto retry_grab; 2992d5a0d4f7SEric Sandeen } 299347564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29947afe5aa5SDmitry Monakhov wait_for_stable_page(page); 299564769240SAlex Tomas 2996643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29972058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 29982058f83aSMichael Halcrow ext4_da_get_block_prep); 29992058f83aSMichael Halcrow #else 30006e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 30012058f83aSMichael Halcrow #endif 300264769240SAlex Tomas if (ret < 0) { 300364769240SAlex Tomas unlock_page(page); 300464769240SAlex Tomas ext4_journal_stop(handle); 3005ae4d5372SAneesh Kumar K.V /* 3006ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3007ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3008ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3009ae4d5372SAneesh Kumar K.V */ 3010ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3011b9a4207dSJan Kara ext4_truncate_failed_write(inode); 301247564bfbSTheodore Ts'o 301347564bfbSTheodore Ts'o if (ret == -ENOSPC && 301447564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 301547564bfbSTheodore Ts'o goto retry_journal; 301647564bfbSTheodore Ts'o 301709cbfeafSKirill A. Shutemov put_page(page); 301847564bfbSTheodore Ts'o return ret; 301964769240SAlex Tomas } 302064769240SAlex Tomas 302147564bfbSTheodore Ts'o *pagep = page; 302264769240SAlex Tomas return ret; 302364769240SAlex Tomas } 302464769240SAlex Tomas 3025632eaeabSMingming Cao /* 3026632eaeabSMingming Cao * Check if we should update i_disksize 3027632eaeabSMingming Cao * when write to the end of file but not require block allocation 3028632eaeabSMingming Cao */ 3029632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3030632eaeabSMingming Cao unsigned long offset) 3031632eaeabSMingming Cao { 3032632eaeabSMingming Cao struct buffer_head *bh; 3033632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3034632eaeabSMingming Cao unsigned int idx; 3035632eaeabSMingming Cao int i; 3036632eaeabSMingming Cao 3037632eaeabSMingming Cao bh = page_buffers(page); 3038632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3039632eaeabSMingming Cao 3040632eaeabSMingming Cao for (i = 0; i < idx; i++) 3041632eaeabSMingming Cao bh = bh->b_this_page; 3042632eaeabSMingming Cao 304329fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3044632eaeabSMingming Cao return 0; 3045632eaeabSMingming Cao return 1; 3046632eaeabSMingming Cao } 3047632eaeabSMingming Cao 304864769240SAlex Tomas static int ext4_da_write_end(struct file *file, 304964769240SAlex Tomas struct address_space *mapping, 305064769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 305164769240SAlex Tomas struct page *page, void *fsdata) 305264769240SAlex Tomas { 305364769240SAlex Tomas struct inode *inode = mapping->host; 305464769240SAlex Tomas int ret = 0, ret2; 305564769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 305664769240SAlex Tomas loff_t new_i_size; 3057632eaeabSMingming Cao unsigned long start, end; 305879f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 305979f0be8dSAneesh Kumar K.V 306074d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 306174d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 306279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3063632eaeabSMingming Cao 30649bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 306509cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 3066632eaeabSMingming Cao end = start + copied - 1; 306764769240SAlex Tomas 306864769240SAlex Tomas /* 306964769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 307064769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 307164769240SAlex Tomas * into that. 307264769240SAlex Tomas */ 307364769240SAlex Tomas new_i_size = pos + copied; 3074ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 30759c3569b5STao Ma if (ext4_has_inline_data(inode) || 30769c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 3077ee124d27SDmitry Monakhov ext4_update_i_disksize(inode, new_i_size); 3078cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3079cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3080cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3081cf17fea6SAneesh Kumar K.V */ 30824209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 3083632eaeabSMingming Cao } 3084632eaeabSMingming Cao } 30859c3569b5STao Ma 30869c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30879c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30889c3569b5STao Ma ext4_has_inline_data(inode)) 30899c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 30909c3569b5STao Ma page); 30919c3569b5STao Ma else 309264769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 309364769240SAlex Tomas page, fsdata); 30949c3569b5STao Ma 309564769240SAlex Tomas copied = ret2; 309664769240SAlex Tomas if (ret2 < 0) 309764769240SAlex Tomas ret = ret2; 309864769240SAlex Tomas ret2 = ext4_journal_stop(handle); 30994209ae12SHarshad Shirwadkar if (unlikely(ret2 && !ret)) 310064769240SAlex Tomas ret = ret2; 310164769240SAlex Tomas 310264769240SAlex Tomas return ret ? ret : copied; 310364769240SAlex Tomas } 310464769240SAlex Tomas 3105ccd2506bSTheodore Ts'o /* 3106ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3107ccd2506bSTheodore Ts'o */ 3108ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3109ccd2506bSTheodore Ts'o { 3110fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3111fb40ba0dSTheodore Ts'o 311271d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3113ccd2506bSTheodore Ts'o return 0; 3114ccd2506bSTheodore Ts'o 3115ccd2506bSTheodore Ts'o /* 3116ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3117ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3118ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3119ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3120ccd2506bSTheodore Ts'o * would require replicating code paths in: 3121ccd2506bSTheodore Ts'o * 312220970ba6STheodore Ts'o * ext4_writepages() -> 3123ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3124ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3125ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3126ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3127ccd2506bSTheodore Ts'o * 3128ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3129ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3130ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3131ccd2506bSTheodore Ts'o * doing I/O at all. 3132ccd2506bSTheodore Ts'o * 3133ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3134380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3135ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3136ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 313725985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3138ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3139ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3140ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3141ccd2506bSTheodore Ts'o * 3142ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3143ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3144ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3145ccd2506bSTheodore Ts'o */ 3146ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3147ccd2506bSTheodore Ts'o } 314864769240SAlex Tomas 314964769240SAlex Tomas /* 3150ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3151ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3152ac27a0ecSDave Kleikamp * 3153ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3154617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3155ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3156ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3157ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3158ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3159ac27a0ecSDave Kleikamp * 3160ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3161ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3162ac27a0ecSDave Kleikamp */ 3163617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3164ac27a0ecSDave Kleikamp { 3165ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3166ac27a0ecSDave Kleikamp journal_t *journal; 3167ac27a0ecSDave Kleikamp int err; 3168ac27a0ecSDave Kleikamp 316946c7f254STao Ma /* 317046c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 317146c7f254STao Ma */ 317246c7f254STao Ma if (ext4_has_inline_data(inode)) 317346c7f254STao Ma return 0; 317446c7f254STao Ma 317564769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 317664769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 317764769240SAlex Tomas /* 317864769240SAlex Tomas * With delalloc we want to sync the file 317964769240SAlex Tomas * so that we can make sure we allocate 318064769240SAlex Tomas * blocks for file 318164769240SAlex Tomas */ 318264769240SAlex Tomas filemap_write_and_wait(mapping); 318364769240SAlex Tomas } 318464769240SAlex Tomas 318519f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 318619f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3187ac27a0ecSDave Kleikamp /* 3188ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3189ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3190ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3191ac27a0ecSDave Kleikamp * do we expect this to happen. 3192ac27a0ecSDave Kleikamp * 3193ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3194ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3195ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3196ac27a0ecSDave Kleikamp * will.) 3197ac27a0ecSDave Kleikamp * 3198617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3199ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3200ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3201ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3202ac27a0ecSDave Kleikamp * everything they get. 3203ac27a0ecSDave Kleikamp */ 3204ac27a0ecSDave Kleikamp 320519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3206617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3207dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3208dab291afSMingming Cao err = jbd2_journal_flush(journal); 3209dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3210ac27a0ecSDave Kleikamp 3211ac27a0ecSDave Kleikamp if (err) 3212ac27a0ecSDave Kleikamp return 0; 3213ac27a0ecSDave Kleikamp } 3214ac27a0ecSDave Kleikamp 3215ac58e4fbSRitesh Harjani return iomap_bmap(mapping, block, &ext4_iomap_ops); 3216ac27a0ecSDave Kleikamp } 3217ac27a0ecSDave Kleikamp 3218617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3219ac27a0ecSDave Kleikamp { 322046c7f254STao Ma int ret = -EAGAIN; 322146c7f254STao Ma struct inode *inode = page->mapping->host; 322246c7f254STao Ma 32230562e0baSJiaying Zhang trace_ext4_readpage(page); 322446c7f254STao Ma 322546c7f254STao Ma if (ext4_has_inline_data(inode)) 322646c7f254STao Ma ret = ext4_readpage_inline(inode, page); 322746c7f254STao Ma 322846c7f254STao Ma if (ret == -EAGAIN) 3229a07f624bSMatthew Wilcox (Oracle) return ext4_mpage_readpages(inode, NULL, page); 323046c7f254STao Ma 323146c7f254STao Ma return ret; 3232ac27a0ecSDave Kleikamp } 3233ac27a0ecSDave Kleikamp 32346311f91fSMatthew Wilcox (Oracle) static void ext4_readahead(struct readahead_control *rac) 3235ac27a0ecSDave Kleikamp { 32366311f91fSMatthew Wilcox (Oracle) struct inode *inode = rac->mapping->host; 323746c7f254STao Ma 32386311f91fSMatthew Wilcox (Oracle) /* If the file has inline data, no need to do readahead. */ 323946c7f254STao Ma if (ext4_has_inline_data(inode)) 32406311f91fSMatthew Wilcox (Oracle) return; 324146c7f254STao Ma 3242a07f624bSMatthew Wilcox (Oracle) ext4_mpage_readpages(inode, rac, NULL); 3243ac27a0ecSDave Kleikamp } 3244ac27a0ecSDave Kleikamp 3245d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3246d47992f8SLukas Czerner unsigned int length) 3247ac27a0ecSDave Kleikamp { 3248ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32490562e0baSJiaying Zhang 32504520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32514520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32524520fb3cSJan Kara 3253ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32544520fb3cSJan Kara } 32554520fb3cSJan Kara 325653e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3257ca99fdd2SLukas Czerner unsigned int offset, 3258ca99fdd2SLukas Czerner unsigned int length) 32594520fb3cSJan Kara { 32604520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32614520fb3cSJan Kara 3262ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32634520fb3cSJan Kara 3264744692dcSJiaying Zhang /* 3265ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3266ac27a0ecSDave Kleikamp */ 326709cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3268ac27a0ecSDave Kleikamp ClearPageChecked(page); 3269ac27a0ecSDave Kleikamp 3270ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 327153e87268SJan Kara } 327253e87268SJan Kara 327353e87268SJan Kara /* Wrapper for aops... */ 327453e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3275d47992f8SLukas Czerner unsigned int offset, 3276d47992f8SLukas Czerner unsigned int length) 327753e87268SJan Kara { 3278ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3279ac27a0ecSDave Kleikamp } 3280ac27a0ecSDave Kleikamp 3281617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3282ac27a0ecSDave Kleikamp { 3283617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3284ac27a0ecSDave Kleikamp 32850562e0baSJiaying Zhang trace_ext4_releasepage(page); 32860562e0baSJiaying Zhang 3287e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3288e1c36595SJan Kara if (PageChecked(page)) 3289ac27a0ecSDave Kleikamp return 0; 32900390131bSFrank Mayhar if (journal) 3291529a781eSzhangyi (F) return jbd2_journal_try_to_free_buffers(journal, page); 32920390131bSFrank Mayhar else 32930390131bSFrank Mayhar return try_to_free_buffers(page); 3294ac27a0ecSDave Kleikamp } 3295ac27a0ecSDave Kleikamp 3296b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3297b8a6176cSJan Kara { 3298b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3299b8a6176cSJan Kara 3300b8a6176cSJan Kara if (journal) 3301b8a6176cSJan Kara return !jbd2_transaction_committed(journal, 3302b8a6176cSJan Kara EXT4_I(inode)->i_datasync_tid); 3303b8a6176cSJan Kara /* Any metadata buffers to write? */ 3304b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3305b8a6176cSJan Kara return true; 3306b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3307b8a6176cSJan Kara } 3308b8a6176cSJan Kara 3309c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3310c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3311c8fdfe29SMatthew Bobrowski loff_t length) 3312364443cbSJan Kara { 3313c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3314c8fdfe29SMatthew Bobrowski 3315c8fdfe29SMatthew Bobrowski /* 3316c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3317c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3318c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3319c8fdfe29SMatthew Bobrowski */ 3320c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3321c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3322c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3323c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3324c8fdfe29SMatthew Bobrowski 3325c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3326c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3327c8fdfe29SMatthew Bobrowski 3328c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3329c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3330c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3331c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3332c8fdfe29SMatthew Bobrowski 33336386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 33346386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 33356386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 33366386722aSRitesh Harjani 3337c8fdfe29SMatthew Bobrowski /* 3338c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3339c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3340c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3341c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3342c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3343c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3344c8fdfe29SMatthew Bobrowski * been set first. 3345c8fdfe29SMatthew Bobrowski */ 3346c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3347c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3348c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3349c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3350c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3351c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3352c8fdfe29SMatthew Bobrowski } else { 3353c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3354c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3355c8fdfe29SMatthew Bobrowski } 3356c8fdfe29SMatthew Bobrowski } 3357c8fdfe29SMatthew Bobrowski 3358f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3359f063db5eSMatthew Bobrowski unsigned int flags) 3360f063db5eSMatthew Bobrowski { 3361f063db5eSMatthew Bobrowski handle_t *handle; 3362378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3363378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3364f063db5eSMatthew Bobrowski 3365f063db5eSMatthew Bobrowski /* 3366f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3367f063db5eSMatthew Bobrowski * once for direct I/O. 3368f063db5eSMatthew Bobrowski */ 3369f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3370f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3371f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3372f063db5eSMatthew Bobrowski 3373f063db5eSMatthew Bobrowski retry: 3374f063db5eSMatthew Bobrowski /* 3375f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3376f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3377f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3378f063db5eSMatthew Bobrowski * fits into the credits as well. 3379f063db5eSMatthew Bobrowski */ 3380f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3381f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3382f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3383f063db5eSMatthew Bobrowski 3384378f32baSMatthew Bobrowski /* 3385378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3386378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3387378f32baSMatthew Bobrowski */ 3388378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3389378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3390378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3391378f32baSMatthew Bobrowski /* 3392378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3393378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3394378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3395378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3396378f32baSMatthew Bobrowski */ 3397378f32baSMatthew Bobrowski else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode)) 3398378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3399378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3400378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3401378f32baSMatthew Bobrowski 3402378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3403378f32baSMatthew Bobrowski 3404378f32baSMatthew Bobrowski /* 3405378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3406378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3407378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3408378f32baSMatthew Bobrowski */ 3409378f32baSMatthew Bobrowski if (!m_flags && !ret) 3410378f32baSMatthew Bobrowski ret = -ENOTBLK; 3411f063db5eSMatthew Bobrowski 3412f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3413f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3414f063db5eSMatthew Bobrowski goto retry; 3415f063db5eSMatthew Bobrowski 3416f063db5eSMatthew Bobrowski return ret; 3417f063db5eSMatthew Bobrowski } 3418f063db5eSMatthew Bobrowski 3419f063db5eSMatthew Bobrowski 3420364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3421c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3422364443cbSJan Kara { 3423364443cbSJan Kara int ret; 342409edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 342509edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3426364443cbSJan Kara 3427bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3428bcd8e91fSTheodore Ts'o return -EINVAL; 34297046ae35SAndreas Gruenbacher 3430364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3431364443cbSJan Kara return -ERANGE; 3432364443cbSJan Kara 343309edf4d3SMatthew Bobrowski /* 343409edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 343509edf4d3SMatthew Bobrowski */ 343609edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 343709edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 343809edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3439364443cbSJan Kara 34409faac62dSRitesh Harjani if (flags & IOMAP_WRITE) { 34419faac62dSRitesh Harjani /* 34429faac62dSRitesh Harjani * We check here if the blocks are already allocated, then we 34439faac62dSRitesh Harjani * don't need to start a journal txn and we can directly return 34449faac62dSRitesh Harjani * the mapping information. This could boost performance 34459faac62dSRitesh Harjani * especially in multi-threaded overwrite requests. 34469faac62dSRitesh Harjani */ 34479faac62dSRitesh Harjani if (offset + length <= i_size_read(inode)) { 3448545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 34499faac62dSRitesh Harjani if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED)) 34509faac62dSRitesh Harjani goto out; 34519faac62dSRitesh Harjani } 34529faac62dSRitesh Harjani ret = ext4_iomap_alloc(inode, &map, flags); 34539faac62dSRitesh Harjani } else { 34549faac62dSRitesh Harjani ret = ext4_map_blocks(NULL, inode, &map, 0); 34559faac62dSRitesh Harjani } 3456f063db5eSMatthew Bobrowski 3457545052e9SChristoph Hellwig if (ret < 0) 3458545052e9SChristoph Hellwig return ret; 34599faac62dSRitesh Harjani out: 3460c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3461545052e9SChristoph Hellwig 3462364443cbSJan Kara return 0; 3463364443cbSJan Kara } 3464364443cbSJan Kara 34658cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34668cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34678cd115bdSJan Kara struct iomap *srcmap) 34688cd115bdSJan Kara { 34698cd115bdSJan Kara int ret; 34708cd115bdSJan Kara 34718cd115bdSJan Kara /* 34728cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34738cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34748cd115bdSJan Kara */ 34758cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34768cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34778cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34788cd115bdSJan Kara return ret; 34798cd115bdSJan Kara } 34808cd115bdSJan Kara 3481776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3482776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3483776722e8SJan Kara { 3484378f32baSMatthew Bobrowski /* 3485378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3486378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3487378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3488378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3489378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3490378f32baSMatthew Bobrowski */ 3491378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3492378f32baSMatthew Bobrowski return -ENOTBLK; 3493378f32baSMatthew Bobrowski 3494776722e8SJan Kara return 0; 3495776722e8SJan Kara } 3496776722e8SJan Kara 34978ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3498364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3499776722e8SJan Kara .iomap_end = ext4_iomap_end, 3500364443cbSJan Kara }; 3501364443cbSJan Kara 35028cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 35038cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 35048cd115bdSJan Kara .iomap_end = ext4_iomap_end, 35058cd115bdSJan Kara }; 35068cd115bdSJan Kara 350709edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 350809edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 350909edf4d3SMatthew Bobrowski { 351009edf4d3SMatthew Bobrowski struct extent_status es; 351109edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 351209edf4d3SMatthew Bobrowski 351309edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 351409edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 351509edf4d3SMatthew Bobrowski 351609edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 351709edf4d3SMatthew Bobrowski return false; 351809edf4d3SMatthew Bobrowski 351909edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 352009edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 352109edf4d3SMatthew Bobrowski return false; 352209edf4d3SMatthew Bobrowski } 352309edf4d3SMatthew Bobrowski 352409edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 352509edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 352609edf4d3SMatthew Bobrowski 352709edf4d3SMatthew Bobrowski return true; 352809edf4d3SMatthew Bobrowski } 352909edf4d3SMatthew Bobrowski 353009edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 353109edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 353209edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 353309edf4d3SMatthew Bobrowski { 353409edf4d3SMatthew Bobrowski int ret; 353509edf4d3SMatthew Bobrowski bool delalloc = false; 353609edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 353709edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 353809edf4d3SMatthew Bobrowski 353909edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 354009edf4d3SMatthew Bobrowski return -EINVAL; 354109edf4d3SMatthew Bobrowski 35427cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35437cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3544ba5843f5SJan Kara if (ret != -EAGAIN) { 3545ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3546ba5843f5SJan Kara ret = -ENOENT; 3547ba5843f5SJan Kara return ret; 3548ba5843f5SJan Kara } 3549ba5843f5SJan Kara } 355012735f88SJan Kara 355109edf4d3SMatthew Bobrowski /* 355209edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 355309edf4d3SMatthew Bobrowski */ 355409edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 355509edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 355609edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 355712735f88SJan Kara 3558b2c57642SRitesh Harjani /* 3559b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3560b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3561b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3562b2c57642SRitesh Harjani * -EIO error. 3563b2c57642SRitesh Harjani */ 3564b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3565b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3566b2c57642SRitesh Harjani 3567b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3568b2c57642SRitesh Harjani map.m_flags = 0; 3569b2c57642SRitesh Harjani goto set_iomap; 3570b2c57642SRitesh Harjani } 3571b2c57642SRitesh Harjani } 3572b2c57642SRitesh Harjani 357312735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3574ba5843f5SJan Kara if (ret < 0) 3575ba5843f5SJan Kara return ret; 3576914f82a3SJan Kara if (ret == 0) 357709edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 357809edf4d3SMatthew Bobrowski 3579b2c57642SRitesh Harjani set_iomap: 358009edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 358109edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 358209edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 358309edf4d3SMatthew Bobrowski 358409edf4d3SMatthew Bobrowski return 0; 3585914f82a3SJan Kara } 3586914f82a3SJan Kara 358709edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 358809edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 358909edf4d3SMatthew Bobrowski }; 35904c0425ffSMingming Cao 3591ac27a0ecSDave Kleikamp /* 3592617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3593ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3594ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3595ac27a0ecSDave Kleikamp * not necessarily locked. 3596ac27a0ecSDave Kleikamp * 3597ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3598ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3599ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3600ac27a0ecSDave Kleikamp * 3601ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3602ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3603ac27a0ecSDave Kleikamp */ 3604617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3605ac27a0ecSDave Kleikamp { 3606ac27a0ecSDave Kleikamp SetPageChecked(page); 3607ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3608ac27a0ecSDave Kleikamp } 3609ac27a0ecSDave Kleikamp 36106dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 36116dcc693bSJan Kara { 36126dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 36136dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 36146dcc693bSJan Kara return __set_page_dirty_buffers(page); 36156dcc693bSJan Kara } 36166dcc693bSJan Kara 36170e6895baSRitesh Harjani static int ext4_iomap_swap_activate(struct swap_info_struct *sis, 36180e6895baSRitesh Harjani struct file *file, sector_t *span) 36190e6895baSRitesh Harjani { 36200e6895baSRitesh Harjani return iomap_swapfile_activate(sis, file, span, 36210e6895baSRitesh Harjani &ext4_iomap_report_ops); 36220e6895baSRitesh Harjani } 36230e6895baSRitesh Harjani 362474d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3625617ba13bSMingming Cao .readpage = ext4_readpage, 36266311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 362743ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 362820970ba6STheodore Ts'o .writepages = ext4_writepages, 3629bfc1af65SNick Piggin .write_begin = ext4_write_begin, 363074d553aaSTheodore Ts'o .write_end = ext4_write_end, 36316dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3632617ba13bSMingming Cao .bmap = ext4_bmap, 3633617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3634617ba13bSMingming Cao .releasepage = ext4_releasepage, 3635378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3636ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 36378ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3638aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36390e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3640ac27a0ecSDave Kleikamp }; 3641ac27a0ecSDave Kleikamp 3642617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3643617ba13bSMingming Cao .readpage = ext4_readpage, 36446311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 364543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 364620970ba6STheodore Ts'o .writepages = ext4_writepages, 3647bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3648bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3649617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3650617ba13bSMingming Cao .bmap = ext4_bmap, 36514520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3652617ba13bSMingming Cao .releasepage = ext4_releasepage, 3653378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 36548ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3655aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36560e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 3657ac27a0ecSDave Kleikamp }; 3658ac27a0ecSDave Kleikamp 365964769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 366064769240SAlex Tomas .readpage = ext4_readpage, 36616311f91fSMatthew Wilcox (Oracle) .readahead = ext4_readahead, 366243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 366320970ba6STheodore Ts'o .writepages = ext4_writepages, 366464769240SAlex Tomas .write_begin = ext4_da_write_begin, 366564769240SAlex Tomas .write_end = ext4_da_write_end, 36666dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 366764769240SAlex Tomas .bmap = ext4_bmap, 36688fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 366964769240SAlex Tomas .releasepage = ext4_releasepage, 3670378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 367164769240SAlex Tomas .migratepage = buffer_migrate_page, 36728ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3673aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36740e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 367564769240SAlex Tomas }; 367664769240SAlex Tomas 36775f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36785f0663bbSDan Williams .writepages = ext4_dax_writepages, 36795f0663bbSDan Williams .direct_IO = noop_direct_IO, 36805f0663bbSDan Williams .set_page_dirty = noop_set_page_dirty, 368194dbb631SToshi Kani .bmap = ext4_bmap, 36825f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36830e6895baSRitesh Harjani .swap_activate = ext4_iomap_swap_activate, 36845f0663bbSDan Williams }; 36855f0663bbSDan Williams 3686617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3687ac27a0ecSDave Kleikamp { 36883d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36893d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36903d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36913d2b1582SLukas Czerner break; 36923d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3693617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 369474d553aaSTheodore Ts'o return; 36953d2b1582SLukas Czerner default: 36963d2b1582SLukas Czerner BUG(); 36973d2b1582SLukas Czerner } 36985f0663bbSDan Williams if (IS_DAX(inode)) 36995f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 37005f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 370174d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 370274d553aaSTheodore Ts'o else 370374d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3704ac27a0ecSDave Kleikamp } 3705ac27a0ecSDave Kleikamp 3706923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3707d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3708d863dc36SLukas Czerner { 370909cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 371009cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3711923ae0ffSRoss Zwisler unsigned blocksize, pos; 3712d863dc36SLukas Czerner ext4_lblk_t iblock; 3713d863dc36SLukas Czerner struct inode *inode = mapping->host; 3714d863dc36SLukas Czerner struct buffer_head *bh; 3715d863dc36SLukas Czerner struct page *page; 3716d863dc36SLukas Czerner int err = 0; 3717d863dc36SLukas Czerner 371809cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3719c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3720d863dc36SLukas Czerner if (!page) 3721d863dc36SLukas Czerner return -ENOMEM; 3722d863dc36SLukas Czerner 3723d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3724d863dc36SLukas Czerner 372509cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3726d863dc36SLukas Czerner 3727d863dc36SLukas Czerner if (!page_has_buffers(page)) 3728d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3729d863dc36SLukas Czerner 3730d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3731d863dc36SLukas Czerner bh = page_buffers(page); 3732d863dc36SLukas Czerner pos = blocksize; 3733d863dc36SLukas Czerner while (offset >= pos) { 3734d863dc36SLukas Czerner bh = bh->b_this_page; 3735d863dc36SLukas Czerner iblock++; 3736d863dc36SLukas Czerner pos += blocksize; 3737d863dc36SLukas Czerner } 3738d863dc36SLukas Czerner if (buffer_freed(bh)) { 3739d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3740d863dc36SLukas Czerner goto unlock; 3741d863dc36SLukas Czerner } 3742d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3743d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3744d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3745d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3746d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3747d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3748d863dc36SLukas Czerner goto unlock; 3749d863dc36SLukas Czerner } 3750d863dc36SLukas Czerner } 3751d863dc36SLukas Czerner 3752d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3753d863dc36SLukas Czerner if (PageUptodate(page)) 3754d863dc36SLukas Czerner set_buffer_uptodate(bh); 3755d863dc36SLukas Czerner 3756d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 3757*2d069c08Szhangyi (F) err = ext4_read_bh_lock(bh, 0, true); 3758*2d069c08Szhangyi (F) if (err) 3759d863dc36SLukas Czerner goto unlock; 37604f74d15fSEric Biggers if (fscrypt_inode_uses_fs_layer_crypto(inode)) { 3761c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3762a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3763834f1565SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3764834f1565SEric Biggers bh_offset(bh)); 3765834f1565SEric Biggers if (err) { 3766834f1565SEric Biggers clear_buffer_uptodate(bh); 3767834f1565SEric Biggers goto unlock; 3768834f1565SEric Biggers } 3769c9c7429cSMichael Halcrow } 3770d863dc36SLukas Czerner } 3771d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3772d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3773d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3774d863dc36SLukas Czerner if (err) 3775d863dc36SLukas Czerner goto unlock; 3776d863dc36SLukas Czerner } 3777d863dc36SLukas Czerner zero_user(page, offset, length); 3778d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3779d863dc36SLukas Czerner 3780d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3781d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37820713ed0cSLukas Czerner } else { 3783353eefd3Sjon ernst err = 0; 3784d863dc36SLukas Czerner mark_buffer_dirty(bh); 37853957ef53SJan Kara if (ext4_should_order_data(inode)) 378673131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 378773131fbbSRoss Zwisler length); 37880713ed0cSLukas Czerner } 3789d863dc36SLukas Czerner 3790d863dc36SLukas Czerner unlock: 3791d863dc36SLukas Czerner unlock_page(page); 379209cbfeafSKirill A. Shutemov put_page(page); 3793d863dc36SLukas Czerner return err; 3794d863dc36SLukas Czerner } 3795d863dc36SLukas Czerner 379694350ab5SMatthew Wilcox /* 3797923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3798923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3799923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3800923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 3801923ae0ffSRoss Zwisler * that cooresponds to 'from' 3802923ae0ffSRoss Zwisler */ 3803923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3804923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3805923ae0ffSRoss Zwisler { 3806923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 380709cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3808923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3809923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3810923ae0ffSRoss Zwisler 3811923ae0ffSRoss Zwisler /* 3812923ae0ffSRoss Zwisler * correct length if it does not fall between 3813923ae0ffSRoss Zwisler * 'from' and the end of the block 3814923ae0ffSRoss Zwisler */ 3815923ae0ffSRoss Zwisler if (length > max || length < 0) 3816923ae0ffSRoss Zwisler length = max; 3817923ae0ffSRoss Zwisler 381847e69351SJan Kara if (IS_DAX(inode)) { 381947e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 382047e69351SJan Kara &ext4_iomap_ops); 382147e69351SJan Kara } 3822923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3823923ae0ffSRoss Zwisler } 3824923ae0ffSRoss Zwisler 3825923ae0ffSRoss Zwisler /* 382694350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 382794350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 382894350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 382994350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 383094350ab5SMatthew Wilcox */ 3831c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 383294350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 383394350ab5SMatthew Wilcox { 383409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 383594350ab5SMatthew Wilcox unsigned length; 383694350ab5SMatthew Wilcox unsigned blocksize; 383794350ab5SMatthew Wilcox struct inode *inode = mapping->host; 383894350ab5SMatthew Wilcox 38390d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3840592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 38410d06863fSTheodore Ts'o return 0; 38420d06863fSTheodore Ts'o 384394350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 384494350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 384594350ab5SMatthew Wilcox 384694350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 384794350ab5SMatthew Wilcox } 384894350ab5SMatthew Wilcox 3849a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3850a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3851a87dd18cSLukas Czerner { 3852a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3853a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3854e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3855a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3856a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3857a87dd18cSLukas Czerner int err = 0; 3858a87dd18cSLukas Czerner 3859e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3860e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3861e1be3a92SLukas Czerner 3862a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3863a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3864a87dd18cSLukas Czerner 3865a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3866e1be3a92SLukas Czerner if (start == end && 3867e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3868a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3869a87dd18cSLukas Czerner lstart, length); 3870a87dd18cSLukas Czerner return err; 3871a87dd18cSLukas Czerner } 3872a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3873e1be3a92SLukas Czerner if (partial_start) { 3874a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3875a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3876a87dd18cSLukas Czerner if (err) 3877a87dd18cSLukas Czerner return err; 3878a87dd18cSLukas Czerner } 3879a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3880e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3881a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3882e1be3a92SLukas Czerner byte_end - partial_end, 3883e1be3a92SLukas Czerner partial_end + 1); 3884a87dd18cSLukas Czerner return err; 3885a87dd18cSLukas Czerner } 3886a87dd18cSLukas Czerner 388791ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 388891ef4cafSDuane Griffin { 388991ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 389091ef4cafSDuane Griffin return 1; 389191ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 389291ef4cafSDuane Griffin return 1; 389391ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 389491ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 389591ef4cafSDuane Griffin return 0; 389691ef4cafSDuane Griffin } 389791ef4cafSDuane Griffin 3898ac27a0ecSDave Kleikamp /* 389901127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 390001127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 390101127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 390201127848SJan Kara * that will never happen after we truncate page cache. 390301127848SJan Kara */ 390401127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 390501127848SJan Kara loff_t len) 390601127848SJan Kara { 390701127848SJan Kara handle_t *handle; 39084209ae12SHarshad Shirwadkar int ret; 39094209ae12SHarshad Shirwadkar 391001127848SJan Kara loff_t size = i_size_read(inode); 391101127848SJan Kara 39125955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 391301127848SJan Kara if (offset > size || offset + len < size) 391401127848SJan Kara return 0; 391501127848SJan Kara 391601127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 391701127848SJan Kara return 0; 391801127848SJan Kara 391901127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 392001127848SJan Kara if (IS_ERR(handle)) 392101127848SJan Kara return PTR_ERR(handle); 392201127848SJan Kara ext4_update_i_disksize(inode, size); 39234209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 392401127848SJan Kara ext4_journal_stop(handle); 392501127848SJan Kara 39264209ae12SHarshad Shirwadkar return ret; 392701127848SJan Kara } 392801127848SJan Kara 3929b1f38217SRoss Zwisler static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3930430657b6SRoss Zwisler { 3931430657b6SRoss Zwisler up_write(&ei->i_mmap_sem); 3932430657b6SRoss Zwisler schedule(); 3933430657b6SRoss Zwisler down_write(&ei->i_mmap_sem); 3934430657b6SRoss Zwisler } 3935430657b6SRoss Zwisler 3936430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3937430657b6SRoss Zwisler { 3938430657b6SRoss Zwisler struct ext4_inode_info *ei = EXT4_I(inode); 3939430657b6SRoss Zwisler struct page *page; 3940430657b6SRoss Zwisler int error; 3941430657b6SRoss Zwisler 3942430657b6SRoss Zwisler if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3943430657b6SRoss Zwisler return -EINVAL; 3944430657b6SRoss Zwisler 3945430657b6SRoss Zwisler do { 3946430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3947430657b6SRoss Zwisler if (!page) 3948430657b6SRoss Zwisler return 0; 3949430657b6SRoss Zwisler 3950430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3951430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3952430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3953b1f38217SRoss Zwisler ext4_wait_dax_page(ei)); 3954b1f38217SRoss Zwisler } while (error == 0); 3955430657b6SRoss Zwisler 3956430657b6SRoss Zwisler return error; 3957430657b6SRoss Zwisler } 3958430657b6SRoss Zwisler 395901127848SJan Kara /* 3960cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3961a4bb6b64SAllison Henderson * associated with the given offset and length 3962a4bb6b64SAllison Henderson * 3963a4bb6b64SAllison Henderson * @inode: File inode 3964a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3965a4bb6b64SAllison Henderson * @len: The length of the hole 3966a4bb6b64SAllison Henderson * 39674907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3968a4bb6b64SAllison Henderson */ 3969a4bb6b64SAllison Henderson 3970aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3971a4bb6b64SAllison Henderson { 397226a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 397326a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 397426a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3975a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 397626a4c0c6STheodore Ts'o handle_t *handle; 397726a4c0c6STheodore Ts'o unsigned int credits; 39784209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 397926a4c0c6STheodore Ts'o 3980b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3981aaddea81SZheng Liu 3982c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3983c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3984c1e8220bSTheodore Ts'o down_write(&EXT4_I(inode)->i_mmap_sem); 3985c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3986c1e8220bSTheodore Ts'o up_write(&EXT4_I(inode)->i_mmap_sem); 3987c1e8220bSTheodore Ts'o if (ret) 3988c1e8220bSTheodore Ts'o return ret; 3989c1e8220bSTheodore Ts'o } 3990c1e8220bSTheodore Ts'o 399126a4c0c6STheodore Ts'o /* 399226a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 399326a4c0c6STheodore Ts'o * Then release them. 399426a4c0c6STheodore Ts'o */ 3995cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 399626a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 399726a4c0c6STheodore Ts'o offset + length - 1); 399826a4c0c6STheodore Ts'o if (ret) 399926a4c0c6STheodore Ts'o return ret; 400026a4c0c6STheodore Ts'o } 400126a4c0c6STheodore Ts'o 40025955102cSAl Viro inode_lock(inode); 40039ef06cecSLukas Czerner 400426a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 400526a4c0c6STheodore Ts'o if (offset >= inode->i_size) 400626a4c0c6STheodore Ts'o goto out_mutex; 400726a4c0c6STheodore Ts'o 400826a4c0c6STheodore Ts'o /* 400926a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 401026a4c0c6STheodore Ts'o * to end after the page that contains i_size 401126a4c0c6STheodore Ts'o */ 401226a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 401326a4c0c6STheodore Ts'o length = inode->i_size + 401409cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 401526a4c0c6STheodore Ts'o offset; 401626a4c0c6STheodore Ts'o } 401726a4c0c6STheodore Ts'o 4018a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 4019a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 4020a361293fSJan Kara /* 4021a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 4022a361293fSJan Kara * partial block 4023a361293fSJan Kara */ 4024a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 4025a361293fSJan Kara if (ret < 0) 4026a361293fSJan Kara goto out_mutex; 4027a361293fSJan Kara 4028a361293fSJan Kara } 4029a361293fSJan Kara 4030ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 4031ea3d7209SJan Kara inode_dio_wait(inode); 4032ea3d7209SJan Kara 4033ea3d7209SJan Kara /* 4034ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 4035ea3d7209SJan Kara * page cache. 4036ea3d7209SJan Kara */ 4037ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 4038430657b6SRoss Zwisler 4039430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4040430657b6SRoss Zwisler if (ret) 4041430657b6SRoss Zwisler goto out_dio; 4042430657b6SRoss Zwisler 4043a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4044a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 404526a4c0c6STheodore Ts'o 4046a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 404701127848SJan Kara if (last_block_offset > first_block_offset) { 404801127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 404901127848SJan Kara if (ret) 405001127848SJan Kara goto out_dio; 4051a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4052a87dd18cSLukas Czerner last_block_offset); 405301127848SJan Kara } 405426a4c0c6STheodore Ts'o 405526a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 405626a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 405726a4c0c6STheodore Ts'o else 405826a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 405926a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 406026a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 406126a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 406226a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 406326a4c0c6STheodore Ts'o goto out_dio; 406426a4c0c6STheodore Ts'o } 406526a4c0c6STheodore Ts'o 4066a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4067a87dd18cSLukas Czerner length); 406826a4c0c6STheodore Ts'o if (ret) 406926a4c0c6STheodore Ts'o goto out_stop; 407026a4c0c6STheodore Ts'o 407126a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 407226a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 407326a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 407426a4c0c6STheodore Ts'o 4075eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4076eee597acSLukas Czerner if (stop_block > first_block) { 407726a4c0c6STheodore Ts'o 407826a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 407927bc446eSbrookxu ext4_discard_preallocations(inode, 0); 408026a4c0c6STheodore Ts'o 408126a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 408226a4c0c6STheodore Ts'o stop_block - first_block); 408326a4c0c6STheodore Ts'o if (ret) { 408426a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 408526a4c0c6STheodore Ts'o goto out_stop; 408626a4c0c6STheodore Ts'o } 408726a4c0c6STheodore Ts'o 408826a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 408926a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 409026a4c0c6STheodore Ts'o stop_block - 1); 409126a4c0c6STheodore Ts'o else 40924f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 409326a4c0c6STheodore Ts'o stop_block); 409426a4c0c6STheodore Ts'o 4095819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4096eee597acSLukas Czerner } 409726a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 409826a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4099e251f9bcSMaxim Patlasov 4100eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 41014209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 41024209ae12SHarshad Shirwadkar if (unlikely(ret2)) 41034209ae12SHarshad Shirwadkar ret = ret2; 410467a7d5f5SJan Kara if (ret >= 0) 410567a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 410626a4c0c6STheodore Ts'o out_stop: 410726a4c0c6STheodore Ts'o ext4_journal_stop(handle); 410826a4c0c6STheodore Ts'o out_dio: 4109ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 411026a4c0c6STheodore Ts'o out_mutex: 41115955102cSAl Viro inode_unlock(inode); 411226a4c0c6STheodore Ts'o return ret; 4113a4bb6b64SAllison Henderson } 4114a4bb6b64SAllison Henderson 4115a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4116a361293fSJan Kara { 4117a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4118a361293fSJan Kara struct jbd2_inode *jinode; 4119a361293fSJan Kara 4120a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4121a361293fSJan Kara return 0; 4122a361293fSJan Kara 4123a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4124a361293fSJan Kara spin_lock(&inode->i_lock); 4125a361293fSJan Kara if (!ei->jinode) { 4126a361293fSJan Kara if (!jinode) { 4127a361293fSJan Kara spin_unlock(&inode->i_lock); 4128a361293fSJan Kara return -ENOMEM; 4129a361293fSJan Kara } 4130a361293fSJan Kara ei->jinode = jinode; 4131a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4132a361293fSJan Kara jinode = NULL; 4133a361293fSJan Kara } 4134a361293fSJan Kara spin_unlock(&inode->i_lock); 4135a361293fSJan Kara if (unlikely(jinode != NULL)) 4136a361293fSJan Kara jbd2_free_inode(jinode); 4137a361293fSJan Kara return 0; 4138a361293fSJan Kara } 4139a361293fSJan Kara 4140a4bb6b64SAllison Henderson /* 4141617ba13bSMingming Cao * ext4_truncate() 4142ac27a0ecSDave Kleikamp * 4143617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4144617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4145ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4146ac27a0ecSDave Kleikamp * 414742b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4148ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4149ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4150ac27a0ecSDave Kleikamp * 4151ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4152ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4153ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4154ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4155ac27a0ecSDave Kleikamp * left-to-right works OK too). 4156ac27a0ecSDave Kleikamp * 4157ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4158ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4159ac27a0ecSDave Kleikamp * 4160ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4161617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4162ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4163617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4164617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4165ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4166617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4167ac27a0ecSDave Kleikamp */ 41682c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4169ac27a0ecSDave Kleikamp { 4170819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4171819c4920STheodore Ts'o unsigned int credits; 41724209ae12SHarshad Shirwadkar int err = 0, err2; 4173819c4920STheodore Ts'o handle_t *handle; 4174819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4175819c4920STheodore Ts'o 417619b5ef61STheodore Ts'o /* 417719b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4178e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 417919b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 418019b5ef61STheodore Ts'o */ 418119b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41825955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41830562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41840562e0baSJiaying Zhang 418591ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41869a5d265fSzhengliang goto out_trace; 4187ac27a0ecSDave Kleikamp 41885534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 418919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41907d8f9f7dSTheodore Ts'o 4191aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4192aef1c851STao Ma int has_inline = 1; 4193aef1c851STao Ma 419401daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 41959a5d265fSzhengliang if (err || has_inline) 41969a5d265fSzhengliang goto out_trace; 4197aef1c851STao Ma } 4198aef1c851STao Ma 4199a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4200a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4201a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 42029a5d265fSzhengliang goto out_trace; 4203a361293fSJan Kara } 4204a361293fSJan Kara 4205ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4206819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4207ff9893dcSAmir Goldstein else 4208819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4209819c4920STheodore Ts'o 4210819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 42119a5d265fSzhengliang if (IS_ERR(handle)) { 42129a5d265fSzhengliang err = PTR_ERR(handle); 42139a5d265fSzhengliang goto out_trace; 42149a5d265fSzhengliang } 4215819c4920STheodore Ts'o 4216eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4217eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4218819c4920STheodore Ts'o 4219819c4920STheodore Ts'o /* 4220819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4221819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4222819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4223819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4224819c4920STheodore Ts'o * 4225819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4226819c4920STheodore Ts'o * truncatable state while each transaction commits. 4227819c4920STheodore Ts'o */ 42282c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 42292c98eb5eSTheodore Ts'o if (err) 4230819c4920STheodore Ts'o goto out_stop; 4231819c4920STheodore Ts'o 4232819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4233819c4920STheodore Ts'o 423427bc446eSbrookxu ext4_discard_preallocations(inode, 0); 4235819c4920STheodore Ts'o 4236819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4237d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4238819c4920STheodore Ts'o else 4239819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4240819c4920STheodore Ts'o 4241819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4242d0abb36dSTheodore Ts'o if (err) 4243d0abb36dSTheodore Ts'o goto out_stop; 4244819c4920STheodore Ts'o 4245819c4920STheodore Ts'o if (IS_SYNC(inode)) 4246819c4920STheodore Ts'o ext4_handle_sync(handle); 4247819c4920STheodore Ts'o 4248819c4920STheodore Ts'o out_stop: 4249819c4920STheodore Ts'o /* 4250819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4251819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4252819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 425358d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4254819c4920STheodore Ts'o * orphan info for us. 4255819c4920STheodore Ts'o */ 4256819c4920STheodore Ts'o if (inode->i_nlink) 4257819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4258819c4920STheodore Ts'o 4259eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 42604209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 42614209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 42624209ae12SHarshad Shirwadkar err = err2; 4263819c4920STheodore Ts'o ext4_journal_stop(handle); 4264a86c6181SAlex Tomas 42659a5d265fSzhengliang out_trace: 42660562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42672c98eb5eSTheodore Ts'o return err; 4268ac27a0ecSDave Kleikamp } 4269ac27a0ecSDave Kleikamp 4270ac27a0ecSDave Kleikamp /* 4271617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4272ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4273ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4274ac27a0ecSDave Kleikamp * inode. 4275ac27a0ecSDave Kleikamp */ 4276617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4277617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4278ac27a0ecSDave Kleikamp { 4279240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4280ac27a0ecSDave Kleikamp struct buffer_head *bh; 4281240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4282240799cdSTheodore Ts'o ext4_fsblk_t block; 428302f03c42SLinus Torvalds struct blk_plug plug; 4284240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4285ac27a0ecSDave Kleikamp 42863a06d778SAneesh Kumar K.V iloc->bh = NULL; 4287c37e9e01STheodore Ts'o if (inode->i_ino < EXT4_ROOT_INO || 4288c37e9e01STheodore Ts'o inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 42896a797d27SDarrick J. Wong return -EFSCORRUPTED; 4290ac27a0ecSDave Kleikamp 4291240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4292240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4293240799cdSTheodore Ts'o if (!gdp) 4294240799cdSTheodore Ts'o return -EIO; 4295240799cdSTheodore Ts'o 4296240799cdSTheodore Ts'o /* 4297240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4298240799cdSTheodore Ts'o */ 429900d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4300240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4301240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4302240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4303240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4304240799cdSTheodore Ts'o 4305240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4306aebf0243SWang Shilong if (unlikely(!bh)) 4307860d21e2STheodore Ts'o return -ENOMEM; 430846f870d6STheodore Ts'o if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO)) 430946f870d6STheodore Ts'o goto simulate_eio; 4310ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4311ac27a0ecSDave Kleikamp lock_buffer(bh); 43129c83a923SHidehiro Kawai 43139c83a923SHidehiro Kawai /* 43149c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 43159c83a923SHidehiro Kawai * to write out another inode in the same block. In this 43169c83a923SHidehiro Kawai * case, we don't have to read the block because we may 43179c83a923SHidehiro Kawai * read the old inode data successfully. 43189c83a923SHidehiro Kawai */ 43199c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 43209c83a923SHidehiro Kawai set_buffer_uptodate(bh); 43219c83a923SHidehiro Kawai 4322ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4323ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4324ac27a0ecSDave Kleikamp unlock_buffer(bh); 4325ac27a0ecSDave Kleikamp goto has_buffer; 4326ac27a0ecSDave Kleikamp } 4327ac27a0ecSDave Kleikamp 4328ac27a0ecSDave Kleikamp /* 4329ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4330ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4331ac27a0ecSDave Kleikamp * block. 4332ac27a0ecSDave Kleikamp */ 4333ac27a0ecSDave Kleikamp if (in_mem) { 4334ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4335240799cdSTheodore Ts'o int i, start; 4336ac27a0ecSDave Kleikamp 4337240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4338ac27a0ecSDave Kleikamp 4339ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4340240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4341aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4342ac27a0ecSDave Kleikamp goto make_io; 4343ac27a0ecSDave Kleikamp 4344ac27a0ecSDave Kleikamp /* 4345ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4346ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4347ac27a0ecSDave Kleikamp * of one, so skip it. 4348ac27a0ecSDave Kleikamp */ 4349ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4350ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4351ac27a0ecSDave Kleikamp goto make_io; 4352ac27a0ecSDave Kleikamp } 4353240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4354ac27a0ecSDave Kleikamp if (i == inode_offset) 4355ac27a0ecSDave Kleikamp continue; 4356617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4357ac27a0ecSDave Kleikamp break; 4358ac27a0ecSDave Kleikamp } 4359ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4360240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4361ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4362ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4363ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4364ac27a0ecSDave Kleikamp unlock_buffer(bh); 4365ac27a0ecSDave Kleikamp goto has_buffer; 4366ac27a0ecSDave Kleikamp } 4367ac27a0ecSDave Kleikamp } 4368ac27a0ecSDave Kleikamp 4369ac27a0ecSDave Kleikamp make_io: 4370ac27a0ecSDave Kleikamp /* 4371240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4372240799cdSTheodore Ts'o * blocks from the inode table. 4373240799cdSTheodore Ts'o */ 437402f03c42SLinus Torvalds blk_start_plug(&plug); 4375240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4376240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4377240799cdSTheodore Ts'o unsigned num; 43780d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4379240799cdSTheodore Ts'o 4380240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4381b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 43820d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4383240799cdSTheodore Ts'o if (table > b) 4384240799cdSTheodore Ts'o b = table; 43850d606e2cSTheodore Ts'o end = b + ra_blks; 4386240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4387feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4388560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4389240799cdSTheodore Ts'o table += num / inodes_per_block; 4390240799cdSTheodore Ts'o if (end > table) 4391240799cdSTheodore Ts'o end = table; 4392240799cdSTheodore Ts'o while (b <= end) 4393d87f6392SRoman Gushchin sb_breadahead_unmovable(sb, b++); 4394240799cdSTheodore Ts'o } 4395240799cdSTheodore Ts'o 4396240799cdSTheodore Ts'o /* 4397ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4398ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4399ac27a0ecSDave Kleikamp * Read the block from disk. 4400ac27a0ecSDave Kleikamp */ 44010562e0baSJiaying Zhang trace_ext4_load_inode(inode); 4402*2d069c08Szhangyi (F) ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 440302f03c42SLinus Torvalds blk_finish_plug(&plug); 4404ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4405ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 440646f870d6STheodore Ts'o simulate_eio: 440754d3adbcSTheodore Ts'o ext4_error_inode_block(inode, block, EIO, 4408c398eda0STheodore Ts'o "unable to read itable block"); 4409ac27a0ecSDave Kleikamp brelse(bh); 4410ac27a0ecSDave Kleikamp return -EIO; 4411ac27a0ecSDave Kleikamp } 4412ac27a0ecSDave Kleikamp } 4413ac27a0ecSDave Kleikamp has_buffer: 4414ac27a0ecSDave Kleikamp iloc->bh = bh; 4415ac27a0ecSDave Kleikamp return 0; 4416ac27a0ecSDave Kleikamp } 4417ac27a0ecSDave Kleikamp 4418617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4419ac27a0ecSDave Kleikamp { 4420ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4421617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 442219f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4423ac27a0ecSDave Kleikamp } 4424ac27a0ecSDave Kleikamp 4425a8ab6d38SIra Weiny static bool ext4_should_enable_dax(struct inode *inode) 44266642586bSRoss Zwisler { 4427a8ab6d38SIra Weiny struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4428a8ab6d38SIra Weiny 44299cb20f94SIra Weiny if (test_opt2(inode->i_sb, DAX_NEVER)) 44306642586bSRoss Zwisler return false; 44316642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 44326642586bSRoss Zwisler return false; 44336642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 44346642586bSRoss Zwisler return false; 44356642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 44366642586bSRoss Zwisler return false; 4437592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 44386642586bSRoss Zwisler return false; 4439c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4440c93d8f88SEric Biggers return false; 4441a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) 4442a8ab6d38SIra Weiny return false; 4443a8ab6d38SIra Weiny if (test_opt(inode->i_sb, DAX_ALWAYS)) 44446642586bSRoss Zwisler return true; 4445a8ab6d38SIra Weiny 4446b383a73fSIra Weiny return ext4_test_inode_flag(inode, EXT4_INODE_DAX); 44476642586bSRoss Zwisler } 44486642586bSRoss Zwisler 4449043546e4SIra Weiny void ext4_set_inode_flags(struct inode *inode, bool init) 4450ac27a0ecSDave Kleikamp { 4451617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 445200a1a053STheodore Ts'o unsigned int new_fl = 0; 4453ac27a0ecSDave Kleikamp 4454043546e4SIra Weiny WARN_ON_ONCE(IS_DAX(inode) && init); 4455043546e4SIra Weiny 4456617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 445700a1a053STheodore Ts'o new_fl |= S_SYNC; 4458617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 445900a1a053STheodore Ts'o new_fl |= S_APPEND; 4460617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 446100a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4462617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 446300a1a053STheodore Ts'o new_fl |= S_NOATIME; 4464617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 446500a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 4466043546e4SIra Weiny 4467043546e4SIra Weiny /* Because of the way inode_set_flags() works we must preserve S_DAX 4468043546e4SIra Weiny * here if already set. */ 4469043546e4SIra Weiny new_fl |= (inode->i_flags & S_DAX); 4470043546e4SIra Weiny if (init && ext4_should_enable_dax(inode)) 4471923ae0ffSRoss Zwisler new_fl |= S_DAX; 4472043546e4SIra Weiny 44732ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 44742ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4475b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4476b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4477c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4478c93d8f88SEric Biggers new_fl |= S_VERITY; 44795f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 44802ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4481c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4482ac27a0ecSDave Kleikamp } 4483ac27a0ecSDave Kleikamp 44840fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 44850fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 44860fc1b451SAneesh Kumar K.V { 44870fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 44888180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 44898180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 44900fc1b451SAneesh Kumar K.V 4491e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 44920fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 44930fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 44940fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 449507a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 44968180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 44978180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 44988180a562SAneesh Kumar K.V } else { 44990fc1b451SAneesh Kumar K.V return i_blocks; 45008180a562SAneesh Kumar K.V } 45010fc1b451SAneesh Kumar K.V } else { 45020fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 45030fc1b451SAneesh Kumar K.V } 45040fc1b451SAneesh Kumar K.V } 4505ff9ddf7eSJan Kara 4506eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4507152a7b0aSTao Ma struct ext4_inode *raw_inode, 4508152a7b0aSTao Ma struct ext4_inode_info *ei) 4509152a7b0aSTao Ma { 4510152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4511152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4512eb9b5f01STheodore Ts'o 4513290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4514290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4515290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4516152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4517eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4518f19d5870STao Ma } else 4519f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4520eb9b5f01STheodore Ts'o return 0; 4521152a7b0aSTao Ma } 4522152a7b0aSTao Ma 4523040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4524040cb378SLi Xi { 45250b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4526040cb378SLi Xi return -EOPNOTSUPP; 4527040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4528040cb378SLi Xi return 0; 4529040cb378SLi Xi } 4530040cb378SLi Xi 4531e254d1afSEryu Guan /* 4532e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4533e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4534e254d1afSEryu Guan * set. 4535e254d1afSEryu Guan */ 4536e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4537e254d1afSEryu Guan { 4538e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4539e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4540e254d1afSEryu Guan else 4541e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4542e254d1afSEryu Guan } 4543e254d1afSEryu Guan static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4544e254d1afSEryu Guan { 4545e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4546e254d1afSEryu Guan return inode_peek_iversion_raw(inode); 4547e254d1afSEryu Guan else 4548e254d1afSEryu Guan return inode_peek_iversion(inode); 4549e254d1afSEryu Guan } 4550e254d1afSEryu Guan 45518a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 45528a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 45538a363970STheodore Ts'o unsigned int line) 4554ac27a0ecSDave Kleikamp { 4555617ba13bSMingming Cao struct ext4_iloc iloc; 4556617ba13bSMingming Cao struct ext4_inode *raw_inode; 45571d1fe1eeSDavid Howells struct ext4_inode_info *ei; 45581d1fe1eeSDavid Howells struct inode *inode; 4559b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 45601d1fe1eeSDavid Howells long ret; 45617e6e1ef4SDarrick J. Wong loff_t size; 4562ac27a0ecSDave Kleikamp int block; 456308cefc7aSEric W. Biederman uid_t i_uid; 456408cefc7aSEric W. Biederman gid_t i_gid; 4565040cb378SLi Xi projid_t i_projid; 4566ac27a0ecSDave Kleikamp 4567191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 45688a363970STheodore Ts'o (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 45698a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 45708a363970STheodore Ts'o (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 45718a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 45728a363970STheodore Ts'o return ERR_PTR(-ESTALE); 457354d3adbcSTheodore Ts'o __ext4_error(sb, function, line, EFSCORRUPTED, 0, 45748a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 45758a363970STheodore Ts'o ino, current->comm); 45768a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 45778a363970STheodore Ts'o } 45788a363970STheodore Ts'o 45791d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 45801d1fe1eeSDavid Howells if (!inode) 45811d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 45821d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 45831d1fe1eeSDavid Howells return inode; 45841d1fe1eeSDavid Howells 45851d1fe1eeSDavid Howells ei = EXT4_I(inode); 45867dc57615SPeter Huewe iloc.bh = NULL; 4587ac27a0ecSDave Kleikamp 45881d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 45891d1fe1eeSDavid Howells if (ret < 0) 4590ac27a0ecSDave Kleikamp goto bad_inode; 4591617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4592814525f4SDarrick J. Wong 45938e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 45948a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45958a363970STheodore Ts'o "iget: root inode unallocated"); 45968e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 45978e4b5eaeSTheodore Ts'o goto bad_inode; 45988e4b5eaeSTheodore Ts'o } 45998e4b5eaeSTheodore Ts'o 46008a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 46018a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 46028a363970STheodore Ts'o ret = -ESTALE; 46038a363970STheodore Ts'o goto bad_inode; 46048a363970STheodore Ts'o } 46058a363970STheodore Ts'o 4606814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4607814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4608814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 46092dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 46102dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 46118a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46128a363970STheodore Ts'o "iget: bad extra_isize %u " 46138a363970STheodore Ts'o "(inode size %u)", 46142dc8d9e1SEric Biggers ei->i_extra_isize, 4615814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 46166a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4617814525f4SDarrick J. Wong goto bad_inode; 4618814525f4SDarrick J. Wong } 4619814525f4SDarrick J. Wong } else 4620814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4621814525f4SDarrick J. Wong 4622814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 46239aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4624814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4625814525f4SDarrick J. Wong __u32 csum; 4626814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4627814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4628814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4629814525f4SDarrick J. Wong sizeof(inum)); 4630814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4631814525f4SDarrick J. Wong sizeof(gen)); 4632814525f4SDarrick J. Wong } 4633814525f4SDarrick J. Wong 463446f870d6STheodore Ts'o if (!ext4_inode_csum_verify(inode, raw_inode, ei) || 463546f870d6STheodore Ts'o ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) { 463654d3adbcSTheodore Ts'o ext4_error_inode_err(inode, function, line, 0, EFSBADCRC, 46378a363970STheodore Ts'o "iget: checksum invalid"); 46386a797d27SDarrick J. Wong ret = -EFSBADCRC; 4639814525f4SDarrick J. Wong goto bad_inode; 4640814525f4SDarrick J. Wong } 4641814525f4SDarrick J. Wong 4642ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 464308cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 464408cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 46450b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4646040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4647040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4648040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4649040cb378SLi Xi else 4650040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4651040cb378SLi Xi 4652ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 465308cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 465408cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4655ac27a0ecSDave Kleikamp } 465608cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 465708cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4658040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4659bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4660ac27a0ecSDave Kleikamp 4661353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 466267cf5b09STao Ma ei->i_inline_off = 0; 4663ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4664ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4665ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4666ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4667ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4668ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4669ac27a0ecSDave Kleikamp */ 4670ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4671393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4672393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4673393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4674ac27a0ecSDave Kleikamp /* this inode is deleted */ 46751d1fe1eeSDavid Howells ret = -ESTALE; 4676ac27a0ecSDave Kleikamp goto bad_inode; 4677ac27a0ecSDave Kleikamp } 4678ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4679ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4680ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4681393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4682393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4683393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4684ac27a0ecSDave Kleikamp } 4685ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4686043546e4SIra Weiny ext4_set_inode_flags(inode, true); 46870fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 46887973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4689e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4690a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4691a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4692e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 46937e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 46948a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46958a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 46967e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 46977e6e1ef4SDarrick J. Wong goto bad_inode; 46987e6e1ef4SDarrick J. Wong } 469948a34311SJan Kara /* 470048a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 470148a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 470248a34311SJan Kara * checksumming that corrupts checksums so forbid that. 470348a34311SJan Kara */ 470448a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 470548a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 470648a34311SJan Kara ext4_error_inode(inode, function, line, 0, 470748a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 470848a34311SJan Kara ret = -EFSCORRUPTED; 470948a34311SJan Kara goto bad_inode; 471048a34311SJan Kara } 4711ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4712a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4713a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4714a9e7f447SDmitry Monakhov #endif 4715ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4716ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4717a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4718ac27a0ecSDave Kleikamp /* 4719ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4720ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4721ac27a0ecSDave Kleikamp */ 4722617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4723ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4724ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4725ac27a0ecSDave Kleikamp 4726b436b9beSJan Kara /* 4727b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4728b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4729b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4730b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4731b436b9beSJan Kara * now it is reread from disk. 4732b436b9beSJan Kara */ 4733b436b9beSJan Kara if (journal) { 4734b436b9beSJan Kara transaction_t *transaction; 4735b436b9beSJan Kara tid_t tid; 4736b436b9beSJan Kara 4737a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4738b436b9beSJan Kara if (journal->j_running_transaction) 4739b436b9beSJan Kara transaction = journal->j_running_transaction; 4740b436b9beSJan Kara else 4741b436b9beSJan Kara transaction = journal->j_committing_transaction; 4742b436b9beSJan Kara if (transaction) 4743b436b9beSJan Kara tid = transaction->t_tid; 4744b436b9beSJan Kara else 4745b436b9beSJan Kara tid = journal->j_commit_sequence; 4746a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4747b436b9beSJan Kara ei->i_sync_tid = tid; 4748b436b9beSJan Kara ei->i_datasync_tid = tid; 4749b436b9beSJan Kara } 4750b436b9beSJan Kara 47510040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4752ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4753ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 47542dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4755617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4756617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4757ac27a0ecSDave Kleikamp } else { 4758eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4759eb9b5f01STheodore Ts'o if (ret) 4760eb9b5f01STheodore Ts'o goto bad_inode; 4761ac27a0ecSDave Kleikamp } 4762814525f4SDarrick J. Wong } 4763ac27a0ecSDave Kleikamp 4764ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4765ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4766ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4767ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4768ef7f3835SKalpak Shah 4769ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4770ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4771ee73f9a5SJeff Layton 477225ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 477325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4774ee73f9a5SJeff Layton ivers |= 477525ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 477625ec56b5SJean Noel Cordenner } 4777e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4778c4f65706STheodore Ts'o } 477925ec56b5SJean Noel Cordenner 4780c4b5a614STheodore Ts'o ret = 0; 4781485c26ecSTheodore Ts'o if (ei->i_file_acl && 4782ce9f24ccSJan Kara !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) { 47838a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47848a363970STheodore Ts'o "iget: bad extended attribute block %llu", 478524676da4STheodore Ts'o ei->i_file_acl); 47866a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4787485c26ecSTheodore Ts'o goto bad_inode; 4788f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4789bc716523SLiu Song /* validate the block references in the inode */ 4790bc716523SLiu Song if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4791fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4792fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4793bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4794bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4795bc716523SLiu Song else 47961f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4797fe2c8191SThiemo Nagel } 4798f19d5870STao Ma } 4799567f3e9aSTheodore Ts'o if (ret) 48007a262f7cSAneesh Kumar K.V goto bad_inode; 48017a262f7cSAneesh Kumar K.V 4802ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4803617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4804617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4805617ba13bSMingming Cao ext4_set_aops(inode); 4806ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4807617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4808617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4809ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 48106390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 48116390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 48128a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48138a363970STheodore Ts'o "iget: immutable or append flags " 48148a363970STheodore Ts'o "not allowed on symlinks"); 48156390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 48166390d33bSLuis R. Rodriguez goto bad_inode; 48176390d33bSLuis R. Rodriguez } 4818592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4819a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4820a7a67e8aSAl Viro ext4_set_aops(inode); 4821a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 482275e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4823617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4824e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4825e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4826e83c1397SDuane Griffin } else { 4827617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4828617ba13bSMingming Cao ext4_set_aops(inode); 4829ac27a0ecSDave Kleikamp } 483021fc61c7SAl Viro inode_nohighmem(inode); 4831563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4832563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4833617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4834ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4835ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4836ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4837ac27a0ecSDave Kleikamp else 4838ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4839ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4840393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4841393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4842563bdd61STheodore Ts'o } else { 48436a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 48448a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48458a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4846563bdd61STheodore Ts'o goto bad_inode; 4847ac27a0ecSDave Kleikamp } 48486456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 48496456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48506456ca65STheodore Ts'o "casefold flag without casefold feature"); 4851ac27a0ecSDave Kleikamp brelse(iloc.bh); 4852dec214d0STahsin Erdogan 48531d1fe1eeSDavid Howells unlock_new_inode(inode); 48541d1fe1eeSDavid Howells return inode; 4855ac27a0ecSDave Kleikamp 4856ac27a0ecSDave Kleikamp bad_inode: 4857567f3e9aSTheodore Ts'o brelse(iloc.bh); 48581d1fe1eeSDavid Howells iget_failed(inode); 48591d1fe1eeSDavid Howells return ERR_PTR(ret); 4860ac27a0ecSDave Kleikamp } 4861ac27a0ecSDave Kleikamp 48620fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 48630fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 48640fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 48650fc1b451SAneesh Kumar K.V { 48660fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 486728936b62SQian Cai u64 i_blocks = READ_ONCE(inode->i_blocks); 48680fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 48690fc1b451SAneesh Kumar K.V 48700fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 48710fc1b451SAneesh Kumar K.V /* 48724907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 48730fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 48740fc1b451SAneesh Kumar K.V */ 48758180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48760fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 487784a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4878f287a1a5STheodore Ts'o return 0; 4879f287a1a5STheodore Ts'o } 4880e2b911c5SDarrick J. Wong if (!ext4_has_feature_huge_file(sb)) 4881f287a1a5STheodore Ts'o return -EFBIG; 4882f287a1a5STheodore Ts'o 4883f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 48840fc1b451SAneesh Kumar K.V /* 48850fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 48860fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 48870fc1b451SAneesh Kumar K.V */ 48888180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48890fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 489084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 48910fc1b451SAneesh Kumar K.V } else { 489284a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 48938180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 48948180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 48958180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48968180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 48970fc1b451SAneesh Kumar K.V } 4898f287a1a5STheodore Ts'o return 0; 48990fc1b451SAneesh Kumar K.V } 49000fc1b451SAneesh Kumar K.V 49013f19b2abSDavid Howells static void __ext4_update_other_inode_time(struct super_block *sb, 49023f19b2abSDavid Howells unsigned long orig_ino, 49033f19b2abSDavid Howells unsigned long ino, 49043f19b2abSDavid Howells struct ext4_inode *raw_inode) 4905a26f4992STheodore Ts'o { 49063f19b2abSDavid Howells struct inode *inode; 4907a26f4992STheodore Ts'o 49083f19b2abSDavid Howells inode = find_inode_by_ino_rcu(sb, ino); 49093f19b2abSDavid Howells if (!inode) 49103f19b2abSDavid Howells return; 49113f19b2abSDavid Howells 49123f19b2abSDavid Howells if ((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 49130e11f644SChristoph Hellwig I_DIRTY_INODE)) || 4914a26f4992STheodore Ts'o ((inode->i_state & I_DIRTY_TIME) == 0)) 49153f19b2abSDavid Howells return; 49163f19b2abSDavid Howells 4917a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4918a26f4992STheodore Ts'o if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 49190e11f644SChristoph Hellwig I_DIRTY_INODE)) == 0) && 4920a26f4992STheodore Ts'o (inode->i_state & I_DIRTY_TIME)) { 4921a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4922a26f4992STheodore Ts'o 49235fcd5750SJan Kara inode->i_state &= ~I_DIRTY_TIME; 4924a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4925a26f4992STheodore Ts'o 4926a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 49273f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 49283f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 49293f19b2abSDavid Howells EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 49303f19b2abSDavid Howells ext4_inode_csum_set(inode, raw_inode, ei); 4931a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 49323f19b2abSDavid Howells trace_ext4_other_inode_update_time(inode, orig_ino); 49333f19b2abSDavid Howells return; 4934a26f4992STheodore Ts'o } 4935a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4936a26f4992STheodore Ts'o } 4937a26f4992STheodore Ts'o 4938a26f4992STheodore Ts'o /* 4939a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 4940a26f4992STheodore Ts'o * the same inode table block. 4941a26f4992STheodore Ts'o */ 4942a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 4943a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 4944a26f4992STheodore Ts'o { 4945a26f4992STheodore Ts'o unsigned long ino; 4946a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4947a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 4948a26f4992STheodore Ts'o 49490f0ff9a9STheodore Ts'o /* 49500f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 49510f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 49520f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 49530f0ff9a9STheodore Ts'o */ 49540f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 49553f19b2abSDavid Howells rcu_read_lock(); 4956a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 4957a26f4992STheodore Ts'o if (ino == orig_ino) 4958a26f4992STheodore Ts'o continue; 49593f19b2abSDavid Howells __ext4_update_other_inode_time(sb, orig_ino, ino, 49603f19b2abSDavid Howells (struct ext4_inode *)buf); 4961a26f4992STheodore Ts'o } 49623f19b2abSDavid Howells rcu_read_unlock(); 4963a26f4992STheodore Ts'o } 4964a26f4992STheodore Ts'o 4965ac27a0ecSDave Kleikamp /* 4966ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4967ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4968ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4969ac27a0ecSDave Kleikamp * 4970ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4971ac27a0ecSDave Kleikamp */ 4972617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4973ac27a0ecSDave Kleikamp struct inode *inode, 4974830156c7SFrank Mayhar struct ext4_iloc *iloc) 4975ac27a0ecSDave Kleikamp { 4976617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4977617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4978ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4979202ee5dfSTheodore Ts'o struct super_block *sb = inode->i_sb; 4980ac27a0ecSDave Kleikamp int err = 0, rc, block; 4981202ee5dfSTheodore Ts'o int need_datasync = 0, set_large_file = 0; 498208cefc7aSEric W. Biederman uid_t i_uid; 498308cefc7aSEric W. Biederman gid_t i_gid; 4984040cb378SLi Xi projid_t i_projid; 4985ac27a0ecSDave Kleikamp 4986202ee5dfSTheodore Ts'o spin_lock(&ei->i_raw_lock); 4987202ee5dfSTheodore Ts'o 4988202ee5dfSTheodore Ts'o /* For fields not tracked in the in-memory inode, 4989ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 499019f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4991617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4992ac27a0ecSDave Kleikamp 4993ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 499408cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 499508cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4996040cb378SLi Xi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 4997ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 499808cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 499908cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 5000ac27a0ecSDave Kleikamp /* 5001ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5002ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5003ac27a0ecSDave Kleikamp */ 500493e3b4e6SDaeho Jeong if (ei->i_dtime && list_empty(&ei->i_orphan)) { 500593e3b4e6SDaeho Jeong raw_inode->i_uid_high = 0; 500693e3b4e6SDaeho Jeong raw_inode->i_gid_high = 0; 500793e3b4e6SDaeho Jeong } else { 5008ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 500908cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 5010ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 501108cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 5012ac27a0ecSDave Kleikamp } 5013ac27a0ecSDave Kleikamp } else { 501408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 501508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 5016ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5017ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5018ac27a0ecSDave Kleikamp } 5019ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5020ef7f3835SKalpak Shah 5021ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5022ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5023ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5024ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5025ef7f3835SKalpak Shah 5026bce92d56SLi Xi err = ext4_inode_blocks_set(handle, raw_inode, ei); 5027bce92d56SLi Xi if (err) { 5028202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 50290fc1b451SAneesh Kumar K.V goto out_brelse; 5030202ee5dfSTheodore Ts'o } 5031ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 5032353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 5033ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 5034a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5035a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 50367973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5037dce8e237SQiujun Huang if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) { 5038a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5039b71fc079SJan Kara need_datasync = 1; 5040b71fc079SJan Kara } 5041ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5042e2b911c5SDarrick J. Wong if (!ext4_has_feature_large_file(sb) || 5043617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5044202ee5dfSTheodore Ts'o cpu_to_le32(EXT4_GOOD_OLD_REV)) 5045202ee5dfSTheodore Ts'o set_large_file = 1; 5046ac27a0ecSDave Kleikamp } 5047ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5048ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5049ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5050ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5051ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5052ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5053ac27a0ecSDave Kleikamp } else { 5054ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5055ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5056ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5057ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5058ac27a0ecSDave Kleikamp } 5059f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 5060de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5061ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5062f19d5870STao Ma } 5063ac27a0ecSDave Kleikamp 5064ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 5065e254d1afSEryu Guan u64 ivers = ext4_inode_peek_iversion(inode); 5066ee73f9a5SJeff Layton 5067ee73f9a5SJeff Layton raw_inode->i_disk_version = cpu_to_le32(ivers); 506825ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 506925ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 507025ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 5071ee73f9a5SJeff Layton cpu_to_le32(ivers >> 32); 5072c4f65706STheodore Ts'o raw_inode->i_extra_isize = 5073c4f65706STheodore Ts'o cpu_to_le16(ei->i_extra_isize); 5074c4f65706STheodore Ts'o } 507525ec56b5SJean Noel Cordenner } 5076040cb378SLi Xi 50770b7b7779SKaho Ng BUG_ON(!ext4_has_feature_project(inode->i_sb) && 5078040cb378SLi Xi i_projid != EXT4_DEF_PROJID); 5079040cb378SLi Xi 5080040cb378SLi Xi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 5081040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 5082040cb378SLi Xi raw_inode->i_projid = cpu_to_le32(i_projid); 5083040cb378SLi Xi 5084814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 5085202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 50861751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5087a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5088a26f4992STheodore Ts'o bh->b_data); 5089202ee5dfSTheodore Ts'o 50900390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 509173b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 5092ac27a0ecSDave Kleikamp if (!err) 5093ac27a0ecSDave Kleikamp err = rc; 509419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5095202ee5dfSTheodore Ts'o if (set_large_file) { 50965d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5097202ee5dfSTheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5098202ee5dfSTheodore Ts'o if (err) 5099202ee5dfSTheodore Ts'o goto out_brelse; 5100e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 5101202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5102202ee5dfSTheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 5103202ee5dfSTheodore Ts'o } 5104b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5105ac27a0ecSDave Kleikamp out_brelse: 5106ac27a0ecSDave Kleikamp brelse(bh); 5107617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5108ac27a0ecSDave Kleikamp return err; 5109ac27a0ecSDave Kleikamp } 5110ac27a0ecSDave Kleikamp 5111ac27a0ecSDave Kleikamp /* 5112617ba13bSMingming Cao * ext4_write_inode() 5113ac27a0ecSDave Kleikamp * 5114ac27a0ecSDave Kleikamp * We are called from a few places: 5115ac27a0ecSDave Kleikamp * 511687f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5117ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 51184907cb7bSAnatol Pomozov * transaction to commit. 5119ac27a0ecSDave Kleikamp * 512087f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 512187f7e416STheodore Ts'o * We wait on commit, if told to. 5122ac27a0ecSDave Kleikamp * 512387f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 512487f7e416STheodore Ts'o * We wait on commit, if told to. 5125ac27a0ecSDave Kleikamp * 5126ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5127ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 512887f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 512987f7e416STheodore Ts'o * writeback. 5130ac27a0ecSDave Kleikamp * 5131ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5132ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5133ac27a0ecSDave Kleikamp * which we are interested. 5134ac27a0ecSDave Kleikamp * 5135ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5136ac27a0ecSDave Kleikamp * 5137ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5138ac27a0ecSDave Kleikamp * stuff(); 5139ac27a0ecSDave Kleikamp * inode->i_size = expr; 5140ac27a0ecSDave Kleikamp * 514187f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 514287f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 514387f7e416STheodore Ts'o * superblock's dirty inode list. 5144ac27a0ecSDave Kleikamp */ 5145a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5146ac27a0ecSDave Kleikamp { 514791ac6f43SFrank Mayhar int err; 514891ac6f43SFrank Mayhar 514918f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 515018f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5151ac27a0ecSDave Kleikamp return 0; 5152ac27a0ecSDave Kleikamp 515318f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 515418f2c4fcSTheodore Ts'o return -EIO; 515518f2c4fcSTheodore Ts'o 515691ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5157617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5158b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5159ac27a0ecSDave Kleikamp dump_stack(); 5160ac27a0ecSDave Kleikamp return -EIO; 5161ac27a0ecSDave Kleikamp } 5162ac27a0ecSDave Kleikamp 516310542c22SJan Kara /* 516410542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 516510542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 516610542c22SJan Kara * written. 516710542c22SJan Kara */ 516810542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5169ac27a0ecSDave Kleikamp return 0; 5170ac27a0ecSDave Kleikamp 517118f2c4fcSTheodore Ts'o err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, 517218f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 517391ac6f43SFrank Mayhar } else { 517491ac6f43SFrank Mayhar struct ext4_iloc iloc; 517591ac6f43SFrank Mayhar 51768b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 517791ac6f43SFrank Mayhar if (err) 517891ac6f43SFrank Mayhar return err; 517910542c22SJan Kara /* 518010542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 518110542c22SJan Kara * it here separately for each inode. 518210542c22SJan Kara */ 518310542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5184830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5185830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 518654d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5187c398eda0STheodore Ts'o "IO error syncing inode"); 5188830156c7SFrank Mayhar err = -EIO; 5189830156c7SFrank Mayhar } 5190fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 519191ac6f43SFrank Mayhar } 519291ac6f43SFrank Mayhar return err; 5193ac27a0ecSDave Kleikamp } 5194ac27a0ecSDave Kleikamp 5195ac27a0ecSDave Kleikamp /* 519653e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 519753e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 519853e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 519953e87268SJan Kara */ 520053e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 520153e87268SJan Kara { 520253e87268SJan Kara struct page *page; 520353e87268SJan Kara unsigned offset; 520453e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 520553e87268SJan Kara tid_t commit_tid = 0; 520653e87268SJan Kara int ret; 520753e87268SJan Kara 520809cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 520953e87268SJan Kara /* 5210565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5211565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5212565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5213565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5214565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5215565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5216565333a1Syangerkun * blocksize == PAGESIZE. 521753e87268SJan Kara */ 5218565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 521953e87268SJan Kara return; 522053e87268SJan Kara while (1) { 522153e87268SJan Kara page = find_lock_page(inode->i_mapping, 522209cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 522353e87268SJan Kara if (!page) 522453e87268SJan Kara return; 5225ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 522609cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 522753e87268SJan Kara unlock_page(page); 522809cbfeafSKirill A. Shutemov put_page(page); 522953e87268SJan Kara if (ret != -EBUSY) 523053e87268SJan Kara return; 523153e87268SJan Kara commit_tid = 0; 523253e87268SJan Kara read_lock(&journal->j_state_lock); 523353e87268SJan Kara if (journal->j_committing_transaction) 523453e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 523553e87268SJan Kara read_unlock(&journal->j_state_lock); 523653e87268SJan Kara if (commit_tid) 523753e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 523853e87268SJan Kara } 523953e87268SJan Kara } 524053e87268SJan Kara 524153e87268SJan Kara /* 5242617ba13bSMingming Cao * ext4_setattr() 5243ac27a0ecSDave Kleikamp * 5244ac27a0ecSDave Kleikamp * Called from notify_change. 5245ac27a0ecSDave Kleikamp * 5246ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5247ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5248ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5249ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5250ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5251ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5252ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5253ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5254ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5255ac27a0ecSDave Kleikamp * 5256678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5257678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5258678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5259678aaf48SJan Kara * This way we are sure that all the data written in the previous 5260678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5261678aaf48SJan Kara * writeback). 5262678aaf48SJan Kara * 5263678aaf48SJan Kara * Called with inode->i_mutex down. 5264ac27a0ecSDave Kleikamp */ 5265617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5266ac27a0ecSDave Kleikamp { 52672b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5268ac27a0ecSDave Kleikamp int error, rc = 0; 52693d287de3SDmitry Monakhov int orphan = 0; 5270ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5271ac27a0ecSDave Kleikamp 52720db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 52730db1ff22STheodore Ts'o return -EIO; 52740db1ff22STheodore Ts'o 527502b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 527602b016caSTheodore Ts'o return -EPERM; 527702b016caSTheodore Ts'o 527802b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 527902b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 528002b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 528102b016caSTheodore Ts'o return -EPERM; 528202b016caSTheodore Ts'o 528331051c85SJan Kara error = setattr_prepare(dentry, attr); 5284ac27a0ecSDave Kleikamp if (error) 5285ac27a0ecSDave Kleikamp return error; 5286ac27a0ecSDave Kleikamp 52873ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 52883ce2b8ddSEric Biggers if (error) 52893ce2b8ddSEric Biggers return error; 52903ce2b8ddSEric Biggers 5291c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5292c93d8f88SEric Biggers if (error) 5293c93d8f88SEric Biggers return error; 5294c93d8f88SEric Biggers 5295a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5296a7cdadeeSJan Kara error = dquot_initialize(inode); 5297a7cdadeeSJan Kara if (error) 5298a7cdadeeSJan Kara return error; 5299a7cdadeeSJan Kara } 530008cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 530108cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5302ac27a0ecSDave Kleikamp handle_t *handle; 5303ac27a0ecSDave Kleikamp 5304ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5305ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 53069924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 53079924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5308194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5309ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5310ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5311ac27a0ecSDave Kleikamp goto err_out; 5312ac27a0ecSDave Kleikamp } 53137a9ca53aSTahsin Erdogan 53147a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 53157a9ca53aSTahsin Erdogan * counts xattr inode references. 53167a9ca53aSTahsin Erdogan */ 53177a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5318b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 53197a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 53207a9ca53aSTahsin Erdogan 5321ac27a0ecSDave Kleikamp if (error) { 5322617ba13bSMingming Cao ext4_journal_stop(handle); 5323ac27a0ecSDave Kleikamp return error; 5324ac27a0ecSDave Kleikamp } 5325ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5326ac27a0ecSDave Kleikamp * one transaction */ 5327ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5328ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5329ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5330ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5331617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5332617ba13bSMingming Cao ext4_journal_stop(handle); 53334209ae12SHarshad Shirwadkar if (unlikely(error)) 53344209ae12SHarshad Shirwadkar return error; 5335ac27a0ecSDave Kleikamp } 5336ac27a0ecSDave Kleikamp 53373da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53385208386cSJan Kara handle_t *handle; 53393da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5340b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5341562c72aaSChristoph Hellwig 534212e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5343e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5344e2b46574SEric Sandeen 53450c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 53460c095c7fSTheodore Ts'o return -EFBIG; 5347e2b46574SEric Sandeen } 53483da40c7bSJosef Bacik if (!S_ISREG(inode->i_mode)) 53493da40c7bSJosef Bacik return -EINVAL; 5350dff6efc3SChristoph Hellwig 5351dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5352dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5353dff6efc3SChristoph Hellwig 5354b9c1c267SJan Kara if (shrink) { 5355b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 53565208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 53575208386cSJan Kara attr->ia_size); 53585208386cSJan Kara if (error) 53595208386cSJan Kara goto err_out; 53605208386cSJan Kara } 5361b9c1c267SJan Kara /* 5362b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5363b9c1c267SJan Kara * for dio in flight. 5364b9c1c267SJan Kara */ 5365b9c1c267SJan Kara inode_dio_wait(inode); 5366b9c1c267SJan Kara } 5367b9c1c267SJan Kara 5368b9c1c267SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5369b9c1c267SJan Kara 5370b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5371b9c1c267SJan Kara if (rc) { 5372b9c1c267SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 5373b9c1c267SJan Kara return rc; 5374b9c1c267SJan Kara } 5375b9c1c267SJan Kara 53763da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 53779924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5378ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5379ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5380b9c1c267SJan Kara goto out_mmap_sem; 5381ac27a0ecSDave Kleikamp } 53823da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5383617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 53843d287de3SDmitry Monakhov orphan = 1; 53853d287de3SDmitry Monakhov } 5386911af577SEryu Guan /* 5387911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5388911af577SEryu Guan * update c/mtime in shrink case below 5389911af577SEryu Guan */ 5390911af577SEryu Guan if (!shrink) { 5391eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5392911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5393911af577SEryu Guan } 539490e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5395617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5396617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5397ac27a0ecSDave Kleikamp if (!error) 5398ac27a0ecSDave Kleikamp error = rc; 539990e775b7SJan Kara /* 540090e775b7SJan Kara * We have to update i_size under i_data_sem together 540190e775b7SJan Kara * with i_disksize to avoid races with writeback code 540290e775b7SJan Kara * running ext4_wb_update_i_disksize(). 540390e775b7SJan Kara */ 540490e775b7SJan Kara if (!error) 540590e775b7SJan Kara i_size_write(inode, attr->ia_size); 540690e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5407617ba13bSMingming Cao ext4_journal_stop(handle); 5408b9c1c267SJan Kara if (error) 5409b9c1c267SJan Kara goto out_mmap_sem; 541082a25b02SJan Kara if (!shrink) { 5411b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5412b9c1c267SJan Kara inode->i_size); 5413b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 541482a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5415b9c1c267SJan Kara } 5416430657b6SRoss Zwisler } 5417430657b6SRoss Zwisler 541853e87268SJan Kara /* 541953e87268SJan Kara * Truncate pagecache after we've waited for commit 542053e87268SJan Kara * in data=journal mode to make pages freeable. 542153e87268SJan Kara */ 54227caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5423b9c1c267SJan Kara /* 5424b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5425b9c1c267SJan Kara * truncate possible preallocated blocks. 5426b9c1c267SJan Kara */ 5427b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54282c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54292c98eb5eSTheodore Ts'o if (rc) 54302c98eb5eSTheodore Ts'o error = rc; 54312c98eb5eSTheodore Ts'o } 5432b9c1c267SJan Kara out_mmap_sem: 5433ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 54343da40c7bSJosef Bacik } 5435ac27a0ecSDave Kleikamp 54362c98eb5eSTheodore Ts'o if (!error) { 54371025774cSChristoph Hellwig setattr_copy(inode, attr); 54381025774cSChristoph Hellwig mark_inode_dirty(inode); 54391025774cSChristoph Hellwig } 54401025774cSChristoph Hellwig 54411025774cSChristoph Hellwig /* 54421025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 54431025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 54441025774cSChristoph Hellwig */ 54453d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5446617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5447ac27a0ecSDave Kleikamp 54482c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 544964e178a7SChristoph Hellwig rc = posix_acl_chmod(inode, inode->i_mode); 5450ac27a0ecSDave Kleikamp 5451ac27a0ecSDave Kleikamp err_out: 5452617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5453ac27a0ecSDave Kleikamp if (!error) 5454ac27a0ecSDave Kleikamp error = rc; 5455ac27a0ecSDave Kleikamp return error; 5456ac27a0ecSDave Kleikamp } 5457ac27a0ecSDave Kleikamp 5458a528d35eSDavid Howells int ext4_getattr(const struct path *path, struct kstat *stat, 5459a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 54603e3398a0SMingming Cao { 546199652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 546299652ea5SDavid Howells struct ext4_inode *raw_inode; 546399652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 546499652ea5SDavid Howells unsigned int flags; 54653e3398a0SMingming Cao 5466d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5467d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 546899652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 546999652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 547099652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 547199652ea5SDavid Howells } 547299652ea5SDavid Howells 547399652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 547499652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 547599652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 547699652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 547799652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 547899652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 547999652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 548099652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 548199652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 548299652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 548399652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 54841f607195SEric Biggers if (flags & EXT4_VERITY_FL) 54851f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 548699652ea5SDavid Howells 54873209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 54883209f68bSDavid Howells STATX_ATTR_COMPRESSED | 54893209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 54903209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 54911f607195SEric Biggers STATX_ATTR_NODUMP | 54921f607195SEric Biggers STATX_ATTR_VERITY); 54933209f68bSDavid Howells 54943e3398a0SMingming Cao generic_fillattr(inode, stat); 549599652ea5SDavid Howells return 0; 549699652ea5SDavid Howells } 549799652ea5SDavid Howells 549899652ea5SDavid Howells int ext4_file_getattr(const struct path *path, struct kstat *stat, 549999652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 550099652ea5SDavid Howells { 550199652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 550299652ea5SDavid Howells u64 delalloc_blocks; 550399652ea5SDavid Howells 550499652ea5SDavid Howells ext4_getattr(path, stat, request_mask, query_flags); 55053e3398a0SMingming Cao 55063e3398a0SMingming Cao /* 55079206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 55089206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 55099206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5510d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 55119206c561SAndreas Dilger */ 55129206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 55139206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 55149206c561SAndreas Dilger 55159206c561SAndreas Dilger /* 55163e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 55173e3398a0SMingming Cao * otherwise in the case of system crash before the real block 55183e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 55193e3398a0SMingming Cao * on-disk file blocks. 55203e3398a0SMingming Cao * We always keep i_blocks updated together with real 55213e3398a0SMingming Cao * allocation. But to not confuse with user, stat 55223e3398a0SMingming Cao * will return the blocks that include the delayed allocation 55233e3398a0SMingming Cao * blocks for this file. 55243e3398a0SMingming Cao */ 552596607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 552696607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55278af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55283e3398a0SMingming Cao return 0; 55293e3398a0SMingming Cao } 5530ac27a0ecSDave Kleikamp 5531fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5532fffb2739SJan Kara int pextents) 5533a02908f1SMingming Cao { 553412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5535fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5536fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5537a02908f1SMingming Cao } 5538ac51d837STheodore Ts'o 5539a02908f1SMingming Cao /* 5540a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5541a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5542a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5543a02908f1SMingming Cao * 5544a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 55454907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5546a02908f1SMingming Cao * they could still across block group boundary. 5547a02908f1SMingming Cao * 5548a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5549a02908f1SMingming Cao */ 5550dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5551fffb2739SJan Kara int pextents) 5552a02908f1SMingming Cao { 55538df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 55548df9675fSTheodore Ts'o int gdpblocks; 5555a02908f1SMingming Cao int idxblocks; 5556a02908f1SMingming Cao int ret = 0; 5557a02908f1SMingming Cao 5558a02908f1SMingming Cao /* 5559fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5560fffb2739SJan Kara * to @pextents physical extents? 5561a02908f1SMingming Cao */ 5562fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5563a02908f1SMingming Cao 5564a02908f1SMingming Cao ret = idxblocks; 5565a02908f1SMingming Cao 5566a02908f1SMingming Cao /* 5567a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5568a02908f1SMingming Cao * to account 5569a02908f1SMingming Cao */ 5570fffb2739SJan Kara groups = idxblocks + pextents; 5571a02908f1SMingming Cao gdpblocks = groups; 55728df9675fSTheodore Ts'o if (groups > ngroups) 55738df9675fSTheodore Ts'o groups = ngroups; 5574a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5575a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5576a02908f1SMingming Cao 5577a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5578a02908f1SMingming Cao ret += groups + gdpblocks; 5579a02908f1SMingming Cao 5580a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5581a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5582ac27a0ecSDave Kleikamp 5583ac27a0ecSDave Kleikamp return ret; 5584ac27a0ecSDave Kleikamp } 5585ac27a0ecSDave Kleikamp 5586ac27a0ecSDave Kleikamp /* 558725985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5588f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5589f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5590a02908f1SMingming Cao * 5591525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5592a02908f1SMingming Cao * 5593525f4ed8SMingming Cao * We need to consider the worse case, when 5594a02908f1SMingming Cao * one new block per extent. 5595a02908f1SMingming Cao */ 5596a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5597a02908f1SMingming Cao { 5598a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5599a02908f1SMingming Cao int ret; 5600a02908f1SMingming Cao 5601fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5602a02908f1SMingming Cao 5603a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5604a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5605a02908f1SMingming Cao ret += bpp; 5606a02908f1SMingming Cao return ret; 5607a02908f1SMingming Cao } 5608f3bd1f3fSMingming Cao 5609f3bd1f3fSMingming Cao /* 5610f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5611f3bd1f3fSMingming Cao * 5612f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 561379e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5614f3bd1f3fSMingming Cao * 5615f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5616f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5617f3bd1f3fSMingming Cao */ 5618f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5619f3bd1f3fSMingming Cao { 5620f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5621f3bd1f3fSMingming Cao } 5622f3bd1f3fSMingming Cao 5623a02908f1SMingming Cao /* 5624617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5625ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5626ac27a0ecSDave Kleikamp */ 5627617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5628617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5629ac27a0ecSDave Kleikamp { 5630ac27a0ecSDave Kleikamp int err = 0; 5631ac27a0ecSDave Kleikamp 5632a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5633a6758309SVasily Averin put_bh(iloc->bh); 56340db1ff22STheodore Ts'o return -EIO; 5635a6758309SVasily Averin } 5636c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 563725ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 563825ec56b5SJean Noel Cordenner 5639ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5640ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5641ac27a0ecSDave Kleikamp 5642dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5643830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5644ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5645ac27a0ecSDave Kleikamp return err; 5646ac27a0ecSDave Kleikamp } 5647ac27a0ecSDave Kleikamp 5648ac27a0ecSDave Kleikamp /* 5649ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5650ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5651ac27a0ecSDave Kleikamp */ 5652ac27a0ecSDave Kleikamp 5653ac27a0ecSDave Kleikamp int 5654617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5655617ba13bSMingming Cao struct ext4_iloc *iloc) 5656ac27a0ecSDave Kleikamp { 56570390131bSFrank Mayhar int err; 56580390131bSFrank Mayhar 56590db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 56600db1ff22STheodore Ts'o return -EIO; 56610db1ff22STheodore Ts'o 5662617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5663ac27a0ecSDave Kleikamp if (!err) { 5664ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5665617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5666ac27a0ecSDave Kleikamp if (err) { 5667ac27a0ecSDave Kleikamp brelse(iloc->bh); 5668ac27a0ecSDave Kleikamp iloc->bh = NULL; 5669ac27a0ecSDave Kleikamp } 5670ac27a0ecSDave Kleikamp } 5671617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5672ac27a0ecSDave Kleikamp return err; 5673ac27a0ecSDave Kleikamp } 5674ac27a0ecSDave Kleikamp 5675c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5676c03b45b8SMiao Xie unsigned int new_extra_isize, 5677c03b45b8SMiao Xie struct ext4_iloc *iloc, 5678c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5679c03b45b8SMiao Xie { 5680c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5681c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 56824ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 56834ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5684c03b45b8SMiao Xie int error; 5685c03b45b8SMiao Xie 56864ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 56874ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 56884ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 56894ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 56904ea99936STheodore Ts'o ei->i_extra_isize, 56914ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 56924ea99936STheodore Ts'o return -EFSCORRUPTED; 56934ea99936STheodore Ts'o } 56944ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 56954ea99936STheodore Ts'o (new_extra_isize < 4) || 56964ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 56974ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 56984ea99936STheodore Ts'o 5699c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5700c03b45b8SMiao Xie 5701c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5702c03b45b8SMiao Xie 5703c03b45b8SMiao Xie /* No extended attributes present */ 5704c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5705c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5706c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5707c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5708c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5709c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5710c03b45b8SMiao Xie return 0; 5711c03b45b8SMiao Xie } 5712c03b45b8SMiao Xie 5713c03b45b8SMiao Xie /* try to expand with EAs present */ 5714c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5715c03b45b8SMiao Xie raw_inode, handle); 5716c03b45b8SMiao Xie if (error) { 5717c03b45b8SMiao Xie /* 5718c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5719c03b45b8SMiao Xie */ 5720c03b45b8SMiao Xie *no_expand = 1; 5721c03b45b8SMiao Xie } 5722c03b45b8SMiao Xie 5723c03b45b8SMiao Xie return error; 5724c03b45b8SMiao Xie } 5725c03b45b8SMiao Xie 5726ac27a0ecSDave Kleikamp /* 57276dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57286dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 57296dd4ee7cSKalpak Shah */ 5730cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 57311d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 57321d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 57331d03ec98SAneesh Kumar K.V handle_t *handle) 57346dd4ee7cSKalpak Shah { 57353b10fdc6SMiao Xie int no_expand; 57363b10fdc6SMiao Xie int error; 57376dd4ee7cSKalpak Shah 5738cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5739cf0a5e81SMiao Xie return -EOVERFLOW; 5740cf0a5e81SMiao Xie 5741cf0a5e81SMiao Xie /* 5742cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5743cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5744cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5745cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5746cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5747cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5748cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5749cf0a5e81SMiao Xie */ 57506cb367c2SJan Kara if (ext4_journal_extend(handle, 575183448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5752cf0a5e81SMiao Xie return -ENOSPC; 57536dd4ee7cSKalpak Shah 57543b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5755cf0a5e81SMiao Xie return -EBUSY; 57563b10fdc6SMiao Xie 5757c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5758c03b45b8SMiao Xie handle, &no_expand); 57593b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5760c03b45b8SMiao Xie 5761c03b45b8SMiao Xie return error; 57626dd4ee7cSKalpak Shah } 57636dd4ee7cSKalpak Shah 5764c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5765c03b45b8SMiao Xie unsigned int new_extra_isize, 5766c03b45b8SMiao Xie struct ext4_iloc *iloc) 5767c03b45b8SMiao Xie { 5768c03b45b8SMiao Xie handle_t *handle; 5769c03b45b8SMiao Xie int no_expand; 5770c03b45b8SMiao Xie int error, rc; 5771c03b45b8SMiao Xie 5772c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5773c03b45b8SMiao Xie brelse(iloc->bh); 5774c03b45b8SMiao Xie return -EOVERFLOW; 5775c03b45b8SMiao Xie } 5776c03b45b8SMiao Xie 5777c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5778c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5779c03b45b8SMiao Xie if (IS_ERR(handle)) { 5780c03b45b8SMiao Xie error = PTR_ERR(handle); 5781c03b45b8SMiao Xie brelse(iloc->bh); 5782c03b45b8SMiao Xie return error; 5783c03b45b8SMiao Xie } 5784c03b45b8SMiao Xie 5785c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5786c03b45b8SMiao Xie 5787ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5788c03b45b8SMiao Xie error = ext4_journal_get_write_access(handle, iloc->bh); 57893b10fdc6SMiao Xie if (error) { 5790c03b45b8SMiao Xie brelse(iloc->bh); 57917f420d64SDan Carpenter goto out_unlock; 57923b10fdc6SMiao Xie } 5793cf0a5e81SMiao Xie 5794c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5795c03b45b8SMiao Xie handle, &no_expand); 5796c03b45b8SMiao Xie 5797c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5798c03b45b8SMiao Xie if (!error) 5799c03b45b8SMiao Xie error = rc; 5800c03b45b8SMiao Xie 58017f420d64SDan Carpenter out_unlock: 5802c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5803c03b45b8SMiao Xie ext4_journal_stop(handle); 58043b10fdc6SMiao Xie return error; 58056dd4ee7cSKalpak Shah } 58066dd4ee7cSKalpak Shah 58076dd4ee7cSKalpak Shah /* 5808ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5809ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5810ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5811ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5812ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5813ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5814ac27a0ecSDave Kleikamp * 5815ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5816ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5817ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5818ac27a0ecSDave Kleikamp * we start and wait on commits. 5819ac27a0ecSDave Kleikamp */ 58204209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 58214209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5822ac27a0ecSDave Kleikamp { 5823617ba13bSMingming Cao struct ext4_iloc iloc; 58246dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5825cf0a5e81SMiao Xie int err; 5826ac27a0ecSDave Kleikamp 5827ac27a0ecSDave Kleikamp might_sleep(); 58287ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5829617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58305e1021f2SEryu Guan if (err) 58314209ae12SHarshad Shirwadkar goto out; 5832cf0a5e81SMiao Xie 5833cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5834cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 58356dd4ee7cSKalpak Shah iloc, handle); 5836cf0a5e81SMiao Xie 58374209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 58384209ae12SHarshad Shirwadkar out: 58394209ae12SHarshad Shirwadkar if (unlikely(err)) 58404209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 58414209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 58424209ae12SHarshad Shirwadkar return err; 5843ac27a0ecSDave Kleikamp } 5844ac27a0ecSDave Kleikamp 5845ac27a0ecSDave Kleikamp /* 5846617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5847ac27a0ecSDave Kleikamp * 5848ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5849ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5850ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5851ac27a0ecSDave Kleikamp * 58525dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5853ac27a0ecSDave Kleikamp * are allocated to the file. 5854ac27a0ecSDave Kleikamp * 5855ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5856ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5857ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 58580ae45f63STheodore Ts'o * 58590ae45f63STheodore Ts'o * If only the I_DIRTY_TIME flag is set, we can skip everything. If 58600ae45f63STheodore Ts'o * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need 58610ae45f63STheodore Ts'o * to copy into the on-disk inode structure are the timestamp files. 5862ac27a0ecSDave Kleikamp */ 5863aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5864ac27a0ecSDave Kleikamp { 5865ac27a0ecSDave Kleikamp handle_t *handle; 5866ac27a0ecSDave Kleikamp 58670ae45f63STheodore Ts'o if (flags == I_DIRTY_TIME) 58680ae45f63STheodore Ts'o return; 58699924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5870ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5871ac27a0ecSDave Kleikamp goto out; 5872f3dc272fSCurt Wohlgemuth 5873617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5874f3dc272fSCurt Wohlgemuth 5875617ba13bSMingming Cao ext4_journal_stop(handle); 5876ac27a0ecSDave Kleikamp out: 5877ac27a0ecSDave Kleikamp return; 5878ac27a0ecSDave Kleikamp } 5879ac27a0ecSDave Kleikamp 5880617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5881ac27a0ecSDave Kleikamp { 5882ac27a0ecSDave Kleikamp journal_t *journal; 5883ac27a0ecSDave Kleikamp handle_t *handle; 5884ac27a0ecSDave Kleikamp int err; 5885c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5886ac27a0ecSDave Kleikamp 5887ac27a0ecSDave Kleikamp /* 5888ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5889ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5890ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5891ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5892ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5893ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5894ac27a0ecSDave Kleikamp * nobody is changing anything. 5895ac27a0ecSDave Kleikamp */ 5896ac27a0ecSDave Kleikamp 5897617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 58980390131bSFrank Mayhar if (!journal) 58990390131bSFrank Mayhar return 0; 5900d699594dSDave Hansen if (is_journal_aborted(journal)) 5901ac27a0ecSDave Kleikamp return -EROFS; 5902ac27a0ecSDave Kleikamp 590317335dccSDmitry Monakhov /* Wait for all existing dio workers */ 590417335dccSDmitry Monakhov inode_dio_wait(inode); 590517335dccSDmitry Monakhov 59064c546592SDaeho Jeong /* 59074c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 59084c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 59094c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 59104c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 59114c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 59124c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 59134c546592SDaeho Jeong */ 59144c546592SDaeho Jeong if (val) { 59154c546592SDaeho Jeong down_write(&EXT4_I(inode)->i_mmap_sem); 59164c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 59174c546592SDaeho Jeong if (err < 0) { 59184c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 59194c546592SDaeho Jeong return err; 59204c546592SDaeho Jeong } 59214c546592SDaeho Jeong } 59224c546592SDaeho Jeong 5923bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 5924dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5925ac27a0ecSDave Kleikamp 5926ac27a0ecSDave Kleikamp /* 5927ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5928ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5929ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5930ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5931ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5932ac27a0ecSDave Kleikamp */ 5933ac27a0ecSDave Kleikamp 5934ac27a0ecSDave Kleikamp if (val) 593512e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59365872ddaaSYongqiang Yang else { 59374f879ca6SJan Kara err = jbd2_journal_flush(journal); 59384f879ca6SJan Kara if (err < 0) { 59394f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 5940bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 59414f879ca6SJan Kara return err; 59424f879ca6SJan Kara } 594312e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59445872ddaaSYongqiang Yang } 5945617ba13bSMingming Cao ext4_set_aops(inode); 5946ac27a0ecSDave Kleikamp 5947dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5948bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 5949c8585c6fSDaeho Jeong 59504c546592SDaeho Jeong if (val) 59514c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 5952ac27a0ecSDave Kleikamp 5953ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5954ac27a0ecSDave Kleikamp 59559924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5956ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5957ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5958ac27a0ecSDave Kleikamp 5959617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 59600390131bSFrank Mayhar ext4_handle_sync(handle); 5961617ba13bSMingming Cao ext4_journal_stop(handle); 5962617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5963ac27a0ecSDave Kleikamp 5964ac27a0ecSDave Kleikamp return err; 5965ac27a0ecSDave Kleikamp } 59662e9ee850SAneesh Kumar K.V 59672e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 59682e9ee850SAneesh Kumar K.V { 59692e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 59702e9ee850SAneesh Kumar K.V } 59712e9ee850SAneesh Kumar K.V 5972401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 59732e9ee850SAneesh Kumar K.V { 597411bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 5975c2ec175cSNick Piggin struct page *page = vmf->page; 59762e9ee850SAneesh Kumar K.V loff_t size; 59772e9ee850SAneesh Kumar K.V unsigned long len; 5978401b25aaSSouptick Joarder int err; 5979401b25aaSSouptick Joarder vm_fault_t ret; 59802e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5981496ad9aaSAl Viro struct inode *inode = file_inode(file); 59822e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 59839ea7df53SJan Kara handle_t *handle; 59849ea7df53SJan Kara get_block_t *get_block; 59859ea7df53SJan Kara int retries = 0; 59862e9ee850SAneesh Kumar K.V 598702b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 598802b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 598902b016caSTheodore Ts'o 59908e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5991041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 5992ea3d7209SJan Kara 5993ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 59947b4cc978SEric Biggers 5995401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 5996401b25aaSSouptick Joarder if (err) 59977b4cc978SEric Biggers goto out_ret; 59987b4cc978SEric Biggers 59999ea7df53SJan Kara /* Delalloc case is easy... */ 60009ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 60019ea7df53SJan Kara !ext4_should_journal_data(inode) && 60029ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 60039ea7df53SJan Kara do { 6004401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 60059ea7df53SJan Kara ext4_da_get_block_prep); 6006401b25aaSSouptick Joarder } while (err == -ENOSPC && 60079ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 60089ea7df53SJan Kara goto out_ret; 60092e9ee850SAneesh Kumar K.V } 60100e499890SDarrick J. Wong 60110e499890SDarrick J. Wong lock_page(page); 60129ea7df53SJan Kara size = i_size_read(inode); 60139ea7df53SJan Kara /* Page got truncated from under us? */ 60149ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 60159ea7df53SJan Kara unlock_page(page); 60169ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 60179ea7df53SJan Kara goto out; 60180e499890SDarrick J. Wong } 60192e9ee850SAneesh Kumar K.V 602009cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 602109cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 60222e9ee850SAneesh Kumar K.V else 602309cbfeafSKirill A. Shutemov len = PAGE_SIZE; 6024a827eaffSAneesh Kumar K.V /* 60259ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 60269ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 6027a827eaffSAneesh Kumar K.V */ 60282e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6029f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 6030f19d5870STao Ma 0, len, NULL, 6031a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 60329ea7df53SJan Kara /* Wait so that we don't change page under IO */ 60331d1d1a76SDarrick J. Wong wait_for_stable_page(page); 60349ea7df53SJan Kara ret = VM_FAULT_LOCKED; 60359ea7df53SJan Kara goto out; 60362e9ee850SAneesh Kumar K.V } 6037a827eaffSAneesh Kumar K.V } 6038a827eaffSAneesh Kumar K.V unlock_page(page); 60399ea7df53SJan Kara /* OK, we need to fill the hole... */ 60409ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6041705965bdSJan Kara get_block = ext4_get_block_unwritten; 60429ea7df53SJan Kara else 60439ea7df53SJan Kara get_block = ext4_get_block; 60449ea7df53SJan Kara retry_alloc: 60459924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 60469924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 60479ea7df53SJan Kara if (IS_ERR(handle)) { 6048c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 60499ea7df53SJan Kara goto out; 60509ea7df53SJan Kara } 6051401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 6052401b25aaSSouptick Joarder if (!err && ext4_should_journal_data(inode)) { 6053f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 605409cbfeafSKirill A. Shutemov PAGE_SIZE, NULL, do_journal_get_write_access)) { 60559ea7df53SJan Kara unlock_page(page); 60569ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6057fcbb5515SYongqiang Yang ext4_journal_stop(handle); 60589ea7df53SJan Kara goto out; 60599ea7df53SJan Kara } 60609ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 60619ea7df53SJan Kara } 60629ea7df53SJan Kara ext4_journal_stop(handle); 6063401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 60649ea7df53SJan Kara goto retry_alloc; 60659ea7df53SJan Kara out_ret: 6066401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 60679ea7df53SJan Kara out: 6068ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 60698e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 60702e9ee850SAneesh Kumar K.V return ret; 60712e9ee850SAneesh Kumar K.V } 6072ea3d7209SJan Kara 6073401b25aaSSouptick Joarder vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 6074ea3d7209SJan Kara { 607511bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file); 6076401b25aaSSouptick Joarder vm_fault_t ret; 6077ea3d7209SJan Kara 6078ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 6079401b25aaSSouptick Joarder ret = filemap_fault(vmf); 6080ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 6081ea3d7209SJan Kara 6082401b25aaSSouptick Joarder return ret; 6083ea3d7209SJan Kara } 6084