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/segment.h>
31 #include <asm/system.h>
33 #define NFSDBG_FACILITY NFSDBG_FILE
35 static int nfs_file_mmap(struct file
*, struct vm_area_struct
*);
36 static ssize_t
nfs_file_read(struct file
*, char *, size_t, loff_t
*);
37 static ssize_t
nfs_file_write(struct file
*, const char *, size_t, loff_t
*);
38 static int nfs_file_flush(struct file
*);
39 static int nfs_file_close(struct inode
*, 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 */
50 NULL
, /* no special open is needed */
51 nfs_file_flush
, /* flush */
52 nfs_file_close
, /* 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 nfs_updatepage
, /* updatepage */
80 nfs_revalidate
, /* revalidate */
83 /* Hack for future NFS swap support */
85 # define IS_SWAPFILE(inode) (0)
92 nfs_file_close(struct inode
*inode
, struct file
*file
)
96 dfprintk(VFS
, "nfs: close(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
98 status
= nfs_flush_dirty_pages(inode
, 0, 0, 0);
99 error
= nfs_write_error(inode
);
106 * Flush all dirty pages, and check for write errors.
108 * We should probably do this better - this does get called at every
109 * close, so we should probably just flush the changes that "this"
110 * file has done and report on only those.
112 * Right now we use the "flush everything" approach. Overkill, but
116 nfs_file_flush(struct file
*file
)
118 return nfs_file_close(file
->f_dentry
->d_inode
, file
);
122 nfs_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
124 struct dentry
* dentry
= file
->f_dentry
;
127 dfprintk(VFS
, "nfs: read(%s/%s, %lu@%lu)\n",
128 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
129 (unsigned long) count
, (unsigned long) *ppos
);
131 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
133 result
= generic_file_read(file
, buf
, count
, ppos
);
138 nfs_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
140 struct dentry
*dentry
= file
->f_dentry
;
143 dfprintk(VFS
, "nfs: mmap(%s/%s)\n",
144 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
146 status
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
148 status
= generic_file_mmap(file
, vma
);
153 * Flush any dirty pages for this process, and check for write errors.
154 * The return status from this call provides a reliable indication of
155 * whether any write errors occurred for this process.
158 nfs_fsync(struct file
*file
, struct dentry
*dentry
)
160 struct inode
*inode
= dentry
->d_inode
;
163 dfprintk(VFS
, "nfs: fsync(%x/%ld)\n", inode
->i_dev
, inode
->i_ino
);
165 status
= nfs_flush_dirty_pages(inode
, current
->pid
, 0, 0);
166 error
= nfs_write_error(inode
);
173 * Write to a file (through the page cache).
176 nfs_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
178 struct dentry
* dentry
= file
->f_dentry
;
179 struct inode
* inode
= dentry
->d_inode
;
182 dfprintk(VFS
, "nfs: write(%s/%s (%d), %lu@%lu)\n",
183 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
184 inode
->i_count
, (unsigned long) count
, (unsigned long) *ppos
);
187 printk("nfs_file_write: inode = NULL\n");
191 if (IS_SWAPFILE(inode
))
193 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
198 /* N.B. This should be impossible now -- inodes can't change mode */
199 if (!S_ISREG(inode
->i_mode
)) {
200 printk("nfs_file_write: write to non-file, mode %07o\n",
209 /* Check for an error from a previous async call */
210 result
= nfs_write_error(inode
);
212 result
= generic_file_write(file
, buf
, count
, ppos
);
217 printk(KERN_ERR
"NFS: attempt to write to active swap file!\n");
222 * Lock a (portion of) a file
225 nfs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
227 struct inode
* inode
= filp
->f_dentry
->d_inode
;
230 dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%ld:%ld)\n",
231 inode
->i_dev
, inode
->i_ino
,
232 fl
->fl_type
, fl
->fl_flags
,
233 fl
->fl_start
, fl
->fl_end
);
238 /* No mandatory locks over NFS */
239 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
242 /* Fake OK code if mounted without NLM support */
243 if (NFS_SERVER(inode
)->flags
& NFS_MOUNT_NONLM
)
247 * No BSD flocks over NFS allowed.
248 * Note: we could try to fake a POSIX lock request here by
249 * using ((u32) filp | 0x80000000) or some such as the pid.
250 * Not sure whether that would be unique, though, or whether
251 * that would break in other places.
253 if (!fl
->fl_owner
|| (fl
->fl_flags
& (FL_POSIX
|FL_BROKEN
)) != FL_POSIX
)
256 /* If unlocking a file region, flush dirty pages (unless we've
257 * been killed by a signal, that is). */
258 if (cmd
== F_SETLK
&& fl
->fl_type
== F_UNLCK
259 && !signal_pending(current
)) {
260 status
= nfs_flush_dirty_pages(inode
, current
->pid
,
261 fl
->fl_start
, fl
->fl_end
== NLM_OFFSET_MAX
? 0 :
262 fl
->fl_end
- fl
->fl_start
+ 1);
267 if ((status
= nlmclnt_proc(inode
, cmd
, fl
)) < 0)
270 /* Here, we could turn off write-back of pages in the
271 * locked file region */