blob: 96044f5dbc0e0232386e9bd489dc20ae6ecdf066 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/ext2/file.c
4 *
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)
9 *
10 * from
11 *
12 * linux/fs/minix/file.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
16 * ext2 fs regular file handling primitives
17 *
18 * 64-bit file support on 64-bit platforms by Jakub Jelinek
19 * (jj@sunsite.ms.mff.cuni.cz)
20 */
21
22#include <linux/time.h>
Jan Kara48bde862009-12-15 16:46:49 -080023#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070024#include <linux/dax.h>
Christoph Hellwig871a2932010-03-03 09:05:07 -050025#include <linux/quotaops.h>
Christoph Hellwig25f4e702016-09-19 11:30:29 +100026#include <linux/iomap.h>
27#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "ext2.h"
29#include "xattr.h"
30#include "acl.h"
31
Matthew Wilcox6cd176a2015-02-16 15:59:25 -080032#ifdef CONFIG_FS_DAX
Christoph Hellwig25f4e702016-09-19 11:30:29 +100033static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
34{
35 struct inode *inode = iocb->ki_filp->f_mapping->host;
36 ssize_t ret;
37
38 if (!iov_iter_count(to))
39 return 0; /* skip atime */
40
41 inode_lock_shared(inode);
Ross Zwisler11c59c92016-11-08 11:32:46 +110042 ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops);
Christoph Hellwig25f4e702016-09-19 11:30:29 +100043 inode_unlock_shared(inode);
44
45 file_accessed(iocb->ki_filp);
46 return ret;
47}
48
49static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
50{
51 struct file *file = iocb->ki_filp;
52 struct inode *inode = file->f_mapping->host;
53 ssize_t ret;
54
55 inode_lock(inode);
56 ret = generic_write_checks(iocb, from);
57 if (ret <= 0)
58 goto out_unlock;
59 ret = file_remove_privs(file);
60 if (ret)
61 goto out_unlock;
62 ret = file_update_time(file);
63 if (ret)
64 goto out_unlock;
65
Ross Zwisler11c59c92016-11-08 11:32:46 +110066 ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops);
Christoph Hellwig25f4e702016-09-19 11:30:29 +100067 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
68 i_size_write(inode, iocb->ki_pos);
69 mark_inode_dirty(inode);
70 }
71
72out_unlock:
73 inode_unlock(inode);
74 if (ret > 0)
75 ret = generic_write_sync(iocb, ret);
76 return ret;
77}
78
Ross Zwisler5726b272015-10-13 16:25:37 -060079/*
80 * The lock ordering for ext2 DAX fault paths is:
81 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070082 * mmap_lock (MM)
Ross Zwisler5726b272015-10-13 16:25:37 -060083 * sb_start_pagefault (vfs, freeze)
84 * ext2_inode_info->dax_sem
85 * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
86 * ext2_inode_info->truncate_mutex
87 *
88 * The default page_lock and i_size verification done by non-DAX fault paths
89 * is sufficient because ext2 doesn't support hole punching.
90 */
Souptick Joarder06856932018-04-15 01:03:42 +053091static vm_fault_t ext2_dax_fault(struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080092{
Dave Jiang11bac802017-02-24 14:56:41 -080093 struct inode *inode = file_inode(vmf->vma->vm_file);
Ross Zwisler5726b272015-10-13 16:25:37 -060094 struct ext2_inode_info *ei = EXT2_I(inode);
Souptick Joarder06856932018-04-15 01:03:42 +053095 vm_fault_t ret;
Mikulas Patocka1ef6ea02020-09-05 08:12:01 -040096 bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
97 (vmf->vma->vm_flags & VM_SHARED);
Ross Zwisler5726b272015-10-13 16:25:37 -060098
Mikulas Patocka1ef6ea02020-09-05 08:12:01 -040099 if (write) {
Ross Zwisler5726b272015-10-13 16:25:37 -0600100 sb_start_pagefault(inode->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -0800101 file_update_time(vmf->vma->vm_file);
Ross Zwisler5726b272015-10-13 16:25:37 -0600102 }
103 down_read(&ei->dax_sem);
104
Jan Karac0b24622018-01-07 16:38:43 -0500105 ret = dax_iomap_fault(vmf, PE_SIZE_PTE, NULL, NULL, &ext2_iomap_ops);
Ross Zwisler5726b272015-10-13 16:25:37 -0600106
107 up_read(&ei->dax_sem);
Mikulas Patocka1ef6ea02020-09-05 08:12:01 -0400108 if (write)
Ross Zwisler5726b272015-10-13 16:25:37 -0600109 sb_end_pagefault(inode->i_sb);
110 return ret;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800111}
112
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800113static const struct vm_operations_struct ext2_dax_vm_ops = {
114 .fault = ext2_dax_fault,
Ross Zwisler03e09902016-11-08 11:31:33 +1100115 /*
Dave Jianga2d58162017-02-24 14:56:59 -0800116 * .huge_fault is not supported for DAX because allocation in ext2
Ross Zwisler03e09902016-11-08 11:31:33 +1100117 * cannot be reliably aligned to huge page sizes and so pmd faults
118 * will always fail and fail back to regular faults.
119 */
Ross Zwisler1e9d1802016-02-27 14:01:13 -0500120 .page_mkwrite = ext2_dax_fault,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700121 .pfn_mkwrite = ext2_dax_fault,
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800122};
123
124static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
125{
126 if (!IS_DAX(file_inode(file)))
127 return generic_file_mmap(file, vma);
128
129 file_accessed(file);
130 vma->vm_ops = &ext2_dax_vm_ops;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800131 return 0;
132}
133#else
134#define ext2_file_mmap generic_file_mmap
135#endif
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
Jan Karaa6739af2007-07-15 23:40:22 -0700138 * Called when filp is released. This happens when all file descriptors
139 * for a single struct file are closed. Note that different open() calls
140 * for the same file yield different struct file structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142static int ext2_release_file (struct inode * inode, struct file * filp)
143{
Martin J. Bligha686cd82007-10-16 23:30:46 -0700144 if (filp->f_mode & FMODE_WRITE) {
145 mutex_lock(&EXT2_I(inode)->truncate_mutex);
146 ext2_discard_reservation(inode);
147 mutex_unlock(&EXT2_I(inode)->truncate_mutex);
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
150}
151
Josef Bacik02c24a82011-07-16 20:44:56 -0400152int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Jan Kara48bde862009-12-15 16:46:49 -0800153{
154 int ret;
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200155 struct super_block *sb = file->f_mapping->host->i_sb;
Jan Kara48bde862009-12-15 16:46:49 -0800156
Josef Bacik02c24a82011-07-16 20:44:56 -0400157 ret = generic_file_fsync(file, start, end, datasync);
Jeff Laytondac257f2017-07-06 07:02:21 -0400158 if (ret == -EIO)
Jan Kara48bde862009-12-15 16:46:49 -0800159 /* We don't really know where the IO error happened... */
160 ext2_error(sb, __func__,
161 "detected IO error when writing metadata buffers");
Jan Kara48bde862009-12-15 16:46:49 -0800162 return ret;
163}
164
Christoph Hellwig25f4e702016-09-19 11:30:29 +1000165static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
166{
167#ifdef CONFIG_FS_DAX
168 if (IS_DAX(iocb->ki_filp->f_mapping->host))
169 return ext2_dax_read_iter(iocb, to);
170#endif
171 return generic_file_read_iter(iocb, to);
172}
173
174static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
175{
176#ifdef CONFIG_FS_DAX
177 if (IS_DAX(iocb->ki_filp->f_mapping->host))
178 return ext2_dax_write_iter(iocb, from);
179#endif
180 return generic_file_write_iter(iocb, from);
181}
182
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800183const struct file_operations ext2_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .llseek = generic_file_llseek,
Christoph Hellwig25f4e702016-09-19 11:30:29 +1000185 .read_iter = ext2_file_read_iter,
186 .write_iter = ext2_file_write_iter,
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800187 .unlocked_ioctl = ext2_ioctl,
David Howellse322ff02006-08-29 19:06:20 +0100188#ifdef CONFIG_COMPAT
189 .compat_ioctl = ext2_compat_ioctl,
190#endif
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800191 .mmap = ext2_file_mmap,
Christoph Hellwig907f4552010-03-03 09:05:06 -0500192 .open = dquot_file_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .release = ext2_release_file,
Jan Kara48bde862009-12-15 16:46:49 -0800194 .fsync = ext2_fsync,
Toshi Kanidbe6ec82016-10-07 16:59:59 -0700195 .get_unmapped_area = thp_get_unmapped_area,
Jens Axboe5274f052006-03-30 15:15:30 +0200196 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400197 .splice_write = iter_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
Arjan van de Ven754661f2007-02-12 00:55:38 -0800200const struct inode_operations ext2_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .listxattr = ext2_listxattr,
yangerkun93bc4202019-02-18 09:07:02 +0800202 .getattr = ext2_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .setattr = ext2_setattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200204 .get_acl = ext2_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800205 .set_acl = ext2_set_acl,
Josef Bacik68c9d702008-10-03 17:32:43 -0400206 .fiemap = ext2_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};