Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/file.c |
| 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * from |
| 10 | * |
| 11 | * linux/fs/minix/file.c |
| 12 | * |
| 13 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 14 | * |
| 15 | * ext2 fs regular file handling primitives |
| 16 | * |
| 17 | * 64-bit file support on 64-bit platforms by Jakub Jelinek |
| 18 | * (jj@sunsite.ms.mff.cuni.cz) |
| 19 | */ |
| 20 | |
| 21 | #include <linux/time.h> |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
Matthew Wilcox | c94c2ac | 2015-09-08 14:58:40 -0700 | [diff] [blame] | 23 | #include <linux/dax.h> |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 24 | #include <linux/quotaops.h> |
Christoph Hellwig | 25f4e70 | 2016-09-19 11:30:29 +1000 | [diff] [blame] | 25 | #include <linux/iomap.h> |
| 26 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "ext2.h" |
| 28 | #include "xattr.h" |
| 29 | #include "acl.h" |
| 30 | |
Matthew Wilcox | 6cd176a | 2015-02-16 15:59:25 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_FS_DAX |
Christoph Hellwig | 25f4e70 | 2016-09-19 11:30:29 +1000 | [diff] [blame] | 32 | static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to) |
| 33 | { |
| 34 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 35 | ssize_t ret; |
| 36 | |
| 37 | if (!iov_iter_count(to)) |
| 38 | return 0; /* skip atime */ |
| 39 | |
| 40 | inode_lock_shared(inode); |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 41 | ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops); |
Christoph Hellwig | 25f4e70 | 2016-09-19 11:30:29 +1000 | [diff] [blame] | 42 | inode_unlock_shared(inode); |
| 43 | |
| 44 | file_accessed(iocb->ki_filp); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 49 | { |
| 50 | struct file *file = iocb->ki_filp; |
| 51 | struct inode *inode = file->f_mapping->host; |
| 52 | ssize_t ret; |
| 53 | |
| 54 | inode_lock(inode); |
| 55 | ret = generic_write_checks(iocb, from); |
| 56 | if (ret <= 0) |
| 57 | goto out_unlock; |
| 58 | ret = file_remove_privs(file); |
| 59 | if (ret) |
| 60 | goto out_unlock; |
| 61 | ret = file_update_time(file); |
| 62 | if (ret) |
| 63 | goto out_unlock; |
| 64 | |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 65 | ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops); |
Christoph Hellwig | 25f4e70 | 2016-09-19 11:30:29 +1000 | [diff] [blame] | 66 | if (ret > 0 && iocb->ki_pos > i_size_read(inode)) { |
| 67 | i_size_write(inode, iocb->ki_pos); |
| 68 | mark_inode_dirty(inode); |
| 69 | } |
| 70 | |
| 71 | out_unlock: |
| 72 | inode_unlock(inode); |
| 73 | if (ret > 0) |
| 74 | ret = generic_write_sync(iocb, ret); |
| 75 | return ret; |
| 76 | } |
| 77 | |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 78 | /* |
| 79 | * The lock ordering for ext2 DAX fault paths is: |
| 80 | * |
| 81 | * mmap_sem (MM) |
| 82 | * sb_start_pagefault (vfs, freeze) |
| 83 | * ext2_inode_info->dax_sem |
| 84 | * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX) |
| 85 | * ext2_inode_info->truncate_mutex |
| 86 | * |
| 87 | * The default page_lock and i_size verification done by non-DAX fault paths |
| 88 | * is sufficient because ext2 doesn't support hole punching. |
| 89 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 90 | static int ext2_dax_fault(struct vm_fault *vmf) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 91 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 92 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 93 | struct ext2_inode_info *ei = EXT2_I(inode); |
| 94 | int ret; |
| 95 | |
| 96 | if (vmf->flags & FAULT_FLAG_WRITE) { |
| 97 | sb_start_pagefault(inode->i_sb); |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 98 | file_update_time(vmf->vma->vm_file); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 99 | } |
| 100 | down_read(&ei->dax_sem); |
| 101 | |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 102 | ret = dax_iomap_fault(vmf, PE_SIZE_PTE, &ext2_iomap_ops); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 103 | |
| 104 | up_read(&ei->dax_sem); |
| 105 | if (vmf->flags & FAULT_FLAG_WRITE) |
| 106 | sb_end_pagefault(inode->i_sb); |
| 107 | return ret; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 110 | static int ext2_dax_pfn_mkwrite(struct vm_fault *vmf) |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 111 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 112 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 113 | struct ext2_inode_info *ei = EXT2_I(inode); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 114 | loff_t size; |
Ross Zwisler | 80b4adc | 2016-01-22 15:10:50 -0800 | [diff] [blame] | 115 | int ret; |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 116 | |
| 117 | sb_start_pagefault(inode->i_sb); |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 118 | file_update_time(vmf->vma->vm_file); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 119 | down_read(&ei->dax_sem); |
| 120 | |
| 121 | /* check that the faulting page hasn't raced with truncate */ |
| 122 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 123 | if (vmf->pgoff >= size) |
| 124 | ret = VM_FAULT_SIGBUS; |
Ross Zwisler | 80b4adc | 2016-01-22 15:10:50 -0800 | [diff] [blame] | 125 | else |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 126 | ret = dax_pfn_mkwrite(vmf); |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 127 | |
| 128 | up_read(&ei->dax_sem); |
| 129 | sb_end_pagefault(inode->i_sb); |
| 130 | return ret; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static const struct vm_operations_struct ext2_dax_vm_ops = { |
| 134 | .fault = ext2_dax_fault, |
Ross Zwisler | 03e0990 | 2016-11-08 11:31:33 +1100 | [diff] [blame] | 135 | /* |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 136 | * .huge_fault is not supported for DAX because allocation in ext2 |
Ross Zwisler | 03e0990 | 2016-11-08 11:31:33 +1100 | [diff] [blame] | 137 | * cannot be reliably aligned to huge page sizes and so pmd faults |
| 138 | * will always fail and fail back to regular faults. |
| 139 | */ |
Ross Zwisler | 1e9d180 | 2016-02-27 14:01:13 -0500 | [diff] [blame] | 140 | .page_mkwrite = ext2_dax_fault, |
Ross Zwisler | 5726b27 | 2015-10-13 16:25:37 -0600 | [diff] [blame] | 141 | .pfn_mkwrite = ext2_dax_pfn_mkwrite, |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 145 | { |
| 146 | if (!IS_DAX(file_inode(file))) |
| 147 | return generic_file_mmap(file, vma); |
| 148 | |
| 149 | file_accessed(file); |
| 150 | vma->vm_ops = &ext2_dax_vm_ops; |
Ross Zwisler | 03e0990 | 2016-11-08 11:31:33 +1100 | [diff] [blame] | 151 | vma->vm_flags |= VM_MIXEDMAP; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | #else |
| 155 | #define ext2_file_mmap generic_file_mmap |
| 156 | #endif |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* |
Jan Kara | a6739af | 2007-07-15 23:40:22 -0700 | [diff] [blame] | 159 | * Called when filp is released. This happens when all file descriptors |
| 160 | * for a single struct file are closed. Note that different open() calls |
| 161 | * for the same file yield different struct file structures. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | */ |
| 163 | static int ext2_release_file (struct inode * inode, struct file * filp) |
| 164 | { |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 165 | if (filp->f_mode & FMODE_WRITE) { |
| 166 | mutex_lock(&EXT2_I(inode)->truncate_mutex); |
| 167 | ext2_discard_reservation(inode); |
| 168 | mutex_unlock(&EXT2_I(inode)->truncate_mutex); |
| 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 173 | int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 174 | { |
| 175 | int ret; |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 176 | struct super_block *sb = file->f_mapping->host->i_sb; |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 177 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 178 | ret = generic_file_fsync(file, start, end, datasync); |
Jeff Layton | dac257f | 2017-07-06 07:02:21 -0400 | [diff] [blame] | 179 | if (ret == -EIO) |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 180 | /* We don't really know where the IO error happened... */ |
| 181 | ext2_error(sb, __func__, |
| 182 | "detected IO error when writing metadata buffers"); |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
Christoph Hellwig | 25f4e70 | 2016-09-19 11:30:29 +1000 | [diff] [blame] | 186 | static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
| 187 | { |
| 188 | #ifdef CONFIG_FS_DAX |
| 189 | if (IS_DAX(iocb->ki_filp->f_mapping->host)) |
| 190 | return ext2_dax_read_iter(iocb, to); |
| 191 | #endif |
| 192 | return generic_file_read_iter(iocb, to); |
| 193 | } |
| 194 | |
| 195 | static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 196 | { |
| 197 | #ifdef CONFIG_FS_DAX |
| 198 | if (IS_DAX(iocb->ki_filp->f_mapping->host)) |
| 199 | return ext2_dax_write_iter(iocb, from); |
| 200 | #endif |
| 201 | return generic_file_write_iter(iocb, from); |
| 202 | } |
| 203 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 204 | const struct file_operations ext2_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | .llseek = generic_file_llseek, |
Christoph Hellwig | 25f4e70 | 2016-09-19 11:30:29 +1000 | [diff] [blame] | 206 | .read_iter = ext2_file_read_iter, |
| 207 | .write_iter = ext2_file_write_iter, |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 208 | .unlocked_ioctl = ext2_ioctl, |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 209 | #ifdef CONFIG_COMPAT |
| 210 | .compat_ioctl = ext2_compat_ioctl, |
| 211 | #endif |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 212 | .mmap = ext2_file_mmap, |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 213 | .open = dquot_file_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | .release = ext2_release_file, |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 215 | .fsync = ext2_fsync, |
Toshi Kani | dbe6ec8 | 2016-10-07 16:59:59 -0700 | [diff] [blame] | 216 | .get_unmapped_area = thp_get_unmapped_area, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 217 | .splice_read = generic_file_splice_read, |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 218 | .splice_write = iter_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 221 | const struct inode_operations ext2_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #ifdef CONFIG_EXT2_FS_XATTR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | .listxattr = ext2_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | #endif |
| 225 | .setattr = ext2_setattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 226 | .get_acl = ext2_get_acl, |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 227 | .set_acl = ext2_set_acl, |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 228 | .fiemap = ext2_fiemap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | }; |