Blame view

fs/nfs/file.c 21.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   *  linux/fs/nfs/file.c
   *
   *  Copyright (C) 1992  Rick Sladkey
   *
   *  Changes Copyright (C) 1994 by Florian La Roche
   *   - Do not copy data too often around in the kernel.
   *   - In nfs_file_read the return value of kmalloc wasn't checked.
   *   - Put in a better version of read look-ahead buffering. Original idea
   *     and implementation by Wai S Kok elekokws@ee.nus.sg.
   *
   *  Expire cache on write to a file by Wai S Kok (Oct 1994).
   *
   *  Total rewrite of read side for new NFS buffer cache.. Linus.
   *
   *  nfs regular file handling functions
   */
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
18
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
24
25
26
  #include <linux/time.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/fcntl.h>
  #include <linux/stat.h>
  #include <linux/nfs_fs.h>
  #include <linux/nfs_mount.h>
  #include <linux/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  #include <linux/pagemap.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
28
  #include <linux/gfp.h>
b608b283a   Trond Myklebust   NFS: kswapd must ...
29
  #include <linux/swap.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
  
  #include <asm/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
  
  #include "delegation.h"
94387fb1a   Trond Myklebust   NFS: Add the help...
34
  #include "internal.h"
91d5b4702   Chuck Lever   NFS: add I/O perf...
35
  #include "iostat.h"
545db45f0   David Howells   NFS: FS-Cache pag...
36
  #include "fscache.h"
612aa983a   Christoph Hellwig   pnfs: add flag to...
37
  #include "pnfs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38

f4ce1299b   Trond Myklebust   NFS: Add event tr...
39
  #include "nfstrace.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
  #define NFSDBG_FACILITY		NFSDBG_FILE
f0f37e2f7   Alexey Dobriyan   const: mark struc...
41
  static const struct vm_operations_struct nfs_file_vm_ops;
94387fb1a   Trond Myklebust   NFS: Add the help...
42

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
  /* Hack for future NFS swap support */
  #ifndef IS_SWAPFILE
  # define IS_SWAPFILE(inode)	(0)
  #endif
