Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Process version 2 NFS requests.
  4 *
  5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
  6 */
  7
  8#include <linux/namei.h>
  9
 10#include "cache.h"
 11#include "xdr.h"
 12#include "vfs.h"
 13
 14#define NFSDDBG_FACILITY		NFSDDBG_PROC
 15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 16static __be32
 17nfsd_proc_null(struct svc_rqst *rqstp)
 18{
 19	return rpc_success;
 20}
 21
 22/*
 23 * Get a file's attributes
 24 * N.B. After this call resp->fh needs an fh_put
 25 */
 26static __be32
 27nfsd_proc_getattr(struct svc_rqst *rqstp)
 28{
 29	struct nfsd_fhandle *argp = rqstp->rq_argp;
 30	struct nfsd_attrstat *resp = rqstp->rq_resp;
 31
 32	dprintk("nfsd: GETATTR  %s\n", SVCFH_fmt(&argp->fh));
 33
 34	fh_copy(&resp->fh, &argp->fh);
 35	resp->status = fh_verify(rqstp, &resp->fh, 0,
 36				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
 37	if (resp->status != nfs_ok)
 38		goto out;
 39	resp->status = fh_getattr(&resp->fh, &resp->stat);
 40out:
 
 41	return rpc_success;
 42}
 43
 44/*
 45 * Set a file's attributes
 46 * N.B. After this call resp->fh needs an fh_put
 47 */
 48static __be32
 49nfsd_proc_setattr(struct svc_rqst *rqstp)
 50{
 51	struct nfsd_sattrargs *argp = rqstp->rq_argp;
 52	struct nfsd_attrstat *resp = rqstp->rq_resp;
 53	struct iattr *iap = &argp->attrs;
 54	struct nfsd_attrs attrs = {
 55		.na_iattr	= iap,
 56	};
 57	struct svc_fh *fhp;
 58
 59	dprintk("nfsd: SETATTR  %s, valid=%x, size=%ld\n",
 60		SVCFH_fmt(&argp->fh),
 61		argp->attrs.ia_valid, (long) argp->attrs.ia_size);
 62
 63	fhp = fh_copy(&resp->fh, &argp->fh);
 64
 65	/*
 66	 * NFSv2 does not differentiate between "set-[ac]time-to-now"
 67	 * which only requires access, and "set-[ac]time-to-X" which
 68	 * requires ownership.
 69	 * So if it looks like it might be "set both to the same time which
 70	 * is close to now", and if setattr_prepare fails, then we
 71	 * convert to "set to now" instead of "set to explicit time"
 72	 *
 73	 * We only call setattr_prepare as the last test as technically
 74	 * it is not an interface that we should be using.
 75	 */
 76#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
 77#define	MAX_TOUCH_TIME_ERROR (30*60)
 78	if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
 79	    iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
 80		/*
 81		 * Looks probable.
 82		 *
 83		 * Now just make sure time is in the right ballpark.
 84		 * Solaris, at least, doesn't seem to care what the time
 85		 * request is.  We require it be within 30 minutes of now.
 86		 */
 87		time64_t delta = iap->ia_atime.tv_sec - ktime_get_real_seconds();
 88
 89		resp->status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
 90		if (resp->status != nfs_ok)
 91			goto out;
 92
 93		if (delta < 0)
 94			delta = -delta;
 95		if (delta < MAX_TOUCH_TIME_ERROR &&
 96		    setattr_prepare(&init_user_ns, fhp->fh_dentry, iap) != 0) {
 97			/*
 98			 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
 99			 * This will cause notify_change to set these times
100			 * to "now"
101			 */
102			iap->ia_valid &= ~BOTH_TIME_SET;
103		}
104	}
105
106	resp->status = nfsd_setattr(rqstp, fhp, &attrs, 0, (time64_t)0);
107	if (resp->status != nfs_ok)
108		goto out;
109
110	resp->status = fh_getattr(&resp->fh, &resp->stat);
111out:
 
112	return rpc_success;
113}
114
115/* Obsolete, replaced by MNTPROC_MNT. */
116static __be32
117nfsd_proc_root(struct svc_rqst *rqstp)
118{
119	return rpc_success;
120}
121
122/*
123 * Look up a path name component
124 * Note: the dentry in the resp->fh may be negative if the file
125 * doesn't exist yet.
126 * N.B. After this call resp->fh needs an fh_put
127 */
128static __be32
129nfsd_proc_lookup(struct svc_rqst *rqstp)
130{
131	struct nfsd_diropargs *argp = rqstp->rq_argp;
132	struct nfsd_diropres *resp = rqstp->rq_resp;
133
134	dprintk("nfsd: LOOKUP   %s %.*s\n",
135		SVCFH_fmt(&argp->fh), argp->len, argp->name);
136
137	fh_init(&resp->fh, NFS_FHSIZE);
138	resp->status = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
139				   &resp->fh);
140	fh_put(&argp->fh);
141	if (resp->status != nfs_ok)
142		goto out;
143
144	resp->status = fh_getattr(&resp->fh, &resp->stat);
145out:
 
146	return rpc_success;
147}
148
149/*
150 * Read a symlink.
151 */
152static __be32
153nfsd_proc_readlink(struct svc_rqst *rqstp)
154{
155	struct nfsd_fhandle *argp = rqstp->rq_argp;
156	struct nfsd_readlinkres *resp = rqstp->rq_resp;
157
158	dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
159
160	/* Read the symlink. */
161	resp->len = NFS_MAXPATHLEN;
162	resp->page = *(rqstp->rq_next_page++);
163	resp->status = nfsd_readlink(rqstp, &argp->fh,
164				     page_address(resp->page), &resp->len);
165
166	fh_put(&argp->fh);
 
167	return rpc_success;
168}
169
170/*
171 * Read a portion of a file.
172 * N.B. After this call resp->fh needs an fh_put
173 */
174static __be32
175nfsd_proc_read(struct svc_rqst *rqstp)
176{
177	struct nfsd_readargs *argp = rqstp->rq_argp;
178	struct nfsd_readres *resp = rqstp->rq_resp;
179	unsigned int len;
180	u32 eof;
181	int v;
182
183	dprintk("nfsd: READ    %s %d bytes at %d\n",
184		SVCFH_fmt(&argp->fh),
185		argp->count, argp->offset);
186
187	argp->count = min_t(u32, argp->count, NFSSVC_MAXBLKSIZE_V2);
188	argp->count = min_t(u32, argp->count, rqstp->rq_res.buflen);
189
190	v = 0;
191	len = argp->count;
192	resp->pages = rqstp->rq_next_page;
193	while (len > 0) {
194		struct page *page = *(rqstp->rq_next_page++);
195
196		rqstp->rq_vec[v].iov_base = page_address(page);
197		rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
198		len -= rqstp->rq_vec[v].iov_len;
199		v++;
200	}
201
202	/* Obtain buffer pointer for payload. 19 is 1 word for
203	 * status, 17 words for fattr, and 1 word for the byte count.
204	 */
205	svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
206
207	resp->count = argp->count;
208	fh_copy(&resp->fh, &argp->fh);
209	resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
210				 rqstp->rq_vec, v, &resp->count, &eof);
211	if (resp->status == nfs_ok)
212		resp->status = fh_getattr(&resp->fh, &resp->stat);
213	else if (resp->status == nfserr_jukebox)
214		set_bit(RQ_DROPME, &rqstp->rq_flags);
 
215	return rpc_success;
216}
217
218/* Reserved */
219static __be32
220nfsd_proc_writecache(struct svc_rqst *rqstp)
221{
222	return rpc_success;
223}
224
225/*
226 * Write data to a file
227 * N.B. After this call resp->fh needs an fh_put
228 */
229static __be32
230nfsd_proc_write(struct svc_rqst *rqstp)
231{
232	struct nfsd_writeargs *argp = rqstp->rq_argp;
233	struct nfsd_attrstat *resp = rqstp->rq_resp;
234	unsigned long cnt = argp->len;
235	unsigned int nvecs;
236
237	dprintk("nfsd: WRITE    %s %u bytes at %d\n",
238		SVCFH_fmt(&argp->fh),
239		argp->len, argp->offset);
240
241	nvecs = svc_fill_write_vector(rqstp, &argp->payload);
242
243	resp->status = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
244				  argp->offset, rqstp->rq_vec, nvecs,
245				  &cnt, NFS_DATA_SYNC, NULL);
246	if (resp->status == nfs_ok)
247		resp->status = fh_getattr(&resp->fh, &resp->stat);
248	else if (resp->status == nfserr_jukebox)
249		set_bit(RQ_DROPME, &rqstp->rq_flags);
 
