1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994, 1995
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/file.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * ext4 fs regular file handling primitives
18 * 64-bit file support on 64-bit platforms by Jakub Jelinek
22 #include <linux/time.h>
24 #include <linux/iomap.h>
25 #include <linux/mount.h>
26 #include <linux/path.h>
27 #include <linux/dax.h>
28 #include <linux/quotaops.h>
29 #include <linux/pagevec.h>
30 #include <linux/uio.h>
31 #include <linux/mman.h>
32 #include <linux/backing-dev.h>
34 #include "ext4_jbd2.h"
39 static bool ext4_dio_supported(struct inode *inode)
41 if (IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENCRYPTED(inode))
43 if (fsverity_active(inode))
45 if (ext4_should_journal_data(inode))
47 if (ext4_has_inline_data(inode))
52 static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
55 struct inode *inode = file_inode(iocb->ki_filp);
57 if (iocb->ki_flags & IOCB_NOWAIT) {
58 if (!inode_trylock_shared(inode))
61 inode_lock_shared(inode);
64 if (!ext4_dio_supported(inode)) {
65 inode_unlock_shared(inode);
67 * Fallback to buffered I/O if the operation being performed on
68 * the inode is not supported by direct I/O. The IOCB_DIRECT
69 * flag needs to be cleared here in order to ensure that the
70 * direct I/O path within generic_file_read_iter() is not
73 iocb->ki_flags &= ~IOCB_DIRECT;
74 return generic_file_read_iter(iocb, to);
77 ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL,
79 inode_unlock_shared(inode);
81 file_accessed(iocb->ki_filp);
86 static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
88 struct inode *inode = file_inode(iocb->ki_filp);
91 if (!inode_trylock_shared(inode)) {
92 if (iocb->ki_flags & IOCB_NOWAIT)
94 inode_lock_shared(inode);
97 * Recheck under inode lock - at this point we are sure it cannot
100 if (!IS_DAX(inode)) {
101 inode_unlock_shared(inode);
102 /* Fallback to buffered IO in case we cannot support DAX */
103 return generic_file_read_iter(iocb, to);
105 ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
106 inode_unlock_shared(inode);
108 file_accessed(iocb->ki_filp);
113 static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
115 struct inode *inode = file_inode(iocb->ki_filp);
117 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
120 if (!iov_iter_count(to))
121 return 0; /* skip atime */
125 return ext4_dax_read_iter(iocb, to);
127 if (iocb->ki_flags & IOCB_DIRECT)
128 return ext4_dio_read_iter(iocb, to);
130 return generic_file_read_iter(iocb, to);
134 * Called when an inode is released. Note that this is different
135 * from ext4_file_open: open gets called at every open, but release
136 * gets called only when /all/ the files are closed.
138 static int ext4_release_file(struct inode *inode, struct file *filp)
140 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
141 ext4_alloc_da_blocks(inode);
142 ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
144 /* if we are the last writer on the inode, drop the block reservation */
145 if ((filp->f_mode & FMODE_WRITE) &&
146 (atomic_read(&inode->i_writecount) == 1) &&
147 !EXT4_I(inode)->i_reserved_data_blocks)
149 down_write(&EXT4_I(inode)->i_data_sem);
150 ext4_discard_preallocations(inode);
151 up_write(&EXT4_I(inode)->i_data_sem);
153 if (is_dx(inode) && filp->private_data)
154 ext4_htree_free_dir_info(filp->private_data);
160 * This tests whether the IO in question is block-aligned or not.
161 * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
162 * are converted to written only after the IO is complete. Until they are
163 * mapped, these blocks appear as holes, so dio_zero_block() will assume that
164 * it needs to zero out portions of the start and/or end block. If 2 AIO
165 * threads are at work on the same unwritten block, they must be synchronized
166 * or one thread will zero the other's data, causing corruption.
169 ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
171 struct super_block *sb = inode->i_sb;
172 int blockmask = sb->s_blocksize - 1;
174 if (pos >= ALIGN(i_size_read(inode), sb->s_blocksize))
177 if ((pos | iov_iter_alignment(from)) & blockmask)
183 /* Is IO overwriting allocated and initialized blocks? */
184 static bool ext4_overwrite_io(struct inode *inode, loff_t pos, loff_t len)
186 struct ext4_map_blocks map;
187 unsigned int blkbits = inode->i_blkbits;
190 if (pos + len > i_size_read(inode))
193 map.m_lblk = pos >> blkbits;
194 map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
197 err = ext4_map_blocks(NULL, inode, &map, 0);
199 * 'err==len' means that all of the blocks have been preallocated,
200 * regardless of whether they have been initialized or not. To exclude
201 * unwritten extents, we need to check m_flags.
203 return err == blklen && (map.m_flags & EXT4_MAP_MAPPED);
206 static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
208 struct inode *inode = file_inode(iocb->ki_filp);
211 if (unlikely(IS_IMMUTABLE(inode)))
214 ret = generic_write_checks(iocb, from);
219 * If we have encountered a bitmap-format file, the size limit
220 * is smaller than s_maxbytes, which is for extent-mapped files.
222 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
223 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
225 if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
227 iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
230 ret = file_modified(iocb->ki_filp);
234 return iov_iter_count(from);
237 static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
238 struct iov_iter *from)
241 struct inode *inode = file_inode(iocb->ki_filp);
243 if (iocb->ki_flags & IOCB_NOWAIT)
247 ret = ext4_write_checks(iocb, from);
251 current->backing_dev_info = inode_to_bdi(inode);
252 ret = generic_perform_write(iocb->ki_filp, from, iocb->ki_pos);
253 current->backing_dev_info = NULL;
257 if (likely(ret > 0)) {
259 ret = generic_write_sync(iocb, ret);
265 static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,
266 ssize_t written, size_t count)
269 bool truncate = false;
270 u8 blkbits = inode->i_blkbits;
271 ext4_lblk_t written_blk, end_blk;
274 * Note that EXT4_I(inode)->i_disksize can get extended up to
275 * inode->i_size while the I/O was running due to writeback of delalloc
276 * blocks. But, the code in ext4_iomap_alloc() is careful to use
277 * zeroed/unwritten extents if this is possible; thus we won't leave
278 * uninitialized blocks in a file even if we didn't succeed in writing
279 * as much as we intended.
281 WARN_ON_ONCE(i_size_read(inode) < EXT4_I(inode)->i_disksize);
282 if (offset + count <= EXT4_I(inode)->i_disksize) {
284 * We need to ensure that the inode is removed from the orphan
285 * list if it has been added prematurely, due to writeback of
288 if (!list_empty(&EXT4_I(inode)->i_orphan) && inode->i_nlink) {
289 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
291 if (IS_ERR(handle)) {
292 ext4_orphan_del(NULL, inode);
293 return PTR_ERR(handle);
296 ext4_orphan_del(handle, inode);
297 ext4_journal_stop(handle);
306 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
307 if (IS_ERR(handle)) {
308 written = PTR_ERR(handle);
312 if (ext4_update_inode_size(inode, offset + written))
313 ext4_mark_inode_dirty(handle, inode);
316 * We may need to truncate allocated but not written blocks beyond EOF.
318 written_blk = ALIGN(offset + written, 1 << blkbits);
319 end_blk = ALIGN(offset + count, 1 << blkbits);
320 if (written_blk < end_blk && ext4_can_truncate(inode))
324 * Remove the inode from the orphan list if it has been extended and
325 * everything went OK.
327 if (!truncate && inode->i_nlink)
328 ext4_orphan_del(handle, inode);
329 ext4_journal_stop(handle);
333 ext4_truncate_failed_write(inode);
335 * If the truncate operation failed early, then the inode may
336 * still be on the orphan list. In that case, we need to try
337 * remove the inode from the in-memory linked list.
340 ext4_orphan_del(NULL, inode);
346 static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
347 int error, unsigned int flags)
349 loff_t offset = iocb->ki_pos;
350 struct inode *inode = file_inode(iocb->ki_filp);
355 if (size && flags & IOMAP_DIO_UNWRITTEN)
356 return ext4_convert_unwritten_extents(NULL, inode,
362 static const struct iomap_dio_ops ext4_dio_write_ops = {
363 .end_io = ext4_dio_write_end_io,
366 static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
372 struct inode *inode = file_inode(iocb->ki_filp);
373 bool extend = false, overwrite = false, unaligned_aio = false;
375 if (iocb->ki_flags & IOCB_NOWAIT) {
376 if (!inode_trylock(inode))
382 if (!ext4_dio_supported(inode)) {
385 * Fallback to buffered I/O if the inode does not support
388 return ext4_buffered_write_iter(iocb, from);
391 ret = ext4_write_checks(iocb, from);
398 * Unaligned asynchronous direct I/O must be serialized among each
399 * other as the zeroing of partial blocks of two competing unaligned
400 * asynchronous direct I/O writes can result in data corruption.
402 offset = iocb->ki_pos;
403 count = iov_iter_count(from);
404 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
405 !is_sync_kiocb(iocb) && ext4_unaligned_aio(inode, from, offset)) {
406 unaligned_aio = true;
407 inode_dio_wait(inode);
411 * Determine whether the I/O will overwrite allocated and initialized
412 * blocks. If so, check to see whether it is possible to take the
413 * dioread_nolock path.
415 if (!unaligned_aio && ext4_overwrite_io(inode, offset, count) &&
416 ext4_should_dioread_nolock(inode)) {
418 downgrade_write(&inode->i_rwsem);
421 if (offset + count > EXT4_I(inode)->i_disksize) {
422 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
423 if (IS_ERR(handle)) {
424 ret = PTR_ERR(handle);
428 ret = ext4_orphan_add(handle, inode);
430 ext4_journal_stop(handle);
435 ext4_journal_stop(handle);
438 ret = iomap_dio_rw(iocb, from, &ext4_iomap_ops, &ext4_dio_write_ops,
439 is_sync_kiocb(iocb) || unaligned_aio || extend);
442 ret = ext4_handle_inode_extension(inode, offset, ret, count);
446 inode_unlock_shared(inode);
450 if (ret >= 0 && iov_iter_count(from)) {
454 offset = iocb->ki_pos;
455 err = ext4_buffered_write_iter(iocb, from);
460 * We need to ensure that the pages within the page cache for
461 * the range covered by this I/O are written to disk and
462 * invalidated. This is in attempt to preserve the expected
463 * direct I/O semantics in the case we fallback to buffered I/O
464 * to complete off the I/O request.
467 endbyte = offset + err - 1;
468 err = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
471 invalidate_mapping_pages(iocb->ki_filp->f_mapping,
472 offset >> PAGE_SHIFT,
473 endbyte >> PAGE_SHIFT);
481 ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
488 struct inode *inode = file_inode(iocb->ki_filp);
490 if (!inode_trylock(inode)) {
491 if (iocb->ki_flags & IOCB_NOWAIT)
496 ret = ext4_write_checks(iocb, from);
500 offset = iocb->ki_pos;
501 count = iov_iter_count(from);
503 if (offset + count > EXT4_I(inode)->i_disksize) {
504 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
505 if (IS_ERR(handle)) {
506 ret = PTR_ERR(handle);
510 ret = ext4_orphan_add(handle, inode);
512 ext4_journal_stop(handle);
517 ext4_journal_stop(handle);
520 ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
523 ret = ext4_handle_inode_extension(inode, offset, ret, count);
527 ret = generic_write_sync(iocb, ret);
533 ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
535 struct inode *inode = file_inode(iocb->ki_filp);
537 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
542 return ext4_dax_write_iter(iocb, from);
544 if (iocb->ki_flags & IOCB_DIRECT)
545 return ext4_dio_write_iter(iocb, from);
547 return ext4_buffered_write_iter(iocb, from);
551 static vm_fault_t ext4_dax_huge_fault(struct vm_fault *vmf,
552 enum page_entry_size pe_size)
557 handle_t *handle = NULL;
558 struct inode *inode = file_inode(vmf->vma->vm_file);
559 struct super_block *sb = inode->i_sb;
562 * We have to distinguish real writes from writes which will result in a
563 * COW page; COW writes should *not* poke the journal (the file will not
564 * be changed). Doing so would cause unintended failures when mounted
567 * We check for VM_SHARED rather than vmf->cow_page since the latter is
568 * unset for pe_size != PE_SIZE_PTE (i.e. only in do_cow_fault); for
569 * other sizes, dax_iomap_fault will handle splitting / fallback so that
570 * we eventually come back with a COW page.
572 bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
573 (vmf->vma->vm_flags & VM_SHARED);
577 sb_start_pagefault(sb);
578 file_update_time(vmf->vma->vm_file);
579 down_read(&EXT4_I(inode)->i_mmap_sem);
581 handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
582 EXT4_DATA_TRANS_BLOCKS(sb));
583 if (IS_ERR(handle)) {
584 up_read(&EXT4_I(inode)->i_mmap_sem);
585 sb_end_pagefault(sb);
586 return VM_FAULT_SIGBUS;
589 down_read(&EXT4_I(inode)->i_mmap_sem);
591 result = dax_iomap_fault(vmf, pe_size, &pfn, &error, &ext4_iomap_ops);
593 ext4_journal_stop(handle);
595 if ((result & VM_FAULT_ERROR) && error == -ENOSPC &&
596 ext4_should_retry_alloc(sb, &retries))
598 /* Handling synchronous page fault? */
599 if (result & VM_FAULT_NEEDDSYNC)
600 result = dax_finish_sync_fault(vmf, pe_size, pfn);
601 up_read(&EXT4_I(inode)->i_mmap_sem);
602 sb_end_pagefault(sb);
604 up_read(&EXT4_I(inode)->i_mmap_sem);
610 static vm_fault_t ext4_dax_fault(struct vm_fault *vmf)
612 return ext4_dax_huge_fault(vmf, PE_SIZE_PTE);
615 static const struct vm_operations_struct ext4_dax_vm_ops = {
616 .fault = ext4_dax_fault,
617 .huge_fault = ext4_dax_huge_fault,
618 .page_mkwrite = ext4_dax_fault,
619 .pfn_mkwrite = ext4_dax_fault,
622 #define ext4_dax_vm_ops ext4_file_vm_ops
625 static const struct vm_operations_struct ext4_file_vm_ops = {
626 .fault = ext4_filemap_fault,
627 .map_pages = filemap_map_pages,
628 .page_mkwrite = ext4_page_mkwrite,
631 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
633 struct inode *inode = file->f_mapping->host;
634 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
635 struct dax_device *dax_dev = sbi->s_daxdev;
637 if (unlikely(ext4_forced_shutdown(sbi)))
641 * We don't support synchronous mappings for non-DAX files and
642 * for DAX files if underneath dax_device is not synchronous.
644 if (!daxdev_mapping_supported(vma, dax_dev))
648 if (IS_DAX(file_inode(file))) {
649 vma->vm_ops = &ext4_dax_vm_ops;
650 vma->vm_flags |= VM_HUGEPAGE;
652 vma->vm_ops = &ext4_file_vm_ops;
657 static int ext4_sample_last_mounted(struct super_block *sb,
658 struct vfsmount *mnt)
660 struct ext4_sb_info *sbi = EXT4_SB(sb);
666 if (likely(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED))
669 if (sb_rdonly(sb) || !sb_start_intwrite_trylock(sb))
672 sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
674 * Sample where the filesystem has been mounted and
675 * store it in the superblock for sysadmin convenience
676 * when trying to sort through large numbers of block
677 * devices or filesystem images.
679 memset(buf, 0, sizeof(buf));
681 path.dentry = mnt->mnt_root;
682 cp = d_path(&path, buf, sizeof(buf));
687 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
688 err = PTR_ERR(handle);
691 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
692 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
695 strlcpy(sbi->s_es->s_last_mounted, cp,
696 sizeof(sbi->s_es->s_last_mounted));
697 ext4_handle_dirty_super(handle, sb);
699 ext4_journal_stop(handle);
705 static int ext4_file_open(struct inode * inode, struct file * filp)
709 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
712 ret = ext4_sample_last_mounted(inode->i_sb, filp->f_path.mnt);
716 ret = fscrypt_file_open(inode, filp);
720 ret = fsverity_file_open(inode, filp);
725 * Set up the jbd2_inode if we are opening the inode for
726 * writing and the journal is present
728 if (filp->f_mode & FMODE_WRITE) {
729 ret = ext4_inode_attach_jinode(inode);
734 filp->f_mode |= FMODE_NOWAIT;
735 return dquot_file_open(inode, filp);
739 * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
740 * by calling generic_file_llseek_size() with the appropriate maxbytes
743 loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
745 struct inode *inode = file->f_mapping->host;
748 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
749 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
751 maxbytes = inode->i_sb->s_maxbytes;
755 return generic_file_llseek_size(file, offset, whence,
756 maxbytes, i_size_read(inode));
758 inode_lock_shared(inode);
759 offset = iomap_seek_hole(inode, offset,
760 &ext4_iomap_report_ops);
761 inode_unlock_shared(inode);
764 inode_lock_shared(inode);
765 offset = iomap_seek_data(inode, offset,
766 &ext4_iomap_report_ops);
767 inode_unlock_shared(inode);
773 return vfs_setpos(file, offset, maxbytes);
776 const struct file_operations ext4_file_operations = {
777 .llseek = ext4_llseek,
778 .read_iter = ext4_file_read_iter,
779 .write_iter = ext4_file_write_iter,
780 .unlocked_ioctl = ext4_ioctl,
782 .compat_ioctl = ext4_compat_ioctl,
784 .mmap = ext4_file_mmap,
785 .mmap_supported_flags = MAP_SYNC,
786 .open = ext4_file_open,
787 .release = ext4_release_file,
788 .fsync = ext4_sync_file,
789 .get_unmapped_area = thp_get_unmapped_area,
790 .splice_read = generic_file_splice_read,
791 .splice_write = iter_file_splice_write,
792 .fallocate = ext4_fallocate,
795 const struct inode_operations ext4_file_inode_operations = {
796 .setattr = ext4_setattr,
797 .getattr = ext4_file_getattr,
798 .listxattr = ext4_listxattr,
799 .get_acl = ext4_get_acl,
800 .set_acl = ext4_set_acl,
801 .fiemap = ext4_fiemap,