Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / fs / nfs / file.c
blob2b3f22777b0fe0c7ffb9bc633a3a7b787f744ec9
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, int datasync);
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 permission: nfs_permission,
56 revalidate: nfs_revalidate,
57 setattr: nfs_notify_change,
60 /* Hack for future NFS swap support */
61 #ifndef IS_SWAPFILE
62 # define IS_SWAPFILE(inode) (0)
63 #endif
66 * Flush all dirty pages, and check for write errors.
69 static int
70 nfs_file_flush(struct file *file)
72 struct inode *inode = file->f_dentry->d_inode;
73 int status;
75 dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
77 /* Make sure all async reads have been sent off. We don't bother
78 * waiting on them though... */
79 if (file->f_mode & FMODE_READ)
80 nfs_pagein_inode(inode, 0, 0);
82 status = nfs_wb_file(inode, file);
83 if (!status) {
84 status = file->f_error;
85 file->f_error = 0;
87 return status;
90 static ssize_t
91 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
93 struct dentry * dentry = file->f_dentry;
94 struct inode * inode = dentry->d_inode;
95 ssize_t result;
97 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
98 dentry->d_parent->d_name.name, dentry->d_name.name,
99 (unsigned long) count, (unsigned long) *ppos);
101 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
102 if (!result)
103 result = generic_file_read(file, buf, count, ppos);
104 return result;
107 static int
108 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
110 struct dentry *dentry = file->f_dentry;
111 struct inode *inode = dentry->d_inode;
112 int status;
114 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
115 dentry->d_parent->d_name.name, dentry->d_name.name);
117 status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
118 if (!status)
119 status = generic_file_mmap(file, vma);
120 return status;
124 * Flush any dirty pages for this process, and check for write errors.
125 * The return status from this call provides a reliable indication of
126 * whether any write errors occurred for this process.
128 static int
129 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
131 struct inode *inode = dentry->d_inode;
132 int status;
134 dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
136 lock_kernel();
137 status = nfs_wb_file(inode, file);
138 if (!status) {
139 status = file->f_error;
140 file->f_error = 0;
142 unlock_kernel();
143 return status;
147 * This does the "real" work of the write. The generic routine has
148 * allocated the page, locked it, done all the page alignment stuff
149 * calculations etc. Now we should just copy the data from user
150 * space and write it back to the real medium..
152 * If the writer ends up delaying the write, the writer needs to
153 * increment the page use counts until he is done with the page.
155 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
157 kmap(page);
158 return nfs_flush_incompatible(file, page);
160 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
162 long status;
163 loff_t pos = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + to;
164 struct inode *inode = page->mapping->host;
166 kunmap(page);
167 lock_kernel();
168 status = nfs_updatepage(file, page, offset, to-offset);
169 unlock_kernel();
170 /* most likely it's already done. CHECKME */
171 if (pos > inode->i_size)
172 inode->i_size = pos;
173 return status;
177 * The following is used by wait_on_page(), generic_file_readahead()
178 * to initiate the completion of any page readahead operations.
180 static int nfs_sync_page(struct page *page)
182 struct address_space *mapping;
183 struct inode *inode;
184 unsigned long index = page_index(page);
185 unsigned int rpages;
186 int result;
188 mapping = page->mapping;
189 if (!mapping)
190 return 0;
191 inode = mapping->host;
192 if (!inode)
193 return 0;
195 rpages = NFS_SERVER(inode)->rpages;
196 result = nfs_pagein_inode(inode, index, rpages);
197 if (result < 0)
198 return result;
199 return 0;
202 struct address_space_operations nfs_file_aops = {
203 readpage: nfs_readpage,
204 sync_page: nfs_sync_page,
205 writepage: nfs_writepage,
206 prepare_write: nfs_prepare_write,
207 commit_write: nfs_commit_write
211 * Write to a file (through the page cache).
213 static ssize_t
214 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
216 struct dentry * dentry = file->f_dentry;
217 struct inode * inode = dentry->d_inode;
218 ssize_t result;
220 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
221 dentry->d_parent->d_name.name, dentry->d_name.name,
222 inode->i_ino, (unsigned long) count, (unsigned long) *ppos);
224 result = -EBUSY;
225 if (IS_SWAPFILE(inode))
226 goto out_swapfile;
227 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
228 if (result)
229 goto out;
231 result = count;
232 if (!count)
233 goto out;
235 result = generic_file_write(file, buf, count, ppos);
236 out:
237 return result;
239 out_swapfile:
240 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
241 goto out;
245 * Lock a (portion of) a file
248 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
250 struct inode * inode = filp->f_dentry->d_inode;
251 int status = 0;
253 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
254 inode->i_dev, inode->i_ino,
255 fl->fl_type, fl->fl_flags,
256 (long long)fl->fl_start, (long long)fl->fl_end);
258 if (!inode)
259 return -EINVAL;
261 /* No mandatory locks over NFS */
262 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
263 return -ENOLCK;
265 /* Fake OK code if mounted without NLM support */
266 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
267 if (cmd == F_GETLK)
268 status = LOCK_USE_CLNT;
269 goto out_ok;
273 * No BSD flocks over NFS allowed.
274 * Note: we could try to fake a POSIX lock request here by
275 * using ((u32) filp | 0x80000000) or some such as the pid.
276 * Not sure whether that would be unique, though, or whether
277 * that would break in other places.
279 if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
280 return -ENOLCK;
283 * Flush all pending writes before doing anything
284 * with locks..
286 down(&filp->f_dentry->d_inode->i_sem);
287 status = nfs_wb_all(inode);
288 up(&filp->f_dentry->d_inode->i_sem);
289 if (status < 0)
290 return status;
292 if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
293 return status;
294 else
295 status = 0;
298 * Make sure we clear the cache whenever we try to get the lock.
299 * This makes locking act as a cache coherency point.
301 out_ok:
302 if ((cmd == F_SETLK || cmd == F_SETLKW) && fl->fl_type != F_UNLCK) {
303 down(&filp->f_dentry->d_inode->i_sem);
304 nfs_wb_all(inode); /* we may have slept */
305 nfs_zap_caches(inode);
306 up(&filp->f_dentry->d_inode->i_sem);
308 return status;