blob: f6b9eda925c526d18c2a1606c1e12568380a557f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *);
Trond Myklebust980802e2005-06-13 11:14:01 -040040static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static 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 = {
Trond Myklebust980802e2005-06-13 11:14:01 -040052 .llseek = nfs_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .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
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000074#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* 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
Trond Myklebust980802e2005-06-13 11:14:01 -0400130/**
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000131 * nfs_revalidate_file - Revalidate the page cache & related metadata
132 * @inode - pointer to inode struct
133 * @file - pointer to file
134 */
135static int nfs_revalidate_file(struct inode *inode, struct file *filp)
136{
Chuck Lever55296802005-08-18 11:24:09 -0700137 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000138 int retval = 0;
139
Chuck Lever55296802005-08-18 11:24:09 -0700140 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) || nfs_attribute_timeout(inode))
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000141 retval = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
142 nfs_revalidate_mapping(inode, filp->f_mapping);
143 return 0;
144}
145
146/**
Trond Myklebust980802e2005-06-13 11:14:01 -0400147 * nfs_revalidate_size - Revalidate the file size
148 * @inode - pointer to inode struct
149 * @file - pointer to struct file
150 *
151 * Revalidates the file length. This is basically a wrapper around
152 * nfs_revalidate_inode() that takes into account the fact that we may
153 * have cached writes (in which case we don't care about the server's
154 * idea of what the file length is), or O_DIRECT (in which case we
155 * shouldn't trust the cache).
156 */
157static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
158{
159 struct nfs_server *server = NFS_SERVER(inode);
160 struct nfs_inode *nfsi = NFS_I(inode);
161
162 if (server->flags & NFS_MOUNT_NOAC)
163 goto force_reval;
164 if (filp->f_flags & O_DIRECT)
165 goto force_reval;
166 if (nfsi->npages != 0)
167 return 0;
Chuck Lever55296802005-08-18 11:24:09 -0700168 if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000169 return 0;
Trond Myklebust980802e2005-06-13 11:14:01 -0400170force_reval:
171 return __nfs_revalidate_inode(server, inode);
172}
173
174static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
175{
176 /* origin == SEEK_END => we must revalidate the cached file length */
177 if (origin == 2) {
178 struct inode *inode = filp->f_mapping->host;
179 int retval = nfs_revalidate_file_size(inode, filp);
180 if (retval < 0)
181 return (loff_t)retval;
182 }
183 return remote_llseek(filp, offset, origin);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * Flush all dirty pages, and check for write errors.
188 *
189 */
190static int
191nfs_file_flush(struct file *file)
192{
193 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
194 struct inode *inode = file->f_dentry->d_inode;
195 int status;
196
197 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
198
199 if ((file->f_mode & FMODE_WRITE) == 0)
200 return 0;
201 lock_kernel();
202 /* Ensure that data+attribute caches are up to date after close() */
203 status = nfs_wb_all(inode);
204 if (!status) {
205 status = ctx->error;
206 ctx->error = 0;
207 if (!status && !nfs_have_delegation(inode, FMODE_READ))
208 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
209 }
210 unlock_kernel();
211 return status;
212}
213
214static ssize_t
215nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
216{
217 struct dentry * dentry = iocb->ki_filp->f_dentry;
218 struct inode * inode = dentry->d_inode;
219 ssize_t result;
220
221#ifdef CONFIG_NFS_DIRECTIO
222 if (iocb->ki_filp->f_flags & O_DIRECT)
223 return nfs_file_direct_read(iocb, buf, count, pos);
224#endif
225
226 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
227 dentry->d_parent->d_name.name, dentry->d_name.name,
228 (unsigned long) count, (unsigned long) pos);
229
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000230 result = nfs_revalidate_file(inode, iocb->ki_filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!result)
232 result = generic_file_aio_read(iocb, buf, count, pos);
233 return result;
234}
235
236static ssize_t
237nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
238 read_actor_t actor, void *target)
239{
240 struct dentry *dentry = filp->f_dentry;
241 struct inode *inode = dentry->d_inode;
242 ssize_t res;
243
244 dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
245 dentry->d_parent->d_name.name, dentry->d_name.name,
246 (unsigned long) count, (unsigned long long) *ppos);
247
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000248 res = nfs_revalidate_file(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!res)
250 res = generic_file_sendfile(filp, ppos, count, actor, target);
251 return res;
252}
253
254static int
255nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
256{
257 struct dentry *dentry = file->f_dentry;
258 struct inode *inode = dentry->d_inode;
259 int status;
260
261 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
262 dentry->d_parent->d_name.name, dentry->d_name.name);
263
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000264 status = nfs_revalidate_file(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!status)
266 status = generic_file_mmap(file, vma);
267 return status;
268}
269
270/*
271 * Flush any dirty pages for this process, and check for write errors.
272 * The return status from this call provides a reliable indication of
273 * whether any write errors occurred for this process.
274 */
275static int
276nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
277{
278 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
279 struct inode *inode = dentry->d_inode;
280 int status;
281
282 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
283
284 lock_kernel();
285 status = nfs_wb_all(inode);
286 if (!status) {
287 status = ctx->error;
288 ctx->error = 0;
289 }
290 unlock_kernel();
291 return status;
292}
293
294/*
295 * This does the "real" work of the write. The generic routine has
296 * allocated the page, locked it, done all the page alignment stuff
297 * calculations etc. Now we should just copy the data from user
298 * space and write it back to the real medium..
299 *
300 * If the writer ends up delaying the write, the writer needs to
301 * increment the page use counts until he is done with the page.
302 */
303static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
304{
305 return nfs_flush_incompatible(file, page);
306}
307
308static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
309{
310 long status;
311
312 lock_kernel();
313 status = nfs_updatepage(file, page, offset, to-offset);
314 unlock_kernel();
315 return status;
316}
317
318struct address_space_operations nfs_file_aops = {
319 .readpage = nfs_readpage,
320 .readpages = nfs_readpages,
321 .set_page_dirty = __set_page_dirty_nobuffers,
322 .writepage = nfs_writepage,
323 .writepages = nfs_writepages,
324 .prepare_write = nfs_prepare_write,
325 .commit_write = nfs_commit_write,
326#ifdef CONFIG_NFS_DIRECTIO
327 .direct_IO = nfs_direct_IO,
328#endif
329};
330
331/*
332 * Write to a file (through the page cache).
333 */
334static ssize_t
335nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
336{
337 struct dentry * dentry = iocb->ki_filp->f_dentry;
338 struct inode * inode = dentry->d_inode;
339 ssize_t result;
340
341#ifdef CONFIG_NFS_DIRECTIO
342 if (iocb->ki_filp->f_flags & O_DIRECT)
343 return nfs_file_direct_write(iocb, buf, count, pos);
344#endif
345
346 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
347 dentry->d_parent->d_name.name, dentry->d_name.name,
348 inode->i_ino, (unsigned long) count, (unsigned long) pos);
349
350 result = -EBUSY;
351 if (IS_SWAPFILE(inode))
352 goto out_swapfile;
Trond Myklebust7d52e862005-06-22 17:16:30 +0000353 /*
354 * O_APPEND implies that we must revalidate the file length.
355 */
356 if (iocb->ki_filp->f_flags & O_APPEND) {
357 result = nfs_revalidate_file_size(inode, iocb->ki_filp);
358 if (result)
359 goto out;
Trond Myklebustfe51beec2005-06-22 17:16:30 +0000360 }
361 nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 result = count;
364 if (!count)
365 goto out;
366
367 result = generic_file_aio_write(iocb, buf, count, pos);
368out:
369 return result;
370
371out_swapfile:
372 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
373 goto out;
374}
375
376static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
377{
378 struct inode *inode = filp->f_mapping->host;
379 int status = 0;
380
381 lock_kernel();
382 /* Use local locking if mounted with "-onolock" */
383 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
384 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
385 else {
386 struct file_lock *cfl = posix_test_lock(filp, fl);
387
388 fl->fl_type = F_UNLCK;
389 if (cfl != NULL)
390 memcpy(fl, cfl, sizeof(*fl));
391 }
392 unlock_kernel();
393 return status;
394}
395
396static int do_vfs_lock(struct file *file, struct file_lock *fl)
397{
398 int res = 0;
399 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
400 case FL_POSIX:
401 res = posix_lock_file_wait(file, fl);
402 break;
403 case FL_FLOCK:
404 res = flock_lock_file_wait(file, fl);
405 break;
406 default:
407 BUG();
408 }
409 if (res < 0)
410 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
411 __FUNCTION__);
412 return res;
413}
414
415static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
416{
417 struct inode *inode = filp->f_mapping->host;
418 sigset_t oldset;
419 int status;
420
421 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
422 /*
423 * Flush all pending writes before doing anything
424 * with locks..
425 */
426 filemap_fdatawrite(filp->f_mapping);
427 down(&inode->i_sem);
428 nfs_wb_all(inode);
429 up(&inode->i_sem);
430 filemap_fdatawait(filp->f_mapping);
431
432 /* NOTE: special case
433 * If we're signalled while cleaning up locks on process exit, we
434 * still need to complete the unlock.
435 */
436 lock_kernel();
437 /* Use local locking if mounted with "-onolock" */
438 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
439 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
440 else
441 status = do_vfs_lock(filp, fl);
442 unlock_kernel();
443 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
444 return status;
445}
446
447static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
448{
449 struct inode *inode = filp->f_mapping->host;
450 sigset_t oldset;
451 int status;
452
453 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
454 /*
455 * Flush all pending writes before doing anything
456 * with locks..
457 */
458 status = filemap_fdatawrite(filp->f_mapping);
459 if (status == 0) {
460 down(&inode->i_sem);
461 status = nfs_wb_all(inode);
462 up(&inode->i_sem);
463 if (status == 0)
464 status = filemap_fdatawait(filp->f_mapping);
465 }
466 if (status < 0)
467 goto out;
468
469 lock_kernel();
470 /* Use local locking if mounted with "-onolock" */
471 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
472 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
473 /* If we were signalled we still need to ensure that
474 * we clean up any state on the server. We therefore
475 * record the lock call as having succeeded in order to
476 * ensure that locks_remove_posix() cleans it out when
477 * the process exits.
478 */
479 if (status == -EINTR || status == -ERESTARTSYS)
480 do_vfs_lock(filp, fl);
481 } else
482 status = do_vfs_lock(filp, fl);
483 unlock_kernel();
484 if (status < 0)
485 goto out;
486 /*
487 * Make sure we clear the cache whenever we try to get the lock.
488 * This makes locking act as a cache coherency point.
489 */
490 filemap_fdatawrite(filp->f_mapping);
491 down(&inode->i_sem);
492 nfs_wb_all(inode); /* we may have slept */
493 up(&inode->i_sem);
494 filemap_fdatawait(filp->f_mapping);
495 nfs_zap_caches(inode);
496out:
497 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
498 return status;
499}
500
501/*
502 * Lock a (portion of) a file
503 */
504static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
505{
506 struct inode * inode = filp->f_mapping->host;
507
508 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
509 inode->i_sb->s_id, inode->i_ino,
510 fl->fl_type, fl->fl_flags,
511 (long long)fl->fl_start, (long long)fl->fl_end);
512
513 if (!inode)
514 return -EINVAL;
515
516 /* No mandatory locks over NFS */
517 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
518 return -ENOLCK;
519
520 if (IS_GETLK(cmd))
521 return do_getlk(filp, cmd, fl);
522 if (fl->fl_type == F_UNLCK)
523 return do_unlk(filp, cmd, fl);
524 return do_setlk(filp, cmd, fl);
525}
526
527/*
528 * Lock a (portion of) a file
529 */
530static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
531{
532 struct inode * inode = filp->f_mapping->host;
533
534 dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
535 inode->i_sb->s_id, inode->i_ino,
536 fl->fl_type, fl->fl_flags);
537
538 if (!inode)
539 return -EINVAL;
540
541 /*
542 * No BSD flocks over NFS allowed.
543 * Note: we could try to fake a POSIX lock request here by
544 * using ((u32) filp | 0x80000000) or some such as the pid.
545 * Not sure whether that would be unique, though, or whether
546 * that would break in other places.
547 */
548 if (!(fl->fl_flags & FL_FLOCK))
549 return -ENOLCK;
550
551 /* We're simulating flock() locks using posix locks on the server */
552 fl->fl_owner = (fl_owner_t)filp;
553 fl->fl_start = 0;
554 fl->fl_end = OFFSET_MAX;
555
556 if (fl->fl_type == F_UNLCK)
557 return do_unlk(filp, cmd, fl);
558 return do_setlk(filp, cmd, fl);
559}