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