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/sched.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>
27 #include <linux/malloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/lockd/bind.h>
30 #include <linux/smp_lock.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
35 #define NFSDBG_FACILITY NFSDBG_FILE
37 static int nfs_file_mmap(struct file
*, struct vm_area_struct
*);
38 static ssize_t
nfs_file_read(struct file
*, char *, size_t, loff_t
*);
39 static ssize_t
nfs_file_write(struct file
*, const char *, size_t, loff_t
*);
40 static int nfs_file_flush(struct file
*);
41 static int nfs_fsync(struct file
*, struct dentry
*dentry
, int datasync
);
43 struct file_operations nfs_file_operations
= {
45 write
: nfs_file_write
,
48 flush
: nfs_file_flush
,
54 struct inode_operations nfs_file_inode_operations
= {
55 permission
: nfs_permission
,
56 revalidate
: nfs_revalidate
,
57 setattr
: nfs_notify_change
,
60 /* Hack for future NFS swap support */
62 # define IS_SWAPFILE(inode) (0)
66 * Flush all dirty pages, and check for write errors.
70 nfs_file_flush(struct file
*file
)
72 struct inode
*inode
= file
->f_dentry
->d_inode
;
75 dfprintk(VFS
, "nfs: flush(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
77 /* Make sure all async reads have been sent off. We don't bother
78 * waiting on them though... */
79 if (file
->f_mode
& FMODE_READ
)
80 nfs_pagein_inode(inode
, 0, 0);
82 status
= nfs_wb_file(inode
, file
);
84 status
= file
->f_error
;
91 nfs_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
93 struct dentry
* dentry
= file
->f_dentry
;
94 struct inode
* inode
= dentry
->d_inode
;
97 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
98 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
99 (unsigned long) count
, (unsigned long) *ppos
);
101 result
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
103 result
= generic_file_read(file
, buf
, count
, ppos
);
108 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
110 struct dentry
*dentry
= file
->f_dentry
;
111 struct inode
*inode
= dentry
->d_inode
;
114 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
115 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
117 status
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
119 status
= generic_file_mmap(file
, vma
);
124 * Flush any dirty pages for this process, and check for write errors.
125 * The return status from this call provides a reliable indication of
126 * whether any write errors occurred for this process.
129 nfs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
131 struct inode
*inode
= dentry
->d_inode
;
134 dfprintk(VFS
, "nfs: fsync(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
137 status
= nfs_wb_file(inode
, file
);
139 status
= file
->f_error
;
147 * This does the "real" work of the write. The generic routine has
148 * allocated the page, locked it, done all the page alignment stuff
149 * calculations etc. Now we should just copy the data from user
150 * space and write it back to the real medium..
152 * If the writer ends up delaying the write, the writer needs to
153 * increment the page use counts until he is done with the page.
155 static int nfs_prepare_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
158 return nfs_flush_incompatible(file
, page
);
160 static int nfs_commit_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
163 loff_t pos
= ((loff_t
)page
->index
<<PAGE_CACHE_SHIFT
) + to
;
164 struct inode
*inode
= page
->mapping
->host
;
168 status
= nfs_updatepage(file
, page
, offset
, to
-offset
);
170 /* most likely it's already done. CHECKME */
171 if (pos
> inode
->i_size
)
177 * The following is used by wait_on_page(), generic_file_readahead()
178 * to initiate the completion of any page readahead operations.
180 static int nfs_sync_page(struct page
*page
)
182 struct address_space
*mapping
;
184 unsigned long index
= page_index(page
);
188 mapping
= page
->mapping
;
191 inode
= mapping
->host
;
195 rpages
= NFS_SERVER(inode
)->rpages
;
196 result
= nfs_pagein_inode(inode
, index
, rpages
);
202 struct address_space_operations nfs_file_aops
= {
203 readpage
: nfs_readpage
,
204 sync_page
: nfs_sync_page
,
205 writepage
: nfs_writepage
,
206 prepare_write
: nfs_prepare_write
,
207 commit_write
: nfs_commit_write
211 * Write to a file (through the page cache).
214 nfs_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
216 struct dentry
* dentry
= file
->f_dentry
;
217 struct inode
* inode
= dentry
->d_inode
;
220 dfprintk(VFS
, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
221 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
222 inode
->i_ino
, (unsigned long) count
, (unsigned long) *ppos
);
225 if (IS_SWAPFILE(inode
))
227 result
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
235 result
= generic_file_write(file
, buf
, count
, ppos
);
240 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
245 * Lock a (portion of) a file
248 nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
250 struct inode
* inode
= filp
->f_dentry
->d_inode
;
253 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
254 inode
->i_dev
, inode
->i_ino
,
255 fl
->fl_type
, fl
->fl_flags
,
256 (long long)fl
->fl_start
, (long long)fl
->fl_end
);
261 /* No mandatory locks over NFS */
262 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
265 /* Fake OK code if mounted without NLM support */
266 if (NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
) {
268 status
= LOCK_USE_CLNT
;
273 * No BSD flocks over NFS allowed.
274 * Note: we could try to fake a POSIX lock request here by
275 * using ((u32) filp | 0x80000000) or some such as the pid.
276 * Not sure whether that would be unique, though, or whether
277 * that would break in other places.
279 if (!fl
->fl_owner
|| (fl
->fl_flags
& (FL_POSIX
|FL_BROKEN
)) != FL_POSIX
)
283 * Flush all pending writes before doing anything
286 down(&filp
->f_dentry
->d_inode
->i_sem
);
287 status
= nfs_wb_all(inode
);
288 up(&filp
->f_dentry
->d_inode
->i_sem
);
292 if ((status
= nlmclnt_proc(inode
, cmd
, fl
)) < 0)
298 * Make sure we clear the cache whenever we try to get the lock.
299 * This makes locking act as a cache coherency point.
302 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) && fl
->fl_type
!= F_UNLCK
) {
303 down(&filp
->f_dentry
->d_inode
->i_sem
);
304 nfs_wb_all(inode
); /* we may have slept */
305 nfs_zap_caches(inode
);
306 up(&filp
->f_dentry
->d_inode
->i_sem
);