xref: /linux/fs/nfs/file.c (revision 63af040b0b4067d75e59df9fa093f3f5dbc5899e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/nfs/file.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  *
7  *  Changes Copyright (C) 1994 by Florian La Roche
8  *   - Do not copy data too often around in the kernel.
9  *   - In nfs_file_read the return value of kmalloc wasn't checked.
10  *   - Put in a better version of read look-ahead buffering. Original idea
11  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
12  *
13  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
14  *
15  *  Total rewrite of read side for new NFS buffer cache.. Linus.
16  *
17  *  nfs regular file handling functions
18  */
19 
20 #include <linux/module.h>
21 #include <linux/time.h>
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/fcntl.h>
25 #include <linux/stat.h>
26 #include <linux/nfs_fs.h>
27 #include <linux/nfs_mount.h>
28 #include <linux/mm.h>
29 #include <linux/pagemap.h>
30 #include <linux/gfp.h>
31 #include <linux/rmap.h>
32 #include <linux/swap.h>
33 #include <linux/compaction.h>
34 
35 #include <linux/uaccess.h>
36 #include <linux/filelock.h>
37 
38 #include "delegation.h"
39 #include "internal.h"
40 #include "iostat.h"
41 #include "fscache.h"
42 #include "pnfs.h"
43 
44 #include "nfstrace.h"
45 
46 #define NFSDBG_FACILITY		NFSDBG_FILE
47 
48 static const struct vm_operations_struct nfs_file_vm_ops;
49 
50 int nfs_check_flags(int flags)
51 {
52 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
53 		return -EINVAL;
54 
55 	return 0;
56 }
57 EXPORT_SYMBOL_GPL(nfs_check_flags);
58 
59 /*
60  * Open file
61  */
62 static int
63 nfs_file_open(struct inode *inode, struct file *filp)
64 {
65 	int res;
66 
67 	dprintk("NFS: open file(%pD2)\n", filp);
68 
69 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
70 	res = nfs_check_flags(filp->f_flags);
71 	if (res)
72 		return res;
73 
74 	res = nfs_open(inode, filp);
75 	if (res == 0) {
76 		filp->f_mode |= FMODE_CAN_ODIRECT;
77 		/* flag NOWAIT on read-only files only */
78 		if (!(filp->f_mode & FMODE_WRITE))
79 			filp->f_mode |= FMODE_NOWAIT;
80 	}
81 	return res;
82 }
83 
84 int
85 nfs_file_release(struct inode *inode, struct file *filp)
86 {
87 	dprintk("NFS: release(%pD2)\n", filp);
88 
89 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
90 	nfs_file_clear_open_context(filp);
91 	nfs_fscache_release_file(inode, filp);
92 	return 0;
93 }
94 EXPORT_SYMBOL_GPL(nfs_file_release);
95 
96 /**
97  * nfs_revalidate_file_size - Revalidate the file size
98  * @inode: pointer to inode struct
99  * @filp: pointer to struct file
100  *
101  * Revalidates the file length. This is basically a wrapper around
102  * nfs_revalidate_inode() that takes into account the fact that we may
103  * have cached writes (in which case we don't care about the server's
104  * idea of what the file length is), or O_DIRECT (in which case we
105  * shouldn't trust the cache).
106  */
107 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
108 {
109 	struct nfs_server *server = NFS_SERVER(inode);
110 
111 	if (filp->f_flags & O_DIRECT)
112 		goto force_reval;
113 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE))
114 		goto force_reval;
115 	return 0;
116 force_reval:
117 	return __nfs_revalidate_inode(server, inode);
118 }
119 
120 loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
121 {
122 	dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
123 			filp, offset, whence);
124 
125 	/*
126 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
127 	 * the cached file length
128 	 */
129 	if (whence != SEEK_SET && whence != SEEK_CUR) {
130 		struct inode *inode = filp->f_mapping->host;
131 
132 		int retval = nfs_revalidate_file_size(inode, filp);
133 		if (retval < 0)
134 			return (loff_t)retval;
135 	}
136 
137 	return generic_file_llseek(filp, offset, whence);
138 }
139 EXPORT_SYMBOL_GPL(nfs_file_llseek);
140 
141 /*
142  * Flush all dirty pages, and check for write errors.
143  */
144 static int
145 nfs_file_flush(struct file *file, fl_owner_t id)
146 {
147 	struct inode	*inode = file_inode(file);
148 	errseq_t since;
149 
150 	dprintk("NFS: flush(%pD2)\n", file);
151 
152 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
153 	if ((file->f_mode & FMODE_WRITE) == 0)
154 		return 0;
155 
156 	/* Flush writes to the server and return any errors */
157 	since = filemap_sample_wb_err(file->f_mapping);
158 	nfs_wb_all(inode);
159 	return filemap_check_wb_err(file->f_mapping, since);
160 }
161 
162 ssize_t
163 nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
164 {
165 	struct inode *inode = file_inode(iocb->ki_filp);
166 	ssize_t result;
167 
168 	trace_nfs_file_read(iocb, to);
169 
170 	if (iocb->ki_flags & IOCB_DIRECT)
171 		return nfs_file_direct_read(iocb, to, false);
172 
173 	/* NOWAIT only supported on direct reads */
174 	if (iocb->ki_flags & IOCB_NOWAIT)
175 		return -EAGAIN;
176 
177 	dprintk("NFS: read(%pD2, %zu@%lu)\n",
178 		iocb->ki_filp,
179 		iov_iter_count(to), (unsigned long) iocb->ki_pos);
180 
181 	result = nfs_start_io_read(inode);
182 	if (result)
183 		return result;
184 
185 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
186 	if (!result) {
187 		result = generic_file_read_iter(iocb, to);
188 		if (result > 0)
189 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
190 	}
191 	nfs_end_io_read(inode);
192 	return result;
193 }
194 EXPORT_SYMBOL_GPL(nfs_file_read);
195 
196 ssize_t
197 nfs_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe,
198 		     size_t len, unsigned int flags)
199 {
200 	struct inode *inode = file_inode(in);
201 	ssize_t result;
202 
203 	dprintk("NFS: splice_read(%pD2, %zu@%llu)\n", in, len, *ppos);
204 
205 	result = nfs_start_io_read(inode);
206 	if (result)
207 		return result;
208 
209 	result = nfs_revalidate_mapping(inode, in->f_mapping);
210 	if (!result) {
211 		result = filemap_splice_read(in, ppos, pipe, len, flags);
212 		if (result > 0)
213 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
214 	}
215 	nfs_end_io_read(inode);
216 	return result;
217 }
218 EXPORT_SYMBOL_GPL(nfs_file_splice_read);
219 
220 int
221 nfs_file_mmap_prepare(struct vm_area_desc *desc)
222 {
223 	struct file *file = desc->file;
224 	struct inode *inode = file_inode(file);
225 	int	status;
226 
227 	dprintk("NFS: mmap(%pD2)\n", file);
228 
229 	/* Note: generic_file_mmap_prepare() returns ENOSYS on nommu systems
230 	 *       so we call that before revalidating the mapping
231 	 */
232 	status = generic_file_mmap_prepare(desc);
233 	if (!status) {
234 		desc->vm_ops = &nfs_file_vm_ops;
235 		status = nfs_revalidate_mapping(inode, file->f_mapping);
236 	}
237 	return status;
238 }
239 EXPORT_SYMBOL_GPL(nfs_file_mmap_prepare);
240 
241 /*
242  * Flush any dirty pages for this process, and check for write errors.
243  * The return status from this call provides a reliable indication of
244  * whether any write errors occurred for this process.
245  */
246 static int
247 nfs_file_fsync_commit(struct file *file, int datasync)
248 {
249 	struct inode *inode = file_inode(file);
250 	int ret, ret2;
251 
252 	dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
253 
254 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
255 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
256 	ret2 = file_check_and_advance_wb_err(file);
257 	if (ret2 < 0)
258 		return ret2;
259 	return ret;
260 }
261 
262 int
263 nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
264 {
265 	struct inode *inode = file_inode(file);
266 	struct nfs_inode *nfsi = NFS_I(inode);
267 	long save_nredirtied = atomic_long_read(&nfsi->redirtied_pages);
268 	long nredirtied;
269 	int ret;
270 
271 	trace_nfs_fsync_enter(inode);
272 
273 	for (;;) {
274 		ret = file_write_and_wait_range(file, start, end);
275 		if (ret != 0)
276 			break;
277 		ret = nfs_file_fsync_commit(file, datasync);
278 		if (ret != 0)
279 			break;
280 		ret = pnfs_sync_inode(inode, !!datasync);
281 		if (ret != 0)
282 			break;
283 		nredirtied = atomic_long_read(&nfsi->redirtied_pages);
284 		if (nredirtied == save_nredirtied)
285 			break;
286 		save_nredirtied = nredirtied;
287 	}
288 
289 	trace_nfs_fsync_exit(inode, ret);
290 	return ret;
291 }
292 EXPORT_SYMBOL_GPL(nfs_file_fsync);
293 
294 void nfs_truncate_last_folio(struct address_space *mapping, loff_t from,
295 			     loff_t to)
296 {
297 	struct folio *folio;
298 
299 	if (from >= to)
300 		return;
301 
302 	folio = filemap_lock_folio(mapping, from >> PAGE_SHIFT);
303 	if (IS_ERR(folio))
304 		return;
305 
306 	if (folio_mkclean(folio))
307 		folio_mark_dirty(folio);
308 
309 	if (folio_test_uptodate(folio)) {
310 		loff_t fpos = folio_pos(folio);
311 		size_t offset = from - fpos;
312 		size_t end = folio_size(folio);
313 
314 		if (to - fpos < end)
315 			end = to - fpos;
316 		folio_zero_segment(folio, offset, end);
317 		trace_nfs_size_truncate_folio(mapping->host, to);
318 	}
319 
320 	folio_unlock(folio);
321 	folio_put(folio);
322 }
323 EXPORT_SYMBOL_GPL(nfs_truncate_last_folio);
324 
325 /*
326  * Decide whether a read/modify/write cycle may be more efficient
327  * then a modify/write/read cycle when writing to a page in the
328  * page cache.
329  *
330  * Some pNFS layout drivers can only read/write at a certain block
331  * granularity like all block devices and therefore we must perform
332  * read/modify/write whenever a page hasn't read yet and the data
333  * to be written there is not aligned to a block boundary and/or
334  * smaller than the block size.
335  *
336  * The modify/write/read cycle may occur if a page is read before
337  * being completely filled by the writer.  In this situation, the
338  * page must be completely written to stable storage on the server
339  * before it can be refilled by reading in the page from the server.
340  * This can lead to expensive, small, FILE_SYNC mode writes being
341  * done.
342  *
343  * It may be more efficient to read the page first if the file is
344  * open for reading in addition to writing, the page is not marked
345  * as Uptodate, it is not dirty or waiting to be committed,
346  * indicating that it was previously allocated and then modified,
347  * that there were valid bytes of data in that range of the file,
348  * and that the new data won't completely replace the old data in
349  * that range of the file.
350  */
351 static bool nfs_folio_is_full_write(struct folio *folio, loff_t pos,
352 				    unsigned int len)
353 {
354 	unsigned int pglen = nfs_folio_length(folio);
355 	unsigned int offset = offset_in_folio(folio, pos);
356 	unsigned int end = offset + len;
357 
358 	return !pglen || (end >= pglen && !offset);
359 }
360 
361 static bool nfs_want_read_modify_write(struct file *file, struct folio *folio,
362 				       loff_t pos, unsigned int len)
363 {
364 	/*
365 	 * Up-to-date pages, those with ongoing or full-page write
366 	 * don't need read/modify/write
367 	 */
368 	if (folio_test_uptodate(folio) || folio_test_private(folio) ||
369 	    nfs_folio_is_full_write(folio, pos, len))
370 		return false;
371 
372 	if (pnfs_ld_read_whole_page(file_inode(file)))
373 		return true;
374 	if (folio_test_dropbehind(folio))
375 		return false;
376 	/* Open for reading too? */
377 	if (file->f_mode & FMODE_READ)
378 		return true;
379 	return false;
380 }
381 
382 /*
383  * This does the "real" work of the write. We must allocate and lock the
384  * page to be sent back to the generic routine, which then copies the
385  * data from user space.
386  *
387  * If the writer ends up delaying the write, the writer needs to
388  * increment the page use counts until he is done with the page.
389  */
390 static int nfs_write_begin(const struct kiocb *iocb,
391 			   struct address_space *mapping,
392 			   loff_t pos, unsigned len, struct folio **foliop,
393 			   void **fsdata)
394 {
395 	struct folio *folio;
396 	struct file *file = iocb->ki_filp;
397 	int once_thru = 0;
398 	int ret;
399 
400 	trace_nfs_write_begin(file_inode(file), pos, len);
401 
402 	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%llu), %u@%lld)\n",
403 		file, mapping->host->i_ino, len, (long long) pos);
404 	nfs_truncate_last_folio(mapping, i_size_read(mapping->host), pos);
405 
406 start:
407 	folio = write_begin_get_folio(iocb, mapping, pos >> PAGE_SHIFT, len);
408 	if (IS_ERR(folio)) {
409 		ret = PTR_ERR(folio);
410 		goto out;
411 	}
412 	*foliop = folio;
413 
414 	ret = nfs_flush_incompatible(file, folio);
415 	if (ret) {
416 		folio_unlock(folio);
417 		folio_put(folio);
418 	} else if (!once_thru &&
419 		   nfs_want_read_modify_write(file, folio, pos, len)) {
420 		once_thru = 1;
421 		folio_clear_dropbehind(folio);
422 		ret = nfs_read_folio(file, folio);
423 		folio_put(folio);
424 		if (!ret)
425 			goto start;
426 	}
427 out:
428 	trace_nfs_write_begin_done(file_inode(file), pos, len, ret);
429 	return ret;
430 }
431 
432 static int nfs_write_end(const struct kiocb *iocb,
433 			 struct address_space *mapping,
434 			 loff_t pos, unsigned len, unsigned copied,
435 			 struct folio *folio, void *fsdata)
436 {
437 	struct file *file = iocb->ki_filp;
438 	struct nfs_open_context *ctx = nfs_file_open_context(file);
439 	unsigned offset = offset_in_folio(folio, pos);
440 	int status;
441 
442 	trace_nfs_write_end(file_inode(file), pos, len);
443 	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%llu), %u@%lld)\n",
444 		file, mapping->host->i_ino, len, (long long) pos);
445 
446 	/*
447 	 * Zero any uninitialised parts of the page, and then mark the page
448 	 * as up to date if it turns out that we're extending the file.
449 	 */
450 	if (!folio_test_uptodate(folio)) {
451 		size_t fsize = folio_size(folio);
452 		unsigned pglen = nfs_folio_length(folio);
453 		unsigned end = offset + copied;
454 
455 		if (pglen == 0) {
456 			folio_zero_segments(folio, 0, offset, end, fsize);
457 			folio_mark_uptodate(folio);
458 		} else if (end >= pglen) {
459 			folio_zero_segment(folio, end, fsize);
460 			if (offset == 0)
461 				folio_mark_uptodate(folio);
462 		} else
463 			folio_zero_segment(folio, pglen, fsize);
464 	}
465 
466 	status = nfs_update_folio(file, folio, offset, copied);
467 
468 	folio_unlock(folio);
469 	folio_put(folio);
470 
471 	if (status < 0) {
472 		trace_nfs_write_end_done(file_inode(file), pos, len, status);
473 		return status;
474 	}
475 	NFS_I(mapping->host)->write_io += copied;
476 
477 	if (nfs_ctx_key_to_expire(ctx, mapping->host))
478 		nfs_wb_all(mapping->host);
479 
480 	trace_nfs_write_end_done(file_inode(file), pos, len, copied);
481 	return copied;
482 }
483 
484 /*
485  * Partially or wholly invalidate a page
486  * - Release the private state associated with a page if undergoing complete
487  *   page invalidation
488  * - Called if either PG_private or PG_fscache is set on the page
489  * - Caller holds page lock
490  */
491 static void nfs_invalidate_folio(struct folio *folio, size_t offset,
492 				size_t length)
493 {
494 	struct inode *inode = folio->mapping->host;
495 	dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n",
496 		 folio->index, offset, length);
497 
498 	/* Cancel any unstarted writes on this page */
499 	if (offset != 0 || length < folio_size(folio))
500 		nfs_wb_folio(inode, folio);
501 	else
502 		nfs_wb_folio_cancel(inode, folio);
503 	folio_wait_private_2(folio); /* [DEPRECATED] */
504 	trace_nfs_invalidate_folio(inode, folio_pos(folio) + offset, length);
505 }
506 
507 /*
508  * Attempt to release the private state associated with a folio
509  * - Called if either private or fscache flags are set on the folio
510  * - Caller holds folio lock
511  * - Return true (may release folio) or false (may not)
512  */
513 static bool nfs_release_folio(struct folio *folio, gfp_t gfp)
514 {
515 	dfprintk(PAGECACHE, "NFS: release_folio(%p)\n", folio);
516 
517 	/* If the private flag is set, then the folio is not freeable */
518 	if (folio_test_private(folio)) {
519 		if ((current_gfp_context(gfp) & GFP_KERNEL) != GFP_KERNEL ||
520 		    current_is_kswapd() || current_is_kcompactd())
521 			return false;
522 		if (nfs_wb_folio_reclaim(folio->mapping->host, folio) < 0 ||
523 		    folio_test_private(folio))
524 			return false;
525 	}
526 	return nfs_fscache_release_folio(folio, gfp);
527 }
528 
529 static void nfs_check_dirty_writeback(struct folio *folio,
530 				bool *dirty, bool *writeback)
531 {
532 	struct nfs_inode *nfsi;
533 	struct address_space *mapping = folio->mapping;
534 
535 	/*
536 	 * Check if an unstable folio is currently being committed and
537 	 * if so, have the VM treat it as if the folio is under writeback
538 	 * so it will not block due to folios that will shortly be freeable.
539 	 */
540 	nfsi = NFS_I(mapping->host);
541 	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
542 		*writeback = true;
543 		return;
544 	}
545 
546 	/*
547 	 * If the private flag is set, then the folio is not freeable
548 	 * and as the inode is not being committed, it's not going to
549 	 * be cleaned in the near future so treat it as dirty
550 	 */
551 	if (folio_test_private(folio))
552 		*dirty = true;
553 }
554 
555 /*
556  * Attempt to clear the private state associated with a page when an error
557  * occurs that requires the cached contents of an inode to be written back or
558  * destroyed
559  * - Called if either PG_private or fscache is set on the page
560  * - Caller holds page lock
561  * - Return 0 if successful, -error otherwise
562  */
563 static int nfs_launder_folio(struct folio *folio)
564 {
565 	struct inode *inode = folio->mapping->host;
566 	int ret;
567 
568 	dfprintk(PAGECACHE, "NFS: launder_folio(%llu, %llu)\n",
569 		inode->i_ino, folio_pos(folio));
570 
571 	folio_wait_private_2(folio); /* [DEPRECATED] */
572 	ret = nfs_wb_folio(inode, folio);
573 	trace_nfs_launder_folio_done(inode, folio_pos(folio),
574 			folio_size(folio), ret);
575 	return ret;
576 }
577 
578 static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
579 						sector_t *span)
580 {
581 	unsigned long blocks;
582 	long long isize;
583 	int ret;
584 	struct inode *inode = file_inode(file);
585 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
586 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
587 
588 	spin_lock(&inode->i_lock);
589 	blocks = inode->i_blocks;
590 	isize = inode->i_size;
591 	spin_unlock(&inode->i_lock);
592 	if (blocks*512 < isize) {
593 		pr_warn("swap activate: swapfile has holes\n");
594 		return -EINVAL;
595 	}
596 
597 	ret = rpc_clnt_swap_activate(clnt);
598 	if (ret)
599 		return ret;
600 	ret = add_swap_extent(sis, 0, sis->max, 0);
601 	if (ret < 0) {
602 		rpc_clnt_swap_deactivate(clnt);
603 		return ret;
604 	}
605 
606 	*span = sis->pages;
607 
608 	if (cl->rpc_ops->enable_swap)
609 		cl->rpc_ops->enable_swap(inode);
610 
611 	sis->flags |= SWP_FS_OPS;
612 	return ret;
613 }
614 
615 static void nfs_swap_deactivate(struct file *file)
616 {
617 	struct inode *inode = file_inode(file);
618 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
619 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
620 
621 	rpc_clnt_swap_deactivate(clnt);
622 	if (cl->rpc_ops->disable_swap)
623 		cl->rpc_ops->disable_swap(file_inode(file));
624 }
625 
626 const struct address_space_operations nfs_file_aops = {
627 	.read_folio = nfs_read_folio,
628 	.readahead = nfs_readahead,
629 	.dirty_folio = filemap_dirty_folio,
630 	.writepages = nfs_writepages,
631 	.write_begin = nfs_write_begin,
632 	.write_end = nfs_write_end,
633 	.invalidate_folio = nfs_invalidate_folio,
634 	.release_folio = nfs_release_folio,
635 	.migrate_folio = nfs_migrate_folio,
636 	.launder_folio = nfs_launder_folio,
637 	.is_dirty_writeback = nfs_check_dirty_writeback,
638 	.error_remove_folio = generic_error_remove_folio,
639 	.swap_activate = nfs_swap_activate,
640 	.swap_deactivate = nfs_swap_deactivate,
641 	.swap_rw = nfs_swap_rw,
642 };
643 
644 /*
645  * Notification that a PTE pointing to an NFS page is about to be made
646  * writable, implying that someone is about to modify the page through a
647  * shared-writable mapping
648  */
649 static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
650 {
651 	struct file *filp = vmf->vma->vm_file;
652 	struct inode *inode = file_inode(filp);
653 	unsigned pagelen;
654 	vm_fault_t ret = VM_FAULT_NOPAGE;
655 	struct address_space *mapping;
656 	struct folio *folio = page_folio(vmf->page);
657 
658 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%llu), offset %lld)\n",
659 		 filp, filp->f_mapping->host->i_ino,
660 		 (long long)folio_pos(folio));
661 
662 	sb_start_pagefault(inode->i_sb);
663 
664 	/* make sure the cache has finished storing the page */
665 	if (folio_test_private_2(folio) && /* [DEPRECATED] */
666 	    folio_wait_private_2_killable(folio) < 0) {
667 		ret = VM_FAULT_RETRY;
668 		goto out;
669 	}
670 
671 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
672 			   nfs_wait_bit_killable,
673 			   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
674 
675 	folio_lock(folio);
676 	mapping = folio->mapping;
677 	if (mapping != inode->i_mapping)
678 		goto out_unlock;
679 
680 	folio_wait_writeback(folio);
681 
682 	pagelen = nfs_folio_length(folio);
683 	if (pagelen == 0)
684 		goto out_unlock;
685 
686 	ret = VM_FAULT_LOCKED;
687 	if (nfs_flush_incompatible(filp, folio) == 0 &&
688 	    nfs_update_folio(filp, folio, 0, pagelen) == 0)
689 		goto out;
690 
691 	ret = VM_FAULT_SIGBUS;
692 out_unlock:
693 	folio_unlock(folio);
694 out:
695 	sb_end_pagefault(inode->i_sb);
696 	return ret;
697 }
698 
699 static const struct vm_operations_struct nfs_file_vm_ops = {
700 	.fault = filemap_fault,
701 	.map_pages = filemap_map_pages,
702 	.page_mkwrite = nfs_vm_page_mkwrite,
703 };
704 
705 ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
706 {
707 	struct file *file = iocb->ki_filp;
708 	struct inode *inode = file_inode(file);
709 	unsigned int mntflags = NFS_SERVER(inode)->flags;
710 	ssize_t result, written;
711 	errseq_t since;
712 	int error;
713 
714 	trace_nfs_file_write(iocb, from);
715 
716 	/*
717 	 * FMODE_NOWAIT is not set for writable files
718 	 */
719 	if (WARN_ON_ONCE(iocb->ki_flags & IOCB_NOWAIT))
720 		return -EAGAIN;
721 
722 	result = nfs_key_timeout_notify(file, inode);
723 	if (result)
724 		return result;
725 
726 	if (iocb->ki_flags & IOCB_DIRECT)
727 		return nfs_file_direct_write(iocb, from, false);
728 
729 	dprintk("NFS: write(%pD2, %zu@%Ld)\n",
730 		file, iov_iter_count(from), (long long) iocb->ki_pos);
731 
732 	if (IS_SWAPFILE(inode))
733 		goto out_swapfile;
734 	/*
735 	 * O_APPEND implies that we must revalidate the file length.
736 	 */
737 	if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) {
738 		result = nfs_revalidate_file_size(inode, file);
739 		if (result)
740 			return result;
741 	}
742 
743 	nfs_clear_invalid_mapping(file->f_mapping);
744 
745 	since = filemap_sample_wb_err(file->f_mapping);
746 	error = nfs_start_io_write(inode);
747 	if (error)
748 		return error;
749 	result = generic_write_checks(iocb, from);
750 	if (result > 0)
751 		result = generic_perform_write(iocb, from);
752 	nfs_end_io_write(inode);
753 	if (result <= 0)
754 		goto out;
755 
756 	written = result;
757 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
758 
759 	if (mntflags & NFS_MOUNT_WRITE_EAGER) {
760 		result = filemap_fdatawrite_range(file->f_mapping,
761 						  iocb->ki_pos - written,
762 						  iocb->ki_pos - 1);
763 		if (result < 0)
764 			goto out;
765 	}
766 	if (mntflags & NFS_MOUNT_WRITE_WAIT) {
767 		filemap_fdatawait_range(file->f_mapping,
768 					iocb->ki_pos - written,
769 					iocb->ki_pos - 1);
770 	}
771 	result = generic_write_sync(iocb, written);
772 	if (result < 0)
773 		return result;
774 
775 out:
776 	/* Return error values */
777 	error = filemap_check_wb_err(file->f_mapping, since);
778 	switch (error) {
779 	default:
780 		break;
781 	case -EDQUOT:
782 	case -EFBIG:
783 	case -ENOSPC:
784 		nfs_wb_all(inode);
785 		error = file_check_and_advance_wb_err(file);
786 		if (error < 0)
787 			result = error;
788 	}
789 	return result;
790 
791 out_swapfile:
792 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
793 	return -ETXTBSY;
794 }
795 EXPORT_SYMBOL_GPL(nfs_file_write);
796 
797 static int
798 do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
799 {
800 	struct inode *inode = filp->f_mapping->host;
801 	int status = 0;
802 	unsigned int saved_type = fl->c.flc_type;
803 
804 	/* Try local locking first */
805 	posix_test_lock(filp, fl);
806 	if (fl->c.flc_type != F_UNLCK) {
807 		/* found a conflict */
808 		goto out;
809 	}
810 	fl->c.flc_type = saved_type;
811 
812 	if (nfs_have_read_or_write_delegation(inode))
813 		goto out_noconflict;
814 
815 	if (is_local)
816 		goto out_noconflict;
817 
818 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
819 out:
820 	return status;
821 out_noconflict:
822 	fl->c.flc_type = F_UNLCK;
823 	goto out;
824 }
825 
826 static int
827 do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
828 {
829 	struct inode *inode = filp->f_mapping->host;
830 	struct nfs_lock_context *l_ctx;
831 	int status;
832 
833 	/*
834 	 * Flush all pending writes before doing anything
835 	 * with locks..
836 	 */
837 	nfs_wb_all(inode);
838 
839 	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
840 	if (!IS_ERR(l_ctx)) {
841 		status = nfs_iocounter_wait(l_ctx);
842 		nfs_put_lock_context(l_ctx);
843 		/*  NOTE: special case
844 		 * 	If we're signalled while cleaning up locks on process exit, we
845 		 * 	still need to complete the unlock.
846 		 */
847 		if (status < 0 && !(fl->c.flc_flags & FL_CLOSE))
848 			return status;
849 	}
850 
851 	/*
852 	 * Use local locking if mounted with "-onolock" or with appropriate
853 	 * "-olocal_lock="
854 	 */
855 	if (!is_local)
856 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
857 	else
858 		status = locks_lock_file_wait(filp, fl);
859 	return status;
860 }
861 
862 static int
863 do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
864 {
865 	struct inode *inode = filp->f_mapping->host;
866 	int status;
867 
868 	/*
869 	 * Flush all pending writes before doing anything
870 	 * with locks..
871 	 */
872 	status = nfs_sync_mapping(filp->f_mapping);
873 	if (status != 0)
874 		goto out;
875 
876 	/*
877 	 * Use local locking if mounted with "-onolock" or with appropriate
878 	 * "-olocal_lock="
879 	 */
880 	if (!is_local)
881 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
882 	else
883 		status = locks_lock_file_wait(filp, fl);
884 	if (status < 0)
885 		goto out;
886 
887 	/*
888 	 * Invalidate cache to prevent missing any changes.  If
889 	 * the file is mapped, clear the page cache as well so
890 	 * those mappings will be loaded.
891 	 *
892 	 * This makes locking act as a cache coherency point.
893 	 */
894 	nfs_sync_mapping(filp->f_mapping);
895 	if (!nfs_have_read_or_write_delegation(inode)) {
896 		nfs_zap_caches(inode);
897 		if (mapping_mapped(filp->f_mapping))
898 			nfs_revalidate_mapping(inode, filp->f_mapping);
899 	}
900 out:
901 	return status;
902 }
903 
904 /*
905  * Lock a (portion of) a file
906  */
907 int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
908 {
909 	struct inode *inode = filp->f_mapping->host;
910 	int ret = -ENOLCK;
911 	int is_local = 0;
912 
913 	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
914 			filp, fl->c.flc_type, fl->c.flc_flags,
915 			(long long)fl->fl_start, (long long)fl->fl_end);
916 
917 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
918 
919 	if (fl->c.flc_flags & FL_RECLAIM)
920 		return -ENOGRACE;
921 
922 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
923 		is_local = 1;
924 
925 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
926 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
927 		if (ret < 0)
928 			goto out_err;
929 	}
930 
931 	if (IS_GETLK(cmd))
932 		ret = do_getlk(filp, cmd, fl, is_local);
933 	else if (lock_is_unlock(fl))
934 		ret = do_unlk(filp, cmd, fl, is_local);
935 	else
936 		ret = do_setlk(filp, cmd, fl, is_local);
937 out_err:
938 	return ret;
939 }
940 EXPORT_SYMBOL_GPL(nfs_lock);
941 
942 /*
943  * Lock a (portion of) a file
944  */
945 int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
946 {
947 	struct inode *inode = filp->f_mapping->host;
948 	int is_local = 0;
949 
950 	dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
951 			filp, fl->c.flc_type, fl->c.flc_flags);
952 
953 	if (!(fl->c.flc_flags & FL_FLOCK))
954 		return -ENOLCK;
955 
956 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
957 		is_local = 1;
958 
959 	/* We're simulating flock() locks using posix locks on the server */
960 	if (lock_is_unlock(fl))
961 		return do_unlk(filp, cmd, fl, is_local);
962 	return do_setlk(filp, cmd, fl, is_local);
963 }
964 EXPORT_SYMBOL_GPL(nfs_flock);
965 
966 const struct file_operations nfs_file_operations = {
967 	.llseek		= nfs_file_llseek,
968 	.read_iter	= nfs_file_read,
969 	.write_iter	= nfs_file_write,
970 	.mmap_prepare	= nfs_file_mmap_prepare,
971 	.open		= nfs_file_open,
972 	.flush		= nfs_file_flush,
973 	.release	= nfs_file_release,
974 	.fsync		= nfs_file_fsync,
975 	.lock		= nfs_lock,
976 	.flock		= nfs_flock,
977 	.splice_read	= nfs_file_splice_read,
978 	.splice_write	= iter_file_splice_write,
979 	.check_flags	= nfs_check_flags,
980 	.fop_flags	= FOP_DONTCACHE,
981 };
982 EXPORT_SYMBOL_GPL(nfs_file_operations);
983