MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / nfs / file.c
blob21071ddd466277f0bb585f375ad6010d513109eb
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/time.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/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
34 #include "delegation.h"
36 #define NFSDBG_FACILITY NFSDBG_FILE
38 static int nfs_file_open(struct inode *, struct file *);
39 static int nfs_file_release(struct inode *, struct file *);
40 static int nfs_file_mmap(struct file *, struct vm_area_struct *);
41 static ssize_t nfs_file_sendfile(struct file *, loff_t *, size_t, read_actor_t, void *);
42 static ssize_t nfs_file_read(struct kiocb *, char __user *, size_t, loff_t);
43 static ssize_t nfs_file_write(struct kiocb *, const char __user *, size_t, loff_t);
44 static int nfs_file_flush(struct file *);
45 static int nfs_fsync(struct file *, struct dentry *dentry, int datasync);
46 static int nfs_check_flags(int flags);
48 struct file_operations nfs_file_operations = {
49 .llseek = remote_llseek,
50 .read = do_sync_read,
51 .write = do_sync_write,
52 .aio_read = nfs_file_read,
53 .aio_write = nfs_file_write,
54 .mmap = nfs_file_mmap,
55 .open = nfs_file_open,
56 .flush = nfs_file_flush,
57 .release = nfs_file_release,
58 .fsync = nfs_fsync,
59 .lock = nfs_lock,
60 .sendfile = nfs_file_sendfile,
61 .check_flags = nfs_check_flags,
64 struct inode_operations nfs_file_inode_operations = {
65 .permission = nfs_permission,
66 .getattr = nfs_getattr,
67 .setattr = nfs_setattr,
70 /* Hack for future NFS swap support */
71 #ifndef IS_SWAPFILE
72 # define IS_SWAPFILE(inode) (0)
73 #endif
75 static int nfs_check_flags(int flags)
77 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
78 return -EINVAL;
80 return 0;
84 * Open file
86 static int
87 nfs_file_open(struct inode *inode, struct file *filp)
89 struct nfs_server *server = NFS_SERVER(inode);
90 int (*open)(struct inode *, struct file *);
91 int res;
93 res = nfs_check_flags(filp->f_flags);
94 if (res)
95 return res;
97 lock_kernel();
98 /* Do NFSv4 open() call */
99 if ((open = server->rpc_ops->file_open) != NULL)
100 res = open(inode, filp);
101 unlock_kernel();
102 return res;
105 static int
106 nfs_file_release(struct inode *inode, struct file *filp)
108 return NFS_PROTO(inode)->file_release(inode, filp);
112 * Flush all dirty pages, and check for write errors.
115 static int
116 nfs_file_flush(struct file *file)
118 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
119 struct inode *inode = file->f_dentry->d_inode;
120 int status;
122 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
124 if ((file->f_mode & FMODE_WRITE) == 0)
125 return 0;
126 lock_kernel();
127 /* Ensure that data+attribute caches are up to date after close() */
128 status = nfs_wb_all(inode);
129 if (!status) {
130 status = ctx->error;
131 ctx->error = 0;
132 if (!status && !nfs_have_delegation(inode, FMODE_READ))
133 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
135 unlock_kernel();
136 return status;
139 static ssize_t
140 nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
142 struct dentry * dentry = iocb->ki_filp->f_dentry;
143 struct inode * inode = dentry->d_inode;
144 ssize_t result;
146 #ifdef CONFIG_NFS_DIRECTIO
147 if (iocb->ki_filp->f_flags & O_DIRECT)
148 return nfs_file_direct_read(iocb, buf, count, pos);
149 #endif
151 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
152 dentry->d_parent->d_name.name, dentry->d_name.name,
153 (unsigned long) count, (unsigned long) pos);
155 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
156 if (!result)
157 result = generic_file_aio_read(iocb, buf, count, pos);
158 return result;
161 static ssize_t
162 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
163 read_actor_t actor, void *target)
165 struct dentry *dentry = filp->f_dentry;
166 struct inode *inode = dentry->d_inode;
167 ssize_t res;
169 dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
170 dentry->d_parent->d_name.name, dentry->d_name.name,
171 (unsigned long) count, (unsigned long long) *ppos);
173 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
174 if (!res)
175 res = generic_file_sendfile(filp, ppos, count, actor, target);
176 return res;
179 static int
180 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
182 struct dentry *dentry = file->f_dentry;
183 struct inode *inode = dentry->d_inode;
184 int status;
186 dfprintk(VFS, "nfs: mmap(%s/%s)\n",
187 dentry->d_parent->d_name.name, dentry->d_name.name);
189 status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
190 if (!status)
191 status = generic_file_mmap(file, vma);
192 return status;
196 * Flush any dirty pages for this process, and check for write errors.
197 * The return status from this call provides a reliable indication of
198 * whether any write errors occurred for this process.
200 static int
201 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
203 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
204 struct inode *inode = dentry->d_inode;
205 int status;
207 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
209 lock_kernel();
210 status = nfs_wb_all(inode);
211 if (!status) {
212 status = ctx->error;
213 ctx->error = 0;
215 unlock_kernel();
216 return status;
220 * This does the "real" work of the write. The generic routine has
221 * allocated the page, locked it, done all the page alignment stuff
222 * calculations etc. Now we should just copy the data from user
223 * space and write it back to the real medium..
225 * If the writer ends up delaying the write, the writer needs to
226 * increment the page use counts until he is done with the page.
228 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
230 return nfs_flush_incompatible(file, page);
233 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
235 long status;
237 lock_kernel();
238 status = nfs_updatepage(file, page, offset, to-offset);
239 unlock_kernel();
240 return status;
243 struct address_space_operations nfs_file_aops = {
244 .readpage = nfs_readpage,
245 .readpages = nfs_readpages,
246 .set_page_dirty = __set_page_dirty_nobuffers,
247 .writepage = nfs_writepage,
248 .writepages = nfs_writepages,
249 .prepare_write = nfs_prepare_write,
250 .commit_write = nfs_commit_write,
251 #ifdef CONFIG_NFS_DIRECTIO
252 .direct_IO = nfs_direct_IO,
253 #endif
257 * Write to a file (through the page cache).
259 static ssize_t
260 nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
262 struct dentry * dentry = iocb->ki_filp->f_dentry;
263 struct inode * inode = dentry->d_inode;
264 ssize_t result;
266 #ifdef CONFIG_NFS_DIRECTIO
267 if (iocb->ki_filp->f_flags & O_DIRECT)
268 return nfs_file_direct_write(iocb, buf, count, pos);
269 #endif
271 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
272 dentry->d_parent->d_name.name, dentry->d_name.name,
273 inode->i_ino, (unsigned long) count, (unsigned long) pos);
275 result = -EBUSY;
276 if (IS_SWAPFILE(inode))
277 goto out_swapfile;
278 result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
279 if (result)
280 goto out;
282 result = count;
283 if (!count)
284 goto out;
286 result = generic_file_aio_write(iocb, buf, count, pos);
287 out:
288 return result;
290 out_swapfile:
291 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
292 goto out;
295 static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
297 struct inode *inode = filp->f_mapping->host;
298 int status;
300 lock_kernel();
301 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
302 unlock_kernel();
303 return status;
306 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
308 struct inode *inode = filp->f_mapping->host;
309 sigset_t oldset;
310 int status;
312 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
314 * Flush all pending writes before doing anything
315 * with locks..
317 filemap_fdatawrite(filp->f_mapping);
318 down(&inode->i_sem);
319 nfs_wb_all(inode);
320 up(&inode->i_sem);
321 filemap_fdatawait(filp->f_mapping);
323 /* NOTE: special case
324 * If we're signalled while cleaning up locks on process exit, we
325 * still need to complete the unlock.
327 lock_kernel();
328 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
329 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
330 return status;
333 static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
335 struct inode *inode = filp->f_mapping->host;
336 int status;
339 * Flush all pending writes before doing anything
340 * with locks..
342 status = filemap_fdatawrite(filp->f_mapping);
343 if (status == 0) {
344 down(&inode->i_sem);
345 status = nfs_wb_all(inode);
346 up(&inode->i_sem);
347 if (status == 0)
348 status = filemap_fdatawait(filp->f_mapping);
350 if (status < 0)
351 return status;
353 lock_kernel();
354 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
355 /* If we were signalled we still need to ensure that
356 * we clean up any state on the server. We therefore
357 * record the lock call as having succeeded in order to
358 * ensure that locks_remove_posix() cleans it out when
359 * the process exits.
361 if (status == -EINTR || status == -ERESTARTSYS)
362 posix_lock_file(filp, fl);
363 unlock_kernel();
364 if (status < 0)
365 return status;
367 * Make sure we clear the cache whenever we try to get the lock.
368 * This makes locking act as a cache coherency point.
370 filemap_fdatawrite(filp->f_mapping);
371 down(&inode->i_sem);
372 nfs_wb_all(inode); /* we may have slept */
373 up(&inode->i_sem);
374 filemap_fdatawait(filp->f_mapping);
375 nfs_zap_caches(inode);
376 return 0;
380 * Lock a (portion of) a file
383 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
385 struct inode * inode = filp->f_mapping->host;
387 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
388 inode->i_sb->s_id, inode->i_ino,
389 fl->fl_type, fl->fl_flags,
390 (long long)fl->fl_start, (long long)fl->fl_end);
392 if (!inode)
393 return -EINVAL;
395 /* No mandatory locks over NFS */
396 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
397 return -ENOLCK;
399 if (NFS_PROTO(inode)->version != 4) {
400 /* Fake OK code if mounted without NLM support */
401 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
402 if (IS_GETLK(cmd))
403 return LOCK_USE_CLNT;
404 return 0;
409 * No BSD flocks over NFS allowed.
410 * Note: we could try to fake a POSIX lock request here by
411 * using ((u32) filp | 0x80000000) or some such as the pid.
412 * Not sure whether that would be unique, though, or whether
413 * that would break in other places.
415 if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
416 return -ENOLCK;
418 if (IS_GETLK(cmd))
419 return do_getlk(filp, cmd, fl);
420 if (fl->fl_type == F_UNLCK)
421 return do_unlk(filp, cmd, fl);
422 return do_setlk(filp, cmd, fl);