Linux 3.12.5
[linux/fpc-iii.git] / fs / nfsd / nfs4xdr.c
blobecc735e30beaa16929611c269dc7530eddd05463
1 /*
2 * Server-side XDR for NFSv4
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
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.
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.
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>
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"
58 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59 #include <linux/security.h>
60 #endif
63 #define NFSDDBG_FACILITY NFSDDBG_XDR
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
70 #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71 #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
73 static __be32
74 check_filename(char *str, int len)
76 int i;
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;
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
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; \
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)
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; \
140 } while (0)
142 static void next_decode_page(struct nfsd4_compoundargs *argp)
144 argp->p = page_address(argp->pagelist[0]);
145 argp->pagelist++;
146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + (argp->pagelen>>2);
148 argp->pagelen = 0;
149 } else {
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
155 static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
160 unsigned int avail = (char *)argp->end - (char *)argp->p;
161 __be32 *p;
162 if (avail + argp->pagelen < nbytes)
163 return NULL;
164 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
165 return NULL;
166 /* ok, we can do it with the current plus the next page */
167 if (nbytes <= sizeof(argp->tmp))
168 p = argp->tmp;
169 else {
170 kfree(argp->tmpp);
171 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
172 if (!p)
173 return NULL;
177 * The following memcpy is safe because read_buf is always
178 * called with nbytes > avail, and the two cases above both
179 * guarantee p points to at least nbytes bytes.
181 memcpy(p, argp->p, avail);
182 next_decode_page(argp);
183 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
184 argp->p += XDR_QUADLEN(nbytes - avail);
185 return p;
188 static int zero_clientid(clientid_t *clid)
190 return (clid->cl_boot == 0) && (clid->cl_id == 0);
193 static int
194 defer_free(struct nfsd4_compoundargs *argp,
195 void (*release)(const void *), void *p)
197 struct tmpbuf *tb;
199 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
200 if (!tb)
201 return -ENOMEM;
202 tb->buf = p;
203 tb->release = release;
204 tb->next = argp->to_free;
205 argp->to_free = tb;
206 return 0;
209 static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
211 if (p == argp->tmp) {
212 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
213 if (!p)
214 return NULL;
215 } else {
216 BUG_ON(p != argp->tmpp);
217 argp->tmpp = NULL;
219 if (defer_free(argp, kfree, p)) {
220 kfree(p);
221 return NULL;
222 } else
223 return (char *)p;
226 static __be32
227 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
229 u32 bmlen;
230 DECODE_HEAD;
232 bmval[0] = 0;
233 bmval[1] = 0;
234 bmval[2] = 0;
236 READ_BUF(4);
237 READ32(bmlen);
238 if (bmlen > 1000)
239 goto xdr_error;
241 READ_BUF(bmlen << 2);
242 if (bmlen > 0)
243 READ32(bmval[0]);
244 if (bmlen > 1)
245 READ32(bmval[1]);
246 if (bmlen > 2)
247 READ32(bmval[2]);
249 DECODE_TAIL;
252 static __be32
253 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
254 struct iattr *iattr, struct nfs4_acl **acl,
255 struct xdr_netobj *label)
257 int expected_len, len = 0;
258 u32 dummy32;
259 char *buf;
260 int host_err;
262 DECODE_HEAD;
263 iattr->ia_valid = 0;
264 if ((status = nfsd4_decode_bitmap(argp, bmval)))
265 return status;
267 READ_BUF(4);
268 READ32(expected_len);
270 if (bmval[0] & FATTR4_WORD0_SIZE) {
271 READ_BUF(8);
272 len += 8;
273 READ64(iattr->ia_size);
274 iattr->ia_valid |= ATTR_SIZE;
276 if (bmval[0] & FATTR4_WORD0_ACL) {
277 u32 nace;
278 struct nfs4_ace *ace;
280 READ_BUF(4); len += 4;
281 READ32(nace);
283 if (nace > NFS4_ACL_MAX)
284 return nfserr_resource;
286 *acl = nfs4_acl_new(nace);
287 if (*acl == NULL) {
288 host_err = -ENOMEM;
289 goto out_nfserr;
291 defer_free(argp, kfree, *acl);
293 (*acl)->naces = nace;
294 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
295 READ_BUF(16); len += 16;
296 READ32(ace->type);
297 READ32(ace->flag);
298 READ32(ace->access_mask);
299 READ32(dummy32);
300 READ_BUF(dummy32);
301 len += XDR_QUADLEN(dummy32) << 2;
302 READMEM(buf, dummy32);
303 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
304 status = nfs_ok;
305 if (ace->whotype != NFS4_ACL_WHO_NAMED)
307 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
308 status = nfsd_map_name_to_gid(argp->rqstp,
309 buf, dummy32, &ace->who_gid);
310 else
311 status = nfsd_map_name_to_uid(argp->rqstp,
312 buf, dummy32, &ace->who_uid);
313 if (status)
314 return status;
316 } else
317 *acl = NULL;
318 if (bmval[1] & FATTR4_WORD1_MODE) {
319 READ_BUF(4);
320 len += 4;
321 READ32(iattr->ia_mode);
322 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
323 iattr->ia_valid |= ATTR_MODE;
325 if (bmval[1] & FATTR4_WORD1_OWNER) {
326 READ_BUF(4);
327 len += 4;
328 READ32(dummy32);
329 READ_BUF(dummy32);
330 len += (XDR_QUADLEN(dummy32) << 2);
331 READMEM(buf, dummy32);
332 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
333 return status;
334 iattr->ia_valid |= ATTR_UID;
336 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
337 READ_BUF(4);
338 len += 4;
339 READ32(dummy32);
340 READ_BUF(dummy32);
341 len += (XDR_QUADLEN(dummy32) << 2);
342 READMEM(buf, dummy32);
343 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
344 return status;
345 iattr->ia_valid |= ATTR_GID;
347 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
348 READ_BUF(4);
349 len += 4;
350 READ32(dummy32);
351 switch (dummy32) {
352 case NFS4_SET_TO_CLIENT_TIME:
353 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
354 all 32 bits of 'nseconds'. */
355 READ_BUF(12);
356 len += 12;
357 READ64(iattr->ia_atime.tv_sec);
358 READ32(iattr->ia_atime.tv_nsec);
359 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
360 return nfserr_inval;
361 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
362 break;
363 case NFS4_SET_TO_SERVER_TIME:
364 iattr->ia_valid |= ATTR_ATIME;
365 break;
366 default:
367 goto xdr_error;
370 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
371 READ_BUF(4);
372 len += 4;
373 READ32(dummy32);
374 switch (dummy32) {
375 case NFS4_SET_TO_CLIENT_TIME:
376 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
377 all 32 bits of 'nseconds'. */
378 READ_BUF(12);
379 len += 12;
380 READ64(iattr->ia_mtime.tv_sec);
381 READ32(iattr->ia_mtime.tv_nsec);
382 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
383 return nfserr_inval;
384 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
385 break;
386 case NFS4_SET_TO_SERVER_TIME:
387 iattr->ia_valid |= ATTR_MTIME;
388 break;
389 default:
390 goto xdr_error;
394 label->len = 0;
395 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
396 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
397 READ_BUF(4);
398 len += 4;
399 READ32(dummy32); /* lfs: we don't use it */
400 READ_BUF(4);
401 len += 4;
402 READ32(dummy32); /* pi: we don't use it either */
403 READ_BUF(4);
404 len += 4;
405 READ32(dummy32);
406 READ_BUF(dummy32);
407 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
408 return nfserr_badlabel;
409 len += (XDR_QUADLEN(dummy32) << 2);
410 READMEM(buf, dummy32);
411 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
412 if (!label->data)
413 return nfserr_jukebox;
414 label->len = dummy32;
415 defer_free(argp, kfree, label->data);
416 memcpy(label->data, buf, dummy32);
418 #endif
420 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
421 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
422 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
423 READ_BUF(expected_len - len);
424 else if (len != expected_len)
425 goto xdr_error;
427 DECODE_TAIL;
429 out_nfserr:
430 status = nfserrno(host_err);
431 goto out;
434 static __be32
435 nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
437 DECODE_HEAD;
439 READ_BUF(sizeof(stateid_t));
440 READ32(sid->si_generation);
441 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
443 DECODE_TAIL;
446 static __be32
447 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
449 DECODE_HEAD;
451 READ_BUF(4);
452 READ32(access->ac_req_access);
454 DECODE_TAIL;
457 static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
459 DECODE_HEAD;
460 u32 dummy, uid, gid;
461 char *machine_name;
462 int i;
463 int nr_secflavs;
465 /* callback_sec_params4 */
466 READ_BUF(4);
467 READ32(nr_secflavs);
468 if (nr_secflavs)
469 cbs->flavor = (u32)(-1);
470 else
471 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
472 cbs->flavor = 0;
473 for (i = 0; i < nr_secflavs; ++i) {
474 READ_BUF(4);
475 READ32(dummy);
476 switch (dummy) {
477 case RPC_AUTH_NULL:
478 /* Nothing to read */
479 if (cbs->flavor == (u32)(-1))
480 cbs->flavor = RPC_AUTH_NULL;
481 break;
482 case RPC_AUTH_UNIX:
483 READ_BUF(8);
484 /* stamp */
485 READ32(dummy);
487 /* machine name */
488 READ32(dummy);
489 READ_BUF(dummy);
490 SAVEMEM(machine_name, dummy);
492 /* uid, gid */
493 READ_BUF(8);
494 READ32(uid);
495 READ32(gid);
497 /* more gids */
498 READ_BUF(4);
499 READ32(dummy);
500 READ_BUF(dummy * 4);
501 if (cbs->flavor == (u32)(-1)) {
502 kuid_t kuid = make_kuid(&init_user_ns, uid);
503 kgid_t kgid = make_kgid(&init_user_ns, gid);
504 if (uid_valid(kuid) && gid_valid(kgid)) {
505 cbs->uid = kuid;
506 cbs->gid = kgid;
507 cbs->flavor = RPC_AUTH_UNIX;
508 } else {
509 dprintk("RPC_AUTH_UNIX with invalid"
510 "uid or gid ignoring!\n");
513 break;
514 case RPC_AUTH_GSS:
515 dprintk("RPC_AUTH_GSS callback secflavor "
516 "not supported!\n");
517 READ_BUF(8);
518 /* gcbp_service */
519 READ32(dummy);
520 /* gcbp_handle_from_server */
521 READ32(dummy);
522 READ_BUF(dummy);
523 p += XDR_QUADLEN(dummy);
524 /* gcbp_handle_from_client */
525 READ_BUF(4);
526 READ32(dummy);
527 READ_BUF(dummy);
528 break;
529 default:
530 dprintk("Illegal callback secflavor\n");
531 return nfserr_inval;
534 DECODE_TAIL;
537 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
539 DECODE_HEAD;
541 READ_BUF(4);
542 READ32(bc->bc_cb_program);
543 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
545 DECODE_TAIL;
548 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
550 DECODE_HEAD;
552 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
553 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
554 READ32(bcts->dir);
555 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
556 * could help us figure out we should be using it. */
557 DECODE_TAIL;
560 static __be32
561 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
563 DECODE_HEAD;
565 READ_BUF(4);
566 READ32(close->cl_seqid);
567 return nfsd4_decode_stateid(argp, &close->cl_stateid);
569 DECODE_TAIL;
573 static __be32
574 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
576 DECODE_HEAD;
578 READ_BUF(12);
579 READ64(commit->co_offset);
580 READ32(commit->co_count);
582 DECODE_TAIL;
585 static __be32
586 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
588 DECODE_HEAD;
590 READ_BUF(4);
591 READ32(create->cr_type);
592 switch (create->cr_type) {
593 case NF4LNK:
594 READ_BUF(4);
595 READ32(create->cr_linklen);
596 READ_BUF(create->cr_linklen);
597 SAVEMEM(create->cr_linkname, create->cr_linklen);
598 break;
599 case NF4BLK:
600 case NF4CHR:
601 READ_BUF(8);
602 READ32(create->cr_specdata1);
603 READ32(create->cr_specdata2);
604 break;
605 case NF4SOCK:
606 case NF4FIFO:
607 case NF4DIR:
608 default:
609 break;
612 READ_BUF(4);
613 READ32(create->cr_namelen);
614 READ_BUF(create->cr_namelen);
615 SAVEMEM(create->cr_name, create->cr_namelen);
616 if ((status = check_filename(create->cr_name, create->cr_namelen)))
617 return status;
619 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
620 &create->cr_acl, &create->cr_label);
621 if (status)
622 goto out;
624 DECODE_TAIL;
627 static inline __be32
628 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
630 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
633 static inline __be32
634 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
636 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
639 static __be32
640 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
642 DECODE_HEAD;
644 READ_BUF(4);
645 READ32(link->li_namelen);
646 READ_BUF(link->li_namelen);
647 SAVEMEM(link->li_name, link->li_namelen);
648 if ((status = check_filename(link->li_name, link->li_namelen)))
649 return status;
651 DECODE_TAIL;
654 static __be32
655 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
657 DECODE_HEAD;
660 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
662 READ_BUF(28);
663 READ32(lock->lk_type);
664 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
665 goto xdr_error;
666 READ32(lock->lk_reclaim);
667 READ64(lock->lk_offset);
668 READ64(lock->lk_length);
669 READ32(lock->lk_is_new);
671 if (lock->lk_is_new) {
672 READ_BUF(4);
673 READ32(lock->lk_new_open_seqid);
674 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
675 if (status)
676 return status;
677 READ_BUF(8 + sizeof(clientid_t));
678 READ32(lock->lk_new_lock_seqid);
679 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
680 READ32(lock->lk_new_owner.len);
681 READ_BUF(lock->lk_new_owner.len);
682 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
683 } else {
684 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
685 if (status)
686 return status;
687 READ_BUF(4);
688 READ32(lock->lk_old_lock_seqid);
691 DECODE_TAIL;
694 static __be32
695 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
697 DECODE_HEAD;
699 READ_BUF(32);
700 READ32(lockt->lt_type);
701 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
702 goto xdr_error;
703 READ64(lockt->lt_offset);
704 READ64(lockt->lt_length);
705 COPYMEM(&lockt->lt_clientid, 8);
706 READ32(lockt->lt_owner.len);
707 READ_BUF(lockt->lt_owner.len);
708 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
710 DECODE_TAIL;
713 static __be32
714 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
716 DECODE_HEAD;
718 READ_BUF(8);
719 READ32(locku->lu_type);
720 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
721 goto xdr_error;
722 READ32(locku->lu_seqid);
723 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
724 if (status)
725 return status;
726 READ_BUF(16);
727 READ64(locku->lu_offset);
728 READ64(locku->lu_length);
730 DECODE_TAIL;
733 static __be32
734 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
736 DECODE_HEAD;
738 READ_BUF(4);
739 READ32(lookup->lo_len);
740 READ_BUF(lookup->lo_len);
741 SAVEMEM(lookup->lo_name, lookup->lo_len);
742 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
743 return status;
745 DECODE_TAIL;
748 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
750 __be32 *p;
751 u32 w;
753 READ_BUF(4);
754 READ32(w);
755 *share_access = w & NFS4_SHARE_ACCESS_MASK;
756 *deleg_want = w & NFS4_SHARE_WANT_MASK;
757 if (deleg_when)
758 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
760 switch (w & NFS4_SHARE_ACCESS_MASK) {
761 case NFS4_SHARE_ACCESS_READ:
762 case NFS4_SHARE_ACCESS_WRITE:
763 case NFS4_SHARE_ACCESS_BOTH:
764 break;
765 default:
766 return nfserr_bad_xdr;
768 w &= ~NFS4_SHARE_ACCESS_MASK;
769 if (!w)
770 return nfs_ok;
771 if (!argp->minorversion)
772 return nfserr_bad_xdr;
773 switch (w & NFS4_SHARE_WANT_MASK) {
774 case NFS4_SHARE_WANT_NO_PREFERENCE:
775 case NFS4_SHARE_WANT_READ_DELEG:
776 case NFS4_SHARE_WANT_WRITE_DELEG:
777 case NFS4_SHARE_WANT_ANY_DELEG:
778 case NFS4_SHARE_WANT_NO_DELEG:
779 case NFS4_SHARE_WANT_CANCEL:
780 break;
781 default:
782 return nfserr_bad_xdr;
784 w &= ~NFS4_SHARE_WANT_MASK;
785 if (!w)
786 return nfs_ok;
788 if (!deleg_when) /* open_downgrade */
789 return nfserr_inval;
790 switch (w) {
791 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
792 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
793 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
794 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
795 return nfs_ok;
797 xdr_error:
798 return nfserr_bad_xdr;
801 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
803 __be32 *p;
805 READ_BUF(4);
806 READ32(*x);
807 /* Note: unlinke access bits, deny bits may be zero. */
808 if (*x & ~NFS4_SHARE_DENY_BOTH)
809 return nfserr_bad_xdr;
810 return nfs_ok;
811 xdr_error:
812 return nfserr_bad_xdr;
815 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
817 __be32 *p;
819 READ_BUF(4);
820 READ32(o->len);
822 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
823 return nfserr_bad_xdr;
825 READ_BUF(o->len);
826 SAVEMEM(o->data, o->len);
827 return nfs_ok;
828 xdr_error:
829 return nfserr_bad_xdr;
832 static __be32
833 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
835 DECODE_HEAD;
836 u32 dummy;
838 memset(open->op_bmval, 0, sizeof(open->op_bmval));
839 open->op_iattr.ia_valid = 0;
840 open->op_openowner = NULL;
842 open->op_xdr_error = 0;
843 /* seqid, share_access, share_deny, clientid, ownerlen */
844 READ_BUF(4);
845 READ32(open->op_seqid);
846 /* decode, yet ignore deleg_when until supported */
847 status = nfsd4_decode_share_access(argp, &open->op_share_access,
848 &open->op_deleg_want, &dummy);
849 if (status)
850 goto xdr_error;
851 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
852 if (status)
853 goto xdr_error;
854 READ_BUF(sizeof(clientid_t));
855 COPYMEM(&open->op_clientid, sizeof(clientid_t));
856 status = nfsd4_decode_opaque(argp, &open->op_owner);
857 if (status)
858 goto xdr_error;
859 READ_BUF(4);
860 READ32(open->op_create);
861 switch (open->op_create) {
862 case NFS4_OPEN_NOCREATE:
863 break;
864 case NFS4_OPEN_CREATE:
865 READ_BUF(4);
866 READ32(open->op_createmode);
867 switch (open->op_createmode) {
868 case NFS4_CREATE_UNCHECKED:
869 case NFS4_CREATE_GUARDED:
870 status = nfsd4_decode_fattr(argp, open->op_bmval,
871 &open->op_iattr, &open->op_acl, &open->op_label);
872 if (status)
873 goto out;
874 break;
875 case NFS4_CREATE_EXCLUSIVE:
876 READ_BUF(NFS4_VERIFIER_SIZE);
877 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
878 break;
879 case NFS4_CREATE_EXCLUSIVE4_1:
880 if (argp->minorversion < 1)
881 goto xdr_error;
882 READ_BUF(NFS4_VERIFIER_SIZE);
883 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
884 status = nfsd4_decode_fattr(argp, open->op_bmval,
885 &open->op_iattr, &open->op_acl, &open->op_label);
886 if (status)
887 goto out;
888 break;
889 default:
890 goto xdr_error;
892 break;
893 default:
894 goto xdr_error;
897 /* open_claim */
898 READ_BUF(4);
899 READ32(open->op_claim_type);
900 switch (open->op_claim_type) {
901 case NFS4_OPEN_CLAIM_NULL:
902 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
903 READ_BUF(4);
904 READ32(open->op_fname.len);
905 READ_BUF(open->op_fname.len);
906 SAVEMEM(open->op_fname.data, open->op_fname.len);
907 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
908 return status;
909 break;
910 case NFS4_OPEN_CLAIM_PREVIOUS:
911 READ_BUF(4);
912 READ32(open->op_delegate_type);
913 break;
914 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
915 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
916 if (status)
917 return status;
918 READ_BUF(4);
919 READ32(open->op_fname.len);
920 READ_BUF(open->op_fname.len);
921 SAVEMEM(open->op_fname.data, open->op_fname.len);
922 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
923 return status;
924 break;
925 case NFS4_OPEN_CLAIM_FH:
926 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
927 if (argp->minorversion < 1)
928 goto xdr_error;
929 /* void */
930 break;
931 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
932 if (argp->minorversion < 1)
933 goto xdr_error;
934 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
935 if (status)
936 return status;
937 break;
938 default:
939 goto xdr_error;
942 DECODE_TAIL;
945 static __be32
946 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
948 DECODE_HEAD;
950 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
951 if (status)
952 return status;
953 READ_BUF(4);
954 READ32(open_conf->oc_seqid);
956 DECODE_TAIL;
959 static __be32
960 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
962 DECODE_HEAD;
964 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
965 if (status)
966 return status;
967 READ_BUF(4);
968 READ32(open_down->od_seqid);
969 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
970 &open_down->od_deleg_want, NULL);
971 if (status)
972 return status;
973 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
974 if (status)
975 return status;
976 DECODE_TAIL;
979 static __be32
980 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
982 DECODE_HEAD;
984 READ_BUF(4);
985 READ32(putfh->pf_fhlen);
986 if (putfh->pf_fhlen > NFS4_FHSIZE)
987 goto xdr_error;
988 READ_BUF(putfh->pf_fhlen);
989 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
991 DECODE_TAIL;
994 static __be32
995 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
997 DECODE_HEAD;
999 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1000 if (status)
1001 return status;
1002 READ_BUF(12);
1003 READ64(read->rd_offset);
1004 READ32(read->rd_length);
1006 DECODE_TAIL;
1009 static __be32
1010 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1012 DECODE_HEAD;
1014 READ_BUF(24);
1015 READ64(readdir->rd_cookie);
1016 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1017 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1018 READ32(readdir->rd_maxcount);
1019 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1020 goto out;
1022 DECODE_TAIL;
1025 static __be32
1026 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1028 DECODE_HEAD;
1030 READ_BUF(4);
1031 READ32(remove->rm_namelen);
1032 READ_BUF(remove->rm_namelen);
1033 SAVEMEM(remove->rm_name, remove->rm_namelen);
1034 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1035 return status;
1037 DECODE_TAIL;
1040 static __be32
1041 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1043 DECODE_HEAD;
1045 READ_BUF(4);
1046 READ32(rename->rn_snamelen);
1047 READ_BUF(rename->rn_snamelen + 4);
1048 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1049 READ32(rename->rn_tnamelen);
1050 READ_BUF(rename->rn_tnamelen);
1051 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1052 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1053 return status;
1054 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1055 return status;
1057 DECODE_TAIL;
1060 static __be32
1061 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1063 DECODE_HEAD;
1065 READ_BUF(sizeof(clientid_t));
1066 COPYMEM(clientid, sizeof(clientid_t));
1068 DECODE_TAIL;
1071 static __be32
1072 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1073 struct nfsd4_secinfo *secinfo)
1075 DECODE_HEAD;
1077 READ_BUF(4);
1078 READ32(secinfo->si_namelen);
1079 READ_BUF(secinfo->si_namelen);
1080 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1081 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1082 if (status)
1083 return status;
1084 DECODE_TAIL;
1087 static __be32
1088 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1089 struct nfsd4_secinfo_no_name *sin)
1091 DECODE_HEAD;
1093 READ_BUF(4);
1094 READ32(sin->sin_style);
1095 DECODE_TAIL;
1098 static __be32
1099 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1101 __be32 status;
1103 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1104 if (status)
1105 return status;
1106 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1107 &setattr->sa_acl, &setattr->sa_label);
1110 static __be32
1111 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1113 DECODE_HEAD;
1115 READ_BUF(NFS4_VERIFIER_SIZE);
1116 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1118 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1119 if (status)
1120 return nfserr_bad_xdr;
1121 READ_BUF(8);
1122 READ32(setclientid->se_callback_prog);
1123 READ32(setclientid->se_callback_netid_len);
1125 READ_BUF(setclientid->se_callback_netid_len + 4);
1126 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1127 READ32(setclientid->se_callback_addr_len);
1129 READ_BUF(setclientid->se_callback_addr_len + 4);
1130 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1131 READ32(setclientid->se_callback_ident);
1133 DECODE_TAIL;
1136 static __be32
1137 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1139 DECODE_HEAD;
1141 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1142 COPYMEM(&scd_c->sc_clientid, 8);
1143 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1145 DECODE_TAIL;
1148 /* Also used for NVERIFY */
1149 static __be32
1150 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1152 DECODE_HEAD;
1154 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1155 goto out;
1157 /* For convenience's sake, we compare raw xdr'd attributes in
1158 * nfsd4_proc_verify */
1160 READ_BUF(4);
1161 READ32(verify->ve_attrlen);
1162 READ_BUF(verify->ve_attrlen);
1163 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1165 DECODE_TAIL;
1168 static __be32
1169 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1171 int avail;
1172 int len;
1173 DECODE_HEAD;
1175 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1176 if (status)
1177 return status;
1178 READ_BUF(16);
1179 READ64(write->wr_offset);
1180 READ32(write->wr_stable_how);
1181 if (write->wr_stable_how > 2)
1182 goto xdr_error;
1183 READ32(write->wr_buflen);
1185 /* Sorry .. no magic macros for this.. *
1186 * READ_BUF(write->wr_buflen);
1187 * SAVEMEM(write->wr_buf, write->wr_buflen);
1189 avail = (char*)argp->end - (char*)argp->p;
1190 if (avail + argp->pagelen < write->wr_buflen) {
1191 dprintk("NFSD: xdr error (%s:%d)\n",
1192 __FILE__, __LINE__);
1193 goto xdr_error;
1195 write->wr_head.iov_base = p;
1196 write->wr_head.iov_len = avail;
1197 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
1198 write->wr_pagelist = argp->pagelist;
1200 len = XDR_QUADLEN(write->wr_buflen) << 2;
1201 if (len >= avail) {
1202 int pages;
1204 len -= avail;
1206 pages = len >> PAGE_SHIFT;
1207 argp->pagelist += pages;
1208 argp->pagelen -= pages * PAGE_SIZE;
1209 len -= pages * PAGE_SIZE;
1211 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1212 argp->pagelist++;
1213 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
1215 argp->p += XDR_QUADLEN(len);
1217 DECODE_TAIL;
1220 static __be32
1221 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1223 DECODE_HEAD;
1225 READ_BUF(12);
1226 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1227 READ32(rlockowner->rl_owner.len);
1228 READ_BUF(rlockowner->rl_owner.len);
1229 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1231 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1232 return nfserr_inval;
1233 DECODE_TAIL;
1236 static __be32
1237 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1238 struct nfsd4_exchange_id *exid)
1240 int dummy, tmp;
1241 DECODE_HEAD;
1243 READ_BUF(NFS4_VERIFIER_SIZE);
1244 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1246 status = nfsd4_decode_opaque(argp, &exid->clname);
1247 if (status)
1248 return nfserr_bad_xdr;
1250 READ_BUF(4);
1251 READ32(exid->flags);
1253 /* Ignore state_protect4_a */
1254 READ_BUF(4);
1255 READ32(exid->spa_how);
1256 switch (exid->spa_how) {
1257 case SP4_NONE:
1258 break;
1259 case SP4_MACH_CRED:
1260 /* spo_must_enforce */
1261 READ_BUF(4);
1262 READ32(dummy);
1263 READ_BUF(dummy * 4);
1264 p += dummy;
1266 /* spo_must_allow */
1267 READ_BUF(4);
1268 READ32(dummy);
1269 READ_BUF(dummy * 4);
1270 p += dummy;
1271 break;
1272 case SP4_SSV:
1273 /* ssp_ops */
1274 READ_BUF(4);
1275 READ32(dummy);
1276 READ_BUF(dummy * 4);
1277 p += dummy;
1279 READ_BUF(4);
1280 READ32(dummy);
1281 READ_BUF(dummy * 4);
1282 p += dummy;
1284 /* ssp_hash_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);
1294 /* ssp_encr_algs<> */
1295 READ_BUF(4);
1296 READ32(tmp);
1297 while (tmp--) {
1298 READ_BUF(4);
1299 READ32(dummy);
1300 READ_BUF(dummy);
1301 p += XDR_QUADLEN(dummy);
1304 /* ssp_window and ssp_num_gss_handles */
1305 READ_BUF(8);
1306 READ32(dummy);
1307 READ32(dummy);
1308 break;
1309 default:
1310 goto xdr_error;
1313 /* Ignore Implementation ID */
1314 READ_BUF(4); /* nfs_impl_id4 array length */
1315 READ32(dummy);
1317 if (dummy > 1)
1318 goto xdr_error;
1320 if (dummy == 1) {
1321 /* nii_domain */
1322 READ_BUF(4);
1323 READ32(dummy);
1324 READ_BUF(dummy);
1325 p += XDR_QUADLEN(dummy);
1327 /* nii_name */
1328 READ_BUF(4);
1329 READ32(dummy);
1330 READ_BUF(dummy);
1331 p += XDR_QUADLEN(dummy);
1333 /* nii_date */
1334 READ_BUF(12);
1335 p += 3;
1337 DECODE_TAIL;
1340 static __be32
1341 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1342 struct nfsd4_create_session *sess)
1344 DECODE_HEAD;
1345 u32 dummy;
1347 READ_BUF(16);
1348 COPYMEM(&sess->clientid, 8);
1349 READ32(sess->seqid);
1350 READ32(sess->flags);
1352 /* Fore channel attrs */
1353 READ_BUF(28);
1354 READ32(dummy); /* headerpadsz is always 0 */
1355 READ32(sess->fore_channel.maxreq_sz);
1356 READ32(sess->fore_channel.maxresp_sz);
1357 READ32(sess->fore_channel.maxresp_cached);
1358 READ32(sess->fore_channel.maxops);
1359 READ32(sess->fore_channel.maxreqs);
1360 READ32(sess->fore_channel.nr_rdma_attrs);
1361 if (sess->fore_channel.nr_rdma_attrs == 1) {
1362 READ_BUF(4);
1363 READ32(sess->fore_channel.rdma_attrs);
1364 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1365 dprintk("Too many fore channel attr bitmaps!\n");
1366 goto xdr_error;
1369 /* Back channel attrs */
1370 READ_BUF(28);
1371 READ32(dummy); /* headerpadsz is always 0 */
1372 READ32(sess->back_channel.maxreq_sz);
1373 READ32(sess->back_channel.maxresp_sz);
1374 READ32(sess->back_channel.maxresp_cached);
1375 READ32(sess->back_channel.maxops);
1376 READ32(sess->back_channel.maxreqs);
1377 READ32(sess->back_channel.nr_rdma_attrs);
1378 if (sess->back_channel.nr_rdma_attrs == 1) {
1379 READ_BUF(4);
1380 READ32(sess->back_channel.rdma_attrs);
1381 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1382 dprintk("Too many back channel attr bitmaps!\n");
1383 goto xdr_error;
1386 READ_BUF(4);
1387 READ32(sess->callback_prog);
1388 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1389 DECODE_TAIL;
1392 static __be32
1393 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1394 struct nfsd4_destroy_session *destroy_session)
1396 DECODE_HEAD;
1397 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1398 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1400 DECODE_TAIL;
1403 static __be32
1404 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1405 struct nfsd4_free_stateid *free_stateid)
1407 DECODE_HEAD;
1409 READ_BUF(sizeof(stateid_t));
1410 READ32(free_stateid->fr_stateid.si_generation);
1411 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1413 DECODE_TAIL;
1416 static __be32
1417 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1418 struct nfsd4_sequence *seq)
1420 DECODE_HEAD;
1422 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1423 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1424 READ32(seq->seqid);
1425 READ32(seq->slotid);
1426 READ32(seq->maxslots);
1427 READ32(seq->cachethis);
1429 DECODE_TAIL;
1432 static __be32
1433 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1435 int i;
1436 __be32 *p, status;
1437 struct nfsd4_test_stateid_id *stateid;
1439 READ_BUF(4);
1440 test_stateid->ts_num_ids = ntohl(*p++);
1442 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1444 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1445 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1446 if (!stateid) {
1447 status = nfserrno(-ENOMEM);
1448 goto out;
1451 defer_free(argp, kfree, stateid);
1452 INIT_LIST_HEAD(&stateid->ts_id_list);
1453 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1455 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1456 if (status)
1457 goto out;
1460 status = 0;
1461 out:
1462 return status;
1463 xdr_error:
1464 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1465 status = nfserr_bad_xdr;
1466 goto out;
1469 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1471 DECODE_HEAD;
1473 READ_BUF(8);
1474 COPYMEM(&dc->clientid, 8);
1476 DECODE_TAIL;
1479 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1481 DECODE_HEAD;
1483 READ_BUF(4);
1484 READ32(rc->rca_one_fs);
1486 DECODE_TAIL;
1489 static __be32
1490 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1492 return nfs_ok;
1495 static __be32
1496 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1498 return nfserr_notsupp;
1501 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1503 static nfsd4_dec nfsd4_dec_ops[] = {
1504 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1505 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1506 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1507 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1508 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1509 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1510 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1511 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1512 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1513 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1514 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1515 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1516 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1517 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1518 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1519 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1520 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1521 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1522 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1523 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1524 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
1525 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1526 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1527 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1528 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1529 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1530 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1531 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1532 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1533 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1534 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1535 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1536 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1537 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1538 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1539 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1540 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1543 static nfsd4_dec nfsd41_dec_ops[] = {
1544 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1545 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1546 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1547 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1548 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1549 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1550 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1551 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1552 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1553 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1554 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1555 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1556 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1557 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1558 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1559 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1560 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1561 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1562 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1563 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1564 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1565 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1566 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1567 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1568 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1569 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1570 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1571 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1572 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1573 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1574 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1575 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1576 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1577 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1578 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1579 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1580 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
1582 /* new operations for NFSv4.1 */
1583 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1584 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1585 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1586 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1587 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1588 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1589 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1590 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1591 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1592 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1593 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1594 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1595 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1596 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1597 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1598 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1599 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1600 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1601 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1604 struct nfsd4_minorversion_ops {
1605 nfsd4_dec *decoders;
1606 int nops;
1609 static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
1610 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
1611 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1612 [2] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1615 static __be32
1616 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1618 DECODE_HEAD;
1619 struct nfsd4_op *op;
1620 struct nfsd4_minorversion_ops *ops;
1621 bool cachethis = false;
1622 int i;
1624 READ_BUF(4);
1625 READ32(argp->taglen);
1626 READ_BUF(argp->taglen + 8);
1627 SAVEMEM(argp->tag, argp->taglen);
1628 READ32(argp->minorversion);
1629 READ32(argp->opcnt);
1631 if (argp->taglen > NFSD4_MAX_TAGLEN)
1632 goto xdr_error;
1633 if (argp->opcnt > 100)
1634 goto xdr_error;
1636 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1637 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1638 if (!argp->ops) {
1639 argp->ops = argp->iops;
1640 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1641 goto xdr_error;
1645 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
1646 argp->opcnt = 0;
1648 ops = &nfsd4_minorversion[argp->minorversion];
1649 for (i = 0; i < argp->opcnt; i++) {
1650 op = &argp->ops[i];
1651 op->replay = NULL;
1653 READ_BUF(4);
1654 READ32(op->opnum);
1656 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
1657 op->status = ops->decoders[op->opnum](argp, &op->u);
1658 else {
1659 op->opnum = OP_ILLEGAL;
1660 op->status = nfserr_op_illegal;
1663 if (op->status) {
1664 argp->opcnt = i+1;
1665 break;
1668 * We'll try to cache the result in the DRC if any one
1669 * op in the compound wants to be cached:
1671 cachethis |= nfsd4_cache_this_op(op);
1673 /* Sessions make the DRC unnecessary: */
1674 if (argp->minorversion)
1675 cachethis = false;
1676 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1678 DECODE_TAIL;
1681 #define WRITE32(n) *p++ = htonl(n)
1682 #define WRITE64(n) do { \
1683 *p++ = htonl((u32)((n) >> 32)); \
1684 *p++ = htonl((u32)(n)); \
1685 } while (0)
1686 #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
1687 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1688 memcpy(p, ptr, nbytes); \
1689 p += XDR_QUADLEN(nbytes); \
1690 }} while (0)
1692 static void write32(__be32 **p, u32 n)
1694 *(*p)++ = htonl(n);
1697 static void write64(__be32 **p, u64 n)
1699 write32(p, (n >> 32));
1700 write32(p, (u32)n);
1703 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1705 if (IS_I_VERSION(inode)) {
1706 write64(p, inode->i_version);
1707 } else {
1708 write32(p, stat->ctime.tv_sec);
1709 write32(p, stat->ctime.tv_nsec);
1713 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1715 write32(p, c->atomic);
1716 if (c->change_supported) {
1717 write64(p, c->before_change);
1718 write64(p, c->after_change);
1719 } else {
1720 write32(p, c->before_ctime_sec);
1721 write32(p, c->before_ctime_nsec);
1722 write32(p, c->after_ctime_sec);
1723 write32(p, c->after_ctime_nsec);
1727 #define RESERVE_SPACE(nbytes) do { \
1728 p = resp->p; \
1729 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1730 } while (0)
1731 #define ADJUST_ARGS() resp->p = p
1733 /* Encode as an array of strings the string given with components
1734 * separated @sep, escaped with esc_enter and esc_exit.
1736 static __be32 nfsd4_encode_components_esc(char sep, char *components,
1737 __be32 **pp, int *buflen,
1738 char esc_enter, char esc_exit)
1740 __be32 *p = *pp;
1741 __be32 *countp = p;
1742 int strlen, count=0;
1743 char *str, *end, *next;
1745 dprintk("nfsd4_encode_components(%s)\n", components);
1746 if ((*buflen -= 4) < 0)
1747 return nfserr_resource;
1748 WRITE32(0); /* We will fill this in with @count later */
1749 end = str = components;
1750 while (*end) {
1751 bool found_esc = false;
1753 /* try to parse as esc_start, ..., esc_end, sep */
1754 if (*str == esc_enter) {
1755 for (; *end && (*end != esc_exit); end++)
1756 /* find esc_exit or end of string */;
1757 next = end + 1;
1758 if (*end && (!*next || *next == sep)) {
1759 str++;
1760 found_esc = true;
1764 if (!found_esc)
1765 for (; *end && (*end != sep); end++)
1766 /* find sep or end of string */;
1768 strlen = end - str;
1769 if (strlen) {
1770 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1771 return nfserr_resource;
1772 WRITE32(strlen);
1773 WRITEMEM(str, strlen);
1774 count++;
1776 else
1777 end++;
1778 str = end;
1780 *pp = p;
1781 p = countp;
1782 WRITE32(count);
1783 return 0;
1786 /* Encode as an array of strings the string given with components
1787 * separated @sep.
1789 static __be32 nfsd4_encode_components(char sep, char *components,
1790 __be32 **pp, int *buflen)
1792 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1796 * encode a location element of a fs_locations structure
1798 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1799 __be32 **pp, int *buflen)
1801 __be32 status;
1802 __be32 *p = *pp;
1804 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1805 '[', ']');
1806 if (status)
1807 return status;
1808 status = nfsd4_encode_components('/', location->path, &p, buflen);
1809 if (status)
1810 return status;
1811 *pp = p;
1812 return 0;
1816 * Encode a path in RFC3530 'pathname4' format
1818 static __be32 nfsd4_encode_path(const struct path *root,
1819 const struct path *path, __be32 **pp, int *buflen)
1821 struct path cur = *path;
1822 __be32 *p = *pp;
1823 struct dentry **components = NULL;
1824 unsigned int ncomponents = 0;
1825 __be32 err = nfserr_jukebox;
1827 dprintk("nfsd4_encode_components(");
1829 path_get(&cur);
1830 /* First walk the path up to the nfsd root, and store the
1831 * dentries/path components in an array.
1833 for (;;) {
1834 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1835 break;
1836 if (cur.dentry == cur.mnt->mnt_root) {
1837 if (follow_up(&cur))
1838 continue;
1839 goto out_free;
1841 if ((ncomponents & 15) == 0) {
1842 struct dentry **new;
1843 new = krealloc(components,
1844 sizeof(*new) * (ncomponents + 16),
1845 GFP_KERNEL);
1846 if (!new)
1847 goto out_free;
1848 components = new;
1850 components[ncomponents++] = cur.dentry;
1851 cur.dentry = dget_parent(cur.dentry);
1854 *buflen -= 4;
1855 if (*buflen < 0)
1856 goto out_free;
1857 WRITE32(ncomponents);
1859 while (ncomponents) {
1860 struct dentry *dentry = components[ncomponents - 1];
1861 unsigned int len;
1863 spin_lock(&dentry->d_lock);
1864 len = dentry->d_name.len;
1865 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1866 if (*buflen < 0) {
1867 spin_unlock(&dentry->d_lock);
1868 goto out_free;
1870 WRITE32(len);
1871 WRITEMEM(dentry->d_name.name, len);
1872 dprintk("/%s", dentry->d_name.name);
1873 spin_unlock(&dentry->d_lock);
1874 dput(dentry);
1875 ncomponents--;
1878 *pp = p;
1879 err = 0;
1880 out_free:
1881 dprintk(")\n");
1882 while (ncomponents)
1883 dput(components[--ncomponents]);
1884 kfree(components);
1885 path_put(&cur);
1886 return err;
1889 static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1890 const struct path *path, __be32 **pp, int *buflen)
1892 struct svc_export *exp_ps;
1893 __be32 res;
1895 exp_ps = rqst_find_fsidzero_export(rqstp);
1896 if (IS_ERR(exp_ps))
1897 return nfserrno(PTR_ERR(exp_ps));
1898 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1899 exp_put(exp_ps);
1900 return res;
1904 * encode a fs_locations structure
1906 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1907 struct svc_export *exp,
1908 __be32 **pp, int *buflen)
1910 __be32 status;
1911 int i;
1912 __be32 *p = *pp;
1913 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1915 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
1916 if (status)
1917 return status;
1918 if ((*buflen -= 4) < 0)
1919 return nfserr_resource;
1920 WRITE32(fslocs->locations_count);
1921 for (i=0; i<fslocs->locations_count; i++) {
1922 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1923 &p, buflen);
1924 if (status)
1925 return status;
1927 *pp = p;
1928 return 0;
1931 static u32 nfs4_file_type(umode_t mode)
1933 switch (mode & S_IFMT) {
1934 case S_IFIFO: return NF4FIFO;
1935 case S_IFCHR: return NF4CHR;
1936 case S_IFDIR: return NF4DIR;
1937 case S_IFBLK: return NF4BLK;
1938 case S_IFLNK: return NF4LNK;
1939 case S_IFREG: return NF4REG;
1940 case S_IFSOCK: return NF4SOCK;
1941 default: return NF4BAD;
1945 static __be32
1946 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
1947 __be32 **p, int *buflen)
1949 int status;
1951 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1952 return nfserr_resource;
1953 if (whotype != NFS4_ACL_WHO_NAMED)
1954 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1955 else if (gid_valid(gid))
1956 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
1957 else
1958 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
1959 if (status < 0)
1960 return nfserrno(status);
1961 *p = xdr_encode_opaque(*p, NULL, status);
1962 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1963 BUG_ON(*buflen < 0);
1964 return 0;
1967 static inline __be32
1968 nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
1970 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1971 p, buflen);
1974 static inline __be32
1975 nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
1977 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1978 p, buflen);
1981 static inline __be32
1982 nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
1983 __be32 **p, int *buflen)
1985 kuid_t uid = INVALID_UID;
1986 kgid_t gid = INVALID_GID;
1988 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1989 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1990 gid = ace->who_gid;
1991 else
1992 uid = ace->who_uid;
1994 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
1997 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1998 FATTR4_WORD0_RDATTR_ERROR)
1999 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2001 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2002 static inline __be32
2003 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2005 __be32 *p = *pp;
2007 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
2008 return nfserr_resource;
2011 * For now we use a 0 here to indicate the null translation; in
2012 * the future we may place a call to translation code here.
2014 if ((*buflen -= 8) < 0)
2015 return nfserr_resource;
2017 WRITE32(0); /* lfs */
2018 WRITE32(0); /* pi */
2019 p = xdr_encode_opaque(p, context, len);
2020 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2022 *pp = p;
2023 return 0;
2025 #else
2026 static inline __be32
2027 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2028 { return 0; }
2029 #endif
2031 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
2033 /* As per referral draft: */
2034 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2035 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2036 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2037 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2038 *rdattr_err = NFSERR_MOVED;
2039 else
2040 return nfserr_moved;
2042 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2043 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2044 return 0;
2048 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2050 struct path path = exp->ex_path;
2051 int err;
2053 path_get(&path);
2054 while (follow_up(&path)) {
2055 if (path.dentry != path.mnt->mnt_root)
2056 break;
2058 err = vfs_getattr(&path, stat);
2059 path_put(&path);
2060 return err;
2064 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2065 * ourselves.
2067 * countp is the buffer size in _words_
2069 __be32
2070 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
2071 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
2072 struct svc_rqst *rqstp, int ignore_crossmnt)
2074 u32 bmval0 = bmval[0];
2075 u32 bmval1 = bmval[1];
2076 u32 bmval2 = bmval[2];
2077 struct kstat stat;
2078 struct svc_fh tempfh;
2079 struct kstatfs statfs;
2080 int buflen = count << 2;
2081 __be32 *attrlenp;
2082 u32 dummy;
2083 u64 dummy64;
2084 u32 rdattr_err = 0;
2085 __be32 *p = *buffer;
2086 __be32 status;
2087 int err;
2088 int aclsupport = 0;
2089 struct nfs4_acl *acl = NULL;
2090 void *context = NULL;
2091 int contextlen;
2092 bool contextsupport = false;
2093 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2094 u32 minorversion = resp->cstate.minorversion;
2095 struct path path = {
2096 .mnt = exp->ex_path.mnt,
2097 .dentry = dentry,
2099 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2101 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2102 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2103 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2104 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
2106 if (exp->ex_fslocs.migrated) {
2107 BUG_ON(bmval[2]);
2108 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2109 if (status)
2110 goto out;
2113 err = vfs_getattr(&path, &stat);
2114 if (err)
2115 goto out_nfserr;
2116 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2117 FATTR4_WORD0_MAXNAME)) ||
2118 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2119 FATTR4_WORD1_SPACE_TOTAL))) {
2120 err = vfs_statfs(&path, &statfs);
2121 if (err)
2122 goto out_nfserr;
2124 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2125 fh_init(&tempfh, NFS4_FHSIZE);
2126 status = fh_compose(&tempfh, exp, dentry, NULL);
2127 if (status)
2128 goto out;
2129 fhp = &tempfh;
2131 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2132 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
2133 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2134 aclsupport = (err == 0);
2135 if (bmval0 & FATTR4_WORD0_ACL) {
2136 if (err == -EOPNOTSUPP)
2137 bmval0 &= ~FATTR4_WORD0_ACL;
2138 else if (err == -EINVAL) {
2139 status = nfserr_attrnotsupp;
2140 goto out;
2141 } else if (err != 0)
2142 goto out_nfserr;
2146 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2147 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2148 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2149 err = security_inode_getsecctx(dentry->d_inode,
2150 &context, &contextlen);
2151 contextsupport = (err == 0);
2152 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2153 if (err == -EOPNOTSUPP)
2154 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2155 else if (err)
2156 goto out_nfserr;
2159 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2161 if (bmval2) {
2162 if ((buflen -= 16) < 0)
2163 goto out_resource;
2164 WRITE32(3);
2165 WRITE32(bmval0);
2166 WRITE32(bmval1);
2167 WRITE32(bmval2);
2168 } else if (bmval1) {
2169 if ((buflen -= 12) < 0)
2170 goto out_resource;
2171 WRITE32(2);
2172 WRITE32(bmval0);
2173 WRITE32(bmval1);
2174 } else {
2175 if ((buflen -= 8) < 0)
2176 goto out_resource;
2177 WRITE32(1);
2178 WRITE32(bmval0);
2180 attrlenp = p++; /* to be backfilled later */
2182 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2183 u32 word0 = nfsd_suppattrs0(minorversion);
2184 u32 word1 = nfsd_suppattrs1(minorversion);
2185 u32 word2 = nfsd_suppattrs2(minorversion);
2187 if (!aclsupport)
2188 word0 &= ~FATTR4_WORD0_ACL;
2189 if (!contextsupport)
2190 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2191 if (!word2) {
2192 if ((buflen -= 12) < 0)
2193 goto out_resource;
2194 WRITE32(2);
2195 WRITE32(word0);
2196 WRITE32(word1);
2197 } else {
2198 if ((buflen -= 16) < 0)
2199 goto out_resource;
2200 WRITE32(3);
2201 WRITE32(word0);
2202 WRITE32(word1);
2203 WRITE32(word2);
2206 if (bmval0 & FATTR4_WORD0_TYPE) {
2207 if ((buflen -= 4) < 0)
2208 goto out_resource;
2209 dummy = nfs4_file_type(stat.mode);
2210 if (dummy == NF4BAD)
2211 goto out_serverfault;
2212 WRITE32(dummy);
2214 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2215 if ((buflen -= 4) < 0)
2216 goto out_resource;
2217 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2218 WRITE32(NFS4_FH_PERSISTENT);
2219 else
2220 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
2222 if (bmval0 & FATTR4_WORD0_CHANGE) {
2223 if ((buflen -= 8) < 0)
2224 goto out_resource;
2225 write_change(&p, &stat, dentry->d_inode);
2227 if (bmval0 & FATTR4_WORD0_SIZE) {
2228 if ((buflen -= 8) < 0)
2229 goto out_resource;
2230 WRITE64(stat.size);
2232 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2233 if ((buflen -= 4) < 0)
2234 goto out_resource;
2235 WRITE32(1);
2237 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2238 if ((buflen -= 4) < 0)
2239 goto out_resource;
2240 WRITE32(1);
2242 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2243 if ((buflen -= 4) < 0)
2244 goto out_resource;
2245 WRITE32(0);
2247 if (bmval0 & FATTR4_WORD0_FSID) {
2248 if ((buflen -= 16) < 0)
2249 goto out_resource;
2250 if (exp->ex_fslocs.migrated) {
2251 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2252 WRITE64(NFS4_REFERRAL_FSID_MINOR);
2253 } else switch(fsid_source(fhp)) {
2254 case FSIDSOURCE_FSID:
2255 WRITE64((u64)exp->ex_fsid);
2256 WRITE64((u64)0);
2257 break;
2258 case FSIDSOURCE_DEV:
2259 WRITE32(0);
2260 WRITE32(MAJOR(stat.dev));
2261 WRITE32(0);
2262 WRITE32(MINOR(stat.dev));
2263 break;
2264 case FSIDSOURCE_UUID:
2265 WRITEMEM(exp->ex_uuid, 16);
2266 break;
2269 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2270 if ((buflen -= 4) < 0)
2271 goto out_resource;
2272 WRITE32(0);
2274 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2275 if ((buflen -= 4) < 0)
2276 goto out_resource;
2277 WRITE32(nn->nfsd4_lease);
2279 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2280 if ((buflen -= 4) < 0)
2281 goto out_resource;
2282 WRITE32(rdattr_err);
2284 if (bmval0 & FATTR4_WORD0_ACL) {
2285 struct nfs4_ace *ace;
2287 if (acl == NULL) {
2288 if ((buflen -= 4) < 0)
2289 goto out_resource;
2291 WRITE32(0);
2292 goto out_acl;
2294 if ((buflen -= 4) < 0)
2295 goto out_resource;
2296 WRITE32(acl->naces);
2298 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2299 if ((buflen -= 4*3) < 0)
2300 goto out_resource;
2301 WRITE32(ace->type);
2302 WRITE32(ace->flag);
2303 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2304 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
2305 if (status == nfserr_resource)
2306 goto out_resource;
2307 if (status)
2308 goto out;
2311 out_acl:
2312 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2313 if ((buflen -= 4) < 0)
2314 goto out_resource;
2315 WRITE32(aclsupport ?
2316 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2318 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2319 if ((buflen -= 4) < 0)
2320 goto out_resource;
2321 WRITE32(1);
2323 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2324 if ((buflen -= 4) < 0)
2325 goto out_resource;
2326 WRITE32(0);
2328 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2329 if ((buflen -= 4) < 0)
2330 goto out_resource;
2331 WRITE32(1);
2333 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2334 if ((buflen -= 4) < 0)
2335 goto out_resource;
2336 WRITE32(1);
2338 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2339 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2340 if (buflen < 0)
2341 goto out_resource;
2342 WRITE32(fhp->fh_handle.fh_size);
2343 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2345 if (bmval0 & FATTR4_WORD0_FILEID) {
2346 if ((buflen -= 8) < 0)
2347 goto out_resource;
2348 WRITE64(stat.ino);
2350 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2351 if ((buflen -= 8) < 0)
2352 goto out_resource;
2353 WRITE64((u64) statfs.f_ffree);
2355 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2356 if ((buflen -= 8) < 0)
2357 goto out_resource;
2358 WRITE64((u64) statfs.f_ffree);
2360 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2361 if ((buflen -= 8) < 0)
2362 goto out_resource;
2363 WRITE64((u64) statfs.f_files);
2365 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2366 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2367 if (status == nfserr_resource)
2368 goto out_resource;
2369 if (status)
2370 goto out;
2372 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2373 if ((buflen -= 4) < 0)
2374 goto out_resource;
2375 WRITE32(1);
2377 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2378 if ((buflen -= 8) < 0)
2379 goto out_resource;
2380 WRITE64(~(u64)0);
2382 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2383 if ((buflen -= 4) < 0)
2384 goto out_resource;
2385 WRITE32(255);
2387 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2388 if ((buflen -= 4) < 0)
2389 goto out_resource;
2390 WRITE32(statfs.f_namelen);
2392 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2393 if ((buflen -= 8) < 0)
2394 goto out_resource;
2395 WRITE64((u64) svc_max_payload(rqstp));
2397 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2398 if ((buflen -= 8) < 0)
2399 goto out_resource;
2400 WRITE64((u64) svc_max_payload(rqstp));
2402 if (bmval1 & FATTR4_WORD1_MODE) {
2403 if ((buflen -= 4) < 0)
2404 goto out_resource;
2405 WRITE32(stat.mode & S_IALLUGO);
2407 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2408 if ((buflen -= 4) < 0)
2409 goto out_resource;
2410 WRITE32(1);
2412 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2413 if ((buflen -= 4) < 0)
2414 goto out_resource;
2415 WRITE32(stat.nlink);
2417 if (bmval1 & FATTR4_WORD1_OWNER) {
2418 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2419 if (status == nfserr_resource)
2420 goto out_resource;
2421 if (status)
2422 goto out;
2424 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2425 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2426 if (status == nfserr_resource)
2427 goto out_resource;
2428 if (status)
2429 goto out;
2431 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2432 if ((buflen -= 8) < 0)
2433 goto out_resource;
2434 WRITE32((u32) MAJOR(stat.rdev));
2435 WRITE32((u32) MINOR(stat.rdev));
2437 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2438 if ((buflen -= 8) < 0)
2439 goto out_resource;
2440 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2441 WRITE64(dummy64);
2443 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2444 if ((buflen -= 8) < 0)
2445 goto out_resource;
2446 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2447 WRITE64(dummy64);
2449 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2450 if ((buflen -= 8) < 0)
2451 goto out_resource;
2452 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2453 WRITE64(dummy64);
2455 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2456 if ((buflen -= 8) < 0)
2457 goto out_resource;
2458 dummy64 = (u64)stat.blocks << 9;
2459 WRITE64(dummy64);
2461 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2462 if ((buflen -= 12) < 0)
2463 goto out_resource;
2464 WRITE64((s64)stat.atime.tv_sec);
2465 WRITE32(stat.atime.tv_nsec);
2467 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2468 if ((buflen -= 12) < 0)
2469 goto out_resource;
2470 WRITE32(0);
2471 WRITE32(1);
2472 WRITE32(0);
2474 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2475 if ((buflen -= 12) < 0)
2476 goto out_resource;
2477 WRITE64((s64)stat.ctime.tv_sec);
2478 WRITE32(stat.ctime.tv_nsec);
2480 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2481 if ((buflen -= 12) < 0)
2482 goto out_resource;
2483 WRITE64((s64)stat.mtime.tv_sec);
2484 WRITE32(stat.mtime.tv_nsec);
2486 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2487 if ((buflen -= 8) < 0)
2488 goto out_resource;
2490 * Get parent's attributes if not ignoring crossmount
2491 * and this is the root of a cross-mounted filesystem.
2493 if (ignore_crossmnt == 0 &&
2494 dentry == exp->ex_path.mnt->mnt_root)
2495 get_parent_attributes(exp, &stat);
2496 WRITE64(stat.ino);
2498 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2499 status = nfsd4_encode_security_label(rqstp, context,
2500 contextlen, &p, &buflen);
2501 if (status)
2502 goto out;
2504 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2505 WRITE32(3);
2506 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2507 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2508 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2511 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2512 *buffer = p;
2513 status = nfs_ok;
2515 out:
2516 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2517 if (context)
2518 security_release_secctx(context, contextlen);
2519 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2520 kfree(acl);
2521 if (fhp == &tempfh)
2522 fh_put(&tempfh);
2523 return status;
2524 out_nfserr:
2525 status = nfserrno(err);
2526 goto out;
2527 out_resource:
2528 status = nfserr_resource;
2529 goto out;
2530 out_serverfault:
2531 status = nfserr_serverfault;
2532 goto out;
2535 static inline int attributes_need_mount(u32 *bmval)
2537 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2538 return 1;
2539 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2540 return 1;
2541 return 0;
2544 static __be32
2545 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2546 const char *name, int namlen, __be32 **p, int buflen)
2548 struct svc_export *exp = cd->rd_fhp->fh_export;
2549 struct dentry *dentry;
2550 __be32 nfserr;
2551 int ignore_crossmnt = 0;
2553 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2554 if (IS_ERR(dentry))
2555 return nfserrno(PTR_ERR(dentry));
2556 if (!dentry->d_inode) {
2558 * nfsd_buffered_readdir drops the i_mutex between
2559 * readdir and calling this callback, leaving a window
2560 * where this directory entry could have gone away.
2562 dput(dentry);
2563 return nfserr_noent;
2566 exp_get(exp);
2568 * In the case of a mountpoint, the client may be asking for
2569 * attributes that are only properties of the underlying filesystem
2570 * as opposed to the cross-mounted file system. In such a case,
2571 * we will not follow the cross mount and will fill the attribtutes
2572 * directly from the mountpoint dentry.
2574 if (nfsd_mountpoint(dentry, exp)) {
2575 int err;
2577 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2578 && !attributes_need_mount(cd->rd_bmval)) {
2579 ignore_crossmnt = 1;
2580 goto out_encode;
2583 * Why the heck aren't we just using nfsd_lookup??
2584 * Different "."/".." handling? Something else?
2585 * At least, add a comment here to explain....
2587 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2588 if (err) {
2589 nfserr = nfserrno(err);
2590 goto out_put;
2592 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2593 if (nfserr)
2594 goto out_put;
2597 out_encode:
2598 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2599 cd->rd_rqstp, ignore_crossmnt);
2600 out_put:
2601 dput(dentry);
2602 exp_put(exp);
2603 return nfserr;
2606 static __be32 *
2607 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2609 __be32 *attrlenp;
2611 if (buflen < 6)
2612 return NULL;
2613 *p++ = htonl(2);
2614 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2615 *p++ = htonl(0); /* bmval1 */
2617 attrlenp = p++;
2618 *p++ = nfserr; /* no htonl */
2619 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2620 return p;
2623 static int
2624 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2625 loff_t offset, u64 ino, unsigned int d_type)
2627 struct readdir_cd *ccd = ccdv;
2628 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2629 int buflen;
2630 __be32 *p = cd->buffer;
2631 __be32 *cookiep;
2632 __be32 nfserr = nfserr_toosmall;
2634 /* In nfsv4, "." and ".." never make it onto the wire.. */
2635 if (name && isdotent(name, namlen)) {
2636 cd->common.err = nfs_ok;
2637 return 0;
2640 if (cd->offset)
2641 xdr_encode_hyper(cd->offset, (u64) offset);
2643 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2644 if (buflen < 0)
2645 goto fail;
2647 *p++ = xdr_one; /* mark entry present */
2648 cookiep = p;
2649 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2650 p = xdr_encode_array(p, name, namlen); /* name length & name */
2652 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
2653 switch (nfserr) {
2654 case nfs_ok:
2655 break;
2656 case nfserr_resource:
2657 nfserr = nfserr_toosmall;
2658 goto fail;
2659 case nfserr_noent:
2660 goto skip_entry;
2661 default:
2663 * If the client requested the RDATTR_ERROR attribute,
2664 * we stuff the error code into this attribute
2665 * and continue. If this attribute was not requested,
2666 * then in accordance with the spec, we fail the
2667 * entire READDIR operation(!)
2669 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2670 goto fail;
2671 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2672 if (p == NULL) {
2673 nfserr = nfserr_toosmall;
2674 goto fail;
2677 cd->buflen -= (p - cd->buffer);
2678 cd->buffer = p;
2679 cd->offset = cookiep;
2680 skip_entry:
2681 cd->common.err = nfs_ok;
2682 return 0;
2683 fail:
2684 cd->common.err = nfserr;
2685 return -EINVAL;
2688 static void
2689 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2691 __be32 *p;
2693 RESERVE_SPACE(sizeof(stateid_t));
2694 WRITE32(sid->si_generation);
2695 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2696 ADJUST_ARGS();
2699 static __be32
2700 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2702 __be32 *p;
2704 if (!nfserr) {
2705 RESERVE_SPACE(8);
2706 WRITE32(access->ac_supported);
2707 WRITE32(access->ac_resp_access);
2708 ADJUST_ARGS();
2710 return nfserr;
2713 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2715 __be32 *p;
2717 if (!nfserr) {
2718 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2719 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2720 WRITE32(bcts->dir);
2721 /* Sorry, we do not yet support RDMA over 4.1: */
2722 WRITE32(0);
2723 ADJUST_ARGS();
2725 return nfserr;
2728 static __be32
2729 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2731 if (!nfserr)
2732 nfsd4_encode_stateid(resp, &close->cl_stateid);
2734 return nfserr;
2738 static __be32
2739 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2741 __be32 *p;
2743 if (!nfserr) {
2744 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2745 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
2746 ADJUST_ARGS();
2748 return nfserr;
2751 static __be32
2752 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2754 __be32 *p;
2756 if (!nfserr) {
2757 RESERVE_SPACE(32);
2758 write_cinfo(&p, &create->cr_cinfo);
2759 WRITE32(2);
2760 WRITE32(create->cr_bmval[0]);
2761 WRITE32(create->cr_bmval[1]);
2762 ADJUST_ARGS();
2764 return nfserr;
2767 static __be32
2768 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2770 struct svc_fh *fhp = getattr->ga_fhp;
2771 int buflen;
2773 if (nfserr)
2774 return nfserr;
2776 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2777 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2778 &resp->p, buflen, getattr->ga_bmval,
2779 resp->rqstp, 0);
2780 return nfserr;
2783 static __be32
2784 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2786 struct svc_fh *fhp = *fhpp;
2787 unsigned int len;
2788 __be32 *p;
2790 if (!nfserr) {
2791 len = fhp->fh_handle.fh_size;
2792 RESERVE_SPACE(len + 4);
2793 WRITE32(len);
2794 WRITEMEM(&fhp->fh_handle.fh_base, len);
2795 ADJUST_ARGS();
2797 return nfserr;
2801 * Including all fields other than the name, a LOCK4denied structure requires
2802 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2804 static void
2805 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2807 struct xdr_netobj *conf = &ld->ld_owner;
2808 __be32 *p;
2810 RESERVE_SPACE(32 + XDR_LEN(conf->len));
2811 WRITE64(ld->ld_start);
2812 WRITE64(ld->ld_length);
2813 WRITE32(ld->ld_type);
2814 if (conf->len) {
2815 WRITEMEM(&ld->ld_clientid, 8);
2816 WRITE32(conf->len);
2817 WRITEMEM(conf->data, conf->len);
2818 kfree(conf->data);
2819 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2820 WRITE64((u64)0); /* clientid */
2821 WRITE32(0); /* length of owner name */
2823 ADJUST_ARGS();
2826 static __be32
2827 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2829 if (!nfserr)
2830 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2831 else if (nfserr == nfserr_denied)
2832 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2834 return nfserr;
2837 static __be32
2838 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2840 if (nfserr == nfserr_denied)
2841 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2842 return nfserr;
2845 static __be32
2846 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2848 if (!nfserr)
2849 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2851 return nfserr;
2855 static __be32
2856 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2858 __be32 *p;
2860 if (!nfserr) {
2861 RESERVE_SPACE(20);
2862 write_cinfo(&p, &link->li_cinfo);
2863 ADJUST_ARGS();
2865 return nfserr;
2869 static __be32
2870 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2872 __be32 *p;
2874 if (nfserr)
2875 goto out;
2877 nfsd4_encode_stateid(resp, &open->op_stateid);
2878 RESERVE_SPACE(40);
2879 write_cinfo(&p, &open->op_cinfo);
2880 WRITE32(open->op_rflags);
2881 WRITE32(2);
2882 WRITE32(open->op_bmval[0]);
2883 WRITE32(open->op_bmval[1]);
2884 WRITE32(open->op_delegate_type);
2885 ADJUST_ARGS();
2887 switch (open->op_delegate_type) {
2888 case NFS4_OPEN_DELEGATE_NONE:
2889 break;
2890 case NFS4_OPEN_DELEGATE_READ:
2891 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2892 RESERVE_SPACE(20);
2893 WRITE32(open->op_recall);
2896 * TODO: ACE's in delegations
2898 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2899 WRITE32(0);
2900 WRITE32(0);
2901 WRITE32(0); /* XXX: is NULL principal ok? */
2902 ADJUST_ARGS();
2903 break;
2904 case NFS4_OPEN_DELEGATE_WRITE:
2905 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2906 RESERVE_SPACE(32);
2907 WRITE32(0);
2910 * TODO: space_limit's in delegations
2912 WRITE32(NFS4_LIMIT_SIZE);
2913 WRITE32(~(u32)0);
2914 WRITE32(~(u32)0);
2917 * TODO: ACE's in delegations
2919 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2920 WRITE32(0);
2921 WRITE32(0);
2922 WRITE32(0); /* XXX: is NULL principal ok? */
2923 ADJUST_ARGS();
2924 break;
2925 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2926 switch (open->op_why_no_deleg) {
2927 case WND4_CONTENTION:
2928 case WND4_RESOURCE:
2929 RESERVE_SPACE(8);
2930 WRITE32(open->op_why_no_deleg);
2931 WRITE32(0); /* deleg signaling not supported yet */
2932 break;
2933 default:
2934 RESERVE_SPACE(4);
2935 WRITE32(open->op_why_no_deleg);
2937 ADJUST_ARGS();
2938 break;
2939 default:
2940 BUG();
2942 /* XXX save filehandle here */
2943 out:
2944 return nfserr;
2947 static __be32
2948 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2950 if (!nfserr)
2951 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2953 return nfserr;
2956 static __be32
2957 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2959 if (!nfserr)
2960 nfsd4_encode_stateid(resp, &od->od_stateid);
2962 return nfserr;
2965 static __be32
2966 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2967 struct nfsd4_read *read)
2969 u32 eof;
2970 int v;
2971 struct page *page;
2972 unsigned long maxcount;
2973 long len;
2974 __be32 *p;
2976 if (nfserr)
2977 return nfserr;
2978 if (resp->xbuf->page_len)
2979 return nfserr_resource;
2981 RESERVE_SPACE(8); /* eof flag and byte count */
2983 maxcount = svc_max_payload(resp->rqstp);
2984 if (maxcount > read->rd_length)
2985 maxcount = read->rd_length;
2987 len = maxcount;
2988 v = 0;
2989 while (len > 0) {
2990 page = *(resp->rqstp->rq_next_page);
2991 if (!page) { /* ran out of pages */
2992 maxcount -= len;
2993 break;
2995 resp->rqstp->rq_vec[v].iov_base = page_address(page);
2996 resp->rqstp->rq_vec[v].iov_len =
2997 len < PAGE_SIZE ? len : PAGE_SIZE;
2998 resp->rqstp->rq_next_page++;
2999 v++;
3000 len -= PAGE_SIZE;
3002 read->rd_vlen = v;
3004 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
3005 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
3006 &maxcount);
3008 if (nfserr)
3009 return nfserr;
3010 eof = (read->rd_offset + maxcount >=
3011 read->rd_fhp->fh_dentry->d_inode->i_size);
3013 WRITE32(eof);
3014 WRITE32(maxcount);
3015 ADJUST_ARGS();
3016 resp->xbuf->head[0].iov_len = (char*)p
3017 - (char*)resp->xbuf->head[0].iov_base;
3018 resp->xbuf->page_len = maxcount;
3020 /* Use rest of head for padding and remaining ops: */
3021 resp->xbuf->tail[0].iov_base = p;
3022 resp->xbuf->tail[0].iov_len = 0;
3023 if (maxcount&3) {
3024 RESERVE_SPACE(4);
3025 WRITE32(0);
3026 resp->xbuf->tail[0].iov_base += maxcount&3;
3027 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3028 ADJUST_ARGS();
3030 return 0;
3033 static __be32
3034 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3036 int maxcount;
3037 char *page;
3038 __be32 *p;
3040 if (nfserr)
3041 return nfserr;
3042 if (resp->xbuf->page_len)
3043 return nfserr_resource;
3044 if (!*resp->rqstp->rq_next_page)
3045 return nfserr_resource;
3047 page = page_address(*(resp->rqstp->rq_next_page++));
3049 maxcount = PAGE_SIZE;
3050 RESERVE_SPACE(4);
3053 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3054 * if they would overflow the buffer. Is this kosher in NFSv4? If
3055 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3056 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3058 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3059 if (nfserr == nfserr_isdir)
3060 return nfserr_inval;
3061 if (nfserr)
3062 return nfserr;
3064 WRITE32(maxcount);
3065 ADJUST_ARGS();
3066 resp->xbuf->head[0].iov_len = (char*)p
3067 - (char*)resp->xbuf->head[0].iov_base;
3068 resp->xbuf->page_len = maxcount;
3070 /* Use rest of head for padding and remaining ops: */
3071 resp->xbuf->tail[0].iov_base = p;
3072 resp->xbuf->tail[0].iov_len = 0;
3073 if (maxcount&3) {
3074 RESERVE_SPACE(4);
3075 WRITE32(0);
3076 resp->xbuf->tail[0].iov_base += maxcount&3;
3077 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3078 ADJUST_ARGS();
3080 return 0;
3083 static __be32
3084 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3086 int maxcount;
3087 loff_t offset;
3088 __be32 *page, *savep, *tailbase;
3089 __be32 *p;
3091 if (nfserr)
3092 return nfserr;
3093 if (resp->xbuf->page_len)
3094 return nfserr_resource;
3095 if (!*resp->rqstp->rq_next_page)
3096 return nfserr_resource;
3098 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
3099 savep = p;
3101 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3102 WRITE32(0);
3103 WRITE32(0);
3104 ADJUST_ARGS();
3105 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
3106 tailbase = p;
3108 maxcount = PAGE_SIZE;
3109 if (maxcount > readdir->rd_maxcount)
3110 maxcount = readdir->rd_maxcount;
3113 * Convert from bytes to words, account for the two words already
3114 * written, make sure to leave two words at the end for the next
3115 * pointer and eof field.
3117 maxcount = (maxcount >> 2) - 4;
3118 if (maxcount < 0) {
3119 nfserr = nfserr_toosmall;
3120 goto err_no_verf;
3123 page = page_address(*(resp->rqstp->rq_next_page++));
3124 readdir->common.err = 0;
3125 readdir->buflen = maxcount;
3126 readdir->buffer = page;
3127 readdir->offset = NULL;
3129 offset = readdir->rd_cookie;
3130 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3131 &offset,
3132 &readdir->common, nfsd4_encode_dirent);
3133 if (nfserr == nfs_ok &&
3134 readdir->common.err == nfserr_toosmall &&
3135 readdir->buffer == page)
3136 nfserr = nfserr_toosmall;
3137 if (nfserr)
3138 goto err_no_verf;
3140 if (readdir->offset)
3141 xdr_encode_hyper(readdir->offset, offset);
3143 p = readdir->buffer;
3144 *p++ = 0; /* no more entries */
3145 *p++ = htonl(readdir->common.err == nfserr_eof);
3146 resp->xbuf->page_len = ((char*)p) -
3147 (char*)page_address(*(resp->rqstp->rq_next_page-1));
3149 /* Use rest of head for padding and remaining ops: */
3150 resp->xbuf->tail[0].iov_base = tailbase;
3151 resp->xbuf->tail[0].iov_len = 0;
3152 resp->p = resp->xbuf->tail[0].iov_base;
3153 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
3155 return 0;
3156 err_no_verf:
3157 p = savep;
3158 ADJUST_ARGS();
3159 return nfserr;
3162 static __be32
3163 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3165 __be32 *p;
3167 if (!nfserr) {
3168 RESERVE_SPACE(20);
3169 write_cinfo(&p, &remove->rm_cinfo);
3170 ADJUST_ARGS();
3172 return nfserr;
3175 static __be32
3176 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3178 __be32 *p;
3180 if (!nfserr) {
3181 RESERVE_SPACE(40);
3182 write_cinfo(&p, &rename->rn_sinfo);
3183 write_cinfo(&p, &rename->rn_tinfo);
3184 ADJUST_ARGS();
3186 return nfserr;
3189 static __be32
3190 nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3191 __be32 nfserr, struct svc_export *exp)
3193 u32 i, nflavs, supported;
3194 struct exp_flavor_info *flavs;
3195 struct exp_flavor_info def_flavs[2];
3196 __be32 *p, *flavorsp;
3197 static bool report = true;
3199 if (nfserr)
3200 goto out;
3201 if (exp->ex_nflavors) {
3202 flavs = exp->ex_flavors;
3203 nflavs = exp->ex_nflavors;
3204 } else { /* Handling of some defaults in absence of real secinfo: */
3205 flavs = def_flavs;
3206 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3207 nflavs = 2;
3208 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3209 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3210 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3211 nflavs = 1;
3212 flavs[0].pseudoflavor
3213 = svcauth_gss_flavor(exp->ex_client);
3214 } else {
3215 nflavs = 1;
3216 flavs[0].pseudoflavor
3217 = exp->ex_client->flavour->flavour;
3221 supported = 0;
3222 RESERVE_SPACE(4);
3223 flavorsp = p++; /* to be backfilled later */
3224 ADJUST_ARGS();
3226 for (i = 0; i < nflavs; i++) {
3227 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3228 struct rpcsec_gss_info info;
3230 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3231 supported++;
3232 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
3233 WRITE32(RPC_AUTH_GSS);
3234 WRITE32(info.oid.len);
3235 WRITEMEM(info.oid.data, info.oid.len);
3236 WRITE32(info.qop);
3237 WRITE32(info.service);
3238 ADJUST_ARGS();
3239 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3240 supported++;
3241 RESERVE_SPACE(4);
3242 WRITE32(pf);
3243 ADJUST_ARGS();
3244 } else {
3245 if (report)
3246 pr_warn("NFS: SECINFO: security flavor %u "
3247 "is not supported\n", pf);
3251 if (nflavs != supported)
3252 report = false;
3253 *flavorsp = htonl(supported);
3255 out:
3256 if (exp)
3257 exp_put(exp);
3258 return nfserr;
3261 static __be32
3262 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3263 struct nfsd4_secinfo *secinfo)
3265 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3268 static __be32
3269 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3270 struct nfsd4_secinfo_no_name *secinfo)
3272 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3276 * The SETATTR encode routine is special -- it always encodes a bitmap,
3277 * regardless of the error status.
3279 static __be32
3280 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3282 __be32 *p;
3284 RESERVE_SPACE(16);
3285 if (nfserr) {
3286 WRITE32(3);
3287 WRITE32(0);
3288 WRITE32(0);
3289 WRITE32(0);
3291 else {
3292 WRITE32(3);
3293 WRITE32(setattr->sa_bmval[0]);
3294 WRITE32(setattr->sa_bmval[1]);
3295 WRITE32(setattr->sa_bmval[2]);
3297 ADJUST_ARGS();
3298 return nfserr;
3301 static __be32
3302 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3304 __be32 *p;
3306 if (!nfserr) {
3307 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
3308 WRITEMEM(&scd->se_clientid, 8);
3309 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
3310 ADJUST_ARGS();
3312 else if (nfserr == nfserr_clid_inuse) {
3313 RESERVE_SPACE(8);
3314 WRITE32(0);
3315 WRITE32(0);
3316 ADJUST_ARGS();
3318 return nfserr;
3321 static __be32
3322 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3324 __be32 *p;
3326 if (!nfserr) {
3327 RESERVE_SPACE(16);
3328 WRITE32(write->wr_bytes_written);
3329 WRITE32(write->wr_how_written);
3330 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
3331 ADJUST_ARGS();
3333 return nfserr;
3336 static const u32 nfs4_minimal_spo_must_enforce[2] = {
3337 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3338 1 << (OP_EXCHANGE_ID - 32) |
3339 1 << (OP_CREATE_SESSION - 32) |
3340 1 << (OP_DESTROY_SESSION - 32) |
3341 1 << (OP_DESTROY_CLIENTID - 32)
3344 static __be32
3345 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
3346 struct nfsd4_exchange_id *exid)
3348 __be32 *p;
3349 char *major_id;
3350 char *server_scope;
3351 int major_id_sz;
3352 int server_scope_sz;
3353 uint64_t minor_id = 0;
3355 if (nfserr)
3356 return nfserr;
3358 major_id = utsname()->nodename;
3359 major_id_sz = strlen(major_id);
3360 server_scope = utsname()->nodename;
3361 server_scope_sz = strlen(server_scope);
3363 RESERVE_SPACE(
3364 8 /* eir_clientid */ +
3365 4 /* eir_sequenceid */ +
3366 4 /* eir_flags */ +
3367 4 /* spr_how */ +
3368 8 /* spo_must_enforce, spo_must_allow */ +
3369 8 /* so_minor_id */ +
3370 4 /* so_major_id.len */ +
3371 (XDR_QUADLEN(major_id_sz) * 4) +
3372 4 /* eir_server_scope.len */ +
3373 (XDR_QUADLEN(server_scope_sz) * 4) +
3374 4 /* eir_server_impl_id.count (0) */);
3376 WRITEMEM(&exid->clientid, 8);
3377 WRITE32(exid->seqid);
3378 WRITE32(exid->flags);
3380 WRITE32(exid->spa_how);
3381 switch (exid->spa_how) {
3382 case SP4_NONE:
3383 break;
3384 case SP4_MACH_CRED:
3385 /* spo_must_enforce bitmap: */
3386 WRITE32(2);
3387 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3388 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3389 /* empty spo_must_allow bitmap: */
3390 WRITE32(0);
3391 break;
3392 default:
3393 WARN_ON_ONCE(1);
3396 /* The server_owner struct */
3397 WRITE64(minor_id); /* Minor id */
3398 /* major id */
3399 WRITE32(major_id_sz);
3400 WRITEMEM(major_id, major_id_sz);
3402 /* Server scope */
3403 WRITE32(server_scope_sz);
3404 WRITEMEM(server_scope, server_scope_sz);
3406 /* Implementation id */
3407 WRITE32(0); /* zero length nfs_impl_id4 array */
3408 ADJUST_ARGS();
3409 return 0;
3412 static __be32
3413 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3414 struct nfsd4_create_session *sess)
3416 __be32 *p;
3418 if (nfserr)
3419 return nfserr;
3421 RESERVE_SPACE(24);
3422 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3423 WRITE32(sess->seqid);
3424 WRITE32(sess->flags);
3425 ADJUST_ARGS();
3427 RESERVE_SPACE(28);
3428 WRITE32(0); /* headerpadsz */
3429 WRITE32(sess->fore_channel.maxreq_sz);
3430 WRITE32(sess->fore_channel.maxresp_sz);
3431 WRITE32(sess->fore_channel.maxresp_cached);
3432 WRITE32(sess->fore_channel.maxops);
3433 WRITE32(sess->fore_channel.maxreqs);
3434 WRITE32(sess->fore_channel.nr_rdma_attrs);
3435 ADJUST_ARGS();
3437 if (sess->fore_channel.nr_rdma_attrs) {
3438 RESERVE_SPACE(4);
3439 WRITE32(sess->fore_channel.rdma_attrs);
3440 ADJUST_ARGS();
3443 RESERVE_SPACE(28);
3444 WRITE32(0); /* headerpadsz */
3445 WRITE32(sess->back_channel.maxreq_sz);
3446 WRITE32(sess->back_channel.maxresp_sz);
3447 WRITE32(sess->back_channel.maxresp_cached);
3448 WRITE32(sess->back_channel.maxops);
3449 WRITE32(sess->back_channel.maxreqs);
3450 WRITE32(sess->back_channel.nr_rdma_attrs);
3451 ADJUST_ARGS();
3453 if (sess->back_channel.nr_rdma_attrs) {
3454 RESERVE_SPACE(4);
3455 WRITE32(sess->back_channel.rdma_attrs);
3456 ADJUST_ARGS();
3458 return 0;
3461 static __be32
3462 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3463 struct nfsd4_destroy_session *destroy_session)
3465 return nfserr;
3468 static __be32
3469 nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3470 struct nfsd4_free_stateid *free_stateid)
3472 __be32 *p;
3474 if (nfserr)
3475 return nfserr;
3477 RESERVE_SPACE(4);
3478 *p++ = nfserr;
3479 ADJUST_ARGS();
3480 return nfserr;
3483 static __be32
3484 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
3485 struct nfsd4_sequence *seq)
3487 __be32 *p;
3489 if (nfserr)
3490 return nfserr;
3492 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3493 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3494 WRITE32(seq->seqid);
3495 WRITE32(seq->slotid);
3496 /* Note slotid's are numbered from zero: */
3497 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3498 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
3499 WRITE32(seq->status_flags);
3501 ADJUST_ARGS();
3502 resp->cstate.datap = p; /* DRC cache data pointer */
3503 return 0;
3506 static __be32
3507 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3508 struct nfsd4_test_stateid *test_stateid)
3510 struct nfsd4_test_stateid_id *stateid, *next;
3511 __be32 *p;
3513 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
3514 *p++ = htonl(test_stateid->ts_num_ids);
3516 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
3517 *p++ = stateid->ts_id_status;
3520 ADJUST_ARGS();
3521 return nfserr;
3524 static __be32
3525 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3527 return nfserr;
3530 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3533 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3534 * since we don't need to filter out obsolete ops as this is
3535 * done in the decoding phase.
3537 static nfsd4_enc nfsd4_enc_ops[] = {
3538 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3539 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3540 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3541 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3542 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3543 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3544 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3545 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3546 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3547 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3548 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3549 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3550 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3551 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3552 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3553 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
3554 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
3555 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3556 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3557 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3558 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3559 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3560 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3561 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3562 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3563 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3564 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3565 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3566 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3567 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3568 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3569 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3570 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3571 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3572 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3573 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3574 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
3576 /* NFSv4.1 operations */
3577 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3578 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
3579 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3580 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3581 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3582 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
3583 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3584 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3585 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3586 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3587 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3588 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3589 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
3590 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3591 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3592 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
3593 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3594 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3595 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
3599 * Calculate the total amount of memory that the compound response has taken
3600 * after encoding the current operation with pad.
3602 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3603 * which was specified at nfsd4_operation, else pad is zero.
3605 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
3607 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3608 * will be at least a page and will therefore hold the xdr_buf head.
3610 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
3612 struct xdr_buf *xb = &resp->rqstp->rq_res;
3613 struct nfsd4_session *session = NULL;
3614 struct nfsd4_slot *slot = resp->cstate.slot;
3615 u32 length, tlen = 0;
3617 if (!nfsd4_has_session(&resp->cstate))
3618 return 0;
3620 session = resp->cstate.session;
3621 if (session == NULL)
3622 return 0;
3624 if (xb->page_len == 0) {
3625 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3626 } else {
3627 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3628 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3630 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3632 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3633 length, xb->page_len, tlen, pad);
3635 if (length > session->se_fchannel.maxresp_sz)
3636 return nfserr_rep_too_big;
3638 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
3639 length > session->se_fchannel.maxresp_cached)
3640 return nfserr_rep_too_big_to_cache;
3642 return 0;
3645 void
3646 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3648 struct nfs4_stateowner *so = resp->cstate.replay_owner;
3649 __be32 *statp;
3650 __be32 *p;
3652 RESERVE_SPACE(8);
3653 WRITE32(op->opnum);
3654 statp = p++; /* to be backfilled at the end */
3655 ADJUST_ARGS();
3657 if (op->opnum == OP_ILLEGAL)
3658 goto status;
3659 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3660 !nfsd4_enc_ops[op->opnum]);
3661 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3662 /* nfsd4_check_drc_limit guarantees enough room for error status */
3663 if (!op->status)
3664 op->status = nfsd4_check_resp_size(resp, 0);
3665 if (so) {
3666 so->so_replay.rp_status = op->status;
3667 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3668 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3670 status:
3672 * Note: We write the status directly, instead of using WRITE32(),
3673 * since it is already in network byte order.
3675 *statp = op->status;
3679 * Encode the reply stored in the stateowner reply cache
3681 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3682 * previously sent already encoded operation.
3684 * called with nfs4_lock_state() held
3686 void
3687 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3689 __be32 *p;
3690 struct nfs4_replay *rp = op->replay;
3692 BUG_ON(!rp);
3694 RESERVE_SPACE(8);
3695 WRITE32(op->opnum);
3696 *p++ = rp->rp_status; /* already xdr'ed */
3697 ADJUST_ARGS();
3699 RESERVE_SPACE(rp->rp_buflen);
3700 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3701 ADJUST_ARGS();
3705 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3707 return xdr_ressize_check(rqstp, p);
3710 int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
3712 struct svc_rqst *rqstp = rq;
3713 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3715 if (args->ops != args->iops) {
3716 kfree(args->ops);
3717 args->ops = args->iops;
3719 kfree(args->tmpp);
3720 args->tmpp = NULL;
3721 while (args->to_free) {
3722 struct tmpbuf *tb = args->to_free;
3723 args->to_free = tb->next;
3724 tb->release(tb->buf);
3725 kfree(tb);
3727 return 1;
3731 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3733 args->p = p;
3734 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3735 args->pagelist = rqstp->rq_arg.pages;
3736 args->pagelen = rqstp->rq_arg.page_len;
3737 args->tmpp = NULL;
3738 args->to_free = NULL;
3739 args->ops = args->iops;
3740 args->rqstp = rqstp;
3742 return !nfsd4_decode_compound(args);
3746 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3749 * All that remains is to write the tag and operation count...
3751 struct nfsd4_compound_state *cs = &resp->cstate;
3752 struct kvec *iov;
3753 p = resp->tagp;
3754 *p++ = htonl(resp->taglen);
3755 memcpy(p, resp->tag, resp->taglen);
3756 p += XDR_QUADLEN(resp->taglen);
3757 *p++ = htonl(resp->opcnt);
3759 if (rqstp->rq_res.page_len)
3760 iov = &rqstp->rq_res.tail[0];
3761 else
3762 iov = &rqstp->rq_res.head[0];
3763 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3764 BUG_ON(iov->iov_len > PAGE_SIZE);
3765 if (nfsd4_has_session(cs)) {
3766 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3767 struct nfs4_client *clp = cs->session->se_client;
3768 if (cs->status != nfserr_replay_cache) {
3769 nfsd4_store_cache_entry(resp);
3770 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3772 /* Renew the clientid on success and on replay */
3773 spin_lock(&nn->client_lock);
3774 nfsd4_put_session(cs->session);
3775 spin_unlock(&nn->client_lock);
3776 put_client_renew(clp);
3778 return 1;
3782 * Local variables:
3783 * c-basic-offset: 8
3784 * End: