Import 2.3.5
[davej-history.git] / fs / nfs / file.c
blob75b14988669e30feb71f48c47fd80d374bf8c725
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/uaccess.h>
31 #include <asm/segment.h>
32 #include <asm/system.h>
34 #define NFSDBG_FACILITY NFSDBG_FILE
36 static int nfs_file_mmap(struct file *, struct vm_area_struct *);
37 static ssize_t nfs_file_read(struct file *, char *, size_t, loff_t *);
38 static ssize_t nfs_file_write(struct file *, const char *, size_t, loff_t *);
39 static int nfs_file_flush(struct file *);
40 static int nfs_fsync(struct file *, struct dentry *dentry);
42 static struct file_operations nfs_file_operations = {
43 NULL, /* lseek - default */
44 nfs_file_read, /* read */
45 nfs_file_write, /* write */
46 NULL, /* readdir - bad */
47 NULL, /* select - default */
48 NULL, /* ioctl - default */
49 nfs_file_mmap, /* mmap */
50 nfs_open, /* open */
51 nfs_file_flush, /* flush */
52 nfs_release, /* release */
53 nfs_fsync, /* fsync */
54 NULL, /* fasync */
55 NULL, /* check_media_change */
56 NULL, /* revalidate */
57 nfs_lock, /* lock */
60 struct inode_operations nfs_file_inode_operations = {
61 &nfs_file_operations, /* default file operations */
62 NULL, /* create */
63 NULL, /* lookup */
64 NULL, /* link */
65 NULL, /* unlink */
66 NULL, /* symlink */
67 NULL, /* mkdir */
68 NULL, /* rmdir */
69 NULL, /* mknod */
70 NULL, /* rename */
71 NULL, /* readlink */
72 NULL, /* follow_link */
73 nfs_readpage, /* readpage */
74 nfs_writepage, /* writepage */
75 NULL, /* bmap */
76 NULL, /* truncate */
77 NULL, /* permission */
78 NULL, /* smap */
79 NULL, /* updatepage */
80 nfs_revalidate, /* revalidate */
83 /* Hack for future NFS swap support */
84 #ifndef IS_SWAPFILE
85 # define IS_SWAPFILE(inode) (0)
86 #endif
89 * Flush all dirty pages, and check for write errors.
92 static int
93 nfs_file_flush(struct file *file)
95 struct inode *inode = file->f_dentry->d_inode;
96 int status;
98 dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
100 status = nfs_wb_file(inode, file);
101 if (!status) {
102 status = file->f_error;
103 file->f_error = 0;
105 return status;
108 static ssize_t
109 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
111 struct dentry * dentry = file->f_dentry;
112 ssize_t result;
114 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
115 dentry->d_parent->d_name.name, dentry->d_name.name,
116 (unsigned long) count, (unsigned long) *ppos);
118 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
119 if (!result)
120 result = generic_file_read(file, buf, count, ppos);
121 return result;
124 static int
125 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
127 struct dentry *dentry = file->f_dentry;
128 int status;
130 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
131 dentry->d_parent->d_name.name, dentry->d_name.name);
133 status = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
134 if (!status)
135 status = generic_file_mmap(file, vma);
136 return status;
140 * Flush any dirty pages for this process, and check for write errors.
141 * The return status from this call provides a reliable indication of
142 * whether any write errors occurred for this process.
144 static int
145 nfs_fsync(struct file *file, struct dentry *dentry)
147 struct inode *inode = dentry->d_inode;
148 int status;
150 dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
152 status = nfs_wb_file(inode, file);
153 if (!status) {
154 status = file->f_error;
155 file->f_error = 0;
157 return status;
161 * This does the "real" work of the write. The generic routine has
162 * allocated the page, locked it, done all the page alignment stuff
163 * calculations etc. Now we should just copy the data from user
164 * space and write it back to the real medium..
166 * If the writer ends up delaying the write, the writer needs to
167 * increment the page use counts until he is done with the page.
169 static long nfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
171 long status;
173 bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
174 status = -EFAULT;
175 if (bytes)
176 status = nfs_updatepage(file, page, offset, bytes);
177 return status;
181 * Write to a file (through the page cache).
183 static ssize_t
184 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
186 struct dentry * dentry = file->f_dentry;
187 struct inode * inode = dentry->d_inode;
188 ssize_t result;
190 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
191 dentry->d_parent->d_name.name, dentry->d_name.name,
192 inode->i_ino, (unsigned long) count, (unsigned long) *ppos);
194 result = -EBUSY;
195 if (IS_SWAPFILE(inode))
196 goto out_swapfile;
197 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
198 if (result)
199 goto out;
201 result = count;
202 if (!count)
203 goto out;
205 result = generic_file_write(file, buf, count, ppos, nfs_write_one_page);
206 out:
207 return result;
209 out_swapfile:
210 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
211 goto out;
215 * Lock a (portion of) a file
218 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
220 struct inode * inode = filp->f_dentry->d_inode;
221 int status;
223 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
224 inode->i_dev, inode->i_ino,
225 fl->fl_type, fl->fl_flags,
226 fl->fl_start, fl->fl_end);
228 if (!inode)
229 return -EINVAL;
231 /* No mandatory locks over NFS */
232 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
233 return -ENOLCK;
235 /* Fake OK code if mounted without NLM support */
236 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
237 return 0;
240 * No BSD flocks over NFS allowed.
241 * Note: we could try to fake a POSIX lock request here by
242 * using ((u32) filp | 0x80000000) or some such as the pid.
243 * Not sure whether that would be unique, though, or whether
244 * that would break in other places.
246 if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
247 return -ENOLCK;
250 * Flush all pending writes before doing anything
251 * with locks..
253 status = nfs_wb_all(inode);
254 if (status < 0)
255 return status;
257 if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
258 return status;
261 * Make sure we re-validate anything we've got cached.
262 * This makes locking act as a cache coherency point.
264 NFS_CACHEINV(inode);
265 return 0;