Linux-2.4.0-test2
[davej-history.git] / fs / nfs / file.c
blob62b37c8cf0c80497e3b8868e9da78251ce22fd71
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/nfs_mount.h>
26 #include <linux/mm.h>
27 #include <linux/malloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/lockd/bind.h>
30 #include <linux/smp_lock.h>
32 #include <asm/uaccess.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 struct file_operations nfs_file_operations = {
44 read: nfs_file_read,
45 write: nfs_file_write,
46 mmap: nfs_file_mmap,
47 open: nfs_open,
48 flush: nfs_file_flush,
49 release: nfs_release,
50 fsync: nfs_fsync,
51 lock: nfs_lock,
54 struct inode_operations nfs_file_inode_operations = {
55 revalidate: nfs_revalidate,
56 setattr: nfs_notify_change,
59 /* Hack for future NFS swap support */
60 #ifndef IS_SWAPFILE
61 # define IS_SWAPFILE(inode) (0)
62 #endif
65 * Flush all dirty pages, and check for write errors.
68 static int
69 nfs_file_flush(struct file *file)
71 struct inode *inode = file->f_dentry->d_inode;
72 int status;
74 dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
76 /* Make sure all async reads have been sent off. We don't bother
77 * waiting on them though... */
78 if (file->f_mode & FMODE_READ)
79 nfs_pagein_inode(inode, 0, 0);
81 status = nfs_wb_file(inode, file);
82 if (!status) {
83 status = file->f_error;
84 file->f_error = 0;
86 return status;
89 static ssize_t
90 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
92 struct dentry * dentry = file->f_dentry;
93 ssize_t result;
95 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
96 dentry->d_parent->d_name.name, dentry->d_name.name,
97 (unsigned long) count, (unsigned long) *ppos);
99 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
100 if (!result)
101 result = generic_file_read(file, buf, count, ppos);
102 return result;
105 static int
106 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
108 struct dentry *dentry = file->f_dentry;
109 int status;
111 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
112 dentry->d_parent->d_name.name, dentry->d_name.name);
114 status = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
115 if (!status)
116 status = generic_file_mmap(file, vma);
117 return status;
121 * Flush any dirty pages for this process, and check for write errors.
122 * The return status from this call provides a reliable indication of
123 * whether any write errors occurred for this process.
125 static int
126 nfs_fsync(struct file *file, struct dentry *dentry)
128 struct inode *inode = dentry->d_inode;
129 int status;
131 dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
133 lock_kernel();
134 status = nfs_wb_file(inode, file);
135 if (!status) {
136 status = file->f_error;
137 file->f_error = 0;
139 unlock_kernel();
140 return status;
144 * This does the "real" work of the write. The generic routine has
145 * allocated the page, locked it, done all the page alignment stuff
146 * calculations etc. Now we should just copy the data from user
147 * space and write it back to the real medium..
149 * If the writer ends up delaying the write, the writer needs to
150 * increment the page use counts until he is done with the page.
152 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
154 kmap(page);
155 return nfs_flush_incompatible(file, page);
157 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
159 long status;
160 loff_t pos = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + to;
161 struct inode *inode = (struct inode*)page->mapping->host;
163 kunmap(page);
164 lock_kernel();
165 status = nfs_updatepage(file, page, offset, to-offset);
166 unlock_kernel();
167 /* most likely it's already done. CHECKME */
168 if (pos > inode->i_size)
169 inode->i_size = pos;
170 return status;
174 * The following is used by wait_on_page(), generic_file_readahead()
175 * to initiate the completion of any page readahead operations.
177 static int nfs_sync_page(struct page *page)
179 struct inode *inode = (struct inode *)page->mapping->host;
180 unsigned long index = page_index(page);
181 unsigned int rpages, wpages;
182 int result;
184 if (!inode)
185 return 0;
187 rpages = NFS_SERVER(inode)->rpages;
188 result = nfs_pagein_inode(inode, index, rpages);
189 if (result < 0)
190 goto out_bad;
191 wpages = NFS_SERVER(inode)->wpages;
192 result = nfs_sync_file(inode, NULL, index, wpages, FLUSH_STABLE);
193 if (result < 0)
194 goto out_bad;
195 return 0;
196 out_bad:
197 return result;
200 struct address_space_operations nfs_file_aops = {
201 readpage: nfs_readpage,
202 sync_page: nfs_sync_page,
203 writepage: nfs_writepage,
204 prepare_write: nfs_prepare_write,
205 commit_write: nfs_commit_write
209 * Write to a file (through the page cache).
211 static ssize_t
212 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
214 struct dentry * dentry = file->f_dentry;
215 struct inode * inode = dentry->d_inode;
216 ssize_t result;
218 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
219 dentry->d_parent->d_name.name, dentry->d_name.name,
220 inode->i_ino, (unsigned long) count, (unsigned long) *ppos);
222 result = -EBUSY;
223 if (IS_SWAPFILE(inode))
224 goto out_swapfile;
225 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
226 if (result)
227 goto out;
229 result = count;
230 if (!count)
231 goto out;
233 result = generic_file_write(file, buf, count, ppos);
234 out:
235 return result;
237 out_swapfile:
238 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
239 goto out;
243 * Lock a (portion of) a file
246 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
248 struct inode * inode = filp->f_dentry->d_inode;
249 int status = 0;
251 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
252 inode->i_dev, inode->i_ino,
253 fl->fl_type, fl->fl_flags,
254 (long long)fl->fl_start, (long long)fl->fl_end);
256 if (!inode)
257 return -EINVAL;
259 /* No mandatory locks over NFS */
260 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
261 return -ENOLCK;
263 /* Fake OK code if mounted without NLM support */
264 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
265 if (cmd == F_GETLK)
266 status = LOCK_USE_CLNT;
267 goto out_ok;
271 * No BSD flocks over NFS allowed.
272 * Note: we could try to fake a POSIX lock request here by
273 * using ((u32) filp | 0x80000000) or some such as the pid.
274 * Not sure whether that would be unique, though, or whether
275 * that would break in other places.
277 if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
278 return -ENOLCK;
281 * Flush all pending writes before doing anything
282 * with locks..
284 status = nfs_wb_all(inode);
285 if (status < 0)
286 return status;
288 if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
289 return status;
290 else
291 status = 0;
294 * Make sure we re-validate anything we've got cached.
295 * This makes locking act as a cache coherency point.
297 out_ok:
298 NFS_CACHEINV(inode);
299 return status;