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