File: /usr/src/linux/fs/nfs/file.c
1 /*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
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.
11 *
12 * Expire cache on write to a file by Wai S Kok (Oct 1994).
13 *
14 * Total rewrite of read side for new NFS buffer cache.. Linus.
15 *
16 * nfs regular file handling functions
17 */
18
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/slab.h>
28 #include <linux/pagemap.h>
29 #include <linux/lockd/bind.h>
30 #include <linux/smp_lock.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34
35 #define NFSDBG_FACILITY NFSDBG_FILE
36
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);
42
43 struct file_operations nfs_file_operations = {
44 llseek: generic_file_llseek,
45 read: nfs_file_read,
46 write: nfs_file_write,
47 mmap: nfs_file_mmap,
48 open: nfs_open,
49 flush: nfs_file_flush,
50 release: nfs_release,
51 fsync: nfs_fsync,
52 lock: nfs_lock,
53 };
54
55 struct inode_operations nfs_file_inode_operations = {
56 permission: nfs_permission,
57 revalidate: nfs_revalidate,
58 setattr: nfs_notify_change,
59 };
60
61 /* Hack for future NFS swap support */
62 #ifndef IS_SWAPFILE
63 # define IS_SWAPFILE(inode) (0)
64 #endif
65
66 /*
67 * Flush all dirty pages, and check for write errors.
68 *
69 */
70 static int
71 nfs_file_flush(struct file *file)
72 {
73 struct inode *inode = file->f_dentry->d_inode;
74 int status;
75
76 dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
77
78 /* Make sure all async reads have been sent off. We don't bother
79 * waiting on them though... */
80 if (file->f_mode & FMODE_READ)
81 nfs_pagein_inode(inode, 0, 0);
82
83 status = nfs_wb_file(inode, file);
84 if (!status) {
85 status = file->f_error;
86 file->f_error = 0;
87 }
88 return status;
89 }
90
91 static ssize_t
92 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
93 {
94 struct dentry * dentry = file->f_dentry;
95 struct inode * inode = dentry->d_inode;
96 ssize_t result;
97
98 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
99 dentry->d_parent->d_name.name, dentry->d_name.name,
100 (unsigned long) count, (unsigned long) *ppos);
101
102 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
103 if (!result)
104 result = generic_file_read(file, buf, count, ppos);
105 return result;
106 }
107
108 static int
109 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
110 {
111 struct dentry *dentry = file->f_dentry;
112 struct inode *inode = dentry->d_inode;
113 int status;
114
115 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
116 dentry->d_parent->d_name.name, dentry->d_name.name);
117
118 status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
119 if (!status)
120 status = generic_file_mmap(file, vma);
121 return status;
122 }
123
124 /*
125 * Flush any dirty pages for this process, and check for write errors.
126 * The return status from this call provides a reliable indication of
127 * whether any write errors occurred for this process.
128 */
129 static int
130 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
131 {
132 struct inode *inode = dentry->d_inode;
133 int status;
134
135 dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
136
137 lock_kernel();
138 status = nfs_wb_file(inode, file);
139 if (!status) {
140 status = file->f_error;
141 file->f_error = 0;
142 }
143 unlock_kernel();
144 return status;
145 }
146
147 /*
148 * This does the "real" work of the write. The generic routine has
149 * allocated the page, locked it, done all the page alignment stuff
150 * calculations etc. Now we should just copy the data from user
151 * space and write it back to the real medium..
152 *
153 * If the writer ends up delaying the write, the writer needs to
154 * increment the page use counts until he is done with the page.
155 */
156 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
157 {
158 return nfs_flush_incompatible(file, page);
159 }
160
161 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
162 {
163 long status;
164 loff_t pos = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + to;
165 struct inode *inode = page->mapping->host;
166
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;
174 }
175
176 /*
177 * The following is used by wait_on_page(), generic_file_readahead()
178 * to initiate the completion of any page readahead operations.
179 */
180 static int nfs_sync_page(struct page *page)
181 {
182 struct address_space *mapping;
183 struct inode *inode;
184 unsigned long index = page_index(page);
185 unsigned int rpages;
186 int result;
187
188 mapping = page->mapping;
189 if (!mapping)
190 return 0;
191 inode = mapping->host;
192 if (!inode)
193 return 0;
194
195 rpages = NFS_SERVER(inode)->rpages;
196 result = nfs_pagein_inode(inode, index, rpages);
197 if (result < 0)
198 return result;
199 return 0;
200 }
201
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
208 };
209
210 /*
211 * Write to a file (through the page cache).
212 */
213 static ssize_t
214 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
215 {
216 struct dentry * dentry = file->f_dentry;
217 struct inode * inode = dentry->d_inode;
218 ssize_t result;
219
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);
223
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;
230
231 result = count;
232 if (!count)
233 goto out;
234
235 result = generic_file_write(file, buf, count, ppos);
236 out:
237 return result;
238
239 out_swapfile:
240 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
241 goto out;
242 }
243
244 /*
245 * Lock a (portion of) a file
246 */
247 int
248 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
249 {
250 struct inode * inode = filp->f_dentry->d_inode;
251 int status = 0;
252
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);
257
258 if (!inode)
259 return -EINVAL;
260
261 /* No mandatory locks over NFS */
262 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
263 return -ENOLCK;
264
265 /* Fake OK code if mounted without NLM support */
266 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
267 if (IS_GETLK(cmd))
268 status = LOCK_USE_CLNT;
269 goto out_ok;
270 }
271
272 /*
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.
278 */
279 if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
280 return -ENOLCK;
281
282 /*
283 * Flush all pending writes before doing anything
284 * with locks..
285 */
286 filemap_fdatasync(inode->i_mapping);
287 down(&inode->i_sem);
288 status = nfs_wb_all(inode);
289 up(&inode->i_sem);
290 filemap_fdatawait(inode->i_mapping);
291 if (status < 0)
292 return status;
293
294 lock_kernel();
295 status = nlmclnt_proc(inode, cmd, fl);
296 unlock_kernel();
297 if (status < 0)
298 return status;
299
300 status = 0;
301
302 /*
303 * Make sure we clear the cache whenever we try to get the lock.
304 * This makes locking act as a cache coherency point.
305 */
306 out_ok:
307 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
308 filemap_fdatasync(inode->i_mapping);
309 down(&inode->i_sem);
310 nfs_wb_all(inode); /* we may have slept */
311 up(&inode->i_sem);
312 filemap_fdatawait(inode->i_mapping);
313 nfs_zap_caches(inode);
314 }
315 return status;
316 }
317