1 #define MSNFS	/* HACK HACK */
2 /*
3  * linux/fs/nfsd/vfs.c
4  *
5  * File operations used by nfsd. Some of these have been ripped from
6  * other parts of the kernel because they weren't in ksyms.c, others
7  * are partial duplicates with added or changed functionality.
8  *
9  * Note that several functions dget() the dentry upon which they want
10  * to act, most notably those that create directory entries. Response
11  * dentry's are dput()'d if necessary in the release callback.
12  * So if you notice code paths that apparently fail to dput() the
13  * dentry, don't worry--they have been taken care of.
14  *
15  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
16  */
17 
18 #include <linux/config.h>
19 #include <linux/version.h>
20 #include <linux/string.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/locks.h>
24 #include <linux/fs.h>
25 #include <linux/major.h>
26 #include <linux/ext2_fs.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stat.h>
29 #include <linux/fcntl.h>
30 #include <linux/net.h>
31 #include <linux/unistd.h>
32 #include <linux/slab.h>
33 #include <linux/in.h>
34 #define __NO_VERSION__
35 #include <linux/module.h>
36 
37 #include <linux/sunrpc/svc.h>
38 #include <linux/nfsd/nfsd.h>
39 #ifdef CONFIG_NFSD_V3
40 #include <linux/nfs3.h>
41 #include <linux/nfsd/xdr3.h>
42 #endif /* CONFIG_NFSD_V3 */
43 #include <linux/nfsd/nfsfh.h>
44 #include <linux/quotaops.h>
45 
46 #include <asm/uaccess.h>
47 
48 #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
49 #define NFSD_PARANOIA
50 
51 
52 /* We must ignore files (but only files) which might have mandatory
53  * locks on them because there is no way to know if the accesser has
54  * the lock.
55  */
56 #define IS_ISMNDLK(i)	(S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
57 
58 /*
59  * This is a cache of readahead params that help us choose the proper
60  * readahead strategy. Initially, we set all readahead parameters to 0
61  * and let the VFS handle things.
62  * If you increase the number of cached files very much, you'll need to
63  * add a hash table here.
64  */
65 struct raparms {
66 	struct raparms		*p_next;
67 	unsigned int		p_count;
68 	ino_t			p_ino;
69 	dev_t			p_dev;
70 	unsigned long		p_reada,
71 				p_ramax,
72 				p_raend,
73 				p_ralen,
74 				p_rawin;
75 };
76 
77 static struct raparms *		raparml;
78 static struct raparms *		raparm_cache;
79 
80 /*
81  * Look up one component of a pathname.
82  * N.B. After this call _both_ fhp and resfh need an fh_put
83  *
84  * If the lookup would cross a mountpoint, and the mounted filesystem
85  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
86  * accepted as it stands and the mounted directory is
87  * returned. Otherwise the covered directory is returned.
88  * NOTE: this mountpoint crossing is not supported properly by all
89  *   clients and is explicitly disallowed for NFSv3
90  *      NeilBrown <neilb@cse.unsw.edu.au>
91  */
92 int
nfsd_lookup(struct svc_rqst * rqstp,struct svc_fh * fhp,const char * name,int len,struct svc_fh * resfh)93 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
94 					int len, struct svc_fh *resfh)
95 {
96 	struct svc_export	*exp;
97 	struct dentry		*dparent;
98 	struct dentry		*dentry;
99 	int			err;
100 
101 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
102 
103 	/* Obtain dentry and export. */
104 	err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
105 	if (err)
106 		goto out;
107 
108 	dparent = fhp->fh_dentry;
109 	exp  = fhp->fh_export;
110 
111 	err = nfserr_acces;
112 
113 	/* Lookup the name, but don't follow links */
114 	if (isdotent(name, len)) {
115 		if (len==1)
116 			dentry = dget(dparent);
117 		else if (dparent != exp->ex_dentry)
118 			dentry = dget(dparent->d_parent);
119 		else if (!EX_NOHIDE(exp))
120 			dentry = dget(dparent); /* .. == . just like at / */
121 		else {
122 			/* checking mountpoint crossing is very different when stepping up */
123 			struct svc_export *exp2 = NULL;
124 			struct dentry *dp;
125 			struct vfsmount *mnt = mntget(exp->ex_mnt);
126 			dentry = dget(dparent);
127 			while(follow_up(&mnt, &dentry))
128 				;
129 			dp = dget(dentry->d_parent);
130 			dput(dentry);
131 			dentry = dp;
132 			for ( ; exp2 == NULL && dp->d_parent != dp;
133 			      dp=dp->d_parent)
134 				exp2 = exp_get(exp->ex_client, dp->d_inode->i_dev, dp->d_inode->i_ino);
135 			if (exp2==NULL) {
136 				dput(dentry);
137 				dentry = dget(dparent);
138 			} else {
139 				exp = exp2;
140 			}
141 			mntput(mnt);
142 		}
143 	} else {
144 		fh_lock(fhp);
145 		dentry = lookup_one_len(name, dparent, len);
146 		err = PTR_ERR(dentry);
147 		if (IS_ERR(dentry))
148 			goto out_nfserr;
149 		/*
150 		 * check if we have crossed a mount point ...
151 		 */
152 		if (d_mountpoint(dentry)) {
153 			struct svc_export *exp2 = NULL;
154 			struct vfsmount *mnt = mntget(exp->ex_mnt);
155 			struct dentry *mounts = dget(dentry);
156 			while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts))
157 				;
158 			exp2 = exp_get(rqstp->rq_client,
159 				       mounts->d_inode->i_dev,
160 				       mounts->d_inode->i_ino);
161 			if (exp2 && EX_NOHIDE(exp2)) {
162 				/* successfully crossed mount point */
163 				exp = exp2;
164 				dput(dentry);
165 				dentry = mounts;
166 			} else
167 				dput(mounts);
168 			mntput(mnt);
169 		}
170 	}
171 
172 	if (dentry->d_inode && dentry->d_inode->i_op &&
173 	    dentry->d_inode->i_op->revalidate &&
174 	    dentry->d_inode->i_op->revalidate(dentry))
175 		err = nfserr_noent;
176 	else
177 		err = fh_compose(resfh, exp, dentry, fhp);
178 	if (!err && !dentry->d_inode)
179 		err = nfserr_noent;
180 out:
181 	return err;
182 
183 out_nfserr:
184 	err = nfserrno(err);
185 	goto out;
186 }
187 
188 /*
189  * Set various file attributes.
190  * N.B. After this call fhp needs an fh_put
191  */
192 int
nfsd_setattr(struct svc_rqst * rqstp,struct svc_fh * fhp,struct iattr * iap,int check_guard,time_t guardtime)193 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
194 	     int check_guard, time_t guardtime)
195 {
196 	struct dentry	*dentry;
197 	struct inode	*inode;
198 	int		accmode = MAY_SATTR;
199 	int		ftype = 0;
200 	int		imode;
201 	int		err;
202 	int		size_change = 0;
203 
204 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
205 		accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
206 	if (iap->ia_valid & ATTR_SIZE)
207 		ftype = S_IFREG;
208 
209 	/* Get inode */
210 	err = fh_verify(rqstp, fhp, ftype, accmode);
211 	if (err || !iap->ia_valid)
212 		goto out;
213 
214 	dentry = fhp->fh_dentry;
215 	inode = dentry->d_inode;
216 
217 	/* NFSv2 does not differentiate between "set-[ac]time-to-now"
218 	 * which only requires access, and "set-[ac]time-to-X" which
219 	 * requires ownership.
220 	 * So if it looks like it might be "set both to the same time which
221 	 * is close to now", and if inode_change_ok fails, then we
222 	 * convert to "set to now" instead of "set to explicit time"
223 	 *
224 	 * We only call inode_change_ok as the last test as technically
225 	 * it is not an interface that we should be using.  It is only
226 	 * valid if the filesystem does not define it's own i_op->setattr.
227 	 */
228 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
229 #define	MAX_TOUCH_TIME_ERROR (30*60)
230 	if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
231 	    && iap->ia_mtime == iap->ia_atime
232 	    ) {
233 	    /* Looks probable.  Now just make sure time is in the right ballpark.
234 	     * Solaris, at least, doesn't seem to care what the time request is.
235 	     * We require it be within 30 minutes of now.
236 	     */
237 	    time_t delta = iap->ia_atime - CURRENT_TIME;
238 	    if (delta<0) delta = -delta;
239 	    if (delta < MAX_TOUCH_TIME_ERROR &&
240 		inode_change_ok(inode, iap) != 0) {
241 		/* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
242 		 * this will cause notify_change to set these times to "now"
243 		 */
244 		iap->ia_valid &= ~BOTH_TIME_SET;
245 	    }
246 	}
247 
248 	/* The size case is special. It changes the file as well as the attributes.  */
249 	if (iap->ia_valid & ATTR_SIZE) {
250 		if (iap->ia_size < inode->i_size) {
251 			err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
252 			if (err)
253 				goto out;
254 		}
255 
256 		/*
257 		 * If we are changing the size of the file, then
258 		 * we need to break all leases.
259 		 */
260 		err = get_lease(inode, FMODE_WRITE);
261 		if (err)
262 			goto out_nfserr;
263 
264 		err = get_write_access(inode);
265 		if (err)
266 			goto out_nfserr;
267 
268 		err = locks_verify_truncate(inode, NULL, iap->ia_size);
269 		if (err) {
270 			put_write_access(inode);
271 			goto out_nfserr;
272 		}
273 		DQUOT_INIT(inode);
274 	}
275 
276 	imode = inode->i_mode;
277 	if (iap->ia_valid & ATTR_MODE) {
278 		iap->ia_mode &= S_IALLUGO;
279 		imode = iap->ia_mode |= (imode & ~S_IALLUGO);
280 	}
281 
282 	/* Revoke setuid/setgid bit on chown/chgrp */
283 	if ((iap->ia_valid & ATTR_UID)
284 	    && (imode & S_ISUID)
285 	    && !S_ISDIR(imode)
286 	    && iap->ia_uid != inode->i_uid) {
287 		iap->ia_valid |= ATTR_MODE;
288 		iap->ia_mode = imode &= ~S_ISUID;
289 	}
290 	if ((iap->ia_valid & ATTR_GID)
291 	    && (imode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)
292 	    && !S_ISDIR(imode)
293 	    && iap->ia_gid != inode->i_gid) {
294 		iap->ia_valid |= ATTR_MODE;
295 		iap->ia_mode = imode &= ~S_ISGID;
296 	}
297 
298 	/* Change the attributes. */
299 
300 
301 	iap->ia_valid |= ATTR_CTIME;
302 
303 	if (iap->ia_valid & ATTR_SIZE) {
304 		down_write(&inode->i_alloc_sem);
305 		fh_lock(fhp);
306 		size_change = 1;
307 	}
308 	err = nfserr_notsync;
309 	if (!check_guard || guardtime == inode->i_ctime) {
310 		err = notify_change(dentry, iap);
311 		err = nfserrno(err);
312 	}
313 	if (size_change) {
314 		fh_unlock(fhp);
315 		up_write(&inode->i_alloc_sem);
316 		put_write_access(inode);
317 	}
318 	if (!err)
319 		if (EX_ISSYNC(fhp->fh_export))
320 			write_inode_now(inode, 1);
321 out:
322 	return err;
323 
324 out_nfserr:
325 	err = nfserrno(err);
326 	goto out;
327 }
328 
329 #ifdef CONFIG_NFSD_V3
330 /*
331  * Check server access rights to a file system object
332  */
333 struct accessmap {
334 	u32		access;
335 	int		how;
336 };
337 static struct accessmap	nfs3_regaccess[] = {
338     {	NFS3_ACCESS_READ,	MAY_READ			},
339     {	NFS3_ACCESS_EXECUTE,	MAY_EXEC			},
340     {	NFS3_ACCESS_MODIFY,	MAY_WRITE|MAY_TRUNC		},
341     {	NFS3_ACCESS_EXTEND,	MAY_WRITE			},
342 
343     {	0,			0				}
344 };
345 
346 static struct accessmap	nfs3_diraccess[] = {
347     {	NFS3_ACCESS_READ,	MAY_READ			},
348     {	NFS3_ACCESS_LOOKUP,	MAY_EXEC			},
349     {	NFS3_ACCESS_MODIFY,	MAY_EXEC|MAY_WRITE|MAY_TRUNC	},
350     {	NFS3_ACCESS_EXTEND,	MAY_EXEC|MAY_WRITE		},
351     {	NFS3_ACCESS_DELETE,	MAY_REMOVE			},
352 
353     {	0,			0				}
354 };
355 
356 static struct accessmap	nfs3_anyaccess[] = {
357 	/* Some clients - Solaris 2.6 at least, make an access call
358 	 * to the server to check for access for things like /dev/null
359 	 * (which really, the server doesn't care about).  So
360 	 * We provide simple access checking for them, looking
361 	 * mainly at mode bits
362 	 */
363     {	NFS3_ACCESS_READ,	MAY_READ			},
364     {	NFS3_ACCESS_EXECUTE,	MAY_EXEC			},
365     {	NFS3_ACCESS_MODIFY,	MAY_WRITE			},
366     {	NFS3_ACCESS_EXTEND,	MAY_WRITE			},
367 
368     {	0,			0				}
369 };
370 
371 int
nfsd_access(struct svc_rqst * rqstp,struct svc_fh * fhp,u32 * access)372 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access)
373 {
374 	struct accessmap	*map;
375 	struct svc_export	*export;
376 	struct dentry		*dentry;
377 	u32			query, result = 0;
378 	unsigned int		error;
379 
380 	error = fh_verify(rqstp, fhp, 0, MAY_NOP);
381 	if (error)
382 		goto out;
383 
384 	export = fhp->fh_export;
385 	dentry = fhp->fh_dentry;
386 
387 	if (S_ISREG(dentry->d_inode->i_mode))
388 		map = nfs3_regaccess;
389 	else if (S_ISDIR(dentry->d_inode->i_mode))
390 		map = nfs3_diraccess;
391 	else
392 		map = nfs3_anyaccess;
393 
394 
395 	query = *access;
396 	for  (; map->access; map++) {
397 		if (map->access & query) {
398 			unsigned int err2;
399 			err2 = nfsd_permission(export, dentry, map->how);
400 			switch (err2) {
401 			case nfs_ok:
402 				result |= map->access;
403 				break;
404 
405 			/* the following error codes just mean the access was not allowed,
406 			 * rather than an error occurred */
407 			case nfserr_rofs:
408 			case nfserr_acces:
409 			case nfserr_perm:
410 				/* simply don't "or" in the access bit. */
411 				break;
412 			default:
413 				error = err2;
414 				goto out;
415 			}
416 		}
417 	}
418 	*access = result;
419 
420  out:
421 	return error;
422 }
423 #endif /* CONFIG_NFSD_V3 */
424 
425 
426 
427 /*
428  * Open an existing file or directory.
429  * The access argument indicates the type of open (read/write/lock)
430  * N.B. After this call fhp needs an fh_put
431  */
432 int
nfsd_open(struct svc_rqst * rqstp,struct svc_fh * fhp,int type,int access,struct file * filp)433 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
434 			int access, struct file *filp)
435 {
436 	struct dentry	*dentry;
437 	struct inode	*inode;
438 	int		err;
439 
440 	/* If we get here, then the client has already done an "open", and (hopefully)
441 	 * checked permission - so allow OWNER_OVERRIDE in case a chmod has now revoked
442 	 * permission */
443 	err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
444 	if (err)
445 		goto out;
446 
447 	dentry = fhp->fh_dentry;
448 	inode = dentry->d_inode;
449 
450 	/* Disallow access to files with the append-only bit set or
451 	 * with mandatory locking enabled
452 	 */
453 	err = nfserr_perm;
454 	if (IS_APPEND(inode) || IS_ISMNDLK(inode))
455 		goto out;
456 	if (!inode->i_fop)
457 		goto out;
458 
459 	/*
460 	 * Check to see if there are any leases on this file.
461 	 * This may block while leases are broken.
462 	 */
463 	err = get_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
464 	if (err)
465 		goto out_nfserr;
466 
467 	if ((access & MAY_WRITE) && (err = get_write_access(inode)) != 0)
468 		goto out_nfserr;
469 
470 	memset(filp, 0, sizeof(*filp));
471 	filp->f_op    = fops_get(inode->i_fop);
472 	atomic_set(&filp->f_count, 1);
473 	filp->f_dentry = dentry;
474 	filp->f_vfsmnt = fhp->fh_export->ex_mnt;
475 	filp->f_maxcount = INT_MAX;
476 
477 	if (access & MAY_WRITE) {
478 		filp->f_flags = O_WRONLY|O_LARGEFILE;
479 		filp->f_mode  = FMODE_WRITE;
480 		DQUOT_INIT(inode);
481 	} else {
482 		filp->f_flags = O_RDONLY|O_LARGEFILE;
483 		filp->f_mode  = FMODE_READ;
484 	}
485 
486 	err = 0;
487 	if (filp->f_op && filp->f_op->open) {
488 		err = filp->f_op->open(inode, filp);
489 		if (err) {
490 			fops_put(filp->f_op);
491 			if (access & MAY_WRITE)
492 				put_write_access(inode);
493 
494 			/* I nearly added put_filp() call here, but this filp
495 			 * is really on callers stack frame. -DaveM
496 			 */
497 			atomic_dec(&filp->f_count);
498 		}
499 	}
500 out_nfserr:
501 	if (err)
502 		err = nfserrno(err);
503 out:
504 	return err;
505 }
506 
507 /*
508  * Close a file.
509  */
510 void
nfsd_close(struct file * filp)511 nfsd_close(struct file *filp)
512 {
513 	struct dentry	*dentry = filp->f_dentry;
514 	struct inode	*inode = dentry->d_inode;
515 
516 	if (filp->f_op && filp->f_op->release)
517 		filp->f_op->release(inode, filp);
518 	fops_put(filp->f_op);
519 	if (filp->f_mode & FMODE_WRITE)
520 		put_write_access(inode);
521 }
522 
523 /*
524  * Sync a file
525  * As this calls fsync (not fdatasync) there is no need for a write_inode
526  * after it.
527  */
nfsd_dosync(struct file * filp,struct dentry * dp,struct file_operations * fop)528 inline void nfsd_dosync(struct file *filp, struct dentry *dp,
529 			struct file_operations *fop)
530 {
531 	struct inode *inode = dp->d_inode;
532 	int (*fsync) (struct file *, struct dentry *, int);
533 
534 	filemap_fdatasync(inode->i_mapping);
535 	if (fop && (fsync = fop->fsync))
536 		fsync(filp, dp, 0);
537 	filemap_fdatawait(inode->i_mapping);
538 }
539 
540 
541 void
nfsd_sync(struct file * filp)542 nfsd_sync(struct file *filp)
543 {
544 	struct inode *inode = filp->f_dentry->d_inode;
545 	dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
546 	down(&inode->i_sem);
547 	nfsd_dosync(filp, filp->f_dentry, filp->f_op);
548 	up(&inode->i_sem);
549 }
550 
551 void
nfsd_sync_dir(struct dentry * dp)552 nfsd_sync_dir(struct dentry *dp)
553 {
554 	nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
555 }
556 
557 /*
558  * Obtain the readahead parameters for the file
559  * specified by (dev, ino).
560  */
561 static inline struct raparms *
nfsd_get_raparms(dev_t dev,ino_t ino)562 nfsd_get_raparms(dev_t dev, ino_t ino)
563 {
564 	struct raparms	*ra, **rap, **frap = NULL;
565 	int depth = 0;
566 
567 	for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
568 		if (ra->p_ino == ino && ra->p_dev == dev)
569 			goto found;
570 		depth++;
571 		if (ra->p_count == 0)
572 			frap = rap;
573 	}
574 	depth = nfsdstats.ra_size*11/10;
575 	if (!frap)
576 		return NULL;
577 	rap = frap;
578 	ra = *frap;
579 	ra->p_dev = dev;
580 	ra->p_ino = ino;
581 	ra->p_reada = 0;
582 	ra->p_ramax = 0;
583 	ra->p_raend = 0;
584 	ra->p_ralen = 0;
585 	ra->p_rawin = 0;
586 found:
587 	if (rap != &raparm_cache) {
588 		*rap = ra->p_next;
589 		ra->p_next   = raparm_cache;
590 		raparm_cache = ra;
591 	}
592 	ra->p_count++;
593 	nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
594 	return ra;
595 }
596 
597 /* copied from fs/read_write.c */
llseek(struct file * file,loff_t offset,int origin)598 static inline loff_t llseek(struct file *file, loff_t offset, int origin)
599 {
600 	loff_t (*fn)(struct file *, loff_t, int);
601 	loff_t retval;
602 
603 	fn = default_llseek;
604 	if (file->f_op && file->f_op->llseek)
605 		fn = file->f_op->llseek;
606 	lock_kernel();
607 	retval = fn(file, offset, origin);
608 	unlock_kernel();
609 	return retval;
610 }
611 
612 
613 /*
614  * Read data from a file. count must contain the requested read count
615  * on entry. On return, *count contains the number of bytes actually read.
616  * N.B. After this call fhp needs an fh_put
617  */
618 int
nfsd_read(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t offset,char * buf,unsigned long * count)619 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
620           char *buf, unsigned long *count)
621 {
622 	struct raparms	*ra;
623 	mm_segment_t	oldfs;
624 	int		err;
625 	struct file	file;
626 
627 	err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
628 	if (err)
629 		goto out;
630 	err = nfserr_perm;
631 	if (!file.f_op->read)
632 		goto out_close;
633 #ifdef MSNFS
634 	if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
635 		(!lock_may_read(file.f_dentry->d_inode, offset, *count)))
636 		goto out_close;
637 #endif
638 
639 	/* Get readahead parameters */
640 	ra = nfsd_get_raparms(fhp->fh_export->ex_dev, fhp->fh_dentry->d_inode->i_ino);
641 	if (ra) {
642 		file.f_reada = ra->p_reada;
643 		file.f_ramax = ra->p_ramax;
644 		file.f_raend = ra->p_raend;
645 		file.f_ralen = ra->p_ralen;
646 		file.f_rawin = ra->p_rawin;
647 	}
648 	llseek(&file, offset, 0);
649 
650 	oldfs = get_fs(); set_fs(KERNEL_DS);
651 	err = file.f_op->read(&file, buf, *count, &file.f_pos);
652 	set_fs(oldfs);
653 
654 	/* Write back readahead params */
655 	if (ra != NULL) {
656 		dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
657 			file.f_reada, file.f_ramax, file.f_raend,
658 			file.f_ralen, file.f_rawin);
659 		ra->p_reada = file.f_reada;
660 		ra->p_ramax = file.f_ramax;
661 		ra->p_raend = file.f_raend;
662 		ra->p_ralen = file.f_ralen;
663 		ra->p_rawin = file.f_rawin;
664 		ra->p_count -= 1;
665 	}
666 
667 	if (err >= 0) {
668 		nfsdstats.io_read += err;
669 		*count = err;
670 		err = 0;
671 	} else
672 		err = nfserrno(err);
673 out_close:
674 	nfsd_close(&file);
675 out:
676 	return err;
677 }
678 
679 /*
680  * Write data to a file.
681  * The stable flag requests synchronous writes.
682  * N.B. After this call fhp needs an fh_put
683  */
684 int
nfsd_write(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t offset,char * buf,unsigned long cnt,int * stablep)685 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
686 				char *buf, unsigned long cnt, int *stablep)
687 {
688 	struct svc_export	*exp;
689 	struct file		file;
690 	struct dentry		*dentry;
691 	struct inode		*inode;
692 	mm_segment_t		oldfs;
693 	int			err = 0;
694 	int			stable = *stablep;
695 
696 	err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
697 	if (err)
698 		goto out;
699 	if (!cnt)
700 		goto out_close;
701 	err = nfserr_perm;
702 	if (!file.f_op->write)
703 		goto out_close;
704 #ifdef MSNFS
705 	if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
706 		(!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
707 		goto out_close;
708 #endif
709 
710 	dentry = file.f_dentry;
711 	inode = dentry->d_inode;
712 	exp   = fhp->fh_export;
713 
714 	/*
715 	 * Request sync writes if
716 	 *  -	the sync export option has been set, or
717 	 *  -	the client requested O_SYNC behavior (NFSv3 feature).
718 	 *  -   The file system doesn't support fsync().
719 	 * When gathered writes have been configured for this volume,
720 	 * flushing the data to disk is handled separately below.
721 	 */
722 
723 	if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
724 	       stable = 2;
725 	       *stablep = 2; /* FILE_SYNC */
726 	}
727 
728 	if (!EX_ISSYNC(exp))
729 		stable = 0;
730 	if (stable && !EX_WGATHER(exp))
731 		file.f_flags |= O_SYNC;
732 
733 
734 	llseek(&file, offset, 0);
735 
736 	/* Write the data. */
737 	oldfs = get_fs(); set_fs(KERNEL_DS);
738 	err = file.f_op->write(&file, buf, cnt, &file.f_pos);
739 	if (err >= 0)
740 		nfsdstats.io_write += cnt;
741 	set_fs(oldfs);
742 
743 	/* clear setuid/setgid flag after write */
744 	if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
745 		struct iattr	ia;
746 
747 		ia.ia_valid = ATTR_MODE;
748 		ia.ia_mode  = inode->i_mode & ~(S_ISUID | S_ISGID);
749 		notify_change(dentry, &ia);
750 	}
751 
752 	if (err >= 0 && stable) {
753 		static unsigned long	last_ino;
754 		static kdev_t		last_dev = NODEV;
755 
756 		/*
757 		 * Gathered writes: If another process is currently
758 		 * writing to the file, there's a high chance
759 		 * this is another nfsd (triggered by a bulk write
760 		 * from a client's biod). Rather than syncing the
761 		 * file with each write request, we sleep for 10 msec.
762 		 *
763 		 * I don't know if this roughly approximates
764 		 * C. Juszak's idea of gathered writes, but it's a
765 		 * nice and simple solution (IMHO), and it seems to
766 		 * work:-)
767 		 */
768 		if (EX_WGATHER(exp)) {
769 			if (atomic_read(&inode->i_writecount) > 1
770 			    || (last_ino == inode->i_ino && last_dev == inode->i_dev)) {
771 				dprintk("nfsd: write defer %d\n", current->pid);
772 				set_current_state(TASK_UNINTERRUPTIBLE);
773 				schedule_timeout((HZ+99)/100);
774 				current->state = TASK_RUNNING;
775 				dprintk("nfsd: write resume %d\n", current->pid);
776 			}
777 
778 			if (inode->i_state & I_DIRTY) {
779 				dprintk("nfsd: write sync %d\n", current->pid);
780 				nfsd_sync(&file);
781 			}
782 #if 0
783 			wake_up(&inode->i_wait);
784 #endif
785 		}
786 		last_ino = inode->i_ino;
787 		last_dev = inode->i_dev;
788 	}
789 
790 	dprintk("nfsd: write complete err=%d\n", err);
791 	if (err >= 0)
792 		err = 0;
793 	else
794 		err = nfserrno(err);
795 out_close:
796 	nfsd_close(&file);
797 out:
798 	return err;
799 }
800 
801 
802 #ifdef CONFIG_NFSD_V3
803 /*
804  * Commit all pending writes to stable storage.
805  * Strictly speaking, we could sync just the indicated file region here,
806  * but there's currently no way we can ask the VFS to do so.
807  *
808  * Unfortunately we cannot lock the file to make sure we return full WCC
809  * data to the client, as locking happens lower down in the filesystem.
810  */
811 int
nfsd_commit(struct svc_rqst * rqstp,struct svc_fh * fhp,off_t offset,unsigned long count)812 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
813                off_t offset, unsigned long count)
814 {
815 	struct file	file;
816 	int		err;
817 
818 	if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
819 		return err;
820 	if (EX_ISSYNC(fhp->fh_export)) {
821 		if (file.f_op && file.f_op->fsync) {
822 			nfsd_sync(&file);
823 		} else {
824 			err = nfserr_notsupp;
825 		}
826 	}
827 
828 	nfsd_close(&file);
829 	return err;
830 }
831 #endif /* CONFIG_NFSD_V3 */
832 
833 /*
834  * Create a file (regular, directory, device, fifo); UNIX sockets
835  * not yet implemented.
836  * If the response fh has been verified, the parent directory should
837  * already be locked. Note that the parent directory is left locked.
838  *
839  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
840  */
841 int
nfsd_create(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,struct iattr * iap,int type,dev_t rdev,struct svc_fh * resfhp)842 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
843 		char *fname, int flen, struct iattr *iap,
844 		int type, dev_t rdev, struct svc_fh *resfhp)
845 {
846 	struct dentry	*dentry, *dchild;
847 	struct inode	*dirp;
848 	int		err;
849 
850 	err = nfserr_perm;
851 	if (!flen)
852 		goto out;
853 	err = nfserr_exist;
854 	if (isdotent(fname, flen))
855 		goto out;
856 
857 	err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
858 	if (err)
859 		goto out;
860 
861 	dentry = fhp->fh_dentry;
862 	dirp = dentry->d_inode;
863 
864 	err = nfserr_notdir;
865 	if(!dirp->i_op || !dirp->i_op->lookup)
866 		goto out;
867 	/*
868 	 * Check whether the response file handle has been verified yet.
869 	 * If it has, the parent directory should already be locked.
870 	 */
871 	if (!resfhp->fh_dentry) {
872 		/* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
873 		fh_lock(fhp);
874 		dchild = lookup_one_len(fname, dentry, flen);
875 		err = PTR_ERR(dchild);
876 		if (IS_ERR(dchild))
877 			goto out_nfserr;
878 		err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
879 		if (err)
880 			goto out;
881 	} else {
882 		/* called from nfsd_proc_create */
883 		dchild = resfhp->fh_dentry;
884 		if (!fhp->fh_locked) {
885 			/* not actually possible */
886 			printk(KERN_ERR
887 				"nfsd_create: parent %s/%s not locked!\n",
888 				dentry->d_parent->d_name.name,
889 				dentry->d_name.name);
890 			err = -EIO;
891 			goto out;
892 		}
893 	}
894 	/*
895 	 * Make sure the child dentry is still negative ...
896 	 */
897 	err = nfserr_exist;
898 	if (dchild->d_inode) {
899 		dprintk("nfsd_create: dentry %s/%s not negative!\n",
900 			dentry->d_name.name, dchild->d_name.name);
901 		goto out;
902 	}
903 
904 	if (!(iap->ia_valid & ATTR_MODE))
905 		iap->ia_mode = 0;
906 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
907 
908 	/*
909 	 * Get the dir op function pointer.
910 	 */
911 	err = nfserr_perm;
912 	switch (type) {
913 	case S_IFREG:
914 		err = vfs_create(dirp, dchild, iap->ia_mode);
915 		break;
916 	case S_IFDIR:
917 		err = vfs_mkdir(dirp, dchild, iap->ia_mode);
918 		break;
919 	case S_IFCHR:
920 	case S_IFBLK:
921 	case S_IFIFO:
922 	case S_IFSOCK:
923 		err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
924 		break;
925 	default:
926 	        printk("nfsd: bad file type %o in nfsd_create\n", type);
927 		err = -EINVAL;
928 	}
929 	if (err < 0)
930 		goto out_nfserr;
931 
932 	if (EX_ISSYNC(fhp->fh_export)) {
933 		nfsd_sync_dir(dentry);
934 		write_inode_now(dchild->d_inode, 1);
935 	}
936 
937 
938 	/* Set file attributes. Mode has already been set and
939 	 * setting uid/gid works only for root. Irix appears to
940 	 * send along the gid when it tries to implement setgid
941 	 * directories via NFS.
942 	 */
943 	err = 0;
944 	if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
945 		err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
946 	/*
947 	 * Update the file handle to get the new inode info.
948 	 */
949 	if (!err)
950 		err = fh_update(resfhp);
951 out:
952 	return err;
953 
954 out_nfserr:
955 	err = nfserrno(err);
956 	goto out;
957 }
958 
959 #ifdef CONFIG_NFSD_V3
960 /*
961  * NFSv3 version of nfsd_create
962  */
963 int
nfsd_create_v3(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,struct iattr * iap,struct svc_fh * resfhp,int createmode,u32 * verifier)964 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
965 		char *fname, int flen, struct iattr *iap,
966 		struct svc_fh *resfhp, int createmode, u32 *verifier)
967 {
968 	struct dentry	*dentry, *dchild;
969 	struct inode	*dirp;
970 	int		err;
971 	__u32		v_mtime=0, v_atime=0;
972 	int		v_mode=0;
973 
974 	err = nfserr_perm;
975 	if (!flen)
976 		goto out;
977 	err = nfserr_exist;
978 	if (isdotent(fname, flen))
979 		goto out;
980 	if (!(iap->ia_valid & ATTR_MODE))
981 		iap->ia_mode = 0;
982 	err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
983 	if (err)
984 		goto out;
985 
986 	dentry = fhp->fh_dentry;
987 	dirp = dentry->d_inode;
988 
989 	/* Get all the sanity checks out of the way before
990 	 * we lock the parent. */
991 	err = nfserr_notdir;
992 	if(!dirp->i_op || !dirp->i_op->lookup)
993 		goto out;
994 	fh_lock(fhp);
995 
996 	/*
997 	 * Compose the response file handle.
998 	 */
999 	dchild = lookup_one_len(fname, dentry, flen);
1000 	err = PTR_ERR(dchild);
1001 	if (IS_ERR(dchild))
1002 		goto out_nfserr;
1003 
1004 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1005 	if (err)
1006 		goto out;
1007 
1008 	if (createmode == NFS3_CREATE_EXCLUSIVE) {
1009 		/* while the verifier would fit in mtime+atime,
1010 		 * solaris7 gets confused (bugid 4218508) if these have
1011 		 * the high bit set, so we use the mode as well
1012 		 */
1013 		v_mtime = verifier[0]&0x7fffffff;
1014 		v_atime = verifier[1]&0x7fffffff;
1015 		v_mode  = S_IFREG
1016 			| ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1017 			| ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1018 			;
1019 	}
1020 
1021 	if (dchild->d_inode) {
1022 		err = 0;
1023 
1024 		switch (createmode) {
1025 		case NFS3_CREATE_UNCHECKED:
1026 			if (! S_ISREG(dchild->d_inode->i_mode))
1027 				err = nfserr_exist;
1028 			else {
1029 				iap->ia_valid &= ATTR_SIZE;
1030 				goto set_attr;
1031 			}
1032 			break;
1033 		case NFS3_CREATE_EXCLUSIVE:
1034 			if (   dchild->d_inode->i_mtime == v_mtime
1035 			    && dchild->d_inode->i_atime == v_atime
1036 			    && dchild->d_inode->i_mode  == v_mode
1037 			    && dchild->d_inode->i_size  == 0 )
1038 				break;
1039 			 /* fallthru */
1040 		case NFS3_CREATE_GUARDED:
1041 			err = nfserr_exist;
1042 		}
1043 		goto out;
1044 	}
1045 
1046 	err = vfs_create(dirp, dchild, iap->ia_mode);
1047 	if (err < 0)
1048 		goto out_nfserr;
1049 
1050 	if (EX_ISSYNC(fhp->fh_export)) {
1051 		nfsd_sync_dir(dentry);
1052 		/* setattr will sync the child (or not) */
1053 	}
1054 
1055 	/*
1056 	 * Update the filehandle to get the new inode info.
1057 	 */
1058 	err = fh_update(resfhp);
1059 	if (err)
1060 		goto out;
1061 
1062 	if (createmode == NFS3_CREATE_EXCLUSIVE) {
1063 		/* Cram the verifier into atime/mtime/mode */
1064 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1065 			| ATTR_MTIME_SET|ATTR_ATIME_SET
1066 			| ATTR_MODE;
1067 		iap->ia_mtime = v_mtime;
1068 		iap->ia_atime = v_atime;
1069 		iap->ia_mode  = v_mode;
1070 	}
1071 
1072 	/* Set file attributes.
1073 	 * Mode has already been set but we might need to reset it
1074 	 * for CREATE_EXCLUSIVE
1075 	 * Irix appears to send along the gid when it tries to
1076 	 * implement setgid directories via NFS. Clear out all that cruft.
1077 	 */
1078  set_attr:
1079 	if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1080  		err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1081 
1082  out:
1083 	fh_unlock(fhp);
1084  	return err;
1085 
1086  out_nfserr:
1087 	err = nfserrno(err);
1088 	goto out;
1089 }
1090 #endif /* CONFIG_NFSD_V3 */
1091 
1092 /*
1093  * Read a symlink. On entry, *lenp must contain the maximum path length that
1094  * fits into the buffer. On return, it contains the true length.
1095  * N.B. After this call fhp needs an fh_put
1096  */
1097 int
nfsd_readlink(struct svc_rqst * rqstp,struct svc_fh * fhp,char * buf,int * lenp)1098 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1099 {
1100 	struct dentry	*dentry;
1101 	struct inode	*inode;
1102 	mm_segment_t	oldfs;
1103 	int		err;
1104 
1105 	err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1106 	if (err)
1107 		goto out;
1108 
1109 	dentry = fhp->fh_dentry;
1110 	inode = dentry->d_inode;
1111 
1112 	err = nfserr_inval;
1113 	if (!inode->i_op || !inode->i_op->readlink)
1114 		goto out;
1115 
1116 	UPDATE_ATIME(inode);
1117 	/* N.B. Why does this call need a get_fs()??
1118 	 * Remove the set_fs and watch the fireworks:-) --okir
1119 	 */
1120 
1121 	oldfs = get_fs(); set_fs(KERNEL_DS);
1122 	err = inode->i_op->readlink(dentry, buf, *lenp);
1123 	set_fs(oldfs);
1124 
1125 	if (err < 0)
1126 		goto out_nfserr;
1127 	*lenp = err;
1128 	err = 0;
1129 out:
1130 	return err;
1131 
1132 out_nfserr:
1133 	err = nfserrno(err);
1134 	goto out;
1135 }
1136 
1137 /*
1138  * Create a symlink and look up its inode
1139  * N.B. After this call _both_ fhp and resfhp need an fh_put
1140  */
1141 int
nfsd_symlink(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,char * path,int plen,struct svc_fh * resfhp,struct iattr * iap)1142 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1143 				char *fname, int flen,
1144 				char *path,  int plen,
1145 				struct svc_fh *resfhp,
1146 				struct iattr *iap)
1147 {
1148 	struct dentry	*dentry, *dnew;
1149 	int		err, cerr;
1150 
1151 	err = nfserr_noent;
1152 	if (!flen || !plen)
1153 		goto out;
1154 	err = nfserr_exist;
1155 	if (isdotent(fname, flen))
1156 		goto out;
1157 
1158 	err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1159 	if (err)
1160 		goto out;
1161 	fh_lock(fhp);
1162 	dentry = fhp->fh_dentry;
1163 	dnew = lookup_one_len(fname, dentry, flen);
1164 	err = PTR_ERR(dnew);
1165 	if (IS_ERR(dnew))
1166 		goto out_nfserr;
1167 
1168 	err = vfs_symlink(dentry->d_inode, dnew, path);
1169 	if (!err) {
1170 		if (EX_ISSYNC(fhp->fh_export))
1171 			nfsd_sync_dir(dentry);
1172 		if (iap) {
1173 			iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1174 			if (iap->ia_valid) {
1175 				iap->ia_valid |= ATTR_CTIME;
1176 				iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1177 					| S_IFLNK;
1178 				err = notify_change(dnew, iap);
1179 				if (err)
1180 					err = nfserrno(err);
1181 				else if (EX_ISSYNC(fhp->fh_export))
1182 					write_inode_now(dentry->d_inode, 1);
1183 		       }
1184 		}
1185 	} else
1186 		err = nfserrno(err);
1187 	fh_unlock(fhp);
1188 
1189 	/* Compose the fh so the dentry will be freed ... */
1190 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1191 	if (err==0) err = cerr;
1192 out:
1193 	return err;
1194 
1195 out_nfserr:
1196 	err = nfserrno(err);
1197 	goto out;
1198 }
1199 
1200 /*
1201  * Create a hardlink
1202  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1203  */
1204 int
nfsd_link(struct svc_rqst * rqstp,struct svc_fh * ffhp,char * name,int len,struct svc_fh * tfhp)1205 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1206 				char *name, int len, struct svc_fh *tfhp)
1207 {
1208 	struct dentry	*ddir, *dnew, *dold;
1209 	struct inode	*dirp, *dest;
1210 	int		err;
1211 
1212 	err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1213 	if (err)
1214 		goto out;
1215 	err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1216 	if (err)
1217 		goto out;
1218 
1219 	err = nfserr_perm;
1220 	if (!len)
1221 		goto out;
1222 	err = nfserr_exist;
1223 	if (isdotent(name, len))
1224 		goto out;
1225 
1226 	fh_lock(ffhp);
1227 	ddir = ffhp->fh_dentry;
1228 	dirp = ddir->d_inode;
1229 
1230 	dnew = lookup_one_len(name, ddir, len);
1231 	err = PTR_ERR(dnew);
1232 	if (IS_ERR(dnew))
1233 		goto out_nfserr;
1234 
1235 	dold = tfhp->fh_dentry;
1236 	dest = dold->d_inode;
1237 
1238 	err = vfs_link(dold, dirp, dnew);
1239 	if (!err) {
1240 		if (EX_ISSYNC(ffhp->fh_export)) {
1241 			nfsd_sync_dir(ddir);
1242 			write_inode_now(dest, 1);
1243 		}
1244 	} else {
1245 		if (err == -EXDEV && rqstp->rq_vers == 2)
1246 			err = nfserr_acces;
1247 		else
1248 			err = nfserrno(err);
1249 	}
1250 
1251 	fh_unlock(ffhp);
1252 	dput(dnew);
1253 out:
1254 	return err;
1255 
1256 out_nfserr:
1257 	err = nfserrno(err);
1258 	goto out;
1259 }
1260 
1261 /*
1262  * Rename a file
1263  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1264  */
1265 int
nfsd_rename(struct svc_rqst * rqstp,struct svc_fh * ffhp,char * fname,int flen,struct svc_fh * tfhp,char * tname,int tlen)1266 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1267 			    struct svc_fh *tfhp, char *tname, int tlen)
1268 {
1269 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry;
1270 	struct inode	*fdir, *tdir;
1271 	int		err;
1272 
1273 	err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1274 	if (err)
1275 		goto out;
1276 	err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1277 	if (err)
1278 		goto out;
1279 
1280 	fdentry = ffhp->fh_dentry;
1281 	fdir = fdentry->d_inode;
1282 
1283 	tdentry = tfhp->fh_dentry;
1284 	tdir = tdentry->d_inode;
1285 
1286 	err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1287 	if (fdir->i_dev != tdir->i_dev)
1288 		goto out;
1289 
1290 	err = nfserr_perm;
1291 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1292 		goto out;
1293 
1294 	/* cannot use fh_lock as we need deadlock protective ordering
1295 	 * so do it by hand */
1296 	double_down(&tdir->i_sem, &fdir->i_sem);
1297 	ffhp->fh_locked = tfhp->fh_locked = 1;
1298 	fill_pre_wcc(ffhp);
1299 	fill_pre_wcc(tfhp);
1300 
1301 	odentry = lookup_one_len(fname, fdentry, flen);
1302 	err = PTR_ERR(odentry);
1303 	if (IS_ERR(odentry))
1304 		goto out_nfserr;
1305 
1306 	err = -ENOENT;
1307 	if (!odentry->d_inode)
1308 		goto out_dput_old;
1309 
1310 	ndentry = lookup_one_len(tname, tdentry, tlen);
1311 	err = PTR_ERR(ndentry);
1312 	if (IS_ERR(ndentry))
1313 		goto out_dput_old;
1314 
1315 
1316 #ifdef MSNFS
1317 	if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1318 		((atomic_read(&odentry->d_count) > 1)
1319 		 || (atomic_read(&ndentry->d_count) > 1))) {
1320 			err = nfserr_perm;
1321 	} else
1322 #endif
1323 	err = vfs_rename(fdir, odentry, tdir, ndentry);
1324 	if (!err && EX_ISSYNC(tfhp->fh_export)) {
1325 		nfsd_sync_dir(tdentry);
1326 		nfsd_sync_dir(fdentry);
1327 	}
1328 	dput(ndentry);
1329 
1330  out_dput_old:
1331 	dput(odentry);
1332  out_nfserr:
1333 	if (err)
1334 		err = nfserrno(err);
1335 
1336 	/* we cannot reply on fh_unlock on the two filehandles,
1337 	 * as that would do the wrong thing if the two directories
1338 	 * were the same, so again we do it by hand
1339 	 */
1340 	fill_post_wcc(ffhp);
1341 	fill_post_wcc(tfhp);
1342 	double_up(&tdir->i_sem, &fdir->i_sem);
1343 	ffhp->fh_locked = tfhp->fh_locked = 0;
1344 
1345 out:
1346 	return err;
1347 }
1348 
1349 /*
1350  * Unlink a file or directory
1351  * N.B. After this call fhp needs an fh_put
1352  */
1353 int
nfsd_unlink(struct svc_rqst * rqstp,struct svc_fh * fhp,int type,char * fname,int flen)1354 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1355 				char *fname, int flen)
1356 {
1357 	struct dentry	*dentry, *rdentry;
1358 	struct inode	*dirp;
1359 	int		err;
1360 
1361 	err = nfserr_acces;
1362 	if (!flen || isdotent(fname, flen))
1363 		goto out;
1364 	err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1365 	if (err)
1366 		goto out;
1367 
1368 	fh_lock(fhp);
1369 	dentry = fhp->fh_dentry;
1370 	dirp = dentry->d_inode;
1371 
1372 	rdentry = lookup_one_len(fname, dentry, flen);
1373 	err = PTR_ERR(rdentry);
1374 	if (IS_ERR(rdentry))
1375 		goto out_nfserr;
1376 
1377 	if (!rdentry->d_inode) {
1378 		dput(rdentry);
1379 		err = nfserr_noent;
1380 		goto out;
1381 	}
1382 
1383 	if (type != S_IFDIR) { /* It's UNLINK */
1384 #ifdef MSNFS
1385 		if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1386 			(atomic_read(&rdentry->d_count) > 1)) {
1387 			err = nfserr_perm;
1388 		} else
1389 #endif
1390 		err = vfs_unlink(dirp, rdentry);
1391 	} else { /* It's RMDIR */
1392 		err = vfs_rmdir(dirp, rdentry);
1393 	}
1394 
1395 	dput(rdentry);
1396 
1397 	if (err)
1398 		goto out_nfserr;
1399 	if (EX_ISSYNC(fhp->fh_export))
1400 		nfsd_sync_dir(dentry);
1401 
1402 out:
1403 	return err;
1404 
1405 out_nfserr:
1406 	err = nfserrno(err);
1407 	goto out;
1408 }
1409 
1410 /*
1411  * Read entries from a directory.
1412  * The verifier is an NFSv3 thing we ignore for now.
1413  */
1414 int
nfsd_readdir(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t offset,encode_dent_fn func,u32 * buffer,int * countp,u32 * verf)1415 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1416              encode_dent_fn func, u32 *buffer, int *countp, u32 *verf)
1417 {
1418 	u32		*p;
1419 	int		oldlen, eof, err;
1420 	struct file	file;
1421 	struct readdir_cd cd;
1422 
1423 	err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1424 	if (err)
1425 		goto out;
1426 
1427 	offset = llseek(&file, offset, 0);
1428 	if (offset < 0) {
1429 		err = nfserrno((int)offset);
1430 		goto out_close;
1431 	}
1432 
1433 	/* Set up the readdir context */
1434 	memset(&cd, 0, sizeof(cd));
1435 	cd.rqstp  = rqstp;
1436 	cd.buffer = buffer;
1437 	cd.buflen = *countp; /* count of words */
1438 	cd.dirfh  = fhp;
1439 
1440 	/*
1441 	 * Read the directory entries. This silly loop is necessary because
1442 	 * readdir() is not guaranteed to fill up the entire buffer, but
1443 	 * may choose to do less.
1444 	 */
1445 
1446 	do {
1447 		oldlen = cd.buflen;
1448 
1449 		err = vfs_readdir(&file, (filldir_t) func, &cd);
1450 
1451 		if (err < 0)
1452 			goto out_nfserr;
1453 
1454 	} while (oldlen != cd.buflen && !cd.eob);
1455 
1456 	/* If we didn't fill the buffer completely, we're at EOF */
1457 	eof = !cd.eob;
1458 
1459 
1460 	offset = llseek(&file, 0LL, 1);
1461 	if (cd.offset) {
1462 		if (rqstp->rq_vers == 3)
1463 			(void)xdr_encode_hyper(cd.offset, offset);
1464 		else
1465 			*cd.offset = htonl(offset);
1466 	}
1467 
1468 	p = cd.buffer;
1469 	*p++ = 0;			/* no more entries */
1470 	*p++ = htonl(eof);		/* end of directory */
1471 	*countp = (caddr_t) p - (caddr_t) buffer;
1472 
1473 	dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1474 				*countp, eof,
1475 				cd.offset? ntohl(*cd.offset) : -1);
1476 	err = 0;
1477 out_close:
1478 	nfsd_close(&file);
1479 out:
1480 	return err;
1481 
1482 out_nfserr:
1483 	err = nfserrno(err);
1484 	goto out_close;
1485 }
1486 
1487 /*
1488  * Get file system stats
1489  * N.B. After this call fhp needs an fh_put
1490  */
1491 int
nfsd_statfs(struct svc_rqst * rqstp,struct svc_fh * fhp,struct statfs * stat)1492 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct statfs *stat)
1493 {
1494 	int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1495 	if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1496 		err = nfserr_io;
1497 	return err;
1498 }
1499 
1500 /*
1501  * Check for a user's access permissions to this inode.
1502  */
1503 int
nfsd_permission(struct svc_export * exp,struct dentry * dentry,int acc)1504 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1505 {
1506 	struct inode	*inode = dentry->d_inode;
1507 	int		err;
1508 
1509 	if (acc == MAY_NOP)
1510 		return 0;
1511 #if 0
1512 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1513 		acc,
1514 		(acc & MAY_READ)?	" read"  : "",
1515 		(acc & MAY_WRITE)?	" write" : "",
1516 		(acc & MAY_EXEC)?	" exec"  : "",
1517 		(acc & MAY_SATTR)?	" sattr" : "",
1518 		(acc & MAY_TRUNC)?	" trunc" : "",
1519 		(acc & MAY_LOCK)?	" lock"  : "",
1520 		(acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1521 		inode->i_mode,
1522 		IS_IMMUTABLE(inode)?	" immut" : "",
1523 		IS_APPEND(inode)?	" append" : "",
1524 		IS_RDONLY(inode)?	" ro" : "");
1525 	dprintk("      owner %d/%d user %d/%d\n",
1526 		inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1527 #endif
1528 
1529 	/* The following code is here to make IRIX happy, which
1530 	 * does a permission check every time a user does
1531 	 *	echo yaddayadda > special-file
1532 	 * by sending a CREATE request.
1533 	 * The original code would check read-only export status
1534 	 * only for regular files and directories, allowing
1535 	 * clients to chown/chmod device files and fifos even
1536 	 * on volumes exported read-only. */
1537 	if (!(acc & _NFSD_IRIX_BOGOSITY)
1538 	 && (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC))) {
1539 		if (EX_RDONLY(exp) || IS_RDONLY(inode))
1540 			return nfserr_rofs;
1541 		if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1542 			return nfserr_perm;
1543 	}
1544 	if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1545 		return nfserr_perm;
1546 
1547 	if (acc & MAY_LOCK) {
1548 		/* If we cannot rely on authentication in NLM requests,
1549 		 * just allow locks, otherwise require read permission, or
1550 		 * ownership
1551 		 */
1552 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1553 			return 0;
1554 		else
1555 			acc = MAY_READ | MAY_OWNER_OVERRIDE;
1556 	}
1557 	/*
1558 	 * The file owner always gets access permission for accesses that
1559 	 * would normally be checked at open time. This is to make
1560 	 * file access work even when the client has done a fchmod(fd, 0).
1561 	 *
1562 	 * However, `cp foo bar' should fail nevertheless when bar is
1563 	 * readonly. A sensible way to do this might be to reject all
1564 	 * attempts to truncate a read-only file, because a creat() call
1565 	 * always implies file truncation.
1566 	 * ... but this isn't really fair.  A process may reasonably call
1567 	 * ftruncate on an open file descriptor on a file with perm 000.
1568 	 * We must trust the client to do permission checking - using "ACCESS"
1569 	 * with NFSv3.
1570 	 */
1571 	if ((acc & MAY_OWNER_OVERRIDE) &&
1572 	    inode->i_uid == current->fsuid)
1573 		return 0;
1574 
1575 	err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1576 
1577 	/* Allow read access to binaries even when mode 111 */
1578 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
1579 	    acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1580 		err = permission(inode, MAY_EXEC);
1581 
1582 	return err? nfserrno(err) : 0;
1583 }
1584 
1585 void
nfsd_racache_shutdown(void)1586 nfsd_racache_shutdown(void)
1587 {
1588 	if (!raparm_cache)
1589 		return;
1590 	dprintk("nfsd: freeing readahead buffers.\n");
1591 	kfree(raparml);
1592 	raparm_cache = raparml = NULL;
1593 }
1594 /*
1595  * Initialize readahead param cache
1596  */
1597 int
nfsd_racache_init(int cache_size)1598 nfsd_racache_init(int cache_size)
1599 {
1600 	int	i;
1601 
1602 	if (raparm_cache)
1603 		return 0;
1604 	raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1605 
1606 	if (raparml != NULL) {
1607 		dprintk("nfsd: allocating %d readahead buffers.\n",
1608 			cache_size);
1609 		memset(raparml, 0, sizeof(struct raparms) * cache_size);
1610 		for (i = 0; i < cache_size - 1; i++) {
1611 			raparml[i].p_next = raparml + i + 1;
1612 		}
1613 		raparm_cache = raparml;
1614 	} else {
1615 		printk(KERN_WARNING
1616 		       "nfsd: Could not allocate memory read-ahead cache.\n");
1617 		return -ENOMEM;
1618 	}
1619 	nfsdstats.ra_size = cache_size;
1620 	return 0;
1621 }
1622