ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 *);
42 static ssize_t nfs_file_read(struct kiocb *, char *, size_t, loff_t);
43 static ssize_t nfs_file_write(struct kiocb *, const char *, 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 * 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         dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
158                 dentry->d_parent->d_name.name, dentry->d_name.name,
159                 (unsigned long) count, (unsigned long) pos);
160
161         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
162         if (!result)
163                 result = generic_file_aio_read(iocb, buf, count, pos);
164         return result;
165 }
166
167 static ssize_t
168 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
169                 read_actor_t actor, void *target)
170 {
171         struct dentry *dentry = filp->f_dentry;
172         struct inode *inode = dentry->d_inode;
173         ssize_t res;
174
175         dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
176                 dentry->d_parent->d_name.name, dentry->d_name.name,
177                 (unsigned long) count, (unsigned long long) *ppos);
178
179         res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
180         if (!res)
181                 res = generic_file_sendfile(filp, ppos, count, actor, target);
182         return res;
183 }
184
185 static int
186 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
187 {
188         struct dentry *dentry = file->f_dentry;
189         struct inode *inode = dentry->d_inode;
190         int     status;
191
192         dfprintk(VFS, "nfs: mmap(%s/%s)\n",
193                 dentry->d_parent->d_name.name, dentry->d_name.name);
194
195         status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
196         if (!status)
197                 status = generic_file_mmap(file, vma);
198         return status;
199 }
200
201 /*
202  * Flush any dirty pages for this process, and check for write errors.
203  * The return status from this call provides a reliable indication of
204  * whether any write errors occurred for this process.
205  */
206 static int
207 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
208 {
209         struct inode *inode = dentry->d_inode;
210         int status;
211
212         dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
213
214         lock_kernel();
215         status = nfs_wb_all(inode);
216         if (!status) {
217                 status = file->f_error;
218                 file->f_error = 0;
219         }
220         unlock_kernel();
221         return status;
222 }
223
224 /*
225  * This does the "real" work of the write. The generic routine has
226  * allocated the page, locked it, done all the page alignment stuff
227  * calculations etc. Now we should just copy the data from user
228  * space and write it back to the real medium..
229  *
230  * If the writer ends up delaying the write, the writer needs to
231  * increment the page use counts until he is done with the page.
232  */
233 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
234 {
235         return nfs_flush_incompatible(file, page);
236 }
237
238 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
239 {
240         long status;
241
242         lock_kernel();
243         status = nfs_updatepage(file, page, offset, to-offset);
244         unlock_kernel();
245         return status;
246 }
247
248 struct address_space_operations nfs_file_aops = {
249         .readpage = nfs_readpage,
250         .readpages = nfs_readpages,
251         .set_page_dirty = __set_page_dirty_nobuffers,
252         .writepage = nfs_writepage,
253         .writepages = nfs_writepages,
254         .prepare_write = nfs_prepare_write,
255         .commit_write = nfs_commit_write,
256 #ifdef CONFIG_NFS_DIRECTIO
257         .direct_IO = nfs_direct_IO,
258 #endif
259 };
260
261 /* 
262  * Write to a file (through the page cache).
263  */
264 static ssize_t
265 nfs_file_write(struct kiocb *iocb, const char *buf, size_t count, loff_t pos)
266 {
267         struct dentry * dentry = iocb->ki_filp->f_dentry;
268         struct inode * inode = dentry->d_inode;
269         ssize_t result;
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 /*
296  * Lock a (portion of) a file
297  */
298 int
299 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
300 {
301         struct inode * inode = filp->f_mapping->host;
302         int     status = 0;
303         int     status2;
304
305         dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
306                         inode->i_sb->s_id, inode->i_ino,
307                         fl->fl_type, fl->fl_flags,
308                         (long long)fl->fl_start, (long long)fl->fl_end);
309
310         if (!inode)
311                 return -EINVAL;
312
313         /* No mandatory locks over NFS */
314         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
315                 return -ENOLCK;
316
317         if (NFS_PROTO(inode)->version != 4) {
318                 /* Fake OK code if mounted without NLM support */
319                 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
320                         if (IS_GETLK(cmd))
321                                 status = LOCK_USE_CLNT;
322                         goto out_ok;
323                 }
324         }
325
326         /*
327          * No BSD flocks over NFS allowed.
328          * Note: we could try to fake a POSIX lock request here by
329          * using ((u32) filp | 0x80000000) or some such as the pid.
330          * Not sure whether that would be unique, though, or whether
331          * that would break in other places.
332          */
333         if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
334                 return -ENOLCK;
335
336         /*
337          * Flush all pending writes before doing anything
338          * with locks..
339          */
340         status = filemap_fdatawrite(filp->f_mapping);
341         down(&inode->i_sem);
342         status2 = nfs_wb_all(inode);
343         if (!status)
344                 status = status2;
345         up(&inode->i_sem);
346         status2 = filemap_fdatawait(filp->f_mapping);
347         if (!status)
348                 status = status2;
349         if (status < 0)
350                 return status;
351
352         lock_kernel();
353         status = NFS_PROTO(inode)->lock(filp, cmd, fl);
354         unlock_kernel();
355         if (status < 0)
356                 return status;
357         
358         status = 0;
359
360         /*
361          * Make sure we clear the cache whenever we try to get the lock.
362          * This makes locking act as a cache coherency point.
363          */
364  out_ok:
365         if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
366                 filemap_fdatawrite(filp->f_mapping);
367                 down(&inode->i_sem);
368                 nfs_wb_all(inode);      /* we may have slept */
369                 up(&inode->i_sem);
370                 filemap_fdatawait(filp->f_mapping);
371                 nfs_zap_caches(inode);
372         }
373         return status;
374 }