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