xref: /openbmc/linux/fs/nilfs2/file.c (revision ea5ddbc1)
1ae98043fSRyusuke Konishi // SPDX-License-Identifier: GPL-2.0+
2f183ff4fSRyusuke Konishi /*
394ee1d91SRyusuke Konishi  * NILFS regular file handling primitives including fsync().
4f183ff4fSRyusuke Konishi  *
5f183ff4fSRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6f183ff4fSRyusuke Konishi  *
74b420ab4SRyusuke Konishi  * Written by Amagai Yoshiji and Ryusuke Konishi.
8f183ff4fSRyusuke Konishi  */
9f183ff4fSRyusuke Konishi 
10f183ff4fSRyusuke Konishi #include <linux/fs.h>
11f183ff4fSRyusuke Konishi #include <linux/mm.h>
12f183ff4fSRyusuke Konishi #include <linux/writeback.h>
13f183ff4fSRyusuke Konishi #include "nilfs.h"
14f183ff4fSRyusuke Konishi #include "segment.h"
15f183ff4fSRyusuke Konishi 
nilfs_sync_file(struct file * file,loff_t start,loff_t end,int datasync)1602c24a82SJosef Bacik int nilfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
17f183ff4fSRyusuke Konishi {
18f183ff4fSRyusuke Konishi 	/*
19f183ff4fSRyusuke Konishi 	 * Called from fsync() system call
20f183ff4fSRyusuke Konishi 	 * This is the only entry point that can catch write and synch
21f183ff4fSRyusuke Konishi 	 * timing for both data blocks and intermediate blocks.
22f183ff4fSRyusuke Konishi 	 *
23f183ff4fSRyusuke Konishi 	 * This function should be implemented when the writeback function
24f183ff4fSRyusuke Konishi 	 * will be implemented.
25f183ff4fSRyusuke Konishi 	 */
2611475975SRyusuke Konishi 	struct the_nilfs *nilfs;
277ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
2875dc857cSAndreas Rohner 	int err = 0;
2902c24a82SJosef Bacik 
3011475975SRyusuke Konishi 	if (nilfs_inode_dirty(inode)) {
31f183ff4fSRyusuke Konishi 		if (datasync)
3211475975SRyusuke Konishi 			err = nilfs_construct_dsync_segment(inode->i_sb, inode,
3375dc857cSAndreas Rohner 							    start, end);
34f183ff4fSRyusuke Konishi 		else
35f183ff4fSRyusuke Konishi 			err = nilfs_construct_segment(inode->i_sb);
3611475975SRyusuke Konishi 	}
3711475975SRyusuke Konishi 
3811475975SRyusuke Konishi 	nilfs = inode->i_sb->s_fs_info;
39e2c7617aSAndreas Rohner 	if (!err)
40e2c7617aSAndreas Rohner 		err = nilfs_flush_device(nilfs);
41e2c7617aSAndreas Rohner 
42f183ff4fSRyusuke Konishi 	return err;
43f183ff4fSRyusuke Konishi }
44f183ff4fSRyusuke Konishi 
nilfs_page_mkwrite(struct vm_fault * vmf)45c8ed98cdSSouptick Joarder static vm_fault_t nilfs_page_mkwrite(struct vm_fault *vmf)
46f183ff4fSRyusuke Konishi {
4711bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
489ff05123SRyusuke Konishi 	struct page *page = vmf->page;
49496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
509ff05123SRyusuke Konishi 	struct nilfs_transaction_info ti;
512c22b337SJan Kara 	int ret = 0;
529ff05123SRyusuke Konishi 
53e3154e97SRyusuke Konishi 	if (unlikely(nilfs_near_disk_full(inode->i_sb->s_fs_info)))
549ff05123SRyusuke Konishi 		return VM_FAULT_SIGBUS; /* -ENOSPC */
559ff05123SRyusuke Konishi 
562c22b337SJan Kara 	sb_start_pagefault(inode->i_sb);
579ff05123SRyusuke Konishi 	lock_page(page);
589ff05123SRyusuke Konishi 	if (page->mapping != inode->i_mapping ||
599ff05123SRyusuke Konishi 	    page_offset(page) >= i_size_read(inode) || !PageUptodate(page)) {
609ff05123SRyusuke Konishi 		unlock_page(page);
612c22b337SJan Kara 		ret = -EFAULT;	/* make the VM retry the fault */
622c22b337SJan Kara 		goto out;
639ff05123SRyusuke Konishi 	}
649ff05123SRyusuke Konishi 
659ff05123SRyusuke Konishi 	/*
669ff05123SRyusuke Konishi 	 * check to see if the page is mapped already (no holes)
679ff05123SRyusuke Konishi 	 */
6834094537SRyusuke Konishi 	if (PageMappedToDisk(page))
699ff05123SRyusuke Konishi 		goto mapped;
7034094537SRyusuke Konishi 
719ff05123SRyusuke Konishi 	if (page_has_buffers(page)) {
729ff05123SRyusuke Konishi 		struct buffer_head *bh, *head;
739ff05123SRyusuke Konishi 		int fully_mapped = 1;
749ff05123SRyusuke Konishi 
759ff05123SRyusuke Konishi 		bh = head = page_buffers(page);
769ff05123SRyusuke Konishi 		do {
779ff05123SRyusuke Konishi 			if (!buffer_mapped(bh)) {
789ff05123SRyusuke Konishi 				fully_mapped = 0;
799ff05123SRyusuke Konishi 				break;
809ff05123SRyusuke Konishi 			}
819ff05123SRyusuke Konishi 		} while (bh = bh->b_this_page, bh != head);
829ff05123SRyusuke Konishi 
839ff05123SRyusuke Konishi 		if (fully_mapped) {
849ff05123SRyusuke Konishi 			SetPageMappedToDisk(page);
859ff05123SRyusuke Konishi 			goto mapped;
869ff05123SRyusuke Konishi 		}
879ff05123SRyusuke Konishi 	}
889ff05123SRyusuke Konishi 	unlock_page(page);
899ff05123SRyusuke Konishi 
909ff05123SRyusuke Konishi 	/*
919ff05123SRyusuke Konishi 	 * fill hole blocks
929ff05123SRyusuke Konishi 	 */
939ff05123SRyusuke Konishi 	ret = nilfs_transaction_begin(inode->i_sb, &ti, 1);
949ff05123SRyusuke Konishi 	/* never returns -ENOMEM, but may return -ENOSPC */
959ff05123SRyusuke Konishi 	if (unlikely(ret))
962c22b337SJan Kara 		goto out;
979ff05123SRyusuke Konishi 
98041bbb6dSTheodore Ts'o 	file_update_time(vma->vm_file);
995c500029SRoss Zwisler 	ret = block_page_mkwrite(vma, vmf, nilfs_get_block);
1002c22b337SJan Kara 	if (ret) {
1019ff05123SRyusuke Konishi 		nilfs_transaction_abort(inode->i_sb);
1022c22b337SJan Kara 		goto out;
1039ff05123SRyusuke Konishi 	}
10434094537SRyusuke Konishi 	nilfs_set_file_dirty(inode, 1 << (PAGE_SHIFT - inode->i_blkbits));
1059ff05123SRyusuke Konishi 	nilfs_transaction_commit(inode->i_sb);
1069ff05123SRyusuke Konishi 
1079ff05123SRyusuke Konishi  mapped:
108*ea5ddbc1SRyusuke Konishi 	/*
109*ea5ddbc1SRyusuke Konishi 	 * Since checksumming including data blocks is performed to determine
110*ea5ddbc1SRyusuke Konishi 	 * the validity of the log to be written and used for recovery, it is
111*ea5ddbc1SRyusuke Konishi 	 * necessary to wait for writeback to finish here, regardless of the
112*ea5ddbc1SRyusuke Konishi 	 * stable write requirement of the backing device.
113*ea5ddbc1SRyusuke Konishi 	 */
114*ea5ddbc1SRyusuke Konishi 	wait_on_page_writeback(page);
1152c22b337SJan Kara  out:
1162c22b337SJan Kara 	sb_end_pagefault(inode->i_sb);
1172ba39cc4SChristoph Hellwig 	return vmf_fs_error(ret);
118f183ff4fSRyusuke Konishi }
119f183ff4fSRyusuke Konishi 
120f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nilfs_file_vm_ops = {
121f183ff4fSRyusuke Konishi 	.fault		= filemap_fault,
122f1820361SKirill A. Shutemov 	.map_pages	= filemap_map_pages,
123f183ff4fSRyusuke Konishi 	.page_mkwrite	= nilfs_page_mkwrite,
124f183ff4fSRyusuke Konishi };
125f183ff4fSRyusuke Konishi 
nilfs_file_mmap(struct file * file,struct vm_area_struct * vma)126f183ff4fSRyusuke Konishi static int nilfs_file_mmap(struct file *file, struct vm_area_struct *vma)
127f183ff4fSRyusuke Konishi {
128f183ff4fSRyusuke Konishi 	file_accessed(file);
129f183ff4fSRyusuke Konishi 	vma->vm_ops = &nilfs_file_vm_ops;
130f183ff4fSRyusuke Konishi 	return 0;
131f183ff4fSRyusuke Konishi }
132f183ff4fSRyusuke Konishi 
133f183ff4fSRyusuke Konishi /*
134f183ff4fSRyusuke Konishi  * We have mostly NULL's here: the current defaults are ok for
135f183ff4fSRyusuke Konishi  * the nilfs filesystem.
136f183ff4fSRyusuke Konishi  */
137828c0950SAlexey Dobriyan const struct file_operations nilfs_file_operations = {
138f183ff4fSRyusuke Konishi 	.llseek		= generic_file_llseek,
139aad4f8bbSAl Viro 	.read_iter	= generic_file_read_iter,
1408174202bSAl Viro 	.write_iter	= generic_file_write_iter,
1417a946193SRyusuke Konishi 	.unlocked_ioctl	= nilfs_ioctl,
142f183ff4fSRyusuke Konishi #ifdef CONFIG_COMPAT
143828b1c50SRyusuke Konishi 	.compat_ioctl	= nilfs_compat_ioctl,
144f183ff4fSRyusuke Konishi #endif	/* CONFIG_COMPAT */
145f183ff4fSRyusuke Konishi 	.mmap		= nilfs_file_mmap,
146f183ff4fSRyusuke Konishi 	.open		= generic_file_open,
147f183ff4fSRyusuke Konishi 	/* .release	= nilfs_release_file, */
148f183ff4fSRyusuke Konishi 	.fsync		= nilfs_sync_file,
1492cb1e089SDavid Howells 	.splice_read	= filemap_splice_read,
150a35d8f01SJoachim Henke 	.splice_write   = iter_file_splice_write,
151f183ff4fSRyusuke Konishi };
152f183ff4fSRyusuke Konishi 
1536e1d5dccSAlexey Dobriyan const struct inode_operations nilfs_file_inode_operations = {
154f183ff4fSRyusuke Konishi 	.setattr	= nilfs_setattr,
155f183ff4fSRyusuke Konishi 	.permission     = nilfs_permission,
156622daaffSRyusuke Konishi 	.fiemap		= nilfs_fiemap,
1577c7c436eSMiklos Szeredi 	.fileattr_get	= nilfs_fileattr_get,
1587c7c436eSMiklos Szeredi 	.fileattr_set	= nilfs_fileattr_set,
159f183ff4fSRyusuke Konishi };
160f183ff4fSRyusuke Konishi 
161f183ff4fSRyusuke Konishi /* end of file */
162