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 #define NFSDBG_FACILITY NFSDBG_FILE
36 static long nfs_file_fcntl(int fd, unsigned int cmd,
37 unsigned long arg, struct file *filp);
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);
47 struct file_operations nfs_file_operations = {
48 .llseek = remote_llseek,
50 .write = do_sync_write,
51 .aio_read = nfs_file_read,
52 .aio_write = nfs_file_write,
53 .mmap = nfs_file_mmap,
54 .open = nfs_file_open,
55 .flush = nfs_file_flush,
56 .release = nfs_file_release,
59 .sendfile = nfs_file_sendfile,
60 .fcntl = nfs_file_fcntl,
63 struct inode_operations nfs_file_inode_operations = {
64 .permission = nfs_permission,
65 .getattr = nfs_getattr,
66 .setattr = nfs_setattr,
69 /* Hack for future NFS swap support */
71 # define IS_SWAPFILE(inode) (0)
74 #define nfs_invalid_flags (O_APPEND | O_DIRECT)
77 * Check for special cases that NFS doesn't support, and
78 * pass the rest to the generic fcntl function.
81 nfs_file_fcntl(int fd, unsigned int cmd,
82 unsigned long arg, struct file *filp)
86 if ((filp->f_flags & nfs_invalid_flags) == nfs_invalid_flags)
93 return generic_file_fcntl(fd, cmd, arg, filp);
100 nfs_file_open(struct inode *inode, struct file *filp)
102 struct nfs_server *server = NFS_SERVER(inode);
103 int (*open)(struct inode *, struct file *);
106 if ((filp->f_flags & nfs_invalid_flags) == nfs_invalid_flags)
110 /* Do NFSv4 open() call */
111 if ((open = server->rpc_ops->file_open) != NULL)
112 res = open(inode, filp);
118 nfs_file_release(struct inode *inode, struct file *filp)
120 return NFS_PROTO(inode)->file_release(inode, filp);
124 * Flush all dirty pages, and check for write errors.
128 nfs_file_flush(struct file *file)
130 struct inode *inode = file->f_dentry->d_inode;
133 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
135 if ((file->f_mode & FMODE_WRITE) == 0)
138 /* Ensure that data+attribute caches are up to date after close() */
139 status = nfs_wb_all(inode);
141 status = file->f_error;
144 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
151 nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
153 struct dentry * dentry = iocb->ki_filp->f_dentry;
154 struct inode * inode = dentry->d_inode;
157 #ifdef CONFIG_NFS_DIRECTIO
158 if (iocb->ki_filp->f_flags & O_DIRECT)
159 return nfs_file_direct_read(iocb, buf, count, pos);
162 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
163 dentry->d_parent->d_name.name, dentry->d_name.name,
164 (unsigned long) count, (unsigned long) pos);
166 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
168 result = generic_file_aio_read(iocb, buf, count, pos);
173 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
174 read_actor_t actor, void *target)
176 struct dentry *dentry = filp->f_dentry;
177 struct inode *inode = dentry->d_inode;
180 dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
181 dentry->d_parent->d_name.name, dentry->d_name.name,
182 (unsigned long) count, (unsigned long long) *ppos);
184 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
186 res = generic_file_sendfile(filp, ppos, count, actor, target);
191 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
193 struct dentry *dentry = file->f_dentry;
194 struct inode *inode = dentry->d_inode;
197 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
198 dentry->d_parent->d_name.name, dentry->d_name.name);
200 status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
202 status = generic_file_mmap(file, vma);
207 * Flush any dirty pages for this process, and check for write errors.
208 * The return status from this call provides a reliable indication of
209 * whether any write errors occurred for this process.
212 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
214 struct inode *inode = dentry->d_inode;
217 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
220 status = nfs_wb_all(inode);
222 status = file->f_error;
230 * This does the "real" work of the write. The generic routine has
231 * allocated the page, locked it, done all the page alignment stuff
232 * calculations etc. Now we should just copy the data from user
233 * space and write it back to the real medium..
235 * If the writer ends up delaying the write, the writer needs to
236 * increment the page use counts until he is done with the page.
238 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
240 return nfs_flush_incompatible(file, page);
243 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
248 status = nfs_updatepage(file, page, offset, to-offset);
253 struct address_space_operations nfs_file_aops = {
254 .readpage = nfs_readpage,
255 .readpages = nfs_readpages,
256 .set_page_dirty = __set_page_dirty_nobuffers,
257 .writepage = nfs_writepage,
258 .writepages = nfs_writepages,
259 .prepare_write = nfs_prepare_write,
260 .commit_write = nfs_commit_write,
261 #ifdef CONFIG_NFS_DIRECTIO
262 .direct_IO = nfs_direct_IO,
267 * Write to a file (through the page cache).
270 nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
272 struct dentry * dentry = iocb->ki_filp->f_dentry;
273 struct inode * inode = dentry->d_inode;
276 #ifdef CONFIG_NFS_DIRECTIO
277 if (iocb->ki_filp->f_flags & O_DIRECT)
278 return nfs_file_direct_write(iocb, buf, count, pos);
281 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
282 dentry->d_parent->d_name.name, dentry->d_name.name,
283 inode->i_ino, (unsigned long) count, (unsigned long) pos);
286 if (IS_SWAPFILE(inode))
288 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
296 result = generic_file_aio_write(iocb, buf, count, pos);
301 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
306 * Lock a (portion of) a file
309 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
311 struct inode * inode = filp->f_mapping->host;
315 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
316 inode->i_sb->s_id, inode->i_ino,
317 fl->fl_type, fl->fl_flags,
318 (long long)fl->fl_start, (long long)fl->fl_end);
323 /* No mandatory locks over NFS */
324 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
327 if (NFS_PROTO(inode)->version != 4) {
328 /* Fake OK code if mounted without NLM support */
329 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
331 status = LOCK_USE_CLNT;
337 * No BSD flocks over NFS allowed.
338 * Note: we could try to fake a POSIX lock request here by
339 * using ((u32) filp | 0x80000000) or some such as the pid.
340 * Not sure whether that would be unique, though, or whether
341 * that would break in other places.
343 if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
347 * Flush all pending writes before doing anything
350 status = filemap_fdatawrite(filp->f_mapping);
352 status2 = nfs_wb_all(inode);
356 status2 = filemap_fdatawait(filp->f_mapping);
363 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
371 * Make sure we clear the cache whenever we try to get the lock.
372 * This makes locking act as a cache coherency point.
375 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
376 filemap_fdatawrite(filp->f_mapping);
378 nfs_wb_all(inode); /* we may have slept */
380 filemap_fdatawait(filp->f_mapping);
381 nfs_zap_caches(inode);