1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * XDR support for nfsd/protocol version 3.
4 *
5 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
6 *
7 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
8 */
9
10 #include <linux/namei.h>
11 #include <linux/sunrpc/svc_xprt.h>
12 #include "xdr3.h"
13 #include "auth.h"
14 #include "netns.h"
15 #include "vfs.h"
16
17 /*
18 * Force construction of an empty post-op attr
19 */
20 static const struct svc_fh nfs3svc_null_fh = {
21 .fh_no_wcc = true,
22 };
23
24 /*
25 * time_delta. {1, 0} means the server is accurate only
26 * to the nearest second.
27 */
28 static const struct timespec64 nfs3svc_time_delta = {
29 .tv_sec = 1,
30 .tv_nsec = 0,
31 };
32
33 /*
34 * Mapping of S_IF* types to NFS file types
35 */
36 static const u32 nfs3_ftypes[] = {
37 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
38 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
39 NF3REG, NF3BAD, NF3LNK, NF3BAD,
40 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
41 };
42
43
44 /*
45 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
46 */
47
48 static __be32 *
encode_nfstime3(__be32 * p,const struct timespec64 * time)49 encode_nfstime3(__be32 *p, const struct timespec64 *time)
50 {
51 *p++ = cpu_to_be32((u32)time->tv_sec);
52 *p++ = cpu_to_be32(time->tv_nsec);
53
54 return p;
55 }
56
57 static bool
svcxdr_decode_nfstime3(struct xdr_stream * xdr,struct timespec64 * timep)58 svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
59 {
60 __be32 *p;
61
62 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
63 if (!p)
64 return false;
65 timep->tv_sec = be32_to_cpup(p++);
66 timep->tv_nsec = be32_to_cpup(p);
67
68 return true;
69 }
70
71 /**
72 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
73 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
74 * @fhp: OUT: filled-in server file handle
75 *
76 * Return values:
77 * %false: The encoded file handle was not valid
78 * %true: @fhp has been initialized
79 */
80 bool
svcxdr_decode_nfs_fh3(struct xdr_stream * xdr,struct svc_fh * fhp)81 svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
82 {
83 __be32 *p;
84 u32 size;
85
86 if (xdr_stream_decode_u32(xdr, &size) < 0)
87 return false;
88 if (size == 0 || size > NFS3_FHSIZE)
89 return false;
90 p = xdr_inline_decode(xdr, size);
91 if (!p)
92 return false;
93 fh_init(fhp, NFS3_FHSIZE);
94 fhp->fh_handle.fh_size = size;
95 memcpy(&fhp->fh_handle.fh_base, p, size);
96
97 return true;
98 }
99
100 /**
101 * svcxdr_encode_nfsstat3 - Encode an NFSv3 status code
102 * @xdr: XDR stream
103 * @status: status value to encode
104 *
105 * Return values:
106 * %false: Send buffer space was exhausted
107 * %true: Success
108 */
109 bool
svcxdr_encode_nfsstat3(struct xdr_stream * xdr,__be32 status)110 svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
111 {
112 __be32 *p;
113
114 p = xdr_reserve_space(xdr, sizeof(status));
115 if (!p)
116 return false;
117 *p = status;
118
119 return true;
120 }
121
122 static bool
svcxdr_encode_nfs_fh3(struct xdr_stream * xdr,const struct svc_fh * fhp)123 svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
124 {
125 u32 size = fhp->fh_handle.fh_size;
126 __be32 *p;
127
128 p = xdr_reserve_space(xdr, XDR_UNIT + size);
129 if (!p)
130 return false;
131 *p++ = cpu_to_be32(size);
132 if (size)
133 p[XDR_QUADLEN(size) - 1] = 0;
134 memcpy(p, &fhp->fh_handle.fh_base, size);
135
136 return true;
137 }
138
139 static bool
svcxdr_encode_post_op_fh3(struct xdr_stream * xdr,const struct svc_fh * fhp)140 svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
141 {
142 if (xdr_stream_encode_item_present(xdr) < 0)
143 return false;
144 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
145 return false;
146
147 return true;
148 }
149
150 static bool
svcxdr_encode_cookieverf3(struct xdr_stream * xdr,const __be32 * verf)151 svcxdr_encode_cookieverf3(struct xdr_stream *xdr, const __be32 *verf)
152 {
153 __be32 *p;
154
155 p = xdr_reserve_space(xdr, NFS3_COOKIEVERFSIZE);
156 if (!p)
157 return false;
158 memcpy(p, verf, NFS3_COOKIEVERFSIZE);
159
160 return true;
161 }
162
163 static bool
svcxdr_encode_writeverf3(struct xdr_stream * xdr,const __be32 * verf)164 svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
165 {
166 __be32 *p;
167
168 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
169 if (!p)
170 return false;
171 memcpy(p, verf, NFS3_WRITEVERFSIZE);
172
173 return true;
174 }
175
176 static bool
svcxdr_decode_filename3(struct xdr_stream * xdr,char ** name,unsigned int * len)177 svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
178 {
179 u32 size, i;
180 __be32 *p;
181 char *c;
182
183 if (xdr_stream_decode_u32(xdr, &size) < 0)
184 return false;
185 if (size == 0 || size > NFS3_MAXNAMLEN)
186 return false;
187 p = xdr_inline_decode(xdr, size);
188 if (!p)
189 return false;
190
191 *len = size;
192 *name = (char *)p;
193 for (i = 0, c = *name; i < size; i++, c++) {
194 if (*c == '\0' || *c == '/')
195 return false;
196 }
197
198 return true;
199 }
200
201 static bool
svcxdr_decode_diropargs3(struct xdr_stream * xdr,struct svc_fh * fhp,char ** name,unsigned int * len)202 svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
203 char **name, unsigned int *len)
204 {
205 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
206 svcxdr_decode_filename3(xdr, name, len);
207 }
208
209 static bool
svcxdr_decode_sattr3(struct svc_rqst * rqstp,struct xdr_stream * xdr,struct iattr * iap)210 svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
211 struct iattr *iap)
212 {
213 u32 set_it;
214
215 iap->ia_valid = 0;
216
217 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
218 return false;
219 if (set_it) {
220 u32 mode;
221
222 if (xdr_stream_decode_u32(xdr, &mode) < 0)
223 return false;
224 iap->ia_valid |= ATTR_MODE;
225 iap->ia_mode = mode;
226 }
227 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
228 return false;
229 if (set_it) {
230 u32 uid;
231
232 if (xdr_stream_decode_u32(xdr, &uid) < 0)
233 return false;
234 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
235 if (uid_valid(iap->ia_uid))
236 iap->ia_valid |= ATTR_UID;
237 }
238 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
239 return false;
240 if (set_it) {
241 u32 gid;
242
243 if (xdr_stream_decode_u32(xdr, &gid) < 0)
244 return false;
245 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
246 if (gid_valid(iap->ia_gid))
247 iap->ia_valid |= ATTR_GID;
248 }
249 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
250 return false;
251 if (set_it) {
252 u64 newsize;
253
254 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
255 return false;
256 iap->ia_valid |= ATTR_SIZE;
257 iap->ia_size = newsize;
258 }
259 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
260 return false;
261 switch (set_it) {
262 case DONT_CHANGE:
263 break;
264 case SET_TO_SERVER_TIME:
265 iap->ia_valid |= ATTR_ATIME;
266 break;
267 case SET_TO_CLIENT_TIME:
268 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
269 return false;
270 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
271 break;
272 default:
273 return false;
274 }
275 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
276 return false;
277 switch (set_it) {
278 case DONT_CHANGE:
279 break;
280 case SET_TO_SERVER_TIME:
281 iap->ia_valid |= ATTR_MTIME;
282 break;
283 case SET_TO_CLIENT_TIME:
284 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
285 return false;
286 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
287 break;
288 default:
289 return false;
290 }
291
292 return true;
293 }
294
295 static bool
svcxdr_decode_sattrguard3(struct xdr_stream * xdr,struct nfsd3_sattrargs * args)296 svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
297 {
298 __be32 *p;
299 u32 check;
300
301 if (xdr_stream_decode_bool(xdr, &check) < 0)
302 return false;
303 if (check) {
304 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
305 if (!p)
306 return false;
307 args->check_guard = 1;
308 args->guardtime = be32_to_cpup(p);
309 } else
310 args->check_guard = 0;
311
312 return true;
313 }
314
315 static bool
svcxdr_decode_specdata3(struct xdr_stream * xdr,struct nfsd3_mknodargs * args)316 svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
317 {
318 __be32 *p;
319
320 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
321 if (!p)
322 return false;
323 args->major = be32_to_cpup(p++);
324 args->minor = be32_to_cpup(p);
325
326 return true;
327 }
328
329 static bool
svcxdr_decode_devicedata3(struct svc_rqst * rqstp,struct xdr_stream * xdr,struct nfsd3_mknodargs * args)330 svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
331 struct nfsd3_mknodargs *args)
332 {
333 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
334 svcxdr_decode_specdata3(xdr, args);
335 }
336
337 static bool
svcxdr_encode_fattr3(struct svc_rqst * rqstp,struct xdr_stream * xdr,const struct svc_fh * fhp,const struct kstat * stat)338 svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
339 const struct svc_fh *fhp, const struct kstat *stat)
340 {
341 struct user_namespace *userns = nfsd_user_namespace(rqstp);
342 __be32 *p;
343 u64 fsid;
344
345 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
346 if (!p)
347 return false;
348
349 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
350 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
351 *p++ = cpu_to_be32((u32)stat->nlink);
352 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
353 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
354 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
355 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
356 else
357 p = xdr_encode_hyper(p, (u64)stat->size);
358
359 /* used */
360 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
361
362 /* rdev */
363 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
364 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
365
366 switch(fsid_source(fhp)) {
367 case FSIDSOURCE_FSID:
368 fsid = (u64)fhp->fh_export->ex_fsid;
369 break;
370 case FSIDSOURCE_UUID:
371 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
372 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
373 break;
374 default:
375 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
376 }
377 p = xdr_encode_hyper(p, fsid);
378
379 /* fileid */
380 p = xdr_encode_hyper(p, stat->ino);
381
382 p = encode_nfstime3(p, &stat->atime);
383 p = encode_nfstime3(p, &stat->mtime);
384 encode_nfstime3(p, &stat->ctime);
385
386 return true;
387 }
388
389 static bool
svcxdr_encode_wcc_attr(struct xdr_stream * xdr,const struct svc_fh * fhp)390 svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
391 {
392 __be32 *p;
393
394 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
395 if (!p)
396 return false;
397 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
398 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
399 encode_nfstime3(p, &fhp->fh_pre_ctime);
400
401 return true;
402 }
403
404 static bool
svcxdr_encode_pre_op_attr(struct xdr_stream * xdr,const struct svc_fh * fhp)405 svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
406 {
407 if (!fhp->fh_pre_saved) {
408 if (xdr_stream_encode_item_absent(xdr) < 0)
409 return false;
410 return true;
411 }
412
413 if (xdr_stream_encode_item_present(xdr) < 0)
414 return false;
415 return svcxdr_encode_wcc_attr(xdr, fhp);
416 }
417
418 /**
419 * svcxdr_encode_post_op_attr - Encode NFSv3 post-op attributes
420 * @rqstp: Context of a completed RPC transaction
421 * @xdr: XDR stream
422 * @fhp: File handle to encode
423 *
424 * Return values:
425 * %false: Send buffer space was exhausted
426 * %true: Success
427 */
428 bool
svcxdr_encode_post_op_attr(struct svc_rqst * rqstp,struct xdr_stream * xdr,const struct svc_fh * fhp)429 svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
430 const struct svc_fh *fhp)
431 {
432 struct dentry *dentry = fhp->fh_dentry;
433 struct kstat stat;
434
435 /*
436 * The inode may be NULL if the call failed because of a
437 * stale file handle. In this case, no attributes are
438 * returned.
439 */
440 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
441 goto no_post_op_attrs;
442 if (fh_getattr(fhp, &stat) != nfs_ok)
443 goto no_post_op_attrs;
444
445 if (xdr_stream_encode_item_present(xdr) < 0)
446 return false;
447 lease_get_mtime(d_inode(dentry), &stat.mtime);
448 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
449 return false;
450
451 return true;
452
453 no_post_op_attrs:
454 return xdr_stream_encode_item_absent(xdr) > 0;
455 }
456
457 /*
458 * Encode weak cache consistency data
459 */
460 static bool
svcxdr_encode_wcc_data(struct svc_rqst * rqstp,struct xdr_stream * xdr,const struct svc_fh * fhp)461 svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
462 const struct svc_fh *fhp)
463 {
464 struct dentry *dentry = fhp->fh_dentry;
465
466 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
467 goto neither;
468
469 /* before */
470 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
471 return false;
472
473 /* after */
474 if (xdr_stream_encode_item_present(xdr) < 0)
475 return false;
476 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
477 return false;
478
479 return true;
480
481 neither:
482 if (xdr_stream_encode_item_absent(xdr) < 0)
483 return false;
484 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
485 return false;
486
487 return true;
488 }
489
490 /*
491 * Fill in the pre_op attr for the wcc data
492 */
fill_pre_wcc(struct svc_fh * fhp)493 void fill_pre_wcc(struct svc_fh *fhp)
494 {
495 struct inode *inode;
496 struct kstat stat;
497 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
498 __be32 err;
499
500 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
501 return;
502 inode = d_inode(fhp->fh_dentry);
503 err = fh_getattr(fhp, &stat);
504 if (err) {
505 /* Grab the times from inode anyway */
506 stat.mtime = inode->i_mtime;
507 stat.ctime = inode->i_ctime;
508 stat.size = inode->i_size;
509 }
510 if (v4)
511 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
512
513 fhp->fh_pre_mtime = stat.mtime;
514 fhp->fh_pre_ctime = stat.ctime;
515 fhp->fh_pre_size = stat.size;
516 fhp->fh_pre_saved = true;
517 }
518
519 /*
520 * Fill in the post_op attr for the wcc data
521 */
fill_post_wcc(struct svc_fh * fhp)522 void fill_post_wcc(struct svc_fh *fhp)
523 {
524 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
525 struct inode *inode = d_inode(fhp->fh_dentry);
526 __be32 err;
527
528 if (fhp->fh_no_wcc)
529 return;
530
531 if (fhp->fh_post_saved)
532 printk("nfsd: inode locked twice during operation.\n");
533
534 err = fh_getattr(fhp, &fhp->fh_post_attr);
535 if (err) {
536 fhp->fh_post_saved = false;
537 fhp->fh_post_attr.ctime = inode->i_ctime;
538 } else
539 fhp->fh_post_saved = true;
540 if (v4)
541 fhp->fh_post_change =
542 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
543 }
544
545 /*
546 * XDR decode functions
547 */
548
549 int
nfs3svc_decode_fhandleargs(struct svc_rqst * rqstp,__be32 * p)550 nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
551 {
552 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
553 struct nfsd_fhandle *args = rqstp->rq_argp;
554
555 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
556 }
557
558 int
nfs3svc_decode_sattrargs(struct svc_rqst * rqstp,__be32 * p)559 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
560 {
561 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
562 struct nfsd3_sattrargs *args = rqstp->rq_argp;
563
564 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
565 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
566 svcxdr_decode_sattrguard3(xdr, args);
567 }
568
569 int
nfs3svc_decode_diropargs(struct svc_rqst * rqstp,__be32 * p)570 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
571 {
572 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
573 struct nfsd3_diropargs *args = rqstp->rq_argp;
574
575 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
576 }
577
578 int
nfs3svc_decode_accessargs(struct svc_rqst * rqstp,__be32 * p)579 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
580 {
581 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
582 struct nfsd3_accessargs *args = rqstp->rq_argp;
583
584 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
585 return 0;
586 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
587 return 0;
588
589 return 1;
590 }
591
592 int
nfs3svc_decode_readargs(struct svc_rqst * rqstp,__be32 * p)593 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
594 {
595 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
596 struct nfsd3_readargs *args = rqstp->rq_argp;
597
598 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
599 return 0;
600 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
601 return 0;
602 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
603 return 0;
604
605 return 1;
606 }
607
608 int
nfs3svc_decode_writeargs(struct svc_rqst * rqstp,__be32 * p)609 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
610 {
611 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
612 struct nfsd3_writeargs *args = rqstp->rq_argp;
613 u32 max_blocksize = svc_max_payload(rqstp);
614
615 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
616 return 0;
617 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
618 return 0;
619 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
620 return 0;
621 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
622 return 0;
623
624 /* opaque data */
625 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
626 return 0;
627
628 /* request sanity */
629 if (args->count != args->len)
630 return 0;
631 if (args->count > max_blocksize) {
632 args->count = max_blocksize;
633 args->len = max_blocksize;
634 }
635 if (!xdr_stream_subsegment(xdr, &args->payload, args->count))
636 return 0;
637
638 return 1;
639 }
640
641 int
nfs3svc_decode_createargs(struct svc_rqst * rqstp,__be32 * p)642 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
643 {
644 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
645 struct nfsd3_createargs *args = rqstp->rq_argp;
646
647 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
648 return 0;
649 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
650 return 0;
651 switch (args->createmode) {
652 case NFS3_CREATE_UNCHECKED:
653 case NFS3_CREATE_GUARDED:
654 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
655 case NFS3_CREATE_EXCLUSIVE:
656 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
657 if (!args->verf)
658 return 0;
659 break;
660 default:
661 return 0;
662 }
663 return 1;
664 }
665
666 int
nfs3svc_decode_mkdirargs(struct svc_rqst * rqstp,__be32 * p)667 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
668 {
669 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
670 struct nfsd3_createargs *args = rqstp->rq_argp;
671
672 return svcxdr_decode_diropargs3(xdr, &args->fh,
673 &args->name, &args->len) &&
674 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
675 }
676
677 int
nfs3svc_decode_symlinkargs(struct svc_rqst * rqstp,__be32 * p)678 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
679 {
680 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
681 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
682 struct kvec *head = rqstp->rq_arg.head;
683 struct kvec *tail = rqstp->rq_arg.tail;
684 size_t remaining;
685
686 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
687 return 0;
688 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
689 return 0;
690 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
691 return 0;
692
693 /* request sanity */
694 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
695 remaining -= xdr_stream_pos(xdr);
696 if (remaining < xdr_align_size(args->tlen))
697 return 0;
698
699 args->first.iov_base = xdr->p;
700 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
701
702 return 1;
703 }
704
705 int
nfs3svc_decode_mknodargs(struct svc_rqst * rqstp,__be32 * p)706 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
707 {
708 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
709 struct nfsd3_mknodargs *args = rqstp->rq_argp;
710
711 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
712 return 0;
713 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
714 return 0;
715 switch (args->ftype) {
716 case NF3CHR:
717 case NF3BLK:
718 return svcxdr_decode_devicedata3(rqstp, xdr, args);
719 case NF3SOCK:
720 case NF3FIFO:
721 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
722 case NF3REG:
723 case NF3DIR:
724 case NF3LNK:
725 /* Valid XDR but illegal file types */
726 break;
727 default:
728 return 0;
729 }
730
731 return 1;
732 }
733
734 int
nfs3svc_decode_renameargs(struct svc_rqst * rqstp,__be32 * p)735 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
736 {
737 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
738 struct nfsd3_renameargs *args = rqstp->rq_argp;
739
740 return svcxdr_decode_diropargs3(xdr, &args->ffh,
741 &args->fname, &args->flen) &&
742 svcxdr_decode_diropargs3(xdr, &args->tfh,
743 &args->tname, &args->tlen);
744 }
745
746 int
nfs3svc_decode_linkargs(struct svc_rqst * rqstp,__be32 * p)747 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
748 {
749 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
750 struct nfsd3_linkargs *args = rqstp->rq_argp;
751
752 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
753 svcxdr_decode_diropargs3(xdr, &args->tfh,
754 &args->tname, &args->tlen);
755 }
756
757 int
nfs3svc_decode_readdirargs(struct svc_rqst * rqstp,__be32 * p)758 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
759 {
760 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
761 struct nfsd3_readdirargs *args = rqstp->rq_argp;
762
763 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
764 return 0;
765 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
766 return 0;
767 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
768 if (!args->verf)
769 return 0;
770 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
771 return 0;
772
773 return 1;
774 }
775
776 int
nfs3svc_decode_readdirplusargs(struct svc_rqst * rqstp,__be32 * p)777 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
778 {
779 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
780 struct nfsd3_readdirargs *args = rqstp->rq_argp;
781 u32 dircount;
782
783 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
784 return 0;
785 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
786 return 0;
787 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
788 if (!args->verf)
789 return 0;
790 /* dircount is ignored */
791 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
792 return 0;
793 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
794 return 0;
795
796 return 1;
797 }
798
799 int
nfs3svc_decode_commitargs(struct svc_rqst * rqstp,__be32 * p)800 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
801 {
802 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
803 struct nfsd3_commitargs *args = rqstp->rq_argp;
804
805 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
806 return 0;
807 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
808 return 0;
809 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
810 return 0;
811
812 return 1;
813 }
814
815 /*
816 * XDR encode functions
817 */
818
819 /* GETATTR */
820 int
nfs3svc_encode_getattrres(struct svc_rqst * rqstp,__be32 * p)821 nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
822 {
823 struct xdr_stream *xdr = &rqstp->rq_res_stream;
824 struct nfsd3_attrstat *resp = rqstp->rq_resp;
825
826 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
827 return 0;
828 switch (resp->status) {
829 case nfs_ok:
830 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
831 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
832 return 0;
833 break;
834 }
835
836 return 1;
837 }
838
839 /* SETATTR, REMOVE, RMDIR */
840 int
nfs3svc_encode_wccstat(struct svc_rqst * rqstp,__be32 * p)841 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
842 {
843 struct xdr_stream *xdr = &rqstp->rq_res_stream;
844 struct nfsd3_attrstat *resp = rqstp->rq_resp;
845
846 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
847 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
848 }
849
850 /* LOOKUP */
nfs3svc_encode_lookupres(struct svc_rqst * rqstp,__be32 * p)851 int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
852 {
853 struct xdr_stream *xdr = &rqstp->rq_res_stream;
854 struct nfsd3_diropres *resp = rqstp->rq_resp;
855
856 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
857 return 0;
858 switch (resp->status) {
859 case nfs_ok:
860 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
861 return 0;
862 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
863 return 0;
864 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
865 return 0;
866 break;
867 default:
868 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
869 return 0;
870 }
871
872 return 1;
873 }
874
875 /* ACCESS */
876 int
nfs3svc_encode_accessres(struct svc_rqst * rqstp,__be32 * p)877 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
878 {
879 struct xdr_stream *xdr = &rqstp->rq_res_stream;
880 struct nfsd3_accessres *resp = rqstp->rq_resp;
881
882 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
883 return 0;
884 switch (resp->status) {
885 case nfs_ok:
886 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
887 return 0;
888 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
889 return 0;
890 break;
891 default:
892 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
893 return 0;
894 }
895
896 return 1;
897 }
898
899 /* READLINK */
900 int
nfs3svc_encode_readlinkres(struct svc_rqst * rqstp,__be32 * p)901 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
902 {
903 struct xdr_stream *xdr = &rqstp->rq_res_stream;
904 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
905 struct kvec *head = rqstp->rq_res.head;
906
907 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
908 return 0;
909 switch (resp->status) {
910 case nfs_ok:
911 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
912 return 0;
913 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
914 return 0;
915 xdr_write_pages(xdr, resp->pages, 0, resp->len);
916 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
917 return 0;
918 break;
919 default:
920 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
921 return 0;
922 }
923
924 return 1;
925 }
926
927 /* READ */
928 int
nfs3svc_encode_readres(struct svc_rqst * rqstp,__be32 * p)929 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
930 {
931 struct xdr_stream *xdr = &rqstp->rq_res_stream;
932 struct nfsd3_readres *resp = rqstp->rq_resp;
933 struct kvec *head = rqstp->rq_res.head;
934
935 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
936 return 0;
937 switch (resp->status) {
938 case nfs_ok:
939 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
940 return 0;
941 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
942 return 0;
943 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
944 return 0;
945 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
946 return 0;
947 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
948 resp->count);
949 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
950 return 0;
951 break;
952 default:
953 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
954 return 0;
955 }
956
957 return 1;
958 }
959
960 /* WRITE */
961 int
nfs3svc_encode_writeres(struct svc_rqst * rqstp,__be32 * p)962 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
963 {
964 struct xdr_stream *xdr = &rqstp->rq_res_stream;
965 struct nfsd3_writeres *resp = rqstp->rq_resp;
966
967 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
968 return 0;
969 switch (resp->status) {
970 case nfs_ok:
971 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
972 return 0;
973 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
974 return 0;
975 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
976 return 0;
977 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
978 return 0;
979 break;
980 default:
981 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
982 return 0;
983 }
984
985 return 1;
986 }
987
988 /* CREATE, MKDIR, SYMLINK, MKNOD */
989 int
nfs3svc_encode_createres(struct svc_rqst * rqstp,__be32 * p)990 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
991 {
992 struct xdr_stream *xdr = &rqstp->rq_res_stream;
993 struct nfsd3_diropres *resp = rqstp->rq_resp;
994
995 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
996 return 0;
997 switch (resp->status) {
998 case nfs_ok:
999 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
1000 return 0;
1001 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1002 return 0;
1003 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1004 return 0;
1005 break;
1006 default:
1007 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1008 return 0;
1009 }
1010
1011 return 1;
1012 }
1013
1014 /* RENAME */
1015 int
nfs3svc_encode_renameres(struct svc_rqst * rqstp,__be32 * p)1016 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
1017 {
1018 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1019 struct nfsd3_renameres *resp = rqstp->rq_resp;
1020
1021 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1022 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1023 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1024 }
1025
1026 /* LINK */
1027 int
nfs3svc_encode_linkres(struct svc_rqst * rqstp,__be32 * p)1028 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
1029 {
1030 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1031 struct nfsd3_linkres *resp = rqstp->rq_resp;
1032
1033 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1034 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1035 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1036 }
1037
1038 /* READDIR */
1039 int
nfs3svc_encode_readdirres(struct svc_rqst * rqstp,__be32 * p)1040 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
1041 {
1042 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1043 struct nfsd3_readdirres *resp = rqstp->rq_resp;
1044 struct xdr_buf *dirlist = &resp->dirlist;
1045
1046 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1047 return 0;
1048 switch (resp->status) {
1049 case nfs_ok:
1050 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1051 return 0;
1052 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
1053 return 0;
1054 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
1055 /* no more entries */
1056 if (xdr_stream_encode_item_absent(xdr) < 0)
1057 return 0;
1058 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
1059 return 0;
1060 break;
1061 default:
1062 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1063 return 0;
1064 }
1065
1066 return 1;
1067 }
1068
1069 static __be32
compose_entry_fh(struct nfsd3_readdirres * cd,struct svc_fh * fhp,const char * name,int namlen,u64 ino)1070 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
1071 const char *name, int namlen, u64 ino)
1072 {
1073 struct svc_export *exp;
1074 struct dentry *dparent, *dchild;
1075 __be32 rv = nfserr_noent;
1076
1077 dparent = cd->fh.fh_dentry;
1078 exp = cd->fh.fh_export;
1079
1080 if (isdotent(name, namlen)) {
1081 if (namlen == 2) {
1082 dchild = dget_parent(dparent);
1083 /*
1084 * Don't return filehandle for ".." if we're at
1085 * the filesystem or export root:
1086 */
1087 if (dchild == dparent)
1088 goto out;
1089 if (dparent == exp->ex_path.dentry)
1090 goto out;
1091 } else
1092 dchild = dget(dparent);
1093 } else
1094 dchild = lookup_positive_unlocked(name, dparent, namlen);
1095 if (IS_ERR(dchild))
1096 return rv;
1097 if (d_mountpoint(dchild))
1098 goto out;
1099 if (dchild->d_inode->i_ino != ino)
1100 goto out;
1101 rv = fh_compose(fhp, exp, dchild, &cd->fh);
1102 out:
1103 dput(dchild);
1104 return rv;
1105 }
1106
1107 /**
1108 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1109 * @resp: readdir result context
1110 * @offset: offset cookie to encode
1111 *
1112 * The buffer space for the offset cookie has already been reserved
1113 * by svcxdr_encode_entry3_common().
1114 */
nfs3svc_encode_cookie3(struct nfsd3_readdirres * resp,u64 offset)1115 void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1116 {
1117 __be64 cookie = cpu_to_be64(offset);
1118
1119 if (!resp->cookie_offset)
1120 return;
1121 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1122 sizeof(cookie));
1123 resp->cookie_offset = 0;
1124 }
1125
1126 static bool
svcxdr_encode_entry3_common(struct nfsd3_readdirres * resp,const char * name,int namlen,loff_t offset,u64 ino)1127 svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1128 int namlen, loff_t offset, u64 ino)
1129 {
1130 struct xdr_buf *dirlist = &resp->dirlist;
1131 struct xdr_stream *xdr = &resp->xdr;
1132
1133 if (xdr_stream_encode_item_present(xdr) < 0)
1134 return false;
1135 /* fileid */
1136 if (xdr_stream_encode_u64(xdr, ino) < 0)
1137 return false;
1138 /* name */
1139 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1140 return false;
1141 /* cookie */
1142 resp->cookie_offset = dirlist->len;
1143 if (xdr_stream_encode_u64(xdr, NFS_OFFSET_MAX) < 0)
1144 return false;
1145
1146 return true;
1147 }
1148
1149 /**
1150 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR entry
1151 * @data: directory context
1152 * @name: name of the object to be encoded
1153 * @namlen: length of that name, in bytes
1154 * @offset: the offset of the previous entry
1155 * @ino: the fileid of this entry
1156 * @d_type: unused
1157 *
1158 * Return values:
1159 * %0: Entry was successfully encoded.
1160 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1161 *
1162 * On exit, the following fields are updated:
1163 * - resp->xdr
1164 * - resp->common.err
1165 * - resp->cookie_offset
1166 */
nfs3svc_encode_entry3(void * data,const char * name,int namlen,loff_t offset,u64 ino,unsigned int d_type)1167 int nfs3svc_encode_entry3(void *data, const char *name, int namlen,
1168 loff_t offset, u64 ino, unsigned int d_type)
1169 {
1170 struct readdir_cd *ccd = data;
1171 struct nfsd3_readdirres *resp = container_of(ccd,
1172 struct nfsd3_readdirres,
1173 common);
1174 unsigned int starting_length = resp->dirlist.len;
1175
1176 /* The offset cookie for the previous entry */
1177 nfs3svc_encode_cookie3(resp, offset);
1178
1179 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1180 goto out_toosmall;
1181
1182 xdr_commit_encode(&resp->xdr);
1183 resp->common.err = nfs_ok;
1184 return 0;
1185
1186 out_toosmall:
1187 resp->cookie_offset = 0;
1188 resp->common.err = nfserr_toosmall;
1189 resp->dirlist.len = starting_length;
1190 return -EINVAL;
1191 }
1192
1193 static bool
svcxdr_encode_entry3_plus(struct nfsd3_readdirres * resp,const char * name,int namlen,u64 ino)1194 svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1195 int namlen, u64 ino)
1196 {
1197 struct xdr_stream *xdr = &resp->xdr;
1198 struct svc_fh *fhp = &resp->scratch;
1199 bool result;
1200
1201 result = false;
1202 fh_init(fhp, NFS3_FHSIZE);
1203 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1204 goto out_noattrs;
1205
1206 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1207 goto out;
1208 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1209 goto out;
1210 result = true;
1211
1212 out:
1213 fh_put(fhp);
1214 return result;
1215
1216 out_noattrs:
1217 if (xdr_stream_encode_item_absent(xdr) < 0)
1218 return false;
1219 if (xdr_stream_encode_item_absent(xdr) < 0)
1220 return false;
1221 return true;
1222 }
1223
1224 /**
1225 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1226 * @data: directory context
1227 * @name: name of the object to be encoded
1228 * @namlen: length of that name, in bytes
1229 * @offset: the offset of the previous entry
1230 * @ino: the fileid of this entry
1231 * @d_type: unused
1232 *
1233 * Return values:
1234 * %0: Entry was successfully encoded.
1235 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1236 *
1237 * On exit, the following fields are updated:
1238 * - resp->xdr
1239 * - resp->common.err
1240 * - resp->cookie_offset
1241 */
nfs3svc_encode_entryplus3(void * data,const char * name,int namlen,loff_t offset,u64 ino,unsigned int d_type)1242 int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1243 loff_t offset, u64 ino, unsigned int d_type)
1244 {
1245 struct readdir_cd *ccd = data;
1246 struct nfsd3_readdirres *resp = container_of(ccd,
1247 struct nfsd3_readdirres,
1248 common);
1249 unsigned int starting_length = resp->dirlist.len;
1250
1251 /* The offset cookie for the previous entry */
1252 nfs3svc_encode_cookie3(resp, offset);
1253
1254 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1255 goto out_toosmall;
1256 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1257 goto out_toosmall;
1258
1259 xdr_commit_encode(&resp->xdr);
1260 resp->common.err = nfs_ok;
1261 return 0;
1262
1263 out_toosmall:
1264 resp->cookie_offset = 0;
1265 resp->common.err = nfserr_toosmall;
1266 resp->dirlist.len = starting_length;
1267 return -EINVAL;
1268 }
1269
1270 static bool
svcxdr_encode_fsstat3resok(struct xdr_stream * xdr,const struct nfsd3_fsstatres * resp)1271 svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1272 const struct nfsd3_fsstatres *resp)
1273 {
1274 const struct kstatfs *s = &resp->stats;
1275 u64 bs = s->f_bsize;
1276 __be32 *p;
1277
1278 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1279 if (!p)
1280 return false;
1281 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1282 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1283 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1284 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1285 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1286 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1287 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1288
1289 return true;
1290 }
1291
1292 /* FSSTAT */
1293 int
nfs3svc_encode_fsstatres(struct svc_rqst * rqstp,__be32 * p)1294 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1295 {
1296 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1297 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1298
1299 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1300 return 0;
1301 switch (resp->status) {
1302 case nfs_ok:
1303 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1304 return 0;
1305 if (!svcxdr_encode_fsstat3resok(xdr, resp))
1306 return 0;
1307 break;
1308 default:
1309 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1310 return 0;
1311 }
1312
1313 return 1;
1314 }
1315
1316 static bool
svcxdr_encode_fsinfo3resok(struct xdr_stream * xdr,const struct nfsd3_fsinfores * resp)1317 svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1318 const struct nfsd3_fsinfores *resp)
1319 {
1320 __be32 *p;
1321
1322 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1323 if (!p)
1324 return false;
1325 *p++ = cpu_to_be32(resp->f_rtmax);
1326 *p++ = cpu_to_be32(resp->f_rtpref);
1327 *p++ = cpu_to_be32(resp->f_rtmult);
1328 *p++ = cpu_to_be32(resp->f_wtmax);
1329 *p++ = cpu_to_be32(resp->f_wtpref);
1330 *p++ = cpu_to_be32(resp->f_wtmult);
1331 *p++ = cpu_to_be32(resp->f_dtpref);
1332 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1333 p = encode_nfstime3(p, &nfs3svc_time_delta);
1334 *p = cpu_to_be32(resp->f_properties);
1335
1336 return true;
1337 }
1338
1339 /* FSINFO */
1340 int
nfs3svc_encode_fsinfores(struct svc_rqst * rqstp,__be32 * p)1341 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1342 {
1343 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1344 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1345
1346 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1347 return 0;
1348 switch (resp->status) {
1349 case nfs_ok:
1350 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1351 return 0;
1352 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
1353 return 0;
1354 break;
1355 default:
1356 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1357 return 0;
1358 }
1359
1360 return 1;
1361 }
1362
1363 static bool
svcxdr_encode_pathconf3resok(struct xdr_stream * xdr,const struct nfsd3_pathconfres * resp)1364 svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1365 const struct nfsd3_pathconfres *resp)
1366 {
1367 __be32 *p;
1368
1369 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1370 if (!p)
1371 return false;
1372 *p++ = cpu_to_be32(resp->p_link_max);
1373 *p++ = cpu_to_be32(resp->p_name_max);
1374 p = xdr_encode_bool(p, resp->p_no_trunc);
1375 p = xdr_encode_bool(p, resp->p_chown_restricted);
1376 p = xdr_encode_bool(p, resp->p_case_insensitive);
1377 xdr_encode_bool(p, resp->p_case_preserving);
1378
1379 return true;
1380 }
1381
1382 /* PATHCONF */
1383 int
nfs3svc_encode_pathconfres(struct svc_rqst * rqstp,__be32 * p)1384 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1385 {
1386 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1387 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1388
1389 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1390 return 0;
1391 switch (resp->status) {
1392 case nfs_ok:
1393 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1394 return 0;
1395 if (!svcxdr_encode_pathconf3resok(xdr, resp))
1396 return 0;
1397 break;
1398 default:
1399 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1400 return 0;
1401 }
1402
1403 return 1;
1404 }
1405
1406 /* COMMIT */
1407 int
nfs3svc_encode_commitres(struct svc_rqst * rqstp,__be32 * p)1408 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1409 {
1410 struct xdr_stream *xdr = &rqstp->rq_res_stream;
1411 struct nfsd3_commitres *resp = rqstp->rq_resp;
1412
1413 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1414 return 0;
1415 switch (resp->status) {
1416 case nfs_ok:
1417 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1418 return 0;
1419 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1420 return 0;
1421 break;
1422 default:
1423 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1424 return 0;
1425 }
1426
1427 return 1;
1428 }
1429
1430 /*
1431 * XDR release functions
1432 */
1433 void
nfs3svc_release_fhandle(struct svc_rqst * rqstp)1434 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1435 {
1436 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1437
1438 fh_put(&resp->fh);
1439 }
1440
1441 void
nfs3svc_release_fhandle2(struct svc_rqst * rqstp)1442 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1443 {
1444 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1445
1446 fh_put(&resp->fh1);
1447 fh_put(&resp->fh2);
1448 }
1449