250	return rpc_success;
251}
252
253/*
254 * CREATE processing is complicated. The keyword here is `overloaded.'
255 * The parent directory is kept locked between the check for existence
256 * and the actual create() call in compliance with VFS protocols.
257 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
258 */
259static __be32
260nfsd_proc_create(struct svc_rqst *rqstp)
261{
262	struct nfsd_createargs *argp = rqstp->rq_argp;
263	struct nfsd_diropres *resp = rqstp->rq_resp;
264	svc_fh		*dirfhp = &argp->fh;
265	svc_fh		*newfhp = &resp->fh;
266	struct iattr	*attr = &argp->attrs;
267	struct nfsd_attrs attrs = {
268		.na_iattr	= attr,
269	};
270	struct inode	*inode;
271	struct dentry	*dchild;
272	int		type, mode;
273	int		hosterr;
274	dev_t		rdev = 0, wanted = new_decode_dev(attr->ia_size);
275
276	dprintk("nfsd: CREATE   %s %.*s\n",
277		SVCFH_fmt(dirfhp), argp->len, argp->name);
278
279	/* First verify the parent file handle */
280	resp->status = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
281	if (resp->status != nfs_ok)
282		goto done; /* must fh_put dirfhp even on error */
283
284	/* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
285
286	resp->status = nfserr_exist;
287	if (isdotent(argp->name, argp->len))
288		goto done;
289	hosterr = fh_want_write(dirfhp);
290	if (hosterr) {
291		resp->status = nfserrno(hosterr);
292		goto done;
293	}
294
295	inode_lock_nested(dirfhp->fh_dentry->d_inode, I_MUTEX_PARENT);
296	dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
297	if (IS_ERR(dchild)) {
298		resp->status = nfserrno(PTR_ERR(dchild));
299		goto out_unlock;
300	}
301	fh_init(newfhp, NFS_FHSIZE);
302	resp->status = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
303	if (!resp->status && d_really_is_negative(dchild))
304		resp->status = nfserr_noent;
305	dput(dchild);
306	if (resp->status) {
307		if (resp->status != nfserr_noent)
308			goto out_unlock;
309		/*
310		 * If the new file handle wasn't verified, we can't tell
311		 * whether the file exists or not. Time to bail ...
312		 */
313		resp->status = nfserr_acces;
314		if (!newfhp->fh_dentry) {
315			printk(KERN_WARNING 
316				"nfsd_proc_create: file handle not verified\n");
317			goto out_unlock;
318		}
319	}
320
321	inode = d_inode(newfhp->fh_dentry);
322
323	/* Unfudge the mode bits */
324	if (attr->ia_valid & ATTR_MODE) {
325		type = attr->ia_mode & S_IFMT;
326		mode = attr->ia_mode & ~S_IFMT;
327		if (!type) {
328			/* no type, so if target exists, assume same as that,
329			 * else assume a file */
330			if (inode) {
331				type = inode->i_mode & S_IFMT;
332				switch(type) {
333				case S_IFCHR:
334				case S_IFBLK:
335					/* reserve rdev for later checking */
336					rdev = inode->i_rdev;
337					attr->ia_valid |= ATTR_SIZE;
338
339					fallthrough;
340				case S_IFIFO:
341					/* this is probably a permission check..
342					 * at least IRIX implements perm checking on
343					 *   echo thing > device-special-file-or-pipe
344					 * by doing a CREATE with type==0
345					 */
346					resp->status = nfsd_permission(rqstp,
347								 newfhp->fh_export,
348								 newfhp->fh_dentry,
349								 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
 
350					if (resp->status && resp->status != nfserr_rofs)
351						goto out_unlock;
352				}
353			} else
354				type = S_IFREG;
355		}
356	} else if (inode) {
357		type = inode->i_mode & S_IFMT;
358		mode = inode->i_mode & ~S_IFMT;
359	} else {
360		type = S_IFREG;
361		mode = 0;	/* ??? */
362	}
363
364	attr->ia_valid |= ATTR_MODE;
365	attr->ia_mode = mode;
366
367	/* Special treatment for non-regular files according to the
368	 * gospel of sun micro
369	 */
370	if (type != S_IFREG) {
371		if (type != S_IFBLK && type != S_IFCHR) {
372			rdev = 0;
373		} else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
374			/* If you think you've seen the worst, grok this. */
375			type = S_IFIFO;
376		} else {
377			/* Okay, char or block special */
378			if (!rdev)
379				rdev = wanted;
380		}
381
382		/* we've used the SIZE information, so discard it */
383		attr->ia_valid &= ~ATTR_SIZE;
384
385		/* Make sure the type and device matches */
386		resp->status = nfserr_exist;
387		if (inode && inode_wrong_type(inode, type))
388			goto out_unlock;
389	}
390
391	resp->status = nfs_ok;
392	if (!inode) {
393		/* File doesn't exist. Create it and set attrs */
394		resp->status = nfsd_create_locked(rqstp, dirfhp, &attrs, type,
395						  rdev, newfhp);
396	} else if (type == S_IFREG) {
397		dprintk("nfsd:   existing %s, valid=%x, size=%ld\n",
398			argp->name, attr->ia_valid, (long) attr->ia_size);
399		/* File already exists. We ignore all attributes except
400		 * size, so that creat() behaves exactly like
401		 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
402		 */
403		attr->ia_valid &= ATTR_SIZE;
404		if (attr->ia_valid)
405			resp->status = nfsd_setattr(rqstp, newfhp, &attrs, 0,
406						    (time64_t)0);
407	}
408
409out_unlock:
410	inode_unlock(dirfhp->fh_dentry->d_inode);
411	fh_drop_write(dirfhp);
412done:
413	fh_put(dirfhp);
414	if (resp->status != nfs_ok)
415		goto out;
416	resp->status = fh_getattr(&resp->fh, &resp->stat);
417out:
 
418	return rpc_success;
419}
420
421static __be32
422nfsd_proc_remove(struct svc_rqst *rqstp)
423{
424	struct nfsd_diropargs *argp = rqstp->rq_argp;
425	struct nfsd_stat *resp = rqstp->rq_resp;
426
427	dprintk("nfsd: REMOVE   %s %.*s\n", SVCFH_fmt(&argp->fh),
428		argp->len, argp->name);
429
430	/* Unlink. -SIFDIR means file must not be a directory */
431	resp->status = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR,
432				   argp->name, argp->len);
433	fh_put(&argp->fh);
 
434	return rpc_success;
435}
436
437static __be32
438nfsd_proc_rename(struct svc_rqst *rqstp)
439{
440	struct nfsd_renameargs *argp = rqstp->rq_argp;
441	struct nfsd_stat *resp = rqstp->rq_resp;
442
443	dprintk("nfsd: RENAME   %s %.*s -> \n",
444		SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
445	dprintk("nfsd:        ->  %s %.*s\n",
446		SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
447
448	resp->status = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
449				   &argp->tfh, argp->tname, argp->tlen);
450	fh_put(&argp->ffh);
451	fh_put(&argp->tfh);
 
452	return rpc_success;
453}
454
455static __be32
456nfsd_proc_link(struct svc_rqst *rqstp)
457{
458	struct nfsd_linkargs *argp = rqstp->rq_argp;
459	struct nfsd_stat *resp = rqstp->rq_resp;
460
461	dprintk("nfsd: LINK     %s ->\n",
462		SVCFH_fmt(&argp->ffh));
463	dprintk("nfsd:    %s %.*s\n",
464		SVCFH_fmt(&argp->tfh),
465		argp->tlen,
466		argp->tname);
467
468	resp->status = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
469				 &argp->ffh);
470	fh_put(&argp->ffh);
471	fh_put(&argp->tfh);
 
472	return rpc_success;
473}
474
475static __be32
476nfsd_proc_symlink(struct svc_rqst *rqstp)
477{
478	struct nfsd_symlinkargs *argp = rqstp->rq_argp;
479	struct nfsd_stat *resp = rqstp->rq_resp;
480	struct nfsd_attrs attrs = {
481		.na_iattr	= &argp->attrs,
482	};
483	struct svc_fh	newfh;
484
485	if (argp->tlen > NFS_MAXPATHLEN) {
486		resp->status = nfserr_nametoolong;
487		goto out;
488	}
489
490	argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
491						page_address(rqstp->rq_arg.pages[0]),
492						argp->tlen);
493	if (IS_ERR(argp->tname)) {
494		resp->status = nfserrno(PTR_ERR(argp->tname));
495		goto out;
496	}
497
498	dprintk("nfsd: SYMLINK  %s %.*s -> %.*s\n",
499		SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
500		argp->tlen, argp->tname);
501
502	fh_init(&newfh, NFS_FHSIZE);
503	resp->status = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
504				    argp->tname, &attrs, &newfh);
505
506	kfree(argp->tname);
507	fh_put(&argp->ffh);
508	fh_put(&newfh);
509out:
 
