projects/linux·blob master · refs

201 lines · 4.9 KiB

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/uio.h>
14#include <linux/iomap.h>
15#include <linux/writeback.h>
16#include "nilfs.h"
17#include "segment.h"
18#include "iomap.h"
19
20int nilfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
21{
22 /*
23 * Called from fsync() system call
24 * This is the only entry point that can catch write and synch
25 * timing for both data blocks and intermediate blocks.
26 *
27 * This function should be implemented when the writeback function
28 * will be implemented.
29 */
30 struct the_nilfs *nilfs;
31 struct inode *inode = file->f_mapping->host;
32 int err = 0;
33
34 if (nilfs_inode_dirty(inode)) {
35 if (datasync)
36 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
37 start, end);
38 else
39 err = nilfs_construct_segment(inode->i_sb);
40 }
41
42 nilfs = inode->i_sb->s_fs_info;
43 if (!err)
44 err = nilfs_flush_device(nilfs);
45
46 return err;
47}
48
49static vm_fault_t nilfs_page_mkwrite(struct vm_fault *vmf)
50{
51 struct vm_area_struct *vma = vmf->vma;
52 struct folio *folio = page_folio(vmf->page);
53 struct inode *inode = file_inode(vma->vm_file);
54 struct nilfs_transaction_info ti;
55 struct buffer_head *bh, *head;
56 int ret = 0;
57
58 if (unlikely(nilfs_near_disk_full(inode->i_sb->s_fs_info)))
59 return VM_FAULT_SIGBUS; /* -ENOSPC */
60
61 sb_start_pagefault(inode->i_sb);
62 folio_lock(folio);
63 if (folio->mapping != inode->i_mapping ||
64 folio_pos(folio) >= i_size_read(inode) ||
65 !folio_test_uptodate(folio)) {
66 folio_unlock(folio);
67 ret = -EFAULT; /* make the VM retry the fault */
68 goto out;
69 }
70
71 /*
72 * check to see if the folio is mapped already (no holes)
73 */
74 if (folio_test_mappedtodisk(folio))
75 goto mapped;
76
77 head = folio_buffers(folio);
78 if (head) {
79 int fully_mapped = 1;
80
81 bh = head;
82 do {
83 if (!buffer_mapped(bh)) {
84 fully_mapped = 0;
85 break;
86 }
87 } while (bh = bh->b_this_page, bh != head);
88
89 if (fully_mapped) {
90 folio_set_mappedtodisk(folio);
91 goto mapped;
92 }
93 }
94 folio_unlock(folio);
95
96 /*
97 * fill hole blocks
98 */
99 ret = nilfs_transaction_begin(inode->i_sb, &ti, 1);
100 /* never returns -ENOMEM, but may return -ENOSPC */
101 if (unlikely(ret))
102 goto out;
103
104 file_update_time(vma->vm_file);
105 ret = block_page_mkwrite(vma, vmf, nilfs_get_block);
106 if (ret) {
107 nilfs_transaction_abort(inode->i_sb);
108 goto out;
109 }
110 nilfs_set_file_dirty(inode, 1 << (PAGE_SHIFT - inode->i_blkbits));
111 nilfs_transaction_commit(inode->i_sb);
112
113 mapped:
114 /*
115 * Since checksumming including data blocks is performed to determine
116 * the validity of the log to be written and used for recovery, it is
117 * necessary to wait for writeback to finish here, regardless of the
118 * stable write requirement of the backing device.
119 */
120 folio_wait_writeback(folio);
121 out:
122 sb_end_pagefault(inode->i_sb);
123 return vmf_fs_error(ret);
124}
125
126static const struct vm_operations_struct nilfs_file_vm_ops = {
127 .fault = filemap_fault,
128 .map_pages = filemap_map_pages,
129 .page_mkwrite = nilfs_page_mkwrite,
130};
131
132static int nilfs_file_mmap_prepare(struct vm_area_desc *desc)
133{
134 file_accessed(desc->file);
135 desc->vm_ops = &nilfs_file_vm_ops;
136 return 0;
137}
138
139static int nilfs_file_open(struct inode *inode, struct file *file)
140{
141 file->f_mode |= FMODE_CAN_ODIRECT;
142 return generic_file_open(inode, file);
143}
144
145static ssize_t nilfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
146{
147 struct inode *inode = file_inode(iocb->ki_filp);
148 ssize_t ret;
149
150 if (iocb->ki_flags & IOCB_DIRECT) {
151 inode_lock_shared(inode);
152 ret = iomap_dio_rw(iocb, to, &nilfs_iomap_ops,
153 NULL, 0, NULL, 0);
154 inode_unlock_shared(inode);
155 } else
156 ret = generic_file_read_iter(iocb, to);
157
158 return ret;
159}
160
161static ssize_t nilfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
162{
163 /*
164 * NILFS2 lacks direct I/O write support: fall back to buffered writes.
165 */
166 if (iocb->ki_flags & IOCB_DIRECT)
167 iocb->ki_flags &= ~IOCB_DIRECT;
168
169 return generic_file_write_iter(iocb, from);
170}
171
172/*
173 * We have mostly NULL's here: the current defaults are ok for
174 * the nilfs filesystem.
175 */
176const struct file_operations nilfs_file_operations = {
177 .llseek = generic_file_llseek,
178 .read_iter = nilfs_file_read_iter,
179 .write_iter = nilfs_file_write_iter,
180 .unlocked_ioctl = nilfs_ioctl,
181#ifdef CONFIG_COMPAT
182 .compat_ioctl = nilfs_compat_ioctl,
183#endif /* CONFIG_COMPAT */
184 .mmap_prepare = nilfs_file_mmap_prepare,
185 .open = nilfs_file_open,
186 /* .release = nilfs_release_file, */
187 .fsync = nilfs_sync_file,
188 .splice_read = filemap_splice_read,
189 .splice_write = iter_file_splice_write,
190 .setlease = generic_setlease,
191};
192
193const struct inode_operations nilfs_file_inode_operations = {
194 .setattr = nilfs_setattr,
195 .permission = nilfs_permission,
196 .fiemap = nilfs_fiemap,
197 .fileattr_get = nilfs_fileattr_get,
198 .fileattr_set = nilfs_fileattr_set,
199};
200
201/* end of file */