• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Server-side XDR for NFSv4
3  *
4  *  Copyright (c) 2002 The Regents of the University of Michigan.
5  *  All rights reserved.
6  *
7  *  Kendrick Smith <kmsmith@umich.edu>
8  *  Andy Adamson   <andros@umich.edu>
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *  1. Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *  2. Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  *  3. Neither the name of the University nor the names of its
20  *     contributors may be used to endorse or promote products derived
21  *     from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * TODO: Neil Brown made the following observation:  We currently
36  * initially reserve NFSD_BUFSIZE space on the transmit queue and
37  * never release any of that until the request is complete.
38  * It would be good to calculate a new maximum response size while
39  * decoding the COMPOUND, and call svc_reserve with this number
40  * at the end of nfs4svc_decode_compoundargs.
41  */
42 
43 #include <linux/slab.h>
44 #include <linux/namei.h>
45 #include <linux/statfs.h>
46 #include <linux/utsname.h>
47 #include <linux/pagemap.h>
48 #include <linux/sunrpc/svcauth_gss.h>
49 
50 #include "idmap.h"
51 #include "acl.h"
52 #include "xdr4.h"
53 #include "vfs.h"
54 #include "state.h"
55 #include "cache.h"
56 
57 #define NFSDDBG_FACILITY		NFSDDBG_XDR
58 
59 /*
60  * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
61  * directory in order to indicate to the client that a filesystem boundary is present
62  * We use a fixed fsid for a referral
63  */
64 #define NFS4_REFERRAL_FSID_MAJOR	0x8000000ULL
65 #define NFS4_REFERRAL_FSID_MINOR	0x8000000ULL
66 
67 static __be32
check_filename(char * str,int len,__be32 err)68 check_filename(char *str, int len, __be32 err)
69 {
70 	int i;
71 
72 	if (len == 0)
73 		return nfserr_inval;
74 	if (isdotent(str, len))
75 		return err;
76 	for (i = 0; i < len; i++)
77 		if (str[i] == '/')
78 			return err;
79 	return 0;
80 }
81 
82 #define DECODE_HEAD				\
83 	__be32 *p;				\
84 	__be32 status
85 #define DECODE_TAIL				\
86 	status = 0;				\
87 out:						\
88 	return status;				\
89 xdr_error:					\
90 	dprintk("NFSD: xdr error (%s:%d)\n",	\
91 			__FILE__, __LINE__);	\
92 	status = nfserr_bad_xdr;		\
93 	goto out
94 
95 #define READ32(x)         (x) = ntohl(*p++)
96 #define READ64(x)         do {			\
97 	(x) = (u64)ntohl(*p++) << 32;		\
98 	(x) |= ntohl(*p++);			\
99 } while (0)
100 #define READTIME(x)       do {			\
101 	p++;					\
102 	(x) = ntohl(*p++);			\
103 	p++;					\
104 } while (0)
105 #define READMEM(x,nbytes) do {			\
106 	x = (char *)p;				\
107 	p += XDR_QUADLEN(nbytes);		\
108 } while (0)
109 #define SAVEMEM(x,nbytes) do {			\
110 	if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
111  		savemem(argp, p, nbytes) :	\
112  		(char *)p)) {			\
113 		dprintk("NFSD: xdr error (%s:%d)\n", \
114 				__FILE__, __LINE__); \
115 		goto xdr_error;			\
116 		}				\
117 	p += XDR_QUADLEN(nbytes);		\
118 } while (0)
119 #define COPYMEM(x,nbytes) do {			\
120 	memcpy((x), p, nbytes);			\
121 	p += XDR_QUADLEN(nbytes);		\
122 } while (0)
123 
124 /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
125 #define READ_BUF(nbytes)  do {			\
126 	if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) {	\
127 		p = argp->p;			\
128 		argp->p += XDR_QUADLEN(nbytes);	\
129 	} else if (!(p = read_buf(argp, nbytes))) { \
130 		dprintk("NFSD: xdr error (%s:%d)\n", \
131 				__FILE__, __LINE__); \
132 		goto xdr_error;			\
133 	}					\
134 } while (0)
135 
read_buf(struct nfsd4_compoundargs * argp,u32 nbytes)136 static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
137 {
138 	/* We want more bytes than seem to be available.
139 	 * Maybe we need a new page, maybe we have just run out
140 	 */
141 	unsigned int avail = (char *)argp->end - (char *)argp->p;
142 	__be32 *p;
143 	if (avail + argp->pagelen < nbytes)
144 		return NULL;
145 	if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
146 		return NULL;
147 	/* ok, we can do it with the current plus the next page */
148 	if (nbytes <= sizeof(argp->tmp))
149 		p = argp->tmp;
150 	else {
151 		kfree(argp->tmpp);
152 		p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
153 		if (!p)
154 			return NULL;
155 
156 	}
157 	/*
158 	 * The following memcpy is safe because read_buf is always
159 	 * called with nbytes > avail, and the two cases above both
160 	 * guarantee p points to at least nbytes bytes.
161 	 */
162 	memcpy(p, argp->p, avail);
163 	/* step to next page */
164 	argp->pagelist++;
165 	argp->p = page_address(argp->pagelist[0]);
166 	if (argp->pagelen < PAGE_SIZE) {
167 		argp->end = argp->p + (argp->pagelen>>2);
168 		argp->pagelen = 0;
169 	} else {
170 		argp->end = argp->p + (PAGE_SIZE>>2);
171 		argp->pagelen -= PAGE_SIZE;
172 	}
173 	memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
174 	argp->p += XDR_QUADLEN(nbytes - avail);
175 	return p;
176 }
177 
zero_clientid(clientid_t * clid)178 static int zero_clientid(clientid_t *clid)
179 {
180 	return (clid->cl_boot == 0) && (clid->cl_id == 0);
181 }
182 
183 static int
defer_free(struct nfsd4_compoundargs * argp,void (* release)(const void *),void * p)184 defer_free(struct nfsd4_compoundargs *argp,
185 		void (*release)(const void *), void *p)
186 {
187 	struct tmpbuf *tb;
188 
189 	tb = kmalloc(sizeof(*tb), GFP_KERNEL);
190 	if (!tb)
191 		return -ENOMEM;
192 	tb->buf = p;
193 	tb->release = release;
194 	tb->next = argp->to_free;
195 	argp->to_free = tb;
196 	return 0;
197 }
198 
savemem(struct nfsd4_compoundargs * argp,__be32 * p,int nbytes)199 static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
200 {
201 	if (p == argp->tmp) {
202 		p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
203 		if (!p)
204 			return NULL;
205 	} else {
206 		BUG_ON(p != argp->tmpp);
207 		argp->tmpp = NULL;
208 	}
209 	if (defer_free(argp, kfree, p)) {
210 		kfree(p);
211 		return NULL;
212 	} else
213 		return (char *)p;
214 }
215 
216 static __be32
nfsd4_decode_bitmap(struct nfsd4_compoundargs * argp,u32 * bmval)217 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
218 {
219 	u32 bmlen;
220 	DECODE_HEAD;
221 
222 	bmval[0] = 0;
223 	bmval[1] = 0;
224 	bmval[2] = 0;
225 
226 	READ_BUF(4);
227 	READ32(bmlen);
228 	if (bmlen > 1000)
229 		goto xdr_error;
230 
231 	READ_BUF(bmlen << 2);
232 	if (bmlen > 0)
233 		READ32(bmval[0]);
234 	if (bmlen > 1)
235 		READ32(bmval[1]);
236 	if (bmlen > 2)
237 		READ32(bmval[2]);
238 
239 	DECODE_TAIL;
240 }
241 
242 static __be32
nfsd4_decode_fattr(struct nfsd4_compoundargs * argp,u32 * bmval,struct iattr * iattr,struct nfs4_acl ** acl)243 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
244 		   struct iattr *iattr, struct nfs4_acl **acl)
245 {
246 	int expected_len, len = 0;
247 	u32 dummy32;
248 	char *buf;
249 	int host_err;
250 
251 	DECODE_HEAD;
252 	iattr->ia_valid = 0;
253 	if ((status = nfsd4_decode_bitmap(argp, bmval)))
254 		return status;
255 
256 	READ_BUF(4);
257 	READ32(expected_len);
258 
259 	if (bmval[0] & FATTR4_WORD0_SIZE) {
260 		READ_BUF(8);
261 		len += 8;
262 		READ64(iattr->ia_size);
263 		iattr->ia_valid |= ATTR_SIZE;
264 	}
265 	if (bmval[0] & FATTR4_WORD0_ACL) {
266 		u32 nace;
267 		struct nfs4_ace *ace;
268 
269 		READ_BUF(4); len += 4;
270 		READ32(nace);
271 
272 		if (nace > NFS4_ACL_MAX)
273 			return nfserr_resource;
274 
275 		*acl = nfs4_acl_new(nace);
276 		if (*acl == NULL) {
277 			host_err = -ENOMEM;
278 			goto out_nfserr;
279 		}
280 		defer_free(argp, kfree, *acl);
281 
282 		(*acl)->naces = nace;
283 		for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
284 			READ_BUF(16); len += 16;
285 			READ32(ace->type);
286 			READ32(ace->flag);
287 			READ32(ace->access_mask);
288 			READ32(dummy32);
289 			READ_BUF(dummy32);
290 			len += XDR_QUADLEN(dummy32) << 2;
291 			READMEM(buf, dummy32);
292 			ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
293 			status = nfs_ok;
294 			if (ace->whotype != NFS4_ACL_WHO_NAMED)
295 				ace->who = 0;
296 			else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
297 				status = nfsd_map_name_to_gid(argp->rqstp,
298 						buf, dummy32, &ace->who);
299 			else
300 				status = nfsd_map_name_to_uid(argp->rqstp,
301 						buf, dummy32, &ace->who);
302 			if (status)
303 				return status;
304 		}
305 	} else
306 		*acl = NULL;
307 	if (bmval[1] & FATTR4_WORD1_MODE) {
308 		READ_BUF(4);
309 		len += 4;
310 		READ32(iattr->ia_mode);
311 		iattr->ia_mode &= (S_IFMT | S_IALLUGO);
312 		iattr->ia_valid |= ATTR_MODE;
313 	}
314 	if (bmval[1] & FATTR4_WORD1_OWNER) {
315 		READ_BUF(4);
316 		len += 4;
317 		READ32(dummy32);
318 		READ_BUF(dummy32);
319 		len += (XDR_QUADLEN(dummy32) << 2);
320 		READMEM(buf, dummy32);
321 		if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
322 			return status;
323 		iattr->ia_valid |= ATTR_UID;
324 	}
325 	if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
326 		READ_BUF(4);
327 		len += 4;
328 		READ32(dummy32);
329 		READ_BUF(dummy32);
330 		len += (XDR_QUADLEN(dummy32) << 2);
331 		READMEM(buf, dummy32);
332 		if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
333 			return status;
334 		iattr->ia_valid |= ATTR_GID;
335 	}
336 	if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
337 		READ_BUF(4);
338 		len += 4;
339 		READ32(dummy32);
340 		switch (dummy32) {
341 		case NFS4_SET_TO_CLIENT_TIME:
342 			/* We require the high 32 bits of 'seconds' to be 0, and we ignore
343 			   all 32 bits of 'nseconds'. */
344 			READ_BUF(12);
345 			len += 12;
346 			READ64(iattr->ia_atime.tv_sec);
347 			READ32(iattr->ia_atime.tv_nsec);
348 			if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
349 				return nfserr_inval;
350 			iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
351 			break;
352 		case NFS4_SET_TO_SERVER_TIME:
353 			iattr->ia_valid |= ATTR_ATIME;
354 			break;
355 		default:
356 			goto xdr_error;
357 		}
358 	}
359 	if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
360 		READ_BUF(4);
361 		len += 4;
362 		READ32(dummy32);
363 		switch (dummy32) {
364 		case NFS4_SET_TO_CLIENT_TIME:
365 			/* We require the high 32 bits of 'seconds' to be 0, and we ignore
366 			   all 32 bits of 'nseconds'. */
367 			READ_BUF(12);
368 			len += 12;
369 			READ64(iattr->ia_mtime.tv_sec);
370 			READ32(iattr->ia_mtime.tv_nsec);
371 			if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
372 				return nfserr_inval;
373 			iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
374 			break;
375 		case NFS4_SET_TO_SERVER_TIME:
376 			iattr->ia_valid |= ATTR_MTIME;
377 			break;
378 		default:
379 			goto xdr_error;
380 		}
381 	}
382 	if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
383 	    || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
384 	    || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
385 		READ_BUF(expected_len - len);
386 	else if (len != expected_len)
387 		goto xdr_error;
388 
389 	DECODE_TAIL;
390 
391 out_nfserr:
392 	status = nfserrno(host_err);
393 	goto out;
394 }
395 
396 static __be32
nfsd4_decode_stateid(struct nfsd4_compoundargs * argp,stateid_t * sid)397 nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
398 {
399 	DECODE_HEAD;
400 
401 	READ_BUF(sizeof(stateid_t));
402 	READ32(sid->si_generation);
403 	COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
404 
405 	DECODE_TAIL;
406 }
407 
408 static __be32
nfsd4_decode_access(struct nfsd4_compoundargs * argp,struct nfsd4_access * access)409 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
410 {
411 	DECODE_HEAD;
412 
413 	READ_BUF(4);
414 	READ32(access->ac_req_access);
415 
416 	DECODE_TAIL;
417 }
418 
nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs * argp,struct nfsd4_bind_conn_to_session * bcts)419 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
420 {
421 	DECODE_HEAD;
422 
423 	READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
424 	COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
425 	READ32(bcts->dir);
426 	/* XXX: skipping ctsa_use_conn_in_rdma_mode.  Perhaps Tom Tucker
427 	 * could help us figure out we should be using it. */
428 	DECODE_TAIL;
429 }
430 
431 static __be32
nfsd4_decode_close(struct nfsd4_compoundargs * argp,struct nfsd4_close * close)432 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
433 {
434 	DECODE_HEAD;
435 
436 	READ_BUF(4);
437 	READ32(close->cl_seqid);
438 	return nfsd4_decode_stateid(argp, &close->cl_stateid);
439 
440 	DECODE_TAIL;
441 }
442 
443 
444 static __be32
nfsd4_decode_commit(struct nfsd4_compoundargs * argp,struct nfsd4_commit * commit)445 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
446 {
447 	DECODE_HEAD;
448 
449 	READ_BUF(12);
450 	READ64(commit->co_offset);
451 	READ32(commit->co_count);
452 
453 	DECODE_TAIL;
454 }
455 
456 static __be32
nfsd4_decode_create(struct nfsd4_compoundargs * argp,struct nfsd4_create * create)457 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
458 {
459 	DECODE_HEAD;
460 
461 	READ_BUF(4);
462 	READ32(create->cr_type);
463 	switch (create->cr_type) {
464 	case NF4LNK:
465 		READ_BUF(4);
466 		READ32(create->cr_linklen);
467 		READ_BUF(create->cr_linklen);
468 		SAVEMEM(create->cr_linkname, create->cr_linklen);
469 		break;
470 	case NF4BLK:
471 	case NF4CHR:
472 		READ_BUF(8);
473 		READ32(create->cr_specdata1);
474 		READ32(create->cr_specdata2);
475 		break;
476 	case NF4SOCK:
477 	case NF4FIFO:
478 	case NF4DIR:
479 	default:
480 		break;
481 	}
482 
483 	READ_BUF(4);
484 	READ32(create->cr_namelen);
485 	READ_BUF(create->cr_namelen);
486 	SAVEMEM(create->cr_name, create->cr_namelen);
487 	if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
488 		return status;
489 
490 	status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
491 				    &create->cr_acl);
492 	if (status)
493 		goto out;
494 
495 	DECODE_TAIL;
496 }
497 
498 static inline __be32
nfsd4_decode_delegreturn(struct nfsd4_compoundargs * argp,struct nfsd4_delegreturn * dr)499 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
500 {
501 	return nfsd4_decode_stateid(argp, &dr->dr_stateid);
502 }
503 
504 static inline __be32
nfsd4_decode_getattr(struct nfsd4_compoundargs * argp,struct nfsd4_getattr * getattr)505 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
506 {
507 	return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
508 }
509 
510 static __be32
nfsd4_decode_link(struct nfsd4_compoundargs * argp,struct nfsd4_link * link)511 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
512 {
513 	DECODE_HEAD;
514 
515 	READ_BUF(4);
516 	READ32(link->li_namelen);
517 	READ_BUF(link->li_namelen);
518 	SAVEMEM(link->li_name, link->li_namelen);
519 	if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
520 		return status;
521 
522 	DECODE_TAIL;
523 }
524 
525 static __be32
nfsd4_decode_lock(struct nfsd4_compoundargs * argp,struct nfsd4_lock * lock)526 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
527 {
528 	DECODE_HEAD;
529 
530 	/*
531 	* type, reclaim(boolean), offset, length, new_lock_owner(boolean)
532 	*/
533 	READ_BUF(28);
534 	READ32(lock->lk_type);
535 	if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
536 		goto xdr_error;
537 	READ32(lock->lk_reclaim);
538 	READ64(lock->lk_offset);
539 	READ64(lock->lk_length);
540 	READ32(lock->lk_is_new);
541 
542 	if (lock->lk_is_new) {
543 		READ_BUF(4);
544 		READ32(lock->lk_new_open_seqid);
545 		status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
546 		if (status)
547 			return status;
548 		READ_BUF(8 + sizeof(clientid_t));
549 		READ32(lock->lk_new_lock_seqid);
550 		COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
551 		READ32(lock->lk_new_owner.len);
552 		READ_BUF(lock->lk_new_owner.len);
553 		READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
554 	} else {
555 		status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
556 		if (status)
557 			return status;
558 		READ_BUF(4);
559 		READ32(lock->lk_old_lock_seqid);
560 	}
561 
562 	DECODE_TAIL;
563 }
564 
565 static __be32
nfsd4_decode_lockt(struct nfsd4_compoundargs * argp,struct nfsd4_lockt * lockt)566 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
567 {
568 	DECODE_HEAD;
569 
570 	READ_BUF(32);
571 	READ32(lockt->lt_type);
572 	if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
573 		goto xdr_error;
574 	READ64(lockt->lt_offset);
575 	READ64(lockt->lt_length);
576 	COPYMEM(&lockt->lt_clientid, 8);
577 	READ32(lockt->lt_owner.len);
578 	READ_BUF(lockt->lt_owner.len);
579 	READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
580 
581 	DECODE_TAIL;
582 }
583 
584 static __be32
nfsd4_decode_locku(struct nfsd4_compoundargs * argp,struct nfsd4_locku * locku)585 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
586 {
587 	DECODE_HEAD;
588 
589 	READ_BUF(8);
590 	READ32(locku->lu_type);
591 	if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
592 		goto xdr_error;
593 	READ32(locku->lu_seqid);
594 	status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
595 	if (status)
596 		return status;
597 	READ_BUF(16);
598 	READ64(locku->lu_offset);
599 	READ64(locku->lu_length);
600 
601 	DECODE_TAIL;
602 }
603 
604 static __be32
nfsd4_decode_lookup(struct nfsd4_compoundargs * argp,struct nfsd4_lookup * lookup)605 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
606 {
607 	DECODE_HEAD;
608 
609 	READ_BUF(4);
610 	READ32(lookup->lo_len);
611 	READ_BUF(lookup->lo_len);
612 	SAVEMEM(lookup->lo_name, lookup->lo_len);
613 	if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
614 		return status;
615 
616 	DECODE_TAIL;
617 }
618 
nfsd4_decode_share_access(struct nfsd4_compoundargs * argp,u32 * share_access,u32 * deleg_want,u32 * deleg_when)619 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
620 {
621 	__be32 *p;
622 	u32 w;
623 
624 	READ_BUF(4);
625 	READ32(w);
626 	*share_access = w & NFS4_SHARE_ACCESS_MASK;
627 	*deleg_want = w & NFS4_SHARE_WANT_MASK;
628 	if (deleg_when)
629 		*deleg_when = w & NFS4_SHARE_WHEN_MASK;
630 
631 	switch (w & NFS4_SHARE_ACCESS_MASK) {
632 	case NFS4_SHARE_ACCESS_READ:
633 	case NFS4_SHARE_ACCESS_WRITE:
634 	case NFS4_SHARE_ACCESS_BOTH:
635 		break;
636 	default:
637 		return nfserr_bad_xdr;
638 	}
639 	w &= ~NFS4_SHARE_ACCESS_MASK;
640 	if (!w)
641 		return nfs_ok;
642 	if (!argp->minorversion)
643 		return nfserr_bad_xdr;
644 	switch (w & NFS4_SHARE_WANT_MASK) {
645 	case NFS4_SHARE_WANT_NO_PREFERENCE:
646 	case NFS4_SHARE_WANT_READ_DELEG:
647 	case NFS4_SHARE_WANT_WRITE_DELEG:
648 	case NFS4_SHARE_WANT_ANY_DELEG:
649 	case NFS4_SHARE_WANT_NO_DELEG:
650 	case NFS4_SHARE_WANT_CANCEL:
651 		break;
652 	default:
653 		return nfserr_bad_xdr;
654 	}
655 	w &= ~NFS4_SHARE_WANT_MASK;
656 	if (!w)
657 		return nfs_ok;
658 
659 	if (!deleg_when)	/* open_downgrade */
660 		return nfserr_inval;
661 	switch (w) {
662 	case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
663 	case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
664 	case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
665 	      NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
666 		return nfs_ok;
667 	}
668 xdr_error:
669 	return nfserr_bad_xdr;
670 }
671 
nfsd4_decode_share_deny(struct nfsd4_compoundargs * argp,u32 * x)672 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
673 {
674 	__be32 *p;
675 
676 	READ_BUF(4);
677 	READ32(*x);
678 	/* Note: unlinke access bits, deny bits may be zero. */
679 	if (*x & ~NFS4_SHARE_DENY_BOTH)
680 		return nfserr_bad_xdr;
681 	return nfs_ok;
682 xdr_error:
683 	return nfserr_bad_xdr;
684 }
685 
nfsd4_decode_opaque(struct nfsd4_compoundargs * argp,struct xdr_netobj * o)686 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
687 {
688 	__be32 *p;
689 
690 	READ_BUF(4);
691 	READ32(o->len);
692 
693 	if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
694 		return nfserr_bad_xdr;
695 
696 	READ_BUF(o->len);
697 	SAVEMEM(o->data, o->len);
698 	return nfs_ok;
699 xdr_error:
700 	return nfserr_bad_xdr;
701 }
702 
703 static __be32
nfsd4_decode_open(struct nfsd4_compoundargs * argp,struct nfsd4_open * open)704 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
705 {
706 	DECODE_HEAD;
707 	u32 dummy;
708 
709 	memset(open->op_bmval, 0, sizeof(open->op_bmval));
710 	open->op_iattr.ia_valid = 0;
711 	open->op_openowner = NULL;
712 
713 	/* seqid, share_access, share_deny, clientid, ownerlen */
714 	READ_BUF(4);
715 	READ32(open->op_seqid);
716 	/* decode, yet ignore deleg_when until supported */
717 	status = nfsd4_decode_share_access(argp, &open->op_share_access,
718 					   &open->op_deleg_want, &dummy);
719 	if (status)
720 		goto xdr_error;
721 	status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
722 	if (status)
723 		goto xdr_error;
724 	READ_BUF(sizeof(clientid_t));
725 	COPYMEM(&open->op_clientid, sizeof(clientid_t));
726 	status = nfsd4_decode_opaque(argp, &open->op_owner);
727 	if (status)
728 		goto xdr_error;
729 	READ_BUF(4);
730 	READ32(open->op_create);
731 	switch (open->op_create) {
732 	case NFS4_OPEN_NOCREATE:
733 		break;
734 	case NFS4_OPEN_CREATE:
735 		READ_BUF(4);
736 		READ32(open->op_createmode);
737 		switch (open->op_createmode) {
738 		case NFS4_CREATE_UNCHECKED:
739 		case NFS4_CREATE_GUARDED:
740 			status = nfsd4_decode_fattr(argp, open->op_bmval,
741 				&open->op_iattr, &open->op_acl);
742 			if (status)
743 				goto out;
744 			break;
745 		case NFS4_CREATE_EXCLUSIVE:
746 			READ_BUF(NFS4_VERIFIER_SIZE);
747 			COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
748 			break;
749 		case NFS4_CREATE_EXCLUSIVE4_1:
750 			if (argp->minorversion < 1)
751 				goto xdr_error;
752 			READ_BUF(NFS4_VERIFIER_SIZE);
753 			COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
754 			status = nfsd4_decode_fattr(argp, open->op_bmval,
755 				&open->op_iattr, &open->op_acl);
756 			if (status)
757 				goto out;
758 			break;
759 		default:
760 			goto xdr_error;
761 		}
762 		break;
763 	default:
764 		goto xdr_error;
765 	}
766 
767 	/* open_claim */
768 	READ_BUF(4);
769 	READ32(open->op_claim_type);
770 	switch (open->op_claim_type) {
771 	case NFS4_OPEN_CLAIM_NULL:
772 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
773 		READ_BUF(4);
774 		READ32(open->op_fname.len);
775 		READ_BUF(open->op_fname.len);
776 		SAVEMEM(open->op_fname.data, open->op_fname.len);
777 		if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
778 			return status;
779 		break;
780 	case NFS4_OPEN_CLAIM_PREVIOUS:
781 		READ_BUF(4);
782 		READ32(open->op_delegate_type);
783 		break;
784 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
785 		status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
786 		if (status)
787 			return status;
788 		READ_BUF(4);
789 		READ32(open->op_fname.len);
790 		READ_BUF(open->op_fname.len);
791 		SAVEMEM(open->op_fname.data, open->op_fname.len);
792 		if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
793 			return status;
794 		break;
795 	case NFS4_OPEN_CLAIM_FH:
796 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
797 		if (argp->minorversion < 1)
798 			goto xdr_error;
799 		/* void */
800 		break;
801 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
802 		if (argp->minorversion < 1)
803 			goto xdr_error;
804 		status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
805 		if (status)
806 			return status;
807 		break;
808 	default:
809 		goto xdr_error;
810 	}
811 
812 	DECODE_TAIL;
813 }
814 
815 static __be32
nfsd4_decode_open_confirm(struct nfsd4_compoundargs * argp,struct nfsd4_open_confirm * open_conf)816 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
817 {
818 	DECODE_HEAD;
819 
820 	status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
821 	if (status)
822 		return status;
823 	READ_BUF(4);
824 	READ32(open_conf->oc_seqid);
825 
826 	DECODE_TAIL;
827 }
828 
829 static __be32
nfsd4_decode_open_downgrade(struct nfsd4_compoundargs * argp,struct nfsd4_open_downgrade * open_down)830 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
831 {
832 	DECODE_HEAD;
833 
834 	status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
835 	if (status)
836 		return status;
837 	READ_BUF(4);
838 	READ32(open_down->od_seqid);
839 	status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
840 					   &open_down->od_deleg_want, NULL);
841 	if (status)
842 		return status;
843 	status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
844 	if (status)
845 		return status;
846 	DECODE_TAIL;
847 }
848 
849 static __be32
nfsd4_decode_putfh(struct nfsd4_compoundargs * argp,struct nfsd4_putfh * putfh)850 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
851 {
852 	DECODE_HEAD;
853 
854 	READ_BUF(4);
855 	READ32(putfh->pf_fhlen);
856 	if (putfh->pf_fhlen > NFS4_FHSIZE)
857 		goto xdr_error;
858 	READ_BUF(putfh->pf_fhlen);
859 	SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
860 
861 	DECODE_TAIL;
862 }
863 
864 static __be32
nfsd4_decode_read(struct nfsd4_compoundargs * argp,struct nfsd4_read * read)865 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
866 {
867 	DECODE_HEAD;
868 
869 	status = nfsd4_decode_stateid(argp, &read->rd_stateid);
870 	if (status)
871 		return status;
872 	READ_BUF(12);
873 	READ64(read->rd_offset);
874 	READ32(read->rd_length);
875 
876 	DECODE_TAIL;
877 }
878 
879 static __be32
nfsd4_decode_readdir(struct nfsd4_compoundargs * argp,struct nfsd4_readdir * readdir)880 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
881 {
882 	DECODE_HEAD;
883 
884 	READ_BUF(24);
885 	READ64(readdir->rd_cookie);
886 	COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
887 	READ32(readdir->rd_dircount);    /* just in case you needed a useless field... */
888 	READ32(readdir->rd_maxcount);
889 	if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
890 		goto out;
891 
892 	DECODE_TAIL;
893 }
894 
895 static __be32
nfsd4_decode_remove(struct nfsd4_compoundargs * argp,struct nfsd4_remove * remove)896 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
897 {
898 	DECODE_HEAD;
899 
900 	READ_BUF(4);
901 	READ32(remove->rm_namelen);
902 	READ_BUF(remove->rm_namelen);
903 	SAVEMEM(remove->rm_name, remove->rm_namelen);
904 	if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
905 		return status;
906 
907 	DECODE_TAIL;
908 }
909 
910 static __be32
nfsd4_decode_rename(struct nfsd4_compoundargs * argp,struct nfsd4_rename * rename)911 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
912 {
913 	DECODE_HEAD;
914 
915 	READ_BUF(4);
916 	READ32(rename->rn_snamelen);
917 	READ_BUF(rename->rn_snamelen + 4);
918 	SAVEMEM(rename->rn_sname, rename->rn_snamelen);
919 	READ32(rename->rn_tnamelen);
920 	READ_BUF(rename->rn_tnamelen);
921 	SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
922 	if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
923 		return status;
924 	if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
925 		return status;
926 
927 	DECODE_TAIL;
928 }
929 
930 static __be32
nfsd4_decode_renew(struct nfsd4_compoundargs * argp,clientid_t * clientid)931 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
932 {
933 	DECODE_HEAD;
934 
935 	READ_BUF(sizeof(clientid_t));
936 	COPYMEM(clientid, sizeof(clientid_t));
937 
938 	DECODE_TAIL;
939 }
940 
941 static __be32
nfsd4_decode_secinfo(struct nfsd4_compoundargs * argp,struct nfsd4_secinfo * secinfo)942 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
943 		     struct nfsd4_secinfo *secinfo)
944 {
945 	DECODE_HEAD;
946 
947 	READ_BUF(4);
948 	READ32(secinfo->si_namelen);
949 	READ_BUF(secinfo->si_namelen);
950 	SAVEMEM(secinfo->si_name, secinfo->si_namelen);
951 	status = check_filename(secinfo->si_name, secinfo->si_namelen,
952 								nfserr_noent);
953 	if (status)
954 		return status;
955 	DECODE_TAIL;
956 }
957 
958 static __be32
nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs * argp,struct nfsd4_secinfo_no_name * sin)959 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
960 		     struct nfsd4_secinfo_no_name *sin)
961 {
962 	DECODE_HEAD;
963 
964 	READ_BUF(4);
965 	READ32(sin->sin_style);
966 	DECODE_TAIL;
967 }
968 
969 static __be32
nfsd4_decode_setattr(struct nfsd4_compoundargs * argp,struct nfsd4_setattr * setattr)970 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
971 {
972 	__be32 status;
973 
974 	status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
975 	if (status)
976 		return status;
977 	return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
978 				  &setattr->sa_acl);
979 }
980 
981 static __be32
nfsd4_decode_setclientid(struct nfsd4_compoundargs * argp,struct nfsd4_setclientid * setclientid)982 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
983 {
984 	DECODE_HEAD;
985 
986 	READ_BUF(NFS4_VERIFIER_SIZE);
987 	COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
988 
989 	status = nfsd4_decode_opaque(argp, &setclientid->se_name);
990 	if (status)
991 		return nfserr_bad_xdr;
992 	READ_BUF(8);
993 	READ32(setclientid->se_callback_prog);
994 	READ32(setclientid->se_callback_netid_len);
995 
996 	READ_BUF(setclientid->se_callback_netid_len + 4);
997 	SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
998 	READ32(setclientid->se_callback_addr_len);
999 
1000 	READ_BUF(setclientid->se_callback_addr_len + 4);
1001 	SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1002 	READ32(setclientid->se_callback_ident);
1003 
1004 	DECODE_TAIL;
1005 }
1006 
1007 static __be32
nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs * argp,struct nfsd4_setclientid_confirm * scd_c)1008 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1009 {
1010 	DECODE_HEAD;
1011 
1012 	READ_BUF(8 + NFS4_VERIFIER_SIZE);
1013 	COPYMEM(&scd_c->sc_clientid, 8);
1014 	COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1015 
1016 	DECODE_TAIL;
1017 }
1018 
1019 /* Also used for NVERIFY */
1020 static __be32
nfsd4_decode_verify(struct nfsd4_compoundargs * argp,struct nfsd4_verify * verify)1021 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1022 {
1023 #if 0
1024 	struct nfsd4_compoundargs save = {
1025 		.p = argp->p,
1026 		.end = argp->end,
1027 		.rqstp = argp->rqstp,
1028 	};
1029 	u32             ve_bmval[2];
1030 	struct iattr    ve_iattr;           /* request */
1031 	struct nfs4_acl *ve_acl;            /* request */
1032 #endif
1033 	DECODE_HEAD;
1034 
1035 	if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1036 		goto out;
1037 
1038 	/* For convenience's sake, we compare raw xdr'd attributes in
1039 	 * nfsd4_proc_verify; however we still decode here just to return
1040 	 * correct error in case of bad xdr. */
1041 #if 0
1042 	status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
1043 	if (status == nfserr_inval) {
1044 		status = nfserrno(status);
1045 		goto out;
1046 	}
1047 #endif
1048 	READ_BUF(4);
1049 	READ32(verify->ve_attrlen);
1050 	READ_BUF(verify->ve_attrlen);
1051 	SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1052 
1053 	DECODE_TAIL;
1054 }
1055 
1056 static __be32
nfsd4_decode_write(struct nfsd4_compoundargs * argp,struct nfsd4_write * write)1057 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1058 {
1059 	int avail;
1060 	int v;
1061 	int len;
1062 	DECODE_HEAD;
1063 
1064 	status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1065 	if (status)
1066 		return status;
1067 	READ_BUF(16);
1068 	READ64(write->wr_offset);
1069 	READ32(write->wr_stable_how);
1070 	if (write->wr_stable_how > 2)
1071 		goto xdr_error;
1072 	READ32(write->wr_buflen);
1073 
1074 	/* Sorry .. no magic macros for this.. *
1075 	 * READ_BUF(write->wr_buflen);
1076 	 * SAVEMEM(write->wr_buf, write->wr_buflen);
1077 	 */
1078 	avail = (char*)argp->end - (char*)argp->p;
1079 	if (avail + argp->pagelen < write->wr_buflen) {
1080 		dprintk("NFSD: xdr error (%s:%d)\n",
1081 				__FILE__, __LINE__);
1082 		goto xdr_error;
1083 	}
1084 	argp->rqstp->rq_vec[0].iov_base = p;
1085 	argp->rqstp->rq_vec[0].iov_len = avail;
1086 	v = 0;
1087 	len = write->wr_buflen;
1088 	while (len > argp->rqstp->rq_vec[v].iov_len) {
1089 		len -= argp->rqstp->rq_vec[v].iov_len;
1090 		v++;
1091 		argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
1092 		argp->pagelist++;
1093 		if (argp->pagelen >= PAGE_SIZE) {
1094 			argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
1095 			argp->pagelen -= PAGE_SIZE;
1096 		} else {
1097 			argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
1098 			argp->pagelen -= len;
1099 		}
1100 	}
1101 	argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1102 	argp->p = (__be32*)  (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
1103 	argp->rqstp->rq_vec[v].iov_len = len;
1104 	write->wr_vlen = v+1;
1105 
1106 	DECODE_TAIL;
1107 }
1108 
1109 static __be32
nfsd4_decode_release_lockowner(struct nfsd4_compoundargs * argp,struct nfsd4_release_lockowner * rlockowner)1110 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1111 {
1112 	DECODE_HEAD;
1113 
1114 	READ_BUF(12);
1115 	COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1116 	READ32(rlockowner->rl_owner.len);
1117 	READ_BUF(rlockowner->rl_owner.len);
1118 	READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1119 
1120 	if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1121 		return nfserr_inval;
1122 	DECODE_TAIL;
1123 }
1124 
1125 static __be32
nfsd4_decode_exchange_id(struct nfsd4_compoundargs * argp,struct nfsd4_exchange_id * exid)1126 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1127 			 struct nfsd4_exchange_id *exid)
1128 {
1129 	int dummy, tmp;
1130 	DECODE_HEAD;
1131 
1132 	READ_BUF(NFS4_VERIFIER_SIZE);
1133 	COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1134 
1135 	status = nfsd4_decode_opaque(argp, &exid->clname);
1136 	if (status)
1137 		return nfserr_bad_xdr;
1138 
1139 	READ_BUF(4);
1140 	READ32(exid->flags);
1141 
1142 	/* Ignore state_protect4_a */
1143 	READ_BUF(4);
1144 	READ32(exid->spa_how);
1145 	switch (exid->spa_how) {
1146 	case SP4_NONE:
1147 		break;
1148 	case SP4_MACH_CRED:
1149 		/* spo_must_enforce */
1150 		READ_BUF(4);
1151 		READ32(dummy);
1152 		READ_BUF(dummy * 4);
1153 		p += dummy;
1154 
1155 		/* spo_must_allow */
1156 		READ_BUF(4);
1157 		READ32(dummy);
1158 		READ_BUF(dummy * 4);
1159 		p += dummy;
1160 		break;
1161 	case SP4_SSV:
1162 		/* ssp_ops */
1163 		READ_BUF(4);
1164 		READ32(dummy);
1165 		READ_BUF(dummy * 4);
1166 		p += dummy;
1167 
1168 		READ_BUF(4);
1169 		READ32(dummy);
1170 		READ_BUF(dummy * 4);
1171 		p += dummy;
1172 
1173 		/* ssp_hash_algs<> */
1174 		READ_BUF(4);
1175 		READ32(tmp);
1176 		while (tmp--) {
1177 			READ_BUF(4);
1178 			READ32(dummy);
1179 			READ_BUF(dummy);
1180 			p += XDR_QUADLEN(dummy);
1181 		}
1182 
1183 		/* ssp_encr_algs<> */
1184 		READ_BUF(4);
1185 		READ32(tmp);
1186 		while (tmp--) {
1187 			READ_BUF(4);
1188 			READ32(dummy);
1189 			READ_BUF(dummy);
1190 			p += XDR_QUADLEN(dummy);
1191 		}
1192 
1193 		/* ssp_window and ssp_num_gss_handles */
1194 		READ_BUF(8);
1195 		READ32(dummy);
1196 		READ32(dummy);
1197 		break;
1198 	default:
1199 		goto xdr_error;
1200 	}
1201 
1202 	/* Ignore Implementation ID */
1203 	READ_BUF(4);    /* nfs_impl_id4 array length */
1204 	READ32(dummy);
1205 
1206 	if (dummy > 1)
1207 		goto xdr_error;
1208 
1209 	if (dummy == 1) {
1210 		/* nii_domain */
1211 		READ_BUF(4);
1212 		READ32(dummy);
1213 		READ_BUF(dummy);
1214 		p += XDR_QUADLEN(dummy);
1215 
1216 		/* nii_name */
1217 		READ_BUF(4);
1218 		READ32(dummy);
1219 		READ_BUF(dummy);
1220 		p += XDR_QUADLEN(dummy);
1221 
1222 		/* nii_date */
1223 		READ_BUF(12);
1224 		p += 3;
1225 	}
1226 	DECODE_TAIL;
1227 }
1228 
1229 static __be32
nfsd4_decode_create_session(struct nfsd4_compoundargs * argp,struct nfsd4_create_session * sess)1230 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1231 			    struct nfsd4_create_session *sess)
1232 {
1233 	DECODE_HEAD;
1234 
1235 	u32 dummy;
1236 	char *machine_name;
1237 	int i;
1238 	int nr_secflavs;
1239 
1240 	READ_BUF(16);
1241 	COPYMEM(&sess->clientid, 8);
1242 	READ32(sess->seqid);
1243 	READ32(sess->flags);
1244 
1245 	/* Fore channel attrs */
1246 	READ_BUF(28);
1247 	READ32(dummy); /* headerpadsz is always 0 */
1248 	READ32(sess->fore_channel.maxreq_sz);
1249 	READ32(sess->fore_channel.maxresp_sz);
1250 	READ32(sess->fore_channel.maxresp_cached);
1251 	READ32(sess->fore_channel.maxops);
1252 	READ32(sess->fore_channel.maxreqs);
1253 	READ32(sess->fore_channel.nr_rdma_attrs);
1254 	if (sess->fore_channel.nr_rdma_attrs == 1) {
1255 		READ_BUF(4);
1256 		READ32(sess->fore_channel.rdma_attrs);
1257 	} else if (sess->fore_channel.nr_rdma_attrs > 1) {
1258 		dprintk("Too many fore channel attr bitmaps!\n");
1259 		goto xdr_error;
1260 	}
1261 
1262 	/* Back channel attrs */
1263 	READ_BUF(28);
1264 	READ32(dummy); /* headerpadsz is always 0 */
1265 	READ32(sess->back_channel.maxreq_sz);
1266 	READ32(sess->back_channel.maxresp_sz);
1267 	READ32(sess->back_channel.maxresp_cached);
1268 	READ32(sess->back_channel.maxops);
1269 	READ32(sess->back_channel.maxreqs);
1270 	READ32(sess->back_channel.nr_rdma_attrs);
1271 	if (sess->back_channel.nr_rdma_attrs == 1) {
1272 		READ_BUF(4);
1273 		READ32(sess->back_channel.rdma_attrs);
1274 	} else if (sess->back_channel.nr_rdma_attrs > 1) {
1275 		dprintk("Too many back channel attr bitmaps!\n");
1276 		goto xdr_error;
1277 	}
1278 
1279 	READ_BUF(8);
1280 	READ32(sess->callback_prog);
1281 
1282 	/* callback_sec_params4 */
1283 	READ32(nr_secflavs);
1284 	for (i = 0; i < nr_secflavs; ++i) {
1285 		READ_BUF(4);
1286 		READ32(dummy);
1287 		switch (dummy) {
1288 		case RPC_AUTH_NULL:
1289 			/* Nothing to read */
1290 			break;
1291 		case RPC_AUTH_UNIX:
1292 			READ_BUF(8);
1293 			/* stamp */
1294 			READ32(dummy);
1295 
1296 			/* machine name */
1297 			READ32(dummy);
1298 			READ_BUF(dummy);
1299 			SAVEMEM(machine_name, dummy);
1300 
1301 			/* uid, gid */
1302 			READ_BUF(8);
1303 			READ32(sess->uid);
1304 			READ32(sess->gid);
1305 
1306 			/* more gids */
1307 			READ_BUF(4);
1308 			READ32(dummy);
1309 			READ_BUF(dummy * 4);
1310 			break;
1311 		case RPC_AUTH_GSS:
1312 			dprintk("RPC_AUTH_GSS callback secflavor "
1313 				"not supported!\n");
1314 			READ_BUF(8);
1315 			/* gcbp_service */
1316 			READ32(dummy);
1317 			/* gcbp_handle_from_server */
1318 			READ32(dummy);
1319 			READ_BUF(dummy);
1320 			p += XDR_QUADLEN(dummy);
1321 			/* gcbp_handle_from_client */
1322 			READ_BUF(4);
1323 			READ32(dummy);
1324 			READ_BUF(dummy);
1325 			break;
1326 		default:
1327 			dprintk("Illegal callback secflavor\n");
1328 			return nfserr_inval;
1329 		}
1330 	}
1331 	DECODE_TAIL;
1332 }
1333 
1334 static __be32
nfsd4_decode_destroy_session(struct nfsd4_compoundargs * argp,struct nfsd4_destroy_session * destroy_session)1335 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1336 			     struct nfsd4_destroy_session *destroy_session)
1337 {
1338 	DECODE_HEAD;
1339 	READ_BUF(NFS4_MAX_SESSIONID_LEN);
1340 	COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1341 
1342 	DECODE_TAIL;
1343 }
1344 
1345 static __be32
nfsd4_decode_free_stateid(struct nfsd4_compoundargs * argp,struct nfsd4_free_stateid * free_stateid)1346 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1347 			  struct nfsd4_free_stateid *free_stateid)
1348 {
1349 	DECODE_HEAD;
1350 
1351 	READ_BUF(sizeof(stateid_t));
1352 	READ32(free_stateid->fr_stateid.si_generation);
1353 	COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1354 
1355 	DECODE_TAIL;
1356 }
1357 
1358 static __be32
nfsd4_decode_sequence(struct nfsd4_compoundargs * argp,struct nfsd4_sequence * seq)1359 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1360 		      struct nfsd4_sequence *seq)
1361 {
1362 	DECODE_HEAD;
1363 
1364 	READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1365 	COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1366 	READ32(seq->seqid);
1367 	READ32(seq->slotid);
1368 	READ32(seq->maxslots);
1369 	READ32(seq->cachethis);
1370 
1371 	DECODE_TAIL;
1372 }
1373 
1374 static __be32
nfsd4_decode_test_stateid(struct nfsd4_compoundargs * argp,struct nfsd4_test_stateid * test_stateid)1375 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1376 {
1377 	int i;
1378 	__be32 *p, status;
1379 	struct nfsd4_test_stateid_id *stateid;
1380 
1381 	READ_BUF(4);
1382 	test_stateid->ts_num_ids = ntohl(*p++);
1383 
1384 	INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1385 
1386 	for (i = 0; i < test_stateid->ts_num_ids; i++) {
1387 		stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1388 		if (!stateid) {
1389 			status = nfserrno(-ENOMEM);
1390 			goto out;
1391 		}
1392 
1393 		defer_free(argp, kfree, stateid);
1394 		INIT_LIST_HEAD(&stateid->ts_id_list);
1395 		list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1396 
1397 		status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1398 		if (status)
1399 			goto out;
1400 	}
1401 
1402 	status = 0;
1403 out:
1404 	return status;
1405 xdr_error:
1406 	dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1407 	status = nfserr_bad_xdr;
1408 	goto out;
1409 }
1410 
nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs * argp,struct nfsd4_destroy_clientid * dc)1411 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1412 {
1413 	DECODE_HEAD;
1414 
1415 	READ_BUF(8);
1416 	COPYMEM(&dc->clientid, 8);
1417 
1418 	DECODE_TAIL;
1419 }
1420 
nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs * argp,struct nfsd4_reclaim_complete * rc)1421 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1422 {
1423 	DECODE_HEAD;
1424 
1425 	READ_BUF(4);
1426 	READ32(rc->rca_one_fs);
1427 
1428 	DECODE_TAIL;
1429 }
1430 
1431 static __be32
nfsd4_decode_noop(struct nfsd4_compoundargs * argp,void * p)1432 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1433 {
1434 	return nfs_ok;
1435 }
1436 
1437 static __be32
nfsd4_decode_notsupp(struct nfsd4_compoundargs * argp,void * p)1438 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1439 {
1440 	return nfserr_notsupp;
1441 }
1442 
1443 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1444 
1445 static nfsd4_dec nfsd4_dec_ops[] = {
1446 	[OP_ACCESS]		= (nfsd4_dec)nfsd4_decode_access,
1447 	[OP_CLOSE]		= (nfsd4_dec)nfsd4_decode_close,
1448 	[OP_COMMIT]		= (nfsd4_dec)nfsd4_decode_commit,
1449 	[OP_CREATE]		= (nfsd4_dec)nfsd4_decode_create,
1450 	[OP_DELEGPURGE]		= (nfsd4_dec)nfsd4_decode_notsupp,
1451 	[OP_DELEGRETURN]	= (nfsd4_dec)nfsd4_decode_delegreturn,
1452 	[OP_GETATTR]		= (nfsd4_dec)nfsd4_decode_getattr,
1453 	[OP_GETFH]		= (nfsd4_dec)nfsd4_decode_noop,
1454 	[OP_LINK]		= (nfsd4_dec)nfsd4_decode_link,
1455 	[OP_LOCK]		= (nfsd4_dec)nfsd4_decode_lock,
1456 	[OP_LOCKT]		= (nfsd4_dec)nfsd4_decode_lockt,
1457 	[OP_LOCKU]		= (nfsd4_dec)nfsd4_decode_locku,
1458 	[OP_LOOKUP]		= (nfsd4_dec)nfsd4_decode_lookup,
1459 	[OP_LOOKUPP]		= (nfsd4_dec)nfsd4_decode_noop,
1460 	[OP_NVERIFY]		= (nfsd4_dec)nfsd4_decode_verify,
1461 	[OP_OPEN]		= (nfsd4_dec)nfsd4_decode_open,
1462 	[OP_OPENATTR]		= (nfsd4_dec)nfsd4_decode_notsupp,
1463 	[OP_OPEN_CONFIRM]	= (nfsd4_dec)nfsd4_decode_open_confirm,
1464 	[OP_OPEN_DOWNGRADE]	= (nfsd4_dec)nfsd4_decode_open_downgrade,
1465 	[OP_PUTFH]		= (nfsd4_dec)nfsd4_decode_putfh,
1466 	[OP_PUTPUBFH]		= (nfsd4_dec)nfsd4_decode_noop,
1467 	[OP_PUTROOTFH]		= (nfsd4_dec)nfsd4_decode_noop,
1468 	[OP_READ]		= (nfsd4_dec)nfsd4_decode_read,
1469 	[OP_READDIR]		= (nfsd4_dec)nfsd4_decode_readdir,
1470 	[OP_READLINK]		= (nfsd4_dec)nfsd4_decode_noop,
1471 	[OP_REMOVE]		= (nfsd4_dec)nfsd4_decode_remove,
1472 	[OP_RENAME]		= (nfsd4_dec)nfsd4_decode_rename,
1473 	[OP_RENEW]		= (nfsd4_dec)nfsd4_decode_renew,
1474 	[OP_RESTOREFH]		= (nfsd4_dec)nfsd4_decode_noop,
1475 	[OP_SAVEFH]		= (nfsd4_dec)nfsd4_decode_noop,
1476 	[OP_SECINFO]		= (nfsd4_dec)nfsd4_decode_secinfo,
1477 	[OP_SETATTR]		= (nfsd4_dec)nfsd4_decode_setattr,
1478 	[OP_SETCLIENTID]	= (nfsd4_dec)nfsd4_decode_setclientid,
1479 	[OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1480 	[OP_VERIFY]		= (nfsd4_dec)nfsd4_decode_verify,
1481 	[OP_WRITE]		= (nfsd4_dec)nfsd4_decode_write,
1482 	[OP_RELEASE_LOCKOWNER]	= (nfsd4_dec)nfsd4_decode_release_lockowner,
1483 };
1484 
1485 static nfsd4_dec nfsd41_dec_ops[] = {
1486 	[OP_ACCESS]		= (nfsd4_dec)nfsd4_decode_access,
1487 	[OP_CLOSE]		= (nfsd4_dec)nfsd4_decode_close,
1488 	[OP_COMMIT]		= (nfsd4_dec)nfsd4_decode_commit,
1489 	[OP_CREATE]		= (nfsd4_dec)nfsd4_decode_create,
1490 	[OP_DELEGPURGE]		= (nfsd4_dec)nfsd4_decode_notsupp,
1491 	[OP_DELEGRETURN]	= (nfsd4_dec)nfsd4_decode_delegreturn,
1492 	[OP_GETATTR]		= (nfsd4_dec)nfsd4_decode_getattr,
1493 	[OP_GETFH]		= (nfsd4_dec)nfsd4_decode_noop,
1494 	[OP_LINK]		= (nfsd4_dec)nfsd4_decode_link,
1495 	[OP_LOCK]		= (nfsd4_dec)nfsd4_decode_lock,
1496 	[OP_LOCKT]		= (nfsd4_dec)nfsd4_decode_lockt,
1497 	[OP_LOCKU]		= (nfsd4_dec)nfsd4_decode_locku,
1498 	[OP_LOOKUP]		= (nfsd4_dec)nfsd4_decode_lookup,
1499 	[OP_LOOKUPP]		= (nfsd4_dec)nfsd4_decode_noop,
1500 	[OP_NVERIFY]		= (nfsd4_dec)nfsd4_decode_verify,
1501 	[OP_OPEN]		= (nfsd4_dec)nfsd4_decode_open,
1502 	[OP_OPENATTR]		= (nfsd4_dec)nfsd4_decode_notsupp,
1503 	[OP_OPEN_CONFIRM]	= (nfsd4_dec)nfsd4_decode_notsupp,
1504 	[OP_OPEN_DOWNGRADE]	= (nfsd4_dec)nfsd4_decode_open_downgrade,
1505 	[OP_PUTFH]		= (nfsd4_dec)nfsd4_decode_putfh,
1506 	[OP_PUTPUBFH]		= (nfsd4_dec)nfsd4_decode_notsupp,
1507 	[OP_PUTROOTFH]		= (nfsd4_dec)nfsd4_decode_noop,
1508 	[OP_READ]		= (nfsd4_dec)nfsd4_decode_read,
1509 	[OP_READDIR]		= (nfsd4_dec)nfsd4_decode_readdir,
1510 	[OP_READLINK]		= (nfsd4_dec)nfsd4_decode_noop,
1511 	[OP_REMOVE]		= (nfsd4_dec)nfsd4_decode_remove,
1512 	[OP_RENAME]		= (nfsd4_dec)nfsd4_decode_rename,
1513 	[OP_RENEW]		= (nfsd4_dec)nfsd4_decode_notsupp,
1514 	[OP_RESTOREFH]		= (nfsd4_dec)nfsd4_decode_noop,
1515 	[OP_SAVEFH]		= (nfsd4_dec)nfsd4_decode_noop,
1516 	[OP_SECINFO]		= (nfsd4_dec)nfsd4_decode_secinfo,
1517 	[OP_SETATTR]		= (nfsd4_dec)nfsd4_decode_setattr,
1518 	[OP_SETCLIENTID]	= (nfsd4_dec)nfsd4_decode_notsupp,
1519 	[OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1520 	[OP_VERIFY]		= (nfsd4_dec)nfsd4_decode_verify,
1521 	[OP_WRITE]		= (nfsd4_dec)nfsd4_decode_write,
1522 	[OP_RELEASE_LOCKOWNER]	= (nfsd4_dec)nfsd4_decode_notsupp,
1523 
1524 	/* new operations for NFSv4.1 */
1525 	[OP_BACKCHANNEL_CTL]	= (nfsd4_dec)nfsd4_decode_notsupp,
1526 	[OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1527 	[OP_EXCHANGE_ID]	= (nfsd4_dec)nfsd4_decode_exchange_id,
1528 	[OP_CREATE_SESSION]	= (nfsd4_dec)nfsd4_decode_create_session,
1529 	[OP_DESTROY_SESSION]	= (nfsd4_dec)nfsd4_decode_destroy_session,
1530 	[OP_FREE_STATEID]	= (nfsd4_dec)nfsd4_decode_free_stateid,
1531 	[OP_GET_DIR_DELEGATION]	= (nfsd4_dec)nfsd4_decode_notsupp,
1532 	[OP_GETDEVICEINFO]	= (nfsd4_dec)nfsd4_decode_notsupp,
1533 	[OP_GETDEVICELIST]	= (nfsd4_dec)nfsd4_decode_notsupp,
1534 	[OP_LAYOUTCOMMIT]	= (nfsd4_dec)nfsd4_decode_notsupp,
1535 	[OP_LAYOUTGET]		= (nfsd4_dec)nfsd4_decode_notsupp,
1536 	[OP_LAYOUTRETURN]	= (nfsd4_dec)nfsd4_decode_notsupp,
1537 	[OP_SECINFO_NO_NAME]	= (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1538 	[OP_SEQUENCE]		= (nfsd4_dec)nfsd4_decode_sequence,
1539 	[OP_SET_SSV]		= (nfsd4_dec)nfsd4_decode_notsupp,
1540 	[OP_TEST_STATEID]	= (nfsd4_dec)nfsd4_decode_test_stateid,
1541 	[OP_WANT_DELEGATION]	= (nfsd4_dec)nfsd4_decode_notsupp,
1542 	[OP_DESTROY_CLIENTID]	= (nfsd4_dec)nfsd4_decode_destroy_clientid,
1543 	[OP_RECLAIM_COMPLETE]	= (nfsd4_dec)nfsd4_decode_reclaim_complete,
1544 };
1545 
1546 struct nfsd4_minorversion_ops {
1547 	nfsd4_dec *decoders;
1548 	int nops;
1549 };
1550 
1551 static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
1552 	[0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
1553 	[1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1554 };
1555 
1556 static __be32
nfsd4_decode_compound(struct nfsd4_compoundargs * argp)1557 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1558 {
1559 	DECODE_HEAD;
1560 	struct nfsd4_op *op;
1561 	struct nfsd4_minorversion_ops *ops;
1562 	bool cachethis = false;
1563 	int i;
1564 
1565 	/*
1566 	 * XXX: According to spec, we should check the tag
1567 	 * for UTF-8 compliance.  I'm postponing this for
1568 	 * now because it seems that some clients do use
1569 	 * binary tags.
1570 	 */
1571 	READ_BUF(4);
1572 	READ32(argp->taglen);
1573 	READ_BUF(argp->taglen + 8);
1574 	SAVEMEM(argp->tag, argp->taglen);
1575 	READ32(argp->minorversion);
1576 	READ32(argp->opcnt);
1577 
1578 	if (argp->taglen > NFSD4_MAX_TAGLEN)
1579 		goto xdr_error;
1580 	if (argp->opcnt > 100)
1581 		goto xdr_error;
1582 
1583 	if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1584 		argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1585 		if (!argp->ops) {
1586 			argp->ops = argp->iops;
1587 			dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1588 			goto xdr_error;
1589 		}
1590 	}
1591 
1592 	if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
1593 		argp->opcnt = 0;
1594 
1595 	ops = &nfsd4_minorversion[argp->minorversion];
1596 	for (i = 0; i < argp->opcnt; i++) {
1597 		op = &argp->ops[i];
1598 		op->replay = NULL;
1599 
1600 		/*
1601 		 * We can't use READ_BUF() here because we need to handle
1602 		 * a missing opcode as an OP_WRITE + 1. So we need to check
1603 		 * to see if we're truly at the end of our buffer or if there
1604 		 * is another page we need to flip to.
1605 		 */
1606 
1607 		if (argp->p == argp->end) {
1608 			if (argp->pagelen < 4) {
1609 				/* There isn't an opcode still on the wire */
1610 				op->opnum = OP_WRITE + 1;
1611 				op->status = nfserr_bad_xdr;
1612 				argp->opcnt = i+1;
1613 				break;
1614 			}
1615 
1616 			/*
1617 			 * False alarm. We just hit a page boundary, but there
1618 			 * is still data available.  Move pointer across page
1619 			 * boundary.  *snip from READ_BUF*
1620 			 */
1621 			argp->p = page_address(argp->pagelist[0]);
1622 			argp->pagelist++;
1623 			if (argp->pagelen < PAGE_SIZE) {
1624 				argp->end = argp->p + (argp->pagelen>>2);
1625 				argp->pagelen = 0;
1626 			} else {
1627 				argp->end = argp->p + (PAGE_SIZE>>2);
1628 				argp->pagelen -= PAGE_SIZE;
1629 			}
1630 		}
1631 		op->opnum = ntohl(*argp->p++);
1632 
1633 		if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
1634 			op->status = ops->decoders[op->opnum](argp, &op->u);
1635 		else {
1636 			op->opnum = OP_ILLEGAL;
1637 			op->status = nfserr_op_illegal;
1638 		}
1639 
1640 		if (op->status) {
1641 			argp->opcnt = i+1;
1642 			break;
1643 		}
1644 		/*
1645 		 * We'll try to cache the result in the DRC if any one
1646 		 * op in the compound wants to be cached:
1647 		 */
1648 		cachethis |= nfsd4_cache_this_op(op);
1649 	}
1650 	/* Sessions make the DRC unnecessary: */
1651 	if (argp->minorversion)
1652 		cachethis = false;
1653 	argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1654 
1655 	DECODE_TAIL;
1656 }
1657 
1658 #define WRITE32(n)               *p++ = htonl(n)
1659 #define WRITE64(n)               do {				\
1660 	*p++ = htonl((u32)((n) >> 32));				\
1661 	*p++ = htonl((u32)(n));					\
1662 } while (0)
1663 #define WRITEMEM(ptr,nbytes)     do { if (nbytes > 0) {		\
1664 	*(p + XDR_QUADLEN(nbytes) -1) = 0;                      \
1665 	memcpy(p, ptr, nbytes);					\
1666 	p += XDR_QUADLEN(nbytes);				\
1667 }} while (0)
1668 
write32(__be32 ** p,u32 n)1669 static void write32(__be32 **p, u32 n)
1670 {
1671 	*(*p)++ = n;
1672 }
1673 
write64(__be32 ** p,u64 n)1674 static void write64(__be32 **p, u64 n)
1675 {
1676 	write32(p, (u32)(n >> 32));
1677 	write32(p, (u32)n);
1678 }
1679 
write_change(__be32 ** p,struct kstat * stat,struct inode * inode)1680 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1681 {
1682 	if (IS_I_VERSION(inode)) {
1683 		write64(p, inode->i_version);
1684 	} else {
1685 		write32(p, stat->ctime.tv_sec);
1686 		write32(p, stat->ctime.tv_nsec);
1687 	}
1688 }
1689 
write_cinfo(__be32 ** p,struct nfsd4_change_info * c)1690 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1691 {
1692 	write32(p, c->atomic);
1693 	if (c->change_supported) {
1694 		write64(p, c->before_change);
1695 		write64(p, c->after_change);
1696 	} else {
1697 		write32(p, c->before_ctime_sec);
1698 		write32(p, c->before_ctime_nsec);
1699 		write32(p, c->after_ctime_sec);
1700 		write32(p, c->after_ctime_nsec);
1701 	}
1702 }
1703 
1704 #define RESERVE_SPACE(nbytes)	do {				\
1705 	p = resp->p;						\
1706 	BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end);		\
1707 } while (0)
1708 #define ADJUST_ARGS()		resp->p = p
1709 
1710 /*
1711  * Header routine to setup seqid operation replay cache
1712  */
1713 #define ENCODE_SEQID_OP_HEAD					\
1714 	__be32 *save;						\
1715 								\
1716 	save = resp->p;
1717 
1718 /*
1719  * Routine for encoding the result of a "seqid-mutating" NFSv4 operation.  This
1720  * is where sequence id's are incremented, and the replay cache is filled.
1721  * Note that we increment sequence id's here, at the last moment, so we're sure
1722  * we know whether the error to be returned is a sequence id mutating error.
1723  */
1724 
encode_seqid_op_tail(struct nfsd4_compoundres * resp,__be32 * save,__be32 nfserr)1725 static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1726 {
1727 	struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1728 
1729 	if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1730 		stateowner->so_seqid++;
1731 		stateowner->so_replay.rp_status = nfserr;
1732 		stateowner->so_replay.rp_buflen =
1733 			  (char *)resp->p - (char *)save;
1734 		memcpy(stateowner->so_replay.rp_buf, save,
1735 			stateowner->so_replay.rp_buflen);
1736 		nfsd4_purge_closed_stateid(stateowner);
1737 	}
1738 }
1739 
1740 /* Encode as an array of strings the string given with components
1741  * separated @sep.
1742  */
nfsd4_encode_components(char sep,char * components,__be32 ** pp,int * buflen)1743 static __be32 nfsd4_encode_components(char sep, char *components,
1744 				   __be32 **pp, int *buflen)
1745 {
1746 	__be32 *p = *pp;
1747 	__be32 *countp = p;
1748 	int strlen, count=0;
1749 	char *str, *end;
1750 
1751 	dprintk("nfsd4_encode_components(%s)\n", components);
1752 	if ((*buflen -= 4) < 0)
1753 		return nfserr_resource;
1754 	WRITE32(0); /* We will fill this in with @count later */
1755 	end = str = components;
1756 	while (*end) {
1757 		for (; *end && (*end != sep); end++)
1758 			; /* Point to end of component */
1759 		strlen = end - str;
1760 		if (strlen) {
1761 			if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1762 				return nfserr_resource;
1763 			WRITE32(strlen);
1764 			WRITEMEM(str, strlen);
1765 			count++;
1766 		}
1767 		else
1768 			end++;
1769 		str = end;
1770 	}
1771 	*pp = p;
1772 	p = countp;
1773 	WRITE32(count);
1774 	return 0;
1775 }
1776 
1777 /*
1778  * encode a location element of a fs_locations structure
1779  */
nfsd4_encode_fs_location4(struct nfsd4_fs_location * location,__be32 ** pp,int * buflen)1780 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1781 				    __be32 **pp, int *buflen)
1782 {
1783 	__be32 status;
1784 	__be32 *p = *pp;
1785 
1786 	status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1787 	if (status)
1788 		return status;
1789 	status = nfsd4_encode_components('/', location->path, &p, buflen);
1790 	if (status)
1791 		return status;
1792 	*pp = p;
1793 	return 0;
1794 }
1795 
1796 /*
1797  * Encode a path in RFC3530 'pathname4' format
1798  */
nfsd4_encode_path(const struct path * root,const struct path * path,__be32 ** pp,int * buflen)1799 static __be32 nfsd4_encode_path(const struct path *root,
1800 		const struct path *path, __be32 **pp, int *buflen)
1801 {
1802 	struct path cur = {
1803 		.mnt = path->mnt,
1804 		.dentry = path->dentry,
1805 	};
1806 	__be32 *p = *pp;
1807 	struct dentry **components = NULL;
1808 	unsigned int ncomponents = 0;
1809 	__be32 err = nfserr_jukebox;
1810 
1811 	dprintk("nfsd4_encode_components(");
1812 
1813 	path_get(&cur);
1814 	/* First walk the path up to the nfsd root, and store the
1815 	 * dentries/path components in an array.
1816 	 */
1817 	for (;;) {
1818 		if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1819 			break;
1820 		if (cur.dentry == cur.mnt->mnt_root) {
1821 			if (follow_up(&cur))
1822 				continue;
1823 			goto out_free;
1824 		}
1825 		if ((ncomponents & 15) == 0) {
1826 			struct dentry **new;
1827 			new = krealloc(components,
1828 					sizeof(*new) * (ncomponents + 16),
1829 					GFP_KERNEL);
1830 			if (!new)
1831 				goto out_free;
1832 			components = new;
1833 		}
1834 		components[ncomponents++] = cur.dentry;
1835 		cur.dentry = dget_parent(cur.dentry);
1836 	}
1837 
1838 	*buflen -= 4;
1839 	if (*buflen < 0)
1840 		goto out_free;
1841 	WRITE32(ncomponents);
1842 
1843 	while (ncomponents) {
1844 		struct dentry *dentry = components[ncomponents - 1];
1845 		unsigned int len = dentry->d_name.len;
1846 
1847 		*buflen -= 4 + (XDR_QUADLEN(len) << 2);
1848 		if (*buflen < 0)
1849 			goto out_free;
1850 		WRITE32(len);
1851 		WRITEMEM(dentry->d_name.name, len);
1852 		dprintk("/%s", dentry->d_name.name);
1853 		dput(dentry);
1854 		ncomponents--;
1855 	}
1856 
1857 	*pp = p;
1858 	err = 0;
1859 out_free:
1860 	dprintk(")\n");
1861 	while (ncomponents)
1862 		dput(components[--ncomponents]);
1863 	kfree(components);
1864 	path_put(&cur);
1865 	return err;
1866 }
1867 
nfsd4_encode_fsloc_fsroot(struct svc_rqst * rqstp,const struct path * path,__be32 ** pp,int * buflen)1868 static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1869 		const struct path *path, __be32 **pp, int *buflen)
1870 {
1871 	struct svc_export *exp_ps;
1872 	__be32 res;
1873 
1874 	exp_ps = rqst_find_fsidzero_export(rqstp);
1875 	if (IS_ERR(exp_ps))
1876 		return nfserrno(PTR_ERR(exp_ps));
1877 	res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1878 	exp_put(exp_ps);
1879 	return res;
1880 }
1881 
1882 /*
1883  *  encode a fs_locations structure
1884  */
nfsd4_encode_fs_locations(struct svc_rqst * rqstp,struct svc_export * exp,__be32 ** pp,int * buflen)1885 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1886 				     struct svc_export *exp,
1887 				     __be32 **pp, int *buflen)
1888 {
1889 	__be32 status;
1890 	int i;
1891 	__be32 *p = *pp;
1892 	struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1893 
1894 	status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
1895 	if (status)
1896 		return status;
1897 	if ((*buflen -= 4) < 0)
1898 		return nfserr_resource;
1899 	WRITE32(fslocs->locations_count);
1900 	for (i=0; i<fslocs->locations_count; i++) {
1901 		status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1902 						   &p, buflen);
1903 		if (status)
1904 			return status;
1905 	}
1906 	*pp = p;
1907 	return 0;
1908 }
1909 
nfs4_file_type(umode_t mode)1910 static u32 nfs4_file_type(umode_t mode)
1911 {
1912 	switch (mode & S_IFMT) {
1913 	case S_IFIFO:	return NF4FIFO;
1914 	case S_IFCHR:	return NF4CHR;
1915 	case S_IFDIR:	return NF4DIR;
1916 	case S_IFBLK:	return NF4BLK;
1917 	case S_IFLNK:	return NF4LNK;
1918 	case S_IFREG:	return NF4REG;
1919 	case S_IFSOCK:	return NF4SOCK;
1920 	default:	return NF4BAD;
1921 	};
1922 }
1923 
1924 static __be32
nfsd4_encode_name(struct svc_rqst * rqstp,int whotype,uid_t id,int group,__be32 ** p,int * buflen)1925 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
1926 			__be32 **p, int *buflen)
1927 {
1928 	int status;
1929 
1930 	if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1931 		return nfserr_resource;
1932 	if (whotype != NFS4_ACL_WHO_NAMED)
1933 		status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1934 	else if (group)
1935 		status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1936 	else
1937 		status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1938 	if (status < 0)
1939 		return nfserrno(status);
1940 	*p = xdr_encode_opaque(*p, NULL, status);
1941 	*buflen -= (XDR_QUADLEN(status) << 2) + 4;
1942 	BUG_ON(*buflen < 0);
1943 	return 0;
1944 }
1945 
1946 static inline __be32
nfsd4_encode_user(struct svc_rqst * rqstp,uid_t uid,__be32 ** p,int * buflen)1947 nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
1948 {
1949 	return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1950 }
1951 
1952 static inline __be32
nfsd4_encode_group(struct svc_rqst * rqstp,uid_t gid,__be32 ** p,int * buflen)1953 nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
1954 {
1955 	return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1956 }
1957 
1958 static inline __be32
nfsd4_encode_aclname(struct svc_rqst * rqstp,int whotype,uid_t id,int group,__be32 ** p,int * buflen)1959 nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
1960 		__be32 **p, int *buflen)
1961 {
1962 	return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1963 }
1964 
1965 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1966 			      FATTR4_WORD0_RDATTR_ERROR)
1967 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1968 
fattr_handle_absent_fs(u32 * bmval0,u32 * bmval1,u32 * rdattr_err)1969 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
1970 {
1971 	/* As per referral draft:  */
1972 	if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1973 	    *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1974 		if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1975 	            *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1976 			*rdattr_err = NFSERR_MOVED;
1977 		else
1978 			return nfserr_moved;
1979 	}
1980 	*bmval0 &= WORD0_ABSENT_FS_ATTRS;
1981 	*bmval1 &= WORD1_ABSENT_FS_ATTRS;
1982 	return 0;
1983 }
1984 
1985 /*
1986  * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1987  * ourselves.
1988  *
1989  * @countp is the buffer size in _words_; upon successful return this becomes
1990  * replaced with the number of words written.
1991  */
1992 __be32
nfsd4_encode_fattr(struct svc_fh * fhp,struct svc_export * exp,struct dentry * dentry,__be32 * buffer,int * countp,u32 * bmval,struct svc_rqst * rqstp,int ignore_crossmnt)1993 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
1994 		struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
1995 		struct svc_rqst *rqstp, int ignore_crossmnt)
1996 {
1997 	u32 bmval0 = bmval[0];
1998 	u32 bmval1 = bmval[1];
1999 	u32 bmval2 = bmval[2];
2000 	struct kstat stat;
2001 	struct svc_fh tempfh;
2002 	struct kstatfs statfs;
2003 	int buflen = *countp << 2;
2004 	__be32 *attrlenp;
2005 	u32 dummy;
2006 	u64 dummy64;
2007 	u32 rdattr_err = 0;
2008 	__be32 *p = buffer;
2009 	__be32 status;
2010 	int err;
2011 	int aclsupport = 0;
2012 	struct nfs4_acl *acl = NULL;
2013 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2014 	u32 minorversion = resp->cstate.minorversion;
2015 	struct path path = {
2016 		.mnt	= exp->ex_path.mnt,
2017 		.dentry	= dentry,
2018 	};
2019 
2020 	BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2021 	BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2022 	BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2023 	BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
2024 
2025 	if (exp->ex_fslocs.migrated) {
2026 		BUG_ON(bmval[2]);
2027 		status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2028 		if (status)
2029 			goto out;
2030 	}
2031 
2032 	err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
2033 	if (err)
2034 		goto out_nfserr;
2035 	if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2036 			FATTR4_WORD0_MAXNAME)) ||
2037 	    (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2038 		       FATTR4_WORD1_SPACE_TOTAL))) {
2039 		err = vfs_statfs(&path, &statfs);
2040 		if (err)
2041 			goto out_nfserr;
2042 	}
2043 	if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2044 		fh_init(&tempfh, NFS4_FHSIZE);
2045 		status = fh_compose(&tempfh, exp, dentry, NULL);
2046 		if (status)
2047 			goto out;
2048 		fhp = &tempfh;
2049 	}
2050 	if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2051 			| FATTR4_WORD0_SUPPORTED_ATTRS)) {
2052 		err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2053 		aclsupport = (err == 0);
2054 		if (bmval0 & FATTR4_WORD0_ACL) {
2055 			if (err == -EOPNOTSUPP)
2056 				bmval0 &= ~FATTR4_WORD0_ACL;
2057 			else if (err == -EINVAL) {
2058 				status = nfserr_attrnotsupp;
2059 				goto out;
2060 			} else if (err != 0)
2061 				goto out_nfserr;
2062 		}
2063 	}
2064 
2065 	if (bmval2) {
2066 		if ((buflen -= 16) < 0)
2067 			goto out_resource;
2068 		WRITE32(3);
2069 		WRITE32(bmval0);
2070 		WRITE32(bmval1);
2071 		WRITE32(bmval2);
2072 	} else if (bmval1) {
2073 		if ((buflen -= 12) < 0)
2074 			goto out_resource;
2075 		WRITE32(2);
2076 		WRITE32(bmval0);
2077 		WRITE32(bmval1);
2078 	} else {
2079 		if ((buflen -= 8) < 0)
2080 			goto out_resource;
2081 		WRITE32(1);
2082 		WRITE32(bmval0);
2083 	}
2084 	attrlenp = p++;                /* to be backfilled later */
2085 
2086 	if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2087 		u32 word0 = nfsd_suppattrs0(minorversion);
2088 		u32 word1 = nfsd_suppattrs1(minorversion);
2089 		u32 word2 = nfsd_suppattrs2(minorversion);
2090 
2091 		if (!aclsupport)
2092 			word0 &= ~FATTR4_WORD0_ACL;
2093 		if (!word2) {
2094 			if ((buflen -= 12) < 0)
2095 				goto out_resource;
2096 			WRITE32(2);
2097 			WRITE32(word0);
2098 			WRITE32(word1);
2099 		} else {
2100 			if ((buflen -= 16) < 0)
2101 				goto out_resource;
2102 			WRITE32(3);
2103 			WRITE32(word0);
2104 			WRITE32(word1);
2105 			WRITE32(word2);
2106 		}
2107 	}
2108 	if (bmval0 & FATTR4_WORD0_TYPE) {
2109 		if ((buflen -= 4) < 0)
2110 			goto out_resource;
2111 		dummy = nfs4_file_type(stat.mode);
2112 		if (dummy == NF4BAD)
2113 			goto out_serverfault;
2114 		WRITE32(dummy);
2115 	}
2116 	if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2117 		if ((buflen -= 4) < 0)
2118 			goto out_resource;
2119 		if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2120 			WRITE32(NFS4_FH_PERSISTENT);
2121 		else
2122 			WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
2123 	}
2124 	if (bmval0 & FATTR4_WORD0_CHANGE) {
2125 		if ((buflen -= 8) < 0)
2126 			goto out_resource;
2127 		write_change(&p, &stat, dentry->d_inode);
2128 	}
2129 	if (bmval0 & FATTR4_WORD0_SIZE) {
2130 		if ((buflen -= 8) < 0)
2131 			goto out_resource;
2132 		WRITE64(stat.size);
2133 	}
2134 	if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2135 		if ((buflen -= 4) < 0)
2136 			goto out_resource;
2137 		WRITE32(1);
2138 	}
2139 	if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2140 		if ((buflen -= 4) < 0)
2141 			goto out_resource;
2142 		WRITE32(1);
2143 	}
2144 	if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2145 		if ((buflen -= 4) < 0)
2146 			goto out_resource;
2147 		WRITE32(0);
2148 	}
2149 	if (bmval0 & FATTR4_WORD0_FSID) {
2150 		if ((buflen -= 16) < 0)
2151 			goto out_resource;
2152 		if (exp->ex_fslocs.migrated) {
2153 			WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2154 			WRITE64(NFS4_REFERRAL_FSID_MINOR);
2155 		} else switch(fsid_source(fhp)) {
2156 		case FSIDSOURCE_FSID:
2157 			WRITE64((u64)exp->ex_fsid);
2158 			WRITE64((u64)0);
2159 			break;
2160 		case FSIDSOURCE_DEV:
2161 			WRITE32(0);
2162 			WRITE32(MAJOR(stat.dev));
2163 			WRITE32(0);
2164 			WRITE32(MINOR(stat.dev));
2165 			break;
2166 		case FSIDSOURCE_UUID:
2167 			WRITEMEM(exp->ex_uuid, 16);
2168 			break;
2169 		}
2170 	}
2171 	if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2172 		if ((buflen -= 4) < 0)
2173 			goto out_resource;
2174 		WRITE32(0);
2175 	}
2176 	if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2177 		if ((buflen -= 4) < 0)
2178 			goto out_resource;
2179 		WRITE32(nfsd4_lease);
2180 	}
2181 	if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2182 		if ((buflen -= 4) < 0)
2183 			goto out_resource;
2184 		WRITE32(rdattr_err);
2185 	}
2186 	if (bmval0 & FATTR4_WORD0_ACL) {
2187 		struct nfs4_ace *ace;
2188 
2189 		if (acl == NULL) {
2190 			if ((buflen -= 4) < 0)
2191 				goto out_resource;
2192 
2193 			WRITE32(0);
2194 			goto out_acl;
2195 		}
2196 		if ((buflen -= 4) < 0)
2197 			goto out_resource;
2198 		WRITE32(acl->naces);
2199 
2200 		for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2201 			if ((buflen -= 4*3) < 0)
2202 				goto out_resource;
2203 			WRITE32(ace->type);
2204 			WRITE32(ace->flag);
2205 			WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2206 			status = nfsd4_encode_aclname(rqstp, ace->whotype,
2207 				ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2208 				&p, &buflen);
2209 			if (status == nfserr_resource)
2210 				goto out_resource;
2211 			if (status)
2212 				goto out;
2213 		}
2214 	}
2215 out_acl:
2216 	if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2217 		if ((buflen -= 4) < 0)
2218 			goto out_resource;
2219 		WRITE32(aclsupport ?
2220 			ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2221 	}
2222 	if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2223 		if ((buflen -= 4) < 0)
2224 			goto out_resource;
2225 		WRITE32(1);
2226 	}
2227 	if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2228 		if ((buflen -= 4) < 0)
2229 			goto out_resource;
2230 		WRITE32(0);
2231 	}
2232 	if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2233 		if ((buflen -= 4) < 0)
2234 			goto out_resource;
2235 		WRITE32(1);
2236 	}
2237 	if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2238 		if ((buflen -= 4) < 0)
2239 			goto out_resource;
2240 		WRITE32(1);
2241 	}
2242 	if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2243 		buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2244 		if (buflen < 0)
2245 			goto out_resource;
2246 		WRITE32(fhp->fh_handle.fh_size);
2247 		WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2248 	}
2249 	if (bmval0 & FATTR4_WORD0_FILEID) {
2250 		if ((buflen -= 8) < 0)
2251 			goto out_resource;
2252 		WRITE64(stat.ino);
2253 	}
2254 	if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2255 		if ((buflen -= 8) < 0)
2256 			goto out_resource;
2257 		WRITE64((u64) statfs.f_ffree);
2258 	}
2259 	if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2260 		if ((buflen -= 8) < 0)
2261 			goto out_resource;
2262 		WRITE64((u64) statfs.f_ffree);
2263 	}
2264 	if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2265 		if ((buflen -= 8) < 0)
2266 			goto out_resource;
2267 		WRITE64((u64) statfs.f_files);
2268 	}
2269 	if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2270 		status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2271 		if (status == nfserr_resource)
2272 			goto out_resource;
2273 		if (status)
2274 			goto out;
2275 	}
2276 	if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2277 		if ((buflen -= 4) < 0)
2278 			goto out_resource;
2279 		WRITE32(1);
2280 	}
2281 	if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2282 		if ((buflen -= 8) < 0)
2283 			goto out_resource;
2284 		WRITE64(~(u64)0);
2285 	}
2286 	if (bmval0 & FATTR4_WORD0_MAXLINK) {
2287 		if ((buflen -= 4) < 0)
2288 			goto out_resource;
2289 		WRITE32(255);
2290 	}
2291 	if (bmval0 & FATTR4_WORD0_MAXNAME) {
2292 		if ((buflen -= 4) < 0)
2293 			goto out_resource;
2294 		WRITE32(statfs.f_namelen);
2295 	}
2296 	if (bmval0 & FATTR4_WORD0_MAXREAD) {
2297 		if ((buflen -= 8) < 0)
2298 			goto out_resource;
2299 		WRITE64((u64) svc_max_payload(rqstp));
2300 	}
2301 	if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2302 		if ((buflen -= 8) < 0)
2303 			goto out_resource;
2304 		WRITE64((u64) svc_max_payload(rqstp));
2305 	}
2306 	if (bmval1 & FATTR4_WORD1_MODE) {
2307 		if ((buflen -= 4) < 0)
2308 			goto out_resource;
2309 		WRITE32(stat.mode & S_IALLUGO);
2310 	}
2311 	if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2312 		if ((buflen -= 4) < 0)
2313 			goto out_resource;
2314 		WRITE32(1);
2315 	}
2316 	if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2317 		if ((buflen -= 4) < 0)
2318 			goto out_resource;
2319 		WRITE32(stat.nlink);
2320 	}
2321 	if (bmval1 & FATTR4_WORD1_OWNER) {
2322 		status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2323 		if (status == nfserr_resource)
2324 			goto out_resource;
2325 		if (status)
2326 			goto out;
2327 	}
2328 	if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2329 		status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2330 		if (status == nfserr_resource)
2331 			goto out_resource;
2332 		if (status)
2333 			goto out;
2334 	}
2335 	if (bmval1 & FATTR4_WORD1_RAWDEV) {
2336 		if ((buflen -= 8) < 0)
2337 			goto out_resource;
2338 		WRITE32((u32) MAJOR(stat.rdev));
2339 		WRITE32((u32) MINOR(stat.rdev));
2340 	}
2341 	if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2342 		if ((buflen -= 8) < 0)
2343 			goto out_resource;
2344 		dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2345 		WRITE64(dummy64);
2346 	}
2347 	if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2348 		if ((buflen -= 8) < 0)
2349 			goto out_resource;
2350 		dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2351 		WRITE64(dummy64);
2352 	}
2353 	if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2354 		if ((buflen -= 8) < 0)
2355 			goto out_resource;
2356 		dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2357 		WRITE64(dummy64);
2358 	}
2359 	if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2360 		if ((buflen -= 8) < 0)
2361 			goto out_resource;
2362 		dummy64 = (u64)stat.blocks << 9;
2363 		WRITE64(dummy64);
2364 	}
2365 	if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2366 		if ((buflen -= 12) < 0)
2367 			goto out_resource;
2368 		WRITE64((s64)stat.atime.tv_sec);
2369 		WRITE32(stat.atime.tv_nsec);
2370 	}
2371 	if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2372 		if ((buflen -= 12) < 0)
2373 			goto out_resource;
2374 		WRITE32(0);
2375 		WRITE32(1);
2376 		WRITE32(0);
2377 	}
2378 	if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2379 		if ((buflen -= 12) < 0)
2380 			goto out_resource;
2381 		WRITE64((s64)stat.ctime.tv_sec);
2382 		WRITE32(stat.ctime.tv_nsec);
2383 	}
2384 	if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2385 		if ((buflen -= 12) < 0)
2386 			goto out_resource;
2387 		WRITE64((s64)stat.mtime.tv_sec);
2388 		WRITE32(stat.mtime.tv_nsec);
2389 	}
2390 	if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2391 		if ((buflen -= 8) < 0)
2392                 	goto out_resource;
2393 		/*
2394 		 * Get parent's attributes if not ignoring crossmount
2395 		 * and this is the root of a cross-mounted filesystem.
2396 		 */
2397 		if (ignore_crossmnt == 0 &&
2398 		    dentry == exp->ex_path.mnt->mnt_root) {
2399 			struct path path = exp->ex_path;
2400 			path_get(&path);
2401 			while (follow_up(&path)) {
2402 				if (path.dentry != path.mnt->mnt_root)
2403 					break;
2404 			}
2405 			err = vfs_getattr(path.mnt, path.dentry, &stat);
2406 			path_put(&path);
2407 			if (err)
2408 				goto out_nfserr;
2409 		}
2410 		WRITE64(stat.ino);
2411 	}
2412 	if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2413 		WRITE32(3);
2414 		WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2415 		WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2416 		WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2417 	}
2418 
2419 	*attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2420 	*countp = p - buffer;
2421 	status = nfs_ok;
2422 
2423 out:
2424 	kfree(acl);
2425 	if (fhp == &tempfh)
2426 		fh_put(&tempfh);
2427 	return status;
2428 out_nfserr:
2429 	status = nfserrno(err);
2430 	goto out;
2431 out_resource:
2432 	*countp = 0;
2433 	status = nfserr_resource;
2434 	goto out;
2435 out_serverfault:
2436 	status = nfserr_serverfault;
2437 	goto out;
2438 }
2439 
attributes_need_mount(u32 * bmval)2440 static inline int attributes_need_mount(u32 *bmval)
2441 {
2442 	if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2443 		return 1;
2444 	if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2445 		return 1;
2446 	return 0;
2447 }
2448 
2449 static __be32
nfsd4_encode_dirent_fattr(struct nfsd4_readdir * cd,const char * name,int namlen,__be32 * p,int * buflen)2450 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2451 		const char *name, int namlen, __be32 *p, int *buflen)
2452 {
2453 	struct svc_export *exp = cd->rd_fhp->fh_export;
2454 	struct dentry *dentry;
2455 	__be32 nfserr;
2456 	int ignore_crossmnt = 0;
2457 
2458 	dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2459 	if (IS_ERR(dentry))
2460 		return nfserrno(PTR_ERR(dentry));
2461 	if (!dentry->d_inode) {
2462 		/*
2463 		 * nfsd_buffered_readdir drops the i_mutex between
2464 		 * readdir and calling this callback, leaving a window
2465 		 * where this directory entry could have gone away.
2466 		 */
2467 		dput(dentry);
2468 		return nfserr_noent;
2469 	}
2470 
2471 	exp_get(exp);
2472 	/*
2473 	 * In the case of a mountpoint, the client may be asking for
2474 	 * attributes that are only properties of the underlying filesystem
2475 	 * as opposed to the cross-mounted file system. In such a case,
2476 	 * we will not follow the cross mount and will fill the attribtutes
2477 	 * directly from the mountpoint dentry.
2478 	 */
2479 	if (nfsd_mountpoint(dentry, exp)) {
2480 		int err;
2481 
2482 		if (!(exp->ex_flags & NFSEXP_V4ROOT)
2483 				&& !attributes_need_mount(cd->rd_bmval)) {
2484 			ignore_crossmnt = 1;
2485 			goto out_encode;
2486 		}
2487 		/*
2488 		 * Why the heck aren't we just using nfsd_lookup??
2489 		 * Different "."/".." handling?  Something else?
2490 		 * At least, add a comment here to explain....
2491 		 */
2492 		err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2493 		if (err) {
2494 			nfserr = nfserrno(err);
2495 			goto out_put;
2496 		}
2497 		nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2498 		if (nfserr)
2499 			goto out_put;
2500 
2501 	}
2502 out_encode:
2503 	nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2504 					cd->rd_rqstp, ignore_crossmnt);
2505 out_put:
2506 	dput(dentry);
2507 	exp_put(exp);
2508 	return nfserr;
2509 }
2510 
2511 static __be32 *
nfsd4_encode_rdattr_error(__be32 * p,int buflen,__be32 nfserr)2512 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2513 {
2514 	__be32 *attrlenp;
2515 
2516 	if (buflen < 6)
2517 		return NULL;
2518 	*p++ = htonl(2);
2519 	*p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2520 	*p++ = htonl(0);			 /* bmval1 */
2521 
2522 	attrlenp = p++;
2523 	*p++ = nfserr;       /* no htonl */
2524 	*attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2525 	return p;
2526 }
2527 
2528 static int
nfsd4_encode_dirent(void * ccdv,const char * name,int namlen,loff_t offset,u64 ino,unsigned int d_type)2529 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2530 		    loff_t offset, u64 ino, unsigned int d_type)
2531 {
2532 	struct readdir_cd *ccd = ccdv;
2533 	struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2534 	int buflen;
2535 	__be32 *p = cd->buffer;
2536 	__be32 *cookiep;
2537 	__be32 nfserr = nfserr_toosmall;
2538 
2539 	/* In nfsv4, "." and ".." never make it onto the wire.. */
2540 	if (name && isdotent(name, namlen)) {
2541 		cd->common.err = nfs_ok;
2542 		return 0;
2543 	}
2544 
2545 	if (cd->offset)
2546 		xdr_encode_hyper(cd->offset, (u64) offset);
2547 
2548 	buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2549 	if (buflen < 0)
2550 		goto fail;
2551 
2552 	*p++ = xdr_one;                             /* mark entry present */
2553 	cookiep = p;
2554 	p = xdr_encode_hyper(p, NFS_OFFSET_MAX);    /* offset of next entry */
2555 	p = xdr_encode_array(p, name, namlen);      /* name length & name */
2556 
2557 	nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2558 	switch (nfserr) {
2559 	case nfs_ok:
2560 		p += buflen;
2561 		break;
2562 	case nfserr_resource:
2563 		nfserr = nfserr_toosmall;
2564 		goto fail;
2565 	case nfserr_noent:
2566 		goto skip_entry;
2567 	default:
2568 		/*
2569 		 * If the client requested the RDATTR_ERROR attribute,
2570 		 * we stuff the error code into this attribute
2571 		 * and continue.  If this attribute was not requested,
2572 		 * then in accordance with the spec, we fail the
2573 		 * entire READDIR operation(!)
2574 		 */
2575 		if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2576 			goto fail;
2577 		p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2578 		if (p == NULL) {
2579 			nfserr = nfserr_toosmall;
2580 			goto fail;
2581 		}
2582 	}
2583 	cd->buflen -= (p - cd->buffer);
2584 	cd->buffer = p;
2585 	cd->offset = cookiep;
2586 skip_entry:
2587 	cd->common.err = nfs_ok;
2588 	return 0;
2589 fail:
2590 	cd->common.err = nfserr;
2591 	return -EINVAL;
2592 }
2593 
2594 static void
nfsd4_encode_stateid(struct nfsd4_compoundres * resp,stateid_t * sid)2595 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2596 {
2597 	__be32 *p;
2598 
2599 	RESERVE_SPACE(sizeof(stateid_t));
2600 	WRITE32(sid->si_generation);
2601 	WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2602 	ADJUST_ARGS();
2603 }
2604 
2605 static __be32
nfsd4_encode_access(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_access * access)2606 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2607 {
2608 	__be32 *p;
2609 
2610 	if (!nfserr) {
2611 		RESERVE_SPACE(8);
2612 		WRITE32(access->ac_supported);
2613 		WRITE32(access->ac_resp_access);
2614 		ADJUST_ARGS();
2615 	}
2616 	return nfserr;
2617 }
2618 
nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_bind_conn_to_session * bcts)2619 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2620 {
2621 	__be32 *p;
2622 
2623 	if (!nfserr) {
2624 		RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2625 		WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2626 		WRITE32(bcts->dir);
2627 		/* XXX: ? */
2628 		WRITE32(0);
2629 		ADJUST_ARGS();
2630 	}
2631 	return nfserr;
2632 }
2633 
2634 static __be32
nfsd4_encode_close(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_close * close)2635 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2636 {
2637 	ENCODE_SEQID_OP_HEAD;
2638 
2639 	if (!nfserr)
2640 		nfsd4_encode_stateid(resp, &close->cl_stateid);
2641 
2642 	encode_seqid_op_tail(resp, save, nfserr);
2643 	return nfserr;
2644 }
2645 
2646 
2647 static __be32
nfsd4_encode_commit(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_commit * commit)2648 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2649 {
2650 	__be32 *p;
2651 
2652 	if (!nfserr) {
2653 		RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2654 		WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
2655 		ADJUST_ARGS();
2656 	}
2657 	return nfserr;
2658 }
2659 
2660 static __be32
nfsd4_encode_create(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_create * create)2661 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2662 {
2663 	__be32 *p;
2664 
2665 	if (!nfserr) {
2666 		RESERVE_SPACE(32);
2667 		write_cinfo(&p, &create->cr_cinfo);
2668 		WRITE32(2);
2669 		WRITE32(create->cr_bmval[0]);
2670 		WRITE32(create->cr_bmval[1]);
2671 		ADJUST_ARGS();
2672 	}
2673 	return nfserr;
2674 }
2675 
2676 static __be32
nfsd4_encode_getattr(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_getattr * getattr)2677 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2678 {
2679 	struct svc_fh *fhp = getattr->ga_fhp;
2680 	int buflen;
2681 
2682 	if (nfserr)
2683 		return nfserr;
2684 
2685 	buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2686 	nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2687 				    resp->p, &buflen, getattr->ga_bmval,
2688 				    resp->rqstp, 0);
2689 	if (!nfserr)
2690 		resp->p += buflen;
2691 	return nfserr;
2692 }
2693 
2694 static __be32
nfsd4_encode_getfh(struct nfsd4_compoundres * resp,__be32 nfserr,struct svc_fh ** fhpp)2695 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2696 {
2697 	struct svc_fh *fhp = *fhpp;
2698 	unsigned int len;
2699 	__be32 *p;
2700 
2701 	if (!nfserr) {
2702 		len = fhp->fh_handle.fh_size;
2703 		RESERVE_SPACE(len + 4);
2704 		WRITE32(len);
2705 		WRITEMEM(&fhp->fh_handle.fh_base, len);
2706 		ADJUST_ARGS();
2707 	}
2708 	return nfserr;
2709 }
2710 
2711 /*
2712 * Including all fields other than the name, a LOCK4denied structure requires
2713 *   8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2714 */
2715 static void
nfsd4_encode_lock_denied(struct nfsd4_compoundres * resp,struct nfsd4_lock_denied * ld)2716 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2717 {
2718 	struct xdr_netobj *conf = &ld->ld_owner;
2719 	__be32 *p;
2720 
2721 	RESERVE_SPACE(32 + XDR_LEN(conf->len));
2722 	WRITE64(ld->ld_start);
2723 	WRITE64(ld->ld_length);
2724 	WRITE32(ld->ld_type);
2725 	if (conf->len) {
2726 		WRITEMEM(&ld->ld_clientid, 8);
2727 		WRITE32(conf->len);
2728 		WRITEMEM(conf->data, conf->len);
2729 		kfree(conf->data);
2730 	}  else {  /* non - nfsv4 lock in conflict, no clientid nor owner */
2731 		WRITE64((u64)0); /* clientid */
2732 		WRITE32(0); /* length of owner name */
2733 	}
2734 	ADJUST_ARGS();
2735 }
2736 
2737 static __be32
nfsd4_encode_lock(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_lock * lock)2738 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2739 {
2740 	ENCODE_SEQID_OP_HEAD;
2741 
2742 	if (!nfserr)
2743 		nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2744 	else if (nfserr == nfserr_denied)
2745 		nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2746 
2747 	encode_seqid_op_tail(resp, save, nfserr);
2748 	return nfserr;
2749 }
2750 
2751 static __be32
nfsd4_encode_lockt(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_lockt * lockt)2752 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2753 {
2754 	if (nfserr == nfserr_denied)
2755 		nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2756 	return nfserr;
2757 }
2758 
2759 static __be32
nfsd4_encode_locku(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_locku * locku)2760 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2761 {
2762 	ENCODE_SEQID_OP_HEAD;
2763 
2764 	if (!nfserr)
2765 		nfsd4_encode_stateid(resp, &locku->lu_stateid);
2766 
2767 	encode_seqid_op_tail(resp, save, nfserr);
2768 	return nfserr;
2769 }
2770 
2771 
2772 static __be32
nfsd4_encode_link(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_link * link)2773 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2774 {
2775 	__be32 *p;
2776 
2777 	if (!nfserr) {
2778 		RESERVE_SPACE(20);
2779 		write_cinfo(&p, &link->li_cinfo);
2780 		ADJUST_ARGS();
2781 	}
2782 	return nfserr;
2783 }
2784 
2785 
2786 static __be32
nfsd4_encode_open(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_open * open)2787 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2788 {
2789 	__be32 *p;
2790 	ENCODE_SEQID_OP_HEAD;
2791 
2792 	if (nfserr)
2793 		goto out;
2794 
2795 	nfsd4_encode_stateid(resp, &open->op_stateid);
2796 	RESERVE_SPACE(40);
2797 	write_cinfo(&p, &open->op_cinfo);
2798 	WRITE32(open->op_rflags);
2799 	WRITE32(2);
2800 	WRITE32(open->op_bmval[0]);
2801 	WRITE32(open->op_bmval[1]);
2802 	WRITE32(open->op_delegate_type);
2803 	ADJUST_ARGS();
2804 
2805 	switch (open->op_delegate_type) {
2806 	case NFS4_OPEN_DELEGATE_NONE:
2807 		break;
2808 	case NFS4_OPEN_DELEGATE_READ:
2809 		nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2810 		RESERVE_SPACE(20);
2811 		WRITE32(open->op_recall);
2812 
2813 		/*
2814 		 * TODO: ACE's in delegations
2815 		 */
2816 		WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2817 		WRITE32(0);
2818 		WRITE32(0);
2819 		WRITE32(0);   /* XXX: is NULL principal ok? */
2820 		ADJUST_ARGS();
2821 		break;
2822 	case NFS4_OPEN_DELEGATE_WRITE:
2823 		nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2824 		RESERVE_SPACE(32);
2825 		WRITE32(0);
2826 
2827 		/*
2828 		 * TODO: space_limit's in delegations
2829 		 */
2830 		WRITE32(NFS4_LIMIT_SIZE);
2831 		WRITE32(~(u32)0);
2832 		WRITE32(~(u32)0);
2833 
2834 		/*
2835 		 * TODO: ACE's in delegations
2836 		 */
2837 		WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2838 		WRITE32(0);
2839 		WRITE32(0);
2840 		WRITE32(0);   /* XXX: is NULL principal ok? */
2841 		ADJUST_ARGS();
2842 		break;
2843 	case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2844 		switch (open->op_why_no_deleg) {
2845 		case WND4_CONTENTION:
2846 		case WND4_RESOURCE:
2847 			RESERVE_SPACE(8);
2848 			WRITE32(open->op_why_no_deleg);
2849 			WRITE32(0);	/* deleg signaling not supported yet */
2850 			break;
2851 		default:
2852 			RESERVE_SPACE(4);
2853 			WRITE32(open->op_why_no_deleg);
2854 		}
2855 		ADJUST_ARGS();
2856 		break;
2857 	default:
2858 		BUG();
2859 	}
2860 	/* XXX save filehandle here */
2861 out:
2862 	encode_seqid_op_tail(resp, save, nfserr);
2863 	return nfserr;
2864 }
2865 
2866 static __be32
nfsd4_encode_open_confirm(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_open_confirm * oc)2867 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2868 {
2869 	ENCODE_SEQID_OP_HEAD;
2870 
2871 	if (!nfserr)
2872 		nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2873 
2874 	encode_seqid_op_tail(resp, save, nfserr);
2875 	return nfserr;
2876 }
2877 
2878 static __be32
nfsd4_encode_open_downgrade(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_open_downgrade * od)2879 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2880 {
2881 	ENCODE_SEQID_OP_HEAD;
2882 
2883 	if (!nfserr)
2884 		nfsd4_encode_stateid(resp, &od->od_stateid);
2885 
2886 	encode_seqid_op_tail(resp, save, nfserr);
2887 	return nfserr;
2888 }
2889 
2890 static __be32
nfsd4_encode_read(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_read * read)2891 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2892 		  struct nfsd4_read *read)
2893 {
2894 	u32 eof;
2895 	int v, pn;
2896 	unsigned long maxcount;
2897 	long len;
2898 	__be32 *p;
2899 
2900 	if (nfserr)
2901 		return nfserr;
2902 	if (resp->xbuf->page_len)
2903 		return nfserr_resource;
2904 
2905 	RESERVE_SPACE(8); /* eof flag and byte count */
2906 
2907 	maxcount = svc_max_payload(resp->rqstp);
2908 	if (maxcount > read->rd_length)
2909 		maxcount = read->rd_length;
2910 
2911 	len = maxcount;
2912 	v = 0;
2913 	while (len > 0) {
2914 		pn = resp->rqstp->rq_resused;
2915 		if (!resp->rqstp->rq_respages[pn]) { /* ran out of pages */
2916 			maxcount -= len;
2917 			break;
2918 		}
2919 		resp->rqstp->rq_vec[v].iov_base =
2920 			page_address(resp->rqstp->rq_respages[pn]);
2921 		resp->rqstp->rq_vec[v].iov_len =
2922 			len < PAGE_SIZE ? len : PAGE_SIZE;
2923 		resp->rqstp->rq_resused++;
2924 		v++;
2925 		len -= PAGE_SIZE;
2926 	}
2927 	read->rd_vlen = v;
2928 
2929 	nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
2930 			read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
2931 			&maxcount);
2932 
2933 	if (nfserr)
2934 		return nfserr;
2935 	eof = (read->rd_offset + maxcount >=
2936 	       read->rd_fhp->fh_dentry->d_inode->i_size);
2937 
2938 	WRITE32(eof);
2939 	WRITE32(maxcount);
2940 	ADJUST_ARGS();
2941 	resp->xbuf->head[0].iov_len = (char*)p
2942 					- (char*)resp->xbuf->head[0].iov_base;
2943 	resp->xbuf->page_len = maxcount;
2944 
2945 	/* Use rest of head for padding and remaining ops: */
2946 	resp->xbuf->tail[0].iov_base = p;
2947 	resp->xbuf->tail[0].iov_len = 0;
2948 	if (maxcount&3) {
2949 		RESERVE_SPACE(4);
2950 		WRITE32(0);
2951 		resp->xbuf->tail[0].iov_base += maxcount&3;
2952 		resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2953 		ADJUST_ARGS();
2954 	}
2955 	return 0;
2956 }
2957 
2958 static __be32
nfsd4_encode_readlink(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_readlink * readlink)2959 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
2960 {
2961 	int maxcount;
2962 	char *page;
2963 	__be32 *p;
2964 
2965 	if (nfserr)
2966 		return nfserr;
2967 	if (resp->xbuf->page_len)
2968 		return nfserr_resource;
2969 	if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
2970 		return nfserr_resource;
2971 
2972 	page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
2973 
2974 	maxcount = PAGE_SIZE;
2975 	RESERVE_SPACE(4);
2976 
2977 	/*
2978 	 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2979 	 * if they would overflow the buffer.  Is this kosher in NFSv4?  If
2980 	 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2981 	 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2982 	 */
2983 	nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2984 	if (nfserr == nfserr_isdir)
2985 		return nfserr_inval;
2986 	if (nfserr)
2987 		return nfserr;
2988 
2989 	WRITE32(maxcount);
2990 	ADJUST_ARGS();
2991 	resp->xbuf->head[0].iov_len = (char*)p
2992 				- (char*)resp->xbuf->head[0].iov_base;
2993 	resp->xbuf->page_len = maxcount;
2994 
2995 	/* Use rest of head for padding and remaining ops: */
2996 	resp->xbuf->tail[0].iov_base = p;
2997 	resp->xbuf->tail[0].iov_len = 0;
2998 	if (maxcount&3) {
2999 		RESERVE_SPACE(4);
3000 		WRITE32(0);
3001 		resp->xbuf->tail[0].iov_base += maxcount&3;
3002 		resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3003 		ADJUST_ARGS();
3004 	}
3005 	return 0;
3006 }
3007 
3008 static __be32
nfsd4_encode_readdir(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_readdir * readdir)3009 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3010 {
3011 	int maxcount;
3012 	loff_t offset;
3013 	__be32 *page, *savep, *tailbase;
3014 	__be32 *p;
3015 
3016 	if (nfserr)
3017 		return nfserr;
3018 	if (resp->xbuf->page_len)
3019 		return nfserr_resource;
3020 	if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
3021 		return nfserr_resource;
3022 
3023 	RESERVE_SPACE(NFS4_VERIFIER_SIZE);
3024 	savep = p;
3025 
3026 	/* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3027 	WRITE32(0);
3028 	WRITE32(0);
3029 	ADJUST_ARGS();
3030 	resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
3031 	tailbase = p;
3032 
3033 	maxcount = PAGE_SIZE;
3034 	if (maxcount > readdir->rd_maxcount)
3035 		maxcount = readdir->rd_maxcount;
3036 
3037 	/*
3038 	 * Convert from bytes to words, account for the two words already
3039 	 * written, make sure to leave two words at the end for the next
3040 	 * pointer and eof field.
3041 	 */
3042 	maxcount = (maxcount >> 2) - 4;
3043 	if (maxcount < 0) {
3044 		nfserr =  nfserr_toosmall;
3045 		goto err_no_verf;
3046 	}
3047 
3048 	page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
3049 	readdir->common.err = 0;
3050 	readdir->buflen = maxcount;
3051 	readdir->buffer = page;
3052 	readdir->offset = NULL;
3053 
3054 	offset = readdir->rd_cookie;
3055 	nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3056 			      &offset,
3057 			      &readdir->common, nfsd4_encode_dirent);
3058 	if (nfserr == nfs_ok &&
3059 	    readdir->common.err == nfserr_toosmall &&
3060 	    readdir->buffer == page)
3061 		nfserr = nfserr_toosmall;
3062 	if (nfserr)
3063 		goto err_no_verf;
3064 
3065 	if (readdir->offset)
3066 		xdr_encode_hyper(readdir->offset, offset);
3067 
3068 	p = readdir->buffer;
3069 	*p++ = 0;	/* no more entries */
3070 	*p++ = htonl(readdir->common.err == nfserr_eof);
3071 	resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3072 		resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
3073 
3074 	/* Use rest of head for padding and remaining ops: */
3075 	resp->xbuf->tail[0].iov_base = tailbase;
3076 	resp->xbuf->tail[0].iov_len = 0;
3077 	resp->p = resp->xbuf->tail[0].iov_base;
3078 	resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
3079 
3080 	return 0;
3081 err_no_verf:
3082 	p = savep;
3083 	ADJUST_ARGS();
3084 	return nfserr;
3085 }
3086 
3087 static __be32
nfsd4_encode_remove(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_remove * remove)3088 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3089 {
3090 	__be32 *p;
3091 
3092 	if (!nfserr) {
3093 		RESERVE_SPACE(20);
3094 		write_cinfo(&p, &remove->rm_cinfo);
3095 		ADJUST_ARGS();
3096 	}
3097 	return nfserr;
3098 }
3099 
3100 static __be32
nfsd4_encode_rename(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_rename * rename)3101 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3102 {
3103 	__be32 *p;
3104 
3105 	if (!nfserr) {
3106 		RESERVE_SPACE(40);
3107 		write_cinfo(&p, &rename->rn_sinfo);
3108 		write_cinfo(&p, &rename->rn_tinfo);
3109 		ADJUST_ARGS();
3110 	}
3111 	return nfserr;
3112 }
3113 
3114 static __be32
nfsd4_do_encode_secinfo(struct nfsd4_compoundres * resp,__be32 nfserr,struct svc_export * exp)3115 nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3116 			 __be32 nfserr,struct svc_export *exp)
3117 {
3118 	int i = 0;
3119 	u32 nflavs;
3120 	struct exp_flavor_info *flavs;
3121 	struct exp_flavor_info def_flavs[2];
3122 	__be32 *p;
3123 
3124 	if (nfserr)
3125 		goto out;
3126 	if (exp->ex_nflavors) {
3127 		flavs = exp->ex_flavors;
3128 		nflavs = exp->ex_nflavors;
3129 	} else { /* Handling of some defaults in absence of real secinfo: */
3130 		flavs = def_flavs;
3131 		if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3132 			nflavs = 2;
3133 			flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3134 			flavs[1].pseudoflavor = RPC_AUTH_NULL;
3135 		} else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3136 			nflavs = 1;
3137 			flavs[0].pseudoflavor
3138 					= svcauth_gss_flavor(exp->ex_client);
3139 		} else {
3140 			nflavs = 1;
3141 			flavs[0].pseudoflavor
3142 					= exp->ex_client->flavour->flavour;
3143 		}
3144 	}
3145 
3146 	RESERVE_SPACE(4);
3147 	WRITE32(nflavs);
3148 	ADJUST_ARGS();
3149 	for (i = 0; i < nflavs; i++) {
3150 		u32 flav = flavs[i].pseudoflavor;
3151 		struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3152 
3153 		if (gm) {
3154 			RESERVE_SPACE(4);
3155 			WRITE32(RPC_AUTH_GSS);
3156 			ADJUST_ARGS();
3157 			RESERVE_SPACE(4 + gm->gm_oid.len);
3158 			WRITE32(gm->gm_oid.len);
3159 			WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3160 			ADJUST_ARGS();
3161 			RESERVE_SPACE(4);
3162 			WRITE32(0); /* qop */
3163 			ADJUST_ARGS();
3164 			RESERVE_SPACE(4);
3165 			WRITE32(gss_pseudoflavor_to_service(gm, flav));
3166 			ADJUST_ARGS();
3167 			gss_mech_put(gm);
3168 		} else {
3169 			RESERVE_SPACE(4);
3170 			WRITE32(flav);
3171 			ADJUST_ARGS();
3172 		}
3173 	}
3174 out:
3175 	if (exp)
3176 		exp_put(exp);
3177 	return nfserr;
3178 }
3179 
3180 static __be32
nfsd4_encode_secinfo(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_secinfo * secinfo)3181 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3182 		     struct nfsd4_secinfo *secinfo)
3183 {
3184 	return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3185 }
3186 
3187 static __be32
nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_secinfo_no_name * secinfo)3188 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3189 		     struct nfsd4_secinfo_no_name *secinfo)
3190 {
3191 	return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3192 }
3193 
3194 /*
3195  * The SETATTR encode routine is special -- it always encodes a bitmap,
3196  * regardless of the error status.
3197  */
3198 static __be32
nfsd4_encode_setattr(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_setattr * setattr)3199 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3200 {
3201 	__be32 *p;
3202 
3203 	RESERVE_SPACE(12);
3204 	if (nfserr) {
3205 		WRITE32(2);
3206 		WRITE32(0);
3207 		WRITE32(0);
3208 	}
3209 	else {
3210 		WRITE32(2);
3211 		WRITE32(setattr->sa_bmval[0]);
3212 		WRITE32(setattr->sa_bmval[1]);
3213 	}
3214 	ADJUST_ARGS();
3215 	return nfserr;
3216 }
3217 
3218 static __be32
nfsd4_encode_setclientid(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_setclientid * scd)3219 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3220 {
3221 	__be32 *p;
3222 
3223 	if (!nfserr) {
3224 		RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
3225 		WRITEMEM(&scd->se_clientid, 8);
3226 		WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
3227 		ADJUST_ARGS();
3228 	}
3229 	else if (nfserr == nfserr_clid_inuse) {
3230 		RESERVE_SPACE(8);
3231 		WRITE32(0);
3232 		WRITE32(0);
3233 		ADJUST_ARGS();
3234 	}
3235 	return nfserr;
3236 }
3237 
3238 static __be32
nfsd4_encode_write(struct nfsd4_compoundres * resp,__be32 nfserr,struct nfsd4_write * write)3239 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3240 {
3241 	__be32 *p;
3242 
3243 	if (!nfserr) {
3244 		RESERVE_SPACE(16);
3245 		WRITE32(write->wr_bytes_written);
3246 		WRITE32(write->wr_how_written);
3247 		WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
3248 		ADJUST_ARGS();
3249 	}
3250 	return nfserr;
3251 }
3252 
3253 static __be32
nfsd4_encode_exchange_id(struct nfsd4_compoundres * resp,int nfserr,struct nfsd4_exchange_id * exid)3254 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
3255 			 struct nfsd4_exchange_id *exid)
3256 {
3257 	__be32 *p;
3258 	char *major_id;
3259 	char *server_scope;
3260 	int major_id_sz;
3261 	int server_scope_sz;
3262 	uint64_t minor_id = 0;
3263 
3264 	if (nfserr)
3265 		return nfserr;
3266 
3267 	major_id = utsname()->nodename;
3268 	major_id_sz = strlen(major_id);
3269 	server_scope = utsname()->nodename;
3270 	server_scope_sz = strlen(server_scope);
3271 
3272 	RESERVE_SPACE(
3273 		8 /* eir_clientid */ +
3274 		4 /* eir_sequenceid */ +
3275 		4 /* eir_flags */ +
3276 		4 /* spr_how (SP4_NONE) */ +
3277 		8 /* so_minor_id */ +
3278 		4 /* so_major_id.len */ +
3279 		(XDR_QUADLEN(major_id_sz) * 4) +
3280 		4 /* eir_server_scope.len */ +
3281 		(XDR_QUADLEN(server_scope_sz) * 4) +
3282 		4 /* eir_server_impl_id.count (0) */);
3283 
3284 	WRITEMEM(&exid->clientid, 8);
3285 	WRITE32(exid->seqid);
3286 	WRITE32(exid->flags);
3287 
3288 	/* state_protect4_r. Currently only support SP4_NONE */
3289 	BUG_ON(exid->spa_how != SP4_NONE);
3290 	WRITE32(exid->spa_how);
3291 
3292 	/* The server_owner struct */
3293 	WRITE64(minor_id);      /* Minor id */
3294 	/* major id */
3295 	WRITE32(major_id_sz);
3296 	WRITEMEM(major_id, major_id_sz);
3297 
3298 	/* Server scope */
3299 	WRITE32(server_scope_sz);
3300 	WRITEMEM(server_scope, server_scope_sz);
3301 
3302 	/* Implementation id */
3303 	WRITE32(0);	/* zero length nfs_impl_id4 array */
3304 	ADJUST_ARGS();
3305 	return 0;
3306 }
3307 
3308 static __be32
nfsd4_encode_create_session(struct nfsd4_compoundres * resp,int nfserr,struct nfsd4_create_session * sess)3309 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
3310 			    struct nfsd4_create_session *sess)
3311 {
3312 	__be32 *p;
3313 
3314 	if (nfserr)
3315 		return nfserr;
3316 
3317 	RESERVE_SPACE(24);
3318 	WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3319 	WRITE32(sess->seqid);
3320 	WRITE32(sess->flags);
3321 	ADJUST_ARGS();
3322 
3323 	RESERVE_SPACE(28);
3324 	WRITE32(0); /* headerpadsz */
3325 	WRITE32(sess->fore_channel.maxreq_sz);
3326 	WRITE32(sess->fore_channel.maxresp_sz);
3327 	WRITE32(sess->fore_channel.maxresp_cached);
3328 	WRITE32(sess->fore_channel.maxops);
3329 	WRITE32(sess->fore_channel.maxreqs);
3330 	WRITE32(sess->fore_channel.nr_rdma_attrs);
3331 	ADJUST_ARGS();
3332 
3333 	if (sess->fore_channel.nr_rdma_attrs) {
3334 		RESERVE_SPACE(4);
3335 		WRITE32(sess->fore_channel.rdma_attrs);
3336 		ADJUST_ARGS();
3337 	}
3338 
3339 	RESERVE_SPACE(28);
3340 	WRITE32(0); /* headerpadsz */
3341 	WRITE32(sess->back_channel.maxreq_sz);
3342 	WRITE32(sess->back_channel.maxresp_sz);
3343 	WRITE32(sess->back_channel.maxresp_cached);
3344 	WRITE32(sess->back_channel.maxops);
3345 	WRITE32(sess->back_channel.maxreqs);
3346 	WRITE32(sess->back_channel.nr_rdma_attrs);
3347 	ADJUST_ARGS();
3348 
3349 	if (sess->back_channel.nr_rdma_attrs) {
3350 		RESERVE_SPACE(4);
3351 		WRITE32(sess->back_channel.rdma_attrs);
3352 		ADJUST_ARGS();
3353 	}
3354 	return 0;
3355 }
3356 
3357 static __be32
nfsd4_encode_destroy_session(struct nfsd4_compoundres * resp,int nfserr,struct nfsd4_destroy_session * destroy_session)3358 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3359 			     struct nfsd4_destroy_session *destroy_session)
3360 {
3361 	return nfserr;
3362 }
3363 
3364 static __be32
nfsd4_encode_free_stateid(struct nfsd4_compoundres * resp,int nfserr,struct nfsd4_free_stateid * free_stateid)3365 nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
3366 			  struct nfsd4_free_stateid *free_stateid)
3367 {
3368 	__be32 *p;
3369 
3370 	if (nfserr)
3371 		return nfserr;
3372 
3373 	RESERVE_SPACE(4);
3374 	WRITE32(nfserr);
3375 	ADJUST_ARGS();
3376 	return nfserr;
3377 }
3378 
3379 static __be32
nfsd4_encode_sequence(struct nfsd4_compoundres * resp,int nfserr,struct nfsd4_sequence * seq)3380 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3381 		      struct nfsd4_sequence *seq)
3382 {
3383 	__be32 *p;
3384 
3385 	if (nfserr)
3386 		return nfserr;
3387 
3388 	RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3389 	WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3390 	WRITE32(seq->seqid);
3391 	WRITE32(seq->slotid);
3392 	/* Note slotid's are numbered from zero: */
3393 	WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3394 	WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
3395 	WRITE32(seq->status_flags);
3396 
3397 	ADJUST_ARGS();
3398 	resp->cstate.datap = p; /* DRC cache data pointer */
3399 	return 0;
3400 }
3401 
3402 __be32
nfsd4_encode_test_stateid(struct nfsd4_compoundres * resp,int nfserr,struct nfsd4_test_stateid * test_stateid)3403 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
3404 			  struct nfsd4_test_stateid *test_stateid)
3405 {
3406 	struct nfsd4_test_stateid_id *stateid, *next;
3407 	__be32 *p;
3408 
3409 	RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
3410 	*p++ = htonl(test_stateid->ts_num_ids);
3411 
3412 	list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
3413 		*p++ = stateid->ts_id_status;
3414 	}
3415 
3416 	ADJUST_ARGS();
3417 	return nfserr;
3418 }
3419 
3420 static __be32
nfsd4_encode_noop(struct nfsd4_compoundres * resp,__be32 nfserr,void * p)3421 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3422 {
3423 	return nfserr;
3424 }
3425 
3426 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3427 
3428 /*
3429  * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3430  * since we don't need to filter out obsolete ops as this is
3431  * done in the decoding phase.
3432  */
3433 static nfsd4_enc nfsd4_enc_ops[] = {
3434 	[OP_ACCESS]		= (nfsd4_enc)nfsd4_encode_access,
3435 	[OP_CLOSE]		= (nfsd4_enc)nfsd4_encode_close,
3436 	[OP_COMMIT]		= (nfsd4_enc)nfsd4_encode_commit,
3437 	[OP_CREATE]		= (nfsd4_enc)nfsd4_encode_create,
3438 	[OP_DELEGPURGE]		= (nfsd4_enc)nfsd4_encode_noop,
3439 	[OP_DELEGRETURN]	= (nfsd4_enc)nfsd4_encode_noop,
3440 	[OP_GETATTR]		= (nfsd4_enc)nfsd4_encode_getattr,
3441 	[OP_GETFH]		= (nfsd4_enc)nfsd4_encode_getfh,
3442 	[OP_LINK]		= (nfsd4_enc)nfsd4_encode_link,
3443 	[OP_LOCK]		= (nfsd4_enc)nfsd4_encode_lock,
3444 	[OP_LOCKT]		= (nfsd4_enc)nfsd4_encode_lockt,
3445 	[OP_LOCKU]		= (nfsd4_enc)nfsd4_encode_locku,
3446 	[OP_LOOKUP]		= (nfsd4_enc)nfsd4_encode_noop,
3447 	[OP_LOOKUPP]		= (nfsd4_enc)nfsd4_encode_noop,
3448 	[OP_NVERIFY]		= (nfsd4_enc)nfsd4_encode_noop,
3449 	[OP_OPEN]		= (nfsd4_enc)nfsd4_encode_open,
3450 	[OP_OPENATTR]		= (nfsd4_enc)nfsd4_encode_noop,
3451 	[OP_OPEN_CONFIRM]	= (nfsd4_enc)nfsd4_encode_open_confirm,
3452 	[OP_OPEN_DOWNGRADE]	= (nfsd4_enc)nfsd4_encode_open_downgrade,
3453 	[OP_PUTFH]		= (nfsd4_enc)nfsd4_encode_noop,
3454 	[OP_PUTPUBFH]		= (nfsd4_enc)nfsd4_encode_noop,
3455 	[OP_PUTROOTFH]		= (nfsd4_enc)nfsd4_encode_noop,
3456 	[OP_READ]		= (nfsd4_enc)nfsd4_encode_read,
3457 	[OP_READDIR]		= (nfsd4_enc)nfsd4_encode_readdir,
3458 	[OP_READLINK]		= (nfsd4_enc)nfsd4_encode_readlink,
3459 	[OP_REMOVE]		= (nfsd4_enc)nfsd4_encode_remove,
3460 	[OP_RENAME]		= (nfsd4_enc)nfsd4_encode_rename,
3461 	[OP_RENEW]		= (nfsd4_enc)nfsd4_encode_noop,
3462 	[OP_RESTOREFH]		= (nfsd4_enc)nfsd4_encode_noop,
3463 	[OP_SAVEFH]		= (nfsd4_enc)nfsd4_encode_noop,
3464 	[OP_SECINFO]		= (nfsd4_enc)nfsd4_encode_secinfo,
3465 	[OP_SETATTR]		= (nfsd4_enc)nfsd4_encode_setattr,
3466 	[OP_SETCLIENTID]	= (nfsd4_enc)nfsd4_encode_setclientid,
3467 	[OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3468 	[OP_VERIFY]		= (nfsd4_enc)nfsd4_encode_noop,
3469 	[OP_WRITE]		= (nfsd4_enc)nfsd4_encode_write,
3470 	[OP_RELEASE_LOCKOWNER]	= (nfsd4_enc)nfsd4_encode_noop,
3471 
3472 	/* NFSv4.1 operations */
3473 	[OP_BACKCHANNEL_CTL]	= (nfsd4_enc)nfsd4_encode_noop,
3474 	[OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
3475 	[OP_EXCHANGE_ID]	= (nfsd4_enc)nfsd4_encode_exchange_id,
3476 	[OP_CREATE_SESSION]	= (nfsd4_enc)nfsd4_encode_create_session,
3477 	[OP_DESTROY_SESSION]	= (nfsd4_enc)nfsd4_encode_destroy_session,
3478 	[OP_FREE_STATEID]	= (nfsd4_enc)nfsd4_encode_free_stateid,
3479 	[OP_GET_DIR_DELEGATION]	= (nfsd4_enc)nfsd4_encode_noop,
3480 	[OP_GETDEVICEINFO]	= (nfsd4_enc)nfsd4_encode_noop,
3481 	[OP_GETDEVICELIST]	= (nfsd4_enc)nfsd4_encode_noop,
3482 	[OP_LAYOUTCOMMIT]	= (nfsd4_enc)nfsd4_encode_noop,
3483 	[OP_LAYOUTGET]		= (nfsd4_enc)nfsd4_encode_noop,
3484 	[OP_LAYOUTRETURN]	= (nfsd4_enc)nfsd4_encode_noop,
3485 	[OP_SECINFO_NO_NAME]	= (nfsd4_enc)nfsd4_encode_secinfo_no_name,
3486 	[OP_SEQUENCE]		= (nfsd4_enc)nfsd4_encode_sequence,
3487 	[OP_SET_SSV]		= (nfsd4_enc)nfsd4_encode_noop,
3488 	[OP_TEST_STATEID]	= (nfsd4_enc)nfsd4_encode_test_stateid,
3489 	[OP_WANT_DELEGATION]	= (nfsd4_enc)nfsd4_encode_noop,
3490 	[OP_DESTROY_CLIENTID]	= (nfsd4_enc)nfsd4_encode_noop,
3491 	[OP_RECLAIM_COMPLETE]	= (nfsd4_enc)nfsd4_encode_noop,
3492 };
3493 
3494 /*
3495  * Calculate the total amount of memory that the compound response has taken
3496  * after encoding the current operation with pad.
3497  *
3498  * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3499  *      which was specified at nfsd4_operation, else pad is zero.
3500  *
3501  * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
3502  *
3503  * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3504  * will be at least a page and will therefore hold the xdr_buf head.
3505  */
nfsd4_check_resp_size(struct nfsd4_compoundres * resp,u32 pad)3506 int nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
3507 {
3508 	struct xdr_buf *xb = &resp->rqstp->rq_res;
3509 	struct nfsd4_session *session = NULL;
3510 	struct nfsd4_slot *slot = resp->cstate.slot;
3511 	u32 length, tlen = 0;
3512 
3513 	if (!nfsd4_has_session(&resp->cstate))
3514 		return 0;
3515 
3516 	session = resp->cstate.session;
3517 	if (session == NULL)
3518 		return 0;
3519 
3520 	if (xb->page_len == 0) {
3521 		length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3522 	} else {
3523 		if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3524 			tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3525 
3526 		length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3527 	}
3528 	dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3529 		length, xb->page_len, tlen, pad);
3530 
3531 	if (length > session->se_fchannel.maxresp_sz)
3532 		return nfserr_rep_too_big;
3533 
3534 	if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
3535 	    length > session->se_fchannel.maxresp_cached)
3536 		return nfserr_rep_too_big_to_cache;
3537 
3538 	return 0;
3539 }
3540 
3541 void
nfsd4_encode_operation(struct nfsd4_compoundres * resp,struct nfsd4_op * op)3542 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3543 {
3544 	__be32 *statp;
3545 	__be32 *p;
3546 
3547 	RESERVE_SPACE(8);
3548 	WRITE32(op->opnum);
3549 	statp = p++;	/* to be backfilled at the end */
3550 	ADJUST_ARGS();
3551 
3552 	if (op->opnum == OP_ILLEGAL)
3553 		goto status;
3554 	BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3555 	       !nfsd4_enc_ops[op->opnum]);
3556 	op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3557 	/* nfsd4_check_drc_limit guarantees enough room for error status */
3558 	if (!op->status)
3559 		op->status = nfsd4_check_resp_size(resp, 0);
3560 status:
3561 	/*
3562 	 * Note: We write the status directly, instead of using WRITE32(),
3563 	 * since it is already in network byte order.
3564 	 */
3565 	*statp = op->status;
3566 }
3567 
3568 /*
3569  * Encode the reply stored in the stateowner reply cache
3570  *
3571  * XDR note: do not encode rp->rp_buflen: the buffer contains the
3572  * previously sent already encoded operation.
3573  *
3574  * called with nfs4_lock_state() held
3575  */
3576 void
nfsd4_encode_replay(struct nfsd4_compoundres * resp,struct nfsd4_op * op)3577 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3578 {
3579 	__be32 *p;
3580 	struct nfs4_replay *rp = op->replay;
3581 
3582 	BUG_ON(!rp);
3583 
3584 	RESERVE_SPACE(8);
3585 	WRITE32(op->opnum);
3586 	*p++ = rp->rp_status;  /* already xdr'ed */
3587 	ADJUST_ARGS();
3588 
3589 	RESERVE_SPACE(rp->rp_buflen);
3590 	WRITEMEM(rp->rp_buf, rp->rp_buflen);
3591 	ADJUST_ARGS();
3592 }
3593 
3594 int
nfs4svc_encode_voidres(struct svc_rqst * rqstp,__be32 * p,void * dummy)3595 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3596 {
3597         return xdr_ressize_check(rqstp, p);
3598 }
3599 
nfsd4_release_compoundargs(void * rq,__be32 * p,void * resp)3600 int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
3601 {
3602 	struct svc_rqst *rqstp = rq;
3603 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
3604 
3605 	if (args->ops != args->iops) {
3606 		kfree(args->ops);
3607 		args->ops = args->iops;
3608 	}
3609 	kfree(args->tmpp);
3610 	args->tmpp = NULL;
3611 	while (args->to_free) {
3612 		struct tmpbuf *tb = args->to_free;
3613 		args->to_free = tb->next;
3614 		tb->release(tb->buf);
3615 		kfree(tb);
3616 	}
3617 	return 1;
3618 }
3619 
3620 int
nfs4svc_decode_compoundargs(struct svc_rqst * rqstp,__be32 * p,struct nfsd4_compoundargs * args)3621 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3622 {
3623 	args->p = p;
3624 	args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3625 	args->pagelist = rqstp->rq_arg.pages;
3626 	args->pagelen = rqstp->rq_arg.page_len;
3627 	args->tmpp = NULL;
3628 	args->to_free = NULL;
3629 	args->ops = args->iops;
3630 	args->rqstp = rqstp;
3631 
3632 	return !nfsd4_decode_compound(args);
3633 }
3634 
3635 int
nfs4svc_encode_compoundres(struct svc_rqst * rqstp,__be32 * p,struct nfsd4_compoundres * resp)3636 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3637 {
3638 	/*
3639 	 * All that remains is to write the tag and operation count...
3640 	 */
3641 	struct nfsd4_compound_state *cs = &resp->cstate;
3642 	struct kvec *iov;
3643 	p = resp->tagp;
3644 	*p++ = htonl(resp->taglen);
3645 	memcpy(p, resp->tag, resp->taglen);
3646 	p += XDR_QUADLEN(resp->taglen);
3647 	*p++ = htonl(resp->opcnt);
3648 
3649 	if (rqstp->rq_res.page_len)
3650 		iov = &rqstp->rq_res.tail[0];
3651 	else
3652 		iov = &rqstp->rq_res.head[0];
3653 	iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3654 	BUG_ON(iov->iov_len > PAGE_SIZE);
3655 	if (nfsd4_has_session(cs)) {
3656 		if (cs->status != nfserr_replay_cache) {
3657 			nfsd4_store_cache_entry(resp);
3658 			cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3659 		}
3660 		/* Renew the clientid on success and on replay */
3661 		release_session_client(cs->session);
3662 		nfsd4_put_session(cs->session);
3663 	}
3664 	return 1;
3665 }
3666 
3667 /*
3668  * Local variables:
3669  *  c-basic-offset: 8
3670  * End:
3671  */
3672