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