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);
48 struct file_operations nfs_file_operations = {
49 .llseek = remote_llseek,
51 .write = do_sync_write,
52 .aio_read = nfs_file_read,
53 .aio_write = nfs_file_write,
54 .mmap = nfs_file_mmap,
55 .open = nfs_file_open,
56 .flush = nfs_file_flush,
57 .release = nfs_file_release,
60 .sendfile = nfs_file_sendfile,
61 .check_flags = nfs_check_flags,
64 struct inode_operations nfs_file_inode_operations = {
65 .permission = nfs_permission,
66 .getattr = nfs_getattr,
67 .setattr = nfs_setattr,
70 /* Hack for future NFS swap support */
72 # define IS_SWAPFILE(inode) (0)
75 static int nfs_check_flags(int flags)
77 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
87 nfs_file_open(struct inode *inode, struct file *filp)
89 struct nfs_server *server = NFS_SERVER(inode);
90 int (*open)(struct inode *, struct file *);
93 res = nfs_check_flags(filp->f_flags);
98 /* Do NFSv4 open() call */
99 if ((open = server->rpc_ops->file_open) != NULL)
100 res = open(inode, filp);
106 nfs_file_release(struct inode *inode, struct file *filp)
108 return NFS_PROTO(inode)->file_release(inode, filp);
112 * Flush all dirty pages, and check for write errors.
116 nfs_file_flush(struct file *file)
118 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
119 struct inode *inode = file->f_dentry->d_inode;
122 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
124 if ((file->f_mode & FMODE_WRITE) == 0)
127 /* Ensure that data+attribute caches are up to date after close() */
128 status = nfs_wb_all(inode);
132 if (!status && !nfs_have_delegation(inode, FMODE_READ))
133 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
140 nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
142 struct dentry * dentry = iocb->ki_filp->f_dentry;
143 struct inode * inode = dentry->d_inode;
146 #ifdef CONFIG_NFS_DIRECTIO
147 if (iocb->ki_filp->f_flags & O_DIRECT)
148 return nfs_file_direct_read(iocb, buf, count, pos);
151 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
152 dentry->d_parent->d_name.name, dentry->d_name.name,
153 (unsigned long) count, (unsigned long) pos);
155 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
157 result = generic_file_aio_read(iocb, buf, count, pos);
162 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
163 read_actor_t actor, void *target)
165 struct dentry *dentry = filp->f_dentry;
166 struct inode *inode = dentry->d_inode;
169 dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
170 dentry->d_parent->d_name.name, dentry->d_name.name,
171 (unsigned long) count, (unsigned long long) *ppos);
173 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
175 res = generic_file_sendfile(filp, ppos, count, actor, target);
180 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
182 struct dentry *dentry = file->f_dentry;
183 struct inode *inode = dentry->d_inode;
186 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
187 dentry->d_parent->d_name.name, dentry->d_name.name);
189 status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
191 status = generic_file_mmap(file, vma);
196 * Flush any dirty pages for this process, and check for write errors.
197 * The return status from this call provides a reliable indication of
198 * whether any write errors occurred for this process.
201 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
203 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
204 struct inode *inode = dentry->d_inode;
207 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
210 status = nfs_wb_all(inode);
220 * This does the "real" work of the write. The generic routine has
221 * allocated the page, locked it, done all the page alignment stuff
222 * calculations etc. Now we should just copy the data from user
223 * space and write it back to the real medium..
225 * If the writer ends up delaying the write, the writer needs to
226 * increment the page use counts until he is done with the page.
228 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
230 return nfs_flush_incompatible(file, page);
233 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
238 status = nfs_updatepage(file, page, offset, to-offset);
243 struct address_space_operations nfs_file_aops = {
244 .readpage = nfs_readpage,
245 .readpages = nfs_readpages,
246 .set_page_dirty = __set_page_dirty_nobuffers,
247 .writepage = nfs_writepage,
248 .writepages = nfs_writepages,
249 .prepare_write = nfs_prepare_write,
250 .commit_write = nfs_commit_write,
251 #ifdef CONFIG_NFS_DIRECTIO
252 .direct_IO = nfs_direct_IO,
257 * Write to a file (through the page cache).
260 nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
262 struct dentry * dentry = iocb->ki_filp->f_dentry;
263 struct inode * inode = dentry->d_inode;
266 #ifdef CONFIG_NFS_DIRECTIO
267 if (iocb->ki_filp->f_flags & O_DIRECT)
268 return nfs_file_direct_write(iocb, buf, count, pos);
271 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
272 dentry->d_parent->d_name.name, dentry->d_name.name,
273 inode->i_ino, (unsigned long) count, (unsigned long) pos);
276 if (IS_SWAPFILE(inode))
278 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
286 result = generic_file_aio_write(iocb, buf, count, pos);
291 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
295 static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
297 struct inode *inode = filp->f_mapping->host;
301 /* Use local locking if mounted with "-onolock" */
302 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
303 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
305 struct file_lock *cfl = posix_test_lock(filp, fl);
307 fl->fl_type = F_UNLCK;
309 memcpy(fl, cfl, sizeof(*fl));
315 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
317 struct inode *inode = filp->f_mapping->host;
321 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
323 * Flush all pending writes before doing anything
326 filemap_fdatawrite(filp->f_mapping);
330 filemap_fdatawait(filp->f_mapping);
332 /* NOTE: special case
333 * If we're signalled while cleaning up locks on process exit, we
334 * still need to complete the unlock.
337 /* Use local locking if mounted with "-onolock" */
338 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
339 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
341 status = posix_lock_file_wait(filp, fl);
343 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
347 static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
349 struct inode *inode = filp->f_mapping->host;
353 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
355 * Flush all pending writes before doing anything
358 status = filemap_fdatawrite(filp->f_mapping);
361 status = nfs_wb_all(inode);
364 status = filemap_fdatawait(filp->f_mapping);
370 /* Use local locking if mounted with "-onolock" */
371 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
372 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
373 /* If we were signalled we still need to ensure that
374 * we clean up any state on the server. We therefore
375 * record the lock call as having succeeded in order to
376 * ensure that locks_remove_posix() cleans it out when
379 if (status == -EINTR || status == -ERESTARTSYS)
380 posix_lock_file_wait(filp, fl);
382 status = posix_lock_file_wait(filp, fl);
387 * Make sure we clear the cache whenever we try to get the lock.
388 * This makes locking act as a cache coherency point.
390 filemap_fdatawrite(filp->f_mapping);
392 nfs_wb_all(inode); /* we may have slept */
394 filemap_fdatawait(filp->f_mapping);
395 nfs_zap_caches(inode);
397 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
402 * Lock a (portion of) a file
405 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
407 struct inode * inode = filp->f_mapping->host;
409 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
410 inode->i_sb->s_id, inode->i_ino,
411 fl->fl_type, fl->fl_flags,
412 (long long)fl->fl_start, (long long)fl->fl_end);
417 /* No mandatory locks over NFS */
418 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
422 * No BSD flocks over NFS allowed.
423 * Note: we could try to fake a POSIX lock request here by
424 * using ((u32) filp | 0x80000000) or some such as the pid.
425 * Not sure whether that would be unique, though, or whether
426 * that would break in other places.
428 if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
432 return do_getlk(filp, cmd, fl);
433 if (fl->fl_type == F_UNLCK)
434 return do_unlk(filp, cmd, fl);
435 return do_setlk(filp, cmd, fl);