^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/ext2/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1992, 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * linux/fs/minix/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * ext2 fs regular file handling primitives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * 64-bit file support on 64-bit platforms by Jakub Jelinek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * (jj@sunsite.ms.mff.cuni.cz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/dax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/iomap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "ext2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #ifdef CONFIG_FS_DAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct inode *inode = iocb->ki_filp->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!iov_iter_count(to))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0; /* skip atime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) inode_lock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) inode_unlock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) file_accessed(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = generic_write_checks(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ret = file_remove_privs(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = file_update_time(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) i_size_write(inode, iocb->ki_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = generic_write_sync(iocb, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * The lock ordering for ext2 DAX fault paths is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * mmap_lock (MM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * sb_start_pagefault (vfs, freeze)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * ext2_inode_info->dax_sem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * ext2_inode_info->truncate_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * The default page_lock and i_size verification done by non-DAX fault paths
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * is sufficient because ext2 doesn't support hole punching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static vm_fault_t ext2_dax_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct inode *inode = file_inode(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct ext2_inode_info *ei = EXT2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) (vmf->vma->vm_flags & VM_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) sb_start_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) file_update_time(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) down_read(&ei->dax_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = dax_iomap_fault(vmf, PE_SIZE_PTE, NULL, NULL, &ext2_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) up_read(&ei->dax_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) sb_end_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static const struct vm_operations_struct ext2_dax_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .fault = ext2_dax_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * .huge_fault is not supported for DAX because allocation in ext2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * cannot be reliably aligned to huge page sizes and so pmd faults
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * will always fail and fail back to regular faults.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .page_mkwrite = ext2_dax_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .pfn_mkwrite = ext2_dax_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!IS_DAX(file_inode(file)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return generic_file_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) file_accessed(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) vma->vm_ops = &ext2_dax_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define ext2_file_mmap generic_file_mmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Called when filp is released. This happens when all file descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * for a single struct file are closed. Note that different open() calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * for the same file yield different struct file structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int ext2_release_file (struct inode * inode, struct file * filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (filp->f_mode & FMODE_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mutex_lock(&EXT2_I(inode)->truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ext2_discard_reservation(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) mutex_unlock(&EXT2_I(inode)->truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct super_block *sb = file->f_mapping->host->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ret = generic_file_fsync(file, start, end, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* We don't really know where the IO error happened... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ext2_error(sb, __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) "detected IO error when writing metadata buffers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #ifdef CONFIG_FS_DAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (IS_DAX(iocb->ki_filp->f_mapping->host))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return ext2_dax_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return generic_file_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #ifdef CONFIG_FS_DAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (IS_DAX(iocb->ki_filp->f_mapping->host))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return ext2_dax_write_iter(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return generic_file_write_iter(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) const struct file_operations ext2_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .read_iter = ext2_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .write_iter = ext2_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .unlocked_ioctl = ext2_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .compat_ioctl = ext2_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .mmap = ext2_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .open = dquot_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .release = ext2_release_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .fsync = ext2_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .get_unmapped_area = thp_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .splice_write = iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) const struct inode_operations ext2_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .listxattr = ext2_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .getattr = ext2_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .setattr = ext2_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .get_acl = ext2_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .set_acl = ext2_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .fiemap = ext2_fiemap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };