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 51a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 52a1d6cc56SAneesh Kumar K.V 53814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 54814525f4SDarrick J. Wong struct ext4_inode_info *ei) 55814525f4SDarrick J. Wong { 56814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 57814525f4SDarrick J. Wong __u32 csum; 58b47820edSDaeho Jeong __u16 dummy_csum = 0; 59b47820edSDaeho Jeong int offset = offsetof(struct ext4_inode, i_checksum_lo); 60b47820edSDaeho Jeong unsigned int csum_size = sizeof(dummy_csum); 61814525f4SDarrick J. Wong 62b47820edSDaeho Jeong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); 63b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); 64b47820edSDaeho Jeong offset += csum_size; 65b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 66b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE - offset); 67b47820edSDaeho Jeong 68b47820edSDaeho Jeong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 69b47820edSDaeho Jeong offset = offsetof(struct ext4_inode, i_checksum_hi); 70b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + 71b47820edSDaeho Jeong EXT4_GOOD_OLD_INODE_SIZE, 72b47820edSDaeho Jeong offset - EXT4_GOOD_OLD_INODE_SIZE); 73b47820edSDaeho Jeong if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 74b47820edSDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, 75b47820edSDaeho Jeong csum_size); 76b47820edSDaeho Jeong offset += csum_size; 77814525f4SDarrick J. Wong } 7805ac5aa1SDaeho Jeong csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, 7905ac5aa1SDaeho Jeong EXT4_INODE_SIZE(inode->i_sb) - offset); 80b47820edSDaeho Jeong } 81814525f4SDarrick J. Wong 82814525f4SDarrick J. Wong return csum; 83814525f4SDarrick J. Wong } 84814525f4SDarrick J. Wong 85814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 86814525f4SDarrick J. Wong struct ext4_inode_info *ei) 87814525f4SDarrick J. Wong { 88814525f4SDarrick J. Wong __u32 provided, calculated; 89814525f4SDarrick J. Wong 90814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 91814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 929aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 93814525f4SDarrick J. Wong return 1; 94814525f4SDarrick J. Wong 95814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 96814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 97814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 98814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 99814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 100814525f4SDarrick J. Wong else 101814525f4SDarrick J. Wong calculated &= 0xFFFF; 102814525f4SDarrick J. Wong 103814525f4SDarrick J. Wong return provided == calculated; 104814525f4SDarrick J. Wong } 105814525f4SDarrick J. Wong 106814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 107814525f4SDarrick J. Wong struct ext4_inode_info *ei) 108814525f4SDarrick J. Wong { 109814525f4SDarrick J. Wong __u32 csum; 110814525f4SDarrick J. Wong 111814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 112814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 1139aa5d32bSDmitry Monakhov !ext4_has_metadata_csum(inode->i_sb)) 114814525f4SDarrick J. Wong return; 115814525f4SDarrick J. Wong 116814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 117814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 118814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 119814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 120814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 121814525f4SDarrick J. Wong } 122814525f4SDarrick J. Wong 123678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 124678aaf48SJan Kara loff_t new_size) 125678aaf48SJan Kara { 1267ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1278aefcd55STheodore Ts'o /* 1288aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1298aefcd55STheodore Ts'o * writing, so there's no need to call 1308aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1318aefcd55STheodore Ts'o * outstanding writes we need to flush. 1328aefcd55STheodore Ts'o */ 1338aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1348aefcd55STheodore Ts'o return 0; 1358aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1368aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 137678aaf48SJan Kara new_size); 138678aaf48SJan Kara } 139678aaf48SJan Kara 140d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 141d47992f8SLukas Czerner unsigned int length); 142cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 143cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 144dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 145dec214d0STahsin Erdogan int pextents); 14664769240SAlex Tomas 147ac27a0ecSDave Kleikamp /* 148ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 149407cd7fbSTahsin Erdogan * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 150ac27a0ecSDave Kleikamp */ 151f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode) 152ac27a0ecSDave Kleikamp { 153fc82228aSAndi Kleen if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 154fc82228aSAndi Kleen int ea_blocks = EXT4_I(inode)->i_file_acl ? 155fc82228aSAndi Kleen EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 156fc82228aSAndi Kleen 157fc82228aSAndi Kleen if (ext4_has_inline_data(inode)) 158fc82228aSAndi Kleen return 0; 159fc82228aSAndi Kleen 160fc82228aSAndi Kleen return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 161fc82228aSAndi Kleen } 162407cd7fbSTahsin Erdogan return S_ISLNK(inode->i_mode) && inode->i_size && 163407cd7fbSTahsin Erdogan (inode->i_size < EXT4_N_BLOCKS * 4); 164ac27a0ecSDave Kleikamp } 165ac27a0ecSDave Kleikamp 166ac27a0ecSDave Kleikamp /* 167ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 168ac27a0ecSDave Kleikamp */ 1690930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 170ac27a0ecSDave Kleikamp { 171ac27a0ecSDave Kleikamp handle_t *handle; 172bc965ab3STheodore Ts'o int err; 17365db869cSJan Kara /* 17465db869cSJan Kara * Credits for final inode cleanup and freeing: 17565db869cSJan Kara * sb + inode (ext4_orphan_del()), block bitmap, group descriptor 17665db869cSJan Kara * (xattr block freeing), bitmap, group descriptor (inode freeing) 17765db869cSJan Kara */ 17865db869cSJan Kara int extra_credits = 6; 1790421a189STahsin Erdogan struct ext4_xattr_inode_array *ea_inode_array = NULL; 180ac27a0ecSDave Kleikamp 1817ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1822581fdc8SJiaying Zhang 1830930fcc1SAl Viro if (inode->i_nlink) { 1842d859db3SJan Kara /* 1852d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1862d859db3SJan Kara * journal. So although mm thinks everything is clean and 1872d859db3SJan Kara * ready for reaping the inode might still have some pages to 1882d859db3SJan Kara * write in the running transaction or waiting to be 1892d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1902d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1912d859db3SJan Kara * cause data loss. Also even if we did not discard these 1922d859db3SJan Kara * buffers, we would have no way to find them after the inode 1932d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1942d859db3SJan Kara * read them before the transaction is checkpointed. So be 1952d859db3SJan Kara * careful and force everything to disk here... We use 1962d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 1972d859db3SJan Kara * containing inode's data. 1982d859db3SJan Kara * 1992d859db3SJan Kara * Note that directories do not have this problem because they 2002d859db3SJan Kara * don't use page cache. 2012d859db3SJan Kara */ 2026a7fd522SVegard Nossum if (inode->i_ino != EXT4_JOURNAL_INO && 2036a7fd522SVegard Nossum ext4_should_journal_data(inode) && 2043abb1a0fSJan Kara (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2053abb1a0fSJan Kara inode->i_data.nrpages) { 2062d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2072d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2082d859db3SJan Kara 209d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2102d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2112d859db3SJan Kara } 21291b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 2135dc23bddSJan Kara 2140930fcc1SAl Viro goto no_delete; 2150930fcc1SAl Viro } 2160930fcc1SAl Viro 217e2bfb088STheodore Ts'o if (is_bad_inode(inode)) 218e2bfb088STheodore Ts'o goto no_delete; 219871a2931SChristoph Hellwig dquot_initialize(inode); 220907f4554SChristoph Hellwig 221678aaf48SJan Kara if (ext4_should_order_data(inode)) 222678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 22391b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 224ac27a0ecSDave Kleikamp 2258e8ad8a5SJan Kara /* 2268e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2278e8ad8a5SJan Kara * protection against it 2288e8ad8a5SJan Kara */ 2298e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 230e50e5129SAndreas Dilger 23130a7eb97STahsin Erdogan if (!IS_NOQUOTA(inode)) 23230a7eb97STahsin Erdogan extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb); 23330a7eb97STahsin Erdogan 23465db869cSJan Kara /* 23565db869cSJan Kara * Block bitmap, group descriptor, and inode are accounted in both 23665db869cSJan Kara * ext4_blocks_for_truncate() and extra_credits. So subtract 3. 23765db869cSJan Kara */ 23830a7eb97STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 23965db869cSJan Kara ext4_blocks_for_truncate(inode) + extra_credits - 3); 240ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 241bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 242ac27a0ecSDave Kleikamp /* 243ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 244ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 245ac27a0ecSDave Kleikamp * cleaned up. 246ac27a0ecSDave Kleikamp */ 247617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2488e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 249ac27a0ecSDave Kleikamp goto no_delete; 250ac27a0ecSDave Kleikamp } 25130a7eb97STahsin Erdogan 252ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2530390131bSFrank Mayhar ext4_handle_sync(handle); 254407cd7fbSTahsin Erdogan 255407cd7fbSTahsin Erdogan /* 256407cd7fbSTahsin Erdogan * Set inode->i_size to 0 before calling ext4_truncate(). We need 257407cd7fbSTahsin Erdogan * special handling of symlinks here because i_size is used to 258407cd7fbSTahsin Erdogan * determine whether ext4_inode_info->i_data contains symlink data or 259407cd7fbSTahsin Erdogan * block mappings. Setting i_size to 0 will remove its fast symlink 260407cd7fbSTahsin Erdogan * status. Erase i_data so that it becomes a valid empty block map. 261407cd7fbSTahsin Erdogan */ 262407cd7fbSTahsin Erdogan if (ext4_inode_is_fast_symlink(inode)) 263407cd7fbSTahsin Erdogan memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 264ac27a0ecSDave Kleikamp inode->i_size = 0; 265bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 266bc965ab3STheodore Ts'o if (err) { 26712062dddSEric Sandeen ext4_warning(inode->i_sb, 268bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 269bc965ab3STheodore Ts'o goto stop_handle; 270bc965ab3STheodore Ts'o } 2712c98eb5eSTheodore Ts'o if (inode->i_blocks) { 2722c98eb5eSTheodore Ts'o err = ext4_truncate(inode); 2732c98eb5eSTheodore Ts'o if (err) { 2742c98eb5eSTheodore Ts'o ext4_error(inode->i_sb, 2752c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2762c98eb5eSTheodore Ts'o inode->i_ino, err); 2772c98eb5eSTheodore Ts'o goto stop_handle; 2782c98eb5eSTheodore Ts'o } 2792c98eb5eSTheodore Ts'o } 280bc965ab3STheodore Ts'o 28130a7eb97STahsin Erdogan /* Remove xattr references. */ 28230a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 28330a7eb97STahsin Erdogan extra_credits); 28430a7eb97STahsin Erdogan if (err) { 28530a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 286bc965ab3STheodore Ts'o stop_handle: 287bc965ab3STheodore Ts'o ext4_journal_stop(handle); 28845388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2898e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 29030a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 291bc965ab3STheodore Ts'o goto no_delete; 292bc965ab3STheodore Ts'o } 293bc965ab3STheodore Ts'o 294ac27a0ecSDave Kleikamp /* 295617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 296ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 297617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 298ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 299617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 300ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 301ac27a0ecSDave Kleikamp */ 302617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3035ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 304ac27a0ecSDave Kleikamp 305ac27a0ecSDave Kleikamp /* 306ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 307ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 308ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 309ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 310ac27a0ecSDave Kleikamp * fails. 311ac27a0ecSDave Kleikamp */ 312617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 313ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3140930fcc1SAl Viro ext4_clear_inode(inode); 315ac27a0ecSDave Kleikamp else 316617ba13bSMingming Cao ext4_free_inode(handle, inode); 317617ba13bSMingming Cao ext4_journal_stop(handle); 3188e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3190421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 320ac27a0ecSDave Kleikamp return; 321ac27a0ecSDave Kleikamp no_delete: 3220930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 323ac27a0ecSDave Kleikamp } 324ac27a0ecSDave Kleikamp 325a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 326a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 32760e58e0fSMingming Cao { 328a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 32960e58e0fSMingming Cao } 330a9e7f447SDmitry Monakhov #endif 3319d0be502STheodore Ts'o 33212219aeaSAneesh Kumar K.V /* 3330637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3340637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3350637c6f4STheodore Ts'o */ 3365f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3375f634d06SAneesh Kumar K.V int used, int quota_claim) 33812219aeaSAneesh Kumar K.V { 33912219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3400637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34112219aeaSAneesh Kumar K.V 3420637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 343d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3440637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3458de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3461084f252STheodore Ts'o "with only %d reserved data blocks", 3470637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3480637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3490637c6f4STheodore Ts'o WARN_ON(1); 3500637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3516bc6e63fSAneesh Kumar K.V } 35212219aeaSAneesh Kumar K.V 3530637c6f4STheodore Ts'o /* Update per-inode reservations */ 3540637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 35571d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3560637c6f4STheodore Ts'o 35712219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 35860e58e0fSMingming Cao 35972b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 36072b8ab9dSEric Sandeen if (quota_claim) 3617b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 36272b8ab9dSEric Sandeen else { 3635f634d06SAneesh Kumar K.V /* 3645f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3655f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 36672b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3675f634d06SAneesh Kumar K.V */ 3687b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3695f634d06SAneesh Kumar K.V } 370d6014301SAneesh Kumar K.V 371d6014301SAneesh Kumar K.V /* 372d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 373d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 374d6014301SAneesh Kumar K.V * inode's preallocations. 375d6014301SAneesh Kumar K.V */ 3760637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 37782dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 378d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 37912219aeaSAneesh Kumar K.V } 38012219aeaSAneesh Kumar K.V 381e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 382c398eda0STheodore Ts'o unsigned int line, 38324676da4STheodore Ts'o struct ext4_map_blocks *map) 3846fd058f7STheodore Ts'o { 385345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 386345c0dbfSTheodore Ts'o (inode->i_ino == 387345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 388345c0dbfSTheodore Ts'o return 0; 38924676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 39024676da4STheodore Ts'o map->m_len)) { 391c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 392bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 39324676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 394bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 3956a797d27SDarrick J. Wong return -EFSCORRUPTED; 3966fd058f7STheodore Ts'o } 3976fd058f7STheodore Ts'o return 0; 3986fd058f7STheodore Ts'o } 3996fd058f7STheodore Ts'o 40053085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 40153085facSJan Kara ext4_lblk_t len) 40253085facSJan Kara { 40353085facSJan Kara int ret; 40453085facSJan Kara 405592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) 406a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 40753085facSJan Kara 40853085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 40953085facSJan Kara if (ret > 0) 41053085facSJan Kara ret = 0; 41153085facSJan Kara 41253085facSJan Kara return ret; 41353085facSJan Kara } 41453085facSJan Kara 415e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 416c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 417e29136f8STheodore Ts'o 418921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 419921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 420921f266bSDmitry Monakhov struct inode *inode, 421921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 422921f266bSDmitry Monakhov struct ext4_map_blocks *map, 423921f266bSDmitry Monakhov int flags) 424921f266bSDmitry Monakhov { 425921f266bSDmitry Monakhov int retval; 426921f266bSDmitry Monakhov 427921f266bSDmitry Monakhov map->m_flags = 0; 428921f266bSDmitry Monakhov /* 429921f266bSDmitry Monakhov * There is a race window that the result is not the same. 430921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 431921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 432921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 433921f266bSDmitry Monakhov * could be converted. 434921f266bSDmitry Monakhov */ 435c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 436921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 437921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 438921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 439921f266bSDmitry Monakhov } else { 440921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 441921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 442921f266bSDmitry Monakhov } 443921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 444921f266bSDmitry Monakhov 445921f266bSDmitry Monakhov /* 446921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 447921f266bSDmitry Monakhov * tree. So the m_len might not equal. 448921f266bSDmitry Monakhov */ 449921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 450921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 451921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 452bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 453921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 454921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 455921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 456921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 457921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 458921f266bSDmitry Monakhov retval, flags); 459921f266bSDmitry Monakhov } 460921f266bSDmitry Monakhov } 461921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 462921f266bSDmitry Monakhov 46355138e0bSTheodore Ts'o /* 464e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4652b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 466f5ab0d1fSMingming Cao * 467f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 468f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 469f5ab0d1fSMingming Cao * mapped. 470f5ab0d1fSMingming Cao * 471e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 472e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 473f5ab0d1fSMingming Cao * based files 474f5ab0d1fSMingming Cao * 475facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 476facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 477facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 478f5ab0d1fSMingming Cao * 479f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 480facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 481facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 482f5ab0d1fSMingming Cao * 483f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 484f5ab0d1fSMingming Cao */ 485e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 486e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4870e855ac8SAneesh Kumar K.V { 488d100eef2SZheng Liu struct extent_status es; 4890e855ac8SAneesh Kumar K.V int retval; 490b8a86845SLukas Czerner int ret = 0; 491921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 492921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 493921f266bSDmitry Monakhov 494921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 495921f266bSDmitry Monakhov #endif 496f5ab0d1fSMingming Cao 497e35fd660STheodore Ts'o map->m_flags = 0; 498e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 499e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 500e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 501d100eef2SZheng Liu 502e861b5e9STheodore Ts'o /* 503e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 504e861b5e9STheodore Ts'o */ 505e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 506e861b5e9STheodore Ts'o map->m_len = INT_MAX; 507e861b5e9STheodore Ts'o 5084adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5094adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5106a797d27SDarrick J. Wong return -EFSCORRUPTED; 5114adb6ab3SKazuya Mio 512d100eef2SZheng Liu /* Lookup extent status tree firstly */ 513bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 514d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 515d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 516d100eef2SZheng Liu map->m_lblk - es.es_lblk; 517d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 518d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 519d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 520d100eef2SZheng Liu if (retval > map->m_len) 521d100eef2SZheng Liu retval = map->m_len; 522d100eef2SZheng Liu map->m_len = retval; 523d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 524facab4d9SJan Kara map->m_pblk = 0; 525facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 526facab4d9SJan Kara if (retval > map->m_len) 527facab4d9SJan Kara retval = map->m_len; 528facab4d9SJan Kara map->m_len = retval; 529d100eef2SZheng Liu retval = 0; 530d100eef2SZheng Liu } else { 5311e83bc81SArnd Bergmann BUG(); 532d100eef2SZheng Liu } 533921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 534921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 535921f266bSDmitry Monakhov &orig_map, flags); 536921f266bSDmitry Monakhov #endif 537d100eef2SZheng Liu goto found; 538d100eef2SZheng Liu } 539d100eef2SZheng Liu 5404df3d265SAneesh Kumar K.V /* 541b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 542b920c755STheodore Ts'o * file system block. 5434df3d265SAneesh Kumar K.V */ 544c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 54512e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 546a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 547a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5484df3d265SAneesh Kumar K.V } else { 549a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 550a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5510e855ac8SAneesh Kumar K.V } 552f7fec032SZheng Liu if (retval > 0) { 5533be78c73STheodore Ts'o unsigned int status; 554f7fec032SZheng Liu 55544fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 55644fb851dSZheng Liu ext4_warning(inode->i_sb, 55744fb851dSZheng Liu "ES len assertion failed for inode " 55844fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 55944fb851dSZheng Liu inode->i_ino, retval, map->m_len); 56044fb851dSZheng Liu WARN_ON(1); 561921f266bSDmitry Monakhov } 562921f266bSDmitry Monakhov 563f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 564f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 565f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 566d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 567ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 568f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 569f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 570f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 571f7fec032SZheng Liu map->m_len, map->m_pblk, status); 572f7fec032SZheng Liu if (ret < 0) 573f7fec032SZheng Liu retval = ret; 574f7fec032SZheng Liu } 5754df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 576f5ab0d1fSMingming Cao 577d100eef2SZheng Liu found: 578e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 579b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5806fd058f7STheodore Ts'o if (ret != 0) 5816fd058f7STheodore Ts'o return ret; 5826fd058f7STheodore Ts'o } 5836fd058f7STheodore Ts'o 584f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 585c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5864df3d265SAneesh Kumar K.V return retval; 5874df3d265SAneesh Kumar K.V 5884df3d265SAneesh Kumar K.V /* 589f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 590f5ab0d1fSMingming Cao * 591f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 592df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 593f5ab0d1fSMingming Cao * with buffer head unmapped. 594f5ab0d1fSMingming Cao */ 595e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 596b8a86845SLukas Czerner /* 597b8a86845SLukas Czerner * If we need to convert extent to unwritten 598b8a86845SLukas Czerner * we continue and do the actual work in 599b8a86845SLukas Czerner * ext4_ext_map_blocks() 600b8a86845SLukas Czerner */ 601b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 602f5ab0d1fSMingming Cao return retval; 603f5ab0d1fSMingming Cao 604f5ab0d1fSMingming Cao /* 605a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 606a25a4e1aSZheng Liu * it will be set again. 6072a8964d6SAneesh Kumar K.V */ 608a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6092a8964d6SAneesh Kumar K.V 6102a8964d6SAneesh Kumar K.V /* 611556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 612f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 613d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 614f5ab0d1fSMingming Cao * with create == 1 flag. 6154df3d265SAneesh Kumar K.V */ 616c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 617d2a17637SMingming Cao 618d2a17637SMingming Cao /* 6194df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6204df3d265SAneesh Kumar K.V * could have changed the inode type in between 6214df3d265SAneesh Kumar K.V */ 62212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 623e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6240e855ac8SAneesh Kumar K.V } else { 625e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 626267e4db9SAneesh Kumar K.V 627e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 628267e4db9SAneesh Kumar K.V /* 629267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 630267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 631267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 632267e4db9SAneesh Kumar K.V */ 63319f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 634267e4db9SAneesh Kumar K.V } 6352ac3b6e0STheodore Ts'o 636d2a17637SMingming Cao /* 6372ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6385f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6395f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6405f634d06SAneesh Kumar K.V * reserve space here. 641d2a17637SMingming Cao */ 6425f634d06SAneesh Kumar K.V if ((retval > 0) && 6431296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6445f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6455f634d06SAneesh Kumar K.V } 646d2a17637SMingming Cao 647f7fec032SZheng Liu if (retval > 0) { 6483be78c73STheodore Ts'o unsigned int status; 649f7fec032SZheng Liu 65044fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 65144fb851dSZheng Liu ext4_warning(inode->i_sb, 65244fb851dSZheng Liu "ES len assertion failed for inode " 65344fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 65444fb851dSZheng Liu inode->i_ino, retval, map->m_len); 65544fb851dSZheng Liu WARN_ON(1); 656921f266bSDmitry Monakhov } 657921f266bSDmitry Monakhov 658adb23551SZheng Liu /* 659c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 660c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6619b623df6SJan Kara * use them before they are really zeroed. We also have to 6629b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6639b623df6SJan Kara * overwrite zeros with stale data from block device. 664c86d8db3SJan Kara */ 665c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 666c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 667c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 668c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 669c86d8db3SJan Kara map->m_pblk, map->m_len); 670c86d8db3SJan Kara if (ret) { 671c86d8db3SJan Kara retval = ret; 672c86d8db3SJan Kara goto out_sem; 673c86d8db3SJan Kara } 674c86d8db3SJan Kara } 675c86d8db3SJan Kara 676c86d8db3SJan Kara /* 677adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 678adb23551SZheng Liu * extent status tree. 679adb23551SZheng Liu */ 680adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 681bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 682adb23551SZheng Liu if (ext4_es_is_written(&es)) 683c86d8db3SJan Kara goto out_sem; 684adb23551SZheng Liu } 685f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 686f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 687f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 688d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 689ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 690f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 691f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 692f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 693f7fec032SZheng Liu map->m_pblk, status); 694c86d8db3SJan Kara if (ret < 0) { 69551865fdaSZheng Liu retval = ret; 696c86d8db3SJan Kara goto out_sem; 697c86d8db3SJan Kara } 69851865fdaSZheng Liu } 6995356f261SAditya Kali 700c86d8db3SJan Kara out_sem: 7010e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 702e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 703b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7046fd058f7STheodore Ts'o if (ret != 0) 7056fd058f7STheodore Ts'o return ret; 70606bd3c36SJan Kara 70706bd3c36SJan Kara /* 70806bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 70906bd3c36SJan Kara * visible after transaction commit must be on transaction's 71006bd3c36SJan Kara * ordered data list. 71106bd3c36SJan Kara */ 71206bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 71306bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 71406bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 71502749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 71606bd3c36SJan Kara ext4_should_order_data(inode)) { 71773131fbbSRoss Zwisler loff_t start_byte = 71873131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 71973131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 72073131fbbSRoss Zwisler 721ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 72273131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 72373131fbbSRoss Zwisler start_byte, length); 724ee0876bcSJan Kara else 72573131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 72673131fbbSRoss Zwisler start_byte, length); 72706bd3c36SJan Kara if (ret) 72806bd3c36SJan Kara return ret; 72906bd3c36SJan Kara } 7306fd058f7STheodore Ts'o } 7310e855ac8SAneesh Kumar K.V return retval; 7320e855ac8SAneesh Kumar K.V } 7330e855ac8SAneesh Kumar K.V 734ed8ad838SJan Kara /* 735ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 736ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 737ed8ad838SJan Kara */ 738ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 739ed8ad838SJan Kara { 740ed8ad838SJan Kara unsigned long old_state; 741ed8ad838SJan Kara unsigned long new_state; 742ed8ad838SJan Kara 743ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 744ed8ad838SJan Kara 745ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 746ed8ad838SJan Kara if (!bh->b_page) { 747ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 748ed8ad838SJan Kara return; 749ed8ad838SJan Kara } 750ed8ad838SJan Kara /* 751ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 752ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 753ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 754ed8ad838SJan Kara */ 755ed8ad838SJan Kara do { 756ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 757ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 758ed8ad838SJan Kara } while (unlikely( 759ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 760ed8ad838SJan Kara } 761ed8ad838SJan Kara 7622ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7632ed88685STheodore Ts'o struct buffer_head *bh, int flags) 764ac27a0ecSDave Kleikamp { 7652ed88685STheodore Ts'o struct ext4_map_blocks map; 766efe70c29SJan Kara int ret = 0; 767ac27a0ecSDave Kleikamp 76846c7f254STao Ma if (ext4_has_inline_data(inode)) 76946c7f254STao Ma return -ERANGE; 77046c7f254STao Ma 7712ed88685STheodore Ts'o map.m_lblk = iblock; 7722ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7732ed88685STheodore Ts'o 774efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 775efe70c29SJan Kara flags); 776ac27a0ecSDave Kleikamp if (ret > 0) { 7772ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 778ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7792ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 780ac27a0ecSDave Kleikamp ret = 0; 781547edce3SRoss Zwisler } else if (ret == 0) { 782547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 783547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 784ac27a0ecSDave Kleikamp } 785ac27a0ecSDave Kleikamp return ret; 786ac27a0ecSDave Kleikamp } 787ac27a0ecSDave Kleikamp 7882ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7892ed88685STheodore Ts'o struct buffer_head *bh, int create) 7902ed88685STheodore Ts'o { 7912ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7922ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7932ed88685STheodore Ts'o } 7942ed88685STheodore Ts'o 795ac27a0ecSDave Kleikamp /* 796705965bdSJan Kara * Get block function used when preparing for buffered write if we require 797705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 798705965bdSJan Kara * will be converted to written after the IO is complete. 799705965bdSJan Kara */ 800705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 801705965bdSJan Kara struct buffer_head *bh_result, int create) 802705965bdSJan Kara { 803705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 804705965bdSJan Kara inode->i_ino, create); 805705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 806705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 807705965bdSJan Kara } 808705965bdSJan Kara 809efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 810efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 811efe70c29SJan Kara 812e84dfbe2SJan Kara /* 813ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 814ac27a0ecSDave Kleikamp */ 815617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 816c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 817ac27a0ecSDave Kleikamp { 8182ed88685STheodore Ts'o struct ext4_map_blocks map; 8192ed88685STheodore Ts'o struct buffer_head *bh; 820c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 82110560082STheodore Ts'o int err; 822ac27a0ecSDave Kleikamp 823ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 824ac27a0ecSDave Kleikamp 8252ed88685STheodore Ts'o map.m_lblk = block; 8262ed88685STheodore Ts'o map.m_len = 1; 827c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8282ed88685STheodore Ts'o 82910560082STheodore Ts'o if (err == 0) 83010560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8312ed88685STheodore Ts'o if (err < 0) 83210560082STheodore Ts'o return ERR_PTR(err); 8332ed88685STheodore Ts'o 8342ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 83510560082STheodore Ts'o if (unlikely(!bh)) 83610560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8372ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 838ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 839ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 840ac27a0ecSDave Kleikamp 841ac27a0ecSDave Kleikamp /* 842ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 843ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 844ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 845617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 846ac27a0ecSDave Kleikamp * problem. 847ac27a0ecSDave Kleikamp */ 848ac27a0ecSDave Kleikamp lock_buffer(bh); 849ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 85010560082STheodore Ts'o err = ext4_journal_get_create_access(handle, bh); 85110560082STheodore Ts'o if (unlikely(err)) { 85210560082STheodore Ts'o unlock_buffer(bh); 85310560082STheodore Ts'o goto errout; 85410560082STheodore Ts'o } 85510560082STheodore Ts'o if (!buffer_uptodate(bh)) { 856ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 857ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 858ac27a0ecSDave Kleikamp } 859ac27a0ecSDave Kleikamp unlock_buffer(bh); 8600390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8610390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 86210560082STheodore Ts'o if (unlikely(err)) 86310560082STheodore Ts'o goto errout; 86410560082STheodore Ts'o } else 865ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 866ac27a0ecSDave Kleikamp return bh; 86710560082STheodore Ts'o errout: 86810560082STheodore Ts'o brelse(bh); 86910560082STheodore Ts'o return ERR_PTR(err); 870ac27a0ecSDave Kleikamp } 871ac27a0ecSDave Kleikamp 872617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 873c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 874ac27a0ecSDave Kleikamp { 875ac27a0ecSDave Kleikamp struct buffer_head *bh; 876ac27a0ecSDave Kleikamp 877c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 8781c215028STheodore Ts'o if (IS_ERR(bh)) 879ac27a0ecSDave Kleikamp return bh; 8807963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 881ac27a0ecSDave Kleikamp return bh; 882dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh); 883ac27a0ecSDave Kleikamp wait_on_buffer(bh); 884ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 885ac27a0ecSDave Kleikamp return bh; 886ac27a0ecSDave Kleikamp put_bh(bh); 8871c215028STheodore Ts'o return ERR_PTR(-EIO); 888ac27a0ecSDave Kleikamp } 889ac27a0ecSDave Kleikamp 8909699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 8919699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 8929699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 8939699d4f9STahsin Erdogan { 8949699d4f9STahsin Erdogan int i, err; 8959699d4f9STahsin Erdogan 8969699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 8979699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 8989699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 8999699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9009699d4f9STahsin Erdogan bh_count = i; 9019699d4f9STahsin Erdogan goto out_brelse; 9029699d4f9STahsin Erdogan } 9039699d4f9STahsin Erdogan } 9049699d4f9STahsin Erdogan 9059699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9069699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9077963e5acSZhangXiaoxu if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9089699d4f9STahsin Erdogan ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, 9099699d4f9STahsin Erdogan &bhs[i]); 9109699d4f9STahsin Erdogan 9119699d4f9STahsin Erdogan if (!wait) 9129699d4f9STahsin Erdogan return 0; 9139699d4f9STahsin Erdogan 9149699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9159699d4f9STahsin Erdogan if (bhs[i]) 9169699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9179699d4f9STahsin Erdogan 9189699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9199699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9209699d4f9STahsin Erdogan err = -EIO; 9219699d4f9STahsin Erdogan goto out_brelse; 9229699d4f9STahsin Erdogan } 9239699d4f9STahsin Erdogan } 9249699d4f9STahsin Erdogan return 0; 9259699d4f9STahsin Erdogan 9269699d4f9STahsin Erdogan out_brelse: 9279699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9289699d4f9STahsin Erdogan brelse(bhs[i]); 9299699d4f9STahsin Erdogan bhs[i] = NULL; 9309699d4f9STahsin Erdogan } 9319699d4f9STahsin Erdogan return err; 9329699d4f9STahsin Erdogan } 9339699d4f9STahsin Erdogan 934f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 935ac27a0ecSDave Kleikamp struct buffer_head *head, 936ac27a0ecSDave Kleikamp unsigned from, 937ac27a0ecSDave Kleikamp unsigned to, 938ac27a0ecSDave Kleikamp int *partial, 939ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 940ac27a0ecSDave Kleikamp struct buffer_head *bh)) 941ac27a0ecSDave Kleikamp { 942ac27a0ecSDave Kleikamp struct buffer_head *bh; 943ac27a0ecSDave Kleikamp unsigned block_start, block_end; 944ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 945ac27a0ecSDave Kleikamp int err, ret = 0; 946ac27a0ecSDave Kleikamp struct buffer_head *next; 947ac27a0ecSDave Kleikamp 948ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 949ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 950de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 951ac27a0ecSDave Kleikamp next = bh->b_this_page; 952ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 953ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 954ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 955ac27a0ecSDave Kleikamp *partial = 1; 956ac27a0ecSDave Kleikamp continue; 957ac27a0ecSDave Kleikamp } 958ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 959ac27a0ecSDave Kleikamp if (!ret) 960ac27a0ecSDave Kleikamp ret = err; 961ac27a0ecSDave Kleikamp } 962ac27a0ecSDave Kleikamp return ret; 963ac27a0ecSDave Kleikamp } 964ac27a0ecSDave Kleikamp 965ac27a0ecSDave Kleikamp /* 966ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 967ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 968617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 969dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 970ac27a0ecSDave Kleikamp * prepare_write() is the right place. 971ac27a0ecSDave Kleikamp * 97236ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 97336ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 97436ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 97536ade451SJan Kara * because the caller may be PF_MEMALLOC. 976ac27a0ecSDave Kleikamp * 977617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 978ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 979ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 980ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 981ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 982ac27a0ecSDave Kleikamp * violation. 983ac27a0ecSDave Kleikamp * 984dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 985ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 986ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 987ac27a0ecSDave Kleikamp * write. 988ac27a0ecSDave Kleikamp */ 989f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 990ac27a0ecSDave Kleikamp struct buffer_head *bh) 991ac27a0ecSDave Kleikamp { 99256d35a4cSJan Kara int dirty = buffer_dirty(bh); 99356d35a4cSJan Kara int ret; 99456d35a4cSJan Kara 995ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 996ac27a0ecSDave Kleikamp return 0; 99756d35a4cSJan Kara /* 998ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 99956d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 100056d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1001ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 100256d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 100356d35a4cSJan Kara * ever write the buffer. 100456d35a4cSJan Kara */ 100556d35a4cSJan Kara if (dirty) 100656d35a4cSJan Kara clear_buffer_dirty(bh); 10075d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 100856d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 100956d35a4cSJan Kara if (!ret && dirty) 101056d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 101156d35a4cSJan Kara return ret; 1012ac27a0ecSDave Kleikamp } 1013ac27a0ecSDave Kleikamp 1014643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10152058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10162058f83aSMichael Halcrow get_block_t *get_block) 10172058f83aSMichael Halcrow { 101809cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10192058f83aSMichael Halcrow unsigned to = from + len; 10202058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10212058f83aSMichael Halcrow unsigned block_start, block_end; 10222058f83aSMichael Halcrow sector_t block; 10232058f83aSMichael Halcrow int err = 0; 10242058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10252058f83aSMichael Halcrow unsigned bbits; 10260b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10270b578f35SChandan Rajendra int nr_wait = 0; 10280b578f35SChandan Rajendra int i; 10292058f83aSMichael Halcrow 10302058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 103109cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 103209cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10332058f83aSMichael Halcrow BUG_ON(from > to); 10342058f83aSMichael Halcrow 10352058f83aSMichael Halcrow if (!page_has_buffers(page)) 10362058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10372058f83aSMichael Halcrow head = page_buffers(page); 10382058f83aSMichael Halcrow bbits = ilog2(blocksize); 103909cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10402058f83aSMichael Halcrow 10412058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10422058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10432058f83aSMichael Halcrow block_end = block_start + blocksize; 10442058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10452058f83aSMichael Halcrow if (PageUptodate(page)) { 10462058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10472058f83aSMichael Halcrow set_buffer_uptodate(bh); 10482058f83aSMichael Halcrow } 10492058f83aSMichael Halcrow continue; 10502058f83aSMichael Halcrow } 10512058f83aSMichael Halcrow if (buffer_new(bh)) 10522058f83aSMichael Halcrow clear_buffer_new(bh); 10532058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10542058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10552058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10562058f83aSMichael Halcrow if (err) 10572058f83aSMichael Halcrow break; 10582058f83aSMichael Halcrow if (buffer_new(bh)) { 10592058f83aSMichael Halcrow if (PageUptodate(page)) { 10602058f83aSMichael Halcrow clear_buffer_new(bh); 10612058f83aSMichael Halcrow set_buffer_uptodate(bh); 10622058f83aSMichael Halcrow mark_buffer_dirty(bh); 10632058f83aSMichael Halcrow continue; 10642058f83aSMichael Halcrow } 10652058f83aSMichael Halcrow if (block_end > to || block_start < from) 10662058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10672058f83aSMichael Halcrow block_start, from); 10682058f83aSMichael Halcrow continue; 10692058f83aSMichael Halcrow } 10702058f83aSMichael Halcrow } 10712058f83aSMichael Halcrow if (PageUptodate(page)) { 10722058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10732058f83aSMichael Halcrow set_buffer_uptodate(bh); 10742058f83aSMichael Halcrow continue; 10752058f83aSMichael Halcrow } 10762058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10772058f83aSMichael Halcrow !buffer_unwritten(bh) && 10782058f83aSMichael Halcrow (block_start < from || block_end > to)) { 1079dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 10800b578f35SChandan Rajendra wait[nr_wait++] = bh; 10812058f83aSMichael Halcrow } 10822058f83aSMichael Halcrow } 10832058f83aSMichael Halcrow /* 10842058f83aSMichael Halcrow * If we issued read requests, let them complete. 10852058f83aSMichael Halcrow */ 10860b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10870b578f35SChandan Rajendra wait_on_buffer(wait[i]); 10880b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 10892058f83aSMichael Halcrow err = -EIO; 10902058f83aSMichael Halcrow } 10917e0785fcSChandan Rajendra if (unlikely(err)) { 10922058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 10930b578f35SChandan Rajendra } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) { 10940b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10950b578f35SChandan Rajendra int err2; 10960b578f35SChandan Rajendra 10970b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 10980b578f35SChandan Rajendra bh_offset(wait[i])); 10990b578f35SChandan Rajendra if (err2) { 11000b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11010b578f35SChandan Rajendra err = err2; 11020b578f35SChandan Rajendra } 11030b578f35SChandan Rajendra } 11047e0785fcSChandan Rajendra } 11057e0785fcSChandan Rajendra 11062058f83aSMichael Halcrow return err; 11072058f83aSMichael Halcrow } 11082058f83aSMichael Halcrow #endif 11092058f83aSMichael Halcrow 1110bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1111bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1112bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1113ac27a0ecSDave Kleikamp { 1114bfc1af65SNick Piggin struct inode *inode = mapping->host; 11151938a150SAneesh Kumar K.V int ret, needed_blocks; 1116ac27a0ecSDave Kleikamp handle_t *handle; 1117ac27a0ecSDave Kleikamp int retries = 0; 1118bfc1af65SNick Piggin struct page *page; 1119bfc1af65SNick Piggin pgoff_t index; 1120bfc1af65SNick Piggin unsigned from, to; 1121bfc1af65SNick Piggin 11220db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11230db1ff22STheodore Ts'o return -EIO; 11240db1ff22STheodore Ts'o 11259bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11261938a150SAneesh Kumar K.V /* 11271938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11281938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11291938a150SAneesh Kumar K.V */ 11301938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 113109cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 113209cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1133bfc1af65SNick Piggin to = from + len; 1134ac27a0ecSDave Kleikamp 1135f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1136f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1137f19d5870STao Ma flags, pagep); 1138f19d5870STao Ma if (ret < 0) 113947564bfbSTheodore Ts'o return ret; 114047564bfbSTheodore Ts'o if (ret == 1) 114147564bfbSTheodore Ts'o return 0; 1142f19d5870STao Ma } 1143f19d5870STao Ma 114447564bfbSTheodore Ts'o /* 114547564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 114647564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 114747564bfbSTheodore Ts'o * is being written back. So grab it first before we start 114847564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 114947564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 115047564bfbSTheodore Ts'o */ 115147564bfbSTheodore Ts'o retry_grab: 115254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 115347564bfbSTheodore Ts'o if (!page) 115447564bfbSTheodore Ts'o return -ENOMEM; 115547564bfbSTheodore Ts'o unlock_page(page); 115647564bfbSTheodore Ts'o 115747564bfbSTheodore Ts'o retry_journal: 11589924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1159ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 116009cbfeafSKirill A. Shutemov put_page(page); 116147564bfbSTheodore Ts'o return PTR_ERR(handle); 1162cf108bcaSJan Kara } 1163f19d5870STao Ma 116447564bfbSTheodore Ts'o lock_page(page); 116547564bfbSTheodore Ts'o if (page->mapping != mapping) { 116647564bfbSTheodore Ts'o /* The page got truncated from under us */ 116747564bfbSTheodore Ts'o unlock_page(page); 116809cbfeafSKirill A. Shutemov put_page(page); 1169cf108bcaSJan Kara ext4_journal_stop(handle); 117047564bfbSTheodore Ts'o goto retry_grab; 1171cf108bcaSJan Kara } 11727afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11737afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1174cf108bcaSJan Kara 1175643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11762058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11772058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1178705965bdSJan Kara ext4_get_block_unwritten); 11792058f83aSMichael Halcrow else 11802058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 11812058f83aSMichael Halcrow ext4_get_block); 11822058f83aSMichael Halcrow #else 1183744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1184705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1185705965bdSJan Kara ext4_get_block_unwritten); 1186744692dcSJiaying Zhang else 11876e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 11882058f83aSMichael Halcrow #endif 1189bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1190f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1191f19d5870STao Ma from, to, NULL, 1192f19d5870STao Ma do_journal_get_write_access); 1193b46be050SAndrey Savochkin } 1194bfc1af65SNick Piggin 1195bfc1af65SNick Piggin if (ret) { 1196c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1197c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1198c93d8f88SEric Biggers 1199bfc1af65SNick Piggin unlock_page(page); 1200ae4d5372SAneesh Kumar K.V /* 12016e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1202ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1203ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12041938a150SAneesh Kumar K.V * 12051938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12061938a150SAneesh Kumar K.V * truncate finishes 1207ae4d5372SAneesh Kumar K.V */ 1208c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12091938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12101938a150SAneesh Kumar K.V 12111938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1212c93d8f88SEric Biggers if (extended) { 1213b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12141938a150SAneesh Kumar K.V /* 1215ffacfa7aSJan Kara * If truncate failed early the inode might 12161938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12171938a150SAneesh Kumar K.V * make sure the inode is removed from the 12181938a150SAneesh Kumar K.V * orphan list in that case. 12191938a150SAneesh Kumar K.V */ 12201938a150SAneesh Kumar K.V if (inode->i_nlink) 12211938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12221938a150SAneesh Kumar K.V } 1223bfc1af65SNick Piggin 122447564bfbSTheodore Ts'o if (ret == -ENOSPC && 122547564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 122647564bfbSTheodore Ts'o goto retry_journal; 122709cbfeafSKirill A. Shutemov put_page(page); 122847564bfbSTheodore Ts'o return ret; 122947564bfbSTheodore Ts'o } 123047564bfbSTheodore Ts'o *pagep = page; 1231ac27a0ecSDave Kleikamp return ret; 1232ac27a0ecSDave Kleikamp } 1233ac27a0ecSDave Kleikamp 1234bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1235bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1236ac27a0ecSDave Kleikamp { 123713fca323STheodore Ts'o int ret; 1238ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1239ac27a0ecSDave Kleikamp return 0; 1240ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 124113fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 124213fca323STheodore Ts'o clear_buffer_meta(bh); 124313fca323STheodore Ts'o clear_buffer_prio(bh); 124413fca323STheodore Ts'o return ret; 1245ac27a0ecSDave Kleikamp } 1246ac27a0ecSDave Kleikamp 1247eed4333fSZheng Liu /* 1248eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1249eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1250eed4333fSZheng Liu * 1251eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1252eed4333fSZheng Liu * buffers are managed internally. 1253eed4333fSZheng Liu */ 1254eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1255f8514083SAneesh Kumar K.V struct address_space *mapping, 1256f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1257f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1258f8514083SAneesh Kumar K.V { 1259f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1260eed4333fSZheng Liu struct inode *inode = mapping->host; 12610572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1262eed4333fSZheng Liu int ret = 0, ret2; 1263eed4333fSZheng Liu int i_size_changed = 0; 1264362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1265c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1266eed4333fSZheng Liu 1267eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1268362eca70STheodore Ts'o if (inline_data) { 126942c832deSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 1270f19d5870STao Ma copied, page); 1271eb5efbcbSTheodore Ts'o if (ret < 0) { 1272eb5efbcbSTheodore Ts'o unlock_page(page); 1273eb5efbcbSTheodore Ts'o put_page(page); 127442c832deSTheodore Ts'o goto errout; 1275eb5efbcbSTheodore Ts'o } 127642c832deSTheodore Ts'o copied = ret; 127742c832deSTheodore Ts'o } else 1278f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1279f19d5870STao Ma len, copied, page, fsdata); 1280f8514083SAneesh Kumar K.V /* 12814631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1282f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1283c93d8f88SEric Biggers * 1284c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1285c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1286f8514083SAneesh Kumar K.V */ 1287c93d8f88SEric Biggers if (!verity) 12884631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1289f8514083SAneesh Kumar K.V unlock_page(page); 129009cbfeafSKirill A. Shutemov put_page(page); 1291f8514083SAneesh Kumar K.V 1292c93d8f88SEric Biggers if (old_size < pos && !verity) 12930572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1294f8514083SAneesh Kumar K.V /* 1295f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1296f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1297f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1298f8514083SAneesh Kumar K.V * filesystems. 1299f8514083SAneesh Kumar K.V */ 1300362eca70STheodore Ts'o if (i_size_changed || inline_data) 1301f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1302f8514083SAneesh Kumar K.V 1303c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1304f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1305f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1306f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1307f8514083SAneesh Kumar K.V */ 1308f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 130974d553aaSTheodore Ts'o errout: 1310617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1311ac27a0ecSDave Kleikamp if (!ret) 1312ac27a0ecSDave Kleikamp ret = ret2; 1313bfc1af65SNick Piggin 1314c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1315b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1316f8514083SAneesh Kumar K.V /* 1317ffacfa7aSJan Kara * If truncate failed early the inode might still be 1318f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1319f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1320f8514083SAneesh Kumar K.V */ 1321f8514083SAneesh Kumar K.V if (inode->i_nlink) 1322f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1323f8514083SAneesh Kumar K.V } 1324f8514083SAneesh Kumar K.V 1325bfc1af65SNick Piggin return ret ? ret : copied; 1326ac27a0ecSDave Kleikamp } 1327ac27a0ecSDave Kleikamp 1328b90197b6STheodore Ts'o /* 1329b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1330b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1331b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1332b90197b6STheodore Ts'o */ 13333b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 13343b136499SJan Kara struct page *page, 13353b136499SJan Kara unsigned from, unsigned to) 1336b90197b6STheodore Ts'o { 1337b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1338b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1339b90197b6STheodore Ts'o 1340b90197b6STheodore Ts'o bh = head = page_buffers(page); 1341b90197b6STheodore Ts'o do { 1342b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1343b90197b6STheodore Ts'o if (buffer_new(bh)) { 1344b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1345b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1346b90197b6STheodore Ts'o unsigned start, size; 1347b90197b6STheodore Ts'o 1348b90197b6STheodore Ts'o start = max(from, block_start); 1349b90197b6STheodore Ts'o size = min(to, block_end) - start; 1350b90197b6STheodore Ts'o 1351b90197b6STheodore Ts'o zero_user(page, start, size); 13523b136499SJan Kara write_end_fn(handle, bh); 1353b90197b6STheodore Ts'o } 1354b90197b6STheodore Ts'o clear_buffer_new(bh); 1355b90197b6STheodore Ts'o } 1356b90197b6STheodore Ts'o } 1357b90197b6STheodore Ts'o block_start = block_end; 1358b90197b6STheodore Ts'o bh = bh->b_this_page; 1359b90197b6STheodore Ts'o } while (bh != head); 1360b90197b6STheodore Ts'o } 1361b90197b6STheodore Ts'o 1362bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1363bfc1af65SNick Piggin struct address_space *mapping, 1364bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1365bfc1af65SNick Piggin struct page *page, void *fsdata) 1366ac27a0ecSDave Kleikamp { 1367617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1368bfc1af65SNick Piggin struct inode *inode = mapping->host; 13690572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1370ac27a0ecSDave Kleikamp int ret = 0, ret2; 1371ac27a0ecSDave Kleikamp int partial = 0; 1372bfc1af65SNick Piggin unsigned from, to; 13734631dbf6SDmitry Monakhov int size_changed = 0; 1374362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1375c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1376ac27a0ecSDave Kleikamp 13779bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 137809cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1379bfc1af65SNick Piggin to = from + len; 1380bfc1af65SNick Piggin 1381441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1382441c8508SCurt Wohlgemuth 1383362eca70STheodore Ts'o if (inline_data) { 1384eb5efbcbSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 13853fdcfb66STao Ma copied, page); 1386eb5efbcbSTheodore Ts'o if (ret < 0) { 1387eb5efbcbSTheodore Ts'o unlock_page(page); 1388eb5efbcbSTheodore Ts'o put_page(page); 1389eb5efbcbSTheodore Ts'o goto errout; 1390eb5efbcbSTheodore Ts'o } 1391eb5efbcbSTheodore Ts'o copied = ret; 1392eb5efbcbSTheodore Ts'o } else if (unlikely(copied < len) && !PageUptodate(page)) { 1393bfc1af65SNick Piggin copied = 0; 13943b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, from, to); 13953b136499SJan Kara } else { 13963b136499SJan Kara if (unlikely(copied < len)) 13973b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, 13983b136499SJan Kara from + copied, to); 1399f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 14003b136499SJan Kara from + copied, &partial, 14013b136499SJan Kara write_end_fn); 1402ac27a0ecSDave Kleikamp if (!partial) 1403ac27a0ecSDave Kleikamp SetPageUptodate(page); 14043fdcfb66STao Ma } 1405c93d8f88SEric Biggers if (!verity) 14064631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 140719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14082d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14094631dbf6SDmitry Monakhov unlock_page(page); 141009cbfeafSKirill A. Shutemov put_page(page); 14114631dbf6SDmitry Monakhov 1412c93d8f88SEric Biggers if (old_size < pos && !verity) 14130572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14140572639fSXiaoguang Wang 1415362eca70STheodore Ts'o if (size_changed || inline_data) { 1416617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1417ac27a0ecSDave Kleikamp if (!ret) 1418ac27a0ecSDave Kleikamp ret = ret2; 1419ac27a0ecSDave Kleikamp } 1420bfc1af65SNick Piggin 1421c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1422f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1423f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1424f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1425f8514083SAneesh Kumar K.V */ 1426f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1427f8514083SAneesh Kumar K.V 1428eb5efbcbSTheodore Ts'o errout: 1429617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1430ac27a0ecSDave Kleikamp if (!ret) 1431ac27a0ecSDave Kleikamp ret = ret2; 1432c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1433b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1434f8514083SAneesh Kumar K.V /* 1435ffacfa7aSJan Kara * If truncate failed early the inode might still be 1436f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1437f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1438f8514083SAneesh Kumar K.V */ 1439f8514083SAneesh Kumar K.V if (inode->i_nlink) 1440f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1441f8514083SAneesh Kumar K.V } 1442bfc1af65SNick Piggin 1443bfc1af65SNick Piggin return ret ? ret : copied; 1444ac27a0ecSDave Kleikamp } 1445d2a17637SMingming Cao 14469d0be502STheodore Ts'o /* 1447c27e43a1SEric Whitney * Reserve space for a single cluster 14489d0be502STheodore Ts'o */ 1449c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1450d2a17637SMingming Cao { 1451d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14520637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14535dd4056dSChristoph Hellwig int ret; 1454d2a17637SMingming Cao 145560e58e0fSMingming Cao /* 145672b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 145772b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 145872b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 145960e58e0fSMingming Cao */ 14607b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14615dd4056dSChristoph Hellwig if (ret) 14625dd4056dSChristoph Hellwig return ret; 146303179fe9STheodore Ts'o 146403179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 146571d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 146603179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 146703179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1468d2a17637SMingming Cao return -ENOSPC; 1469d2a17637SMingming Cao } 14709d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1471c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14720637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147339bc680aSDmitry Monakhov 1474d2a17637SMingming Cao return 0; /* success */ 1475d2a17637SMingming Cao } 1476d2a17637SMingming Cao 1477f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1478d2a17637SMingming Cao { 1479d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14800637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1481d2a17637SMingming Cao 1482cd213226SMingming Cao if (!to_free) 1483cd213226SMingming Cao return; /* Nothing to release, exit */ 1484cd213226SMingming Cao 1485d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1486cd213226SMingming Cao 14875a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14880637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1489cd213226SMingming Cao /* 14900637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14910637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14920637c6f4STheodore Ts'o * function is called from invalidate page, it's 14930637c6f4STheodore Ts'o * harmless to return without any action. 1494cd213226SMingming Cao */ 14958de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 14960637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 14971084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 14980637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 14990637c6f4STheodore Ts'o WARN_ON(1); 15000637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15010637c6f4STheodore Ts'o } 15020637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15030637c6f4STheodore Ts'o 150472b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 150557042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1506d2a17637SMingming Cao 1507d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 150860e58e0fSMingming Cao 15097b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1510d2a17637SMingming Cao } 1511d2a17637SMingming Cao 1512ac27a0ecSDave Kleikamp /* 151364769240SAlex Tomas * Delayed allocation stuff 151464769240SAlex Tomas */ 151564769240SAlex Tomas 15164e7ea81dSJan Kara struct mpage_da_data { 15174e7ea81dSJan Kara struct inode *inode; 15184e7ea81dSJan Kara struct writeback_control *wbc; 15196b523df4SJan Kara 15204e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15214e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15224e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 152364769240SAlex Tomas /* 15244e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15254e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15264e7ea81dSJan Kara * is delalloc or unwritten. 152764769240SAlex Tomas */ 15284e7ea81dSJan Kara struct ext4_map_blocks map; 15294e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1530dddbd6acSJan Kara unsigned int do_map:1; 15314e7ea81dSJan Kara }; 153264769240SAlex Tomas 15334e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15344e7ea81dSJan Kara bool invalidate) 1535c4a0c46eSAneesh Kumar K.V { 1536c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1537c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1538c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1539c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1540c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15414e7ea81dSJan Kara 15424e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15434e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15444e7ea81dSJan Kara return; 1545c4a0c46eSAneesh Kumar K.V 1546c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1547c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15484e7ea81dSJan Kara if (invalidate) { 15494e7ea81dSJan Kara ext4_lblk_t start, last; 155009cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 155109cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 155251865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15534e7ea81dSJan Kara } 155451865fdaSZheng Liu 155586679820SMel Gorman pagevec_init(&pvec); 1556c4a0c46eSAneesh Kumar K.V while (index <= end) { 1557397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1558c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1559c4a0c46eSAneesh Kumar K.V break; 1560c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1561c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15622b85a617SJan Kara 1563c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1564c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15654e7ea81dSJan Kara if (invalidate) { 15664e800c03Swangguang if (page_mapped(page)) 15674e800c03Swangguang clear_page_dirty_for_io(page); 156809cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1569c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15704e7ea81dSJan Kara } 1571c4a0c46eSAneesh Kumar K.V unlock_page(page); 1572c4a0c46eSAneesh Kumar K.V } 15739b1d0998SJan Kara pagevec_release(&pvec); 1574c4a0c46eSAneesh Kumar K.V } 1575c4a0c46eSAneesh Kumar K.V } 1576c4a0c46eSAneesh Kumar K.V 1577df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1578df22291fSAneesh Kumar K.V { 1579df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 158092b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1581f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 158292b97816STheodore Ts'o 158392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15845dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1585f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 158692b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 158792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1588f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 158957042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 159092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1591f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 15927b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 159392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 159492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1595f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1596df22291fSAneesh Kumar K.V return; 1597df22291fSAneesh Kumar K.V } 1598df22291fSAneesh Kumar K.V 1599c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 160029fa89d0SAneesh Kumar K.V { 1601c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 160229fa89d0SAneesh Kumar K.V } 160329fa89d0SAneesh Kumar K.V 160464769240SAlex Tomas /* 16050b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16060b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16070b02f4c0SEric Whitney * count or making a pending reservation 16080b02f4c0SEric Whitney * where needed 16090b02f4c0SEric Whitney * 16100b02f4c0SEric Whitney * @inode - file containing the newly added block 16110b02f4c0SEric Whitney * @lblk - logical block to be added 16120b02f4c0SEric Whitney * 16130b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16140b02f4c0SEric Whitney */ 16150b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16160b02f4c0SEric Whitney { 16170b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16180b02f4c0SEric Whitney int ret; 16190b02f4c0SEric Whitney bool allocated = false; 16200b02f4c0SEric Whitney 16210b02f4c0SEric Whitney /* 16220b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16230b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16240b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16250b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16260b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16270b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16280b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16290b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16300b02f4c0SEric Whitney * extents status tree doesn't get a match. 16310b02f4c0SEric Whitney */ 16320b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16330b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16340b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16350b02f4c0SEric Whitney goto errout; 16360b02f4c0SEric Whitney } else { /* bigalloc */ 16370b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16380b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16390b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16400b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16410b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16420b02f4c0SEric Whitney if (ret < 0) 16430b02f4c0SEric Whitney goto errout; 16440b02f4c0SEric Whitney if (ret == 0) { 16450b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16460b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16470b02f4c0SEric Whitney goto errout; 16480b02f4c0SEric Whitney } else { 16490b02f4c0SEric Whitney allocated = true; 16500b02f4c0SEric Whitney } 16510b02f4c0SEric Whitney } else { 16520b02f4c0SEric Whitney allocated = true; 16530b02f4c0SEric Whitney } 16540b02f4c0SEric Whitney } 16550b02f4c0SEric Whitney } 16560b02f4c0SEric Whitney 16570b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16580b02f4c0SEric Whitney 16590b02f4c0SEric Whitney errout: 16600b02f4c0SEric Whitney return ret; 16610b02f4c0SEric Whitney } 16620b02f4c0SEric Whitney 16630b02f4c0SEric Whitney /* 16645356f261SAditya Kali * This function is grabs code from the very beginning of 16655356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16665356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16675356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16685356f261SAditya Kali */ 16695356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16705356f261SAditya Kali struct ext4_map_blocks *map, 16715356f261SAditya Kali struct buffer_head *bh) 16725356f261SAditya Kali { 1673d100eef2SZheng Liu struct extent_status es; 16745356f261SAditya Kali int retval; 16755356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1676921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1677921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1678921f266bSDmitry Monakhov 1679921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1680921f266bSDmitry Monakhov #endif 16815356f261SAditya Kali 16825356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16835356f261SAditya Kali invalid_block = ~0; 16845356f261SAditya Kali 16855356f261SAditya Kali map->m_flags = 0; 16865356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 16875356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 16885356f261SAditya Kali (unsigned long) map->m_lblk); 1689d100eef2SZheng Liu 1690d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1691bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1692d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1693d100eef2SZheng Liu retval = 0; 1694c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1695d100eef2SZheng Liu goto add_delayed; 1696d100eef2SZheng Liu } 1697d100eef2SZheng Liu 1698d100eef2SZheng Liu /* 1699d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1700d100eef2SZheng Liu * So we need to check it. 1701d100eef2SZheng Liu */ 1702d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1703d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1704d100eef2SZheng Liu set_buffer_new(bh); 1705d100eef2SZheng Liu set_buffer_delay(bh); 1706d100eef2SZheng Liu return 0; 1707d100eef2SZheng Liu } 1708d100eef2SZheng Liu 1709d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1710d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1711d100eef2SZheng Liu if (retval > map->m_len) 1712d100eef2SZheng Liu retval = map->m_len; 1713d100eef2SZheng Liu map->m_len = retval; 1714d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1715d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1716d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1717d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1718d100eef2SZheng Liu else 17191e83bc81SArnd Bergmann BUG(); 1720d100eef2SZheng Liu 1721921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1722921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1723921f266bSDmitry Monakhov #endif 1724d100eef2SZheng Liu return retval; 1725d100eef2SZheng Liu } 1726d100eef2SZheng Liu 17275356f261SAditya Kali /* 17285356f261SAditya Kali * Try to see if we can get the block without requesting a new 17295356f261SAditya Kali * file system block. 17305356f261SAditya Kali */ 1731c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1732cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17339c3569b5STao Ma retval = 0; 1734cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17352f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17365356f261SAditya Kali else 17372f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17385356f261SAditya Kali 1739d100eef2SZheng Liu add_delayed: 17405356f261SAditya Kali if (retval == 0) { 1741f7fec032SZheng Liu int ret; 1742ad431025SEric Whitney 17435356f261SAditya Kali /* 17445356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17455356f261SAditya Kali * is it OK? 17465356f261SAditya Kali */ 17475356f261SAditya Kali 17480b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17490b02f4c0SEric Whitney if (ret != 0) { 1750f7fec032SZheng Liu retval = ret; 175151865fdaSZheng Liu goto out_unlock; 1752f7fec032SZheng Liu } 175351865fdaSZheng Liu 17545356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17555356f261SAditya Kali set_buffer_new(bh); 17565356f261SAditya Kali set_buffer_delay(bh); 1757f7fec032SZheng Liu } else if (retval > 0) { 1758f7fec032SZheng Liu int ret; 17593be78c73STheodore Ts'o unsigned int status; 1760f7fec032SZheng Liu 176144fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 176244fb851dSZheng Liu ext4_warning(inode->i_sb, 176344fb851dSZheng Liu "ES len assertion failed for inode " 176444fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 176544fb851dSZheng Liu inode->i_ino, retval, map->m_len); 176644fb851dSZheng Liu WARN_ON(1); 1767921f266bSDmitry Monakhov } 1768921f266bSDmitry Monakhov 1769f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1770f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1771f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1772f7fec032SZheng Liu map->m_pblk, status); 1773f7fec032SZheng Liu if (ret != 0) 1774f7fec032SZheng Liu retval = ret; 17755356f261SAditya Kali } 17765356f261SAditya Kali 17775356f261SAditya Kali out_unlock: 17785356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17795356f261SAditya Kali 17805356f261SAditya Kali return retval; 17815356f261SAditya Kali } 17825356f261SAditya Kali 17835356f261SAditya Kali /* 1784d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1785b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1786b920c755STheodore Ts'o * reserve space for a single block. 178729fa89d0SAneesh Kumar K.V * 178829fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 178929fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 179029fa89d0SAneesh Kumar K.V * 179129fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 179229fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 179329fa89d0SAneesh Kumar K.V * initialized properly. 179464769240SAlex Tomas */ 17959c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 17962ed88685STheodore Ts'o struct buffer_head *bh, int create) 179764769240SAlex Tomas { 17982ed88685STheodore Ts'o struct ext4_map_blocks map; 179964769240SAlex Tomas int ret = 0; 180064769240SAlex Tomas 180164769240SAlex Tomas BUG_ON(create == 0); 18022ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18032ed88685STheodore Ts'o 18042ed88685STheodore Ts'o map.m_lblk = iblock; 18052ed88685STheodore Ts'o map.m_len = 1; 180664769240SAlex Tomas 180764769240SAlex Tomas /* 180864769240SAlex Tomas * first, we need to know whether the block is allocated already 180964769240SAlex Tomas * preallocated blocks are unmapped but should treated 181064769240SAlex Tomas * the same as allocated blocks. 181164769240SAlex Tomas */ 18125356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18135356f261SAditya Kali if (ret <= 0) 18142ed88685STheodore Ts'o return ret; 181564769240SAlex Tomas 18162ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1817ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18182ed88685STheodore Ts'o 18192ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18202ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18212ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18222ed88685STheodore Ts'o * get_block multiple times when we write to the same 18232ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18242ed88685STheodore Ts'o * for partial write. 18252ed88685STheodore Ts'o */ 18262ed88685STheodore Ts'o set_buffer_new(bh); 1827c8205636STheodore Ts'o set_buffer_mapped(bh); 18282ed88685STheodore Ts'o } 18292ed88685STheodore Ts'o return 0; 183064769240SAlex Tomas } 183161628a3fSMingming Cao 183262e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 183362e086beSAneesh Kumar K.V { 183462e086beSAneesh Kumar K.V get_bh(bh); 183562e086beSAneesh Kumar K.V return 0; 183662e086beSAneesh Kumar K.V } 183762e086beSAneesh Kumar K.V 183862e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 183962e086beSAneesh Kumar K.V { 184062e086beSAneesh Kumar K.V put_bh(bh); 184162e086beSAneesh Kumar K.V return 0; 184262e086beSAneesh Kumar K.V } 184362e086beSAneesh Kumar K.V 184462e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 184562e086beSAneesh Kumar K.V unsigned int len) 184662e086beSAneesh Kumar K.V { 184762e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 184862e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18493fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 185062e086beSAneesh Kumar K.V handle_t *handle = NULL; 18513fdcfb66STao Ma int ret = 0, err = 0; 18523fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18533fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 185462e086beSAneesh Kumar K.V 1855cb20d518STheodore Ts'o ClearPageChecked(page); 18563fdcfb66STao Ma 18573fdcfb66STao Ma if (inline_data) { 18583fdcfb66STao Ma BUG_ON(page->index != 0); 18593fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18603fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18613fdcfb66STao Ma if (inode_bh == NULL) 18623fdcfb66STao Ma goto out; 18633fdcfb66STao Ma } else { 186462e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18653fdcfb66STao Ma if (!page_bufs) { 18663fdcfb66STao Ma BUG(); 18673fdcfb66STao Ma goto out; 18683fdcfb66STao Ma } 18693fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18703fdcfb66STao Ma NULL, bget_one); 18713fdcfb66STao Ma } 1872bdf96838STheodore Ts'o /* 1873bdf96838STheodore Ts'o * We need to release the page lock before we start the 1874bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1875bdf96838STheodore Ts'o * out from under us. 1876bdf96838STheodore Ts'o */ 1877bdf96838STheodore Ts'o get_page(page); 187862e086beSAneesh Kumar K.V unlock_page(page); 187962e086beSAneesh Kumar K.V 18809924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18819924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 188262e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 188362e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1884bdf96838STheodore Ts'o put_page(page); 1885bdf96838STheodore Ts'o goto out_no_pagelock; 1886bdf96838STheodore Ts'o } 1887bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1888bdf96838STheodore Ts'o 1889bdf96838STheodore Ts'o lock_page(page); 1890bdf96838STheodore Ts'o put_page(page); 1891bdf96838STheodore Ts'o if (page->mapping != mapping) { 1892bdf96838STheodore Ts'o /* The page got truncated from under us */ 1893bdf96838STheodore Ts'o ext4_journal_stop(handle); 1894bdf96838STheodore Ts'o ret = 0; 189562e086beSAneesh Kumar K.V goto out; 189662e086beSAneesh Kumar K.V } 189762e086beSAneesh Kumar K.V 18983fdcfb66STao Ma if (inline_data) { 1899362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19003fdcfb66STao Ma } else { 1901f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190262e086beSAneesh Kumar K.V do_journal_get_write_access); 190362e086beSAneesh Kumar K.V 1904f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190562e086beSAneesh Kumar K.V write_end_fn); 19063fdcfb66STao Ma } 190762e086beSAneesh Kumar K.V if (ret == 0) 190862e086beSAneesh Kumar K.V ret = err; 19092d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 191062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 191162e086beSAneesh Kumar K.V if (!ret) 191262e086beSAneesh Kumar K.V ret = err; 191362e086beSAneesh Kumar K.V 19143fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19158c9367fdSTheodore Ts'o ext4_walk_page_buffers(NULL, page_bufs, 0, len, 19163fdcfb66STao Ma NULL, bput_one); 191719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 191862e086beSAneesh Kumar K.V out: 1919bdf96838STheodore Ts'o unlock_page(page); 1920bdf96838STheodore Ts'o out_no_pagelock: 19213fdcfb66STao Ma brelse(inode_bh); 192262e086beSAneesh Kumar K.V return ret; 192362e086beSAneesh Kumar K.V } 192462e086beSAneesh Kumar K.V 192561628a3fSMingming Cao /* 192643ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 192743ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 192843ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 192943ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193043ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 193143ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 193243ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 193343ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 193443ce1d23SAneesh Kumar K.V * 1935b920c755STheodore Ts'o * This function can get called via... 193620970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1937b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1938f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1939b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194043ce1d23SAneesh Kumar K.V * 194143ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 194243ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 194343ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 194443ce1d23SAneesh Kumar K.V * truncate(f, 1024); 194543ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 194643ce1d23SAneesh Kumar K.V * a[0] = 'a'; 194743ce1d23SAneesh Kumar K.V * truncate(f, 4096); 194843ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 194990802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195043ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 195143ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 195243ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 195343ce1d23SAneesh Kumar K.V * buffer_heads mapped. 195443ce1d23SAneesh Kumar K.V * 195543ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 195643ce1d23SAneesh Kumar K.V * unwritten in the page. 195743ce1d23SAneesh Kumar K.V * 195843ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 195943ce1d23SAneesh Kumar K.V * 196043ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 196143ce1d23SAneesh Kumar K.V * ext4_writepage() 196243ce1d23SAneesh Kumar K.V * 196343ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 196443ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 196561628a3fSMingming Cao */ 196643ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 196764769240SAlex Tomas struct writeback_control *wbc) 196864769240SAlex Tomas { 1969f8bec370SJan Kara int ret = 0; 197061628a3fSMingming Cao loff_t size; 1971498e5f24STheodore Ts'o unsigned int len; 1972744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 197361628a3fSMingming Cao struct inode *inode = page->mapping->host; 197436ade451SJan Kara struct ext4_io_submit io_submit; 19751c8349a1SNamjae Jeon bool keep_towrite = false; 197664769240SAlex Tomas 19770db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 19780db1ff22STheodore Ts'o ext4_invalidatepage(page, 0, PAGE_SIZE); 19790db1ff22STheodore Ts'o unlock_page(page); 19800db1ff22STheodore Ts'o return -EIO; 19810db1ff22STheodore Ts'o } 19820db1ff22STheodore Ts'o 1983a9c667f8SLukas Czerner trace_ext4_writepage(page); 198461628a3fSMingming Cao size = i_size_read(inode); 1985c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 1986c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 198709cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 198861628a3fSMingming Cao else 198909cbfeafSKirill A. Shutemov len = PAGE_SIZE; 199061628a3fSMingming Cao 1991f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 199264769240SAlex Tomas /* 1993fe386132SJan Kara * We cannot do block allocation or other extent handling in this 1994fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 1995fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 1996fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 1997fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 1998cccd147aSTheodore Ts'o * 1999cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2000cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2001cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2002cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2003cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2004cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2005cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2006cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2007cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 200864769240SAlex Tomas */ 2009f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2010c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 201161628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2012cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 201309cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2014fe386132SJan Kara /* 2015fe386132SJan Kara * For memory cleaning there's no point in writing only 2016fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2017fe386132SJan Kara * from direct reclaim. 2018fe386132SJan Kara */ 2019fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2020fe386132SJan Kara == PF_MEMALLOC); 202161628a3fSMingming Cao unlock_page(page); 202261628a3fSMingming Cao return 0; 202361628a3fSMingming Cao } 20241c8349a1SNamjae Jeon keep_towrite = true; 2025f0e6c985SAneesh Kumar K.V } 202664769240SAlex Tomas 2027cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 202843ce1d23SAneesh Kumar K.V /* 202943ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 203043ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 203143ce1d23SAneesh Kumar K.V */ 20323f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 203343ce1d23SAneesh Kumar K.V 203497a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 203597a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 203697a851edSJan Kara if (!io_submit.io_end) { 203797a851edSJan Kara redirty_page_for_writepage(wbc, page); 203897a851edSJan Kara unlock_page(page); 203997a851edSJan Kara return -ENOMEM; 204097a851edSJan Kara } 20411c8349a1SNamjae Jeon ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite); 204236ade451SJan Kara ext4_io_submit(&io_submit); 204397a851edSJan Kara /* Drop io_end reference we got from init */ 204497a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 204564769240SAlex Tomas return ret; 204664769240SAlex Tomas } 204764769240SAlex Tomas 20485f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20495f1132b2SJan Kara { 20505f1132b2SJan Kara int len; 2051a056bdaaSJan Kara loff_t size; 20525f1132b2SJan Kara int err; 20535f1132b2SJan Kara 20545f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2055a056bdaaSJan Kara clear_page_dirty_for_io(page); 2056a056bdaaSJan Kara /* 2057a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2058a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2059a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2060a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2061a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2062a056bdaaSJan Kara * written to again until we release page lock. So only after 2063a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2064a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2065a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2066a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2067a056bdaaSJan Kara * after page tables are updated. 2068a056bdaaSJan Kara */ 2069a056bdaaSJan Kara size = i_size_read(mpd->inode); 2070c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2071c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 207209cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20735f1132b2SJan Kara else 207409cbfeafSKirill A. Shutemov len = PAGE_SIZE; 20751c8349a1SNamjae Jeon err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false); 20765f1132b2SJan Kara if (!err) 20775f1132b2SJan Kara mpd->wbc->nr_to_write--; 20785f1132b2SJan Kara mpd->first_page++; 20795f1132b2SJan Kara 20805f1132b2SJan Kara return err; 20815f1132b2SJan Kara } 20825f1132b2SJan Kara 20834e7ea81dSJan Kara #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay)) 20844e7ea81dSJan Kara 208561628a3fSMingming Cao /* 2086fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2087fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2088fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 208961628a3fSMingming Cao */ 2090fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2091525f4ed8SMingming Cao 2092525f4ed8SMingming Cao /* 20934e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 20944e7ea81dSJan Kara * 20954e7ea81dSJan Kara * @mpd - extent of blocks 20964e7ea81dSJan Kara * @lblk - logical number of the block in the file 209709930042SJan Kara * @bh - buffer head we want to add to the extent 20984e7ea81dSJan Kara * 209909930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 210009930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 210109930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 210209930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 210309930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 210409930042SJan Kara * added. 21054e7ea81dSJan Kara */ 210609930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 210709930042SJan Kara struct buffer_head *bh) 21084e7ea81dSJan Kara { 21094e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21104e7ea81dSJan Kara 211109930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 211209930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 211309930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 211409930042SJan Kara /* So far no extent to map => we write the buffer right away */ 211509930042SJan Kara if (map->m_len == 0) 211609930042SJan Kara return true; 211709930042SJan Kara return false; 211809930042SJan Kara } 21194e7ea81dSJan Kara 21204e7ea81dSJan Kara /* First block in the extent? */ 21214e7ea81dSJan Kara if (map->m_len == 0) { 2122dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2123dddbd6acSJan Kara if (!mpd->do_map) 2124dddbd6acSJan Kara return false; 21254e7ea81dSJan Kara map->m_lblk = lblk; 21264e7ea81dSJan Kara map->m_len = 1; 212709930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 212809930042SJan Kara return true; 21294e7ea81dSJan Kara } 21304e7ea81dSJan Kara 213109930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 213209930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 213309930042SJan Kara return false; 213409930042SJan Kara 21354e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21364e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 213709930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21384e7ea81dSJan Kara map->m_len++; 213909930042SJan Kara return true; 21404e7ea81dSJan Kara } 214109930042SJan Kara return false; 21424e7ea81dSJan Kara } 21434e7ea81dSJan Kara 21445f1132b2SJan Kara /* 21455f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21465f1132b2SJan Kara * 21475f1132b2SJan Kara * @mpd - extent of blocks for mapping 21485f1132b2SJan Kara * @head - the first buffer in the page 21495f1132b2SJan Kara * @bh - buffer we should start processing from 21505f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21515f1132b2SJan Kara * 21525f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21535f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21545f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21555f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21565f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21575f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21585f1132b2SJan Kara * < 0 in case of error during IO submission. 21595f1132b2SJan Kara */ 21605f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21614e7ea81dSJan Kara struct buffer_head *head, 21624e7ea81dSJan Kara struct buffer_head *bh, 21634e7ea81dSJan Kara ext4_lblk_t lblk) 21644e7ea81dSJan Kara { 21654e7ea81dSJan Kara struct inode *inode = mpd->inode; 21665f1132b2SJan Kara int err; 216793407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21684e7ea81dSJan Kara >> inode->i_blkbits; 21694e7ea81dSJan Kara 2170c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2171c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2172c93d8f88SEric Biggers 21734e7ea81dSJan Kara do { 21744e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21754e7ea81dSJan Kara 217609930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21774e7ea81dSJan Kara /* Found extent to map? */ 21784e7ea81dSJan Kara if (mpd->map.m_len) 21795f1132b2SJan Kara return 0; 2180dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2181dddbd6acSJan Kara if (!mpd->do_map) 2182dddbd6acSJan Kara return 0; 218309930042SJan Kara /* Everything mapped so far and we hit EOF */ 21845f1132b2SJan Kara break; 21854e7ea81dSJan Kara } 21864e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 21875f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 21885f1132b2SJan Kara if (mpd->map.m_len == 0) { 21895f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 21905f1132b2SJan Kara if (err < 0) 21914e7ea81dSJan Kara return err; 21924e7ea81dSJan Kara } 21935f1132b2SJan Kara return lblk < blocks; 21945f1132b2SJan Kara } 21954e7ea81dSJan Kara 21964e7ea81dSJan Kara /* 21972943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 21982943fdbcSRitesh Harjani * may submit fully mapped page for IO 21992943fdbcSRitesh Harjani * 22002943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22012943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22022943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22032943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22042943fdbcSRitesh Harjani * mapping or not. 22052943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22062943fdbcSRitesh Harjani * state according to new extent state. 22072943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22082943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22092943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22102943fdbcSRitesh Harjani */ 22112943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22122943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22132943fdbcSRitesh Harjani bool *map_bh) 22142943fdbcSRitesh Harjani { 22152943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22162943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22172943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22182943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22192943fdbcSRitesh Harjani int err = 0; 2220c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2221c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2222c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22232943fdbcSRitesh Harjani 22242943fdbcSRitesh Harjani bh = head = page_buffers(page); 22252943fdbcSRitesh Harjani do { 22262943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22272943fdbcSRitesh Harjani continue; 22282943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22292943fdbcSRitesh Harjani /* 22302943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22312943fdbcSRitesh Harjani * Find next buffer in the page to map. 22322943fdbcSRitesh Harjani */ 22332943fdbcSRitesh Harjani mpd->map.m_len = 0; 22342943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2235c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2236c8cc8816SRitesh Harjani io_end_size = 0; 22372943fdbcSRitesh Harjani 22382943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22392943fdbcSRitesh Harjani if (err > 0) 22402943fdbcSRitesh Harjani err = 0; 2241c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2242c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 2243c8cc8816SRitesh Harjani io_end_vec->offset = mpd->map.m_lblk << blkbits; 2244c8cc8816SRitesh Harjani } 22452943fdbcSRitesh Harjani *map_bh = true; 22462943fdbcSRitesh Harjani goto out; 22472943fdbcSRitesh Harjani } 22482943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22492943fdbcSRitesh Harjani clear_buffer_delay(bh); 22502943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22512943fdbcSRitesh Harjani } 22522943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2253c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22542943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2255c8cc8816SRitesh Harjani 2256c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2257c8cc8816SRitesh Harjani io_end_size = 0; 22582943fdbcSRitesh Harjani *map_bh = false; 22592943fdbcSRitesh Harjani out: 22602943fdbcSRitesh Harjani *m_lblk = lblk; 22612943fdbcSRitesh Harjani *m_pblk = pblock; 22622943fdbcSRitesh Harjani return err; 22632943fdbcSRitesh Harjani } 22642943fdbcSRitesh Harjani 22652943fdbcSRitesh Harjani /* 22664e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22674e7ea81dSJan Kara * submit fully mapped pages for IO 22684e7ea81dSJan Kara * 22694e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22704e7ea81dSJan Kara * 22714e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22724e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22734e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2274556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 22754e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 22764e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 22774e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 22784e7ea81dSJan Kara */ 22794e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 22804e7ea81dSJan Kara { 22814e7ea81dSJan Kara struct pagevec pvec; 22824e7ea81dSJan Kara int nr_pages, i; 22834e7ea81dSJan Kara struct inode *inode = mpd->inode; 228409cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 22854e7ea81dSJan Kara pgoff_t start, end; 22864e7ea81dSJan Kara ext4_lblk_t lblk; 22872943fdbcSRitesh Harjani ext4_fsblk_t pblock; 22884e7ea81dSJan Kara int err; 22892943fdbcSRitesh Harjani bool map_bh = false; 22904e7ea81dSJan Kara 22914e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 22924e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 22934e7ea81dSJan Kara lblk = start << bpp_bits; 22944e7ea81dSJan Kara pblock = mpd->map.m_pblk; 22954e7ea81dSJan Kara 229686679820SMel Gorman pagevec_init(&pvec); 22974e7ea81dSJan Kara while (start <= end) { 22982b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2299397162ffSJan Kara &start, end); 23004e7ea81dSJan Kara if (nr_pages == 0) 23014e7ea81dSJan Kara break; 23024e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23034e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23044e7ea81dSJan Kara 23052943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23062943fdbcSRitesh Harjani &map_bh); 23074e7ea81dSJan Kara /* 23082943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23092943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23102943fdbcSRitesh Harjani * So we return to call further extent mapping. 23114e7ea81dSJan Kara */ 23122943fdbcSRitesh Harjani if (err < 0 || map_bh == true) 23132943fdbcSRitesh Harjani goto out; 23144e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23154e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23162943fdbcSRitesh Harjani if (err < 0) 23172943fdbcSRitesh Harjani goto out; 23184e7ea81dSJan Kara } 23194e7ea81dSJan Kara pagevec_release(&pvec); 23204e7ea81dSJan Kara } 23214e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23224e7ea81dSJan Kara mpd->map.m_len = 0; 23234e7ea81dSJan Kara mpd->map.m_flags = 0; 23244e7ea81dSJan Kara return 0; 23252943fdbcSRitesh Harjani out: 23262943fdbcSRitesh Harjani pagevec_release(&pvec); 23272943fdbcSRitesh Harjani return err; 23284e7ea81dSJan Kara } 23294e7ea81dSJan Kara 23304e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23314e7ea81dSJan Kara { 23324e7ea81dSJan Kara struct inode *inode = mpd->inode; 23334e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23344e7ea81dSJan Kara int get_blocks_flags; 2335090f32eeSLukas Czerner int err, dioread_nolock; 23364e7ea81dSJan Kara 23374e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23384e7ea81dSJan Kara /* 23394e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2340556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23414e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23424e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23434e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23444e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23454e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23464e7ea81dSJan Kara * possible. 23474e7ea81dSJan Kara * 2348754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2349754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2350754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2351754cfed6STheodore Ts'o * the data was copied into the page cache. 23524e7ea81dSJan Kara */ 23534e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2354ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2355ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2356090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2357090f32eeSLukas Czerner if (dioread_nolock) 23584e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23594e7ea81dSJan Kara if (map->m_flags & (1 << BH_Delay)) 23604e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23614e7ea81dSJan Kara 23624e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23634e7ea81dSJan Kara if (err < 0) 23644e7ea81dSJan Kara return err; 2365090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23666b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23676b523df4SJan Kara ext4_handle_valid(handle)) { 23686b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23696b523df4SJan Kara handle->h_rsv_handle = NULL; 23706b523df4SJan Kara } 23713613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23726b523df4SJan Kara } 23734e7ea81dSJan Kara 23744e7ea81dSJan Kara BUG_ON(map->m_len == 0); 23754e7ea81dSJan Kara return 0; 23764e7ea81dSJan Kara } 23774e7ea81dSJan Kara 23784e7ea81dSJan Kara /* 23794e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 23804e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 23814e7ea81dSJan Kara * 23824e7ea81dSJan Kara * @handle - handle for journal operations 23834e7ea81dSJan Kara * @mpd - extent to map 23847534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 23857534e854SJan Kara * is no hope of writing the data. The caller should discard 23867534e854SJan Kara * dirty pages to avoid infinite loops. 23874e7ea81dSJan Kara * 23884e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 23894e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 23904e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 23914e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 23924e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 23934e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 23944e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 23954e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 23964e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 23974e7ea81dSJan Kara */ 23984e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2399cb530541STheodore Ts'o struct mpage_da_data *mpd, 2400cb530541STheodore Ts'o bool *give_up_on_write) 24014e7ea81dSJan Kara { 24024e7ea81dSJan Kara struct inode *inode = mpd->inode; 24034e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24044e7ea81dSJan Kara int err; 24054e7ea81dSJan Kara loff_t disksize; 24066603120eSDmitry Monakhov int progress = 0; 2407c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 2408c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_alloc_io_end_vec(io_end); 24094e7ea81dSJan Kara 2410c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 241127d7c4edSJan Kara do { 24124e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24134e7ea81dSJan Kara if (err < 0) { 24144e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24154e7ea81dSJan Kara 24160db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24170db1ff22STheodore Ts'o EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED) 2418cb530541STheodore Ts'o goto invalidate_dirty_pages; 24194e7ea81dSJan Kara /* 2420cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2421cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2422cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24234e7ea81dSJan Kara */ 2424cb530541STheodore Ts'o if ((err == -ENOMEM) || 24256603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24266603120eSDmitry Monakhov if (progress) 24276603120eSDmitry Monakhov goto update_disksize; 2428cb530541STheodore Ts'o return err; 24296603120eSDmitry Monakhov } 24304e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24314e7ea81dSJan Kara "Delayed block allocation failed for " 24324e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24334e7ea81dSJan Kara " max blocks %u with error %d", 24344e7ea81dSJan Kara inode->i_ino, 24354e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2436cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24374e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24384e7ea81dSJan Kara "This should not happen!! Data will " 24394e7ea81dSJan Kara "be lost\n"); 24404e7ea81dSJan Kara if (err == -ENOSPC) 24414e7ea81dSJan Kara ext4_print_free_blocks(inode); 2442cb530541STheodore Ts'o invalidate_dirty_pages: 2443cb530541STheodore Ts'o *give_up_on_write = true; 24444e7ea81dSJan Kara return err; 24454e7ea81dSJan Kara } 24466603120eSDmitry Monakhov progress = 1; 24474e7ea81dSJan Kara /* 24484e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24494e7ea81dSJan Kara * extent to map 24504e7ea81dSJan Kara */ 24514e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24524e7ea81dSJan Kara if (err < 0) 24536603120eSDmitry Monakhov goto update_disksize; 245427d7c4edSJan Kara } while (map->m_len); 24554e7ea81dSJan Kara 24566603120eSDmitry Monakhov update_disksize: 2457622cad13STheodore Ts'o /* 2458622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2459622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2460622cad13STheodore Ts'o */ 246109cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 24624e7ea81dSJan Kara if (disksize > EXT4_I(inode)->i_disksize) { 24634e7ea81dSJan Kara int err2; 2464622cad13STheodore Ts'o loff_t i_size; 24654e7ea81dSJan Kara 2466622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2467622cad13STheodore Ts'o i_size = i_size_read(inode); 2468622cad13STheodore Ts'o if (disksize > i_size) 2469622cad13STheodore Ts'o disksize = i_size; 2470622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2471622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2472622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2473b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 24744e7ea81dSJan Kara if (err2) 24754e7ea81dSJan Kara ext4_error(inode->i_sb, 24764e7ea81dSJan Kara "Failed to mark inode %lu dirty", 24774e7ea81dSJan Kara inode->i_ino); 24784e7ea81dSJan Kara if (!err) 24794e7ea81dSJan Kara err = err2; 24804e7ea81dSJan Kara } 24814e7ea81dSJan Kara return err; 24824e7ea81dSJan Kara } 24834e7ea81dSJan Kara 24844e7ea81dSJan Kara /* 2485fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 248620970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2487fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2488fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2489fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2490525f4ed8SMingming Cao */ 2491fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2492fffb2739SJan Kara { 2493fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2494525f4ed8SMingming Cao 2495fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2496fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2497525f4ed8SMingming Cao } 249861628a3fSMingming Cao 24998e48dcfbSTheodore Ts'o /* 25004e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25014e7ea81dSJan Kara * and underlying extent to map 25024e7ea81dSJan Kara * 25034e7ea81dSJan Kara * @mpd - where to look for pages 25044e7ea81dSJan Kara * 25054e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25064e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25074e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25084e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25094e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25104e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25114e7ea81dSJan Kara * 25124e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25134e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25144e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25154e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25168e48dcfbSTheodore Ts'o */ 25174e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25188e48dcfbSTheodore Ts'o { 25194e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25208e48dcfbSTheodore Ts'o struct pagevec pvec; 25214f01b02cSTheodore Ts'o unsigned int nr_pages; 2522aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25234e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25244e7ea81dSJan Kara pgoff_t end = mpd->last_page; 252510bbd235SMatthew Wilcox xa_mark_t tag; 25264e7ea81dSJan Kara int i, err = 0; 25274e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25284e7ea81dSJan Kara ext4_lblk_t lblk; 25294e7ea81dSJan Kara struct buffer_head *head; 25308e48dcfbSTheodore Ts'o 25314e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25325b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25335b41d924SEric Sandeen else 25345b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25355b41d924SEric Sandeen 253686679820SMel Gorman pagevec_init(&pvec); 25374e7ea81dSJan Kara mpd->map.m_len = 0; 25384e7ea81dSJan Kara mpd->next_page = index; 25394f01b02cSTheodore Ts'o while (index <= end) { 2540dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 254167fd707fSJan Kara tag); 25428e48dcfbSTheodore Ts'o if (nr_pages == 0) 25434e7ea81dSJan Kara goto out; 25448e48dcfbSTheodore Ts'o 25458e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25468e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25478e48dcfbSTheodore Ts'o 25488e48dcfbSTheodore Ts'o /* 2549aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2550aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2551aeac589aSMing Lei * keep going because someone may be concurrently 2552aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2553aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2554aeac589aSMing Lei * of the old dirty pages. 2555aeac589aSMing Lei */ 2556aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2557aeac589aSMing Lei goto out; 2558aeac589aSMing Lei 25594e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25604e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25614e7ea81dSJan Kara goto out; 256278aaced3STheodore Ts'o 25638e48dcfbSTheodore Ts'o lock_page(page); 25648e48dcfbSTheodore Ts'o /* 25654e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25664e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25674e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25684e7ea81dSJan Kara * page is already under writeback and we are not doing 25694e7ea81dSJan Kara * a data integrity writeback, skip the page 25708e48dcfbSTheodore Ts'o */ 25714f01b02cSTheodore Ts'o if (!PageDirty(page) || 25724f01b02cSTheodore Ts'o (PageWriteback(page) && 25734e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 25744f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 25758e48dcfbSTheodore Ts'o unlock_page(page); 25768e48dcfbSTheodore Ts'o continue; 25778e48dcfbSTheodore Ts'o } 25788e48dcfbSTheodore Ts'o 25798e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 25808e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 25818e48dcfbSTheodore Ts'o 25824e7ea81dSJan Kara if (mpd->map.m_len == 0) 25838eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 25848eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2585f8bec370SJan Kara /* Add all dirty buffers to mpd */ 25864e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 258709cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 25888eb9e5ceSTheodore Ts'o head = page_buffers(page); 25895f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 25905f1132b2SJan Kara if (err <= 0) 25914e7ea81dSJan Kara goto out; 25925f1132b2SJan Kara err = 0; 2593aeac589aSMing Lei left--; 25948e48dcfbSTheodore Ts'o } 25958e48dcfbSTheodore Ts'o pagevec_release(&pvec); 25968e48dcfbSTheodore Ts'o cond_resched(); 25978e48dcfbSTheodore Ts'o } 25984f01b02cSTheodore Ts'o return 0; 25998eb9e5ceSTheodore Ts'o out: 26008eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26014e7ea81dSJan Kara return err; 26028e48dcfbSTheodore Ts'o } 26038e48dcfbSTheodore Ts'o 260420970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 260564769240SAlex Tomas struct writeback_control *wbc) 260664769240SAlex Tomas { 26074e7ea81dSJan Kara pgoff_t writeback_index = 0; 26084e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 260922208dedSAneesh Kumar K.V int range_whole = 0; 26104e7ea81dSJan Kara int cycled = 1; 261161628a3fSMingming Cao handle_t *handle = NULL; 2612df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26135e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26146b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26155e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26164e7ea81dSJan Kara bool done; 26171bce63d1SShaohua Li struct blk_plug plug; 2618cb530541STheodore Ts'o bool give_up_on_write = false; 261961628a3fSMingming Cao 26200db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26210db1ff22STheodore Ts'o return -EIO; 26220db1ff22STheodore Ts'o 2623c8585c6fSDaeho Jeong percpu_down_read(&sbi->s_journal_flag_rwsem); 262420970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2625ba80b101STheodore Ts'o 262661628a3fSMingming Cao /* 262761628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 262861628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 262961628a3fSMingming Cao * because that could violate lock ordering on umount 263061628a3fSMingming Cao */ 2631a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2632bbf023c7SMing Lei goto out_writepages; 26332a21e37eSTheodore Ts'o 263420970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2635043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2636bbf023c7SMing Lei goto out_writepages; 263720970ba6STheodore Ts'o } 263820970ba6STheodore Ts'o 26392a21e37eSTheodore Ts'o /* 26402a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26412a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26422a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26431751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26442a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 264520970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26462a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26472a21e37eSTheodore Ts'o * the stack trace. 26482a21e37eSTheodore Ts'o */ 26490db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26500db1ff22STheodore Ts'o sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) { 2651bbf023c7SMing Lei ret = -EROFS; 2652bbf023c7SMing Lei goto out_writepages; 2653bbf023c7SMing Lei } 26542a21e37eSTheodore Ts'o 26554e7ea81dSJan Kara /* 26564e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26574e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26584e7ea81dSJan Kara * we'd better clear the inline data here. 26594e7ea81dSJan Kara */ 26604e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26614e7ea81dSJan Kara /* Just inode will be modified... */ 26624e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26634e7ea81dSJan Kara if (IS_ERR(handle)) { 26644e7ea81dSJan Kara ret = PTR_ERR(handle); 26654e7ea81dSJan Kara goto out_writepages; 26664e7ea81dSJan Kara } 26674e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26684e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26694e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26704e7ea81dSJan Kara ext4_journal_stop(handle); 26714e7ea81dSJan Kara } 26724e7ea81dSJan Kara 26734e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26744e343231Syangerkun /* 26754e343231Syangerkun * We may need to convert up to one extent per block in 26764e343231Syangerkun * the page and we may dirty the inode. 26774e343231Syangerkun */ 26784e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 26794e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 26804e343231Syangerkun } 26814e343231Syangerkun 268222208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 268322208dedSAneesh Kumar K.V range_whole = 1; 268461628a3fSMingming Cao 26852acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 26864e7ea81dSJan Kara writeback_index = mapping->writeback_index; 26874e7ea81dSJan Kara if (writeback_index) 26882acf2c26SAneesh Kumar K.V cycled = 0; 26894e7ea81dSJan Kara mpd.first_page = writeback_index; 26904e7ea81dSJan Kara mpd.last_page = -1; 26915b41d924SEric Sandeen } else { 269209cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 269309cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 26945b41d924SEric Sandeen } 2695a1d6cc56SAneesh Kumar K.V 26964e7ea81dSJan Kara mpd.inode = inode; 26974e7ea81dSJan Kara mpd.wbc = wbc; 26984e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 26992acf2c26SAneesh Kumar K.V retry: 27006e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27014e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27024e7ea81dSJan Kara done = false; 27031bce63d1SShaohua Li blk_start_plug(&plug); 2704dddbd6acSJan Kara 2705dddbd6acSJan Kara /* 2706dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2707dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2708dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2709dddbd6acSJan Kara * started. 2710dddbd6acSJan Kara */ 2711dddbd6acSJan Kara mpd.do_map = 0; 2712dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2713dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2714dddbd6acSJan Kara ret = -ENOMEM; 2715dddbd6acSJan Kara goto unplug; 2716dddbd6acSJan Kara } 2717dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2718a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2719a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2720dddbd6acSJan Kara /* Submit prepared bio */ 2721dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2722dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2723dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2724dddbd6acSJan Kara if (ret < 0) 2725dddbd6acSJan Kara goto unplug; 2726dddbd6acSJan Kara 27274e7ea81dSJan Kara while (!done && mpd.first_page <= mpd.last_page) { 27284e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27294e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27304e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27314e7ea81dSJan Kara ret = -ENOMEM; 27324e7ea81dSJan Kara break; 27334e7ea81dSJan Kara } 2734a1d6cc56SAneesh Kumar K.V 2735a1d6cc56SAneesh Kumar K.V /* 27364e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27374e7ea81dSJan Kara * must always write out whole page (makes a difference when 27384e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27394e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27404e7ea81dSJan Kara * not supported by delalloc. 2741a1d6cc56SAneesh Kumar K.V */ 2742a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2743525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2744a1d6cc56SAneesh Kumar K.V 274561628a3fSMingming Cao /* start a new transaction */ 27466b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27476b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 274861628a3fSMingming Cao if (IS_ERR(handle)) { 274961628a3fSMingming Cao ret = PTR_ERR(handle); 27501693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2751fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2752a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27534e7ea81dSJan Kara /* Release allocated io_end */ 27544e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2755dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27564e7ea81dSJan Kara break; 275761628a3fSMingming Cao } 2758dddbd6acSJan Kara mpd.do_map = 1; 2759f63e6005STheodore Ts'o 27604e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27614e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27624e7ea81dSJan Kara if (!ret) { 27634e7ea81dSJan Kara if (mpd.map.m_len) 2764cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2765cb530541STheodore Ts'o &give_up_on_write); 27664e7ea81dSJan Kara else { 2767f63e6005STheodore Ts'o /* 27684e7ea81dSJan Kara * We scanned the whole range (or exhausted 27694e7ea81dSJan Kara * nr_to_write), submitted what was mapped and 27704e7ea81dSJan Kara * didn't find anything needing mapping. We are 27714e7ea81dSJan Kara * done. 2772f63e6005STheodore Ts'o */ 27734e7ea81dSJan Kara done = true; 2774f63e6005STheodore Ts'o } 27754e7ea81dSJan Kara } 2776646caa9cSJan Kara /* 2777646caa9cSJan Kara * Caution: If the handle is synchronous, 2778646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2779646caa9cSJan Kara * to finish which may depend on writeback of pages to 2780646caa9cSJan Kara * complete or on page lock to be released. In that 2781646caa9cSJan Kara * case, we have to wait until after after we have 2782646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2783646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2784646caa9cSJan Kara * to be able to complete) before stopping the handle. 2785646caa9cSJan Kara */ 2786646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 278761628a3fSMingming Cao ext4_journal_stop(handle); 2788646caa9cSJan Kara handle = NULL; 2789dddbd6acSJan Kara mpd.do_map = 0; 2790646caa9cSJan Kara } 27914e7ea81dSJan Kara /* Unlock pages we didn't use */ 2792cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2793a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2794a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2795a297b2fcSXiaoguang Wang 2796646caa9cSJan Kara /* 2797646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2798646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2799646caa9cSJan Kara * we are still holding the transaction as we can 2800646caa9cSJan Kara * release the last reference to io_end which may end 2801646caa9cSJan Kara * up doing unwritten extent conversion. 2802646caa9cSJan Kara */ 2803646caa9cSJan Kara if (handle) { 2804646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2805646caa9cSJan Kara ext4_journal_stop(handle); 2806646caa9cSJan Kara } else 28074e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2808dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2809df22291fSAneesh Kumar K.V 28104e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28114e7ea81dSJan Kara /* 28124e7ea81dSJan Kara * Commit the transaction which would 281322208dedSAneesh Kumar K.V * free blocks released in the transaction 281422208dedSAneesh Kumar K.V * and try again 281522208dedSAneesh Kumar K.V */ 2816df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 281722208dedSAneesh Kumar K.V ret = 0; 28184e7ea81dSJan Kara continue; 28194e7ea81dSJan Kara } 28204e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28214e7ea81dSJan Kara if (ret) 282261628a3fSMingming Cao break; 282361628a3fSMingming Cao } 2824dddbd6acSJan Kara unplug: 28251bce63d1SShaohua Li blk_finish_plug(&plug); 28269c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28272acf2c26SAneesh Kumar K.V cycled = 1; 28284e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28294e7ea81dSJan Kara mpd.first_page = 0; 28302acf2c26SAneesh Kumar K.V goto retry; 28312acf2c26SAneesh Kumar K.V } 283261628a3fSMingming Cao 283322208dedSAneesh Kumar K.V /* Update index */ 283422208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 283522208dedSAneesh Kumar K.V /* 28364e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 283722208dedSAneesh Kumar K.V * mode will write it back later 283822208dedSAneesh Kumar K.V */ 28394e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2840a1d6cc56SAneesh Kumar K.V 284161628a3fSMingming Cao out_writepages: 284220970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28434e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2844c8585c6fSDaeho Jeong percpu_up_read(&sbi->s_journal_flag_rwsem); 284561628a3fSMingming Cao return ret; 284664769240SAlex Tomas } 284764769240SAlex Tomas 28485f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28495f0663bbSDan Williams struct writeback_control *wbc) 28505f0663bbSDan Williams { 28515f0663bbSDan Williams int ret; 28525f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28535f0663bbSDan Williams struct inode *inode = mapping->host; 28545f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28555f0663bbSDan Williams 28565f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28575f0663bbSDan Williams return -EIO; 28585f0663bbSDan Williams 28595f0663bbSDan Williams percpu_down_read(&sbi->s_journal_flag_rwsem); 28605f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28615f0663bbSDan Williams 28625f0663bbSDan Williams ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev, wbc); 28635f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28645f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 28655f0663bbSDan Williams percpu_up_read(&sbi->s_journal_flag_rwsem); 28665f0663bbSDan Williams return ret; 28675f0663bbSDan Williams } 28685f0663bbSDan Williams 286979f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 287079f0be8dSAneesh Kumar K.V { 28715c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 287279f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 287379f0be8dSAneesh Kumar K.V 287479f0be8dSAneesh Kumar K.V /* 287579f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 287679f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2877179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 287879f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 287979f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 288079f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 288179f0be8dSAneesh Kumar K.V */ 28825c1ff336SEric Whitney free_clusters = 28835c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28845c1ff336SEric Whitney dirty_clusters = 28855c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 288600d4e736STheodore Ts'o /* 288700d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 288800d4e736STheodore Ts'o */ 28895c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 289010ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 289100d4e736STheodore Ts'o 28925c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 28935c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 289479f0be8dSAneesh Kumar K.V /* 2895c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2896c8afb446SEric Sandeen * or free blocks is less than watermark 289779f0be8dSAneesh Kumar K.V */ 289879f0be8dSAneesh Kumar K.V return 1; 289979f0be8dSAneesh Kumar K.V } 290079f0be8dSAneesh Kumar K.V return 0; 290179f0be8dSAneesh Kumar K.V } 290279f0be8dSAneesh Kumar K.V 29030ff8947fSEric Sandeen /* We always reserve for an inode update; the superblock could be there too */ 29040ff8947fSEric Sandeen static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) 29050ff8947fSEric Sandeen { 2906e2b911c5SDarrick J. Wong if (likely(ext4_has_feature_large_file(inode->i_sb))) 29070ff8947fSEric Sandeen return 1; 29080ff8947fSEric Sandeen 29090ff8947fSEric Sandeen if (pos + len <= 0x7fffffffULL) 29100ff8947fSEric Sandeen return 1; 29110ff8947fSEric Sandeen 29120ff8947fSEric Sandeen /* We might need to update the superblock to set LARGE_FILE */ 29130ff8947fSEric Sandeen return 2; 29140ff8947fSEric Sandeen } 29150ff8947fSEric Sandeen 291664769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 291764769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 291864769240SAlex Tomas struct page **pagep, void **fsdata) 291964769240SAlex Tomas { 292072b8ab9dSEric Sandeen int ret, retries = 0; 292164769240SAlex Tomas struct page *page; 292264769240SAlex Tomas pgoff_t index; 292364769240SAlex Tomas struct inode *inode = mapping->host; 292464769240SAlex Tomas handle_t *handle; 292564769240SAlex Tomas 29260db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29270db1ff22STheodore Ts'o return -EIO; 29280db1ff22STheodore Ts'o 292909cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 293079f0be8dSAneesh Kumar K.V 2931c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2932c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 293379f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 293479f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 293579f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 293679f0be8dSAneesh Kumar K.V } 293779f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29389bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29399c3569b5STao Ma 29409c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29419c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29429c3569b5STao Ma pos, len, flags, 29439c3569b5STao Ma pagep, fsdata); 29449c3569b5STao Ma if (ret < 0) 294547564bfbSTheodore Ts'o return ret; 294647564bfbSTheodore Ts'o if (ret == 1) 294747564bfbSTheodore Ts'o return 0; 29489c3569b5STao Ma } 29499c3569b5STao Ma 295047564bfbSTheodore Ts'o /* 295147564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 295247564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 295347564bfbSTheodore Ts'o * is being written back. So grab it first before we start 295447564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 295547564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 295647564bfbSTheodore Ts'o */ 295747564bfbSTheodore Ts'o retry_grab: 295847564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 295947564bfbSTheodore Ts'o if (!page) 296047564bfbSTheodore Ts'o return -ENOMEM; 296147564bfbSTheodore Ts'o unlock_page(page); 296247564bfbSTheodore Ts'o 296364769240SAlex Tomas /* 296464769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 296564769240SAlex Tomas * if there is delayed block allocation. But we still need 296664769240SAlex Tomas * to journalling the i_disksize update if writes to the end 296764769240SAlex Tomas * of file which has an already mapped buffer. 296864769240SAlex Tomas */ 296947564bfbSTheodore Ts'o retry_journal: 29700ff8947fSEric Sandeen handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 29710ff8947fSEric Sandeen ext4_da_write_credits(inode, pos, len)); 297264769240SAlex Tomas if (IS_ERR(handle)) { 297309cbfeafSKirill A. Shutemov put_page(page); 297447564bfbSTheodore Ts'o return PTR_ERR(handle); 297564769240SAlex Tomas } 297664769240SAlex Tomas 297747564bfbSTheodore Ts'o lock_page(page); 297847564bfbSTheodore Ts'o if (page->mapping != mapping) { 297947564bfbSTheodore Ts'o /* The page got truncated from under us */ 298047564bfbSTheodore Ts'o unlock_page(page); 298109cbfeafSKirill A. Shutemov put_page(page); 2982d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 298347564bfbSTheodore Ts'o goto retry_grab; 2984d5a0d4f7SEric Sandeen } 298547564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29867afe5aa5SDmitry Monakhov wait_for_stable_page(page); 298764769240SAlex Tomas 2988643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29892058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 29902058f83aSMichael Halcrow ext4_da_get_block_prep); 29912058f83aSMichael Halcrow #else 29926e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 29932058f83aSMichael Halcrow #endif 299464769240SAlex Tomas if (ret < 0) { 299564769240SAlex Tomas unlock_page(page); 299664769240SAlex Tomas ext4_journal_stop(handle); 2997ae4d5372SAneesh Kumar K.V /* 2998ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2999ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3000ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3001ae4d5372SAneesh Kumar K.V */ 3002ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3003b9a4207dSJan Kara ext4_truncate_failed_write(inode); 300447564bfbSTheodore Ts'o 300547564bfbSTheodore Ts'o if (ret == -ENOSPC && 300647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 300747564bfbSTheodore Ts'o goto retry_journal; 300847564bfbSTheodore Ts'o 300909cbfeafSKirill A. Shutemov put_page(page); 301047564bfbSTheodore Ts'o return ret; 301164769240SAlex Tomas } 301264769240SAlex Tomas 301347564bfbSTheodore Ts'o *pagep = page; 301464769240SAlex Tomas return ret; 301564769240SAlex Tomas } 301664769240SAlex Tomas 3017632eaeabSMingming Cao /* 3018632eaeabSMingming Cao * Check if we should update i_disksize 3019632eaeabSMingming Cao * when write to the end of file but not require block allocation 3020632eaeabSMingming Cao */ 3021632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3022632eaeabSMingming Cao unsigned long offset) 3023632eaeabSMingming Cao { 3024632eaeabSMingming Cao struct buffer_head *bh; 3025632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3026632eaeabSMingming Cao unsigned int idx; 3027632eaeabSMingming Cao int i; 3028632eaeabSMingming Cao 3029632eaeabSMingming Cao bh = page_buffers(page); 3030632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3031632eaeabSMingming Cao 3032632eaeabSMingming Cao for (i = 0; i < idx; i++) 3033632eaeabSMingming Cao bh = bh->b_this_page; 3034632eaeabSMingming Cao 303529fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3036632eaeabSMingming Cao return 0; 3037632eaeabSMingming Cao return 1; 3038632eaeabSMingming Cao } 3039632eaeabSMingming Cao 304064769240SAlex Tomas static int ext4_da_write_end(struct file *file, 304164769240SAlex Tomas struct address_space *mapping, 304264769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 304364769240SAlex Tomas struct page *page, void *fsdata) 304464769240SAlex Tomas { 304564769240SAlex Tomas struct inode *inode = mapping->host; 304664769240SAlex Tomas int ret = 0, ret2; 304764769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 304864769240SAlex Tomas loff_t new_i_size; 3049632eaeabSMingming Cao unsigned long start, end; 305079f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 305179f0be8dSAneesh Kumar K.V 305274d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 305374d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 305479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3055632eaeabSMingming Cao 30569bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 305709cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 3058632eaeabSMingming Cao end = start + copied - 1; 305964769240SAlex Tomas 306064769240SAlex Tomas /* 306164769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 306264769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 306364769240SAlex Tomas * into that. 306464769240SAlex Tomas */ 306564769240SAlex Tomas new_i_size = pos + copied; 3066ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 30679c3569b5STao Ma if (ext4_has_inline_data(inode) || 30689c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 3069ee124d27SDmitry Monakhov ext4_update_i_disksize(inode, new_i_size); 3070cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3071cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3072cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3073cf17fea6SAneesh Kumar K.V */ 3074cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3075632eaeabSMingming Cao } 3076632eaeabSMingming Cao } 30779c3569b5STao Ma 30789c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30799c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30809c3569b5STao Ma ext4_has_inline_data(inode)) 30819c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 30829c3569b5STao Ma page); 30839c3569b5STao Ma else 308464769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 308564769240SAlex Tomas page, fsdata); 30869c3569b5STao Ma 308764769240SAlex Tomas copied = ret2; 308864769240SAlex Tomas if (ret2 < 0) 308964769240SAlex Tomas ret = ret2; 309064769240SAlex Tomas ret2 = ext4_journal_stop(handle); 309164769240SAlex Tomas if (!ret) 309264769240SAlex Tomas ret = ret2; 309364769240SAlex Tomas 309464769240SAlex Tomas return ret ? ret : copied; 309564769240SAlex Tomas } 309664769240SAlex Tomas 3097ccd2506bSTheodore Ts'o /* 3098ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3099ccd2506bSTheodore Ts'o */ 3100ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3101ccd2506bSTheodore Ts'o { 3102fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3103fb40ba0dSTheodore Ts'o 310471d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3105ccd2506bSTheodore Ts'o return 0; 3106ccd2506bSTheodore Ts'o 3107ccd2506bSTheodore Ts'o /* 3108ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3109ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3110ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3111ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3112ccd2506bSTheodore Ts'o * would require replicating code paths in: 3113ccd2506bSTheodore Ts'o * 311420970ba6STheodore Ts'o * ext4_writepages() -> 3115ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3116ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3117ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3118ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3119ccd2506bSTheodore Ts'o * 3120ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3121ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3122ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3123ccd2506bSTheodore Ts'o * doing I/O at all. 3124ccd2506bSTheodore Ts'o * 3125ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3126380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3127ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3128ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 312925985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3130ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3131ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3132ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3133ccd2506bSTheodore Ts'o * 3134ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3135ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3136ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3137ccd2506bSTheodore Ts'o */ 3138ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3139ccd2506bSTheodore Ts'o } 314064769240SAlex Tomas 314164769240SAlex Tomas /* 3142ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3143ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3144ac27a0ecSDave Kleikamp * 3145ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3146617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3147ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3148ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3149ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3150ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3151ac27a0ecSDave Kleikamp * 3152ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3153ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3154ac27a0ecSDave Kleikamp */ 3155617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3156ac27a0ecSDave Kleikamp { 3157ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3158ac27a0ecSDave Kleikamp journal_t *journal; 3159ac27a0ecSDave Kleikamp int err; 3160ac27a0ecSDave Kleikamp 316146c7f254STao Ma /* 316246c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 316346c7f254STao Ma */ 316446c7f254STao Ma if (ext4_has_inline_data(inode)) 316546c7f254STao Ma return 0; 316646c7f254STao Ma 316764769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 316864769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 316964769240SAlex Tomas /* 317064769240SAlex Tomas * With delalloc we want to sync the file 317164769240SAlex Tomas * so that we can make sure we allocate 317264769240SAlex Tomas * blocks for file 317364769240SAlex Tomas */ 317464769240SAlex Tomas filemap_write_and_wait(mapping); 317564769240SAlex Tomas } 317664769240SAlex Tomas 317719f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 317819f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3179ac27a0ecSDave Kleikamp /* 3180ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3181ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3182ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3183ac27a0ecSDave Kleikamp * do we expect this to happen. 3184ac27a0ecSDave Kleikamp * 3185ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3186ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3187ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3188ac27a0ecSDave Kleikamp * will.) 3189ac27a0ecSDave Kleikamp * 3190617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3191ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3192ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3193ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3194ac27a0ecSDave Kleikamp * everything they get. 3195ac27a0ecSDave Kleikamp */ 3196ac27a0ecSDave Kleikamp 319719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3198617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3199dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3200dab291afSMingming Cao err = jbd2_journal_flush(journal); 3201dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3202ac27a0ecSDave Kleikamp 3203ac27a0ecSDave Kleikamp if (err) 3204ac27a0ecSDave Kleikamp return 0; 3205ac27a0ecSDave Kleikamp } 3206ac27a0ecSDave Kleikamp 3207617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3208ac27a0ecSDave Kleikamp } 3209ac27a0ecSDave Kleikamp 3210617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3211ac27a0ecSDave Kleikamp { 321246c7f254STao Ma int ret = -EAGAIN; 321346c7f254STao Ma struct inode *inode = page->mapping->host; 321446c7f254STao Ma 32150562e0baSJiaying Zhang trace_ext4_readpage(page); 321646c7f254STao Ma 321746c7f254STao Ma if (ext4_has_inline_data(inode)) 321846c7f254STao Ma ret = ext4_readpage_inline(inode, page); 321946c7f254STao Ma 322046c7f254STao Ma if (ret == -EAGAIN) 3221ac22b46aSJens Axboe return ext4_mpage_readpages(page->mapping, NULL, page, 1, 3222ac22b46aSJens Axboe false); 322346c7f254STao Ma 322446c7f254STao Ma return ret; 3225ac27a0ecSDave Kleikamp } 3226ac27a0ecSDave Kleikamp 3227ac27a0ecSDave Kleikamp static int 3228617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3229ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3230ac27a0ecSDave Kleikamp { 323146c7f254STao Ma struct inode *inode = mapping->host; 323246c7f254STao Ma 323346c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 323446c7f254STao Ma if (ext4_has_inline_data(inode)) 323546c7f254STao Ma return 0; 323646c7f254STao Ma 3237ac22b46aSJens Axboe return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true); 3238ac27a0ecSDave Kleikamp } 3239ac27a0ecSDave Kleikamp 3240d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3241d47992f8SLukas Czerner unsigned int length) 3242ac27a0ecSDave Kleikamp { 3243ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32440562e0baSJiaying Zhang 32454520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32464520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32474520fb3cSJan Kara 3248ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32494520fb3cSJan Kara } 32504520fb3cSJan Kara 325153e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3252ca99fdd2SLukas Czerner unsigned int offset, 3253ca99fdd2SLukas Czerner unsigned int length) 32544520fb3cSJan Kara { 32554520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32564520fb3cSJan Kara 3257ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32584520fb3cSJan Kara 3259744692dcSJiaying Zhang /* 3260ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3261ac27a0ecSDave Kleikamp */ 326209cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3263ac27a0ecSDave Kleikamp ClearPageChecked(page); 3264ac27a0ecSDave Kleikamp 3265ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 326653e87268SJan Kara } 326753e87268SJan Kara 326853e87268SJan Kara /* Wrapper for aops... */ 326953e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3270d47992f8SLukas Czerner unsigned int offset, 3271d47992f8SLukas Czerner unsigned int length) 327253e87268SJan Kara { 3273ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3274ac27a0ecSDave Kleikamp } 3275ac27a0ecSDave Kleikamp 3276617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3277ac27a0ecSDave Kleikamp { 3278617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3279ac27a0ecSDave Kleikamp 32800562e0baSJiaying Zhang trace_ext4_releasepage(page); 32810562e0baSJiaying Zhang 3282e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3283e1c36595SJan Kara if (PageChecked(page)) 3284ac27a0ecSDave Kleikamp return 0; 32850390131bSFrank Mayhar if (journal) 3286dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 32870390131bSFrank Mayhar else 32880390131bSFrank Mayhar return try_to_free_buffers(page); 3289ac27a0ecSDave Kleikamp } 3290ac27a0ecSDave Kleikamp 3291b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3292b8a6176cSJan Kara { 3293b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3294b8a6176cSJan Kara 3295b8a6176cSJan Kara if (journal) 3296b8a6176cSJan Kara return !jbd2_transaction_committed(journal, 3297b8a6176cSJan Kara EXT4_I(inode)->i_datasync_tid); 3298b8a6176cSJan Kara /* Any metadata buffers to write? */ 3299b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3300b8a6176cSJan Kara return true; 3301b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3302b8a6176cSJan Kara } 3303b8a6176cSJan Kara 3304c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3305c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3306c8fdfe29SMatthew Bobrowski loff_t length) 3307364443cbSJan Kara { 3308c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3309c8fdfe29SMatthew Bobrowski 3310c8fdfe29SMatthew Bobrowski /* 3311c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3312c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3313c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3314c8fdfe29SMatthew Bobrowski */ 3315c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3316c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3317c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3318c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3319c8fdfe29SMatthew Bobrowski 3320c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3321c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3322c8fdfe29SMatthew Bobrowski 3323c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3324c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3325c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3326c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3327c8fdfe29SMatthew Bobrowski 3328c8fdfe29SMatthew Bobrowski /* 3329c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3330c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3331c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3332c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3333c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3334c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3335c8fdfe29SMatthew Bobrowski * been set first. 3336c8fdfe29SMatthew Bobrowski */ 3337c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3338c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3339c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3340c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3341c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3342c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3343c8fdfe29SMatthew Bobrowski } else { 3344c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3345c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3346c8fdfe29SMatthew Bobrowski } 3347c8fdfe29SMatthew Bobrowski } 3348c8fdfe29SMatthew Bobrowski 3349f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3350f063db5eSMatthew Bobrowski unsigned int flags) 3351f063db5eSMatthew Bobrowski { 3352f063db5eSMatthew Bobrowski handle_t *handle; 3353378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3354378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3355f063db5eSMatthew Bobrowski 3356f063db5eSMatthew Bobrowski /* 3357f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3358f063db5eSMatthew Bobrowski * once for direct I/O. 3359f063db5eSMatthew Bobrowski */ 3360f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3361f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3362f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3363f063db5eSMatthew Bobrowski 3364f063db5eSMatthew Bobrowski retry: 3365f063db5eSMatthew Bobrowski /* 3366f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3367f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3368f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3369f063db5eSMatthew Bobrowski * fits into the credits as well. 3370f063db5eSMatthew Bobrowski */ 3371f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3372f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3373f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3374f063db5eSMatthew Bobrowski 3375378f32baSMatthew Bobrowski /* 3376378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3377378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3378378f32baSMatthew Bobrowski */ 3379378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3380378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3381378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3382378f32baSMatthew Bobrowski /* 3383378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3384378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3385378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3386378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3387378f32baSMatthew Bobrowski */ 3388378f32baSMatthew Bobrowski else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode)) 3389378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3390378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3391378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3392378f32baSMatthew Bobrowski 3393378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3394378f32baSMatthew Bobrowski 3395378f32baSMatthew Bobrowski /* 3396378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3397378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3398378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3399378f32baSMatthew Bobrowski */ 3400378f32baSMatthew Bobrowski if (!m_flags && !ret) 3401378f32baSMatthew Bobrowski ret = -ENOTBLK; 3402f063db5eSMatthew Bobrowski 3403f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3404f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3405f063db5eSMatthew Bobrowski goto retry; 3406f063db5eSMatthew Bobrowski 3407f063db5eSMatthew Bobrowski return ret; 3408f063db5eSMatthew Bobrowski } 3409f063db5eSMatthew Bobrowski 3410f063db5eSMatthew Bobrowski 3411364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3412c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3413364443cbSJan Kara { 3414364443cbSJan Kara int ret; 341509edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 341609edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3417364443cbSJan Kara 3418bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3419bcd8e91fSTheodore Ts'o return -EINVAL; 34207046ae35SAndreas Gruenbacher 3421364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3422364443cbSJan Kara return -ERANGE; 3423364443cbSJan Kara 342409edf4d3SMatthew Bobrowski /* 342509edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 342609edf4d3SMatthew Bobrowski */ 342709edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 342809edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 342909edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3430364443cbSJan Kara 343109edf4d3SMatthew Bobrowski if (flags & IOMAP_WRITE) 3432f063db5eSMatthew Bobrowski ret = ext4_iomap_alloc(inode, &map, flags); 343309edf4d3SMatthew Bobrowski else 3434545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 3435f063db5eSMatthew Bobrowski 3436545052e9SChristoph Hellwig if (ret < 0) 3437545052e9SChristoph Hellwig return ret; 3438364443cbSJan Kara 3439c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3440545052e9SChristoph Hellwig 3441364443cbSJan Kara return 0; 3442364443cbSJan Kara } 3443364443cbSJan Kara 3444776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3445776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3446776722e8SJan Kara { 3447378f32baSMatthew Bobrowski /* 3448378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3449378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3450378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3451378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3452378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3453378f32baSMatthew Bobrowski */ 3454378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3455378f32baSMatthew Bobrowski return -ENOTBLK; 3456378f32baSMatthew Bobrowski 3457776722e8SJan Kara return 0; 3458776722e8SJan Kara } 3459776722e8SJan Kara 34608ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3461364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3462776722e8SJan Kara .iomap_end = ext4_iomap_end, 3463364443cbSJan Kara }; 3464364443cbSJan Kara 346509edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 346609edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 346709edf4d3SMatthew Bobrowski { 346809edf4d3SMatthew Bobrowski struct extent_status es; 346909edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 347009edf4d3SMatthew Bobrowski 347109edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 347209edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 347309edf4d3SMatthew Bobrowski 347409edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 347509edf4d3SMatthew Bobrowski return false; 347609edf4d3SMatthew Bobrowski 347709edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 347809edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 347909edf4d3SMatthew Bobrowski return false; 348009edf4d3SMatthew Bobrowski } 348109edf4d3SMatthew Bobrowski 348209edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 348309edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 348409edf4d3SMatthew Bobrowski 348509edf4d3SMatthew Bobrowski return true; 348609edf4d3SMatthew Bobrowski } 348709edf4d3SMatthew Bobrowski 348809edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 348909edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 349009edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 349109edf4d3SMatthew Bobrowski { 349209edf4d3SMatthew Bobrowski int ret; 349309edf4d3SMatthew Bobrowski bool delalloc = false; 349409edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 349509edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 349609edf4d3SMatthew Bobrowski 349709edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 349809edf4d3SMatthew Bobrowski return -EINVAL; 349909edf4d3SMatthew Bobrowski 35007cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35017cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3502ba5843f5SJan Kara if (ret != -EAGAIN) { 3503ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3504ba5843f5SJan Kara ret = -ENOENT; 3505ba5843f5SJan Kara return ret; 3506ba5843f5SJan Kara } 3507ba5843f5SJan Kara } 350812735f88SJan Kara 350909edf4d3SMatthew Bobrowski /* 351009edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 351109edf4d3SMatthew Bobrowski */ 351209edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 351309edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 351409edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 351512735f88SJan Kara 351612735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3517ba5843f5SJan Kara if (ret < 0) 3518ba5843f5SJan Kara return ret; 3519914f82a3SJan Kara if (ret == 0) 352009edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 352109edf4d3SMatthew Bobrowski 352209edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 352309edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 352409edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 352509edf4d3SMatthew Bobrowski 352609edf4d3SMatthew Bobrowski return 0; 3527914f82a3SJan Kara } 3528914f82a3SJan Kara 352909edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 353009edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 353109edf4d3SMatthew Bobrowski }; 35324c0425ffSMingming Cao 3533ac27a0ecSDave Kleikamp /* 3534617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3535ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3536ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3537ac27a0ecSDave Kleikamp * not necessarily locked. 3538ac27a0ecSDave Kleikamp * 3539ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3540ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3541ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3542ac27a0ecSDave Kleikamp * 3543ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3544ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3545ac27a0ecSDave Kleikamp */ 3546617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3547ac27a0ecSDave Kleikamp { 3548ac27a0ecSDave Kleikamp SetPageChecked(page); 3549ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3550ac27a0ecSDave Kleikamp } 3551ac27a0ecSDave Kleikamp 35526dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 35536dcc693bSJan Kara { 35546dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 35556dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 35566dcc693bSJan Kara return __set_page_dirty_buffers(page); 35576dcc693bSJan Kara } 35586dcc693bSJan Kara 355974d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3560617ba13bSMingming Cao .readpage = ext4_readpage, 3561617ba13bSMingming Cao .readpages = ext4_readpages, 356243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 356320970ba6STheodore Ts'o .writepages = ext4_writepages, 3564bfc1af65SNick Piggin .write_begin = ext4_write_begin, 356574d553aaSTheodore Ts'o .write_end = ext4_write_end, 35666dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3567617ba13bSMingming Cao .bmap = ext4_bmap, 3568617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3569617ba13bSMingming Cao .releasepage = ext4_releasepage, 3570378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3571ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 35728ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3573aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3574ac27a0ecSDave Kleikamp }; 3575ac27a0ecSDave Kleikamp 3576617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3577617ba13bSMingming Cao .readpage = ext4_readpage, 3578617ba13bSMingming Cao .readpages = ext4_readpages, 357943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 358020970ba6STheodore Ts'o .writepages = ext4_writepages, 3581bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3582bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3583617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3584617ba13bSMingming Cao .bmap = ext4_bmap, 35854520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3586617ba13bSMingming Cao .releasepage = ext4_releasepage, 3587378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 35888ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3589aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3590ac27a0ecSDave Kleikamp }; 3591ac27a0ecSDave Kleikamp 359264769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 359364769240SAlex Tomas .readpage = ext4_readpage, 359464769240SAlex Tomas .readpages = ext4_readpages, 359543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 359620970ba6STheodore Ts'o .writepages = ext4_writepages, 359764769240SAlex Tomas .write_begin = ext4_da_write_begin, 359864769240SAlex Tomas .write_end = ext4_da_write_end, 35996dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 360064769240SAlex Tomas .bmap = ext4_bmap, 36018fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 360264769240SAlex Tomas .releasepage = ext4_releasepage, 3603378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 360464769240SAlex Tomas .migratepage = buffer_migrate_page, 36058ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3606aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 360764769240SAlex Tomas }; 360864769240SAlex Tomas 36095f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36105f0663bbSDan Williams .writepages = ext4_dax_writepages, 36115f0663bbSDan Williams .direct_IO = noop_direct_IO, 36125f0663bbSDan Williams .set_page_dirty = noop_set_page_dirty, 361394dbb631SToshi Kani .bmap = ext4_bmap, 36145f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36155f0663bbSDan Williams }; 36165f0663bbSDan Williams 3617617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3618ac27a0ecSDave Kleikamp { 36193d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36203d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36213d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36223d2b1582SLukas Czerner break; 36233d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3624617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 362574d553aaSTheodore Ts'o return; 36263d2b1582SLukas Czerner default: 36273d2b1582SLukas Czerner BUG(); 36283d2b1582SLukas Czerner } 36295f0663bbSDan Williams if (IS_DAX(inode)) 36305f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 36315f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 363274d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 363374d553aaSTheodore Ts'o else 363474d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3635ac27a0ecSDave Kleikamp } 3636ac27a0ecSDave Kleikamp 3637923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3638d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3639d863dc36SLukas Czerner { 364009cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 364109cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3642923ae0ffSRoss Zwisler unsigned blocksize, pos; 3643d863dc36SLukas Czerner ext4_lblk_t iblock; 3644d863dc36SLukas Czerner struct inode *inode = mapping->host; 3645d863dc36SLukas Czerner struct buffer_head *bh; 3646d863dc36SLukas Czerner struct page *page; 3647d863dc36SLukas Czerner int err = 0; 3648d863dc36SLukas Czerner 364909cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3650c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3651d863dc36SLukas Czerner if (!page) 3652d863dc36SLukas Czerner return -ENOMEM; 3653d863dc36SLukas Czerner 3654d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3655d863dc36SLukas Czerner 365609cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3657d863dc36SLukas Czerner 3658d863dc36SLukas Czerner if (!page_has_buffers(page)) 3659d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3660d863dc36SLukas Czerner 3661d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3662d863dc36SLukas Czerner bh = page_buffers(page); 3663d863dc36SLukas Czerner pos = blocksize; 3664d863dc36SLukas Czerner while (offset >= pos) { 3665d863dc36SLukas Czerner bh = bh->b_this_page; 3666d863dc36SLukas Czerner iblock++; 3667d863dc36SLukas Czerner pos += blocksize; 3668d863dc36SLukas Czerner } 3669d863dc36SLukas Czerner if (buffer_freed(bh)) { 3670d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3671d863dc36SLukas Czerner goto unlock; 3672d863dc36SLukas Czerner } 3673d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3674d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3675d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3676d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3677d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3678d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3679d863dc36SLukas Czerner goto unlock; 3680d863dc36SLukas Czerner } 3681d863dc36SLukas Czerner } 3682d863dc36SLukas Czerner 3683d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3684d863dc36SLukas Czerner if (PageUptodate(page)) 3685d863dc36SLukas Czerner set_buffer_uptodate(bh); 3686d863dc36SLukas Czerner 3687d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 3688d863dc36SLukas Czerner err = -EIO; 3689dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 3690d863dc36SLukas Czerner wait_on_buffer(bh); 3691d863dc36SLukas Czerner /* Uhhuh. Read error. Complain and punt. */ 3692d863dc36SLukas Czerner if (!buffer_uptodate(bh)) 3693d863dc36SLukas Czerner goto unlock; 3694592ddec7SChandan Rajendra if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) { 3695c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3696a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3697aa8bc1acSEric Biggers WARN_ON_ONCE(fscrypt_decrypt_pagecache_blocks( 3698ec39a368SChandan Rajendra page, blocksize, bh_offset(bh))); 3699c9c7429cSMichael Halcrow } 3700d863dc36SLukas Czerner } 3701d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3702d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3703d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3704d863dc36SLukas Czerner if (err) 3705d863dc36SLukas Czerner goto unlock; 3706d863dc36SLukas Czerner } 3707d863dc36SLukas Czerner zero_user(page, offset, length); 3708d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3709d863dc36SLukas Czerner 3710d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3711d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37120713ed0cSLukas Czerner } else { 3713353eefd3Sjon ernst err = 0; 3714d863dc36SLukas Czerner mark_buffer_dirty(bh); 37153957ef53SJan Kara if (ext4_should_order_data(inode)) 371673131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 371773131fbbSRoss Zwisler length); 37180713ed0cSLukas Czerner } 3719d863dc36SLukas Czerner 3720d863dc36SLukas Czerner unlock: 3721d863dc36SLukas Czerner unlock_page(page); 372209cbfeafSKirill A. Shutemov put_page(page); 3723d863dc36SLukas Czerner return err; 3724d863dc36SLukas Czerner } 3725d863dc36SLukas Czerner 372694350ab5SMatthew Wilcox /* 3727923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3728923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3729923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3730923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 3731923ae0ffSRoss Zwisler * that cooresponds to 'from' 3732923ae0ffSRoss Zwisler */ 3733923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3734923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3735923ae0ffSRoss Zwisler { 3736923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 373709cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3738923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3739923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3740923ae0ffSRoss Zwisler 3741923ae0ffSRoss Zwisler /* 3742923ae0ffSRoss Zwisler * correct length if it does not fall between 3743923ae0ffSRoss Zwisler * 'from' and the end of the block 3744923ae0ffSRoss Zwisler */ 3745923ae0ffSRoss Zwisler if (length > max || length < 0) 3746923ae0ffSRoss Zwisler length = max; 3747923ae0ffSRoss Zwisler 374847e69351SJan Kara if (IS_DAX(inode)) { 374947e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 375047e69351SJan Kara &ext4_iomap_ops); 375147e69351SJan Kara } 3752923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3753923ae0ffSRoss Zwisler } 3754923ae0ffSRoss Zwisler 3755923ae0ffSRoss Zwisler /* 375694350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 375794350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 375894350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 375994350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 376094350ab5SMatthew Wilcox */ 3761c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 376294350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 376394350ab5SMatthew Wilcox { 376409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 376594350ab5SMatthew Wilcox unsigned length; 376694350ab5SMatthew Wilcox unsigned blocksize; 376794350ab5SMatthew Wilcox struct inode *inode = mapping->host; 376894350ab5SMatthew Wilcox 37690d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3770592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 37710d06863fSTheodore Ts'o return 0; 37720d06863fSTheodore Ts'o 377394350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 377494350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 377594350ab5SMatthew Wilcox 377694350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 377794350ab5SMatthew Wilcox } 377894350ab5SMatthew Wilcox 3779a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3780a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3781a87dd18cSLukas Czerner { 3782a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3783a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3784e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3785a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3786a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3787a87dd18cSLukas Czerner int err = 0; 3788a87dd18cSLukas Czerner 3789e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3790e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3791e1be3a92SLukas Czerner 3792a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3793a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3794a87dd18cSLukas Czerner 3795a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3796e1be3a92SLukas Czerner if (start == end && 3797e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3798a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3799a87dd18cSLukas Czerner lstart, length); 3800a87dd18cSLukas Czerner return err; 3801a87dd18cSLukas Czerner } 3802a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3803e1be3a92SLukas Czerner if (partial_start) { 3804a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3805a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3806a87dd18cSLukas Czerner if (err) 3807a87dd18cSLukas Czerner return err; 3808a87dd18cSLukas Czerner } 3809a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3810e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3811a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3812e1be3a92SLukas Czerner byte_end - partial_end, 3813e1be3a92SLukas Czerner partial_end + 1); 3814a87dd18cSLukas Czerner return err; 3815a87dd18cSLukas Czerner } 3816a87dd18cSLukas Czerner 381791ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 381891ef4cafSDuane Griffin { 381991ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 382091ef4cafSDuane Griffin return 1; 382191ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 382291ef4cafSDuane Griffin return 1; 382391ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 382491ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 382591ef4cafSDuane Griffin return 0; 382691ef4cafSDuane Griffin } 382791ef4cafSDuane Griffin 3828ac27a0ecSDave Kleikamp /* 382901127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 383001127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 383101127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 383201127848SJan Kara * that will never happen after we truncate page cache. 383301127848SJan Kara */ 383401127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 383501127848SJan Kara loff_t len) 383601127848SJan Kara { 383701127848SJan Kara handle_t *handle; 383801127848SJan Kara loff_t size = i_size_read(inode); 383901127848SJan Kara 38405955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 384101127848SJan Kara if (offset > size || offset + len < size) 384201127848SJan Kara return 0; 384301127848SJan Kara 384401127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 384501127848SJan Kara return 0; 384601127848SJan Kara 384701127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 384801127848SJan Kara if (IS_ERR(handle)) 384901127848SJan Kara return PTR_ERR(handle); 385001127848SJan Kara ext4_update_i_disksize(inode, size); 385101127848SJan Kara ext4_mark_inode_dirty(handle, inode); 385201127848SJan Kara ext4_journal_stop(handle); 385301127848SJan Kara 385401127848SJan Kara return 0; 385501127848SJan Kara } 385601127848SJan Kara 3857b1f38217SRoss Zwisler static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3858430657b6SRoss Zwisler { 3859430657b6SRoss Zwisler up_write(&ei->i_mmap_sem); 3860430657b6SRoss Zwisler schedule(); 3861430657b6SRoss Zwisler down_write(&ei->i_mmap_sem); 3862430657b6SRoss Zwisler } 3863430657b6SRoss Zwisler 3864430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3865430657b6SRoss Zwisler { 3866430657b6SRoss Zwisler struct ext4_inode_info *ei = EXT4_I(inode); 3867430657b6SRoss Zwisler struct page *page; 3868430657b6SRoss Zwisler int error; 3869430657b6SRoss Zwisler 3870430657b6SRoss Zwisler if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3871430657b6SRoss Zwisler return -EINVAL; 3872430657b6SRoss Zwisler 3873430657b6SRoss Zwisler do { 3874430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3875430657b6SRoss Zwisler if (!page) 3876430657b6SRoss Zwisler return 0; 3877430657b6SRoss Zwisler 3878430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3879430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3880430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3881b1f38217SRoss Zwisler ext4_wait_dax_page(ei)); 3882b1f38217SRoss Zwisler } while (error == 0); 3883430657b6SRoss Zwisler 3884430657b6SRoss Zwisler return error; 3885430657b6SRoss Zwisler } 3886430657b6SRoss Zwisler 388701127848SJan Kara /* 3888cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3889a4bb6b64SAllison Henderson * associated with the given offset and length 3890a4bb6b64SAllison Henderson * 3891a4bb6b64SAllison Henderson * @inode: File inode 3892a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3893a4bb6b64SAllison Henderson * @len: The length of the hole 3894a4bb6b64SAllison Henderson * 38954907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3896a4bb6b64SAllison Henderson */ 3897a4bb6b64SAllison Henderson 3898aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3899a4bb6b64SAllison Henderson { 390026a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 390126a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 390226a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3903a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 390426a4c0c6STheodore Ts'o handle_t *handle; 390526a4c0c6STheodore Ts'o unsigned int credits; 390626a4c0c6STheodore Ts'o int ret = 0; 390726a4c0c6STheodore Ts'o 3908a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 390973355192SAllison Henderson return -EOPNOTSUPP; 3910a4bb6b64SAllison Henderson 3911b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3912aaddea81SZheng Liu 3913c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3914c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3915c1e8220bSTheodore Ts'o down_write(&EXT4_I(inode)->i_mmap_sem); 3916c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3917c1e8220bSTheodore Ts'o up_write(&EXT4_I(inode)->i_mmap_sem); 3918c1e8220bSTheodore Ts'o if (ret) 3919c1e8220bSTheodore Ts'o return ret; 3920c1e8220bSTheodore Ts'o } 3921c1e8220bSTheodore Ts'o 392226a4c0c6STheodore Ts'o /* 392326a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 392426a4c0c6STheodore Ts'o * Then release them. 392526a4c0c6STheodore Ts'o */ 3926cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 392726a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 392826a4c0c6STheodore Ts'o offset + length - 1); 392926a4c0c6STheodore Ts'o if (ret) 393026a4c0c6STheodore Ts'o return ret; 393126a4c0c6STheodore Ts'o } 393226a4c0c6STheodore Ts'o 39335955102cSAl Viro inode_lock(inode); 39349ef06cecSLukas Czerner 393526a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 393626a4c0c6STheodore Ts'o if (offset >= inode->i_size) 393726a4c0c6STheodore Ts'o goto out_mutex; 393826a4c0c6STheodore Ts'o 393926a4c0c6STheodore Ts'o /* 394026a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 394126a4c0c6STheodore Ts'o * to end after the page that contains i_size 394226a4c0c6STheodore Ts'o */ 394326a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 394426a4c0c6STheodore Ts'o length = inode->i_size + 394509cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 394626a4c0c6STheodore Ts'o offset; 394726a4c0c6STheodore Ts'o } 394826a4c0c6STheodore Ts'o 3949a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 3950a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 3951a361293fSJan Kara /* 3952a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 3953a361293fSJan Kara * partial block 3954a361293fSJan Kara */ 3955a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 3956a361293fSJan Kara if (ret < 0) 3957a361293fSJan Kara goto out_mutex; 3958a361293fSJan Kara 3959a361293fSJan Kara } 3960a361293fSJan Kara 3961ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 3962ea3d7209SJan Kara inode_dio_wait(inode); 3963ea3d7209SJan Kara 3964ea3d7209SJan Kara /* 3965ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 3966ea3d7209SJan Kara * page cache. 3967ea3d7209SJan Kara */ 3968ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 3969430657b6SRoss Zwisler 3970430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 3971430657b6SRoss Zwisler if (ret) 3972430657b6SRoss Zwisler goto out_dio; 3973430657b6SRoss Zwisler 3974a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 3975a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 397626a4c0c6STheodore Ts'o 3977a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 397801127848SJan Kara if (last_block_offset > first_block_offset) { 397901127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 398001127848SJan Kara if (ret) 398101127848SJan Kara goto out_dio; 3982a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 3983a87dd18cSLukas Czerner last_block_offset); 398401127848SJan Kara } 398526a4c0c6STheodore Ts'o 398626a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 398726a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 398826a4c0c6STheodore Ts'o else 398926a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 399026a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 399126a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 399226a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 399326a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 399426a4c0c6STheodore Ts'o goto out_dio; 399526a4c0c6STheodore Ts'o } 399626a4c0c6STheodore Ts'o 3997a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 3998a87dd18cSLukas Czerner length); 399926a4c0c6STheodore Ts'o if (ret) 400026a4c0c6STheodore Ts'o goto out_stop; 400126a4c0c6STheodore Ts'o 400226a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 400326a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 400426a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 400526a4c0c6STheodore Ts'o 4006eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4007eee597acSLukas Czerner if (stop_block > first_block) { 400826a4c0c6STheodore Ts'o 400926a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 401026a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 401126a4c0c6STheodore Ts'o 401226a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 401326a4c0c6STheodore Ts'o stop_block - first_block); 401426a4c0c6STheodore Ts'o if (ret) { 401526a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 401626a4c0c6STheodore Ts'o goto out_stop; 401726a4c0c6STheodore Ts'o } 401826a4c0c6STheodore Ts'o 401926a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 402026a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 402126a4c0c6STheodore Ts'o stop_block - 1); 402226a4c0c6STheodore Ts'o else 40234f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 402426a4c0c6STheodore Ts'o stop_block); 402526a4c0c6STheodore Ts'o 4026819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4027eee597acSLukas Czerner } 402826a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 402926a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4030e251f9bcSMaxim Patlasov 4031eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 403226a4c0c6STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 403367a7d5f5SJan Kara if (ret >= 0) 403467a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 403526a4c0c6STheodore Ts'o out_stop: 403626a4c0c6STheodore Ts'o ext4_journal_stop(handle); 403726a4c0c6STheodore Ts'o out_dio: 4038ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 403926a4c0c6STheodore Ts'o out_mutex: 40405955102cSAl Viro inode_unlock(inode); 404126a4c0c6STheodore Ts'o return ret; 4042a4bb6b64SAllison Henderson } 4043a4bb6b64SAllison Henderson 4044a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4045a361293fSJan Kara { 4046a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4047a361293fSJan Kara struct jbd2_inode *jinode; 4048a361293fSJan Kara 4049a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4050a361293fSJan Kara return 0; 4051a361293fSJan Kara 4052a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4053a361293fSJan Kara spin_lock(&inode->i_lock); 4054a361293fSJan Kara if (!ei->jinode) { 4055a361293fSJan Kara if (!jinode) { 4056a361293fSJan Kara spin_unlock(&inode->i_lock); 4057a361293fSJan Kara return -ENOMEM; 4058a361293fSJan Kara } 4059a361293fSJan Kara ei->jinode = jinode; 4060a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4061a361293fSJan Kara jinode = NULL; 4062a361293fSJan Kara } 4063a361293fSJan Kara spin_unlock(&inode->i_lock); 4064a361293fSJan Kara if (unlikely(jinode != NULL)) 4065a361293fSJan Kara jbd2_free_inode(jinode); 4066a361293fSJan Kara return 0; 4067a361293fSJan Kara } 4068a361293fSJan Kara 4069a4bb6b64SAllison Henderson /* 4070617ba13bSMingming Cao * ext4_truncate() 4071ac27a0ecSDave Kleikamp * 4072617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4073617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4074ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4075ac27a0ecSDave Kleikamp * 407642b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4077ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4078ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4079ac27a0ecSDave Kleikamp * 4080ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4081ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4082ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4083ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4084ac27a0ecSDave Kleikamp * left-to-right works OK too). 4085ac27a0ecSDave Kleikamp * 4086ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4087ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4088ac27a0ecSDave Kleikamp * 4089ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4090617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4091ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4092617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4093617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4094ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4095617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4096ac27a0ecSDave Kleikamp */ 40972c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4098ac27a0ecSDave Kleikamp { 4099819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4100819c4920STheodore Ts'o unsigned int credits; 41012c98eb5eSTheodore Ts'o int err = 0; 4102819c4920STheodore Ts'o handle_t *handle; 4103819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4104819c4920STheodore Ts'o 410519b5ef61STheodore Ts'o /* 410619b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4107e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 410819b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 410919b5ef61STheodore Ts'o */ 411019b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41115955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41120562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41130562e0baSJiaying Zhang 411491ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41152c98eb5eSTheodore Ts'o return 0; 4116ac27a0ecSDave Kleikamp 411712e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 4118c8d46e41SJiaying Zhang 41195534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 412019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41217d8f9f7dSTheodore Ts'o 4122aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4123aef1c851STao Ma int has_inline = 1; 4124aef1c851STao Ma 412501daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 412601daf945STheodore Ts'o if (err) 412701daf945STheodore Ts'o return err; 4128aef1c851STao Ma if (has_inline) 41292c98eb5eSTheodore Ts'o return 0; 4130aef1c851STao Ma } 4131aef1c851STao Ma 4132a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4133a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4134a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 41352c98eb5eSTheodore Ts'o return 0; 4136a361293fSJan Kara } 4137a361293fSJan Kara 4138ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4139819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4140ff9893dcSAmir Goldstein else 4141819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4142819c4920STheodore Ts'o 4143819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 41442c98eb5eSTheodore Ts'o if (IS_ERR(handle)) 41452c98eb5eSTheodore Ts'o return PTR_ERR(handle); 4146819c4920STheodore Ts'o 4147eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4148eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4149819c4920STheodore Ts'o 4150819c4920STheodore Ts'o /* 4151819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4152819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4153819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4154819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4155819c4920STheodore Ts'o * 4156819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4157819c4920STheodore Ts'o * truncatable state while each transaction commits. 4158819c4920STheodore Ts'o */ 41592c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 41602c98eb5eSTheodore Ts'o if (err) 4161819c4920STheodore Ts'o goto out_stop; 4162819c4920STheodore Ts'o 4163819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4164819c4920STheodore Ts'o 4165819c4920STheodore Ts'o ext4_discard_preallocations(inode); 4166819c4920STheodore Ts'o 4167819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4168d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4169819c4920STheodore Ts'o else 4170819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4171819c4920STheodore Ts'o 4172819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4173d0abb36dSTheodore Ts'o if (err) 4174d0abb36dSTheodore Ts'o goto out_stop; 4175819c4920STheodore Ts'o 4176819c4920STheodore Ts'o if (IS_SYNC(inode)) 4177819c4920STheodore Ts'o ext4_handle_sync(handle); 4178819c4920STheodore Ts'o 4179819c4920STheodore Ts'o out_stop: 4180819c4920STheodore Ts'o /* 4181819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4182819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4183819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 418458d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4185819c4920STheodore Ts'o * orphan info for us. 4186819c4920STheodore Ts'o */ 4187819c4920STheodore Ts'o if (inode->i_nlink) 4188819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4189819c4920STheodore Ts'o 4190eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 4191819c4920STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 4192819c4920STheodore Ts'o ext4_journal_stop(handle); 4193a86c6181SAlex Tomas 41940562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 41952c98eb5eSTheodore Ts'o return err; 4196ac27a0ecSDave Kleikamp } 4197ac27a0ecSDave Kleikamp 4198ac27a0ecSDave Kleikamp /* 4199617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4200ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4201ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4202ac27a0ecSDave Kleikamp * inode. 4203ac27a0ecSDave Kleikamp */ 4204617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4205617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4206ac27a0ecSDave Kleikamp { 4207240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4208ac27a0ecSDave Kleikamp struct buffer_head *bh; 4209240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4210240799cdSTheodore Ts'o ext4_fsblk_t block; 421102f03c42SLinus Torvalds struct blk_plug plug; 4212240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4213ac27a0ecSDave Kleikamp 42143a06d778SAneesh Kumar K.V iloc->bh = NULL; 4215c37e9e01STheodore Ts'o if (inode->i_ino < EXT4_ROOT_INO || 4216c37e9e01STheodore Ts'o inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 42176a797d27SDarrick J. Wong return -EFSCORRUPTED; 4218ac27a0ecSDave Kleikamp 4219240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4220240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4221240799cdSTheodore Ts'o if (!gdp) 4222240799cdSTheodore Ts'o return -EIO; 4223240799cdSTheodore Ts'o 4224240799cdSTheodore Ts'o /* 4225240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4226240799cdSTheodore Ts'o */ 422700d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4228240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4229240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4230240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4231240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4232240799cdSTheodore Ts'o 4233240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4234aebf0243SWang Shilong if (unlikely(!bh)) 4235860d21e2STheodore Ts'o return -ENOMEM; 4236ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4237ac27a0ecSDave Kleikamp lock_buffer(bh); 42389c83a923SHidehiro Kawai 42399c83a923SHidehiro Kawai /* 42409c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 42419c83a923SHidehiro Kawai * to write out another inode in the same block. In this 42429c83a923SHidehiro Kawai * case, we don't have to read the block because we may 42439c83a923SHidehiro Kawai * read the old inode data successfully. 42449c83a923SHidehiro Kawai */ 42459c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 42469c83a923SHidehiro Kawai set_buffer_uptodate(bh); 42479c83a923SHidehiro Kawai 4248ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4249ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4250ac27a0ecSDave Kleikamp unlock_buffer(bh); 4251ac27a0ecSDave Kleikamp goto has_buffer; 4252ac27a0ecSDave Kleikamp } 4253ac27a0ecSDave Kleikamp 4254ac27a0ecSDave Kleikamp /* 4255ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4256ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4257ac27a0ecSDave Kleikamp * block. 4258ac27a0ecSDave Kleikamp */ 4259ac27a0ecSDave Kleikamp if (in_mem) { 4260ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4261240799cdSTheodore Ts'o int i, start; 4262ac27a0ecSDave Kleikamp 4263240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4264ac27a0ecSDave Kleikamp 4265ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4266240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4267aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4268ac27a0ecSDave Kleikamp goto make_io; 4269ac27a0ecSDave Kleikamp 4270ac27a0ecSDave Kleikamp /* 4271ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4272ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4273ac27a0ecSDave Kleikamp * of one, so skip it. 4274ac27a0ecSDave Kleikamp */ 4275ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4276ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4277ac27a0ecSDave Kleikamp goto make_io; 4278ac27a0ecSDave Kleikamp } 4279240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4280ac27a0ecSDave Kleikamp if (i == inode_offset) 4281ac27a0ecSDave Kleikamp continue; 4282617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4283ac27a0ecSDave Kleikamp break; 4284ac27a0ecSDave Kleikamp } 4285ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4286240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4287ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4288ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4289ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4290ac27a0ecSDave Kleikamp unlock_buffer(bh); 4291ac27a0ecSDave Kleikamp goto has_buffer; 4292ac27a0ecSDave Kleikamp } 4293ac27a0ecSDave Kleikamp } 4294ac27a0ecSDave Kleikamp 4295ac27a0ecSDave Kleikamp make_io: 4296ac27a0ecSDave Kleikamp /* 4297240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4298240799cdSTheodore Ts'o * blocks from the inode table. 4299240799cdSTheodore Ts'o */ 430002f03c42SLinus Torvalds blk_start_plug(&plug); 4301240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4302240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4303240799cdSTheodore Ts'o unsigned num; 43040d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4305240799cdSTheodore Ts'o 4306240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4307b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 43080d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4309240799cdSTheodore Ts'o if (table > b) 4310240799cdSTheodore Ts'o b = table; 43110d606e2cSTheodore Ts'o end = b + ra_blks; 4312240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4313feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4314560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4315240799cdSTheodore Ts'o table += num / inodes_per_block; 4316240799cdSTheodore Ts'o if (end > table) 4317240799cdSTheodore Ts'o end = table; 4318240799cdSTheodore Ts'o while (b <= end) 4319240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4320240799cdSTheodore Ts'o } 4321240799cdSTheodore Ts'o 4322240799cdSTheodore Ts'o /* 4323ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4324ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4325ac27a0ecSDave Kleikamp * Read the block from disk. 4326ac27a0ecSDave Kleikamp */ 43270562e0baSJiaying Zhang trace_ext4_load_inode(inode); 4328ac27a0ecSDave Kleikamp get_bh(bh); 4329ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 43302a222ca9SMike Christie submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); 433102f03c42SLinus Torvalds blk_finish_plug(&plug); 4332ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4333ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4334c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 4335c398eda0STheodore Ts'o "unable to read itable block"); 4336ac27a0ecSDave Kleikamp brelse(bh); 4337ac27a0ecSDave Kleikamp return -EIO; 4338ac27a0ecSDave Kleikamp } 4339ac27a0ecSDave Kleikamp } 4340ac27a0ecSDave Kleikamp has_buffer: 4341ac27a0ecSDave Kleikamp iloc->bh = bh; 4342ac27a0ecSDave Kleikamp return 0; 4343ac27a0ecSDave Kleikamp } 4344ac27a0ecSDave Kleikamp 4345617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4346ac27a0ecSDave Kleikamp { 4347ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4348617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 434919f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4350ac27a0ecSDave Kleikamp } 4351ac27a0ecSDave Kleikamp 43526642586bSRoss Zwisler static bool ext4_should_use_dax(struct inode *inode) 43536642586bSRoss Zwisler { 43546642586bSRoss Zwisler if (!test_opt(inode->i_sb, DAX)) 43556642586bSRoss Zwisler return false; 43566642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 43576642586bSRoss Zwisler return false; 43586642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 43596642586bSRoss Zwisler return false; 43606642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 43616642586bSRoss Zwisler return false; 4362592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 43636642586bSRoss Zwisler return false; 4364c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4365c93d8f88SEric Biggers return false; 43666642586bSRoss Zwisler return true; 43676642586bSRoss Zwisler } 43686642586bSRoss Zwisler 4369617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4370ac27a0ecSDave Kleikamp { 4371617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 437200a1a053STheodore Ts'o unsigned int new_fl = 0; 4373ac27a0ecSDave Kleikamp 4374617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 437500a1a053STheodore Ts'o new_fl |= S_SYNC; 4376617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 437700a1a053STheodore Ts'o new_fl |= S_APPEND; 4378617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 437900a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4380617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 438100a1a053STheodore Ts'o new_fl |= S_NOATIME; 4382617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 438300a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 43846642586bSRoss Zwisler if (ext4_should_use_dax(inode)) 4385923ae0ffSRoss Zwisler new_fl |= S_DAX; 43862ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 43872ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4388b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4389b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4390c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4391c93d8f88SEric Biggers new_fl |= S_VERITY; 43925f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 43932ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4394c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4395ac27a0ecSDave Kleikamp } 4396ac27a0ecSDave Kleikamp 43970fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 43980fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 43990fc1b451SAneesh Kumar K.V { 44000fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 44018180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 44028180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 44030fc1b451SAneesh Kumar K.V 4404e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 44050fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 44060fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 44070fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 440807a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 44098180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 44108180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 44118180a562SAneesh Kumar K.V } else { 44120fc1b451SAneesh Kumar K.V return i_blocks; 44138180a562SAneesh Kumar K.V } 44140fc1b451SAneesh Kumar K.V } else { 44150fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 44160fc1b451SAneesh Kumar K.V } 44170fc1b451SAneesh Kumar K.V } 4418ff9ddf7eSJan Kara 4419eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4420152a7b0aSTao Ma struct ext4_inode *raw_inode, 4421152a7b0aSTao Ma struct ext4_inode_info *ei) 4422152a7b0aSTao Ma { 4423152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4424152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4425eb9b5f01STheodore Ts'o 4426290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4427290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4428290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4429152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4430eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4431f19d5870STao Ma } else 4432f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4433eb9b5f01STheodore Ts'o return 0; 4434152a7b0aSTao Ma } 4435152a7b0aSTao Ma 4436040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4437040cb378SLi Xi { 44380b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4439040cb378SLi Xi return -EOPNOTSUPP; 4440040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4441040cb378SLi Xi return 0; 4442040cb378SLi Xi } 4443040cb378SLi Xi 4444e254d1afSEryu Guan /* 4445e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4446e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4447e254d1afSEryu Guan * set. 4448e254d1afSEryu Guan */ 4449e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4450e254d1afSEryu Guan { 4451e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4452e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4453e254d1afSEryu Guan else 4454e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4455e254d1afSEryu Guan } 4456e254d1afSEryu Guan static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4457e254d1afSEryu Guan { 4458e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4459e254d1afSEryu Guan return inode_peek_iversion_raw(inode); 4460e254d1afSEryu Guan else 4461e254d1afSEryu Guan return inode_peek_iversion(inode); 4462e254d1afSEryu Guan } 4463e254d1afSEryu Guan 44648a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 44658a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 44668a363970STheodore Ts'o unsigned int line) 4467ac27a0ecSDave Kleikamp { 4468617ba13bSMingming Cao struct ext4_iloc iloc; 4469617ba13bSMingming Cao struct ext4_inode *raw_inode; 44701d1fe1eeSDavid Howells struct ext4_inode_info *ei; 44711d1fe1eeSDavid Howells struct inode *inode; 4472b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 44731d1fe1eeSDavid Howells long ret; 44747e6e1ef4SDarrick J. Wong loff_t size; 4475ac27a0ecSDave Kleikamp int block; 447608cefc7aSEric W. Biederman uid_t i_uid; 447708cefc7aSEric W. Biederman gid_t i_gid; 4478040cb378SLi Xi projid_t i_projid; 4479ac27a0ecSDave Kleikamp 4480191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 44818a363970STheodore Ts'o (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 44828a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 44838a363970STheodore Ts'o (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 44848a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 44858a363970STheodore Ts'o return ERR_PTR(-ESTALE); 44868a363970STheodore Ts'o __ext4_error(sb, function, line, 44878a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 44888a363970STheodore Ts'o ino, current->comm); 44898a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 44908a363970STheodore Ts'o } 44918a363970STheodore Ts'o 44921d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 44931d1fe1eeSDavid Howells if (!inode) 44941d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 44951d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 44961d1fe1eeSDavid Howells return inode; 44971d1fe1eeSDavid Howells 44981d1fe1eeSDavid Howells ei = EXT4_I(inode); 44997dc57615SPeter Huewe iloc.bh = NULL; 4500ac27a0ecSDave Kleikamp 45011d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 45021d1fe1eeSDavid Howells if (ret < 0) 4503ac27a0ecSDave Kleikamp goto bad_inode; 4504617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4505814525f4SDarrick J. Wong 45068e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 45078a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45088a363970STheodore Ts'o "iget: root inode unallocated"); 45098e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 45108e4b5eaeSTheodore Ts'o goto bad_inode; 45118e4b5eaeSTheodore Ts'o } 45128e4b5eaeSTheodore Ts'o 45138a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 45148a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 45158a363970STheodore Ts'o ret = -ESTALE; 45168a363970STheodore Ts'o goto bad_inode; 45178a363970STheodore Ts'o } 45188a363970STheodore Ts'o 4519814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4520814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4521814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 45222dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 45232dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 45248a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45258a363970STheodore Ts'o "iget: bad extra_isize %u " 45268a363970STheodore Ts'o "(inode size %u)", 45272dc8d9e1SEric Biggers ei->i_extra_isize, 4528814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 45296a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4530814525f4SDarrick J. Wong goto bad_inode; 4531814525f4SDarrick J. Wong } 4532814525f4SDarrick J. Wong } else 4533814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4534814525f4SDarrick J. Wong 4535814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 45369aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4537814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4538814525f4SDarrick J. Wong __u32 csum; 4539814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4540814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4541814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4542814525f4SDarrick J. Wong sizeof(inum)); 4543814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4544814525f4SDarrick J. Wong sizeof(gen)); 4545814525f4SDarrick J. Wong } 4546814525f4SDarrick J. Wong 4547814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 45488a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45498a363970STheodore Ts'o "iget: checksum invalid"); 45506a797d27SDarrick J. Wong ret = -EFSBADCRC; 4551814525f4SDarrick J. Wong goto bad_inode; 4552814525f4SDarrick J. Wong } 4553814525f4SDarrick J. Wong 4554ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 455508cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 455608cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 45570b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4558040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4559040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4560040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4561040cb378SLi Xi else 4562040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4563040cb378SLi Xi 4564ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 456508cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 456608cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4567ac27a0ecSDave Kleikamp } 456808cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 456908cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4570040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4571bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4572ac27a0ecSDave Kleikamp 4573353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 457467cf5b09STao Ma ei->i_inline_off = 0; 4575ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4576ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4577ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4578ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4579ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4580ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4581ac27a0ecSDave Kleikamp */ 4582ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4583393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4584393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4585393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4586ac27a0ecSDave Kleikamp /* this inode is deleted */ 45871d1fe1eeSDavid Howells ret = -ESTALE; 4588ac27a0ecSDave Kleikamp goto bad_inode; 4589ac27a0ecSDave Kleikamp } 4590ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4591ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4592ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4593393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4594393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4595393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4596ac27a0ecSDave Kleikamp } 4597ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4598cce6c9f7SToshi Kani ext4_set_inode_flags(inode); 45990fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 46007973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4601e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4602a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4603a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4604e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 46057e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 46068a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46078a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 46087e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 46097e6e1ef4SDarrick J. Wong goto bad_inode; 46107e6e1ef4SDarrick J. Wong } 4611ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4612a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4613a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4614a9e7f447SDmitry Monakhov #endif 4615ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4616ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4617a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4618ac27a0ecSDave Kleikamp /* 4619ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4620ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4621ac27a0ecSDave Kleikamp */ 4622617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4623ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4624ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4625ac27a0ecSDave Kleikamp 4626b436b9beSJan Kara /* 4627b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4628b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4629b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4630b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4631b436b9beSJan Kara * now it is reread from disk. 4632b436b9beSJan Kara */ 4633b436b9beSJan Kara if (journal) { 4634b436b9beSJan Kara transaction_t *transaction; 4635b436b9beSJan Kara tid_t tid; 4636b436b9beSJan Kara 4637a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4638b436b9beSJan Kara if (journal->j_running_transaction) 4639b436b9beSJan Kara transaction = journal->j_running_transaction; 4640b436b9beSJan Kara else 4641b436b9beSJan Kara transaction = journal->j_committing_transaction; 4642b436b9beSJan Kara if (transaction) 4643b436b9beSJan Kara tid = transaction->t_tid; 4644b436b9beSJan Kara else 4645b436b9beSJan Kara tid = journal->j_commit_sequence; 4646a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4647b436b9beSJan Kara ei->i_sync_tid = tid; 4648b436b9beSJan Kara ei->i_datasync_tid = tid; 4649b436b9beSJan Kara } 4650b436b9beSJan Kara 46510040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4652ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4653ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 46542dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4655617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4656617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4657ac27a0ecSDave Kleikamp } else { 4658eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4659eb9b5f01STheodore Ts'o if (ret) 4660eb9b5f01STheodore Ts'o goto bad_inode; 4661ac27a0ecSDave Kleikamp } 4662814525f4SDarrick J. Wong } 4663ac27a0ecSDave Kleikamp 4664ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4665ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4666ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4667ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4668ef7f3835SKalpak Shah 4669ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4670ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4671ee73f9a5SJeff Layton 467225ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 467325ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4674ee73f9a5SJeff Layton ivers |= 467525ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 467625ec56b5SJean Noel Cordenner } 4677e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4678c4f65706STheodore Ts'o } 467925ec56b5SJean Noel Cordenner 4680c4b5a614STheodore Ts'o ret = 0; 4681485c26ecSTheodore Ts'o if (ei->i_file_acl && 46821032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 46838a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46848a363970STheodore Ts'o "iget: bad extended attribute block %llu", 468524676da4STheodore Ts'o ei->i_file_acl); 46866a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4687485c26ecSTheodore Ts'o goto bad_inode; 4688f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4689bc716523SLiu Song /* validate the block references in the inode */ 4690bc716523SLiu Song if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4691fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4692fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4693bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4694bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4695bc716523SLiu Song else 46961f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4697fe2c8191SThiemo Nagel } 4698f19d5870STao Ma } 4699567f3e9aSTheodore Ts'o if (ret) 47007a262f7cSAneesh Kumar K.V goto bad_inode; 47017a262f7cSAneesh Kumar K.V 4702ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4703617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4704617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4705617ba13bSMingming Cao ext4_set_aops(inode); 4706ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4707617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4708617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4709ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 47106390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 47116390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 47128a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47138a363970STheodore Ts'o "iget: immutable or append flags " 47148a363970STheodore Ts'o "not allowed on symlinks"); 47156390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 47166390d33bSLuis R. Rodriguez goto bad_inode; 47176390d33bSLuis R. Rodriguez } 4718592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4719a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4720a7a67e8aSAl Viro ext4_set_aops(inode); 4721a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 472275e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4723617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4724e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4725e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4726e83c1397SDuane Griffin } else { 4727617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4728617ba13bSMingming Cao ext4_set_aops(inode); 4729ac27a0ecSDave Kleikamp } 473021fc61c7SAl Viro inode_nohighmem(inode); 4731563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4732563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4733617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4734ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4735ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4736ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4737ac27a0ecSDave Kleikamp else 4738ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4739ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4740393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4741393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4742563bdd61STheodore Ts'o } else { 47436a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 47448a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47458a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4746563bdd61STheodore Ts'o goto bad_inode; 4747ac27a0ecSDave Kleikamp } 47486456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 47496456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47506456ca65STheodore Ts'o "casefold flag without casefold feature"); 4751ac27a0ecSDave Kleikamp brelse(iloc.bh); 4752dec214d0STahsin Erdogan 47531d1fe1eeSDavid Howells unlock_new_inode(inode); 47541d1fe1eeSDavid Howells return inode; 4755ac27a0ecSDave Kleikamp 4756ac27a0ecSDave Kleikamp bad_inode: 4757567f3e9aSTheodore Ts'o brelse(iloc.bh); 47581d1fe1eeSDavid Howells iget_failed(inode); 47591d1fe1eeSDavid Howells return ERR_PTR(ret); 4760ac27a0ecSDave Kleikamp } 4761ac27a0ecSDave Kleikamp 47620fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 47630fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 47640fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 47650fc1b451SAneesh Kumar K.V { 47660fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 47670fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 47680fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 47690fc1b451SAneesh Kumar K.V 47700fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 47710fc1b451SAneesh Kumar K.V /* 47724907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 47730fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 47740fc1b451SAneesh Kumar K.V */ 47758180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 47760fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 477784a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4778f287a1a5STheodore Ts'o return 0; 4779f287a1a5STheodore Ts'o } 4780e2b911c5SDarrick J. Wong if (!ext4_has_feature_huge_file(sb)) 4781f287a1a5STheodore Ts'o return -EFBIG; 4782f287a1a5STheodore Ts'o 4783f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 47840fc1b451SAneesh Kumar K.V /* 47850fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 47860fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 47870fc1b451SAneesh Kumar K.V */ 47888180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 47890fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 479084a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 47910fc1b451SAneesh Kumar K.V } else { 479284a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 47938180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 47948180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 47958180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 47968180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 47970fc1b451SAneesh Kumar K.V } 4798f287a1a5STheodore Ts'o return 0; 47990fc1b451SAneesh Kumar K.V } 48000fc1b451SAneesh Kumar K.V 4801a26f4992STheodore Ts'o struct other_inode { 4802a26f4992STheodore Ts'o unsigned long orig_ino; 4803a26f4992STheodore Ts'o struct ext4_inode *raw_inode; 4804a26f4992STheodore Ts'o }; 4805a26f4992STheodore Ts'o 4806a26f4992STheodore Ts'o static int other_inode_match(struct inode * inode, unsigned long ino, 4807a26f4992STheodore Ts'o void *data) 4808a26f4992STheodore Ts'o { 4809a26f4992STheodore Ts'o struct other_inode *oi = (struct other_inode *) data; 4810a26f4992STheodore Ts'o 4811a26f4992STheodore Ts'o if ((inode->i_ino != ino) || 4812a26f4992STheodore Ts'o (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 48130e11f644SChristoph Hellwig I_DIRTY_INODE)) || 4814a26f4992STheodore Ts'o ((inode->i_state & I_DIRTY_TIME) == 0)) 4815a26f4992STheodore Ts'o return 0; 4816a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4817a26f4992STheodore Ts'o if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 48180e11f644SChristoph Hellwig I_DIRTY_INODE)) == 0) && 4819a26f4992STheodore Ts'o (inode->i_state & I_DIRTY_TIME)) { 4820a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4821a26f4992STheodore Ts'o 4822a26f4992STheodore Ts'o inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED); 4823a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4824a26f4992STheodore Ts'o 4825a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 4826a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode); 4827a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode); 4828a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode); 4829a26f4992STheodore Ts'o ext4_inode_csum_set(inode, oi->raw_inode, ei); 4830a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 4831a26f4992STheodore Ts'o trace_ext4_other_inode_update_time(inode, oi->orig_ino); 4832a26f4992STheodore Ts'o return -1; 4833a26f4992STheodore Ts'o } 4834a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4835a26f4992STheodore Ts'o return -1; 4836a26f4992STheodore Ts'o } 4837a26f4992STheodore Ts'o 4838a26f4992STheodore Ts'o /* 4839a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 4840a26f4992STheodore Ts'o * the same inode table block. 4841a26f4992STheodore Ts'o */ 4842a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 4843a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 4844a26f4992STheodore Ts'o { 4845a26f4992STheodore Ts'o struct other_inode oi; 4846a26f4992STheodore Ts'o unsigned long ino; 4847a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4848a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 4849a26f4992STheodore Ts'o 4850a26f4992STheodore Ts'o oi.orig_ino = orig_ino; 48510f0ff9a9STheodore Ts'o /* 48520f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 48530f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 48540f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 48550f0ff9a9STheodore Ts'o */ 48560f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 4857a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 4858a26f4992STheodore Ts'o if (ino == orig_ino) 4859a26f4992STheodore Ts'o continue; 4860a26f4992STheodore Ts'o oi.raw_inode = (struct ext4_inode *) buf; 4861a26f4992STheodore Ts'o (void) find_inode_nowait(sb, ino, other_inode_match, &oi); 4862a26f4992STheodore Ts'o } 4863a26f4992STheodore Ts'o } 4864a26f4992STheodore Ts'o 4865ac27a0ecSDave Kleikamp /* 4866ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4867ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4868ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4869ac27a0ecSDave Kleikamp * 4870ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4871ac27a0ecSDave Kleikamp */ 4872617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4873ac27a0ecSDave Kleikamp struct inode *inode, 4874830156c7SFrank Mayhar struct ext4_iloc *iloc) 4875ac27a0ecSDave Kleikamp { 4876617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4877617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4878ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4879202ee5dfSTheodore Ts'o struct super_block *sb = inode->i_sb; 4880ac27a0ecSDave Kleikamp int err = 0, rc, block; 4881202ee5dfSTheodore Ts'o int need_datasync = 0, set_large_file = 0; 488208cefc7aSEric W. Biederman uid_t i_uid; 488308cefc7aSEric W. Biederman gid_t i_gid; 4884040cb378SLi Xi projid_t i_projid; 4885ac27a0ecSDave Kleikamp 4886202ee5dfSTheodore Ts'o spin_lock(&ei->i_raw_lock); 4887202ee5dfSTheodore Ts'o 4888202ee5dfSTheodore Ts'o /* For fields not tracked in the in-memory inode, 4889ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 489019f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4891617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4892ac27a0ecSDave Kleikamp 4893ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 489408cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 489508cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4896040cb378SLi Xi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 4897ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 489808cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 489908cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4900ac27a0ecSDave Kleikamp /* 4901ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4902ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4903ac27a0ecSDave Kleikamp */ 490493e3b4e6SDaeho Jeong if (ei->i_dtime && list_empty(&ei->i_orphan)) { 490593e3b4e6SDaeho Jeong raw_inode->i_uid_high = 0; 490693e3b4e6SDaeho Jeong raw_inode->i_gid_high = 0; 490793e3b4e6SDaeho Jeong } else { 4908ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 490908cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4910ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 491108cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4912ac27a0ecSDave Kleikamp } 4913ac27a0ecSDave Kleikamp } else { 491408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 491508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4916ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4917ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4918ac27a0ecSDave Kleikamp } 4919ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4920ef7f3835SKalpak Shah 4921ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4922ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4923ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4924ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4925ef7f3835SKalpak Shah 4926bce92d56SLi Xi err = ext4_inode_blocks_set(handle, raw_inode, ei); 4927bce92d56SLi Xi if (err) { 4928202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 49290fc1b451SAneesh Kumar K.V goto out_brelse; 4930202ee5dfSTheodore Ts'o } 4931ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4932353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 4933ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 4934a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4935a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 49367973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4937e08ac99fSArtem Blagodarenko if (ei->i_disksize != ext4_isize(inode->i_sb, raw_inode)) { 4938a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4939b71fc079SJan Kara need_datasync = 1; 4940b71fc079SJan Kara } 4941ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4942e2b911c5SDarrick J. Wong if (!ext4_has_feature_large_file(sb) || 4943617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4944202ee5dfSTheodore Ts'o cpu_to_le32(EXT4_GOOD_OLD_REV)) 4945202ee5dfSTheodore Ts'o set_large_file = 1; 4946ac27a0ecSDave Kleikamp } 4947ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4948ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4949ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4950ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4951ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4952ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4953ac27a0ecSDave Kleikamp } else { 4954ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4955ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4956ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4957ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4958ac27a0ecSDave Kleikamp } 4959f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4960de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4961ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4962f19d5870STao Ma } 4963ac27a0ecSDave Kleikamp 4964ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4965e254d1afSEryu Guan u64 ivers = ext4_inode_peek_iversion(inode); 4966ee73f9a5SJeff Layton 4967ee73f9a5SJeff Layton raw_inode->i_disk_version = cpu_to_le32(ivers); 496825ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 496925ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 497025ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 4971ee73f9a5SJeff Layton cpu_to_le32(ivers >> 32); 4972c4f65706STheodore Ts'o raw_inode->i_extra_isize = 4973c4f65706STheodore Ts'o cpu_to_le16(ei->i_extra_isize); 4974c4f65706STheodore Ts'o } 497525ec56b5SJean Noel Cordenner } 4976040cb378SLi Xi 49770b7b7779SKaho Ng BUG_ON(!ext4_has_feature_project(inode->i_sb) && 4978040cb378SLi Xi i_projid != EXT4_DEF_PROJID); 4979040cb378SLi Xi 4980040cb378SLi Xi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 4981040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4982040cb378SLi Xi raw_inode->i_projid = cpu_to_le32(i_projid); 4983040cb378SLi Xi 4984814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4985202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 49861751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 4987a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 4988a26f4992STheodore Ts'o bh->b_data); 4989202ee5dfSTheodore Ts'o 49900390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 499173b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4992ac27a0ecSDave Kleikamp if (!err) 4993ac27a0ecSDave Kleikamp err = rc; 499419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4995202ee5dfSTheodore Ts'o if (set_large_file) { 49965d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 4997202ee5dfSTheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 4998202ee5dfSTheodore Ts'o if (err) 4999202ee5dfSTheodore Ts'o goto out_brelse; 5000e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 5001202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5002202ee5dfSTheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 5003202ee5dfSTheodore Ts'o } 5004b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5005ac27a0ecSDave Kleikamp out_brelse: 5006ac27a0ecSDave Kleikamp brelse(bh); 5007617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5008ac27a0ecSDave Kleikamp return err; 5009ac27a0ecSDave Kleikamp } 5010ac27a0ecSDave Kleikamp 5011ac27a0ecSDave Kleikamp /* 5012617ba13bSMingming Cao * ext4_write_inode() 5013ac27a0ecSDave Kleikamp * 5014ac27a0ecSDave Kleikamp * We are called from a few places: 5015ac27a0ecSDave Kleikamp * 501687f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5017ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 50184907cb7bSAnatol Pomozov * transaction to commit. 5019ac27a0ecSDave Kleikamp * 502087f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 502187f7e416STheodore Ts'o * We wait on commit, if told to. 5022ac27a0ecSDave Kleikamp * 502387f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 502487f7e416STheodore Ts'o * We wait on commit, if told to. 5025ac27a0ecSDave Kleikamp * 5026ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5027ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 502887f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 502987f7e416STheodore Ts'o * writeback. 5030ac27a0ecSDave Kleikamp * 5031ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5032ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5033ac27a0ecSDave Kleikamp * which we are interested. 5034ac27a0ecSDave Kleikamp * 5035ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5036ac27a0ecSDave Kleikamp * 5037ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5038ac27a0ecSDave Kleikamp * stuff(); 5039ac27a0ecSDave Kleikamp * inode->i_size = expr; 5040ac27a0ecSDave Kleikamp * 504187f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 504287f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 504387f7e416STheodore Ts'o * superblock's dirty inode list. 5044ac27a0ecSDave Kleikamp */ 5045a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5046ac27a0ecSDave Kleikamp { 504791ac6f43SFrank Mayhar int err; 504891ac6f43SFrank Mayhar 504918f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 505018f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5051ac27a0ecSDave Kleikamp return 0; 5052ac27a0ecSDave Kleikamp 505318f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 505418f2c4fcSTheodore Ts'o return -EIO; 505518f2c4fcSTheodore Ts'o 505691ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5057617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5058b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5059ac27a0ecSDave Kleikamp dump_stack(); 5060ac27a0ecSDave Kleikamp return -EIO; 5061ac27a0ecSDave Kleikamp } 5062ac27a0ecSDave Kleikamp 506310542c22SJan Kara /* 506410542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 506510542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 506610542c22SJan Kara * written. 506710542c22SJan Kara */ 506810542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5069ac27a0ecSDave Kleikamp return 0; 5070ac27a0ecSDave Kleikamp 507118f2c4fcSTheodore Ts'o err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, 507218f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 507391ac6f43SFrank Mayhar } else { 507491ac6f43SFrank Mayhar struct ext4_iloc iloc; 507591ac6f43SFrank Mayhar 50768b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 507791ac6f43SFrank Mayhar if (err) 507891ac6f43SFrank Mayhar return err; 507910542c22SJan Kara /* 508010542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 508110542c22SJan Kara * it here separately for each inode. 508210542c22SJan Kara */ 508310542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5084830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5085830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 5086c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 5087c398eda0STheodore Ts'o "IO error syncing inode"); 5088830156c7SFrank Mayhar err = -EIO; 5089830156c7SFrank Mayhar } 5090fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 509191ac6f43SFrank Mayhar } 509291ac6f43SFrank Mayhar return err; 5093ac27a0ecSDave Kleikamp } 5094ac27a0ecSDave Kleikamp 5095ac27a0ecSDave Kleikamp /* 509653e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 509753e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 509853e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 509953e87268SJan Kara */ 510053e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 510153e87268SJan Kara { 510253e87268SJan Kara struct page *page; 510353e87268SJan Kara unsigned offset; 510453e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 510553e87268SJan Kara tid_t commit_tid = 0; 510653e87268SJan Kara int ret; 510753e87268SJan Kara 510809cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 510953e87268SJan Kara /* 511053e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 5111ea1754a0SKirill A. Shutemov * do. We do the check mainly to optimize the common PAGE_SIZE == 511253e87268SJan Kara * blocksize case 511353e87268SJan Kara */ 511493407472SFabian Frederick if (offset > PAGE_SIZE - i_blocksize(inode)) 511553e87268SJan Kara return; 511653e87268SJan Kara while (1) { 511753e87268SJan Kara page = find_lock_page(inode->i_mapping, 511809cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 511953e87268SJan Kara if (!page) 512053e87268SJan Kara return; 5121ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 512209cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 512353e87268SJan Kara unlock_page(page); 512409cbfeafSKirill A. Shutemov put_page(page); 512553e87268SJan Kara if (ret != -EBUSY) 512653e87268SJan Kara return; 512753e87268SJan Kara commit_tid = 0; 512853e87268SJan Kara read_lock(&journal->j_state_lock); 512953e87268SJan Kara if (journal->j_committing_transaction) 513053e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 513153e87268SJan Kara read_unlock(&journal->j_state_lock); 513253e87268SJan Kara if (commit_tid) 513353e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 513453e87268SJan Kara } 513553e87268SJan Kara } 513653e87268SJan Kara 513753e87268SJan Kara /* 5138617ba13bSMingming Cao * ext4_setattr() 5139ac27a0ecSDave Kleikamp * 5140ac27a0ecSDave Kleikamp * Called from notify_change. 5141ac27a0ecSDave Kleikamp * 5142ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5143ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5144ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5145ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5146ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5147ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5148ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5149ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5150ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5151ac27a0ecSDave Kleikamp * 5152678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5153678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5154678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5155678aaf48SJan Kara * This way we are sure that all the data written in the previous 5156678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5157678aaf48SJan Kara * writeback). 5158678aaf48SJan Kara * 5159678aaf48SJan Kara * Called with inode->i_mutex down. 5160ac27a0ecSDave Kleikamp */ 5161617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5162ac27a0ecSDave Kleikamp { 51632b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5164ac27a0ecSDave Kleikamp int error, rc = 0; 51653d287de3SDmitry Monakhov int orphan = 0; 5166ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5167ac27a0ecSDave Kleikamp 51680db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 51690db1ff22STheodore Ts'o return -EIO; 51700db1ff22STheodore Ts'o 517102b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 517202b016caSTheodore Ts'o return -EPERM; 517302b016caSTheodore Ts'o 517402b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 517502b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 517602b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 517702b016caSTheodore Ts'o return -EPERM; 517802b016caSTheodore Ts'o 517931051c85SJan Kara error = setattr_prepare(dentry, attr); 5180ac27a0ecSDave Kleikamp if (error) 5181ac27a0ecSDave Kleikamp return error; 5182ac27a0ecSDave Kleikamp 51833ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 51843ce2b8ddSEric Biggers if (error) 51853ce2b8ddSEric Biggers return error; 51863ce2b8ddSEric Biggers 5187c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5188c93d8f88SEric Biggers if (error) 5189c93d8f88SEric Biggers return error; 5190c93d8f88SEric Biggers 5191a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5192a7cdadeeSJan Kara error = dquot_initialize(inode); 5193a7cdadeeSJan Kara if (error) 5194a7cdadeeSJan Kara return error; 5195a7cdadeeSJan Kara } 519608cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 519708cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5198ac27a0ecSDave Kleikamp handle_t *handle; 5199ac27a0ecSDave Kleikamp 5200ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5201ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 52029924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 52039924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5204194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5205ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5206ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5207ac27a0ecSDave Kleikamp goto err_out; 5208ac27a0ecSDave Kleikamp } 52097a9ca53aSTahsin Erdogan 52107a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 52117a9ca53aSTahsin Erdogan * counts xattr inode references. 52127a9ca53aSTahsin Erdogan */ 52137a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5214b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 52157a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 52167a9ca53aSTahsin Erdogan 5217ac27a0ecSDave Kleikamp if (error) { 5218617ba13bSMingming Cao ext4_journal_stop(handle); 5219ac27a0ecSDave Kleikamp return error; 5220ac27a0ecSDave Kleikamp } 5221ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5222ac27a0ecSDave Kleikamp * one transaction */ 5223ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5224ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5225ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5226ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5227617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5228617ba13bSMingming Cao ext4_journal_stop(handle); 5229ac27a0ecSDave Kleikamp } 5230ac27a0ecSDave Kleikamp 52313da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 52325208386cSJan Kara handle_t *handle; 52333da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5234b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5235562c72aaSChristoph Hellwig 523612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5237e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5238e2b46574SEric Sandeen 52390c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 52400c095c7fSTheodore Ts'o return -EFBIG; 5241e2b46574SEric Sandeen } 52423da40c7bSJosef Bacik if (!S_ISREG(inode->i_mode)) 52433da40c7bSJosef Bacik return -EINVAL; 5244dff6efc3SChristoph Hellwig 5245dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5246dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5247dff6efc3SChristoph Hellwig 5248b9c1c267SJan Kara if (shrink) { 5249b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 52505208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 52515208386cSJan Kara attr->ia_size); 52525208386cSJan Kara if (error) 52535208386cSJan Kara goto err_out; 52545208386cSJan Kara } 5255b9c1c267SJan Kara /* 5256b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5257b9c1c267SJan Kara * for dio in flight. 5258b9c1c267SJan Kara */ 5259b9c1c267SJan Kara inode_dio_wait(inode); 5260b9c1c267SJan Kara } 5261b9c1c267SJan Kara 5262b9c1c267SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5263b9c1c267SJan Kara 5264b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5265b9c1c267SJan Kara if (rc) { 5266b9c1c267SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 5267b9c1c267SJan Kara return rc; 5268b9c1c267SJan Kara } 5269b9c1c267SJan Kara 52703da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 52719924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5272ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5273ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5274b9c1c267SJan Kara goto out_mmap_sem; 5275ac27a0ecSDave Kleikamp } 52763da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5277617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 52783d287de3SDmitry Monakhov orphan = 1; 52793d287de3SDmitry Monakhov } 5280911af577SEryu Guan /* 5281911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5282911af577SEryu Guan * update c/mtime in shrink case below 5283911af577SEryu Guan */ 5284911af577SEryu Guan if (!shrink) { 5285eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5286911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5287911af577SEryu Guan } 528890e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5289617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5290617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5291ac27a0ecSDave Kleikamp if (!error) 5292ac27a0ecSDave Kleikamp error = rc; 529390e775b7SJan Kara /* 529490e775b7SJan Kara * We have to update i_size under i_data_sem together 529590e775b7SJan Kara * with i_disksize to avoid races with writeback code 529690e775b7SJan Kara * running ext4_wb_update_i_disksize(). 529790e775b7SJan Kara */ 529890e775b7SJan Kara if (!error) 529990e775b7SJan Kara i_size_write(inode, attr->ia_size); 530090e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5301617ba13bSMingming Cao ext4_journal_stop(handle); 5302b9c1c267SJan Kara if (error) 5303b9c1c267SJan Kara goto out_mmap_sem; 530482a25b02SJan Kara if (!shrink) { 5305b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5306b9c1c267SJan Kara inode->i_size); 5307b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 530882a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5309b9c1c267SJan Kara } 5310430657b6SRoss Zwisler } 5311430657b6SRoss Zwisler 531253e87268SJan Kara /* 531353e87268SJan Kara * Truncate pagecache after we've waited for commit 531453e87268SJan Kara * in data=journal mode to make pages freeable. 531553e87268SJan Kara */ 53167caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5317b9c1c267SJan Kara /* 5318b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5319b9c1c267SJan Kara * truncate possible preallocated blocks. 5320b9c1c267SJan Kara */ 5321b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 53222c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 53232c98eb5eSTheodore Ts'o if (rc) 53242c98eb5eSTheodore Ts'o error = rc; 53252c98eb5eSTheodore Ts'o } 5326b9c1c267SJan Kara out_mmap_sem: 5327ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 53283da40c7bSJosef Bacik } 5329ac27a0ecSDave Kleikamp 53302c98eb5eSTheodore Ts'o if (!error) { 53311025774cSChristoph Hellwig setattr_copy(inode, attr); 53321025774cSChristoph Hellwig mark_inode_dirty(inode); 53331025774cSChristoph Hellwig } 53341025774cSChristoph Hellwig 53351025774cSChristoph Hellwig /* 53361025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 53371025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 53381025774cSChristoph Hellwig */ 53393d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5340617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5341ac27a0ecSDave Kleikamp 53422c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 534364e178a7SChristoph Hellwig rc = posix_acl_chmod(inode, inode->i_mode); 5344ac27a0ecSDave Kleikamp 5345ac27a0ecSDave Kleikamp err_out: 5346617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5347ac27a0ecSDave Kleikamp if (!error) 5348ac27a0ecSDave Kleikamp error = rc; 5349ac27a0ecSDave Kleikamp return error; 5350ac27a0ecSDave Kleikamp } 5351ac27a0ecSDave Kleikamp 5352a528d35eSDavid Howells int ext4_getattr(const struct path *path, struct kstat *stat, 5353a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 53543e3398a0SMingming Cao { 535599652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 535699652ea5SDavid Howells struct ext4_inode *raw_inode; 535799652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 535899652ea5SDavid Howells unsigned int flags; 53593e3398a0SMingming Cao 536099652ea5SDavid Howells if (EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 536199652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 536299652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 536399652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 536499652ea5SDavid Howells } 536599652ea5SDavid Howells 536699652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 536799652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 536899652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 536999652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 537099652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 537199652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 537299652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 537399652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 537499652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 537599652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 537699652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 537799652ea5SDavid Howells 53783209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 53793209f68bSDavid Howells STATX_ATTR_COMPRESSED | 53803209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 53813209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 53823209f68bSDavid Howells STATX_ATTR_NODUMP); 53833209f68bSDavid Howells 53843e3398a0SMingming Cao generic_fillattr(inode, stat); 538599652ea5SDavid Howells return 0; 538699652ea5SDavid Howells } 538799652ea5SDavid Howells 538899652ea5SDavid Howells int ext4_file_getattr(const struct path *path, struct kstat *stat, 538999652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 539099652ea5SDavid Howells { 539199652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 539299652ea5SDavid Howells u64 delalloc_blocks; 539399652ea5SDavid Howells 539499652ea5SDavid Howells ext4_getattr(path, stat, request_mask, query_flags); 53953e3398a0SMingming Cao 53963e3398a0SMingming Cao /* 53979206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 53989206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 53999206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5400d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 54019206c561SAndreas Dilger */ 54029206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 54039206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 54049206c561SAndreas Dilger 54059206c561SAndreas Dilger /* 54063e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 54073e3398a0SMingming Cao * otherwise in the case of system crash before the real block 54083e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 54093e3398a0SMingming Cao * on-disk file blocks. 54103e3398a0SMingming Cao * We always keep i_blocks updated together with real 54113e3398a0SMingming Cao * allocation. But to not confuse with user, stat 54123e3398a0SMingming Cao * will return the blocks that include the delayed allocation 54133e3398a0SMingming Cao * blocks for this file. 54143e3398a0SMingming Cao */ 541596607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 541696607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 54178af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 54183e3398a0SMingming Cao return 0; 54193e3398a0SMingming Cao } 5420ac27a0ecSDave Kleikamp 5421fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5422fffb2739SJan Kara int pextents) 5423a02908f1SMingming Cao { 542412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5425fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5426fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5427a02908f1SMingming Cao } 5428ac51d837STheodore Ts'o 5429a02908f1SMingming Cao /* 5430a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5431a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5432a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5433a02908f1SMingming Cao * 5434a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 54354907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5436a02908f1SMingming Cao * they could still across block group boundary. 5437a02908f1SMingming Cao * 5438a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5439a02908f1SMingming Cao */ 5440dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5441fffb2739SJan Kara int pextents) 5442a02908f1SMingming Cao { 54438df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 54448df9675fSTheodore Ts'o int gdpblocks; 5445a02908f1SMingming Cao int idxblocks; 5446a02908f1SMingming Cao int ret = 0; 5447a02908f1SMingming Cao 5448a02908f1SMingming Cao /* 5449fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5450fffb2739SJan Kara * to @pextents physical extents? 5451a02908f1SMingming Cao */ 5452fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5453a02908f1SMingming Cao 5454a02908f1SMingming Cao ret = idxblocks; 5455a02908f1SMingming Cao 5456a02908f1SMingming Cao /* 5457a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5458a02908f1SMingming Cao * to account 5459a02908f1SMingming Cao */ 5460fffb2739SJan Kara groups = idxblocks + pextents; 5461a02908f1SMingming Cao gdpblocks = groups; 54628df9675fSTheodore Ts'o if (groups > ngroups) 54638df9675fSTheodore Ts'o groups = ngroups; 5464a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5465a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5466a02908f1SMingming Cao 5467a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5468a02908f1SMingming Cao ret += groups + gdpblocks; 5469a02908f1SMingming Cao 5470a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5471a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5472ac27a0ecSDave Kleikamp 5473ac27a0ecSDave Kleikamp return ret; 5474ac27a0ecSDave Kleikamp } 5475ac27a0ecSDave Kleikamp 5476ac27a0ecSDave Kleikamp /* 547725985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5478f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5479f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5480a02908f1SMingming Cao * 5481525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5482a02908f1SMingming Cao * 5483525f4ed8SMingming Cao * We need to consider the worse case, when 5484a02908f1SMingming Cao * one new block per extent. 5485a02908f1SMingming Cao */ 5486a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5487a02908f1SMingming Cao { 5488a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5489a02908f1SMingming Cao int ret; 5490a02908f1SMingming Cao 5491fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5492a02908f1SMingming Cao 5493a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5494a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5495a02908f1SMingming Cao ret += bpp; 5496a02908f1SMingming Cao return ret; 5497a02908f1SMingming Cao } 5498f3bd1f3fSMingming Cao 5499f3bd1f3fSMingming Cao /* 5500f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5501f3bd1f3fSMingming Cao * 5502f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 550379e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5504f3bd1f3fSMingming Cao * 5505f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5506f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5507f3bd1f3fSMingming Cao */ 5508f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5509f3bd1f3fSMingming Cao { 5510f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5511f3bd1f3fSMingming Cao } 5512f3bd1f3fSMingming Cao 5513a02908f1SMingming Cao /* 5514617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5515ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5516ac27a0ecSDave Kleikamp */ 5517617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5518617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5519ac27a0ecSDave Kleikamp { 5520ac27a0ecSDave Kleikamp int err = 0; 5521ac27a0ecSDave Kleikamp 5522a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5523a6758309SVasily Averin put_bh(iloc->bh); 55240db1ff22STheodore Ts'o return -EIO; 5525a6758309SVasily Averin } 5526c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 552725ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 552825ec56b5SJean Noel Cordenner 5529ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5530ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5531ac27a0ecSDave Kleikamp 5532dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5533830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5534ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5535ac27a0ecSDave Kleikamp return err; 5536ac27a0ecSDave Kleikamp } 5537ac27a0ecSDave Kleikamp 5538ac27a0ecSDave Kleikamp /* 5539ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5540ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5541ac27a0ecSDave Kleikamp */ 5542ac27a0ecSDave Kleikamp 5543ac27a0ecSDave Kleikamp int 5544617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5545617ba13bSMingming Cao struct ext4_iloc *iloc) 5546ac27a0ecSDave Kleikamp { 55470390131bSFrank Mayhar int err; 55480390131bSFrank Mayhar 55490db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 55500db1ff22STheodore Ts'o return -EIO; 55510db1ff22STheodore Ts'o 5552617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5553ac27a0ecSDave Kleikamp if (!err) { 5554ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5555617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5556ac27a0ecSDave Kleikamp if (err) { 5557ac27a0ecSDave Kleikamp brelse(iloc->bh); 5558ac27a0ecSDave Kleikamp iloc->bh = NULL; 5559ac27a0ecSDave Kleikamp } 5560ac27a0ecSDave Kleikamp } 5561617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5562ac27a0ecSDave Kleikamp return err; 5563ac27a0ecSDave Kleikamp } 5564ac27a0ecSDave Kleikamp 5565c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5566c03b45b8SMiao Xie unsigned int new_extra_isize, 5567c03b45b8SMiao Xie struct ext4_iloc *iloc, 5568c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5569c03b45b8SMiao Xie { 5570c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5571c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 5572*4ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 5573*4ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5574c03b45b8SMiao Xie int error; 5575c03b45b8SMiao Xie 5576*4ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 5577*4ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 5578*4ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 5579*4ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 5580*4ea99936STheodore Ts'o ei->i_extra_isize, 5581*4ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 5582*4ea99936STheodore Ts'o return -EFSCORRUPTED; 5583*4ea99936STheodore Ts'o } 5584*4ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 5585*4ea99936STheodore Ts'o (new_extra_isize < 4) || 5586*4ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 5587*4ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 5588*4ea99936STheodore Ts'o 5589c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5590c03b45b8SMiao Xie 5591c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5592c03b45b8SMiao Xie 5593c03b45b8SMiao Xie /* No extended attributes present */ 5594c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5595c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5596c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5597c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5598c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5599c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5600c03b45b8SMiao Xie return 0; 5601c03b45b8SMiao Xie } 5602c03b45b8SMiao Xie 5603c03b45b8SMiao Xie /* try to expand with EAs present */ 5604c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5605c03b45b8SMiao Xie raw_inode, handle); 5606c03b45b8SMiao Xie if (error) { 5607c03b45b8SMiao Xie /* 5608c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5609c03b45b8SMiao Xie */ 5610c03b45b8SMiao Xie *no_expand = 1; 5611c03b45b8SMiao Xie } 5612c03b45b8SMiao Xie 5613c03b45b8SMiao Xie return error; 5614c03b45b8SMiao Xie } 5615c03b45b8SMiao Xie 5616ac27a0ecSDave Kleikamp /* 56176dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 56186dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 56196dd4ee7cSKalpak Shah */ 5620cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 56211d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 56221d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 56231d03ec98SAneesh Kumar K.V handle_t *handle) 56246dd4ee7cSKalpak Shah { 56253b10fdc6SMiao Xie int no_expand; 56263b10fdc6SMiao Xie int error; 56276dd4ee7cSKalpak Shah 5628cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5629cf0a5e81SMiao Xie return -EOVERFLOW; 5630cf0a5e81SMiao Xie 5631cf0a5e81SMiao Xie /* 5632cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5633cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5634cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5635cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5636cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5637cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5638cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5639cf0a5e81SMiao Xie */ 56406cb367c2SJan Kara if (ext4_journal_extend(handle, 564183448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5642cf0a5e81SMiao Xie return -ENOSPC; 56436dd4ee7cSKalpak Shah 56443b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5645cf0a5e81SMiao Xie return -EBUSY; 56463b10fdc6SMiao Xie 5647c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5648c03b45b8SMiao Xie handle, &no_expand); 56493b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5650c03b45b8SMiao Xie 5651c03b45b8SMiao Xie return error; 56526dd4ee7cSKalpak Shah } 56536dd4ee7cSKalpak Shah 5654c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5655c03b45b8SMiao Xie unsigned int new_extra_isize, 5656c03b45b8SMiao Xie struct ext4_iloc *iloc) 5657c03b45b8SMiao Xie { 5658c03b45b8SMiao Xie handle_t *handle; 5659c03b45b8SMiao Xie int no_expand; 5660c03b45b8SMiao Xie int error, rc; 5661c03b45b8SMiao Xie 5662c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5663c03b45b8SMiao Xie brelse(iloc->bh); 5664c03b45b8SMiao Xie return -EOVERFLOW; 5665c03b45b8SMiao Xie } 5666c03b45b8SMiao Xie 5667c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5668c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5669c03b45b8SMiao Xie if (IS_ERR(handle)) { 5670c03b45b8SMiao Xie error = PTR_ERR(handle); 5671c03b45b8SMiao Xie brelse(iloc->bh); 5672c03b45b8SMiao Xie return error; 5673c03b45b8SMiao Xie } 5674c03b45b8SMiao Xie 5675c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5676c03b45b8SMiao Xie 5677ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5678c03b45b8SMiao Xie error = ext4_journal_get_write_access(handle, iloc->bh); 56793b10fdc6SMiao Xie if (error) { 5680c03b45b8SMiao Xie brelse(iloc->bh); 5681c03b45b8SMiao Xie goto out_stop; 56823b10fdc6SMiao Xie } 5683cf0a5e81SMiao Xie 5684c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5685c03b45b8SMiao Xie handle, &no_expand); 5686c03b45b8SMiao Xie 5687c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5688c03b45b8SMiao Xie if (!error) 5689c03b45b8SMiao Xie error = rc; 5690c03b45b8SMiao Xie 5691c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5692c03b45b8SMiao Xie out_stop: 5693c03b45b8SMiao Xie ext4_journal_stop(handle); 56943b10fdc6SMiao Xie return error; 56956dd4ee7cSKalpak Shah } 56966dd4ee7cSKalpak Shah 56976dd4ee7cSKalpak Shah /* 5698ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5699ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5700ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5701ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5702ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5703ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5704ac27a0ecSDave Kleikamp * 5705ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5706ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5707ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5708ac27a0ecSDave Kleikamp * we start and wait on commits. 5709ac27a0ecSDave Kleikamp */ 5710617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5711ac27a0ecSDave Kleikamp { 5712617ba13bSMingming Cao struct ext4_iloc iloc; 57136dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5714cf0a5e81SMiao Xie int err; 5715ac27a0ecSDave Kleikamp 5716ac27a0ecSDave Kleikamp might_sleep(); 57177ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5718617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 57195e1021f2SEryu Guan if (err) 57205e1021f2SEryu Guan return err; 5721cf0a5e81SMiao Xie 5722cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5723cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 57246dd4ee7cSKalpak Shah iloc, handle); 5725cf0a5e81SMiao Xie 57265e1021f2SEryu Guan return ext4_mark_iloc_dirty(handle, inode, &iloc); 5727ac27a0ecSDave Kleikamp } 5728ac27a0ecSDave Kleikamp 5729ac27a0ecSDave Kleikamp /* 5730617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5731ac27a0ecSDave Kleikamp * 5732ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5733ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5734ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5735ac27a0ecSDave Kleikamp * 57365dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5737ac27a0ecSDave Kleikamp * are allocated to the file. 5738ac27a0ecSDave Kleikamp * 5739ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5740ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5741ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 57420ae45f63STheodore Ts'o * 57430ae45f63STheodore Ts'o * If only the I_DIRTY_TIME flag is set, we can skip everything. If 57440ae45f63STheodore Ts'o * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need 57450ae45f63STheodore Ts'o * to copy into the on-disk inode structure are the timestamp files. 5746ac27a0ecSDave Kleikamp */ 5747aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5748ac27a0ecSDave Kleikamp { 5749ac27a0ecSDave Kleikamp handle_t *handle; 5750ac27a0ecSDave Kleikamp 57510ae45f63STheodore Ts'o if (flags == I_DIRTY_TIME) 57520ae45f63STheodore Ts'o return; 57539924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5754ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5755ac27a0ecSDave Kleikamp goto out; 5756f3dc272fSCurt Wohlgemuth 5757617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5758f3dc272fSCurt Wohlgemuth 5759617ba13bSMingming Cao ext4_journal_stop(handle); 5760ac27a0ecSDave Kleikamp out: 5761ac27a0ecSDave Kleikamp return; 5762ac27a0ecSDave Kleikamp } 5763ac27a0ecSDave Kleikamp 5764617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5765ac27a0ecSDave Kleikamp { 5766ac27a0ecSDave Kleikamp journal_t *journal; 5767ac27a0ecSDave Kleikamp handle_t *handle; 5768ac27a0ecSDave Kleikamp int err; 5769c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5770ac27a0ecSDave Kleikamp 5771ac27a0ecSDave Kleikamp /* 5772ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5773ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5774ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5775ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5776ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5777ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5778ac27a0ecSDave Kleikamp * nobody is changing anything. 5779ac27a0ecSDave Kleikamp */ 5780ac27a0ecSDave Kleikamp 5781617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 57820390131bSFrank Mayhar if (!journal) 57830390131bSFrank Mayhar return 0; 5784d699594dSDave Hansen if (is_journal_aborted(journal)) 5785ac27a0ecSDave Kleikamp return -EROFS; 5786ac27a0ecSDave Kleikamp 578717335dccSDmitry Monakhov /* Wait for all existing dio workers */ 578817335dccSDmitry Monakhov inode_dio_wait(inode); 578917335dccSDmitry Monakhov 57904c546592SDaeho Jeong /* 57914c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 57924c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 57934c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 57944c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 57954c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 57964c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 57974c546592SDaeho Jeong */ 57984c546592SDaeho Jeong if (val) { 57994c546592SDaeho Jeong down_write(&EXT4_I(inode)->i_mmap_sem); 58004c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 58014c546592SDaeho Jeong if (err < 0) { 58024c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 58034c546592SDaeho Jeong return err; 58044c546592SDaeho Jeong } 58054c546592SDaeho Jeong } 58064c546592SDaeho Jeong 5807c8585c6fSDaeho Jeong percpu_down_write(&sbi->s_journal_flag_rwsem); 5808dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5809ac27a0ecSDave Kleikamp 5810ac27a0ecSDave Kleikamp /* 5811ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5812ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5813ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5814ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5815ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5816ac27a0ecSDave Kleikamp */ 5817ac27a0ecSDave Kleikamp 5818ac27a0ecSDave Kleikamp if (val) 581912e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 58205872ddaaSYongqiang Yang else { 58214f879ca6SJan Kara err = jbd2_journal_flush(journal); 58224f879ca6SJan Kara if (err < 0) { 58234f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 5824c8585c6fSDaeho Jeong percpu_up_write(&sbi->s_journal_flag_rwsem); 58254f879ca6SJan Kara return err; 58264f879ca6SJan Kara } 582712e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 58285872ddaaSYongqiang Yang } 5829617ba13bSMingming Cao ext4_set_aops(inode); 5830ac27a0ecSDave Kleikamp 5831dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5832c8585c6fSDaeho Jeong percpu_up_write(&sbi->s_journal_flag_rwsem); 5833c8585c6fSDaeho Jeong 58344c546592SDaeho Jeong if (val) 58354c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 5836ac27a0ecSDave Kleikamp 5837ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5838ac27a0ecSDave Kleikamp 58399924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5840ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5841ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5842ac27a0ecSDave Kleikamp 5843617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 58440390131bSFrank Mayhar ext4_handle_sync(handle); 5845617ba13bSMingming Cao ext4_journal_stop(handle); 5846617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5847ac27a0ecSDave Kleikamp 5848ac27a0ecSDave Kleikamp return err; 5849ac27a0ecSDave Kleikamp } 58502e9ee850SAneesh Kumar K.V 58512e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 58522e9ee850SAneesh Kumar K.V { 58532e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 58542e9ee850SAneesh Kumar K.V } 58552e9ee850SAneesh Kumar K.V 5856401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 58572e9ee850SAneesh Kumar K.V { 585811bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 5859c2ec175cSNick Piggin struct page *page = vmf->page; 58602e9ee850SAneesh Kumar K.V loff_t size; 58612e9ee850SAneesh Kumar K.V unsigned long len; 5862401b25aaSSouptick Joarder int err; 5863401b25aaSSouptick Joarder vm_fault_t ret; 58642e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5865496ad9aaSAl Viro struct inode *inode = file_inode(file); 58662e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 58679ea7df53SJan Kara handle_t *handle; 58689ea7df53SJan Kara get_block_t *get_block; 58699ea7df53SJan Kara int retries = 0; 58702e9ee850SAneesh Kumar K.V 587102b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 587202b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 587302b016caSTheodore Ts'o 58748e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5875041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 5876ea3d7209SJan Kara 5877ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 58787b4cc978SEric Biggers 5879401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 5880401b25aaSSouptick Joarder if (err) 58817b4cc978SEric Biggers goto out_ret; 58827b4cc978SEric Biggers 58839ea7df53SJan Kara /* Delalloc case is easy... */ 58849ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 58859ea7df53SJan Kara !ext4_should_journal_data(inode) && 58869ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 58879ea7df53SJan Kara do { 5888401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 58899ea7df53SJan Kara ext4_da_get_block_prep); 5890401b25aaSSouptick Joarder } while (err == -ENOSPC && 58919ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 58929ea7df53SJan Kara goto out_ret; 58932e9ee850SAneesh Kumar K.V } 58940e499890SDarrick J. Wong 58950e499890SDarrick J. Wong lock_page(page); 58969ea7df53SJan Kara size = i_size_read(inode); 58979ea7df53SJan Kara /* Page got truncated from under us? */ 58989ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 58999ea7df53SJan Kara unlock_page(page); 59009ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 59019ea7df53SJan Kara goto out; 59020e499890SDarrick J. Wong } 59032e9ee850SAneesh Kumar K.V 590409cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 590509cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 59062e9ee850SAneesh Kumar K.V else 590709cbfeafSKirill A. Shutemov len = PAGE_SIZE; 5908a827eaffSAneesh Kumar K.V /* 59099ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 59109ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 5911a827eaffSAneesh Kumar K.V */ 59122e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 5913f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 5914f19d5870STao Ma 0, len, NULL, 5915a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 59169ea7df53SJan Kara /* Wait so that we don't change page under IO */ 59171d1d1a76SDarrick J. Wong wait_for_stable_page(page); 59189ea7df53SJan Kara ret = VM_FAULT_LOCKED; 59199ea7df53SJan Kara goto out; 59202e9ee850SAneesh Kumar K.V } 5921a827eaffSAneesh Kumar K.V } 5922a827eaffSAneesh Kumar K.V unlock_page(page); 59239ea7df53SJan Kara /* OK, we need to fill the hole... */ 59249ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 5925705965bdSJan Kara get_block = ext4_get_block_unwritten; 59269ea7df53SJan Kara else 59279ea7df53SJan Kara get_block = ext4_get_block; 59289ea7df53SJan Kara retry_alloc: 59299924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 59309924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 59319ea7df53SJan Kara if (IS_ERR(handle)) { 5932c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 59339ea7df53SJan Kara goto out; 59349ea7df53SJan Kara } 5935401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 5936401b25aaSSouptick Joarder if (!err && ext4_should_journal_data(inode)) { 5937f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 593809cbfeafSKirill A. Shutemov PAGE_SIZE, NULL, do_journal_get_write_access)) { 59399ea7df53SJan Kara unlock_page(page); 59409ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5941fcbb5515SYongqiang Yang ext4_journal_stop(handle); 59429ea7df53SJan Kara goto out; 59439ea7df53SJan Kara } 59449ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 59459ea7df53SJan Kara } 59469ea7df53SJan Kara ext4_journal_stop(handle); 5947401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 59489ea7df53SJan Kara goto retry_alloc; 59499ea7df53SJan Kara out_ret: 5950401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 59519ea7df53SJan Kara out: 5952ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 59538e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 59542e9ee850SAneesh Kumar K.V return ret; 59552e9ee850SAneesh Kumar K.V } 5956ea3d7209SJan Kara 5957401b25aaSSouptick Joarder vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 5958ea3d7209SJan Kara { 595911bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file); 5960401b25aaSSouptick Joarder vm_fault_t ret; 5961ea3d7209SJan Kara 5962ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 5963401b25aaSSouptick Joarder ret = filemap_fault(vmf); 5964ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 5965ea3d7209SJan Kara 5966401b25aaSSouptick Joarder return ret; 5967ea3d7209SJan Kara } 5968