File Index
Files and directories
Sign in
FILE

file.c

File preview with syntax highlighting.

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