Import 2.1.118
[davej-history.git] / fs / nfs / file.c
blob973ad52ece0f69818b616ad04ce318fbb6245822
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_file_close(struct inode *, 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 NULL, /* no special open is needed */
51 nfs_file_flush, /* flush */
52 nfs_file_close, /* 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 nfs_updatepage, /* 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 * Sync the file..
91 static int
92 nfs_file_close(struct inode *inode, struct file *file)
94 int status, error;
96 dfprintk(VFS, "nfs: close(%x/%ld)\n", inode->i_dev, inode->i_ino);
98 status = nfs_flush_dirty_pages(inode, 0, 0, 0);
99 error = nfs_write_error(inode);
100 if (!status)
101 status = error;
102 return status;
106 * Flush all dirty pages, and check for write errors.
108 * We should probably do this better - this does get called at every
109 * close, so we should probably just flush the changes that "this"
110 * file has done and report on only those.
112 * Right now we use the "flush everything" approach. Overkill, but
113 * works.
115 static int
116 nfs_file_flush(struct file *file)
118 return nfs_file_close(file->f_dentry->d_inode, file);
121 static ssize_t
122 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
124 struct dentry * dentry = file->f_dentry;
125 ssize_t result;
127 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
128 dentry->d_parent->d_name.name, dentry->d_name.name,
129 (unsigned long) count, (unsigned long) *ppos);
131 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
132 if (!result)
133 result = generic_file_read(file, buf, count, ppos);
134 return result;
137 static int
138 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
140 struct dentry *dentry = file->f_dentry;
141 int status;
143 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
144 dentry->d_parent->d_name.name, dentry->d_name.name);
146 status = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
147 if (!status)
148 status = generic_file_mmap(file, vma);
149 return status;
153 * Flush any dirty pages for this process, and check for write errors.
154 * The return status from this call provides a reliable indication of
155 * whether any write errors occurred for this process.
157 static int
158 nfs_fsync(struct file *file, struct dentry *dentry)
160 struct inode *inode = dentry->d_inode;
161 int status, error;
163 dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
165 status = nfs_flush_dirty_pages(inode, current->pid, 0, 0);
166 error = nfs_write_error(inode);
167 if (!status)
168 status = error;
169 return status;
173 * Write to a file (through the page cache).
175 static ssize_t
176 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
178 struct dentry * dentry = file->f_dentry;
179 struct inode * inode = dentry->d_inode;
180 ssize_t result;
182 dfprintk(VFS, "nfs: write(%s/%s (%d), %lu@%lu)\n",
183 dentry->d_parent->d_name.name, dentry->d_name.name,
184 inode->i_count, (unsigned long) count, (unsigned long) *ppos);
186 if (!inode) {
187 printk("nfs_file_write: inode = NULL\n");
188 return -EINVAL;
190 result = -EBUSY;
191 if (IS_SWAPFILE(inode))
192 goto out_swapfile;
193 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
194 if (result)
195 goto out;
197 #ifdef NFS_PARANOIA
198 /* N.B. This should be impossible now -- inodes can't change mode */
199 if (!S_ISREG(inode->i_mode)) {
200 printk("nfs_file_write: write to non-file, mode %07o\n",
201 inode->i_mode);
202 return -EINVAL;
204 #endif
205 result = count;
206 if (!count)
207 goto out;
209 /* Check for an error from a previous async call */
210 result = nfs_write_error(inode);
211 if (!result)
212 result = generic_file_write(file, buf, count, ppos);
213 out:
214 return result;
216 out_swapfile:
217 printk(KERN_ERR "NFS: attempt to write to active swap file!\n");
218 goto out;
222 * Lock a (portion of) a file
225 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
227 struct inode * inode = filp->f_dentry->d_inode;
228 int status;
230 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
231 inode->i_dev, inode->i_ino,
232 fl->fl_type, fl->fl_flags,
233 fl->fl_start, fl->fl_end);
235 if (!inode)
236 return -EINVAL;
238 /* No mandatory locks over NFS */
239 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
240 return -ENOLCK;
242 /* Fake OK code if mounted without NLM support */
243 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
244 return 0;
247 * No BSD flocks over NFS allowed.
248 * Note: we could try to fake a POSIX lock request here by
249 * using ((u32) filp | 0x80000000) or some such as the pid.
250 * Not sure whether that would be unique, though, or whether
251 * that would break in other places.
253 if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
254 return -ENOLCK;
256 /* If unlocking a file region, flush dirty pages (unless we've
257 * been killed by a signal, that is). */
258 if (cmd == F_SETLK && fl->fl_type == F_UNLCK
259 && !signal_pending(current)) {
260 status = nfs_flush_dirty_pages(inode, current->pid,
261 fl->fl_start, fl->fl_end == NLM_OFFSET_MAX? 0 :
262 fl->fl_end - fl->fl_start + 1);
263 if (status < 0)
264 return status;
267 if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
268 return status;
270 /* Here, we could turn off write-back of pages in the
271 * locked file region */
273 return 0;