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>
30 #include <asm/uaccess.h>
31 #include <asm/segment.h>
32 #include <asm/system.h>
34 #define NFSDBG_FACILITY NFSDBG_FILE
36 static int nfs_file_mmap(struct file
*, struct vm_area_struct
*);
37 static ssize_t
nfs_file_read(struct file
*, char *, size_t, loff_t
*);
38 static ssize_t
nfs_file_write(struct file
*, const char *, size_t, loff_t
*);
39 static int nfs_file_flush(struct file
*);
40 static int nfs_fsync(struct file
*, struct dentry
*dentry
);
42 static struct file_operations nfs_file_operations
= {
43 NULL
, /* lseek - default */
44 nfs_file_read
, /* read */
45 nfs_file_write
, /* write */
46 NULL
, /* readdir - bad */
47 NULL
, /* select - default */
48 NULL
, /* ioctl - default */
49 nfs_file_mmap
, /* mmap */
51 nfs_file_flush
, /* flush */
52 nfs_release
, /* release */
53 nfs_fsync
, /* fsync */
55 NULL
, /* check_media_change */
56 NULL
, /* revalidate */
60 struct inode_operations nfs_file_inode_operations
= {
61 &nfs_file_operations
, /* default file operations */
72 NULL
, /* follow_link */
73 nfs_readpage
, /* readpage */
74 nfs_writepage
, /* writepage */
77 NULL
, /* permission */
79 NULL
, /* updatepage */
80 nfs_revalidate
, /* revalidate */
83 /* Hack for future NFS swap support */
85 # define IS_SWAPFILE(inode) (0)
89 * Flush all dirty pages, and check for write errors.
93 nfs_file_flush(struct file
*file
)
95 struct inode
*inode
= file
->f_dentry
->d_inode
;
98 dfprintk(VFS
, "nfs: flush(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
100 status
= nfs_wb_file(inode
, file
);
102 status
= file
->f_error
;
109 nfs_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
111 struct dentry
* dentry
= file
->f_dentry
;
114 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
115 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
116 (unsigned long) count
, (unsigned long) *ppos
);
118 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
120 result
= generic_file_read(file
, buf
, count
, ppos
);
125 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
127 struct dentry
*dentry
= file
->f_dentry
;
130 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
131 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
133 status
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
135 status
= generic_file_mmap(file
, vma
);
140 * Flush any dirty pages for this process, and check for write errors.
141 * The return status from this call provides a reliable indication of
142 * whether any write errors occurred for this process.
145 nfs_fsync(struct file
*file
, struct dentry
*dentry
)
147 struct inode
*inode
= dentry
->d_inode
;
150 dfprintk(VFS
, "nfs: fsync(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
152 status
= nfs_wb_file(inode
, file
);
154 status
= file
->f_error
;
161 * This does the "real" work of the write. The generic routine has
162 * allocated the page, locked it, done all the page alignment stuff
163 * calculations etc. Now we should just copy the data from user
164 * space and write it back to the real medium..
166 * If the writer ends up delaying the write, the writer needs to
167 * increment the page use counts until he is done with the page.
169 static long nfs_write_one_page(struct file
*file
, struct page
*page
, unsigned long offset
, unsigned long bytes
, const char * buf
)
173 bytes
-= copy_from_user((u8
*)page_address(page
) + offset
, buf
, bytes
);
176 status
= nfs_updatepage(file
, page
, offset
, bytes
);
181 * Write to a file (through the page cache).
184 nfs_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
186 struct dentry
* dentry
= file
->f_dentry
;
187 struct inode
* inode
= dentry
->d_inode
;
190 dfprintk(VFS
, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
191 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
192 inode
->i_ino
, (unsigned long) count
, (unsigned long) *ppos
);
195 if (IS_SWAPFILE(inode
))
197 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
205 result
= generic_file_write(file
, buf
, count
, ppos
, nfs_write_one_page
);
210 printk(KERN_INFO
"NFS: attempt to write to active swap file!\n");
215 * Lock a (portion of) a file
218 nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
220 struct inode
* inode
= filp
->f_dentry
->d_inode
;
223 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
224 inode
->i_dev
, inode
->i_ino
,
225 fl
->fl_type
, fl
->fl_flags
,
226 fl
->fl_start
, fl
->fl_end
);
231 /* No mandatory locks over NFS */
232 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
235 /* Fake OK code if mounted without NLM support */
236 if (NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
)
240 * No BSD flocks over NFS allowed.
241 * Note: we could try to fake a POSIX lock request here by
242 * using ((u32) filp | 0x80000000) or some such as the pid.
243 * Not sure whether that would be unique, though, or whether
244 * that would break in other places.
246 if (!fl
->fl_owner
|| (fl
->fl_flags
& (FL_POSIX
|FL_BROKEN
)) != FL_POSIX
)
250 * Flush all pending writes before doing anything
253 status
= nfs_wb_all(inode
);
257 if ((status
= nlmclnt_proc(inode
, cmd
, fl
)) < 0)
261 * Make sure we re-validate anything we've got cached.
262 * This makes locking act as a cache coherency point.