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 int nfs_file_mmap(struct file
*, struct vm_area_struct
*);
41 static ssize_t
nfs_file_sendfile(struct file
*, loff_t
*, size_t, read_actor_t
, void *);
42 static ssize_t
nfs_file_read(struct kiocb
*, char __user
*, size_t, loff_t
);
43 static ssize_t
nfs_file_write(struct kiocb
*, const char __user
*, size_t, loff_t
);
44 static int nfs_file_flush(struct file
*);
45 static int nfs_fsync(struct file
*, struct dentry
*dentry
, int datasync
);
46 static int nfs_check_flags(int flags
);
47 static int nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
);
48 static int nfs_flock(struct file
*filp
, int cmd
, struct file_lock
*fl
);
50 struct file_operations nfs_file_operations
= {
51 .llseek
= remote_llseek
,
53 .write
= do_sync_write
,
54 .aio_read
= nfs_file_read
,
55 .aio_write
= nfs_file_write
,
56 .mmap
= nfs_file_mmap
,
57 .open
= nfs_file_open
,
58 .flush
= nfs_file_flush
,
59 .release
= nfs_file_release
,
63 .sendfile
= nfs_file_sendfile
,
64 .check_flags
= nfs_check_flags
,
67 struct inode_operations nfs_file_inode_operations
= {
68 .permission
= nfs_permission
,
69 .getattr
= nfs_getattr
,
70 .setattr
= nfs_setattr
,
73 /* Hack for future NFS swap support */
75 # define IS_SWAPFILE(inode) (0)
78 static int nfs_check_flags(int flags
)
80 if ((flags
& (O_APPEND
| O_DIRECT
)) == (O_APPEND
| O_DIRECT
))
90 nfs_file_open(struct inode
*inode
, struct file
*filp
)
92 struct nfs_server
*server
= NFS_SERVER(inode
);
93 int (*open
)(struct inode
*, struct file
*);
96 res
= nfs_check_flags(filp
->f_flags
);
101 /* Do NFSv4 open() call */
102 if ((open
= server
->rpc_ops
->file_open
) != NULL
)
103 res
= open(inode
, filp
);
109 nfs_file_release(struct inode
*inode
, struct file
*filp
)
111 /* Ensure that dirty pages are flushed out with the right creds */
112 if (filp
->f_mode
& FMODE_WRITE
)
113 filemap_fdatawrite(filp
->f_mapping
);
114 return NFS_PROTO(inode
)->file_release(inode
, filp
);
118 * Flush all dirty pages, and check for write errors.
122 nfs_file_flush(struct file
*file
)
124 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
125 struct inode
*inode
= file
->f_dentry
->d_inode
;
128 dfprintk(VFS
, "nfs: flush(%s/%ld)\n", inode
->i_sb
->s_id
, inode
->i_ino
);
130 if ((file
->f_mode
& FMODE_WRITE
) == 0)
133 /* Ensure that data+attribute caches are up to date after close() */
134 status
= nfs_wb_all(inode
);
138 if (!status
&& !nfs_have_delegation(inode
, FMODE_READ
))
139 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
146 nfs_file_read(struct kiocb
*iocb
, char __user
* buf
, size_t count
, loff_t pos
)
148 struct dentry
* dentry
= iocb
->ki_filp
->f_dentry
;
149 struct inode
* inode
= dentry
->d_inode
;
152 #ifdef CONFIG_NFS_DIRECTIO
153 if (iocb
->ki_filp
->f_flags
& O_DIRECT
)
154 return nfs_file_direct_read(iocb
, buf
, count
, pos
);
157 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
158 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
159 (unsigned long) count
, (unsigned long) pos
);
161 result
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
163 result
= generic_file_aio_read(iocb
, buf
, count
, pos
);
168 nfs_file_sendfile(struct file
*filp
, loff_t
*ppos
, size_t count
,
169 read_actor_t actor
, void *target
)
171 struct dentry
*dentry
= filp
->f_dentry
;
172 struct inode
*inode
= dentry
->d_inode
;
175 dfprintk(VFS
, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
176 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
177 (unsigned long) count
, (unsigned long long) *ppos
);
179 res
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
181 res
= generic_file_sendfile(filp
, ppos
, count
, actor
, target
);
186 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
188 struct dentry
*dentry
= file
->f_dentry
;
189 struct inode
*inode
= dentry
->d_inode
;
192 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
193 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
195 status
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
197 status
= generic_file_mmap(file
, vma
);
202 * Flush any dirty pages for this process, and check for write errors.
203 * The return status from this call provides a reliable indication of
204 * whether any write errors occurred for this process.
207 nfs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
209 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
210 struct inode
*inode
= dentry
->d_inode
;
213 dfprintk(VFS
, "nfs: fsync(%s/%ld)\n", inode
->i_sb
->s_id
, inode
->i_ino
);
216 status
= nfs_wb_all(inode
);
226 * This does the "real" work of the write. The generic routine has
227 * allocated the page, locked it, done all the page alignment stuff
228 * calculations etc. Now we should just copy the data from user
229 * space and write it back to the real medium..
231 * If the writer ends up delaying the write, the writer needs to
232 * increment the page use counts until he is done with the page.
234 static int nfs_prepare_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
236 return nfs_flush_incompatible(file
, page
);
239 static int nfs_commit_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
244 status
= nfs_updatepage(file
, page
, offset
, to
-offset
);
249 struct address_space_operations nfs_file_aops
= {
250 .readpage
= nfs_readpage
,
251 .readpages
= nfs_readpages
,
252 .set_page_dirty
= __set_page_dirty_nobuffers
,
253 .writepage
= nfs_writepage
,
254 .writepages
= nfs_writepages
,
255 .prepare_write
= nfs_prepare_write
,
256 .commit_write
= nfs_commit_write
,
257 #ifdef CONFIG_NFS_DIRECTIO
258 .direct_IO
= nfs_direct_IO
,
263 * Write to a file (through the page cache).
266 nfs_file_write(struct kiocb
*iocb
, const char __user
*buf
, size_t count
, loff_t pos
)
268 struct dentry
* dentry
= iocb
->ki_filp
->f_dentry
;
269 struct inode
* inode
= dentry
->d_inode
;
272 #ifdef CONFIG_NFS_DIRECTIO
273 if (iocb
->ki_filp
->f_flags
& O_DIRECT
)
274 return nfs_file_direct_write(iocb
, buf
, count
, pos
);
277 dfprintk(VFS
, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
278 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
279 inode
->i_ino
, (unsigned long) count
, (unsigned long) pos
);
282 if (IS_SWAPFILE(inode
))
284 result
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
292 result
= generic_file_aio_write(iocb
, buf
, count
, pos
);
297 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
301 static int do_getlk(struct file
*filp
, int cmd
, struct file_lock
*fl
)
303 struct inode
*inode
= filp
->f_mapping
->host
;
307 /* Use local locking if mounted with "-onolock" */
308 if (!(NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
))
309 status
= NFS_PROTO(inode
)->lock(filp
, cmd
, fl
);
311 struct file_lock
*cfl
= posix_test_lock(filp
, fl
);
313 fl
->fl_type
= F_UNLCK
;
315 memcpy(fl
, cfl
, sizeof(*fl
));
321 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
324 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
326 res
= posix_lock_file_wait(file
, fl
);
329 res
= flock_lock_file_wait(file
, fl
);
335 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
340 static int do_unlk(struct file
*filp
, int cmd
, struct file_lock
*fl
)
342 struct inode
*inode
= filp
->f_mapping
->host
;
346 rpc_clnt_sigmask(NFS_CLIENT(inode
), &oldset
);
348 * Flush all pending writes before doing anything
351 filemap_fdatawrite(filp
->f_mapping
);
355 filemap_fdatawait(filp
->f_mapping
);
357 /* NOTE: special case
358 * If we're signalled while cleaning up locks on process exit, we
359 * still need to complete the unlock.
362 /* Use local locking if mounted with "-onolock" */
363 if (!(NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
))
364 status
= NFS_PROTO(inode
)->lock(filp
, cmd
, fl
);
366 status
= do_vfs_lock(filp
, fl
);
368 rpc_clnt_sigunmask(NFS_CLIENT(inode
), &oldset
);
372 static int do_setlk(struct file
*filp
, int cmd
, struct file_lock
*fl
)
374 struct inode
*inode
= filp
->f_mapping
->host
;
378 rpc_clnt_sigmask(NFS_CLIENT(inode
), &oldset
);
380 * Flush all pending writes before doing anything
383 status
= filemap_fdatawrite(filp
->f_mapping
);
386 status
= nfs_wb_all(inode
);
389 status
= filemap_fdatawait(filp
->f_mapping
);
395 /* Use local locking if mounted with "-onolock" */
396 if (!(NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
)) {
397 status
= NFS_PROTO(inode
)->lock(filp
, cmd
, fl
);
398 /* If we were signalled we still need to ensure that
399 * we clean up any state on the server. We therefore
400 * record the lock call as having succeeded in order to
401 * ensure that locks_remove_posix() cleans it out when
404 if (status
== -EINTR
|| status
== -ERESTARTSYS
)
405 do_vfs_lock(filp
, fl
);
407 status
= do_vfs_lock(filp
, fl
);
412 * Make sure we clear the cache whenever we try to get the lock.
413 * This makes locking act as a cache coherency point.
415 filemap_fdatawrite(filp
->f_mapping
);
417 nfs_wb_all(inode
); /* we may have slept */
419 filemap_fdatawait(filp
->f_mapping
);
420 nfs_zap_caches(inode
);
422 rpc_clnt_sigunmask(NFS_CLIENT(inode
), &oldset
);
427 * Lock a (portion of) a file
429 static int nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
431 struct inode
* inode
= filp
->f_mapping
->host
;
433 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
434 inode
->i_sb
->s_id
, inode
->i_ino
,
435 fl
->fl_type
, fl
->fl_flags
,
436 (long long)fl
->fl_start
, (long long)fl
->fl_end
);
441 /* No mandatory locks over NFS */
442 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
446 return do_getlk(filp
, cmd
, fl
);
447 if (fl
->fl_type
== F_UNLCK
)
448 return do_unlk(filp
, cmd
, fl
);
449 return do_setlk(filp
, cmd
, fl
);
453 * Lock a (portion of) a file
455 static int nfs_flock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
457 struct inode
* inode
= filp
->f_mapping
->host
;
459 dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
460 inode
->i_sb
->s_id
, inode
->i_ino
,
461 fl
->fl_type
, fl
->fl_flags
);
467 * No BSD flocks over NFS allowed.
468 * Note: we could try to fake a POSIX lock request here by
469 * using ((u32) filp | 0x80000000) or some such as the pid.
470 * Not sure whether that would be unique, though, or whether
471 * that would break in other places.
473 if (!(fl
->fl_flags
& FL_FLOCK
))
476 /* We're simulating flock() locks using posix locks on the server */
477 fl
->fl_owner
= (fl_owner_t
)filp
;
479 fl
->fl_end
= OFFSET_MAX
;
481 if (fl
->fl_type
== F_UNLCK
)
482 return do_unlk(filp
, cmd
, fl
);
483 return do_setlk(filp
, cmd
, fl
);