patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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/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>
30
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33
34 #define NFSDBG_FACILITY         NFSDBG_FILE
35
36 static long nfs_file_fcntl(int fd, unsigned int cmd,
37                         unsigned long arg, struct file *filp);
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 __user *);
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
47 struct file_operations nfs_file_operations = {
48         .llseek         = remote_llseek,
49         .read           = do_sync_read,
50         .write          = do_sync_write,
51         .aio_read               = nfs_file_read,
52         .aio_write              = nfs_file_write,
53         .mmap           = nfs_file_mmap,
54         .open           = nfs_file_open,
55         .flush          = nfs_file_flush,
56         .release        = nfs_file_release,
57         .fsync          = nfs_fsync,
58         .lock           = nfs_lock,
59         .sendfile       = nfs_file_sendfile,
60         .fcntl          = nfs_file_fcntl,
61 };
62
63 struct inode_operations nfs_file_inode_operations = {
64         .permission     = nfs_permission,
65         .getattr        = nfs_getattr,
66         .setattr        = nfs_setattr,
67 };
68
69 /* Hack for future NFS swap support */
70 #ifndef IS_SWAPFILE
71 # define IS_SWAPFILE(inode)     (0)
72 #endif
73
74 #define nfs_invalid_flags       (O_APPEND | O_DIRECT)
75
76 /*
77  * Check for special cases that NFS doesn't support, and
78  * pass the rest to the generic fcntl function.
79  */
80 static long
81 nfs_file_fcntl(int fd, unsigned int cmd,
82                 unsigned long arg, struct file *filp)
83 {
84         switch (cmd) {
85         case F_SETFL:
86                 if ((filp->f_flags & nfs_invalid_flags) == nfs_invalid_flags)
87                         return -EINVAL;
88                 break;
89         default:
90                 break;
91         }
92
93         return generic_file_fcntl(fd, cmd, arg, filp);
94 }
95
96 /*
97  * Open file
98  */
99 static int
100 nfs_file_open(struct inode *inode, struct file *filp)
101 {
102         struct nfs_server *server = NFS_SERVER(inode);
103         int (*open)(struct inode *, struct file *);
104         int res = 0;
105
106         if ((filp->f_flags & nfs_invalid_flags) == nfs_invalid_flags)
107                 return -EINVAL;
108
109         lock_kernel();
110         /* Do NFSv4 open() call */
111         if ((open = server->rpc_ops->file_open) != NULL)
112                 res = open(inode, filp);
113         unlock_kernel();
114         return res;
115 }
116
117 static int
118 nfs_file_release(struct inode *inode, struct file *filp)
119 {
120         return NFS_PROTO(inode)->file_release(inode, filp);
121 }
122
123 /*
124  * Flush all dirty pages, and check for write errors.
125  *
126  */
127 static int
128 nfs_file_flush(struct file *file)
129 {
130         struct inode    *inode = file->f_dentry->d_inode;
131         int             status;
132
133         dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
134
135         if ((file->f_mode & FMODE_WRITE) == 0)
136                 return 0;
137         lock_kernel();
138         /* Ensure that data+attribute caches are up to date after close() */
139         status = nfs_wb_all(inode);
140         if (!status) {
141                 status = file->f_error;
142                 file->f_error = 0;
143                 if (!status)
144                         __nfs_revalidate_inode(NFS_SERVER(inode), inode);
145         }
146         unlock_kernel();
147         return status;
148 }
149
150 static ssize_t
151 nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
152 {
153         struct dentry * dentry = iocb->ki_filp->f_dentry;
154         struct inode * inode = dentry->d_inode;
155         ssize_t result;
156
157 #ifdef CONFIG_NFS_DIRECTIO
158         if (iocb->ki_filp->f_flags & O_DIRECT)
159                 return nfs_file_direct_read(iocb, buf, count, pos);
160 #endif
161
162         dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
163                 dentry->d_parent->d_name.name, dentry->d_name.name,
164                 (unsigned long) count, (unsigned long) pos);
165
166         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
167         if (!result)
168                 result = generic_file_aio_read(iocb, buf, count, pos);
169         return result;
170 }
171
172 static ssize_t
173 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
174                 read_actor_t actor, void __user *target)
175 {
176         struct dentry *dentry = filp->f_dentry;
177         struct inode *inode = dentry->d_inode;
178         ssize_t res;
179
180         dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
181                 dentry->d_parent->d_name.name, dentry->d_name.name,
182                 (unsigned long) count, (unsigned long long) *ppos);
183
184         res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
185         if (!res)
186                 res = generic_file_sendfile(filp, ppos, count, actor, target);
187         return res;
188 }
189
190 static int
191 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
192 {
193         struct dentry *dentry = file->f_dentry;
194         struct inode *inode = dentry->d_inode;
195         int     status;
196
197         dfprintk(VFS, "nfs: mmap(%s/%s)\n",
198                 dentry->d_parent->d_name.name, dentry->d_name.name);
199
200         status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
201         if (!status)
202                 status = generic_file_mmap(file, vma);
203         return status;
204 }
205
206 /*
207  * Flush any dirty pages for this process, and check for write errors.
208  * The return status from this call provides a reliable indication of
209  * whether any write errors occurred for this process.
210  */
211 static int
212 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
213 {
214         struct inode *inode = dentry->d_inode;
215         int status;
216
217         dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
218
219         lock_kernel();
220         status = nfs_wb_all(inode);
221         if (!status) {
222                 status = file->f_error;
223                 file->f_error = 0;
224         }
225         unlock_kernel();
226         return status;
227 }
228
229 /*
230  * This does the "real" work of the write. The generic routine has
231  * allocated the page, locked it, done all the page alignment stuff
232  * calculations etc. Now we should just copy the data from user
233  * space and write it back to the real medium..
234  *
235  * If the writer ends up delaying the write, the writer needs to
236  * increment the page use counts until he is done with the page.
237  */
238 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
239 {
240         return nfs_flush_incompatible(file, page);
241 }
242
243 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
244 {
245         long status;
246
247         lock_kernel();
248         status = nfs_updatepage(file, page, offset, to-offset);
249         unlock_kernel();
250         return status;
251 }
252
253 struct address_space_operations nfs_file_aops = {
254         .readpage = nfs_readpage,
255         .readpages = nfs_readpages,
256         .set_page_dirty = __set_page_dirty_nobuffers,
257         .writepage = nfs_writepage,
258         .writepages = nfs_writepages,
259         .prepare_write = nfs_prepare_write,
260         .commit_write = nfs_commit_write,
261 #ifdef CONFIG_NFS_DIRECTIO
262         .direct_IO = nfs_direct_IO,
263 #endif
264 };
265
266 /* 
267  * Write to a file (through the page cache).
268  */
269 static ssize_t
270 nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
271 {
272         struct dentry * dentry = iocb->ki_filp->f_dentry;
273         struct inode * inode = dentry->d_inode;
274         ssize_t result;
275
276 #ifdef CONFIG_NFS_DIRECTIO
277         if (iocb->ki_filp->f_flags & O_DIRECT)
278                 return nfs_file_direct_write(iocb, buf, count, pos);
279 #endif
280
281         dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
282                 dentry->d_parent->d_name.name, dentry->d_name.name,
283                 inode->i_ino, (unsigned long) count, (unsigned long) pos);
284
285         result = -EBUSY;
286         if (IS_SWAPFILE(inode))
287                 goto out_swapfile;
288         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
289         if (result)
290                 goto out;
291
292         result = count;
293         if (!count)
294                 goto out;
295
296         result = generic_file_aio_write(iocb, buf, count, pos);
297 out:
298         return result;
299
300 out_swapfile:
301         printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
302         goto out;
303 }
304
305 /*
306  * Lock a (portion of) a file
307  */
308 int
309 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
310 {
311         struct inode * inode = filp->f_mapping->host;
312         int     status = 0;
313         int     status2;
314
315         dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
316                         inode->i_sb->s_id, inode->i_ino,
317                         fl->fl_type, fl->fl_flags,
318                         (long long)fl->fl_start, (long long)fl->fl_end);
319
320         if (!inode)
321                 return -EINVAL;
322
323         /* No mandatory locks over NFS */
324         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
325                 return -ENOLCK;
326
327         if (NFS_PROTO(inode)->version != 4) {
328                 /* Fake OK code if mounted without NLM support */
329                 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
330                         if (IS_GETLK(cmd))
331                                 status = LOCK_USE_CLNT;
332                         goto out_ok;
333                 }
334         }
335
336         /*
337          * No BSD flocks over NFS allowed.
338          * Note: we could try to fake a POSIX lock request here by
339          * using ((u32) filp | 0x80000000) or some such as the pid.
340          * Not sure whether that would be unique, though, or whether
341          * that would break in other places.
342          */
343         if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
344                 return -ENOLCK;
345
346         /*
347          * Flush all pending writes before doing anything
348          * with locks..
349          */
350         status = filemap_fdatawrite(filp->f_mapping);
351         down(&inode->i_sem);
352         status2 = nfs_wb_all(inode);
353         if (!status)
354                 status = status2;
355         up(&inode->i_sem);
356         status2 = filemap_fdatawait(filp->f_mapping);
357         if (!status)
358                 status = status2;
359         if (status < 0)
360                 return status;
361
362         lock_kernel();
363         status = NFS_PROTO(inode)->lock(filp, cmd, fl);
364         unlock_kernel();
365         if (status < 0)
366                 return status;
367         
368         status = 0;
369
370         /*
371          * Make sure we clear the cache whenever we try to get the lock.
372          * This makes locking act as a cache coherency point.
373          */
374  out_ok:
375         if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
376                 filemap_fdatawrite(filp->f_mapping);
377                 down(&inode->i_sem);
378                 nfs_wb_all(inode);      /* we may have slept */
379                 up(&inode->i_sem);
380                 filemap_fdatawait(filp->f_mapping);
381                 nfs_zap_caches(inode);
382         }
383         return status;
384 }