xref: /linux/fs/nfsd/nfs4proc.c (revision d67b569f5f620c0fb95d5212642746b7ba9d29e4)
1 /*
2  *  fs/nfsd/nfs4proc.c
3  *
4  *  Server-side procedures for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * Note: some routines in this file are just trivial wrappers
38  * (e.g. nfsd4_lookup()) defined solely for the sake of consistent
39  * naming.  Since all such routines have been declared "inline",
40  * there shouldn't be any associated overhead.  At some point in
41  * the future, I might inline these "by hand" to clean up a
42  * little.
43  */
44 
45 #include <linux/param.h>
46 #include <linux/major.h>
47 #include <linux/slab.h>
48 #include <linux/file.h>
49 
50 #include <linux/sunrpc/svc.h>
51 #include <linux/nfsd/nfsd.h>
52 #include <linux/nfsd/cache.h>
53 #include <linux/nfs4.h>
54 #include <linux/nfsd/state.h>
55 #include <linux/nfsd/xdr4.h>
56 #include <linux/nfs4_acl.h>
57 
58 #define NFSDDBG_FACILITY		NFSDDBG_PROC
59 
60 static inline void
61 fh_dup2(struct svc_fh *dst, struct svc_fh *src)
62 {
63 	fh_put(dst);
64 	dget(src->fh_dentry);
65 	if (src->fh_export)
66 		cache_get(&src->fh_export->h);
67 	*dst = *src;
68 }
69 
70 static int
71 do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
72 {
73 	int accmode, status;
74 
75 	if (open->op_truncate &&
76 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
77 		return nfserr_inval;
78 
79 	accmode = MAY_NOP;
80 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
81 		accmode = MAY_READ;
82 	if (open->op_share_deny & NFS4_SHARE_ACCESS_WRITE)
83 		accmode |= (MAY_WRITE | MAY_TRUNC);
84 	accmode |= MAY_OWNER_OVERRIDE;
85 
86 	status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
87 
88 	return status;
89 }
90 
91 static int
92 do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
93 {
94 	struct svc_fh resfh;
95 	int status;
96 
97 	fh_init(&resfh, NFS4_FHSIZE);
98 	open->op_truncate = 0;
99 
100 	if (open->op_create) {
101 		/*
102 		 * Note: create modes (UNCHECKED,GUARDED...) are the same
103 		 * in NFSv4 as in v3.
104 		 */
105 		status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data,
106 					open->op_fname.len, &open->op_iattr,
107 					&resfh, open->op_createmode,
108 					(u32 *)open->op_verf.data, &open->op_truncate);
109 	}
110 	else {
111 		status = nfsd_lookup(rqstp, current_fh,
112 				     open->op_fname.data, open->op_fname.len, &resfh);
113 		fh_unlock(current_fh);
114 	}
115 
116 	if (!status) {
117 		set_change_info(&open->op_cinfo, current_fh);
118 
119 		/* set reply cache */
120 		fh_dup2(current_fh, &resfh);
121 		open->op_stateowner->so_replay.rp_openfh_len =
122 			resfh.fh_handle.fh_size;
123 		memcpy(open->op_stateowner->so_replay.rp_openfh,
124 				&resfh.fh_handle.fh_base,
125 				resfh.fh_handle.fh_size);
126 
127 		status = do_open_permission(rqstp, current_fh, open);
128 	}
129 
130 	fh_put(&resfh);
131 	return status;
132 }
133 
134 static int
135 do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
136 {
137 	int status;
138 
139 	/* Only reclaims from previously confirmed clients are valid */
140 	if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
141 		return status;
142 
143 	/* We don't know the target directory, and therefore can not
144 	* set the change info
145 	*/
146 
147 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
148 
149 	/* set replay cache */
150 	open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size;
151 	memcpy(open->op_stateowner->so_replay.rp_openfh,
152 		&current_fh->fh_handle.fh_base,
153 		current_fh->fh_handle.fh_size);
154 
155 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
156 		(open->op_iattr.ia_size == 0);
157 
158 	status = do_open_permission(rqstp, current_fh, open);
159 
160 	return status;
161 }
162 
163 
164 static inline int
165 nfsd4_open(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
166 {
167 	int status;
168 	dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
169 		(int)open->op_fname.len, open->op_fname.data,
170 		open->op_stateowner);
171 
172 	if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
173 		return nfserr_grace;
174 
175 	if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
176 		return nfserr_no_grace;
177 
178 	/* This check required by spec. */
179 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
180 		return nfserr_inval;
181 
182 	nfs4_lock_state();
183 
184 	/* check seqid for replay. set nfs4_owner */
185 	status = nfsd4_process_open1(open);
186 	if (status == NFSERR_REPLAY_ME) {
187 		struct nfs4_replay *rp = &open->op_stateowner->so_replay;
188 		fh_put(current_fh);
189 		current_fh->fh_handle.fh_size = rp->rp_openfh_len;
190 		memcpy(&current_fh->fh_handle.fh_base, rp->rp_openfh,
191 				rp->rp_openfh_len);
192 		status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
193 		if (status)
194 			dprintk("nfsd4_open: replay failed"
195 				" restoring previous filehandle\n");
196 		else
197 			status = NFSERR_REPLAY_ME;
198 	}
199 	if (status)
200 		goto out;
201 	switch (open->op_claim_type) {
202 		case NFS4_OPEN_CLAIM_DELEGATE_CUR:
203 			status = nfserr_inval;
204 			if (open->op_create)
205 				goto out;
206 			/* fall through */
207 		case NFS4_OPEN_CLAIM_NULL:
208 			/*
209 			 * (1) set CURRENT_FH to the file being opened,
210 			 * creating it if necessary, (2) set open->op_cinfo,
211 			 * (3) set open->op_truncate if the file is to be
212 			 * truncated after opening, (4) do permission checking.
213 			 */
214 			status = do_open_lookup(rqstp, current_fh, open);
215 			if (status)
216 				goto out;
217 			break;
218 		case NFS4_OPEN_CLAIM_PREVIOUS:
219 			/*
220 			 * The CURRENT_FH is already set to the file being
221 			 * opened.  (1) set open->op_cinfo, (2) set
222 			 * open->op_truncate if the file is to be truncated
223 			 * after opening, (3) do permission checking.
224 			*/
225 			status = do_open_fhandle(rqstp, current_fh, open);
226 			if (status)
227 				goto out;
228 			break;
229              	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
230 			printk("NFSD: unsupported OPEN claim type %d\n",
231 				open->op_claim_type);
232 			status = nfserr_notsupp;
233 			goto out;
234 		default:
235 			printk("NFSD: Invalid OPEN claim type %d\n",
236 				open->op_claim_type);
237 			status = nfserr_inval;
238 			goto out;
239 	}
240 	/*
241 	 * nfsd4_process_open2() does the actual opening of the file.  If
242 	 * successful, it (1) truncates the file if open->op_truncate was
243 	 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
244 	 */
245 	status = nfsd4_process_open2(rqstp, current_fh, open);
246 out:
247 	if (open->op_stateowner)
248 		nfs4_get_stateowner(open->op_stateowner);
249 	nfs4_unlock_state();
250 	return status;
251 }
252 
253 /*
254  * filehandle-manipulating ops.
255  */
256 static inline int
257 nfsd4_getfh(struct svc_fh *current_fh, struct svc_fh **getfh)
258 {
259 	if (!current_fh->fh_dentry)
260 		return nfserr_nofilehandle;
261 
262 	*getfh = current_fh;
263 	return nfs_ok;
264 }
265 
266 static inline int
267 nfsd4_putfh(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_putfh *putfh)
268 {
269 	fh_put(current_fh);
270 	current_fh->fh_handle.fh_size = putfh->pf_fhlen;
271 	memcpy(&current_fh->fh_handle.fh_base, putfh->pf_fhval, putfh->pf_fhlen);
272 	return fh_verify(rqstp, current_fh, 0, MAY_NOP);
273 }
274 
275 static inline int
276 nfsd4_putrootfh(struct svc_rqst *rqstp, struct svc_fh *current_fh)
277 {
278 	int status;
279 
280 	fh_put(current_fh);
281 	status = exp_pseudoroot(rqstp->rq_client, current_fh,
282 			      &rqstp->rq_chandle);
283 	if (!status)
284 		status = nfserrno(nfsd_setuser(rqstp, current_fh->fh_export));
285 	return status;
286 }
287 
288 static inline int
289 nfsd4_restorefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
290 {
291 	if (!save_fh->fh_dentry)
292 		return nfserr_restorefh;
293 
294 	fh_dup2(current_fh, save_fh);
295 	return nfs_ok;
296 }
297 
298 static inline int
299 nfsd4_savefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
300 {
301 	if (!current_fh->fh_dentry)
302 		return nfserr_nofilehandle;
303 
304 	fh_dup2(save_fh, current_fh);
305 	return nfs_ok;
306 }
307 
308 /*
309  * misc nfsv4 ops
310  */
311 static inline int
312 nfsd4_access(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_access *access)
313 {
314 	if (access->ac_req_access & ~NFS3_ACCESS_FULL)
315 		return nfserr_inval;
316 
317 	access->ac_resp_access = access->ac_req_access;
318 	return nfsd_access(rqstp, current_fh, &access->ac_resp_access, &access->ac_supported);
319 }
320 
321 static inline int
322 nfsd4_commit(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_commit *commit)
323 {
324 	int status;
325 
326 	u32 *p = (u32 *)commit->co_verf.data;
327 	*p++ = nfssvc_boot.tv_sec;
328 	*p++ = nfssvc_boot.tv_usec;
329 
330 	status = nfsd_commit(rqstp, current_fh, commit->co_offset, commit->co_count);
331 	if (status == nfserr_symlink)
332 		status = nfserr_inval;
333 	return status;
334 }
335 
336 static int
337 nfsd4_create(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_create *create)
338 {
339 	struct svc_fh resfh;
340 	int status;
341 	dev_t rdev;
342 
343 	fh_init(&resfh, NFS4_FHSIZE);
344 
345 	status = fh_verify(rqstp, current_fh, S_IFDIR, MAY_CREATE);
346 	if (status == nfserr_symlink)
347 		status = nfserr_notdir;
348 	if (status)
349 		return status;
350 
351 	switch (create->cr_type) {
352 	case NF4LNK:
353 		/* ugh! we have to null-terminate the linktext, or
354 		 * vfs_symlink() will choke.  it is always safe to
355 		 * null-terminate by brute force, since at worst we
356 		 * will overwrite the first byte of the create namelen
357 		 * in the XDR buffer, which has already been extracted
358 		 * during XDR decode.
359 		 */
360 		create->cr_linkname[create->cr_linklen] = 0;
361 
362 		status = nfsd_symlink(rqstp, current_fh, create->cr_name,
363 				      create->cr_namelen, create->cr_linkname,
364 				      create->cr_linklen, &resfh, &create->cr_iattr);
365 		break;
366 
367 	case NF4BLK:
368 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
369 		if (MAJOR(rdev) != create->cr_specdata1 ||
370 		    MINOR(rdev) != create->cr_specdata2)
371 			return nfserr_inval;
372 		status = nfsd_create(rqstp, current_fh, create->cr_name,
373 				     create->cr_namelen, &create->cr_iattr,
374 				     S_IFBLK, rdev, &resfh);
375 		break;
376 
377 	case NF4CHR:
378 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
379 		if (MAJOR(rdev) != create->cr_specdata1 ||
380 		    MINOR(rdev) != create->cr_specdata2)
381 			return nfserr_inval;
382 		status = nfsd_create(rqstp, current_fh, create->cr_name,
383 				     create->cr_namelen, &create->cr_iattr,
384 				     S_IFCHR, rdev, &resfh);
385 		break;
386 
387 	case NF4SOCK:
388 		status = nfsd_create(rqstp, current_fh, create->cr_name,
389 				     create->cr_namelen, &create->cr_iattr,
390 				     S_IFSOCK, 0, &resfh);
391 		break;
392 
393 	case NF4FIFO:
394 		status = nfsd_create(rqstp, current_fh, create->cr_name,
395 				     create->cr_namelen, &create->cr_iattr,
396 				     S_IFIFO, 0, &resfh);
397 		break;
398 
399 	case NF4DIR:
400 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
401 		status = nfsd_create(rqstp, current_fh, create->cr_name,
402 				     create->cr_namelen, &create->cr_iattr,
403 				     S_IFDIR, 0, &resfh);
404 		break;
405 
406 	default:
407 		status = nfserr_badtype;
408 	}
409 
410 	if (!status) {
411 		fh_unlock(current_fh);
412 		set_change_info(&create->cr_cinfo, current_fh);
413 		fh_dup2(current_fh, &resfh);
414 	}
415 
416 	fh_put(&resfh);
417 	return status;
418 }
419 
420 static inline int
421 nfsd4_getattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_getattr *getattr)
422 {
423 	int status;
424 
425 	status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
426 	if (status)
427 		return status;
428 
429 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
430 		return nfserr_inval;
431 
432 	getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
433 	getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
434 
435 	getattr->ga_fhp = current_fh;
436 	return nfs_ok;
437 }
438 
439 static inline int
440 nfsd4_link(struct svc_rqst *rqstp, struct svc_fh *current_fh,
441 	   struct svc_fh *save_fh, struct nfsd4_link *link)
442 {
443 	int status = nfserr_nofilehandle;
444 
445 	if (!save_fh->fh_dentry)
446 		return status;
447 	status = nfsd_link(rqstp, current_fh, link->li_name, link->li_namelen, save_fh);
448 	if (!status)
449 		set_change_info(&link->li_cinfo, current_fh);
450 	return status;
451 }
452 
453 static int
454 nfsd4_lookupp(struct svc_rqst *rqstp, struct svc_fh *current_fh)
455 {
456 	struct svc_fh tmp_fh;
457 	int ret;
458 
459 	fh_init(&tmp_fh, NFS4_FHSIZE);
460 	if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh,
461 			      &rqstp->rq_chandle)) != 0)
462 		return ret;
463 	if (tmp_fh.fh_dentry == current_fh->fh_dentry) {
464 		fh_put(&tmp_fh);
465 		return nfserr_noent;
466 	}
467 	fh_put(&tmp_fh);
468 	return nfsd_lookup(rqstp, current_fh, "..", 2, current_fh);
469 }
470 
471 static inline int
472 nfsd4_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lookup *lookup)
473 {
474 	return nfsd_lookup(rqstp, current_fh, lookup->lo_name, lookup->lo_len, current_fh);
475 }
476 
477 static inline int
478 nfsd4_read(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_read *read)
479 {
480 	int status;
481 
482 	/* no need to check permission - this will be done in nfsd_read() */
483 
484 	read->rd_filp = NULL;
485 	if (read->rd_offset >= OFFSET_MAX)
486 		return nfserr_inval;
487 
488 	nfs4_lock_state();
489 	/* check stateid */
490 	if ((status = nfs4_preprocess_stateid_op(current_fh, &read->rd_stateid,
491 				CHECK_FH | RD_STATE, &read->rd_filp))) {
492 		dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
493 		goto out;
494 	}
495 	if (read->rd_filp)
496 		get_file(read->rd_filp);
497 	status = nfs_ok;
498 out:
499 	nfs4_unlock_state();
500 	read->rd_rqstp = rqstp;
501 	read->rd_fhp = current_fh;
502 	return status;
503 }
504 
505 static inline int
506 nfsd4_readdir(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readdir *readdir)
507 {
508 	u64 cookie = readdir->rd_cookie;
509 	static const nfs4_verifier zeroverf;
510 
511 	/* no need to check permission - this will be done in nfsd_readdir() */
512 
513 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
514 		return nfserr_inval;
515 
516 	readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
517 	readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
518 
519 	if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
520 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
521 		return nfserr_bad_cookie;
522 
523 	readdir->rd_rqstp = rqstp;
524 	readdir->rd_fhp = current_fh;
525 	return nfs_ok;
526 }
527 
528 static inline int
529 nfsd4_readlink(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readlink *readlink)
530 {
531 	readlink->rl_rqstp = rqstp;
532 	readlink->rl_fhp = current_fh;
533 	return nfs_ok;
534 }
535 
536 static inline int
537 nfsd4_remove(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_remove *remove)
538 {
539 	int status;
540 
541 	if (nfs4_in_grace())
542 		return nfserr_grace;
543 	status = nfsd_unlink(rqstp, current_fh, 0, remove->rm_name, remove->rm_namelen);
544 	if (status == nfserr_symlink)
545 		return nfserr_notdir;
546 	if (!status) {
547 		fh_unlock(current_fh);
548 		set_change_info(&remove->rm_cinfo, current_fh);
549 	}
550 	return status;
551 }
552 
553 static inline int
554 nfsd4_rename(struct svc_rqst *rqstp, struct svc_fh *current_fh,
555 	     struct svc_fh *save_fh, struct nfsd4_rename *rename)
556 {
557 	int status = nfserr_nofilehandle;
558 
559 	if (!save_fh->fh_dentry)
560 		return status;
561 	if (nfs4_in_grace() && !(save_fh->fh_export->ex_flags
562 					& NFSEXP_NOSUBTREECHECK))
563 		return nfserr_grace;
564 	status = nfsd_rename(rqstp, save_fh, rename->rn_sname,
565 			     rename->rn_snamelen, current_fh,
566 			     rename->rn_tname, rename->rn_tnamelen);
567 
568 	/* the underlying filesystem returns different error's than required
569 	 * by NFSv4. both save_fh and current_fh have been verified.. */
570 	if (status == nfserr_isdir)
571 		status = nfserr_exist;
572 	else if ((status == nfserr_notdir) &&
573                   (S_ISDIR(save_fh->fh_dentry->d_inode->i_mode) &&
574                    S_ISDIR(current_fh->fh_dentry->d_inode->i_mode)))
575 		status = nfserr_exist;
576 	else if (status == nfserr_symlink)
577 		status = nfserr_notdir;
578 
579 	if (!status) {
580 		set_change_info(&rename->rn_sinfo, current_fh);
581 		set_change_info(&rename->rn_tinfo, save_fh);
582 	}
583 	return status;
584 }
585 
586 static inline int
587 nfsd4_setattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_setattr *setattr)
588 {
589 	int status = nfs_ok;
590 
591 	if (!current_fh->fh_dentry)
592 		return nfserr_nofilehandle;
593 
594 	status = nfs_ok;
595 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
596 		nfs4_lock_state();
597 		if ((status = nfs4_preprocess_stateid_op(current_fh,
598 						&setattr->sa_stateid,
599 						CHECK_FH | WR_STATE, NULL))) {
600 			dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
601 			goto out_unlock;
602 		}
603 		nfs4_unlock_state();
604 	}
605 	status = nfs_ok;
606 	if (setattr->sa_acl != NULL)
607 		status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl);
608 	if (status)
609 		goto out;
610 	status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr,
611 				0, (time_t)0);
612 out:
613 	return status;
614 out_unlock:
615 	nfs4_unlock_state();
616 	return status;
617 }
618 
619 static inline int
620 nfsd4_write(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_write *write)
621 {
622 	stateid_t *stateid = &write->wr_stateid;
623 	struct file *filp = NULL;
624 	u32 *p;
625 	int status = nfs_ok;
626 
627 	/* no need to check permission - this will be done in nfsd_write() */
628 
629 	if (write->wr_offset >= OFFSET_MAX)
630 		return nfserr_inval;
631 
632 	nfs4_lock_state();
633 	if ((status = nfs4_preprocess_stateid_op(current_fh, stateid,
634 					CHECK_FH | WR_STATE, &filp))) {
635 		dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
636 		goto out;
637 	}
638 	if (filp)
639 		get_file(filp);
640 	nfs4_unlock_state();
641 
642 	write->wr_bytes_written = write->wr_buflen;
643 	write->wr_how_written = write->wr_stable_how;
644 	p = (u32 *)write->wr_verifier.data;
645 	*p++ = nfssvc_boot.tv_sec;
646 	*p++ = nfssvc_boot.tv_usec;
647 
648 	status =  nfsd_write(rqstp, current_fh, filp, write->wr_offset,
649 			write->wr_vec, write->wr_vlen, write->wr_buflen,
650 			&write->wr_how_written);
651 	if (filp)
652 		fput(filp);
653 
654 	if (status == nfserr_symlink)
655 		status = nfserr_inval;
656 	return status;
657 out:
658 	nfs4_unlock_state();
659 	return status;
660 }
661 
662 /* This routine never returns NFS_OK!  If there are no other errors, it
663  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
664  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
665  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
666  */
667 static int
668 nfsd4_verify(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_verify *verify)
669 {
670 	u32 *buf, *p;
671 	int count;
672 	int status;
673 
674 	status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
675 	if (status)
676 		return status;
677 
678 	if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
679 	    || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
680 		return nfserr_attrnotsupp;
681 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
682 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
683 		return nfserr_inval;
684 	if (verify->ve_attrlen & 3)
685 		return nfserr_inval;
686 
687 	/* count in words:
688 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
689 	 */
690 	count = 4 + (verify->ve_attrlen >> 2);
691 	buf = kmalloc(count << 2, GFP_KERNEL);
692 	if (!buf)
693 		return nfserr_resource;
694 
695 	status = nfsd4_encode_fattr(current_fh, current_fh->fh_export,
696 				    current_fh->fh_dentry, buf,
697 				    &count, verify->ve_bmval,
698 				    rqstp);
699 
700 	/* this means that nfsd4_encode_fattr() ran out of space */
701 	if (status == nfserr_resource && count == 0)
702 		status = nfserr_not_same;
703 	if (status)
704 		goto out_kfree;
705 
706 	p = buf + 3;
707 	status = nfserr_not_same;
708 	if (ntohl(*p++) != verify->ve_attrlen)
709 		goto out_kfree;
710 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
711 		status = nfserr_same;
712 
713 out_kfree:
714 	kfree(buf);
715 	return status;
716 }
717 
718 /*
719  * NULL call.
720  */
721 static int
722 nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
723 {
724 	return nfs_ok;
725 }
726 
727 
728 /*
729  * COMPOUND call.
730  */
731 static int
732 nfsd4_proc_compound(struct svc_rqst *rqstp,
733 		    struct nfsd4_compoundargs *args,
734 		    struct nfsd4_compoundres *resp)
735 {
736 	struct nfsd4_op	*op;
737 	struct svc_fh	*current_fh = NULL;
738 	struct svc_fh	*save_fh = NULL;
739 	struct nfs4_stateowner *replay_owner = NULL;
740 	int		slack_space;    /* in words, not bytes! */
741 	int		status;
742 
743 	status = nfserr_resource;
744 	current_fh = kmalloc(sizeof(*current_fh), GFP_KERNEL);
745 	if (current_fh == NULL)
746 		goto out;
747 	fh_init(current_fh, NFS4_FHSIZE);
748 	save_fh = kmalloc(sizeof(*save_fh), GFP_KERNEL);
749 	if (save_fh == NULL)
750 		goto out;
751 	fh_init(save_fh, NFS4_FHSIZE);
752 
753 	resp->xbuf = &rqstp->rq_res;
754 	resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
755 	resp->tagp = resp->p;
756 	/* reserve space for: taglen, tag, and opcnt */
757 	resp->p += 2 + XDR_QUADLEN(args->taglen);
758 	resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
759 	resp->taglen = args->taglen;
760 	resp->tag = args->tag;
761 	resp->opcnt = 0;
762 	resp->rqstp = rqstp;
763 
764 	/*
765 	 * According to RFC3010, this takes precedence over all other errors.
766 	 */
767 	status = nfserr_minor_vers_mismatch;
768 	if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
769 		goto out;
770 
771 	status = nfs_ok;
772 	while (!status && resp->opcnt < args->opcnt) {
773 		op = &args->ops[resp->opcnt++];
774 
775 		/*
776 		 * The XDR decode routines may have pre-set op->status;
777 		 * for example, if there is a miscellaneous XDR error
778 		 * it will be set to nfserr_bad_xdr.
779 		 */
780 		if (op->status)
781 			goto encode_op;
782 
783 		/* We must be able to encode a successful response to
784 		 * this operation, with enough room left over to encode a
785 		 * failed response to the next operation.  If we don't
786 		 * have enough room, fail with ERR_RESOURCE.
787 		 */
788 /* FIXME - is slack_space *really* words, or bytes??? - neilb */
789 		slack_space = (char *)resp->end - (char *)resp->p;
790 		if (slack_space < COMPOUND_SLACK_SPACE + COMPOUND_ERR_SLACK_SPACE) {
791 			BUG_ON(slack_space < COMPOUND_ERR_SLACK_SPACE);
792 			op->status = nfserr_resource;
793 			goto encode_op;
794 		}
795 
796 		/* All operations except RENEW, SETCLIENTID, RESTOREFH
797 		* SETCLIENTID_CONFIRM, PUTFH and PUTROOTFH
798 		* require a valid current filehandle
799 		*
800 		* SETATTR NOFILEHANDLE error handled in nfsd4_setattr
801 		* due to required returned bitmap argument
802 		*/
803 		if ((!current_fh->fh_dentry) &&
804 		   !((op->opnum == OP_PUTFH) || (op->opnum == OP_PUTROOTFH) ||
805 		   (op->opnum == OP_SETCLIENTID) ||
806 		   (op->opnum == OP_SETCLIENTID_CONFIRM) ||
807 		   (op->opnum == OP_RENEW) || (op->opnum == OP_RESTOREFH) ||
808 		   (op->opnum == OP_RELEASE_LOCKOWNER) ||
809 		   (op->opnum == OP_SETATTR))) {
810 			op->status = nfserr_nofilehandle;
811 			goto encode_op;
812 		}
813 		switch (op->opnum) {
814 		case OP_ACCESS:
815 			op->status = nfsd4_access(rqstp, current_fh, &op->u.access);
816 			break;
817 		case OP_CLOSE:
818 			op->status = nfsd4_close(rqstp, current_fh, &op->u.close);
819 			replay_owner = op->u.close.cl_stateowner;
820 			break;
821 		case OP_COMMIT:
822 			op->status = nfsd4_commit(rqstp, current_fh, &op->u.commit);
823 			break;
824 		case OP_CREATE:
825 			op->status = nfsd4_create(rqstp, current_fh, &op->u.create);
826 			break;
827 		case OP_DELEGRETURN:
828 			op->status = nfsd4_delegreturn(rqstp, current_fh, &op->u.delegreturn);
829 			break;
830 		case OP_GETATTR:
831 			op->status = nfsd4_getattr(rqstp, current_fh, &op->u.getattr);
832 			break;
833 		case OP_GETFH:
834 			op->status = nfsd4_getfh(current_fh, &op->u.getfh);
835 			break;
836 		case OP_LINK:
837 			op->status = nfsd4_link(rqstp, current_fh, save_fh, &op->u.link);
838 			break;
839 		case OP_LOCK:
840 			op->status = nfsd4_lock(rqstp, current_fh, &op->u.lock);
841 			replay_owner = op->u.lock.lk_stateowner;
842 			break;
843 		case OP_LOCKT:
844 			op->status = nfsd4_lockt(rqstp, current_fh, &op->u.lockt);
845 			break;
846 		case OP_LOCKU:
847 			op->status = nfsd4_locku(rqstp, current_fh, &op->u.locku);
848 			replay_owner = op->u.locku.lu_stateowner;
849 			break;
850 		case OP_LOOKUP:
851 			op->status = nfsd4_lookup(rqstp, current_fh, &op->u.lookup);
852 			break;
853 		case OP_LOOKUPP:
854 			op->status = nfsd4_lookupp(rqstp, current_fh);
855 			break;
856 		case OP_NVERIFY:
857 			op->status = nfsd4_verify(rqstp, current_fh, &op->u.nverify);
858 			if (op->status == nfserr_not_same)
859 				op->status = nfs_ok;
860 			break;
861 		case OP_OPEN:
862 			op->status = nfsd4_open(rqstp, current_fh, &op->u.open);
863 			replay_owner = op->u.open.op_stateowner;
864 			break;
865 		case OP_OPEN_CONFIRM:
866 			op->status = nfsd4_open_confirm(rqstp, current_fh, &op->u.open_confirm);
867 			replay_owner = op->u.open_confirm.oc_stateowner;
868 			break;
869 		case OP_OPEN_DOWNGRADE:
870 			op->status = nfsd4_open_downgrade(rqstp, current_fh, &op->u.open_downgrade);
871 			replay_owner = op->u.open_downgrade.od_stateowner;
872 			break;
873 		case OP_PUTFH:
874 			op->status = nfsd4_putfh(rqstp, current_fh, &op->u.putfh);
875 			break;
876 		case OP_PUTROOTFH:
877 			op->status = nfsd4_putrootfh(rqstp, current_fh);
878 			break;
879 		case OP_READ:
880 			op->status = nfsd4_read(rqstp, current_fh, &op->u.read);
881 			break;
882 		case OP_READDIR:
883 			op->status = nfsd4_readdir(rqstp, current_fh, &op->u.readdir);
884 			break;
885 		case OP_READLINK:
886 			op->status = nfsd4_readlink(rqstp, current_fh, &op->u.readlink);
887 			break;
888 		case OP_REMOVE:
889 			op->status = nfsd4_remove(rqstp, current_fh, &op->u.remove);
890 			break;
891 		case OP_RENAME:
892 			op->status = nfsd4_rename(rqstp, current_fh, save_fh, &op->u.rename);
893 			break;
894 		case OP_RENEW:
895 			op->status = nfsd4_renew(&op->u.renew);
896 			break;
897 		case OP_RESTOREFH:
898 			op->status = nfsd4_restorefh(current_fh, save_fh);
899 			break;
900 		case OP_SAVEFH:
901 			op->status = nfsd4_savefh(current_fh, save_fh);
902 			break;
903 		case OP_SETATTR:
904 			op->status = nfsd4_setattr(rqstp, current_fh, &op->u.setattr);
905 			break;
906 		case OP_SETCLIENTID:
907 			op->status = nfsd4_setclientid(rqstp, &op->u.setclientid);
908 			break;
909 		case OP_SETCLIENTID_CONFIRM:
910 			op->status = nfsd4_setclientid_confirm(rqstp, &op->u.setclientid_confirm);
911 			break;
912 		case OP_VERIFY:
913 			op->status = nfsd4_verify(rqstp, current_fh, &op->u.verify);
914 			if (op->status == nfserr_same)
915 				op->status = nfs_ok;
916 			break;
917 		case OP_WRITE:
918 			op->status = nfsd4_write(rqstp, current_fh, &op->u.write);
919 			break;
920 		case OP_RELEASE_LOCKOWNER:
921 			op->status = nfsd4_release_lockowner(rqstp, &op->u.release_lockowner);
922 			break;
923 		default:
924 			BUG_ON(op->status == nfs_ok);
925 			break;
926 		}
927 
928 encode_op:
929 		if (op->status == NFSERR_REPLAY_ME) {
930 			op->replay = &replay_owner->so_replay;
931 			nfsd4_encode_replay(resp, op);
932 			status = op->status = op->replay->rp_status;
933 		} else {
934 			nfsd4_encode_operation(resp, op);
935 			status = op->status;
936 		}
937 		if (replay_owner && (replay_owner != (void *)(-1))) {
938 			nfs4_put_stateowner(replay_owner);
939 			replay_owner = NULL;
940 		}
941 		/* XXX Ugh, we need to get rid of this kind of special case: */
942 		if (op->opnum == OP_READ && op->u.read.rd_filp)
943 			fput(op->u.read.rd_filp);
944 	}
945 
946 out:
947 	nfsd4_release_compoundargs(args);
948 	if (current_fh)
949 		fh_put(current_fh);
950 	kfree(current_fh);
951 	if (save_fh)
952 		fh_put(save_fh);
953 	kfree(save_fh);
954 	return status;
955 }
956 
957 #define nfs4svc_decode_voidargs		NULL
958 #define nfs4svc_release_void		NULL
959 #define nfsd4_voidres			nfsd4_voidargs
960 #define nfs4svc_release_compound	NULL
961 struct nfsd4_voidargs { int dummy; };
962 
963 #define PROC(name, argt, rest, relt, cache, respsize)	\
964  { (svc_procfunc) nfsd4_proc_##name,		\
965    (kxdrproc_t) nfs4svc_decode_##argt##args,	\
966    (kxdrproc_t) nfs4svc_encode_##rest##res,	\
967    (kxdrproc_t) nfs4svc_release_##relt,		\
968    sizeof(struct nfsd4_##argt##args),		\
969    sizeof(struct nfsd4_##rest##res),		\
970    0,						\
971    cache,					\
972    respsize,					\
973  }
974 
975 /*
976  * TODO: At the present time, the NFSv4 server does not do XID caching
977  * of requests.  Implementing XID caching would not be a serious problem,
978  * although it would require a mild change in interfaces since one
979  * doesn't know whether an NFSv4 request is idempotent until after the
980  * XDR decode.  However, XID caching totally confuses pynfs (Peter
981  * Astrand's regression testsuite for NFSv4 servers), which reuses
982  * XID's liberally, so I've left it unimplemented until pynfs generates
983  * better XID's.
984  */
985 static struct svc_procedure		nfsd_procedures4[2] = {
986   PROC(null,	 void,		void,		void,	  RC_NOCACHE, 1),
987   PROC(compound, compound,	compound,	compound, RC_NOCACHE, NFSD_BUFSIZE)
988 };
989 
990 struct svc_version	nfsd_version4 = {
991 		.vs_vers	= 4,
992 		.vs_nproc	= 2,
993 		.vs_proc	= nfsd_procedures4,
994 		.vs_dispatch	= nfsd_dispatch,
995 		.vs_xdrsize	= NFS4_SVC_XDRSIZE,
996 };
997 
998 /*
999  * Local variables:
1000  *  c-basic-offset: 8
1001  * End:
1002  */
1003