xref: /openbmc/linux/fs/ext2/file.c (revision 25f4e702)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/ext2/file.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1992, 1993, 1994, 1995
51da177e4SLinus Torvalds  * Remy Card (card@masi.ibp.fr)
61da177e4SLinus Torvalds  * Laboratoire MASI - Institut Blaise Pascal
71da177e4SLinus Torvalds  * Universite Pierre et Marie Curie (Paris VI)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  from
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  linux/fs/minix/file.c
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  ext2 fs regular file handling primitives
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *  64-bit file support on 64-bit platforms by Jakub Jelinek
181da177e4SLinus Torvalds  * 	(jj@sunsite.ms.mff.cuni.cz)
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/time.h>
2248bde86dSJan Kara #include <linux/pagemap.h>
23c94c2acfSMatthew Wilcox #include <linux/dax.h>
24871a2931SChristoph Hellwig #include <linux/quotaops.h>
2525f4e702SChristoph Hellwig #include <linux/iomap.h>
2625f4e702SChristoph Hellwig #include <linux/uio.h>
271da177e4SLinus Torvalds #include "ext2.h"
281da177e4SLinus Torvalds #include "xattr.h"
291da177e4SLinus Torvalds #include "acl.h"
301da177e4SLinus Torvalds 
316cd176a5SMatthew Wilcox #ifdef CONFIG_FS_DAX
3225f4e702SChristoph Hellwig static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
3325f4e702SChristoph Hellwig {
3425f4e702SChristoph Hellwig 	struct inode *inode = iocb->ki_filp->f_mapping->host;
3525f4e702SChristoph Hellwig 	ssize_t ret;
3625f4e702SChristoph Hellwig 
3725f4e702SChristoph Hellwig 	if (!iov_iter_count(to))
3825f4e702SChristoph Hellwig 		return 0; /* skip atime */
3925f4e702SChristoph Hellwig 
4025f4e702SChristoph Hellwig 	inode_lock_shared(inode);
4125f4e702SChristoph Hellwig 	ret = iomap_dax_rw(iocb, to, &ext2_iomap_ops);
4225f4e702SChristoph Hellwig 	inode_unlock_shared(inode);
4325f4e702SChristoph Hellwig 
4425f4e702SChristoph Hellwig 	file_accessed(iocb->ki_filp);
4525f4e702SChristoph Hellwig 	return ret;
4625f4e702SChristoph Hellwig }
4725f4e702SChristoph Hellwig 
4825f4e702SChristoph Hellwig static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
4925f4e702SChristoph Hellwig {
5025f4e702SChristoph Hellwig 	struct file *file = iocb->ki_filp;
5125f4e702SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
5225f4e702SChristoph Hellwig 	ssize_t ret;
5325f4e702SChristoph Hellwig 
5425f4e702SChristoph Hellwig 	inode_lock(inode);
5525f4e702SChristoph Hellwig 	ret = generic_write_checks(iocb, from);
5625f4e702SChristoph Hellwig 	if (ret <= 0)
5725f4e702SChristoph Hellwig 		goto out_unlock;
5825f4e702SChristoph Hellwig 	ret = file_remove_privs(file);
5925f4e702SChristoph Hellwig 	if (ret)
6025f4e702SChristoph Hellwig 		goto out_unlock;
6125f4e702SChristoph Hellwig 	ret = file_update_time(file);
6225f4e702SChristoph Hellwig 	if (ret)
6325f4e702SChristoph Hellwig 		goto out_unlock;
6425f4e702SChristoph Hellwig 
6525f4e702SChristoph Hellwig 	ret = iomap_dax_rw(iocb, from, &ext2_iomap_ops);
6625f4e702SChristoph Hellwig 	if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
6725f4e702SChristoph Hellwig 		i_size_write(inode, iocb->ki_pos);
6825f4e702SChristoph Hellwig 		mark_inode_dirty(inode);
6925f4e702SChristoph Hellwig 	}
7025f4e702SChristoph Hellwig 
7125f4e702SChristoph Hellwig out_unlock:
7225f4e702SChristoph Hellwig 	inode_unlock(inode);
7325f4e702SChristoph Hellwig 	if (ret > 0)
7425f4e702SChristoph Hellwig 		ret = generic_write_sync(iocb, ret);
7525f4e702SChristoph Hellwig 	return ret;
7625f4e702SChristoph Hellwig }
7725f4e702SChristoph Hellwig 
785726b27bSRoss Zwisler /*
795726b27bSRoss Zwisler  * The lock ordering for ext2 DAX fault paths is:
805726b27bSRoss Zwisler  *
815726b27bSRoss Zwisler  * mmap_sem (MM)
825726b27bSRoss Zwisler  *   sb_start_pagefault (vfs, freeze)
835726b27bSRoss Zwisler  *     ext2_inode_info->dax_sem
845726b27bSRoss Zwisler  *       address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
855726b27bSRoss Zwisler  *         ext2_inode_info->truncate_mutex
865726b27bSRoss Zwisler  *
875726b27bSRoss Zwisler  * The default page_lock and i_size verification done by non-DAX fault paths
885726b27bSRoss Zwisler  * is sufficient because ext2 doesn't support hole punching.
895726b27bSRoss Zwisler  */
90f7ca90b1SMatthew Wilcox static int ext2_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
91f7ca90b1SMatthew Wilcox {
925726b27bSRoss Zwisler 	struct inode *inode = file_inode(vma->vm_file);
935726b27bSRoss Zwisler 	struct ext2_inode_info *ei = EXT2_I(inode);
945726b27bSRoss Zwisler 	int ret;
955726b27bSRoss Zwisler 
965726b27bSRoss Zwisler 	if (vmf->flags & FAULT_FLAG_WRITE) {
975726b27bSRoss Zwisler 		sb_start_pagefault(inode->i_sb);
985726b27bSRoss Zwisler 		file_update_time(vma->vm_file);
995726b27bSRoss Zwisler 	}
1005726b27bSRoss Zwisler 	down_read(&ei->dax_sem);
1015726b27bSRoss Zwisler 
10225f4e702SChristoph Hellwig 	ret = iomap_dax_fault(vma, vmf, &ext2_iomap_ops);
1035726b27bSRoss Zwisler 
1045726b27bSRoss Zwisler 	up_read(&ei->dax_sem);
1055726b27bSRoss Zwisler 	if (vmf->flags & FAULT_FLAG_WRITE)
1065726b27bSRoss Zwisler 		sb_end_pagefault(inode->i_sb);
1075726b27bSRoss Zwisler 	return ret;
108f7ca90b1SMatthew Wilcox }
109f7ca90b1SMatthew Wilcox 
110e7b1ea2aSMatthew Wilcox static int ext2_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
111e7b1ea2aSMatthew Wilcox 						pmd_t *pmd, unsigned int flags)
112e7b1ea2aSMatthew Wilcox {
1135726b27bSRoss Zwisler 	struct inode *inode = file_inode(vma->vm_file);
1145726b27bSRoss Zwisler 	struct ext2_inode_info *ei = EXT2_I(inode);
1155726b27bSRoss Zwisler 	int ret;
1165726b27bSRoss Zwisler 
1175726b27bSRoss Zwisler 	if (flags & FAULT_FLAG_WRITE) {
1185726b27bSRoss Zwisler 		sb_start_pagefault(inode->i_sb);
1195726b27bSRoss Zwisler 		file_update_time(vma->vm_file);
1205726b27bSRoss Zwisler 	}
1215726b27bSRoss Zwisler 	down_read(&ei->dax_sem);
1225726b27bSRoss Zwisler 
1236b524995SRoss Zwisler 	ret = dax_pmd_fault(vma, addr, pmd, flags, ext2_get_block);
1245726b27bSRoss Zwisler 
1255726b27bSRoss Zwisler 	up_read(&ei->dax_sem);
1265726b27bSRoss Zwisler 	if (flags & FAULT_FLAG_WRITE)
1275726b27bSRoss Zwisler 		sb_end_pagefault(inode->i_sb);
1285726b27bSRoss Zwisler 	return ret;
129e7b1ea2aSMatthew Wilcox }
130e7b1ea2aSMatthew Wilcox 
1315726b27bSRoss Zwisler static int ext2_dax_pfn_mkwrite(struct vm_area_struct *vma,
1325726b27bSRoss Zwisler 		struct vm_fault *vmf)
1335726b27bSRoss Zwisler {
1345726b27bSRoss Zwisler 	struct inode *inode = file_inode(vma->vm_file);
1355726b27bSRoss Zwisler 	struct ext2_inode_info *ei = EXT2_I(inode);
1365726b27bSRoss Zwisler 	loff_t size;
13780b4adcaSRoss Zwisler 	int ret;
1385726b27bSRoss Zwisler 
1395726b27bSRoss Zwisler 	sb_start_pagefault(inode->i_sb);
1405726b27bSRoss Zwisler 	file_update_time(vma->vm_file);
1415726b27bSRoss Zwisler 	down_read(&ei->dax_sem);
1425726b27bSRoss Zwisler 
1435726b27bSRoss Zwisler 	/* check that the faulting page hasn't raced with truncate */
1445726b27bSRoss Zwisler 	size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1455726b27bSRoss Zwisler 	if (vmf->pgoff >= size)
1465726b27bSRoss Zwisler 		ret = VM_FAULT_SIGBUS;
14780b4adcaSRoss Zwisler 	else
14880b4adcaSRoss Zwisler 		ret = dax_pfn_mkwrite(vma, vmf);
1495726b27bSRoss Zwisler 
1505726b27bSRoss Zwisler 	up_read(&ei->dax_sem);
1515726b27bSRoss Zwisler 	sb_end_pagefault(inode->i_sb);
1525726b27bSRoss Zwisler 	return ret;
153f7ca90b1SMatthew Wilcox }
154f7ca90b1SMatthew Wilcox 
155f7ca90b1SMatthew Wilcox static const struct vm_operations_struct ext2_dax_vm_ops = {
156f7ca90b1SMatthew Wilcox 	.fault		= ext2_dax_fault,
157e7b1ea2aSMatthew Wilcox 	.pmd_fault	= ext2_dax_pmd_fault,
1581e9d180bSRoss Zwisler 	.page_mkwrite	= ext2_dax_fault,
1595726b27bSRoss Zwisler 	.pfn_mkwrite	= ext2_dax_pfn_mkwrite,
160f7ca90b1SMatthew Wilcox };
161f7ca90b1SMatthew Wilcox 
162f7ca90b1SMatthew Wilcox static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
163f7ca90b1SMatthew Wilcox {
164f7ca90b1SMatthew Wilcox 	if (!IS_DAX(file_inode(file)))
165f7ca90b1SMatthew Wilcox 		return generic_file_mmap(file, vma);
166f7ca90b1SMatthew Wilcox 
167f7ca90b1SMatthew Wilcox 	file_accessed(file);
168f7ca90b1SMatthew Wilcox 	vma->vm_ops = &ext2_dax_vm_ops;
169e7b1ea2aSMatthew Wilcox 	vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
170f7ca90b1SMatthew Wilcox 	return 0;
171f7ca90b1SMatthew Wilcox }
172f7ca90b1SMatthew Wilcox #else
173f7ca90b1SMatthew Wilcox #define ext2_file_mmap	generic_file_mmap
174f7ca90b1SMatthew Wilcox #endif
175f7ca90b1SMatthew Wilcox 
1761da177e4SLinus Torvalds /*
177a6739af8SJan Kara  * Called when filp is released. This happens when all file descriptors
178a6739af8SJan Kara  * for a single struct file are closed. Note that different open() calls
179a6739af8SJan Kara  * for the same file yield different struct file structures.
1801da177e4SLinus Torvalds  */
1811da177e4SLinus Torvalds static int ext2_release_file (struct inode * inode, struct file * filp)
1821da177e4SLinus Torvalds {
183a686cd89SMartin J. Bligh 	if (filp->f_mode & FMODE_WRITE) {
184a686cd89SMartin J. Bligh 		mutex_lock(&EXT2_I(inode)->truncate_mutex);
185a686cd89SMartin J. Bligh 		ext2_discard_reservation(inode);
186a686cd89SMartin J. Bligh 		mutex_unlock(&EXT2_I(inode)->truncate_mutex);
187a686cd89SMartin J. Bligh 	}
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
19102c24a82SJosef Bacik int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
19248bde86dSJan Kara {
19348bde86dSJan Kara 	int ret;
1947ea80859SChristoph Hellwig 	struct super_block *sb = file->f_mapping->host->i_sb;
19548bde86dSJan Kara 	struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
19648bde86dSJan Kara 
19702c24a82SJosef Bacik 	ret = generic_file_fsync(file, start, end, datasync);
19848bde86dSJan Kara 	if (ret == -EIO || test_and_clear_bit(AS_EIO, &mapping->flags)) {
19948bde86dSJan Kara 		/* We don't really know where the IO error happened... */
20048bde86dSJan Kara 		ext2_error(sb, __func__,
20148bde86dSJan Kara 			   "detected IO error when writing metadata buffers");
20248bde86dSJan Kara 		ret = -EIO;
20348bde86dSJan Kara 	}
20448bde86dSJan Kara 	return ret;
20548bde86dSJan Kara }
20648bde86dSJan Kara 
20725f4e702SChristoph Hellwig static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
20825f4e702SChristoph Hellwig {
20925f4e702SChristoph Hellwig #ifdef CONFIG_FS_DAX
21025f4e702SChristoph Hellwig 	if (IS_DAX(iocb->ki_filp->f_mapping->host))
21125f4e702SChristoph Hellwig 		return ext2_dax_read_iter(iocb, to);
21225f4e702SChristoph Hellwig #endif
21325f4e702SChristoph Hellwig 	return generic_file_read_iter(iocb, to);
21425f4e702SChristoph Hellwig }
21525f4e702SChristoph Hellwig 
21625f4e702SChristoph Hellwig static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
21725f4e702SChristoph Hellwig {
21825f4e702SChristoph Hellwig #ifdef CONFIG_FS_DAX
21925f4e702SChristoph Hellwig 	if (IS_DAX(iocb->ki_filp->f_mapping->host))
22025f4e702SChristoph Hellwig 		return ext2_dax_write_iter(iocb, from);
22125f4e702SChristoph Hellwig #endif
22225f4e702SChristoph Hellwig 	return generic_file_write_iter(iocb, from);
22325f4e702SChristoph Hellwig }
22425f4e702SChristoph Hellwig 
2254b6f5d20SArjan van de Ven const struct file_operations ext2_file_operations = {
2261da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
22725f4e702SChristoph Hellwig 	.read_iter	= ext2_file_read_iter,
22825f4e702SChristoph Hellwig 	.write_iter	= ext2_file_write_iter,
22914f9f7b2SAndi Kleen 	.unlocked_ioctl = ext2_ioctl,
230e322ff07SDavid Howells #ifdef CONFIG_COMPAT
231e322ff07SDavid Howells 	.compat_ioctl	= ext2_compat_ioctl,
232e322ff07SDavid Howells #endif
233f7ca90b1SMatthew Wilcox 	.mmap		= ext2_file_mmap,
234907f4554SChristoph Hellwig 	.open		= dquot_file_open,
2351da177e4SLinus Torvalds 	.release	= ext2_release_file,
23648bde86dSJan Kara 	.fsync		= ext2_fsync,
2375274f052SJens Axboe 	.splice_read	= generic_file_splice_read,
2388d020765SAl Viro 	.splice_write	= iter_file_splice_write,
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds 
241754661f1SArjan van de Ven const struct inode_operations ext2_file_inode_operations = {
2421da177e4SLinus Torvalds #ifdef CONFIG_EXT2_FS_XATTR
2431da177e4SLinus Torvalds 	.setxattr	= generic_setxattr,
2441da177e4SLinus Torvalds 	.getxattr	= generic_getxattr,
2451da177e4SLinus Torvalds 	.listxattr	= ext2_listxattr,
2461da177e4SLinus Torvalds 	.removexattr	= generic_removexattr,
2471da177e4SLinus Torvalds #endif
2481da177e4SLinus Torvalds 	.setattr	= ext2_setattr,
2494e34e719SChristoph Hellwig 	.get_acl	= ext2_get_acl,
25064e178a7SChristoph Hellwig 	.set_acl	= ext2_set_acl,
25168c9d702SJosef Bacik 	.fiemap		= ext2_fiemap,
2521da177e4SLinus Torvalds };
253