vserver 1.9.5.x5
[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 #include "delegation.h"
35
36 #define NFSDBG_FACILITY         NFSDBG_FILE
37
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);
47
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,
62 };
63
64 struct inode_operations nfs_file_inode_operations = {
65         .permission     = nfs_permission,
66         .getattr        = nfs_getattr,
67         .setattr        = nfs_setattr,
68 };
69
70 /* Hack for future NFS swap support */
71 #ifndef IS_SWAPFILE
72 # define IS_SWAPFILE(inode)     (0)
73 #endif
74
75 static int nfs_check_flags(int flags)
76 {
77         if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
78                 return -EINVAL;
79
80         return 0;
81 }
82
83 /*
84  * Open file
85  */
86 static int
87 nfs_file_open(struct inode *inode, struct file *filp)
88 {
89         struct nfs_server *server = NFS_SERVER(inode);
90         int (*open)(struct inode *, struct file *);
91         int res;
92
93         res = nfs_check_flags(filp->f_flags);
94         if (res)
95                 return res;
96
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;
103 }
104
105 static int
106 nfs_file_release(struct inode *inode, struct file *filp)
107 {
108         return NFS_PROTO(inode)->file_release(inode, filp);
109 }
110
111 /*
112  * Flush all dirty pages, and check for write errors.
113  *
114  */
115 static int
116 nfs_file_flush(struct file *file)
117 {
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;
121
122         dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
123
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);
134         }
135         unlock_kernel();
136         return status;
137 }
138
139 static ssize_t
140 nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
141 {
142         struct dentry * dentry = iocb->ki_filp->f_dentry;
143         struct inode * inode = dentry->d_inode;
144         ssize_t result;
145
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
150
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);
154
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;
159 }
160
161 static ssize_t
162 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
163                 read_actor_t actor, void *target)
164 {
165         struct dentry *dentry = filp->f_dentry;
166         struct inode *inode = dentry->d_inode;
167         ssize_t res;
168
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);
172
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;
177 }
178
179 static int
180 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
181 {
182         struct dentry *dentry = file->f_dentry;
183         struct inode *inode = dentry->d_inode;
184         int     status;
185
186         dfprintk(VFS, "nfs: mmap(%s/%s)\n",
187                 dentry->d_parent->d_name.name, dentry->d_name.name);
188
189         status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
190         if (!status)
191                 status = generic_file_mmap(file, vma);
192         return status;
193 }
194
195 /*
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.
199  */
200 static int
201 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
202 {
203         struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
204         struct inode *inode = dentry->d_inode;
205         int status;
206
207         dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
208
209         lock_kernel();
210         status = nfs_wb_all(inode);
211         if (!status) {
212                 status = ctx->error;
213                 ctx->error = 0;
214         }
215         unlock_kernel();
216         return status;
217 }
218
219 /*
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..
224  *
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.
227  */
228 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
229 {
230         return nfs_flush_incompatible(file, page);
231 }
232
233 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
234 {
235         long status;
236
237         lock_kernel();
238         status = nfs_updatepage(file, page, offset, to-offset);
239         unlock_kernel();
240         return status;
241 }
242
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
254 };
255
256 /* 
257  * Write to a file (through the page cache).
258  */
259 static ssize_t
260 nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
261 {
262         struct dentry * dentry = iocb->ki_filp->f_dentry;
263         struct inode * inode = dentry->d_inode;
264         ssize_t result;
265
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
270
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);
274
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;
281
282         result = count;
283         if (!count)
284                 goto out;
285
286         result = generic_file_aio_write(iocb, buf, count, pos);
287 out:
288         return result;
289
290 out_swapfile:
291         printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
292         goto out;
293 }
294
295 static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
296 {
297         struct inode *inode = filp->f_mapping->host;
298         int status = 0;
299
300         lock_kernel();
301         /* Use local locking if mounted with "-onolock" */
302         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
303                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
304         else {
305                 struct file_lock *cfl = posix_test_lock(filp, fl);
306
307                 fl->fl_type = F_UNLCK;
308                 if (cfl != NULL)
309                         memcpy(fl, cfl, sizeof(*fl));
310         }
311         unlock_kernel();
312         return status;
313 }
314
315 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
316 {
317         struct inode *inode = filp->f_mapping->host;
318         sigset_t oldset;
319         int status;
320
321         rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
322         /*
323          * Flush all pending writes before doing anything
324          * with locks..
325          */
326         filemap_fdatawrite(filp->f_mapping);
327         down(&inode->i_sem);
328         nfs_wb_all(inode);
329         up(&inode->i_sem);
330         filemap_fdatawait(filp->f_mapping);
331
332         /* NOTE: special case
333          *      If we're signalled while cleaning up locks on process exit, we
334          *      still need to complete the unlock.
335          */
336         lock_kernel();
337         /* Use local locking if mounted with "-onolock" */
338         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
339                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
340         else
341                 status = posix_lock_file_wait(filp, fl);
342         unlock_kernel();
343         rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
344         return status;
345 }
346
347 static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
348 {
349         struct inode *inode = filp->f_mapping->host;
350         sigset_t oldset;
351         int status;
352
353         rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
354         /*
355          * Flush all pending writes before doing anything
356          * with locks..
357          */
358         status = filemap_fdatawrite(filp->f_mapping);
359         if (status == 0) {
360                 down(&inode->i_sem);
361                 status = nfs_wb_all(inode);
362                 up(&inode->i_sem);
363                 if (status == 0)
364                         status = filemap_fdatawait(filp->f_mapping);
365         }
366         if (status < 0)
367                 goto out;
368
369         lock_kernel();
370         /* Use local locking if mounted with "-onolock" */
371         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
372                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
373                 /* If we were signalled we still need to ensure that
374                  * we clean up any state on the server. We therefore
375                  * record the lock call as having succeeded in order to
376                  * ensure that locks_remove_posix() cleans it out when
377                  * the process exits.
378                  */
379                 if (status == -EINTR || status == -ERESTARTSYS)
380                         posix_lock_file_wait(filp, fl);
381         } else
382                 status = posix_lock_file_wait(filp, fl);
383         unlock_kernel();
384         if (status < 0)
385                 goto out;
386         /*
387          * Make sure we clear the cache whenever we try to get the lock.
388          * This makes locking act as a cache coherency point.
389          */
390         filemap_fdatawrite(filp->f_mapping);
391         down(&inode->i_sem);
392         nfs_wb_all(inode);      /* we may have slept */
393         up(&inode->i_sem);
394         filemap_fdatawait(filp->f_mapping);
395         nfs_zap_caches(inode);
396 out:
397         rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
398         return status;
399 }
400
401 /*
402  * Lock a (portion of) a file
403  */
404 int
405 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
406 {
407         struct inode * inode = filp->f_mapping->host;
408
409         dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
410                         inode->i_sb->s_id, inode->i_ino,
411                         fl->fl_type, fl->fl_flags,
412                         (long long)fl->fl_start, (long long)fl->fl_end);
413
414         if (!inode)
415                 return -EINVAL;
416
417         /* No mandatory locks over NFS */
418         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
419                 return -ENOLCK;
420
421         /*
422          * No BSD flocks over NFS allowed.
423          * Note: we could try to fake a POSIX lock request here by
424          * using ((u32) filp | 0x80000000) or some such as the pid.
425          * Not sure whether that would be unique, though, or whether
426          * that would break in other places.
427          */
428         if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
429                 return -ENOLCK;
430
431         if (IS_GETLK(cmd))
432                 return do_getlk(filp, cmd, fl);
433         if (fl->fl_type == F_UNLCK)
434                 return do_unlk(filp, cmd, fl);
435         return do_setlk(filp, cmd, fl);
436 }