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 *, size_t, loff_t);
43 static ssize_t nfs_file_write(struct kiocb *, const char *, 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 * buf, size_t count, loff_t pos)
153 struct dentry * dentry = iocb->ki_filp->f_dentry;
154 struct inode * inode = dentry->d_inode;
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 inode *inode = dentry->d_inode;
212 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
215 status = nfs_wb_all(inode);
217 status = file->f_error;
225 * This does the "real" work of the write. The generic routine has
226 * allocated the page, locked it, done all the page alignment stuff
227 * calculations etc. Now we should just copy the data from user
228 * space and write it back to the real medium..
230 * If the writer ends up delaying the write, the writer needs to
231 * increment the page use counts until he is done with the page.
233 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
235 return nfs_flush_incompatible(file, page);
238 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
243 status = nfs_updatepage(file, page, offset, to-offset);
248 struct address_space_operations nfs_file_aops = {
249 .readpage = nfs_readpage,
250 .readpages = nfs_readpages,
251 .set_page_dirty = __set_page_dirty_nobuffers,
252 .writepage = nfs_writepage,
253 .writepages = nfs_writepages,
254 .prepare_write = nfs_prepare_write,
255 .commit_write = nfs_commit_write,
256 #ifdef CONFIG_NFS_DIRECTIO
257 .direct_IO = nfs_direct_IO,
262 * Write to a file (through the page cache).
265 nfs_file_write(struct kiocb *iocb, const char *buf, size_t count, loff_t pos)
267 struct dentry * dentry = iocb->ki_filp->f_dentry;
268 struct inode * inode = dentry->d_inode;
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");
296 * Lock a (portion of) a file
299 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
301 struct inode * inode = filp->f_mapping->host;
305 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
306 inode->i_sb->s_id, inode->i_ino,
307 fl->fl_type, fl->fl_flags,
308 (long long)fl->fl_start, (long long)fl->fl_end);
313 /* No mandatory locks over NFS */
314 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
317 if (NFS_PROTO(inode)->version != 4) {
318 /* Fake OK code if mounted without NLM support */
319 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
321 status = LOCK_USE_CLNT;
327 * No BSD flocks over NFS allowed.
328 * Note: we could try to fake a POSIX lock request here by
329 * using ((u32) filp | 0x80000000) or some such as the pid.
330 * Not sure whether that would be unique, though, or whether
331 * that would break in other places.
333 if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
337 * Flush all pending writes before doing anything
340 status = filemap_fdatawrite(filp->f_mapping);
342 status2 = nfs_wb_all(inode);
346 status2 = filemap_fdatawait(filp->f_mapping);
353 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
361 * Make sure we clear the cache whenever we try to get the lock.
362 * This makes locking act as a cache coherency point.
365 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
366 filemap_fdatawrite(filp->f_mapping);
368 nfs_wb_all(inode); /* we may have slept */
370 filemap_fdatawait(filp->f_mapping);
371 nfs_zap_caches(inode);