xref: /linux/fs/nfsd/nfsxdr.c (revision d141ec2825b4d3ec52f27c43bdd864090159273a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * XDR support for nfsd
4  *
5  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6  */
7 
8 #include <linux/filelock.h>
9 
10 #include "vfs.h"
11 #include "xdr.h"
12 #include "auth.h"
13 
14 /*
15  * Sun convention: a sattr time-useconds field of one full second (an
16  * otherwise out-of-range value) means "set this time to the current
17  * server time." It's needed to make permissions checks for the "touch"
18  * program across NFSv2 mounts work correctly. See description of
19  * sattr in section 6.1 of "NFS Illustrated" by Brent Callaghan,
20  * Addison-Wesley, ISBN 0-201-32750-5
21  */
22 #define NFS2_SATTR_SET_TO_SERVER_TIME	(1000000)
23 
24 /*
25  * Mapping of S_IF* types to NFS file types
26  */
27 static const u32 nfs_ftypes[] = {
28 	NFNON,  NFCHR,  NFCHR, NFBAD,
29 	NFDIR,  NFBAD,  NFBLK, NFBAD,
30 	NFREG,  NFBAD,  NFLNK, NFBAD,
31 	NFSOCK, NFBAD,  NFLNK, NFBAD,
32 };
33 
34 
35 /*
36  * Basic NFSv2 data types (RFC 1094 Section 2.3)
37  */
38 
39 /**
40  * svcxdr_encode_stat - Encode an NFSv2 status code
41  * @xdr: XDR stream
42  * @status: status value to encode
43  *
44  * Return values:
45  *   %false: Send buffer space was exhausted
46  *   %true: Success
47  */
48 bool
svcxdr_encode_stat(struct xdr_stream * xdr,__be32 status)49 svcxdr_encode_stat(struct xdr_stream *xdr, __be32 status)
50 {
51 	__be32 *p;
52 
53 	p = xdr_reserve_space(xdr, sizeof(status));
54 	if (!p)
55 		return false;
56 	*p = status;
57 
58 	return true;
59 }
60 
61 /**
62  * svcxdr_decode_fhandle - Decode an NFSv2 file handle
63  * @xdr: XDR stream positioned at an encoded NFSv2 FH
64  * @fhp: OUT: filled-in server file handle
65  *
66  * Return values:
67  *  %false: The encoded file handle was not valid
68  *  %true: @fhp has been initialized
69  */
70 bool
svcxdr_decode_fhandle(struct xdr_stream * xdr,struct svc_fh * fhp)71 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp)
72 {
73 	__be32 *p;
74 
75 	p = xdr_inline_decode(xdr, NFS_FHSIZE);
76 	if (!p)
77 		return false;
78 	fh_init(fhp, NFS_FHSIZE);
79 	memcpy(&fhp->fh_handle.fh_raw, p, NFS_FHSIZE);
80 	fhp->fh_handle.fh_size = NFS_FHSIZE;
81 
82 	return true;
83 }
84 
85 static bool
svcxdr_encode_fhandle(struct xdr_stream * xdr,const struct svc_fh * fhp)86 svcxdr_encode_fhandle(struct xdr_stream *xdr, const struct svc_fh *fhp)
87 {
88 	__be32 *p;
89 
90 	p = xdr_reserve_space(xdr, NFS_FHSIZE);
91 	if (!p)
92 		return false;
93 	memcpy(p, &fhp->fh_handle.fh_raw, NFS_FHSIZE);
94 
95 	return true;
96 }
97 
98 static __be32 *
encode_timeval(__be32 * p,const struct timespec64 * time)99 encode_timeval(__be32 *p, const struct timespec64 *time)
100 {
101 	*p++ = cpu_to_be32((u32)time->tv_sec);
102 	if (time->tv_nsec)
103 		*p++ = cpu_to_be32(time->tv_nsec / NSEC_PER_USEC);
104 	else
105 		*p++ = xdr_zero;
106 	return p;
107 }
108 
109 static bool
svcxdr_decode_filename(struct xdr_stream * xdr,char ** name,unsigned int * len)110 svcxdr_decode_filename(struct xdr_stream *xdr, char **name, unsigned int *len)
111 {
112 	u32 size, i;
113 	__be32 *p;
114 	char *c;
115 
116 	if (xdr_stream_decode_u32(xdr, &size) < 0)
117 		return false;
118 	if (size == 0 || size > NFS_MAXNAMLEN)
119 		return false;
120 	p = xdr_inline_decode(xdr, size);
121 	if (!p)
122 		return false;
123 
124 	*len = size;
125 	*name = (char *)p;
126 	for (i = 0, c = *name; i < size; i++, c++)
127 		if (*c == '\0' || *c == '/')
128 			return false;
129 
130 	return true;
131 }
132 
133 static bool
svcxdr_decode_diropargs(struct xdr_stream * xdr,struct svc_fh * fhp,char ** name,unsigned int * len)134 svcxdr_decode_diropargs(struct xdr_stream *xdr, struct svc_fh *fhp,
135 			char **name, unsigned int *len)
136 {
137 	return svcxdr_decode_fhandle(xdr, fhp) &&
138 		svcxdr_decode_filename(xdr, name, len);
139 }
140 
141 static bool
svcxdr_decode_sattr(struct svc_rqst * rqstp,struct xdr_stream * xdr,struct iattr * iap)142 svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
143 		    struct iattr *iap)
144 {
145 	u32 tmp1, tmp2;
146 	__be32 *p;
147 
148 	p = xdr_inline_decode(xdr, XDR_UNIT * 8);
149 	if (!p)
150 		return false;
151 
152 	iap->ia_valid = 0;
153 
154 	/*
155 	 * Some Sun clients put 0xffff in the mode field when they
156 	 * mean 0xffffffff.
157 	 */
158 	tmp1 = be32_to_cpup(p++);
159 	if (tmp1 != (u32)-1 && tmp1 != 0xffff) {
160 		iap->ia_valid |= ATTR_MODE;
161 		iap->ia_mode = tmp1;
162 	}
163 
164 	tmp1 = be32_to_cpup(p++);
165 	if (tmp1 != (u32)-1) {
166 		iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), tmp1);
167 		if (uid_valid(iap->ia_uid))
168 			iap->ia_valid |= ATTR_UID;
169 	}
170 
171 	tmp1 = be32_to_cpup(p++);
172 	if (tmp1 != (u32)-1) {
173 		iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), tmp1);
174 		if (gid_valid(iap->ia_gid))
175 			iap->ia_valid |= ATTR_GID;
176 	}
177 
178 	tmp1 = be32_to_cpup(p++);
179 	if (tmp1 != (u32)-1) {
180 		iap->ia_valid |= ATTR_SIZE;
181 		iap->ia_size = tmp1;
182 	}
183 
184 	tmp1 = be32_to_cpup(p++);
185 	tmp2 = be32_to_cpup(p++);
186 	if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
187 		/*
188 		 * Range test here to prevent the multiplication from
189 		 * wrapping to a valid (but incorrect) value on 32-bit
190 		 * platforms.
191 		 */
192 		if (tmp2 > NFS2_SATTR_SET_TO_SERVER_TIME)
193 			return false;
194 		iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
195 		iap->ia_atime.tv_sec = tmp1;
196 		iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;
197 		if (tmp2 == NFS2_SATTR_SET_TO_SERVER_TIME)
198 			iap->ia_valid &= ~ATTR_ATIME_SET;
199 	}
200 
201 	tmp1 = be32_to_cpup(p++);
202 	tmp2 = be32_to_cpup(p++);
203 	if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
204 		if (tmp2 > NFS2_SATTR_SET_TO_SERVER_TIME)
205 			return false;
206 		iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
207 		iap->ia_mtime.tv_sec = tmp1;
208 		iap->ia_mtime.tv_nsec = tmp2 * NSEC_PER_USEC;
209 		if (tmp2 == NFS2_SATTR_SET_TO_SERVER_TIME)
210 			iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
211 	}
212 
213 	return true;
214 }
215 
216 /**
217  * svcxdr_encode_fattr - Encode NFSv2 file attributes
218  * @rqstp: Context of a completed RPC transaction
219  * @xdr: XDR stream
220  * @fhp: File handle to encode
221  * @stat: Attributes to encode
222  *
223  * Return values:
224  *   %false: Send buffer space was exhausted
225  *   %true: Success
226  */
227 bool
svcxdr_encode_fattr(struct svc_rqst * rqstp,struct xdr_stream * xdr,const struct svc_fh * fhp,const struct kstat * stat)228 svcxdr_encode_fattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
229 		    const struct svc_fh *fhp, const struct kstat *stat)
230 {
231 	struct user_namespace *userns = nfsd_user_namespace(rqstp);
232 	struct dentry *dentry = fhp->fh_dentry;
233 	int type = stat->mode & S_IFMT;
234 	struct timespec64 time;
235 	__be32 *p;
236 	u32 fsid;
237 
238 	p = xdr_reserve_space(xdr, XDR_UNIT * 17);
239 	if (!p)
240 		return false;
241 
242 	*p++ = cpu_to_be32(nfs_ftypes[type >> 12]);
243 	*p++ = cpu_to_be32((u32)stat->mode);
244 	*p++ = cpu_to_be32((u32)stat->nlink);
245 	*p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
246 	*p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
247 
248 	if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN)
249 		*p++ = cpu_to_be32(NFS_MAXPATHLEN);
250 	else
251 		*p++ = cpu_to_be32((u32) stat->size);
252 	*p++ = cpu_to_be32((u32) stat->blksize);
253 	if (S_ISCHR(type) || S_ISBLK(type))
254 		*p++ = cpu_to_be32(new_encode_dev(stat->rdev));
255 	else
256 		*p++ = cpu_to_be32(0xffffffff);
257 	*p++ = cpu_to_be32((u32)stat->blocks);
258 
259 	switch (fsid_source(fhp)) {
260 	case FSIDSOURCE_FSID:
261 		fsid = (u32)fhp->fh_export->ex_fsid;
262 		break;
263 	case FSIDSOURCE_UUID:
264 		fsid = ((u32 *)fhp->fh_export->ex_uuid)[0];
265 		fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[1];
266 		fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[2];
267 		fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[3];
268 		break;
269 	default:
270 		fsid = new_encode_dev(stat->dev);
271 		break;
272 	}
273 	*p++ = cpu_to_be32(fsid);
274 
275 	*p++ = cpu_to_be32((u32)stat->ino);
276 	p = encode_timeval(p, &stat->atime);
277 	time = stat->mtime;
278 	lease_get_mtime(d_inode(dentry), &time);
279 	p = encode_timeval(p, &time);
280 	encode_timeval(p, &stat->ctime);
281 
282 	return true;
283 }
284 
285 /*
286  * XDR decode functions
287  */
288 
289 bool
nfssvc_decode_fhandleargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)290 nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
291 {
292 	struct nfsd_fhandle *args = rqstp->rq_argp;
293 
294 	return svcxdr_decode_fhandle(xdr, &args->fh);
295 }
296 
297 bool
nfssvc_decode_sattrargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)298 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
299 {
300 	struct nfsd_sattrargs *args = rqstp->rq_argp;
301 
302 	return svcxdr_decode_fhandle(xdr, &args->fh) &&
303 		svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
304 }
305 
306 bool
nfssvc_decode_diropargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)307 nfssvc_decode_diropargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
308 {
309 	struct nfsd_diropargs *args = rqstp->rq_argp;
310 
311 	return svcxdr_decode_diropargs(xdr, &args->fh, &args->name, &args->len);
312 }
313 
314 bool
nfssvc_decode_readargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)315 nfssvc_decode_readargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
316 {
317 	struct nfsd_readargs *args = rqstp->rq_argp;
318 	u32 totalcount;
319 
320 	if (!svcxdr_decode_fhandle(xdr, &args->fh))
321 		return false;
322 	if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
323 		return false;
324 	if (xdr_stream_decode_u32(xdr, &args->count) < 0)
325 		return false;
326 	/* totalcount is ignored */
327 	if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
328 		return false;
329 
330 	return true;
331 }
332 
333 bool
nfssvc_decode_writeargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)334 nfssvc_decode_writeargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
335 {
336 	struct nfsd_writeargs *args = rqstp->rq_argp;
337 	u32 beginoffset, totalcount;
338 
339 	if (!svcxdr_decode_fhandle(xdr, &args->fh))
340 		return false;
341 	/* beginoffset is ignored */
342 	if (xdr_stream_decode_u32(xdr, &beginoffset) < 0)
343 		return false;
344 	if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
345 		return false;
346 	/* totalcount is ignored */
347 	if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
348 		return false;
349 
350 	/* opaque data */
351 	if (xdr_stream_decode_u32(xdr, &args->len) < 0)
352 		return false;
353 	if (args->len > NFS_MAXDATA)
354 		return false;
355 
356 	return xdr_stream_subsegment(xdr, &args->payload, args->len);
357 }
358 
359 bool
nfssvc_decode_createargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)360 nfssvc_decode_createargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
361 {
362 	struct nfsd_createargs *args = rqstp->rq_argp;
363 
364 	return svcxdr_decode_diropargs(xdr, &args->fh,
365 				       &args->name, &args->len) &&
366 		svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
367 }
368 
369 bool
nfssvc_decode_renameargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)370 nfssvc_decode_renameargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
371 {
372 	struct nfsd_renameargs *args = rqstp->rq_argp;
373 
374 	return svcxdr_decode_diropargs(xdr, &args->ffh,
375 				       &args->fname, &args->flen) &&
376 		svcxdr_decode_diropargs(xdr, &args->tfh,
377 					&args->tname, &args->tlen);
378 }
379 
380 bool
nfssvc_decode_linkargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)381 nfssvc_decode_linkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
382 {
383 	struct nfsd_linkargs *args = rqstp->rq_argp;
384 
385 	return svcxdr_decode_fhandle(xdr, &args->ffh) &&
386 		svcxdr_decode_diropargs(xdr, &args->tfh,
387 					&args->tname, &args->tlen);
388 }
389 
390 bool
nfssvc_decode_symlinkargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)391 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
392 {
393 	struct nfsd_symlinkargs *args = rqstp->rq_argp;
394 	struct kvec *head = rqstp->rq_arg.head;
395 
396 	if (!svcxdr_decode_diropargs(xdr, &args->ffh, &args->fname, &args->flen))
397 		return false;
398 	if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
399 		return false;
400 	if (args->tlen == 0)
401 		return false;
402 
403 	args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
404 	args->first.iov_base = xdr_inline_decode(xdr, args->tlen);
405 	if (!args->first.iov_base)
406 		return false;
407 	return svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
408 }
409 
410 bool
nfssvc_decode_readdirargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)411 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
412 {
413 	struct nfsd_readdirargs *args = rqstp->rq_argp;
414 
415 	if (!svcxdr_decode_fhandle(xdr, &args->fh))
416 		return false;
417 	if (xdr_stream_decode_u32(xdr, &args->cookie) < 0)
418 		return false;
419 	if (xdr_stream_decode_u32(xdr, &args->count) < 0)
420 		return false;
421 
422 	return true;
423 }
424 
425 /*
426  * XDR encode functions
427  */
428 
429 bool
nfssvc_encode_statres(struct svc_rqst * rqstp,struct xdr_stream * xdr)430 nfssvc_encode_statres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
431 {
432 	struct nfsd_stat *resp = rqstp->rq_resp;
433 
434 	return svcxdr_encode_stat(xdr, resp->status);
435 }
436 
437 bool
nfssvc_encode_attrstatres(struct svc_rqst * rqstp,struct xdr_stream * xdr)438 nfssvc_encode_attrstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
439 {
440 	struct nfsd_attrstat *resp = rqstp->rq_resp;
441 
442 	if (!svcxdr_encode_stat(xdr, resp->status))
443 		return false;
444 	switch (resp->status) {
445 	case nfs_ok:
446 		if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
447 			return false;
448 		break;
449 	}
450 
451 	return true;
452 }
453 
454 bool
nfssvc_encode_diropres(struct svc_rqst * rqstp,struct xdr_stream * xdr)455 nfssvc_encode_diropres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
456 {
457 	struct nfsd_diropres *resp = rqstp->rq_resp;
458 
459 	if (!svcxdr_encode_stat(xdr, resp->status))
460 		return false;
461 	switch (resp->status) {
462 	case nfs_ok:
463 		if (!svcxdr_encode_fhandle(xdr, &resp->fh))
464 			return false;
465 		if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
466 			return false;
467 		break;
468 	}
469 
470 	return true;
471 }
472 
473 bool
nfssvc_encode_readlinkres(struct svc_rqst * rqstp,struct xdr_stream * xdr)474 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
475 {
476 	struct nfsd_readlinkres *resp = rqstp->rq_resp;
477 	struct kvec *head = rqstp->rq_res.head;
478 
479 	if (!svcxdr_encode_stat(xdr, resp->status))
480 		return false;
481 	switch (resp->status) {
482 	case nfs_ok:
483 		if (xdr_stream_encode_u32(xdr, resp->len) < 0)
484 			return false;
485 		svcxdr_encode_opaque_pages(rqstp, xdr, &resp->page, 0,
486 					   resp->len);
487 		if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
488 			return false;
489 		break;
490 	}
491 
492 	return true;
493 }
494 
495 bool
nfssvc_encode_readres(struct svc_rqst * rqstp,struct xdr_stream * xdr)496 nfssvc_encode_readres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
497 {
498 	struct nfsd_readres *resp = rqstp->rq_resp;
499 	struct kvec *head = rqstp->rq_res.head;
500 
501 	if (!svcxdr_encode_stat(xdr, resp->status))
502 		return false;
503 	switch (resp->status) {
504 	case nfs_ok:
505 		if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
506 			return false;
507 		if (xdr_stream_encode_u32(xdr, resp->count) < 0)
508 			return false;
509 		svcxdr_encode_opaque_pages(rqstp, xdr, resp->pages,
510 					   rqstp->rq_res.page_base,
511 					   resp->count);
512 		if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
513 			return false;
514 		break;
515 	}
516 
517 	return true;
518 }
519 
520 bool
nfssvc_encode_readdirres(struct svc_rqst * rqstp,struct xdr_stream * xdr)521 nfssvc_encode_readdirres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
522 {
523 	struct nfsd_readdirres *resp = rqstp->rq_resp;
524 	struct xdr_buf *dirlist = &resp->dirlist;
525 
526 	if (!svcxdr_encode_stat(xdr, resp->status))
527 		return false;
528 	switch (resp->status) {
529 	case nfs_ok:
530 		svcxdr_encode_opaque_pages(rqstp, xdr, dirlist->pages, 0,
531 					   dirlist->len);
532 		/* no more entries */
533 		if (xdr_stream_encode_item_absent(xdr) < 0)
534 			return false;
535 		if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
536 			return false;
537 		break;
538 	}
539 
540 	return true;
541 }
542 
543 bool
nfssvc_encode_statfsres(struct svc_rqst * rqstp,struct xdr_stream * xdr)544 nfssvc_encode_statfsres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
545 {
546 	struct nfsd_statfsres *resp = rqstp->rq_resp;
547 	struct kstatfs	*stat = &resp->stats;
548 	__be32 *p;
549 
550 	if (!svcxdr_encode_stat(xdr, resp->status))
551 		return false;
552 	switch (resp->status) {
553 	case nfs_ok:
554 		p = xdr_reserve_space(xdr, XDR_UNIT * 5);
555 		if (!p)
556 			return false;
557 		*p++ = cpu_to_be32(NFS_MAXDATA);
558 		*p++ = cpu_to_be32(stat->f_bsize);
559 		*p++ = cpu_to_be32(stat->f_blocks);
560 		*p++ = cpu_to_be32(stat->f_bfree);
561 		*p = cpu_to_be32(stat->f_bavail);
562 		break;
563 	}
564 
565 	return true;
566 }
567 
568 /**
569  * nfssvc_encode_nfscookie - Encode a directory offset cookie
570  * @resp: readdir result context
571  * @offset: offset cookie to encode
572  *
573  * The buffer space for the offset cookie has already been reserved
574  * by svcxdr_encode_entry_common().
575  */
nfssvc_encode_nfscookie(struct nfsd_readdirres * resp,u32 offset)576 void nfssvc_encode_nfscookie(struct nfsd_readdirres *resp, u32 offset)
577 {
578 	__be32 cookie = cpu_to_be32(offset);
579 
580 	if (!resp->cookie_offset)
581 		return;
582 
583 	write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
584 			       sizeof(cookie));
585 	resp->cookie_offset = 0;
586 }
587 
588 static bool
svcxdr_encode_entry_common(struct nfsd_readdirres * resp,const char * name,int namlen,loff_t offset,u64 ino)589 svcxdr_encode_entry_common(struct nfsd_readdirres *resp, const char *name,
590 			   int namlen, loff_t offset, u64 ino)
591 {
592 	struct xdr_buf *dirlist = &resp->dirlist;
593 	struct xdr_stream *xdr = &resp->xdr;
594 
595 	if (xdr_stream_encode_item_present(xdr) < 0)
596 		return false;
597 	/* fileid */
598 	if (xdr_stream_encode_u32(xdr, (u32)ino) < 0)
599 		return false;
600 	/* name */
601 	if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS2_MAXNAMLEN)) < 0)
602 		return false;
603 	/* cookie */
604 	resp->cookie_offset = dirlist->len;
605 	if (xdr_stream_encode_u32(xdr, ~0U) < 0)
606 		return false;
607 
608 	return true;
609 }
610 
611 /**
612  * nfssvc_encode_entry - encode one NFSv2 READDIR entry
613  * @data: directory context
614  * @name: name of the object to be encoded
615  * @namlen: length of that name, in bytes
616  * @offset: the offset of the previous entry
617  * @ino: the fileid of this entry
618  * @d_type: unused
619  *
620  * Return values:
621  *   %0: Entry was successfully encoded.
622  *   %-EINVAL: An encoding problem occurred, secondary status code in resp->common.err
623  *
624  * On exit, the following fields are updated:
625  *   - resp->xdr
626  *   - resp->common.err
627  *   - resp->cookie_offset
628  */
nfssvc_encode_entry(void * data,const char * name,int namlen,loff_t offset,u64 ino,unsigned int d_type)629 int nfssvc_encode_entry(void *data, const char *name, int namlen,
630 			loff_t offset, u64 ino, unsigned int d_type)
631 {
632 	struct readdir_cd *ccd = data;
633 	struct nfsd_readdirres *resp = container_of(ccd,
634 						    struct nfsd_readdirres,
635 						    common);
636 	unsigned int starting_length = resp->dirlist.len;
637 
638 	/* The offset cookie for the previous entry */
639 	nfssvc_encode_nfscookie(resp, offset);
640 
641 	if (!svcxdr_encode_entry_common(resp, name, namlen, offset, ino))
642 		goto out_toosmall;
643 
644 	xdr_commit_encode(&resp->xdr);
645 	resp->common.err = nfs_ok;
646 	return 0;
647 
648 out_toosmall:
649 	resp->cookie_offset = 0;
650 	resp->common.err = nfserr_toosmall;
651 	resp->dirlist.len = starting_length;
652 	return -EINVAL;
653 }
654 
655 /*
656  * XDR release functions
657  */
nfssvc_release_attrstat(struct svc_rqst * rqstp)658 void nfssvc_release_attrstat(struct svc_rqst *rqstp)
659 {
660 	struct nfsd_attrstat *resp = rqstp->rq_resp;
661 
662 	fh_put(&resp->fh);
663 }
664 
nfssvc_release_diropres(struct svc_rqst * rqstp)665 void nfssvc_release_diropres(struct svc_rqst *rqstp)
666 {
667 	struct nfsd_diropres *resp = rqstp->rq_resp;
668 
669 	fh_put(&resp->fh);
670 }
671 
nfssvc_release_readres(struct svc_rqst * rqstp)672 void nfssvc_release_readres(struct svc_rqst *rqstp)
673 {
674 	struct nfsd_readres *resp = rqstp->rq_resp;
675 
676 	fh_put(&resp->fh);
677 }
678