Import 2.3.32
[davej-history.git] / fs / nfs / file.c
blob00279fc6a4b9e1bdd1bebd456125205d20906a59
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>
29 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <asm/segment.h>
33 #include <asm/system.h>
35 #define NFSDBG_FACILITY NFSDBG_FILE
37 static int nfs_file_mmap(struct file *, struct vm_area_struct *);
38 static ssize_t nfs_file_read(struct file *, char *, size_t, loff_t *);
39 static ssize_t nfs_file_write(struct file *, const char *, size_t, loff_t *);
40 static int nfs_file_flush(struct file *);
41 static int nfs_fsync(struct file *, struct dentry *dentry);
43 static struct file_operations nfs_file_operations = {
44 NULL, /* lseek - default */
45 nfs_file_read, /* read */
46 nfs_file_write, /* write */
47 NULL, /* readdir - bad */
48 NULL, /* select - default */
49 NULL, /* ioctl - default */
50 nfs_file_mmap, /* mmap */
51 nfs_open, /* open */
52 nfs_file_flush, /* flush */
53 nfs_release, /* release */
54 nfs_fsync, /* fsync */
55 NULL, /* fasync */
56 NULL, /* check_media_change */
57 NULL, /* revalidate */
58 nfs_lock, /* lock */
61 struct inode_operations nfs_file_inode_operations = {
62 &nfs_file_operations, /* default file operations */
63 NULL, /* create */
64 NULL, /* lookup */
65 NULL, /* link */
66 NULL, /* unlink */
67 NULL, /* symlink */
68 NULL, /* mkdir */
69 NULL, /* rmdir */
70 NULL, /* mknod */
71 NULL, /* rename */
72 NULL, /* readlink */
73 NULL, /* follow_link */
74 NULL, /* get_block */
75 nfs_readpage, /* readpage */
76 nfs_writepage, /* writepage */
77 NULL, /* truncate */
78 NULL, /* permission */
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;
160 * This does the "real" work of the write. The generic routine has
161 * allocated the page, locked it, done all the page alignment stuff
162 * calculations etc. Now we should just copy the data from user
163 * space and write it back to the real medium..
165 * If the writer ends up delaying the write, the writer needs to
166 * increment the page use counts until he is done with the page.
168 static int nfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
170 long status;
172 bytes -= copy_from_user((u8*)kmap(page) + offset, buf, bytes);
173 kunmap(page);
174 status = -EFAULT;
175 if (bytes) {
176 lock_kernel();
177 status = nfs_updatepage(file, page, offset, bytes);
178 unlock_kernel();
180 return status;
184 * Write to a file (through the page cache).
186 static ssize_t
187 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
189 struct dentry * dentry = file->f_dentry;
190 struct inode * inode = dentry->d_inode;
191 ssize_t result;
193 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
194 dentry->d_parent->d_name.name, dentry->d_name.name,
195 inode->i_ino, (unsigned long) count, (unsigned long) *ppos);
197 result = -EBUSY;
198 if (IS_SWAPFILE(inode))
199 goto out_swapfile;
200 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
201 if (result)
202 goto out;
204 result = count;
205 if (!count)
206 goto out;
208 result = generic_file_write(file, buf, count, ppos, nfs_write_one_page);
209 out:
210 return result;
212 out_swapfile:
213 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
214 goto out;
218 * Lock a (portion of) a file
221 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
223 struct inode * inode = filp->f_dentry->d_inode;
224 int status = 0;
226 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
227 inode->i_dev, inode->i_ino,
228 fl->fl_type, fl->fl_flags,
229 fl->fl_start, fl->fl_end);
231 if (!inode)
232 return -EINVAL;
234 /* No mandatory locks over NFS */
235 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
236 return -ENOLCK;
238 /* Fake OK code if mounted without NLM support */
239 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
240 if (cmd == F_GETLK)
241 status = LOCK_USE_CLNT;
242 goto out_ok;
246 * No BSD flocks over NFS allowed.
247 * Note: we could try to fake a POSIX lock request here by
248 * using ((u32) filp | 0x80000000) or some such as the pid.
249 * Not sure whether that would be unique, though, or whether
250 * that would break in other places.
252 if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
253 return -ENOLCK;
256 * Flush all pending writes before doing anything
257 * with locks..
259 status = nfs_wb_all(inode);
260 if (status < 0)
261 return status;
263 if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
264 return status;
265 else
266 status = 0;
269 * Make sure we re-validate anything we've got cached.
270 * This makes locking act as a cache coherency point.
272 out_ok:
273 NFS_CACHEINV(inode);
274 return status;