1 /*
2  *  linux/fs/nfs/file.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
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.
11  *
12  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
13  *
14  *  Total rewrite of read side for new NFS buffer cache.. Linus.
15  *
16  *  nfs regular file handling functions
17  */
18 
19 #include <linux/config.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/fcntl.h>
24 #include <linux/stat.h>
25 #include <linux/nfs_fs.h>
26 #include <linux/nfs_mount.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/pagemap.h>
30 #include <linux/lockd/bind.h>
31 #include <linux/smp_lock.h>
32 
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 
36 #define NFSDBG_FACILITY		NFSDBG_FILE
37 
38 static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
39 static ssize_t nfs_file_read(struct file *, char *, size_t, loff_t *);
40 static ssize_t nfs_file_write(struct file *, const char *, size_t, loff_t *);
41 static int  nfs_file_flush(struct file *);
42 static int  nfs_fsync(struct file *, struct dentry *dentry, int datasync);
43 
44 struct file_operations nfs_file_operations = {
45 	llseek:		generic_file_llseek,
46 	read:		nfs_file_read,
47 	write:		nfs_file_write,
48 	mmap:		nfs_file_mmap,
49 	open:		nfs_open,
50 	flush:		nfs_file_flush,
51 	release:	nfs_release,
52 	fsync:		nfs_fsync,
53 	lock:		nfs_lock,
54 };
55 
56 struct inode_operations nfs_file_inode_operations = {
57 	permission:	nfs_permission,
58 	revalidate:	nfs_revalidate,
59 	setattr:	nfs_notify_change,
60 };
61 
62 /* Hack for future NFS swap support */
63 #ifndef IS_SWAPFILE
64 # define IS_SWAPFILE(inode)	(0)
65 #endif
66 
67 /*
68  * Flush all dirty pages, and check for write errors.
69  *
70  */
71 static int
nfs_file_flush(struct file * file)72 nfs_file_flush(struct file *file)
73 {
74 	struct inode	*inode = file->f_dentry->d_inode;
75 	int		status;
76 
77 	dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
78 
79 	/* Make sure all async reads have been sent off. We don't bother
80 	 * waiting on them though... */
81 	if (file->f_mode & FMODE_READ)
82 		nfs_pagein_inode(inode, 0, 0);
83 
84 	status = nfs_wb_all(inode);
85 	if (!status) {
86 		status = file->f_error;
87 		file->f_error = 0;
88 	}
89 	return status;
90 }
91 
92 static ssize_t
nfs_file_read(struct file * file,char * buf,size_t count,loff_t * ppos)93 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
94 {
95 	struct dentry * dentry = file->f_dentry;
96 	struct inode * inode = dentry->d_inode;
97 	ssize_t result;
98 
99 	dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
100 		dentry->d_parent->d_name.name, dentry->d_name.name,
101 		(unsigned long) count, (unsigned long) *ppos);
102 
103 	result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
104 	if (!result)
105 		result = generic_file_read(file, buf, count, ppos);
106 	return result;
107 }
108 
109 static int
nfs_file_mmap(struct file * file,struct vm_area_struct * vma)110 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
111 {
112 	struct dentry *dentry = file->f_dentry;
113 	struct inode *inode = dentry->d_inode;
114 	int	status;
115 
116 	dfprintk(VFS, "nfs: mmap(%s/%s)\n",
117 		dentry->d_parent->d_name.name, dentry->d_name.name);
118 
119 	status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
120 	if (!status)
121 		status = generic_file_mmap(file, vma);
122 	return status;
123 }
124 
125 /*
126  * Flush any dirty pages for this process, and check for write errors.
127  * The return status from this call provides a reliable indication of
128  * whether any write errors occurred for this process.
129  */
130 static int
nfs_fsync(struct file * file,struct dentry * dentry,int datasync)131 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
132 {
133 	struct inode *inode = dentry->d_inode;
134 	int status;
135 
136 	dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
137 
138 	lock_kernel();
139 	status = nfs_wb_all(inode);
140 	if (!status) {
141 		status = file->f_error;
142 		file->f_error = 0;
143 	}
144 	unlock_kernel();
145 	return status;
146 }
147 
148 /*
149  * This does the "real" work of the write. The generic routine has
150  * allocated the page, locked it, done all the page alignment stuff
151  * calculations etc. Now we should just copy the data from user
152  * space and write it back to the real medium..
153  *
154  * If the writer ends up delaying the write, the writer needs to
155  * increment the page use counts until he is done with the page.
156  */
nfs_prepare_write(struct file * file,struct page * page,unsigned offset,unsigned to)157 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
158 {
159 	return nfs_flush_incompatible(file, page);
160 }
161 
nfs_commit_write(struct file * file,struct page * page,unsigned offset,unsigned to)162 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
163 {
164 	long status;
165 
166 	lock_kernel();
167 	status = nfs_updatepage(file, page, offset, to-offset);
168 	unlock_kernel();
169 	return status;
170 }
171 
172 /*
173  * The following is used by wait_on_page(), generic_file_readahead()
174  * to initiate the completion of any page readahead operations.
175  */
nfs_sync_page(struct page * page)176 static int nfs_sync_page(struct page *page)
177 {
178 	struct address_space *mapping;
179 	struct inode	*inode;
180 	unsigned long	index = page_index(page);
181 	unsigned int	rpages;
182 	int		result;
183 
184 	mapping = page->mapping;
185 	if (!mapping)
186 		return 0;
187 	inode = mapping->host;
188 	if (!inode)
189 		return 0;
190 
191 	NFS_SetPageSync(page);
192 	rpages = NFS_SERVER(inode)->rpages;
193 	result = nfs_pagein_inode(inode, index, rpages);
194 	if (result < 0)
195 		return result;
196 	return 0;
197 }
198 
199 struct address_space_operations nfs_file_aops = {
200 	readpage: nfs_readpage,
201 	sync_page: nfs_sync_page,
202 	writepage: nfs_writepage,
203 #ifdef CONFIG_NFS_DIRECTIO
204 	direct_fileIO: nfs_direct_IO,
205 #endif
206 	prepare_write: nfs_prepare_write,
207 	commit_write: nfs_commit_write
208 };
209 
210 /*
211  * Write to a file (through the page cache).
212  */
213 static ssize_t
nfs_file_write(struct file * file,const char * buf,size_t count,loff_t * ppos)214 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
215 {
216 	struct dentry * dentry = file->f_dentry;
217 	struct inode * inode = dentry->d_inode;
218 	ssize_t result;
219 
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);
223 
224 	result = -EBUSY;
225 	if (IS_SWAPFILE(inode))
226 		goto out_swapfile;
227 	result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
228 	if (result)
229 		goto out;
230 
231 	result = count;
232 	if (!count)
233 		goto out;
234 
235 	result = generic_file_write(file, buf, count, ppos);
236 out:
237 	return result;
238 
239 out_swapfile:
240 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
241 	goto out;
242 }
243 
244 static int
do_getlk(struct inode * inode,int cmd,struct file_lock * fl)245 do_getlk(struct inode *inode, int cmd, struct file_lock *fl)
246 {
247 	int status;
248 
249 	lock_kernel();
250 	status = nlmclnt_proc(inode, cmd, fl);
251 	unlock_kernel();
252 	return status;
253 }
254 
255 static int
do_unlk(struct inode * inode,int cmd,struct file_lock * fl)256 do_unlk(struct inode *inode, int cmd, struct file_lock *fl)
257 {
258 	sigset_t oldset;
259 	int status;
260 
261 	rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
262 	/*
263 	 * Flush all pending writes before doing anything
264 	 * with locks..
265 	 */
266 	filemap_fdatasync(inode->i_mapping);
267 	down(&inode->i_sem);
268 	nfs_wb_all(inode);
269 	up(&inode->i_sem);
270 	filemap_fdatawait(inode->i_mapping);
271 
272 	/* NOTE: special case
273 	 *	If we're signalled while cleaning up locks on process exit, we
274 	 *	still need to complete the unlock.
275 	 */
276 	lock_kernel();
277 	status = nlmclnt_proc(inode, cmd, fl);
278 	unlock_kernel();
279 	rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
280 	return status;
281 }
282 
283 static int
do_setlk(struct file * filp,int cmd,struct file_lock * fl)284 do_setlk(struct file *filp, int cmd, struct file_lock *fl)
285 {
286 	struct inode *inode = filp->f_dentry->d_inode;
287 	int status;
288 
289 	/*
290 	 * Flush all pending writes before doing anything
291 	 * with locks..
292 	 */
293 	status = filemap_fdatasync(inode->i_mapping);
294 	if (status == 0) {
295 		down(&inode->i_sem);
296 		status = nfs_wb_all(inode);
297 		up(&inode->i_sem);
298 		if (status == 0)
299 			status = filemap_fdatawait(inode->i_mapping);
300 	}
301 	if (status < 0)
302 		return status;
303 
304 	lock_kernel();
305 	status = nlmclnt_proc(inode, cmd, fl);
306 	/* If we were signalled we still need to ensure that
307 	 * we clean up any state on the server. We therefore
308 	 * record the lock call as having succeeded in order to
309 	 * ensure that locks_remove_posix() cleans it out when
310 	 * the process exits.
311 	 */
312 	if (status == -EINTR || status == -ERESTARTSYS)
313 		posix_lock_file(filp, fl, 0);
314 	unlock_kernel();
315 	if (status < 0)
316 		return status;
317 
318 	/*
319 	 * Make sure we clear the cache whenever we try to get the lock.
320 	 * This makes locking act as a cache coherency point.
321 	 */
322 	filemap_fdatasync(inode->i_mapping);
323 	down(&inode->i_sem);
324 	nfs_wb_all(inode);      /* we may have slept */
325 	up(&inode->i_sem);
326 	filemap_fdatawait(inode->i_mapping);
327 	nfs_zap_caches(inode);
328 	return 0;
329 }
330 
331 /*
332  * Lock a (portion of) a file
333  */
334 int
nfs_lock(struct file * filp,int cmd,struct file_lock * fl)335 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
336 {
337 	struct inode * inode = filp->f_dentry->d_inode;
338 
339 	dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
340 			inode->i_dev, inode->i_ino,
341 			fl->fl_type, fl->fl_flags,
342 			(long long)fl->fl_start, (long long)fl->fl_end);
343 
344 	if (!inode)
345 		return -EINVAL;
346 
347 	/* No mandatory locks over NFS */
348 	if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
349 		return -ENOLCK;
350 
351 	/* Fake OK code if mounted without NLM support */
352 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
353 		if (IS_GETLK(cmd))
354 			return LOCK_USE_CLNT;
355 		return 0;
356 	}
357 
358 	/*
359 	 * No BSD flocks over NFS allowed.
360 	 * Note: we could try to fake a POSIX lock request here by
361 	 * using ((u32) filp | 0x80000000) or some such as the pid.
362 	 * Not sure whether that would be unique, though, or whether
363 	 * that would break in other places.
364 	 */
365 	if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
366 		return -ENOLCK;
367 
368 	if (IS_GETLK(cmd))
369 		return do_getlk(inode, cmd, fl);
370 	if (fl->fl_type == F_UNLCK)
371 		return do_unlk(inode, cmd, fl);
372 	return do_setlk(filp, cmd, fl);
373 }
374