pre-2.3.4..
[davej-history.git] / fs / nfs / file.c
blob1cf40d3ae3f3c73a05b9a401205bb951b534a125
1 /*
2 * linux/fs/nfs/file.c
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>
25 #include <linux/mm.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 */
49 nfs_open, /* open */
50 nfs_file_flush, /* flush */
51 nfs_release, /* release */
52 nfs_fsync, /* fsync */
53 NULL, /* fasync */
54 NULL, /* check_media_change */
55 NULL, /* revalidate */
56 nfs_lock, /* lock */
59 struct inode_operations nfs_file_inode_operations = {
60 &nfs_file_operations, /* default file operations */
61 NULL, /* create */
62 NULL, /* lookup */
63 NULL, /* link */
64 NULL, /* unlink */
65 NULL, /* symlink */
66 NULL, /* mkdir */
67 NULL, /* rmdir */
68 NULL, /* mknod */
69 NULL, /* rename */
70 NULL, /* readlink */
71 NULL, /* follow_link */
72 nfs_readpage, /* readpage */
73 nfs_writepage, /* writepage */
74 NULL, /* bmap */
75 NULL, /* truncate */
76 NULL, /* permission */
77 NULL, /* smap */
78 nfs_updatepage, /* updatepage */
79 nfs_revalidate, /* revalidate */
82 /* Hack for future NFS swap support */
83 #ifndef IS_SWAPFILE
84 # define IS_SWAPFILE(inode) (0)
85 #endif
88 * Flush all dirty pages, and check for write errors.
91 static int
92 nfs_file_flush(struct file *file)
94 struct inode *inode = file->f_dentry->d_inode;
95 int status;
97 dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
99 status = nfs_wb_file(inode, file);
100 if (!status) {
101 status = file->f_error;
102 file->f_error = 0;
104 return status;
107 static ssize_t
108 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
110 struct dentry * dentry = file->f_dentry;
111 ssize_t result;
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);
118 if (!result)
119 result = generic_file_read(file, buf, count, ppos);
120 return result;
123 static int
124 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
126 struct dentry *dentry = file->f_dentry;
127 int status;
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);
133 if (!status)
134 status = generic_file_mmap(file, vma);
135 return status;
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.
143 static int
144 nfs_fsync(struct file *file, struct dentry *dentry)
146 struct inode *inode = dentry->d_inode;
147 int status;
149 dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
151 status = nfs_wb_file(inode, file);
152 if (!status) {
153 status = file->f_error;
154 file->f_error = 0;
156 return status;
161 * Write to a file (through the page cache).
163 static ssize_t
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;
168 ssize_t result;
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);
174 result = -EBUSY;
175 if (IS_SWAPFILE(inode))
176 goto out_swapfile;
177 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
178 if (result)
179 goto out;
181 result = count;
182 if (!count)
183 goto out;
185 result = generic_file_write(file, buf, count, ppos);
186 out:
187 return result;
189 out_swapfile:
190 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
191 goto out;
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;
201 int status;
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);
208 if (!inode)
209 return -EINVAL;
211 /* No mandatory locks over NFS */
212 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
213 return -ENOLCK;
215 /* Fake OK code if mounted without NLM support */
216 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
217 return 0;
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)
227 return -ENOLCK;
230 * Flush all pending writes before doing anything
231 * with locks..
233 status = nfs_wb_all(inode);
234 if (status < 0)
235 return status;
237 if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
238 return status;
241 * Make sure we re-validate anything we've got cached.
242 * This makes locking act as a cache coherency point.
244 NFS_CACHEINV(inode);
245 return 0;