4 * Copyright (C) 1992 Rick Sladkey
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.
12 * Expire cache on write to a file by Wai S Kok (Oct 1994).
14 * Total rewrite of read side for new NFS buffer cache.. Linus.
16 * nfs regular file handling functions
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>
27 #include <linux/slab.h>
28 #include <linux/pagemap.h>
29 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
34 #include "delegation.h"
36 #define NFSDBG_FACILITY NFSDBG_FILE
38 static int nfs_file_open(struct inode
*, struct file
*);
39 static int nfs_file_release(struct inode
*, struct file
*);
40 static loff_t
nfs_file_llseek(struct file
*file
, loff_t offset
, int origin
);
41 static int nfs_file_mmap(struct file
*, struct vm_area_struct
*);
42 static ssize_t
nfs_file_sendfile(struct file
*, loff_t
*, size_t, read_actor_t
, void *);
43 static ssize_t
nfs_file_read(struct kiocb
*, char __user
*, size_t, loff_t
);
44 static ssize_t
nfs_file_write(struct kiocb
*, const char __user
*, size_t, loff_t
);
45 static int nfs_file_flush(struct file
*);
46 static int nfs_fsync(struct file
*, struct dentry
*dentry
, int datasync
);
47 static int nfs_check_flags(int flags
);
48 static int nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
);
49 static int nfs_flock(struct file
*filp
, int cmd
, struct file_lock
*fl
);
51 struct file_operations nfs_file_operations
= {
52 .llseek
= nfs_file_llseek
,
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
,
64 .sendfile
= nfs_file_sendfile
,
65 .check_flags
= nfs_check_flags
,
68 struct inode_operations nfs_file_inode_operations
= {
69 .permission
= nfs_permission
,
70 .getattr
= nfs_getattr
,
71 .setattr
= nfs_setattr
,
75 struct 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
,
84 #endif /* CONFIG_NFS_v3 */
86 /* Hack for future NFS swap support */
88 # define IS_SWAPFILE(inode) (0)
91 static int nfs_check_flags(int flags
)
93 if ((flags
& (O_APPEND
| O_DIRECT
)) == (O_APPEND
| O_DIRECT
))
103 nfs_file_open(struct inode
*inode
, struct file
*filp
)
105 struct nfs_server
*server
= NFS_SERVER(inode
);
106 int (*open
)(struct inode
*, struct file
*);
109 res
= nfs_check_flags(filp
->f_flags
);
114 /* Do NFSv4 open() call */
115 if ((open
= server
->rpc_ops
->file_open
) != NULL
)
116 res
= open(inode
, filp
);
122 nfs_file_release(struct inode
*inode
, struct file
*filp
)
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
);
131 * nfs_revalidate_size - Revalidate the file size
132 * @inode - pointer to inode struct
133 * @file - pointer to struct file
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).
141 static int nfs_revalidate_file_size(struct inode
*inode
, struct file
*filp
)
143 struct nfs_server
*server
= NFS_SERVER(inode
);
144 struct nfs_inode
*nfsi
= NFS_I(inode
);
146 if (server
->flags
& NFS_MOUNT_NOAC
)
148 if (filp
->f_flags
& O_DIRECT
)
150 if (nfsi
->npages
!= 0)
152 return nfs_revalidate_inode(server
, inode
);
154 return __nfs_revalidate_inode(server
, inode
);
157 static loff_t
nfs_file_llseek(struct file
*filp
, loff_t offset
, int origin
)
159 /* origin == SEEK_END => we must revalidate the cached file length */
161 struct inode
*inode
= filp
->f_mapping
->host
;
162 int retval
= nfs_revalidate_file_size(inode
, filp
);
164 return (loff_t
)retval
;
166 return remote_llseek(filp
, offset
, origin
);
170 * Flush all dirty pages, and check for write errors.
174 nfs_file_flush(struct file
*file
)
176 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
177 struct inode
*inode
= file
->f_dentry
->d_inode
;
180 dfprintk(VFS
, "nfs: flush(%s/%ld)\n", inode
->i_sb
->s_id
, inode
->i_ino
);
182 if ((file
->f_mode
& FMODE_WRITE
) == 0)
185 /* Ensure that data+attribute caches are up to date after close() */
186 status
= nfs_wb_all(inode
);
190 if (!status
&& !nfs_have_delegation(inode
, FMODE_READ
))
191 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
198 nfs_file_read(struct kiocb
*iocb
, char __user
* buf
, size_t count
, loff_t pos
)
200 struct dentry
* dentry
= iocb
->ki_filp
->f_dentry
;
201 struct inode
* inode
= dentry
->d_inode
;
204 #ifdef CONFIG_NFS_DIRECTIO
205 if (iocb
->ki_filp
->f_flags
& O_DIRECT
)
206 return nfs_file_direct_read(iocb
, buf
, count
, pos
);
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
);
213 result
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
215 result
= generic_file_aio_read(iocb
, buf
, count
, pos
);
220 nfs_file_sendfile(struct file
*filp
, loff_t
*ppos
, size_t count
,
221 read_actor_t actor
, void *target
)
223 struct dentry
*dentry
= filp
->f_dentry
;
224 struct inode
*inode
= dentry
->d_inode
;
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
);
231 res
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
233 res
= generic_file_sendfile(filp
, ppos
, count
, actor
, target
);
238 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
240 struct dentry
*dentry
= file
->f_dentry
;
241 struct inode
*inode
= dentry
->d_inode
;
244 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
245 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
247 status
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
249 status
= generic_file_mmap(file
, vma
);
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.
259 nfs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
261 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
262 struct inode
*inode
= dentry
->d_inode
;
265 dfprintk(VFS
, "nfs: fsync(%s/%ld)\n", inode
->i_sb
->s_id
, inode
->i_ino
);
268 status
= nfs_wb_all(inode
);
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..
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.
286 static int nfs_prepare_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
288 return nfs_flush_incompatible(file
, page
);
291 static int nfs_commit_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
296 status
= nfs_updatepage(file
, page
, offset
, to
-offset
);
301 struct 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
,
315 * Write to a file (through the page cache).
318 nfs_file_write(struct kiocb
*iocb
, const char __user
*buf
, size_t count
, loff_t pos
)
320 struct dentry
* dentry
= iocb
->ki_filp
->f_dentry
;
321 struct inode
* inode
= dentry
->d_inode
;
324 #ifdef CONFIG_NFS_DIRECTIO
325 if (iocb
->ki_filp
->f_flags
& O_DIRECT
)
326 return nfs_file_direct_write(iocb
, buf
, count
, pos
);
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
);
334 if (IS_SWAPFILE(inode
))
336 result
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
344 result
= generic_file_aio_write(iocb
, buf
, count
, pos
);
349 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
353 static int do_getlk(struct file
*filp
, int cmd
, struct file_lock
*fl
)
355 struct inode
*inode
= filp
->f_mapping
->host
;
359 /* Use local locking if mounted with "-onolock" */
360 if (!(NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
))
361 status
= NFS_PROTO(inode
)->lock(filp
, cmd
, fl
);
363 struct file_lock
*cfl
= posix_test_lock(filp
, fl
);
365 fl
->fl_type
= F_UNLCK
;
367 memcpy(fl
, cfl
, sizeof(*fl
));
373 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
376 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
378 res
= posix_lock_file_wait(file
, fl
);
381 res
= flock_lock_file_wait(file
, fl
);
387 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
392 static int do_unlk(struct file
*filp
, int cmd
, struct file_lock
*fl
)
394 struct inode
*inode
= filp
->f_mapping
->host
;
398 rpc_clnt_sigmask(NFS_CLIENT(inode
), &oldset
);
400 * Flush all pending writes before doing anything
403 filemap_fdatawrite(filp
->f_mapping
);
407 filemap_fdatawait(filp
->f_mapping
);
409 /* NOTE: special case
410 * If we're signalled while cleaning up locks on process exit, we
411 * still need to complete the unlock.
414 /* Use local locking if mounted with "-onolock" */
415 if (!(NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
))
416 status
= NFS_PROTO(inode
)->lock(filp
, cmd
, fl
);
418 status
= do_vfs_lock(filp
, fl
);
420 rpc_clnt_sigunmask(NFS_CLIENT(inode
), &oldset
);
424 static int do_setlk(struct file
*filp
, int cmd
, struct file_lock
*fl
)
426 struct inode
*inode
= filp
->f_mapping
->host
;
430 rpc_clnt_sigmask(NFS_CLIENT(inode
), &oldset
);
432 * Flush all pending writes before doing anything
435 status
= filemap_fdatawrite(filp
->f_mapping
);
438 status
= nfs_wb_all(inode
);
441 status
= filemap_fdatawait(filp
->f_mapping
);
447 /* Use local locking if mounted with "-onolock" */
448 if (!(NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
)) {
449 status
= NFS_PROTO(inode
)->lock(filp
, cmd
, fl
);
450 /* If we were signalled we still need to ensure that
451 * we clean up any state on the server. We therefore
452 * record the lock call as having succeeded in order to
453 * ensure that locks_remove_posix() cleans it out when
456 if (status
== -EINTR
|| status
== -ERESTARTSYS
)
457 do_vfs_lock(filp
, fl
);
459 status
= do_vfs_lock(filp
, fl
);
464 * Make sure we clear the cache whenever we try to get the lock.
465 * This makes locking act as a cache coherency point.
467 filemap_fdatawrite(filp
->f_mapping
);
469 nfs_wb_all(inode
); /* we may have slept */
471 filemap_fdatawait(filp
->f_mapping
);
472 nfs_zap_caches(inode
);
474 rpc_clnt_sigunmask(NFS_CLIENT(inode
), &oldset
);
479 * Lock a (portion of) a file
481 static int nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
483 struct inode
* inode
= filp
->f_mapping
->host
;
485 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
486 inode
->i_sb
->s_id
, inode
->i_ino
,
487 fl
->fl_type
, fl
->fl_flags
,
488 (long long)fl
->fl_start
, (long long)fl
->fl_end
);
493 /* No mandatory locks over NFS */
494 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
498 return do_getlk(filp
, cmd
, fl
);
499 if (fl
->fl_type
== F_UNLCK
)
500 return do_unlk(filp
, cmd
, fl
);
501 return do_setlk(filp
, cmd
, fl
);
505 * Lock a (portion of) a file
507 static int nfs_flock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
509 struct inode
* inode
= filp
->f_mapping
->host
;
511 dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
512 inode
->i_sb
->s_id
, inode
->i_ino
,
513 fl
->fl_type
, fl
->fl_flags
);
519 * No BSD flocks over NFS allowed.
520 * Note: we could try to fake a POSIX lock request here by
521 * using ((u32) filp | 0x80000000) or some such as the pid.
522 * Not sure whether that would be unique, though, or whether
523 * that would break in other places.
525 if (!(fl
->fl_flags
& FL_FLOCK
))
528 /* We're simulating flock() locks using posix locks on the server */
529 fl
->fl_owner
= (fl_owner_t
)filp
;
531 fl
->fl_end
= OFFSET_MAX
;
533 if (fl
->fl_type
== F_UNLCK
)
534 return do_unlk(filp
, cmd
, fl
);
535 return do_setlk(filp
, cmd
, fl
);