blob: b0f241528a30a5e631e134556c1e9db9e3772c41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Kara48bde862009-12-15 16:46:49 -080022#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070023#include <linux/dax.h>
Christoph Hellwig871a2932010-03-03 09:05:07 -050024#include <linux/quotaops.h>
Christoph Hellwig25f4e702016-09-19 11:30:29 +100025#include <linux/iomap.h>
26#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "ext2.h"
28#include "xattr.h"
29#include "acl.h"
30
Matthew Wilcox6cd176a2015-02-16 15:59:25 -080031#ifdef CONFIG_FS_DAX
Christoph Hellwig25f4e702016-09-19 11:30:29 +100032static 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 Zwisler11c59c92016-11-08 11:32:46 +110041 ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops);
Christoph Hellwig25f4e702016-09-19 11:30:29 +100042 inode_unlock_shared(inode);
43
44 file_accessed(iocb->ki_filp);
45 return ret;
46}
47
48static 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 Zwisler11c59c92016-11-08 11:32:46 +110065 ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops);
Christoph Hellwig25f4e702016-09-19 11:30:29 +100066 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
71out_unlock:
72 inode_unlock(inode);
73 if (ret > 0)
74 ret = generic_write_sync(iocb, ret);
75 return ret;
76}
77
Ross Zwisler5726b272015-10-13 16:25:37 -060078/*
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 */
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080090static int ext2_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
91{
Ross Zwisler5726b272015-10-13 16:25:37 -060092 struct inode *inode = file_inode(vma->vm_file);
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);
98 file_update_time(vma->vm_file);
99 }
100 down_read(&ei->dax_sem);
101
Ross Zwisler11c59c92016-11-08 11:32:46 +1100102 ret = dax_iomap_fault(vma, vmf, &ext2_iomap_ops);
Ross Zwisler5726b272015-10-13 16:25:37 -0600103
104 up_read(&ei->dax_sem);
105 if (vmf->flags & FAULT_FLAG_WRITE)
106 sb_end_pagefault(inode->i_sb);
107 return ret;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800108}
109
Ross Zwisler5726b272015-10-13 16:25:37 -0600110static int ext2_dax_pfn_mkwrite(struct vm_area_struct *vma,
111 struct vm_fault *vmf)
112{
113 struct inode *inode = file_inode(vma->vm_file);
114 struct ext2_inode_info *ei = EXT2_I(inode);
Ross Zwisler5726b272015-10-13 16:25:37 -0600115 loff_t size;
Ross Zwisler80b4adc2016-01-22 15:10:50 -0800116 int ret;
Ross Zwisler5726b272015-10-13 16:25:37 -0600117
118 sb_start_pagefault(inode->i_sb);
119 file_update_time(vma->vm_file);
120 down_read(&ei->dax_sem);
121
122 /* check that the faulting page hasn't raced with truncate */
123 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
124 if (vmf->pgoff >= size)
125 ret = VM_FAULT_SIGBUS;
Ross Zwisler80b4adc2016-01-22 15:10:50 -0800126 else
127 ret = dax_pfn_mkwrite(vma, vmf);
Ross Zwisler5726b272015-10-13 16:25:37 -0600128
129 up_read(&ei->dax_sem);
130 sb_end_pagefault(inode->i_sb);
131 return ret;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800132}
133
134static const struct vm_operations_struct ext2_dax_vm_ops = {
135 .fault = ext2_dax_fault,
Ross Zwisler03e09902016-11-08 11:31:33 +1100136 /*
137 * .pmd_fault is not supported for DAX because allocation in ext2
138 * cannot be reliably aligned to huge page sizes and so pmd faults
139 * will always fail and fail back to regular faults.
140 */
Ross Zwisler1e9d1802016-02-27 14:01:13 -0500141 .page_mkwrite = ext2_dax_fault,
Ross Zwisler5726b272015-10-13 16:25:37 -0600142 .pfn_mkwrite = ext2_dax_pfn_mkwrite,
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800143};
144
145static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
146{
147 if (!IS_DAX(file_inode(file)))
148 return generic_file_mmap(file, vma);
149
150 file_accessed(file);
151 vma->vm_ops = &ext2_dax_vm_ops;
Ross Zwisler03e09902016-11-08 11:31:33 +1100152 vma->vm_flags |= VM_MIXEDMAP;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800153 return 0;
154}
155#else
156#define ext2_file_mmap generic_file_mmap
157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Jan Karaa6739af2007-07-15 23:40:22 -0700160 * Called when filp is released. This happens when all file descriptors
161 * for a single struct file are closed. Note that different open() calls
162 * for the same file yield different struct file structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
164static int ext2_release_file (struct inode * inode, struct file * filp)
165{
Martin J. Bligha686cd82007-10-16 23:30:46 -0700166 if (filp->f_mode & FMODE_WRITE) {
167 mutex_lock(&EXT2_I(inode)->truncate_mutex);
168 ext2_discard_reservation(inode);
169 mutex_unlock(&EXT2_I(inode)->truncate_mutex);
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
Josef Bacik02c24a82011-07-16 20:44:56 -0400174int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Jan Kara48bde862009-12-15 16:46:49 -0800175{
176 int ret;
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200177 struct super_block *sb = file->f_mapping->host->i_sb;
Jan Kara48bde862009-12-15 16:46:49 -0800178 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
179
Josef Bacik02c24a82011-07-16 20:44:56 -0400180 ret = generic_file_fsync(file, start, end, datasync);
Jan Kara48bde862009-12-15 16:46:49 -0800181 if (ret == -EIO || test_and_clear_bit(AS_EIO, &mapping->flags)) {
182 /* We don't really know where the IO error happened... */
183 ext2_error(sb, __func__,
184 "detected IO error when writing metadata buffers");
185 ret = -EIO;
186 }
187 return ret;
188}
189
Christoph Hellwig25f4e702016-09-19 11:30:29 +1000190static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
191{
192#ifdef CONFIG_FS_DAX
193 if (IS_DAX(iocb->ki_filp->f_mapping->host))
194 return ext2_dax_read_iter(iocb, to);
195#endif
196 return generic_file_read_iter(iocb, to);
197}
198
199static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
200{
201#ifdef CONFIG_FS_DAX
202 if (IS_DAX(iocb->ki_filp->f_mapping->host))
203 return ext2_dax_write_iter(iocb, from);
204#endif
205 return generic_file_write_iter(iocb, from);
206}
207
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800208const struct file_operations ext2_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .llseek = generic_file_llseek,
Christoph Hellwig25f4e702016-09-19 11:30:29 +1000210 .read_iter = ext2_file_read_iter,
211 .write_iter = ext2_file_write_iter,
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800212 .unlocked_ioctl = ext2_ioctl,
David Howellse322ff02006-08-29 19:06:20 +0100213#ifdef CONFIG_COMPAT
214 .compat_ioctl = ext2_compat_ioctl,
215#endif
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800216 .mmap = ext2_file_mmap,
Christoph Hellwig907f4552010-03-03 09:05:06 -0500217 .open = dquot_file_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .release = ext2_release_file,
Jan Kara48bde862009-12-15 16:46:49 -0800219 .fsync = ext2_fsync,
Toshi Kanidbe6ec82016-10-07 16:59:59 -0700220 .get_unmapped_area = thp_get_unmapped_area,
Jens Axboe5274f052006-03-30 15:15:30 +0200221 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400222 .splice_write = iter_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
Arjan van de Ven754661f2007-02-12 00:55:38 -0800225const struct inode_operations ext2_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#ifdef CONFIG_EXT2_FS_XATTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .listxattr = ext2_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#endif
229 .setattr = ext2_setattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200230 .get_acl = ext2_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800231 .set_acl = ext2_set_acl,
Josef Bacik68c9d702008-10-03 17:32:43 -0400232 .fiemap = ext2_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};