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>
26 #include <linux/malloc.h>
27 #include <linux/pagemap.h>
28 #include <linux/lockd/bind.h>
29 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <asm/segment.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 static struct file_operations nfs_file_operations
= {
44 NULL
, /* lseek - default */
45 nfs_file_read
, /* read */
46 nfs_file_write
, /* write */
47 NULL
, /* readdir - bad */
48 NULL
, /* select - default */
49 NULL
, /* ioctl - default */
50 nfs_file_mmap
, /* mmap */
52 nfs_file_flush
, /* flush */
53 nfs_release
, /* release */
54 nfs_fsync
, /* fsync */
56 NULL
, /* check_media_change */
57 NULL
, /* revalidate */
61 struct inode_operations nfs_file_inode_operations
= {
62 &nfs_file_operations
, /* default file operations */
73 NULL
, /* follow_link */
75 nfs_readpage
, /* readpage */
76 nfs_writepage
, /* writepage */
78 NULL
, /* permission */
79 nfs_revalidate
, /* revalidate */
82 /* Hack for future NFS swap support */
84 # define IS_SWAPFILE(inode) (0)
88 * Flush all dirty pages, and check for write errors.
92 nfs_file_flush(struct file
*file
)
94 struct inode
*inode
= file
->f_dentry
->d_inode
;
97 dfprintk(VFS
, "nfs: flush(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
99 status
= nfs_wb_file(inode
, file
);
101 status
= file
->f_error
;
108 nfs_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
110 struct dentry
* dentry
= file
->f_dentry
;
113 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
114 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
115 (unsigned long) count
, (unsigned long) *ppos
);
117 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
119 result
= generic_file_read(file
, buf
, count
, ppos
);
124 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
126 struct dentry
*dentry
= file
->f_dentry
;
129 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
130 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
132 status
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
134 status
= generic_file_mmap(file
, vma
);
139 * Flush any dirty pages for this process, and check for write errors.
140 * The return status from this call provides a reliable indication of
141 * whether any write errors occurred for this process.
144 nfs_fsync(struct file
*file
, struct dentry
*dentry
)
146 struct inode
*inode
= dentry
->d_inode
;
149 dfprintk(VFS
, "nfs: fsync(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
151 status
= nfs_wb_file(inode
, file
);
153 status
= file
->f_error
;
160 * This does the "real" work of the write. The generic routine has
161 * allocated the page, locked it, done all the page alignment stuff
162 * calculations etc. Now we should just copy the data from user
163 * space and write it back to the real medium..
165 * If the writer ends up delaying the write, the writer needs to
166 * increment the page use counts until he is done with the page.
168 static int nfs_write_one_page(struct file
*file
, struct page
*page
, unsigned long offset
, unsigned long bytes
, const char * buf
)
172 bytes
-= copy_from_user((u8
*)kmap(page
) + offset
, buf
, bytes
);
177 status
= nfs_updatepage(file
, page
, offset
, bytes
);
184 * Write to a file (through the page cache).
187 nfs_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
189 struct dentry
* dentry
= file
->f_dentry
;
190 struct inode
* inode
= dentry
->d_inode
;
193 dfprintk(VFS
, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
194 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
195 inode
->i_ino
, (unsigned long) count
, (unsigned long) *ppos
);
198 if (IS_SWAPFILE(inode
))
200 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
208 result
= generic_file_write(file
, buf
, count
, ppos
, nfs_write_one_page
);
213 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
218 * Lock a (portion of) a file
221 nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
223 struct inode
* inode
= filp
->f_dentry
->d_inode
;
226 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
227 inode
->i_dev
, inode
->i_ino
,
228 fl
->fl_type
, fl
->fl_flags
,
229 fl
->fl_start
, fl
->fl_end
);
234 /* No mandatory locks over NFS */
235 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
238 /* Fake OK code if mounted without NLM support */
239 if (NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
) {
241 status
= LOCK_USE_CLNT
;
246 * No BSD flocks over NFS allowed.
247 * Note: we could try to fake a POSIX lock request here by
248 * using ((u32) filp | 0x80000000) or some such as the pid.
249 * Not sure whether that would be unique, though, or whether
250 * that would break in other places.
252 if (!fl
->fl_owner
|| (fl
->fl_flags
& (FL_POSIX
|FL_BROKEN
)) != FL_POSIX
)
256 * Flush all pending writes before doing anything
259 status
= nfs_wb_all(inode
);
263 if ((status
= nlmclnt_proc(inode
, cmd
, fl
)) < 0)
269 * Make sure we re-validate anything we've got cached.
270 * This makes locking act as a cache coherency point.