]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/nfsd/nfs3xdr.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[mirror_ubuntu-hirsute-kernel.git] / fs / nfsd / nfs3xdr.c
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 #define NFSDDBG_FACILITY NFSDDBG_XDR
18
19
20 /*
21 * Mapping of S_IF* types to NFS file types
22 */
23 static u32 nfs3_ftypes[] = {
24 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
25 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
26 NF3REG, NF3BAD, NF3LNK, NF3BAD,
27 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
28 };
29
30
31 /*
32 * XDR functions for basic NFS types
33 */
34 static __be32 *
35 encode_time3(__be32 *p, struct timespec64 *time)
36 {
37 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
38 return p;
39 }
40
41 static __be32 *
42 decode_time3(__be32 *p, struct timespec64 *time)
43 {
44 time->tv_sec = ntohl(*p++);
45 time->tv_nsec = ntohl(*p++);
46 return p;
47 }
48
49 static __be32 *
50 decode_fh(__be32 *p, struct svc_fh *fhp)
51 {
52 unsigned int size;
53 fh_init(fhp, NFS3_FHSIZE);
54 size = ntohl(*p++);
55 if (size > NFS3_FHSIZE)
56 return NULL;
57
58 memcpy(&fhp->fh_handle.fh_base, p, size);
59 fhp->fh_handle.fh_size = size;
60 return p + XDR_QUADLEN(size);
61 }
62
63 /* Helper function for NFSv3 ACL code */
64 __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
65 {
66 return decode_fh(p, fhp);
67 }
68
69 static __be32 *
70 encode_fh(__be32 *p, struct svc_fh *fhp)
71 {
72 unsigned int size = fhp->fh_handle.fh_size;
73 *p++ = htonl(size);
74 if (size) p[XDR_QUADLEN(size)-1]=0;
75 memcpy(p, &fhp->fh_handle.fh_base, size);
76 return p + XDR_QUADLEN(size);
77 }
78
79 /*
80 * Decode a file name and make sure that the path contains
81 * no slashes or null bytes.
82 */
83 static __be32 *
84 decode_filename(__be32 *p, char **namp, unsigned int *lenp)
85 {
86 char *name;
87 unsigned int i;
88
89 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
90 for (i = 0, name = *namp; i < *lenp; i++, name++) {
91 if (*name == '\0' || *name == '/')
92 return NULL;
93 }
94 }
95
96 return p;
97 }
98
99 static __be32 *
100 decode_sattr3(__be32 *p, struct iattr *iap, struct user_namespace *userns)
101 {
102 u32 tmp;
103
104 iap->ia_valid = 0;
105
106 if (*p++) {
107 iap->ia_valid |= ATTR_MODE;
108 iap->ia_mode = ntohl(*p++);
109 }
110 if (*p++) {
111 iap->ia_uid = make_kuid(userns, ntohl(*p++));
112 if (uid_valid(iap->ia_uid))
113 iap->ia_valid |= ATTR_UID;
114 }
115 if (*p++) {
116 iap->ia_gid = make_kgid(userns, ntohl(*p++));
117 if (gid_valid(iap->ia_gid))
118 iap->ia_valid |= ATTR_GID;
119 }
120 if (*p++) {
121 u64 newsize;
122
123 iap->ia_valid |= ATTR_SIZE;
124 p = xdr_decode_hyper(p, &newsize);
125 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
126 }
127 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
128 iap->ia_valid |= ATTR_ATIME;
129 } else if (tmp == 2) { /* set to client time */
130 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
131 iap->ia_atime.tv_sec = ntohl(*p++);
132 iap->ia_atime.tv_nsec = ntohl(*p++);
133 }
134 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
135 iap->ia_valid |= ATTR_MTIME;
136 } else if (tmp == 2) { /* set to client time */
137 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
138 iap->ia_mtime.tv_sec = ntohl(*p++);
139 iap->ia_mtime.tv_nsec = ntohl(*p++);
140 }
141 return p;
142 }
143
144 static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
145 {
146 u64 f;
147 switch(fsid_source(fhp)) {
148 default:
149 case FSIDSOURCE_DEV:
150 p = xdr_encode_hyper(p, (u64)huge_encode_dev
151 (fhp->fh_dentry->d_sb->s_dev));
152 break;
153 case FSIDSOURCE_FSID:
154 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
155 break;
156 case FSIDSOURCE_UUID:
157 f = ((u64*)fhp->fh_export->ex_uuid)[0];
158 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
159 p = xdr_encode_hyper(p, f);
160 break;
161 }
162 return p;
163 }
164
165 static __be32 *
166 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
167 struct kstat *stat)
168 {
169 struct user_namespace *userns = nfsd_user_namespace(rqstp);
170 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
171 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
172 *p++ = htonl((u32) stat->nlink);
173 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
174 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
175 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
176 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
177 } else {
178 p = xdr_encode_hyper(p, (u64) stat->size);
179 }
180 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
181 *p++ = htonl((u32) MAJOR(stat->rdev));
182 *p++ = htonl((u32) MINOR(stat->rdev));
183 p = encode_fsid(p, fhp);
184 p = xdr_encode_hyper(p, stat->ino);
185 p = encode_time3(p, &stat->atime);
186 p = encode_time3(p, &stat->mtime);
187 p = encode_time3(p, &stat->ctime);
188
189 return p;
190 }
191
192 static __be32 *
193 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
194 {
195 /* Attributes to follow */
196 *p++ = xdr_one;
197 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
198 }
199
200 /*
201 * Encode post-operation attributes.
202 * The inode may be NULL if the call failed because of a stale file
203 * handle. In this case, no attributes are returned.
204 */
205 static __be32 *
206 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
207 {
208 struct dentry *dentry = fhp->fh_dentry;
209 if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) {
210 __be32 err;
211 struct kstat stat;
212
213 err = fh_getattr(fhp, &stat);
214 if (!err) {
215 *p++ = xdr_one; /* attributes follow */
216 lease_get_mtime(d_inode(dentry), &stat.mtime);
217 return encode_fattr3(rqstp, p, fhp, &stat);
218 }
219 }
220 *p++ = xdr_zero;
221 return p;
222 }
223
224 /* Helper for NFSv3 ACLs */
225 __be32 *
226 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
227 {
228 return encode_post_op_attr(rqstp, p, fhp);
229 }
230
231 /*
232 * Enocde weak cache consistency data
233 */
234 static __be32 *
235 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
236 {
237 struct dentry *dentry = fhp->fh_dentry;
238
239 if (dentry && d_really_is_positive(dentry) && fhp->fh_post_saved) {
240 if (fhp->fh_pre_saved) {
241 *p++ = xdr_one;
242 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
243 p = encode_time3(p, &fhp->fh_pre_mtime);
244 p = encode_time3(p, &fhp->fh_pre_ctime);
245 } else {
246 *p++ = xdr_zero;
247 }
248 return encode_saved_post_attr(rqstp, p, fhp);
249 }
250 /* no pre- or post-attrs */
251 *p++ = xdr_zero;
252 return encode_post_op_attr(rqstp, p, fhp);
253 }
254
255 /*
256 * Fill in the pre_op attr for the wcc data
257 */
258 void fill_pre_wcc(struct svc_fh *fhp)
259 {
260 struct inode *inode;
261 struct kstat stat;
262 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
263 __be32 err;
264
265 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
266 return;
267 inode = d_inode(fhp->fh_dentry);
268 err = fh_getattr(fhp, &stat);
269 if (err) {
270 /* Grab the times from inode anyway */
271 stat.mtime = inode->i_mtime;
272 stat.ctime = inode->i_ctime;
273 stat.size = inode->i_size;
274 }
275 if (v4)
276 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
277
278 fhp->fh_pre_mtime = stat.mtime;
279 fhp->fh_pre_ctime = stat.ctime;
280 fhp->fh_pre_size = stat.size;
281 fhp->fh_pre_saved = true;
282 }
283
284 /*
285 * Fill in the post_op attr for the wcc data
286 */
287 void fill_post_wcc(struct svc_fh *fhp)
288 {
289 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
290 struct inode *inode = d_inode(fhp->fh_dentry);
291 __be32 err;
292
293 if (fhp->fh_no_wcc)
294 return;
295
296 if (fhp->fh_post_saved)
297 printk("nfsd: inode locked twice during operation.\n");
298
299 err = fh_getattr(fhp, &fhp->fh_post_attr);
300 if (err) {
301 fhp->fh_post_saved = false;
302 fhp->fh_post_attr.ctime = inode->i_ctime;
303 } else
304 fhp->fh_post_saved = true;
305 if (v4)
306 fhp->fh_post_change =
307 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
308 }
309
310 /*
311 * XDR decode functions
312 */
313
314 int
315 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
316 {
317 struct nfsd_fhandle *args = rqstp->rq_argp;
318
319 p = decode_fh(p, &args->fh);
320 if (!p)
321 return 0;
322 return xdr_argsize_check(rqstp, p);
323 }
324
325 int
326 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
327 {
328 struct nfsd3_sattrargs *args = rqstp->rq_argp;
329
330 p = decode_fh(p, &args->fh);
331 if (!p)
332 return 0;
333 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
334
335 if ((args->check_guard = ntohl(*p++)) != 0) {
336 struct timespec64 time;
337 p = decode_time3(p, &time);
338 args->guardtime = time.tv_sec;
339 }
340
341 return xdr_argsize_check(rqstp, p);
342 }
343
344 int
345 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
346 {
347 struct nfsd3_diropargs *args = rqstp->rq_argp;
348
349 if (!(p = decode_fh(p, &args->fh))
350 || !(p = decode_filename(p, &args->name, &args->len)))
351 return 0;
352
353 return xdr_argsize_check(rqstp, p);
354 }
355
356 int
357 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
358 {
359 struct nfsd3_accessargs *args = rqstp->rq_argp;
360
361 p = decode_fh(p, &args->fh);
362 if (!p)
363 return 0;
364 args->access = ntohl(*p++);
365
366 return xdr_argsize_check(rqstp, p);
367 }
368
369 int
370 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
371 {
372 struct nfsd3_readargs *args = rqstp->rq_argp;
373 unsigned int len;
374 int v;
375 u32 max_blocksize = svc_max_payload(rqstp);
376
377 p = decode_fh(p, &args->fh);
378 if (!p)
379 return 0;
380 p = xdr_decode_hyper(p, &args->offset);
381
382 args->count = ntohl(*p++);
383 len = min(args->count, max_blocksize);
384
385 /* set up the kvec */
386 v=0;
387 while (len > 0) {
388 struct page *p = *(rqstp->rq_next_page++);
389
390 rqstp->rq_vec[v].iov_base = page_address(p);
391 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
392 len -= rqstp->rq_vec[v].iov_len;
393 v++;
394 }
395 args->vlen = v;
396 return xdr_argsize_check(rqstp, p);
397 }
398
399 int
400 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
401 {
402 struct nfsd3_writeargs *args = rqstp->rq_argp;
403 unsigned int len, hdr, dlen;
404 u32 max_blocksize = svc_max_payload(rqstp);
405 struct kvec *head = rqstp->rq_arg.head;
406 struct kvec *tail = rqstp->rq_arg.tail;
407
408 p = decode_fh(p, &args->fh);
409 if (!p)
410 return 0;
411 p = xdr_decode_hyper(p, &args->offset);
412
413 args->count = ntohl(*p++);
414 args->stable = ntohl(*p++);
415 len = args->len = ntohl(*p++);
416 if ((void *)p > head->iov_base + head->iov_len)
417 return 0;
418 /*
419 * The count must equal the amount of data passed.
420 */
421 if (args->count != args->len)
422 return 0;
423
424 /*
425 * Check to make sure that we got the right number of
426 * bytes.
427 */
428 hdr = (void*)p - head->iov_base;
429 dlen = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len - hdr;
430 /*
431 * Round the length of the data which was specified up to
432 * the next multiple of XDR units and then compare that
433 * against the length which was actually received.
434 * Note that when RPCSEC/GSS (for example) is used, the
435 * data buffer can be padded so dlen might be larger
436 * than required. It must never be smaller.
437 */
438 if (dlen < XDR_QUADLEN(len)*4)
439 return 0;
440
441 if (args->count > max_blocksize) {
442 args->count = max_blocksize;
443 len = args->len = max_blocksize;
444 }
445
446 args->first.iov_base = (void *)p;
447 args->first.iov_len = head->iov_len - hdr;
448 return 1;
449 }
450
451 int
452 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
453 {
454 struct nfsd3_createargs *args = rqstp->rq_argp;
455
456 if (!(p = decode_fh(p, &args->fh))
457 || !(p = decode_filename(p, &args->name, &args->len)))
458 return 0;
459
460 switch (args->createmode = ntohl(*p++)) {
461 case NFS3_CREATE_UNCHECKED:
462 case NFS3_CREATE_GUARDED:
463 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
464 break;
465 case NFS3_CREATE_EXCLUSIVE:
466 args->verf = p;
467 p += 2;
468 break;
469 default:
470 return 0;
471 }
472
473 return xdr_argsize_check(rqstp, p);
474 }
475
476 int
477 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
478 {
479 struct nfsd3_createargs *args = rqstp->rq_argp;
480
481 if (!(p = decode_fh(p, &args->fh)) ||
482 !(p = decode_filename(p, &args->name, &args->len)))
483 return 0;
484 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
485
486 return xdr_argsize_check(rqstp, p);
487 }
488
489 int
490 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
491 {
492 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
493 char *base = (char *)p;
494 size_t dlen;
495
496 if (!(p = decode_fh(p, &args->ffh)) ||
497 !(p = decode_filename(p, &args->fname, &args->flen)))
498 return 0;
499 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
500
501 args->tlen = ntohl(*p++);
502
503 args->first.iov_base = p;
504 args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
505 args->first.iov_len -= (char *)p - base;
506
507 dlen = args->first.iov_len + rqstp->rq_arg.page_len +
508 rqstp->rq_arg.tail[0].iov_len;
509 if (dlen < XDR_QUADLEN(args->tlen) << 2)
510 return 0;
511 return 1;
512 }
513
514 int
515 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
516 {
517 struct nfsd3_mknodargs *args = rqstp->rq_argp;
518
519 if (!(p = decode_fh(p, &args->fh))
520 || !(p = decode_filename(p, &args->name, &args->len)))
521 return 0;
522
523 args->ftype = ntohl(*p++);
524
525 if (args->ftype == NF3BLK || args->ftype == NF3CHR
526 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
527 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
528
529 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
530 args->major = ntohl(*p++);
531 args->minor = ntohl(*p++);
532 }
533
534 return xdr_argsize_check(rqstp, p);
535 }
536
537 int
538 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
539 {
540 struct nfsd3_renameargs *args = rqstp->rq_argp;
541
542 if (!(p = decode_fh(p, &args->ffh))
543 || !(p = decode_filename(p, &args->fname, &args->flen))
544 || !(p = decode_fh(p, &args->tfh))
545 || !(p = decode_filename(p, &args->tname, &args->tlen)))
546 return 0;
547
548 return xdr_argsize_check(rqstp, p);
549 }
550
551 int
552 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
553 {
554 struct nfsd3_readlinkargs *args = rqstp->rq_argp;
555
556 p = decode_fh(p, &args->fh);
557 if (!p)
558 return 0;
559 args->buffer = page_address(*(rqstp->rq_next_page++));
560
561 return xdr_argsize_check(rqstp, p);
562 }
563
564 int
565 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
566 {
567 struct nfsd3_linkargs *args = rqstp->rq_argp;
568
569 if (!(p = decode_fh(p, &args->ffh))
570 || !(p = decode_fh(p, &args->tfh))
571 || !(p = decode_filename(p, &args->tname, &args->tlen)))
572 return 0;
573
574 return xdr_argsize_check(rqstp, p);
575 }
576
577 int
578 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
579 {
580 struct nfsd3_readdirargs *args = rqstp->rq_argp;
581 int len;
582 u32 max_blocksize = svc_max_payload(rqstp);
583
584 p = decode_fh(p, &args->fh);
585 if (!p)
586 return 0;
587 p = xdr_decode_hyper(p, &args->cookie);
588 args->verf = p; p += 2;
589 args->dircount = ~0;
590 args->count = ntohl(*p++);
591 len = args->count = min_t(u32, args->count, max_blocksize);
592
593 while (len > 0) {
594 struct page *p = *(rqstp->rq_next_page++);
595 if (!args->buffer)
596 args->buffer = page_address(p);
597 len -= PAGE_SIZE;
598 }
599
600 return xdr_argsize_check(rqstp, p);
601 }
602
603 int
604 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
605 {
606 struct nfsd3_readdirargs *args = rqstp->rq_argp;
607 int len;
608 u32 max_blocksize = svc_max_payload(rqstp);
609
610 p = decode_fh(p, &args->fh);
611 if (!p)
612 return 0;
613 p = xdr_decode_hyper(p, &args->cookie);
614 args->verf = p; p += 2;
615 args->dircount = ntohl(*p++);
616 args->count = ntohl(*p++);
617
618 len = args->count = min(args->count, max_blocksize);
619 while (len > 0) {
620 struct page *p = *(rqstp->rq_next_page++);
621 if (!args->buffer)
622 args->buffer = page_address(p);
623 len -= PAGE_SIZE;
624 }
625
626 return xdr_argsize_check(rqstp, p);
627 }
628
629 int
630 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
631 {
632 struct nfsd3_commitargs *args = rqstp->rq_argp;
633 p = decode_fh(p, &args->fh);
634 if (!p)
635 return 0;
636 p = xdr_decode_hyper(p, &args->offset);
637 args->count = ntohl(*p++);
638
639 return xdr_argsize_check(rqstp, p);
640 }
641
642 /*
643 * XDR encode functions
644 */
645
646 /* GETATTR */
647 int
648 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
649 {
650 struct nfsd3_attrstat *resp = rqstp->rq_resp;
651
652 *p++ = resp->status;
653 if (resp->status == 0) {
654 lease_get_mtime(d_inode(resp->fh.fh_dentry),
655 &resp->stat.mtime);
656 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
657 }
658 return xdr_ressize_check(rqstp, p);
659 }
660
661 /* SETATTR, REMOVE, RMDIR */
662 int
663 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
664 {
665 struct nfsd3_attrstat *resp = rqstp->rq_resp;
666
667 *p++ = resp->status;
668 p = encode_wcc_data(rqstp, p, &resp->fh);
669 return xdr_ressize_check(rqstp, p);
670 }
671
672 /* LOOKUP */
673 int
674 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
675 {
676 struct nfsd3_diropres *resp = rqstp->rq_resp;
677
678 *p++ = resp->status;
679 if (resp->status == 0) {
680 p = encode_fh(p, &resp->fh);
681 p = encode_post_op_attr(rqstp, p, &resp->fh);
682 }
683 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
684 return xdr_ressize_check(rqstp, p);
685 }
686
687 /* ACCESS */
688 int
689 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
690 {
691 struct nfsd3_accessres *resp = rqstp->rq_resp;
692
693 *p++ = resp->status;
694 p = encode_post_op_attr(rqstp, p, &resp->fh);
695 if (resp->status == 0)
696 *p++ = htonl(resp->access);
697 return xdr_ressize_check(rqstp, p);
698 }
699
700 /* READLINK */
701 int
702 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
703 {
704 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
705 struct kvec *head = rqstp->rq_res.head;
706
707 *p++ = resp->status;
708 p = encode_post_op_attr(rqstp, p, &resp->fh);
709 if (resp->status == 0) {
710 *p++ = htonl(resp->len);
711 xdr_ressize_check(rqstp, p);
712 rqstp->rq_res.page_len = resp->len;
713 if (resp->len & 3) {
714 /* need to pad the tail */
715 rqstp->rq_res.tail[0].iov_base = p;
716 *p = 0;
717 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
718 }
719 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len))
720 return 0;
721 return 1;
722 } else
723 return xdr_ressize_check(rqstp, p);
724 }
725
726 /* READ */
727 int
728 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
729 {
730 struct nfsd3_readres *resp = rqstp->rq_resp;
731 struct kvec *head = rqstp->rq_res.head;
732
733 *p++ = resp->status;
734 p = encode_post_op_attr(rqstp, p, &resp->fh);
735 if (resp->status == 0) {
736 *p++ = htonl(resp->count);
737 *p++ = htonl(resp->eof);
738 *p++ = htonl(resp->count); /* xdr opaque count */
739 xdr_ressize_check(rqstp, p);
740 /* now update rqstp->rq_res to reflect data as well */
741 rqstp->rq_res.page_len = resp->count;
742 if (resp->count & 3) {
743 /* need to pad the tail */
744 rqstp->rq_res.tail[0].iov_base = p;
745 *p = 0;
746 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
747 }
748 if (svc_encode_result_payload(rqstp, head->iov_len,
749 resp->count))
750 return 0;
751 return 1;
752 } else
753 return xdr_ressize_check(rqstp, p);
754 }
755
756 /* WRITE */
757 int
758 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
759 {
760 struct nfsd3_writeres *resp = rqstp->rq_resp;
761
762 *p++ = resp->status;
763 p = encode_wcc_data(rqstp, p, &resp->fh);
764 if (resp->status == 0) {
765 *p++ = htonl(resp->count);
766 *p++ = htonl(resp->committed);
767 *p++ = resp->verf[0];
768 *p++ = resp->verf[1];
769 }
770 return xdr_ressize_check(rqstp, p);
771 }
772
773 /* CREATE, MKDIR, SYMLINK, MKNOD */
774 int
775 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
776 {
777 struct nfsd3_diropres *resp = rqstp->rq_resp;
778
779 *p++ = resp->status;
780 if (resp->status == 0) {
781 *p++ = xdr_one;
782 p = encode_fh(p, &resp->fh);
783 p = encode_post_op_attr(rqstp, p, &resp->fh);
784 }
785 p = encode_wcc_data(rqstp, p, &resp->dirfh);
786 return xdr_ressize_check(rqstp, p);
787 }
788
789 /* RENAME */
790 int
791 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
792 {
793 struct nfsd3_renameres *resp = rqstp->rq_resp;
794
795 *p++ = resp->status;
796 p = encode_wcc_data(rqstp, p, &resp->ffh);
797 p = encode_wcc_data(rqstp, p, &resp->tfh);
798 return xdr_ressize_check(rqstp, p);
799 }
800
801 /* LINK */
802 int
803 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
804 {
805 struct nfsd3_linkres *resp = rqstp->rq_resp;
806
807 *p++ = resp->status;
808 p = encode_post_op_attr(rqstp, p, &resp->fh);
809 p = encode_wcc_data(rqstp, p, &resp->tfh);
810 return xdr_ressize_check(rqstp, p);
811 }
812
813 /* READDIR */
814 int
815 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
816 {
817 struct nfsd3_readdirres *resp = rqstp->rq_resp;
818
819 *p++ = resp->status;
820 p = encode_post_op_attr(rqstp, p, &resp->fh);
821
822 if (resp->status == 0) {
823 /* stupid readdir cookie */
824 memcpy(p, resp->verf, 8); p += 2;
825 xdr_ressize_check(rqstp, p);
826 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
827 return 1; /*No room for trailer */
828 rqstp->rq_res.page_len = (resp->count) << 2;
829
830 /* add the 'tail' to the end of the 'head' page - page 0. */
831 rqstp->rq_res.tail[0].iov_base = p;
832 *p++ = 0; /* no more entries */
833 *p++ = htonl(resp->common.err == nfserr_eof);
834 rqstp->rq_res.tail[0].iov_len = 2<<2;
835 return 1;
836 } else
837 return xdr_ressize_check(rqstp, p);
838 }
839
840 static __be32 *
841 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
842 int namlen, u64 ino)
843 {
844 *p++ = xdr_one; /* mark entry present */
845 p = xdr_encode_hyper(p, ino); /* file id */
846 p = xdr_encode_array(p, name, namlen);/* name length & name */
847
848 cd->offset = p; /* remember pointer */
849 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
850
851 return p;
852 }
853
854 static __be32
855 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
856 const char *name, int namlen, u64 ino)
857 {
858 struct svc_export *exp;
859 struct dentry *dparent, *dchild;
860 __be32 rv = nfserr_noent;
861
862 dparent = cd->fh.fh_dentry;
863 exp = cd->fh.fh_export;
864
865 if (isdotent(name, namlen)) {
866 if (namlen == 2) {
867 dchild = dget_parent(dparent);
868 /*
869 * Don't return filehandle for ".." if we're at
870 * the filesystem or export root:
871 */
872 if (dchild == dparent)
873 goto out;
874 if (dparent == exp->ex_path.dentry)
875 goto out;
876 } else
877 dchild = dget(dparent);
878 } else
879 dchild = lookup_positive_unlocked(name, dparent, namlen);
880 if (IS_ERR(dchild))
881 return rv;
882 if (d_mountpoint(dchild))
883 goto out;
884 if (dchild->d_inode->i_ino != ino)
885 goto out;
886 rv = fh_compose(fhp, exp, dchild, &cd->fh);
887 out:
888 dput(dchild);
889 return rv;
890 }
891
892 static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino)
893 {
894 struct svc_fh *fh = &cd->scratch;
895 __be32 err;
896
897 fh_init(fh, NFS3_FHSIZE);
898 err = compose_entry_fh(cd, fh, name, namlen, ino);
899 if (err) {
900 *p++ = 0;
901 *p++ = 0;
902 goto out;
903 }
904 p = encode_post_op_attr(cd->rqstp, p, fh);
905 *p++ = xdr_one; /* yes, a file handle follows */
906 p = encode_fh(p, fh);
907 out:
908 fh_put(fh);
909 return p;
910 }
911
912 /*
913 * Encode a directory entry. This one works for both normal readdir
914 * and readdirplus.
915 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
916 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
917 *
918 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
919 * file handle.
920 */
921
922 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
923 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
924 static int
925 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
926 loff_t offset, u64 ino, unsigned int d_type, int plus)
927 {
928 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
929 common);
930 __be32 *p = cd->buffer;
931 caddr_t curr_page_addr = NULL;
932 struct page ** page;
933 int slen; /* string (name) length */
934 int elen; /* estimated entry length in words */
935 int num_entry_words = 0; /* actual number of words */
936
937 if (cd->offset) {
938 u64 offset64 = offset;
939
940 if (unlikely(cd->offset1)) {
941 /* we ended up with offset on a page boundary */
942 *cd->offset = htonl(offset64 >> 32);
943 *cd->offset1 = htonl(offset64 & 0xffffffff);
944 cd->offset1 = NULL;
945 } else {
946 xdr_encode_hyper(cd->offset, offset64);
947 }
948 cd->offset = NULL;
949 }
950
951 /*
952 dprintk("encode_entry(%.*s @%ld%s)\n",
953 namlen, name, (long) offset, plus? " plus" : "");
954 */
955
956 /* truncate filename if too long */
957 namlen = min(namlen, NFS3_MAXNAMLEN);
958
959 slen = XDR_QUADLEN(namlen);
960 elen = slen + NFS3_ENTRY_BAGGAGE
961 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
962
963 if (cd->buflen < elen) {
964 cd->common.err = nfserr_toosmall;
965 return -EINVAL;
966 }
967
968 /* determine which page in rq_respages[] we are currently filling */
969 for (page = cd->rqstp->rq_respages + 1;
970 page < cd->rqstp->rq_next_page; page++) {
971 curr_page_addr = page_address(*page);
972
973 if (((caddr_t)cd->buffer >= curr_page_addr) &&
974 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
975 break;
976 }
977
978 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
979 /* encode entry in current page */
980
981 p = encode_entry_baggage(cd, p, name, namlen, ino);
982
983 if (plus)
984 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
985 num_entry_words = p - cd->buffer;
986 } else if (*(page+1) != NULL) {
987 /* temporarily encode entry into next page, then move back to
988 * current and next page in rq_respages[] */
989 __be32 *p1, *tmp;
990 int len1, len2;
991
992 /* grab next page for temporary storage of entry */
993 p1 = tmp = page_address(*(page+1));
994
995 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
996
997 if (plus)
998 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
999
1000 /* determine entry word length and lengths to go in pages */
1001 num_entry_words = p1 - tmp;
1002 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1003 if ((num_entry_words << 2) < len1) {
1004 /* the actual number of words in the entry is less
1005 * than elen and can still fit in the current page
1006 */
1007 memmove(p, tmp, num_entry_words << 2);
1008 p += num_entry_words;
1009
1010 /* update offset */
1011 cd->offset = cd->buffer + (cd->offset - tmp);
1012 } else {
1013 unsigned int offset_r = (cd->offset - tmp) << 2;
1014
1015 /* update pointer to offset location.
1016 * This is a 64bit quantity, so we need to
1017 * deal with 3 cases:
1018 * - entirely in first page
1019 * - entirely in second page
1020 * - 4 bytes in each page
1021 */
1022 if (offset_r + 8 <= len1) {
1023 cd->offset = p + (cd->offset - tmp);
1024 } else if (offset_r >= len1) {
1025 cd->offset -= len1 >> 2;
1026 } else {
1027 /* sitting on the fence */
1028 BUG_ON(offset_r != len1 - 4);
1029 cd->offset = p + (cd->offset - tmp);
1030 cd->offset1 = tmp;
1031 }
1032
1033 len2 = (num_entry_words << 2) - len1;
1034
1035 /* move from temp page to current and next pages */
1036 memmove(p, tmp, len1);
1037 memmove(tmp, (caddr_t)tmp+len1, len2);
1038
1039 p = tmp + (len2 >> 2);
1040 }
1041 }
1042 else {
1043 cd->common.err = nfserr_toosmall;
1044 return -EINVAL;
1045 }
1046
1047 cd->buflen -= num_entry_words;
1048 cd->buffer = p;
1049 cd->common.err = nfs_ok;
1050 return 0;
1051
1052 }
1053
1054 int
1055 nfs3svc_encode_entry(void *cd, const char *name,
1056 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1057 {
1058 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1059 }
1060
1061 int
1062 nfs3svc_encode_entry_plus(void *cd, const char *name,
1063 int namlen, loff_t offset, u64 ino,
1064 unsigned int d_type)
1065 {
1066 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1067 }
1068
1069 /* FSSTAT */
1070 int
1071 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
1072 {
1073 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1074 struct kstatfs *s = &resp->stats;
1075 u64 bs = s->f_bsize;
1076
1077 *p++ = resp->status;
1078 *p++ = xdr_zero; /* no post_op_attr */
1079
1080 if (resp->status == 0) {
1081 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1082 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1083 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1084 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1085 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1086 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1087 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1088 }
1089 return xdr_ressize_check(rqstp, p);
1090 }
1091
1092 /* FSINFO */
1093 int
1094 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
1095 {
1096 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1097
1098 *p++ = resp->status;
1099 *p++ = xdr_zero; /* no post_op_attr */
1100
1101 if (resp->status == 0) {
1102 *p++ = htonl(resp->f_rtmax);
1103 *p++ = htonl(resp->f_rtpref);
1104 *p++ = htonl(resp->f_rtmult);
1105 *p++ = htonl(resp->f_wtmax);
1106 *p++ = htonl(resp->f_wtpref);
1107 *p++ = htonl(resp->f_wtmult);
1108 *p++ = htonl(resp->f_dtpref);
1109 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1110 *p++ = xdr_one;
1111 *p++ = xdr_zero;
1112 *p++ = htonl(resp->f_properties);
1113 }
1114
1115 return xdr_ressize_check(rqstp, p);
1116 }
1117
1118 /* PATHCONF */
1119 int
1120 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
1121 {
1122 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1123
1124 *p++ = resp->status;
1125 *p++ = xdr_zero; /* no post_op_attr */
1126
1127 if (resp->status == 0) {
1128 *p++ = htonl(resp->p_link_max);
1129 *p++ = htonl(resp->p_name_max);
1130 *p++ = htonl(resp->p_no_trunc);
1131 *p++ = htonl(resp->p_chown_restricted);
1132 *p++ = htonl(resp->p_case_insensitive);
1133 *p++ = htonl(resp->p_case_preserving);
1134 }
1135
1136 return xdr_ressize_check(rqstp, p);
1137 }
1138
1139 /* COMMIT */
1140 int
1141 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
1142 {
1143 struct nfsd3_commitres *resp = rqstp->rq_resp;
1144
1145 *p++ = resp->status;
1146 p = encode_wcc_data(rqstp, p, &resp->fh);
1147 /* Write verifier */
1148 if (resp->status == 0) {
1149 *p++ = resp->verf[0];
1150 *p++ = resp->verf[1];
1151 }
1152 return xdr_ressize_check(rqstp, p);
1153 }
1154
1155 /*
1156 * XDR release functions
1157 */
1158 void
1159 nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1160 {
1161 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1162
1163 fh_put(&resp->fh);
1164 }
1165
1166 void
1167 nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1168 {
1169 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1170
1171 fh_put(&resp->fh1);
1172 fh_put(&resp->fh2);
1173 }