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