xref: /linux/fs/nilfs2/file.c (revision f46bb13dc5d94471507ff9aed7a62a04d406b8b8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NILFS regular file handling primitives including fsync().
4  *
5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6  *
7  * Written by Amagai Yoshiji and Ryusuke Konishi.
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/filelock.h>
12 #include <linux/mm.h>
13 #include <linux/writeback.h>
14 #include "nilfs.h"
15 #include "segment.h"
16 
17 int nilfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
18 {
19 	/*
20 	 * Called from fsync() system call
21 	 * This is the only entry point that can catch write and synch
22 	 * timing for both data blocks and intermediate blocks.
23 	 *
24 	 * This function should be implemented when the writeback function
25 	 * will be implemented.
26 	 */
27 	struct the_nilfs *nilfs;
28 	struct inode *inode = file->f_mapping->host;
29 	int err = 0;
30 
31 	if (nilfs_inode_dirty(inode)) {
32 		if (datasync)
33 			err = nilfs_construct_dsync_segment(inode->i_sb, inode,
34 							    start, end);
35 		else
36 			err = nilfs_construct_segment(inode->i_sb);
37 	}
38 
39 	nilfs = inode->i_sb->s_fs_info;
40 	if (!err)
41 		err = nilfs_flush_device(nilfs);
42 
43 	return err;
44 }
45 
46 static vm_fault_t nilfs_page_mkwrite(struct vm_fault *vmf)
47 {
48 	struct vm_area_struct *vma = vmf->vma;
49 	struct folio *folio = page_folio(vmf->page);
50 	struct inode *inode = file_inode(vma->vm_file);
51 	struct nilfs_transaction_info ti;
52 	struct buffer_head *bh, *head;
53 	int ret = 0;
54 
55 	if (unlikely(nilfs_near_disk_full(inode->i_sb->s_fs_info)))
56 		return VM_FAULT_SIGBUS; /* -ENOSPC */
57 
58 	sb_start_pagefault(inode->i_sb);
59 	folio_lock(folio);
60 	if (folio->mapping != inode->i_mapping ||
61 	    folio_pos(folio) >= i_size_read(inode) ||
62 	    !folio_test_uptodate(folio)) {
63 		folio_unlock(folio);
64 		ret = -EFAULT;	/* make the VM retry the fault */
65 		goto out;
66 	}
67 
68 	/*
69 	 * check to see if the folio is mapped already (no holes)
70 	 */
71 	if (folio_test_mappedtodisk(folio))
72 		goto mapped;
73 
74 	head = folio_buffers(folio);
75 	if (head) {
76 		int fully_mapped = 1;
77 
78 		bh = head;
79 		do {
80 			if (!buffer_mapped(bh)) {
81 				fully_mapped = 0;
82 				break;
83 			}
84 		} while (bh = bh->b_this_page, bh != head);
85 
86 		if (fully_mapped) {
87 			folio_set_mappedtodisk(folio);
88 			goto mapped;
89 		}
90 	}
91 	folio_unlock(folio);
92 
93 	/*
94 	 * fill hole blocks
95 	 */
96 	ret = nilfs_transaction_begin(inode->i_sb, &ti, 1);
97 	/* never returns -ENOMEM, but may return -ENOSPC */
98 	if (unlikely(ret))
99 		goto out;
100 
101 	file_update_time(vma->vm_file);
102 	ret = block_page_mkwrite(vma, vmf, nilfs_get_block);
103 	if (ret) {
104 		nilfs_transaction_abort(inode->i_sb);
105 		goto out;
106 	}
107 	nilfs_set_file_dirty(inode, 1 << (PAGE_SHIFT - inode->i_blkbits));
108 	nilfs_transaction_commit(inode->i_sb);
109 
110  mapped:
111 	/*
112 	 * Since checksumming including data blocks is performed to determine
113 	 * the validity of the log to be written and used for recovery, it is
114 	 * necessary to wait for writeback to finish here, regardless of the
115 	 * stable write requirement of the backing device.
116 	 */
117 	folio_wait_writeback(folio);
118  out:
119 	sb_end_pagefault(inode->i_sb);
120 	return vmf_fs_error(ret);
121 }
122 
123 static const struct vm_operations_struct nilfs_file_vm_ops = {
124 	.fault		= filemap_fault,
125 	.map_pages	= filemap_map_pages,
126 	.page_mkwrite	= nilfs_page_mkwrite,
127 };
128 
129 static int nilfs_file_mmap_prepare(struct vm_area_desc *desc)
130 {
131 	file_accessed(desc->file);
132 	desc->vm_ops = &nilfs_file_vm_ops;
133 	return 0;
134 }
135 
136 /*
137  * We have mostly NULL's here: the current defaults are ok for
138  * the nilfs filesystem.
139  */
140 const struct file_operations nilfs_file_operations = {
141 	.llseek		= generic_file_llseek,
142 	.read_iter	= generic_file_read_iter,
143 	.write_iter	= generic_file_write_iter,
144 	.unlocked_ioctl	= nilfs_ioctl,
145 #ifdef CONFIG_COMPAT
146 	.compat_ioctl	= nilfs_compat_ioctl,
147 #endif	/* CONFIG_COMPAT */
148 	.mmap_prepare	= nilfs_file_mmap_prepare,
149 	.open		= generic_file_open,
150 	/* .release	= nilfs_release_file, */
151 	.fsync		= nilfs_sync_file,
152 	.splice_read	= filemap_splice_read,
153 	.splice_write   = iter_file_splice_write,
154 	.setlease	= generic_setlease,
155 };
156 
157 const struct inode_operations nilfs_file_inode_operations = {
158 	.setattr	= nilfs_setattr,
159 	.permission     = nilfs_permission,
160 	.fiemap		= nilfs_fiemap,
161 	.fileattr_get	= nilfs_fileattr_get,
162 	.fileattr_set	= nilfs_fileattr_set,
163 };
164 
165 /* end of file */
166