1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
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
19 * (jj@sunsite.ms.mff.cuni.cz)
22 #include <linux/time.h>
24 #include <linux/mount.h>
25 #include <linux/path.h>
26 #include <linux/dax.h>
27 #include <linux/quotaops.h>
28 #include <linux/pagevec.h>
29 #include <linux/uio.h>
31 #include "ext4_jbd2.h"
36 static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
38 struct inode *inode = file_inode(iocb->ki_filp);
41 if (iocb->ki_flags & IOCB_NOWAIT) {
42 if (!inode_trylock_shared(inode))
45 inode_lock_shared(inode);
48 * Recheck under inode lock - at this point we are sure it cannot
52 inode_unlock_shared(inode);
53 /* Fallback to buffered IO in case we cannot support DAX */
54 return generic_file_read_iter(iocb, to);
56 ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
57 inode_unlock_shared(inode);
59 file_accessed(iocb->ki_filp);
64 static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
66 if (unlikely(ext4_forced_shutdown(EXT4_SB(file_inode(iocb->ki_filp)->i_sb))))
69 if (!iov_iter_count(to))
70 return 0; /* skip atime */
73 if (IS_DAX(file_inode(iocb->ki_filp)))
74 return ext4_dax_read_iter(iocb, to);
76 return generic_file_read_iter(iocb, to);
80 * Called when an inode is released. Note that this is different
81 * from ext4_file_open: open gets called at every open, but release
82 * gets called only when /all/ the files are closed.
84 static int ext4_release_file(struct inode *inode, struct file *filp)
86 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
87 ext4_alloc_da_blocks(inode);
88 ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
90 /* if we are the last writer on the inode, drop the block reservation */
91 if ((filp->f_mode & FMODE_WRITE) &&
92 (atomic_read(&inode->i_writecount) == 1) &&
93 !EXT4_I(inode)->i_reserved_data_blocks)
95 down_write(&EXT4_I(inode)->i_data_sem);
96 ext4_discard_preallocations(inode);
97 up_write(&EXT4_I(inode)->i_data_sem);
99 if (is_dx(inode) && filp->private_data)
100 ext4_htree_free_dir_info(filp->private_data);
105 static void ext4_unwritten_wait(struct inode *inode)
107 wait_queue_head_t *wq = ext4_ioend_wq(inode);
109 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_unwritten) == 0));
113 * This tests whether the IO in question is block-aligned or not.
114 * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
115 * are converted to written only after the IO is complete. Until they are
116 * mapped, these blocks appear as holes, so dio_zero_block() will assume that
117 * it needs to zero out portions of the start and/or end block. If 2 AIO
118 * threads are at work on the same unwritten block, they must be synchronized
119 * or one thread will zero the other's data, causing corruption.
122 ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
124 struct super_block *sb = inode->i_sb;
125 int blockmask = sb->s_blocksize - 1;
127 if (pos >= ALIGN(i_size_read(inode), sb->s_blocksize))
130 if ((pos | iov_iter_alignment(from)) & blockmask)
136 /* Is IO overwriting allocated and initialized blocks? */
137 static bool ext4_overwrite_io(struct inode *inode, loff_t pos, loff_t len)
139 struct ext4_map_blocks map;
140 unsigned int blkbits = inode->i_blkbits;
143 if (pos + len > i_size_read(inode))
146 map.m_lblk = pos >> blkbits;
147 map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
150 err = ext4_map_blocks(NULL, inode, &map, 0);
152 * 'err==len' means that all of the blocks have been preallocated,
153 * regardless of whether they have been initialized or not. To exclude
154 * unwritten extents, we need to check m_flags.
156 return err == blklen && (map.m_flags & EXT4_MAP_MAPPED);
159 static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
161 struct inode *inode = file_inode(iocb->ki_filp);
164 ret = generic_write_checks(iocb, from);
168 if (unlikely(IS_IMMUTABLE(inode)))
172 * If we have encountered a bitmap-format file, the size limit
173 * is smaller than s_maxbytes, which is for extent-mapped files.
175 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
176 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
178 if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
180 iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
182 return iov_iter_count(from);
187 ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
189 struct inode *inode = file_inode(iocb->ki_filp);
192 if (iocb->ki_flags & IOCB_NOWAIT) {
193 if (!inode_trylock(inode))
198 ret = ext4_write_checks(iocb, from);
201 ret = file_remove_privs(iocb->ki_filp);
204 ret = file_update_time(iocb->ki_filp);
208 ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
212 ret = generic_write_sync(iocb, ret);
218 ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
220 struct inode *inode = file_inode(iocb->ki_filp);
221 int o_direct = iocb->ki_flags & IOCB_DIRECT;
222 int unaligned_aio = 0;
226 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
231 return ext4_dax_write_iter(iocb, from);
233 if (!o_direct && (iocb->ki_flags & IOCB_NOWAIT))
236 if (!inode_trylock(inode)) {
237 if (iocb->ki_flags & IOCB_NOWAIT)
242 ret = ext4_write_checks(iocb, from);
247 * Unaligned direct AIO must be serialized among each other as zeroing
248 * of partial blocks of two competing unaligned AIOs can result in data
251 if (o_direct && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
252 !is_sync_kiocb(iocb) &&
253 ext4_unaligned_aio(inode, from, iocb->ki_pos)) {
255 ext4_unwritten_wait(inode);
258 iocb->private = &overwrite;
259 /* Check whether we do a DIO overwrite or not */
260 if (o_direct && !unaligned_aio) {
261 if (ext4_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from))) {
262 if (ext4_should_dioread_nolock(inode))
264 } else if (iocb->ki_flags & IOCB_NOWAIT) {
270 ret = __generic_file_write_iter(iocb, from);
272 * Unaligned direct AIO must be the only IO in flight. Otherwise
273 * overlapping aligned IO after unaligned might result in data
276 if (ret == -EIOCBQUEUED && unaligned_aio)
277 ext4_unwritten_wait(inode);
281 ret = generic_write_sync(iocb, ret);
291 static int ext4_dax_huge_fault(struct vm_fault *vmf,
292 enum page_entry_size pe_size)
295 handle_t *handle = NULL;
296 struct inode *inode = file_inode(vmf->vma->vm_file);
297 struct super_block *sb = inode->i_sb;
300 * We have to distinguish real writes from writes which will result in a
301 * COW page; COW writes should *not* poke the journal (the file will not
302 * be changed). Doing so would cause unintended failures when mounted
305 * We check for VM_SHARED rather than vmf->cow_page since the latter is
306 * unset for pe_size != PE_SIZE_PTE (i.e. only in do_cow_fault); for
307 * other sizes, dax_iomap_fault will handle splitting / fallback so that
308 * we eventually come back with a COW page.
310 bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
311 (vmf->vma->vm_flags & VM_SHARED);
314 sb_start_pagefault(sb);
315 file_update_time(vmf->vma->vm_file);
316 down_read(&EXT4_I(inode)->i_mmap_sem);
317 handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
318 EXT4_DATA_TRANS_BLOCKS(sb));
320 down_read(&EXT4_I(inode)->i_mmap_sem);
323 result = dax_iomap_fault(vmf, pe_size, &ext4_iomap_ops);
325 result = VM_FAULT_SIGBUS;
328 ext4_journal_stop(handle);
329 up_read(&EXT4_I(inode)->i_mmap_sem);
330 sb_end_pagefault(sb);
332 up_read(&EXT4_I(inode)->i_mmap_sem);
338 static int ext4_dax_fault(struct vm_fault *vmf)
340 return ext4_dax_huge_fault(vmf, PE_SIZE_PTE);
343 static const struct vm_operations_struct ext4_dax_vm_ops = {
344 .fault = ext4_dax_fault,
345 .huge_fault = ext4_dax_huge_fault,
346 .page_mkwrite = ext4_dax_fault,
347 .pfn_mkwrite = ext4_dax_fault,
350 #define ext4_dax_vm_ops ext4_file_vm_ops
353 static const struct vm_operations_struct ext4_file_vm_ops = {
354 .fault = ext4_filemap_fault,
355 .map_pages = filemap_map_pages,
356 .page_mkwrite = ext4_page_mkwrite,
359 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
361 struct inode *inode = file->f_mapping->host;
363 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
367 if (IS_DAX(file_inode(file))) {
368 vma->vm_ops = &ext4_dax_vm_ops;
369 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
371 vma->vm_ops = &ext4_file_vm_ops;
376 static int ext4_file_open(struct inode * inode, struct file * filp)
378 struct super_block *sb = inode->i_sb;
379 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
380 struct vfsmount *mnt = filp->f_path.mnt;
386 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
389 if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
391 sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
393 * Sample where the filesystem has been mounted and
394 * store it in the superblock for sysadmin convenience
395 * when trying to sort through large numbers of block
396 * devices or filesystem images.
398 memset(buf, 0, sizeof(buf));
400 path.dentry = mnt->mnt_root;
401 cp = d_path(&path, buf, sizeof(buf));
406 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
408 return PTR_ERR(handle);
409 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
410 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
412 ext4_journal_stop(handle);
415 strlcpy(sbi->s_es->s_last_mounted, cp,
416 sizeof(sbi->s_es->s_last_mounted));
417 ext4_handle_dirty_super(handle, sb);
418 ext4_journal_stop(handle);
421 if (ext4_encrypted_inode(inode)) {
422 ret = fscrypt_get_encryption_info(inode);
425 if (!fscrypt_has_encryption_key(inode))
429 dir = dget_parent(file_dentry(filp));
430 if (ext4_encrypted_inode(d_inode(dir)) &&
431 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
432 ext4_warning(inode->i_sb,
433 "Inconsistent encryption contexts: %lu/%lu",
434 (unsigned long) d_inode(dir)->i_ino,
435 (unsigned long) inode->i_ino);
441 * Set up the jbd2_inode if we are opening the inode for
442 * writing and the journal is present
444 if (filp->f_mode & FMODE_WRITE) {
445 ret = ext4_inode_attach_jinode(inode);
450 filp->f_mode |= FMODE_NOWAIT;
451 return dquot_file_open(inode, filp);
455 * Here we use ext4_map_blocks() to get a block mapping for a extent-based
456 * file rather than ext4_ext_walk_space() because we can introduce
457 * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
458 * function. When extent status tree has been fully implemented, it will
459 * track all extent status for a file and we can directly use it to
460 * retrieve the offset for SEEK_DATA/SEEK_HOLE.
464 * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
465 * lookup page cache to check whether or not there has some data between
466 * [startoff, endoff] because, if this range contains an unwritten extent,
467 * we determine this extent as a data or a hole according to whether the
468 * page cache has data or not.
470 static int ext4_find_unwritten_pgoff(struct inode *inode,
476 unsigned int blkbits;
484 blkbits = inode->i_sb->s_blocksize_bits;
487 endoff = (loff_t)end_blk << blkbits;
489 index = startoff >> PAGE_SHIFT;
490 end = (endoff - 1) >> PAGE_SHIFT;
492 pagevec_init(&pvec, 0);
495 unsigned long nr_pages;
497 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
502 for (i = 0; i < nr_pages; i++) {
503 struct page *page = pvec.pages[i];
504 struct buffer_head *bh, *head;
507 * If current offset is smaller than the page offset,
508 * there is a hole at this offset.
510 if (whence == SEEK_HOLE && lastoff < endoff &&
511 lastoff < page_offset(pvec.pages[i])) {
519 if (unlikely(page->mapping != inode->i_mapping)) {
524 if (!page_has_buffers(page)) {
529 if (page_has_buffers(page)) {
530 lastoff = page_offset(page);
531 bh = head = page_buffers(page);
533 if (lastoff + bh->b_size <= startoff)
535 if (buffer_uptodate(bh) ||
536 buffer_unwritten(bh)) {
537 if (whence == SEEK_DATA)
540 if (whence == SEEK_HOLE)
544 *offset = max_t(loff_t,
550 lastoff += bh->b_size;
551 bh = bh->b_this_page;
552 } while (bh != head);
555 lastoff = page_offset(page) + PAGE_SIZE;
559 pagevec_release(&pvec);
560 } while (index <= end);
562 /* There are no pages upto endoff - that would be a hole in there. */
563 if (whence == SEEK_HOLE && lastoff < endoff) {
568 pagevec_release(&pvec);
573 * ext4_seek_data() retrieves the offset for SEEK_DATA.
575 static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
577 struct inode *inode = file->f_mapping->host;
578 struct extent_status es;
579 ext4_lblk_t start, last, end;
580 loff_t dataoff, isize;
586 isize = i_size_read(inode);
587 if (offset < 0 || offset >= isize) {
592 * Make sure inline data cannot be created anymore since we are going
593 * to allocate blocks for DIO. We know the inode does not have any
594 * inline data now because ext4_dio_supported() checked for that.
596 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
598 blkbits = inode->i_sb->s_blocksize_bits;
599 start = offset >> blkbits;
601 end = isize >> blkbits;
605 ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
607 /* No extent found -> no data */
616 dataoff = (loff_t)last << blkbits;
617 if (!ext4_es_is_unwritten(&es))
621 * If there is a unwritten extent at this offset,
622 * it will be as a data or a hole according to page
623 * cache that has data or not.
625 if (ext4_find_unwritten_pgoff(inode, SEEK_DATA,
626 es.es_lblk + es.es_len, &dataoff))
629 dataoff = (loff_t)last << blkbits;
631 } while (last <= end);
638 return vfs_setpos(file, dataoff, maxsize);
642 * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
644 static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
646 struct inode *inode = file->f_mapping->host;
647 struct extent_status es;
648 ext4_lblk_t start, last, end;
649 loff_t holeoff, isize;
655 isize = i_size_read(inode);
656 if (offset < 0 || offset >= isize) {
661 blkbits = inode->i_sb->s_blocksize_bits;
662 start = offset >> blkbits;
664 end = isize >> blkbits;
668 ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
674 if (ret == 0 || es.es_lblk > last) {
676 holeoff = (loff_t)last << blkbits;
680 * If there is a unwritten extent at this offset,
681 * it will be as a data or a hole according to page
682 * cache that has data or not.
684 if (ext4_es_is_unwritten(&es) &&
685 ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
686 last + es.es_len, &holeoff))
690 holeoff = (loff_t)last << blkbits;
692 } while (last <= end);
699 return vfs_setpos(file, holeoff, maxsize);
703 * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
704 * by calling generic_file_llseek_size() with the appropriate maxbytes
707 loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
709 struct inode *inode = file->f_mapping->host;
712 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
713 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
715 maxbytes = inode->i_sb->s_maxbytes;
721 return generic_file_llseek_size(file, offset, whence,
722 maxbytes, i_size_read(inode));
724 return ext4_seek_data(file, offset, maxbytes);
726 return ext4_seek_hole(file, offset, maxbytes);
732 const struct file_operations ext4_file_operations = {
733 .llseek = ext4_llseek,
734 .read_iter = ext4_file_read_iter,
735 .write_iter = ext4_file_write_iter,
736 .unlocked_ioctl = ext4_ioctl,
738 .compat_ioctl = ext4_compat_ioctl,
740 .mmap = ext4_file_mmap,
741 .open = ext4_file_open,
742 .release = ext4_release_file,
743 .fsync = ext4_sync_file,
744 .get_unmapped_area = thp_get_unmapped_area,
745 .splice_read = generic_file_splice_read,
746 .splice_write = iter_file_splice_write,
747 .fallocate = ext4_fallocate,
750 const struct inode_operations ext4_file_inode_operations = {
751 .setattr = ext4_setattr,
752 .getattr = ext4_file_getattr,
753 .listxattr = ext4_listxattr,
754 .get_acl = ext4_get_acl,
755 .set_acl = ext4_set_acl,
756 .fiemap = ext4_fiemap,