ce4ef7c0a   Bryan Schumaker   NFS: Split out NF...
47
  int nfs_check_flags(int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
53
  {
  	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
  		return -EINVAL;
  
  	return 0;
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
54
  EXPORT_SYMBOL_GPL(nfs_check_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
59
60
61
  
  /*
   * Open file
   */
  static int
  nfs_file_open(struct inode *inode, struct file *filp)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  	int res;
6de1472f1   Al Viro   nfs: use %p[dD] i...
63
64
  	dprintk("NFS: open file(%pD2)
  ", filp);
cc0dd2d10   Chuck Lever   NFS: Make nfs_ope...
65

c2459dc46   Chuck Lever   NFS: Proper accou...
66
  	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
69
  	res = nfs_check_flags(filp->f_flags);
  	if (res)
  		return res;
46cb650c2   Trond Myklebust   NFS: Remove the r...
70
  	res = nfs_open(inode, filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
  	return res;
  }
ce4ef7c0a   Bryan Schumaker   NFS: Split out NF...
73
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
  nfs_file_release(struct inode *inode, struct file *filp)
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
76
77
  	dprintk("NFS: release(%pD2)
  ", filp);
6da24bc9c   Chuck Lever   NFS: Use NFSDBG_F...
78

91d5b4702   Chuck Lever   NFS: add I/O perf...
79
  	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
aff8d8dc4   Anna Schumaker   NFS: Remove nfs_r...
80
81
  	nfs_file_clear_open_context(filp);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
83
  EXPORT_SYMBOL_GPL(nfs_file_release);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84

980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  /**
   * nfs_revalidate_size - Revalidate the file size
   * @inode - pointer to inode struct
   * @file - pointer to struct file
   *
   * Revalidates the file length. This is basically a wrapper around
   * nfs_revalidate_inode() that takes into account the fact that we may
   * have cached writes (in which case we don't care about the server's
   * idea of what the file length is), or O_DIRECT (in which case we
   * shouldn't trust the cache).
   */
  static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  {
  	struct nfs_server *server = NFS_SERVER(inode);
  	struct nfs_inode *nfsi = NFS_I(inode);
d7cf8dd01   Trond Myklebust   NFSv4: Allow attr...
100
101
  	if (nfs_have_delegated_attributes(inode))
  		goto out_noreval;
980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
102
103
  	if (filp->f_flags & O_DIRECT)
  		goto force_reval;
d7cf8dd01   Trond Myklebust   NFSv4: Allow attr...
104
105
106
107
108
109
  	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  		goto force_reval;
  	if (nfs_attribute_timeout(inode))
  		goto force_reval;
  out_noreval:
  	return 0;
980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
110
111
112
  force_reval:
  	return __nfs_revalidate_inode(server, inode);
  }
965c8e59c   Andrew Morton   lseek: the "whenc...
113
  loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
114
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
115
116
117
  	dprintk("NFS: llseek file(%pD2, %lld, %d)
  ",
  			filp, offset, whence);
b84e06c58   Chuck Lever   NFS: Make nfs_lls...
118

06222e491   Josef Bacik   fs: handle SEEK_H...
119
  	/*
965c8e59c   Andrew Morton   lseek: the "whenc...
120
  	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
06222e491   Josef Bacik   fs: handle SEEK_H...
121
122
  	 * the cached file length
  	 */
965c8e59c   Andrew Morton   lseek: the "whenc...
123
  	if (whence != SEEK_SET && whence != SEEK_CUR) {
980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
124
  		struct inode *inode = filp->f_mapping->host;
d5e66348b   Trond Myklebust   NFS: Fix nfs_file...
125

980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
126
127
128
  		int retval = nfs_revalidate_file_size(inode, filp);
  		if (retval < 0)
  			return (loff_t)retval;
79835a710   Andi Kleen   nfs: drop unneces...
129
  	}
d5e66348b   Trond Myklebust   NFS: Fix nfs_file...
130

965c8e59c   Andrew Morton   lseek: the "whenc...
131
  	return generic_file_llseek(filp, offset, whence);
980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
132
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
133
  EXPORT_SYMBOL_GPL(nfs_file_llseek);
980802e31   Trond Myklebust   [PATCH] NFS: Ensu...
134

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
136
  /*
   * Flush all dirty pages, and check for write errors.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
   */
5445b1fbd   Trond Myklebust   NFSv4: Respect th...
138
  static int
75e1fcc0b   Miklos Szeredi   [PATCH] vfs: add ...
139
  nfs_file_flush(struct file *file, fl_owner_t id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
141
  	struct inode	*inode = file_inode(file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142

6de1472f1   Al Viro   nfs: use %p[dD] i...
143
144
  	dprintk("NFS: flush(%pD2)
  ", file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145

c2459dc46   Chuck Lever   NFS: Proper accou...
146
  	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  	if ((file->f_mode & FMODE_WRITE) == 0)
  		return 0;
7b159fc18   Trond Myklebust   NFS: Fall back to...
149

7fe5c398f   Trond Myklebust   NFS: Optimise NFS...
150
  	/* Flush writes to the server and return any errors */
af7fa1650   Trond Myklebust   NFS: Fix up the f...
151
  	return vfs_fsync(file, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  }
ce4ef7c0a   Bryan Schumaker   NFS: Split out NF...
153
  ssize_t
3aa2d199f   Al Viro   nfs: switch to ->...
154
  nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
156
  	struct inode *inode = file_inode(iocb->ki_filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  	ssize_t result;
2ba48ce51   Al Viro   mirror O_APPEND a...
158
  	if (iocb->ki_flags & IOCB_DIRECT)
c8b8e32d7   Christoph Hellwig   direct-io: elimin...
159
  		return nfs_file_direct_read(iocb, to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160

619d30b4b   Al Viro   convert the guts ...
161
162
  	dprintk("NFS: read(%pD2, %zu@%lu)
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
163
  		iocb->ki_filp,
3aa2d199f   Al Viro   nfs: switch to ->...
164
  		iov_iter_count(to), (unsigned long) iocb->ki_pos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165

a5864c999   Trond Myklebust   NFS: Do not seria...
166
167
  	nfs_start_io_read(inode);
  	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
4184dcf2d   Chuck Lever   NFS: Fix byte acc...
168
  	if (!result) {
3aa2d199f   Al Viro   nfs: switch to ->...
169
  		result = generic_file_read_iter(iocb, to);
4184dcf2d   Chuck Lever   NFS: Fix byte acc...
170
171
172
  		if (result > 0)
  			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  	}
a5864c999   Trond Myklebust   NFS: Do not seria...
173
  	nfs_end_io_read(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
  	return result;
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
176
  EXPORT_SYMBOL_GPL(nfs_file_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177

ce4ef7c0a   Bryan Schumaker   NFS: Split out NF...
178
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
  nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
181
  	struct inode *inode = file_inode(file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  	int	status;
6de1472f1   Al Viro   nfs: use %p[dD] i...
183
184
  	dprintk("NFS: mmap(%pD2)
  ", file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185

e1ebfd33b   Trond Myklebust   NFS: Kill the "de...
186
187
188
189
  	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
  	 *       so we call that before revalidating the mapping
  	 */
  	status = generic_file_mmap(file, vma);
94387fb1a   Trond Myklebust   NFS: Add the help...
190
191
  	if (!status) {
  		vma->vm_ops = &nfs_file_vm_ops;
e1ebfd33b   Trond Myklebust   NFS: Kill the "de...
192
  		status = nfs_revalidate_mapping(inode, file->f_mapping);
94387fb1a   Trond Myklebust   NFS: Add the help...
193
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
  	return status;
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
196
  EXPORT_SYMBOL_GPL(nfs_file_mmap);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
198
199
200
201
  
  /*
   * Flush any dirty pages for this process, and check for write errors.
   * The return status from this call provides a reliable indication of
   * whether any write errors occurred for this process.
af7fa1650   Trond Myklebust   NFS: Fix up the f...
202
203
204
205
206
   *
   * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
   * disk, but it retrieves and clears ctx->error after synching, despite
   * the two being set at the same time in nfs_context_set_write_error().
   * This is because the former is used to notify the _next_ call to
25985edce   Lucas De Marchi   Fix common misspe...
207
   * nfs_file_write() that a write error occurred, and hence cause it to
af7fa1650   Trond Myklebust   NFS: Fix up the f...
208
   * fall back to doing a synchronous write.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
   */
4ff79bc70   Christoph Hellwig   nfs: remove nfs4_...
210
  static int
a5c58892b   Bryan Schumaker   NFS: Create a v4-...
211
  nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
  {
cd3758e37   Trond Myklebust   NFS: Replace file...
213
  	struct nfs_open_context *ctx = nfs_file_open_context(file);
6de1472f1   Al Viro   nfs: use %p[dD] i...
214
  	struct inode *inode = file_inode(file);
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
215
  	int have_error, do_resend, status;
af7fa1650   Trond Myklebust   NFS: Fix up the f...
216
  	int ret = 0;
6de1472f1   Al Viro   nfs: use %p[dD] i...
217
218
  	dprintk("NFS: fsync file(%pD2) datasync %d
  ", file, datasync);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219

91d5b4702   Chuck Lever   NFS: add I/O perf...
220
  	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
221
  	do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
af7fa1650   Trond Myklebust   NFS: Fix up the f...
222
223
224
  	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  	status = nfs_commit_inode(inode, FLUSH_SYNC);
  	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
225
  	if (have_error) {
af7fa1650   Trond Myklebust   NFS: Fix up the f...
226
  		ret = xchg(&ctx->error, 0);
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
227
228
229
230
  		if (ret)
  			goto out;
  	}
  	if (status < 0) {
af7fa1650   Trond Myklebust   NFS: Fix up the f...
231
  		ret = status;
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
232
233
234
235
236
237
  		goto out;
  	}
  	do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  	if (do_resend)
  		ret = -EAGAIN;
  out:
a5c58892b   Bryan Schumaker   NFS: Create a v4-...
238
239
  	return ret;
  }
4ff79bc70   Christoph Hellwig   nfs: remove nfs4_...
240
  int
a5c58892b   Bryan Schumaker   NFS: Create a v4-...
241
242
243
  nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  {
  	int ret;
496ad9aa8   Al Viro   new helper: file_...
244
  	struct inode *inode = file_inode(file);
a5c58892b   Bryan Schumaker   NFS: Create a v4-...
245

f4ce1299b   Trond Myklebust   NFS: Add event tr...
246
  	trace_nfs_fsync_enter(inode);
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
247
248
249
250
  	do {
  		ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  		if (ret != 0)
  			break;
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
251
  		ret = nfs_file_fsync_commit(file, start, end, datasync);
4ff79bc70   Christoph Hellwig   nfs: remove nfs4_...
252
253
  		if (!ret)
  			ret = pnfs_sync_inode(inode, !!datasync);
dcfc4f254   Trond Myklebust   NFS: Write the en...
254
255
256
257
258
259
260
  		/*
  		 * If nfs_file_fsync_commit detected a server reboot, then
  		 * resend all dirty pages that might have been covered by
  		 * the NFS_CONTEXT_RESEND_WRITES flag
  		 */
  		start = 0;
  		end = LLONG_MAX;
05990d1bf   Trond Myklebust   NFS: Fix fdatasyn...
261
  	} while (ret == -EAGAIN);
f4ce1299b   Trond Myklebust   NFS: Add event tr...
262
  	trace_nfs_fsync_exit(inode, ret);
af7fa1650   Trond Myklebust   NFS: Fix up the f...
263
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  }
4ff79bc70   Christoph Hellwig   nfs: remove nfs4_...
265
  EXPORT_SYMBOL_GPL(nfs_file_fsync);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
  
  /*
38c73044f   Peter Staubach   NFS: read-modify-...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
   * Decide whether a read/modify/write cycle may be more efficient
   * then a modify/write/read cycle when writing to a page in the
   * page cache.
   *
   * The modify/write/read cycle may occur if a page is read before
   * being completely filled by the writer.  In this situation, the
   * page must be completely written to stable storage on the server
   * before it can be refilled by reading in the page from the server.
   * This can lead to expensive, small, FILE_SYNC mode writes being
   * done.
   *
   * It may be more efficient to read the page first if the file is
   * open for reading in addition to writing, the page is not marked
   * as Uptodate, it is not dirty or waiting to be committed,
   * indicating that it was previously allocated and then modified,
   * that there were valid bytes of data in that range of the file,
   * and that the new data won't completely replace the old data in
   * that range of the file.
   */
  static int nfs_want_read_modify_write(struct file *file, struct page *page,
  			loff_t pos, unsigned len)
  {
  	unsigned int pglen = nfs_page_length(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
291
  	unsigned int offset = pos & (PAGE_SIZE - 1);
38c73044f   Peter Staubach   NFS: read-modify-...
292
  	unsigned int end = offset + len;
612aa983a   Christoph Hellwig   pnfs: add flag to...
293
294
295
296
297
  	if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  		if (!PageUptodate(page))
  			return 1;
  		return 0;
  	}
38c73044f   Peter Staubach   NFS: read-modify-...
298
299
300
301
302
303
304
305
306
307
  	if ((file->f_mode & FMODE_READ) &&	/* open for read? */
  	    !PageUptodate(page) &&		/* Uptodate? */
  	    !PagePrivate(page) &&		/* i/o request already? */
  	    pglen &&				/* valid bytes of file? */
  	    (end < pglen || offset))		/* replace all valid bytes? */
  		return 1;
  	return 0;
  }
  
  /*
4899f9c85   Nick Piggin   nfs: convert to n...
308
309
310
   * This does the "real" work of the write. We must allocate and lock the
   * page to be sent back to the generic routine, which then copies the
   * data from user space.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
313
314
   *
   * If the writer ends up delaying the write, the writer needs to
   * increment the page use counts until he is done with the page.
   */
4899f9c85   Nick Piggin   nfs: convert to n...
315
316
317
  static int nfs_write_begin(struct file *file, struct address_space *mapping,
  			loff_t pos, unsigned len, unsigned flags,
  			struct page **pagep, void **fsdata)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
  {
4899f9c85   Nick Piggin   nfs: convert to n...
319
  	int ret;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
320
  	pgoff_t index = pos >> PAGE_SHIFT;
4899f9c85   Nick Piggin   nfs: convert to n...
321
  	struct page *page;
38c73044f   Peter Staubach   NFS: read-modify-...
322
  	int once_thru = 0;
4899f9c85   Nick Piggin   nfs: convert to n...
323

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
324
325
  	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
326
  		file, mapping->host->i_ino, len, (long long) pos);
b7eaefaa8   Chuck Lever   NFS: Add debuggin...
327

38c73044f   Peter Staubach   NFS: read-modify-...
328
  start:
54566b2c1   Nick Piggin   fs: symlink write...
329
  	page = grab_cache_page_write_begin(mapping, index, flags);
4899f9c85   Nick Piggin   nfs: convert to n...
330
331
332
333
334
335
336
  	if (!page)
  		return -ENOMEM;
  	*pagep = page;
  
  	ret = nfs_flush_incompatible(file, page);
  	if (ret) {
  		unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
337
  		put_page(page);
38c73044f   Peter Staubach   NFS: read-modify-...
338
339
340
341
  	} else if (!once_thru &&
  		   nfs_want_read_modify_write(file, page, pos, len)) {
  		once_thru = 1;
  		ret = nfs_readpage(file, page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
342
  		put_page(page);
38c73044f   Peter Staubach   NFS: read-modify-...
343
344
  		if (!ret)
  			goto start;
4899f9c85   Nick Piggin   nfs: convert to n...
345
346
  	}
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
  }
4899f9c85   Nick Piggin   nfs: convert to n...
348
349
350
  static int nfs_write_end(struct file *file, struct address_space *mapping,
  			loff_t pos, unsigned len, unsigned copied,
  			struct page *page, void *fsdata)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
  {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
352
  	unsigned offset = pos & (PAGE_SIZE - 1);
dc24826bf   Andy Adamson   NFS avoid expired...
353
  	struct nfs_open_context *ctx = nfs_file_open_context(file);
4899f9c85   Nick Piggin   nfs: convert to n...
354
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
356
357
  	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
358
  		file, mapping->host->i_ino, len, (long long) pos);
b7eaefaa8   Chuck Lever   NFS: Add debuggin...
359

efc91ed01   Trond Myklebust   NFS: Optimise app...
360
361
362
363
364
365
  	/*
  	 * Zero any uninitialised parts of the page, and then mark the page
  	 * as up to date if it turns out that we're extending the file.
  	 */
  	if (!PageUptodate(page)) {
  		unsigned pglen = nfs_page_length(page);
b775b86a5   Al Viro   nfs_write_end(): ...
366
  		unsigned end = offset + copied;
efc91ed01   Trond Myklebust   NFS: Optimise app...
367
368
369
  
  		if (pglen == 0) {
  			zero_user_segments(page, 0, offset,
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
370
  					end, PAGE_SIZE);
efc91ed01   Trond Myklebust   NFS: Optimise app...
371
372
  			SetPageUptodate(page);
  		} else if (end >= pglen) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
373
  			zero_user_segment(page, end, PAGE_SIZE);
efc91ed01   Trond Myklebust   NFS: Optimise app...
374
375
376
  			if (offset == 0)
  				SetPageUptodate(page);
  		} else
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
377
  			zero_user_segment(page, pglen, PAGE_SIZE);
efc91ed01   Trond Myklebust   NFS: Optimise app...
378
  	}
4899f9c85   Nick Piggin   nfs: convert to n...
379
  	status = nfs_updatepage(file, page, offset, copied);
4899f9c85   Nick Piggin   nfs: convert to n...
380
381
  
  	unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
382
  	put_page(page);
4899f9c85   Nick Piggin   nfs: convert to n...
383

3d509e545   Chuck Lever   NFS: nfs_write_en...
384
385
  	if (status < 0)
  		return status;
2701d086d   Andy Adamson   NFSv4.1 add nfs_i...
386
  	NFS_I(mapping->host)->write_io += copied;
dc24826bf   Andy Adamson   NFS avoid expired...
387

ce52914eb   Scott Mayhew   sunrpc: move NO_C...
388
  	if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
dc24826bf   Andy Adamson   NFS avoid expired...
389
390
391
392
  		status = nfs_wb_all(mapping->host);
  		if (status < 0)
  			return status;
  	}
3d509e545   Chuck Lever   NFS: nfs_write_en...
393
  	return copied;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  }
6b9b3514a   David Howells   NFS: Add comment ...
395
396
397
398
  /*
   * Partially or wholly invalidate a page
   * - Release the private state associated with a page if undergoing complete
   *   page invalidation
545db45f0   David Howells   NFS: FS-Cache pag...
399
   * - Called if either PG_private or PG_fscache is set on the page
6b9b3514a   David Howells   NFS: Add comment ...
400
401
   * - Caller holds page lock
   */
d47992f86   Lukas Czerner   mm: change invali...
402
403
  static void nfs_invalidate_page(struct page *page, unsigned int offset,
  				unsigned int length)
cd52ed355   Trond Myklebust   NFS: Avoid races ...
404
  {
d47992f86   Lukas Czerner   mm: change invali...
405
406
407
  	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)
  ",
  		 page, offset, length);
b7eaefaa8   Chuck Lever   NFS: Add debuggin...
408

09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
409
  	if (offset != 0 || length < PAGE_SIZE)
1c75950b9   Trond Myklebust   NFS: cleanup of n...
410
  		return;
d2ccddf04   Trond Myklebust   NFS: Flesh out nf...
411
  	/* Cancel any unstarted writes on this page */
d56b4ddf7   Mel Gorman   nfs: teach the NF...
412
  	nfs_wb_page_cancel(page_file_mapping(page)->host, page);
545db45f0   David Howells   NFS: FS-Cache pag...
413
414
  
  	nfs_fscache_invalidate_page(page, page->mapping->host);
cd52ed355   Trond Myklebust   NFS: Avoid races ...
415
  }
6b9b3514a   David Howells   NFS: Add comment ...
416
417
  /*
   * Attempt to release the private state associated with a page
545db45f0   David Howells   NFS: FS-Cache pag...
418
   * - Called if either PG_private or PG_fscache is set on the page
6b9b3514a   David Howells   NFS: Add comment ...
419
420
421
   * - Caller holds page lock
   * - Return true (may release page) or false (may not)
   */
cd52ed355   Trond Myklebust   NFS: Avoid races ...
422
423
  static int nfs_release_page(struct page *page, gfp_t gfp)
  {
b7eaefaa8   Chuck Lever   NFS: Add debuggin...
424
425
  	dfprintk(PAGECACHE, "NFS: release_page(%p)
  ", page);
e3db7691e   Trond Myklebust   [PATCH] NFS: Fix ...
426
  	/* If PagePrivate() is set, then the page is not freeable */
545db45f0   David Howells   NFS: FS-Cache pag...
427
428
429
  	if (PagePrivate(page))
  		return 0;
  	return nfs_fscache_release_page(page, gfp);
e3db7691e   Trond Myklebust   [PATCH] NFS: Fix ...
430
  }
f919b1961   Mel Gorman   fs: nfs: inform t...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  static void nfs_check_dirty_writeback(struct page *page,
  				bool *dirty, bool *writeback)
  {
  	struct nfs_inode *nfsi;
  	struct address_space *mapping = page_file_mapping(page);
  
  	if (!mapping || PageSwapCache(page))
  		return;
  
  	/*
  	 * Check if an unstable page is currently being committed and
  	 * if so, have the VM treat it as if the page is under writeback
  	 * so it will not block due to pages that will shortly be freeable.
  	 */
  	nfsi = NFS_I(mapping->host);
af7cf0579   Trond Myklebust   NFS: Allow multip...
446
  	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
f919b1961   Mel Gorman   fs: nfs: inform t...
447
448
449
450
451
452
453
454
455
456
457
458
  		*writeback = true;
  		return;
  	}
  
  	/*
  	 * If PagePrivate() is set, then the page is not freeable and as the
  	 * inode is not being committed, it's not going to be cleaned in the
  	 * near future so treat it as dirty
  	 */
  	if (PagePrivate(page))
  		*dirty = true;
  }
6b9b3514a   David Howells   NFS: Add comment ...
459
460
461
462
  /*
   * Attempt to clear the private state associated with a page when an error
   * occurs that requires the cached contents of an inode to be written back or
   * destroyed
545db45f0   David Howells   NFS: FS-Cache pag...
463
   * - Called if either PG_private or fscache is set on the page
6b9b3514a   David Howells   NFS: Add comment ...
464
465
466
   * - Caller holds page lock
   * - Return 0 if successful, -error otherwise
   */
e3db7691e   Trond Myklebust   [PATCH] NFS: Fix ...
467
468
  static int nfs_launder_page(struct page *page)
  {
d56b4ddf7   Mel Gorman   nfs: teach the NF...
469
  	struct inode *inode = page_file_mapping(page)->host;
545db45f0   David Howells   NFS: FS-Cache pag...
470
  	struct nfs_inode *nfsi = NFS_I(inode);
b7eaefaa8   Chuck Lever   NFS: Add debuggin...
471
472
473
474
  
  	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)
  ",
  		inode->i_ino, (long long)page_offset(page));
545db45f0   David Howells   NFS: FS-Cache pag...
475
  	nfs_fscache_wait_on_page_write(nfsi, page);
d6c843b96   Peng Tao   nfs: only remove ...
476
  	return nfs_wb_launder_page(inode, page);
cd52ed355   Trond Myklebust   NFS: Avoid races ...
477
  }
a564b8f03   Mel Gorman   nfs: enable swap ...
478
479
480
  static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  						sector_t *span)
  {
dad2b015b   Jeff Layton   nfs: fix RCU cl_x...
481
  	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
a564b8f03   Mel Gorman   nfs: enable swap ...
482
  	*span = sis->pages;
dad2b015b   Jeff Layton   nfs: fix RCU cl_x...
483

3c87ef6ef   Jeff Layton   sunrpc: keep a co...
484
  	return rpc_clnt_swap_activate(clnt);
a564b8f03   Mel Gorman   nfs: enable swap ...
485
486
487
488
  }
  
  static void nfs_swap_deactivate(struct file *file)
  {
dad2b015b   Jeff Layton   nfs: fix RCU cl_x...
489
  	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
3c87ef6ef   Jeff Layton   sunrpc: keep a co...
490
  	rpc_clnt_swap_deactivate(clnt);
a564b8f03   Mel Gorman   nfs: enable swap ...
491
  }
a564b8f03   Mel Gorman   nfs: enable swap ...
492

f5e54d6e5   Christoph Hellwig   [PATCH] mark addr...
493
  const struct address_space_operations nfs_file_aops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
495
  	.readpage = nfs_readpage,
  	.readpages = nfs_readpages,
9cccef950   Trond Myklebust   NFS: Clean up wri...
496
  	.set_page_dirty = __set_page_dirty_nobuffers,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
  	.writepage = nfs_writepage,
  	.writepages = nfs_writepages,
4899f9c85   Nick Piggin   nfs: convert to n...
499
500
  	.write_begin = nfs_write_begin,
  	.write_end = nfs_write_end,
cd52ed355   Trond Myklebust   NFS: Avoid races ...
501
502
  	.invalidatepage = nfs_invalidate_page,
  	.releasepage = nfs_release_page,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
  	.direct_IO = nfs_direct_IO,
f844cd0d7   Chao Yu   nfs: cover ->migr...
504
  #ifdef CONFIG_MIGRATION
074cc1dee   Trond Myklebust   NFS: Add a ->migr...
505
  	.migratepage = nfs_migrate_page,
f844cd0d7   Chao Yu   nfs: cover ->migr...
506
  #endif
e3db7691e   Trond Myklebust   [PATCH] NFS: Fix ...
507
  	.launder_page = nfs_launder_page,
f919b1961   Mel Gorman   fs: nfs: inform t...
508
  	.is_dirty_writeback = nfs_check_dirty_writeback,
f590f333f   Andi Kleen   HWPOISON: Enable ...
509
  	.error_remove_page = generic_error_remove_page,
a564b8f03   Mel Gorman   nfs: enable swap ...
510
511
  	.swap_activate = nfs_swap_activate,
  	.swap_deactivate = nfs_swap_deactivate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
512
  };
6b9b3514a   David Howells   NFS: Add comment ...
513
514
515
516
517
  /*
   * Notification that a PTE pointing to an NFS page is about to be made
   * writable, implying that someone is about to modify the page through a
   * shared-writable mapping
   */
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
518
  static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
94387fb1a   Trond Myklebust   NFS: Add the help...
519
  {
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
520
  	struct page *page = vmf->page;
94387fb1a   Trond Myklebust   NFS: Add the help...
521
  	struct file *filp = vma->vm_file;
6de1472f1   Al Viro   nfs: use %p[dD] i...
522
  	struct inode *inode = file_inode(filp);
94387fb1a   Trond Myklebust   NFS: Add the help...
523
  	unsigned pagelen;
bc4866b6e   Trond Myklebust   NFS: Don't SIGBUS...
524
  	int ret = VM_FAULT_NOPAGE;
4899f9c85   Nick Piggin   nfs: convert to n...
525
  	struct address_space *mapping;
94387fb1a   Trond Myklebust   NFS: Add the help...
526

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
527
528
  	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
529
  		filp, filp->f_mapping->host->i_ino,
b7eaefaa8   Chuck Lever   NFS: Add debuggin...
530
  		(long long)page_offset(page));
9a773e7c8   Trond Myklebust   NFS nfs_vm_page_m...
531
  	sb_start_pagefault(inode->i_sb);
545db45f0   David Howells   NFS: FS-Cache pag...
532
  	/* make sure the cache has finished storing the page */
6de1472f1   Al Viro   nfs: use %p[dD] i...
533
  	nfs_fscache_wait_on_page_write(NFS_I(inode), page);
545db45f0   David Howells   NFS: FS-Cache pag...
534

ef070dcb3   Trond Myklebust   NFS: Don't write ...
535
536
  	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
  			nfs_wait_bit_killable, TASK_KILLABLE);
94387fb1a   Trond Myklebust   NFS: Add the help...
537
  	lock_page(page);
d56b4ddf7   Mel Gorman   nfs: teach the NF...
538
  	mapping = page_file_mapping(page);
6de1472f1   Al Viro   nfs: use %p[dD] i...
539
  	if (mapping != inode->i_mapping)
8b1f9ee56   Trond Myklebust   NFS: Optimise nfs...
540
  		goto out_unlock;
2aeb98f49   Trond Myklebust   NFS: Ensure that ...
541
  	wait_on_page_writeback(page);
94387fb1a   Trond Myklebust   NFS: Add the help...
542
  	pagelen = nfs_page_length(page);
8b1f9ee56   Trond Myklebust   NFS: Optimise nfs...
543
544
  	if (pagelen == 0)
  		goto out_unlock;
4899f9c85   Nick Piggin   nfs: convert to n...
545

bc4866b6e   Trond Myklebust   NFS: Don't SIGBUS...
546
547
548
549
  	ret = VM_FAULT_LOCKED;
  	if (nfs_flush_incompatible(filp, page) == 0 &&
  	    nfs_updatepage(filp, page, 0, pagelen) == 0)
  		goto out;
8b1f9ee56   Trond Myklebust   NFS: Optimise nfs...
550

bc4866b6e   Trond Myklebust   NFS: Don't SIGBUS...
551
  	ret = VM_FAULT_SIGBUS;
8b1f9ee56   Trond Myklebust   NFS: Optimise nfs...
552
553
  out_unlock:
  	unlock_page(page);
bc4866b6e   Trond Myklebust   NFS: Don't SIGBUS...
554
  out:
9a773e7c8   Trond Myklebust   NFS nfs_vm_page_m...
555
  	sb_end_pagefault(inode->i_sb);
bc4866b6e   Trond Myklebust   NFS: Don't SIGBUS...
556
  	return ret;
94387fb1a   Trond Myklebust   NFS: Add the help...
557
  }
f0f37e2f7   Alexey Dobriyan   const: mark struc...
558
  static const struct vm_operations_struct nfs_file_vm_ops = {
94387fb1a   Trond Myklebust   NFS: Add the help...
559
  	.fault = filemap_fault,
f1820361f   Kirill A. Shutemov   mm: implement ->m...
560
  	.map_pages = filemap_map_pages,
94387fb1a   Trond Myklebust   NFS: Add the help...
561
562
  	.page_mkwrite = nfs_vm_page_mkwrite,
  };
7e94d6c4a   Trond Myklebust   NFS: Don't fsync ...
563
  static int nfs_need_check_write(struct file *filp, struct inode *inode)
7b159fc18   Trond Myklebust   NFS: Fall back to...
564
565
  {
  	struct nfs_open_context *ctx;
cd3758e37   Trond Myklebust   NFS: Replace file...
566
  	ctx = nfs_file_open_context(filp);
dc24826bf   Andy Adamson   NFS avoid expired...
567
  	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
ce52914eb   Scott Mayhew   sunrpc: move NO_C...
568
  	    nfs_ctx_key_to_expire(ctx, inode))
7b159fc18   Trond Myklebust   NFS: Fall back to...
569
570
571
  		return 1;
  	return 0;
  }
edaf43694   Al Viro   nfs: switch to ->...
572
  ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
574
575
  	struct file *file = iocb->ki_filp;
  	struct inode *inode = file_inode(file);
7e381172c   Chuck Lever   NFS: Improve NFS ...
576
  	unsigned long written = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
  	ssize_t result;
6de1472f1   Al Viro   nfs: use %p[dD] i...
578
  	result = nfs_key_timeout_notify(file, inode);
dc24826bf   Andy Adamson   NFS avoid expired...
579
580
  	if (result)
  		return result;
89698b24d   Trond Myklebust   NFS Cleanup: move...
581
  	if (iocb->ki_flags & IOCB_DIRECT)
65a4a1cad   Al Viro   nfs: generic_writ...
582
  		return nfs_file_direct_write(iocb, from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
583

619d30b4b   Al Viro   convert the guts ...
584
585
  	dprintk("NFS: write(%pD2, %zu@%Ld)
  ",
18290650b   Trond Myklebust   NFS: Move buffere...
586
  		file, iov_iter_count(from), (long long) iocb->ki_pos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
  	if (IS_SWAPFILE(inode))
  		goto out_swapfile;
7d52e8627   Trond Myklebust   [PATCH] NFS: Clea...
590
591
592
  	/*
  	 * O_APPEND implies that we must revalidate the file length.
  	 */
2ba48ce51   Al Viro   mirror O_APPEND a...
593
  	if (iocb->ki_flags & IOCB_APPEND) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
594
  		result = nfs_revalidate_file_size(inode, file);
7d52e8627   Trond Myklebust   [PATCH] NFS: Clea...
595
596
  		if (result)
  			goto out;
fe51beecc   Trond Myklebust   [PATCH] NFS: Ensu...
597
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
598

a5864c999   Trond Myklebust   NFS: Do not seria...
599
  	nfs_start_io_write(inode);
18290650b   Trond Myklebust   NFS: Move buffere...
600
601
602
603
604
605
  	result = generic_write_checks(iocb, from);
  	if (result > 0) {
  		current->backing_dev_info = inode_to_bdi(inode);
  		result = generic_perform_write(file, from, iocb->ki_pos);
  		current->backing_dev_info = NULL;
  	}
a5864c999   Trond Myklebust   NFS: Do not seria...
606
  	nfs_end_io_write(inode);
18290650b   Trond Myklebust   NFS: Move buffere...
607
  	if (result <= 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
  		goto out;
c49edecd5   Trond Myklebust   NFS: Fix error re...
609
610
611
612
  	result = generic_write_sync(iocb, result);
  	if (result < 0)
  		goto out;
  	written = result;
18290650b   Trond Myklebust   NFS: Move buffere...
613
  	iocb->ki_pos += written;
7e381172c   Chuck Lever   NFS: Improve NFS ...
614

7e94d6c4a   Trond Myklebust   NFS: Don't fsync ...
615
  	/* Return error values */
18290650b   Trond Myklebust   NFS: Move buffere...
616
  	if (nfs_need_check_write(file, inode)) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
617
  		int err = vfs_fsync(file, 0);
200baa211   Trond Myklebust   NFS: Remove nfs_w...
618
619
620
  		if (err < 0)
  			result = err;
  	}
18290650b   Trond Myklebust   NFS: Move buffere...
621
  	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
623
624
625
626
627
  out:
  	return result;
  
  out_swapfile:
  	printk(KERN_INFO "NFS: attempt to write to active swap file!
  ");
18290650b   Trond Myklebust   NFS: Move buffere...
628
  	return -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
630
  EXPORT_SYMBOL_GPL(nfs_file_write);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631

5eebde232   Suresh Jayaraman   nfs: introduce mo...
632
633
  static int
  do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
  {
  	struct inode *inode = filp->f_mapping->host;
  	int status = 0;
21ac19d48   Sergey Vlasov   NFS: Fix fcntl F_...
637
  	unsigned int saved_type = fl->fl_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638

039c4d7a8   Trond Myklebust   NFS: Fix up a rac...
639
  	/* Try local locking first */
6d34ac199   J. Bruce Fields   locks: make posix...
640
641
642
  	posix_test_lock(filp, fl);
  	if (fl->fl_type != F_UNLCK) {
  		/* found a conflict */
039c4d7a8   Trond Myklebust   NFS: Fix up a rac...
643
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
  	}
21ac19d48   Sergey Vlasov   NFS: Fix fcntl F_...
645
  	fl->fl_type = saved_type;
039c4d7a8   Trond Myklebust   NFS: Fix up a rac...
646

011e2a7fd   Bryan Schumaker   NFS: Create a hav...
647
  	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
039c4d7a8   Trond Myklebust   NFS: Fix up a rac...
648
  		goto out_noconflict;
5eebde232   Suresh Jayaraman   nfs: introduce mo...
649
  	if (is_local)
039c4d7a8   Trond Myklebust   NFS: Fix up a rac...
650
651
652
653
  		goto out_noconflict;
  
  	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
  	return status;
039c4d7a8   Trond Myklebust   NFS: Fix up a rac...
655
656
657
  out_noconflict:
  	fl->fl_type = F_UNLCK;
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  }
5eebde232   Suresh Jayaraman   nfs: introduce mo...
659
660
  static int
  do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
662
  {
  	struct inode *inode = filp->f_mapping->host;
7a8203d8c   Trond Myklebust   NFS: Ensure that ...
663
  	struct nfs_lock_context *l_ctx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
666
667
668
  	/*
  	 * Flush all pending writes before doing anything
  	 * with locks..
  	 */
d9dabc1a0   Trond Myklebust   NFS: File unlock ...
669
  	vfs_fsync(filp, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670

7a8203d8c   Trond Myklebust   NFS: Ensure that ...
671
672
  	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  	if (!IS_ERR(l_ctx)) {
210c7c175   Benjamin Coddington   NFS: Use wait_on_...
673
  		status = nfs_iocounter_wait(l_ctx);
7a8203d8c   Trond Myklebust   NFS: Ensure that ...
674
675
676
677
  		nfs_put_lock_context(l_ctx);
  		if (status < 0)
  			return status;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
681
  	/* NOTE: special case
  	 * 	If we're signalled while cleaning up locks on process exit, we
  	 * 	still need to complete the unlock.
  	 */
5eebde232   Suresh Jayaraman   nfs: introduce mo...
682
683
684
685
686
  	/*
  	 * Use local locking if mounted with "-onolock" or with appropriate
  	 * "-olocal_lock="
  	 */
  	if (!is_local)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
  		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  	else
75575ddf2   Jeff Layton   nfs: eliminate po...
689
  		status = locks_lock_file_wait(filp, fl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
  	return status;
  }
5eebde232   Suresh Jayaraman   nfs: introduce mo...
692
693
  static int
  do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
695
  {
  	struct inode *inode = filp->f_mapping->host;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
698
699
700
  	/*
  	 * Flush all pending writes before doing anything
  	 * with locks..
  	 */
29884df0d   Trond Myklebust   NFS: Fix another ...
701
702
  	status = nfs_sync_mapping(filp->f_mapping);
  	if (status != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
  		goto out;
5eebde232   Suresh Jayaraman   nfs: introduce mo...
704
705
706
707
708
  	/*
  	 * Use local locking if mounted with "-onolock" or with appropriate
  	 * "-olocal_lock="
  	 */
  	if (!is_local)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
  		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
c4d7c402b   Trond Myklebust   NFS: Remove the b...
710
  	else
75575ddf2   Jeff Layton   nfs: eliminate po...
711
  		status = locks_lock_file_wait(filp, fl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
713
  	if (status < 0)
  		goto out;
6b96724e5   Ricardo Labiaga   Revalidate caches...
714

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
  	/*
6b96724e5   Ricardo Labiaga   Revalidate caches...
716
717
718
719
  	 * Revalidate the cache if the server has time stamps granular
  	 * enough to detect subsecond changes.  Otherwise, clear the
  	 * cache to prevent missing any changes.
  	 *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
  	 * This makes locking act as a cache coherency point.
  	 */
29884df0d   Trond Myklebust   NFS: Fix another ...
722
  	nfs_sync_mapping(filp->f_mapping);
ca0daa277   Trond Myklebust   NFS: Cache aggres...
723
724
  	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  		nfs_zap_mapping(inode, filp->f_mapping);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
730
731
  	return status;
  }
  
  /*
   * Lock a (portion of) a file
   */
ce4ef7c0a   Bryan Schumaker   NFS: Split out NF...
732
  int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
733
  {
6da24bc9c   Chuck Lever   NFS: Use NFSDBG_F...
734
  	struct inode *inode = filp->f_mapping->host;
2116271a3   Trond Myklebust   NFS: Add correct ...
735
  	int ret = -ENOLCK;
5eebde232   Suresh Jayaraman   nfs: introduce mo...
736
  	int is_local = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737

6de1472f1   Al Viro   nfs: use %p[dD] i...
738
739
740
  	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)
  ",
  			filp, fl->fl_type, fl->fl_flags,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
  			(long long)fl->fl_start, (long long)fl->fl_end);
6da24bc9c   Chuck Lever   NFS: Use NFSDBG_F...
742

91d5b4702   Chuck Lever   NFS: add I/O perf...
743
  	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744
745
  
  	/* No mandatory locks over NFS */
dfad9441b   Pavel Emelyanov   NFS: clean up exp...
746
  	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
2116271a3   Trond Myklebust   NFS: Add correct ...
747
  		goto out_err;
5eebde232   Suresh Jayaraman   nfs: introduce mo...
748
749
  	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  		is_local = 1;
2116271a3   Trond Myklebust   NFS: Add correct ...
750
751
752
753
754
  	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  		if (ret < 0)
  			goto out_err;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755
756
  
  	if (IS_GETLK(cmd))
5eebde232   Suresh Jayaraman   nfs: introduce mo...
757
  		ret = do_getlk(filp, cmd, fl, is_local);
2116271a3   Trond Myklebust   NFS: Add correct ...
758
  	else if (fl->fl_type == F_UNLCK)
5eebde232   Suresh Jayaraman   nfs: introduce mo...
759
  		ret = do_unlk(filp, cmd, fl, is_local);
2116271a3   Trond Myklebust   NFS: Add correct ...
760
  	else
5eebde232   Suresh Jayaraman   nfs: introduce mo...
761
  		ret = do_setlk(filp, cmd, fl, is_local);
2116271a3   Trond Myklebust   NFS: Add correct ...
762
763
  out_err:
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
764
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
765
  EXPORT_SYMBOL_GPL(nfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
767
768
769
  
  /*
   * Lock a (portion of) a file
   */
ce4ef7c0a   Bryan Schumaker   NFS: Split out NF...
770
  int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
  {
5eebde232   Suresh Jayaraman   nfs: introduce mo...
772
773
  	struct inode *inode = filp->f_mapping->host;
  	int is_local = 0;
6de1472f1   Al Viro   nfs: use %p[dD] i...
774
775
776
  	dprintk("NFS: flock(%pD2, t=%x, fl=%x)
  ",
  			filp, fl->fl_type, fl->fl_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
778
779
  	if (!(fl->fl_flags & FL_FLOCK))
  		return -ENOLCK;
ad0fcd4eb   Jeff Layton   nfs: explicitly r...
780
781
782
783
784
785
786
787
  	/*
  	 * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  	 * any standard. In principle we might be able to support LOCK_MAND
  	 * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  	 * NFS code is not set up for it.
  	 */
  	if (fl->fl_type & LOCK_MAND)
  		return -EINVAL;
5eebde232   Suresh Jayaraman   nfs: introduce mo...
788
789
  	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  		is_local = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790
  	/* We're simulating flock() locks using posix locks on the server */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
  	if (fl->fl_type == F_UNLCK)
5eebde232   Suresh Jayaraman   nfs: introduce mo...
792
793
  		return do_unlk(filp, cmd, fl, is_local);
  	return do_setlk(filp, cmd, fl, is_local);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
795
  EXPORT_SYMBOL_GPL(nfs_flock);
370f6599e   J. Bruce Fields   nfs: disable leas...
796

0486958f5   Jeff Layton   nfs: move nfs_fil...
797
798
  const struct file_operations nfs_file_operations = {
  	.llseek		= nfs_file_llseek,
3aa2d199f   Al Viro   nfs: switch to ->...
799
  	.read_iter	= nfs_file_read,
edaf43694   Al Viro   nfs: switch to ->...
800
  	.write_iter	= nfs_file_write,
0486958f5   Jeff Layton   nfs: move nfs_fil...
801
802
803
804
805
806
807
  	.mmap		= nfs_file_mmap,
  	.open		= nfs_file_open,
  	.flush		= nfs_file_flush,
  	.release	= nfs_file_release,
  	.fsync		= nfs_file_fsync,
  	.lock		= nfs_lock,
  	.flock		= nfs_flock,
82c156f85   Al Viro   switch generic_fi...
808
  	.splice_read	= generic_file_splice_read,
4da54c218   Al Viro   nfs: switch to it...
809
  	.splice_write	= iter_file_splice_write,
0486958f5   Jeff Layton   nfs: move nfs_fil...
810
  	.check_flags	= nfs_check_flags,
1c994a090   Jeff Layton   locks: consolidat...
811
  	.setlease	= simple_nosetlease,
0486958f5   Jeff Layton   nfs: move nfs_fil...
812
  };
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
813
  EXPORT_SYMBOL_GPL(nfs_file_operations);