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) { 274*878520acSTheodore Ts'o ext4_set_errno(inode->i_sb, -err); 2752c98eb5eSTheodore Ts'o ext4_error(inode->i_sb, 2762c98eb5eSTheodore Ts'o "couldn't truncate inode %lu (err %d)", 2772c98eb5eSTheodore Ts'o inode->i_ino, err); 2782c98eb5eSTheodore Ts'o goto stop_handle; 2792c98eb5eSTheodore Ts'o } 2802c98eb5eSTheodore Ts'o } 281bc965ab3STheodore Ts'o 28230a7eb97STahsin Erdogan /* Remove xattr references. */ 28330a7eb97STahsin Erdogan err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array, 28430a7eb97STahsin Erdogan extra_credits); 28530a7eb97STahsin Erdogan if (err) { 28630a7eb97STahsin Erdogan ext4_warning(inode->i_sb, "xattr delete (err %d)", err); 287bc965ab3STheodore Ts'o stop_handle: 288bc965ab3STheodore Ts'o ext4_journal_stop(handle); 28945388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2908e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 29130a7eb97STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 292bc965ab3STheodore Ts'o goto no_delete; 293bc965ab3STheodore Ts'o } 294bc965ab3STheodore Ts'o 295ac27a0ecSDave Kleikamp /* 296617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 297ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 298617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 299ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 300617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 301ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 302ac27a0ecSDave Kleikamp */ 303617ba13bSMingming Cao ext4_orphan_del(handle, inode); 3045ffff834SArnd Bergmann EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds(); 305ac27a0ecSDave Kleikamp 306ac27a0ecSDave Kleikamp /* 307ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 308ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 309ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 310ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 311ac27a0ecSDave Kleikamp * fails. 312ac27a0ecSDave Kleikamp */ 313617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 314ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3150930fcc1SAl Viro ext4_clear_inode(inode); 316ac27a0ecSDave Kleikamp else 317617ba13bSMingming Cao ext4_free_inode(handle, inode); 318617ba13bSMingming Cao ext4_journal_stop(handle); 3198e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 3200421a189STahsin Erdogan ext4_xattr_inode_array_free(ea_inode_array); 321ac27a0ecSDave Kleikamp return; 322ac27a0ecSDave Kleikamp no_delete: 3230930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 324ac27a0ecSDave Kleikamp } 325ac27a0ecSDave Kleikamp 326a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 327a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 32860e58e0fSMingming Cao { 329a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 33060e58e0fSMingming Cao } 331a9e7f447SDmitry Monakhov #endif 3329d0be502STheodore Ts'o 33312219aeaSAneesh Kumar K.V /* 3340637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3350637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3360637c6f4STheodore Ts'o */ 3375f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3385f634d06SAneesh Kumar K.V int used, int quota_claim) 33912219aeaSAneesh Kumar K.V { 34012219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3410637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34212219aeaSAneesh Kumar K.V 3430637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 344d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3450637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3468de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3471084f252STheodore Ts'o "with only %d reserved data blocks", 3480637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3490637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3500637c6f4STheodore Ts'o WARN_ON(1); 3510637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3526bc6e63fSAneesh Kumar K.V } 35312219aeaSAneesh Kumar K.V 3540637c6f4STheodore Ts'o /* Update per-inode reservations */ 3550637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 35671d4f7d0STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, used); 3570637c6f4STheodore Ts'o 35812219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 35960e58e0fSMingming Cao 36072b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 36172b8ab9dSEric Sandeen if (quota_claim) 3627b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 36372b8ab9dSEric Sandeen else { 3645f634d06SAneesh Kumar K.V /* 3655f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3665f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 36772b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3685f634d06SAneesh Kumar K.V */ 3697b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3705f634d06SAneesh Kumar K.V } 371d6014301SAneesh Kumar K.V 372d6014301SAneesh Kumar K.V /* 373d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 374d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 375d6014301SAneesh Kumar K.V * inode's preallocations. 376d6014301SAneesh Kumar K.V */ 3770637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 37882dd124cSNikolay Borisov !inode_is_open_for_write(inode)) 379d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 38012219aeaSAneesh Kumar K.V } 38112219aeaSAneesh Kumar K.V 382e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 383c398eda0STheodore Ts'o unsigned int line, 38424676da4STheodore Ts'o struct ext4_map_blocks *map) 3856fd058f7STheodore Ts'o { 386345c0dbfSTheodore Ts'o if (ext4_has_feature_journal(inode->i_sb) && 387345c0dbfSTheodore Ts'o (inode->i_ino == 388345c0dbfSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) 389345c0dbfSTheodore Ts'o return 0; 39024676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 39124676da4STheodore Ts'o map->m_len)) { 392c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 393bdbd6ce0STheodore Ts'o "lblock %lu mapped to illegal pblock %llu " 39424676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 395bdbd6ce0STheodore Ts'o map->m_pblk, map->m_len); 3966a797d27SDarrick J. Wong return -EFSCORRUPTED; 3976fd058f7STheodore Ts'o } 3986fd058f7STheodore Ts'o return 0; 3996fd058f7STheodore Ts'o } 4006fd058f7STheodore Ts'o 40153085facSJan Kara int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk, 40253085facSJan Kara ext4_lblk_t len) 40353085facSJan Kara { 40453085facSJan Kara int ret; 40553085facSJan Kara 406592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) 407a7550b30SJaegeuk Kim return fscrypt_zeroout_range(inode, lblk, pblk, len); 40853085facSJan Kara 40953085facSJan Kara ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 41053085facSJan Kara if (ret > 0) 41153085facSJan Kara ret = 0; 41253085facSJan Kara 41353085facSJan Kara return ret; 41453085facSJan Kara } 41553085facSJan Kara 416e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 417c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 418e29136f8STheodore Ts'o 419921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 420921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 421921f266bSDmitry Monakhov struct inode *inode, 422921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 423921f266bSDmitry Monakhov struct ext4_map_blocks *map, 424921f266bSDmitry Monakhov int flags) 425921f266bSDmitry Monakhov { 426921f266bSDmitry Monakhov int retval; 427921f266bSDmitry Monakhov 428921f266bSDmitry Monakhov map->m_flags = 0; 429921f266bSDmitry Monakhov /* 430921f266bSDmitry Monakhov * There is a race window that the result is not the same. 431921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 432921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 433921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 434921f266bSDmitry Monakhov * could be converted. 435921f266bSDmitry Monakhov */ 436c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 437921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 438921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 439921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 440921f266bSDmitry Monakhov } else { 441921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 442921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 443921f266bSDmitry Monakhov } 444921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 445921f266bSDmitry Monakhov 446921f266bSDmitry Monakhov /* 447921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 448921f266bSDmitry Monakhov * tree. So the m_len might not equal. 449921f266bSDmitry Monakhov */ 450921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 451921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 452921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 453bdafe42aSTheodore Ts'o printk("ES cache assertion failed for inode: %lu " 454921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 455921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 456921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 457921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 458921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 459921f266bSDmitry Monakhov retval, flags); 460921f266bSDmitry Monakhov } 461921f266bSDmitry Monakhov } 462921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 463921f266bSDmitry Monakhov 46455138e0bSTheodore Ts'o /* 465e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 4662b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 467f5ab0d1fSMingming Cao * 468f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 469f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 470f5ab0d1fSMingming Cao * mapped. 471f5ab0d1fSMingming Cao * 472e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 473e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 474f5ab0d1fSMingming Cao * based files 475f5ab0d1fSMingming Cao * 476facab4d9SJan Kara * On success, it returns the number of blocks being mapped or allocated. if 477facab4d9SJan Kara * create==0 and the blocks are pre-allocated and unwritten, the resulting @map 478facab4d9SJan Kara * is marked as unwritten. If the create == 1, it will mark @map as mapped. 479f5ab0d1fSMingming Cao * 480f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 481facab4d9SJan Kara * that case, @map is returned as unmapped but we still do fill map->m_len to 482facab4d9SJan Kara * indicate the length of a hole starting at map->m_lblk. 483f5ab0d1fSMingming Cao * 484f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 485f5ab0d1fSMingming Cao */ 486e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 487e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4880e855ac8SAneesh Kumar K.V { 489d100eef2SZheng Liu struct extent_status es; 4900e855ac8SAneesh Kumar K.V int retval; 491b8a86845SLukas Czerner int ret = 0; 492921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 493921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 494921f266bSDmitry Monakhov 495921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 496921f266bSDmitry Monakhov #endif 497f5ab0d1fSMingming Cao 498e35fd660STheodore Ts'o map->m_flags = 0; 499e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 500e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 501e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 502d100eef2SZheng Liu 503e861b5e9STheodore Ts'o /* 504e861b5e9STheodore Ts'o * ext4_map_blocks returns an int, and m_len is an unsigned int 505e861b5e9STheodore Ts'o */ 506e861b5e9STheodore Ts'o if (unlikely(map->m_len > INT_MAX)) 507e861b5e9STheodore Ts'o map->m_len = INT_MAX; 508e861b5e9STheodore Ts'o 5094adb6ab3SKazuya Mio /* We can handle the block number less than EXT_MAX_BLOCKS */ 5104adb6ab3SKazuya Mio if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS)) 5116a797d27SDarrick J. Wong return -EFSCORRUPTED; 5124adb6ab3SKazuya Mio 513d100eef2SZheng Liu /* Lookup extent status tree firstly */ 514bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 515d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 516d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 517d100eef2SZheng Liu map->m_lblk - es.es_lblk; 518d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 519d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 520d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 521d100eef2SZheng Liu if (retval > map->m_len) 522d100eef2SZheng Liu retval = map->m_len; 523d100eef2SZheng Liu map->m_len = retval; 524d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 525facab4d9SJan Kara map->m_pblk = 0; 526facab4d9SJan Kara retval = es.es_len - (map->m_lblk - es.es_lblk); 527facab4d9SJan Kara if (retval > map->m_len) 528facab4d9SJan Kara retval = map->m_len; 529facab4d9SJan Kara map->m_len = retval; 530d100eef2SZheng Liu retval = 0; 531d100eef2SZheng Liu } else { 5321e83bc81SArnd Bergmann BUG(); 533d100eef2SZheng Liu } 534921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 535921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 536921f266bSDmitry Monakhov &orig_map, flags); 537921f266bSDmitry Monakhov #endif 538d100eef2SZheng Liu goto found; 539d100eef2SZheng Liu } 540d100eef2SZheng Liu 5414df3d265SAneesh Kumar K.V /* 542b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 543b920c755STheodore Ts'o * file system block. 5444df3d265SAneesh Kumar K.V */ 545c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 54612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 547a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 548a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5494df3d265SAneesh Kumar K.V } else { 550a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 551a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 5520e855ac8SAneesh Kumar K.V } 553f7fec032SZheng Liu if (retval > 0) { 5543be78c73STheodore Ts'o unsigned int status; 555f7fec032SZheng Liu 55644fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 55744fb851dSZheng Liu ext4_warning(inode->i_sb, 55844fb851dSZheng Liu "ES len assertion failed for inode " 55944fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 56044fb851dSZheng Liu inode->i_ino, retval, map->m_len); 56144fb851dSZheng Liu WARN_ON(1); 562921f266bSDmitry Monakhov } 563921f266bSDmitry Monakhov 564f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 565f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 566f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 567d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 568ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 569f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 570f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 571f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 572f7fec032SZheng Liu map->m_len, map->m_pblk, status); 573f7fec032SZheng Liu if (ret < 0) 574f7fec032SZheng Liu retval = ret; 575f7fec032SZheng Liu } 5764df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 577f5ab0d1fSMingming Cao 578d100eef2SZheng Liu found: 579e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 580b8a86845SLukas Czerner ret = check_block_validity(inode, map); 5816fd058f7STheodore Ts'o if (ret != 0) 5826fd058f7STheodore Ts'o return ret; 5836fd058f7STheodore Ts'o } 5846fd058f7STheodore Ts'o 585f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 586c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 5874df3d265SAneesh Kumar K.V return retval; 5884df3d265SAneesh Kumar K.V 5894df3d265SAneesh Kumar K.V /* 590f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 591f5ab0d1fSMingming Cao * 592f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 593df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 594f5ab0d1fSMingming Cao * with buffer head unmapped. 595f5ab0d1fSMingming Cao */ 596e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 597b8a86845SLukas Czerner /* 598b8a86845SLukas Czerner * If we need to convert extent to unwritten 599b8a86845SLukas Czerner * we continue and do the actual work in 600b8a86845SLukas Czerner * ext4_ext_map_blocks() 601b8a86845SLukas Czerner */ 602b8a86845SLukas Czerner if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) 603f5ab0d1fSMingming Cao return retval; 604f5ab0d1fSMingming Cao 605f5ab0d1fSMingming Cao /* 606a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 607a25a4e1aSZheng Liu * it will be set again. 6082a8964d6SAneesh Kumar K.V */ 609a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6102a8964d6SAneesh Kumar K.V 6112a8964d6SAneesh Kumar K.V /* 612556615dcSLukas Czerner * New blocks allocate and/or writing to unwritten extent 613f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 614d91bd2c1SSeunghun Lee * the write lock of i_data_sem, and call get_block() 615f5ab0d1fSMingming Cao * with create == 1 flag. 6164df3d265SAneesh Kumar K.V */ 617c8b459f4SLukas Czerner down_write(&EXT4_I(inode)->i_data_sem); 618d2a17637SMingming Cao 619d2a17637SMingming Cao /* 6204df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6214df3d265SAneesh Kumar K.V * could have changed the inode type in between 6224df3d265SAneesh Kumar K.V */ 62312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 624e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6250e855ac8SAneesh Kumar K.V } else { 626e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 627267e4db9SAneesh Kumar K.V 628e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 629267e4db9SAneesh Kumar K.V /* 630267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 631267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 632267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 633267e4db9SAneesh Kumar K.V */ 63419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 635267e4db9SAneesh Kumar K.V } 6362ac3b6e0STheodore Ts'o 637d2a17637SMingming Cao /* 6382ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 6395f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 6405f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 6415f634d06SAneesh Kumar K.V * reserve space here. 642d2a17637SMingming Cao */ 6435f634d06SAneesh Kumar K.V if ((retval > 0) && 6441296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 6455f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 6465f634d06SAneesh Kumar K.V } 647d2a17637SMingming Cao 648f7fec032SZheng Liu if (retval > 0) { 6493be78c73STheodore Ts'o unsigned int status; 650f7fec032SZheng Liu 65144fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 65244fb851dSZheng Liu ext4_warning(inode->i_sb, 65344fb851dSZheng Liu "ES len assertion failed for inode " 65444fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 65544fb851dSZheng Liu inode->i_ino, retval, map->m_len); 65644fb851dSZheng Liu WARN_ON(1); 657921f266bSDmitry Monakhov } 658921f266bSDmitry Monakhov 659adb23551SZheng Liu /* 660c86d8db3SJan Kara * We have to zeroout blocks before inserting them into extent 661c86d8db3SJan Kara * status tree. Otherwise someone could look them up there and 6629b623df6SJan Kara * use them before they are really zeroed. We also have to 6639b623df6SJan Kara * unmap metadata before zeroing as otherwise writeback can 6649b623df6SJan Kara * overwrite zeros with stale data from block device. 665c86d8db3SJan Kara */ 666c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO && 667c86d8db3SJan Kara map->m_flags & EXT4_MAP_MAPPED && 668c86d8db3SJan Kara map->m_flags & EXT4_MAP_NEW) { 669c86d8db3SJan Kara ret = ext4_issue_zeroout(inode, map->m_lblk, 670c86d8db3SJan Kara map->m_pblk, map->m_len); 671c86d8db3SJan Kara if (ret) { 672c86d8db3SJan Kara retval = ret; 673c86d8db3SJan Kara goto out_sem; 674c86d8db3SJan Kara } 675c86d8db3SJan Kara } 676c86d8db3SJan Kara 677c86d8db3SJan Kara /* 678adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 679adb23551SZheng Liu * extent status tree. 680adb23551SZheng Liu */ 681adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 682bb5835edSTheodore Ts'o ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { 683adb23551SZheng Liu if (ext4_es_is_written(&es)) 684c86d8db3SJan Kara goto out_sem; 685adb23551SZheng Liu } 686f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 687f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 688f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 689d2dc317dSLukas Czerner !(status & EXTENT_STATUS_WRITTEN) && 690ad431025SEric Whitney ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk, 691f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 692f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 693f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 694f7fec032SZheng Liu map->m_pblk, status); 695c86d8db3SJan Kara if (ret < 0) { 69651865fdaSZheng Liu retval = ret; 697c86d8db3SJan Kara goto out_sem; 698c86d8db3SJan Kara } 69951865fdaSZheng Liu } 7005356f261SAditya Kali 701c86d8db3SJan Kara out_sem: 7020e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 703e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 704b8a86845SLukas Czerner ret = check_block_validity(inode, map); 7056fd058f7STheodore Ts'o if (ret != 0) 7066fd058f7STheodore Ts'o return ret; 70706bd3c36SJan Kara 70806bd3c36SJan Kara /* 70906bd3c36SJan Kara * Inodes with freshly allocated blocks where contents will be 71006bd3c36SJan Kara * visible after transaction commit must be on transaction's 71106bd3c36SJan Kara * ordered data list. 71206bd3c36SJan Kara */ 71306bd3c36SJan Kara if (map->m_flags & EXT4_MAP_NEW && 71406bd3c36SJan Kara !(map->m_flags & EXT4_MAP_UNWRITTEN) && 71506bd3c36SJan Kara !(flags & EXT4_GET_BLOCKS_ZERO) && 71602749a4cSTahsin Erdogan !ext4_is_quota_file(inode) && 71706bd3c36SJan Kara ext4_should_order_data(inode)) { 71873131fbbSRoss Zwisler loff_t start_byte = 71973131fbbSRoss Zwisler (loff_t)map->m_lblk << inode->i_blkbits; 72073131fbbSRoss Zwisler loff_t length = (loff_t)map->m_len << inode->i_blkbits; 72173131fbbSRoss Zwisler 722ee0876bcSJan Kara if (flags & EXT4_GET_BLOCKS_IO_SUBMIT) 72373131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_wait(handle, inode, 72473131fbbSRoss Zwisler start_byte, length); 725ee0876bcSJan Kara else 72673131fbbSRoss Zwisler ret = ext4_jbd2_inode_add_write(handle, inode, 72773131fbbSRoss Zwisler start_byte, length); 72806bd3c36SJan Kara if (ret) 72906bd3c36SJan Kara return ret; 73006bd3c36SJan Kara } 7316fd058f7STheodore Ts'o } 7320e855ac8SAneesh Kumar K.V return retval; 7330e855ac8SAneesh Kumar K.V } 7340e855ac8SAneesh Kumar K.V 735ed8ad838SJan Kara /* 736ed8ad838SJan Kara * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages 737ed8ad838SJan Kara * we have to be careful as someone else may be manipulating b_state as well. 738ed8ad838SJan Kara */ 739ed8ad838SJan Kara static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags) 740ed8ad838SJan Kara { 741ed8ad838SJan Kara unsigned long old_state; 742ed8ad838SJan Kara unsigned long new_state; 743ed8ad838SJan Kara 744ed8ad838SJan Kara flags &= EXT4_MAP_FLAGS; 745ed8ad838SJan Kara 746ed8ad838SJan Kara /* Dummy buffer_head? Set non-atomically. */ 747ed8ad838SJan Kara if (!bh->b_page) { 748ed8ad838SJan Kara bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags; 749ed8ad838SJan Kara return; 750ed8ad838SJan Kara } 751ed8ad838SJan Kara /* 752ed8ad838SJan Kara * Someone else may be modifying b_state. Be careful! This is ugly but 753ed8ad838SJan Kara * once we get rid of using bh as a container for mapping information 754ed8ad838SJan Kara * to pass to / from get_block functions, this can go away. 755ed8ad838SJan Kara */ 756ed8ad838SJan Kara do { 757ed8ad838SJan Kara old_state = READ_ONCE(bh->b_state); 758ed8ad838SJan Kara new_state = (old_state & ~EXT4_MAP_FLAGS) | flags; 759ed8ad838SJan Kara } while (unlikely( 760ed8ad838SJan Kara cmpxchg(&bh->b_state, old_state, new_state) != old_state)); 761ed8ad838SJan Kara } 762ed8ad838SJan Kara 7632ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7642ed88685STheodore Ts'o struct buffer_head *bh, int flags) 765ac27a0ecSDave Kleikamp { 7662ed88685STheodore Ts'o struct ext4_map_blocks map; 767efe70c29SJan Kara int ret = 0; 768ac27a0ecSDave Kleikamp 76946c7f254STao Ma if (ext4_has_inline_data(inode)) 77046c7f254STao Ma return -ERANGE; 77146c7f254STao Ma 7722ed88685STheodore Ts'o map.m_lblk = iblock; 7732ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7742ed88685STheodore Ts'o 775efe70c29SJan Kara ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map, 776efe70c29SJan Kara flags); 777ac27a0ecSDave Kleikamp if (ret > 0) { 7782ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 779ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 7802ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 781ac27a0ecSDave Kleikamp ret = 0; 782547edce3SRoss Zwisler } else if (ret == 0) { 783547edce3SRoss Zwisler /* hole case, need to fill in bh->b_size */ 784547edce3SRoss Zwisler bh->b_size = inode->i_sb->s_blocksize * map.m_len; 785ac27a0ecSDave Kleikamp } 786ac27a0ecSDave Kleikamp return ret; 787ac27a0ecSDave Kleikamp } 788ac27a0ecSDave Kleikamp 7892ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7902ed88685STheodore Ts'o struct buffer_head *bh, int create) 7912ed88685STheodore Ts'o { 7922ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 7932ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 7942ed88685STheodore Ts'o } 7952ed88685STheodore Ts'o 796ac27a0ecSDave Kleikamp /* 797705965bdSJan Kara * Get block function used when preparing for buffered write if we require 798705965bdSJan Kara * creating an unwritten extent if blocks haven't been allocated. The extent 799705965bdSJan Kara * will be converted to written after the IO is complete. 800705965bdSJan Kara */ 801705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock, 802705965bdSJan Kara struct buffer_head *bh_result, int create) 803705965bdSJan Kara { 804705965bdSJan Kara ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n", 805705965bdSJan Kara inode->i_ino, create); 806705965bdSJan Kara return _ext4_get_block(inode, iblock, bh_result, 807705965bdSJan Kara EXT4_GET_BLOCKS_IO_CREATE_EXT); 808705965bdSJan Kara } 809705965bdSJan Kara 810efe70c29SJan Kara /* Maximum number of blocks we map for direct IO at once. */ 811efe70c29SJan Kara #define DIO_MAX_BLOCKS 4096 812efe70c29SJan Kara 813e84dfbe2SJan Kara /* 814ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 815ac27a0ecSDave Kleikamp */ 816617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 817c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 818ac27a0ecSDave Kleikamp { 8192ed88685STheodore Ts'o struct ext4_map_blocks map; 8202ed88685STheodore Ts'o struct buffer_head *bh; 821c5e298aeSTheodore Ts'o int create = map_flags & EXT4_GET_BLOCKS_CREATE; 82210560082STheodore Ts'o int err; 823ac27a0ecSDave Kleikamp 824ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 825ac27a0ecSDave Kleikamp 8262ed88685STheodore Ts'o map.m_lblk = block; 8272ed88685STheodore Ts'o map.m_len = 1; 828c5e298aeSTheodore Ts'o err = ext4_map_blocks(handle, inode, &map, map_flags); 8292ed88685STheodore Ts'o 83010560082STheodore Ts'o if (err == 0) 83110560082STheodore Ts'o return create ? ERR_PTR(-ENOSPC) : NULL; 8322ed88685STheodore Ts'o if (err < 0) 83310560082STheodore Ts'o return ERR_PTR(err); 8342ed88685STheodore Ts'o 8352ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 83610560082STheodore Ts'o if (unlikely(!bh)) 83710560082STheodore Ts'o return ERR_PTR(-ENOMEM); 8382ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 839ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 840ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 841ac27a0ecSDave Kleikamp 842ac27a0ecSDave Kleikamp /* 843ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 844ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 845ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 846617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 847ac27a0ecSDave Kleikamp * problem. 848ac27a0ecSDave Kleikamp */ 849ac27a0ecSDave Kleikamp lock_buffer(bh); 850ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 85110560082STheodore Ts'o err = ext4_journal_get_create_access(handle, bh); 85210560082STheodore Ts'o if (unlikely(err)) { 85310560082STheodore Ts'o unlock_buffer(bh); 85410560082STheodore Ts'o goto errout; 85510560082STheodore Ts'o } 85610560082STheodore Ts'o if (!buffer_uptodate(bh)) { 857ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 858ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 859ac27a0ecSDave Kleikamp } 860ac27a0ecSDave Kleikamp unlock_buffer(bh); 8610390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8620390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 86310560082STheodore Ts'o if (unlikely(err)) 86410560082STheodore Ts'o goto errout; 86510560082STheodore Ts'o } else 866ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 867ac27a0ecSDave Kleikamp return bh; 86810560082STheodore Ts'o errout: 86910560082STheodore Ts'o brelse(bh); 87010560082STheodore Ts'o return ERR_PTR(err); 871ac27a0ecSDave Kleikamp } 872ac27a0ecSDave Kleikamp 873617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 874c5e298aeSTheodore Ts'o ext4_lblk_t block, int map_flags) 875ac27a0ecSDave Kleikamp { 876ac27a0ecSDave Kleikamp struct buffer_head *bh; 877ac27a0ecSDave Kleikamp 878c5e298aeSTheodore Ts'o bh = ext4_getblk(handle, inode, block, map_flags); 8791c215028STheodore Ts'o if (IS_ERR(bh)) 880ac27a0ecSDave Kleikamp return bh; 8817963e5acSZhangXiaoxu if (!bh || ext4_buffer_uptodate(bh)) 882ac27a0ecSDave Kleikamp return bh; 883dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh); 884ac27a0ecSDave Kleikamp wait_on_buffer(bh); 885ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 886ac27a0ecSDave Kleikamp return bh; 887ac27a0ecSDave Kleikamp put_bh(bh); 8881c215028STheodore Ts'o return ERR_PTR(-EIO); 889ac27a0ecSDave Kleikamp } 890ac27a0ecSDave Kleikamp 8919699d4f9STahsin Erdogan /* Read a contiguous batch of blocks. */ 8929699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count, 8939699d4f9STahsin Erdogan bool wait, struct buffer_head **bhs) 8949699d4f9STahsin Erdogan { 8959699d4f9STahsin Erdogan int i, err; 8969699d4f9STahsin Erdogan 8979699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 8989699d4f9STahsin Erdogan bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */); 8999699d4f9STahsin Erdogan if (IS_ERR(bhs[i])) { 9009699d4f9STahsin Erdogan err = PTR_ERR(bhs[i]); 9019699d4f9STahsin Erdogan bh_count = i; 9029699d4f9STahsin Erdogan goto out_brelse; 9039699d4f9STahsin Erdogan } 9049699d4f9STahsin Erdogan } 9059699d4f9STahsin Erdogan 9069699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9079699d4f9STahsin Erdogan /* Note that NULL bhs[i] is valid because of holes. */ 9087963e5acSZhangXiaoxu if (bhs[i] && !ext4_buffer_uptodate(bhs[i])) 9099699d4f9STahsin Erdogan ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, 9109699d4f9STahsin Erdogan &bhs[i]); 9119699d4f9STahsin Erdogan 9129699d4f9STahsin Erdogan if (!wait) 9139699d4f9STahsin Erdogan return 0; 9149699d4f9STahsin Erdogan 9159699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) 9169699d4f9STahsin Erdogan if (bhs[i]) 9179699d4f9STahsin Erdogan wait_on_buffer(bhs[i]); 9189699d4f9STahsin Erdogan 9199699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9209699d4f9STahsin Erdogan if (bhs[i] && !buffer_uptodate(bhs[i])) { 9219699d4f9STahsin Erdogan err = -EIO; 9229699d4f9STahsin Erdogan goto out_brelse; 9239699d4f9STahsin Erdogan } 9249699d4f9STahsin Erdogan } 9259699d4f9STahsin Erdogan return 0; 9269699d4f9STahsin Erdogan 9279699d4f9STahsin Erdogan out_brelse: 9289699d4f9STahsin Erdogan for (i = 0; i < bh_count; i++) { 9299699d4f9STahsin Erdogan brelse(bhs[i]); 9309699d4f9STahsin Erdogan bhs[i] = NULL; 9319699d4f9STahsin Erdogan } 9329699d4f9STahsin Erdogan return err; 9339699d4f9STahsin Erdogan } 9349699d4f9STahsin Erdogan 935f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 936ac27a0ecSDave Kleikamp struct buffer_head *head, 937ac27a0ecSDave Kleikamp unsigned from, 938ac27a0ecSDave Kleikamp unsigned to, 939ac27a0ecSDave Kleikamp int *partial, 940ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 941ac27a0ecSDave Kleikamp struct buffer_head *bh)) 942ac27a0ecSDave Kleikamp { 943ac27a0ecSDave Kleikamp struct buffer_head *bh; 944ac27a0ecSDave Kleikamp unsigned block_start, block_end; 945ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 946ac27a0ecSDave Kleikamp int err, ret = 0; 947ac27a0ecSDave Kleikamp struct buffer_head *next; 948ac27a0ecSDave Kleikamp 949ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 950ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 951de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 952ac27a0ecSDave Kleikamp next = bh->b_this_page; 953ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 954ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 955ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 956ac27a0ecSDave Kleikamp *partial = 1; 957ac27a0ecSDave Kleikamp continue; 958ac27a0ecSDave Kleikamp } 959ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 960ac27a0ecSDave Kleikamp if (!ret) 961ac27a0ecSDave Kleikamp ret = err; 962ac27a0ecSDave Kleikamp } 963ac27a0ecSDave Kleikamp return ret; 964ac27a0ecSDave Kleikamp } 965ac27a0ecSDave Kleikamp 966ac27a0ecSDave Kleikamp /* 967ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 968ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 969617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 970dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 971ac27a0ecSDave Kleikamp * prepare_write() is the right place. 972ac27a0ecSDave Kleikamp * 97336ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 97436ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 97536ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 97636ade451SJan Kara * because the caller may be PF_MEMALLOC. 977ac27a0ecSDave Kleikamp * 978617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 979ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 980ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 981ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 982ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 983ac27a0ecSDave Kleikamp * violation. 984ac27a0ecSDave Kleikamp * 985dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 986ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 987ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 988ac27a0ecSDave Kleikamp * write. 989ac27a0ecSDave Kleikamp */ 990f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 991ac27a0ecSDave Kleikamp struct buffer_head *bh) 992ac27a0ecSDave Kleikamp { 99356d35a4cSJan Kara int dirty = buffer_dirty(bh); 99456d35a4cSJan Kara int ret; 99556d35a4cSJan Kara 996ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 997ac27a0ecSDave Kleikamp return 0; 99856d35a4cSJan Kara /* 999ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 100056d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 100156d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 1002ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 100356d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 100456d35a4cSJan Kara * ever write the buffer. 100556d35a4cSJan Kara */ 100656d35a4cSJan Kara if (dirty) 100756d35a4cSJan Kara clear_buffer_dirty(bh); 10085d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 100956d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 101056d35a4cSJan Kara if (!ret && dirty) 101156d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 101256d35a4cSJan Kara return ret; 1013ac27a0ecSDave Kleikamp } 1014ac27a0ecSDave Kleikamp 1015643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 10162058f83aSMichael Halcrow static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, 10172058f83aSMichael Halcrow get_block_t *get_block) 10182058f83aSMichael Halcrow { 101909cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 10202058f83aSMichael Halcrow unsigned to = from + len; 10212058f83aSMichael Halcrow struct inode *inode = page->mapping->host; 10222058f83aSMichael Halcrow unsigned block_start, block_end; 10232058f83aSMichael Halcrow sector_t block; 10242058f83aSMichael Halcrow int err = 0; 10252058f83aSMichael Halcrow unsigned blocksize = inode->i_sb->s_blocksize; 10262058f83aSMichael Halcrow unsigned bbits; 10270b578f35SChandan Rajendra struct buffer_head *bh, *head, *wait[2]; 10280b578f35SChandan Rajendra int nr_wait = 0; 10290b578f35SChandan Rajendra int i; 10302058f83aSMichael Halcrow 10312058f83aSMichael Halcrow BUG_ON(!PageLocked(page)); 103209cbfeafSKirill A. Shutemov BUG_ON(from > PAGE_SIZE); 103309cbfeafSKirill A. Shutemov BUG_ON(to > PAGE_SIZE); 10342058f83aSMichael Halcrow BUG_ON(from > to); 10352058f83aSMichael Halcrow 10362058f83aSMichael Halcrow if (!page_has_buffers(page)) 10372058f83aSMichael Halcrow create_empty_buffers(page, blocksize, 0); 10382058f83aSMichael Halcrow head = page_buffers(page); 10392058f83aSMichael Halcrow bbits = ilog2(blocksize); 104009cbfeafSKirill A. Shutemov block = (sector_t)page->index << (PAGE_SHIFT - bbits); 10412058f83aSMichael Halcrow 10422058f83aSMichael Halcrow for (bh = head, block_start = 0; bh != head || !block_start; 10432058f83aSMichael Halcrow block++, block_start = block_end, bh = bh->b_this_page) { 10442058f83aSMichael Halcrow block_end = block_start + blocksize; 10452058f83aSMichael Halcrow if (block_end <= from || block_start >= to) { 10462058f83aSMichael Halcrow if (PageUptodate(page)) { 10472058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10482058f83aSMichael Halcrow set_buffer_uptodate(bh); 10492058f83aSMichael Halcrow } 10502058f83aSMichael Halcrow continue; 10512058f83aSMichael Halcrow } 10522058f83aSMichael Halcrow if (buffer_new(bh)) 10532058f83aSMichael Halcrow clear_buffer_new(bh); 10542058f83aSMichael Halcrow if (!buffer_mapped(bh)) { 10552058f83aSMichael Halcrow WARN_ON(bh->b_size != blocksize); 10562058f83aSMichael Halcrow err = get_block(inode, block, bh, 1); 10572058f83aSMichael Halcrow if (err) 10582058f83aSMichael Halcrow break; 10592058f83aSMichael Halcrow if (buffer_new(bh)) { 10602058f83aSMichael Halcrow if (PageUptodate(page)) { 10612058f83aSMichael Halcrow clear_buffer_new(bh); 10622058f83aSMichael Halcrow set_buffer_uptodate(bh); 10632058f83aSMichael Halcrow mark_buffer_dirty(bh); 10642058f83aSMichael Halcrow continue; 10652058f83aSMichael Halcrow } 10662058f83aSMichael Halcrow if (block_end > to || block_start < from) 10672058f83aSMichael Halcrow zero_user_segments(page, to, block_end, 10682058f83aSMichael Halcrow block_start, from); 10692058f83aSMichael Halcrow continue; 10702058f83aSMichael Halcrow } 10712058f83aSMichael Halcrow } 10722058f83aSMichael Halcrow if (PageUptodate(page)) { 10732058f83aSMichael Halcrow if (!buffer_uptodate(bh)) 10742058f83aSMichael Halcrow set_buffer_uptodate(bh); 10752058f83aSMichael Halcrow continue; 10762058f83aSMichael Halcrow } 10772058f83aSMichael Halcrow if (!buffer_uptodate(bh) && !buffer_delay(bh) && 10782058f83aSMichael Halcrow !buffer_unwritten(bh) && 10792058f83aSMichael Halcrow (block_start < from || block_end > to)) { 1080dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 10810b578f35SChandan Rajendra wait[nr_wait++] = bh; 10822058f83aSMichael Halcrow } 10832058f83aSMichael Halcrow } 10842058f83aSMichael Halcrow /* 10852058f83aSMichael Halcrow * If we issued read requests, let them complete. 10862058f83aSMichael Halcrow */ 10870b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10880b578f35SChandan Rajendra wait_on_buffer(wait[i]); 10890b578f35SChandan Rajendra if (!buffer_uptodate(wait[i])) 10902058f83aSMichael Halcrow err = -EIO; 10912058f83aSMichael Halcrow } 10927e0785fcSChandan Rajendra if (unlikely(err)) { 10932058f83aSMichael Halcrow page_zero_new_buffers(page, from, to); 10940b578f35SChandan Rajendra } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) { 10950b578f35SChandan Rajendra for (i = 0; i < nr_wait; i++) { 10960b578f35SChandan Rajendra int err2; 10970b578f35SChandan Rajendra 10980b578f35SChandan Rajendra err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize, 10990b578f35SChandan Rajendra bh_offset(wait[i])); 11000b578f35SChandan Rajendra if (err2) { 11010b578f35SChandan Rajendra clear_buffer_uptodate(wait[i]); 11020b578f35SChandan Rajendra err = err2; 11030b578f35SChandan Rajendra } 11040b578f35SChandan Rajendra } 11057e0785fcSChandan Rajendra } 11067e0785fcSChandan Rajendra 11072058f83aSMichael Halcrow return err; 11082058f83aSMichael Halcrow } 11092058f83aSMichael Halcrow #endif 11102058f83aSMichael Halcrow 1111bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1112bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1113bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1114ac27a0ecSDave Kleikamp { 1115bfc1af65SNick Piggin struct inode *inode = mapping->host; 11161938a150SAneesh Kumar K.V int ret, needed_blocks; 1117ac27a0ecSDave Kleikamp handle_t *handle; 1118ac27a0ecSDave Kleikamp int retries = 0; 1119bfc1af65SNick Piggin struct page *page; 1120bfc1af65SNick Piggin pgoff_t index; 1121bfc1af65SNick Piggin unsigned from, to; 1122bfc1af65SNick Piggin 11230db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 11240db1ff22STheodore Ts'o return -EIO; 11250db1ff22STheodore Ts'o 11269bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 11271938a150SAneesh Kumar K.V /* 11281938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 11291938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 11301938a150SAneesh Kumar K.V */ 11311938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 113209cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 113309cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1134bfc1af65SNick Piggin to = from + len; 1135ac27a0ecSDave Kleikamp 1136f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 1137f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 1138f19d5870STao Ma flags, pagep); 1139f19d5870STao Ma if (ret < 0) 114047564bfbSTheodore Ts'o return ret; 114147564bfbSTheodore Ts'o if (ret == 1) 114247564bfbSTheodore Ts'o return 0; 1143f19d5870STao Ma } 1144f19d5870STao Ma 114547564bfbSTheodore Ts'o /* 114647564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 114747564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 114847564bfbSTheodore Ts'o * is being written back. So grab it first before we start 114947564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 115047564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 115147564bfbSTheodore Ts'o */ 115247564bfbSTheodore Ts'o retry_grab: 115354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 115447564bfbSTheodore Ts'o if (!page) 115547564bfbSTheodore Ts'o return -ENOMEM; 115647564bfbSTheodore Ts'o unlock_page(page); 115747564bfbSTheodore Ts'o 115847564bfbSTheodore Ts'o retry_journal: 11599924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1160ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 116109cbfeafSKirill A. Shutemov put_page(page); 116247564bfbSTheodore Ts'o return PTR_ERR(handle); 1163cf108bcaSJan Kara } 1164f19d5870STao Ma 116547564bfbSTheodore Ts'o lock_page(page); 116647564bfbSTheodore Ts'o if (page->mapping != mapping) { 116747564bfbSTheodore Ts'o /* The page got truncated from under us */ 116847564bfbSTheodore Ts'o unlock_page(page); 116909cbfeafSKirill A. Shutemov put_page(page); 1170cf108bcaSJan Kara ext4_journal_stop(handle); 117147564bfbSTheodore Ts'o goto retry_grab; 1172cf108bcaSJan Kara } 11737afe5aa5SDmitry Monakhov /* In case writeback began while the page was unlocked */ 11747afe5aa5SDmitry Monakhov wait_for_stable_page(page); 1175cf108bcaSJan Kara 1176643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 11772058f83aSMichael Halcrow if (ext4_should_dioread_nolock(inode)) 11782058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 1179705965bdSJan Kara ext4_get_block_unwritten); 11802058f83aSMichael Halcrow else 11812058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 11822058f83aSMichael Halcrow ext4_get_block); 11832058f83aSMichael Halcrow #else 1184744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1185705965bdSJan Kara ret = __block_write_begin(page, pos, len, 1186705965bdSJan Kara ext4_get_block_unwritten); 1187744692dcSJiaying Zhang else 11886e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 11892058f83aSMichael Halcrow #endif 1190bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1191f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1192f19d5870STao Ma from, to, NULL, 1193f19d5870STao Ma do_journal_get_write_access); 1194b46be050SAndrey Savochkin } 1195bfc1af65SNick Piggin 1196bfc1af65SNick Piggin if (ret) { 1197c93d8f88SEric Biggers bool extended = (pos + len > inode->i_size) && 1198c93d8f88SEric Biggers !ext4_verity_in_progress(inode); 1199c93d8f88SEric Biggers 1200bfc1af65SNick Piggin unlock_page(page); 1201ae4d5372SAneesh Kumar K.V /* 12026e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1203ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1204ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 12051938a150SAneesh Kumar K.V * 12061938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 12071938a150SAneesh Kumar K.V * truncate finishes 1208ae4d5372SAneesh Kumar K.V */ 1209c93d8f88SEric Biggers if (extended && ext4_can_truncate(inode)) 12101938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 12111938a150SAneesh Kumar K.V 12121938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1213c93d8f88SEric Biggers if (extended) { 1214b9a4207dSJan Kara ext4_truncate_failed_write(inode); 12151938a150SAneesh Kumar K.V /* 1216ffacfa7aSJan Kara * If truncate failed early the inode might 12171938a150SAneesh Kumar K.V * still be on the orphan list; we need to 12181938a150SAneesh Kumar K.V * make sure the inode is removed from the 12191938a150SAneesh Kumar K.V * orphan list in that case. 12201938a150SAneesh Kumar K.V */ 12211938a150SAneesh Kumar K.V if (inode->i_nlink) 12221938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 12231938a150SAneesh Kumar K.V } 1224bfc1af65SNick Piggin 122547564bfbSTheodore Ts'o if (ret == -ENOSPC && 122647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 122747564bfbSTheodore Ts'o goto retry_journal; 122809cbfeafSKirill A. Shutemov put_page(page); 122947564bfbSTheodore Ts'o return ret; 123047564bfbSTheodore Ts'o } 123147564bfbSTheodore Ts'o *pagep = page; 1232ac27a0ecSDave Kleikamp return ret; 1233ac27a0ecSDave Kleikamp } 1234ac27a0ecSDave Kleikamp 1235bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1236bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1237ac27a0ecSDave Kleikamp { 123813fca323STheodore Ts'o int ret; 1239ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1240ac27a0ecSDave Kleikamp return 0; 1241ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 124213fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 124313fca323STheodore Ts'o clear_buffer_meta(bh); 124413fca323STheodore Ts'o clear_buffer_prio(bh); 124513fca323STheodore Ts'o return ret; 1246ac27a0ecSDave Kleikamp } 1247ac27a0ecSDave Kleikamp 1248eed4333fSZheng Liu /* 1249eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1250eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1251eed4333fSZheng Liu * 1252eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1253eed4333fSZheng Liu * buffers are managed internally. 1254eed4333fSZheng Liu */ 1255eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1256f8514083SAneesh Kumar K.V struct address_space *mapping, 1257f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1258f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1259f8514083SAneesh Kumar K.V { 1260f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1261eed4333fSZheng Liu struct inode *inode = mapping->host; 12620572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1263eed4333fSZheng Liu int ret = 0, ret2; 1264eed4333fSZheng Liu int i_size_changed = 0; 1265362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1266c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1267eed4333fSZheng Liu 1268eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1269362eca70STheodore Ts'o if (inline_data) { 127042c832deSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 1271f19d5870STao Ma copied, page); 1272eb5efbcbSTheodore Ts'o if (ret < 0) { 1273eb5efbcbSTheodore Ts'o unlock_page(page); 1274eb5efbcbSTheodore Ts'o put_page(page); 127542c832deSTheodore Ts'o goto errout; 1276eb5efbcbSTheodore Ts'o } 127742c832deSTheodore Ts'o copied = ret; 127842c832deSTheodore Ts'o } else 1279f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1280f19d5870STao Ma len, copied, page, fsdata); 1281f8514083SAneesh Kumar K.V /* 12824631dbf6SDmitry Monakhov * it's important to update i_size while still holding page lock: 1283f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1284c93d8f88SEric Biggers * 1285c93d8f88SEric Biggers * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree 1286c93d8f88SEric Biggers * blocks are being written past EOF, so skip the i_size update. 1287f8514083SAneesh Kumar K.V */ 1288c93d8f88SEric Biggers if (!verity) 12894631dbf6SDmitry Monakhov i_size_changed = ext4_update_inode_size(inode, pos + copied); 1290f8514083SAneesh Kumar K.V unlock_page(page); 129109cbfeafSKirill A. Shutemov put_page(page); 1292f8514083SAneesh Kumar K.V 1293c93d8f88SEric Biggers if (old_size < pos && !verity) 12940572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 1295f8514083SAneesh Kumar K.V /* 1296f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1297f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1298f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1299f8514083SAneesh Kumar K.V * filesystems. 1300f8514083SAneesh Kumar K.V */ 1301362eca70STheodore Ts'o if (i_size_changed || inline_data) 1302f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1303f8514083SAneesh Kumar K.V 1304c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1305f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1306f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1307f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1308f8514083SAneesh Kumar K.V */ 1309f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 131074d553aaSTheodore Ts'o errout: 1311617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1312ac27a0ecSDave Kleikamp if (!ret) 1313ac27a0ecSDave Kleikamp ret = ret2; 1314bfc1af65SNick Piggin 1315c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1316b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1317f8514083SAneesh Kumar K.V /* 1318ffacfa7aSJan Kara * If truncate failed early the inode might still be 1319f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1320f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1321f8514083SAneesh Kumar K.V */ 1322f8514083SAneesh Kumar K.V if (inode->i_nlink) 1323f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1324f8514083SAneesh Kumar K.V } 1325f8514083SAneesh Kumar K.V 1326bfc1af65SNick Piggin return ret ? ret : copied; 1327ac27a0ecSDave Kleikamp } 1328ac27a0ecSDave Kleikamp 1329b90197b6STheodore Ts'o /* 1330b90197b6STheodore Ts'o * This is a private version of page_zero_new_buffers() which doesn't 1331b90197b6STheodore Ts'o * set the buffer to be dirty, since in data=journalled mode we need 1332b90197b6STheodore Ts'o * to call ext4_handle_dirty_metadata() instead. 1333b90197b6STheodore Ts'o */ 13343b136499SJan Kara static void ext4_journalled_zero_new_buffers(handle_t *handle, 13353b136499SJan Kara struct page *page, 13363b136499SJan Kara unsigned from, unsigned to) 1337b90197b6STheodore Ts'o { 1338b90197b6STheodore Ts'o unsigned int block_start = 0, block_end; 1339b90197b6STheodore Ts'o struct buffer_head *head, *bh; 1340b90197b6STheodore Ts'o 1341b90197b6STheodore Ts'o bh = head = page_buffers(page); 1342b90197b6STheodore Ts'o do { 1343b90197b6STheodore Ts'o block_end = block_start + bh->b_size; 1344b90197b6STheodore Ts'o if (buffer_new(bh)) { 1345b90197b6STheodore Ts'o if (block_end > from && block_start < to) { 1346b90197b6STheodore Ts'o if (!PageUptodate(page)) { 1347b90197b6STheodore Ts'o unsigned start, size; 1348b90197b6STheodore Ts'o 1349b90197b6STheodore Ts'o start = max(from, block_start); 1350b90197b6STheodore Ts'o size = min(to, block_end) - start; 1351b90197b6STheodore Ts'o 1352b90197b6STheodore Ts'o zero_user(page, start, size); 13533b136499SJan Kara write_end_fn(handle, bh); 1354b90197b6STheodore Ts'o } 1355b90197b6STheodore Ts'o clear_buffer_new(bh); 1356b90197b6STheodore Ts'o } 1357b90197b6STheodore Ts'o } 1358b90197b6STheodore Ts'o block_start = block_end; 1359b90197b6STheodore Ts'o bh = bh->b_this_page; 1360b90197b6STheodore Ts'o } while (bh != head); 1361b90197b6STheodore Ts'o } 1362b90197b6STheodore Ts'o 1363bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1364bfc1af65SNick Piggin struct address_space *mapping, 1365bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1366bfc1af65SNick Piggin struct page *page, void *fsdata) 1367ac27a0ecSDave Kleikamp { 1368617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1369bfc1af65SNick Piggin struct inode *inode = mapping->host; 13700572639fSXiaoguang Wang loff_t old_size = inode->i_size; 1371ac27a0ecSDave Kleikamp int ret = 0, ret2; 1372ac27a0ecSDave Kleikamp int partial = 0; 1373bfc1af65SNick Piggin unsigned from, to; 13744631dbf6SDmitry Monakhov int size_changed = 0; 1375362eca70STheodore Ts'o int inline_data = ext4_has_inline_data(inode); 1376c93d8f88SEric Biggers bool verity = ext4_verity_in_progress(inode); 1377ac27a0ecSDave Kleikamp 13789bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 137909cbfeafSKirill A. Shutemov from = pos & (PAGE_SIZE - 1); 1380bfc1af65SNick Piggin to = from + len; 1381bfc1af65SNick Piggin 1382441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1383441c8508SCurt Wohlgemuth 1384362eca70STheodore Ts'o if (inline_data) { 1385eb5efbcbSTheodore Ts'o ret = ext4_write_inline_data_end(inode, pos, len, 13863fdcfb66STao Ma copied, page); 1387eb5efbcbSTheodore Ts'o if (ret < 0) { 1388eb5efbcbSTheodore Ts'o unlock_page(page); 1389eb5efbcbSTheodore Ts'o put_page(page); 1390eb5efbcbSTheodore Ts'o goto errout; 1391eb5efbcbSTheodore Ts'o } 1392eb5efbcbSTheodore Ts'o copied = ret; 1393eb5efbcbSTheodore Ts'o } else if (unlikely(copied < len) && !PageUptodate(page)) { 1394bfc1af65SNick Piggin copied = 0; 13953b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, from, to); 13963b136499SJan Kara } else { 13973b136499SJan Kara if (unlikely(copied < len)) 13983b136499SJan Kara ext4_journalled_zero_new_buffers(handle, page, 13993b136499SJan Kara from + copied, to); 1400f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 14013b136499SJan Kara from + copied, &partial, 14023b136499SJan Kara write_end_fn); 1403ac27a0ecSDave Kleikamp if (!partial) 1404ac27a0ecSDave Kleikamp SetPageUptodate(page); 14053fdcfb66STao Ma } 1406c93d8f88SEric Biggers if (!verity) 14074631dbf6SDmitry Monakhov size_changed = ext4_update_inode_size(inode, pos + copied); 140819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 14092d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 14104631dbf6SDmitry Monakhov unlock_page(page); 141109cbfeafSKirill A. Shutemov put_page(page); 14124631dbf6SDmitry Monakhov 1413c93d8f88SEric Biggers if (old_size < pos && !verity) 14140572639fSXiaoguang Wang pagecache_isize_extended(inode, old_size, pos); 14150572639fSXiaoguang Wang 1416362eca70STheodore Ts'o if (size_changed || inline_data) { 1417617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1418ac27a0ecSDave Kleikamp if (!ret) 1419ac27a0ecSDave Kleikamp ret = ret2; 1420ac27a0ecSDave Kleikamp } 1421bfc1af65SNick Piggin 1422c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode)) 1423f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1424f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1425f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1426f8514083SAneesh Kumar K.V */ 1427f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1428f8514083SAneesh Kumar K.V 1429eb5efbcbSTheodore Ts'o errout: 1430617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1431ac27a0ecSDave Kleikamp if (!ret) 1432ac27a0ecSDave Kleikamp ret = ret2; 1433c93d8f88SEric Biggers if (pos + len > inode->i_size && !verity) { 1434b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1435f8514083SAneesh Kumar K.V /* 1436ffacfa7aSJan Kara * If truncate failed early the inode might still be 1437f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1438f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1439f8514083SAneesh Kumar K.V */ 1440f8514083SAneesh Kumar K.V if (inode->i_nlink) 1441f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1442f8514083SAneesh Kumar K.V } 1443bfc1af65SNick Piggin 1444bfc1af65SNick Piggin return ret ? ret : copied; 1445ac27a0ecSDave Kleikamp } 1446d2a17637SMingming Cao 14479d0be502STheodore Ts'o /* 1448c27e43a1SEric Whitney * Reserve space for a single cluster 14499d0be502STheodore Ts'o */ 1450c27e43a1SEric Whitney static int ext4_da_reserve_space(struct inode *inode) 1451d2a17637SMingming Cao { 1452d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14530637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 14545dd4056dSChristoph Hellwig int ret; 1455d2a17637SMingming Cao 145660e58e0fSMingming Cao /* 145772b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 145872b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 145972b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 146060e58e0fSMingming Cao */ 14617b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 14625dd4056dSChristoph Hellwig if (ret) 14635dd4056dSChristoph Hellwig return ret; 146403179fe9STheodore Ts'o 146503179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 146671d4f7d0STheodore Ts'o if (ext4_claim_free_clusters(sbi, 1, 0)) { 146703179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 146803179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1469d2a17637SMingming Cao return -ENOSPC; 1470d2a17637SMingming Cao } 14719d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 1472c27e43a1SEric Whitney trace_ext4_da_reserve_space(inode); 14730637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 147439bc680aSDmitry Monakhov 1475d2a17637SMingming Cao return 0; /* success */ 1476d2a17637SMingming Cao } 1477d2a17637SMingming Cao 1478f456767dSEric Whitney void ext4_da_release_space(struct inode *inode, int to_free) 1479d2a17637SMingming Cao { 1480d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 14810637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1482d2a17637SMingming Cao 1483cd213226SMingming Cao if (!to_free) 1484cd213226SMingming Cao return; /* Nothing to release, exit */ 1485cd213226SMingming Cao 1486d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1487cd213226SMingming Cao 14885a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 14890637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1490cd213226SMingming Cao /* 14910637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 14920637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 14930637c6f4STheodore Ts'o * function is called from invalidate page, it's 14940637c6f4STheodore Ts'o * harmless to return without any action. 1495cd213226SMingming Cao */ 14968de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 14970637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 14981084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 14990637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 15000637c6f4STheodore Ts'o WARN_ON(1); 15010637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 15020637c6f4STheodore Ts'o } 15030637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 15040637c6f4STheodore Ts'o 150572b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 150657042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1507d2a17637SMingming Cao 1508d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 150960e58e0fSMingming Cao 15107b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1511d2a17637SMingming Cao } 1512d2a17637SMingming Cao 1513ac27a0ecSDave Kleikamp /* 151464769240SAlex Tomas * Delayed allocation stuff 151564769240SAlex Tomas */ 151664769240SAlex Tomas 15174e7ea81dSJan Kara struct mpage_da_data { 15184e7ea81dSJan Kara struct inode *inode; 15194e7ea81dSJan Kara struct writeback_control *wbc; 15206b523df4SJan Kara 15214e7ea81dSJan Kara pgoff_t first_page; /* The first page to write */ 15224e7ea81dSJan Kara pgoff_t next_page; /* Current page to examine */ 15234e7ea81dSJan Kara pgoff_t last_page; /* Last page to examine */ 152464769240SAlex Tomas /* 15254e7ea81dSJan Kara * Extent to map - this can be after first_page because that can be 15264e7ea81dSJan Kara * fully mapped. We somewhat abuse m_flags to store whether the extent 15274e7ea81dSJan Kara * is delalloc or unwritten. 152864769240SAlex Tomas */ 15294e7ea81dSJan Kara struct ext4_map_blocks map; 15304e7ea81dSJan Kara struct ext4_io_submit io_submit; /* IO submission data */ 1531dddbd6acSJan Kara unsigned int do_map:1; 15324e7ea81dSJan Kara }; 153364769240SAlex Tomas 15344e7ea81dSJan Kara static void mpage_release_unused_pages(struct mpage_da_data *mpd, 15354e7ea81dSJan Kara bool invalidate) 1536c4a0c46eSAneesh Kumar K.V { 1537c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1538c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1539c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1540c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1541c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 15424e7ea81dSJan Kara 15434e7ea81dSJan Kara /* This is necessary when next_page == 0. */ 15444e7ea81dSJan Kara if (mpd->first_page >= mpd->next_page) 15454e7ea81dSJan Kara return; 1546c4a0c46eSAneesh Kumar K.V 1547c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1548c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 15494e7ea81dSJan Kara if (invalidate) { 15504e7ea81dSJan Kara ext4_lblk_t start, last; 155109cbfeafSKirill A. Shutemov start = index << (PAGE_SHIFT - inode->i_blkbits); 155209cbfeafSKirill A. Shutemov last = end << (PAGE_SHIFT - inode->i_blkbits); 155351865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 15544e7ea81dSJan Kara } 155551865fdaSZheng Liu 155686679820SMel Gorman pagevec_init(&pvec); 1557c4a0c46eSAneesh Kumar K.V while (index <= end) { 1558397162ffSJan Kara nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end); 1559c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1560c4a0c46eSAneesh Kumar K.V break; 1561c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1562c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 15632b85a617SJan Kara 1564c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1565c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 15664e7ea81dSJan Kara if (invalidate) { 15674e800c03Swangguang if (page_mapped(page)) 15684e800c03Swangguang clear_page_dirty_for_io(page); 156909cbfeafSKirill A. Shutemov block_invalidatepage(page, 0, PAGE_SIZE); 1570c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 15714e7ea81dSJan Kara } 1572c4a0c46eSAneesh Kumar K.V unlock_page(page); 1573c4a0c46eSAneesh Kumar K.V } 15749b1d0998SJan Kara pagevec_release(&pvec); 1575c4a0c46eSAneesh Kumar K.V } 1576c4a0c46eSAneesh Kumar K.V } 1577c4a0c46eSAneesh Kumar K.V 1578df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1579df22291fSAneesh Kumar K.V { 1580df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 158192b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1582f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 158392b97816STheodore Ts'o 158492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 15855dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1586f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 158792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 158892b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1589f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 159057042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 159192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1592f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 15937b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 159492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 159592b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1596f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 1597df22291fSAneesh Kumar K.V return; 1598df22291fSAneesh Kumar K.V } 1599df22291fSAneesh Kumar K.V 1600c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 160129fa89d0SAneesh Kumar K.V { 1602c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 160329fa89d0SAneesh Kumar K.V } 160429fa89d0SAneesh Kumar K.V 160564769240SAlex Tomas /* 16060b02f4c0SEric Whitney * ext4_insert_delayed_block - adds a delayed block to the extents status 16070b02f4c0SEric Whitney * tree, incrementing the reserved cluster/block 16080b02f4c0SEric Whitney * count or making a pending reservation 16090b02f4c0SEric Whitney * where needed 16100b02f4c0SEric Whitney * 16110b02f4c0SEric Whitney * @inode - file containing the newly added block 16120b02f4c0SEric Whitney * @lblk - logical block to be added 16130b02f4c0SEric Whitney * 16140b02f4c0SEric Whitney * Returns 0 on success, negative error code on failure. 16150b02f4c0SEric Whitney */ 16160b02f4c0SEric Whitney static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk) 16170b02f4c0SEric Whitney { 16180b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 16190b02f4c0SEric Whitney int ret; 16200b02f4c0SEric Whitney bool allocated = false; 16210b02f4c0SEric Whitney 16220b02f4c0SEric Whitney /* 16230b02f4c0SEric Whitney * If the cluster containing lblk is shared with a delayed, 16240b02f4c0SEric Whitney * written, or unwritten extent in a bigalloc file system, it's 16250b02f4c0SEric Whitney * already been accounted for and does not need to be reserved. 16260b02f4c0SEric Whitney * A pending reservation must be made for the cluster if it's 16270b02f4c0SEric Whitney * shared with a written or unwritten extent and doesn't already 16280b02f4c0SEric Whitney * have one. Written and unwritten extents can be purged from the 16290b02f4c0SEric Whitney * extents status tree if the system is under memory pressure, so 16300b02f4c0SEric Whitney * it's necessary to examine the extent tree if a search of the 16310b02f4c0SEric Whitney * extents status tree doesn't get a match. 16320b02f4c0SEric Whitney */ 16330b02f4c0SEric Whitney if (sbi->s_cluster_ratio == 1) { 16340b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16350b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16360b02f4c0SEric Whitney goto errout; 16370b02f4c0SEric Whitney } else { /* bigalloc */ 16380b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) { 16390b02f4c0SEric Whitney if (!ext4_es_scan_clu(inode, 16400b02f4c0SEric Whitney &ext4_es_is_mapped, lblk)) { 16410b02f4c0SEric Whitney ret = ext4_clu_mapped(inode, 16420b02f4c0SEric Whitney EXT4_B2C(sbi, lblk)); 16430b02f4c0SEric Whitney if (ret < 0) 16440b02f4c0SEric Whitney goto errout; 16450b02f4c0SEric Whitney if (ret == 0) { 16460b02f4c0SEric Whitney ret = ext4_da_reserve_space(inode); 16470b02f4c0SEric Whitney if (ret != 0) /* ENOSPC */ 16480b02f4c0SEric Whitney goto errout; 16490b02f4c0SEric Whitney } else { 16500b02f4c0SEric Whitney allocated = true; 16510b02f4c0SEric Whitney } 16520b02f4c0SEric Whitney } else { 16530b02f4c0SEric Whitney allocated = true; 16540b02f4c0SEric Whitney } 16550b02f4c0SEric Whitney } 16560b02f4c0SEric Whitney } 16570b02f4c0SEric Whitney 16580b02f4c0SEric Whitney ret = ext4_es_insert_delayed_block(inode, lblk, allocated); 16590b02f4c0SEric Whitney 16600b02f4c0SEric Whitney errout: 16610b02f4c0SEric Whitney return ret; 16620b02f4c0SEric Whitney } 16630b02f4c0SEric Whitney 16640b02f4c0SEric Whitney /* 16655356f261SAditya Kali * This function is grabs code from the very beginning of 16665356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 16675356f261SAditya Kali * time. This function looks up the requested blocks and sets the 16685356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 16695356f261SAditya Kali */ 16705356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 16715356f261SAditya Kali struct ext4_map_blocks *map, 16725356f261SAditya Kali struct buffer_head *bh) 16735356f261SAditya Kali { 1674d100eef2SZheng Liu struct extent_status es; 16755356f261SAditya Kali int retval; 16765356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1677921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1678921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1679921f266bSDmitry Monakhov 1680921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1681921f266bSDmitry Monakhov #endif 16825356f261SAditya Kali 16835356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 16845356f261SAditya Kali invalid_block = ~0; 16855356f261SAditya Kali 16865356f261SAditya Kali map->m_flags = 0; 16875356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 16885356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 16895356f261SAditya Kali (unsigned long) map->m_lblk); 1690d100eef2SZheng Liu 1691d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1692bb5835edSTheodore Ts'o if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { 1693d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1694d100eef2SZheng Liu retval = 0; 1695c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1696d100eef2SZheng Liu goto add_delayed; 1697d100eef2SZheng Liu } 1698d100eef2SZheng Liu 1699d100eef2SZheng Liu /* 1700d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1701d100eef2SZheng Liu * So we need to check it. 1702d100eef2SZheng Liu */ 1703d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1704d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1705d100eef2SZheng Liu set_buffer_new(bh); 1706d100eef2SZheng Liu set_buffer_delay(bh); 1707d100eef2SZheng Liu return 0; 1708d100eef2SZheng Liu } 1709d100eef2SZheng Liu 1710d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1711d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1712d100eef2SZheng Liu if (retval > map->m_len) 1713d100eef2SZheng Liu retval = map->m_len; 1714d100eef2SZheng Liu map->m_len = retval; 1715d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1716d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1717d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1718d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1719d100eef2SZheng Liu else 17201e83bc81SArnd Bergmann BUG(); 1721d100eef2SZheng Liu 1722921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1723921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1724921f266bSDmitry Monakhov #endif 1725d100eef2SZheng Liu return retval; 1726d100eef2SZheng Liu } 1727d100eef2SZheng Liu 17285356f261SAditya Kali /* 17295356f261SAditya Kali * Try to see if we can get the block without requesting a new 17305356f261SAditya Kali * file system block. 17315356f261SAditya Kali */ 1732c8b459f4SLukas Czerner down_read(&EXT4_I(inode)->i_data_sem); 1733cbd7584eSJan Kara if (ext4_has_inline_data(inode)) 17349c3569b5STao Ma retval = 0; 1735cbd7584eSJan Kara else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 17362f8e0a7cSZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 0); 17375356f261SAditya Kali else 17382f8e0a7cSZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 0); 17395356f261SAditya Kali 1740d100eef2SZheng Liu add_delayed: 17415356f261SAditya Kali if (retval == 0) { 1742f7fec032SZheng Liu int ret; 1743ad431025SEric Whitney 17445356f261SAditya Kali /* 17455356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 17465356f261SAditya Kali * is it OK? 17475356f261SAditya Kali */ 17485356f261SAditya Kali 17490b02f4c0SEric Whitney ret = ext4_insert_delayed_block(inode, map->m_lblk); 17500b02f4c0SEric Whitney if (ret != 0) { 1751f7fec032SZheng Liu retval = ret; 175251865fdaSZheng Liu goto out_unlock; 1753f7fec032SZheng Liu } 175451865fdaSZheng Liu 17555356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 17565356f261SAditya Kali set_buffer_new(bh); 17575356f261SAditya Kali set_buffer_delay(bh); 1758f7fec032SZheng Liu } else if (retval > 0) { 1759f7fec032SZheng Liu int ret; 17603be78c73STheodore Ts'o unsigned int status; 1761f7fec032SZheng Liu 176244fb851dSZheng Liu if (unlikely(retval != map->m_len)) { 176344fb851dSZheng Liu ext4_warning(inode->i_sb, 176444fb851dSZheng Liu "ES len assertion failed for inode " 176544fb851dSZheng Liu "%lu: retval %d != map->m_len %d", 176644fb851dSZheng Liu inode->i_ino, retval, map->m_len); 176744fb851dSZheng Liu WARN_ON(1); 1768921f266bSDmitry Monakhov } 1769921f266bSDmitry Monakhov 1770f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1771f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1772f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1773f7fec032SZheng Liu map->m_pblk, status); 1774f7fec032SZheng Liu if (ret != 0) 1775f7fec032SZheng Liu retval = ret; 17765356f261SAditya Kali } 17775356f261SAditya Kali 17785356f261SAditya Kali out_unlock: 17795356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 17805356f261SAditya Kali 17815356f261SAditya Kali return retval; 17825356f261SAditya Kali } 17835356f261SAditya Kali 17845356f261SAditya Kali /* 1785d91bd2c1SSeunghun Lee * This is a special get_block_t callback which is used by 1786b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 1787b920c755STheodore Ts'o * reserve space for a single block. 178829fa89d0SAneesh Kumar K.V * 178929fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 179029fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 179129fa89d0SAneesh Kumar K.V * 179229fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 179329fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 179429fa89d0SAneesh Kumar K.V * initialized properly. 179564769240SAlex Tomas */ 17969c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 17972ed88685STheodore Ts'o struct buffer_head *bh, int create) 179864769240SAlex Tomas { 17992ed88685STheodore Ts'o struct ext4_map_blocks map; 180064769240SAlex Tomas int ret = 0; 180164769240SAlex Tomas 180264769240SAlex Tomas BUG_ON(create == 0); 18032ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 18042ed88685STheodore Ts'o 18052ed88685STheodore Ts'o map.m_lblk = iblock; 18062ed88685STheodore Ts'o map.m_len = 1; 180764769240SAlex Tomas 180864769240SAlex Tomas /* 180964769240SAlex Tomas * first, we need to know whether the block is allocated already 181064769240SAlex Tomas * preallocated blocks are unmapped but should treated 181164769240SAlex Tomas * the same as allocated blocks. 181264769240SAlex Tomas */ 18135356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 18145356f261SAditya Kali if (ret <= 0) 18152ed88685STheodore Ts'o return ret; 181664769240SAlex Tomas 18172ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 1818ed8ad838SJan Kara ext4_update_bh_state(bh, map.m_flags); 18192ed88685STheodore Ts'o 18202ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 18212ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 18222ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 18232ed88685STheodore Ts'o * get_block multiple times when we write to the same 18242ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 18252ed88685STheodore Ts'o * for partial write. 18262ed88685STheodore Ts'o */ 18272ed88685STheodore Ts'o set_buffer_new(bh); 1828c8205636STheodore Ts'o set_buffer_mapped(bh); 18292ed88685STheodore Ts'o } 18302ed88685STheodore Ts'o return 0; 183164769240SAlex Tomas } 183261628a3fSMingming Cao 183362e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 183462e086beSAneesh Kumar K.V { 183562e086beSAneesh Kumar K.V get_bh(bh); 183662e086beSAneesh Kumar K.V return 0; 183762e086beSAneesh Kumar K.V } 183862e086beSAneesh Kumar K.V 183962e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 184062e086beSAneesh Kumar K.V { 184162e086beSAneesh Kumar K.V put_bh(bh); 184262e086beSAneesh Kumar K.V return 0; 184362e086beSAneesh Kumar K.V } 184462e086beSAneesh Kumar K.V 184562e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 184662e086beSAneesh Kumar K.V unsigned int len) 184762e086beSAneesh Kumar K.V { 184862e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 184962e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 18503fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 185162e086beSAneesh Kumar K.V handle_t *handle = NULL; 18523fdcfb66STao Ma int ret = 0, err = 0; 18533fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 18543fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 185562e086beSAneesh Kumar K.V 1856cb20d518STheodore Ts'o ClearPageChecked(page); 18573fdcfb66STao Ma 18583fdcfb66STao Ma if (inline_data) { 18593fdcfb66STao Ma BUG_ON(page->index != 0); 18603fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 18613fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 18623fdcfb66STao Ma if (inode_bh == NULL) 18633fdcfb66STao Ma goto out; 18643fdcfb66STao Ma } else { 186562e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 18663fdcfb66STao Ma if (!page_bufs) { 18673fdcfb66STao Ma BUG(); 18683fdcfb66STao Ma goto out; 18693fdcfb66STao Ma } 18703fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 18713fdcfb66STao Ma NULL, bget_one); 18723fdcfb66STao Ma } 1873bdf96838STheodore Ts'o /* 1874bdf96838STheodore Ts'o * We need to release the page lock before we start the 1875bdf96838STheodore Ts'o * journal, so grab a reference so the page won't disappear 1876bdf96838STheodore Ts'o * out from under us. 1877bdf96838STheodore Ts'o */ 1878bdf96838STheodore Ts'o get_page(page); 187962e086beSAneesh Kumar K.V unlock_page(page); 188062e086beSAneesh Kumar K.V 18819924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 18829924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 188362e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 188462e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 1885bdf96838STheodore Ts'o put_page(page); 1886bdf96838STheodore Ts'o goto out_no_pagelock; 1887bdf96838STheodore Ts'o } 1888bdf96838STheodore Ts'o BUG_ON(!ext4_handle_valid(handle)); 1889bdf96838STheodore Ts'o 1890bdf96838STheodore Ts'o lock_page(page); 1891bdf96838STheodore Ts'o put_page(page); 1892bdf96838STheodore Ts'o if (page->mapping != mapping) { 1893bdf96838STheodore Ts'o /* The page got truncated from under us */ 1894bdf96838STheodore Ts'o ext4_journal_stop(handle); 1895bdf96838STheodore Ts'o ret = 0; 189662e086beSAneesh Kumar K.V goto out; 189762e086beSAneesh Kumar K.V } 189862e086beSAneesh Kumar K.V 18993fdcfb66STao Ma if (inline_data) { 1900362eca70STheodore Ts'o ret = ext4_mark_inode_dirty(handle, inode); 19013fdcfb66STao Ma } else { 1902f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190362e086beSAneesh Kumar K.V do_journal_get_write_access); 190462e086beSAneesh Kumar K.V 1905f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 190662e086beSAneesh Kumar K.V write_end_fn); 19073fdcfb66STao Ma } 190862e086beSAneesh Kumar K.V if (ret == 0) 190962e086beSAneesh Kumar K.V ret = err; 19102d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 191162e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 191262e086beSAneesh Kumar K.V if (!ret) 191362e086beSAneesh Kumar K.V ret = err; 191462e086beSAneesh Kumar K.V 19153fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 19168c9367fdSTheodore Ts'o ext4_walk_page_buffers(NULL, page_bufs, 0, len, 19173fdcfb66STao Ma NULL, bput_one); 191819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 191962e086beSAneesh Kumar K.V out: 1920bdf96838STheodore Ts'o unlock_page(page); 1921bdf96838STheodore Ts'o out_no_pagelock: 19223fdcfb66STao Ma brelse(inode_bh); 192362e086beSAneesh Kumar K.V return ret; 192462e086beSAneesh Kumar K.V } 192562e086beSAneesh Kumar K.V 192661628a3fSMingming Cao /* 192743ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 192843ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 192943ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 193043ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 193143ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 193243ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 193343ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 193443ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 193543ce1d23SAneesh Kumar K.V * 1936b920c755STheodore Ts'o * This function can get called via... 193720970ba6STheodore Ts'o * - ext4_writepages after taking page lock (have journal handle) 1938b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 1939f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1940b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 194143ce1d23SAneesh Kumar K.V * 194243ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 194343ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 194443ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 194543ce1d23SAneesh Kumar K.V * truncate(f, 1024); 194643ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 194743ce1d23SAneesh Kumar K.V * a[0] = 'a'; 194843ce1d23SAneesh Kumar K.V * truncate(f, 4096); 194943ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 195090802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 195143ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 195243ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 195343ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 195443ce1d23SAneesh Kumar K.V * buffer_heads mapped. 195543ce1d23SAneesh Kumar K.V * 195643ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 195743ce1d23SAneesh Kumar K.V * unwritten in the page. 195843ce1d23SAneesh Kumar K.V * 195943ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 196043ce1d23SAneesh Kumar K.V * 196143ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 196243ce1d23SAneesh Kumar K.V * ext4_writepage() 196343ce1d23SAneesh Kumar K.V * 196443ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 196543ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 196661628a3fSMingming Cao */ 196743ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 196864769240SAlex Tomas struct writeback_control *wbc) 196964769240SAlex Tomas { 1970f8bec370SJan Kara int ret = 0; 197161628a3fSMingming Cao loff_t size; 1972498e5f24STheodore Ts'o unsigned int len; 1973744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 197461628a3fSMingming Cao struct inode *inode = page->mapping->host; 197536ade451SJan Kara struct ext4_io_submit io_submit; 19761c8349a1SNamjae Jeon bool keep_towrite = false; 197764769240SAlex Tomas 19780db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 19790db1ff22STheodore Ts'o ext4_invalidatepage(page, 0, PAGE_SIZE); 19800db1ff22STheodore Ts'o unlock_page(page); 19810db1ff22STheodore Ts'o return -EIO; 19820db1ff22STheodore Ts'o } 19830db1ff22STheodore Ts'o 1984a9c667f8SLukas Czerner trace_ext4_writepage(page); 198561628a3fSMingming Cao size = i_size_read(inode); 1986c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 1987c93d8f88SEric Biggers !ext4_verity_in_progress(inode)) 198809cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 198961628a3fSMingming Cao else 199009cbfeafSKirill A. Shutemov len = PAGE_SIZE; 199161628a3fSMingming Cao 1992f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 199364769240SAlex Tomas /* 1994fe386132SJan Kara * We cannot do block allocation or other extent handling in this 1995fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 1996fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 1997fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 1998fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 1999cccd147aSTheodore Ts'o * 2000cccd147aSTheodore Ts'o * Also, if there is only one buffer per page (the fs block 2001cccd147aSTheodore Ts'o * size == the page size), if one buffer needs block 2002cccd147aSTheodore Ts'o * allocation or needs to modify the extent tree to clear the 2003cccd147aSTheodore Ts'o * unwritten flag, we know that the page can't be written at 2004cccd147aSTheodore Ts'o * all, so we might as well refuse the write immediately. 2005cccd147aSTheodore Ts'o * Unfortunately if the block size != page size, we can't as 2006cccd147aSTheodore Ts'o * easily detect this case using ext4_walk_page_buffers(), but 2007cccd147aSTheodore Ts'o * for the extremely common case, this is an optimization that 2008cccd147aSTheodore Ts'o * skips a useless round trip through ext4_bio_write_page(). 200964769240SAlex Tomas */ 2010f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2011c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 201261628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2013cccd147aSTheodore Ts'o if ((current->flags & PF_MEMALLOC) || 201409cbfeafSKirill A. Shutemov (inode->i_sb->s_blocksize == PAGE_SIZE)) { 2015fe386132SJan Kara /* 2016fe386132SJan Kara * For memory cleaning there's no point in writing only 2017fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2018fe386132SJan Kara * from direct reclaim. 2019fe386132SJan Kara */ 2020fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2021fe386132SJan Kara == PF_MEMALLOC); 202261628a3fSMingming Cao unlock_page(page); 202361628a3fSMingming Cao return 0; 202461628a3fSMingming Cao } 20251c8349a1SNamjae Jeon keep_towrite = true; 2026f0e6c985SAneesh Kumar K.V } 202764769240SAlex Tomas 2028cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 202943ce1d23SAneesh Kumar K.V /* 203043ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 203143ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 203243ce1d23SAneesh Kumar K.V */ 20333f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 203443ce1d23SAneesh Kumar K.V 203597a851edSJan Kara ext4_io_submit_init(&io_submit, wbc); 203697a851edSJan Kara io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS); 203797a851edSJan Kara if (!io_submit.io_end) { 203897a851edSJan Kara redirty_page_for_writepage(wbc, page); 203997a851edSJan Kara unlock_page(page); 204097a851edSJan Kara return -ENOMEM; 204197a851edSJan Kara } 20421c8349a1SNamjae Jeon ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite); 204336ade451SJan Kara ext4_io_submit(&io_submit); 204497a851edSJan Kara /* Drop io_end reference we got from init */ 204597a851edSJan Kara ext4_put_io_end_defer(io_submit.io_end); 204664769240SAlex Tomas return ret; 204764769240SAlex Tomas } 204864769240SAlex Tomas 20495f1132b2SJan Kara static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page) 20505f1132b2SJan Kara { 20515f1132b2SJan Kara int len; 2052a056bdaaSJan Kara loff_t size; 20535f1132b2SJan Kara int err; 20545f1132b2SJan Kara 20555f1132b2SJan Kara BUG_ON(page->index != mpd->first_page); 2056a056bdaaSJan Kara clear_page_dirty_for_io(page); 2057a056bdaaSJan Kara /* 2058a056bdaaSJan Kara * We have to be very careful here! Nothing protects writeback path 2059a056bdaaSJan Kara * against i_size changes and the page can be writeably mapped into 2060a056bdaaSJan Kara * page tables. So an application can be growing i_size and writing 2061a056bdaaSJan Kara * data through mmap while writeback runs. clear_page_dirty_for_io() 2062a056bdaaSJan Kara * write-protects our page in page tables and the page cannot get 2063a056bdaaSJan Kara * written to again until we release page lock. So only after 2064a056bdaaSJan Kara * clear_page_dirty_for_io() we are safe to sample i_size for 2065a056bdaaSJan Kara * ext4_bio_write_page() to zero-out tail of the written page. We rely 2066a056bdaaSJan Kara * on the barrier provided by TestClearPageDirty in 2067a056bdaaSJan Kara * clear_page_dirty_for_io() to make sure i_size is really sampled only 2068a056bdaaSJan Kara * after page tables are updated. 2069a056bdaaSJan Kara */ 2070a056bdaaSJan Kara size = i_size_read(mpd->inode); 2071c93d8f88SEric Biggers if (page->index == size >> PAGE_SHIFT && 2072c93d8f88SEric Biggers !ext4_verity_in_progress(mpd->inode)) 207309cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 20745f1132b2SJan Kara else 207509cbfeafSKirill A. Shutemov len = PAGE_SIZE; 20761c8349a1SNamjae Jeon err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false); 20775f1132b2SJan Kara if (!err) 20785f1132b2SJan Kara mpd->wbc->nr_to_write--; 20795f1132b2SJan Kara mpd->first_page++; 20805f1132b2SJan Kara 20815f1132b2SJan Kara return err; 20825f1132b2SJan Kara } 20835f1132b2SJan Kara 20844e7ea81dSJan Kara #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay)) 20854e7ea81dSJan Kara 208661628a3fSMingming Cao /* 2087fffb2739SJan Kara * mballoc gives us at most this number of blocks... 2088fffb2739SJan Kara * XXX: That seems to be only a limitation of ext4_mb_normalize_request(). 2089fffb2739SJan Kara * The rest of mballoc seems to handle chunks up to full group size. 209061628a3fSMingming Cao */ 2091fffb2739SJan Kara #define MAX_WRITEPAGES_EXTENT_LEN 2048 2092525f4ed8SMingming Cao 2093525f4ed8SMingming Cao /* 20944e7ea81dSJan Kara * mpage_add_bh_to_extent - try to add bh to extent of blocks to map 20954e7ea81dSJan Kara * 20964e7ea81dSJan Kara * @mpd - extent of blocks 20974e7ea81dSJan Kara * @lblk - logical number of the block in the file 209809930042SJan Kara * @bh - buffer head we want to add to the extent 20994e7ea81dSJan Kara * 210009930042SJan Kara * The function is used to collect contig. blocks in the same state. If the 210109930042SJan Kara * buffer doesn't require mapping for writeback and we haven't started the 210209930042SJan Kara * extent of buffers to map yet, the function returns 'true' immediately - the 210309930042SJan Kara * caller can write the buffer right away. Otherwise the function returns true 210409930042SJan Kara * if the block has been added to the extent, false if the block couldn't be 210509930042SJan Kara * added. 21064e7ea81dSJan Kara */ 210709930042SJan Kara static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk, 210809930042SJan Kara struct buffer_head *bh) 21094e7ea81dSJan Kara { 21104e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 21114e7ea81dSJan Kara 211209930042SJan Kara /* Buffer that doesn't need mapping for writeback? */ 211309930042SJan Kara if (!buffer_dirty(bh) || !buffer_mapped(bh) || 211409930042SJan Kara (!buffer_delay(bh) && !buffer_unwritten(bh))) { 211509930042SJan Kara /* So far no extent to map => we write the buffer right away */ 211609930042SJan Kara if (map->m_len == 0) 211709930042SJan Kara return true; 211809930042SJan Kara return false; 211909930042SJan Kara } 21204e7ea81dSJan Kara 21214e7ea81dSJan Kara /* First block in the extent? */ 21224e7ea81dSJan Kara if (map->m_len == 0) { 2123dddbd6acSJan Kara /* We cannot map unless handle is started... */ 2124dddbd6acSJan Kara if (!mpd->do_map) 2125dddbd6acSJan Kara return false; 21264e7ea81dSJan Kara map->m_lblk = lblk; 21274e7ea81dSJan Kara map->m_len = 1; 212809930042SJan Kara map->m_flags = bh->b_state & BH_FLAGS; 212909930042SJan Kara return true; 21304e7ea81dSJan Kara } 21314e7ea81dSJan Kara 213209930042SJan Kara /* Don't go larger than mballoc is willing to allocate */ 213309930042SJan Kara if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN) 213409930042SJan Kara return false; 213509930042SJan Kara 21364e7ea81dSJan Kara /* Can we merge the block to our big extent? */ 21374e7ea81dSJan Kara if (lblk == map->m_lblk + map->m_len && 213809930042SJan Kara (bh->b_state & BH_FLAGS) == map->m_flags) { 21394e7ea81dSJan Kara map->m_len++; 214009930042SJan Kara return true; 21414e7ea81dSJan Kara } 214209930042SJan Kara return false; 21434e7ea81dSJan Kara } 21444e7ea81dSJan Kara 21455f1132b2SJan Kara /* 21465f1132b2SJan Kara * mpage_process_page_bufs - submit page buffers for IO or add them to extent 21475f1132b2SJan Kara * 21485f1132b2SJan Kara * @mpd - extent of blocks for mapping 21495f1132b2SJan Kara * @head - the first buffer in the page 21505f1132b2SJan Kara * @bh - buffer we should start processing from 21515f1132b2SJan Kara * @lblk - logical number of the block in the file corresponding to @bh 21525f1132b2SJan Kara * 21535f1132b2SJan Kara * Walk through page buffers from @bh upto @head (exclusive) and either submit 21545f1132b2SJan Kara * the page for IO if all buffers in this page were mapped and there's no 21555f1132b2SJan Kara * accumulated extent of buffers to map or add buffers in the page to the 21565f1132b2SJan Kara * extent of buffers to map. The function returns 1 if the caller can continue 21575f1132b2SJan Kara * by processing the next page, 0 if it should stop adding buffers to the 21585f1132b2SJan Kara * extent to map because we cannot extend it anymore. It can also return value 21595f1132b2SJan Kara * < 0 in case of error during IO submission. 21605f1132b2SJan Kara */ 21615f1132b2SJan Kara static int mpage_process_page_bufs(struct mpage_da_data *mpd, 21624e7ea81dSJan Kara struct buffer_head *head, 21634e7ea81dSJan Kara struct buffer_head *bh, 21644e7ea81dSJan Kara ext4_lblk_t lblk) 21654e7ea81dSJan Kara { 21664e7ea81dSJan Kara struct inode *inode = mpd->inode; 21675f1132b2SJan Kara int err; 216893407472SFabian Frederick ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1) 21694e7ea81dSJan Kara >> inode->i_blkbits; 21704e7ea81dSJan Kara 2171c93d8f88SEric Biggers if (ext4_verity_in_progress(inode)) 2172c93d8f88SEric Biggers blocks = EXT_MAX_BLOCKS; 2173c93d8f88SEric Biggers 21744e7ea81dSJan Kara do { 21754e7ea81dSJan Kara BUG_ON(buffer_locked(bh)); 21764e7ea81dSJan Kara 217709930042SJan Kara if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) { 21784e7ea81dSJan Kara /* Found extent to map? */ 21794e7ea81dSJan Kara if (mpd->map.m_len) 21805f1132b2SJan Kara return 0; 2181dddbd6acSJan Kara /* Buffer needs mapping and handle is not started? */ 2182dddbd6acSJan Kara if (!mpd->do_map) 2183dddbd6acSJan Kara return 0; 218409930042SJan Kara /* Everything mapped so far and we hit EOF */ 21855f1132b2SJan Kara break; 21864e7ea81dSJan Kara } 21874e7ea81dSJan Kara } while (lblk++, (bh = bh->b_this_page) != head); 21885f1132b2SJan Kara /* So far everything mapped? Submit the page for IO. */ 21895f1132b2SJan Kara if (mpd->map.m_len == 0) { 21905f1132b2SJan Kara err = mpage_submit_page(mpd, head->b_page); 21915f1132b2SJan Kara if (err < 0) 21924e7ea81dSJan Kara return err; 21934e7ea81dSJan Kara } 21945f1132b2SJan Kara return lblk < blocks; 21955f1132b2SJan Kara } 21964e7ea81dSJan Kara 21974e7ea81dSJan Kara /* 21982943fdbcSRitesh Harjani * mpage_process_page - update page buffers corresponding to changed extent and 21992943fdbcSRitesh Harjani * may submit fully mapped page for IO 22002943fdbcSRitesh Harjani * 22012943fdbcSRitesh Harjani * @mpd - description of extent to map, on return next extent to map 22022943fdbcSRitesh Harjani * @m_lblk - logical block mapping. 22032943fdbcSRitesh Harjani * @m_pblk - corresponding physical mapping. 22042943fdbcSRitesh Harjani * @map_bh - determines on return whether this page requires any further 22052943fdbcSRitesh Harjani * mapping or not. 22062943fdbcSRitesh Harjani * Scan given page buffers corresponding to changed extent and update buffer 22072943fdbcSRitesh Harjani * state according to new extent state. 22082943fdbcSRitesh Harjani * We map delalloc buffers to their physical location, clear unwritten bits. 22092943fdbcSRitesh Harjani * If the given page is not fully mapped, we update @map to the next extent in 22102943fdbcSRitesh Harjani * the given page that needs mapping & return @map_bh as true. 22112943fdbcSRitesh Harjani */ 22122943fdbcSRitesh Harjani static int mpage_process_page(struct mpage_da_data *mpd, struct page *page, 22132943fdbcSRitesh Harjani ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk, 22142943fdbcSRitesh Harjani bool *map_bh) 22152943fdbcSRitesh Harjani { 22162943fdbcSRitesh Harjani struct buffer_head *head, *bh; 22172943fdbcSRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 22182943fdbcSRitesh Harjani ext4_lblk_t lblk = *m_lblk; 22192943fdbcSRitesh Harjani ext4_fsblk_t pblock = *m_pblk; 22202943fdbcSRitesh Harjani int err = 0; 2221c8cc8816SRitesh Harjani int blkbits = mpd->inode->i_blkbits; 2222c8cc8816SRitesh Harjani ssize_t io_end_size = 0; 2223c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end); 22242943fdbcSRitesh Harjani 22252943fdbcSRitesh Harjani bh = head = page_buffers(page); 22262943fdbcSRitesh Harjani do { 22272943fdbcSRitesh Harjani if (lblk < mpd->map.m_lblk) 22282943fdbcSRitesh Harjani continue; 22292943fdbcSRitesh Harjani if (lblk >= mpd->map.m_lblk + mpd->map.m_len) { 22302943fdbcSRitesh Harjani /* 22312943fdbcSRitesh Harjani * Buffer after end of mapped extent. 22322943fdbcSRitesh Harjani * Find next buffer in the page to map. 22332943fdbcSRitesh Harjani */ 22342943fdbcSRitesh Harjani mpd->map.m_len = 0; 22352943fdbcSRitesh Harjani mpd->map.m_flags = 0; 2236c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2237c8cc8816SRitesh Harjani io_end_size = 0; 22382943fdbcSRitesh Harjani 22392943fdbcSRitesh Harjani err = mpage_process_page_bufs(mpd, head, bh, lblk); 22402943fdbcSRitesh Harjani if (err > 0) 22412943fdbcSRitesh Harjani err = 0; 2242c8cc8816SRitesh Harjani if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) { 2243c8cc8816SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 22444d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) { 22454d06bfb9SRitesh Harjani err = PTR_ERR(io_end_vec); 22464d06bfb9SRitesh Harjani goto out; 22474d06bfb9SRitesh Harjani } 2248c8cc8816SRitesh Harjani io_end_vec->offset = mpd->map.m_lblk << blkbits; 2249c8cc8816SRitesh Harjani } 22502943fdbcSRitesh Harjani *map_bh = true; 22512943fdbcSRitesh Harjani goto out; 22522943fdbcSRitesh Harjani } 22532943fdbcSRitesh Harjani if (buffer_delay(bh)) { 22542943fdbcSRitesh Harjani clear_buffer_delay(bh); 22552943fdbcSRitesh Harjani bh->b_blocknr = pblock++; 22562943fdbcSRitesh Harjani } 22572943fdbcSRitesh Harjani clear_buffer_unwritten(bh); 2258c8cc8816SRitesh Harjani io_end_size += (1 << blkbits); 22592943fdbcSRitesh Harjani } while (lblk++, (bh = bh->b_this_page) != head); 2260c8cc8816SRitesh Harjani 2261c8cc8816SRitesh Harjani io_end_vec->size += io_end_size; 2262c8cc8816SRitesh Harjani io_end_size = 0; 22632943fdbcSRitesh Harjani *map_bh = false; 22642943fdbcSRitesh Harjani out: 22652943fdbcSRitesh Harjani *m_lblk = lblk; 22662943fdbcSRitesh Harjani *m_pblk = pblock; 22672943fdbcSRitesh Harjani return err; 22682943fdbcSRitesh Harjani } 22692943fdbcSRitesh Harjani 22702943fdbcSRitesh Harjani /* 22714e7ea81dSJan Kara * mpage_map_buffers - update buffers corresponding to changed extent and 22724e7ea81dSJan Kara * submit fully mapped pages for IO 22734e7ea81dSJan Kara * 22744e7ea81dSJan Kara * @mpd - description of extent to map, on return next extent to map 22754e7ea81dSJan Kara * 22764e7ea81dSJan Kara * Scan buffers corresponding to changed extent (we expect corresponding pages 22774e7ea81dSJan Kara * to be already locked) and update buffer state according to new extent state. 22784e7ea81dSJan Kara * We map delalloc buffers to their physical location, clear unwritten bits, 2279556615dcSLukas Czerner * and mark buffers as uninit when we perform writes to unwritten extents 22804e7ea81dSJan Kara * and do extent conversion after IO is finished. If the last page is not fully 22814e7ea81dSJan Kara * mapped, we update @map to the next extent in the last page that needs 22824e7ea81dSJan Kara * mapping. Otherwise we submit the page for IO. 22834e7ea81dSJan Kara */ 22844e7ea81dSJan Kara static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd) 22854e7ea81dSJan Kara { 22864e7ea81dSJan Kara struct pagevec pvec; 22874e7ea81dSJan Kara int nr_pages, i; 22884e7ea81dSJan Kara struct inode *inode = mpd->inode; 228909cbfeafSKirill A. Shutemov int bpp_bits = PAGE_SHIFT - inode->i_blkbits; 22904e7ea81dSJan Kara pgoff_t start, end; 22914e7ea81dSJan Kara ext4_lblk_t lblk; 22922943fdbcSRitesh Harjani ext4_fsblk_t pblock; 22934e7ea81dSJan Kara int err; 22942943fdbcSRitesh Harjani bool map_bh = false; 22954e7ea81dSJan Kara 22964e7ea81dSJan Kara start = mpd->map.m_lblk >> bpp_bits; 22974e7ea81dSJan Kara end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits; 22984e7ea81dSJan Kara lblk = start << bpp_bits; 22994e7ea81dSJan Kara pblock = mpd->map.m_pblk; 23004e7ea81dSJan Kara 230186679820SMel Gorman pagevec_init(&pvec); 23024e7ea81dSJan Kara while (start <= end) { 23032b85a617SJan Kara nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, 2304397162ffSJan Kara &start, end); 23054e7ea81dSJan Kara if (nr_pages == 0) 23064e7ea81dSJan Kara break; 23074e7ea81dSJan Kara for (i = 0; i < nr_pages; i++) { 23084e7ea81dSJan Kara struct page *page = pvec.pages[i]; 23094e7ea81dSJan Kara 23102943fdbcSRitesh Harjani err = mpage_process_page(mpd, page, &lblk, &pblock, 23112943fdbcSRitesh Harjani &map_bh); 23124e7ea81dSJan Kara /* 23132943fdbcSRitesh Harjani * If map_bh is true, means page may require further bh 23142943fdbcSRitesh Harjani * mapping, or maybe the page was submitted for IO. 23152943fdbcSRitesh Harjani * So we return to call further extent mapping. 23164e7ea81dSJan Kara */ 23172943fdbcSRitesh Harjani if (err < 0 || map_bh == true) 23182943fdbcSRitesh Harjani goto out; 23194e7ea81dSJan Kara /* Page fully mapped - let IO run! */ 23204e7ea81dSJan Kara err = mpage_submit_page(mpd, page); 23212943fdbcSRitesh Harjani if (err < 0) 23222943fdbcSRitesh Harjani goto out; 23234e7ea81dSJan Kara } 23244e7ea81dSJan Kara pagevec_release(&pvec); 23254e7ea81dSJan Kara } 23264e7ea81dSJan Kara /* Extent fully mapped and matches with page boundary. We are done. */ 23274e7ea81dSJan Kara mpd->map.m_len = 0; 23284e7ea81dSJan Kara mpd->map.m_flags = 0; 23294e7ea81dSJan Kara return 0; 23302943fdbcSRitesh Harjani out: 23312943fdbcSRitesh Harjani pagevec_release(&pvec); 23322943fdbcSRitesh Harjani return err; 23334e7ea81dSJan Kara } 23344e7ea81dSJan Kara 23354e7ea81dSJan Kara static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd) 23364e7ea81dSJan Kara { 23374e7ea81dSJan Kara struct inode *inode = mpd->inode; 23384e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 23394e7ea81dSJan Kara int get_blocks_flags; 2340090f32eeSLukas Czerner int err, dioread_nolock; 23414e7ea81dSJan Kara 23424e7ea81dSJan Kara trace_ext4_da_write_pages_extent(inode, map); 23434e7ea81dSJan Kara /* 23444e7ea81dSJan Kara * Call ext4_map_blocks() to allocate any delayed allocation blocks, or 2345556615dcSLukas Czerner * to convert an unwritten extent to be initialized (in the case 23464e7ea81dSJan Kara * where we have written into one or more preallocated blocks). It is 23474e7ea81dSJan Kara * possible that we're going to need more metadata blocks than 23484e7ea81dSJan Kara * previously reserved. However we must not fail because we're in 23494e7ea81dSJan Kara * writeback and there is nothing we can do about it so it might result 23504e7ea81dSJan Kara * in data loss. So use reserved blocks to allocate metadata if 23514e7ea81dSJan Kara * possible. 23524e7ea81dSJan Kara * 2353754cfed6STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if 2354754cfed6STheodore Ts'o * the blocks in question are delalloc blocks. This indicates 2355754cfed6STheodore Ts'o * that the blocks and quotas has already been checked when 2356754cfed6STheodore Ts'o * the data was copied into the page cache. 23574e7ea81dSJan Kara */ 23584e7ea81dSJan Kara get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 2359ee0876bcSJan Kara EXT4_GET_BLOCKS_METADATA_NOFAIL | 2360ee0876bcSJan Kara EXT4_GET_BLOCKS_IO_SUBMIT; 2361090f32eeSLukas Czerner dioread_nolock = ext4_should_dioread_nolock(inode); 2362090f32eeSLukas Czerner if (dioread_nolock) 23634e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 23644e7ea81dSJan Kara if (map->m_flags & (1 << BH_Delay)) 23654e7ea81dSJan Kara get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 23664e7ea81dSJan Kara 23674e7ea81dSJan Kara err = ext4_map_blocks(handle, inode, map, get_blocks_flags); 23684e7ea81dSJan Kara if (err < 0) 23694e7ea81dSJan Kara return err; 2370090f32eeSLukas Czerner if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) { 23716b523df4SJan Kara if (!mpd->io_submit.io_end->handle && 23726b523df4SJan Kara ext4_handle_valid(handle)) { 23736b523df4SJan Kara mpd->io_submit.io_end->handle = handle->h_rsv_handle; 23746b523df4SJan Kara handle->h_rsv_handle = NULL; 23756b523df4SJan Kara } 23763613d228SJan Kara ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end); 23776b523df4SJan Kara } 23784e7ea81dSJan Kara 23794e7ea81dSJan Kara BUG_ON(map->m_len == 0); 23804e7ea81dSJan Kara return 0; 23814e7ea81dSJan Kara } 23824e7ea81dSJan Kara 23834e7ea81dSJan Kara /* 23844e7ea81dSJan Kara * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length 23854e7ea81dSJan Kara * mpd->len and submit pages underlying it for IO 23864e7ea81dSJan Kara * 23874e7ea81dSJan Kara * @handle - handle for journal operations 23884e7ea81dSJan Kara * @mpd - extent to map 23897534e854SJan Kara * @give_up_on_write - we set this to true iff there is a fatal error and there 23907534e854SJan Kara * is no hope of writing the data. The caller should discard 23917534e854SJan Kara * dirty pages to avoid infinite loops. 23924e7ea81dSJan Kara * 23934e7ea81dSJan Kara * The function maps extent starting at mpd->lblk of length mpd->len. If it is 23944e7ea81dSJan Kara * delayed, blocks are allocated, if it is unwritten, we may need to convert 23954e7ea81dSJan Kara * them to initialized or split the described range from larger unwritten 23964e7ea81dSJan Kara * extent. Note that we need not map all the described range since allocation 23974e7ea81dSJan Kara * can return less blocks or the range is covered by more unwritten extents. We 23984e7ea81dSJan Kara * cannot map more because we are limited by reserved transaction credits. On 23994e7ea81dSJan Kara * the other hand we always make sure that the last touched page is fully 24004e7ea81dSJan Kara * mapped so that it can be written out (and thus forward progress is 24014e7ea81dSJan Kara * guaranteed). After mapping we submit all mapped pages for IO. 24024e7ea81dSJan Kara */ 24034e7ea81dSJan Kara static int mpage_map_and_submit_extent(handle_t *handle, 2404cb530541STheodore Ts'o struct mpage_da_data *mpd, 2405cb530541STheodore Ts'o bool *give_up_on_write) 24064e7ea81dSJan Kara { 24074e7ea81dSJan Kara struct inode *inode = mpd->inode; 24084e7ea81dSJan Kara struct ext4_map_blocks *map = &mpd->map; 24094e7ea81dSJan Kara int err; 24104e7ea81dSJan Kara loff_t disksize; 24116603120eSDmitry Monakhov int progress = 0; 2412c8cc8816SRitesh Harjani ext4_io_end_t *io_end = mpd->io_submit.io_end; 24134d06bfb9SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 24144e7ea81dSJan Kara 24154d06bfb9SRitesh Harjani io_end_vec = ext4_alloc_io_end_vec(io_end); 24164d06bfb9SRitesh Harjani if (IS_ERR(io_end_vec)) 24174d06bfb9SRitesh Harjani return PTR_ERR(io_end_vec); 2418c8cc8816SRitesh Harjani io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits; 241927d7c4edSJan Kara do { 24204e7ea81dSJan Kara err = mpage_map_one_extent(handle, mpd); 24214e7ea81dSJan Kara if (err < 0) { 24224e7ea81dSJan Kara struct super_block *sb = inode->i_sb; 24234e7ea81dSJan Kara 24240db1ff22STheodore Ts'o if (ext4_forced_shutdown(EXT4_SB(sb)) || 24250db1ff22STheodore Ts'o EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED) 2426cb530541STheodore Ts'o goto invalidate_dirty_pages; 24274e7ea81dSJan Kara /* 2428cb530541STheodore Ts'o * Let the uper layers retry transient errors. 2429cb530541STheodore Ts'o * In the case of ENOSPC, if ext4_count_free_blocks() 2430cb530541STheodore Ts'o * is non-zero, a commit should free up blocks. 24314e7ea81dSJan Kara */ 2432cb530541STheodore Ts'o if ((err == -ENOMEM) || 24336603120eSDmitry Monakhov (err == -ENOSPC && ext4_count_free_clusters(sb))) { 24346603120eSDmitry Monakhov if (progress) 24356603120eSDmitry Monakhov goto update_disksize; 2436cb530541STheodore Ts'o return err; 24376603120eSDmitry Monakhov } 24384e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24394e7ea81dSJan Kara "Delayed block allocation failed for " 24404e7ea81dSJan Kara "inode %lu at logical offset %llu with" 24414e7ea81dSJan Kara " max blocks %u with error %d", 24424e7ea81dSJan Kara inode->i_ino, 24434e7ea81dSJan Kara (unsigned long long)map->m_lblk, 2444cb530541STheodore Ts'o (unsigned)map->m_len, -err); 24454e7ea81dSJan Kara ext4_msg(sb, KERN_CRIT, 24464e7ea81dSJan Kara "This should not happen!! Data will " 24474e7ea81dSJan Kara "be lost\n"); 24484e7ea81dSJan Kara if (err == -ENOSPC) 24494e7ea81dSJan Kara ext4_print_free_blocks(inode); 2450cb530541STheodore Ts'o invalidate_dirty_pages: 2451cb530541STheodore Ts'o *give_up_on_write = true; 24524e7ea81dSJan Kara return err; 24534e7ea81dSJan Kara } 24546603120eSDmitry Monakhov progress = 1; 24554e7ea81dSJan Kara /* 24564e7ea81dSJan Kara * Update buffer state, submit mapped pages, and get us new 24574e7ea81dSJan Kara * extent to map 24584e7ea81dSJan Kara */ 24594e7ea81dSJan Kara err = mpage_map_and_submit_buffers(mpd); 24604e7ea81dSJan Kara if (err < 0) 24616603120eSDmitry Monakhov goto update_disksize; 246227d7c4edSJan Kara } while (map->m_len); 24634e7ea81dSJan Kara 24646603120eSDmitry Monakhov update_disksize: 2465622cad13STheodore Ts'o /* 2466622cad13STheodore Ts'o * Update on-disk size after IO is submitted. Races with 2467622cad13STheodore Ts'o * truncate are avoided by checking i_size under i_data_sem. 2468622cad13STheodore Ts'o */ 246909cbfeafSKirill A. Shutemov disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT; 24704e7ea81dSJan Kara if (disksize > EXT4_I(inode)->i_disksize) { 24714e7ea81dSJan Kara int err2; 2472622cad13STheodore Ts'o loff_t i_size; 24734e7ea81dSJan Kara 2474622cad13STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 2475622cad13STheodore Ts'o i_size = i_size_read(inode); 2476622cad13STheodore Ts'o if (disksize > i_size) 2477622cad13STheodore Ts'o disksize = i_size; 2478622cad13STheodore Ts'o if (disksize > EXT4_I(inode)->i_disksize) 2479622cad13STheodore Ts'o EXT4_I(inode)->i_disksize = disksize; 2480622cad13STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 2481b907f2d5STheodore Ts'o err2 = ext4_mark_inode_dirty(handle, inode); 2482*878520acSTheodore Ts'o if (err2) { 2483*878520acSTheodore Ts'o ext4_set_errno(inode->i_sb, -err2); 24844e7ea81dSJan Kara ext4_error(inode->i_sb, 24854e7ea81dSJan Kara "Failed to mark inode %lu dirty", 24864e7ea81dSJan Kara inode->i_ino); 2487*878520acSTheodore Ts'o } 24884e7ea81dSJan Kara if (!err) 24894e7ea81dSJan Kara err = err2; 24904e7ea81dSJan Kara } 24914e7ea81dSJan Kara return err; 24924e7ea81dSJan Kara } 24934e7ea81dSJan Kara 24944e7ea81dSJan Kara /* 2495fffb2739SJan Kara * Calculate the total number of credits to reserve for one writepages 249620970ba6STheodore Ts'o * iteration. This is called from ext4_writepages(). We map an extent of 2497fffb2739SJan Kara * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping 2498fffb2739SJan Kara * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN + 2499fffb2739SJan Kara * bpp - 1 blocks in bpp different extents. 2500525f4ed8SMingming Cao */ 2501fffb2739SJan Kara static int ext4_da_writepages_trans_blocks(struct inode *inode) 2502fffb2739SJan Kara { 2503fffb2739SJan Kara int bpp = ext4_journal_blocks_per_page(inode); 2504525f4ed8SMingming Cao 2505fffb2739SJan Kara return ext4_meta_trans_blocks(inode, 2506fffb2739SJan Kara MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp); 2507525f4ed8SMingming Cao } 250861628a3fSMingming Cao 25098e48dcfbSTheodore Ts'o /* 25104e7ea81dSJan Kara * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages 25114e7ea81dSJan Kara * and underlying extent to map 25124e7ea81dSJan Kara * 25134e7ea81dSJan Kara * @mpd - where to look for pages 25144e7ea81dSJan Kara * 25154e7ea81dSJan Kara * Walk dirty pages in the mapping. If they are fully mapped, submit them for 25164e7ea81dSJan Kara * IO immediately. When we find a page which isn't mapped we start accumulating 25174e7ea81dSJan Kara * extent of buffers underlying these pages that needs mapping (formed by 25184e7ea81dSJan Kara * either delayed or unwritten buffers). We also lock the pages containing 25194e7ea81dSJan Kara * these buffers. The extent found is returned in @mpd structure (starting at 25204e7ea81dSJan Kara * mpd->lblk with length mpd->len blocks). 25214e7ea81dSJan Kara * 25224e7ea81dSJan Kara * Note that this function can attach bios to one io_end structure which are 25234e7ea81dSJan Kara * neither logically nor physically contiguous. Although it may seem as an 25244e7ea81dSJan Kara * unnecessary complication, it is actually inevitable in blocksize < pagesize 25254e7ea81dSJan Kara * case as we need to track IO to all buffers underlying a page in one io_end. 25268e48dcfbSTheodore Ts'o */ 25274e7ea81dSJan Kara static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) 25288e48dcfbSTheodore Ts'o { 25294e7ea81dSJan Kara struct address_space *mapping = mpd->inode->i_mapping; 25308e48dcfbSTheodore Ts'o struct pagevec pvec; 25314f01b02cSTheodore Ts'o unsigned int nr_pages; 2532aeac589aSMing Lei long left = mpd->wbc->nr_to_write; 25334e7ea81dSJan Kara pgoff_t index = mpd->first_page; 25344e7ea81dSJan Kara pgoff_t end = mpd->last_page; 253510bbd235SMatthew Wilcox xa_mark_t tag; 25364e7ea81dSJan Kara int i, err = 0; 25374e7ea81dSJan Kara int blkbits = mpd->inode->i_blkbits; 25384e7ea81dSJan Kara ext4_lblk_t lblk; 25394e7ea81dSJan Kara struct buffer_head *head; 25408e48dcfbSTheodore Ts'o 25414e7ea81dSJan Kara if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages) 25425b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 25435b41d924SEric Sandeen else 25445b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 25455b41d924SEric Sandeen 254686679820SMel Gorman pagevec_init(&pvec); 25474e7ea81dSJan Kara mpd->map.m_len = 0; 25484e7ea81dSJan Kara mpd->next_page = index; 25494f01b02cSTheodore Ts'o while (index <= end) { 2550dc7f3e86SJan Kara nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, 255167fd707fSJan Kara tag); 25528e48dcfbSTheodore Ts'o if (nr_pages == 0) 25534e7ea81dSJan Kara goto out; 25548e48dcfbSTheodore Ts'o 25558e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 25568e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 25578e48dcfbSTheodore Ts'o 25588e48dcfbSTheodore Ts'o /* 2559aeac589aSMing Lei * Accumulated enough dirty pages? This doesn't apply 2560aeac589aSMing Lei * to WB_SYNC_ALL mode. For integrity sync we have to 2561aeac589aSMing Lei * keep going because someone may be concurrently 2562aeac589aSMing Lei * dirtying pages, and we might have synced a lot of 2563aeac589aSMing Lei * newly appeared dirty pages, but have not synced all 2564aeac589aSMing Lei * of the old dirty pages. 2565aeac589aSMing Lei */ 2566aeac589aSMing Lei if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0) 2567aeac589aSMing Lei goto out; 2568aeac589aSMing Lei 25694e7ea81dSJan Kara /* If we can't merge this page, we are done. */ 25704e7ea81dSJan Kara if (mpd->map.m_len > 0 && mpd->next_page != page->index) 25714e7ea81dSJan Kara goto out; 257278aaced3STheodore Ts'o 25738e48dcfbSTheodore Ts'o lock_page(page); 25748e48dcfbSTheodore Ts'o /* 25754e7ea81dSJan Kara * If the page is no longer dirty, or its mapping no 25764e7ea81dSJan Kara * longer corresponds to inode we are writing (which 25774e7ea81dSJan Kara * means it has been truncated or invalidated), or the 25784e7ea81dSJan Kara * page is already under writeback and we are not doing 25794e7ea81dSJan Kara * a data integrity writeback, skip the page 25808e48dcfbSTheodore Ts'o */ 25814f01b02cSTheodore Ts'o if (!PageDirty(page) || 25824f01b02cSTheodore Ts'o (PageWriteback(page) && 25834e7ea81dSJan Kara (mpd->wbc->sync_mode == WB_SYNC_NONE)) || 25844f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 25858e48dcfbSTheodore Ts'o unlock_page(page); 25868e48dcfbSTheodore Ts'o continue; 25878e48dcfbSTheodore Ts'o } 25888e48dcfbSTheodore Ts'o 25898e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 25908e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 25918e48dcfbSTheodore Ts'o 25924e7ea81dSJan Kara if (mpd->map.m_len == 0) 25938eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 25948eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 2595f8bec370SJan Kara /* Add all dirty buffers to mpd */ 25964e7ea81dSJan Kara lblk = ((ext4_lblk_t)page->index) << 259709cbfeafSKirill A. Shutemov (PAGE_SHIFT - blkbits); 25988eb9e5ceSTheodore Ts'o head = page_buffers(page); 25995f1132b2SJan Kara err = mpage_process_page_bufs(mpd, head, head, lblk); 26005f1132b2SJan Kara if (err <= 0) 26014e7ea81dSJan Kara goto out; 26025f1132b2SJan Kara err = 0; 2603aeac589aSMing Lei left--; 26048e48dcfbSTheodore Ts'o } 26058e48dcfbSTheodore Ts'o pagevec_release(&pvec); 26068e48dcfbSTheodore Ts'o cond_resched(); 26078e48dcfbSTheodore Ts'o } 26084f01b02cSTheodore Ts'o return 0; 26098eb9e5ceSTheodore Ts'o out: 26108eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 26114e7ea81dSJan Kara return err; 26128e48dcfbSTheodore Ts'o } 26138e48dcfbSTheodore Ts'o 261420970ba6STheodore Ts'o static int ext4_writepages(struct address_space *mapping, 261564769240SAlex Tomas struct writeback_control *wbc) 261664769240SAlex Tomas { 26174e7ea81dSJan Kara pgoff_t writeback_index = 0; 26184e7ea81dSJan Kara long nr_to_write = wbc->nr_to_write; 261922208dedSAneesh Kumar K.V int range_whole = 0; 26204e7ea81dSJan Kara int cycled = 1; 262161628a3fSMingming Cao handle_t *handle = NULL; 2622df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 26235e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 26246b523df4SJan Kara int needed_blocks, rsv_blocks = 0, ret = 0; 26255e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 26264e7ea81dSJan Kara bool done; 26271bce63d1SShaohua Li struct blk_plug plug; 2628cb530541STheodore Ts'o bool give_up_on_write = false; 262961628a3fSMingming Cao 26300db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 26310db1ff22STheodore Ts'o return -EIO; 26320db1ff22STheodore Ts'o 2633c8585c6fSDaeho Jeong percpu_down_read(&sbi->s_journal_flag_rwsem); 263420970ba6STheodore Ts'o trace_ext4_writepages(inode, wbc); 2635ba80b101STheodore Ts'o 263661628a3fSMingming Cao /* 263761628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 263861628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 263961628a3fSMingming Cao * because that could violate lock ordering on umount 264061628a3fSMingming Cao */ 2641a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2642bbf023c7SMing Lei goto out_writepages; 26432a21e37eSTheodore Ts'o 264420970ba6STheodore Ts'o if (ext4_should_journal_data(inode)) { 2645043d20d1SGoldwyn Rodrigues ret = generic_writepages(mapping, wbc); 2646bbf023c7SMing Lei goto out_writepages; 264720970ba6STheodore Ts'o } 264820970ba6STheodore Ts'o 26492a21e37eSTheodore Ts'o /* 26502a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26512a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26522a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26531751e8a6SLinus Torvalds * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because 26542a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 265520970ba6STheodore Ts'o * read-only, and in that case, ext4_writepages should 26562a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26572a21e37eSTheodore Ts'o * the stack trace. 26582a21e37eSTheodore Ts'o */ 26590db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) || 26600db1ff22STheodore Ts'o sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) { 2661bbf023c7SMing Lei ret = -EROFS; 2662bbf023c7SMing Lei goto out_writepages; 2663bbf023c7SMing Lei } 26642a21e37eSTheodore Ts'o 26654e7ea81dSJan Kara /* 26664e7ea81dSJan Kara * If we have inline data and arrive here, it means that 26674e7ea81dSJan Kara * we will soon create the block for the 1st page, so 26684e7ea81dSJan Kara * we'd better clear the inline data here. 26694e7ea81dSJan Kara */ 26704e7ea81dSJan Kara if (ext4_has_inline_data(inode)) { 26714e7ea81dSJan Kara /* Just inode will be modified... */ 26724e7ea81dSJan Kara handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 26734e7ea81dSJan Kara if (IS_ERR(handle)) { 26744e7ea81dSJan Kara ret = PTR_ERR(handle); 26754e7ea81dSJan Kara goto out_writepages; 26764e7ea81dSJan Kara } 26774e7ea81dSJan Kara BUG_ON(ext4_test_inode_state(inode, 26784e7ea81dSJan Kara EXT4_STATE_MAY_INLINE_DATA)); 26794e7ea81dSJan Kara ext4_destroy_inline_data(handle, inode); 26804e7ea81dSJan Kara ext4_journal_stop(handle); 26814e7ea81dSJan Kara } 26824e7ea81dSJan Kara 26834e343231Syangerkun if (ext4_should_dioread_nolock(inode)) { 26844e343231Syangerkun /* 26854e343231Syangerkun * We may need to convert up to one extent per block in 26864e343231Syangerkun * the page and we may dirty the inode. 26874e343231Syangerkun */ 26884e343231Syangerkun rsv_blocks = 1 + ext4_chunk_trans_blocks(inode, 26894e343231Syangerkun PAGE_SIZE >> inode->i_blkbits); 26904e343231Syangerkun } 26914e343231Syangerkun 269222208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 269322208dedSAneesh Kumar K.V range_whole = 1; 269461628a3fSMingming Cao 26952acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 26964e7ea81dSJan Kara writeback_index = mapping->writeback_index; 26974e7ea81dSJan Kara if (writeback_index) 26982acf2c26SAneesh Kumar K.V cycled = 0; 26994e7ea81dSJan Kara mpd.first_page = writeback_index; 27004e7ea81dSJan Kara mpd.last_page = -1; 27015b41d924SEric Sandeen } else { 270209cbfeafSKirill A. Shutemov mpd.first_page = wbc->range_start >> PAGE_SHIFT; 270309cbfeafSKirill A. Shutemov mpd.last_page = wbc->range_end >> PAGE_SHIFT; 27045b41d924SEric Sandeen } 2705a1d6cc56SAneesh Kumar K.V 27064e7ea81dSJan Kara mpd.inode = inode; 27074e7ea81dSJan Kara mpd.wbc = wbc; 27084e7ea81dSJan Kara ext4_io_submit_init(&mpd.io_submit, wbc); 27092acf2c26SAneesh Kumar K.V retry: 27106e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 27114e7ea81dSJan Kara tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); 27124e7ea81dSJan Kara done = false; 27131bce63d1SShaohua Li blk_start_plug(&plug); 2714dddbd6acSJan Kara 2715dddbd6acSJan Kara /* 2716dddbd6acSJan Kara * First writeback pages that don't need mapping - we can avoid 2717dddbd6acSJan Kara * starting a transaction unnecessarily and also avoid being blocked 2718dddbd6acSJan Kara * in the block layer on device congestion while having transaction 2719dddbd6acSJan Kara * started. 2720dddbd6acSJan Kara */ 2721dddbd6acSJan Kara mpd.do_map = 0; 2722dddbd6acSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2723dddbd6acSJan Kara if (!mpd.io_submit.io_end) { 2724dddbd6acSJan Kara ret = -ENOMEM; 2725dddbd6acSJan Kara goto unplug; 2726dddbd6acSJan Kara } 2727dddbd6acSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 2728a297b2fcSXiaoguang Wang /* Unlock pages we didn't use */ 2729a297b2fcSXiaoguang Wang mpage_release_unused_pages(&mpd, false); 2730dddbd6acSJan Kara /* Submit prepared bio */ 2731dddbd6acSJan Kara ext4_io_submit(&mpd.io_submit); 2732dddbd6acSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2733dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2734dddbd6acSJan Kara if (ret < 0) 2735dddbd6acSJan Kara goto unplug; 2736dddbd6acSJan Kara 27374e7ea81dSJan Kara while (!done && mpd.first_page <= mpd.last_page) { 27384e7ea81dSJan Kara /* For each extent of pages we use new io_end */ 27394e7ea81dSJan Kara mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 27404e7ea81dSJan Kara if (!mpd.io_submit.io_end) { 27414e7ea81dSJan Kara ret = -ENOMEM; 27424e7ea81dSJan Kara break; 27434e7ea81dSJan Kara } 2744a1d6cc56SAneesh Kumar K.V 2745a1d6cc56SAneesh Kumar K.V /* 27464e7ea81dSJan Kara * We have two constraints: We find one extent to map and we 27474e7ea81dSJan Kara * must always write out whole page (makes a difference when 27484e7ea81dSJan Kara * blocksize < pagesize) so that we don't block on IO when we 27494e7ea81dSJan Kara * try to write out the rest of the page. Journalled mode is 27504e7ea81dSJan Kara * not supported by delalloc. 2751a1d6cc56SAneesh Kumar K.V */ 2752a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2753525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2754a1d6cc56SAneesh Kumar K.V 275561628a3fSMingming Cao /* start a new transaction */ 27566b523df4SJan Kara handle = ext4_journal_start_with_reserve(inode, 27576b523df4SJan Kara EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 275861628a3fSMingming Cao if (IS_ERR(handle)) { 275961628a3fSMingming Cao ret = PTR_ERR(handle); 27601693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2761fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2762a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 27634e7ea81dSJan Kara /* Release allocated io_end */ 27644e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2765dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 27664e7ea81dSJan Kara break; 276761628a3fSMingming Cao } 2768dddbd6acSJan Kara mpd.do_map = 1; 2769f63e6005STheodore Ts'o 27704e7ea81dSJan Kara trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 27714e7ea81dSJan Kara ret = mpage_prepare_extent_to_map(&mpd); 27724e7ea81dSJan Kara if (!ret) { 27734e7ea81dSJan Kara if (mpd.map.m_len) 2774cb530541STheodore Ts'o ret = mpage_map_and_submit_extent(handle, &mpd, 2775cb530541STheodore Ts'o &give_up_on_write); 27764e7ea81dSJan Kara else { 2777f63e6005STheodore Ts'o /* 27784e7ea81dSJan Kara * We scanned the whole range (or exhausted 27794e7ea81dSJan Kara * nr_to_write), submitted what was mapped and 27804e7ea81dSJan Kara * didn't find anything needing mapping. We are 27814e7ea81dSJan Kara * done. 2782f63e6005STheodore Ts'o */ 27834e7ea81dSJan Kara done = true; 2784f63e6005STheodore Ts'o } 27854e7ea81dSJan Kara } 2786646caa9cSJan Kara /* 2787646caa9cSJan Kara * Caution: If the handle is synchronous, 2788646caa9cSJan Kara * ext4_journal_stop() can wait for transaction commit 2789646caa9cSJan Kara * to finish which may depend on writeback of pages to 2790646caa9cSJan Kara * complete or on page lock to be released. In that 2791646caa9cSJan Kara * case, we have to wait until after after we have 2792646caa9cSJan Kara * submitted all the IO, released page locks we hold, 2793646caa9cSJan Kara * and dropped io_end reference (for extent conversion 2794646caa9cSJan Kara * to be able to complete) before stopping the handle. 2795646caa9cSJan Kara */ 2796646caa9cSJan Kara if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 279761628a3fSMingming Cao ext4_journal_stop(handle); 2798646caa9cSJan Kara handle = NULL; 2799dddbd6acSJan Kara mpd.do_map = 0; 2800646caa9cSJan Kara } 28014e7ea81dSJan Kara /* Unlock pages we didn't use */ 2802cb530541STheodore Ts'o mpage_release_unused_pages(&mpd, give_up_on_write); 2803a297b2fcSXiaoguang Wang /* Submit prepared bio */ 2804a297b2fcSXiaoguang Wang ext4_io_submit(&mpd.io_submit); 2805a297b2fcSXiaoguang Wang 2806646caa9cSJan Kara /* 2807646caa9cSJan Kara * Drop our io_end reference we got from init. We have 2808646caa9cSJan Kara * to be careful and use deferred io_end finishing if 2809646caa9cSJan Kara * we are still holding the transaction as we can 2810646caa9cSJan Kara * release the last reference to io_end which may end 2811646caa9cSJan Kara * up doing unwritten extent conversion. 2812646caa9cSJan Kara */ 2813646caa9cSJan Kara if (handle) { 2814646caa9cSJan Kara ext4_put_io_end_defer(mpd.io_submit.io_end); 2815646caa9cSJan Kara ext4_journal_stop(handle); 2816646caa9cSJan Kara } else 28174e7ea81dSJan Kara ext4_put_io_end(mpd.io_submit.io_end); 2818dddbd6acSJan Kara mpd.io_submit.io_end = NULL; 2819df22291fSAneesh Kumar K.V 28204e7ea81dSJan Kara if (ret == -ENOSPC && sbi->s_journal) { 28214e7ea81dSJan Kara /* 28224e7ea81dSJan Kara * Commit the transaction which would 282322208dedSAneesh Kumar K.V * free blocks released in the transaction 282422208dedSAneesh Kumar K.V * and try again 282522208dedSAneesh Kumar K.V */ 2826df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 282722208dedSAneesh Kumar K.V ret = 0; 28284e7ea81dSJan Kara continue; 28294e7ea81dSJan Kara } 28304e7ea81dSJan Kara /* Fatal error - ENOMEM, EIO... */ 28314e7ea81dSJan Kara if (ret) 283261628a3fSMingming Cao break; 283361628a3fSMingming Cao } 2834dddbd6acSJan Kara unplug: 28351bce63d1SShaohua Li blk_finish_plug(&plug); 28369c12a831SJan Kara if (!ret && !cycled && wbc->nr_to_write > 0) { 28372acf2c26SAneesh Kumar K.V cycled = 1; 28384e7ea81dSJan Kara mpd.last_page = writeback_index - 1; 28394e7ea81dSJan Kara mpd.first_page = 0; 28402acf2c26SAneesh Kumar K.V goto retry; 28412acf2c26SAneesh Kumar K.V } 284261628a3fSMingming Cao 284322208dedSAneesh Kumar K.V /* Update index */ 284422208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 284522208dedSAneesh Kumar K.V /* 28464e7ea81dSJan Kara * Set the writeback_index so that range_cyclic 284722208dedSAneesh Kumar K.V * mode will write it back later 284822208dedSAneesh Kumar K.V */ 28494e7ea81dSJan Kara mapping->writeback_index = mpd.first_page; 2850a1d6cc56SAneesh Kumar K.V 285161628a3fSMingming Cao out_writepages: 285220970ba6STheodore Ts'o trace_ext4_writepages_result(inode, wbc, ret, 28534e7ea81dSJan Kara nr_to_write - wbc->nr_to_write); 2854c8585c6fSDaeho Jeong percpu_up_read(&sbi->s_journal_flag_rwsem); 285561628a3fSMingming Cao return ret; 285664769240SAlex Tomas } 285764769240SAlex Tomas 28585f0663bbSDan Williams static int ext4_dax_writepages(struct address_space *mapping, 28595f0663bbSDan Williams struct writeback_control *wbc) 28605f0663bbSDan Williams { 28615f0663bbSDan Williams int ret; 28625f0663bbSDan Williams long nr_to_write = wbc->nr_to_write; 28635f0663bbSDan Williams struct inode *inode = mapping->host; 28645f0663bbSDan Williams struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 28655f0663bbSDan Williams 28665f0663bbSDan Williams if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 28675f0663bbSDan Williams return -EIO; 28685f0663bbSDan Williams 28695f0663bbSDan Williams percpu_down_read(&sbi->s_journal_flag_rwsem); 28705f0663bbSDan Williams trace_ext4_writepages(inode, wbc); 28715f0663bbSDan Williams 28725f0663bbSDan Williams ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev, wbc); 28735f0663bbSDan Williams trace_ext4_writepages_result(inode, wbc, ret, 28745f0663bbSDan Williams nr_to_write - wbc->nr_to_write); 28755f0663bbSDan Williams percpu_up_read(&sbi->s_journal_flag_rwsem); 28765f0663bbSDan Williams return ret; 28775f0663bbSDan Williams } 28785f0663bbSDan Williams 287979f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 288079f0be8dSAneesh Kumar K.V { 28815c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 288279f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 288379f0be8dSAneesh Kumar K.V 288479f0be8dSAneesh Kumar K.V /* 288579f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 288679f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2887179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 288879f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 288979f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 289079f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 289179f0be8dSAneesh Kumar K.V */ 28925c1ff336SEric Whitney free_clusters = 28935c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 28945c1ff336SEric Whitney dirty_clusters = 28955c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 289600d4e736STheodore Ts'o /* 289700d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 289800d4e736STheodore Ts'o */ 28995c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 290010ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 290100d4e736STheodore Ts'o 29025c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 29035c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 290479f0be8dSAneesh Kumar K.V /* 2905c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2906c8afb446SEric Sandeen * or free blocks is less than watermark 290779f0be8dSAneesh Kumar K.V */ 290879f0be8dSAneesh Kumar K.V return 1; 290979f0be8dSAneesh Kumar K.V } 291079f0be8dSAneesh Kumar K.V return 0; 291179f0be8dSAneesh Kumar K.V } 291279f0be8dSAneesh Kumar K.V 29130ff8947fSEric Sandeen /* We always reserve for an inode update; the superblock could be there too */ 29140ff8947fSEric Sandeen static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) 29150ff8947fSEric Sandeen { 2916e2b911c5SDarrick J. Wong if (likely(ext4_has_feature_large_file(inode->i_sb))) 29170ff8947fSEric Sandeen return 1; 29180ff8947fSEric Sandeen 29190ff8947fSEric Sandeen if (pos + len <= 0x7fffffffULL) 29200ff8947fSEric Sandeen return 1; 29210ff8947fSEric Sandeen 29220ff8947fSEric Sandeen /* We might need to update the superblock to set LARGE_FILE */ 29230ff8947fSEric Sandeen return 2; 29240ff8947fSEric Sandeen } 29250ff8947fSEric Sandeen 292664769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 292764769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 292864769240SAlex Tomas struct page **pagep, void **fsdata) 292964769240SAlex Tomas { 293072b8ab9dSEric Sandeen int ret, retries = 0; 293164769240SAlex Tomas struct page *page; 293264769240SAlex Tomas pgoff_t index; 293364769240SAlex Tomas struct inode *inode = mapping->host; 293464769240SAlex Tomas handle_t *handle; 293564769240SAlex Tomas 29360db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 29370db1ff22STheodore Ts'o return -EIO; 29380db1ff22STheodore Ts'o 293909cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 294079f0be8dSAneesh Kumar K.V 2941c93d8f88SEric Biggers if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) || 2942c93d8f88SEric Biggers ext4_verity_in_progress(inode)) { 294379f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 294479f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 294579f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 294679f0be8dSAneesh Kumar K.V } 294779f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29489bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 29499c3569b5STao Ma 29509c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 29519c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 29529c3569b5STao Ma pos, len, flags, 29539c3569b5STao Ma pagep, fsdata); 29549c3569b5STao Ma if (ret < 0) 295547564bfbSTheodore Ts'o return ret; 295647564bfbSTheodore Ts'o if (ret == 1) 295747564bfbSTheodore Ts'o return 0; 29589c3569b5STao Ma } 29599c3569b5STao Ma 296047564bfbSTheodore Ts'o /* 296147564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 296247564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 296347564bfbSTheodore Ts'o * is being written back. So grab it first before we start 296447564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 296547564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 296647564bfbSTheodore Ts'o */ 296747564bfbSTheodore Ts'o retry_grab: 296847564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 296947564bfbSTheodore Ts'o if (!page) 297047564bfbSTheodore Ts'o return -ENOMEM; 297147564bfbSTheodore Ts'o unlock_page(page); 297247564bfbSTheodore Ts'o 297364769240SAlex Tomas /* 297464769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 297564769240SAlex Tomas * if there is delayed block allocation. But we still need 297664769240SAlex Tomas * to journalling the i_disksize update if writes to the end 297764769240SAlex Tomas * of file which has an already mapped buffer. 297864769240SAlex Tomas */ 297947564bfbSTheodore Ts'o retry_journal: 29800ff8947fSEric Sandeen handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 29810ff8947fSEric Sandeen ext4_da_write_credits(inode, pos, len)); 298264769240SAlex Tomas if (IS_ERR(handle)) { 298309cbfeafSKirill A. Shutemov put_page(page); 298447564bfbSTheodore Ts'o return PTR_ERR(handle); 298564769240SAlex Tomas } 298664769240SAlex Tomas 298747564bfbSTheodore Ts'o lock_page(page); 298847564bfbSTheodore Ts'o if (page->mapping != mapping) { 298947564bfbSTheodore Ts'o /* The page got truncated from under us */ 299047564bfbSTheodore Ts'o unlock_page(page); 299109cbfeafSKirill A. Shutemov put_page(page); 2992d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 299347564bfbSTheodore Ts'o goto retry_grab; 2994d5a0d4f7SEric Sandeen } 299547564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 29967afe5aa5SDmitry Monakhov wait_for_stable_page(page); 299764769240SAlex Tomas 2998643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 29992058f83aSMichael Halcrow ret = ext4_block_write_begin(page, pos, len, 30002058f83aSMichael Halcrow ext4_da_get_block_prep); 30012058f83aSMichael Halcrow #else 30026e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 30032058f83aSMichael Halcrow #endif 300464769240SAlex Tomas if (ret < 0) { 300564769240SAlex Tomas unlock_page(page); 300664769240SAlex Tomas ext4_journal_stop(handle); 3007ae4d5372SAneesh Kumar K.V /* 3008ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3009ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3010ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3011ae4d5372SAneesh Kumar K.V */ 3012ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3013b9a4207dSJan Kara ext4_truncate_failed_write(inode); 301447564bfbSTheodore Ts'o 301547564bfbSTheodore Ts'o if (ret == -ENOSPC && 301647564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 301747564bfbSTheodore Ts'o goto retry_journal; 301847564bfbSTheodore Ts'o 301909cbfeafSKirill A. Shutemov put_page(page); 302047564bfbSTheodore Ts'o return ret; 302164769240SAlex Tomas } 302264769240SAlex Tomas 302347564bfbSTheodore Ts'o *pagep = page; 302464769240SAlex Tomas return ret; 302564769240SAlex Tomas } 302664769240SAlex Tomas 3027632eaeabSMingming Cao /* 3028632eaeabSMingming Cao * Check if we should update i_disksize 3029632eaeabSMingming Cao * when write to the end of file but not require block allocation 3030632eaeabSMingming Cao */ 3031632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3032632eaeabSMingming Cao unsigned long offset) 3033632eaeabSMingming Cao { 3034632eaeabSMingming Cao struct buffer_head *bh; 3035632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3036632eaeabSMingming Cao unsigned int idx; 3037632eaeabSMingming Cao int i; 3038632eaeabSMingming Cao 3039632eaeabSMingming Cao bh = page_buffers(page); 3040632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3041632eaeabSMingming Cao 3042632eaeabSMingming Cao for (i = 0; i < idx; i++) 3043632eaeabSMingming Cao bh = bh->b_this_page; 3044632eaeabSMingming Cao 304529fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3046632eaeabSMingming Cao return 0; 3047632eaeabSMingming Cao return 1; 3048632eaeabSMingming Cao } 3049632eaeabSMingming Cao 305064769240SAlex Tomas static int ext4_da_write_end(struct file *file, 305164769240SAlex Tomas struct address_space *mapping, 305264769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 305364769240SAlex Tomas struct page *page, void *fsdata) 305464769240SAlex Tomas { 305564769240SAlex Tomas struct inode *inode = mapping->host; 305664769240SAlex Tomas int ret = 0, ret2; 305764769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 305864769240SAlex Tomas loff_t new_i_size; 3059632eaeabSMingming Cao unsigned long start, end; 306079f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 306179f0be8dSAneesh Kumar K.V 306274d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 306374d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 306479f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 3065632eaeabSMingming Cao 30669bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 306709cbfeafSKirill A. Shutemov start = pos & (PAGE_SIZE - 1); 3068632eaeabSMingming Cao end = start + copied - 1; 306964769240SAlex Tomas 307064769240SAlex Tomas /* 307164769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 307264769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 307364769240SAlex Tomas * into that. 307464769240SAlex Tomas */ 307564769240SAlex Tomas new_i_size = pos + copied; 3076ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 30779c3569b5STao Ma if (ext4_has_inline_data(inode) || 30789c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 3079ee124d27SDmitry Monakhov ext4_update_i_disksize(inode, new_i_size); 3080cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3081cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3082cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3083cf17fea6SAneesh Kumar K.V */ 3084cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3085632eaeabSMingming Cao } 3086632eaeabSMingming Cao } 30879c3569b5STao Ma 30889c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 30899c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 30909c3569b5STao Ma ext4_has_inline_data(inode)) 30919c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 30929c3569b5STao Ma page); 30939c3569b5STao Ma else 309464769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 309564769240SAlex Tomas page, fsdata); 30969c3569b5STao Ma 309764769240SAlex Tomas copied = ret2; 309864769240SAlex Tomas if (ret2 < 0) 309964769240SAlex Tomas ret = ret2; 310064769240SAlex Tomas ret2 = ext4_journal_stop(handle); 310164769240SAlex Tomas if (!ret) 310264769240SAlex Tomas ret = ret2; 310364769240SAlex Tomas 310464769240SAlex Tomas return ret ? ret : copied; 310564769240SAlex Tomas } 310664769240SAlex Tomas 3107ccd2506bSTheodore Ts'o /* 3108ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3109ccd2506bSTheodore Ts'o */ 3110ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3111ccd2506bSTheodore Ts'o { 3112fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3113fb40ba0dSTheodore Ts'o 311471d4f7d0STheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks) 3115ccd2506bSTheodore Ts'o return 0; 3116ccd2506bSTheodore Ts'o 3117ccd2506bSTheodore Ts'o /* 3118ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3119ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3120ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3121ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3122ccd2506bSTheodore Ts'o * would require replicating code paths in: 3123ccd2506bSTheodore Ts'o * 312420970ba6STheodore Ts'o * ext4_writepages() -> 3125ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3126ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3127ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3128ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3129ccd2506bSTheodore Ts'o * 3130ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3131ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3132ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3133ccd2506bSTheodore Ts'o * doing I/O at all. 3134ccd2506bSTheodore Ts'o * 3135ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3136380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 3137ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3138ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 313925985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 3140ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3141ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3142ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3143ccd2506bSTheodore Ts'o * 3144ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3145ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3146ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3147ccd2506bSTheodore Ts'o */ 3148ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3149ccd2506bSTheodore Ts'o } 315064769240SAlex Tomas 315164769240SAlex Tomas /* 3152ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3153ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3154ac27a0ecSDave Kleikamp * 3155ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3156617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3157ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3158ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3159ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3160ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3161ac27a0ecSDave Kleikamp * 3162ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3163ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3164ac27a0ecSDave Kleikamp */ 3165617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3166ac27a0ecSDave Kleikamp { 3167ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3168ac27a0ecSDave Kleikamp journal_t *journal; 3169ac27a0ecSDave Kleikamp int err; 3170ac27a0ecSDave Kleikamp 317146c7f254STao Ma /* 317246c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 317346c7f254STao Ma */ 317446c7f254STao Ma if (ext4_has_inline_data(inode)) 317546c7f254STao Ma return 0; 317646c7f254STao Ma 317764769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 317864769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 317964769240SAlex Tomas /* 318064769240SAlex Tomas * With delalloc we want to sync the file 318164769240SAlex Tomas * so that we can make sure we allocate 318264769240SAlex Tomas * blocks for file 318364769240SAlex Tomas */ 318464769240SAlex Tomas filemap_write_and_wait(mapping); 318564769240SAlex Tomas } 318664769240SAlex Tomas 318719f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 318819f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3189ac27a0ecSDave Kleikamp /* 3190ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3191ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3192ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3193ac27a0ecSDave Kleikamp * do we expect this to happen. 3194ac27a0ecSDave Kleikamp * 3195ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3196ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3197ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3198ac27a0ecSDave Kleikamp * will.) 3199ac27a0ecSDave Kleikamp * 3200617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3201ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3202ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3203ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3204ac27a0ecSDave Kleikamp * everything they get. 3205ac27a0ecSDave Kleikamp */ 3206ac27a0ecSDave Kleikamp 320719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3208617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3209dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3210dab291afSMingming Cao err = jbd2_journal_flush(journal); 3211dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3212ac27a0ecSDave Kleikamp 3213ac27a0ecSDave Kleikamp if (err) 3214ac27a0ecSDave Kleikamp return 0; 3215ac27a0ecSDave Kleikamp } 3216ac27a0ecSDave Kleikamp 3217617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3218ac27a0ecSDave Kleikamp } 3219ac27a0ecSDave Kleikamp 3220617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3221ac27a0ecSDave Kleikamp { 322246c7f254STao Ma int ret = -EAGAIN; 322346c7f254STao Ma struct inode *inode = page->mapping->host; 322446c7f254STao Ma 32250562e0baSJiaying Zhang trace_ext4_readpage(page); 322646c7f254STao Ma 322746c7f254STao Ma if (ext4_has_inline_data(inode)) 322846c7f254STao Ma ret = ext4_readpage_inline(inode, page); 322946c7f254STao Ma 323046c7f254STao Ma if (ret == -EAGAIN) 3231ac22b46aSJens Axboe return ext4_mpage_readpages(page->mapping, NULL, page, 1, 3232ac22b46aSJens Axboe false); 323346c7f254STao Ma 323446c7f254STao Ma return ret; 3235ac27a0ecSDave Kleikamp } 3236ac27a0ecSDave Kleikamp 3237ac27a0ecSDave Kleikamp static int 3238617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3239ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3240ac27a0ecSDave Kleikamp { 324146c7f254STao Ma struct inode *inode = mapping->host; 324246c7f254STao Ma 324346c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 324446c7f254STao Ma if (ext4_has_inline_data(inode)) 324546c7f254STao Ma return 0; 324646c7f254STao Ma 3247ac22b46aSJens Axboe return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true); 3248ac27a0ecSDave Kleikamp } 3249ac27a0ecSDave Kleikamp 3250d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 3251d47992f8SLukas Czerner unsigned int length) 3252ac27a0ecSDave Kleikamp { 3253ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 32540562e0baSJiaying Zhang 32554520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 32564520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 32574520fb3cSJan Kara 3258ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 32594520fb3cSJan Kara } 32604520fb3cSJan Kara 326153e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3262ca99fdd2SLukas Czerner unsigned int offset, 3263ca99fdd2SLukas Czerner unsigned int length) 32644520fb3cSJan Kara { 32654520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 32664520fb3cSJan Kara 3267ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 32684520fb3cSJan Kara 3269744692dcSJiaying Zhang /* 3270ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3271ac27a0ecSDave Kleikamp */ 327209cbfeafSKirill A. Shutemov if (offset == 0 && length == PAGE_SIZE) 3273ac27a0ecSDave Kleikamp ClearPageChecked(page); 3274ac27a0ecSDave Kleikamp 3275ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 327653e87268SJan Kara } 327753e87268SJan Kara 327853e87268SJan Kara /* Wrapper for aops... */ 327953e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3280d47992f8SLukas Czerner unsigned int offset, 3281d47992f8SLukas Czerner unsigned int length) 328253e87268SJan Kara { 3283ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3284ac27a0ecSDave Kleikamp } 3285ac27a0ecSDave Kleikamp 3286617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3287ac27a0ecSDave Kleikamp { 3288617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3289ac27a0ecSDave Kleikamp 32900562e0baSJiaying Zhang trace_ext4_releasepage(page); 32910562e0baSJiaying Zhang 3292e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3293e1c36595SJan Kara if (PageChecked(page)) 3294ac27a0ecSDave Kleikamp return 0; 32950390131bSFrank Mayhar if (journal) 3296dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 32970390131bSFrank Mayhar else 32980390131bSFrank Mayhar return try_to_free_buffers(page); 3299ac27a0ecSDave Kleikamp } 3300ac27a0ecSDave Kleikamp 3301b8a6176cSJan Kara static bool ext4_inode_datasync_dirty(struct inode *inode) 3302b8a6176cSJan Kara { 3303b8a6176cSJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 3304b8a6176cSJan Kara 3305b8a6176cSJan Kara if (journal) 3306b8a6176cSJan Kara return !jbd2_transaction_committed(journal, 3307b8a6176cSJan Kara EXT4_I(inode)->i_datasync_tid); 3308b8a6176cSJan Kara /* Any metadata buffers to write? */ 3309b8a6176cSJan Kara if (!list_empty(&inode->i_mapping->private_list)) 3310b8a6176cSJan Kara return true; 3311b8a6176cSJan Kara return inode->i_state & I_DIRTY_DATASYNC; 3312b8a6176cSJan Kara } 3313b8a6176cSJan Kara 3314c8fdfe29SMatthew Bobrowski static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, 3315c8fdfe29SMatthew Bobrowski struct ext4_map_blocks *map, loff_t offset, 3316c8fdfe29SMatthew Bobrowski loff_t length) 3317364443cbSJan Kara { 3318c8fdfe29SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3319c8fdfe29SMatthew Bobrowski 3320c8fdfe29SMatthew Bobrowski /* 3321c8fdfe29SMatthew Bobrowski * Writes that span EOF might trigger an I/O size update on completion, 3322c8fdfe29SMatthew Bobrowski * so consider them to be dirty for the purpose of O_DSYNC, even if 3323c8fdfe29SMatthew Bobrowski * there is no other metadata changes being made or are pending. 3324c8fdfe29SMatthew Bobrowski */ 3325c8fdfe29SMatthew Bobrowski iomap->flags = 0; 3326c8fdfe29SMatthew Bobrowski if (ext4_inode_datasync_dirty(inode) || 3327c8fdfe29SMatthew Bobrowski offset + length > i_size_read(inode)) 3328c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_DIRTY; 3329c8fdfe29SMatthew Bobrowski 3330c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_NEW) 3331c8fdfe29SMatthew Bobrowski iomap->flags |= IOMAP_F_NEW; 3332c8fdfe29SMatthew Bobrowski 3333c8fdfe29SMatthew Bobrowski iomap->bdev = inode->i_sb->s_bdev; 3334c8fdfe29SMatthew Bobrowski iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev; 3335c8fdfe29SMatthew Bobrowski iomap->offset = (u64) map->m_lblk << blkbits; 3336c8fdfe29SMatthew Bobrowski iomap->length = (u64) map->m_len << blkbits; 3337c8fdfe29SMatthew Bobrowski 3338c8fdfe29SMatthew Bobrowski /* 3339c8fdfe29SMatthew Bobrowski * Flags passed to ext4_map_blocks() for direct I/O writes can result 3340c8fdfe29SMatthew Bobrowski * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits 3341c8fdfe29SMatthew Bobrowski * set. In order for any allocated unwritten extents to be converted 3342c8fdfe29SMatthew Bobrowski * into written extents correctly within the ->end_io() handler, we 3343c8fdfe29SMatthew Bobrowski * need to ensure that the iomap->type is set appropriately. Hence, the 3344c8fdfe29SMatthew Bobrowski * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has 3345c8fdfe29SMatthew Bobrowski * been set first. 3346c8fdfe29SMatthew Bobrowski */ 3347c8fdfe29SMatthew Bobrowski if (map->m_flags & EXT4_MAP_UNWRITTEN) { 3348c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_UNWRITTEN; 3349c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3350c8fdfe29SMatthew Bobrowski } else if (map->m_flags & EXT4_MAP_MAPPED) { 3351c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_MAPPED; 3352c8fdfe29SMatthew Bobrowski iomap->addr = (u64) map->m_pblk << blkbits; 3353c8fdfe29SMatthew Bobrowski } else { 3354c8fdfe29SMatthew Bobrowski iomap->type = IOMAP_HOLE; 3355c8fdfe29SMatthew Bobrowski iomap->addr = IOMAP_NULL_ADDR; 3356c8fdfe29SMatthew Bobrowski } 3357c8fdfe29SMatthew Bobrowski } 3358c8fdfe29SMatthew Bobrowski 3359f063db5eSMatthew Bobrowski static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, 3360f063db5eSMatthew Bobrowski unsigned int flags) 3361f063db5eSMatthew Bobrowski { 3362f063db5eSMatthew Bobrowski handle_t *handle; 3363378f32baSMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3364378f32baSMatthew Bobrowski int ret, dio_credits, m_flags = 0, retries = 0; 3365f063db5eSMatthew Bobrowski 3366f063db5eSMatthew Bobrowski /* 3367f063db5eSMatthew Bobrowski * Trim the mapping request to the maximum value that we can map at 3368f063db5eSMatthew Bobrowski * once for direct I/O. 3369f063db5eSMatthew Bobrowski */ 3370f063db5eSMatthew Bobrowski if (map->m_len > DIO_MAX_BLOCKS) 3371f063db5eSMatthew Bobrowski map->m_len = DIO_MAX_BLOCKS; 3372f063db5eSMatthew Bobrowski dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); 3373f063db5eSMatthew Bobrowski 3374f063db5eSMatthew Bobrowski retry: 3375f063db5eSMatthew Bobrowski /* 3376f063db5eSMatthew Bobrowski * Either we allocate blocks and then don't get an unwritten extent, so 3377f063db5eSMatthew Bobrowski * in that case we have reserved enough credits. Or, the blocks are 3378f063db5eSMatthew Bobrowski * already allocated and unwritten. In that case, the extent conversion 3379f063db5eSMatthew Bobrowski * fits into the credits as well. 3380f063db5eSMatthew Bobrowski */ 3381f063db5eSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); 3382f063db5eSMatthew Bobrowski if (IS_ERR(handle)) 3383f063db5eSMatthew Bobrowski return PTR_ERR(handle); 3384f063db5eSMatthew Bobrowski 3385378f32baSMatthew Bobrowski /* 3386378f32baSMatthew Bobrowski * DAX and direct I/O are the only two operations that are currently 3387378f32baSMatthew Bobrowski * supported with IOMAP_WRITE. 3388378f32baSMatthew Bobrowski */ 3389378f32baSMatthew Bobrowski WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT)); 3390378f32baSMatthew Bobrowski if (IS_DAX(inode)) 3391378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE_ZERO; 3392378f32baSMatthew Bobrowski /* 3393378f32baSMatthew Bobrowski * We use i_size instead of i_disksize here because delalloc writeback 3394378f32baSMatthew Bobrowski * can complete at any point during the I/O and subsequently push the 3395378f32baSMatthew Bobrowski * i_disksize out to i_size. This could be beyond where direct I/O is 3396378f32baSMatthew Bobrowski * happening and thus expose allocated blocks to direct I/O reads. 3397378f32baSMatthew Bobrowski */ 3398378f32baSMatthew Bobrowski else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode)) 3399378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_CREATE; 3400378f32baSMatthew Bobrowski else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3401378f32baSMatthew Bobrowski m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT; 3402378f32baSMatthew Bobrowski 3403378f32baSMatthew Bobrowski ret = ext4_map_blocks(handle, inode, map, m_flags); 3404378f32baSMatthew Bobrowski 3405378f32baSMatthew Bobrowski /* 3406378f32baSMatthew Bobrowski * We cannot fill holes in indirect tree based inodes as that could 3407378f32baSMatthew Bobrowski * expose stale data in the case of a crash. Use the magic error code 3408378f32baSMatthew Bobrowski * to fallback to buffered I/O. 3409378f32baSMatthew Bobrowski */ 3410378f32baSMatthew Bobrowski if (!m_flags && !ret) 3411378f32baSMatthew Bobrowski ret = -ENOTBLK; 3412f063db5eSMatthew Bobrowski 3413f063db5eSMatthew Bobrowski ext4_journal_stop(handle); 3414f063db5eSMatthew Bobrowski if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3415f063db5eSMatthew Bobrowski goto retry; 3416f063db5eSMatthew Bobrowski 3417f063db5eSMatthew Bobrowski return ret; 3418f063db5eSMatthew Bobrowski } 3419f063db5eSMatthew Bobrowski 3420f063db5eSMatthew Bobrowski 3421364443cbSJan Kara static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 3422c039b997SGoldwyn Rodrigues unsigned flags, struct iomap *iomap, struct iomap *srcmap) 3423364443cbSJan Kara { 3424364443cbSJan Kara int ret; 342509edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 342609edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 3427364443cbSJan Kara 3428bcd8e91fSTheodore Ts'o if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 3429bcd8e91fSTheodore Ts'o return -EINVAL; 34307046ae35SAndreas Gruenbacher 3431364443cbSJan Kara if (WARN_ON_ONCE(ext4_has_inline_data(inode))) 3432364443cbSJan Kara return -ERANGE; 3433364443cbSJan Kara 343409edf4d3SMatthew Bobrowski /* 343509edf4d3SMatthew Bobrowski * Calculate the first and last logical blocks respectively. 343609edf4d3SMatthew Bobrowski */ 343709edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 343809edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 343909edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 3440364443cbSJan Kara 344109edf4d3SMatthew Bobrowski if (flags & IOMAP_WRITE) 3442f063db5eSMatthew Bobrowski ret = ext4_iomap_alloc(inode, &map, flags); 344309edf4d3SMatthew Bobrowski else 3444545052e9SChristoph Hellwig ret = ext4_map_blocks(NULL, inode, &map, 0); 3445f063db5eSMatthew Bobrowski 3446545052e9SChristoph Hellwig if (ret < 0) 3447545052e9SChristoph Hellwig return ret; 3448364443cbSJan Kara 3449c8fdfe29SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 3450545052e9SChristoph Hellwig 3451364443cbSJan Kara return 0; 3452364443cbSJan Kara } 3453364443cbSJan Kara 3454776722e8SJan Kara static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length, 3455776722e8SJan Kara ssize_t written, unsigned flags, struct iomap *iomap) 3456776722e8SJan Kara { 3457378f32baSMatthew Bobrowski /* 3458378f32baSMatthew Bobrowski * Check to see whether an error occurred while writing out the data to 3459378f32baSMatthew Bobrowski * the allocated blocks. If so, return the magic error code so that we 3460378f32baSMatthew Bobrowski * fallback to buffered I/O and attempt to complete the remainder of 3461378f32baSMatthew Bobrowski * the I/O. Any blocks that may have been allocated in preparation for 3462378f32baSMatthew Bobrowski * the direct I/O will be reused during buffered I/O. 3463378f32baSMatthew Bobrowski */ 3464378f32baSMatthew Bobrowski if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0) 3465378f32baSMatthew Bobrowski return -ENOTBLK; 3466378f32baSMatthew Bobrowski 3467776722e8SJan Kara return 0; 3468776722e8SJan Kara } 3469776722e8SJan Kara 34708ff6daa1SChristoph Hellwig const struct iomap_ops ext4_iomap_ops = { 3471364443cbSJan Kara .iomap_begin = ext4_iomap_begin, 3472776722e8SJan Kara .iomap_end = ext4_iomap_end, 3473364443cbSJan Kara }; 3474364443cbSJan Kara 347509edf4d3SMatthew Bobrowski static bool ext4_iomap_is_delalloc(struct inode *inode, 347609edf4d3SMatthew Bobrowski struct ext4_map_blocks *map) 347709edf4d3SMatthew Bobrowski { 347809edf4d3SMatthew Bobrowski struct extent_status es; 347909edf4d3SMatthew Bobrowski ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1; 348009edf4d3SMatthew Bobrowski 348109edf4d3SMatthew Bobrowski ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 348209edf4d3SMatthew Bobrowski map->m_lblk, end, &es); 348309edf4d3SMatthew Bobrowski 348409edf4d3SMatthew Bobrowski if (!es.es_len || es.es_lblk > end) 348509edf4d3SMatthew Bobrowski return false; 348609edf4d3SMatthew Bobrowski 348709edf4d3SMatthew Bobrowski if (es.es_lblk > map->m_lblk) { 348809edf4d3SMatthew Bobrowski map->m_len = es.es_lblk - map->m_lblk; 348909edf4d3SMatthew Bobrowski return false; 349009edf4d3SMatthew Bobrowski } 349109edf4d3SMatthew Bobrowski 349209edf4d3SMatthew Bobrowski offset = map->m_lblk - es.es_lblk; 349309edf4d3SMatthew Bobrowski map->m_len = es.es_len - offset; 349409edf4d3SMatthew Bobrowski 349509edf4d3SMatthew Bobrowski return true; 349609edf4d3SMatthew Bobrowski } 349709edf4d3SMatthew Bobrowski 349809edf4d3SMatthew Bobrowski static int ext4_iomap_begin_report(struct inode *inode, loff_t offset, 349909edf4d3SMatthew Bobrowski loff_t length, unsigned int flags, 350009edf4d3SMatthew Bobrowski struct iomap *iomap, struct iomap *srcmap) 350109edf4d3SMatthew Bobrowski { 350209edf4d3SMatthew Bobrowski int ret; 350309edf4d3SMatthew Bobrowski bool delalloc = false; 350409edf4d3SMatthew Bobrowski struct ext4_map_blocks map; 350509edf4d3SMatthew Bobrowski u8 blkbits = inode->i_blkbits; 350609edf4d3SMatthew Bobrowski 350709edf4d3SMatthew Bobrowski if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK) 350809edf4d3SMatthew Bobrowski return -EINVAL; 350909edf4d3SMatthew Bobrowski 35107cb476f8SJan Kara if (ext4_has_inline_data(inode)) { 35117cb476f8SJan Kara ret = ext4_inline_data_iomap(inode, iomap); 3512ba5843f5SJan Kara if (ret != -EAGAIN) { 3513ed923b57SMatthew Wilcox if (ret == 0 && offset >= iomap->length) 3514ba5843f5SJan Kara ret = -ENOENT; 3515ba5843f5SJan Kara return ret; 3516ba5843f5SJan Kara } 3517ba5843f5SJan Kara } 351812735f88SJan Kara 351909edf4d3SMatthew Bobrowski /* 352009edf4d3SMatthew Bobrowski * Calculate the first and last logical block respectively. 352109edf4d3SMatthew Bobrowski */ 352209edf4d3SMatthew Bobrowski map.m_lblk = offset >> blkbits; 352309edf4d3SMatthew Bobrowski map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits, 352409edf4d3SMatthew Bobrowski EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1; 352512735f88SJan Kara 352612735f88SJan Kara ret = ext4_map_blocks(NULL, inode, &map, 0); 3527ba5843f5SJan Kara if (ret < 0) 3528ba5843f5SJan Kara return ret; 3529914f82a3SJan Kara if (ret == 0) 353009edf4d3SMatthew Bobrowski delalloc = ext4_iomap_is_delalloc(inode, &map); 353109edf4d3SMatthew Bobrowski 353209edf4d3SMatthew Bobrowski ext4_set_iomap(inode, iomap, &map, offset, length); 353309edf4d3SMatthew Bobrowski if (delalloc && iomap->type == IOMAP_HOLE) 353409edf4d3SMatthew Bobrowski iomap->type = IOMAP_DELALLOC; 353509edf4d3SMatthew Bobrowski 353609edf4d3SMatthew Bobrowski return 0; 3537914f82a3SJan Kara } 3538914f82a3SJan Kara 353909edf4d3SMatthew Bobrowski const struct iomap_ops ext4_iomap_report_ops = { 354009edf4d3SMatthew Bobrowski .iomap_begin = ext4_iomap_begin_report, 354109edf4d3SMatthew Bobrowski }; 35424c0425ffSMingming Cao 3543ac27a0ecSDave Kleikamp /* 3544617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3545ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3546ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3547ac27a0ecSDave Kleikamp * not necessarily locked. 3548ac27a0ecSDave Kleikamp * 3549ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3550ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3551ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3552ac27a0ecSDave Kleikamp * 3553ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3554ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3555ac27a0ecSDave Kleikamp */ 3556617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3557ac27a0ecSDave Kleikamp { 3558ac27a0ecSDave Kleikamp SetPageChecked(page); 3559ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3560ac27a0ecSDave Kleikamp } 3561ac27a0ecSDave Kleikamp 35626dcc693bSJan Kara static int ext4_set_page_dirty(struct page *page) 35636dcc693bSJan Kara { 35646dcc693bSJan Kara WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page)); 35656dcc693bSJan Kara WARN_ON_ONCE(!page_has_buffers(page)); 35666dcc693bSJan Kara return __set_page_dirty_buffers(page); 35676dcc693bSJan Kara } 35686dcc693bSJan Kara 356974d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3570617ba13bSMingming Cao .readpage = ext4_readpage, 3571617ba13bSMingming Cao .readpages = ext4_readpages, 357243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 357320970ba6STheodore Ts'o .writepages = ext4_writepages, 3574bfc1af65SNick Piggin .write_begin = ext4_write_begin, 357574d553aaSTheodore Ts'o .write_end = ext4_write_end, 35766dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 3577617ba13bSMingming Cao .bmap = ext4_bmap, 3578617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3579617ba13bSMingming Cao .releasepage = ext4_releasepage, 3580378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 3581ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 35828ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3583aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3584ac27a0ecSDave Kleikamp }; 3585ac27a0ecSDave Kleikamp 3586617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3587617ba13bSMingming Cao .readpage = ext4_readpage, 3588617ba13bSMingming Cao .readpages = ext4_readpages, 358943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 359020970ba6STheodore Ts'o .writepages = ext4_writepages, 3591bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3592bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3593617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3594617ba13bSMingming Cao .bmap = ext4_bmap, 35954520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3596617ba13bSMingming Cao .releasepage = ext4_releasepage, 3597378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 35988ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3599aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3600ac27a0ecSDave Kleikamp }; 3601ac27a0ecSDave Kleikamp 360264769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 360364769240SAlex Tomas .readpage = ext4_readpage, 360464769240SAlex Tomas .readpages = ext4_readpages, 360543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 360620970ba6STheodore Ts'o .writepages = ext4_writepages, 360764769240SAlex Tomas .write_begin = ext4_da_write_begin, 360864769240SAlex Tomas .write_end = ext4_da_write_end, 36096dcc693bSJan Kara .set_page_dirty = ext4_set_page_dirty, 361064769240SAlex Tomas .bmap = ext4_bmap, 36118fcc3a58SEric Whitney .invalidatepage = ext4_invalidatepage, 361264769240SAlex Tomas .releasepage = ext4_releasepage, 3613378f32baSMatthew Bobrowski .direct_IO = noop_direct_IO, 361464769240SAlex Tomas .migratepage = buffer_migrate_page, 36158ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3616aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 361764769240SAlex Tomas }; 361864769240SAlex Tomas 36195f0663bbSDan Williams static const struct address_space_operations ext4_dax_aops = { 36205f0663bbSDan Williams .writepages = ext4_dax_writepages, 36215f0663bbSDan Williams .direct_IO = noop_direct_IO, 36225f0663bbSDan Williams .set_page_dirty = noop_set_page_dirty, 362394dbb631SToshi Kani .bmap = ext4_bmap, 36245f0663bbSDan Williams .invalidatepage = noop_invalidatepage, 36255f0663bbSDan Williams }; 36265f0663bbSDan Williams 3627617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3628ac27a0ecSDave Kleikamp { 36293d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 36303d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 36313d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 36323d2b1582SLukas Czerner break; 36333d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3634617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 363574d553aaSTheodore Ts'o return; 36363d2b1582SLukas Czerner default: 36373d2b1582SLukas Czerner BUG(); 36383d2b1582SLukas Czerner } 36395f0663bbSDan Williams if (IS_DAX(inode)) 36405f0663bbSDan Williams inode->i_mapping->a_ops = &ext4_dax_aops; 36415f0663bbSDan Williams else if (test_opt(inode->i_sb, DELALLOC)) 364274d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 364374d553aaSTheodore Ts'o else 364474d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3645ac27a0ecSDave Kleikamp } 3646ac27a0ecSDave Kleikamp 3647923ae0ffSRoss Zwisler static int __ext4_block_zero_page_range(handle_t *handle, 3648d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3649d863dc36SLukas Czerner { 365009cbfeafSKirill A. Shutemov ext4_fsblk_t index = from >> PAGE_SHIFT; 365109cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3652923ae0ffSRoss Zwisler unsigned blocksize, pos; 3653d863dc36SLukas Czerner ext4_lblk_t iblock; 3654d863dc36SLukas Czerner struct inode *inode = mapping->host; 3655d863dc36SLukas Czerner struct buffer_head *bh; 3656d863dc36SLukas Czerner struct page *page; 3657d863dc36SLukas Czerner int err = 0; 3658d863dc36SLukas Czerner 365909cbfeafSKirill A. Shutemov page = find_or_create_page(mapping, from >> PAGE_SHIFT, 3660c62d2555SMichal Hocko mapping_gfp_constraint(mapping, ~__GFP_FS)); 3661d863dc36SLukas Czerner if (!page) 3662d863dc36SLukas Czerner return -ENOMEM; 3663d863dc36SLukas Czerner 3664d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3665d863dc36SLukas Czerner 366609cbfeafSKirill A. Shutemov iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 3667d863dc36SLukas Czerner 3668d863dc36SLukas Czerner if (!page_has_buffers(page)) 3669d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3670d863dc36SLukas Czerner 3671d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3672d863dc36SLukas Czerner bh = page_buffers(page); 3673d863dc36SLukas Czerner pos = blocksize; 3674d863dc36SLukas Czerner while (offset >= pos) { 3675d863dc36SLukas Czerner bh = bh->b_this_page; 3676d863dc36SLukas Czerner iblock++; 3677d863dc36SLukas Czerner pos += blocksize; 3678d863dc36SLukas Czerner } 3679d863dc36SLukas Czerner if (buffer_freed(bh)) { 3680d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3681d863dc36SLukas Czerner goto unlock; 3682d863dc36SLukas Czerner } 3683d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3684d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3685d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3686d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3687d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3688d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3689d863dc36SLukas Czerner goto unlock; 3690d863dc36SLukas Czerner } 3691d863dc36SLukas Czerner } 3692d863dc36SLukas Czerner 3693d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3694d863dc36SLukas Czerner if (PageUptodate(page)) 3695d863dc36SLukas Czerner set_buffer_uptodate(bh); 3696d863dc36SLukas Czerner 3697d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 3698d863dc36SLukas Czerner err = -EIO; 3699dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 3700d863dc36SLukas Czerner wait_on_buffer(bh); 3701d863dc36SLukas Czerner /* Uhhuh. Read error. Complain and punt. */ 3702d863dc36SLukas Czerner if (!buffer_uptodate(bh)) 3703d863dc36SLukas Czerner goto unlock; 3704592ddec7SChandan Rajendra if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) { 3705c9c7429cSMichael Halcrow /* We expect the key to be set. */ 3706a7550b30SJaegeuk Kim BUG_ON(!fscrypt_has_encryption_key(inode)); 3707aa8bc1acSEric Biggers WARN_ON_ONCE(fscrypt_decrypt_pagecache_blocks( 3708ec39a368SChandan Rajendra page, blocksize, bh_offset(bh))); 3709c9c7429cSMichael Halcrow } 3710d863dc36SLukas Czerner } 3711d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3712d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3713d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3714d863dc36SLukas Czerner if (err) 3715d863dc36SLukas Czerner goto unlock; 3716d863dc36SLukas Czerner } 3717d863dc36SLukas Czerner zero_user(page, offset, length); 3718d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3719d863dc36SLukas Czerner 3720d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3721d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 37220713ed0cSLukas Czerner } else { 3723353eefd3Sjon ernst err = 0; 3724d863dc36SLukas Czerner mark_buffer_dirty(bh); 37253957ef53SJan Kara if (ext4_should_order_data(inode)) 372673131fbbSRoss Zwisler err = ext4_jbd2_inode_add_write(handle, inode, from, 372773131fbbSRoss Zwisler length); 37280713ed0cSLukas Czerner } 3729d863dc36SLukas Czerner 3730d863dc36SLukas Czerner unlock: 3731d863dc36SLukas Czerner unlock_page(page); 373209cbfeafSKirill A. Shutemov put_page(page); 3733d863dc36SLukas Czerner return err; 3734d863dc36SLukas Czerner } 3735d863dc36SLukas Czerner 373694350ab5SMatthew Wilcox /* 3737923ae0ffSRoss Zwisler * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3738923ae0ffSRoss Zwisler * starting from file offset 'from'. The range to be zero'd must 3739923ae0ffSRoss Zwisler * be contained with in one block. If the specified range exceeds 3740923ae0ffSRoss Zwisler * the end of the block it will be shortened to end of the block 3741923ae0ffSRoss Zwisler * that cooresponds to 'from' 3742923ae0ffSRoss Zwisler */ 3743923ae0ffSRoss Zwisler static int ext4_block_zero_page_range(handle_t *handle, 3744923ae0ffSRoss Zwisler struct address_space *mapping, loff_t from, loff_t length) 3745923ae0ffSRoss Zwisler { 3746923ae0ffSRoss Zwisler struct inode *inode = mapping->host; 374709cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 3748923ae0ffSRoss Zwisler unsigned blocksize = inode->i_sb->s_blocksize; 3749923ae0ffSRoss Zwisler unsigned max = blocksize - (offset & (blocksize - 1)); 3750923ae0ffSRoss Zwisler 3751923ae0ffSRoss Zwisler /* 3752923ae0ffSRoss Zwisler * correct length if it does not fall between 3753923ae0ffSRoss Zwisler * 'from' and the end of the block 3754923ae0ffSRoss Zwisler */ 3755923ae0ffSRoss Zwisler if (length > max || length < 0) 3756923ae0ffSRoss Zwisler length = max; 3757923ae0ffSRoss Zwisler 375847e69351SJan Kara if (IS_DAX(inode)) { 375947e69351SJan Kara return iomap_zero_range(inode, from, length, NULL, 376047e69351SJan Kara &ext4_iomap_ops); 376147e69351SJan Kara } 3762923ae0ffSRoss Zwisler return __ext4_block_zero_page_range(handle, mapping, from, length); 3763923ae0ffSRoss Zwisler } 3764923ae0ffSRoss Zwisler 3765923ae0ffSRoss Zwisler /* 376694350ab5SMatthew Wilcox * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 376794350ab5SMatthew Wilcox * up to the end of the block which corresponds to `from'. 376894350ab5SMatthew Wilcox * This required during truncate. We need to physically zero the tail end 376994350ab5SMatthew Wilcox * of that block so it doesn't yield old data if the file is later grown. 377094350ab5SMatthew Wilcox */ 3771c197855eSStephen Hemminger static int ext4_block_truncate_page(handle_t *handle, 377294350ab5SMatthew Wilcox struct address_space *mapping, loff_t from) 377394350ab5SMatthew Wilcox { 377409cbfeafSKirill A. Shutemov unsigned offset = from & (PAGE_SIZE-1); 377594350ab5SMatthew Wilcox unsigned length; 377694350ab5SMatthew Wilcox unsigned blocksize; 377794350ab5SMatthew Wilcox struct inode *inode = mapping->host; 377894350ab5SMatthew Wilcox 37790d06863fSTheodore Ts'o /* If we are processing an encrypted inode during orphan list handling */ 3780592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode)) 37810d06863fSTheodore Ts'o return 0; 37820d06863fSTheodore Ts'o 378394350ab5SMatthew Wilcox blocksize = inode->i_sb->s_blocksize; 378494350ab5SMatthew Wilcox length = blocksize - (offset & (blocksize - 1)); 378594350ab5SMatthew Wilcox 378694350ab5SMatthew Wilcox return ext4_block_zero_page_range(handle, mapping, from, length); 378794350ab5SMatthew Wilcox } 378894350ab5SMatthew Wilcox 3789a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3790a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3791a87dd18cSLukas Czerner { 3792a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3793a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3794e1be3a92SLukas Czerner unsigned partial_start, partial_end; 3795a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3796a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3797a87dd18cSLukas Czerner int err = 0; 3798a87dd18cSLukas Czerner 3799e1be3a92SLukas Czerner partial_start = lstart & (sb->s_blocksize - 1); 3800e1be3a92SLukas Czerner partial_end = byte_end & (sb->s_blocksize - 1); 3801e1be3a92SLukas Czerner 3802a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3803a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3804a87dd18cSLukas Czerner 3805a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3806e1be3a92SLukas Czerner if (start == end && 3807e1be3a92SLukas Czerner (partial_start || (partial_end != sb->s_blocksize - 1))) { 3808a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3809a87dd18cSLukas Czerner lstart, length); 3810a87dd18cSLukas Czerner return err; 3811a87dd18cSLukas Czerner } 3812a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3813e1be3a92SLukas Czerner if (partial_start) { 3814a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3815a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3816a87dd18cSLukas Czerner if (err) 3817a87dd18cSLukas Czerner return err; 3818a87dd18cSLukas Czerner } 3819a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3820e1be3a92SLukas Czerner if (partial_end != sb->s_blocksize - 1) 3821a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3822e1be3a92SLukas Czerner byte_end - partial_end, 3823e1be3a92SLukas Czerner partial_end + 1); 3824a87dd18cSLukas Czerner return err; 3825a87dd18cSLukas Czerner } 3826a87dd18cSLukas Czerner 382791ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 382891ef4cafSDuane Griffin { 382991ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 383091ef4cafSDuane Griffin return 1; 383191ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 383291ef4cafSDuane Griffin return 1; 383391ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 383491ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 383591ef4cafSDuane Griffin return 0; 383691ef4cafSDuane Griffin } 383791ef4cafSDuane Griffin 3838ac27a0ecSDave Kleikamp /* 383901127848SJan Kara * We have to make sure i_disksize gets properly updated before we truncate 384001127848SJan Kara * page cache due to hole punching or zero range. Otherwise i_disksize update 384101127848SJan Kara * can get lost as it may have been postponed to submission of writeback but 384201127848SJan Kara * that will never happen after we truncate page cache. 384301127848SJan Kara */ 384401127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, 384501127848SJan Kara loff_t len) 384601127848SJan Kara { 384701127848SJan Kara handle_t *handle; 384801127848SJan Kara loff_t size = i_size_read(inode); 384901127848SJan Kara 38505955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 385101127848SJan Kara if (offset > size || offset + len < size) 385201127848SJan Kara return 0; 385301127848SJan Kara 385401127848SJan Kara if (EXT4_I(inode)->i_disksize >= size) 385501127848SJan Kara return 0; 385601127848SJan Kara 385701127848SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 385801127848SJan Kara if (IS_ERR(handle)) 385901127848SJan Kara return PTR_ERR(handle); 386001127848SJan Kara ext4_update_i_disksize(inode, size); 386101127848SJan Kara ext4_mark_inode_dirty(handle, inode); 386201127848SJan Kara ext4_journal_stop(handle); 386301127848SJan Kara 386401127848SJan Kara return 0; 386501127848SJan Kara } 386601127848SJan Kara 3867b1f38217SRoss Zwisler static void ext4_wait_dax_page(struct ext4_inode_info *ei) 3868430657b6SRoss Zwisler { 3869430657b6SRoss Zwisler up_write(&ei->i_mmap_sem); 3870430657b6SRoss Zwisler schedule(); 3871430657b6SRoss Zwisler down_write(&ei->i_mmap_sem); 3872430657b6SRoss Zwisler } 3873430657b6SRoss Zwisler 3874430657b6SRoss Zwisler int ext4_break_layouts(struct inode *inode) 3875430657b6SRoss Zwisler { 3876430657b6SRoss Zwisler struct ext4_inode_info *ei = EXT4_I(inode); 3877430657b6SRoss Zwisler struct page *page; 3878430657b6SRoss Zwisler int error; 3879430657b6SRoss Zwisler 3880430657b6SRoss Zwisler if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem))) 3881430657b6SRoss Zwisler return -EINVAL; 3882430657b6SRoss Zwisler 3883430657b6SRoss Zwisler do { 3884430657b6SRoss Zwisler page = dax_layout_busy_page(inode->i_mapping); 3885430657b6SRoss Zwisler if (!page) 3886430657b6SRoss Zwisler return 0; 3887430657b6SRoss Zwisler 3888430657b6SRoss Zwisler error = ___wait_var_event(&page->_refcount, 3889430657b6SRoss Zwisler atomic_read(&page->_refcount) == 1, 3890430657b6SRoss Zwisler TASK_INTERRUPTIBLE, 0, 0, 3891b1f38217SRoss Zwisler ext4_wait_dax_page(ei)); 3892b1f38217SRoss Zwisler } while (error == 0); 3893430657b6SRoss Zwisler 3894430657b6SRoss Zwisler return error; 3895430657b6SRoss Zwisler } 3896430657b6SRoss Zwisler 389701127848SJan Kara /* 3898cca32b7eSRoss Zwisler * ext4_punch_hole: punches a hole in a file by releasing the blocks 3899a4bb6b64SAllison Henderson * associated with the given offset and length 3900a4bb6b64SAllison Henderson * 3901a4bb6b64SAllison Henderson * @inode: File inode 3902a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3903a4bb6b64SAllison Henderson * @len: The length of the hole 3904a4bb6b64SAllison Henderson * 39054907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3906a4bb6b64SAllison Henderson */ 3907a4bb6b64SAllison Henderson 3908aeb2817aSAshish Sangwan int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) 3909a4bb6b64SAllison Henderson { 391026a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 391126a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 391226a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3913a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 391426a4c0c6STheodore Ts'o handle_t *handle; 391526a4c0c6STheodore Ts'o unsigned int credits; 391626a4c0c6STheodore Ts'o int ret = 0; 391726a4c0c6STheodore Ts'o 3918a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 391973355192SAllison Henderson return -EOPNOTSUPP; 3920a4bb6b64SAllison Henderson 3921b8a86845SLukas Czerner trace_ext4_punch_hole(inode, offset, length, 0); 3922aaddea81SZheng Liu 3923c1e8220bSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 3924c1e8220bSTheodore Ts'o if (ext4_has_inline_data(inode)) { 3925c1e8220bSTheodore Ts'o down_write(&EXT4_I(inode)->i_mmap_sem); 3926c1e8220bSTheodore Ts'o ret = ext4_convert_inline_data(inode); 3927c1e8220bSTheodore Ts'o up_write(&EXT4_I(inode)->i_mmap_sem); 3928c1e8220bSTheodore Ts'o if (ret) 3929c1e8220bSTheodore Ts'o return ret; 3930c1e8220bSTheodore Ts'o } 3931c1e8220bSTheodore Ts'o 393226a4c0c6STheodore Ts'o /* 393326a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 393426a4c0c6STheodore Ts'o * Then release them. 393526a4c0c6STheodore Ts'o */ 3936cca32b7eSRoss Zwisler if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 393726a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 393826a4c0c6STheodore Ts'o offset + length - 1); 393926a4c0c6STheodore Ts'o if (ret) 394026a4c0c6STheodore Ts'o return ret; 394126a4c0c6STheodore Ts'o } 394226a4c0c6STheodore Ts'o 39435955102cSAl Viro inode_lock(inode); 39449ef06cecSLukas Czerner 394526a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 394626a4c0c6STheodore Ts'o if (offset >= inode->i_size) 394726a4c0c6STheodore Ts'o goto out_mutex; 394826a4c0c6STheodore Ts'o 394926a4c0c6STheodore Ts'o /* 395026a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 395126a4c0c6STheodore Ts'o * to end after the page that contains i_size 395226a4c0c6STheodore Ts'o */ 395326a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 395426a4c0c6STheodore Ts'o length = inode->i_size + 395509cbfeafSKirill A. Shutemov PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) - 395626a4c0c6STheodore Ts'o offset; 395726a4c0c6STheodore Ts'o } 395826a4c0c6STheodore Ts'o 3959a361293fSJan Kara if (offset & (sb->s_blocksize - 1) || 3960a361293fSJan Kara (offset + length) & (sb->s_blocksize - 1)) { 3961a361293fSJan Kara /* 3962a361293fSJan Kara * Attach jinode to inode for jbd2 if we do any zeroing of 3963a361293fSJan Kara * partial block 3964a361293fSJan Kara */ 3965a361293fSJan Kara ret = ext4_inode_attach_jinode(inode); 3966a361293fSJan Kara if (ret < 0) 3967a361293fSJan Kara goto out_mutex; 3968a361293fSJan Kara 3969a361293fSJan Kara } 3970a361293fSJan Kara 3971ea3d7209SJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 3972ea3d7209SJan Kara inode_dio_wait(inode); 3973ea3d7209SJan Kara 3974ea3d7209SJan Kara /* 3975ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 3976ea3d7209SJan Kara * page cache. 3977ea3d7209SJan Kara */ 3978ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 3979430657b6SRoss Zwisler 3980430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 3981430657b6SRoss Zwisler if (ret) 3982430657b6SRoss Zwisler goto out_dio; 3983430657b6SRoss Zwisler 3984a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 3985a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 398626a4c0c6STheodore Ts'o 3987a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 398801127848SJan Kara if (last_block_offset > first_block_offset) { 398901127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, length); 399001127848SJan Kara if (ret) 399101127848SJan Kara goto out_dio; 3992a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 3993a87dd18cSLukas Czerner last_block_offset); 399401127848SJan Kara } 399526a4c0c6STheodore Ts'o 399626a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 399726a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 399826a4c0c6STheodore Ts'o else 399926a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 400026a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 400126a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 400226a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 400326a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 400426a4c0c6STheodore Ts'o goto out_dio; 400526a4c0c6STheodore Ts'o } 400626a4c0c6STheodore Ts'o 4007a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 4008a87dd18cSLukas Czerner length); 400926a4c0c6STheodore Ts'o if (ret) 401026a4c0c6STheodore Ts'o goto out_stop; 401126a4c0c6STheodore Ts'o 401226a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 401326a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 401426a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 401526a4c0c6STheodore Ts'o 4016eee597acSLukas Czerner /* If there are blocks to remove, do it */ 4017eee597acSLukas Czerner if (stop_block > first_block) { 401826a4c0c6STheodore Ts'o 401926a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 402026a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 402126a4c0c6STheodore Ts'o 402226a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 402326a4c0c6STheodore Ts'o stop_block - first_block); 402426a4c0c6STheodore Ts'o if (ret) { 402526a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 402626a4c0c6STheodore Ts'o goto out_stop; 402726a4c0c6STheodore Ts'o } 402826a4c0c6STheodore Ts'o 402926a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 403026a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 403126a4c0c6STheodore Ts'o stop_block - 1); 403226a4c0c6STheodore Ts'o else 40334f579ae7SLukas Czerner ret = ext4_ind_remove_space(handle, inode, first_block, 403426a4c0c6STheodore Ts'o stop_block); 403526a4c0c6STheodore Ts'o 4036819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 4037eee597acSLukas Czerner } 403826a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 403926a4c0c6STheodore Ts'o ext4_handle_sync(handle); 4040e251f9bcSMaxim Patlasov 4041eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 404226a4c0c6STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 404367a7d5f5SJan Kara if (ret >= 0) 404467a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 404526a4c0c6STheodore Ts'o out_stop: 404626a4c0c6STheodore Ts'o ext4_journal_stop(handle); 404726a4c0c6STheodore Ts'o out_dio: 4048ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 404926a4c0c6STheodore Ts'o out_mutex: 40505955102cSAl Viro inode_unlock(inode); 405126a4c0c6STheodore Ts'o return ret; 4052a4bb6b64SAllison Henderson } 4053a4bb6b64SAllison Henderson 4054a361293fSJan Kara int ext4_inode_attach_jinode(struct inode *inode) 4055a361293fSJan Kara { 4056a361293fSJan Kara struct ext4_inode_info *ei = EXT4_I(inode); 4057a361293fSJan Kara struct jbd2_inode *jinode; 4058a361293fSJan Kara 4059a361293fSJan Kara if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal) 4060a361293fSJan Kara return 0; 4061a361293fSJan Kara 4062a361293fSJan Kara jinode = jbd2_alloc_inode(GFP_KERNEL); 4063a361293fSJan Kara spin_lock(&inode->i_lock); 4064a361293fSJan Kara if (!ei->jinode) { 4065a361293fSJan Kara if (!jinode) { 4066a361293fSJan Kara spin_unlock(&inode->i_lock); 4067a361293fSJan Kara return -ENOMEM; 4068a361293fSJan Kara } 4069a361293fSJan Kara ei->jinode = jinode; 4070a361293fSJan Kara jbd2_journal_init_jbd_inode(ei->jinode, inode); 4071a361293fSJan Kara jinode = NULL; 4072a361293fSJan Kara } 4073a361293fSJan Kara spin_unlock(&inode->i_lock); 4074a361293fSJan Kara if (unlikely(jinode != NULL)) 4075a361293fSJan Kara jbd2_free_inode(jinode); 4076a361293fSJan Kara return 0; 4077a361293fSJan Kara } 4078a361293fSJan Kara 4079a4bb6b64SAllison Henderson /* 4080617ba13bSMingming Cao * ext4_truncate() 4081ac27a0ecSDave Kleikamp * 4082617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4083617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4084ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4085ac27a0ecSDave Kleikamp * 408642b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 4087ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4088ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4089ac27a0ecSDave Kleikamp * 4090ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4091ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4092ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4093ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4094ac27a0ecSDave Kleikamp * left-to-right works OK too). 4095ac27a0ecSDave Kleikamp * 4096ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4097ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4098ac27a0ecSDave Kleikamp * 4099ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4100617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4101ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4102617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4103617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4104ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4105617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4106ac27a0ecSDave Kleikamp */ 41072c98eb5eSTheodore Ts'o int ext4_truncate(struct inode *inode) 4108ac27a0ecSDave Kleikamp { 4109819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4110819c4920STheodore Ts'o unsigned int credits; 41112c98eb5eSTheodore Ts'o int err = 0; 4112819c4920STheodore Ts'o handle_t *handle; 4113819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 4114819c4920STheodore Ts'o 411519b5ef61STheodore Ts'o /* 411619b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 4117e04027e8SMatthew Wilcox * or it's a completely new inode. In those cases we might not 411819b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 411919b5ef61STheodore Ts'o */ 412019b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 41215955102cSAl Viro WARN_ON(!inode_is_locked(inode)); 41220562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 41230562e0baSJiaying Zhang 412491ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 41252c98eb5eSTheodore Ts'o return 0; 4126ac27a0ecSDave Kleikamp 412712e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 4128c8d46e41SJiaying Zhang 41295534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 413019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 41317d8f9f7dSTheodore Ts'o 4132aef1c851STao Ma if (ext4_has_inline_data(inode)) { 4133aef1c851STao Ma int has_inline = 1; 4134aef1c851STao Ma 413501daf945STheodore Ts'o err = ext4_inline_data_truncate(inode, &has_inline); 413601daf945STheodore Ts'o if (err) 413701daf945STheodore Ts'o return err; 4138aef1c851STao Ma if (has_inline) 41392c98eb5eSTheodore Ts'o return 0; 4140aef1c851STao Ma } 4141aef1c851STao Ma 4142a361293fSJan Kara /* If we zero-out tail of the page, we have to create jinode for jbd2 */ 4143a361293fSJan Kara if (inode->i_size & (inode->i_sb->s_blocksize - 1)) { 4144a361293fSJan Kara if (ext4_inode_attach_jinode(inode) < 0) 41452c98eb5eSTheodore Ts'o return 0; 4146a361293fSJan Kara } 4147a361293fSJan Kara 4148ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4149819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 4150ff9893dcSAmir Goldstein else 4151819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 4152819c4920STheodore Ts'o 4153819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 41542c98eb5eSTheodore Ts'o if (IS_ERR(handle)) 41552c98eb5eSTheodore Ts'o return PTR_ERR(handle); 4156819c4920STheodore Ts'o 4157eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 4158eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 4159819c4920STheodore Ts'o 4160819c4920STheodore Ts'o /* 4161819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 4162819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 4163819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 4164819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 4165819c4920STheodore Ts'o * 4166819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 4167819c4920STheodore Ts'o * truncatable state while each transaction commits. 4168819c4920STheodore Ts'o */ 41692c98eb5eSTheodore Ts'o err = ext4_orphan_add(handle, inode); 41702c98eb5eSTheodore Ts'o if (err) 4171819c4920STheodore Ts'o goto out_stop; 4172819c4920STheodore Ts'o 4173819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 4174819c4920STheodore Ts'o 4175819c4920STheodore Ts'o ext4_discard_preallocations(inode); 4176819c4920STheodore Ts'o 4177819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4178d0abb36dSTheodore Ts'o err = ext4_ext_truncate(handle, inode); 4179819c4920STheodore Ts'o else 4180819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 4181819c4920STheodore Ts'o 4182819c4920STheodore Ts'o up_write(&ei->i_data_sem); 4183d0abb36dSTheodore Ts'o if (err) 4184d0abb36dSTheodore Ts'o goto out_stop; 4185819c4920STheodore Ts'o 4186819c4920STheodore Ts'o if (IS_SYNC(inode)) 4187819c4920STheodore Ts'o ext4_handle_sync(handle); 4188819c4920STheodore Ts'o 4189819c4920STheodore Ts'o out_stop: 4190819c4920STheodore Ts'o /* 4191819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 4192819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 4193819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 419458d86a50SWang Shilong * ext4_evict_inode(), and we allow that function to clean up the 4195819c4920STheodore Ts'o * orphan info for us. 4196819c4920STheodore Ts'o */ 4197819c4920STheodore Ts'o if (inode->i_nlink) 4198819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 4199819c4920STheodore Ts'o 4200eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 4201819c4920STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 4202819c4920STheodore Ts'o ext4_journal_stop(handle); 4203a86c6181SAlex Tomas 42040562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 42052c98eb5eSTheodore Ts'o return err; 4206ac27a0ecSDave Kleikamp } 4207ac27a0ecSDave Kleikamp 4208ac27a0ecSDave Kleikamp /* 4209617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4210ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4211ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4212ac27a0ecSDave Kleikamp * inode. 4213ac27a0ecSDave Kleikamp */ 4214617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4215617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4216ac27a0ecSDave Kleikamp { 4217240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4218ac27a0ecSDave Kleikamp struct buffer_head *bh; 4219240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4220240799cdSTheodore Ts'o ext4_fsblk_t block; 422102f03c42SLinus Torvalds struct blk_plug plug; 4222240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4223ac27a0ecSDave Kleikamp 42243a06d778SAneesh Kumar K.V iloc->bh = NULL; 4225c37e9e01STheodore Ts'o if (inode->i_ino < EXT4_ROOT_INO || 4226c37e9e01STheodore Ts'o inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 42276a797d27SDarrick J. Wong return -EFSCORRUPTED; 4228ac27a0ecSDave Kleikamp 4229240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4230240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4231240799cdSTheodore Ts'o if (!gdp) 4232240799cdSTheodore Ts'o return -EIO; 4233240799cdSTheodore Ts'o 4234240799cdSTheodore Ts'o /* 4235240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4236240799cdSTheodore Ts'o */ 423700d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4238240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4239240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4240240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4241240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4242240799cdSTheodore Ts'o 4243240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4244aebf0243SWang Shilong if (unlikely(!bh)) 4245860d21e2STheodore Ts'o return -ENOMEM; 4246ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4247ac27a0ecSDave Kleikamp lock_buffer(bh); 42489c83a923SHidehiro Kawai 42499c83a923SHidehiro Kawai /* 42509c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 42519c83a923SHidehiro Kawai * to write out another inode in the same block. In this 42529c83a923SHidehiro Kawai * case, we don't have to read the block because we may 42539c83a923SHidehiro Kawai * read the old inode data successfully. 42549c83a923SHidehiro Kawai */ 42559c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 42569c83a923SHidehiro Kawai set_buffer_uptodate(bh); 42579c83a923SHidehiro Kawai 4258ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4259ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4260ac27a0ecSDave Kleikamp unlock_buffer(bh); 4261ac27a0ecSDave Kleikamp goto has_buffer; 4262ac27a0ecSDave Kleikamp } 4263ac27a0ecSDave Kleikamp 4264ac27a0ecSDave Kleikamp /* 4265ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4266ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4267ac27a0ecSDave Kleikamp * block. 4268ac27a0ecSDave Kleikamp */ 4269ac27a0ecSDave Kleikamp if (in_mem) { 4270ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4271240799cdSTheodore Ts'o int i, start; 4272ac27a0ecSDave Kleikamp 4273240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4274ac27a0ecSDave Kleikamp 4275ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4276240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4277aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 4278ac27a0ecSDave Kleikamp goto make_io; 4279ac27a0ecSDave Kleikamp 4280ac27a0ecSDave Kleikamp /* 4281ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4282ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4283ac27a0ecSDave Kleikamp * of one, so skip it. 4284ac27a0ecSDave Kleikamp */ 4285ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4286ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4287ac27a0ecSDave Kleikamp goto make_io; 4288ac27a0ecSDave Kleikamp } 4289240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4290ac27a0ecSDave Kleikamp if (i == inode_offset) 4291ac27a0ecSDave Kleikamp continue; 4292617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4293ac27a0ecSDave Kleikamp break; 4294ac27a0ecSDave Kleikamp } 4295ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4296240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4297ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4298ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4299ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4300ac27a0ecSDave Kleikamp unlock_buffer(bh); 4301ac27a0ecSDave Kleikamp goto has_buffer; 4302ac27a0ecSDave Kleikamp } 4303ac27a0ecSDave Kleikamp } 4304ac27a0ecSDave Kleikamp 4305ac27a0ecSDave Kleikamp make_io: 4306ac27a0ecSDave Kleikamp /* 4307240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4308240799cdSTheodore Ts'o * blocks from the inode table. 4309240799cdSTheodore Ts'o */ 431002f03c42SLinus Torvalds blk_start_plug(&plug); 4311240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4312240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4313240799cdSTheodore Ts'o unsigned num; 43140d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4315240799cdSTheodore Ts'o 4316240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4317b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 43180d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 4319240799cdSTheodore Ts'o if (table > b) 4320240799cdSTheodore Ts'o b = table; 43210d606e2cSTheodore Ts'o end = b + ra_blks; 4322240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4323feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 4324560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4325240799cdSTheodore Ts'o table += num / inodes_per_block; 4326240799cdSTheodore Ts'o if (end > table) 4327240799cdSTheodore Ts'o end = table; 4328240799cdSTheodore Ts'o while (b <= end) 4329240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4330240799cdSTheodore Ts'o } 4331240799cdSTheodore Ts'o 4332240799cdSTheodore Ts'o /* 4333ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4334ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4335ac27a0ecSDave Kleikamp * Read the block from disk. 4336ac27a0ecSDave Kleikamp */ 43370562e0baSJiaying Zhang trace_ext4_load_inode(inode); 4338ac27a0ecSDave Kleikamp get_bh(bh); 4339ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 43402a222ca9SMike Christie submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); 434102f03c42SLinus Torvalds blk_finish_plug(&plug); 4342ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4343ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4344*878520acSTheodore Ts'o ext4_set_errno(inode->i_sb, EIO); 4345c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 4346c398eda0STheodore Ts'o "unable to read itable block"); 4347ac27a0ecSDave Kleikamp brelse(bh); 4348ac27a0ecSDave Kleikamp return -EIO; 4349ac27a0ecSDave Kleikamp } 4350ac27a0ecSDave Kleikamp } 4351ac27a0ecSDave Kleikamp has_buffer: 4352ac27a0ecSDave Kleikamp iloc->bh = bh; 4353ac27a0ecSDave Kleikamp return 0; 4354ac27a0ecSDave Kleikamp } 4355ac27a0ecSDave Kleikamp 4356617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4357ac27a0ecSDave Kleikamp { 4358ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4359617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 436019f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4361ac27a0ecSDave Kleikamp } 4362ac27a0ecSDave Kleikamp 43636642586bSRoss Zwisler static bool ext4_should_use_dax(struct inode *inode) 43646642586bSRoss Zwisler { 43656642586bSRoss Zwisler if (!test_opt(inode->i_sb, DAX)) 43666642586bSRoss Zwisler return false; 43676642586bSRoss Zwisler if (!S_ISREG(inode->i_mode)) 43686642586bSRoss Zwisler return false; 43696642586bSRoss Zwisler if (ext4_should_journal_data(inode)) 43706642586bSRoss Zwisler return false; 43716642586bSRoss Zwisler if (ext4_has_inline_data(inode)) 43726642586bSRoss Zwisler return false; 4373592ddec7SChandan Rajendra if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT)) 43746642586bSRoss Zwisler return false; 4375c93d8f88SEric Biggers if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY)) 4376c93d8f88SEric Biggers return false; 43776642586bSRoss Zwisler return true; 43786642586bSRoss Zwisler } 43796642586bSRoss Zwisler 4380617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4381ac27a0ecSDave Kleikamp { 4382617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 438300a1a053STheodore Ts'o unsigned int new_fl = 0; 4384ac27a0ecSDave Kleikamp 4385617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 438600a1a053STheodore Ts'o new_fl |= S_SYNC; 4387617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 438800a1a053STheodore Ts'o new_fl |= S_APPEND; 4389617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 439000a1a053STheodore Ts'o new_fl |= S_IMMUTABLE; 4391617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 439200a1a053STheodore Ts'o new_fl |= S_NOATIME; 4393617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 439400a1a053STheodore Ts'o new_fl |= S_DIRSYNC; 43956642586bSRoss Zwisler if (ext4_should_use_dax(inode)) 4396923ae0ffSRoss Zwisler new_fl |= S_DAX; 43972ee6a576SEric Biggers if (flags & EXT4_ENCRYPT_FL) 43982ee6a576SEric Biggers new_fl |= S_ENCRYPTED; 4399b886ee3eSGabriel Krisman Bertazi if (flags & EXT4_CASEFOLD_FL) 4400b886ee3eSGabriel Krisman Bertazi new_fl |= S_CASEFOLD; 4401c93d8f88SEric Biggers if (flags & EXT4_VERITY_FL) 4402c93d8f88SEric Biggers new_fl |= S_VERITY; 44035f16f322STheodore Ts'o inode_set_flags(inode, new_fl, 44042ee6a576SEric Biggers S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| 4405c93d8f88SEric Biggers S_ENCRYPTED|S_CASEFOLD|S_VERITY); 4406ac27a0ecSDave Kleikamp } 4407ac27a0ecSDave Kleikamp 44080fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 44090fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 44100fc1b451SAneesh Kumar K.V { 44110fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 44128180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 44138180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 44140fc1b451SAneesh Kumar K.V 4415e2b911c5SDarrick J. Wong if (ext4_has_feature_huge_file(sb)) { 44160fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 44170fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 44180fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 441907a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 44208180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 44218180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 44228180a562SAneesh Kumar K.V } else { 44230fc1b451SAneesh Kumar K.V return i_blocks; 44248180a562SAneesh Kumar K.V } 44250fc1b451SAneesh Kumar K.V } else { 44260fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 44270fc1b451SAneesh Kumar K.V } 44280fc1b451SAneesh Kumar K.V } 4429ff9ddf7eSJan Kara 4430eb9b5f01STheodore Ts'o static inline int ext4_iget_extra_inode(struct inode *inode, 4431152a7b0aSTao Ma struct ext4_inode *raw_inode, 4432152a7b0aSTao Ma struct ext4_inode_info *ei) 4433152a7b0aSTao Ma { 4434152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4435152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 4436eb9b5f01STheodore Ts'o 4437290ab230SEric Biggers if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <= 4438290ab230SEric Biggers EXT4_INODE_SIZE(inode->i_sb) && 4439290ab230SEric Biggers *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4440152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 4441eb9b5f01STheodore Ts'o return ext4_find_inline_data_nolock(inode); 4442f19d5870STao Ma } else 4443f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4444eb9b5f01STheodore Ts'o return 0; 4445152a7b0aSTao Ma } 4446152a7b0aSTao Ma 4447040cb378SLi Xi int ext4_get_projid(struct inode *inode, kprojid_t *projid) 4448040cb378SLi Xi { 44490b7b7779SKaho Ng if (!ext4_has_feature_project(inode->i_sb)) 4450040cb378SLi Xi return -EOPNOTSUPP; 4451040cb378SLi Xi *projid = EXT4_I(inode)->i_projid; 4452040cb378SLi Xi return 0; 4453040cb378SLi Xi } 4454040cb378SLi Xi 4455e254d1afSEryu Guan /* 4456e254d1afSEryu Guan * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of 4457e254d1afSEryu Guan * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag 4458e254d1afSEryu Guan * set. 4459e254d1afSEryu Guan */ 4460e254d1afSEryu Guan static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val) 4461e254d1afSEryu Guan { 4462e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4463e254d1afSEryu Guan inode_set_iversion_raw(inode, val); 4464e254d1afSEryu Guan else 4465e254d1afSEryu Guan inode_set_iversion_queried(inode, val); 4466e254d1afSEryu Guan } 4467e254d1afSEryu Guan static inline u64 ext4_inode_peek_iversion(const struct inode *inode) 4468e254d1afSEryu Guan { 4469e254d1afSEryu Guan if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) 4470e254d1afSEryu Guan return inode_peek_iversion_raw(inode); 4471e254d1afSEryu Guan else 4472e254d1afSEryu Guan return inode_peek_iversion(inode); 4473e254d1afSEryu Guan } 4474e254d1afSEryu Guan 44758a363970STheodore Ts'o struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, 44768a363970STheodore Ts'o ext4_iget_flags flags, const char *function, 44778a363970STheodore Ts'o unsigned int line) 4478ac27a0ecSDave Kleikamp { 4479617ba13bSMingming Cao struct ext4_iloc iloc; 4480617ba13bSMingming Cao struct ext4_inode *raw_inode; 44811d1fe1eeSDavid Howells struct ext4_inode_info *ei; 44821d1fe1eeSDavid Howells struct inode *inode; 4483b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 44841d1fe1eeSDavid Howells long ret; 44857e6e1ef4SDarrick J. Wong loff_t size; 4486ac27a0ecSDave Kleikamp int block; 448708cefc7aSEric W. Biederman uid_t i_uid; 448808cefc7aSEric W. Biederman gid_t i_gid; 4489040cb378SLi Xi projid_t i_projid; 4490ac27a0ecSDave Kleikamp 4491191ce178STheodore Ts'o if ((!(flags & EXT4_IGET_SPECIAL) && 44928a363970STheodore Ts'o (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 44938a363970STheodore Ts'o (ino < EXT4_ROOT_INO) || 44948a363970STheodore Ts'o (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 44958a363970STheodore Ts'o if (flags & EXT4_IGET_HANDLE) 44968a363970STheodore Ts'o return ERR_PTR(-ESTALE); 44978a363970STheodore Ts'o __ext4_error(sb, function, line, 44988a363970STheodore Ts'o "inode #%lu: comm %s: iget: illegal inode #", 44998a363970STheodore Ts'o ino, current->comm); 45008a363970STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 45018a363970STheodore Ts'o } 45028a363970STheodore Ts'o 45031d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 45041d1fe1eeSDavid Howells if (!inode) 45051d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 45061d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 45071d1fe1eeSDavid Howells return inode; 45081d1fe1eeSDavid Howells 45091d1fe1eeSDavid Howells ei = EXT4_I(inode); 45107dc57615SPeter Huewe iloc.bh = NULL; 4511ac27a0ecSDave Kleikamp 45121d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 45131d1fe1eeSDavid Howells if (ret < 0) 4514ac27a0ecSDave Kleikamp goto bad_inode; 4515617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4516814525f4SDarrick J. Wong 45178e4b5eaeSTheodore Ts'o if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) { 45188a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45198a363970STheodore Ts'o "iget: root inode unallocated"); 45208e4b5eaeSTheodore Ts'o ret = -EFSCORRUPTED; 45218e4b5eaeSTheodore Ts'o goto bad_inode; 45228e4b5eaeSTheodore Ts'o } 45238e4b5eaeSTheodore Ts'o 45248a363970STheodore Ts'o if ((flags & EXT4_IGET_HANDLE) && 45258a363970STheodore Ts'o (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) { 45268a363970STheodore Ts'o ret = -ESTALE; 45278a363970STheodore Ts'o goto bad_inode; 45288a363970STheodore Ts'o } 45298a363970STheodore Ts'o 4530814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4531814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4532814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 45332dc8d9e1SEric Biggers EXT4_INODE_SIZE(inode->i_sb) || 45342dc8d9e1SEric Biggers (ei->i_extra_isize & 3)) { 45358a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45368a363970STheodore Ts'o "iget: bad extra_isize %u " 45378a363970STheodore Ts'o "(inode size %u)", 45382dc8d9e1SEric Biggers ei->i_extra_isize, 4539814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 45406a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4541814525f4SDarrick J. Wong goto bad_inode; 4542814525f4SDarrick J. Wong } 4543814525f4SDarrick J. Wong } else 4544814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4545814525f4SDarrick J. Wong 4546814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 45479aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 4548814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4549814525f4SDarrick J. Wong __u32 csum; 4550814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4551814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4552814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4553814525f4SDarrick J. Wong sizeof(inum)); 4554814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4555814525f4SDarrick J. Wong sizeof(gen)); 4556814525f4SDarrick J. Wong } 4557814525f4SDarrick J. Wong 4558814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 4559*878520acSTheodore Ts'o ext4_set_errno(inode->i_sb, EFSBADCRC); 45608a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 45618a363970STheodore Ts'o "iget: checksum invalid"); 45626a797d27SDarrick J. Wong ret = -EFSBADCRC; 4563814525f4SDarrick J. Wong goto bad_inode; 4564814525f4SDarrick J. Wong } 4565814525f4SDarrick J. Wong 4566ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 456708cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 456808cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 45690b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 4570040cb378SLi Xi EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE && 4571040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4572040cb378SLi Xi i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid); 4573040cb378SLi Xi else 4574040cb378SLi Xi i_projid = EXT4_DEF_PROJID; 4575040cb378SLi Xi 4576ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 457708cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 457808cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4579ac27a0ecSDave Kleikamp } 458008cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 458108cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4582040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, i_projid); 4583bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4584ac27a0ecSDave Kleikamp 4585353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 458667cf5b09STao Ma ei->i_inline_off = 0; 4587ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4588ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4589ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4590ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4591ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4592ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4593ac27a0ecSDave Kleikamp */ 4594ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4595393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4596393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4597393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4598ac27a0ecSDave Kleikamp /* this inode is deleted */ 45991d1fe1eeSDavid Howells ret = -ESTALE; 4600ac27a0ecSDave Kleikamp goto bad_inode; 4601ac27a0ecSDave Kleikamp } 4602ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4603ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4604ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4605393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4606393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4607393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4608ac27a0ecSDave Kleikamp } 4609ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 4610cce6c9f7SToshi Kani ext4_set_inode_flags(inode); 46110fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 46127973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4613e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) 4614a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4615a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4616e08ac99fSArtem Blagodarenko inode->i_size = ext4_isize(sb, raw_inode); 46177e6e1ef4SDarrick J. Wong if ((size = i_size_read(inode)) < 0) { 46188a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46198a363970STheodore Ts'o "iget: bad i_size value: %lld", size); 46207e6e1ef4SDarrick J. Wong ret = -EFSCORRUPTED; 46217e6e1ef4SDarrick J. Wong goto bad_inode; 46227e6e1ef4SDarrick J. Wong } 4623ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4624a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4625a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4626a9e7f447SDmitry Monakhov #endif 4627ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4628ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4629a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4630ac27a0ecSDave Kleikamp /* 4631ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4632ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4633ac27a0ecSDave Kleikamp */ 4634617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4635ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4636ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4637ac27a0ecSDave Kleikamp 4638b436b9beSJan Kara /* 4639b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4640b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4641b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4642b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4643b436b9beSJan Kara * now it is reread from disk. 4644b436b9beSJan Kara */ 4645b436b9beSJan Kara if (journal) { 4646b436b9beSJan Kara transaction_t *transaction; 4647b436b9beSJan Kara tid_t tid; 4648b436b9beSJan Kara 4649a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4650b436b9beSJan Kara if (journal->j_running_transaction) 4651b436b9beSJan Kara transaction = journal->j_running_transaction; 4652b436b9beSJan Kara else 4653b436b9beSJan Kara transaction = journal->j_committing_transaction; 4654b436b9beSJan Kara if (transaction) 4655b436b9beSJan Kara tid = transaction->t_tid; 4656b436b9beSJan Kara else 4657b436b9beSJan Kara tid = journal->j_commit_sequence; 4658a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4659b436b9beSJan Kara ei->i_sync_tid = tid; 4660b436b9beSJan Kara ei->i_datasync_tid = tid; 4661b436b9beSJan Kara } 4662b436b9beSJan Kara 46630040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4664ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4665ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 46662dc8d9e1SEric Biggers BUILD_BUG_ON(sizeof(struct ext4_inode) & 3); 4667617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4668617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4669ac27a0ecSDave Kleikamp } else { 4670eb9b5f01STheodore Ts'o ret = ext4_iget_extra_inode(inode, raw_inode, ei); 4671eb9b5f01STheodore Ts'o if (ret) 4672eb9b5f01STheodore Ts'o goto bad_inode; 4673ac27a0ecSDave Kleikamp } 4674814525f4SDarrick J. Wong } 4675ac27a0ecSDave Kleikamp 4676ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4677ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4678ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4679ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4680ef7f3835SKalpak Shah 4681ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4682ee73f9a5SJeff Layton u64 ivers = le32_to_cpu(raw_inode->i_disk_version); 4683ee73f9a5SJeff Layton 468425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 468525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 4686ee73f9a5SJeff Layton ivers |= 468725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 468825ec56b5SJean Noel Cordenner } 4689e254d1afSEryu Guan ext4_inode_set_iversion_queried(inode, ivers); 4690c4f65706STheodore Ts'o } 469125ec56b5SJean Noel Cordenner 4692c4b5a614STheodore Ts'o ret = 0; 4693485c26ecSTheodore Ts'o if (ei->i_file_acl && 46941032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 46958a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 46968a363970STheodore Ts'o "iget: bad extended attribute block %llu", 469724676da4STheodore Ts'o ei->i_file_acl); 46986a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4699485c26ecSTheodore Ts'o goto bad_inode; 4700f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4701bc716523SLiu Song /* validate the block references in the inode */ 4702bc716523SLiu Song if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4703fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4704fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4705bc716523SLiu Song if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4706bc716523SLiu Song ret = ext4_ext_check_inode(inode); 4707bc716523SLiu Song else 47081f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4709fe2c8191SThiemo Nagel } 4710f19d5870STao Ma } 4711567f3e9aSTheodore Ts'o if (ret) 47127a262f7cSAneesh Kumar K.V goto bad_inode; 47137a262f7cSAneesh Kumar K.V 4714ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4715617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4716617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4717617ba13bSMingming Cao ext4_set_aops(inode); 4718ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4719617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4720617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4721ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 47226390d33bSLuis R. Rodriguez /* VFS does not allow setting these so must be corruption */ 47236390d33bSLuis R. Rodriguez if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 47248a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47258a363970STheodore Ts'o "iget: immutable or append flags " 47268a363970STheodore Ts'o "not allowed on symlinks"); 47276390d33bSLuis R. Rodriguez ret = -EFSCORRUPTED; 47286390d33bSLuis R. Rodriguez goto bad_inode; 47296390d33bSLuis R. Rodriguez } 4730592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode)) { 4731a7a67e8aSAl Viro inode->i_op = &ext4_encrypted_symlink_inode_operations; 4732a7a67e8aSAl Viro ext4_set_aops(inode); 4733a7a67e8aSAl Viro } else if (ext4_inode_is_fast_symlink(inode)) { 473475e7566bSAl Viro inode->i_link = (char *)ei->i_data; 4735617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4736e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4737e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4738e83c1397SDuane Griffin } else { 4739617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4740617ba13bSMingming Cao ext4_set_aops(inode); 4741ac27a0ecSDave Kleikamp } 474221fc61c7SAl Viro inode_nohighmem(inode); 4743563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4744563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4745617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4746ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4747ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4748ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4749ac27a0ecSDave Kleikamp else 4750ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4751ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4752393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4753393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4754563bdd61STheodore Ts'o } else { 47556a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 47568a363970STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47578a363970STheodore Ts'o "iget: bogus i_mode (%o)", inode->i_mode); 4758563bdd61STheodore Ts'o goto bad_inode; 4759ac27a0ecSDave Kleikamp } 47606456ca65STheodore Ts'o if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) 47616456ca65STheodore Ts'o ext4_error_inode(inode, function, line, 0, 47626456ca65STheodore Ts'o "casefold flag without casefold feature"); 4763ac27a0ecSDave Kleikamp brelse(iloc.bh); 4764dec214d0STahsin Erdogan 47651d1fe1eeSDavid Howells unlock_new_inode(inode); 47661d1fe1eeSDavid Howells return inode; 4767ac27a0ecSDave Kleikamp 4768ac27a0ecSDave Kleikamp bad_inode: 4769567f3e9aSTheodore Ts'o brelse(iloc.bh); 47701d1fe1eeSDavid Howells iget_failed(inode); 47711d1fe1eeSDavid Howells return ERR_PTR(ret); 4772ac27a0ecSDave Kleikamp } 4773ac27a0ecSDave Kleikamp 47740fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 47750fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 47760fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 47770fc1b451SAneesh Kumar K.V { 47780fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 47790fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 47800fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 47810fc1b451SAneesh Kumar K.V 47820fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 47830fc1b451SAneesh Kumar K.V /* 47844907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 47850fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 47860fc1b451SAneesh Kumar K.V */ 47878180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 47880fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 478984a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4790f287a1a5STheodore Ts'o return 0; 4791f287a1a5STheodore Ts'o } 4792e2b911c5SDarrick J. Wong if (!ext4_has_feature_huge_file(sb)) 4793f287a1a5STheodore Ts'o return -EFBIG; 4794f287a1a5STheodore Ts'o 4795f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 47960fc1b451SAneesh Kumar K.V /* 47970fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 47980fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 47990fc1b451SAneesh Kumar K.V */ 48008180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48010fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 480284a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 48030fc1b451SAneesh Kumar K.V } else { 480484a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 48058180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 48068180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 48078180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 48088180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 48090fc1b451SAneesh Kumar K.V } 4810f287a1a5STheodore Ts'o return 0; 48110fc1b451SAneesh Kumar K.V } 48120fc1b451SAneesh Kumar K.V 4813a26f4992STheodore Ts'o struct other_inode { 4814a26f4992STheodore Ts'o unsigned long orig_ino; 4815a26f4992STheodore Ts'o struct ext4_inode *raw_inode; 4816a26f4992STheodore Ts'o }; 4817a26f4992STheodore Ts'o 4818a26f4992STheodore Ts'o static int other_inode_match(struct inode * inode, unsigned long ino, 4819a26f4992STheodore Ts'o void *data) 4820a26f4992STheodore Ts'o { 4821a26f4992STheodore Ts'o struct other_inode *oi = (struct other_inode *) data; 4822a26f4992STheodore Ts'o 4823a26f4992STheodore Ts'o if ((inode->i_ino != ino) || 4824a26f4992STheodore Ts'o (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 48250e11f644SChristoph Hellwig I_DIRTY_INODE)) || 4826a26f4992STheodore Ts'o ((inode->i_state & I_DIRTY_TIME) == 0)) 4827a26f4992STheodore Ts'o return 0; 4828a26f4992STheodore Ts'o spin_lock(&inode->i_lock); 4829a26f4992STheodore Ts'o if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW | 48300e11f644SChristoph Hellwig I_DIRTY_INODE)) == 0) && 4831a26f4992STheodore Ts'o (inode->i_state & I_DIRTY_TIME)) { 4832a26f4992STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 4833a26f4992STheodore Ts'o 4834a26f4992STheodore Ts'o inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED); 4835a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4836a26f4992STheodore Ts'o 4837a26f4992STheodore Ts'o spin_lock(&ei->i_raw_lock); 4838a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode); 4839a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode); 4840a26f4992STheodore Ts'o EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode); 4841a26f4992STheodore Ts'o ext4_inode_csum_set(inode, oi->raw_inode, ei); 4842a26f4992STheodore Ts'o spin_unlock(&ei->i_raw_lock); 4843a26f4992STheodore Ts'o trace_ext4_other_inode_update_time(inode, oi->orig_ino); 4844a26f4992STheodore Ts'o return -1; 4845a26f4992STheodore Ts'o } 4846a26f4992STheodore Ts'o spin_unlock(&inode->i_lock); 4847a26f4992STheodore Ts'o return -1; 4848a26f4992STheodore Ts'o } 4849a26f4992STheodore Ts'o 4850a26f4992STheodore Ts'o /* 4851a26f4992STheodore Ts'o * Opportunistically update the other time fields for other inodes in 4852a26f4992STheodore Ts'o * the same inode table block. 4853a26f4992STheodore Ts'o */ 4854a26f4992STheodore Ts'o static void ext4_update_other_inodes_time(struct super_block *sb, 4855a26f4992STheodore Ts'o unsigned long orig_ino, char *buf) 4856a26f4992STheodore Ts'o { 4857a26f4992STheodore Ts'o struct other_inode oi; 4858a26f4992STheodore Ts'o unsigned long ino; 4859a26f4992STheodore Ts'o int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 4860a26f4992STheodore Ts'o int inode_size = EXT4_INODE_SIZE(sb); 4861a26f4992STheodore Ts'o 4862a26f4992STheodore Ts'o oi.orig_ino = orig_ino; 48630f0ff9a9STheodore Ts'o /* 48640f0ff9a9STheodore Ts'o * Calculate the first inode in the inode table block. Inode 48650f0ff9a9STheodore Ts'o * numbers are one-based. That is, the first inode in a block 48660f0ff9a9STheodore Ts'o * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1). 48670f0ff9a9STheodore Ts'o */ 48680f0ff9a9STheodore Ts'o ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1; 4869a26f4992STheodore Ts'o for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) { 4870a26f4992STheodore Ts'o if (ino == orig_ino) 4871a26f4992STheodore Ts'o continue; 4872a26f4992STheodore Ts'o oi.raw_inode = (struct ext4_inode *) buf; 4873a26f4992STheodore Ts'o (void) find_inode_nowait(sb, ino, other_inode_match, &oi); 4874a26f4992STheodore Ts'o } 4875a26f4992STheodore Ts'o } 4876a26f4992STheodore Ts'o 4877ac27a0ecSDave Kleikamp /* 4878ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4879ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4880ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4881ac27a0ecSDave Kleikamp * 4882ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4883ac27a0ecSDave Kleikamp */ 4884617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4885ac27a0ecSDave Kleikamp struct inode *inode, 4886830156c7SFrank Mayhar struct ext4_iloc *iloc) 4887ac27a0ecSDave Kleikamp { 4888617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4889617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4890ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4891202ee5dfSTheodore Ts'o struct super_block *sb = inode->i_sb; 4892ac27a0ecSDave Kleikamp int err = 0, rc, block; 4893202ee5dfSTheodore Ts'o int need_datasync = 0, set_large_file = 0; 489408cefc7aSEric W. Biederman uid_t i_uid; 489508cefc7aSEric W. Biederman gid_t i_gid; 4896040cb378SLi Xi projid_t i_projid; 4897ac27a0ecSDave Kleikamp 4898202ee5dfSTheodore Ts'o spin_lock(&ei->i_raw_lock); 4899202ee5dfSTheodore Ts'o 4900202ee5dfSTheodore Ts'o /* For fields not tracked in the in-memory inode, 4901ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 490219f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4903617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4904ac27a0ecSDave Kleikamp 4905ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 490608cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 490708cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4908040cb378SLi Xi i_projid = from_kprojid(&init_user_ns, ei->i_projid); 4909ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 491008cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 491108cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4912ac27a0ecSDave Kleikamp /* 4913ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4914ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4915ac27a0ecSDave Kleikamp */ 491693e3b4e6SDaeho Jeong if (ei->i_dtime && list_empty(&ei->i_orphan)) { 491793e3b4e6SDaeho Jeong raw_inode->i_uid_high = 0; 491893e3b4e6SDaeho Jeong raw_inode->i_gid_high = 0; 491993e3b4e6SDaeho Jeong } else { 4920ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 492108cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4922ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 492308cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4924ac27a0ecSDave Kleikamp } 4925ac27a0ecSDave Kleikamp } else { 492608cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 492708cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4928ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4929ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4930ac27a0ecSDave Kleikamp } 4931ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4932ef7f3835SKalpak Shah 4933ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4934ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4935ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4936ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4937ef7f3835SKalpak Shah 4938bce92d56SLi Xi err = ext4_inode_blocks_set(handle, raw_inode, ei); 4939bce92d56SLi Xi if (err) { 4940202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 49410fc1b451SAneesh Kumar K.V goto out_brelse; 4942202ee5dfSTheodore Ts'o } 4943ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4944353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 4945ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) 4946a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4947a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 49487973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4949e08ac99fSArtem Blagodarenko if (ei->i_disksize != ext4_isize(inode->i_sb, raw_inode)) { 4950a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4951b71fc079SJan Kara need_datasync = 1; 4952b71fc079SJan Kara } 4953ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4954e2b911c5SDarrick J. Wong if (!ext4_has_feature_large_file(sb) || 4955617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4956202ee5dfSTheodore Ts'o cpu_to_le32(EXT4_GOOD_OLD_REV)) 4957202ee5dfSTheodore Ts'o set_large_file = 1; 4958ac27a0ecSDave Kleikamp } 4959ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4960ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4961ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4962ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4963ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4964ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4965ac27a0ecSDave Kleikamp } else { 4966ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4967ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4968ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4969ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4970ac27a0ecSDave Kleikamp } 4971f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4972de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4973ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4974f19d5870STao Ma } 4975ac27a0ecSDave Kleikamp 4976ed3654ebSTheodore Ts'o if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) { 4977e254d1afSEryu Guan u64 ivers = ext4_inode_peek_iversion(inode); 4978ee73f9a5SJeff Layton 4979ee73f9a5SJeff Layton raw_inode->i_disk_version = cpu_to_le32(ivers); 498025ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 498125ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 498225ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 4983ee73f9a5SJeff Layton cpu_to_le32(ivers >> 32); 4984c4f65706STheodore Ts'o raw_inode->i_extra_isize = 4985c4f65706STheodore Ts'o cpu_to_le16(ei->i_extra_isize); 4986c4f65706STheodore Ts'o } 498725ec56b5SJean Noel Cordenner } 4988040cb378SLi Xi 49890b7b7779SKaho Ng BUG_ON(!ext4_has_feature_project(inode->i_sb) && 4990040cb378SLi Xi i_projid != EXT4_DEF_PROJID); 4991040cb378SLi Xi 4992040cb378SLi Xi if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 4993040cb378SLi Xi EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) 4994040cb378SLi Xi raw_inode->i_projid = cpu_to_le32(i_projid); 4995040cb378SLi Xi 4996814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4997202ee5dfSTheodore Ts'o spin_unlock(&ei->i_raw_lock); 49981751e8a6SLinus Torvalds if (inode->i_sb->s_flags & SB_LAZYTIME) 4999a26f4992STheodore Ts'o ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5000a26f4992STheodore Ts'o bh->b_data); 5001202ee5dfSTheodore Ts'o 50020390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 500373b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 5004ac27a0ecSDave Kleikamp if (!err) 5005ac27a0ecSDave Kleikamp err = rc; 500619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5007202ee5dfSTheodore Ts'o if (set_large_file) { 50085d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5009202ee5dfSTheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5010202ee5dfSTheodore Ts'o if (err) 5011202ee5dfSTheodore Ts'o goto out_brelse; 5012e2b911c5SDarrick J. Wong ext4_set_feature_large_file(sb); 5013202ee5dfSTheodore Ts'o ext4_handle_sync(handle); 5014202ee5dfSTheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 5015202ee5dfSTheodore Ts'o } 5016b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5017ac27a0ecSDave Kleikamp out_brelse: 5018ac27a0ecSDave Kleikamp brelse(bh); 5019617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5020ac27a0ecSDave Kleikamp return err; 5021ac27a0ecSDave Kleikamp } 5022ac27a0ecSDave Kleikamp 5023ac27a0ecSDave Kleikamp /* 5024617ba13bSMingming Cao * ext4_write_inode() 5025ac27a0ecSDave Kleikamp * 5026ac27a0ecSDave Kleikamp * We are called from a few places: 5027ac27a0ecSDave Kleikamp * 502887f7e416STheodore Ts'o * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files. 5029ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 50304907cb7bSAnatol Pomozov * transaction to commit. 5031ac27a0ecSDave Kleikamp * 503287f7e416STheodore Ts'o * - Within flush work (sys_sync(), kupdate and such). 503387f7e416STheodore Ts'o * We wait on commit, if told to. 5034ac27a0ecSDave Kleikamp * 503587f7e416STheodore Ts'o * - Within iput_final() -> write_inode_now() 503687f7e416STheodore Ts'o * We wait on commit, if told to. 5037ac27a0ecSDave Kleikamp * 5038ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5039ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 504087f7e416STheodore Ts'o * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL 504187f7e416STheodore Ts'o * writeback. 5042ac27a0ecSDave Kleikamp * 5043ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5044ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5045ac27a0ecSDave Kleikamp * which we are interested. 5046ac27a0ecSDave Kleikamp * 5047ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5048ac27a0ecSDave Kleikamp * 5049ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5050ac27a0ecSDave Kleikamp * stuff(); 5051ac27a0ecSDave Kleikamp * inode->i_size = expr; 5052ac27a0ecSDave Kleikamp * 505387f7e416STheodore Ts'o * is in error because write_inode() could occur while `stuff()' is running, 505487f7e416STheodore Ts'o * and the new i_size will be lost. Plus the inode will no longer be on the 505587f7e416STheodore Ts'o * superblock's dirty inode list. 5056ac27a0ecSDave Kleikamp */ 5057a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5058ac27a0ecSDave Kleikamp { 505991ac6f43SFrank Mayhar int err; 506091ac6f43SFrank Mayhar 506118f2c4fcSTheodore Ts'o if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) || 506218f2c4fcSTheodore Ts'o sb_rdonly(inode->i_sb)) 5063ac27a0ecSDave Kleikamp return 0; 5064ac27a0ecSDave Kleikamp 506518f2c4fcSTheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 506618f2c4fcSTheodore Ts'o return -EIO; 506718f2c4fcSTheodore Ts'o 506891ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5069617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5070b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5071ac27a0ecSDave Kleikamp dump_stack(); 5072ac27a0ecSDave Kleikamp return -EIO; 5073ac27a0ecSDave Kleikamp } 5074ac27a0ecSDave Kleikamp 507510542c22SJan Kara /* 507610542c22SJan Kara * No need to force transaction in WB_SYNC_NONE mode. Also 507710542c22SJan Kara * ext4_sync_fs() will force the commit after everything is 507810542c22SJan Kara * written. 507910542c22SJan Kara */ 508010542c22SJan Kara if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync) 5081ac27a0ecSDave Kleikamp return 0; 5082ac27a0ecSDave Kleikamp 508318f2c4fcSTheodore Ts'o err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, 508418f2c4fcSTheodore Ts'o EXT4_I(inode)->i_sync_tid); 508591ac6f43SFrank Mayhar } else { 508691ac6f43SFrank Mayhar struct ext4_iloc iloc; 508791ac6f43SFrank Mayhar 50888b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 508991ac6f43SFrank Mayhar if (err) 509091ac6f43SFrank Mayhar return err; 509110542c22SJan Kara /* 509210542c22SJan Kara * sync(2) will flush the whole buffer cache. No need to do 509310542c22SJan Kara * it here separately for each inode. 509410542c22SJan Kara */ 509510542c22SJan Kara if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 5096830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5097830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 5098*878520acSTheodore Ts'o ext4_set_errno(inode->i_sb, EIO); 5099c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 5100c398eda0STheodore Ts'o "IO error syncing inode"); 5101830156c7SFrank Mayhar err = -EIO; 5102830156c7SFrank Mayhar } 5103fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 510491ac6f43SFrank Mayhar } 510591ac6f43SFrank Mayhar return err; 5106ac27a0ecSDave Kleikamp } 5107ac27a0ecSDave Kleikamp 5108ac27a0ecSDave Kleikamp /* 510953e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 511053e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 511153e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 511253e87268SJan Kara */ 511353e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 511453e87268SJan Kara { 511553e87268SJan Kara struct page *page; 511653e87268SJan Kara unsigned offset; 511753e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 511853e87268SJan Kara tid_t commit_tid = 0; 511953e87268SJan Kara int ret; 512053e87268SJan Kara 512109cbfeafSKirill A. Shutemov offset = inode->i_size & (PAGE_SIZE - 1); 512253e87268SJan Kara /* 5123565333a1Syangerkun * If the page is fully truncated, we don't need to wait for any commit 5124565333a1Syangerkun * (and we even should not as __ext4_journalled_invalidatepage() may 5125565333a1Syangerkun * strip all buffers from the page but keep the page dirty which can then 5126565333a1Syangerkun * confuse e.g. concurrent ext4_writepage() seeing dirty page without 5127565333a1Syangerkun * buffers). Also we don't need to wait for any commit if all buffers in 5128565333a1Syangerkun * the page remain valid. This is most beneficial for the common case of 5129565333a1Syangerkun * blocksize == PAGESIZE. 513053e87268SJan Kara */ 5131565333a1Syangerkun if (!offset || offset > (PAGE_SIZE - i_blocksize(inode))) 513253e87268SJan Kara return; 513353e87268SJan Kara while (1) { 513453e87268SJan Kara page = find_lock_page(inode->i_mapping, 513509cbfeafSKirill A. Shutemov inode->i_size >> PAGE_SHIFT); 513653e87268SJan Kara if (!page) 513753e87268SJan Kara return; 5138ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 513909cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 514053e87268SJan Kara unlock_page(page); 514109cbfeafSKirill A. Shutemov put_page(page); 514253e87268SJan Kara if (ret != -EBUSY) 514353e87268SJan Kara return; 514453e87268SJan Kara commit_tid = 0; 514553e87268SJan Kara read_lock(&journal->j_state_lock); 514653e87268SJan Kara if (journal->j_committing_transaction) 514753e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 514853e87268SJan Kara read_unlock(&journal->j_state_lock); 514953e87268SJan Kara if (commit_tid) 515053e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 515153e87268SJan Kara } 515253e87268SJan Kara } 515353e87268SJan Kara 515453e87268SJan Kara /* 5155617ba13bSMingming Cao * ext4_setattr() 5156ac27a0ecSDave Kleikamp * 5157ac27a0ecSDave Kleikamp * Called from notify_change. 5158ac27a0ecSDave Kleikamp * 5159ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5160ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5161ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5162ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5163ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5164ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5165ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5166ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5167ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5168ac27a0ecSDave Kleikamp * 5169678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5170678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5171678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5172678aaf48SJan Kara * This way we are sure that all the data written in the previous 5173678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5174678aaf48SJan Kara * writeback). 5175678aaf48SJan Kara * 5176678aaf48SJan Kara * Called with inode->i_mutex down. 5177ac27a0ecSDave Kleikamp */ 5178617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5179ac27a0ecSDave Kleikamp { 51802b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5181ac27a0ecSDave Kleikamp int error, rc = 0; 51823d287de3SDmitry Monakhov int orphan = 0; 5183ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5184ac27a0ecSDave Kleikamp 51850db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 51860db1ff22STheodore Ts'o return -EIO; 51870db1ff22STheodore Ts'o 518802b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 518902b016caSTheodore Ts'o return -EPERM; 519002b016caSTheodore Ts'o 519102b016caSTheodore Ts'o if (unlikely(IS_APPEND(inode) && 519202b016caSTheodore Ts'o (ia_valid & (ATTR_MODE | ATTR_UID | 519302b016caSTheodore Ts'o ATTR_GID | ATTR_TIMES_SET)))) 519402b016caSTheodore Ts'o return -EPERM; 519502b016caSTheodore Ts'o 519631051c85SJan Kara error = setattr_prepare(dentry, attr); 5197ac27a0ecSDave Kleikamp if (error) 5198ac27a0ecSDave Kleikamp return error; 5199ac27a0ecSDave Kleikamp 52003ce2b8ddSEric Biggers error = fscrypt_prepare_setattr(dentry, attr); 52013ce2b8ddSEric Biggers if (error) 52023ce2b8ddSEric Biggers return error; 52033ce2b8ddSEric Biggers 5204c93d8f88SEric Biggers error = fsverity_prepare_setattr(dentry, attr); 5205c93d8f88SEric Biggers if (error) 5206c93d8f88SEric Biggers return error; 5207c93d8f88SEric Biggers 5208a7cdadeeSJan Kara if (is_quota_modification(inode, attr)) { 5209a7cdadeeSJan Kara error = dquot_initialize(inode); 5210a7cdadeeSJan Kara if (error) 5211a7cdadeeSJan Kara return error; 5212a7cdadeeSJan Kara } 521308cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 521408cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 5215ac27a0ecSDave Kleikamp handle_t *handle; 5216ac27a0ecSDave Kleikamp 5217ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5218ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 52199924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 52209924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 5221194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 5222ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5223ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5224ac27a0ecSDave Kleikamp goto err_out; 5225ac27a0ecSDave Kleikamp } 52267a9ca53aSTahsin Erdogan 52277a9ca53aSTahsin Erdogan /* dquot_transfer() calls back ext4_get_inode_usage() which 52287a9ca53aSTahsin Erdogan * counts xattr inode references. 52297a9ca53aSTahsin Erdogan */ 52307a9ca53aSTahsin Erdogan down_read(&EXT4_I(inode)->xattr_sem); 5231b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 52327a9ca53aSTahsin Erdogan up_read(&EXT4_I(inode)->xattr_sem); 52337a9ca53aSTahsin Erdogan 5234ac27a0ecSDave Kleikamp if (error) { 5235617ba13bSMingming Cao ext4_journal_stop(handle); 5236ac27a0ecSDave Kleikamp return error; 5237ac27a0ecSDave Kleikamp } 5238ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5239ac27a0ecSDave Kleikamp * one transaction */ 5240ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5241ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5242ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5243ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5244617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5245617ba13bSMingming Cao ext4_journal_stop(handle); 5246ac27a0ecSDave Kleikamp } 5247ac27a0ecSDave Kleikamp 52483da40c7bSJosef Bacik if (attr->ia_valid & ATTR_SIZE) { 52495208386cSJan Kara handle_t *handle; 52503da40c7bSJosef Bacik loff_t oldsize = inode->i_size; 5251b9c1c267SJan Kara int shrink = (attr->ia_size < inode->i_size); 5252562c72aaSChristoph Hellwig 525312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5254e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5255e2b46574SEric Sandeen 52560c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 52570c095c7fSTheodore Ts'o return -EFBIG; 5258e2b46574SEric Sandeen } 52593da40c7bSJosef Bacik if (!S_ISREG(inode->i_mode)) 52603da40c7bSJosef Bacik return -EINVAL; 5261dff6efc3SChristoph Hellwig 5262dff6efc3SChristoph Hellwig if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) 5263dff6efc3SChristoph Hellwig inode_inc_iversion(inode); 5264dff6efc3SChristoph Hellwig 5265b9c1c267SJan Kara if (shrink) { 5266b9c1c267SJan Kara if (ext4_should_order_data(inode)) { 52675208386cSJan Kara error = ext4_begin_ordered_truncate(inode, 52685208386cSJan Kara attr->ia_size); 52695208386cSJan Kara if (error) 52705208386cSJan Kara goto err_out; 52715208386cSJan Kara } 5272b9c1c267SJan Kara /* 5273b9c1c267SJan Kara * Blocks are going to be removed from the inode. Wait 5274b9c1c267SJan Kara * for dio in flight. 5275b9c1c267SJan Kara */ 5276b9c1c267SJan Kara inode_dio_wait(inode); 5277b9c1c267SJan Kara } 5278b9c1c267SJan Kara 5279b9c1c267SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5280b9c1c267SJan Kara 5281b9c1c267SJan Kara rc = ext4_break_layouts(inode); 5282b9c1c267SJan Kara if (rc) { 5283b9c1c267SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 5284b9c1c267SJan Kara return rc; 5285b9c1c267SJan Kara } 5286b9c1c267SJan Kara 52873da40c7bSJosef Bacik if (attr->ia_size != inode->i_size) { 52889924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 5289ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5290ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5291b9c1c267SJan Kara goto out_mmap_sem; 5292ac27a0ecSDave Kleikamp } 52933da40c7bSJosef Bacik if (ext4_handle_valid(handle) && shrink) { 5294617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 52953d287de3SDmitry Monakhov orphan = 1; 52963d287de3SDmitry Monakhov } 5297911af577SEryu Guan /* 5298911af577SEryu Guan * Update c/mtime on truncate up, ext4_truncate() will 5299911af577SEryu Guan * update c/mtime in shrink case below 5300911af577SEryu Guan */ 5301911af577SEryu Guan if (!shrink) { 5302eeca7ea1SDeepa Dinamani inode->i_mtime = current_time(inode); 5303911af577SEryu Guan inode->i_ctime = inode->i_mtime; 5304911af577SEryu Guan } 530590e775b7SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 5306617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5307617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5308ac27a0ecSDave Kleikamp if (!error) 5309ac27a0ecSDave Kleikamp error = rc; 531090e775b7SJan Kara /* 531190e775b7SJan Kara * We have to update i_size under i_data_sem together 531290e775b7SJan Kara * with i_disksize to avoid races with writeback code 531390e775b7SJan Kara * running ext4_wb_update_i_disksize(). 531490e775b7SJan Kara */ 531590e775b7SJan Kara if (!error) 531690e775b7SJan Kara i_size_write(inode, attr->ia_size); 531790e775b7SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 5318617ba13bSMingming Cao ext4_journal_stop(handle); 5319b9c1c267SJan Kara if (error) 5320b9c1c267SJan Kara goto out_mmap_sem; 532182a25b02SJan Kara if (!shrink) { 5322b9c1c267SJan Kara pagecache_isize_extended(inode, oldsize, 5323b9c1c267SJan Kara inode->i_size); 5324b9c1c267SJan Kara } else if (ext4_should_journal_data(inode)) { 532582a25b02SJan Kara ext4_wait_for_tail_page_commit(inode); 5326b9c1c267SJan Kara } 5327430657b6SRoss Zwisler } 5328430657b6SRoss Zwisler 532953e87268SJan Kara /* 533053e87268SJan Kara * Truncate pagecache after we've waited for commit 533153e87268SJan Kara * in data=journal mode to make pages freeable. 533253e87268SJan Kara */ 53337caef267SKirill A. Shutemov truncate_pagecache(inode, inode->i_size); 5334b9c1c267SJan Kara /* 5335b9c1c267SJan Kara * Call ext4_truncate() even if i_size didn't change to 5336b9c1c267SJan Kara * truncate possible preallocated blocks. 5337b9c1c267SJan Kara */ 5338b9c1c267SJan Kara if (attr->ia_size <= oldsize) { 53392c98eb5eSTheodore Ts'o rc = ext4_truncate(inode); 53402c98eb5eSTheodore Ts'o if (rc) 53412c98eb5eSTheodore Ts'o error = rc; 53422c98eb5eSTheodore Ts'o } 5343b9c1c267SJan Kara out_mmap_sem: 5344ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 53453da40c7bSJosef Bacik } 5346ac27a0ecSDave Kleikamp 53472c98eb5eSTheodore Ts'o if (!error) { 53481025774cSChristoph Hellwig setattr_copy(inode, attr); 53491025774cSChristoph Hellwig mark_inode_dirty(inode); 53501025774cSChristoph Hellwig } 53511025774cSChristoph Hellwig 53521025774cSChristoph Hellwig /* 53531025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 53541025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 53551025774cSChristoph Hellwig */ 53563d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 5357617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5358ac27a0ecSDave Kleikamp 53592c98eb5eSTheodore Ts'o if (!error && (ia_valid & ATTR_MODE)) 536064e178a7SChristoph Hellwig rc = posix_acl_chmod(inode, inode->i_mode); 5361ac27a0ecSDave Kleikamp 5362ac27a0ecSDave Kleikamp err_out: 5363617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5364ac27a0ecSDave Kleikamp if (!error) 5365ac27a0ecSDave Kleikamp error = rc; 5366ac27a0ecSDave Kleikamp return error; 5367ac27a0ecSDave Kleikamp } 5368ac27a0ecSDave Kleikamp 5369a528d35eSDavid Howells int ext4_getattr(const struct path *path, struct kstat *stat, 5370a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 53713e3398a0SMingming Cao { 537299652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 537399652ea5SDavid Howells struct ext4_inode *raw_inode; 537499652ea5SDavid Howells struct ext4_inode_info *ei = EXT4_I(inode); 537599652ea5SDavid Howells unsigned int flags; 53763e3398a0SMingming Cao 537799652ea5SDavid Howells if (EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) { 537899652ea5SDavid Howells stat->result_mask |= STATX_BTIME; 537999652ea5SDavid Howells stat->btime.tv_sec = ei->i_crtime.tv_sec; 538099652ea5SDavid Howells stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 538199652ea5SDavid Howells } 538299652ea5SDavid Howells 538399652ea5SDavid Howells flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 538499652ea5SDavid Howells if (flags & EXT4_APPEND_FL) 538599652ea5SDavid Howells stat->attributes |= STATX_ATTR_APPEND; 538699652ea5SDavid Howells if (flags & EXT4_COMPR_FL) 538799652ea5SDavid Howells stat->attributes |= STATX_ATTR_COMPRESSED; 538899652ea5SDavid Howells if (flags & EXT4_ENCRYPT_FL) 538999652ea5SDavid Howells stat->attributes |= STATX_ATTR_ENCRYPTED; 539099652ea5SDavid Howells if (flags & EXT4_IMMUTABLE_FL) 539199652ea5SDavid Howells stat->attributes |= STATX_ATTR_IMMUTABLE; 539299652ea5SDavid Howells if (flags & EXT4_NODUMP_FL) 539399652ea5SDavid Howells stat->attributes |= STATX_ATTR_NODUMP; 53941f607195SEric Biggers if (flags & EXT4_VERITY_FL) 53951f607195SEric Biggers stat->attributes |= STATX_ATTR_VERITY; 539699652ea5SDavid Howells 53973209f68bSDavid Howells stat->attributes_mask |= (STATX_ATTR_APPEND | 53983209f68bSDavid Howells STATX_ATTR_COMPRESSED | 53993209f68bSDavid Howells STATX_ATTR_ENCRYPTED | 54003209f68bSDavid Howells STATX_ATTR_IMMUTABLE | 54011f607195SEric Biggers STATX_ATTR_NODUMP | 54021f607195SEric Biggers STATX_ATTR_VERITY); 54033209f68bSDavid Howells 54043e3398a0SMingming Cao generic_fillattr(inode, stat); 540599652ea5SDavid Howells return 0; 540699652ea5SDavid Howells } 540799652ea5SDavid Howells 540899652ea5SDavid Howells int ext4_file_getattr(const struct path *path, struct kstat *stat, 540999652ea5SDavid Howells u32 request_mask, unsigned int query_flags) 541099652ea5SDavid Howells { 541199652ea5SDavid Howells struct inode *inode = d_inode(path->dentry); 541299652ea5SDavid Howells u64 delalloc_blocks; 541399652ea5SDavid Howells 541499652ea5SDavid Howells ext4_getattr(path, stat, request_mask, query_flags); 54153e3398a0SMingming Cao 54163e3398a0SMingming Cao /* 54179206c561SAndreas Dilger * If there is inline data in the inode, the inode will normally not 54189206c561SAndreas Dilger * have data blocks allocated (it may have an external xattr block). 54199206c561SAndreas Dilger * Report at least one sector for such files, so tools like tar, rsync, 5420d67d64f4STheodore Ts'o * others don't incorrectly think the file is completely sparse. 54219206c561SAndreas Dilger */ 54229206c561SAndreas Dilger if (unlikely(ext4_has_inline_data(inode))) 54239206c561SAndreas Dilger stat->blocks += (stat->size + 511) >> 9; 54249206c561SAndreas Dilger 54259206c561SAndreas Dilger /* 54263e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 54273e3398a0SMingming Cao * otherwise in the case of system crash before the real block 54283e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 54293e3398a0SMingming Cao * on-disk file blocks. 54303e3398a0SMingming Cao * We always keep i_blocks updated together with real 54313e3398a0SMingming Cao * allocation. But to not confuse with user, stat 54323e3398a0SMingming Cao * will return the blocks that include the delayed allocation 54333e3398a0SMingming Cao * blocks for this file. 54343e3398a0SMingming Cao */ 543596607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 543696607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 54378af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9); 54383e3398a0SMingming Cao return 0; 54393e3398a0SMingming Cao } 5440ac27a0ecSDave Kleikamp 5441fffb2739SJan Kara static int ext4_index_trans_blocks(struct inode *inode, int lblocks, 5442fffb2739SJan Kara int pextents) 5443a02908f1SMingming Cao { 544412e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5445fffb2739SJan Kara return ext4_ind_trans_blocks(inode, lblocks); 5446fffb2739SJan Kara return ext4_ext_index_trans_blocks(inode, pextents); 5447a02908f1SMingming Cao } 5448ac51d837STheodore Ts'o 5449a02908f1SMingming Cao /* 5450a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5451a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5452a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5453a02908f1SMingming Cao * 5454a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 54554907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 5456a02908f1SMingming Cao * they could still across block group boundary. 5457a02908f1SMingming Cao * 5458a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5459a02908f1SMingming Cao */ 5460dec214d0STahsin Erdogan static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 5461fffb2739SJan Kara int pextents) 5462a02908f1SMingming Cao { 54638df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 54648df9675fSTheodore Ts'o int gdpblocks; 5465a02908f1SMingming Cao int idxblocks; 5466a02908f1SMingming Cao int ret = 0; 5467a02908f1SMingming Cao 5468a02908f1SMingming Cao /* 5469fffb2739SJan Kara * How many index blocks need to touch to map @lblocks logical blocks 5470fffb2739SJan Kara * to @pextents physical extents? 5471a02908f1SMingming Cao */ 5472fffb2739SJan Kara idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents); 5473a02908f1SMingming Cao 5474a02908f1SMingming Cao ret = idxblocks; 5475a02908f1SMingming Cao 5476a02908f1SMingming Cao /* 5477a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5478a02908f1SMingming Cao * to account 5479a02908f1SMingming Cao */ 5480fffb2739SJan Kara groups = idxblocks + pextents; 5481a02908f1SMingming Cao gdpblocks = groups; 54828df9675fSTheodore Ts'o if (groups > ngroups) 54838df9675fSTheodore Ts'o groups = ngroups; 5484a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5485a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5486a02908f1SMingming Cao 5487a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5488a02908f1SMingming Cao ret += groups + gdpblocks; 5489a02908f1SMingming Cao 5490a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5491a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5492ac27a0ecSDave Kleikamp 5493ac27a0ecSDave Kleikamp return ret; 5494ac27a0ecSDave Kleikamp } 5495ac27a0ecSDave Kleikamp 5496ac27a0ecSDave Kleikamp /* 549725985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 5498f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5499f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5500a02908f1SMingming Cao * 5501525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5502a02908f1SMingming Cao * 5503525f4ed8SMingming Cao * We need to consider the worse case, when 5504a02908f1SMingming Cao * one new block per extent. 5505a02908f1SMingming Cao */ 5506a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5507a02908f1SMingming Cao { 5508a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5509a02908f1SMingming Cao int ret; 5510a02908f1SMingming Cao 5511fffb2739SJan Kara ret = ext4_meta_trans_blocks(inode, bpp, bpp); 5512a02908f1SMingming Cao 5513a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5514a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5515a02908f1SMingming Cao ret += bpp; 5516a02908f1SMingming Cao return ret; 5517a02908f1SMingming Cao } 5518f3bd1f3fSMingming Cao 5519f3bd1f3fSMingming Cao /* 5520f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5521f3bd1f3fSMingming Cao * 5522f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 552379e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5524f3bd1f3fSMingming Cao * 5525f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5526f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5527f3bd1f3fSMingming Cao */ 5528f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5529f3bd1f3fSMingming Cao { 5530f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5531f3bd1f3fSMingming Cao } 5532f3bd1f3fSMingming Cao 5533a02908f1SMingming Cao /* 5534617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5535ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5536ac27a0ecSDave Kleikamp */ 5537617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5538617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5539ac27a0ecSDave Kleikamp { 5540ac27a0ecSDave Kleikamp int err = 0; 5541ac27a0ecSDave Kleikamp 5542a6758309SVasily Averin if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) { 5543a6758309SVasily Averin put_bh(iloc->bh); 55440db1ff22STheodore Ts'o return -EIO; 5545a6758309SVasily Averin } 5546c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 554725ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 554825ec56b5SJean Noel Cordenner 5549ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5550ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5551ac27a0ecSDave Kleikamp 5552dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5553830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5554ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5555ac27a0ecSDave Kleikamp return err; 5556ac27a0ecSDave Kleikamp } 5557ac27a0ecSDave Kleikamp 5558ac27a0ecSDave Kleikamp /* 5559ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5560ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5561ac27a0ecSDave Kleikamp */ 5562ac27a0ecSDave Kleikamp 5563ac27a0ecSDave Kleikamp int 5564617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5565617ba13bSMingming Cao struct ext4_iloc *iloc) 5566ac27a0ecSDave Kleikamp { 55670390131bSFrank Mayhar int err; 55680390131bSFrank Mayhar 55690db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 55700db1ff22STheodore Ts'o return -EIO; 55710db1ff22STheodore Ts'o 5572617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5573ac27a0ecSDave Kleikamp if (!err) { 5574ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5575617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5576ac27a0ecSDave Kleikamp if (err) { 5577ac27a0ecSDave Kleikamp brelse(iloc->bh); 5578ac27a0ecSDave Kleikamp iloc->bh = NULL; 5579ac27a0ecSDave Kleikamp } 5580ac27a0ecSDave Kleikamp } 5581617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5582ac27a0ecSDave Kleikamp return err; 5583ac27a0ecSDave Kleikamp } 5584ac27a0ecSDave Kleikamp 5585c03b45b8SMiao Xie static int __ext4_expand_extra_isize(struct inode *inode, 5586c03b45b8SMiao Xie unsigned int new_extra_isize, 5587c03b45b8SMiao Xie struct ext4_iloc *iloc, 5588c03b45b8SMiao Xie handle_t *handle, int *no_expand) 5589c03b45b8SMiao Xie { 5590c03b45b8SMiao Xie struct ext4_inode *raw_inode; 5591c03b45b8SMiao Xie struct ext4_xattr_ibody_header *header; 55924ea99936STheodore Ts'o unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb); 55934ea99936STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5594c03b45b8SMiao Xie int error; 5595c03b45b8SMiao Xie 55964ea99936STheodore Ts'o /* this was checked at iget time, but double check for good measure */ 55974ea99936STheodore Ts'o if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) || 55984ea99936STheodore Ts'o (ei->i_extra_isize & 3)) { 55994ea99936STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)", 56004ea99936STheodore Ts'o ei->i_extra_isize, 56014ea99936STheodore Ts'o EXT4_INODE_SIZE(inode->i_sb)); 56024ea99936STheodore Ts'o return -EFSCORRUPTED; 56034ea99936STheodore Ts'o } 56044ea99936STheodore Ts'o if ((new_extra_isize < ei->i_extra_isize) || 56054ea99936STheodore Ts'o (new_extra_isize < 4) || 56064ea99936STheodore Ts'o (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE)) 56074ea99936STheodore Ts'o return -EINVAL; /* Should never happen */ 56084ea99936STheodore Ts'o 5609c03b45b8SMiao Xie raw_inode = ext4_raw_inode(iloc); 5610c03b45b8SMiao Xie 5611c03b45b8SMiao Xie header = IHDR(inode, raw_inode); 5612c03b45b8SMiao Xie 5613c03b45b8SMiao Xie /* No extended attributes present */ 5614c03b45b8SMiao Xie if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 5615c03b45b8SMiao Xie header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 5616c03b45b8SMiao Xie memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE + 5617c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize, 0, 5618c03b45b8SMiao Xie new_extra_isize - EXT4_I(inode)->i_extra_isize); 5619c03b45b8SMiao Xie EXT4_I(inode)->i_extra_isize = new_extra_isize; 5620c03b45b8SMiao Xie return 0; 5621c03b45b8SMiao Xie } 5622c03b45b8SMiao Xie 5623c03b45b8SMiao Xie /* try to expand with EAs present */ 5624c03b45b8SMiao Xie error = ext4_expand_extra_isize_ea(inode, new_extra_isize, 5625c03b45b8SMiao Xie raw_inode, handle); 5626c03b45b8SMiao Xie if (error) { 5627c03b45b8SMiao Xie /* 5628c03b45b8SMiao Xie * Inode size expansion failed; don't try again 5629c03b45b8SMiao Xie */ 5630c03b45b8SMiao Xie *no_expand = 1; 5631c03b45b8SMiao Xie } 5632c03b45b8SMiao Xie 5633c03b45b8SMiao Xie return error; 5634c03b45b8SMiao Xie } 5635c03b45b8SMiao Xie 5636ac27a0ecSDave Kleikamp /* 56376dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 56386dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 56396dd4ee7cSKalpak Shah */ 5640cf0a5e81SMiao Xie static int ext4_try_to_expand_extra_isize(struct inode *inode, 56411d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 56421d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 56431d03ec98SAneesh Kumar K.V handle_t *handle) 56446dd4ee7cSKalpak Shah { 56453b10fdc6SMiao Xie int no_expand; 56463b10fdc6SMiao Xie int error; 56476dd4ee7cSKalpak Shah 5648cf0a5e81SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) 5649cf0a5e81SMiao Xie return -EOVERFLOW; 5650cf0a5e81SMiao Xie 5651cf0a5e81SMiao Xie /* 5652cf0a5e81SMiao Xie * In nojournal mode, we can immediately attempt to expand 5653cf0a5e81SMiao Xie * the inode. When journaled, we first need to obtain extra 5654cf0a5e81SMiao Xie * buffer credits since we may write into the EA block 5655cf0a5e81SMiao Xie * with this same handle. If journal_extend fails, then it will 5656cf0a5e81SMiao Xie * only result in a minor loss of functionality for that inode. 5657cf0a5e81SMiao Xie * If this is felt to be critical, then e2fsck should be run to 5658cf0a5e81SMiao Xie * force a large enough s_min_extra_isize. 5659cf0a5e81SMiao Xie */ 56606cb367c2SJan Kara if (ext4_journal_extend(handle, 566183448bdfSJan Kara EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0) 5662cf0a5e81SMiao Xie return -ENOSPC; 56636dd4ee7cSKalpak Shah 56643b10fdc6SMiao Xie if (ext4_write_trylock_xattr(inode, &no_expand) == 0) 5665cf0a5e81SMiao Xie return -EBUSY; 56663b10fdc6SMiao Xie 5667c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc, 5668c03b45b8SMiao Xie handle, &no_expand); 56693b10fdc6SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5670c03b45b8SMiao Xie 5671c03b45b8SMiao Xie return error; 56726dd4ee7cSKalpak Shah } 56736dd4ee7cSKalpak Shah 5674c03b45b8SMiao Xie int ext4_expand_extra_isize(struct inode *inode, 5675c03b45b8SMiao Xie unsigned int new_extra_isize, 5676c03b45b8SMiao Xie struct ext4_iloc *iloc) 5677c03b45b8SMiao Xie { 5678c03b45b8SMiao Xie handle_t *handle; 5679c03b45b8SMiao Xie int no_expand; 5680c03b45b8SMiao Xie int error, rc; 5681c03b45b8SMiao Xie 5682c03b45b8SMiao Xie if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 5683c03b45b8SMiao Xie brelse(iloc->bh); 5684c03b45b8SMiao Xie return -EOVERFLOW; 5685c03b45b8SMiao Xie } 5686c03b45b8SMiao Xie 5687c03b45b8SMiao Xie handle = ext4_journal_start(inode, EXT4_HT_INODE, 5688c03b45b8SMiao Xie EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 5689c03b45b8SMiao Xie if (IS_ERR(handle)) { 5690c03b45b8SMiao Xie error = PTR_ERR(handle); 5691c03b45b8SMiao Xie brelse(iloc->bh); 5692c03b45b8SMiao Xie return error; 5693c03b45b8SMiao Xie } 5694c03b45b8SMiao Xie 5695c03b45b8SMiao Xie ext4_write_lock_xattr(inode, &no_expand); 5696c03b45b8SMiao Xie 5697ddccb6dbSzhangyi (F) BUFFER_TRACE(iloc->bh, "get_write_access"); 5698c03b45b8SMiao Xie error = ext4_journal_get_write_access(handle, iloc->bh); 56993b10fdc6SMiao Xie if (error) { 5700c03b45b8SMiao Xie brelse(iloc->bh); 57017f420d64SDan Carpenter goto out_unlock; 57023b10fdc6SMiao Xie } 5703cf0a5e81SMiao Xie 5704c03b45b8SMiao Xie error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc, 5705c03b45b8SMiao Xie handle, &no_expand); 5706c03b45b8SMiao Xie 5707c03b45b8SMiao Xie rc = ext4_mark_iloc_dirty(handle, inode, iloc); 5708c03b45b8SMiao Xie if (!error) 5709c03b45b8SMiao Xie error = rc; 5710c03b45b8SMiao Xie 57117f420d64SDan Carpenter out_unlock: 5712c03b45b8SMiao Xie ext4_write_unlock_xattr(inode, &no_expand); 5713c03b45b8SMiao Xie ext4_journal_stop(handle); 57143b10fdc6SMiao Xie return error; 57156dd4ee7cSKalpak Shah } 57166dd4ee7cSKalpak Shah 57176dd4ee7cSKalpak Shah /* 5718ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5719ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5720ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5721ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5722ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5723ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5724ac27a0ecSDave Kleikamp * 5725ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5726ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5727ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5728ac27a0ecSDave Kleikamp * we start and wait on commits. 5729ac27a0ecSDave Kleikamp */ 5730617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5731ac27a0ecSDave Kleikamp { 5732617ba13bSMingming Cao struct ext4_iloc iloc; 57336dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5734cf0a5e81SMiao Xie int err; 5735ac27a0ecSDave Kleikamp 5736ac27a0ecSDave Kleikamp might_sleep(); 57377ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 5738617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 57395e1021f2SEryu Guan if (err) 57405e1021f2SEryu Guan return err; 5741cf0a5e81SMiao Xie 5742cf0a5e81SMiao Xie if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize) 5743cf0a5e81SMiao Xie ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize, 57446dd4ee7cSKalpak Shah iloc, handle); 5745cf0a5e81SMiao Xie 57465e1021f2SEryu Guan return ext4_mark_iloc_dirty(handle, inode, &iloc); 5747ac27a0ecSDave Kleikamp } 5748ac27a0ecSDave Kleikamp 5749ac27a0ecSDave Kleikamp /* 5750617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5751ac27a0ecSDave Kleikamp * 5752ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5753ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5754ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5755ac27a0ecSDave Kleikamp * 57565dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5757ac27a0ecSDave Kleikamp * are allocated to the file. 5758ac27a0ecSDave Kleikamp * 5759ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5760ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5761ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 57620ae45f63STheodore Ts'o * 57630ae45f63STheodore Ts'o * If only the I_DIRTY_TIME flag is set, we can skip everything. If 57640ae45f63STheodore Ts'o * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need 57650ae45f63STheodore Ts'o * to copy into the on-disk inode structure are the timestamp files. 5766ac27a0ecSDave Kleikamp */ 5767aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 5768ac27a0ecSDave Kleikamp { 5769ac27a0ecSDave Kleikamp handle_t *handle; 5770ac27a0ecSDave Kleikamp 57710ae45f63STheodore Ts'o if (flags == I_DIRTY_TIME) 57720ae45f63STheodore Ts'o return; 57739924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 5774ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5775ac27a0ecSDave Kleikamp goto out; 5776f3dc272fSCurt Wohlgemuth 5777617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5778f3dc272fSCurt Wohlgemuth 5779617ba13bSMingming Cao ext4_journal_stop(handle); 5780ac27a0ecSDave Kleikamp out: 5781ac27a0ecSDave Kleikamp return; 5782ac27a0ecSDave Kleikamp } 5783ac27a0ecSDave Kleikamp 5784617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5785ac27a0ecSDave Kleikamp { 5786ac27a0ecSDave Kleikamp journal_t *journal; 5787ac27a0ecSDave Kleikamp handle_t *handle; 5788ac27a0ecSDave Kleikamp int err; 5789c8585c6fSDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5790ac27a0ecSDave Kleikamp 5791ac27a0ecSDave Kleikamp /* 5792ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5793ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5794ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5795ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5796ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5797ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5798ac27a0ecSDave Kleikamp * nobody is changing anything. 5799ac27a0ecSDave Kleikamp */ 5800ac27a0ecSDave Kleikamp 5801617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 58020390131bSFrank Mayhar if (!journal) 58030390131bSFrank Mayhar return 0; 5804d699594dSDave Hansen if (is_journal_aborted(journal)) 5805ac27a0ecSDave Kleikamp return -EROFS; 5806ac27a0ecSDave Kleikamp 580717335dccSDmitry Monakhov /* Wait for all existing dio workers */ 580817335dccSDmitry Monakhov inode_dio_wait(inode); 580917335dccSDmitry Monakhov 58104c546592SDaeho Jeong /* 58114c546592SDaeho Jeong * Before flushing the journal and switching inode's aops, we have 58124c546592SDaeho Jeong * to flush all dirty data the inode has. There can be outstanding 58134c546592SDaeho Jeong * delayed allocations, there can be unwritten extents created by 58144c546592SDaeho Jeong * fallocate or buffered writes in dioread_nolock mode covered by 58154c546592SDaeho Jeong * dirty data which can be converted only after flushing the dirty 58164c546592SDaeho Jeong * data (and journalled aops don't know how to handle these cases). 58174c546592SDaeho Jeong */ 58184c546592SDaeho Jeong if (val) { 58194c546592SDaeho Jeong down_write(&EXT4_I(inode)->i_mmap_sem); 58204c546592SDaeho Jeong err = filemap_write_and_wait(inode->i_mapping); 58214c546592SDaeho Jeong if (err < 0) { 58224c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 58234c546592SDaeho Jeong return err; 58244c546592SDaeho Jeong } 58254c546592SDaeho Jeong } 58264c546592SDaeho Jeong 5827c8585c6fSDaeho Jeong percpu_down_write(&sbi->s_journal_flag_rwsem); 5828dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5829ac27a0ecSDave Kleikamp 5830ac27a0ecSDave Kleikamp /* 5831ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5832ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5833ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5834ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5835ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5836ac27a0ecSDave Kleikamp */ 5837ac27a0ecSDave Kleikamp 5838ac27a0ecSDave Kleikamp if (val) 583912e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 58405872ddaaSYongqiang Yang else { 58414f879ca6SJan Kara err = jbd2_journal_flush(journal); 58424f879ca6SJan Kara if (err < 0) { 58434f879ca6SJan Kara jbd2_journal_unlock_updates(journal); 5844c8585c6fSDaeho Jeong percpu_up_write(&sbi->s_journal_flag_rwsem); 58454f879ca6SJan Kara return err; 58464f879ca6SJan Kara } 584712e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 58485872ddaaSYongqiang Yang } 5849617ba13bSMingming Cao ext4_set_aops(inode); 5850ac27a0ecSDave Kleikamp 5851dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5852c8585c6fSDaeho Jeong percpu_up_write(&sbi->s_journal_flag_rwsem); 5853c8585c6fSDaeho Jeong 58544c546592SDaeho Jeong if (val) 58554c546592SDaeho Jeong up_write(&EXT4_I(inode)->i_mmap_sem); 5856ac27a0ecSDave Kleikamp 5857ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5858ac27a0ecSDave Kleikamp 58599924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5860ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5861ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5862ac27a0ecSDave Kleikamp 5863617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 58640390131bSFrank Mayhar ext4_handle_sync(handle); 5865617ba13bSMingming Cao ext4_journal_stop(handle); 5866617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5867ac27a0ecSDave Kleikamp 5868ac27a0ecSDave Kleikamp return err; 5869ac27a0ecSDave Kleikamp } 58702e9ee850SAneesh Kumar K.V 58712e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 58722e9ee850SAneesh Kumar K.V { 58732e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 58742e9ee850SAneesh Kumar K.V } 58752e9ee850SAneesh Kumar K.V 5876401b25aaSSouptick Joarder vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf) 58772e9ee850SAneesh Kumar K.V { 587811bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 5879c2ec175cSNick Piggin struct page *page = vmf->page; 58802e9ee850SAneesh Kumar K.V loff_t size; 58812e9ee850SAneesh Kumar K.V unsigned long len; 5882401b25aaSSouptick Joarder int err; 5883401b25aaSSouptick Joarder vm_fault_t ret; 58842e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5885496ad9aaSAl Viro struct inode *inode = file_inode(file); 58862e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 58879ea7df53SJan Kara handle_t *handle; 58889ea7df53SJan Kara get_block_t *get_block; 58899ea7df53SJan Kara int retries = 0; 58902e9ee850SAneesh Kumar K.V 589102b016caSTheodore Ts'o if (unlikely(IS_IMMUTABLE(inode))) 589202b016caSTheodore Ts'o return VM_FAULT_SIGBUS; 589302b016caSTheodore Ts'o 58948e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5895041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 5896ea3d7209SJan Kara 5897ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 58987b4cc978SEric Biggers 5899401b25aaSSouptick Joarder err = ext4_convert_inline_data(inode); 5900401b25aaSSouptick Joarder if (err) 59017b4cc978SEric Biggers goto out_ret; 59027b4cc978SEric Biggers 59039ea7df53SJan Kara /* Delalloc case is easy... */ 59049ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 59059ea7df53SJan Kara !ext4_should_journal_data(inode) && 59069ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 59079ea7df53SJan Kara do { 5908401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, 59099ea7df53SJan Kara ext4_da_get_block_prep); 5910401b25aaSSouptick Joarder } while (err == -ENOSPC && 59119ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 59129ea7df53SJan Kara goto out_ret; 59132e9ee850SAneesh Kumar K.V } 59140e499890SDarrick J. Wong 59150e499890SDarrick J. Wong lock_page(page); 59169ea7df53SJan Kara size = i_size_read(inode); 59179ea7df53SJan Kara /* Page got truncated from under us? */ 59189ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 59199ea7df53SJan Kara unlock_page(page); 59209ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 59219ea7df53SJan Kara goto out; 59220e499890SDarrick J. Wong } 59232e9ee850SAneesh Kumar K.V 592409cbfeafSKirill A. Shutemov if (page->index == size >> PAGE_SHIFT) 592509cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 59262e9ee850SAneesh Kumar K.V else 592709cbfeafSKirill A. Shutemov len = PAGE_SIZE; 5928a827eaffSAneesh Kumar K.V /* 59299ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 59309ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 5931a827eaffSAneesh Kumar K.V */ 59322e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 5933f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 5934f19d5870STao Ma 0, len, NULL, 5935a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 59369ea7df53SJan Kara /* Wait so that we don't change page under IO */ 59371d1d1a76SDarrick J. Wong wait_for_stable_page(page); 59389ea7df53SJan Kara ret = VM_FAULT_LOCKED; 59399ea7df53SJan Kara goto out; 59402e9ee850SAneesh Kumar K.V } 5941a827eaffSAneesh Kumar K.V } 5942a827eaffSAneesh Kumar K.V unlock_page(page); 59439ea7df53SJan Kara /* OK, we need to fill the hole... */ 59449ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 5945705965bdSJan Kara get_block = ext4_get_block_unwritten; 59469ea7df53SJan Kara else 59479ea7df53SJan Kara get_block = ext4_get_block; 59489ea7df53SJan Kara retry_alloc: 59499924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 59509924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 59519ea7df53SJan Kara if (IS_ERR(handle)) { 5952c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 59539ea7df53SJan Kara goto out; 59549ea7df53SJan Kara } 5955401b25aaSSouptick Joarder err = block_page_mkwrite(vma, vmf, get_block); 5956401b25aaSSouptick Joarder if (!err && ext4_should_journal_data(inode)) { 5957f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 595809cbfeafSKirill A. Shutemov PAGE_SIZE, NULL, do_journal_get_write_access)) { 59599ea7df53SJan Kara unlock_page(page); 59609ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5961fcbb5515SYongqiang Yang ext4_journal_stop(handle); 59629ea7df53SJan Kara goto out; 59639ea7df53SJan Kara } 59649ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 59659ea7df53SJan Kara } 59669ea7df53SJan Kara ext4_journal_stop(handle); 5967401b25aaSSouptick Joarder if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 59689ea7df53SJan Kara goto retry_alloc; 59699ea7df53SJan Kara out_ret: 5970401b25aaSSouptick Joarder ret = block_page_mkwrite_return(err); 59719ea7df53SJan Kara out: 5972ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 59738e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 59742e9ee850SAneesh Kumar K.V return ret; 59752e9ee850SAneesh Kumar K.V } 5976ea3d7209SJan Kara 5977401b25aaSSouptick Joarder vm_fault_t ext4_filemap_fault(struct vm_fault *vmf) 5978ea3d7209SJan Kara { 597911bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file); 5980401b25aaSSouptick Joarder vm_fault_t ret; 5981ea3d7209SJan Kara 5982ea3d7209SJan Kara down_read(&EXT4_I(inode)->i_mmap_sem); 5983401b25aaSSouptick Joarder ret = filemap_fault(vmf); 5984ea3d7209SJan Kara up_read(&EXT4_I(inode)->i_mmap_sem); 5985ea3d7209SJan Kara 5986401b25aaSSouptick Joarder return ret; 5987ea3d7209SJan Kara } 5988