]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/file.c
[PATCH] NFS: Cleanup of caching code, and slight optimization of writes.
[mirror_ubuntu-artful-kernel.git] / fs / nfs / file.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * Changes Copyright (C) 1994 by Florian La Roche
7 * - Do not copy data too often around in the kernel.
8 * - In nfs_file_read the return value of kmalloc wasn't checked.
9 * - Put in a better version of read look-ahead buffering. Original idea
10 * and implementation by Wai S Kok elekokws@ee.nus.sg.
11 *
12 * Expire cache on write to a file by Wai S Kok (Oct 1994).
13 *
14 * Total rewrite of read side for new NFS buffer cache.. Linus.
15 *
16 * nfs regular file handling functions
17 */
18
19#include <linux/time.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/fcntl.h>
23#include <linux/stat.h>
24#include <linux/nfs_fs.h>
25#include <linux/nfs_mount.h>
26#include <linux/mm.h>
27#include <linux/slab.h>
28#include <linux/pagemap.h>
29#include <linux/smp_lock.h>
30
31#include <asm/uaccess.h>
32#include <asm/system.h>
33
34#include "delegation.h"
35
36#define NFSDBG_FACILITY NFSDBG_FILE
37
38static int nfs_file_open(struct inode *, struct file *);
39static int nfs_file_release(struct inode *, struct file *);
980802e3 40static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
1da177e4
LT
41static int nfs_file_mmap(struct file *, struct vm_area_struct *);
42static ssize_t nfs_file_sendfile(struct file *, loff_t *, size_t, read_actor_t, void *);
43static ssize_t nfs_file_read(struct kiocb *, char __user *, size_t, loff_t);
44static ssize_t nfs_file_write(struct kiocb *, const char __user *, size_t, loff_t);
45static int nfs_file_flush(struct file *);
46static int nfs_fsync(struct file *, struct dentry *dentry, int datasync);
47static int nfs_check_flags(int flags);
48static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
49static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
50
51struct file_operations nfs_file_operations = {
980802e3 52 .llseek = nfs_file_llseek,
1da177e4
LT
53 .read = do_sync_read,
54 .write = do_sync_write,
55 .aio_read = nfs_file_read,
56 .aio_write = nfs_file_write,
57 .mmap = nfs_file_mmap,
58 .open = nfs_file_open,
59 .flush = nfs_file_flush,
60 .release = nfs_file_release,
61 .fsync = nfs_fsync,
62 .lock = nfs_lock,
63 .flock = nfs_flock,
64 .sendfile = nfs_file_sendfile,
65 .check_flags = nfs_check_flags,
66};
67
68struct inode_operations nfs_file_inode_operations = {
69 .permission = nfs_permission,
70 .getattr = nfs_getattr,
71 .setattr = nfs_setattr,
72};
73
b7fa0554
AG
74#ifdef CONFIG_NFS_V3
75struct inode_operations nfs3_file_inode_operations = {
76 .permission = nfs_permission,
77 .getattr = nfs_getattr,
78 .setattr = nfs_setattr,
79 .listxattr = nfs3_listxattr,
80 .getxattr = nfs3_getxattr,
81 .setxattr = nfs3_setxattr,
82 .removexattr = nfs3_removexattr,
83};
84#endif /* CONFIG_NFS_v3 */
85
1da177e4
LT
86/* Hack for future NFS swap support */
87#ifndef IS_SWAPFILE
88# define IS_SWAPFILE(inode) (0)
89#endif
90
91static int nfs_check_flags(int flags)
92{
93 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
94 return -EINVAL;
95
96 return 0;
97}
98
99/*
100 * Open file
101 */
102static int
103nfs_file_open(struct inode *inode, struct file *filp)
104{
105 struct nfs_server *server = NFS_SERVER(inode);
106 int (*open)(struct inode *, struct file *);
107 int res;
108
109 res = nfs_check_flags(filp->f_flags);
110 if (res)
111 return res;
112
113 lock_kernel();
114 /* Do NFSv4 open() call */
115 if ((open = server->rpc_ops->file_open) != NULL)
116 res = open(inode, filp);
117 unlock_kernel();
118 return res;
119}
120
121static int
122nfs_file_release(struct inode *inode, struct file *filp)
123{
124 /* Ensure that dirty pages are flushed out with the right creds */
125 if (filp->f_mode & FMODE_WRITE)
126 filemap_fdatawrite(filp->f_mapping);
127 return NFS_PROTO(inode)->file_release(inode, filp);
128}
129
980802e3
TM
130/**
131 * nfs_revalidate_size - Revalidate the file size
132 * @inode - pointer to inode struct
133 * @file - pointer to struct file
134 *
135 * Revalidates the file length. This is basically a wrapper around
136 * nfs_revalidate_inode() that takes into account the fact that we may
137 * have cached writes (in which case we don't care about the server's
138 * idea of what the file length is), or O_DIRECT (in which case we
139 * shouldn't trust the cache).
140 */
141static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
142{
143 struct nfs_server *server = NFS_SERVER(inode);
144 struct nfs_inode *nfsi = NFS_I(inode);
145
146 if (server->flags & NFS_MOUNT_NOAC)
147 goto force_reval;
148 if (filp->f_flags & O_DIRECT)
149 goto force_reval;
150 if (nfsi->npages != 0)
151 return 0;
152 return nfs_revalidate_inode(server, inode);
153force_reval:
154 return __nfs_revalidate_inode(server, inode);
155}
156
157static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
158{
159 /* origin == SEEK_END => we must revalidate the cached file length */
160 if (origin == 2) {
161 struct inode *inode = filp->f_mapping->host;
162 int retval = nfs_revalidate_file_size(inode, filp);
163 if (retval < 0)
164 return (loff_t)retval;
165 }
166 return remote_llseek(filp, offset, origin);
167}
168
1da177e4
LT
169/*
170 * Flush all dirty pages, and check for write errors.
171 *
172 */
173static int
174nfs_file_flush(struct file *file)
175{
176 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
177 struct inode *inode = file->f_dentry->d_inode;
178 int status;
179
180 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
181
182 if ((file->f_mode & FMODE_WRITE) == 0)
183 return 0;
184 lock_kernel();
185 /* Ensure that data+attribute caches are up to date after close() */
186 status = nfs_wb_all(inode);
187 if (!status) {
188 status = ctx->error;
189 ctx->error = 0;
190 if (!status && !nfs_have_delegation(inode, FMODE_READ))
191 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
192 }
193 unlock_kernel();
194 return status;
195}
196
197static ssize_t
198nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
199{
200 struct dentry * dentry = iocb->ki_filp->f_dentry;
201 struct inode * inode = dentry->d_inode;
202 ssize_t result;
203
204#ifdef CONFIG_NFS_DIRECTIO
205 if (iocb->ki_filp->f_flags & O_DIRECT)
206 return nfs_file_direct_read(iocb, buf, count, pos);
207#endif
208
209 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
210 dentry->d_parent->d_name.name, dentry->d_name.name,
211 (unsigned long) count, (unsigned long) pos);
212
213 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
214 if (!result)
215 result = generic_file_aio_read(iocb, buf, count, pos);
216 return result;
217}
218
219static ssize_t
220nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
221 read_actor_t actor, void *target)
222{
223 struct dentry *dentry = filp->f_dentry;
224 struct inode *inode = dentry->d_inode;
225 ssize_t res;
226
227 dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
228 dentry->d_parent->d_name.name, dentry->d_name.name,
229 (unsigned long) count, (unsigned long long) *ppos);
230
231 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
232 if (!res)
233 res = generic_file_sendfile(filp, ppos, count, actor, target);
234 return res;
235}
236
237static int
238nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
239{
240 struct dentry *dentry = file->f_dentry;
241 struct inode *inode = dentry->d_inode;
242 int status;
243
244 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
245 dentry->d_parent->d_name.name, dentry->d_name.name);
246
247 status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
248 if (!status)
249 status = generic_file_mmap(file, vma);
250 return status;
251}
252
253/*
254 * Flush any dirty pages for this process, and check for write errors.
255 * The return status from this call provides a reliable indication of
256 * whether any write errors occurred for this process.
257 */
258static int
259nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
260{
261 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
262 struct inode *inode = dentry->d_inode;
263 int status;
264
265 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
266
267 lock_kernel();
268 status = nfs_wb_all(inode);
269 if (!status) {
270 status = ctx->error;
271 ctx->error = 0;
272 }
273 unlock_kernel();
274 return status;
275}
276
277/*
278 * This does the "real" work of the write. The generic routine has
279 * allocated the page, locked it, done all the page alignment stuff
280 * calculations etc. Now we should just copy the data from user
281 * space and write it back to the real medium..
282 *
283 * If the writer ends up delaying the write, the writer needs to
284 * increment the page use counts until he is done with the page.
285 */
286static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
287{
288 return nfs_flush_incompatible(file, page);
289}
290
291static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
292{
293 long status;
294
295 lock_kernel();
296 status = nfs_updatepage(file, page, offset, to-offset);
297 unlock_kernel();
298 return status;
299}
300
301struct address_space_operations nfs_file_aops = {
302 .readpage = nfs_readpage,
303 .readpages = nfs_readpages,
304 .set_page_dirty = __set_page_dirty_nobuffers,
305 .writepage = nfs_writepage,
306 .writepages = nfs_writepages,
307 .prepare_write = nfs_prepare_write,
308 .commit_write = nfs_commit_write,
309#ifdef CONFIG_NFS_DIRECTIO
310 .direct_IO = nfs_direct_IO,
311#endif
312};
313
314/*
315 * Write to a file (through the page cache).
316 */
317static ssize_t
318nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
319{
320 struct dentry * dentry = iocb->ki_filp->f_dentry;
321 struct inode * inode = dentry->d_inode;
322 ssize_t result;
323
324#ifdef CONFIG_NFS_DIRECTIO
325 if (iocb->ki_filp->f_flags & O_DIRECT)
326 return nfs_file_direct_write(iocb, buf, count, pos);
327#endif
328
329 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
330 dentry->d_parent->d_name.name, dentry->d_name.name,
331 inode->i_ino, (unsigned long) count, (unsigned long) pos);
332
333 result = -EBUSY;
334 if (IS_SWAPFILE(inode))
335 goto out_swapfile;
7d52e862
TM
336 /*
337 * O_APPEND implies that we must revalidate the file length.
338 */
339 if (iocb->ki_filp->f_flags & O_APPEND) {
340 result = nfs_revalidate_file_size(inode, iocb->ki_filp);
341 if (result)
342 goto out;
343 } else
344 nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1da177e4
LT
345
346 result = count;
347 if (!count)
348 goto out;
349
350 result = generic_file_aio_write(iocb, buf, count, pos);
351out:
352 return result;
353
354out_swapfile:
355 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
356 goto out;
357}
358
359static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
360{
361 struct inode *inode = filp->f_mapping->host;
362 int status = 0;
363
364 lock_kernel();
365 /* Use local locking if mounted with "-onolock" */
366 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
367 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
368 else {
369 struct file_lock *cfl = posix_test_lock(filp, fl);
370
371 fl->fl_type = F_UNLCK;
372 if (cfl != NULL)
373 memcpy(fl, cfl, sizeof(*fl));
374 }
375 unlock_kernel();
376 return status;
377}
378
379static int do_vfs_lock(struct file *file, struct file_lock *fl)
380{
381 int res = 0;
382 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
383 case FL_POSIX:
384 res = posix_lock_file_wait(file, fl);
385 break;
386 case FL_FLOCK:
387 res = flock_lock_file_wait(file, fl);
388 break;
389 default:
390 BUG();
391 }
392 if (res < 0)
393 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
394 __FUNCTION__);
395 return res;
396}
397
398static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
399{
400 struct inode *inode = filp->f_mapping->host;
401 sigset_t oldset;
402 int status;
403
404 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
405 /*
406 * Flush all pending writes before doing anything
407 * with locks..
408 */
409 filemap_fdatawrite(filp->f_mapping);
410 down(&inode->i_sem);
411 nfs_wb_all(inode);
412 up(&inode->i_sem);
413 filemap_fdatawait(filp->f_mapping);
414
415 /* NOTE: special case
416 * If we're signalled while cleaning up locks on process exit, we
417 * still need to complete the unlock.
418 */
419 lock_kernel();
420 /* Use local locking if mounted with "-onolock" */
421 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
422 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
423 else
424 status = do_vfs_lock(filp, fl);
425 unlock_kernel();
426 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
427 return status;
428}
429
430static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
431{
432 struct inode *inode = filp->f_mapping->host;
433 sigset_t oldset;
434 int status;
435
436 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
437 /*
438 * Flush all pending writes before doing anything
439 * with locks..
440 */
441 status = filemap_fdatawrite(filp->f_mapping);
442 if (status == 0) {
443 down(&inode->i_sem);
444 status = nfs_wb_all(inode);
445 up(&inode->i_sem);
446 if (status == 0)
447 status = filemap_fdatawait(filp->f_mapping);
448 }
449 if (status < 0)
450 goto out;
451
452 lock_kernel();
453 /* Use local locking if mounted with "-onolock" */
454 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
455 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
456 /* If we were signalled we still need to ensure that
457 * we clean up any state on the server. We therefore
458 * record the lock call as having succeeded in order to
459 * ensure that locks_remove_posix() cleans it out when
460 * the process exits.
461 */
462 if (status == -EINTR || status == -ERESTARTSYS)
463 do_vfs_lock(filp, fl);
464 } else
465 status = do_vfs_lock(filp, fl);
466 unlock_kernel();
467 if (status < 0)
468 goto out;
469 /*
470 * Make sure we clear the cache whenever we try to get the lock.
471 * This makes locking act as a cache coherency point.
472 */
473 filemap_fdatawrite(filp->f_mapping);
474 down(&inode->i_sem);
475 nfs_wb_all(inode); /* we may have slept */
476 up(&inode->i_sem);
477 filemap_fdatawait(filp->f_mapping);
478 nfs_zap_caches(inode);
479out:
480 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
481 return status;
482}
483
484/*
485 * Lock a (portion of) a file
486 */
487static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
488{
489 struct inode * inode = filp->f_mapping->host;
490
491 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
492 inode->i_sb->s_id, inode->i_ino,
493 fl->fl_type, fl->fl_flags,
494 (long long)fl->fl_start, (long long)fl->fl_end);
495
496 if (!inode)
497 return -EINVAL;
498
499 /* No mandatory locks over NFS */
500 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
501 return -ENOLCK;
502
503 if (IS_GETLK(cmd))
504 return do_getlk(filp, cmd, fl);
505 if (fl->fl_type == F_UNLCK)
506 return do_unlk(filp, cmd, fl);
507 return do_setlk(filp, cmd, fl);
508}
509
510/*
511 * Lock a (portion of) a file
512 */
513static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
514{
515 struct inode * inode = filp->f_mapping->host;
516
517 dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
518 inode->i_sb->s_id, inode->i_ino,
519 fl->fl_type, fl->fl_flags);
520
521 if (!inode)
522 return -EINVAL;
523
524 /*
525 * No BSD flocks over NFS allowed.
526 * Note: we could try to fake a POSIX lock request here by
527 * using ((u32) filp | 0x80000000) or some such as the pid.
528 * Not sure whether that would be unique, though, or whether
529 * that would break in other places.
530 */
531 if (!(fl->fl_flags & FL_FLOCK))
532 return -ENOLCK;
533
534 /* We're simulating flock() locks using posix locks on the server */
535 fl->fl_owner = (fl_owner_t)filp;
536 fl->fl_start = 0;
537 fl->fl_end = OFFSET_MAX;
538
539 if (fl->fl_type == F_UNLCK)
540 return do_unlk(filp, cmd, fl);
541 return do_setlk(filp, cmd, fl);
542}