1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao * linux/fs/ext4/file.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/file.c
13ac27a0ecSDave Kleikamp *
14ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds
15ac27a0ecSDave Kleikamp *
16617ba13bSMingming Cao * ext4 fs regular file handling primitives
17ac27a0ecSDave Kleikamp *
18ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek
19ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz)
20ac27a0ecSDave Kleikamp */
21ac27a0ecSDave Kleikamp
22ac27a0ecSDave Kleikamp #include <linux/time.h>
23ac27a0ecSDave Kleikamp #include <linux/fs.h>
24545052e9SChristoph Hellwig #include <linux/iomap.h>
25bc0b0d6dSTheodore Ts'o #include <linux/mount.h>
26bc0b0d6dSTheodore Ts'o #include <linux/path.h>
27c94c2acfSMatthew Wilcox #include <linux/dax.h>
28871a2931SChristoph Hellwig #include <linux/quotaops.h>
29c8c0df24SZheng Liu #include <linux/pagevec.h>
30e2e40f2cSChristoph Hellwig #include <linux/uio.h>
31b8a6176cSJan Kara #include <linux/mman.h>
32378f32baSMatthew Bobrowski #include <linux/backing-dev.h>
333dcf5451SChristoph Hellwig #include "ext4.h"
343dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
35ac27a0ecSDave Kleikamp #include "xattr.h"
36ac27a0ecSDave Kleikamp #include "acl.h"
37569342dcSMatthew Bobrowski #include "truncate.h"
38ac27a0ecSDave Kleikamp
398434ef1dSEric Biggers /*
408434ef1dSEric Biggers * Returns %true if the given DIO request should be attempted with DIO, or
418434ef1dSEric Biggers * %false if it should fall back to buffered I/O.
428434ef1dSEric Biggers *
438434ef1dSEric Biggers * DIO isn't well specified; when it's unsupported (either due to the request
448434ef1dSEric Biggers * being misaligned, or due to the file not supporting DIO at all), filesystems
458434ef1dSEric Biggers * either fall back to buffered I/O or return EINVAL. For files that don't use
468434ef1dSEric Biggers * any special features like encryption or verity, ext4 has traditionally
478434ef1dSEric Biggers * returned EINVAL for misaligned DIO. iomap_dio_rw() uses this convention too.
488434ef1dSEric Biggers * In this case, we should attempt the DIO, *not* fall back to buffered I/O.
498434ef1dSEric Biggers *
508434ef1dSEric Biggers * In contrast, in cases where DIO is unsupported due to ext4 features, ext4
518434ef1dSEric Biggers * traditionally falls back to buffered I/O.
528434ef1dSEric Biggers *
538434ef1dSEric Biggers * This function implements the traditional ext4 behavior in all these cases.
548434ef1dSEric Biggers */
ext4_should_use_dio(struct kiocb * iocb,struct iov_iter * iter)558434ef1dSEric Biggers static bool ext4_should_use_dio(struct kiocb *iocb, struct iov_iter *iter)
56b1b4705dSMatthew Bobrowski {
5738ea50daSEric Biggers struct inode *inode = file_inode(iocb->ki_filp);
588434ef1dSEric Biggers u32 dio_align = ext4_dio_alignment(inode);
5938ea50daSEric Biggers
608434ef1dSEric Biggers if (dio_align == 0)
61b1b4705dSMatthew Bobrowski return false;
628434ef1dSEric Biggers
638434ef1dSEric Biggers if (dio_align == 1)
64b1b4705dSMatthew Bobrowski return true;
658434ef1dSEric Biggers
668434ef1dSEric Biggers return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align);
67b1b4705dSMatthew Bobrowski }
68b1b4705dSMatthew Bobrowski
ext4_dio_read_iter(struct kiocb * iocb,struct iov_iter * to)69b1b4705dSMatthew Bobrowski static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
70b1b4705dSMatthew Bobrowski {
71b1b4705dSMatthew Bobrowski ssize_t ret;
72b1b4705dSMatthew Bobrowski struct inode *inode = file_inode(iocb->ki_filp);
73b1b4705dSMatthew Bobrowski
74b1b4705dSMatthew Bobrowski if (iocb->ki_flags & IOCB_NOWAIT) {
75b1b4705dSMatthew Bobrowski if (!inode_trylock_shared(inode))
76b1b4705dSMatthew Bobrowski return -EAGAIN;
77b1b4705dSMatthew Bobrowski } else {
78b1b4705dSMatthew Bobrowski inode_lock_shared(inode);
79b1b4705dSMatthew Bobrowski }
80b1b4705dSMatthew Bobrowski
818434ef1dSEric Biggers if (!ext4_should_use_dio(iocb, to)) {
82b1b4705dSMatthew Bobrowski inode_unlock_shared(inode);
83b1b4705dSMatthew Bobrowski /*
84b1b4705dSMatthew Bobrowski * Fallback to buffered I/O if the operation being performed on
85b1b4705dSMatthew Bobrowski * the inode is not supported by direct I/O. The IOCB_DIRECT
86b1b4705dSMatthew Bobrowski * flag needs to be cleared here in order to ensure that the
87b1b4705dSMatthew Bobrowski * direct I/O path within generic_file_read_iter() is not
88b1b4705dSMatthew Bobrowski * taken.
89b1b4705dSMatthew Bobrowski */
90b1b4705dSMatthew Bobrowski iocb->ki_flags &= ~IOCB_DIRECT;
91b1b4705dSMatthew Bobrowski return generic_file_read_iter(iocb, to);
92b1b4705dSMatthew Bobrowski }
93b1b4705dSMatthew Bobrowski
94786f847fSChristoph Hellwig ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL, 0, NULL, 0);
95b1b4705dSMatthew Bobrowski inode_unlock_shared(inode);
96b1b4705dSMatthew Bobrowski
97b1b4705dSMatthew Bobrowski file_accessed(iocb->ki_filp);
98b1b4705dSMatthew Bobrowski return ret;
99b1b4705dSMatthew Bobrowski }
100b1b4705dSMatthew Bobrowski
101364443cbSJan Kara #ifdef CONFIG_FS_DAX
ext4_dax_read_iter(struct kiocb * iocb,struct iov_iter * to)102364443cbSJan Kara static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
103364443cbSJan Kara {
104364443cbSJan Kara struct inode *inode = file_inode(iocb->ki_filp);
105364443cbSJan Kara ssize_t ret;
106364443cbSJan Kara
107f629afe3SRitesh Harjani if (iocb->ki_flags & IOCB_NOWAIT) {
108f629afe3SRitesh Harjani if (!inode_trylock_shared(inode))
109728fbc0eSGoldwyn Rodrigues return -EAGAIN;
110f629afe3SRitesh Harjani } else {
111364443cbSJan Kara inode_lock_shared(inode);
112728fbc0eSGoldwyn Rodrigues }
113364443cbSJan Kara /*
114364443cbSJan Kara * Recheck under inode lock - at this point we are sure it cannot
115364443cbSJan Kara * change anymore
116364443cbSJan Kara */
117364443cbSJan Kara if (!IS_DAX(inode)) {
118364443cbSJan Kara inode_unlock_shared(inode);
119364443cbSJan Kara /* Fallback to buffered IO in case we cannot support DAX */
120364443cbSJan Kara return generic_file_read_iter(iocb, to);
121364443cbSJan Kara }
122364443cbSJan Kara ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
123364443cbSJan Kara inode_unlock_shared(inode);
124364443cbSJan Kara
125364443cbSJan Kara file_accessed(iocb->ki_filp);
126364443cbSJan Kara return ret;
127364443cbSJan Kara }
128364443cbSJan Kara #endif
129364443cbSJan Kara
ext4_file_read_iter(struct kiocb * iocb,struct iov_iter * to)130364443cbSJan Kara static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
131364443cbSJan Kara {
132b1b4705dSMatthew Bobrowski struct inode *inode = file_inode(iocb->ki_filp);
133b1b4705dSMatthew Bobrowski
134eb8ab444SJan Kara if (unlikely(ext4_forced_shutdown(inode->i_sb)))
1350db1ff22STheodore Ts'o return -EIO;
1360db1ff22STheodore Ts'o
137364443cbSJan Kara if (!iov_iter_count(to))
138364443cbSJan Kara return 0; /* skip atime */
139364443cbSJan Kara
140364443cbSJan Kara #ifdef CONFIG_FS_DAX
141b1b4705dSMatthew Bobrowski if (IS_DAX(inode))
142364443cbSJan Kara return ext4_dax_read_iter(iocb, to);
143364443cbSJan Kara #endif
144b1b4705dSMatthew Bobrowski if (iocb->ki_flags & IOCB_DIRECT)
145b1b4705dSMatthew Bobrowski return ext4_dio_read_iter(iocb, to);
146b1b4705dSMatthew Bobrowski
147364443cbSJan Kara return generic_file_read_iter(iocb, to);
148364443cbSJan Kara }
149364443cbSJan Kara
ext4_file_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)150fa6c46e7SDavid Howells static ssize_t ext4_file_splice_read(struct file *in, loff_t *ppos,
151fa6c46e7SDavid Howells struct pipe_inode_info *pipe,
152fa6c46e7SDavid Howells size_t len, unsigned int flags)
153fa6c46e7SDavid Howells {
154fa6c46e7SDavid Howells struct inode *inode = file_inode(in);
155fa6c46e7SDavid Howells
156eb8ab444SJan Kara if (unlikely(ext4_forced_shutdown(inode->i_sb)))
157fa6c46e7SDavid Howells return -EIO;
1582cb1e089SDavid Howells return filemap_splice_read(in, ppos, pipe, len, flags);
159fa6c46e7SDavid Howells }
160fa6c46e7SDavid Howells
161ac27a0ecSDave Kleikamp /*
162ac27a0ecSDave Kleikamp * Called when an inode is released. Note that this is different
163617ba13bSMingming Cao * from ext4_file_open: open gets called at every open, but release
164ac27a0ecSDave Kleikamp * gets called only when /all/ the files are closed.
165ac27a0ecSDave Kleikamp */
ext4_release_file(struct inode * inode,struct file * filp)166617ba13bSMingming Cao static int ext4_release_file(struct inode *inode, struct file *filp)
167ac27a0ecSDave Kleikamp {
16819f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
1697d8f9f7dSTheodore Ts'o ext4_alloc_da_blocks(inode);
17019f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
1717d8f9f7dSTheodore Ts'o }
172ac27a0ecSDave Kleikamp /* if we are the last writer on the inode, drop the block reservation */
173ac27a0ecSDave Kleikamp if ((filp->f_mode & FMODE_WRITE) &&
174d6014301SAneesh Kumar K.V (atomic_read(&inode->i_writecount) == 1) &&
175e030a288SDio Putra !EXT4_I(inode)->i_reserved_data_blocks) {
1760e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem);
17727bc446eSbrookxu ext4_discard_preallocations(inode, 0);
1780e855ac8SAneesh Kumar K.V up_write(&EXT4_I(inode)->i_data_sem);
179ac27a0ecSDave Kleikamp }
180ac27a0ecSDave Kleikamp if (is_dx(inode) && filp->private_data)
181617ba13bSMingming Cao ext4_htree_free_dir_info(filp->private_data);
182ac27a0ecSDave Kleikamp
183ac27a0ecSDave Kleikamp return 0;
184ac27a0ecSDave Kleikamp }
185ac27a0ecSDave Kleikamp
186e9e3bcecSEric Sandeen /*
187e9e3bcecSEric Sandeen * This tests whether the IO in question is block-aligned or not.
188e9e3bcecSEric Sandeen * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
189e9e3bcecSEric Sandeen * are converted to written only after the IO is complete. Until they are
190e9e3bcecSEric Sandeen * mapped, these blocks appear as holes, so dio_zero_block() will assume that
191e9e3bcecSEric Sandeen * it needs to zero out portions of the start and/or end block. If 2 AIO
192e9e3bcecSEric Sandeen * threads are at work on the same unwritten block, they must be synchronized
193e9e3bcecSEric Sandeen * or one thread will zero the other's data, causing corruption.
194e9e3bcecSEric Sandeen */
195aa9714d0SRitesh Harjani static bool
ext4_unaligned_io(struct inode * inode,struct iov_iter * from,loff_t pos)196aa9714d0SRitesh Harjani ext4_unaligned_io(struct inode *inode, struct iov_iter *from, loff_t pos)
197e9e3bcecSEric Sandeen {
198e9e3bcecSEric Sandeen struct super_block *sb = inode->i_sb;
199aa9714d0SRitesh Harjani unsigned long blockmask = sb->s_blocksize - 1;
200e9e3bcecSEric Sandeen
2019b884164SAl Viro if ((pos | iov_iter_alignment(from)) & blockmask)
202aa9714d0SRitesh Harjani return true;
203e9e3bcecSEric Sandeen
204aa9714d0SRitesh Harjani return false;
205aa9714d0SRitesh Harjani }
206aa9714d0SRitesh Harjani
207aa9714d0SRitesh Harjani static bool
ext4_extending_io(struct inode * inode,loff_t offset,size_t len)208aa9714d0SRitesh Harjani ext4_extending_io(struct inode *inode, loff_t offset, size_t len)
209aa9714d0SRitesh Harjani {
210aa9714d0SRitesh Harjani if (offset + len > i_size_read(inode) ||
211aa9714d0SRitesh Harjani offset + len > EXT4_I(inode)->i_disksize)
212aa9714d0SRitesh Harjani return true;
213aa9714d0SRitesh Harjani return false;
214e9e3bcecSEric Sandeen }
215e9e3bcecSEric Sandeen
216240930fbSZhang Yi /* Is IO overwriting allocated or initialized blocks? */
ext4_overwrite_io(struct inode * inode,loff_t pos,loff_t len,bool * unwritten)217240930fbSZhang Yi static bool ext4_overwrite_io(struct inode *inode,
218240930fbSZhang Yi loff_t pos, loff_t len, bool *unwritten)
219213bcd9cSJan Kara {
220213bcd9cSJan Kara struct ext4_map_blocks map;
221213bcd9cSJan Kara unsigned int blkbits = inode->i_blkbits;
222213bcd9cSJan Kara int err, blklen;
223213bcd9cSJan Kara
224213bcd9cSJan Kara if (pos + len > i_size_read(inode))
225213bcd9cSJan Kara return false;
226213bcd9cSJan Kara
227213bcd9cSJan Kara map.m_lblk = pos >> blkbits;
228213bcd9cSJan Kara map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
229213bcd9cSJan Kara blklen = map.m_len;
230213bcd9cSJan Kara
231213bcd9cSJan Kara err = ext4_map_blocks(NULL, inode, &map, 0);
232240930fbSZhang Yi if (err != blklen)
233240930fbSZhang Yi return false;
234213bcd9cSJan Kara /*
235213bcd9cSJan Kara * 'err==len' means that all of the blocks have been preallocated,
236240930fbSZhang Yi * regardless of whether they have been initialized or not. We need to
237240930fbSZhang Yi * check m_flags to distinguish the unwritten extents.
238213bcd9cSJan Kara */
239240930fbSZhang Yi *unwritten = !(map.m_flags & EXT4_MAP_MAPPED);
240240930fbSZhang Yi return true;
241213bcd9cSJan Kara }
242213bcd9cSJan Kara
ext4_generic_write_checks(struct kiocb * iocb,struct iov_iter * from)243aa9714d0SRitesh Harjani static ssize_t ext4_generic_write_checks(struct kiocb *iocb,
244aa9714d0SRitesh Harjani struct iov_iter *from)
245213bcd9cSJan Kara {
246213bcd9cSJan Kara struct inode *inode = file_inode(iocb->ki_filp);
247213bcd9cSJan Kara ssize_t ret;
248213bcd9cSJan Kara
249378f32baSMatthew Bobrowski if (unlikely(IS_IMMUTABLE(inode)))
250378f32baSMatthew Bobrowski return -EPERM;
251378f32baSMatthew Bobrowski
252213bcd9cSJan Kara ret = generic_write_checks(iocb, from);
253213bcd9cSJan Kara if (ret <= 0)
254213bcd9cSJan Kara return ret;
25502b016caSTheodore Ts'o
256213bcd9cSJan Kara /*
257213bcd9cSJan Kara * If we have encountered a bitmap-format file, the size limit
258213bcd9cSJan Kara * is smaller than s_maxbytes, which is for extent-mapped files.
259213bcd9cSJan Kara */
260213bcd9cSJan Kara if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
261213bcd9cSJan Kara struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
262213bcd9cSJan Kara
263213bcd9cSJan Kara if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
264213bcd9cSJan Kara return -EFBIG;
265213bcd9cSJan Kara iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
266213bcd9cSJan Kara }
267378f32baSMatthew Bobrowski
268aa9714d0SRitesh Harjani return iov_iter_count(from);
269aa9714d0SRitesh Harjani }
270aa9714d0SRitesh Harjani
ext4_write_checks(struct kiocb * iocb,struct iov_iter * from)271aa9714d0SRitesh Harjani static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
272aa9714d0SRitesh Harjani {
273aa9714d0SRitesh Harjani ssize_t ret, count;
274aa9714d0SRitesh Harjani
275aa9714d0SRitesh Harjani count = ext4_generic_write_checks(iocb, from);
276aa9714d0SRitesh Harjani if (count <= 0)
277aa9714d0SRitesh Harjani return count;
278aa9714d0SRitesh Harjani
279378f32baSMatthew Bobrowski ret = file_modified(iocb->ki_filp);
280378f32baSMatthew Bobrowski if (ret)
281378f32baSMatthew Bobrowski return ret;
282aa9714d0SRitesh Harjani return count;
283213bcd9cSJan Kara }
284213bcd9cSJan Kara
ext4_buffered_write_iter(struct kiocb * iocb,struct iov_iter * from)285378f32baSMatthew Bobrowski static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
286378f32baSMatthew Bobrowski struct iov_iter *from)
287378f32baSMatthew Bobrowski {
288378f32baSMatthew Bobrowski ssize_t ret;
289378f32baSMatthew Bobrowski struct inode *inode = file_inode(iocb->ki_filp);
290378f32baSMatthew Bobrowski
291378f32baSMatthew Bobrowski if (iocb->ki_flags & IOCB_NOWAIT)
292378f32baSMatthew Bobrowski return -EOPNOTSUPP;
293378f32baSMatthew Bobrowski
294378f32baSMatthew Bobrowski inode_lock(inode);
295378f32baSMatthew Bobrowski ret = ext4_write_checks(iocb, from);
296378f32baSMatthew Bobrowski if (ret <= 0)
297378f32baSMatthew Bobrowski goto out;
298378f32baSMatthew Bobrowski
299800ba295SMatthew Wilcox (Oracle) ret = generic_perform_write(iocb, from);
300378f32baSMatthew Bobrowski
301378f32baSMatthew Bobrowski out:
302378f32baSMatthew Bobrowski inode_unlock(inode);
303182c25e9SChristoph Hellwig if (unlikely(ret <= 0))
304378f32baSMatthew Bobrowski return ret;
305182c25e9SChristoph Hellwig return generic_write_sync(iocb, ret);
306378f32baSMatthew Bobrowski }
307378f32baSMatthew Bobrowski
ext4_handle_inode_extension(struct inode * inode,loff_t offset,ssize_t count)308569342dcSMatthew Bobrowski static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,
3093eb32071SJan Kara ssize_t count)
310569342dcSMatthew Bobrowski {
311569342dcSMatthew Bobrowski handle_t *handle;
312569342dcSMatthew Bobrowski
3133eb32071SJan Kara lockdep_assert_held_write(&inode->i_rwsem);
314569342dcSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
3153eb32071SJan Kara if (IS_ERR(handle))
316569342dcSMatthew Bobrowski return PTR_ERR(handle);
317569342dcSMatthew Bobrowski
3183eb32071SJan Kara if (ext4_update_inode_size(inode, offset + count)) {
3193eb32071SJan Kara int ret = ext4_mark_inode_dirty(handle, inode);
3204209ae12SHarshad Shirwadkar if (unlikely(ret)) {
3214209ae12SHarshad Shirwadkar ext4_journal_stop(handle);
3223eb32071SJan Kara return ret;
3234209ae12SHarshad Shirwadkar }
3244209ae12SHarshad Shirwadkar }
325569342dcSMatthew Bobrowski
3263eb32071SJan Kara if (inode->i_nlink)
327569342dcSMatthew Bobrowski ext4_orphan_del(handle, inode);
328569342dcSMatthew Bobrowski ext4_journal_stop(handle);
329569342dcSMatthew Bobrowski
3303eb32071SJan Kara return count;
3313eb32071SJan Kara }
3323eb32071SJan Kara
3333eb32071SJan Kara /*
3343eb32071SJan Kara * Clean up the inode after DIO or DAX extending write has completed and the
3353eb32071SJan Kara * inode size has been updated using ext4_handle_inode_extension().
3363eb32071SJan Kara */
ext4_inode_extension_cleanup(struct inode * inode,bool need_trunc)337*5efccdeeSZhihao Cheng static void ext4_inode_extension_cleanup(struct inode *inode, bool need_trunc)
3383eb32071SJan Kara {
3393eb32071SJan Kara lockdep_assert_held_write(&inode->i_rwsem);
340*5efccdeeSZhihao Cheng if (need_trunc) {
341569342dcSMatthew Bobrowski ext4_truncate_failed_write(inode);
342569342dcSMatthew Bobrowski /*
343569342dcSMatthew Bobrowski * If the truncate operation failed early, then the inode may
344569342dcSMatthew Bobrowski * still be on the orphan list. In that case, we need to try
345569342dcSMatthew Bobrowski * remove the inode from the in-memory linked list.
346569342dcSMatthew Bobrowski */
347569342dcSMatthew Bobrowski if (inode->i_nlink)
348569342dcSMatthew Bobrowski ext4_orphan_del(NULL, inode);
3493eb32071SJan Kara return;
350569342dcSMatthew Bobrowski }
3513eb32071SJan Kara /*
35273dddf98SJan Kara * If i_disksize got extended either due to writeback of delalloc
35373dddf98SJan Kara * blocks or extending truncate while the DIO was running we could fail
35473dddf98SJan Kara * to cleanup the orphan list in ext4_handle_inode_extension(). Do it
35573dddf98SJan Kara * now.
3563eb32071SJan Kara */
3573eb32071SJan Kara if (!list_empty(&EXT4_I(inode)->i_orphan) && inode->i_nlink) {
3583eb32071SJan Kara handle_t *handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
359569342dcSMatthew Bobrowski
3603eb32071SJan Kara if (IS_ERR(handle)) {
3613eb32071SJan Kara /*
3623eb32071SJan Kara * The write has successfully completed. Not much to
3633eb32071SJan Kara * do with the error here so just cleanup the orphan
3643eb32071SJan Kara * list and hope for the best.
3653eb32071SJan Kara */
3663eb32071SJan Kara ext4_orphan_del(NULL, inode);
3673eb32071SJan Kara return;
3683eb32071SJan Kara }
3693eb32071SJan Kara ext4_orphan_del(handle, inode);
3703eb32071SJan Kara ext4_journal_stop(handle);
3713eb32071SJan Kara }
372569342dcSMatthew Bobrowski }
373569342dcSMatthew Bobrowski
ext4_dio_write_end_io(struct kiocb * iocb,ssize_t size,int error,unsigned int flags)374378f32baSMatthew Bobrowski static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
375378f32baSMatthew Bobrowski int error, unsigned int flags)
376378f32baSMatthew Bobrowski {
3775899593fSJan Kara loff_t pos = iocb->ki_pos;
378378f32baSMatthew Bobrowski struct inode *inode = file_inode(iocb->ki_filp);
379378f32baSMatthew Bobrowski
3803eb32071SJan Kara if (!error && size && flags & IOMAP_DIO_UNWRITTEN)
3813eb32071SJan Kara error = ext4_convert_unwritten_extents(NULL, inode, pos, size);
382378f32baSMatthew Bobrowski if (error)
383378f32baSMatthew Bobrowski return error;
3845899593fSJan Kara /*
3853eb32071SJan Kara * Note that EXT4_I(inode)->i_disksize can get extended up to
3863eb32071SJan Kara * inode->i_size while the I/O was running due to writeback of delalloc
3873eb32071SJan Kara * blocks. But the code in ext4_iomap_alloc() is careful to use
3883eb32071SJan Kara * zeroed/unwritten extents if this is possible; thus we won't leave
3893eb32071SJan Kara * uninitialized blocks in a file even if we didn't succeed in writing
39073dddf98SJan Kara * as much as we intended. Also we can race with truncate or write
39173dddf98SJan Kara * expanding the file so we have to be a bit careful here.
3925899593fSJan Kara */
39373dddf98SJan Kara if (pos + size <= READ_ONCE(EXT4_I(inode)->i_disksize) &&
39473dddf98SJan Kara pos + size <= i_size_read(inode))
3953eb32071SJan Kara return size;
3963eb32071SJan Kara return ext4_handle_inode_extension(inode, pos, size);
397378f32baSMatthew Bobrowski }
398378f32baSMatthew Bobrowski
399378f32baSMatthew Bobrowski static const struct iomap_dio_ops ext4_dio_write_ops = {
400378f32baSMatthew Bobrowski .end_io = ext4_dio_write_end_io,
401378f32baSMatthew Bobrowski };
402378f32baSMatthew Bobrowski
403378f32baSMatthew Bobrowski /*
404aa9714d0SRitesh Harjani * The intention here is to start with shared lock acquired then see if any
405aa9714d0SRitesh Harjani * condition requires an exclusive inode lock. If yes, then we restart the
406aa9714d0SRitesh Harjani * whole operation by releasing the shared lock and acquiring exclusive lock.
407aa9714d0SRitesh Harjani *
408aa9714d0SRitesh Harjani * - For unaligned_io we never take shared lock as it may cause data corruption
409aa9714d0SRitesh Harjani * when two unaligned IO tries to modify the same block e.g. while zeroing.
410aa9714d0SRitesh Harjani *
411aa9714d0SRitesh Harjani * - For extending writes case we don't take the shared lock, since it requires
412aa9714d0SRitesh Harjani * updating inode i_disksize and/or orphan handling with exclusive lock.
413aa9714d0SRitesh Harjani *
414240930fbSZhang Yi * - shared locking will only be true mostly with overwrites, including
415240930fbSZhang Yi * initialized blocks and unwritten blocks. For overwrite unwritten blocks
416240930fbSZhang Yi * we protect splitting extents by i_data_sem in ext4_inode_info, so we can
417240930fbSZhang Yi * also release exclusive i_rwsem lock.
418240930fbSZhang Yi *
419240930fbSZhang Yi * - Otherwise we will switch to exclusive i_rwsem lock.
420378f32baSMatthew Bobrowski */
ext4_dio_write_checks(struct kiocb * iocb,struct iov_iter * from,bool * ilock_shared,bool * extend,bool * unwritten,int * dio_flags)421aa9714d0SRitesh Harjani static ssize_t ext4_dio_write_checks(struct kiocb *iocb, struct iov_iter *from,
422240930fbSZhang Yi bool *ilock_shared, bool *extend,
423310ee090SBrian Foster bool *unwritten, int *dio_flags)
424aa9714d0SRitesh Harjani {
425aa9714d0SRitesh Harjani struct file *file = iocb->ki_filp;
426aa9714d0SRitesh Harjani struct inode *inode = file_inode(file);
427aa9714d0SRitesh Harjani loff_t offset;
428aa9714d0SRitesh Harjani size_t count;
429aa9714d0SRitesh Harjani ssize_t ret;
430310ee090SBrian Foster bool overwrite, unaligned_io;
431aa9714d0SRitesh Harjani
432aa9714d0SRitesh Harjani restart:
433aa9714d0SRitesh Harjani ret = ext4_generic_write_checks(iocb, from);
434aa9714d0SRitesh Harjani if (ret <= 0)
435aa9714d0SRitesh Harjani goto out;
436aa9714d0SRitesh Harjani
437aa9714d0SRitesh Harjani offset = iocb->ki_pos;
438aa9714d0SRitesh Harjani count = ret;
439310ee090SBrian Foster
440310ee090SBrian Foster unaligned_io = ext4_unaligned_io(inode, from, offset);
441310ee090SBrian Foster *extend = ext4_extending_io(inode, offset, count);
442310ee090SBrian Foster overwrite = ext4_overwrite_io(inode, offset, count, unwritten);
443310ee090SBrian Foster
444aa9714d0SRitesh Harjani /*
445310ee090SBrian Foster * Determine whether we need to upgrade to an exclusive lock. This is
446310ee090SBrian Foster * required to change security info in file_modified(), for extending
447310ee090SBrian Foster * I/O, any form of non-overwrite I/O, and unaligned I/O to unwritten
448310ee090SBrian Foster * extents (as partial block zeroing may be required).
449194505b5SBrian Foster *
450194505b5SBrian Foster * Note that unaligned writes are allowed under shared lock so long as
451194505b5SBrian Foster * they are pure overwrites. Otherwise, concurrent unaligned writes risk
452194505b5SBrian Foster * data corruption due to partial block zeroing in the dio layer, and so
453194505b5SBrian Foster * the I/O must occur exclusively.
454aa9714d0SRitesh Harjani */
455310ee090SBrian Foster if (*ilock_shared &&
456310ee090SBrian Foster ((!IS_NOSEC(inode) || *extend || !overwrite ||
457310ee090SBrian Foster (unaligned_io && *unwritten)))) {
4580b3171b6SJan Kara if (iocb->ki_flags & IOCB_NOWAIT) {
4590b3171b6SJan Kara ret = -EAGAIN;
4600b3171b6SJan Kara goto out;
4610b3171b6SJan Kara }
462aa9714d0SRitesh Harjani inode_unlock_shared(inode);
463aa9714d0SRitesh Harjani *ilock_shared = false;
464aa9714d0SRitesh Harjani inode_lock(inode);
465aa9714d0SRitesh Harjani goto restart;
466378f32baSMatthew Bobrowski }
467378f32baSMatthew Bobrowski
468310ee090SBrian Foster /*
469310ee090SBrian Foster * Now that locking is settled, determine dio flags and exclusivity
470194505b5SBrian Foster * requirements. We don't use DIO_OVERWRITE_ONLY because we enforce
471194505b5SBrian Foster * behavior already. The inode lock is already held exclusive if the
472194505b5SBrian Foster * write is non-overwrite or extending, so drain all outstanding dio and
473194505b5SBrian Foster * set the force wait dio flag.
474310ee090SBrian Foster */
475194505b5SBrian Foster if (!*ilock_shared && (unaligned_io || *extend)) {
476310ee090SBrian Foster if (iocb->ki_flags & IOCB_NOWAIT) {
477310ee090SBrian Foster ret = -EAGAIN;
478310ee090SBrian Foster goto out;
479310ee090SBrian Foster }
480310ee090SBrian Foster if (unaligned_io && (!overwrite || *unwritten))
481310ee090SBrian Foster inode_dio_wait(inode);
482310ee090SBrian Foster *dio_flags = IOMAP_DIO_FORCE_WAIT;
483310ee090SBrian Foster }
484310ee090SBrian Foster
485aa9714d0SRitesh Harjani ret = file_modified(file);
486aa9714d0SRitesh Harjani if (ret < 0)
487aa9714d0SRitesh Harjani goto out;
488aa9714d0SRitesh Harjani
489aa9714d0SRitesh Harjani return count;
490aa9714d0SRitesh Harjani out:
491aa9714d0SRitesh Harjani if (*ilock_shared)
492aa9714d0SRitesh Harjani inode_unlock_shared(inode);
493aa9714d0SRitesh Harjani else
494378f32baSMatthew Bobrowski inode_unlock(inode);
495378f32baSMatthew Bobrowski return ret;
496378f32baSMatthew Bobrowski }
497378f32baSMatthew Bobrowski
ext4_dio_write_iter(struct kiocb * iocb,struct iov_iter * from)498aa9714d0SRitesh Harjani static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
499aa9714d0SRitesh Harjani {
500aa9714d0SRitesh Harjani ssize_t ret;
501aa9714d0SRitesh Harjani handle_t *handle;
502aa9714d0SRitesh Harjani struct inode *inode = file_inode(iocb->ki_filp);
503aa9714d0SRitesh Harjani loff_t offset = iocb->ki_pos;
504aa9714d0SRitesh Harjani size_t count = iov_iter_count(from);
5058cd115bdSJan Kara const struct iomap_ops *iomap_ops = &ext4_iomap_ops;
506310ee090SBrian Foster bool extend = false, unwritten = false;
507aa9714d0SRitesh Harjani bool ilock_shared = true;
508310ee090SBrian Foster int dio_flags = 0;
509aa9714d0SRitesh Harjani
510378f32baSMatthew Bobrowski /*
511aa9714d0SRitesh Harjani * Quick check here without any i_rwsem lock to see if it is extending
512aa9714d0SRitesh Harjani * IO. A more reliable check is done in ext4_dio_write_checks() with
513aa9714d0SRitesh Harjani * proper locking in place.
514aa9714d0SRitesh Harjani */
515aa9714d0SRitesh Harjani if (offset + count > i_size_read(inode))
516aa9714d0SRitesh Harjani ilock_shared = false;
517aa9714d0SRitesh Harjani
518aa9714d0SRitesh Harjani if (iocb->ki_flags & IOCB_NOWAIT) {
519aa9714d0SRitesh Harjani if (ilock_shared) {
520aa9714d0SRitesh Harjani if (!inode_trylock_shared(inode))
521aa9714d0SRitesh Harjani return -EAGAIN;
522aa9714d0SRitesh Harjani } else {
523aa9714d0SRitesh Harjani if (!inode_trylock(inode))
524aa9714d0SRitesh Harjani return -EAGAIN;
525aa9714d0SRitesh Harjani }
526aa9714d0SRitesh Harjani } else {
527aa9714d0SRitesh Harjani if (ilock_shared)
528aa9714d0SRitesh Harjani inode_lock_shared(inode);
529aa9714d0SRitesh Harjani else
530aa9714d0SRitesh Harjani inode_lock(inode);
531aa9714d0SRitesh Harjani }
532aa9714d0SRitesh Harjani
533aa9714d0SRitesh Harjani /* Fallback to buffered I/O if the inode does not support direct I/O. */
5348434ef1dSEric Biggers if (!ext4_should_use_dio(iocb, from)) {
535aa9714d0SRitesh Harjani if (ilock_shared)
536aa9714d0SRitesh Harjani inode_unlock_shared(inode);
537aa9714d0SRitesh Harjani else
538aa9714d0SRitesh Harjani inode_unlock(inode);
539aa9714d0SRitesh Harjani return ext4_buffered_write_iter(iocb, from);
540aa9714d0SRitesh Harjani }
541aa9714d0SRitesh Harjani
5427343c23eSBrian Foster /*
5437343c23eSBrian Foster * Prevent inline data from being created since we are going to allocate
5447343c23eSBrian Foster * blocks for DIO. We know the inode does not currently have inline data
5457343c23eSBrian Foster * because ext4_should_use_dio() checked for it, but we have to clear
5467343c23eSBrian Foster * the state flag before the write checks because a lock cycle could
5477343c23eSBrian Foster * introduce races with other writers.
5487343c23eSBrian Foster */
5497343c23eSBrian Foster ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
5507343c23eSBrian Foster
551310ee090SBrian Foster ret = ext4_dio_write_checks(iocb, from, &ilock_shared, &extend,
552310ee090SBrian Foster &unwritten, &dio_flags);
553aa9714d0SRitesh Harjani if (ret <= 0)
554aa9714d0SRitesh Harjani return ret;
555aa9714d0SRitesh Harjani
556378f32baSMatthew Bobrowski offset = iocb->ki_pos;
557aa9714d0SRitesh Harjani count = ret;
558378f32baSMatthew Bobrowski
559aa9714d0SRitesh Harjani if (extend) {
560378f32baSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
561378f32baSMatthew Bobrowski if (IS_ERR(handle)) {
562378f32baSMatthew Bobrowski ret = PTR_ERR(handle);
563378f32baSMatthew Bobrowski goto out;
564378f32baSMatthew Bobrowski }
565378f32baSMatthew Bobrowski
566378f32baSMatthew Bobrowski ret = ext4_orphan_add(handle, inode);
567378f32baSMatthew Bobrowski if (ret) {
568378f32baSMatthew Bobrowski ext4_journal_stop(handle);
569378f32baSMatthew Bobrowski goto out;
570378f32baSMatthew Bobrowski }
571378f32baSMatthew Bobrowski
572378f32baSMatthew Bobrowski ext4_journal_stop(handle);
573378f32baSMatthew Bobrowski }
574378f32baSMatthew Bobrowski
575240930fbSZhang Yi if (ilock_shared && !unwritten)
5768cd115bdSJan Kara iomap_ops = &ext4_iomap_overwrite_ops;
5778cd115bdSJan Kara ret = iomap_dio_rw(iocb, from, iomap_ops, &ext4_dio_write_ops,
578310ee090SBrian Foster dio_flags, NULL, 0);
57960263d58SChristoph Hellwig if (ret == -ENOTBLK)
58060263d58SChristoph Hellwig ret = 0;
5813eb32071SJan Kara if (extend) {
5823eb32071SJan Kara /*
5833eb32071SJan Kara * We always perform extending DIO write synchronously so by
5843eb32071SJan Kara * now the IO is completed and ext4_handle_inode_extension()
5853eb32071SJan Kara * was called. Cleanup the inode in case of error or race with
5863eb32071SJan Kara * writeback of delalloc blocks.
5873eb32071SJan Kara */
5883eb32071SJan Kara WARN_ON_ONCE(ret == -EIOCBQUEUED);
589*5efccdeeSZhihao Cheng ext4_inode_extension_cleanup(inode, ret < 0);
5903eb32071SJan Kara }
591378f32baSMatthew Bobrowski
592378f32baSMatthew Bobrowski out:
593aa9714d0SRitesh Harjani if (ilock_shared)
594378f32baSMatthew Bobrowski inode_unlock_shared(inode);
595378f32baSMatthew Bobrowski else
596378f32baSMatthew Bobrowski inode_unlock(inode);
597378f32baSMatthew Bobrowski
598378f32baSMatthew Bobrowski if (ret >= 0 && iov_iter_count(from)) {
599378f32baSMatthew Bobrowski ssize_t err;
600378f32baSMatthew Bobrowski loff_t endbyte;
601378f32baSMatthew Bobrowski
602378f32baSMatthew Bobrowski offset = iocb->ki_pos;
603378f32baSMatthew Bobrowski err = ext4_buffered_write_iter(iocb, from);
604378f32baSMatthew Bobrowski if (err < 0)
605378f32baSMatthew Bobrowski return err;
606378f32baSMatthew Bobrowski
607378f32baSMatthew Bobrowski /*
608378f32baSMatthew Bobrowski * We need to ensure that the pages within the page cache for
609378f32baSMatthew Bobrowski * the range covered by this I/O are written to disk and
610378f32baSMatthew Bobrowski * invalidated. This is in attempt to preserve the expected
611378f32baSMatthew Bobrowski * direct I/O semantics in the case we fallback to buffered I/O
612378f32baSMatthew Bobrowski * to complete off the I/O request.
613378f32baSMatthew Bobrowski */
614378f32baSMatthew Bobrowski ret += err;
615378f32baSMatthew Bobrowski endbyte = offset + err - 1;
616378f32baSMatthew Bobrowski err = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
617378f32baSMatthew Bobrowski offset, endbyte);
618378f32baSMatthew Bobrowski if (!err)
619378f32baSMatthew Bobrowski invalidate_mapping_pages(iocb->ki_filp->f_mapping,
620378f32baSMatthew Bobrowski offset >> PAGE_SHIFT,
621378f32baSMatthew Bobrowski endbyte >> PAGE_SHIFT);
622378f32baSMatthew Bobrowski }
623378f32baSMatthew Bobrowski
624378f32baSMatthew Bobrowski return ret;
625378f32baSMatthew Bobrowski }
626378f32baSMatthew Bobrowski
627776722e8SJan Kara #ifdef CONFIG_FS_DAX
628776722e8SJan Kara static ssize_t
ext4_dax_write_iter(struct kiocb * iocb,struct iov_iter * from)629776722e8SJan Kara ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
630776722e8SJan Kara {
631776722e8SJan Kara ssize_t ret;
632569342dcSMatthew Bobrowski size_t count;
633569342dcSMatthew Bobrowski loff_t offset;
6340b9f230bSMatthew Bobrowski handle_t *handle;
6350b9f230bSMatthew Bobrowski bool extend = false;
636569342dcSMatthew Bobrowski struct inode *inode = file_inode(iocb->ki_filp);
637776722e8SJan Kara
638f629afe3SRitesh Harjani if (iocb->ki_flags & IOCB_NOWAIT) {
639f629afe3SRitesh Harjani if (!inode_trylock(inode))
640728fbc0eSGoldwyn Rodrigues return -EAGAIN;
641f629afe3SRitesh Harjani } else {
642776722e8SJan Kara inode_lock(inode);
643728fbc0eSGoldwyn Rodrigues }
644378f32baSMatthew Bobrowski
645776722e8SJan Kara ret = ext4_write_checks(iocb, from);
646776722e8SJan Kara if (ret <= 0)
647776722e8SJan Kara goto out;
648776722e8SJan Kara
649569342dcSMatthew Bobrowski offset = iocb->ki_pos;
650569342dcSMatthew Bobrowski count = iov_iter_count(from);
6510b9f230bSMatthew Bobrowski
6520b9f230bSMatthew Bobrowski if (offset + count > EXT4_I(inode)->i_disksize) {
6530b9f230bSMatthew Bobrowski handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
6540b9f230bSMatthew Bobrowski if (IS_ERR(handle)) {
6550b9f230bSMatthew Bobrowski ret = PTR_ERR(handle);
6560b9f230bSMatthew Bobrowski goto out;
6570b9f230bSMatthew Bobrowski }
6580b9f230bSMatthew Bobrowski
6590b9f230bSMatthew Bobrowski ret = ext4_orphan_add(handle, inode);
6600b9f230bSMatthew Bobrowski if (ret) {
6610b9f230bSMatthew Bobrowski ext4_journal_stop(handle);
6620b9f230bSMatthew Bobrowski goto out;
6630b9f230bSMatthew Bobrowski }
6640b9f230bSMatthew Bobrowski
6650b9f230bSMatthew Bobrowski extend = true;
6660b9f230bSMatthew Bobrowski ext4_journal_stop(handle);
6670b9f230bSMatthew Bobrowski }
6680b9f230bSMatthew Bobrowski
669776722e8SJan Kara ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
6700b9f230bSMatthew Bobrowski
6713eb32071SJan Kara if (extend) {
6723eb32071SJan Kara ret = ext4_handle_inode_extension(inode, offset, ret);
673*5efccdeeSZhihao Cheng ext4_inode_extension_cleanup(inode, ret < (ssize_t)count);
6743eb32071SJan Kara }
675776722e8SJan Kara out:
676776722e8SJan Kara inode_unlock(inode);
677776722e8SJan Kara if (ret > 0)
678776722e8SJan Kara ret = generic_write_sync(iocb, ret);
679776722e8SJan Kara return ret;
680776722e8SJan Kara }
681776722e8SJan Kara #endif
682776722e8SJan Kara
683ac27a0ecSDave Kleikamp static ssize_t
ext4_file_write_iter(struct kiocb * iocb,struct iov_iter * from)6849b884164SAl Viro ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
685ac27a0ecSDave Kleikamp {
6868ad2850fSTheodore Ts'o struct inode *inode = file_inode(iocb->ki_filp);
6878ad2850fSTheodore Ts'o
688eb8ab444SJan Kara if (unlikely(ext4_forced_shutdown(inode->i_sb)))
6890db1ff22STheodore Ts'o return -EIO;
6900db1ff22STheodore Ts'o
691776722e8SJan Kara #ifdef CONFIG_FS_DAX
692776722e8SJan Kara if (IS_DAX(inode))
693776722e8SJan Kara return ext4_dax_write_iter(iocb, from);
694776722e8SJan Kara #endif
695378f32baSMatthew Bobrowski if (iocb->ki_flags & IOCB_DIRECT)
696378f32baSMatthew Bobrowski return ext4_dio_write_iter(iocb, from);
697aa75f4d3SHarshad Shirwadkar else
698378f32baSMatthew Bobrowski return ext4_buffered_write_iter(iocb, from);
699ac27a0ecSDave Kleikamp }
700ac27a0ecSDave Kleikamp
701923ae0ffSRoss Zwisler #ifdef CONFIG_FS_DAX
ext4_dax_huge_fault(struct vm_fault * vmf,unsigned int order)7021d024e7aSMatthew Wilcox (Oracle) static vm_fault_t ext4_dax_huge_fault(struct vm_fault *vmf, unsigned int order)
703923ae0ffSRoss Zwisler {
70471fe9899SSouptick Joarder int error = 0;
70571fe9899SSouptick Joarder vm_fault_t result;
70622446423SJan Kara int retries = 0;
707fb26a1cbSJan Kara handle_t *handle = NULL;
70811bac800SDave Jiang struct inode *inode = file_inode(vmf->vma->vm_file);
709ea3d7209SJan Kara struct super_block *sb = inode->i_sb;
710fd96b8daSRandy Dodgen
711fd96b8daSRandy Dodgen /*
712fd96b8daSRandy Dodgen * We have to distinguish real writes from writes which will result in a
713fd96b8daSRandy Dodgen * COW page; COW writes should *not* poke the journal (the file will not
714fd96b8daSRandy Dodgen * be changed). Doing so would cause unintended failures when mounted
715fd96b8daSRandy Dodgen * read-only.
716fd96b8daSRandy Dodgen *
717fd96b8daSRandy Dodgen * We check for VM_SHARED rather than vmf->cow_page since the latter is
7181d024e7aSMatthew Wilcox (Oracle) * unset for order != 0 (i.e. only in do_cow_fault); for
719fd96b8daSRandy Dodgen * other sizes, dax_iomap_fault will handle splitting / fallback so that
720fd96b8daSRandy Dodgen * we eventually come back with a COW page.
721fd96b8daSRandy Dodgen */
722fd96b8daSRandy Dodgen bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
723fd96b8daSRandy Dodgen (vmf->vma->vm_flags & VM_SHARED);
724d4f5258eSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping;
725b8a6176cSJan Kara pfn_t pfn;
72601a33b4aSMatthew Wilcox
72701a33b4aSMatthew Wilcox if (write) {
72801a33b4aSMatthew Wilcox sb_start_pagefault(sb);
72911bac800SDave Jiang file_update_time(vmf->vma->vm_file);
730d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping);
73122446423SJan Kara retry:
732fb26a1cbSJan Kara handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
733fb26a1cbSJan Kara EXT4_DATA_TRANS_BLOCKS(sb));
734497f6926SJan Kara if (IS_ERR(handle)) {
735d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping);
736497f6926SJan Kara sb_end_pagefault(sb);
737497f6926SJan Kara return VM_FAULT_SIGBUS;
738497f6926SJan Kara }
739fb26a1cbSJan Kara } else {
740d4f5258eSJan Kara filemap_invalidate_lock_shared(mapping);
741fb26a1cbSJan Kara }
7421d024e7aSMatthew Wilcox (Oracle) result = dax_iomap_fault(vmf, order, &pfn, &error, &ext4_iomap_ops);
743fb26a1cbSJan Kara if (write) {
744fb26a1cbSJan Kara ext4_journal_stop(handle);
74522446423SJan Kara
74622446423SJan Kara if ((result & VM_FAULT_ERROR) && error == -ENOSPC &&
74722446423SJan Kara ext4_should_retry_alloc(sb, &retries))
74822446423SJan Kara goto retry;
749b8a6176cSJan Kara /* Handling synchronous page fault? */
750b8a6176cSJan Kara if (result & VM_FAULT_NEEDDSYNC)
7511d024e7aSMatthew Wilcox (Oracle) result = dax_finish_sync_fault(vmf, order, pfn);
752d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping);
75301a33b4aSMatthew Wilcox sb_end_pagefault(sb);
754fb26a1cbSJan Kara } else {
755d4f5258eSJan Kara filemap_invalidate_unlock_shared(mapping);
756fb26a1cbSJan Kara }
75701a33b4aSMatthew Wilcox
75801a33b4aSMatthew Wilcox return result;
759923ae0ffSRoss Zwisler }
760923ae0ffSRoss Zwisler
ext4_dax_fault(struct vm_fault * vmf)76171fe9899SSouptick Joarder static vm_fault_t ext4_dax_fault(struct vm_fault *vmf)
762c791ace1SDave Jiang {
7631d024e7aSMatthew Wilcox (Oracle) return ext4_dax_huge_fault(vmf, 0);
764c791ace1SDave Jiang }
765c791ace1SDave Jiang
766923ae0ffSRoss Zwisler static const struct vm_operations_struct ext4_dax_vm_ops = {
767923ae0ffSRoss Zwisler .fault = ext4_dax_fault,
768c791ace1SDave Jiang .huge_fault = ext4_dax_huge_fault,
7691e9d180bSRoss Zwisler .page_mkwrite = ext4_dax_fault,
77091d25ba8SRoss Zwisler .pfn_mkwrite = ext4_dax_fault,
771923ae0ffSRoss Zwisler };
772923ae0ffSRoss Zwisler #else
773923ae0ffSRoss Zwisler #define ext4_dax_vm_ops ext4_file_vm_ops
774923ae0ffSRoss Zwisler #endif
775923ae0ffSRoss Zwisler
776f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct ext4_file_vm_ops = {
777d4f5258eSJan Kara .fault = filemap_fault,
778f1820361SKirill A. Shutemov .map_pages = filemap_map_pages,
7792e9ee850SAneesh Kumar K.V .page_mkwrite = ext4_page_mkwrite,
7802e9ee850SAneesh Kumar K.V };
7812e9ee850SAneesh Kumar K.V
ext4_file_mmap(struct file * file,struct vm_area_struct * vma)7822e9ee850SAneesh Kumar K.V static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
7832e9ee850SAneesh Kumar K.V {
784c9c7429cSMichael Halcrow struct inode *inode = file->f_mapping->host;
785eb8ab444SJan Kara struct dax_device *dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
786c9c7429cSMichael Halcrow
787eb8ab444SJan Kara if (unlikely(ext4_forced_shutdown(inode->i_sb)))
7880db1ff22STheodore Ts'o return -EIO;
7890db1ff22STheodore Ts'o
790b8a6176cSJan Kara /*
791e46bfc3fSPankaj Gupta * We don't support synchronous mappings for non-DAX files and
792e46bfc3fSPankaj Gupta * for DAX files if underneath dax_device is not synchronous.
793b8a6176cSJan Kara */
794e46bfc3fSPankaj Gupta if (!daxdev_mapping_supported(vma, dax_dev))
795b8a6176cSJan Kara return -EOPNOTSUPP;
796b8a6176cSJan Kara
7972e9ee850SAneesh Kumar K.V file_accessed(file);
798923ae0ffSRoss Zwisler if (IS_DAX(file_inode(file))) {
799923ae0ffSRoss Zwisler vma->vm_ops = &ext4_dax_vm_ops;
8001c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_HUGEPAGE);
801923ae0ffSRoss Zwisler } else {
8022e9ee850SAneesh Kumar K.V vma->vm_ops = &ext4_file_vm_ops;
803923ae0ffSRoss Zwisler }
8042e9ee850SAneesh Kumar K.V return 0;
8052e9ee850SAneesh Kumar K.V }
8062e9ee850SAneesh Kumar K.V
ext4_sample_last_mounted(struct super_block * sb,struct vfsmount * mnt)807833a9508SAmir Goldstein static int ext4_sample_last_mounted(struct super_block *sb,
808833a9508SAmir Goldstein struct vfsmount *mnt)
809bc0b0d6dSTheodore Ts'o {
810833a9508SAmir Goldstein struct ext4_sb_info *sbi = EXT4_SB(sb);
811bc0b0d6dSTheodore Ts'o struct path path;
812bc0b0d6dSTheodore Ts'o char buf[64], *cp;
813833a9508SAmir Goldstein handle_t *handle;
814833a9508SAmir Goldstein int err;
815bc0b0d6dSTheodore Ts'o
8169b5f6c9bSHarshad Shirwadkar if (likely(ext4_test_mount_flag(sb, EXT4_MF_MNTDIR_SAMPLED)))
817833a9508SAmir Goldstein return 0;
8180db1ff22STheodore Ts'o
819db6516a5SAmir Goldstein if (sb_rdonly(sb) || !sb_start_intwrite_trylock(sb))
820833a9508SAmir Goldstein return 0;
821833a9508SAmir Goldstein
8229b5f6c9bSHarshad Shirwadkar ext4_set_mount_flag(sb, EXT4_MF_MNTDIR_SAMPLED);
823bc0b0d6dSTheodore Ts'o /*
824bc0b0d6dSTheodore Ts'o * Sample where the filesystem has been mounted and
825bc0b0d6dSTheodore Ts'o * store it in the superblock for sysadmin convenience
826bc0b0d6dSTheodore Ts'o * when trying to sort through large numbers of block
827bc0b0d6dSTheodore Ts'o * devices or filesystem images.
828bc0b0d6dSTheodore Ts'o */
829bc0b0d6dSTheodore Ts'o memset(buf, 0, sizeof(buf));
8303899167dSAl Viro path.mnt = mnt;
8313899167dSAl Viro path.dentry = mnt->mnt_root;
832bc0b0d6dSTheodore Ts'o cp = d_path(&path, buf, sizeof(buf));
833db6516a5SAmir Goldstein err = 0;
834833a9508SAmir Goldstein if (IS_ERR(cp))
835db6516a5SAmir Goldstein goto out;
836044ce47fSJan Kara
8379924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
838db6516a5SAmir Goldstein err = PTR_ERR(handle);
839044ce47fSJan Kara if (IS_ERR(handle))
840db6516a5SAmir Goldstein goto out;
8415d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access");
842188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
843188c299eSJan Kara EXT4_JTR_NONE);
844833a9508SAmir Goldstein if (err)
845db6516a5SAmir Goldstein goto out_journal;
84605c2c00fSJan Kara lock_buffer(sbi->s_sbh);
8475a3b590dSTheodore Ts'o strncpy(sbi->s_es->s_last_mounted, cp,
848bc0b0d6dSTheodore Ts'o sizeof(sbi->s_es->s_last_mounted));
84905c2c00fSJan Kara ext4_superblock_csum_set(sb);
85005c2c00fSJan Kara unlock_buffer(sbi->s_sbh);
851a3f5cf14SJan Kara ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
852db6516a5SAmir Goldstein out_journal:
853044ce47fSJan Kara ext4_journal_stop(handle);
854db6516a5SAmir Goldstein out:
855db6516a5SAmir Goldstein sb_end_intwrite(sb);
856833a9508SAmir Goldstein return err;
857bc0b0d6dSTheodore Ts'o }
858833a9508SAmir Goldstein
ext4_file_open(struct inode * inode,struct file * filp)859833a9508SAmir Goldstein static int ext4_file_open(struct inode *inode, struct file *filp)
860833a9508SAmir Goldstein {
861833a9508SAmir Goldstein int ret;
862833a9508SAmir Goldstein
863eb8ab444SJan Kara if (unlikely(ext4_forced_shutdown(inode->i_sb)))
864833a9508SAmir Goldstein return -EIO;
865833a9508SAmir Goldstein
866833a9508SAmir Goldstein ret = ext4_sample_last_mounted(inode->i_sb, filp->f_path.mnt);
867833a9508SAmir Goldstein if (ret)
868833a9508SAmir Goldstein return ret;
8699dd78d8cSMiklos Szeredi
87009a5c31cSEric Biggers ret = fscrypt_file_open(inode, filp);
87109a5c31cSEric Biggers if (ret)
87209a5c31cSEric Biggers return ret;
87309a5c31cSEric Biggers
874c93d8f88SEric Biggers ret = fsverity_file_open(inode, filp);
875c93d8f88SEric Biggers if (ret)
876c93d8f88SEric Biggers return ret;
877c93d8f88SEric Biggers
8788aefcd55STheodore Ts'o /*
8798aefcd55STheodore Ts'o * Set up the jbd2_inode if we are opening the inode for
8808aefcd55STheodore Ts'o * writing and the journal is present
8818aefcd55STheodore Ts'o */
882a361293fSJan Kara if (filp->f_mode & FMODE_WRITE) {
883c9c7429cSMichael Halcrow ret = ext4_inode_attach_jinode(inode);
884a361293fSJan Kara if (ret < 0)
885a361293fSJan Kara return ret;
8868aefcd55STheodore Ts'o }
887728fbc0eSGoldwyn Rodrigues
888d8aeb44aSJens Axboe filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC |
889d8aeb44aSJens Axboe FMODE_DIO_PARALLEL_WRITE;
890abdd438bSTheodore Ts'o return dquot_file_open(inode, filp);
891bc0b0d6dSTheodore Ts'o }
892bc0b0d6dSTheodore Ts'o
893e0d10bfaSToshiyuki Okajima /*
894ec7268ceSEric Sandeen * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
895ec7268ceSEric Sandeen * by calling generic_file_llseek_size() with the appropriate maxbytes
896ec7268ceSEric Sandeen * value for each.
897e0d10bfaSToshiyuki Okajima */
ext4_llseek(struct file * file,loff_t offset,int whence)898965c8e59SAndrew Morton loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
899e0d10bfaSToshiyuki Okajima {
900e0d10bfaSToshiyuki Okajima struct inode *inode = file->f_mapping->host;
901e0d10bfaSToshiyuki Okajima loff_t maxbytes;
902e0d10bfaSToshiyuki Okajima
903e0d10bfaSToshiyuki Okajima if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
904e0d10bfaSToshiyuki Okajima maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
905e0d10bfaSToshiyuki Okajima else
906e0d10bfaSToshiyuki Okajima maxbytes = inode->i_sb->s_maxbytes;
907e0d10bfaSToshiyuki Okajima
908965c8e59SAndrew Morton switch (whence) {
909545052e9SChristoph Hellwig default:
910965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence,
911e8b96eb5SEric Sandeen maxbytes, i_size_read(inode));
912c8c0df24SZheng Liu case SEEK_HOLE:
913545052e9SChristoph Hellwig inode_lock_shared(inode);
91409edf4d3SMatthew Bobrowski offset = iomap_seek_hole(inode, offset,
91509edf4d3SMatthew Bobrowski &ext4_iomap_report_ops);
916545052e9SChristoph Hellwig inode_unlock_shared(inode);
917545052e9SChristoph Hellwig break;
918545052e9SChristoph Hellwig case SEEK_DATA:
919545052e9SChristoph Hellwig inode_lock_shared(inode);
92009edf4d3SMatthew Bobrowski offset = iomap_seek_data(inode, offset,
92109edf4d3SMatthew Bobrowski &ext4_iomap_report_ops);
922545052e9SChristoph Hellwig inode_unlock_shared(inode);
923545052e9SChristoph Hellwig break;
924c8c0df24SZheng Liu }
925c8c0df24SZheng Liu
926545052e9SChristoph Hellwig if (offset < 0)
927545052e9SChristoph Hellwig return offset;
928545052e9SChristoph Hellwig return vfs_setpos(file, offset, maxbytes);
929e0d10bfaSToshiyuki Okajima }
930e0d10bfaSToshiyuki Okajima
931617ba13bSMingming Cao const struct file_operations ext4_file_operations = {
932e0d10bfaSToshiyuki Okajima .llseek = ext4_llseek,
933364443cbSJan Kara .read_iter = ext4_file_read_iter,
9349b884164SAl Viro .write_iter = ext4_file_write_iter,
9353e08773cSChristoph Hellwig .iopoll = iocb_bio_iopoll,
9365cdd7b2dSAndi Kleen .unlocked_ioctl = ext4_ioctl,
937ac27a0ecSDave Kleikamp #ifdef CONFIG_COMPAT
938617ba13bSMingming Cao .compat_ioctl = ext4_compat_ioctl,
939ac27a0ecSDave Kleikamp #endif
9402e9ee850SAneesh Kumar K.V .mmap = ext4_file_mmap,
941b8a6176cSJan Kara .mmap_supported_flags = MAP_SYNC,
942bc0b0d6dSTheodore Ts'o .open = ext4_file_open,
943617ba13bSMingming Cao .release = ext4_release_file,
944617ba13bSMingming Cao .fsync = ext4_sync_file,
945dbe6ec81SToshi Kani .get_unmapped_area = thp_get_unmapped_area,
946fa6c46e7SDavid Howells .splice_read = ext4_file_splice_read,
9478d020765SAl Viro .splice_write = iter_file_splice_write,
9482fe17c10SChristoph Hellwig .fallocate = ext4_fallocate,
949ac27a0ecSDave Kleikamp };
950ac27a0ecSDave Kleikamp
951754661f1SArjan van de Ven const struct inode_operations ext4_file_inode_operations = {
952617ba13bSMingming Cao .setattr = ext4_setattr,
95399652ea5SDavid Howells .getattr = ext4_file_getattr,
954617ba13bSMingming Cao .listxattr = ext4_listxattr,
955cac2f8b8SChristian Brauner .get_inode_acl = ext4_get_acl,
95664e178a7SChristoph Hellwig .set_acl = ext4_set_acl,
9576873fa0dSEric Sandeen .fiemap = ext4_fiemap,
9584db5c2e6SMiklos Szeredi .fileattr_get = ext4_fileattr_get,
9594db5c2e6SMiklos Szeredi .fileattr_set = ext4_fileattr_set,
960ac27a0ecSDave Kleikamp };
961ac27a0ecSDave Kleikamp
962