510	return rpc_success;
511}
512
513/*
514 * Make directory. This operation is not idempotent.
515 * N.B. After this call resp->fh needs an fh_put
516 */
517static __be32
518nfsd_proc_mkdir(struct svc_rqst *rqstp)
519{
520	struct nfsd_createargs *argp = rqstp->rq_argp;
521	struct nfsd_diropres *resp = rqstp->rq_resp;
522	struct nfsd_attrs attrs = {
523		.na_iattr	= &argp->attrs,
524	};
525
526	dprintk("nfsd: MKDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
527
528	if (resp->fh.fh_dentry) {
529		printk(KERN_WARNING
530			"nfsd_proc_mkdir: response already verified??\n");
531	}
532
533	argp->attrs.ia_valid &= ~ATTR_SIZE;
534	fh_init(&resp->fh, NFS_FHSIZE);
535	resp->status = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
536				   &attrs, S_IFDIR, 0, &resp->fh);
537	fh_put(&argp->fh);
538	if (resp->status != nfs_ok)
539		goto out;
540
541	resp->status = fh_getattr(&resp->fh, &resp->stat);
542out:
 
543	return rpc_success;
544}
545
546/*
547 * Remove a directory
548 */
549static __be32
550nfsd_proc_rmdir(struct svc_rqst *rqstp)
551{
552	struct nfsd_diropargs *argp = rqstp->rq_argp;
553	struct nfsd_stat *resp = rqstp->rq_resp;
554
555	dprintk("nfsd: RMDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
556
557	resp->status = nfsd_unlink(rqstp, &argp->fh, S_IFDIR,
558				   argp->name, argp->len);
559	fh_put(&argp->fh);
 
560	return rpc_success;
561}
562
563static void nfsd_init_dirlist_pages(struct svc_rqst *rqstp,
564				    struct nfsd_readdirres *resp,
565				    u32 count)
566{
567	struct xdr_buf *buf = &resp->dirlist;
568	struct xdr_stream *xdr = &resp->xdr;
569
570	memset(buf, 0, sizeof(*buf));
571
572	/* Reserve room for the NULL ptr & eof flag (-2 words) */
573	buf->buflen = clamp(count, (u32)(XDR_UNIT * 2), (u32)PAGE_SIZE);
574	buf->buflen -= XDR_UNIT * 2;
575	buf->pages = rqstp->rq_next_page;
576	rqstp->rq_next_page++;
577
578	xdr_init_encode_pages(xdr, buf, buf->pages,  NULL);
579}
580
581/*
582 * Read a portion of a directory.
583 */
584static __be32
585nfsd_proc_readdir(struct svc_rqst *rqstp)
586{
587	struct nfsd_readdirargs *argp = rqstp->rq_argp;
588	struct nfsd_readdirres *resp = rqstp->rq_resp;
589	loff_t		offset;
590
591	dprintk("nfsd: READDIR  %s %d bytes at %d\n",
592		SVCFH_fmt(&argp->fh),		
593		argp->count, argp->cookie);
594
595	nfsd_init_dirlist_pages(rqstp, resp, argp->count);
596
597	resp->common.err = nfs_ok;
598	resp->cookie_offset = 0;
599	offset = argp->cookie;
600	resp->status = nfsd_readdir(rqstp, &argp->fh, &offset,
601				    &resp->common, nfssvc_encode_entry);
602	nfssvc_encode_nfscookie(resp, offset);
603
604	fh_put(&argp->fh);
 
605	return rpc_success;
606}
607
608/*
609 * Get file system info
610 */
611static __be32
612nfsd_proc_statfs(struct svc_rqst *rqstp)
613{
614	struct nfsd_fhandle *argp = rqstp->rq_argp;
615	struct nfsd_statfsres *resp = rqstp->rq_resp;
616
617	dprintk("nfsd: STATFS   %s\n", SVCFH_fmt(&argp->fh));
618
619	resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
620				   NFSD_MAY_BYPASS_GSS_ON_ROOT);
621	fh_put(&argp->fh);
 
622	return rpc_success;
623}
624
625/*
626 * NFSv2 Server procedures.
627 * Only the results of non-idempotent operations are cached.
628 */
629
630#define ST 1		/* status */
631#define FH 8		/* filehandle */
632#define	AT 18		/* attributes */
633
634static const struct svc_procedure nfsd_procedures2[18] = {
635	[NFSPROC_NULL] = {
636		.pc_func = nfsd_proc_null,
637		.pc_decode = nfssvc_decode_voidarg,
638		.pc_encode = nfssvc_encode_voidres,
639		.pc_argsize = sizeof(struct nfsd_voidargs),
640		.pc_argzero = sizeof(struct nfsd_voidargs),
641		.pc_ressize = sizeof(struct nfsd_voidres),
642		.pc_cachetype = RC_NOCACHE,
643		.pc_xdrressize = 0,
644		.pc_name = "NULL",
645	},
646	[NFSPROC_GETATTR] = {
647		.pc_func = nfsd_proc_getattr,
648		.pc_decode = nfssvc_decode_fhandleargs,
649		.pc_encode = nfssvc_encode_attrstatres,
650		.pc_release = nfssvc_release_attrstat,
651		.pc_argsize = sizeof(struct nfsd_fhandle),
652		.pc_argzero = sizeof(struct nfsd_fhandle),
653		.pc_ressize = sizeof(struct nfsd_attrstat),
654		.pc_cachetype = RC_NOCACHE,
655		.pc_xdrressize = ST+AT,
656		.pc_name = "GETATTR",
657	},
658	[NFSPROC_SETATTR] = {
659		.pc_func = nfsd_proc_setattr,
660		.pc_decode = nfssvc_decode_sattrargs,
661		.pc_encode = nfssvc_encode_attrstatres,
662		.pc_release = nfssvc_release_attrstat,
663		.pc_argsize = sizeof(struct nfsd_sattrargs),
664		.pc_argzero = sizeof(struct nfsd_sattrargs),
665		.pc_ressize = sizeof(struct nfsd_attrstat),
666		.pc_cachetype = RC_REPLBUFF,
667		.pc_xdrressize = ST+AT,
668		.pc_name = "SETATTR",
669	},
670	[NFSPROC_ROOT] = {
671		.pc_func = nfsd_proc_root,
672		.pc_decode = nfssvc_decode_voidarg,
673		.pc_encode = nfssvc_encode_voidres,
674		.pc_argsize = sizeof(struct nfsd_voidargs),
675		.pc_argzero = sizeof(struct nfsd_voidargs),
676		.pc_ressize = sizeof(struct nfsd_voidres),
677		.pc_cachetype = RC_NOCACHE,
678		.pc_xdrressize = 0,
679		.pc_name = "ROOT",
680	},
681	[NFSPROC_LOOKUP] = {
682		.pc_func = nfsd_proc_lookup,
683		.pc_decode = nfssvc_decode_diropargs,
684		.pc_encode = nfssvc_encode_diropres,
685		.pc_release = nfssvc_release_diropres,
686		.pc_argsize = sizeof(struct nfsd_diropargs),
687		.pc_argzero = sizeof(struct nfsd_diropargs),
688		.pc_ressize = sizeof(struct nfsd_diropres),
689		.pc_cachetype = RC_NOCACHE,
690		.pc_xdrressize = ST+FH+AT,
691		.pc_name = "LOOKUP",
692	},
693	[NFSPROC_READLINK] = {
694		.pc_func = nfsd_proc_readlink,
695		.pc_decode = nfssvc_decode_fhandleargs,
696		.pc_encode = nfssvc_encode_readlinkres,
697		.pc_argsize = sizeof(struct nfsd_fhandle),
698		.pc_argzero = sizeof(struct nfsd_fhandle),
699		.pc_ressize = sizeof(struct nfsd_readlinkres),
700		.pc_cachetype = RC_NOCACHE,
701		.pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
702		.pc_name = "READLINK",
703	},
704	[NFSPROC_READ] = {
705		.pc_func = nfsd_proc_read,
706		.pc_decode = nfssvc_decode_readargs,
707		.pc_encode = nfssvc_encode_readres,
708		.pc_release = nfssvc_release_readres,
709		.pc_argsize = sizeof(struct nfsd_readargs),
710		.pc_argzero = sizeof(struct nfsd_readargs),
711		.pc_ressize = sizeof(struct nfsd_readres),
712		.pc_cachetype = RC_NOCACHE,
713		.pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
714		.pc_name = "READ",
715	},
716	[NFSPROC_WRITECACHE] = {
717		.pc_func = nfsd_proc_writecache,
718		.pc_decode = nfssvc_decode_voidarg,
719		.pc_encode = nfssvc_encode_voidres,
720		.pc_argsize = sizeof(struct nfsd_voidargs),
721		.pc_argzero = sizeof(struct nfsd_voidargs),
722		.pc_ressize = sizeof(struct nfsd_voidres),
723		.pc_cachetype = RC_NOCACHE,
724		.pc_xdrressize = 0,
725		.pc_name = "WRITECACHE",
726	},
727	[NFSPROC_WRITE] = {
728		.pc_func = nfsd_proc_write,
729		.pc_decode = nfssvc_decode_writeargs,
730		.pc_encode = nfssvc_encode_attrstatres,
731		.pc_release = nfssvc_release_attrstat,
732		.pc_argsize = sizeof(struct nfsd_writeargs),
733		.pc_argzero = sizeof(struct nfsd_writeargs),
734		.pc_ressize = sizeof(struct nfsd_attrstat),
735		.pc_cachetype = RC_REPLBUFF,
736		.pc_xdrressize = ST+AT,
737		.pc_name = "WRITE",
738	},
739	[NFSPROC_CREATE] = {
740		.pc_func = nfsd_proc_create,
741		.pc_decode = nfssvc_decode_createargs,
742		.pc_encode = nfssvc_encode_diropres,
743		.pc_release = nfssvc_release_diropres,
744		.pc_argsize = sizeof(struct nfsd_createargs),
745		.pc_argzero = sizeof(struct nfsd_createargs),
746		.pc_ressize = sizeof(struct nfsd_diropres),
747		.pc_cachetype = RC_REPLBUFF,
748		.pc_xdrressize = ST+FH+AT,
749		.pc_name = "CREATE",
750	},
751	[NFSPROC_REMOVE] = {
752		.pc_func = nfsd_proc_remove,
753		.pc_decode = nfssvc_decode_diropargs,
754		.pc_encode = nfssvc_encode_statres,
755		.pc_argsize = sizeof(struct nfsd_diropargs),
756		.pc_argzero = sizeof(struct nfsd_diropargs),
757		.pc_ressize = sizeof(struct nfsd_stat),
758		.pc_cachetype = RC_REPLSTAT,
759		.pc_xdrressize = ST,
760		.pc_name = "REMOVE",
761	},
762	[NFSPROC_RENAME] = {
763		.pc_func = nfsd_proc_rename,
764		.pc_decode = nfssvc_decode_renameargs,
765		.pc_encode = nfssvc_encode_statres,
766		.pc_argsize = sizeof(struct nfsd_renameargs),
767		.pc_argzero = sizeof(struct nfsd_renameargs),
768		.pc_ressize = sizeof(struct nfsd_stat),
769		.pc_cachetype = RC_REPLSTAT,
770		.pc_xdrressize = ST,
771		.pc_name = "RENAME",
772	},
773	[NFSPROC_LINK] = {
774		.pc_func = nfsd_proc_link,
775		.pc_decode = nfssvc_decode_linkargs,
776		.pc_encode = nfssvc_encode_statres,
777		.pc_argsize = sizeof(struct nfsd_linkargs),
778		.pc_argzero = sizeof(struct nfsd_linkargs),
779		.pc_ressize = sizeof(struct nfsd_stat),
780		.pc_cachetype = RC_REPLSTAT,
781		.pc_xdrressize = ST,
782		.pc_name = "LINK",
783	},
784	[NFSPROC_SYMLINK] = {
785		.pc_func = nfsd_proc_symlink,
786		.pc_decode = nfssvc_decode_symlinkargs,
787		.pc_encode = nfssvc_encode_statres,
788		.pc_argsize = sizeof(struct nfsd_symlinkargs),
789		.pc_argzero = sizeof(struct nfsd_symlinkargs),
790		.pc_ressize = sizeof(struct nfsd_stat),
791		.pc_cachetype = RC_REPLSTAT,
792		.pc_xdrressize = ST,
793		.pc_name = "SYMLINK",
794	},
795	[NFSPROC_MKDIR] = {
796		.pc_func = nfsd_proc_mkdir,
797		.pc_decode = nfssvc_decode_createargs,
798		.pc_encode = nfssvc_encode_diropres,
799		.pc_release = nfssvc_release_diropres,
800		.pc_argsize = sizeof(struct nfsd_createargs),
801		.pc_argzero = sizeof(struct nfsd_createargs),
802		.pc_ressize = sizeof(struct nfsd_diropres),
803		.pc_cachetype = RC_REPLBUFF,
804		.pc_xdrressize = ST+FH+AT,
805		.pc_name = "MKDIR",
806	},
807	[NFSPROC_RMDIR] = {
808		.pc_func = nfsd_proc_rmdir,
809		.pc_decode = nfssvc_decode_diropargs,
810		.pc_encode = nfssvc_encode_statres,
811		.pc_argsize = sizeof(struct nfsd_diropargs),
812		.pc_argzero = sizeof(struct nfsd_diropargs),
813		.pc_ressize = sizeof(struct nfsd_stat),
814		.pc_cachetype = RC_REPLSTAT,
815		.pc_xdrressize = ST,
816		.pc_name = "RMDIR",
817	},
818	[NFSPROC_READDIR] = {
819		.pc_func = nfsd_proc_readdir,
820		.pc_decode = nfssvc_decode_readdirargs,
821		.pc_encode = nfssvc_encode_readdirres,
822		.pc_argsize = sizeof(struct nfsd_readdirargs),
823		.pc_argzero = sizeof(struct nfsd_readdirargs),
824		.pc_ressize = sizeof(struct nfsd_readdirres),
825		.pc_cachetype = RC_NOCACHE,
826		.pc_name = "READDIR",
827	},
828	[NFSPROC_STATFS] = {
829		.pc_func = nfsd_proc_statfs,
830		.pc_decode = nfssvc_decode_fhandleargs,
831		.pc_encode = nfssvc_encode_statfsres,
832		.pc_argsize = sizeof(struct nfsd_fhandle),
833		.pc_argzero = sizeof(struct nfsd_fhandle),
834		.pc_ressize = sizeof(struct nfsd_statfsres),
835		.pc_cachetype = RC_NOCACHE,
836		.pc_xdrressize = ST+5,
837		.pc_name = "STATFS",
838	},
839};
840
841
842static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
843const struct svc_version nfsd_version2 = {
844	.vs_vers	= 2,
845	.vs_nproc	= 18,
846	.vs_proc	= nfsd_procedures2,
847	.vs_count	= nfsd_count2,
848	.vs_dispatch	= nfsd_dispatch,
849	.vs_xdrsize	= NFS2_SVC_XDRSIZE,
850};
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Process version 2 NFS requests.
  4 *
  5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
  6 */
  7
  8#include <linux/namei.h>
  9
 10#include "cache.h"
 11#include "xdr.h"
 12#include "vfs.h"
 13
 14#define NFSDDBG_FACILITY		NFSDDBG_PROC
 15
 16static __be32 nfsd_map_status(__be32 status)
 17{
 18	switch (status) {
 19	case nfs_ok:
 20		break;
 21	case nfserr_nofilehandle:
 22	case nfserr_badhandle:
 23		status = nfserr_stale;
 24		break;
 25	case nfserr_wrongsec:
 26	case nfserr_xdev:
 27	case nfserr_file_open:
 28		status = nfserr_acces;
 29		break;
 30	case nfserr_symlink_not_dir:
 31		status = nfserr_notdir;
 32		break;
 33	case nfserr_symlink:
 34	case nfserr_wrong_type:
 35		status = nfserr_inval;
 36		break;
 37	}
 38	return status;
 39}
 40
 41static __be32
 42nfsd_proc_null(struct svc_rqst *rqstp)
 43{
 44	return rpc_success;
 45}
 46
 47/*
 48 * Get a file's attributes
 49 * N.B. After this call resp->fh needs an fh_put
 50 */
 51static __be32
 52nfsd_proc_getattr(struct svc_rqst *rqstp)
 53{
 54	struct nfsd_fhandle *argp = rqstp->rq_argp;
 55	struct nfsd_attrstat *resp = rqstp->rq_resp;
 56
 57	dprintk("nfsd: GETATTR  %s\n", SVCFH_fmt(&argp->fh));
 58
 59	fh_copy(&resp->fh, &argp->fh);
 60	resp->status = fh_verify(rqstp, &resp->fh, 0,
 61				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
 62	if (resp->status != nfs_ok)
 63		goto out;
 64	resp->status = fh_getattr(&resp->fh, &resp->stat);
 65out:
 66	resp->status = nfsd_map_status(resp->status);
 67	return rpc_success;
 68}
 69
 70/*
 71 * Set a file's attributes
 72 * N.B. After this call resp->fh needs an fh_put
 73 */
 74static __be32
 75nfsd_proc_setattr(struct svc_rqst *rqstp)
 76{
 77	struct nfsd_sattrargs *argp = rqstp->rq_argp;
 78	struct nfsd_attrstat *resp = rqstp->rq_resp;
 79	struct iattr *iap = &argp->attrs;
 80	struct nfsd_attrs attrs = {
 81		.na_iattr	= iap,
 82	};
 83	struct svc_fh *fhp;
 84
 85	dprintk("nfsd: SETATTR  %s, valid=%x, size=%ld\n",
 86		SVCFH_fmt(&argp->fh),
 87		argp->attrs.ia_valid, (long) argp->attrs.ia_size);
 88
 89	fhp = fh_copy(&resp->fh, &argp->fh);
 90
 91	/*
 92	 * NFSv2 does not differentiate between "set-[ac]time-to-now"
 93	 * which only requires access, and "set-[ac]time-to-X" which
 94	 * requires ownership.
 95	 * So if it looks like it might be "set both to the same time which
 96	 * is close to now", and if setattr_prepare fails, then we
 97	 * convert to "set to now" instead of "set to explicit time"
 98	 *
 99	 * We only call setattr_prepare as the last test as technically
100	 * it is not an interface that we should be using.
101	 */
102#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
103#define	MAX_TOUCH_TIME_ERROR (30*60)
104	if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
105	    iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
106		/*
107		 * Looks probable.
108		 *
109		 * Now just make sure time is in the right ballpark.
110		 * Solaris, at least, doesn't seem to care what the time
111		 * request is.  We require it be within 30 minutes of now.
112		 */
113		time64_t delta = iap->ia_atime.tv_sec - ktime_get_real_seconds();
114
115		resp->status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
116		if (resp->status != nfs_ok)
117			goto out;
118
119		if (delta < 0)
120			delta = -delta;
121		if (delta < MAX_TOUCH_TIME_ERROR &&
122		    setattr_prepare(&nop_mnt_idmap, fhp->fh_dentry, iap) != 0) {
123			/*
124			 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
125			 * This will cause notify_change to set these times
126			 * to "now"
127			 */
128			iap->ia_valid &= ~BOTH_TIME_SET;
129		}
130	}
131
132	resp->status = nfsd_setattr(rqstp, fhp, &attrs, NULL);
133	if (resp->status != nfs_ok)
134		goto out;
135
136	resp->status = fh_getattr(&resp->fh, &resp->stat);
137out:
138	resp->status = nfsd_map_status(resp->status);
139	return rpc_success;
140}
141
142/* Obsolete, replaced by MNTPROC_MNT. */
143static __be32
144nfsd_proc_root(struct svc_rqst *rqstp)
145{
146	return rpc_success;
147}
148
149/*
150 * Look up a path name component
151 * Note: the dentry in the resp->fh may be negative if the file
152 * doesn't exist yet.
153 * N.B. After this call resp->fh needs an fh_put
154 */
155static __be32
156nfsd_proc_lookup(struct svc_rqst *rqstp)
157{
158	struct nfsd_diropargs *argp = rqstp->rq_argp;
159	struct nfsd_diropres *resp = rqstp->rq_resp;
160
161	dprintk("nfsd: LOOKUP   %s %.*s\n",
162		SVCFH_fmt(&argp->fh), argp->len, argp->name);
163
164	fh_init(&resp->fh, NFS_FHSIZE);
165	resp->status = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
166				   &resp->fh);
167	fh_put(&argp->fh);
168	if (resp->status != nfs_ok)
169		goto out;
170
171	resp->status = fh_getattr(&resp->fh, &resp->stat);
172out:
173	resp->status = nfsd_map_status(resp->status);
174	return rpc_success;
175}
176
177/*
178 * Read a symlink.
179 */
180static __be32
181nfsd_proc_readlink(struct svc_rqst *rqstp)
182{
183	struct nfsd_fhandle *argp = rqstp->rq_argp;
184	struct nfsd_readlinkres *resp = rqstp->rq_resp;
185
186	dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
187
188	/* Read the symlink. */
189	resp->len = NFS_MAXPATHLEN;
190	resp->page = *(rqstp->rq_next_page++);
191	resp->status = nfsd_readlink(rqstp, &argp->fh,
192				     page_address(resp->page), &resp->len);
193
194	fh_put(&argp->fh);
195	resp->status = nfsd_map_status(resp->status);
196	return rpc_success;
197}
198
199/*
200 * Read a portion of a file.
201 * N.B. After this call resp->fh needs an fh_put
202 */
203static __be32
204nfsd_proc_read(struct svc_rqst *rqstp)
205{
206	struct nfsd_readargs *argp = rqstp->rq_argp;
207	struct nfsd_readres *resp = rqstp->rq_resp;
 
208	u32 eof;
 
209
210	dprintk("nfsd: READ    %s %d bytes at %d\n",
211		SVCFH_fmt(&argp->fh),
212		argp->count, argp->offset);
213
214	argp->count = min_t(u32, argp->count, NFSSVC_MAXBLKSIZE_V2);
215	argp->count = min_t(u32, argp->count, rqstp->rq_res.buflen);
216
 
 
217	resp->pages = rqstp->rq_next_page;
 
 
 
 
 
 
 
 
218
219	/* Obtain buffer pointer for payload. 19 is 1 word for
220	 * status, 17 words for fattr, and 1 word for the byte count.
221	 */
222	svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
223
224	resp->count = argp->count;
225	fh_copy(&resp->fh, &argp->fh);
226	resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
227				 &resp->count, &eof);
228	if (resp->status == nfs_ok)
229		resp->status = fh_getattr(&resp->fh, &resp->stat);
230	else if (resp->status == nfserr_jukebox)
231		set_bit(RQ_DROPME, &rqstp->rq_flags);
232	resp->status = nfsd_map_status(resp->status);
233	return rpc_success;
234}
235
236/* Reserved */
237static __be32
238nfsd_proc_writecache(struct svc_rqst *rqstp)
239{
240	return rpc_success;
241}
242
243/*
244 * Write data to a file
245 * N.B. After this call resp->fh needs an fh_put
246 */
247static __be32
248nfsd_proc_write(struct svc_rqst *rqstp)
249{
250	struct nfsd_writeargs *argp = rqstp->rq_argp;
251	struct nfsd_attrstat *resp = rqstp->rq_resp;
252	unsigned long cnt = argp->len;
253	unsigned int nvecs;
254
255	dprintk("nfsd: WRITE    %s %u bytes at %d\n",
256		SVCFH_fmt(&argp->fh),
257		argp->len, argp->offset);
258
259	nvecs = svc_fill_write_vector(rqstp, &argp->payload);
260
261	resp->status = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
262				  argp->offset, rqstp->rq_vec, nvecs,
263				  &cnt, NFS_DATA_SYNC, NULL);
264	if (resp->status == nfs_ok)
265		resp->status = fh_getattr(&resp->fh, &resp->stat);
266	else if (resp->status == nfserr_jukebox)
267		set_bit(RQ_DROPME, &rqstp->rq_flags);
268	resp->status = nfsd_map_status(resp->status);
269	return rpc_success;
270}
271
272/*
273 * CREATE processing is complicated. The keyword here is `overloaded.'
274 * The parent directory is kept locked between the check for existence
275 * and the actual create() call in compliance with VFS protocols.
276 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
277 */
278static __be32
279nfsd_proc_create(struct svc_rqst *rqstp)
280{
281	struct nfsd_createargs *argp = rqstp->rq_argp;
282	struct nfsd_diropres *resp = rqstp->rq_resp;
283	svc_fh		*dirfhp = &argp->fh;
284	svc_fh		*newfhp = &resp->fh;
285	struct iattr	*attr = &argp->attrs;
286	struct nfsd_attrs attrs = {
287		.na_iattr	= attr,
288	};
289	struct inode	*inode;
290	struct dentry	*dchild;
291	int		type, mode;
292	int		hosterr;
293	dev_t		rdev = 0, wanted = new_decode_dev(attr->ia_size);
294
295	dprintk("nfsd: CREATE   %s %.*s\n",
296		SVCFH_fmt(dirfhp), argp->len, argp->name);
297
298	/* First verify the parent file handle */
299	resp->status = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
300	if (resp->status != nfs_ok)
301		goto done; /* must fh_put dirfhp even on error */
302
303	/* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
304
305	resp->status = nfserr_exist;
306	if (isdotent(argp->name, argp->len))
307		goto done;
308	hosterr = fh_want_write(dirfhp);
309	if (hosterr) {
310		resp->status = nfserrno(hosterr);
311		goto done;
312	}
313
314	inode_lock_nested(dirfhp->fh_dentry->d_inode, I_MUTEX_PARENT);
315	dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
316	if (IS_ERR(dchild)) {
317		resp->status = nfserrno(PTR_ERR(dchild));
318		goto out_unlock;
319	}
320	fh_init(newfhp, NFS_FHSIZE);
321	resp->status = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
322	if (!resp->status && d_really_is_negative(dchild))
323		resp->status = nfserr_noent;
324	dput(dchild);
325	if (resp->status) {
326		if (resp->status != nfserr_noent)
327			goto out_unlock;
328		/*
329		 * If the new file handle wasn't verified, we can't tell
330		 * whether the file exists or not. Time to bail ...
331		 */
332		resp->status = nfserr_acces;
333		if (!newfhp->fh_dentry) {
334			printk(KERN_WARNING 
335				"nfsd_proc_create: file handle not verified\n");
336			goto out_unlock;
337		}
338	}
339
340	inode = d_inode(newfhp->fh_dentry);
341
342	/* Unfudge the mode bits */
343	if (attr->ia_valid & ATTR_MODE) {
344		type = attr->ia_mode & S_IFMT;
345		mode = attr->ia_mode & ~S_IFMT;
346		if (!type) {
347			/* no type, so if target exists, assume same as that,
348			 * else assume a file */
349			if (inode) {
350				type = inode->i_mode & S_IFMT;
351				switch(type) {
352				case S_IFCHR:
353				case S_IFBLK:
354					/* reserve rdev for later checking */
355					rdev = inode->i_rdev;
356					attr->ia_valid |= ATTR_SIZE;
357
358					fallthrough;
359				case S_IFIFO:
360					/* this is probably a permission check..
361					 * at least IRIX implements perm checking on
362					 *   echo thing > device-special-file-or-pipe
363					 * by doing a CREATE with type==0
364					 */
365					resp->status = nfsd_permission(
366						&rqstp->rq_cred,
367						newfhp->fh_export,
368						newfhp->fh_dentry,
369						NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
370					if (resp->status && resp->status != nfserr_rofs)
371						goto out_unlock;
372				}
373			} else
374				type = S_IFREG;
375		}
376	} else if (inode) {
377		type = inode->i_mode & S_IFMT;
378		mode = inode->i_mode & ~S_IFMT;
379	} else {
380		type = S_IFREG;
381		mode = 0;	/* ??? */
382	}
383
384	attr->ia_valid |= ATTR_MODE;
385	attr->ia_mode = mode;
386
387	/* Special treatment for non-regular files according to the
388	 * gospel of sun micro
389	 */
390	if (type != S_IFREG) {
391		if (type != S_IFBLK && type != S_IFCHR) {
392			rdev = 0;
393		} else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
394			/* If you think you've seen the worst, grok this. */
395			type = S_IFIFO;
396		} else {
397			/* Okay, char or block special */
398			if (!rdev)
399				rdev = wanted;
400		}
401
402		/* we've used the SIZE information, so discard it */
403		attr->ia_valid &= ~ATTR_SIZE;
404
405		/* Make sure the type and device matches */
406		resp->status = nfserr_exist;
407		if (inode && inode_wrong_type(inode, type))
408			goto out_unlock;
409	}
410
411	resp->status = nfs_ok;
412	if (!inode) {
413		/* File doesn't exist. Create it and set attrs */
414		resp->status = nfsd_create_locked(rqstp, dirfhp, &attrs, type,
415						  rdev, newfhp);
416	} else if (type == S_IFREG) {
417		dprintk("nfsd:   existing %s, valid=%x, size=%ld\n",
418			argp->name, attr->ia_valid, (long) attr->ia_size);
419		/* File already exists. We ignore all attributes except
420		 * size, so that creat() behaves exactly like
421		 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
422		 */
423		attr->ia_valid &= ATTR_SIZE;
424		if (attr->ia_valid)
425			resp->status = nfsd_setattr(rqstp, newfhp, &attrs,
426						    NULL);
427	}
428
429out_unlock:
430	inode_unlock(dirfhp->fh_dentry->d_inode);
431	fh_drop_write(dirfhp);
432done:
433	fh_put(dirfhp);
434	if (resp->status != nfs_ok)
435		goto out;
436	resp->status = fh_getattr(&resp->fh, &resp->stat);
437out:
438	resp->status = nfsd_map_status(resp->status);
439	return rpc_success;
440}
441
442static __be32
443nfsd_proc_remove(struct svc_rqst *rqstp)
444{
445	struct nfsd_diropargs *argp = rqstp->rq_argp;
446	struct nfsd_stat *resp = rqstp->rq_resp;
447
448	dprintk("nfsd: REMOVE   %s %.*s\n", SVCFH_fmt(&argp->fh),
449		argp->len, argp->name);
450
451	/* Unlink. -SIFDIR means file must not be a directory */
452	resp->status = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR,
453				   argp->name, argp->len);
454	fh_put(&argp->fh);
455	resp->status = nfsd_map_status(resp->status);
456	return rpc_success;
457}
458
459static __be32
460nfsd_proc_rename(struct svc_rqst *rqstp)
461{
462	struct nfsd_renameargs *argp = rqstp->rq_argp;
463	struct nfsd_stat *resp = rqstp->rq_resp;
464
465	dprintk("nfsd: RENAME   %s %.*s -> \n",
466		SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
467	dprintk("nfsd:        ->  %s %.*s\n",
468		SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
469
470	resp->status = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
471				   &argp->tfh, argp->tname, argp->tlen);
472	fh_put(&argp->ffh);
473	fh_put(&argp->tfh);
474	resp->status = nfsd_map_status(resp->status);
475	return rpc_success;
476}
477
478static __be32
479nfsd_proc_link(struct svc_rqst *rqstp)
480{
481	struct nfsd_linkargs *argp = rqstp->rq_argp;
482	struct nfsd_stat *resp = rqstp->rq_resp;
483
484	dprintk("nfsd: LINK     %s ->\n",
485		SVCFH_fmt(&argp->ffh));
486	dprintk("nfsd:    %s %.*s\n",
487		SVCFH_fmt(&argp->tfh),
488		argp->tlen,
489		argp->tname);
490
491	resp->status = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
492				 &argp->ffh);
493	fh_put(&argp->ffh);
494	fh_put(&argp->tfh);
495	resp->status = nfsd_map_status(resp->status);
496	return rpc_success;
497}
498
499static __be32
500nfsd_proc_symlink(struct svc_rqst *rqstp)
501{
502	struct nfsd_symlinkargs *argp = rqstp->rq_argp;
503	struct nfsd_stat *resp = rqstp->rq_resp;
504	struct nfsd_attrs attrs = {
505		.na_iattr	= &argp->attrs,
506	};
507	struct svc_fh	newfh;
508
509	if (argp->tlen > NFS_MAXPATHLEN) {
510		resp->status = nfserr_nametoolong;
511		goto out;
512	}
513
514	argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
515						page_address(rqstp->rq_arg.pages[0]),
516						argp->tlen);
517	if (IS_ERR(argp->tname)) {
518		resp->status = nfserrno(PTR_ERR(argp->tname));
519		goto out;
520	}
521
522	dprintk("nfsd: SYMLINK  %s %.*s -> %.*s\n",
523		SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
524		argp->tlen, argp->tname);
525
526	fh_init(&newfh, NFS_FHSIZE);
527	resp->status = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
528				    argp->tname, &attrs, &newfh);
529
530	kfree(argp->tname);
531	fh_put(&argp->ffh);
532	fh_put(&newfh);
533out:
534	resp->status = nfsd_map_status(resp->status);
535	return rpc_success;
536}
537
538/*
539 * Make directory. This operation is not idempotent.
540 * N.B. After this call resp->fh needs an fh_put
541 */
542static __be32
543nfsd_proc_mkdir(struct svc_rqst *rqstp)
544{
545	struct nfsd_createargs *argp = rqstp->rq_argp;
546	struct nfsd_diropres *resp = rqstp->rq_resp;
547	struct nfsd_attrs attrs = {
548		.na_iattr	= &argp->attrs,
549	};
550
551	dprintk("nfsd: MKDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
552
553	if (resp->fh.fh_dentry) {
554		printk(KERN_WARNING
555			"nfsd_proc_mkdir: response already verified??\n");
556	}
557
558	argp->attrs.ia_valid &= ~ATTR_SIZE;
559	fh_init(&resp->fh, NFS_FHSIZE);
560	resp->status = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
561				   &attrs, S_IFDIR, 0, &resp->fh);
562	fh_put(&argp->fh);
563	if (resp->status != nfs_ok)
564		goto out;
565
566	resp->status = fh_getattr(&resp->fh, &resp->stat);
567out:
568	resp->status = nfsd_map_status(resp->status);
569	return rpc_success;
570}
571
572/*
573 * Remove a directory
574 */
575static __be32
576nfsd_proc_rmdir(struct svc_rqst *rqstp)
577{
578	struct nfsd_diropargs *argp = rqstp->rq_argp;
579	struct nfsd_stat *resp = rqstp->rq_resp;
580
581	dprintk("nfsd: RMDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
582
583	resp->status = nfsd_unlink(rqstp, &argp->fh, S_IFDIR,
584				   argp->name, argp->len);
585	fh_put(&argp->fh);
586	resp->status = nfsd_map_status(resp->status);
587	return rpc_success;
588}
589
590static void nfsd_init_dirlist_pages(struct svc_rqst *rqstp,
591				    struct nfsd_readdirres *resp,
592				    u32 count)
593{
594	struct xdr_buf *buf = &resp->dirlist;
595	struct xdr_stream *xdr = &resp->xdr;
596
597	memset(buf, 0, sizeof(*buf));
598
599	/* Reserve room for the NULL ptr & eof flag (-2 words) */
600	buf->buflen = clamp(count, (u32)(XDR_UNIT * 2), (u32)PAGE_SIZE);
601	buf->buflen -= XDR_UNIT * 2;
602	buf->pages = rqstp->rq_next_page;
603	rqstp->rq_next_page++;
604
605	xdr_init_encode_pages(xdr, buf, buf->pages,  NULL);
606}
607
608/*
609 * Read a portion of a directory.
610 */
611static __be32
612nfsd_proc_readdir(struct svc_rqst *rqstp)
613{
614	struct nfsd_readdirargs *argp = rqstp->rq_argp;
615	struct nfsd_readdirres *resp = rqstp->rq_resp;
616	loff_t		offset;
617
618	dprintk("nfsd: READDIR  %s %d bytes at %d\n",
619		SVCFH_fmt(&argp->fh),		
620		argp->count, argp->cookie);
621
622	nfsd_init_dirlist_pages(rqstp, resp, argp->count);
623
624	resp->common.err = nfs_ok;
625	resp->cookie_offset = 0;
626	offset = argp->cookie;
627	resp->status = nfsd_readdir(rqstp, &argp->fh, &offset,
628				    &resp->common, nfssvc_encode_entry);
629	nfssvc_encode_nfscookie(resp, offset);
630
631	fh_put(&argp->fh);
632	resp->status = nfsd_map_status(resp->status);
633	return rpc_success;
634}
635
636/*
637 * Get file system info
638 */
639static __be32
640nfsd_proc_statfs(struct svc_rqst *rqstp)
641{
642	struct nfsd_fhandle *argp = rqstp->rq_argp;
643	struct nfsd_statfsres *resp = rqstp->rq_resp;
644
645	dprintk("nfsd: STATFS   %s\n", SVCFH_fmt(&argp->fh));
646
647	resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
648				   NFSD_MAY_BYPASS_GSS_ON_ROOT);
649	fh_put(&argp->fh);
650	resp->status = nfsd_map_status(resp->status);
651	return rpc_success;
652}
653
654/*
655 * NFSv2 Server procedures.
656 * Only the results of non-idempotent operations are cached.
657 */
658
659#define ST 1		/* status */
660#define FH 8		/* filehandle */
661#define	AT 18		/* attributes */
662
663static const struct svc_procedure nfsd_procedures2[18] = {
664	[NFSPROC_NULL] = {
665		.pc_func = nfsd_proc_null,
666		.pc_decode = nfssvc_decode_voidarg,
667		.pc_encode = nfssvc_encode_voidres,
668		.pc_argsize = sizeof(struct nfsd_voidargs),
669		.pc_argzero = sizeof(struct nfsd_voidargs),
670		.pc_ressize = sizeof(struct nfsd_voidres),
671		.pc_cachetype = RC_NOCACHE,
672		.pc_xdrressize = 0,
673		.pc_name = "NULL",
674	},
675	[NFSPROC_GETATTR] = {
676		.pc_func = nfsd_proc_getattr,
677		.pc_decode = nfssvc_decode_fhandleargs,
678		.pc_encode = nfssvc_encode_attrstatres,
679		.pc_release = nfssvc_release_attrstat,
680		.pc_argsize = sizeof(struct nfsd_fhandle),
681		.pc_argzero = sizeof(struct nfsd_fhandle),
682		.pc_ressize = sizeof(struct nfsd_attrstat),
683		.pc_cachetype = RC_NOCACHE,
684		.pc_xdrressize = ST+AT,
685		.pc_name = "GETATTR",
686	},
687	[NFSPROC_SETATTR] = {
688		.pc_func = nfsd_proc_setattr,
689		.pc_decode = nfssvc_decode_sattrargs,
690		.pc_encode = nfssvc_encode_attrstatres,
691		.pc_release = nfssvc_release_attrstat,
692		.pc_argsize = sizeof(struct nfsd_sattrargs),
693		.pc_argzero = sizeof(struct nfsd_sattrargs),
694		.pc_ressize = sizeof(struct nfsd_attrstat),
695		.pc_cachetype = RC_REPLBUFF,
696		.pc_xdrressize = ST+AT,
697		.pc_name = "SETATTR",
698	},
699	[NFSPROC_ROOT] = {
700		.pc_func = nfsd_proc_root,
701		.pc_decode = nfssvc_decode_voidarg,
702		.pc_encode = nfssvc_encode_voidres,
703		.pc_argsize = sizeof(struct nfsd_voidargs),
704		.pc_argzero = sizeof(struct nfsd_voidargs),
705		.pc_ressize = sizeof(struct nfsd_voidres),
706		.pc_cachetype = RC_NOCACHE,
707		.pc_xdrressize = 0,
708		.pc_name = "ROOT",
709	},
710	[NFSPROC_LOOKUP] = {
711		.pc_func = nfsd_proc_lookup,
712		.pc_decode = nfssvc_decode_diropargs,
713		.pc_encode = nfssvc_encode_diropres,
714		.pc_release = nfssvc_release_diropres,
715		.pc_argsize = sizeof(struct nfsd_diropargs),
716		.pc_argzero = sizeof(struct nfsd_diropargs),
717		.pc_ressize = sizeof(struct nfsd_diropres),
718		.pc_cachetype = RC_NOCACHE,
719		.pc_xdrressize = ST+FH+AT,
720		.pc_name = "LOOKUP",
721	},
722	[NFSPROC_READLINK] = {
723		.pc_func = nfsd_proc_readlink,
724		.pc_decode = nfssvc_decode_fhandleargs,
725		.pc_encode = nfssvc_encode_readlinkres,
726		.pc_argsize = sizeof(struct nfsd_fhandle),
727		.pc_argzero = sizeof(struct nfsd_fhandle),
728		.pc_ressize = sizeof(struct nfsd_readlinkres),
729		.pc_cachetype = RC_NOCACHE,
730		.pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
731		.pc_name = "READLINK",
732	},
733	[NFSPROC_READ] = {
734		.pc_func = nfsd_proc_read,
735		.pc_decode = nfssvc_decode_readargs,
736		.pc_encode = nfssvc_encode_readres,
737		.pc_release = nfssvc_release_readres,
738		.pc_argsize = sizeof(struct nfsd_readargs),
739		.pc_argzero = sizeof(struct nfsd_readargs),
740		.pc_ressize = sizeof(struct nfsd_readres),
741		.pc_cachetype = RC_NOCACHE,
742		.pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
743		.pc_name = "READ",
744	},
745	[NFSPROC_WRITECACHE] = {
746		.pc_func = nfsd_proc_writecache,
747		.pc_decode = nfssvc_decode_voidarg,
748		.pc_encode = nfssvc_encode_voidres,
749		.pc_argsize = sizeof(struct nfsd_voidargs),
750		.pc_argzero = sizeof(struct nfsd_voidargs),
751		.pc_ressize = sizeof(struct nfsd_voidres),
752		.pc_cachetype = RC_NOCACHE,
753		.pc_xdrressize = 0,
754		.pc_name = "WRITECACHE",
755	},
756	[NFSPROC_WRITE] = {
757		.pc_func = nfsd_proc_write,
758		.pc_decode = nfssvc_decode_writeargs,
759		.pc_encode = nfssvc_encode_attrstatres,
760		.pc_release = nfssvc_release_attrstat,
761		.pc_argsize = sizeof(struct nfsd_writeargs),
762		.pc_argzero = sizeof(struct nfsd_writeargs),
763		.pc_ressize = sizeof(struct nfsd_attrstat),
764		.pc_cachetype = RC_REPLBUFF,
765		.pc_xdrressize = ST+AT,
766		.pc_name = "WRITE",
767	},
768	[NFSPROC_CREATE] = {
769		.pc_func = nfsd_proc_create,
770		.pc_decode = nfssvc_decode_createargs,
771		.pc_encode = nfssvc_encode_diropres,
772		.pc_release = nfssvc_release_diropres,
773		.pc_argsize = sizeof(struct nfsd_createargs),
774		.pc_argzero = sizeof(struct nfsd_createargs),
775		.pc_ressize = sizeof(struct nfsd_diropres),
776		.pc_cachetype = RC_REPLBUFF,
777		.pc_xdrressize = ST+FH+AT,
778		.pc_name = "CREATE",
779	},
780	[NFSPROC_REMOVE] = {
781		.pc_func = nfsd_proc_remove,
782		.pc_decode = nfssvc_decode_diropargs,
783		.pc_encode = nfssvc_encode_statres,
784		.pc_argsize = sizeof(struct nfsd_diropargs),
785		.pc_argzero = sizeof(struct nfsd_diropargs),
786		.pc_ressize = sizeof(struct nfsd_stat),
787		.pc_cachetype = RC_REPLSTAT,
788		.pc_xdrressize = ST,
789		.pc_name = "REMOVE",
790	},
791	[NFSPROC_RENAME] = {
792		.pc_func = nfsd_proc_rename,
793		.pc_decode = nfssvc_decode_renameargs,
794		.pc_encode = nfssvc_encode_statres,
795		.pc_argsize = sizeof(struct nfsd_renameargs),
796		.pc_argzero = sizeof(struct nfsd_renameargs),
797		.pc_ressize = sizeof(struct nfsd_stat),
798		.pc_cachetype = RC_REPLSTAT,
799		.pc_xdrressize = ST,
800		.pc_name = "RENAME",
801	},
802	[NFSPROC_LINK] = {
803		.pc_func = nfsd_proc_link,
804		.pc_decode = nfssvc_decode_linkargs,
805		.pc_encode = nfssvc_encode_statres,
806		.pc_argsize = sizeof(struct nfsd_linkargs),
807		.pc_argzero = sizeof(struct nfsd_linkargs),
808		.pc_ressize = sizeof(struct nfsd_stat),
809		.pc_cachetype = RC_REPLSTAT,
810		.pc_xdrressize = ST,
811		.pc_name = "LINK",
812	},
813	[NFSPROC_SYMLINK] = {
814		.pc_func = nfsd_proc_symlink,
815		.pc_decode = nfssvc_decode_symlinkargs,
816		.pc_encode = nfssvc_encode_statres,
817		.pc_argsize = sizeof(struct nfsd_symlinkargs),
818		.pc_argzero = sizeof(struct nfsd_symlinkargs),
819		.pc_ressize = sizeof(struct nfsd_stat),
820		.pc_cachetype = RC_REPLSTAT,
821		.pc_xdrressize = ST,
822		.pc_name = "SYMLINK",
823	},
824	[NFSPROC_MKDIR] = {
825		.pc_func = nfsd_proc_mkdir,
826		.pc_decode = nfssvc_decode_createargs,
827		.pc_encode = nfssvc_encode_diropres,
828		.pc_release = nfssvc_release_diropres,
829		.pc_argsize = sizeof(struct nfsd_createargs),
830		.pc_argzero = sizeof(struct nfsd_createargs),
831		.pc_ressize = sizeof(struct nfsd_diropres),
832		.pc_cachetype = RC_REPLBUFF,
833		.pc_xdrressize = ST+FH+AT,
834		.pc_name = "MKDIR",
835	},
836	[NFSPROC_RMDIR] = {
837		.pc_func = nfsd_proc_rmdir,
838		.pc_decode = nfssvc_decode_diropargs,
839		.pc_encode = nfssvc_encode_statres,
840		.pc_argsize = sizeof(struct nfsd_diropargs),
841		.pc_argzero = sizeof(struct nfsd_diropargs),
842		.pc_ressize = sizeof(struct nfsd_stat),
843		.pc_cachetype = RC_REPLSTAT,
844		.pc_xdrressize = ST,
845		.pc_name = "RMDIR",
846	},
847	[NFSPROC_READDIR] = {
848		.pc_func = nfsd_proc_readdir,
849		.pc_decode = nfssvc_decode_readdirargs,
850		.pc_encode = nfssvc_encode_readdirres,
851		.pc_argsize = sizeof(struct nfsd_readdirargs),
852		.pc_argzero = sizeof(struct nfsd_readdirargs),
853		.pc_ressize = sizeof(struct nfsd_readdirres),
854		.pc_cachetype = RC_NOCACHE,
855		.pc_name = "READDIR",
856	},
857	[NFSPROC_STATFS] = {
858		.pc_func = nfsd_proc_statfs,
859		.pc_decode = nfssvc_decode_fhandleargs,
860		.pc_encode = nfssvc_encode_statfsres,
861		.pc_argsize = sizeof(struct nfsd_fhandle),
862		.pc_argzero = sizeof(struct nfsd_fhandle),
863		.pc_ressize = sizeof(struct nfsd_statfsres),
864		.pc_cachetype = RC_NOCACHE,
865		.pc_xdrressize = ST+5,
866		.pc_name = "STATFS",
867	},
868};
869
870static DEFINE_PER_CPU_ALIGNED(unsigned long,
871			      nfsd_count2[ARRAY_SIZE(nfsd_procedures2)]);
872const struct svc_version nfsd_version2 = {
873	.vs_vers	= 2,
874	.vs_nproc	= ARRAY_SIZE(nfsd_procedures2),
875	.vs_proc	= nfsd_procedures2,
876	.vs_count	= nfsd_count2,
877	.vs_dispatch	= nfsd_dispatch,
878	.vs_xdrsize	= NFS2_SVC_XDRSIZE,
879};