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/sched.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>
26 #include <linux/malloc.h>
27 #include <linux/pagemap.h>
28 #include <linux/lockd/bind.h>
30 #include <asm/segment.h>
31 #include <asm/system.h>
33 #define NFSDBG_FACILITY NFSDBG_FILE
35 static int nfs_file_mmap(struct file
*, struct vm_area_struct
*);
36 static ssize_t
nfs_file_read(struct file
*, char *, size_t, loff_t
*);
37 static ssize_t
nfs_file_write(struct file
*, const char *, size_t, loff_t
*);
38 static int nfs_file_flush(struct file
*);
39 static int nfs_fsync(struct file
*, struct dentry
*dentry
);
41 static struct file_operations nfs_file_operations
= {
42 NULL
, /* lseek - default */
43 nfs_file_read
, /* read */
44 nfs_file_write
, /* write */
45 NULL
, /* readdir - bad */
46 NULL
, /* select - default */
47 NULL
, /* ioctl - default */
48 nfs_file_mmap
, /* mmap */
50 nfs_file_flush
, /* flush */
51 nfs_release
, /* release */
52 nfs_fsync
, /* fsync */
54 NULL
, /* check_media_change */
55 NULL
, /* revalidate */
59 struct inode_operations nfs_file_inode_operations
= {
60 &nfs_file_operations
, /* default file operations */
71 NULL
, /* follow_link */
72 nfs_readpage
, /* readpage */
73 nfs_writepage
, /* writepage */
76 NULL
, /* permission */
78 nfs_updatepage
, /* updatepage */
79 nfs_revalidate
, /* revalidate */
82 /* Hack for future NFS swap support */
84 # define IS_SWAPFILE(inode) (0)
88 * Flush all dirty pages, and check for write errors.
92 nfs_file_flush(struct file
*file
)
94 struct inode
*inode
= file
->f_dentry
->d_inode
;
97 dfprintk(VFS
, "nfs: flush(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
99 status
= nfs_wb_file(inode
, file
);
101 status
= file
->f_error
;
108 nfs_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
110 struct dentry
* dentry
= file
->f_dentry
;
113 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
114 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
115 (unsigned long) count
, (unsigned long) *ppos
);
117 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
119 result
= generic_file_read(file
, buf
, count
, ppos
);
124 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
126 struct dentry
*dentry
= file
->f_dentry
;
129 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
130 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
132 status
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
134 status
= generic_file_mmap(file
, vma
);
139 * Flush any dirty pages for this process, and check for write errors.
140 * The return status from this call provides a reliable indication of
141 * whether any write errors occurred for this process.
144 nfs_fsync(struct file
*file
, struct dentry
*dentry
)
146 struct inode
*inode
= dentry
->d_inode
;
149 dfprintk(VFS
, "nfs: fsync(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
151 status
= nfs_wb_file(inode
, file
);
153 status
= file
->f_error
;
161 * Write to a file (through the page cache).
164 nfs_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
166 struct dentry
* dentry
= file
->f_dentry
;
167 struct inode
* inode
= dentry
->d_inode
;
170 dfprintk(VFS
, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
171 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
172 inode
->i_ino
, (unsigned long) count
, (unsigned long) *ppos
);
175 if (IS_SWAPFILE(inode
))
177 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
185 result
= generic_file_write(file
, buf
, count
, ppos
);
190 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
195 * Lock a (portion of) a file
198 nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
200 struct inode
* inode
= filp
->f_dentry
->d_inode
;
203 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
204 inode
->i_dev
, inode
->i_ino
,
205 fl
->fl_type
, fl
->fl_flags
,
206 fl
->fl_start
, fl
->fl_end
);
211 /* No mandatory locks over NFS */
212 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
215 /* Fake OK code if mounted without NLM support */
216 if (NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
)
220 * No BSD flocks over NFS allowed.
221 * Note: we could try to fake a POSIX lock request here by
222 * using ((u32) filp | 0x80000000) or some such as the pid.
223 * Not sure whether that would be unique, though, or whether
224 * that would break in other places.
226 if (!fl
->fl_owner
|| (fl
->fl_flags
& (FL_POSIX
|FL_BROKEN
)) != FL_POSIX
)
230 * Flush all pending writes before doing anything
233 status
= nfs_wb_all(inode
);
237 if ((status
= nlmclnt_proc(inode
, cmd
, fl
)) < 0)
241 * Make sure we re-validate anything we've got cached.
242 * This makes locking act as a cache coherency point.