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
);
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 revalidate
: nfs_revalidate
,
56 setattr
: nfs_notify_change
,
59 /* Hack for future NFS swap support */
61 # define IS_SWAPFILE(inode) (0)
65 * Flush all dirty pages, and check for write errors.
69 nfs_file_flush(struct file
*file
)
71 struct inode
*inode
= file
->f_dentry
->d_inode
;
74 dfprintk(VFS
, "nfs: flush(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
76 /* Make sure all async reads have been sent off. We don't bother
77 * waiting on them though... */
78 if (file
->f_mode
& FMODE_READ
)
79 nfs_pagein_inode(inode
, 0, 0);
81 status
= nfs_wb_file(inode
, file
);
83 status
= file
->f_error
;
90 nfs_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
92 struct dentry
* dentry
= file
->f_dentry
;
95 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
96 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
97 (unsigned long) count
, (unsigned long) *ppos
);
99 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
101 result
= generic_file_read(file
, buf
, count
, ppos
);
106 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
108 struct dentry
*dentry
= file
->f_dentry
;
111 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
112 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
114 status
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
116 status
= generic_file_mmap(file
, vma
);
121 * Flush any dirty pages for this process, and check for write errors.
122 * The return status from this call provides a reliable indication of
123 * whether any write errors occurred for this process.
126 nfs_fsync(struct file
*file
, struct dentry
*dentry
)
128 struct inode
*inode
= dentry
->d_inode
;
131 dfprintk(VFS
, "nfs: fsync(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
134 status
= nfs_wb_file(inode
, file
);
136 status
= file
->f_error
;
144 * This does the "real" work of the write. The generic routine has
145 * allocated the page, locked it, done all the page alignment stuff
146 * calculations etc. Now we should just copy the data from user
147 * space and write it back to the real medium..
149 * If the writer ends up delaying the write, the writer needs to
150 * increment the page use counts until he is done with the page.
152 static int nfs_prepare_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
155 return nfs_flush_incompatible(file
, page
);
157 static int nfs_commit_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
160 loff_t pos
= ((loff_t
)page
->index
<<PAGE_CACHE_SHIFT
) + to
;
161 struct inode
*inode
= (struct inode
*)page
->mapping
->host
;
165 status
= nfs_updatepage(file
, page
, offset
, to
-offset
);
167 /* most likely it's already done. CHECKME */
168 if (pos
> inode
->i_size
)
174 * The following is used by wait_on_page(), generic_file_readahead()
175 * to initiate the completion of any page readahead operations.
177 static int nfs_sync_page(struct page
*page
)
179 struct inode
*inode
= (struct inode
*)page
->mapping
->host
;
180 unsigned long index
= page_index(page
);
181 unsigned int rpages
, wpages
;
187 rpages
= NFS_SERVER(inode
)->rpages
;
188 result
= nfs_pagein_inode(inode
, index
, rpages
);
191 wpages
= NFS_SERVER(inode
)->wpages
;
192 result
= nfs_sync_file(inode
, NULL
, index
, wpages
, FLUSH_STABLE
);
200 struct address_space_operations nfs_file_aops
= {
201 readpage
: nfs_readpage
,
202 sync_page
: nfs_sync_page
,
203 writepage
: nfs_writepage
,
204 prepare_write
: nfs_prepare_write
,
205 commit_write
: nfs_commit_write
209 * Write to a file (through the page cache).
212 nfs_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
214 struct dentry
* dentry
= file
->f_dentry
;
215 struct inode
* inode
= dentry
->d_inode
;
218 dfprintk(VFS
, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
219 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
220 inode
->i_ino
, (unsigned long) count
, (unsigned long) *ppos
);
223 if (IS_SWAPFILE(inode
))
225 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
233 result
= generic_file_write(file
, buf
, count
, ppos
);
238 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
243 * Lock a (portion of) a file
246 nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
248 struct inode
* inode
= filp
->f_dentry
->d_inode
;
251 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
252 inode
->i_dev
, inode
->i_ino
,
253 fl
->fl_type
, fl
->fl_flags
,
254 (long long)fl
->fl_start
, (long long)fl
->fl_end
);
259 /* No mandatory locks over NFS */
260 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
263 /* Fake OK code if mounted without NLM support */
264 if (NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
) {
266 status
= LOCK_USE_CLNT
;
271 * No BSD flocks over NFS allowed.
272 * Note: we could try to fake a POSIX lock request here by
273 * using ((u32) filp | 0x80000000) or some such as the pid.
274 * Not sure whether that would be unique, though, or whether
275 * that would break in other places.
277 if (!fl
->fl_owner
|| (fl
->fl_flags
& (FL_POSIX
|FL_BROKEN
)) != FL_POSIX
)
281 * Flush all pending writes before doing anything
284 status
= nfs_wb_all(inode
);
288 if ((status
= nlmclnt_proc(inode
, cmd
, fl
)) < 0)
294 * Make sure we re-validate anything we've got cached.
295 * This makes locking act as a cache coherency point.