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)) 386d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 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; 39724676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 39824676da4STheodore Ts'o map->m_len)) { 399c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 400bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 40124676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 402bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 4036a797d27SDarrick J. Wong return -EFSCORRUPTED; 4046fd058f7STheodore Ts'o } 4056fd058f7STheodore Ts'o return 0; 4066fd058f7STheodore Ts'o } 4076fd058f7STheodore Ts'o 40853085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 40953085facSJan Kara ext4_lblk_t len) 41053085facSJan Kara { 41153085facSJan Kara int ret; 41253085facSJan Kara 41333b4cc25SEric Biggers if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 414a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 41553085facSJan Kara 41653085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 41753085facSJan Kara if (ret > 0) 41853085facSJan Kara ret = 0; 41953085facSJan Kara 42053085facSJan Kara return ret; 42153085facSJan Kara } 42253085facSJan Kara 423e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 424c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 425e29136f8STheodore Ts'o 426921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 427921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 428921f266bSDmitry Monakhov struct inode *inode, 429921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 430921f266bSDmitry Monakhov struct ext4_map_blocks *map, 431921f266bSDmitry Monakhov int flags) 432921f266bSDmitry Monakhov { 433921f266bSDmitry Monakhov int retval; 434921f266bSDmitry Monakhov 435921f266bSDmitry Monakhov map->m_flags = 0; 436921f266bSDmitry Monakhov /* 437921f266bSDmitry Monakhov * There is a race window that the result is not the same. 438921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 439921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 440921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 441921f266bSDmitry Monakhov * could be converted. 442921f266bSDmitry Monakhov */ 443c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 444921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4459e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 446921f266bSDmitry Monakhov } else { 4479e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 448921f266bSDmitry Monakhov } 449921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 450921f266bSDmitry Monakhov 451921f266bSDmitry Monakhov /* 452921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 453921f266bSDmitry Monakhov * tree. So the m_len might not equal. 454921f266bSDmitry Monakhov */ 455921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 456921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 457921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 458bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 459921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 460921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 461921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 462921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 463921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 464921f266bSDmitry Monakhov retval, flags); 465921f266bSDmitry Monakhov } 466921f266bSDmitry Monakhov } 467921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 468921f266bSDmitry Monakhov 46955138e0bSTheodore Ts'o /* 470e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4712b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 472f5ab0d1fSMingming Cao * 473f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 474f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 475f5ab0d1fSMingming Cao * mapped. 476f5ab0d1fSMingming Cao * 477e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 478e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 479f5ab0d1fSMingming Cao * based files 480f5ab0d1fSMingming Cao * 481facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 482facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 483facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 484f5ab0d1fSMingming Cao * 485f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 486facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 487facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 488f5ab0d1fSMingming Cao * 489f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 490f5ab0d1fSMingming Cao */ 491e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 492e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4930e855ac8SAneesh Kumar K.V { 494d100eef2SZheng Liu struct extent_status es; 4950e855ac8SAneesh Kumar K.V int retval; 496b8a86845SLukas Czerner int ret = 0; 497921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 498921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 499921f266bSDmitry Monakhov 500921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 501921f266bSDmitry Monakhov #endif 502f5ab0d1fSMingming Cao 503e35fd660STheodore Ts'o map->m_flags = 0; 504e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 505e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 506e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 507d100eef2SZheng Liu 508e861b5e9STheodore Ts'o /* 509e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 510e861b5e9STheodore Ts'o */ 511e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 512e861b5e9STheodore Ts'o map->m_len = INT_MAX; 513e861b5e9STheodore Ts'o 5144adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5154adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5166a797d27SDarrick J. Wong return -EFSCORRUPTED; 5174adb6ab3SKazuya Mio 518d100eef2SZheng Liu /* Lookup extent status tree firstly */ 519bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 520d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 521d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 522d100eef2SZheng Liu map->m_lblk - es.es_lblk; 523d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 524d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 525d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 526d100eef2SZheng Liu if (retval > map->m_len) 527d100eef2SZheng Liu retval = map->m_len; 528d100eef2SZheng Liu map->m_len = retval; 529d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 530facab4d9SJan Kara map->m_pblk = 0; 531facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 532facab4d9SJan Kara if (retval > map->m_len) 533facab4d9SJan Kara retval = map->m_len; 534facab4d9SJan Kara map->m_len = retval; 535d100eef2SZheng Liu retval = 0; 536d100eef2SZheng Liu } else { 5371e83bc81SArnd Bergmann BUG(); 538d100eef2SZheng Liu } 539921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 540921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 541921f266bSDmitry Monakhov &orig_map, flags); 542921f266bSDmitry Monakhov #endif 543d100eef2SZheng Liu goto found; 544d100eef2SZheng Liu } 545d100eef2SZheng Liu 5464df3d265SAneesh Kumar K.V /* 547b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 548b920c755STheodore Ts'o * file system block. 5494df3d265SAneesh Kumar K.V */ 550c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 55112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5529e52484cSEric Whitney retval = ext4_ext_map_blocks(handle, inode, map, 0); 5534df3d265SAneesh Kumar K.V } else { 5549e52484cSEric Whitney retval = ext4_ind_map_blocks(handle, inode, map, 0); 5550e855ac8SAneesh Kumar K.V } 556f7fec032SZheng Liu if (retval > 0) { 5573be78c73STheodore Ts'o unsigned int status; 558f7fec032SZheng Liu 55944fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 56044fb851dSZheng Liu ext4_warning(inode->i_sb, 56144fb851dSZheng Liu "ES len assertion failed for inode " 56244fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 56344fb851dSZheng Liu inode->i_ino, retval, map->m_len); 56444fb851dSZheng Liu WARN_ON(1); 565921f266bSDmitry Monakhov } 566921f266bSDmitry Monakhov 567f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 568f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 569f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 570d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 571ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 572f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 573f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 574f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 575f7fec032SZheng Liu map->m_len, map->m_pblk, status); 576f7fec032SZheng Liu if (ret < 0) 577f7fec032SZheng Liu retval = ret; 578f7fec032SZheng Liu } 5794df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 580f5ab0d1fSMingming Cao 581d100eef2SZheng Liu found: 582e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 583b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5846fd058f7STheodore Ts'o if (ret != 0) 5856fd058f7STheodore Ts'o return ret; 5866fd058f7STheodore Ts'o } 5876fd058f7STheodore Ts'o 588f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 589c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5904df3d265SAneesh Kumar K.V return retval; 5914df3d265SAneesh Kumar K.V 5924df3d265SAneesh Kumar K.V /* 593f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 594f5ab0d1fSMingming Cao * 595f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 596df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 597f5ab0d1fSMingming Cao * with buffer head unmapped. 598f5ab0d1fSMingming Cao */ 599e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 600b8a86845SLukas Czerner /* 601b8a86845SLukas Czerner * If we need to convert extent to unwritten 602b8a86845SLukas Czerner * we continue and do the actual work in 603b8a86845SLukas Czerner * ext4_ext_map_blocks() 604b8a86845SLukas Czerner */ 605b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 606f5ab0d1fSMingming Cao return retval; 607f5ab0d1fSMingming Cao 608f5ab0d1fSMingming Cao /* 609a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 610a25a4e1aSZheng Liu * it will be set again. 6112a8964d6SAneesh Kumar K.V */ 612a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6132a8964d6SAneesh Kumar K.V 6142a8964d6SAneesh Kumar K.V /* 615556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 616f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 617d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 618f5ab0d1fSMingming Cao * with create == 1 flag. 6194df3d265SAneesh Kumar K.V */ 620c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 621d2a17637SMingming Cao 622d2a17637SMingming Cao /* 6234df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6244df3d265SAneesh Kumar K.V * could have changed the inode type in between 6254df3d265SAneesh Kumar K.V */ 62612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 627e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6280e855ac8SAneesh Kumar K.V } else { 629e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 630267e4db9SAneesh Kumar K.V 631e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 632267e4db9SAneesh Kumar K.V /* 633267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 634267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 635267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 636267e4db9SAneesh Kumar K.V */ 63719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 638267e4db9SAneesh Kumar K.V } 6392ac3b6e0STheodore Ts'o 640d2a17637SMingming Cao /* 6412ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6425f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6435f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6445f634d06SAneesh Kumar K.V * reserve space here. 645d2a17637SMingming Cao */ 6465f634d06SAneesh Kumar K.V if ((retval > 0) && 6471296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6485f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6495f634d06SAneesh Kumar K.V } 650d2a17637SMingming Cao 651f7fec032SZheng Liu if (retval > 0) { 6523be78c73STheodore Ts'o unsigned int status; 653f7fec032SZheng Liu 65444fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 65544fb851dSZheng Liu ext4_warning(inode->i_sb, 65644fb851dSZheng Liu "ES len assertion failed for inode " 65744fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 65844fb851dSZheng Liu inode->i_ino, retval, map->m_len); 65944fb851dSZheng Liu WARN_ON(1); 660921f266bSDmitry Monakhov } 661921f266bSDmitry Monakhov 662adb23551SZheng Liu /* 663c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 664c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6659b623df6SJan Kara * use them before they are really zeroed. We also have to 6669b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6679b623df6SJan Kara * overwrite zeros with stale data from block device. 668c86d8db3SJan Kara */ 669c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 670c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 671c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 672c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 673c86d8db3SJan Kara map->m_pblk, map->m_len); 674c86d8db3SJan Kara if (ret) { 675c86d8db3SJan Kara retval = ret; 676c86d8db3SJan Kara goto out_sem; 677c86d8db3SJan Kara } 678c86d8db3SJan Kara } 679c86d8db3SJan Kara 680c86d8db3SJan Kara /* 681adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 682adb23551SZheng Liu * extent status tree. 683adb23551SZheng Liu */ 684adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 685bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 686adb23551SZheng Liu if (ext4_es_is_written(&es)) 687c86d8db3SJan Kara goto out_sem; 688adb23551SZheng Liu } 689f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 690f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 691f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 692d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 693ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 694f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 695f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 696f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 697f7fec032SZheng Liu map->m_pblk, status); 698c86d8db3SJan Kara if (ret < 0) { 69951865fdaSZheng Liu retval = ret; 700c86d8db3SJan Kara goto out_sem; 701c86d8db3SJan Kara } 70251865fdaSZheng Liu } 7035356f261SAditya Kali 704c86d8db3SJan Kara out_sem: 7050e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 706e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 707b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7086fd058f7STheodore Ts'o if (ret != 0) 7096fd058f7STheodore Ts'o return ret; 71006bd3c36SJan Kara 71106bd3c36SJan Kara /* 71206bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 71306bd3c36SJan Kara * visible after transaction commit must be on transaction's 71406bd3c36SJan Kara * ordered data list. 71506bd3c36SJan Kara */ 71606bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 71706bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 71806bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 71902749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 72006bd3c36SJan Kara ext4_should_order_data(inode)) { 72173131fbbSRoss Zwisler loff_t start_byte = 72273131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 72373131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 72473131fbbSRoss Zwisler 725ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 72673131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 72773131fbbSRoss Zwisler start_byte, length); 728ee0876bcSJan Kara else 72973131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 73073131fbbSRoss Zwisler start_byte, length); 73106bd3c36SJan Kara if (ret) 73206bd3c36SJan Kara return ret; 73306bd3c36SJan Kara } 7346fd058f7STheodore Ts'o } 7350e855ac8SAneesh Kumar K.V return retval; 7360e855ac8SAneesh Kumar K.V } 7370e855ac8SAneesh Kumar K.V 738ed8ad838SJan Kara /* 739ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 740ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 741ed8ad838SJan Kara */ 742ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 743ed8ad838SJan Kara { 744ed8ad838SJan Kara unsigned long old_state; 745ed8ad838SJan Kara unsigned long new_state; 746ed8ad838SJan Kara 747ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 748ed8ad838SJan Kara 749ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 750ed8ad838SJan Kara if (!bh->b_page) { 751ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 752ed8ad838SJan Kara return; 753ed8ad838SJan Kara } 754ed8ad838SJan Kara /* 755ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 756ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 757ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 758ed8ad838SJan Kara */ 759ed8ad838SJan Kara do { 760ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 761ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 762ed8ad838SJan Kara } while (unlikely( 763ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 764ed8ad838SJan Kara } 765ed8ad838SJan Kara 7662ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7672ed88685STheodore Ts'o struct buffer_head *bh, int flags) 768ac27a0ecSDave Kleikamp { 7692ed88685STheodore Ts'o struct ext4_map_blocks map; 770efe70c29SJan Kara int ret = 0; 771ac27a0ecSDave Kleikamp 77246c7f254STao Ma if (ext4_has_inline_data(inode)) 77346c7f254STao Ma return -ERANGE; 77446c7f254STao Ma 7752ed88685STheodore Ts'o map.m_lblk = iblock; 7762ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7772ed88685STheodore Ts'o 778efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 779efe70c29SJan Kara flags); 780ac27a0ecSDave Kleikamp if (ret > 0) { 7812ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 782ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7832ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 784ac27a0ecSDave Kleikamp ret = 0; 785547edce3SRoss Zwisler } else if (ret == 0) { 786547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 787547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 788ac27a0ecSDave Kleikamp } 789ac27a0ecSDave Kleikamp return ret; 790ac27a0ecSDave Kleikamp } 791ac27a0ecSDave Kleikamp 7922ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7932ed88685STheodore Ts'o struct buffer_head *bh, int create) 7942ed88685STheodore Ts'o { 7952ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7962ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7972ed88685STheodore Ts'o } 7982ed88685STheodore Ts'o 799ac27a0ecSDave Kleikamp /* 800705965bdSJan Kara * Get block function used when preparing for buffered write if we require 801705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 802705965bdSJan Kara * will be converted to written after the IO is complete. 803705965bdSJan Kara */ 804705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 805705965bdSJan Kara struct buffer_head *bh_result, int create) 806705965bdSJan Kara { 807705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 808705965bdSJan Kara inode->i_ino, create); 809705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 810705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 811705965bdSJan Kara } 812705965bdSJan Kara 813efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 814efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 815efe70c29SJan Kara 816e84dfbe2SJan Kara /* 817ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 818ac27a0ecSDave Kleikamp */ 819617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 820c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 821ac27a0ecSDave Kleikamp { 8222ed88685STheodore Ts'o struct ext4_map_blocks map; 8232ed88685STheodore Ts'o struct buffer_head *bh; 824c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 82510560082STheodore Ts'o int err; 826ac27a0ecSDave Kleikamp 827ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 828ac27a0ecSDave Kleikamp 8292ed88685STheodore Ts'o map.m_lblk = block; 8302ed88685STheodore Ts'o map.m_len = 1; 831c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8322ed88685STheodore Ts'o 83310560082STheodore Ts'o if (err == 0) 83410560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8352ed88685STheodore Ts'o if (err < 0) 83610560082STheodore Ts'o return ERR_PTR(err); 8372ed88685STheodore Ts'o 8382ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 83910560082STheodore Ts'o if (unlikely(!bh)) 84010560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8412ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 842ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 843ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 844ac27a0ecSDave Kleikamp 845ac27a0ecSDave Kleikamp /* 846ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 847ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 848ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 849617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 850ac27a0ecSDave Kleikamp * problem. 851ac27a0ecSDave Kleikamp */ 852ac27a0ecSDave Kleikamp lock_buffer(bh); 853ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 85410560082STheodore Ts'o err = ext4_journal_get_create_access(handle, bh); 85510560082STheodore Ts'o if (unlikely(err)) { 85610560082STheodore Ts'o unlock_buffer(bh); 85710560082STheodore Ts'o goto errout; 85810560082STheodore Ts'o } 85910560082STheodore Ts'o if (!buffer_uptodate(bh)) { 860ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 861ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 862ac27a0ecSDave Kleikamp } 863ac27a0ecSDave Kleikamp unlock_buffer(bh); 8640390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8650390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 86610560082STheodore Ts'o if (unlikely(err)) 86710560082STheodore Ts'o goto errout; 86810560082STheodore Ts'o } else 869ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 870ac27a0ecSDave Kleikamp return bh; 87110560082STheodore Ts'o errout: 87210560082STheodore Ts'o brelse(bh); 87310560082STheodore Ts'o return ERR_PTR(err); 874ac27a0ecSDave Kleikamp } 875ac27a0ecSDave Kleikamp 876617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 877c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 878ac27a0ecSDave Kleikamp { 879ac27a0ecSDave Kleikamp struct buffer_head *bh; 880ac27a0ecSDave Kleikamp 881c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 8821c215028STheodore Ts'o if (IS_ERR(bh)) 883ac27a0ecSDave Kleikamp return bh; 8847963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 885ac27a0ecSDave Kleikamp return bh; 886dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh); 887ac27a0ecSDave Kleikamp wait_on_buffer(bh); 888ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 889ac27a0ecSDave Kleikamp return bh; 890ac27a0ecSDave Kleikamp put_bh(bh); 8911c215028STheodore Ts'o return ERR_PTR(-EIO); 892ac27a0ecSDave Kleikamp } 893ac27a0ecSDave Kleikamp 8949699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 8959699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 8969699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 8979699d4f9STahsin Erdogan { 8989699d4f9STahsin Erdogan int i, err; 8999699d4f9STahsin Erdogan 9009699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9019699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 9029699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9039699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9049699d4f9STahsin Erdogan bh_count = i; 9059699d4f9STahsin Erdogan goto out_brelse; 9069699d4f9STahsin Erdogan } 9079699d4f9STahsin Erdogan } 9089699d4f9STahsin Erdogan 9099699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9109699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9117963e5acSZhangXiaoxu if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9129699d4f9STahsin Erdogan ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, 9139699d4f9STahsin Erdogan &bhs[i]); 9149699d4f9STahsin Erdogan 9159699d4f9STahsin Erdogan if (!wait) 9169699d4f9STahsin Erdogan return 0; 9179699d4f9STahsin Erdogan 9189699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9199699d4f9STahsin Erdogan if (bhs[i]) 9209699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9219699d4f9STahsin Erdogan 9229699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9239699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9249699d4f9STahsin Erdogan err = -EIO; 9259699d4f9STahsin Erdogan goto out_brelse; 9269699d4f9STahsin Erdogan } 9279699d4f9STahsin Erdogan } 9289699d4f9STahsin Erdogan return 0; 9299699d4f9STahsin Erdogan 9309699d4f9STahsin Erdogan out_brelse: 9319699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9329699d4f9STahsin Erdogan brelse(bhs[i]); 9339699d4f9STahsin Erdogan bhs[i] = NULL; 9349699d4f9STahsin Erdogan } 9359699d4f9STahsin Erdogan return err; 9369699d4f9STahsin Erdogan } 9379699d4f9STahsin Erdogan 938f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 939ac27a0ecSDave Kleikamp struct buffer_head *head, 940ac27a0ecSDave Kleikamp unsigned from, 941ac27a0ecSDave Kleikamp unsigned to, 942ac27a0ecSDave Kleikamp int *partial, 943ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 944ac27a0ecSDave Kleikamp struct buffer_head *bh)) 945ac27a0ecSDave Kleikamp { 946ac27a0ecSDave Kleikamp struct buffer_head *bh; 947ac27a0ecSDave Kleikamp unsigned block_start, block_end; 948ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 949ac27a0ecSDave Kleikamp int err, ret = 0; 950ac27a0ecSDave Kleikamp struct buffer_head *next; 951ac27a0ecSDave Kleikamp 952ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 953ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 954de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 955ac27a0ecSDave Kleikamp next = bh->b_this_page; 956ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 957ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 958ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 959ac27a0ecSDave Kleikamp *partial = 1; 960ac27a0ecSDave Kleikamp continue; 961ac27a0ecSDave Kleikamp } 962ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 963ac27a0ecSDave Kleikamp if (!ret) 964ac27a0ecSDave Kleikamp ret = err; 965ac27a0ecSDave Kleikamp } 966ac27a0ecSDave Kleikamp return ret; 967ac27a0ecSDave Kleikamp } 968ac27a0ecSDave Kleikamp 969ac27a0ecSDave Kleikamp /* 970ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 971ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 972617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 973dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 974ac27a0ecSDave Kleikamp * prepare_write() is the right place. 975ac27a0ecSDave Kleikamp * 97636ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 97736ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 97836ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 97936ade451SJan Kara * because the caller may be PF_MEMALLOC. 980ac27a0ecSDave Kleikamp * 981617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 982ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 983ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 984ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 985ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 986ac27a0ecSDave Kleikamp * violation. 987ac27a0ecSDave Kleikamp * 988dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 989ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 990ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 991ac27a0ecSDave Kleikamp * write. 992ac27a0ecSDave Kleikamp */ 993f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 994ac27a0ecSDave Kleikamp struct buffer_head *bh) 995ac27a0ecSDave Kleikamp { 99656d35a4cSJan Kara int dirty = buffer_dirty(bh); 99756d35a4cSJan Kara int ret; 99856d35a4cSJan Kara 999ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1000ac27a0ecSDave Kleikamp return 0; 100156d35a4cSJan Kara /* 1002ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 100356d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 100456d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1005ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 100656d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 100756d35a4cSJan Kara * ever write the buffer. 100856d35a4cSJan Kara */ 100956d35a4cSJan Kara if (dirty) 101056d35a4cSJan Kara clear_buffer_dirty(bh); 10115d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 101256d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 101356d35a4cSJan Kara if (!ret && dirty) 101456d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 101556d35a4cSJan Kara return ret; 1016ac27a0ecSDave Kleikamp } 1017ac27a0ecSDave Kleikamp 1018643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10192058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10202058f83aSMichael Halcrow get_block_t *get_block) 10212058f83aSMichael Halcrow { 102209cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10232058f83aSMichael Halcrow unsigned to = from + len; 10242058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10252058f83aSMichael Halcrow unsigned block_start, block_end; 10262058f83aSMichael Halcrow sector_t block; 10272058f83aSMichael Halcrow int err = 0; 10282058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10292058f83aSMichael Halcrow unsigned bbits; 10300b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10310b578f35SChandan Rajendra int nr_wait = 0; 10320b578f35SChandan Rajendra int i; 10332058f83aSMichael Halcrow 10342058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 103509cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 103609cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10372058f83aSMichael Halcrow BUG_ON(from > to); 10382058f83aSMichael Halcrow 10392058f83aSMichael Halcrow if (!page_has_buffers(page)) 10402058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10412058f83aSMichael Halcrow head = page_buffers(page); 10422058f83aSMichael Halcrow bbits = ilog2(blocksize); 104309cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10442058f83aSMichael Halcrow 10452058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10462058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10472058f83aSMichael Halcrow block_end = block_start + blocksize; 10482058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10492058f83aSMichael Halcrow if (PageUptodate(page)) { 10502058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10512058f83aSMichael Halcrow set_buffer_uptodate(bh); 10522058f83aSMichael Halcrow } 10532058f83aSMichael Halcrow continue; 10542058f83aSMichael Halcrow } 10552058f83aSMichael Halcrow if (buffer_new(bh)) 10562058f83aSMichael Halcrow clear_buffer_new(bh); 10572058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10582058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10592058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10602058f83aSMichael Halcrow if (err) 10612058f83aSMichael Halcrow break; 10622058f83aSMichael Halcrow if (buffer_new(bh)) { 10632058f83aSMichael Halcrow if (PageUptodate(page)) { 10642058f83aSMichael Halcrow clear_buffer_new(bh); 10652058f83aSMichael Halcrow set_buffer_uptodate(bh); 10662058f83aSMichael Halcrow mark_buffer_dirty(bh); 10672058f83aSMichael Halcrow continue; 10682058f83aSMichael Halcrow } 10692058f83aSMichael Halcrow if (block_end > to || block_start < from) 10702058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10712058f83aSMichael Halcrow block_start, from); 10722058f83aSMichael Halcrow continue; 10732058f83aSMichael Halcrow } 10742058f83aSMichael Halcrow } 10752058f83aSMichael Halcrow if (PageUptodate(page)) { 10762058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10772058f83aSMichael Halcrow set_buffer_uptodate(bh); 10782058f83aSMichael Halcrow continue; 10792058f83aSMichael Halcrow } 10802058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10812058f83aSMichael Halcrow !buffer_unwritten(bh) && 10822058f83aSMichael Halcrow (block_start < from || block_end > to)) { 1083dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 10840b578f35SChandan Rajendra wait[nr_wait++] = bh; 10852058f83aSMichael Halcrow } 10862058f83aSMichael Halcrow } 10872058f83aSMichael Halcrow /* 10882058f83aSMichael Halcrow * If we issued read requests, let them complete. 10892058f83aSMichael Halcrow */ 10900b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10910b578f35SChandan Rajendra wait_on_buffer(wait[i]); 10920b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 10932058f83aSMichael Halcrow err = -EIO; 10942058f83aSMichael Halcrow } 10957e0785fcSChandan Rajendra if (unlikely(err)) { 10962058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 10970b578f35SChandan Rajendra } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) { 10980b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10990b578f35SChandan Rajendra int err2; 11000b578f35SChandan Rajendra 11010b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 11020b578f35SChandan Rajendra bh_offset(wait[i])); 11030b578f35SChandan Rajendra if (err2) { 11040b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11050b578f35SChandan Rajendra err = err2; 11060b578f35SChandan Rajendra } 11070b578f35SChandan Rajendra } 11087e0785fcSChandan Rajendra } 11097e0785fcSChandan Rajendra 11102058f83aSMichael Halcrow return err; 11112058f83aSMichael Halcrow } 11122058f83aSMichael Halcrow #endif 11132058f83aSMichael Halcrow 1114bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1115bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1116bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1117ac27a0ecSDave Kleikamp { 1118bfc1af65SNick Piggin struct inode *inode = mapping->host; 11191938a150SAneesh Kumar K.V int ret, needed_blocks; 1120ac27a0ecSDave Kleikamp handle_t *handle; 1121ac27a0ecSDave Kleikamp int retries = 0; 1122bfc1af65SNick Piggin struct page *page; 1123bfc1af65SNick Piggin pgoff_t index; 1124bfc1af65SNick Piggin unsigned from, to; 1125bfc1af65SNick Piggin 11260db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11270db1ff22STheodore Ts'o return -EIO; 11280db1ff22STheodore Ts'o 11299bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11301938a150SAneesh Kumar K.V /* 11311938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11321938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11331938a150SAneesh Kumar K.V */ 11341938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 113509cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 113609cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1137bfc1af65SNick Piggin to = from + len; 1138ac27a0ecSDave Kleikamp 1139f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1140f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1141f19d5870STao Ma flags, pagep); 1142f19d5870STao Ma if (ret < 0) 114347564bfbSTheodore Ts'o return ret; 114447564bfbSTheodore Ts'o if (ret == 1) 114547564bfbSTheodore Ts'o return 0; 1146f19d5870STao Ma } 1147f19d5870STao Ma 114847564bfbSTheodore Ts'o /* 114947564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 115047564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 115147564bfbSTheodore Ts'o * is being written back. So grab it first before we start 115247564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 115347564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 115447564bfbSTheodore Ts'o */ 115547564bfbSTheodore Ts'o retry_grab: 115654566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 115747564bfbSTheodore Ts'o if (!page) 115847564bfbSTheodore Ts'o return -ENOMEM; 115947564bfbSTheodore Ts'o unlock_page(page); 116047564bfbSTheodore Ts'o 116147564bfbSTheodore Ts'o retry_journal: 11629924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1163ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 116409cbfeafSKirill A. Shutemov put_page(page); 116547564bfbSTheodore Ts'o return PTR_ERR(handle); 1166cf108bcaSJan Kara } 1167f19d5870STao Ma 116847564bfbSTheodore Ts'o lock_page(page); 116947564bfbSTheodore Ts'o if (page->mapping != mapping) { 117047564bfbSTheodore Ts'o /* The page got truncated from under us */ 117147564bfbSTheodore Ts'o unlock_page(page); 117209cbfeafSKirill A. Shutemov put_page(page); 1173cf108bcaSJan Kara ext4_journal_stop(handle); 117447564bfbSTheodore Ts'o goto retry_grab; 1175cf108bcaSJan Kara } 11767afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11777afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1178cf108bcaSJan Kara 1179643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11802058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11812058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1182705965bdSJan Kara ext4_get_block_unwritten); 11832058f83aSMichael Halcrow else 11842058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 11852058f83aSMichael Halcrow ext4_get_block); 11862058f83aSMichael Halcrow #else 1187744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1188705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1189705965bdSJan Kara ext4_get_block_unwritten); 1190744692dcSJiaying Zhang else 11916e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 11922058f83aSMichael Halcrow #endif 1193bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1194f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1195f19d5870STao Ma from, to, NULL, 1196f19d5870STao Ma do_journal_get_write_access); 1197b46be050SAndrey Savochkin } 1198bfc1af65SNick Piggin 1199bfc1af65SNick Piggin if (ret) { 1200c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1201c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1202c93d8f88SEric Biggers 1203bfc1af65SNick Piggin unlock_page(page); 1204ae4d5372SAneesh Kumar K.V /* 12056e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1206ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1207ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12081938a150SAneesh Kumar K.V * 12091938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12101938a150SAneesh Kumar K.V * truncate finishes 1211ae4d5372SAneesh Kumar K.V */ 1212c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12131938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12141938a150SAneesh Kumar K.V 12151938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1216c93d8f88SEric Biggers if (extended) { 1217b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12181938a150SAneesh Kumar K.V /* 1219ffacfa7aSJan Kara * If truncate failed early the inode might 12201938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12211938a150SAneesh Kumar K.V * make sure the inode is removed from the 12221938a150SAneesh Kumar K.V * orphan list in that case. 12231938a150SAneesh Kumar K.V */ 12241938a150SAneesh Kumar K.V if (inode->i_nlink) 12251938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12261938a150SAneesh Kumar K.V } 1227bfc1af65SNick Piggin 122847564bfbSTheodore Ts'o if (ret == -ENOSPC && 122947564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 123047564bfbSTheodore Ts'o goto retry_journal; 123109cbfeafSKirill A. Shutemov put_page(page); 123247564bfbSTheodore Ts'o return ret; 123347564bfbSTheodore Ts'o } 123447564bfbSTheodore Ts'o *pagep = page; 1235ac27a0ecSDave Kleikamp return ret; 1236ac27a0ecSDave Kleikamp } 1237ac27a0ecSDave Kleikamp 1238bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1239bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1240ac27a0ecSDave Kleikamp { 124113fca323STheodore Ts'o int ret; 1242ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1243ac27a0ecSDave Kleikamp return 0; 1244ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 124513fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 124613fca323STheodore Ts'o clear_buffer_meta(bh); 124713fca323STheodore Ts'o clear_buffer_prio(bh); 124813fca323STheodore Ts'o return ret; 1249ac27a0ecSDave Kleikamp } 1250ac27a0ecSDave Kleikamp 1251eed4333fSZheng Liu /* 1252eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1253eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1254eed4333fSZheng Liu * 1255eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1256eed4333fSZheng Liu * buffers are managed internally. 1257eed4333fSZheng Liu */ 1258eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1259f8514083SAneesh Kumar K.V struct address_space *mapping, 1260f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1261f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1262f8514083SAneesh Kumar K.V { 1263f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1264eed4333fSZheng Liu struct inode *inode = mapping->host; 12650572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1266eed4333fSZheng Liu int ret = 0, ret2; 1267eed4333fSZheng Liu int i_size_changed = 0; 1268362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1269c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1270eed4333fSZheng Liu 1271eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1272362eca70STheodore Ts'o if (inline_data) { 127342c832deSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 1274f19d5870STao Ma copied, page); 1275eb5efbcbSTheodore Ts'o if (ret < 0) { 1276eb5efbcbSTheodore Ts'o unlock_page(page); 1277eb5efbcbSTheodore Ts'o put_page(page); 127842c832deSTheodore Ts'o goto errout; 1279eb5efbcbSTheodore Ts'o } 128042c832deSTheodore Ts'o copied = ret; 128142c832deSTheodore Ts'o } else 1282f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1283f19d5870STao Ma len, copied, page, fsdata); 1284f8514083SAneesh Kumar K.V /* 12854631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1286f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1287c93d8f88SEric Biggers * 1288c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1289c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1290f8514083SAneesh Kumar K.V */ 1291c93d8f88SEric Biggers if (!verity) 12924631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1293f8514083SAneesh Kumar K.V unlock_page(page); 129409cbfeafSKirill A. Shutemov put_page(page); 1295f8514083SAneesh Kumar K.V 1296c93d8f88SEric Biggers if (old_size < pos && !verity) 12970572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1298f8514083SAneesh Kumar K.V /* 1299f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1300f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1301f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1302f8514083SAneesh Kumar K.V * filesystems. 1303f8514083SAneesh Kumar K.V */ 1304362eca70STheodore Ts'o if (i_size_changed || inline_data) 1305*4209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 1306f8514083SAneesh Kumar K.V 1307c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1308f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1309f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1310f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1311f8514083SAneesh Kumar K.V */ 1312f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 131374d553aaSTheodore Ts'o errout: 1314617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1315ac27a0ecSDave Kleikamp if (!ret) 1316ac27a0ecSDave Kleikamp ret = ret2; 1317bfc1af65SNick Piggin 1318c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1319b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1320f8514083SAneesh Kumar K.V /* 1321ffacfa7aSJan Kara * If truncate failed early the inode might still be 1322f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1323f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1324f8514083SAneesh Kumar K.V */ 1325f8514083SAneesh Kumar K.V if (inode->i_nlink) 1326f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1327f8514083SAneesh Kumar K.V } 1328f8514083SAneesh Kumar K.V 1329bfc1af65SNick Piggin return ret ? ret : copied; 1330ac27a0ecSDave Kleikamp } 1331ac27a0ecSDave Kleikamp 1332b90197b6STheodore Ts'o /* 1333b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1334b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1335b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1336b90197b6STheodore Ts'o */ 13373b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 13383b136499SJan Kara struct page *page, 13393b136499SJan Kara unsigned from, unsigned to) 1340b90197b6STheodore Ts'o { 1341b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1342b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1343b90197b6STheodore Ts'o 1344b90197b6STheodore Ts'o bh = head = page_buffers(page); 1345b90197b6STheodore Ts'o do { 1346b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1347b90197b6STheodore Ts'o if (buffer_new(bh)) { 1348b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1349b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1350b90197b6STheodore Ts'o unsigned start, size; 1351b90197b6STheodore Ts'o 1352b90197b6STheodore Ts'o start = max(from, block_start); 1353b90197b6STheodore Ts'o size = min(to, block_end) - start; 1354b90197b6STheodore Ts'o 1355b90197b6STheodore Ts'o zero_user(page, start, size); 13563b136499SJan Kara write_end_fn(handle, bh); 1357b90197b6STheodore Ts'o } 1358b90197b6STheodore Ts'o clear_buffer_new(bh); 1359b90197b6STheodore Ts'o } 1360b90197b6STheodore Ts'o } 1361b90197b6STheodore Ts'o block_start = block_end; 1362b90197b6STheodore Ts'o bh = bh->b_this_page; 1363b90197b6STheodore Ts'o } while (bh != head); 1364b90197b6STheodore Ts'o } 1365b90197b6STheodore Ts'o 1366bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1367bfc1af65SNick Piggin struct address_space *mapping, 1368bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1369bfc1af65SNick Piggin struct page *page, void *fsdata) 1370ac27a0ecSDave Kleikamp { 1371617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1372bfc1af65SNick Piggin struct inode *inode = mapping->host; 13730572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1374ac27a0ecSDave Kleikamp int ret = 0, ret2; 1375ac27a0ecSDave Kleikamp int partial = 0; 1376bfc1af65SNick Piggin unsigned from, to; 13774631dbf6SDmitry Monakhov int size_changed = 0; 1378362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1379c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1380ac27a0ecSDave Kleikamp 13819bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 138209cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1383bfc1af65SNick Piggin to = from + len; 1384bfc1af65SNick Piggin 1385441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1386441c8508SCurt Wohlgemuth 1387362eca70STheodore Ts'o if (inline_data) { 1388eb5efbcbSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 13893fdcfb66STao Ma copied, page); 1390eb5efbcbSTheodore Ts'o if (ret < 0) { 1391eb5efbcbSTheodore Ts'o unlock_page(page); 1392eb5efbcbSTheodore Ts'o put_page(page); 1393eb5efbcbSTheodore Ts'o goto errout; 1394eb5efbcbSTheodore Ts'o } 1395eb5efbcbSTheodore Ts'o copied = ret; 1396eb5efbcbSTheodore Ts'o } else if (unlikely(copied < len) && !PageUptodate(page)) { 1397bfc1af65SNick Piggin copied = 0; 13983b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, from, to); 13993b136499SJan Kara } else { 14003b136499SJan Kara if (unlikely(copied < len)) 14013b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, 14023b136499SJan Kara from + copied, to); 1403f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 14043b136499SJan Kara from + copied, &partial, 14053b136499SJan Kara write_end_fn); 1406ac27a0ecSDave Kleikamp if (!partial) 1407ac27a0ecSDave Kleikamp SetPageUptodate(page); 14083fdcfb66STao Ma } 1409c93d8f88SEric Biggers if (!verity) 14104631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 141119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14122d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14134631dbf6SDmitry Monakhov unlock_page(page); 141409cbfeafSKirill A. Shutemov put_page(page); 14154631dbf6SDmitry Monakhov 1416c93d8f88SEric Biggers if (old_size < pos && !verity) 14170572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14180572639fSXiaoguang Wang 1419362eca70STheodore Ts'o if (size_changed || inline_data) { 1420617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1421ac27a0ecSDave Kleikamp if (!ret) 1422ac27a0ecSDave Kleikamp ret = ret2; 1423ac27a0ecSDave Kleikamp } 1424bfc1af65SNick Piggin 1425c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1426f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1427f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1428f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1429f8514083SAneesh Kumar K.V */ 1430f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1431f8514083SAneesh Kumar K.V 1432eb5efbcbSTheodore Ts'o errout: 1433617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1434ac27a0ecSDave Kleikamp if (!ret) 1435ac27a0ecSDave Kleikamp ret = ret2; 1436c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1437b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1438f8514083SAneesh Kumar K.V /* 1439ffacfa7aSJan Kara * If truncate failed early the inode might still be 1440f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1441f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1442f8514083SAneesh Kumar K.V */ 1443f8514083SAneesh Kumar K.V if (inode->i_nlink) 1444f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1445f8514083SAneesh Kumar K.V } 1446bfc1af65SNick Piggin 1447bfc1af65SNick Piggin return ret ? ret : copied; 1448ac27a0ecSDave Kleikamp } 1449d2a17637SMingming Cao 14509d0be502STheodore Ts'o /* 1451c27e43a1SEric Whitney * Reserve space for a single cluster 14529d0be502STheodore Ts'o */ 1453c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1454d2a17637SMingming Cao { 1455d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14560637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14575dd4056dSChristoph Hellwig int ret; 1458d2a17637SMingming Cao 145960e58e0fSMingming Cao /* 146072b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 146172b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 146272b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 146360e58e0fSMingming Cao */ 14647b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14655dd4056dSChristoph Hellwig if (ret) 14665dd4056dSChristoph Hellwig return ret; 146703179fe9STheodore Ts'o 146803179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 146971d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 147003179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147103179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1472d2a17637SMingming Cao return -ENOSPC; 1473d2a17637SMingming Cao } 14749d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1475c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14760637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147739bc680aSDmitry Monakhov 1478d2a17637SMingming Cao return 0; /* success */ 1479d2a17637SMingming Cao } 1480d2a17637SMingming Cao 1481f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1482d2a17637SMingming Cao { 1483d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14840637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1485d2a17637SMingming Cao 1486cd213226SMingming Cao if (!to_free) 1487cd213226SMingming Cao return; /* Nothing to release, exit */ 1488cd213226SMingming Cao 1489d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1490cd213226SMingming Cao 14915a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14920637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1493cd213226SMingming Cao /* 14940637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14950637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14960637c6f4STheodore Ts'o * function is called from invalidate page, it's 14970637c6f4STheodore Ts'o * harmless to return without any action. 1498cd213226SMingming Cao */ 14998de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 15000637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 15011084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 15020637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15030637c6f4STheodore Ts'o WARN_ON(1); 15040637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15050637c6f4STheodore Ts'o } 15060637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15070637c6f4STheodore Ts'o 150872b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 150957042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1510d2a17637SMingming Cao 1511d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 151260e58e0fSMingming Cao 15137b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1514d2a17637SMingming Cao } 1515d2a17637SMingming Cao 1516ac27a0ecSDave Kleikamp /* 151764769240SAlex Tomas * Delayed allocation stuff 151864769240SAlex Tomas */ 151964769240SAlex Tomas 15204e7ea81dSJan Kara struct mpage_da_data { 15214e7ea81dSJan Kara struct inode *inode; 15224e7ea81dSJan Kara struct writeback_control *wbc; 15236b523df4SJan Kara 15244e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15254e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15264e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 152764769240SAlex Tomas /* 15284e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15294e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15304e7ea81dSJan Kara * is delalloc or unwritten. 153164769240SAlex Tomas */ 15324e7ea81dSJan Kara struct ext4_map_blocks map; 15334e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1534dddbd6acSJan Kara unsigned int do_map:1; 15354e7ea81dSJan Kara }; 153664769240SAlex Tomas 15374e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15384e7ea81dSJan Kara bool invalidate) 1539c4a0c46eSAneesh Kumar K.V { 1540c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1541c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1542c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1543c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1544c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15454e7ea81dSJan Kara 15464e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15474e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15484e7ea81dSJan Kara return; 1549c4a0c46eSAneesh Kumar K.V 1550c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1551c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15524e7ea81dSJan Kara if (invalidate) { 15534e7ea81dSJan Kara ext4_lblk_t start, last; 155409cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 155509cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 155651865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15574e7ea81dSJan Kara } 155851865fdaSZheng Liu 155986679820SMel Gorman pagevec_init(&pvec); 1560c4a0c46eSAneesh Kumar K.V while (index <= end) { 1561397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1562c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1563c4a0c46eSAneesh Kumar K.V break; 1564c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1565c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15662b85a617SJan Kara 1567c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1568c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15694e7ea81dSJan Kara if (invalidate) { 15704e800c03Swangguang if (page_mapped(page)) 15714e800c03Swangguang clear_page_dirty_for_io(page); 157209cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1573c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15744e7ea81dSJan Kara } 1575c4a0c46eSAneesh Kumar K.V unlock_page(page); 1576c4a0c46eSAneesh Kumar K.V } 15779b1d0998SJan Kara pagevec_release(&pvec); 1578c4a0c46eSAneesh Kumar K.V } 1579c4a0c46eSAneesh Kumar K.V } 1580c4a0c46eSAneesh Kumar K.V 1581df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1582df22291fSAneesh Kumar K.V { 1583df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 158492b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1585f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 158692b97816STheodore Ts'o 158792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15885dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1589f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 159092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 159192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1592f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 159357042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 159492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1595f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 15967b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 159792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 159892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1599f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1600df22291fSAneesh Kumar K.V return; 1601df22291fSAneesh Kumar K.V } 1602df22291fSAneesh Kumar K.V 1603c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 160429fa89d0SAneesh Kumar K.V { 1605c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 160629fa89d0SAneesh Kumar K.V } 160729fa89d0SAneesh Kumar K.V 160864769240SAlex Tomas /* 16090b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16100b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16110b02f4c0SEric Whitney * count or making a pending reservation 16120b02f4c0SEric Whitney * where needed 16130b02f4c0SEric Whitney * 16140b02f4c0SEric Whitney * @inode - file containing the newly added block 16150b02f4c0SEric Whitney * @lblk - logical block to be added 16160b02f4c0SEric Whitney * 16170b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16180b02f4c0SEric Whitney */ 16190b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16200b02f4c0SEric Whitney { 16210b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16220b02f4c0SEric Whitney int ret; 16230b02f4c0SEric Whitney bool allocated = false; 16240b02f4c0SEric Whitney 16250b02f4c0SEric Whitney /* 16260b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16270b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16280b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16290b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16300b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16310b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16320b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16330b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16340b02f4c0SEric Whitney * extents status tree doesn't get a match. 16350b02f4c0SEric Whitney */ 16360b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16370b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16380b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16390b02f4c0SEric Whitney goto errout; 16400b02f4c0SEric Whitney } else { /* bigalloc */ 16410b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16420b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16430b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16440b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16450b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16460b02f4c0SEric Whitney if (ret < 0) 16470b02f4c0SEric Whitney goto errout; 16480b02f4c0SEric Whitney if (ret == 0) { 16490b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16500b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16510b02f4c0SEric Whitney goto errout; 16520b02f4c0SEric Whitney } else { 16530b02f4c0SEric Whitney allocated = true; 16540b02f4c0SEric Whitney } 16550b02f4c0SEric Whitney } else { 16560b02f4c0SEric Whitney allocated = true; 16570b02f4c0SEric Whitney } 16580b02f4c0SEric Whitney } 16590b02f4c0SEric Whitney } 16600b02f4c0SEric Whitney 16610b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16620b02f4c0SEric Whitney 16630b02f4c0SEric Whitney errout: 16640b02f4c0SEric Whitney return ret; 16650b02f4c0SEric Whitney } 16660b02f4c0SEric Whitney 16670b02f4c0SEric Whitney /* 16685356f261SAditya Kali * This function is grabs code from the very beginning of 16695356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16705356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16715356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16725356f261SAditya Kali */ 16735356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16745356f261SAditya Kali struct ext4_map_blocks *map, 16755356f261SAditya Kali struct buffer_head *bh) 16765356f261SAditya Kali { 1677d100eef2SZheng Liu struct extent_status es; 16785356f261SAditya Kali int retval; 16795356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1680921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1681921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1682921f266bSDmitry Monakhov 1683921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1684921f266bSDmitry Monakhov #endif 16855356f261SAditya Kali 16865356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16875356f261SAditya Kali invalid_block = ~0; 16885356f261SAditya Kali 16895356f261SAditya Kali map->m_flags = 0; 16905356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 16915356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 16925356f261SAditya Kali (unsigned long) map->m_lblk); 1693d100eef2SZheng Liu 1694d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1695bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1696d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1697d100eef2SZheng Liu retval = 0; 1698c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1699d100eef2SZheng Liu goto add_delayed; 1700d100eef2SZheng Liu } 1701d100eef2SZheng Liu 1702d100eef2SZheng Liu /* 1703d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1704d100eef2SZheng Liu * So we need to check it. 1705d100eef2SZheng Liu */ 1706d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1707d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1708d100eef2SZheng Liu set_buffer_new(bh); 1709d100eef2SZheng Liu set_buffer_delay(bh); 1710d100eef2SZheng Liu return 0; 1711d100eef2SZheng Liu } 1712d100eef2SZheng Liu 1713d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1714d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1715d100eef2SZheng Liu if (retval > map->m_len) 1716d100eef2SZheng Liu retval = map->m_len; 1717d100eef2SZheng Liu map->m_len = retval; 1718d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1719d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1720d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1721d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1722d100eef2SZheng Liu else 17231e83bc81SArnd Bergmann BUG(); 1724d100eef2SZheng Liu 1725921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1726921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1727921f266bSDmitry Monakhov #endif 1728d100eef2SZheng Liu return retval; 1729d100eef2SZheng Liu } 1730d100eef2SZheng Liu 17315356f261SAditya Kali /* 17325356f261SAditya Kali * Try to see if we can get the block without requesting a new 17335356f261SAditya Kali * file system block. 17345356f261SAditya Kali */ 1735c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1736cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17379c3569b5STao Ma retval = 0; 1738cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17392f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17405356f261SAditya Kali else 17412f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17425356f261SAditya Kali 1743d100eef2SZheng Liu add_delayed: 17445356f261SAditya Kali if (retval == 0) { 1745f7fec032SZheng Liu int ret; 1746ad431025SEric Whitney 17475356f261SAditya Kali /* 17485356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17495356f261SAditya Kali * is it OK? 17505356f261SAditya Kali */ 17515356f261SAditya Kali 17520b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17530b02f4c0SEric Whitney if (ret != 0) { 1754f7fec032SZheng Liu retval = ret; 175551865fdaSZheng Liu goto out_unlock; 1756f7fec032SZheng Liu } 175751865fdaSZheng Liu 17585356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17595356f261SAditya Kali set_buffer_new(bh); 17605356f261SAditya Kali set_buffer_delay(bh); 1761f7fec032SZheng Liu } else if (retval > 0) { 1762f7fec032SZheng Liu int ret; 17633be78c73STheodore Ts'o unsigned int status; 1764f7fec032SZheng Liu 176544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 176644fb851dSZheng Liu ext4_warning(inode->i_sb, 176744fb851dSZheng Liu "ES len assertion failed for inode " 176844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 176944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 177044fb851dSZheng Liu WARN_ON(1); 1771921f266bSDmitry Monakhov } 1772921f266bSDmitry Monakhov 1773f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1774f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1775f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1776f7fec032SZheng Liu map->m_pblk, status); 1777f7fec032SZheng Liu if (ret != 0) 1778f7fec032SZheng Liu retval = ret; 17795356f261SAditya Kali } 17805356f261SAditya Kali 17815356f261SAditya Kali out_unlock: 17825356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17835356f261SAditya Kali 17845356f261SAditya Kali return retval; 17855356f261SAditya Kali } 17865356f261SAditya Kali 17875356f261SAditya Kali /* 1788d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1789b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1790b920c755STheodore Ts'o * reserve space for a single block. 179129fa89d0SAneesh Kumar K.V * 179229fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 179329fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 179429fa89d0SAneesh Kumar K.V * 179529fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 179629fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 179729fa89d0SAneesh Kumar K.V * initialized properly. 179864769240SAlex Tomas */ 17999c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 18002ed88685STheodore Ts'o struct buffer_head *bh, int create) 180164769240SAlex Tomas { 18022ed88685STheodore Ts'o struct ext4_map_blocks map; 180364769240SAlex Tomas int ret = 0; 180464769240SAlex Tomas 180564769240SAlex Tomas BUG_ON(create == 0); 18062ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18072ed88685STheodore Ts'o 18082ed88685STheodore Ts'o map.m_lblk = iblock; 18092ed88685STheodore Ts'o map.m_len = 1; 181064769240SAlex Tomas 181164769240SAlex Tomas /* 181264769240SAlex Tomas * first, we need to know whether the block is allocated already 181364769240SAlex Tomas * preallocated blocks are unmapped but should treated 181464769240SAlex Tomas * the same as allocated blocks. 181564769240SAlex Tomas */ 18165356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18175356f261SAditya Kali if (ret <= 0) 18182ed88685STheodore Ts'o return ret; 181964769240SAlex Tomas 18202ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1821ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18222ed88685STheodore Ts'o 18232ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18242ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18252ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18262ed88685STheodore Ts'o * get_block multiple times when we write to the same 18272ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18282ed88685STheodore Ts'o * for partial write. 18292ed88685STheodore Ts'o */ 18302ed88685STheodore Ts'o set_buffer_new(bh); 1831c8205636STheodore Ts'o set_buffer_mapped(bh); 18322ed88685STheodore Ts'o } 18332ed88685STheodore Ts'o return 0; 183464769240SAlex Tomas } 183561628a3fSMingming Cao 183662e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 183762e086beSAneesh Kumar K.V { 183862e086beSAneesh Kumar K.V get_bh(bh); 183962e086beSAneesh Kumar K.V return 0; 184062e086beSAneesh Kumar K.V } 184162e086beSAneesh Kumar K.V 184262e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 184362e086beSAneesh Kumar K.V { 184462e086beSAneesh Kumar K.V put_bh(bh); 184562e086beSAneesh Kumar K.V return 0; 184662e086beSAneesh Kumar K.V } 184762e086beSAneesh Kumar K.V 184862e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 184962e086beSAneesh Kumar K.V unsigned int len) 185062e086beSAneesh Kumar K.V { 185162e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 185262e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18533fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 185462e086beSAneesh Kumar K.V handle_t *handle = NULL; 18553fdcfb66STao Ma int ret = 0, err = 0; 18563fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18573fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 185862e086beSAneesh Kumar K.V 1859cb20d518STheodore Ts'o ClearPageChecked(page); 18603fdcfb66STao Ma 18613fdcfb66STao Ma if (inline_data) { 18623fdcfb66STao Ma BUG_ON(page->index != 0); 18633fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18643fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18653fdcfb66STao Ma if (inode_bh == NULL) 18663fdcfb66STao Ma goto out; 18673fdcfb66STao Ma } else { 186862e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18693fdcfb66STao Ma if (!page_bufs) { 18703fdcfb66STao Ma BUG(); 18713fdcfb66STao Ma goto out; 18723fdcfb66STao Ma } 18733fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18743fdcfb66STao Ma NULL, bget_one); 18753fdcfb66STao Ma } 1876bdf96838STheodore Ts'o /* 1877bdf96838STheodore Ts'o * We need to release the page lock before we start the 1878bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1879bdf96838STheodore Ts'o * out from under us. 1880bdf96838STheodore Ts'o */ 1881bdf96838STheodore Ts'o get_page(page); 188262e086beSAneesh Kumar K.V unlock_page(page); 188362e086beSAneesh Kumar K.V 18849924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18859924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 188662e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 188762e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1888bdf96838STheodore Ts'o put_page(page); 1889bdf96838STheodore Ts'o goto out_no_pagelock; 1890bdf96838STheodore Ts'o } 1891bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1892bdf96838STheodore Ts'o 1893bdf96838STheodore Ts'o lock_page(page); 1894bdf96838STheodore Ts'o put_page(page); 1895bdf96838STheodore Ts'o if (page->mapping != mapping) { 1896bdf96838STheodore Ts'o /* The page got truncated from under us */ 1897bdf96838STheodore Ts'o ext4_journal_stop(handle); 1898bdf96838STheodore Ts'o ret = 0; 189962e086beSAneesh Kumar K.V goto out; 190062e086beSAneesh Kumar K.V } 190162e086beSAneesh Kumar K.V 19023fdcfb66STao Ma if (inline_data) { 1903362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19043fdcfb66STao Ma } else { 1905f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190662e086beSAneesh Kumar K.V do_journal_get_write_access); 190762e086beSAneesh Kumar K.V 1908f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190962e086beSAneesh Kumar K.V write_end_fn); 19103fdcfb66STao Ma } 191162e086beSAneesh Kumar K.V if (ret == 0) 191262e086beSAneesh Kumar K.V ret = err; 19132d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 191462e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 191562e086beSAneesh Kumar K.V if (!ret) 191662e086beSAneesh Kumar K.V ret = err; 191762e086beSAneesh Kumar K.V 19183fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19198c9367fdSTheodore Ts'o ext4_walk_page_buffers(NULL, page_bufs, 0, len, 19203fdcfb66STao Ma NULL, bput_one); 192119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 192262e086beSAneesh Kumar K.V out: 1923bdf96838STheodore Ts'o unlock_page(page); 1924bdf96838STheodore Ts'o out_no_pagelock: 19253fdcfb66STao Ma brelse(inode_bh); 192662e086beSAneesh Kumar K.V return ret; 192762e086beSAneesh Kumar K.V } 192862e086beSAneesh Kumar K.V 192961628a3fSMingming Cao /* 193043ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 193143ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 193243ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 193343ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193443ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 193543ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 193643ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 193743ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 193843ce1d23SAneesh Kumar K.V * 1939b920c755STheodore Ts'o * This function can get called via... 194020970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1941b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1942f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1943b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194443ce1d23SAneesh Kumar K.V * 194543ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 194643ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 194743ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 194843ce1d23SAneesh Kumar K.V * truncate(f, 1024); 194943ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 195043ce1d23SAneesh Kumar K.V * a[0] = 'a'; 195143ce1d23SAneesh Kumar K.V * truncate(f, 4096); 195243ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 195390802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195443ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 195543ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 195643ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 195743ce1d23SAneesh Kumar K.V * buffer_heads mapped. 195843ce1d23SAneesh Kumar K.V * 195943ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 196043ce1d23SAneesh Kumar K.V * unwritten in the page. 196143ce1d23SAneesh Kumar K.V * 196243ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 196343ce1d23SAneesh Kumar K.V * 196443ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 196543ce1d23SAneesh Kumar K.V * ext4_writepage() 196643ce1d23SAneesh Kumar K.V * 196743ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 196843ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 196961628a3fSMingming Cao */ 197043ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 197164769240SAlex Tomas struct writeback_control *wbc) 197264769240SAlex Tomas { 1973f8bec370SJan Kara int ret = 0; 197461628a3fSMingming Cao loff_t size; 1975498e5f24STheodore Ts'o unsigned int len; 1976744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 197761628a3fSMingming Cao struct inode *inode = page->mapping->host; 197836ade451SJan Kara struct ext4_io_submit io_submit; 19791c8349a1SNamjae Jeon bool keep_towrite = false; 198064769240SAlex Tomas 19810db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 1982c2a559bcSyangerkun inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 19830db1ff22STheodore Ts'o unlock_page(page); 19840db1ff22STheodore Ts'o return -EIO; 19850db1ff22STheodore Ts'o } 19860db1ff22STheodore Ts'o 1987a9c667f8SLukas Czerner trace_ext4_writepage(page); 198861628a3fSMingming Cao size = i_size_read(inode); 1989c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 1990c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 199109cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 199261628a3fSMingming Cao else 199309cbfeafSKirill A. Shutemov len = PAGE_SIZE; 199461628a3fSMingming Cao 1995f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 199664769240SAlex Tomas /* 1997fe386132SJan Kara * We cannot do block allocation or other extent handling in this 1998fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 1999fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2000fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2001fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 2002cccd147aSTheodore Ts'o * 2003cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2004cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2005cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2006cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2007cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2008cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2009cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2010cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2011cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 201264769240SAlex Tomas */ 2013f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2014c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 201561628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2016cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 201709cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2018fe386132SJan Kara /* 2019fe386132SJan Kara * For memory cleaning there's no point in writing only 2020fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2021fe386132SJan Kara * from direct reclaim. 2022fe386132SJan Kara */ 2023fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2024fe386132SJan Kara == PF_MEMALLOC); 202561628a3fSMingming Cao unlock_page(page); 202661628a3fSMingming Cao return 0; 202761628a3fSMingming Cao } 20281c8349a1SNamjae Jeon keep_towrite = true; 2029f0e6c985SAneesh Kumar K.V } 203064769240SAlex Tomas 2031cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 203243ce1d23SAneesh Kumar K.V /* 203343ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 203443ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 203543ce1d23SAneesh Kumar K.V */ 20363f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 203743ce1d23SAneesh Kumar K.V 203897a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 203997a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 204097a851edSJan Kara if (!io_submit.io_end) { 204197a851edSJan Kara redirty_page_for_writepage(wbc, page); 204297a851edSJan Kara unlock_page(page); 204397a851edSJan Kara return -ENOMEM; 204497a851edSJan Kara } 20451c8349a1SNamjae Jeon ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite); 204636ade451SJan Kara ext4_io_submit(&io_submit); 204797a851edSJan Kara /* Drop io_end reference we got from init */ 204897a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 204964769240SAlex Tomas return ret; 205064769240SAlex Tomas } 205164769240SAlex Tomas 20525f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20535f1132b2SJan Kara { 20545f1132b2SJan Kara int len; 2055a056bdaaSJan Kara loff_t size; 20565f1132b2SJan Kara int err; 20575f1132b2SJan Kara 20585f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2059a056bdaaSJan Kara clear_page_dirty_for_io(page); 2060a056bdaaSJan Kara /* 2061a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2062a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2063a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2064a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2065a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2066a056bdaaSJan Kara * written to again until we release page lock. So only after 2067a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2068a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2069a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2070a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2071a056bdaaSJan Kara * after page tables are updated. 2072a056bdaaSJan Kara */ 2073a056bdaaSJan Kara size = i_size_read(mpd->inode); 2074c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2075c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 207609cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20775f1132b2SJan Kara else 207809cbfeafSKirill A. Shutemov len = PAGE_SIZE; 20791c8349a1SNamjae Jeon err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false); 20805f1132b2SJan Kara if (!err) 20815f1132b2SJan Kara mpd->wbc->nr_to_write--; 20825f1132b2SJan Kara mpd->first_page++; 20835f1132b2SJan Kara 20845f1132b2SJan Kara return err; 20855f1132b2SJan Kara } 20865f1132b2SJan Kara 20874e7ea81dSJan Kara #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay)) 20884e7ea81dSJan Kara 208961628a3fSMingming Cao /* 2090fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2091fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2092fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 209361628a3fSMingming Cao */ 2094fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2095525f4ed8SMingming Cao 2096525f4ed8SMingming Cao /* 20974e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 20984e7ea81dSJan Kara * 20994e7ea81dSJan Kara * @mpd - extent of blocks 21004e7ea81dSJan Kara * @lblk - logical number of the block in the file 210109930042SJan Kara * @bh - buffer head we want to add to the extent 21024e7ea81dSJan Kara * 210309930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 210409930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 210509930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 210609930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 210709930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 210809930042SJan Kara * added. 21094e7ea81dSJan Kara */ 211009930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 211109930042SJan Kara struct buffer_head *bh) 21124e7ea81dSJan Kara { 21134e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21144e7ea81dSJan Kara 211509930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 211609930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 211709930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 211809930042SJan Kara /* So far no extent to map => we write the buffer right away */ 211909930042SJan Kara if (map->m_len == 0) 212009930042SJan Kara return true; 212109930042SJan Kara return false; 212209930042SJan Kara } 21234e7ea81dSJan Kara 21244e7ea81dSJan Kara /* First block in the extent? */ 21254e7ea81dSJan Kara if (map->m_len == 0) { 2126dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2127dddbd6acSJan Kara if (!mpd->do_map) 2128dddbd6acSJan Kara return false; 21294e7ea81dSJan Kara map->m_lblk = lblk; 21304e7ea81dSJan Kara map->m_len = 1; 213109930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 213209930042SJan Kara return true; 21334e7ea81dSJan Kara } 21344e7ea81dSJan Kara 213509930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 213609930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 213709930042SJan Kara return false; 213809930042SJan Kara 21394e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21404e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 214109930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21424e7ea81dSJan Kara map->m_len++; 214309930042SJan Kara return true; 21444e7ea81dSJan Kara } 214509930042SJan Kara return false; 21464e7ea81dSJan Kara } 21474e7ea81dSJan Kara 21485f1132b2SJan Kara /* 21495f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21505f1132b2SJan Kara * 21515f1132b2SJan Kara * @mpd - extent of blocks for mapping 21525f1132b2SJan Kara * @head - the first buffer in the page 21535f1132b2SJan Kara * @bh - buffer we should start processing from 21545f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21555f1132b2SJan Kara * 21565f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21575f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21585f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21595f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21605f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21615f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21625f1132b2SJan Kara * < 0 in case of error during IO submission. 21635f1132b2SJan Kara */ 21645f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21654e7ea81dSJan Kara struct buffer_head *head, 21664e7ea81dSJan Kara struct buffer_head *bh, 21674e7ea81dSJan Kara ext4_lblk_t lblk) 21684e7ea81dSJan Kara { 21694e7ea81dSJan Kara struct inode *inode = mpd->inode; 21705f1132b2SJan Kara int err; 217193407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21724e7ea81dSJan Kara >> inode->i_blkbits; 21734e7ea81dSJan Kara 2174c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2175c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2176c93d8f88SEric Biggers 21774e7ea81dSJan Kara do { 21784e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21794e7ea81dSJan Kara 218009930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21814e7ea81dSJan Kara /* Found extent to map? */ 21824e7ea81dSJan Kara if (mpd->map.m_len) 21835f1132b2SJan Kara return 0; 2184dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2185dddbd6acSJan Kara if (!mpd->do_map) 2186dddbd6acSJan Kara return 0; 218709930042SJan Kara /* Everything mapped so far and we hit EOF */ 21885f1132b2SJan Kara break; 21894e7ea81dSJan Kara } 21904e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 21915f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 21925f1132b2SJan Kara if (mpd->map.m_len == 0) { 21935f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 21945f1132b2SJan Kara if (err < 0) 21954e7ea81dSJan Kara return err; 21964e7ea81dSJan Kara } 21975f1132b2SJan Kara return lblk < blocks; 21985f1132b2SJan Kara } 21994e7ea81dSJan Kara 22004e7ea81dSJan Kara /* 22012943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 22022943fdbcSRitesh Harjani * may submit fully mapped page for IO 22032943fdbcSRitesh Harjani * 22042943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22052943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22062943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22072943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22082943fdbcSRitesh Harjani * mapping or not. 22092943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22102943fdbcSRitesh Harjani * state according to new extent state. 22112943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22122943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22132943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22142943fdbcSRitesh Harjani */ 22152943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22162943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22172943fdbcSRitesh Harjani bool *map_bh) 22182943fdbcSRitesh Harjani { 22192943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22202943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22212943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22222943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22232943fdbcSRitesh Harjani int err = 0; 2224c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2225c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2226c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22272943fdbcSRitesh Harjani 22282943fdbcSRitesh Harjani bh = head = page_buffers(page); 22292943fdbcSRitesh Harjani do { 22302943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22312943fdbcSRitesh Harjani continue; 22322943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22332943fdbcSRitesh Harjani /* 22342943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22352943fdbcSRitesh Harjani * Find next buffer in the page to map. 22362943fdbcSRitesh Harjani */ 22372943fdbcSRitesh Harjani mpd->map.m_len = 0; 22382943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2239c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2240c8cc8816SRitesh Harjani io_end_size = 0; 22412943fdbcSRitesh Harjani 22422943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22432943fdbcSRitesh Harjani if (err > 0) 22442943fdbcSRitesh Harjani err = 0; 2245c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2246c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22474d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22484d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22494d06bfb9SRitesh Harjani goto out; 22504d06bfb9SRitesh Harjani } 2251c8cc8816SRitesh Harjani io_end_vec->offset = mpd->map.m_lblk << blkbits; 2252c8cc8816SRitesh Harjani } 22532943fdbcSRitesh Harjani *map_bh = true; 22542943fdbcSRitesh Harjani goto out; 22552943fdbcSRitesh Harjani } 22562943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22572943fdbcSRitesh Harjani clear_buffer_delay(bh); 22582943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22592943fdbcSRitesh Harjani } 22602943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2261c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22622943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2263c8cc8816SRitesh Harjani 2264c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2265c8cc8816SRitesh Harjani io_end_size = 0; 22662943fdbcSRitesh Harjani *map_bh = false; 22672943fdbcSRitesh Harjani out: 22682943fdbcSRitesh Harjani *m_lblk = lblk; 22692943fdbcSRitesh Harjani *m_pblk = pblock; 22702943fdbcSRitesh Harjani return err; 22712943fdbcSRitesh Harjani } 22722943fdbcSRitesh Harjani 22732943fdbcSRitesh Harjani /* 22744e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22754e7ea81dSJan Kara * submit fully mapped pages for IO 22764e7ea81dSJan Kara * 22774e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22784e7ea81dSJan Kara * 22794e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22804e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22814e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2282556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 22834e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 22844e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 22854e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 22864e7ea81dSJan Kara */ 22874e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 22884e7ea81dSJan Kara { 22894e7ea81dSJan Kara struct pagevec pvec; 22904e7ea81dSJan Kara int nr_pages, i; 22914e7ea81dSJan Kara struct inode *inode = mpd->inode; 229209cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 22934e7ea81dSJan Kara pgoff_t start, end; 22944e7ea81dSJan Kara ext4_lblk_t lblk; 22952943fdbcSRitesh Harjani ext4_fsblk_t pblock; 22964e7ea81dSJan Kara int err; 22972943fdbcSRitesh Harjani bool map_bh = false; 22984e7ea81dSJan Kara 22994e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 23004e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 23014e7ea81dSJan Kara lblk = start << bpp_bits; 23024e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23034e7ea81dSJan Kara 230486679820SMel Gorman pagevec_init(&pvec); 23054e7ea81dSJan Kara while (start <= end) { 23062b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2307397162ffSJan Kara &start, end); 23084e7ea81dSJan Kara if (nr_pages == 0) 23094e7ea81dSJan Kara break; 23104e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23114e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23124e7ea81dSJan Kara 23132943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23142943fdbcSRitesh Harjani &map_bh); 23154e7ea81dSJan Kara /* 23162943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23172943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23182943fdbcSRitesh Harjani * So we return to call further extent mapping. 23194e7ea81dSJan Kara */ 232039c0ae16SJason Yan if (err < 0 || map_bh) 23212943fdbcSRitesh Harjani goto out; 23224e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23234e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23242943fdbcSRitesh Harjani if (err < 0) 23252943fdbcSRitesh Harjani goto out; 23264e7ea81dSJan Kara } 23274e7ea81dSJan Kara pagevec_release(&pvec); 23284e7ea81dSJan Kara } 23294e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23304e7ea81dSJan Kara mpd->map.m_len = 0; 23314e7ea81dSJan Kara mpd->map.m_flags = 0; 23324e7ea81dSJan Kara return 0; 23332943fdbcSRitesh Harjani out: 23342943fdbcSRitesh Harjani pagevec_release(&pvec); 23352943fdbcSRitesh Harjani return err; 23364e7ea81dSJan Kara } 23374e7ea81dSJan Kara 23384e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23394e7ea81dSJan Kara { 23404e7ea81dSJan Kara struct inode *inode = mpd->inode; 23414e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23424e7ea81dSJan Kara int get_blocks_flags; 2343090f32eeSLukas Czerner int err, dioread_nolock; 23444e7ea81dSJan Kara 23454e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23464e7ea81dSJan Kara /* 23474e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2348556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23494e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23504e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23514e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23524e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23534e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23544e7ea81dSJan Kara * possible. 23554e7ea81dSJan Kara * 2356754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2357754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2358754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2359754cfed6STheodore Ts'o * the data was copied into the page cache. 23604e7ea81dSJan Kara */ 23614e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2362ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2363ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2364090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2365090f32eeSLukas Czerner if (dioread_nolock) 23664e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23674e7ea81dSJan Kara if (map->m_flags & (1 << BH_Delay)) 23684e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23694e7ea81dSJan Kara 23704e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23714e7ea81dSJan Kara if (err < 0) 23724e7ea81dSJan Kara return err; 2373090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23746b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23756b523df4SJan Kara ext4_handle_valid(handle)) { 23766b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23776b523df4SJan Kara handle->h_rsv_handle = NULL; 23786b523df4SJan Kara } 23793613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23806b523df4SJan Kara } 23814e7ea81dSJan Kara 23824e7ea81dSJan Kara BUG_ON(map->m_len == 0); 23834e7ea81dSJan Kara return 0; 23844e7ea81dSJan Kara } 23854e7ea81dSJan Kara 23864e7ea81dSJan Kara /* 23874e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 23884e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 23894e7ea81dSJan Kara * 23904e7ea81dSJan Kara * @handle - handle for journal operations 23914e7ea81dSJan Kara * @mpd - extent to map 23927534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 23937534e854SJan Kara * is no hope of writing the data. The caller should discard 23947534e854SJan Kara * dirty pages to avoid infinite loops. 23954e7ea81dSJan Kara * 23964e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 23974e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 23984e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 23994e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 24004e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 24014e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 24024e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24034e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24044e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24054e7ea81dSJan Kara */ 24064e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2407cb530541STheodore Ts'o struct mpage_da_data *mpd, 2408cb530541STheodore Ts'o bool *give_up_on_write) 24094e7ea81dSJan Kara { 24104e7ea81dSJan Kara struct inode *inode = mpd->inode; 24114e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24124e7ea81dSJan Kara int err; 24134e7ea81dSJan Kara loff_t disksize; 24146603120eSDmitry Monakhov int progress = 0; 2415c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24164d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24174e7ea81dSJan Kara 24184d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24194d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24204d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2421c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 242227d7c4edSJan Kara do { 24234e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24244e7ea81dSJan Kara if (err < 0) { 24254e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24264e7ea81dSJan Kara 24270db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24280db1ff22STheodore Ts'o EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED) 2429cb530541STheodore Ts'o goto invalidate_dirty_pages; 24304e7ea81dSJan Kara /* 2431cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2432cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2433cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24344e7ea81dSJan Kara */ 2435cb530541STheodore Ts'o if ((err == -ENOMEM) || 24366603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24376603120eSDmitry Monakhov if (progress) 24386603120eSDmitry Monakhov goto update_disksize; 2439cb530541STheodore Ts'o return err; 24406603120eSDmitry Monakhov } 24414e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24424e7ea81dSJan Kara "Delayed block allocation failed for " 24434e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24444e7ea81dSJan Kara " max blocks %u with error %d", 24454e7ea81dSJan Kara inode->i_ino, 24464e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2447cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24484e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24494e7ea81dSJan Kara "This should not happen!! Data will " 24504e7ea81dSJan Kara "be lost\n"); 24514e7ea81dSJan Kara if (err == -ENOSPC) 24524e7ea81dSJan Kara ext4_print_free_blocks(inode); 2453cb530541STheodore Ts'o invalidate_dirty_pages: 2454cb530541STheodore Ts'o *give_up_on_write = true; 24554e7ea81dSJan Kara return err; 24564e7ea81dSJan Kara } 24576603120eSDmitry Monakhov progress = 1; 24584e7ea81dSJan Kara /* 24594e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24604e7ea81dSJan Kara * extent to map 24614e7ea81dSJan Kara */ 24624e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24634e7ea81dSJan Kara if (err < 0) 24646603120eSDmitry Monakhov goto update_disksize; 246527d7c4edSJan Kara } while (map->m_len); 24664e7ea81dSJan Kara 24676603120eSDmitry Monakhov update_disksize: 2468622cad13STheodore Ts'o /* 2469622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2470622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2471622cad13STheodore Ts'o */ 247209cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 247335df4299SQian Cai if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) { 24744e7ea81dSJan Kara int err2; 2475622cad13STheodore Ts'o loff_t i_size; 24764e7ea81dSJan Kara 2477622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2478622cad13STheodore Ts'o i_size = i_size_read(inode); 2479622cad13STheodore Ts'o if (disksize > i_size) 2480622cad13STheodore Ts'o disksize = i_size; 2481622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2482622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2483622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2484b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2485878520acSTheodore Ts'o if (err2) { 248654d3adbcSTheodore Ts'o ext4_error_err(inode->i_sb, -err2, 24874e7ea81dSJan Kara "Failed to mark inode %lu dirty", 24884e7ea81dSJan Kara inode->i_ino); 2489878520acSTheodore Ts'o } 24904e7ea81dSJan Kara if (!err) 24914e7ea81dSJan Kara err = err2; 24924e7ea81dSJan Kara } 24934e7ea81dSJan Kara return err; 24944e7ea81dSJan Kara } 24954e7ea81dSJan Kara 24964e7ea81dSJan Kara /* 2497fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 249820970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2499fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2500fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2501fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2502525f4ed8SMingming Cao */ 2503fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2504fffb2739SJan Kara { 2505fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2506525f4ed8SMingming Cao 2507fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2508fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2509525f4ed8SMingming Cao } 251061628a3fSMingming Cao 25118e48dcfbSTheodore Ts'o /* 25124e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25134e7ea81dSJan Kara * and underlying extent to map 25144e7ea81dSJan Kara * 25154e7ea81dSJan Kara * @mpd - where to look for pages 25164e7ea81dSJan Kara * 25174e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25184e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25194e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25204e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25214e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25224e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25234e7ea81dSJan Kara * 25244e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25254e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25264e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25274e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25288e48dcfbSTheodore Ts'o */ 25294e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25308e48dcfbSTheodore Ts'o { 25314e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25328e48dcfbSTheodore Ts'o struct pagevec pvec; 25334f01b02cSTheodore Ts'o unsigned int nr_pages; 2534aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25354e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25364e7ea81dSJan Kara pgoff_t end = mpd->last_page; 253710bbd235SMatthew Wilcox xa_mark_t tag; 25384e7ea81dSJan Kara int i, err = 0; 25394e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25404e7ea81dSJan Kara ext4_lblk_t lblk; 25414e7ea81dSJan Kara struct buffer_head *head; 25428e48dcfbSTheodore Ts'o 25434e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25445b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25455b41d924SEric Sandeen else 25465b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25475b41d924SEric Sandeen 254886679820SMel Gorman pagevec_init(&pvec); 25494e7ea81dSJan Kara mpd->map.m_len = 0; 25504e7ea81dSJan Kara mpd->next_page = index; 25514f01b02cSTheodore Ts'o while (index <= end) { 2552dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 255367fd707fSJan Kara tag); 25548e48dcfbSTheodore Ts'o if (nr_pages == 0) 25554e7ea81dSJan Kara goto out; 25568e48dcfbSTheodore Ts'o 25578e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25588e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25598e48dcfbSTheodore Ts'o 25608e48dcfbSTheodore Ts'o /* 2561aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2562aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2563aeac589aSMing Lei * keep going because someone may be concurrently 2564aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2565aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2566aeac589aSMing Lei * of the old dirty pages. 2567aeac589aSMing Lei */ 2568aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2569aeac589aSMing Lei goto out; 2570aeac589aSMing Lei 25714e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25724e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25734e7ea81dSJan Kara goto out; 257478aaced3STheodore Ts'o 25758e48dcfbSTheodore Ts'o lock_page(page); 25768e48dcfbSTheodore Ts'o /* 25774e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25784e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25794e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25804e7ea81dSJan Kara * page is already under writeback and we are not doing 25814e7ea81dSJan Kara * a data integrity writeback, skip the page 25828e48dcfbSTheodore Ts'o */ 25834f01b02cSTheodore Ts'o if (!PageDirty(page) || 25844f01b02cSTheodore Ts'o (PageWriteback(page) && 25854e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 25864f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 25878e48dcfbSTheodore Ts'o unlock_page(page); 25888e48dcfbSTheodore Ts'o continue; 25898e48dcfbSTheodore Ts'o } 25908e48dcfbSTheodore Ts'o 25918e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 25928e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 25938e48dcfbSTheodore Ts'o 25944e7ea81dSJan Kara if (mpd->map.m_len == 0) 25958eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 25968eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2597f8bec370SJan Kara /* Add all dirty buffers to mpd */ 25984e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 259909cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 26008eb9e5ceSTheodore Ts'o head = page_buffers(page); 26015f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26025f1132b2SJan Kara if (err <= 0) 26034e7ea81dSJan Kara goto out; 26045f1132b2SJan Kara err = 0; 2605aeac589aSMing Lei left--; 26068e48dcfbSTheodore Ts'o } 26078e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26088e48dcfbSTheodore Ts'o cond_resched(); 26098e48dcfbSTheodore Ts'o } 26104f01b02cSTheodore Ts'o return 0; 26118eb9e5ceSTheodore Ts'o out: 26128eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26134e7ea81dSJan Kara return err; 26148e48dcfbSTheodore Ts'o } 26158e48dcfbSTheodore Ts'o 261620970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 261764769240SAlex Tomas struct writeback_control *wbc) 261864769240SAlex Tomas { 26194e7ea81dSJan Kara pgoff_t writeback_index = 0; 26204e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 262122208dedSAneesh Kumar K.V int range_whole = 0; 26224e7ea81dSJan Kara int cycled = 1; 262361628a3fSMingming Cao handle_t *handle = NULL; 2624df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26255e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26266b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26275e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26284e7ea81dSJan Kara bool done; 26291bce63d1SShaohua Li struct blk_plug plug; 2630cb530541STheodore Ts'o bool give_up_on_write = false; 263161628a3fSMingming Cao 26320db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26330db1ff22STheodore Ts'o return -EIO; 26340db1ff22STheodore Ts'o 2635bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 263620970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2637ba80b101STheodore Ts'o 263861628a3fSMingming Cao /* 263961628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 264061628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 264161628a3fSMingming Cao * because that could violate lock ordering on umount 264261628a3fSMingming Cao */ 2643a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2644bbf023c7SMing Lei goto out_writepages; 26452a21e37eSTheodore Ts'o 264620970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2647043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2648bbf023c7SMing Lei goto out_writepages; 264920970ba6STheodore Ts'o } 265020970ba6STheodore Ts'o 26512a21e37eSTheodore Ts'o /* 26522a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26532a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26542a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26551751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26562a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 265720970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26582a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26592a21e37eSTheodore Ts'o * the stack trace. 26602a21e37eSTheodore Ts'o */ 26610db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26620db1ff22STheodore Ts'o sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) { 2663bbf023c7SMing Lei ret = -EROFS; 2664bbf023c7SMing Lei goto out_writepages; 2665bbf023c7SMing Lei } 26662a21e37eSTheodore Ts'o 26674e7ea81dSJan Kara /* 26684e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26694e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26704e7ea81dSJan Kara * we'd better clear the inline data here. 26714e7ea81dSJan Kara */ 26724e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26734e7ea81dSJan Kara /* Just inode will be modified... */ 26744e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26754e7ea81dSJan Kara if (IS_ERR(handle)) { 26764e7ea81dSJan Kara ret = PTR_ERR(handle); 26774e7ea81dSJan Kara goto out_writepages; 26784e7ea81dSJan Kara } 26794e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26804e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26814e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26824e7ea81dSJan Kara ext4_journal_stop(handle); 26834e7ea81dSJan Kara } 26844e7ea81dSJan Kara 26854e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26864e343231Syangerkun /* 26874e343231Syangerkun * We may need to convert up to one extent per block in 26884e343231Syangerkun * the page and we may dirty the inode. 26894e343231Syangerkun */ 26904e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 26914e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 26924e343231Syangerkun } 26934e343231Syangerkun 269422208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 269522208dedSAneesh Kumar K.V range_whole = 1; 269661628a3fSMingming Cao 26972acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 26984e7ea81dSJan Kara writeback_index = mapping->writeback_index; 26994e7ea81dSJan Kara if (writeback_index) 27002acf2c26SAneesh Kumar K.V cycled = 0; 27014e7ea81dSJan Kara mpd.first_page = writeback_index; 27024e7ea81dSJan Kara mpd.last_page = -1; 27035b41d924SEric Sandeen } else { 270409cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 270509cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27065b41d924SEric Sandeen } 2707a1d6cc56SAneesh Kumar K.V 27084e7ea81dSJan Kara mpd.inode = inode; 27094e7ea81dSJan Kara mpd.wbc = wbc; 27104e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27112acf2c26SAneesh Kumar K.V retry: 27126e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27134e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27144e7ea81dSJan Kara done = false; 27151bce63d1SShaohua Li blk_start_plug(&plug); 2716dddbd6acSJan Kara 2717dddbd6acSJan Kara /* 2718dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2719dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2720dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2721dddbd6acSJan Kara * started. 2722dddbd6acSJan Kara */ 2723dddbd6acSJan Kara mpd.do_map = 0; 2724dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2725dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2726dddbd6acSJan Kara ret = -ENOMEM; 2727dddbd6acSJan Kara goto unplug; 2728dddbd6acSJan Kara } 2729dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2730a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2731a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2732dddbd6acSJan Kara /* Submit prepared bio */ 2733dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2734dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2735dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2736dddbd6acSJan Kara if (ret < 0) 2737dddbd6acSJan Kara goto unplug; 2738dddbd6acSJan Kara 27394e7ea81dSJan Kara while (!done && mpd.first_page <= mpd.last_page) { 27404e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27414e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27424e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27434e7ea81dSJan Kara ret = -ENOMEM; 27444e7ea81dSJan Kara break; 27454e7ea81dSJan Kara } 2746a1d6cc56SAneesh Kumar K.V 2747a1d6cc56SAneesh Kumar K.V /* 27484e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27494e7ea81dSJan Kara * must always write out whole page (makes a difference when 27504e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27514e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27524e7ea81dSJan Kara * not supported by delalloc. 2753a1d6cc56SAneesh Kumar K.V */ 2754a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2755525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2756a1d6cc56SAneesh Kumar K.V 275761628a3fSMingming Cao /* start a new transaction */ 27586b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27596b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 276061628a3fSMingming Cao if (IS_ERR(handle)) { 276161628a3fSMingming Cao ret = PTR_ERR(handle); 27621693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2763fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2764a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27654e7ea81dSJan Kara /* Release allocated io_end */ 27664e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2767dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27684e7ea81dSJan Kara break; 276961628a3fSMingming Cao } 2770dddbd6acSJan Kara mpd.do_map = 1; 2771f63e6005STheodore Ts'o 27724e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27734e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27744e7ea81dSJan Kara if (!ret) { 27754e7ea81dSJan Kara if (mpd.map.m_len) 2776cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2777cb530541STheodore Ts'o &give_up_on_write); 27784e7ea81dSJan Kara else { 2779f63e6005STheodore Ts'o /* 27804e7ea81dSJan Kara * We scanned the whole range (or exhausted 27814e7ea81dSJan Kara * nr_to_write), submitted what was mapped and 27824e7ea81dSJan Kara * didn't find anything needing mapping. We are 27834e7ea81dSJan Kara * done. 2784f63e6005STheodore Ts'o */ 27854e7ea81dSJan Kara done = true; 2786f63e6005STheodore Ts'o } 27874e7ea81dSJan Kara } 2788646caa9cSJan Kara /* 2789646caa9cSJan Kara * Caution: If the handle is synchronous, 2790646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2791646caa9cSJan Kara * to finish which may depend on writeback of pages to 2792646caa9cSJan Kara * complete or on page lock to be released. In that 2793646caa9cSJan Kara * case, we have to wait until after after we have 2794646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2795646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2796646caa9cSJan Kara * to be able to complete) before stopping the handle. 2797646caa9cSJan Kara */ 2798646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 279961628a3fSMingming Cao ext4_journal_stop(handle); 2800646caa9cSJan Kara handle = NULL; 2801dddbd6acSJan Kara mpd.do_map = 0; 2802646caa9cSJan Kara } 28034e7ea81dSJan Kara /* Unlock pages we didn't use */ 2804cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2805a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2806a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2807a297b2fcSXiaoguang Wang 2808646caa9cSJan Kara /* 2809646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2810646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2811646caa9cSJan Kara * we are still holding the transaction as we can 2812646caa9cSJan Kara * release the last reference to io_end which may end 2813646caa9cSJan Kara * up doing unwritten extent conversion. 2814646caa9cSJan Kara */ 2815646caa9cSJan Kara if (handle) { 2816646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2817646caa9cSJan Kara ext4_journal_stop(handle); 2818646caa9cSJan Kara } else 28194e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2820dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2821df22291fSAneesh Kumar K.V 28224e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28234e7ea81dSJan Kara /* 28244e7ea81dSJan Kara * Commit the transaction which would 282522208dedSAneesh Kumar K.V * free blocks released in the transaction 282622208dedSAneesh Kumar K.V * and try again 282722208dedSAneesh Kumar K.V */ 2828df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 282922208dedSAneesh Kumar K.V ret = 0; 28304e7ea81dSJan Kara continue; 28314e7ea81dSJan Kara } 28324e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28334e7ea81dSJan Kara if (ret) 283461628a3fSMingming Cao break; 283561628a3fSMingming Cao } 2836dddbd6acSJan Kara unplug: 28371bce63d1SShaohua Li blk_finish_plug(&plug); 28389c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28392acf2c26SAneesh Kumar K.V cycled = 1; 28404e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28414e7ea81dSJan Kara mpd.first_page = 0; 28422acf2c26SAneesh Kumar K.V goto retry; 28432acf2c26SAneesh Kumar K.V } 284461628a3fSMingming Cao 284522208dedSAneesh Kumar K.V /* Update index */ 284622208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 284722208dedSAneesh Kumar K.V /* 28484e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 284922208dedSAneesh Kumar K.V * mode will write it back later 285022208dedSAneesh Kumar K.V */ 28514e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2852a1d6cc56SAneesh Kumar K.V 285361628a3fSMingming Cao out_writepages: 285420970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28554e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2856bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 285761628a3fSMingming Cao return ret; 285864769240SAlex Tomas } 285964769240SAlex Tomas 28605f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28615f0663bbSDan Williams struct writeback_control *wbc) 28625f0663bbSDan Williams { 28635f0663bbSDan Williams int ret; 28645f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28655f0663bbSDan Williams struct inode *inode = mapping->host; 28665f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28675f0663bbSDan Williams 28685f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28695f0663bbSDan Williams return -EIO; 28705f0663bbSDan Williams 2871bbd55937SEric Biggers percpu_down_read(&sbi->s_writepages_rwsem); 28725f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28735f0663bbSDan Williams 28743f666c56SVivek Goyal ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc); 28755f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28765f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 2877bbd55937SEric Biggers percpu_up_read(&sbi->s_writepages_rwsem); 28785f0663bbSDan Williams return ret; 28795f0663bbSDan Williams } 28805f0663bbSDan Williams 288179f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 288279f0be8dSAneesh Kumar K.V { 28835c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 288479f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 288579f0be8dSAneesh Kumar K.V 288679f0be8dSAneesh Kumar K.V /* 288779f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 288879f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2889179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 289079f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 289179f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 289279f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 289379f0be8dSAneesh Kumar K.V */ 28945c1ff336SEric Whitney free_clusters = 28955c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28965c1ff336SEric Whitney dirty_clusters = 28975c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 289800d4e736STheodore Ts'o /* 289900d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 290000d4e736STheodore Ts'o */ 29015c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 290210ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 290300d4e736STheodore Ts'o 29045c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29055c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 290679f0be8dSAneesh Kumar K.V /* 2907c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2908c8afb446SEric Sandeen * or free blocks is less than watermark 290979f0be8dSAneesh Kumar K.V */ 291079f0be8dSAneesh Kumar K.V return 1; 291179f0be8dSAneesh Kumar K.V } 291279f0be8dSAneesh Kumar K.V return 0; 291379f0be8dSAneesh Kumar K.V } 291479f0be8dSAneesh Kumar K.V 29150ff8947fSEric Sandeen /* We always reserve for an inode update; the superblock could be there too */ 29160ff8947fSEric Sandeen static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) 29170ff8947fSEric Sandeen { 2918e2b911c5SDarrick J. Wong if (likely(ext4_has_feature_large_file(inode->i_sb))) 29190ff8947fSEric Sandeen return 1; 29200ff8947fSEric Sandeen 29210ff8947fSEric Sandeen if (pos + len <= 0x7fffffffULL) 29220ff8947fSEric Sandeen return 1; 29230ff8947fSEric Sandeen 29240ff8947fSEric Sandeen /* We might need to update the superblock to set LARGE_FILE */ 29250ff8947fSEric Sandeen return 2; 29260ff8947fSEric Sandeen } 29270ff8947fSEric Sandeen 292864769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 292964769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 293064769240SAlex Tomas struct page **pagep, void **fsdata) 293164769240SAlex Tomas { 293272b8ab9dSEric Sandeen int ret, retries = 0; 293364769240SAlex Tomas struct page *page; 293464769240SAlex Tomas pgoff_t index; 293564769240SAlex Tomas struct inode *inode = mapping->host; 293664769240SAlex Tomas handle_t *handle; 293764769240SAlex Tomas 29380db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29390db1ff22STheodore Ts'o return -EIO; 29400db1ff22STheodore Ts'o 294109cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 294279f0be8dSAneesh Kumar K.V 2943c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2944c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 294579f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 294679f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 294779f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 294879f0be8dSAneesh Kumar K.V } 294979f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29509bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29519c3569b5STao Ma 29529c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29539c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29549c3569b5STao Ma pos, len, flags, 29559c3569b5STao Ma pagep, fsdata); 29569c3569b5STao Ma if (ret < 0) 295747564bfbSTheodore Ts'o return ret; 295847564bfbSTheodore Ts'o if (ret == 1) 295947564bfbSTheodore Ts'o return 0; 29609c3569b5STao Ma } 29619c3569b5STao Ma 296247564bfbSTheodore Ts'o /* 296347564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 296447564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 296547564bfbSTheodore Ts'o * is being written back. So grab it first before we start 296647564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 296747564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 296847564bfbSTheodore Ts'o */ 296947564bfbSTheodore Ts'o retry_grab: 297047564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 297147564bfbSTheodore Ts'o if (!page) 297247564bfbSTheodore Ts'o return -ENOMEM; 297347564bfbSTheodore Ts'o unlock_page(page); 297447564bfbSTheodore Ts'o 297564769240SAlex Tomas /* 297664769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 297764769240SAlex Tomas * if there is delayed block allocation. But we still need 297864769240SAlex Tomas * to journalling the i_disksize update if writes to the end 297964769240SAlex Tomas * of file which has an already mapped buffer. 298064769240SAlex Tomas */ 298147564bfbSTheodore Ts'o retry_journal: 29820ff8947fSEric Sandeen handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 29830ff8947fSEric Sandeen ext4_da_write_credits(inode, pos, len)); 298464769240SAlex Tomas if (IS_ERR(handle)) { 298509cbfeafSKirill A. Shutemov put_page(page); 298647564bfbSTheodore Ts'o return PTR_ERR(handle); 298764769240SAlex Tomas } 298864769240SAlex Tomas 298947564bfbSTheodore Ts'o lock_page(page); 299047564bfbSTheodore Ts'o if (page->mapping != mapping) { 299147564bfbSTheodore Ts'o /* The page got truncated from under us */ 299247564bfbSTheodore Ts'o unlock_page(page); 299309cbfeafSKirill A. Shutemov put_page(page); 2994d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 299547564bfbSTheodore Ts'o goto retry_grab; 2996d5a0d4f7SEric Sandeen } 299747564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29987afe5aa5SDmitry Monakhov wait_for_stable_page(page); 299964769240SAlex Tomas 3000643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 30012058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 30022058f83aSMichael Halcrow ext4_da_get_block_prep); 30032058f83aSMichael Halcrow #else 30046e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 30052058f83aSMichael Halcrow #endif 300664769240SAlex Tomas if (ret < 0) { 300764769240SAlex Tomas unlock_page(page); 300864769240SAlex Tomas ext4_journal_stop(handle); 3009ae4d5372SAneesh Kumar K.V /* 3010ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3011ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3012ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3013ae4d5372SAneesh Kumar K.V */ 3014ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3015b9a4207dSJan Kara ext4_truncate_failed_write(inode); 301647564bfbSTheodore Ts'o 301747564bfbSTheodore Ts'o if (ret == -ENOSPC && 301847564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 301947564bfbSTheodore Ts'o goto retry_journal; 302047564bfbSTheodore Ts'o 302109cbfeafSKirill A. Shutemov put_page(page); 302247564bfbSTheodore Ts'o return ret; 302364769240SAlex Tomas } 302464769240SAlex Tomas 302547564bfbSTheodore Ts'o *pagep = page; 302664769240SAlex Tomas return ret; 302764769240SAlex Tomas } 302864769240SAlex Tomas 3029632eaeabSMingming Cao /* 3030632eaeabSMingming Cao * Check if we should update i_disksize 3031632eaeabSMingming Cao * when write to the end of file but not require block allocation 3032632eaeabSMingming Cao */ 3033632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3034632eaeabSMingming Cao unsigned long offset) 3035632eaeabSMingming Cao { 3036632eaeabSMingming Cao struct buffer_head *bh; 3037632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3038632eaeabSMingming Cao unsigned int idx; 3039632eaeabSMingming Cao int i; 3040632eaeabSMingming Cao 3041632eaeabSMingming Cao bh = page_buffers(page); 3042632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3043632eaeabSMingming Cao 3044632eaeabSMingming Cao for (i = 0; i < idx; i++) 3045632eaeabSMingming Cao bh = bh->b_this_page; 3046632eaeabSMingming Cao 304729fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3048632eaeabSMingming Cao return 0; 3049632eaeabSMingming Cao return 1; 3050632eaeabSMingming Cao } 3051632eaeabSMingming Cao 305264769240SAlex Tomas static int ext4_da_write_end(struct file *file, 305364769240SAlex Tomas struct address_space *mapping, 305464769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 305564769240SAlex Tomas struct page *page, void *fsdata) 305664769240SAlex Tomas { 305764769240SAlex Tomas struct inode *inode = mapping->host; 305864769240SAlex Tomas int ret = 0, ret2; 305964769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 306064769240SAlex Tomas loff_t new_i_size; 3061632eaeabSMingming Cao unsigned long start, end; 306279f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 306379f0be8dSAneesh Kumar K.V 306474d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 306574d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 306679f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3067632eaeabSMingming Cao 30689bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 306909cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 3070632eaeabSMingming Cao end = start + copied - 1; 307164769240SAlex Tomas 307264769240SAlex Tomas /* 307364769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 307464769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 307564769240SAlex Tomas * into that. 307664769240SAlex Tomas */ 307764769240SAlex Tomas new_i_size = pos + copied; 3078ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 30799c3569b5STao Ma if (ext4_has_inline_data(inode) || 30809c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 3081ee124d27SDmitry Monakhov ext4_update_i_disksize(inode, new_i_size); 3082cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3083cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3084cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3085cf17fea6SAneesh Kumar K.V */ 3086*4209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 3087632eaeabSMingming Cao } 3088632eaeabSMingming Cao } 30899c3569b5STao Ma 30909c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30919c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30929c3569b5STao Ma ext4_has_inline_data(inode)) 30939c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 30949c3569b5STao Ma page); 30959c3569b5STao Ma else 309664769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 309764769240SAlex Tomas page, fsdata); 30989c3569b5STao Ma 309964769240SAlex Tomas copied = ret2; 310064769240SAlex Tomas if (ret2 < 0) 310164769240SAlex Tomas ret = ret2; 310264769240SAlex Tomas ret2 = ext4_journal_stop(handle); 3103*4209ae12SHarshad Shirwadkar if (unlikely(ret2 && !ret)) 310464769240SAlex Tomas ret = ret2; 310564769240SAlex Tomas 310664769240SAlex Tomas return ret ? ret : copied; 310764769240SAlex Tomas } 310864769240SAlex Tomas 3109ccd2506bSTheodore Ts'o /* 3110ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3111ccd2506bSTheodore Ts'o */ 3112ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3113ccd2506bSTheodore Ts'o { 3114fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3115fb40ba0dSTheodore Ts'o 311671d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3117ccd2506bSTheodore Ts'o return 0; 3118ccd2506bSTheodore Ts'o 3119ccd2506bSTheodore Ts'o /* 3120ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3121ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3122ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3123ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3124ccd2506bSTheodore Ts'o * would require replicating code paths in: 3125ccd2506bSTheodore Ts'o * 312620970ba6STheodore Ts'o * ext4_writepages() -> 3127ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3128ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3129ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3130ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3131ccd2506bSTheodore Ts'o * 3132ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3133ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3134ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3135ccd2506bSTheodore Ts'o * doing I/O at all. 3136ccd2506bSTheodore Ts'o * 3137ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3138380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3139ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3140ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 314125985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3142ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3143ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3144ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3145ccd2506bSTheodore Ts'o * 3146ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3147ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3148ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3149ccd2506bSTheodore Ts'o */ 3150ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3151ccd2506bSTheodore Ts'o } 315264769240SAlex Tomas 315364769240SAlex Tomas /* 3154ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3155ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3156ac27a0ecSDave Kleikamp * 3157ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3158617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3159ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3160ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3161ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3162ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3163ac27a0ecSDave Kleikamp * 3164ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3165ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3166ac27a0ecSDave Kleikamp */ 3167617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3168ac27a0ecSDave Kleikamp { 3169ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3170ac27a0ecSDave Kleikamp journal_t *journal; 3171ac27a0ecSDave Kleikamp int err; 3172ac27a0ecSDave Kleikamp 317346c7f254STao Ma /* 317446c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 317546c7f254STao Ma */ 317646c7f254STao Ma if (ext4_has_inline_data(inode)) 317746c7f254STao Ma return 0; 317846c7f254STao Ma 317964769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 318064769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 318164769240SAlex Tomas /* 318264769240SAlex Tomas * With delalloc we want to sync the file 318364769240SAlex Tomas * so that we can make sure we allocate 318464769240SAlex Tomas * blocks for file 318564769240SAlex Tomas */ 318664769240SAlex Tomas filemap_write_and_wait(mapping); 318764769240SAlex Tomas } 318864769240SAlex Tomas 318919f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 319019f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3191ac27a0ecSDave Kleikamp /* 3192ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3193ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3194ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3195ac27a0ecSDave Kleikamp * do we expect this to happen. 3196ac27a0ecSDave Kleikamp * 3197ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3198ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3199ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3200ac27a0ecSDave Kleikamp * will.) 3201ac27a0ecSDave Kleikamp * 3202617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3203ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3204ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3205ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3206ac27a0ecSDave Kleikamp * everything they get. 3207ac27a0ecSDave Kleikamp */ 3208ac27a0ecSDave Kleikamp 320919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3210617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3211dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3212dab291afSMingming Cao err = jbd2_journal_flush(journal); 3213dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3214ac27a0ecSDave Kleikamp 3215ac27a0ecSDave Kleikamp if (err) 3216ac27a0ecSDave Kleikamp return 0; 3217ac27a0ecSDave Kleikamp } 3218ac27a0ecSDave Kleikamp 3219ac58e4fbSRitesh Harjani return iomap_bmap(mapping, block, &ext4_iomap_ops); 3220ac27a0ecSDave Kleikamp } 3221ac27a0ecSDave Kleikamp 3222617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3223ac27a0ecSDave Kleikamp { 322446c7f254STao Ma int ret = -EAGAIN; 322546c7f254STao Ma struct inode *inode = page->mapping->host; 322646c7f254STao Ma 32270562e0baSJiaying Zhang trace_ext4_readpage(page); 322846c7f254STao Ma 322946c7f254STao Ma if (ext4_has_inline_data(inode)) 323046c7f254STao Ma ret = ext4_readpage_inline(inode, page); 323146c7f254STao Ma 323246c7f254STao Ma if (ret == -EAGAIN) 3233ac22b46aSJens Axboe return ext4_mpage_readpages(page->mapping, NULL, page, 1, 3234ac22b46aSJens Axboe false); 323546c7f254STao Ma 323646c7f254STao Ma return ret; 3237ac27a0ecSDave Kleikamp } 3238ac27a0ecSDave Kleikamp 3239ac27a0ecSDave Kleikamp static int 3240617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3241ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3242ac27a0ecSDave Kleikamp { 324346c7f254STao Ma struct inode *inode = mapping->host; 324446c7f254STao Ma 324546c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 324646c7f254STao Ma if (ext4_has_inline_data(inode)) 324746c7f254STao Ma return 0; 324846c7f254STao Ma 3249ac22b46aSJens Axboe return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true); 3250ac27a0ecSDave Kleikamp } 3251ac27a0ecSDave Kleikamp 3252d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3253d47992f8SLukas Czerner unsigned int length) 3254ac27a0ecSDave Kleikamp { 3255ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32560562e0baSJiaying Zhang 32574520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32584520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32594520fb3cSJan Kara 3260ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32614520fb3cSJan Kara } 32624520fb3cSJan Kara 326353e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3264ca99fdd2SLukas Czerner unsigned int offset, 3265ca99fdd2SLukas Czerner unsigned int length) 32664520fb3cSJan Kara { 32674520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32684520fb3cSJan Kara 3269ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32704520fb3cSJan Kara 3271744692dcSJiaying Zhang /* 3272ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3273ac27a0ecSDave Kleikamp */ 327409cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3275ac27a0ecSDave Kleikamp ClearPageChecked(page); 3276ac27a0ecSDave Kleikamp 3277ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 327853e87268SJan Kara } 327953e87268SJan Kara 328053e87268SJan Kara /* Wrapper for aops... */ 328153e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3282d47992f8SLukas Czerner unsigned int offset, 3283d47992f8SLukas Czerner unsigned int length) 328453e87268SJan Kara { 3285ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3286ac27a0ecSDave Kleikamp } 3287ac27a0ecSDave Kleikamp 3288617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3289ac27a0ecSDave Kleikamp { 3290617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3291ac27a0ecSDave Kleikamp 32920562e0baSJiaying Zhang trace_ext4_releasepage(page); 32930562e0baSJiaying Zhang 3294e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3295e1c36595SJan Kara if (PageChecked(page)) 3296ac27a0ecSDave Kleikamp return 0; 32970390131bSFrank Mayhar if (journal) 3298dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 32990390131bSFrank Mayhar else 33000390131bSFrank Mayhar return try_to_free_buffers(page); 3301ac27a0ecSDave Kleikamp } 3302ac27a0ecSDave Kleikamp 3303b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3304b8a6176cSJan Kara { 3305b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3306b8a6176cSJan Kara 3307b8a6176cSJan Kara if (journal) 3308b8a6176cSJan Kara return !jbd2_transaction_committed(journal, 3309b8a6176cSJan Kara EXT4_I(inode)->i_datasync_tid); 3310b8a6176cSJan Kara /* Any metadata buffers to write? */ 3311b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3312b8a6176cSJan Kara return true; 3313b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3314b8a6176cSJan Kara } 3315b8a6176cSJan Kara 3316c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3317c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3318c8fdfe29SMatthew Bobrowski loff_t length) 3319364443cbSJan Kara { 3320c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3321c8fdfe29SMatthew Bobrowski 3322c8fdfe29SMatthew Bobrowski /* 3323c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3324c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3325c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3326c8fdfe29SMatthew Bobrowski */ 3327c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3328c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3329c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3330c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3331c8fdfe29SMatthew Bobrowski 3332c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3333c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3334c8fdfe29SMatthew Bobrowski 3335c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3336c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3337c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3338c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3339c8fdfe29SMatthew Bobrowski 33406386722aSRitesh Harjani if ((map->m_flags & EXT4_MAP_MAPPED) && 33416386722aSRitesh Harjani !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 33426386722aSRitesh Harjani iomap->flags |= IOMAP_F_MERGED; 33436386722aSRitesh Harjani 3344c8fdfe29SMatthew Bobrowski /* 3345c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3346c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3347c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3348c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3349c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3350c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3351c8fdfe29SMatthew Bobrowski * been set first. 3352c8fdfe29SMatthew Bobrowski */ 3353c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3354c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3355c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3356c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3357c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3358c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3359c8fdfe29SMatthew Bobrowski } else { 3360c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3361c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3362c8fdfe29SMatthew Bobrowski } 3363c8fdfe29SMatthew Bobrowski } 3364c8fdfe29SMatthew Bobrowski 3365f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3366f063db5eSMatthew Bobrowski unsigned int flags) 3367f063db5eSMatthew Bobrowski { 3368f063db5eSMatthew Bobrowski handle_t *handle; 3369378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3370378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3371f063db5eSMatthew Bobrowski 3372f063db5eSMatthew Bobrowski /* 3373f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3374f063db5eSMatthew Bobrowski * once for direct I/O. 3375f063db5eSMatthew Bobrowski */ 3376f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3377f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3378f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3379f063db5eSMatthew Bobrowski 3380f063db5eSMatthew Bobrowski retry: 3381f063db5eSMatthew Bobrowski /* 3382f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3383f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3384f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3385f063db5eSMatthew Bobrowski * fits into the credits as well. 3386f063db5eSMatthew Bobrowski */ 3387f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3388f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3389f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3390f063db5eSMatthew Bobrowski 3391378f32baSMatthew Bobrowski /* 3392378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3393378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3394378f32baSMatthew Bobrowski */ 3395378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3396378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3397378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3398378f32baSMatthew Bobrowski /* 3399378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3400378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3401378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3402378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3403378f32baSMatthew Bobrowski */ 3404378f32baSMatthew Bobrowski else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode)) 3405378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3406378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3407378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3408378f32baSMatthew Bobrowski 3409378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3410378f32baSMatthew Bobrowski 3411378f32baSMatthew Bobrowski /* 3412378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3413378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3414378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3415378f32baSMatthew Bobrowski */ 3416378f32baSMatthew Bobrowski if (!m_flags && !ret) 3417378f32baSMatthew Bobrowski ret = -ENOTBLK; 3418f063db5eSMatthew Bobrowski 3419f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3420f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3421f063db5eSMatthew Bobrowski goto retry; 3422f063db5eSMatthew Bobrowski 3423f063db5eSMatthew Bobrowski return ret; 3424f063db5eSMatthew Bobrowski } 3425f063db5eSMatthew Bobrowski 3426f063db5eSMatthew Bobrowski 3427364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3428c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3429364443cbSJan Kara { 3430364443cbSJan Kara int ret; 343109edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 343209edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3433364443cbSJan Kara 3434bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3435bcd8e91fSTheodore Ts'o return -EINVAL; 34367046ae35SAndreas Gruenbacher 3437364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3438364443cbSJan Kara return -ERANGE; 3439364443cbSJan Kara 344009edf4d3SMatthew Bobrowski /* 344109edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 344209edf4d3SMatthew Bobrowski */ 344309edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 344409edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 344509edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3446364443cbSJan Kara 344709edf4d3SMatthew Bobrowski if (flags & IOMAP_WRITE) 3448f063db5eSMatthew Bobrowski ret = ext4_iomap_alloc(inode, &map, flags); 344909edf4d3SMatthew Bobrowski else 3450545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 3451f063db5eSMatthew Bobrowski 3452545052e9SChristoph Hellwig if (ret < 0) 3453545052e9SChristoph Hellwig return ret; 3454364443cbSJan Kara 3455c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3456545052e9SChristoph Hellwig 3457364443cbSJan Kara return 0; 3458364443cbSJan Kara } 3459364443cbSJan Kara 34608cd115bdSJan Kara static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset, 34618cd115bdSJan Kara loff_t length, unsigned flags, struct iomap *iomap, 34628cd115bdSJan Kara struct iomap *srcmap) 34638cd115bdSJan Kara { 34648cd115bdSJan Kara int ret; 34658cd115bdSJan Kara 34668cd115bdSJan Kara /* 34678cd115bdSJan Kara * Even for writes we don't need to allocate blocks, so just pretend 34688cd115bdSJan Kara * we are reading to save overhead of starting a transaction. 34698cd115bdSJan Kara */ 34708cd115bdSJan Kara flags &= ~IOMAP_WRITE; 34718cd115bdSJan Kara ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap); 34728cd115bdSJan Kara WARN_ON_ONCE(iomap->type != IOMAP_MAPPED); 34738cd115bdSJan Kara return ret; 34748cd115bdSJan Kara } 34758cd115bdSJan Kara 3476776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3477776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3478776722e8SJan Kara { 3479378f32baSMatthew Bobrowski /* 3480378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3481378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3482378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3483378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3484378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3485378f32baSMatthew Bobrowski */ 3486378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3487378f32baSMatthew Bobrowski return -ENOTBLK; 3488378f32baSMatthew Bobrowski 3489776722e8SJan Kara return 0; 3490776722e8SJan Kara } 3491776722e8SJan Kara 34928ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3493364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3494776722e8SJan Kara .iomap_end = ext4_iomap_end, 3495364443cbSJan Kara }; 3496364443cbSJan Kara 34978cd115bdSJan Kara const struct iomap_ops ext4_iomap_overwrite_ops = { 34988cd115bdSJan Kara .iomap_begin = ext4_iomap_overwrite_begin, 34998cd115bdSJan Kara .iomap_end = ext4_iomap_end, 35008cd115bdSJan Kara }; 35018cd115bdSJan Kara 350209edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 350309edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 350409edf4d3SMatthew Bobrowski { 350509edf4d3SMatthew Bobrowski struct extent_status es; 350609edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 350709edf4d3SMatthew Bobrowski 350809edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 350909edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 351009edf4d3SMatthew Bobrowski 351109edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 351209edf4d3SMatthew Bobrowski return false; 351309edf4d3SMatthew Bobrowski 351409edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 351509edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 351609edf4d3SMatthew Bobrowski return false; 351709edf4d3SMatthew Bobrowski } 351809edf4d3SMatthew Bobrowski 351909edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 352009edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 352109edf4d3SMatthew Bobrowski 352209edf4d3SMatthew Bobrowski return true; 352309edf4d3SMatthew Bobrowski } 352409edf4d3SMatthew Bobrowski 352509edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 352609edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 352709edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 352809edf4d3SMatthew Bobrowski { 352909edf4d3SMatthew Bobrowski int ret; 353009edf4d3SMatthew Bobrowski bool delalloc = false; 353109edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 353209edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 353309edf4d3SMatthew Bobrowski 353409edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 353509edf4d3SMatthew Bobrowski return -EINVAL; 353609edf4d3SMatthew Bobrowski 35377cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35387cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3539ba5843f5SJan Kara if (ret != -EAGAIN) { 3540ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3541ba5843f5SJan Kara ret = -ENOENT; 3542ba5843f5SJan Kara return ret; 3543ba5843f5SJan Kara } 3544ba5843f5SJan Kara } 354512735f88SJan Kara 354609edf4d3SMatthew Bobrowski /* 354709edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 354809edf4d3SMatthew Bobrowski */ 354909edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 355009edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 355109edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 355212735f88SJan Kara 3553b2c57642SRitesh Harjani /* 3554b2c57642SRitesh Harjani * Fiemap callers may call for offset beyond s_bitmap_maxbytes. 3555b2c57642SRitesh Harjani * So handle it here itself instead of querying ext4_map_blocks(). 3556b2c57642SRitesh Harjani * Since ext4_map_blocks() will warn about it and will return 3557b2c57642SRitesh Harjani * -EIO error. 3558b2c57642SRitesh Harjani */ 3559b2c57642SRitesh Harjani if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 3560b2c57642SRitesh Harjani struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3561b2c57642SRitesh Harjani 3562b2c57642SRitesh Harjani if (offset >= sbi->s_bitmap_maxbytes) { 3563b2c57642SRitesh Harjani map.m_flags = 0; 3564b2c57642SRitesh Harjani goto set_iomap; 3565b2c57642SRitesh Harjani } 3566b2c57642SRitesh Harjani } 3567b2c57642SRitesh Harjani 356812735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3569ba5843f5SJan Kara if (ret < 0) 3570ba5843f5SJan Kara return ret; 3571914f82a3SJan Kara if (ret == 0) 357209edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 357309edf4d3SMatthew Bobrowski 3574b2c57642SRitesh Harjani set_iomap: 357509edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 357609edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 357709edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 357809edf4d3SMatthew Bobrowski 357909edf4d3SMatthew Bobrowski return 0; 3580914f82a3SJan Kara } 3581914f82a3SJan Kara 358209edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 358309edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 358409edf4d3SMatthew Bobrowski }; 35854c0425ffSMingming Cao 3586ac27a0ecSDave Kleikamp /* 3587617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3588ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3589ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3590ac27a0ecSDave Kleikamp * not necessarily locked. 3591ac27a0ecSDave Kleikamp * 3592ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3593ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3594ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3595ac27a0ecSDave Kleikamp * 3596ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3597ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3598ac27a0ecSDave Kleikamp */ 3599617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3600ac27a0ecSDave Kleikamp { 3601ac27a0ecSDave Kleikamp SetPageChecked(page); 3602ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3603ac27a0ecSDave Kleikamp } 3604ac27a0ecSDave Kleikamp 36056dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 36066dcc693bSJan Kara { 36076dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 36086dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 36096dcc693bSJan Kara return __set_page_dirty_buffers(page); 36106dcc693bSJan Kara } 36116dcc693bSJan Kara 361274d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3613617ba13bSMingming Cao .readpage = ext4_readpage, 3614617ba13bSMingming Cao .readpages = ext4_readpages, 361543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 361620970ba6STheodore Ts'o .writepages = ext4_writepages, 3617bfc1af65SNick Piggin .write_begin = ext4_write_begin, 361874d553aaSTheodore Ts'o .write_end = ext4_write_end, 36196dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3620617ba13bSMingming Cao .bmap = ext4_bmap, 3621617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3622617ba13bSMingming Cao .releasepage = ext4_releasepage, 3623378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3624ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 36258ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3626aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3627ac27a0ecSDave Kleikamp }; 3628ac27a0ecSDave Kleikamp 3629617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3630617ba13bSMingming Cao .readpage = ext4_readpage, 3631617ba13bSMingming Cao .readpages = ext4_readpages, 363243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 363320970ba6STheodore Ts'o .writepages = ext4_writepages, 3634bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3635bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3636617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3637617ba13bSMingming Cao .bmap = ext4_bmap, 36384520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3639617ba13bSMingming Cao .releasepage = ext4_releasepage, 3640378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 36418ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3642aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3643ac27a0ecSDave Kleikamp }; 3644ac27a0ecSDave Kleikamp 364564769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 364664769240SAlex Tomas .readpage = ext4_readpage, 364764769240SAlex Tomas .readpages = ext4_readpages, 364843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 364920970ba6STheodore Ts'o .writepages = ext4_writepages, 365064769240SAlex Tomas .write_begin = ext4_da_write_begin, 365164769240SAlex Tomas .write_end = ext4_da_write_end, 36526dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 365364769240SAlex Tomas .bmap = ext4_bmap, 36548fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 365564769240SAlex Tomas .releasepage = ext4_releasepage, 3656378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 365764769240SAlex Tomas .migratepage = buffer_migrate_page, 36588ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3659aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 366064769240SAlex Tomas }; 366164769240SAlex Tomas 36625f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36635f0663bbSDan Williams .writepages = ext4_dax_writepages, 36645f0663bbSDan Williams .direct_IO = noop_direct_IO, 36655f0663bbSDan Williams .set_page_dirty = noop_set_page_dirty, 366694dbb631SToshi Kani .bmap = ext4_bmap, 36675f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36685f0663bbSDan Williams }; 36695f0663bbSDan Williams 3670617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3671ac27a0ecSDave Kleikamp { 36723d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36733d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36743d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36753d2b1582SLukas Czerner break; 36763d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3677617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 367874d553aaSTheodore Ts'o return; 36793d2b1582SLukas Czerner default: 36803d2b1582SLukas Czerner BUG(); 36813d2b1582SLukas Czerner } 36825f0663bbSDan Williams if (IS_DAX(inode)) 36835f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 36845f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 368574d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 368674d553aaSTheodore Ts'o else 368774d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3688ac27a0ecSDave Kleikamp } 3689ac27a0ecSDave Kleikamp 3690923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3691d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3692d863dc36SLukas Czerner { 369309cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 369409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3695923ae0ffSRoss Zwisler unsigned blocksize, pos; 3696d863dc36SLukas Czerner ext4_lblk_t iblock; 3697d863dc36SLukas Czerner struct inode *inode = mapping->host; 3698d863dc36SLukas Czerner struct buffer_head *bh; 3699d863dc36SLukas Czerner struct page *page; 3700d863dc36SLukas Czerner int err = 0; 3701d863dc36SLukas Czerner 370209cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3703c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3704d863dc36SLukas Czerner if (!page) 3705d863dc36SLukas Czerner return -ENOMEM; 3706d863dc36SLukas Czerner 3707d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3708d863dc36SLukas Czerner 370909cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3710d863dc36SLukas Czerner 3711d863dc36SLukas Czerner if (!page_has_buffers(page)) 3712d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3713d863dc36SLukas Czerner 3714d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3715d863dc36SLukas Czerner bh = page_buffers(page); 3716d863dc36SLukas Czerner pos = blocksize; 3717d863dc36SLukas Czerner while (offset >= pos) { 3718d863dc36SLukas Czerner bh = bh->b_this_page; 3719d863dc36SLukas Czerner iblock++; 3720d863dc36SLukas Czerner pos += blocksize; 3721d863dc36SLukas Czerner } 3722d863dc36SLukas Czerner if (buffer_freed(bh)) { 3723d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3724d863dc36SLukas Czerner goto unlock; 3725d863dc36SLukas Czerner } 3726d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3727d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3728d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3729d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3730d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3731d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3732d863dc36SLukas Czerner goto unlock; 3733d863dc36SLukas Czerner } 3734d863dc36SLukas Czerner } 3735d863dc36SLukas Czerner 3736d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3737d863dc36SLukas Czerner if (PageUptodate(page)) 3738d863dc36SLukas Czerner set_buffer_uptodate(bh); 3739d863dc36SLukas Czerner 3740d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 3741d863dc36SLukas Czerner err = -EIO; 3742dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 3743d863dc36SLukas Czerner wait_on_buffer(bh); 3744d863dc36SLukas Czerner /* Uhhuh. Read error. Complain and punt. */ 3745d863dc36SLukas Czerner if (!buffer_uptodate(bh)) 3746d863dc36SLukas Czerner goto unlock; 3747592ddec7SChandan Rajendra if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) { 3748c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3749a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3750834f1565SEric Biggers err = fscrypt_decrypt_pagecache_blocks(page, blocksize, 3751834f1565SEric Biggers bh_offset(bh)); 3752834f1565SEric Biggers if (err) { 3753834f1565SEric Biggers clear_buffer_uptodate(bh); 3754834f1565SEric Biggers goto unlock; 3755834f1565SEric Biggers } 3756c9c7429cSMichael Halcrow } 3757d863dc36SLukas Czerner } 3758d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3759d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3760d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3761d863dc36SLukas Czerner if (err) 3762d863dc36SLukas Czerner goto unlock; 3763d863dc36SLukas Czerner } 3764d863dc36SLukas Czerner zero_user(page, offset, length); 3765d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3766d863dc36SLukas Czerner 3767d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3768d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37690713ed0cSLukas Czerner } else { 3770353eefd3Sjon ernst err = 0; 3771d863dc36SLukas Czerner mark_buffer_dirty(bh); 37723957ef53SJan Kara if (ext4_should_order_data(inode)) 377373131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 377473131fbbSRoss Zwisler length); 37750713ed0cSLukas Czerner } 3776d863dc36SLukas Czerner 3777d863dc36SLukas Czerner unlock: 3778d863dc36SLukas Czerner unlock_page(page); 377909cbfeafSKirill A. Shutemov put_page(page); 3780d863dc36SLukas Czerner return err; 3781d863dc36SLukas Czerner } 3782d863dc36SLukas Czerner 378394350ab5SMatthew Wilcox /* 3784923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3785923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3786923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3787923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 3788923ae0ffSRoss Zwisler * that cooresponds to 'from' 3789923ae0ffSRoss Zwisler */ 3790923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3791923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3792923ae0ffSRoss Zwisler { 3793923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 379409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3795923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3796923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3797923ae0ffSRoss Zwisler 3798923ae0ffSRoss Zwisler /* 3799923ae0ffSRoss Zwisler * correct length if it does not fall between 3800923ae0ffSRoss Zwisler * 'from' and the end of the block 3801923ae0ffSRoss Zwisler */ 3802923ae0ffSRoss Zwisler if (length > max || length < 0) 3803923ae0ffSRoss Zwisler length = max; 3804923ae0ffSRoss Zwisler 380547e69351SJan Kara if (IS_DAX(inode)) { 380647e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 380747e69351SJan Kara &ext4_iomap_ops); 380847e69351SJan Kara } 3809923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3810923ae0ffSRoss Zwisler } 3811923ae0ffSRoss Zwisler 3812923ae0ffSRoss Zwisler /* 381394350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 381494350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 381594350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 381694350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 381794350ab5SMatthew Wilcox */ 3818c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 381994350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 382094350ab5SMatthew Wilcox { 382109cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 382294350ab5SMatthew Wilcox unsigned length; 382394350ab5SMatthew Wilcox unsigned blocksize; 382494350ab5SMatthew Wilcox struct inode *inode = mapping->host; 382594350ab5SMatthew Wilcox 38260d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3827592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 38280d06863fSTheodore Ts'o return 0; 38290d06863fSTheodore Ts'o 383094350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 383194350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 383294350ab5SMatthew Wilcox 383394350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 383494350ab5SMatthew Wilcox } 383594350ab5SMatthew Wilcox 3836a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3837a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3838a87dd18cSLukas Czerner { 3839a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3840a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3841e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3842a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3843a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3844a87dd18cSLukas Czerner int err = 0; 3845a87dd18cSLukas Czerner 3846e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3847e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3848e1be3a92SLukas Czerner 3849a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3850a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3851a87dd18cSLukas Czerner 3852a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3853e1be3a92SLukas Czerner if (start == end && 3854e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3855a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3856a87dd18cSLukas Czerner lstart, length); 3857a87dd18cSLukas Czerner return err; 3858a87dd18cSLukas Czerner } 3859a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3860e1be3a92SLukas Czerner if (partial_start) { 3861a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3862a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3863a87dd18cSLukas Czerner if (err) 3864a87dd18cSLukas Czerner return err; 3865a87dd18cSLukas Czerner } 3866a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3867e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3868a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3869e1be3a92SLukas Czerner byte_end - partial_end, 3870e1be3a92SLukas Czerner partial_end + 1); 3871a87dd18cSLukas Czerner return err; 3872a87dd18cSLukas Czerner } 3873a87dd18cSLukas Czerner 387491ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 387591ef4cafSDuane Griffin { 387691ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 387791ef4cafSDuane Griffin return 1; 387891ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 387991ef4cafSDuane Griffin return 1; 388091ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 388191ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 388291ef4cafSDuane Griffin return 0; 388391ef4cafSDuane Griffin } 388491ef4cafSDuane Griffin 3885ac27a0ecSDave Kleikamp /* 388601127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 388701127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 388801127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 388901127848SJan Kara * that will never happen after we truncate page cache. 389001127848SJan Kara */ 389101127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 389201127848SJan Kara loff_t len) 389301127848SJan Kara { 389401127848SJan Kara handle_t *handle; 3895*4209ae12SHarshad Shirwadkar int ret; 3896*4209ae12SHarshad Shirwadkar 389701127848SJan Kara loff_t size = i_size_read(inode); 389801127848SJan Kara 38995955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 390001127848SJan Kara if (offset > size || offset + len < size) 390101127848SJan Kara return 0; 390201127848SJan Kara 390301127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 390401127848SJan Kara return 0; 390501127848SJan Kara 390601127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 390701127848SJan Kara if (IS_ERR(handle)) 390801127848SJan Kara return PTR_ERR(handle); 390901127848SJan Kara ext4_update_i_disksize(inode, size); 3910*4209ae12SHarshad Shirwadkar ret = ext4_mark_inode_dirty(handle, inode); 391101127848SJan Kara ext4_journal_stop(handle); 391201127848SJan Kara 3913*4209ae12SHarshad Shirwadkar return ret; 391401127848SJan Kara } 391501127848SJan Kara 3916b1f38217SRoss Zwisler static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3917430657b6SRoss Zwisler { 3918430657b6SRoss Zwisler up_write(&ei->i_mmap_sem); 3919430657b6SRoss Zwisler schedule(); 3920430657b6SRoss Zwisler down_write(&ei->i_mmap_sem); 3921430657b6SRoss Zwisler } 3922430657b6SRoss Zwisler 3923430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3924430657b6SRoss Zwisler { 3925430657b6SRoss Zwisler struct ext4_inode_info *ei = EXT4_I(inode); 3926430657b6SRoss Zwisler struct page *page; 3927430657b6SRoss Zwisler int error; 3928430657b6SRoss Zwisler 3929430657b6SRoss Zwisler if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3930430657b6SRoss Zwisler return -EINVAL; 3931430657b6SRoss Zwisler 3932430657b6SRoss Zwisler do { 3933430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3934430657b6SRoss Zwisler if (!page) 3935430657b6SRoss Zwisler return 0; 3936430657b6SRoss Zwisler 3937430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3938430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3939430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3940b1f38217SRoss Zwisler ext4_wait_dax_page(ei)); 3941b1f38217SRoss Zwisler } while (error == 0); 3942430657b6SRoss Zwisler 3943430657b6SRoss Zwisler return error; 3944430657b6SRoss Zwisler } 3945430657b6SRoss Zwisler 394601127848SJan Kara /* 3947cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3948a4bb6b64SAllison Henderson * associated with the given offset and length 3949a4bb6b64SAllison Henderson * 3950a4bb6b64SAllison Henderson * @inode: File inode 3951a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3952a4bb6b64SAllison Henderson * @len: The length of the hole 3953a4bb6b64SAllison Henderson * 39544907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3955a4bb6b64SAllison Henderson */ 3956a4bb6b64SAllison Henderson 3957aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3958a4bb6b64SAllison Henderson { 395926a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 396026a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 396126a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3962a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 396326a4c0c6STheodore Ts'o handle_t *handle; 396426a4c0c6STheodore Ts'o unsigned int credits; 3965*4209ae12SHarshad Shirwadkar int ret = 0, ret2 = 0; 396626a4c0c6STheodore Ts'o 3967b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3968aaddea81SZheng Liu 3969c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3970c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3971c1e8220bSTheodore Ts'o down_write(&EXT4_I(inode)->i_mmap_sem); 3972c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3973c1e8220bSTheodore Ts'o up_write(&EXT4_I(inode)->i_mmap_sem); 3974c1e8220bSTheodore Ts'o if (ret) 3975c1e8220bSTheodore Ts'o return ret; 3976c1e8220bSTheodore Ts'o } 3977c1e8220bSTheodore Ts'o 397826a4c0c6STheodore Ts'o /* 397926a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 398026a4c0c6STheodore Ts'o * Then release them. 398126a4c0c6STheodore Ts'o */ 3982cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 398326a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 398426a4c0c6STheodore Ts'o offset + length - 1); 398526a4c0c6STheodore Ts'o if (ret) 398626a4c0c6STheodore Ts'o return ret; 398726a4c0c6STheodore Ts'o } 398826a4c0c6STheodore Ts'o 39895955102cSAl Viro inode_lock(inode); 39909ef06cecSLukas Czerner 399126a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 399226a4c0c6STheodore Ts'o if (offset >= inode->i_size) 399326a4c0c6STheodore Ts'o goto out_mutex; 399426a4c0c6STheodore Ts'o 399526a4c0c6STheodore Ts'o /* 399626a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 399726a4c0c6STheodore Ts'o * to end after the page that contains i_size 399826a4c0c6STheodore Ts'o */ 399926a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 400026a4c0c6STheodore Ts'o length = inode->i_size + 400109cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 400226a4c0c6STheodore Ts'o offset; 400326a4c0c6STheodore Ts'o } 400426a4c0c6STheodore Ts'o 4005a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 4006a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 4007a361293fSJan Kara /* 4008a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 4009a361293fSJan Kara * partial block 4010a361293fSJan Kara */ 4011a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 4012a361293fSJan Kara if (ret < 0) 4013a361293fSJan Kara goto out_mutex; 4014a361293fSJan Kara 4015a361293fSJan Kara } 4016a361293fSJan Kara 4017ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 4018ea3d7209SJan Kara inode_dio_wait(inode); 4019ea3d7209SJan Kara 4020ea3d7209SJan Kara /* 4021ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 4022ea3d7209SJan Kara * page cache. 4023ea3d7209SJan Kara */ 4024ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 4025430657b6SRoss Zwisler 4026430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4027430657b6SRoss Zwisler if (ret) 4028430657b6SRoss Zwisler goto out_dio; 4029430657b6SRoss Zwisler 4030a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 4031a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 403226a4c0c6STheodore Ts'o 4033a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 403401127848SJan Kara if (last_block_offset > first_block_offset) { 403501127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 403601127848SJan Kara if (ret) 403701127848SJan Kara goto out_dio; 4038a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 4039a87dd18cSLukas Czerner last_block_offset); 404001127848SJan Kara } 404126a4c0c6STheodore Ts'o 404226a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 404326a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 404426a4c0c6STheodore Ts'o else 404526a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 404626a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 404726a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 404826a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 404926a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 405026a4c0c6STheodore Ts'o goto out_dio; 405126a4c0c6STheodore Ts'o } 405226a4c0c6STheodore Ts'o 4053a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4054a87dd18cSLukas Czerner length); 405526a4c0c6STheodore Ts'o if (ret) 405626a4c0c6STheodore Ts'o goto out_stop; 405726a4c0c6STheodore Ts'o 405826a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 405926a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 406026a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 406126a4c0c6STheodore Ts'o 4062eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4063eee597acSLukas Czerner if (stop_block > first_block) { 406426a4c0c6STheodore Ts'o 406526a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 406626a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 406726a4c0c6STheodore Ts'o 406826a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 406926a4c0c6STheodore Ts'o stop_block - first_block); 407026a4c0c6STheodore Ts'o if (ret) { 407126a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 407226a4c0c6STheodore Ts'o goto out_stop; 407326a4c0c6STheodore Ts'o } 407426a4c0c6STheodore Ts'o 407526a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 407626a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 407726a4c0c6STheodore Ts'o stop_block - 1); 407826a4c0c6STheodore Ts'o else 40794f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 408026a4c0c6STheodore Ts'o stop_block); 408126a4c0c6STheodore Ts'o 4082819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4083eee597acSLukas Czerner } 408426a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 408526a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4086e251f9bcSMaxim Patlasov 4087eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 4088*4209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, inode); 4089*4209ae12SHarshad Shirwadkar if (unlikely(ret2)) 4090*4209ae12SHarshad Shirwadkar ret = ret2; 409167a7d5f5SJan Kara if (ret >= 0) 409267a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 409326a4c0c6STheodore Ts'o out_stop: 409426a4c0c6STheodore Ts'o ext4_journal_stop(handle); 409526a4c0c6STheodore Ts'o out_dio: 4096ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 409726a4c0c6STheodore Ts'o out_mutex: 40985955102cSAl Viro inode_unlock(inode); 409926a4c0c6STheodore Ts'o return ret; 4100a4bb6b64SAllison Henderson } 4101a4bb6b64SAllison Henderson 4102a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4103a361293fSJan Kara { 4104a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4105a361293fSJan Kara struct jbd2_inode *jinode; 4106a361293fSJan Kara 4107a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4108a361293fSJan Kara return 0; 4109a361293fSJan Kara 4110a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4111a361293fSJan Kara spin_lock(&inode->i_lock); 4112a361293fSJan Kara if (!ei->jinode) { 4113a361293fSJan Kara if (!jinode) { 4114a361293fSJan Kara spin_unlock(&inode->i_lock); 4115a361293fSJan Kara return -ENOMEM; 4116a361293fSJan Kara } 4117a361293fSJan Kara ei->jinode = jinode; 4118a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4119a361293fSJan Kara jinode = NULL; 4120a361293fSJan Kara } 4121a361293fSJan Kara spin_unlock(&inode->i_lock); 4122a361293fSJan Kara if (unlikely(jinode != NULL)) 4123a361293fSJan Kara jbd2_free_inode(jinode); 4124a361293fSJan Kara return 0; 4125a361293fSJan Kara } 4126a361293fSJan Kara 4127a4bb6b64SAllison Henderson /* 4128617ba13bSMingming Cao * ext4_truncate() 4129ac27a0ecSDave Kleikamp * 4130617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4131617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4132ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4133ac27a0ecSDave Kleikamp * 413442b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4135ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4136ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4137ac27a0ecSDave Kleikamp * 4138ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4139ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4140ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4141ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4142ac27a0ecSDave Kleikamp * left-to-right works OK too). 4143ac27a0ecSDave Kleikamp * 4144ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4145ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4146ac27a0ecSDave Kleikamp * 4147ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4148617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4149ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4150617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4151617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4152ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4153617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4154ac27a0ecSDave Kleikamp */ 41552c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4156ac27a0ecSDave Kleikamp { 4157819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4158819c4920STheodore Ts'o unsigned int credits; 4159*4209ae12SHarshad Shirwadkar int err = 0, err2; 4160819c4920STheodore Ts'o handle_t *handle; 4161819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4162819c4920STheodore Ts'o 416319b5ef61STheodore Ts'o /* 416419b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4165e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 416619b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 416719b5ef61STheodore Ts'o */ 416819b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41695955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41700562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41710562e0baSJiaying Zhang 417291ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41732c98eb5eSTheodore Ts'o return 0; 4174ac27a0ecSDave Kleikamp 41755534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 417619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41777d8f9f7dSTheodore Ts'o 4178aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4179aef1c851STao Ma int has_inline = 1; 4180aef1c851STao Ma 418101daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 418201daf945STheodore Ts'o if (err) 418301daf945STheodore Ts'o return err; 4184aef1c851STao Ma if (has_inline) 41852c98eb5eSTheodore Ts'o return 0; 4186aef1c851STao Ma } 4187aef1c851STao Ma 4188a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4189a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4190a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 41912c98eb5eSTheodore Ts'o return 0; 4192a361293fSJan Kara } 4193a361293fSJan Kara 4194ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4195819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4196ff9893dcSAmir Goldstein else 4197819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4198819c4920STheodore Ts'o 4199819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 42002c98eb5eSTheodore Ts'o if (IS_ERR(handle)) 42012c98eb5eSTheodore Ts'o return PTR_ERR(handle); 4202819c4920STheodore Ts'o 4203eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4204eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4205819c4920STheodore Ts'o 4206819c4920STheodore Ts'o /* 4207819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4208819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4209819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4210819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4211819c4920STheodore Ts'o * 4212819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4213819c4920STheodore Ts'o * truncatable state while each transaction commits. 4214819c4920STheodore Ts'o */ 42152c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 42162c98eb5eSTheodore Ts'o if (err) 4217819c4920STheodore Ts'o goto out_stop; 4218819c4920STheodore Ts'o 4219819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4220819c4920STheodore Ts'o 4221819c4920STheodore Ts'o ext4_discard_preallocations(inode); 4222819c4920STheodore Ts'o 4223819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4224d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4225819c4920STheodore Ts'o else 4226819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4227819c4920STheodore Ts'o 4228819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4229d0abb36dSTheodore Ts'o if (err) 4230d0abb36dSTheodore Ts'o goto out_stop; 4231819c4920STheodore Ts'o 4232819c4920STheodore Ts'o if (IS_SYNC(inode)) 4233819c4920STheodore Ts'o ext4_handle_sync(handle); 4234819c4920STheodore Ts'o 4235819c4920STheodore Ts'o out_stop: 4236819c4920STheodore Ts'o /* 4237819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4238819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4239819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 424058d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4241819c4920STheodore Ts'o * orphan info for us. 4242819c4920STheodore Ts'o */ 4243819c4920STheodore Ts'o if (inode->i_nlink) 4244819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4245819c4920STheodore Ts'o 4246eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 4247*4209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 4248*4209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 4249*4209ae12SHarshad Shirwadkar err = err2; 4250819c4920STheodore Ts'o ext4_journal_stop(handle); 4251a86c6181SAlex Tomas 42520562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42532c98eb5eSTheodore Ts'o return err; 4254ac27a0ecSDave Kleikamp } 4255ac27a0ecSDave Kleikamp 4256ac27a0ecSDave Kleikamp /* 4257617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4258ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4259ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4260ac27a0ecSDave Kleikamp * inode. 4261ac27a0ecSDave Kleikamp */ 4262617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4263617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4264ac27a0ecSDave Kleikamp { 4265240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4266ac27a0ecSDave Kleikamp struct buffer_head *bh; 4267240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4268240799cdSTheodore Ts'o ext4_fsblk_t block; 426902f03c42SLinus Torvalds struct blk_plug plug; 4270240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4271ac27a0ecSDave Kleikamp 42723a06d778SAneesh Kumar K.V iloc->bh = NULL; 4273c37e9e01STheodore Ts'o if (inode->i_ino < EXT4_ROOT_INO || 4274c37e9e01STheodore Ts'o inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 42756a797d27SDarrick J. Wong return -EFSCORRUPTED; 4276ac27a0ecSDave Kleikamp 4277240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4278240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4279240799cdSTheodore Ts'o if (!gdp) 4280240799cdSTheodore Ts'o return -EIO; 4281240799cdSTheodore Ts'o 4282240799cdSTheodore Ts'o /* 4283240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4284240799cdSTheodore Ts'o */ 428500d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4286240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4287240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4288240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4289240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4290240799cdSTheodore Ts'o 4291240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4292aebf0243SWang Shilong if (unlikely(!bh)) 4293860d21e2STheodore Ts'o return -ENOMEM; 429446f870d6STheodore Ts'o if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO)) 429546f870d6STheodore Ts'o goto simulate_eio; 4296ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4297ac27a0ecSDave Kleikamp lock_buffer(bh); 42989c83a923SHidehiro Kawai 42999c83a923SHidehiro Kawai /* 43009c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 43019c83a923SHidehiro Kawai * to write out another inode in the same block. In this 43029c83a923SHidehiro Kawai * case, we don't have to read the block because we may 43039c83a923SHidehiro Kawai * read the old inode data successfully. 43049c83a923SHidehiro Kawai */ 43059c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 43069c83a923SHidehiro Kawai set_buffer_uptodate(bh); 43079c83a923SHidehiro Kawai 4308ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4309ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4310ac27a0ecSDave Kleikamp unlock_buffer(bh); 4311ac27a0ecSDave Kleikamp goto has_buffer; 4312ac27a0ecSDave Kleikamp } 4313ac27a0ecSDave Kleikamp 4314ac27a0ecSDave Kleikamp /* 4315ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4316ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4317ac27a0ecSDave Kleikamp * block. 4318ac27a0ecSDave Kleikamp */ 4319ac27a0ecSDave Kleikamp if (in_mem) { 4320ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4321240799cdSTheodore Ts'o int i, start; 4322ac27a0ecSDave Kleikamp 4323240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4324ac27a0ecSDave Kleikamp 4325ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4326240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4327aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4328ac27a0ecSDave Kleikamp goto make_io; 4329ac27a0ecSDave Kleikamp 4330ac27a0ecSDave Kleikamp /* 4331ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4332ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4333ac27a0ecSDave Kleikamp * of one, so skip it. 4334ac27a0ecSDave Kleikamp */ 4335ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4336ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4337ac27a0ecSDave Kleikamp goto make_io; 4338ac27a0ecSDave Kleikamp } 4339240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4340ac27a0ecSDave Kleikamp if (i == inode_offset) 4341ac27a0ecSDave Kleikamp continue; 4342617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4343ac27a0ecSDave Kleikamp break; 4344ac27a0ecSDave Kleikamp } 4345ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4346240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4347ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4348ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4349ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4350ac27a0ecSDave Kleikamp unlock_buffer(bh); 4351ac27a0ecSDave Kleikamp goto has_buffer; 4352ac27a0ecSDave Kleikamp } 4353ac27a0ecSDave Kleikamp } 4354ac27a0ecSDave Kleikamp 4355ac27a0ecSDave Kleikamp make_io: 4356ac27a0ecSDave Kleikamp /* 4357240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4358240799cdSTheodore Ts'o * blocks from the inode table. 4359240799cdSTheodore Ts'o */ 436002f03c42SLinus Torvalds blk_start_plug(&plug); 4361240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4362240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4363240799cdSTheodore Ts'o unsigned num; 43640d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4365240799cdSTheodore Ts'o 4366240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4367b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 43680d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4369240799cdSTheodore Ts'o if (table > b) 4370240799cdSTheodore Ts'o b = table; 43710d606e2cSTheodore Ts'o end = b + ra_blks; 4372240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4373feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4374560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4375240799cdSTheodore Ts'o table += num / inodes_per_block; 4376240799cdSTheodore Ts'o if (end > table) 4377240799cdSTheodore Ts'o end = table; 4378240799cdSTheodore Ts'o while (b <= end) 4379d87f6392SRoman Gushchin sb_breadahead_unmovable(sb, b++); 4380240799cdSTheodore Ts'o } 4381240799cdSTheodore Ts'o 4382240799cdSTheodore Ts'o /* 4383ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4384ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4385ac27a0ecSDave Kleikamp * Read the block from disk. 4386ac27a0ecSDave Kleikamp */ 43870562e0baSJiaying Zhang trace_ext4_load_inode(inode); 4388ac27a0ecSDave Kleikamp get_bh(bh); 4389ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 43902a222ca9SMike Christie submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); 439102f03c42SLinus Torvalds blk_finish_plug(&plug); 4392ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4393ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 439446f870d6STheodore Ts'o simulate_eio: 439554d3adbcSTheodore Ts'o ext4_error_inode_block(inode, block, EIO, 4396c398eda0STheodore Ts'o "unable to read itable block"); 4397ac27a0ecSDave Kleikamp brelse(bh); 4398ac27a0ecSDave Kleikamp return -EIO; 4399ac27a0ecSDave Kleikamp } 4400ac27a0ecSDave Kleikamp } 4401ac27a0ecSDave Kleikamp has_buffer: 4402ac27a0ecSDave Kleikamp iloc->bh = bh; 4403ac27a0ecSDave Kleikamp return 0; 4404ac27a0ecSDave Kleikamp } 4405ac27a0ecSDave Kleikamp 4406617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4407ac27a0ecSDave Kleikamp { 4408ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4409617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 441019f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4411ac27a0ecSDave Kleikamp } 4412ac27a0ecSDave Kleikamp 44136642586bSRoss Zwisler static bool ext4_should_use_dax(struct inode *inode) 44146642586bSRoss Zwisler { 44156642586bSRoss Zwisler if (!test_opt(inode->i_sb, DAX)) 44166642586bSRoss Zwisler return false; 44176642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 44186642586bSRoss Zwisler return false; 44196642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 44206642586bSRoss Zwisler return false; 44216642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 44226642586bSRoss Zwisler return false; 4423592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 44246642586bSRoss Zwisler return false; 4425c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4426c93d8f88SEric Biggers return false; 44276642586bSRoss Zwisler return true; 44286642586bSRoss Zwisler } 44296642586bSRoss Zwisler 4430617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4431ac27a0ecSDave Kleikamp { 4432617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 443300a1a053STheodore Ts'o unsigned int new_fl = 0; 4434ac27a0ecSDave Kleikamp 4435617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 443600a1a053STheodore Ts'o new_fl |= S_SYNC; 4437617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 443800a1a053STheodore Ts'o new_fl |= S_APPEND; 4439617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 444000a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4441617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 444200a1a053STheodore Ts'o new_fl |= S_NOATIME; 4443617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 444400a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 44456642586bSRoss Zwisler if (ext4_should_use_dax(inode)) 4446923ae0ffSRoss Zwisler new_fl |= S_DAX; 44472ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 44482ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4449b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4450b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4451c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4452c93d8f88SEric Biggers new_fl |= S_VERITY; 44535f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 44542ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4455c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4456ac27a0ecSDave Kleikamp } 4457ac27a0ecSDave Kleikamp 44580fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 44590fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 44600fc1b451SAneesh Kumar K.V { 44610fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 44628180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 44638180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 44640fc1b451SAneesh Kumar K.V 4465e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 44660fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 44670fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 44680fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 446907a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 44708180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 44718180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 44728180a562SAneesh Kumar K.V } else { 44730fc1b451SAneesh Kumar K.V return i_blocks; 44748180a562SAneesh Kumar K.V } 44750fc1b451SAneesh Kumar K.V } else { 44760fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 44770fc1b451SAneesh Kumar K.V } 44780fc1b451SAneesh Kumar K.V } 4479ff9ddf7eSJan Kara 4480eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4481152a7b0aSTao Ma struct ext4_inode *raw_inode, 4482152a7b0aSTao Ma struct ext4_inode_info *ei) 4483152a7b0aSTao Ma { 4484152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4485152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4486eb9b5f01STheodore Ts'o 4487290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4488290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4489290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4490152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4491eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4492f19d5870STao Ma } else 4493f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4494eb9b5f01STheodore Ts'o return 0; 4495152a7b0aSTao Ma } 4496152a7b0aSTao Ma 4497040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4498040cb378SLi Xi { 44990b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4500040cb378SLi Xi return -EOPNOTSUPP; 4501040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4502040cb378SLi Xi return 0; 4503040cb378SLi Xi } 4504040cb378SLi Xi 4505e254d1afSEryu Guan /* 4506e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4507e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4508e254d1afSEryu Guan * set. 4509e254d1afSEryu Guan */ 4510e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4511e254d1afSEryu Guan { 4512e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4513e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4514e254d1afSEryu Guan else 4515e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4516e254d1afSEryu Guan } 4517e254d1afSEryu Guan static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4518e254d1afSEryu Guan { 4519e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4520e254d1afSEryu Guan return inode_peek_iversion_raw(inode); 4521e254d1afSEryu Guan else 4522e254d1afSEryu Guan return inode_peek_iversion(inode); 4523e254d1afSEryu Guan } 4524e254d1afSEryu Guan 45258a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 45268a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 45278a363970STheodore Ts'o unsigned int line) 4528ac27a0ecSDave Kleikamp { 4529617ba13bSMingming Cao struct ext4_iloc iloc; 4530617ba13bSMingming Cao struct ext4_inode *raw_inode; 45311d1fe1eeSDavid Howells struct ext4_inode_info *ei; 45321d1fe1eeSDavid Howells struct inode *inode; 4533b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 45341d1fe1eeSDavid Howells long ret; 45357e6e1ef4SDarrick J. Wong loff_t size; 4536ac27a0ecSDave Kleikamp int block; 453708cefc7aSEric W. Biederman uid_t i_uid; 453808cefc7aSEric W. Biederman gid_t i_gid; 4539040cb378SLi Xi projid_t i_projid; 4540ac27a0ecSDave Kleikamp 4541191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 45428a363970STheodore Ts'o (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 45438a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 45448a363970STheodore Ts'o (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 45458a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 45468a363970STheodore Ts'o return ERR_PTR(-ESTALE); 454754d3adbcSTheodore Ts'o __ext4_error(sb, function, line, EFSCORRUPTED, 0, 45488a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 45498a363970STheodore Ts'o ino, current->comm); 45508a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 45518a363970STheodore Ts'o } 45528a363970STheodore Ts'o 45531d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 45541d1fe1eeSDavid Howells if (!inode) 45551d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 45561d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 45571d1fe1eeSDavid Howells return inode; 45581d1fe1eeSDavid Howells 45591d1fe1eeSDavid Howells ei = EXT4_I(inode); 45607dc57615SPeter Huewe iloc.bh = NULL; 4561ac27a0ecSDave Kleikamp 45621d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 45631d1fe1eeSDavid Howells if (ret < 0) 4564ac27a0ecSDave Kleikamp goto bad_inode; 4565617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4566814525f4SDarrick J. Wong 45678e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 45688a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45698a363970STheodore Ts'o "iget: root inode unallocated"); 45708e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 45718e4b5eaeSTheodore Ts'o goto bad_inode; 45728e4b5eaeSTheodore Ts'o } 45738e4b5eaeSTheodore Ts'o 45748a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 45758a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 45768a363970STheodore Ts'o ret = -ESTALE; 45778a363970STheodore Ts'o goto bad_inode; 45788a363970STheodore Ts'o } 45798a363970STheodore Ts'o 4580814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4581814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4582814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 45832dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 45842dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 45858a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45868a363970STheodore Ts'o "iget: bad extra_isize %u " 45878a363970STheodore Ts'o "(inode size %u)", 45882dc8d9e1SEric Biggers ei->i_extra_isize, 4589814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 45906a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4591814525f4SDarrick J. Wong goto bad_inode; 4592814525f4SDarrick J. Wong } 4593814525f4SDarrick J. Wong } else 4594814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4595814525f4SDarrick J. Wong 4596814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 45979aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4598814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4599814525f4SDarrick J. Wong __u32 csum; 4600814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4601814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4602814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4603814525f4SDarrick J. Wong sizeof(inum)); 4604814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4605814525f4SDarrick J. Wong sizeof(gen)); 4606814525f4SDarrick J. Wong } 4607814525f4SDarrick J. Wong 460846f870d6STheodore Ts'o if (!ext4_inode_csum_verify(inode, raw_inode, ei) || 460946f870d6STheodore Ts'o ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) { 461054d3adbcSTheodore Ts'o ext4_error_inode_err(inode, function, line, 0, EFSBADCRC, 46118a363970STheodore Ts'o "iget: checksum invalid"); 46126a797d27SDarrick J. Wong ret = -EFSBADCRC; 4613814525f4SDarrick J. Wong goto bad_inode; 4614814525f4SDarrick J. Wong } 4615814525f4SDarrick J. Wong 4616ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 461708cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 461808cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 46190b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4620040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4621040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4622040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4623040cb378SLi Xi else 4624040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4625040cb378SLi Xi 4626ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 462708cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 462808cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4629ac27a0ecSDave Kleikamp } 463008cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 463108cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4632040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4633bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4634ac27a0ecSDave Kleikamp 4635353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 463667cf5b09STao Ma ei->i_inline_off = 0; 4637ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4638ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4639ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4640ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4641ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4642ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4643ac27a0ecSDave Kleikamp */ 4644ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4645393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4646393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4647393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4648ac27a0ecSDave Kleikamp /* this inode is deleted */ 46491d1fe1eeSDavid Howells ret = -ESTALE; 4650ac27a0ecSDave Kleikamp goto bad_inode; 4651ac27a0ecSDave Kleikamp } 4652ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4653ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4654ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4655393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4656393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4657393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4658ac27a0ecSDave Kleikamp } 4659ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4660cce6c9f7SToshi Kani ext4_set_inode_flags(inode); 46610fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 46627973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4663e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4664a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4665a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4666e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 46677e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 46688a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46698a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 46707e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 46717e6e1ef4SDarrick J. Wong goto bad_inode; 46727e6e1ef4SDarrick J. Wong } 467348a34311SJan Kara /* 467448a34311SJan Kara * If dir_index is not enabled but there's dir with INDEX flag set, 467548a34311SJan Kara * we'd normally treat htree data as empty space. But with metadata 467648a34311SJan Kara * checksumming that corrupts checksums so forbid that. 467748a34311SJan Kara */ 467848a34311SJan Kara if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) && 467948a34311SJan Kara ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) { 468048a34311SJan Kara ext4_error_inode(inode, function, line, 0, 468148a34311SJan Kara "iget: Dir with htree data on filesystem without dir_index feature."); 468248a34311SJan Kara ret = -EFSCORRUPTED; 468348a34311SJan Kara goto bad_inode; 468448a34311SJan Kara } 4685ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4686a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4687a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4688a9e7f447SDmitry Monakhov #endif 4689ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4690ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4691a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4692ac27a0ecSDave Kleikamp /* 4693ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4694ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4695ac27a0ecSDave Kleikamp */ 4696617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4697ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4698ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4699ac27a0ecSDave Kleikamp 4700b436b9beSJan Kara /* 4701b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4702b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4703b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4704b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4705b436b9beSJan Kara * now it is reread from disk. 4706b436b9beSJan Kara */ 4707b436b9beSJan Kara if (journal) { 4708b436b9beSJan Kara transaction_t *transaction; 4709b436b9beSJan Kara tid_t tid; 4710b436b9beSJan Kara 4711a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4712b436b9beSJan Kara if (journal->j_running_transaction) 4713b436b9beSJan Kara transaction = journal->j_running_transaction; 4714b436b9beSJan Kara else 4715b436b9beSJan Kara transaction = journal->j_committing_transaction; 4716b436b9beSJan Kara if (transaction) 4717b436b9beSJan Kara tid = transaction->t_tid; 4718b436b9beSJan Kara else 4719b436b9beSJan Kara tid = journal->j_commit_sequence; 4720a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4721b436b9beSJan Kara ei->i_sync_tid = tid; 4722b436b9beSJan Kara ei->i_datasync_tid = tid; 4723b436b9beSJan Kara } 4724b436b9beSJan Kara 47250040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4726ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4727ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 47282dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4729617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4730617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4731ac27a0ecSDave Kleikamp } else { 4732eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4733eb9b5f01STheodore Ts'o if (ret) 4734eb9b5f01STheodore Ts'o goto bad_inode; 4735ac27a0ecSDave Kleikamp } 4736814525f4SDarrick J. Wong } 4737ac27a0ecSDave Kleikamp 4738ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4739ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4740ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4741ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4742ef7f3835SKalpak Shah 4743ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4744ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4745ee73f9a5SJeff Layton 474625ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 474725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4748ee73f9a5SJeff Layton ivers |= 474925ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 475025ec56b5SJean Noel Cordenner } 4751e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4752c4f65706STheodore Ts'o } 475325ec56b5SJean Noel Cordenner 4754c4b5a614STheodore Ts'o ret = 0; 4755485c26ecSTheodore Ts'o if (ei->i_file_acl && 47561032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 47578a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47588a363970STheodore Ts'o "iget: bad extended attribute block %llu", 475924676da4STheodore Ts'o ei->i_file_acl); 47606a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4761485c26ecSTheodore Ts'o goto bad_inode; 4762f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4763bc716523SLiu Song /* validate the block references in the inode */ 4764bc716523SLiu Song if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4765fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4766fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4767bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4768bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4769bc716523SLiu Song else 47701f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4771fe2c8191SThiemo Nagel } 4772f19d5870STao Ma } 4773567f3e9aSTheodore Ts'o if (ret) 47747a262f7cSAneesh Kumar K.V goto bad_inode; 47757a262f7cSAneesh Kumar K.V 4776ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4777617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4778617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4779617ba13bSMingming Cao ext4_set_aops(inode); 4780ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4781617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4782617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4783ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 47846390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 47856390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 47868a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47878a363970STheodore Ts'o "iget: immutable or append flags " 47888a363970STheodore Ts'o "not allowed on symlinks"); 47896390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 47906390d33bSLuis R. Rodriguez goto bad_inode; 47916390d33bSLuis R. Rodriguez } 4792592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4793a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4794a7a67e8aSAl Viro ext4_set_aops(inode); 4795a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 479675e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4797617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4798e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4799e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4800e83c1397SDuane Griffin } else { 4801617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4802617ba13bSMingming Cao ext4_set_aops(inode); 4803ac27a0ecSDave Kleikamp } 480421fc61c7SAl Viro inode_nohighmem(inode); 4805563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4806563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4807617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4808ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4809ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4810ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4811ac27a0ecSDave Kleikamp else 4812ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4813ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4814393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4815393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4816563bdd61STheodore Ts'o } else { 48176a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 48188a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48198a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4820563bdd61STheodore Ts'o goto bad_inode; 4821ac27a0ecSDave Kleikamp } 48226456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 48236456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 48246456ca65STheodore Ts'o "casefold flag without casefold feature"); 4825ac27a0ecSDave Kleikamp brelse(iloc.bh); 4826dec214d0STahsin Erdogan 48271d1fe1eeSDavid Howells unlock_new_inode(inode); 48281d1fe1eeSDavid Howells return inode; 4829ac27a0ecSDave Kleikamp 4830ac27a0ecSDave Kleikamp bad_inode: 4831567f3e9aSTheodore Ts'o brelse(iloc.bh); 48321d1fe1eeSDavid Howells iget_failed(inode); 48331d1fe1eeSDavid Howells return ERR_PTR(ret); 4834ac27a0ecSDave Kleikamp } 4835ac27a0ecSDave Kleikamp 48360fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 48370fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 48380fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 48390fc1b451SAneesh Kumar K.V { 48400fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 484128936b62SQian Cai u64 i_blocks = READ_ONCE(inode->i_blocks); 48420fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 48430fc1b451SAneesh Kumar K.V 48440fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 48450fc1b451SAneesh Kumar K.V /* 48464907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 48470fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 48480fc1b451SAneesh Kumar K.V */ 48498180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48500fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 485184a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4852f287a1a5STheodore Ts'o return 0; 4853f287a1a5STheodore Ts'o } 4854e2b911c5SDarrick J. Wong if (!ext4_has_feature_huge_file(sb)) 4855f287a1a5STheodore Ts'o return -EFBIG; 4856f287a1a5STheodore Ts'o 4857f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 48580fc1b451SAneesh Kumar K.V /* 48590fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 48600fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 48610fc1b451SAneesh Kumar K.V */ 48628180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48630fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 486484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 48650fc1b451SAneesh Kumar K.V } else { 486684a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 48678180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 48688180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 48698180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48708180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 48710fc1b451SAneesh Kumar K.V } 4872f287a1a5STheodore Ts'o return 0; 48730fc1b451SAneesh Kumar K.V } 48740fc1b451SAneesh Kumar K.V 4875a26f4992STheodore Ts'o struct other_inode { 4876a26f4992STheodore Ts'o unsigned long orig_ino; 4877a26f4992STheodore Ts'o struct ext4_inode *raw_inode; 4878a26f4992STheodore Ts'o }; 4879a26f4992STheodore Ts'o 4880a26f4992STheodore Ts'o static int other_inode_match(struct inode * inode, unsigned long ino, 4881a26f4992STheodore Ts'o void *data) 4882a26f4992STheodore Ts'o { 4883a26f4992STheodore Ts'o struct other_inode *oi = (struct other_inode *) data; 4884a26f4992STheodore Ts'o 4885a26f4992STheodore Ts'o if ((inode->i_ino != ino) || 4886a26f4992STheodore Ts'o (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 48870e11f644SChristoph Hellwig I_DIRTY_INODE)) || 4888a26f4992STheodore Ts'o ((inode->i_state & I_DIRTY_TIME) == 0)) 4889a26f4992STheodore Ts'o return 0; 4890a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4891a26f4992STheodore Ts'o if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 48920e11f644SChristoph Hellwig I_DIRTY_INODE)) == 0) && 4893a26f4992STheodore Ts'o (inode->i_state & I_DIRTY_TIME)) { 4894a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4895a26f4992STheodore Ts'o 4896a26f4992STheodore Ts'o inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED); 4897a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4898a26f4992STheodore Ts'o 4899a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 4900a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode); 4901a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode); 4902a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode); 4903a26f4992STheodore Ts'o ext4_inode_csum_set(inode, oi->raw_inode, ei); 4904a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 4905a26f4992STheodore Ts'o trace_ext4_other_inode_update_time(inode, oi->orig_ino); 4906a26f4992STheodore Ts'o return -1; 4907a26f4992STheodore Ts'o } 4908a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4909a26f4992STheodore Ts'o return -1; 4910a26f4992STheodore Ts'o } 4911a26f4992STheodore Ts'o 4912a26f4992STheodore Ts'o /* 4913a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 4914a26f4992STheodore Ts'o * the same inode table block. 4915a26f4992STheodore Ts'o */ 4916a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 4917a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 4918a26f4992STheodore Ts'o { 4919a26f4992STheodore Ts'o struct other_inode oi; 4920a26f4992STheodore Ts'o unsigned long ino; 4921a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4922a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 4923a26f4992STheodore Ts'o 4924a26f4992STheodore Ts'o oi.orig_ino = orig_ino; 49250f0ff9a9STheodore Ts'o /* 49260f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 49270f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 49280f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 49290f0ff9a9STheodore Ts'o */ 49300f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 4931a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 4932a26f4992STheodore Ts'o if (ino == orig_ino) 4933a26f4992STheodore Ts'o continue; 4934a26f4992STheodore Ts'o oi.raw_inode = (struct ext4_inode *) buf; 4935a26f4992STheodore Ts'o (void) find_inode_nowait(sb, ino, other_inode_match, &oi); 4936a26f4992STheodore Ts'o } 4937a26f4992STheodore Ts'o } 4938a26f4992STheodore Ts'o 4939ac27a0ecSDave Kleikamp /* 4940ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4941ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4942ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4943ac27a0ecSDave Kleikamp * 4944ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4945ac27a0ecSDave Kleikamp */ 4946617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4947ac27a0ecSDave Kleikamp struct inode *inode, 4948830156c7SFrank Mayhar struct ext4_iloc *iloc) 4949ac27a0ecSDave Kleikamp { 4950617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4951617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4952ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4953202ee5dfSTheodore Ts'o struct super_block *sb = inode->i_sb; 4954ac27a0ecSDave Kleikamp int err = 0, rc, block; 4955202ee5dfSTheodore Ts'o int need_datasync = 0, set_large_file = 0; 495608cefc7aSEric W. Biederman uid_t i_uid; 495708cefc7aSEric W. Biederman gid_t i_gid; 4958040cb378SLi Xi projid_t i_projid; 4959ac27a0ecSDave Kleikamp 4960202ee5dfSTheodore Ts'o spin_lock(&ei->i_raw_lock); 4961202ee5dfSTheodore Ts'o 4962202ee5dfSTheodore Ts'o /* For fields not tracked in the in-memory inode, 4963ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 496419f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4965617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4966ac27a0ecSDave Kleikamp 4967ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 496808cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 496908cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4970040cb378SLi Xi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 4971ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 497208cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 497308cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4974ac27a0ecSDave Kleikamp /* 4975ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4976ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4977ac27a0ecSDave Kleikamp */ 497893e3b4e6SDaeho Jeong if (ei->i_dtime && list_empty(&ei->i_orphan)) { 497993e3b4e6SDaeho Jeong raw_inode->i_uid_high = 0; 498093e3b4e6SDaeho Jeong raw_inode->i_gid_high = 0; 498193e3b4e6SDaeho Jeong } else { 4982ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 498308cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4984ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 498508cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4986ac27a0ecSDave Kleikamp } 4987ac27a0ecSDave Kleikamp } else { 498808cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 498908cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4990ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4991ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4992ac27a0ecSDave Kleikamp } 4993ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4994ef7f3835SKalpak Shah 4995ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4996ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4997ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4998ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4999ef7f3835SKalpak Shah 5000bce92d56SLi Xi err = ext4_inode_blocks_set(handle, raw_inode, ei); 5001bce92d56SLi Xi if (err) { 5002202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 50030fc1b451SAneesh Kumar K.V goto out_brelse; 5004202ee5dfSTheodore Ts'o } 5005ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 5006353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 5007ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 5008a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5009a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 50107973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5011dce8e237SQiujun Huang if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) { 5012a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5013b71fc079SJan Kara need_datasync = 1; 5014b71fc079SJan Kara } 5015ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5016e2b911c5SDarrick J. Wong if (!ext4_has_feature_large_file(sb) || 5017617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5018202ee5dfSTheodore Ts'o cpu_to_le32(EXT4_GOOD_OLD_REV)) 5019202ee5dfSTheodore Ts'o set_large_file = 1; 5020ac27a0ecSDave Kleikamp } 5021ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5022ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5023ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5024ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5025ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5026ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5027ac27a0ecSDave Kleikamp } else { 5028ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5029ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5030ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5031ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5032ac27a0ecSDave Kleikamp } 5033f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 5034de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5035ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5036f19d5870STao Ma } 5037ac27a0ecSDave Kleikamp 5038ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 5039e254d1afSEryu Guan u64 ivers = ext4_inode_peek_iversion(inode); 5040ee73f9a5SJeff Layton 5041ee73f9a5SJeff Layton raw_inode->i_disk_version = cpu_to_le32(ivers); 504225ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 504325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 504425ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 5045ee73f9a5SJeff Layton cpu_to_le32(ivers >> 32); 5046c4f65706STheodore Ts'o raw_inode->i_extra_isize = 5047c4f65706STheodore Ts'o cpu_to_le16(ei->i_extra_isize); 5048c4f65706STheodore Ts'o } 504925ec56b5SJean Noel Cordenner } 5050040cb378SLi Xi 50510b7b7779SKaho Ng BUG_ON(!ext4_has_feature_project(inode->i_sb) && 5052040cb378SLi Xi i_projid != EXT4_DEF_PROJID); 5053040cb378SLi Xi 5054040cb378SLi Xi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 5055040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 5056040cb378SLi Xi raw_inode->i_projid = cpu_to_le32(i_projid); 5057040cb378SLi Xi 5058814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 5059202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 50601751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 5061a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5062a26f4992STheodore Ts'o bh->b_data); 5063202ee5dfSTheodore Ts'o 50640390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 506573b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 5066ac27a0ecSDave Kleikamp if (!err) 5067ac27a0ecSDave Kleikamp err = rc; 506819f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5069202ee5dfSTheodore Ts'o if (set_large_file) { 50705d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5071202ee5dfSTheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5072202ee5dfSTheodore Ts'o if (err) 5073202ee5dfSTheodore Ts'o goto out_brelse; 5074e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 5075202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5076202ee5dfSTheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 5077202ee5dfSTheodore Ts'o } 5078b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5079ac27a0ecSDave Kleikamp out_brelse: 5080ac27a0ecSDave Kleikamp brelse(bh); 5081617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5082ac27a0ecSDave Kleikamp return err; 5083ac27a0ecSDave Kleikamp } 5084ac27a0ecSDave Kleikamp 5085ac27a0ecSDave Kleikamp /* 5086617ba13bSMingming Cao * ext4_write_inode() 5087ac27a0ecSDave Kleikamp * 5088ac27a0ecSDave Kleikamp * We are called from a few places: 5089ac27a0ecSDave Kleikamp * 509087f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5091ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 50924907cb7bSAnatol Pomozov * transaction to commit. 5093ac27a0ecSDave Kleikamp * 509487f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 509587f7e416STheodore Ts'o * We wait on commit, if told to. 5096ac27a0ecSDave Kleikamp * 509787f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 509887f7e416STheodore Ts'o * We wait on commit, if told to. 5099ac27a0ecSDave Kleikamp * 5100ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5101ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 510287f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 510387f7e416STheodore Ts'o * writeback. 5104ac27a0ecSDave Kleikamp * 5105ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5106ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5107ac27a0ecSDave Kleikamp * which we are interested. 5108ac27a0ecSDave Kleikamp * 5109ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5110ac27a0ecSDave Kleikamp * 5111ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5112ac27a0ecSDave Kleikamp * stuff(); 5113ac27a0ecSDave Kleikamp * inode->i_size = expr; 5114ac27a0ecSDave Kleikamp * 511587f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 511687f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 511787f7e416STheodore Ts'o * superblock's dirty inode list. 5118ac27a0ecSDave Kleikamp */ 5119a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5120ac27a0ecSDave Kleikamp { 512191ac6f43SFrank Mayhar int err; 512291ac6f43SFrank Mayhar 512318f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 512418f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5125ac27a0ecSDave Kleikamp return 0; 5126ac27a0ecSDave Kleikamp 512718f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 512818f2c4fcSTheodore Ts'o return -EIO; 512918f2c4fcSTheodore Ts'o 513091ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5131617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5132b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5133ac27a0ecSDave Kleikamp dump_stack(); 5134ac27a0ecSDave Kleikamp return -EIO; 5135ac27a0ecSDave Kleikamp } 5136ac27a0ecSDave Kleikamp 513710542c22SJan Kara /* 513810542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 513910542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 514010542c22SJan Kara * written. 514110542c22SJan Kara */ 514210542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5143ac27a0ecSDave Kleikamp return 0; 5144ac27a0ecSDave Kleikamp 514518f2c4fcSTheodore Ts'o err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, 514618f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 514791ac6f43SFrank Mayhar } else { 514891ac6f43SFrank Mayhar struct ext4_iloc iloc; 514991ac6f43SFrank Mayhar 51508b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 515191ac6f43SFrank Mayhar if (err) 515291ac6f43SFrank Mayhar return err; 515310542c22SJan Kara /* 515410542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 515510542c22SJan Kara * it here separately for each inode. 515610542c22SJan Kara */ 515710542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5158830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5159830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 516054d3adbcSTheodore Ts'o ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO, 5161c398eda0STheodore Ts'o "IO error syncing inode"); 5162830156c7SFrank Mayhar err = -EIO; 5163830156c7SFrank Mayhar } 5164fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 516591ac6f43SFrank Mayhar } 516691ac6f43SFrank Mayhar return err; 5167ac27a0ecSDave Kleikamp } 5168ac27a0ecSDave Kleikamp 5169ac27a0ecSDave Kleikamp /* 517053e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 517153e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 517253e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 517353e87268SJan Kara */ 517453e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 517553e87268SJan Kara { 517653e87268SJan Kara struct page *page; 517753e87268SJan Kara unsigned offset; 517853e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 517953e87268SJan Kara tid_t commit_tid = 0; 518053e87268SJan Kara int ret; 518153e87268SJan Kara 518209cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 518353e87268SJan Kara /* 5184565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5185565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5186565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5187565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5188565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5189565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5190565333a1Syangerkun * blocksize == PAGESIZE. 519153e87268SJan Kara */ 5192565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 519353e87268SJan Kara return; 519453e87268SJan Kara while (1) { 519553e87268SJan Kara page = find_lock_page(inode->i_mapping, 519609cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 519753e87268SJan Kara if (!page) 519853e87268SJan Kara return; 5199ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 520009cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 520153e87268SJan Kara unlock_page(page); 520209cbfeafSKirill A. Shutemov put_page(page); 520353e87268SJan Kara if (ret != -EBUSY) 520453e87268SJan Kara return; 520553e87268SJan Kara commit_tid = 0; 520653e87268SJan Kara read_lock(&journal->j_state_lock); 520753e87268SJan Kara if (journal->j_committing_transaction) 520853e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 520953e87268SJan Kara read_unlock(&journal->j_state_lock); 521053e87268SJan Kara if (commit_tid) 521153e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 521253e87268SJan Kara } 521353e87268SJan Kara } 521453e87268SJan Kara 521553e87268SJan Kara /* 5216617ba13bSMingming Cao * ext4_setattr() 5217ac27a0ecSDave Kleikamp * 5218ac27a0ecSDave Kleikamp * Called from notify_change. 5219ac27a0ecSDave Kleikamp * 5220ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5221ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5222ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5223ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5224ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5225ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5226ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5227ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5228ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5229ac27a0ecSDave Kleikamp * 5230678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5231678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5232678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5233678aaf48SJan Kara * This way we are sure that all the data written in the previous 5234678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5235678aaf48SJan Kara * writeback). 5236678aaf48SJan Kara * 5237678aaf48SJan Kara * Called with inode->i_mutex down. 5238ac27a0ecSDave Kleikamp */ 5239617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5240ac27a0ecSDave Kleikamp { 52412b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5242ac27a0ecSDave Kleikamp int error, rc = 0; 52433d287de3SDmitry Monakhov int orphan = 0; 5244ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5245ac27a0ecSDave Kleikamp 52460db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 52470db1ff22STheodore Ts'o return -EIO; 52480db1ff22STheodore Ts'o 524902b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 525002b016caSTheodore Ts'o return -EPERM; 525102b016caSTheodore Ts'o 525202b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 525302b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 525402b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 525502b016caSTheodore Ts'o return -EPERM; 525602b016caSTheodore Ts'o 525731051c85SJan Kara error = setattr_prepare(dentry, attr); 5258ac27a0ecSDave Kleikamp if (error) 5259ac27a0ecSDave Kleikamp return error; 5260ac27a0ecSDave Kleikamp 52613ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 52623ce2b8ddSEric Biggers if (error) 52633ce2b8ddSEric Biggers return error; 52643ce2b8ddSEric Biggers 5265c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5266c93d8f88SEric Biggers if (error) 5267c93d8f88SEric Biggers return error; 5268c93d8f88SEric Biggers 5269a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5270a7cdadeeSJan Kara error = dquot_initialize(inode); 5271a7cdadeeSJan Kara if (error) 5272a7cdadeeSJan Kara return error; 5273a7cdadeeSJan Kara } 527408cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 527508cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5276ac27a0ecSDave Kleikamp handle_t *handle; 5277ac27a0ecSDave Kleikamp 5278ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5279ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 52809924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 52819924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5282194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5283ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5284ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5285ac27a0ecSDave Kleikamp goto err_out; 5286ac27a0ecSDave Kleikamp } 52877a9ca53aSTahsin Erdogan 52887a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 52897a9ca53aSTahsin Erdogan * counts xattr inode references. 52907a9ca53aSTahsin Erdogan */ 52917a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5292b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 52937a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 52947a9ca53aSTahsin Erdogan 5295ac27a0ecSDave Kleikamp if (error) { 5296617ba13bSMingming Cao ext4_journal_stop(handle); 5297ac27a0ecSDave Kleikamp return error; 5298ac27a0ecSDave Kleikamp } 5299ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5300ac27a0ecSDave Kleikamp * one transaction */ 5301ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5302ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5303ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5304ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5305617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5306617ba13bSMingming Cao ext4_journal_stop(handle); 5307*4209ae12SHarshad Shirwadkar if (unlikely(error)) 5308*4209ae12SHarshad Shirwadkar return error; 5309ac27a0ecSDave Kleikamp } 5310ac27a0ecSDave Kleikamp 53113da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 53125208386cSJan Kara handle_t *handle; 53133da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5314b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5315562c72aaSChristoph Hellwig 531612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5317e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5318e2b46574SEric Sandeen 53190c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 53200c095c7fSTheodore Ts'o return -EFBIG; 5321e2b46574SEric Sandeen } 53223da40c7bSJosef Bacik if (!S_ISREG(inode->i_mode)) 53233da40c7bSJosef Bacik return -EINVAL; 5324dff6efc3SChristoph Hellwig 5325dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5326dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5327dff6efc3SChristoph Hellwig 5328b9c1c267SJan Kara if (shrink) { 5329b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 53305208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 53315208386cSJan Kara attr->ia_size); 53325208386cSJan Kara if (error) 53335208386cSJan Kara goto err_out; 53345208386cSJan Kara } 5335b9c1c267SJan Kara /* 5336b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5337b9c1c267SJan Kara * for dio in flight. 5338b9c1c267SJan Kara */ 5339b9c1c267SJan Kara inode_dio_wait(inode); 5340b9c1c267SJan Kara } 5341b9c1c267SJan Kara 5342b9c1c267SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5343b9c1c267SJan Kara 5344b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5345b9c1c267SJan Kara if (rc) { 5346b9c1c267SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 5347b9c1c267SJan Kara return rc; 5348b9c1c267SJan Kara } 5349b9c1c267SJan Kara 53503da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 53519924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5352ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5353ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5354b9c1c267SJan Kara goto out_mmap_sem; 5355ac27a0ecSDave Kleikamp } 53563da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5357617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 53583d287de3SDmitry Monakhov orphan = 1; 53593d287de3SDmitry Monakhov } 5360911af577SEryu Guan /* 5361911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5362911af577SEryu Guan * update c/mtime in shrink case below 5363911af577SEryu Guan */ 5364911af577SEryu Guan if (!shrink) { 5365eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5366911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5367911af577SEryu Guan } 536890e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5369617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5370617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5371ac27a0ecSDave Kleikamp if (!error) 5372ac27a0ecSDave Kleikamp error = rc; 537390e775b7SJan Kara /* 537490e775b7SJan Kara * We have to update i_size under i_data_sem together 537590e775b7SJan Kara * with i_disksize to avoid races with writeback code 537690e775b7SJan Kara * running ext4_wb_update_i_disksize(). 537790e775b7SJan Kara */ 537890e775b7SJan Kara if (!error) 537990e775b7SJan Kara i_size_write(inode, attr->ia_size); 538090e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5381617ba13bSMingming Cao ext4_journal_stop(handle); 5382b9c1c267SJan Kara if (error) 5383b9c1c267SJan Kara goto out_mmap_sem; 538482a25b02SJan Kara if (!shrink) { 5385b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5386b9c1c267SJan Kara inode->i_size); 5387b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 538882a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5389b9c1c267SJan Kara } 5390430657b6SRoss Zwisler } 5391430657b6SRoss Zwisler 539253e87268SJan Kara /* 539353e87268SJan Kara * Truncate pagecache after we've waited for commit 539453e87268SJan Kara * in data=journal mode to make pages freeable. 539553e87268SJan Kara */ 53967caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5397b9c1c267SJan Kara /* 5398b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5399b9c1c267SJan Kara * truncate possible preallocated blocks. 5400b9c1c267SJan Kara */ 5401b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 54022c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 54032c98eb5eSTheodore Ts'o if (rc) 54042c98eb5eSTheodore Ts'o error = rc; 54052c98eb5eSTheodore Ts'o } 5406b9c1c267SJan Kara out_mmap_sem: 5407ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 54083da40c7bSJosef Bacik } 5409ac27a0ecSDave Kleikamp 54102c98eb5eSTheodore Ts'o if (!error) { 54111025774cSChristoph Hellwig setattr_copy(inode, attr); 54121025774cSChristoph Hellwig mark_inode_dirty(inode); 54131025774cSChristoph Hellwig } 54141025774cSChristoph Hellwig 54151025774cSChristoph Hellwig /* 54161025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 54171025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 54181025774cSChristoph Hellwig */ 54193d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5420617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5421ac27a0ecSDave Kleikamp 54222c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 542364e178a7SChristoph Hellwig rc = posix_acl_chmod(inode, inode->i_mode); 5424ac27a0ecSDave Kleikamp 5425ac27a0ecSDave Kleikamp err_out: 5426617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5427ac27a0ecSDave Kleikamp if (!error) 5428ac27a0ecSDave Kleikamp error = rc; 5429ac27a0ecSDave Kleikamp return error; 5430ac27a0ecSDave Kleikamp } 5431ac27a0ecSDave Kleikamp 5432a528d35eSDavid Howells int ext4_getattr(const struct path *path, struct kstat *stat, 5433a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 54343e3398a0SMingming Cao { 543599652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 543699652ea5SDavid Howells struct ext4_inode *raw_inode; 543799652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 543899652ea5SDavid Howells unsigned int flags; 54393e3398a0SMingming Cao 5440d4c5e960STheodore Ts'o if ((request_mask & STATX_BTIME) && 5441d4c5e960STheodore Ts'o EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 544299652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 544399652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 544499652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 544599652ea5SDavid Howells } 544699652ea5SDavid Howells 544799652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 544899652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 544999652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 545099652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 545199652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 545299652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 545399652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 545499652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 545599652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 545699652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 545799652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 54581f607195SEric Biggers if (flags & EXT4_VERITY_FL) 54591f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 546099652ea5SDavid Howells 54613209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 54623209f68bSDavid Howells STATX_ATTR_COMPRESSED | 54633209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 54643209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 54651f607195SEric Biggers STATX_ATTR_NODUMP | 54661f607195SEric Biggers STATX_ATTR_VERITY); 54673209f68bSDavid Howells 54683e3398a0SMingming Cao generic_fillattr(inode, stat); 546999652ea5SDavid Howells return 0; 547099652ea5SDavid Howells } 547199652ea5SDavid Howells 547299652ea5SDavid Howells int ext4_file_getattr(const struct path *path, struct kstat *stat, 547399652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 547499652ea5SDavid Howells { 547599652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 547699652ea5SDavid Howells u64 delalloc_blocks; 547799652ea5SDavid Howells 547899652ea5SDavid Howells ext4_getattr(path, stat, request_mask, query_flags); 54793e3398a0SMingming Cao 54803e3398a0SMingming Cao /* 54819206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 54829206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 54839206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5484d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 54859206c561SAndreas Dilger */ 54869206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 54879206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 54889206c561SAndreas Dilger 54899206c561SAndreas Dilger /* 54903e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 54913e3398a0SMingming Cao * otherwise in the case of system crash before the real block 54923e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 54933e3398a0SMingming Cao * on-disk file blocks. 54943e3398a0SMingming Cao * We always keep i_blocks updated together with real 54953e3398a0SMingming Cao * allocation. But to not confuse with user, stat 54963e3398a0SMingming Cao * will return the blocks that include the delayed allocation 54973e3398a0SMingming Cao * blocks for this file. 54983e3398a0SMingming Cao */ 549996607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 550096607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 55018af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 55023e3398a0SMingming Cao return 0; 55033e3398a0SMingming Cao } 5504ac27a0ecSDave Kleikamp 5505fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5506fffb2739SJan Kara int pextents) 5507a02908f1SMingming Cao { 550812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5509fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5510fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5511a02908f1SMingming Cao } 5512ac51d837STheodore Ts'o 5513a02908f1SMingming Cao /* 5514a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5515a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5516a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5517a02908f1SMingming Cao * 5518a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 55194907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5520a02908f1SMingming Cao * they could still across block group boundary. 5521a02908f1SMingming Cao * 5522a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5523a02908f1SMingming Cao */ 5524dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5525fffb2739SJan Kara int pextents) 5526a02908f1SMingming Cao { 55278df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 55288df9675fSTheodore Ts'o int gdpblocks; 5529a02908f1SMingming Cao int idxblocks; 5530a02908f1SMingming Cao int ret = 0; 5531a02908f1SMingming Cao 5532a02908f1SMingming Cao /* 5533fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5534fffb2739SJan Kara * to @pextents physical extents? 5535a02908f1SMingming Cao */ 5536fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5537a02908f1SMingming Cao 5538a02908f1SMingming Cao ret = idxblocks; 5539a02908f1SMingming Cao 5540a02908f1SMingming Cao /* 5541a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5542a02908f1SMingming Cao * to account 5543a02908f1SMingming Cao */ 5544fffb2739SJan Kara groups = idxblocks + pextents; 5545a02908f1SMingming Cao gdpblocks = groups; 55468df9675fSTheodore Ts'o if (groups > ngroups) 55478df9675fSTheodore Ts'o groups = ngroups; 5548a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5549a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5550a02908f1SMingming Cao 5551a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5552a02908f1SMingming Cao ret += groups + gdpblocks; 5553a02908f1SMingming Cao 5554a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5555a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5556ac27a0ecSDave Kleikamp 5557ac27a0ecSDave Kleikamp return ret; 5558ac27a0ecSDave Kleikamp } 5559ac27a0ecSDave Kleikamp 5560ac27a0ecSDave Kleikamp /* 556125985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5562f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5563f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5564a02908f1SMingming Cao * 5565525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5566a02908f1SMingming Cao * 5567525f4ed8SMingming Cao * We need to consider the worse case, when 5568a02908f1SMingming Cao * one new block per extent. 5569a02908f1SMingming Cao */ 5570a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5571a02908f1SMingming Cao { 5572a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5573a02908f1SMingming Cao int ret; 5574a02908f1SMingming Cao 5575fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5576a02908f1SMingming Cao 5577a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5578a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5579a02908f1SMingming Cao ret += bpp; 5580a02908f1SMingming Cao return ret; 5581a02908f1SMingming Cao } 5582f3bd1f3fSMingming Cao 5583f3bd1f3fSMingming Cao /* 5584f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5585f3bd1f3fSMingming Cao * 5586f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 558779e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5588f3bd1f3fSMingming Cao * 5589f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5590f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5591f3bd1f3fSMingming Cao */ 5592f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5593f3bd1f3fSMingming Cao { 5594f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5595f3bd1f3fSMingming Cao } 5596f3bd1f3fSMingming Cao 5597a02908f1SMingming Cao /* 5598617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5599ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5600ac27a0ecSDave Kleikamp */ 5601617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5602617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5603ac27a0ecSDave Kleikamp { 5604ac27a0ecSDave Kleikamp int err = 0; 5605ac27a0ecSDave Kleikamp 5606a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5607a6758309SVasily Averin put_bh(iloc->bh); 56080db1ff22STheodore Ts'o return -EIO; 5609a6758309SVasily Averin } 5610c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 561125ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 561225ec56b5SJean Noel Cordenner 5613ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5614ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5615ac27a0ecSDave Kleikamp 5616dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5617830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5618ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5619ac27a0ecSDave Kleikamp return err; 5620ac27a0ecSDave Kleikamp } 5621ac27a0ecSDave Kleikamp 5622ac27a0ecSDave Kleikamp /* 5623ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5624ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5625ac27a0ecSDave Kleikamp */ 5626ac27a0ecSDave Kleikamp 5627ac27a0ecSDave Kleikamp int 5628617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5629617ba13bSMingming Cao struct ext4_iloc *iloc) 5630ac27a0ecSDave Kleikamp { 56310390131bSFrank Mayhar int err; 56320390131bSFrank Mayhar 56330db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 56340db1ff22STheodore Ts'o return -EIO; 56350db1ff22STheodore Ts'o 5636617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5637ac27a0ecSDave Kleikamp if (!err) { 5638ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5639617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5640ac27a0ecSDave Kleikamp if (err) { 5641ac27a0ecSDave Kleikamp brelse(iloc->bh); 5642ac27a0ecSDave Kleikamp iloc->bh = NULL; 5643ac27a0ecSDave Kleikamp } 5644ac27a0ecSDave Kleikamp } 5645617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5646ac27a0ecSDave Kleikamp return err; 5647ac27a0ecSDave Kleikamp } 5648ac27a0ecSDave Kleikamp 5649c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5650c03b45b8SMiao Xie unsigned int new_extra_isize, 5651c03b45b8SMiao Xie struct ext4_iloc *iloc, 5652c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5653c03b45b8SMiao Xie { 5654c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5655c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 56564ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 56574ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5658c03b45b8SMiao Xie int error; 5659c03b45b8SMiao Xie 56604ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 56614ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 56624ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 56634ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 56644ea99936STheodore Ts'o ei->i_extra_isize, 56654ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 56664ea99936STheodore Ts'o return -EFSCORRUPTED; 56674ea99936STheodore Ts'o } 56684ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 56694ea99936STheodore Ts'o (new_extra_isize < 4) || 56704ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 56714ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 56724ea99936STheodore Ts'o 5673c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5674c03b45b8SMiao Xie 5675c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5676c03b45b8SMiao Xie 5677c03b45b8SMiao Xie /* No extended attributes present */ 5678c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5679c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5680c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5681c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5682c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5683c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5684c03b45b8SMiao Xie return 0; 5685c03b45b8SMiao Xie } 5686c03b45b8SMiao Xie 5687c03b45b8SMiao Xie /* try to expand with EAs present */ 5688c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5689c03b45b8SMiao Xie raw_inode, handle); 5690c03b45b8SMiao Xie if (error) { 5691c03b45b8SMiao Xie /* 5692c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5693c03b45b8SMiao Xie */ 5694c03b45b8SMiao Xie *no_expand = 1; 5695c03b45b8SMiao Xie } 5696c03b45b8SMiao Xie 5697c03b45b8SMiao Xie return error; 5698c03b45b8SMiao Xie } 5699c03b45b8SMiao Xie 5700ac27a0ecSDave Kleikamp /* 57016dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 57026dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 57036dd4ee7cSKalpak Shah */ 5704cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 57051d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 57061d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 57071d03ec98SAneesh Kumar K.V handle_t *handle) 57086dd4ee7cSKalpak Shah { 57093b10fdc6SMiao Xie int no_expand; 57103b10fdc6SMiao Xie int error; 57116dd4ee7cSKalpak Shah 5712cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5713cf0a5e81SMiao Xie return -EOVERFLOW; 5714cf0a5e81SMiao Xie 5715cf0a5e81SMiao Xie /* 5716cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5717cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5718cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5719cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5720cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5721cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5722cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5723cf0a5e81SMiao Xie */ 57246cb367c2SJan Kara if (ext4_journal_extend(handle, 572583448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5726cf0a5e81SMiao Xie return -ENOSPC; 57276dd4ee7cSKalpak Shah 57283b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5729cf0a5e81SMiao Xie return -EBUSY; 57303b10fdc6SMiao Xie 5731c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5732c03b45b8SMiao Xie handle, &no_expand); 57333b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5734c03b45b8SMiao Xie 5735c03b45b8SMiao Xie return error; 57366dd4ee7cSKalpak Shah } 57376dd4ee7cSKalpak Shah 5738c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5739c03b45b8SMiao Xie unsigned int new_extra_isize, 5740c03b45b8SMiao Xie struct ext4_iloc *iloc) 5741c03b45b8SMiao Xie { 5742c03b45b8SMiao Xie handle_t *handle; 5743c03b45b8SMiao Xie int no_expand; 5744c03b45b8SMiao Xie int error, rc; 5745c03b45b8SMiao Xie 5746c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5747c03b45b8SMiao Xie brelse(iloc->bh); 5748c03b45b8SMiao Xie return -EOVERFLOW; 5749c03b45b8SMiao Xie } 5750c03b45b8SMiao Xie 5751c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5752c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5753c03b45b8SMiao Xie if (IS_ERR(handle)) { 5754c03b45b8SMiao Xie error = PTR_ERR(handle); 5755c03b45b8SMiao Xie brelse(iloc->bh); 5756c03b45b8SMiao Xie return error; 5757c03b45b8SMiao Xie } 5758c03b45b8SMiao Xie 5759c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5760c03b45b8SMiao Xie 5761ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5762c03b45b8SMiao Xie error = ext4_journal_get_write_access(handle, iloc->bh); 57633b10fdc6SMiao Xie if (error) { 5764c03b45b8SMiao Xie brelse(iloc->bh); 57657f420d64SDan Carpenter goto out_unlock; 57663b10fdc6SMiao Xie } 5767cf0a5e81SMiao Xie 5768c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5769c03b45b8SMiao Xie handle, &no_expand); 5770c03b45b8SMiao Xie 5771c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5772c03b45b8SMiao Xie if (!error) 5773c03b45b8SMiao Xie error = rc; 5774c03b45b8SMiao Xie 57757f420d64SDan Carpenter out_unlock: 5776c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5777c03b45b8SMiao Xie ext4_journal_stop(handle); 57783b10fdc6SMiao Xie return error; 57796dd4ee7cSKalpak Shah } 57806dd4ee7cSKalpak Shah 57816dd4ee7cSKalpak Shah /* 5782ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5783ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5784ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5785ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5786ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5787ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5788ac27a0ecSDave Kleikamp * 5789ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5790ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5791ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5792ac27a0ecSDave Kleikamp * we start and wait on commits. 5793ac27a0ecSDave Kleikamp */ 5794*4209ae12SHarshad Shirwadkar int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode, 5795*4209ae12SHarshad Shirwadkar const char *func, unsigned int line) 5796ac27a0ecSDave Kleikamp { 5797617ba13bSMingming Cao struct ext4_iloc iloc; 57986dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5799cf0a5e81SMiao Xie int err; 5800ac27a0ecSDave Kleikamp 5801ac27a0ecSDave Kleikamp might_sleep(); 58027ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5803617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58045e1021f2SEryu Guan if (err) 5805*4209ae12SHarshad Shirwadkar goto out; 5806cf0a5e81SMiao Xie 5807cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5808cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 58096dd4ee7cSKalpak Shah iloc, handle); 5810cf0a5e81SMiao Xie 5811*4209ae12SHarshad Shirwadkar err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5812*4209ae12SHarshad Shirwadkar out: 5813*4209ae12SHarshad Shirwadkar if (unlikely(err)) 5814*4209ae12SHarshad Shirwadkar ext4_error_inode_err(inode, func, line, 0, err, 5815*4209ae12SHarshad Shirwadkar "mark_inode_dirty error"); 5816*4209ae12SHarshad Shirwadkar return err; 5817ac27a0ecSDave Kleikamp } 5818ac27a0ecSDave Kleikamp 5819ac27a0ecSDave Kleikamp /* 5820617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5821ac27a0ecSDave Kleikamp * 5822ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5823ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5824ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5825ac27a0ecSDave Kleikamp * 58265dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5827ac27a0ecSDave Kleikamp * are allocated to the file. 5828ac27a0ecSDave Kleikamp * 5829ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5830ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5831ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 58320ae45f63STheodore Ts'o * 58330ae45f63STheodore Ts'o * If only the I_DIRTY_TIME flag is set, we can skip everything. If 58340ae45f63STheodore Ts'o * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need 58350ae45f63STheodore Ts'o * to copy into the on-disk inode structure are the timestamp files. 5836ac27a0ecSDave Kleikamp */ 5837aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5838ac27a0ecSDave Kleikamp { 5839ac27a0ecSDave Kleikamp handle_t *handle; 5840ac27a0ecSDave Kleikamp 58410ae45f63STheodore Ts'o if (flags == I_DIRTY_TIME) 58420ae45f63STheodore Ts'o return; 58439924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5844ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5845ac27a0ecSDave Kleikamp goto out; 5846f3dc272fSCurt Wohlgemuth 5847617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5848f3dc272fSCurt Wohlgemuth 5849617ba13bSMingming Cao ext4_journal_stop(handle); 5850ac27a0ecSDave Kleikamp out: 5851ac27a0ecSDave Kleikamp return; 5852ac27a0ecSDave Kleikamp } 5853ac27a0ecSDave Kleikamp 5854617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5855ac27a0ecSDave Kleikamp { 5856ac27a0ecSDave Kleikamp journal_t *journal; 5857ac27a0ecSDave Kleikamp handle_t *handle; 5858ac27a0ecSDave Kleikamp int err; 5859c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5860ac27a0ecSDave Kleikamp 5861ac27a0ecSDave Kleikamp /* 5862ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5863ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5864ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5865ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5866ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5867ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5868ac27a0ecSDave Kleikamp * nobody is changing anything. 5869ac27a0ecSDave Kleikamp */ 5870ac27a0ecSDave Kleikamp 5871617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 58720390131bSFrank Mayhar if (!journal) 58730390131bSFrank Mayhar return 0; 5874d699594dSDave Hansen if (is_journal_aborted(journal)) 5875ac27a0ecSDave Kleikamp return -EROFS; 5876ac27a0ecSDave Kleikamp 587717335dccSDmitry Monakhov /* Wait for all existing dio workers */ 587817335dccSDmitry Monakhov inode_dio_wait(inode); 587917335dccSDmitry Monakhov 58804c546592SDaeho Jeong /* 58814c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 58824c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 58834c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 58844c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 58854c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 58864c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 58874c546592SDaeho Jeong */ 58884c546592SDaeho Jeong if (val) { 58894c546592SDaeho Jeong down_write(&EXT4_I(inode)->i_mmap_sem); 58904c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 58914c546592SDaeho Jeong if (err < 0) { 58924c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 58934c546592SDaeho Jeong return err; 58944c546592SDaeho Jeong } 58954c546592SDaeho Jeong } 58964c546592SDaeho Jeong 5897bbd55937SEric Biggers percpu_down_write(&sbi->s_writepages_rwsem); 5898dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5899ac27a0ecSDave Kleikamp 5900ac27a0ecSDave Kleikamp /* 5901ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5902ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5903ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5904ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5905ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5906ac27a0ecSDave Kleikamp */ 5907ac27a0ecSDave Kleikamp 5908ac27a0ecSDave Kleikamp if (val) 590912e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59105872ddaaSYongqiang Yang else { 59114f879ca6SJan Kara err = jbd2_journal_flush(journal); 59124f879ca6SJan Kara if (err < 0) { 59134f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 5914bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 59154f879ca6SJan Kara return err; 59164f879ca6SJan Kara } 591712e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 59185872ddaaSYongqiang Yang } 5919617ba13bSMingming Cao ext4_set_aops(inode); 5920ac27a0ecSDave Kleikamp 5921dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5922bbd55937SEric Biggers percpu_up_write(&sbi->s_writepages_rwsem); 5923c8585c6fSDaeho Jeong 59244c546592SDaeho Jeong if (val) 59254c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 5926ac27a0ecSDave Kleikamp 5927ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5928ac27a0ecSDave Kleikamp 59299924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5930ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5931ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5932ac27a0ecSDave Kleikamp 5933617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 59340390131bSFrank Mayhar ext4_handle_sync(handle); 5935617ba13bSMingming Cao ext4_journal_stop(handle); 5936617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5937ac27a0ecSDave Kleikamp 5938ac27a0ecSDave Kleikamp return err; 5939ac27a0ecSDave Kleikamp } 59402e9ee850SAneesh Kumar K.V 59412e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 59422e9ee850SAneesh Kumar K.V { 59432e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 59442e9ee850SAneesh Kumar K.V } 59452e9ee850SAneesh Kumar K.V 5946401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 59472e9ee850SAneesh Kumar K.V { 594811bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 5949c2ec175cSNick Piggin struct page *page = vmf->page; 59502e9ee850SAneesh Kumar K.V loff_t size; 59512e9ee850SAneesh Kumar K.V unsigned long len; 5952401b25aaSSouptick Joarder int err; 5953401b25aaSSouptick Joarder vm_fault_t ret; 59542e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5955496ad9aaSAl Viro struct inode *inode = file_inode(file); 59562e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 59579ea7df53SJan Kara handle_t *handle; 59589ea7df53SJan Kara get_block_t *get_block; 59599ea7df53SJan Kara int retries = 0; 59602e9ee850SAneesh Kumar K.V 596102b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 596202b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 596302b016caSTheodore Ts'o 59648e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5965041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 5966ea3d7209SJan Kara 5967ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 59687b4cc978SEric Biggers 5969401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 5970401b25aaSSouptick Joarder if (err) 59717b4cc978SEric Biggers goto out_ret; 59727b4cc978SEric Biggers 59739ea7df53SJan Kara /* Delalloc case is easy... */ 59749ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 59759ea7df53SJan Kara !ext4_should_journal_data(inode) && 59769ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 59779ea7df53SJan Kara do { 5978401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 59799ea7df53SJan Kara ext4_da_get_block_prep); 5980401b25aaSSouptick Joarder } while (err == -ENOSPC && 59819ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 59829ea7df53SJan Kara goto out_ret; 59832e9ee850SAneesh Kumar K.V } 59840e499890SDarrick J. Wong 59850e499890SDarrick J. Wong lock_page(page); 59869ea7df53SJan Kara size = i_size_read(inode); 59879ea7df53SJan Kara /* Page got truncated from under us? */ 59889ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 59899ea7df53SJan Kara unlock_page(page); 59909ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 59919ea7df53SJan Kara goto out; 59920e499890SDarrick J. Wong } 59932e9ee850SAneesh Kumar K.V 599409cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 599509cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 59962e9ee850SAneesh Kumar K.V else 599709cbfeafSKirill A. Shutemov len = PAGE_SIZE; 5998a827eaffSAneesh Kumar K.V /* 59999ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 60009ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 6001a827eaffSAneesh Kumar K.V */ 60022e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 6003f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 6004f19d5870STao Ma 0, len, NULL, 6005a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 60069ea7df53SJan Kara /* Wait so that we don't change page under IO */ 60071d1d1a76SDarrick J. Wong wait_for_stable_page(page); 60089ea7df53SJan Kara ret = VM_FAULT_LOCKED; 60099ea7df53SJan Kara goto out; 60102e9ee850SAneesh Kumar K.V } 6011a827eaffSAneesh Kumar K.V } 6012a827eaffSAneesh Kumar K.V unlock_page(page); 60139ea7df53SJan Kara /* OK, we need to fill the hole... */ 60149ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 6015705965bdSJan Kara get_block = ext4_get_block_unwritten; 60169ea7df53SJan Kara else 60179ea7df53SJan Kara get_block = ext4_get_block; 60189ea7df53SJan Kara retry_alloc: 60199924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 60209924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 60219ea7df53SJan Kara if (IS_ERR(handle)) { 6022c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 60239ea7df53SJan Kara goto out; 60249ea7df53SJan Kara } 6025401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 6026401b25aaSSouptick Joarder if (!err && ext4_should_journal_data(inode)) { 6027f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 602809cbfeafSKirill A. Shutemov PAGE_SIZE, NULL, do_journal_get_write_access)) { 60299ea7df53SJan Kara unlock_page(page); 60309ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 6031fcbb5515SYongqiang Yang ext4_journal_stop(handle); 60329ea7df53SJan Kara goto out; 60339ea7df53SJan Kara } 60349ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 60359ea7df53SJan Kara } 60369ea7df53SJan Kara ext4_journal_stop(handle); 6037401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 60389ea7df53SJan Kara goto retry_alloc; 60399ea7df53SJan Kara out_ret: 6040401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 60419ea7df53SJan Kara out: 6042ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 60438e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 60442e9ee850SAneesh Kumar K.V return ret; 60452e9ee850SAneesh Kumar K.V } 6046ea3d7209SJan Kara 6047401b25aaSSouptick Joarder vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 6048ea3d7209SJan Kara { 604911bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file); 6050401b25aaSSouptick Joarder vm_fault_t ret; 6051ea3d7209SJan Kara 6052ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 6053401b25aaSSouptick Joarder ret = filemap_fault(vmf); 6054ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 6055ea3d7209SJan Kara 6056401b25aaSSouptick Joarder return ret; 6057ea3d7209SJan Kara } 6058