blob: 6973d61bedeaa7ef5e9e3b0519dc51e9b20e1c0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
41 */
42
43#include <linux/param.h>
44#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/fs.h>
46#include <linux/namei.h>
47#include <linux/vfs.h>
48#include <linux/sunrpc/xdr.h>
49#include <linux/sunrpc/svc.h>
50#include <linux/sunrpc/clnt.h>
51#include <linux/nfsd/nfsd.h>
52#include <linux/nfsd/state.h>
53#include <linux/nfsd/xdr4.h>
54#include <linux/nfsd_idmap.h>
55#include <linux/nfs4.h>
56#include <linux/nfs4_acl.h>
Andy Adamsondcb488a2007-07-17 04:04:51 -070057#include <linux/sunrpc/gss_api.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070058#include <linux/sunrpc/svcauth_gss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define NFSDDBG_FACILITY NFSDDBG_XDR
61
J.Bruce Fields42ca0992006-10-04 02:16:20 -070062/*
63 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
64 * directory in order to indicate to the client that a filesystem boundary is present
65 * We use a fixed fsid for a referral
66 */
67#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
68#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
69
Al Virob37ad282006-10-19 23:28:59 -070070static __be32
71check_filename(char *str, int len, __be32 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 int i;
74
75 if (len == 0)
76 return nfserr_inval;
77 if (isdotent(str, len))
78 return err;
79 for (i = 0; i < len; i++)
80 if (str[i] == '/')
81 return err;
82 return 0;
83}
84
85/*
86 * START OF "GENERIC" DECODE ROUTINES.
87 * These may look a little ugly since they are imported from a "generic"
88 * set of XDR encode/decode routines which are intended to be shared by
89 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
90 *
91 * If the pain of reading these is too great, it should be a straightforward
92 * task to translate them into Linux-specific versions which are more
93 * consistent with the style used in NFSv2/v3...
94 */
95#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070096 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070097 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define DECODE_TAIL \
99 status = 0; \
100out: \
101 return status; \
102xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400103 dprintk("NFSD: xdr error (%s:%d)\n", \
104 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 status = nfserr_bad_xdr; \
106 goto out
107
108#define READ32(x) (x) = ntohl(*p++)
109#define READ64(x) do { \
110 (x) = (u64)ntohl(*p++) << 32; \
111 (x) |= ntohl(*p++); \
112} while (0)
113#define READTIME(x) do { \
114 p++; \
115 (x) = ntohl(*p++); \
116 p++; \
117} while (0)
118#define READMEM(x,nbytes) do { \
119 x = (char *)p; \
120 p += XDR_QUADLEN(nbytes); \
121} while (0)
122#define SAVEMEM(x,nbytes) do { \
123 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
124 savemem(argp, p, nbytes) : \
125 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400126 dprintk("NFSD: xdr error (%s:%d)\n", \
127 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto xdr_error; \
129 } \
130 p += XDR_QUADLEN(nbytes); \
131} while (0)
132#define COPYMEM(x,nbytes) do { \
133 memcpy((x), p, nbytes); \
134 p += XDR_QUADLEN(nbytes); \
135} while (0)
136
137/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
138#define READ_BUF(nbytes) do { \
139 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
140 p = argp->p; \
141 argp->p += XDR_QUADLEN(nbytes); \
142 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400143 dprintk("NFSD: xdr error (%s:%d)\n", \
144 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto xdr_error; \
146 } \
147} while (0)
148
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500149static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 /* We want more bytes than seem to be available.
152 * Maybe we need a new page, maybe we have just run out
153 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500154 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700155 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (avail + argp->pagelen < nbytes)
157 return NULL;
158 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
159 return NULL;
160 /* ok, we can do it with the current plus the next page */
161 if (nbytes <= sizeof(argp->tmp))
162 p = argp->tmp;
163 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800164 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
166 if (!p)
167 return NULL;
168
169 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500170 /*
171 * The following memcpy is safe because read_buf is always
172 * called with nbytes > avail, and the two cases above both
173 * guarantee p points to at least nbytes bytes.
174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 memcpy(p, argp->p, avail);
176 /* step to next page */
177 argp->p = page_address(argp->pagelist[0]);
178 argp->pagelist++;
179 if (argp->pagelen < PAGE_SIZE) {
180 argp->end = p + (argp->pagelen>>2);
181 argp->pagelen = 0;
182 } else {
183 argp->end = p + (PAGE_SIZE>>2);
184 argp->pagelen -= PAGE_SIZE;
185 }
186 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
187 argp->p += XDR_QUADLEN(nbytes - avail);
188 return p;
189}
190
191static int
192defer_free(struct nfsd4_compoundargs *argp,
193 void (*release)(const void *), void *p)
194{
195 struct tmpbuf *tb;
196
197 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
198 if (!tb)
199 return -ENOMEM;
200 tb->buf = p;
201 tb->release = release;
202 tb->next = argp->to_free;
203 argp->to_free = tb;
204 return 0;
205}
206
Al Viro2ebbc012006-10-19 23:28:58 -0700207static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (p == argp->tmp) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800210 p = kmalloc(nbytes, GFP_KERNEL);
211 if (!p)
212 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 memcpy(p, argp->tmp, nbytes);
214 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200215 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 argp->tmpp = NULL;
217 }
218 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800219 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return NULL;
221 } else
222 return (char *)p;
223}
224
Al Virob37ad282006-10-19 23:28:59 -0700225static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
227{
228 u32 bmlen;
229 DECODE_HEAD;
230
231 bmval[0] = 0;
232 bmval[1] = 0;
233
234 READ_BUF(4);
235 READ32(bmlen);
236 if (bmlen > 1000)
237 goto xdr_error;
238
239 READ_BUF(bmlen << 2);
240 if (bmlen > 0)
241 READ32(bmval[0]);
242 if (bmlen > 1)
243 READ32(bmval[1]);
244
245 DECODE_TAIL;
246}
247
Al Virob37ad282006-10-19 23:28:59 -0700248static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *iattr,
250 struct nfs4_acl **acl)
251{
252 int expected_len, len = 0;
253 u32 dummy32;
254 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700255 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 DECODE_HEAD;
258 iattr->ia_valid = 0;
259 if ((status = nfsd4_decode_bitmap(argp, bmval)))
260 return status;
261
262 /*
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800263 * According to spec, unsupported attributes return ERR_ATTRNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * read-only attributes return ERR_INVAL.
265 */
266 if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
267 return nfserr_attrnotsupp;
268 if ((bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0) || (bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1))
269 return nfserr_inval;
270
271 READ_BUF(4);
272 READ32(expected_len);
273
274 if (bmval[0] & FATTR4_WORD0_SIZE) {
275 READ_BUF(8);
276 len += 8;
277 READ64(iattr->ia_size);
278 iattr->ia_valid |= ATTR_SIZE;
279 }
280 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800281 int nace;
282 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 READ_BUF(4); len += 4;
285 READ32(nace);
286
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800287 if (nace > NFS4_ACL_MAX)
288 return nfserr_resource;
289
290 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700292 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto out_nfserr;
294 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800295 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800297 (*acl)->naces = nace;
298 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800300 READ32(ace->type);
301 READ32(ace->flag);
302 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 READ32(dummy32);
304 READ_BUF(dummy32);
305 len += XDR_QUADLEN(dummy32) << 2;
306 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800307 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
Al Virob8dd7b92006-10-19 23:29:01 -0700308 host_err = 0;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800309 if (ace->whotype != NFS4_ACL_WHO_NAMED)
310 ace->who = 0;
311 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
Al Virob8dd7b92006-10-19 23:29:01 -0700312 host_err = nfsd_map_name_to_gid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800313 buf, dummy32, &ace->who);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 else
Al Virob8dd7b92006-10-19 23:29:01 -0700315 host_err = nfsd_map_name_to_uid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800316 buf, dummy32, &ace->who);
Al Virob8dd7b92006-10-19 23:29:01 -0700317 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 } else
321 *acl = NULL;
322 if (bmval[1] & FATTR4_WORD1_MODE) {
323 READ_BUF(4);
324 len += 4;
325 READ32(iattr->ia_mode);
326 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
327 iattr->ia_valid |= ATTR_MODE;
328 }
329 if (bmval[1] & FATTR4_WORD1_OWNER) {
330 READ_BUF(4);
331 len += 4;
332 READ32(dummy32);
333 READ_BUF(dummy32);
334 len += (XDR_QUADLEN(dummy32) << 2);
335 READMEM(buf, dummy32);
Al Virob8dd7b92006-10-19 23:29:01 -0700336 if ((host_err = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto out_nfserr;
338 iattr->ia_valid |= ATTR_UID;
339 }
340 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
341 READ_BUF(4);
342 len += 4;
343 READ32(dummy32);
344 READ_BUF(dummy32);
345 len += (XDR_QUADLEN(dummy32) << 2);
346 READMEM(buf, dummy32);
Al Virob8dd7b92006-10-19 23:29:01 -0700347 if ((host_err = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 goto out_nfserr;
349 iattr->ia_valid |= ATTR_GID;
350 }
351 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
352 READ_BUF(4);
353 len += 4;
354 READ32(dummy32);
355 switch (dummy32) {
356 case NFS4_SET_TO_CLIENT_TIME:
357 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
358 all 32 bits of 'nseconds'. */
359 READ_BUF(12);
360 len += 12;
361 READ32(dummy32);
362 if (dummy32)
363 return nfserr_inval;
364 READ32(iattr->ia_atime.tv_sec);
365 READ32(iattr->ia_atime.tv_nsec);
366 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
367 return nfserr_inval;
368 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
369 break;
370 case NFS4_SET_TO_SERVER_TIME:
371 iattr->ia_valid |= ATTR_ATIME;
372 break;
373 default:
374 goto xdr_error;
375 }
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
378 READ_BUF(4);
379 len += 4;
380 READ32(dummy32);
381 switch (dummy32) {
382 case NFS4_SET_TO_CLIENT_TIME:
383 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
384 all 32 bits of 'nseconds'. */
385 READ_BUF(12);
386 len += 12;
387 READ32(dummy32);
388 if (dummy32)
389 return nfserr_inval;
390 READ32(iattr->ia_mtime.tv_sec);
391 READ32(iattr->ia_mtime.tv_nsec);
392 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
393 return nfserr_inval;
394 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
395 break;
396 case NFS4_SET_TO_SERVER_TIME:
397 iattr->ia_valid |= ATTR_MTIME;
398 break;
399 default:
400 goto xdr_error;
401 }
402 }
403 if (len != expected_len)
404 goto xdr_error;
405
406 DECODE_TAIL;
407
408out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700409 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto out;
411}
412
Al Virob37ad282006-10-19 23:28:59 -0700413static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300414nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
415{
416 DECODE_HEAD;
417
418 READ_BUF(sizeof(stateid_t));
419 READ32(sid->si_generation);
420 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
421
422 DECODE_TAIL;
423}
424
425static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
427{
428 DECODE_HEAD;
429
430 READ_BUF(4);
431 READ32(access->ac_req_access);
432
433 DECODE_TAIL;
434}
435
Al Virob37ad282006-10-19 23:28:59 -0700436static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
438{
439 DECODE_HEAD;
440
441 close->cl_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300442 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300444 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 DECODE_TAIL;
447}
448
449
Al Virob37ad282006-10-19 23:28:59 -0700450static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
452{
453 DECODE_HEAD;
454
455 READ_BUF(12);
456 READ64(commit->co_offset);
457 READ32(commit->co_count);
458
459 DECODE_TAIL;
460}
461
Al Virob37ad282006-10-19 23:28:59 -0700462static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
464{
465 DECODE_HEAD;
466
467 READ_BUF(4);
468 READ32(create->cr_type);
469 switch (create->cr_type) {
470 case NF4LNK:
471 READ_BUF(4);
472 READ32(create->cr_linklen);
473 READ_BUF(create->cr_linklen);
474 SAVEMEM(create->cr_linkname, create->cr_linklen);
475 break;
476 case NF4BLK:
477 case NF4CHR:
478 READ_BUF(8);
479 READ32(create->cr_specdata1);
480 READ32(create->cr_specdata2);
481 break;
482 case NF4SOCK:
483 case NF4FIFO:
484 case NF4DIR:
485 default:
486 break;
487 }
488
489 READ_BUF(4);
490 READ32(create->cr_namelen);
491 READ_BUF(create->cr_namelen);
492 SAVEMEM(create->cr_name, create->cr_namelen);
493 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
494 return status;
495
496 if ((status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr, &create->cr_acl)))
497 goto out;
498
499 DECODE_TAIL;
500}
501
Al Virob37ad282006-10-19 23:28:59 -0700502static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
504{
Benny Halevye31a1b62008-08-12 20:46:18 +0300505 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Al Virob37ad282006-10-19 23:28:59 -0700508static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
510{
511 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
512}
513
Al Virob37ad282006-10-19 23:28:59 -0700514static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
516{
517 DECODE_HEAD;
518
519 READ_BUF(4);
520 READ32(link->li_namelen);
521 READ_BUF(link->li_namelen);
522 SAVEMEM(link->li_name, link->li_namelen);
523 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
524 return status;
525
526 DECODE_TAIL;
527}
528
Al Virob37ad282006-10-19 23:28:59 -0700529static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
531{
532 DECODE_HEAD;
533
J. Bruce Fields3a655882006-01-18 17:43:19 -0800534 lock->lk_replay_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
537 */
538 READ_BUF(28);
539 READ32(lock->lk_type);
540 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
541 goto xdr_error;
542 READ32(lock->lk_reclaim);
543 READ64(lock->lk_offset);
544 READ64(lock->lk_length);
545 READ32(lock->lk_is_new);
546
547 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300548 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300550 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
551 if (status)
552 return status;
553 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 READ32(lock->lk_new_lock_seqid);
555 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
556 READ32(lock->lk_new_owner.len);
557 READ_BUF(lock->lk_new_owner.len);
558 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
559 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300560 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
561 if (status)
562 return status;
563 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 READ32(lock->lk_old_lock_seqid);
565 }
566
567 DECODE_TAIL;
568}
569
Al Virob37ad282006-10-19 23:28:59 -0700570static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
572{
573 DECODE_HEAD;
574
575 READ_BUF(32);
576 READ32(lockt->lt_type);
577 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
578 goto xdr_error;
579 READ64(lockt->lt_offset);
580 READ64(lockt->lt_length);
581 COPYMEM(&lockt->lt_clientid, 8);
582 READ32(lockt->lt_owner.len);
583 READ_BUF(lockt->lt_owner.len);
584 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
585
586 DECODE_TAIL;
587}
588
Al Virob37ad282006-10-19 23:28:59 -0700589static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
591{
592 DECODE_HEAD;
593
594 locku->lu_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300595 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 READ32(locku->lu_type);
597 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
598 goto xdr_error;
599 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300600 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
601 if (status)
602 return status;
603 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 READ64(locku->lu_offset);
605 READ64(locku->lu_length);
606
607 DECODE_TAIL;
608}
609
Al Virob37ad282006-10-19 23:28:59 -0700610static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
612{
613 DECODE_HEAD;
614
615 READ_BUF(4);
616 READ32(lookup->lo_len);
617 READ_BUF(lookup->lo_len);
618 SAVEMEM(lookup->lo_name, lookup->lo_len);
619 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
620 return status;
621
622 DECODE_TAIL;
623}
624
Al Virob37ad282006-10-19 23:28:59 -0700625static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
627{
628 DECODE_HEAD;
629
630 memset(open->op_bmval, 0, sizeof(open->op_bmval));
631 open->op_iattr.ia_valid = 0;
632 open->op_stateowner = NULL;
633
634 /* seqid, share_access, share_deny, clientid, ownerlen */
635 READ_BUF(16 + sizeof(clientid_t));
636 READ32(open->op_seqid);
637 READ32(open->op_share_access);
638 READ32(open->op_share_deny);
639 COPYMEM(&open->op_clientid, sizeof(clientid_t));
640 READ32(open->op_owner.len);
641
642 /* owner, open_flag */
643 READ_BUF(open->op_owner.len + 4);
644 SAVEMEM(open->op_owner.data, open->op_owner.len);
645 READ32(open->op_create);
646 switch (open->op_create) {
647 case NFS4_OPEN_NOCREATE:
648 break;
649 case NFS4_OPEN_CREATE:
650 READ_BUF(4);
651 READ32(open->op_createmode);
652 switch (open->op_createmode) {
653 case NFS4_CREATE_UNCHECKED:
654 case NFS4_CREATE_GUARDED:
655 if ((status = nfsd4_decode_fattr(argp, open->op_bmval, &open->op_iattr, &open->op_acl)))
656 goto out;
657 break;
658 case NFS4_CREATE_EXCLUSIVE:
659 READ_BUF(8);
660 COPYMEM(open->op_verf.data, 8);
661 break;
662 default:
663 goto xdr_error;
664 }
665 break;
666 default:
667 goto xdr_error;
668 }
669
670 /* open_claim */
671 READ_BUF(4);
672 READ32(open->op_claim_type);
673 switch (open->op_claim_type) {
674 case NFS4_OPEN_CLAIM_NULL:
675 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
676 READ_BUF(4);
677 READ32(open->op_fname.len);
678 READ_BUF(open->op_fname.len);
679 SAVEMEM(open->op_fname.data, open->op_fname.len);
680 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
681 return status;
682 break;
683 case NFS4_OPEN_CLAIM_PREVIOUS:
684 READ_BUF(4);
685 READ32(open->op_delegate_type);
686 break;
687 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300688 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
689 if (status)
690 return status;
691 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 READ32(open->op_fname.len);
693 READ_BUF(open->op_fname.len);
694 SAVEMEM(open->op_fname.data, open->op_fname.len);
695 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
696 return status;
697 break;
698 default:
699 goto xdr_error;
700 }
701
702 DECODE_TAIL;
703}
704
Al Virob37ad282006-10-19 23:28:59 -0700705static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
707{
708 DECODE_HEAD;
709
710 open_conf->oc_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300711 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
712 if (status)
713 return status;
714 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 READ32(open_conf->oc_seqid);
716
717 DECODE_TAIL;
718}
719
Al Virob37ad282006-10-19 23:28:59 -0700720static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
722{
723 DECODE_HEAD;
724
725 open_down->od_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300726 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
727 if (status)
728 return status;
729 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 READ32(open_down->od_seqid);
731 READ32(open_down->od_share_access);
732 READ32(open_down->od_share_deny);
733
734 DECODE_TAIL;
735}
736
Al Virob37ad282006-10-19 23:28:59 -0700737static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
739{
740 DECODE_HEAD;
741
742 READ_BUF(4);
743 READ32(putfh->pf_fhlen);
744 if (putfh->pf_fhlen > NFS4_FHSIZE)
745 goto xdr_error;
746 READ_BUF(putfh->pf_fhlen);
747 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
748
749 DECODE_TAIL;
750}
751
Al Virob37ad282006-10-19 23:28:59 -0700752static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
754{
755 DECODE_HEAD;
756
Benny Halevye31a1b62008-08-12 20:46:18 +0300757 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
758 if (status)
759 return status;
760 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 READ64(read->rd_offset);
762 READ32(read->rd_length);
763
764 DECODE_TAIL;
765}
766
Al Virob37ad282006-10-19 23:28:59 -0700767static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
769{
770 DECODE_HEAD;
771
772 READ_BUF(24);
773 READ64(readdir->rd_cookie);
774 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
775 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
776 READ32(readdir->rd_maxcount);
777 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
778 goto out;
779
780 DECODE_TAIL;
781}
782
Al Virob37ad282006-10-19 23:28:59 -0700783static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
785{
786 DECODE_HEAD;
787
788 READ_BUF(4);
789 READ32(remove->rm_namelen);
790 READ_BUF(remove->rm_namelen);
791 SAVEMEM(remove->rm_name, remove->rm_namelen);
792 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
793 return status;
794
795 DECODE_TAIL;
796}
797
Al Virob37ad282006-10-19 23:28:59 -0700798static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
800{
801 DECODE_HEAD;
802
803 READ_BUF(4);
804 READ32(rename->rn_snamelen);
805 READ_BUF(rename->rn_snamelen + 4);
806 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
807 READ32(rename->rn_tnamelen);
808 READ_BUF(rename->rn_tnamelen);
809 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
810 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
811 return status;
812 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
813 return status;
814
815 DECODE_TAIL;
816}
817
Al Virob37ad282006-10-19 23:28:59 -0700818static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
820{
821 DECODE_HEAD;
822
823 READ_BUF(sizeof(clientid_t));
824 COPYMEM(clientid, sizeof(clientid_t));
825
826 DECODE_TAIL;
827}
828
Al Virob37ad282006-10-19 23:28:59 -0700829static __be32
Andy Adamsondcb488a2007-07-17 04:04:51 -0700830nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
831 struct nfsd4_secinfo *secinfo)
832{
833 DECODE_HEAD;
834
835 READ_BUF(4);
836 READ32(secinfo->si_namelen);
837 READ_BUF(secinfo->si_namelen);
838 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
839 status = check_filename(secinfo->si_name, secinfo->si_namelen,
840 nfserr_noent);
841 if (status)
842 return status;
843 DECODE_TAIL;
844}
845
846static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
848{
Benny Halevye31a1b62008-08-12 20:46:18 +0300849 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Benny Halevye31a1b62008-08-12 20:46:18 +0300851 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
852 if (status)
853 return status;
854 return nfsd4_decode_fattr(argp, setattr->sa_bmval,
855 &setattr->sa_iattr, &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Al Virob37ad282006-10-19 23:28:59 -0700858static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
860{
861 DECODE_HEAD;
862
863 READ_BUF(12);
864 COPYMEM(setclientid->se_verf.data, 8);
865 READ32(setclientid->se_namelen);
866
867 READ_BUF(setclientid->se_namelen + 8);
868 SAVEMEM(setclientid->se_name, setclientid->se_namelen);
869 READ32(setclientid->se_callback_prog);
870 READ32(setclientid->se_callback_netid_len);
871
872 READ_BUF(setclientid->se_callback_netid_len + 4);
873 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
874 READ32(setclientid->se_callback_addr_len);
875
876 READ_BUF(setclientid->se_callback_addr_len + 4);
877 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
878 READ32(setclientid->se_callback_ident);
879
880 DECODE_TAIL;
881}
882
Al Virob37ad282006-10-19 23:28:59 -0700883static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
885{
886 DECODE_HEAD;
887
888 READ_BUF(8 + sizeof(nfs4_verifier));
889 COPYMEM(&scd_c->sc_clientid, 8);
890 COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
891
892 DECODE_TAIL;
893}
894
895/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -0700896static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
898{
899#if 0
900 struct nfsd4_compoundargs save = {
901 .p = argp->p,
902 .end = argp->end,
903 .rqstp = argp->rqstp,
904 };
905 u32 ve_bmval[2];
906 struct iattr ve_iattr; /* request */
907 struct nfs4_acl *ve_acl; /* request */
908#endif
909 DECODE_HEAD;
910
911 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
912 goto out;
913
914 /* For convenience's sake, we compare raw xdr'd attributes in
915 * nfsd4_proc_verify; however we still decode here just to return
916 * correct error in case of bad xdr. */
917#if 0
918 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
919 if (status == nfserr_inval) {
920 status = nfserrno(status);
921 goto out;
922 }
923#endif
924 READ_BUF(4);
925 READ32(verify->ve_attrlen);
926 READ_BUF(verify->ve_attrlen);
927 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
928
929 DECODE_TAIL;
930}
931
Al Virob37ad282006-10-19 23:28:59 -0700932static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
934{
935 int avail;
936 int v;
937 int len;
938 DECODE_HEAD;
939
Benny Halevye31a1b62008-08-12 20:46:18 +0300940 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
941 if (status)
942 return status;
943 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 READ64(write->wr_offset);
945 READ32(write->wr_stable_how);
946 if (write->wr_stable_how > 2)
947 goto xdr_error;
948 READ32(write->wr_buflen);
949
950 /* Sorry .. no magic macros for this.. *
951 * READ_BUF(write->wr_buflen);
952 * SAVEMEM(write->wr_buf, write->wr_buflen);
953 */
954 avail = (char*)argp->end - (char*)argp->p;
955 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -0400956 dprintk("NFSD: xdr error (%s:%d)\n",
957 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 goto xdr_error;
959 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700960 argp->rqstp->rq_vec[0].iov_base = p;
961 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 v = 0;
963 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -0700964 while (len > argp->rqstp->rq_vec[v].iov_len) {
965 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700967 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 argp->pagelist++;
969 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -0700970 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 argp->pagelen -= PAGE_SIZE;
972 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -0700973 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 argp->pagelen -= len;
975 }
976 }
Al Viro2ebbc012006-10-19 23:28:58 -0700977 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
978 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -0700979 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 write->wr_vlen = v+1;
981
982 DECODE_TAIL;
983}
984
Al Virob37ad282006-10-19 23:28:59 -0700985static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
987{
988 DECODE_HEAD;
989
990 READ_BUF(12);
991 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
992 READ32(rlockowner->rl_owner.len);
993 READ_BUF(rlockowner->rl_owner.len);
994 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
995
996 DECODE_TAIL;
997}
998
Al Virob37ad282006-10-19 23:28:59 -0700999static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001000nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1001 struct nfsd4_exchange_id *clid)
1002{
1003 return nfserr_opnotsupp; /* stub */
1004}
1005
1006static __be32
1007nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1008 struct nfsd4_create_session *sess)
1009{
1010 return nfserr_opnotsupp; /* stub */
1011}
1012
1013static __be32
1014nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1015 struct nfsd4_destroy_session *destroy_session)
1016{
1017 return nfserr_opnotsupp; /* stub */
1018}
1019
1020static __be32
1021nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1022 struct nfsd4_sequence *seq)
1023{
1024 return nfserr_opnotsupp; /* stub */
1025}
1026
1027static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001028nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1029{
1030 return nfs_ok;
1031}
1032
Benny Halevy3c375c62008-07-02 11:14:01 +03001033static __be32
1034nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1035{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001036 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001037}
1038
Benny Halevy347e0ad2008-07-02 11:13:41 +03001039typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1040
1041static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001042 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1043 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1044 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1045 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1046 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1047 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1048 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1049 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1050 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1051 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1052 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1053 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1054 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1055 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1056 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1057 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1058 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1059 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1060 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1061 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001062 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001063 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1064 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1065 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1066 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1067 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1068 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1069 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1070 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1071 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1072 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1073 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1074 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1075 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1076 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1077 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1078 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001079};
1080
Andy Adamson2db134e2009-04-03 08:27:55 +03001081static nfsd4_dec nfsd41_dec_ops[] = {
1082 [OP_ACCESS] (nfsd4_dec)nfsd4_decode_access,
1083 [OP_CLOSE] (nfsd4_dec)nfsd4_decode_close,
1084 [OP_COMMIT] (nfsd4_dec)nfsd4_decode_commit,
1085 [OP_CREATE] (nfsd4_dec)nfsd4_decode_create,
1086 [OP_DELEGPURGE] (nfsd4_dec)nfsd4_decode_notsupp,
1087 [OP_DELEGRETURN] (nfsd4_dec)nfsd4_decode_delegreturn,
1088 [OP_GETATTR] (nfsd4_dec)nfsd4_decode_getattr,
1089 [OP_GETFH] (nfsd4_dec)nfsd4_decode_noop,
1090 [OP_LINK] (nfsd4_dec)nfsd4_decode_link,
1091 [OP_LOCK] (nfsd4_dec)nfsd4_decode_lock,
1092 [OP_LOCKT] (nfsd4_dec)nfsd4_decode_lockt,
1093 [OP_LOCKU] (nfsd4_dec)nfsd4_decode_locku,
1094 [OP_LOOKUP] (nfsd4_dec)nfsd4_decode_lookup,
1095 [OP_LOOKUPP] (nfsd4_dec)nfsd4_decode_noop,
1096 [OP_NVERIFY] (nfsd4_dec)nfsd4_decode_verify,
1097 [OP_OPEN] (nfsd4_dec)nfsd4_decode_open,
1098 [OP_OPENATTR] (nfsd4_dec)nfsd4_decode_notsupp,
1099 [OP_OPEN_CONFIRM] (nfsd4_dec)nfsd4_decode_notsupp,
1100 [OP_OPEN_DOWNGRADE] (nfsd4_dec)nfsd4_decode_open_downgrade,
1101 [OP_PUTFH] (nfsd4_dec)nfsd4_decode_putfh,
1102 [OP_PUTPUBFH] (nfsd4_dec)nfsd4_decode_notsupp,
1103 [OP_PUTROOTFH] (nfsd4_dec)nfsd4_decode_noop,
1104 [OP_READ] (nfsd4_dec)nfsd4_decode_read,
1105 [OP_READDIR] (nfsd4_dec)nfsd4_decode_readdir,
1106 [OP_READLINK] (nfsd4_dec)nfsd4_decode_noop,
1107 [OP_REMOVE] (nfsd4_dec)nfsd4_decode_remove,
1108 [OP_RENAME] (nfsd4_dec)nfsd4_decode_rename,
1109 [OP_RENEW] (nfsd4_dec)nfsd4_decode_notsupp,
1110 [OP_RESTOREFH] (nfsd4_dec)nfsd4_decode_noop,
1111 [OP_SAVEFH] (nfsd4_dec)nfsd4_decode_noop,
1112 [OP_SECINFO] (nfsd4_dec)nfsd4_decode_secinfo,
1113 [OP_SETATTR] (nfsd4_dec)nfsd4_decode_setattr,
1114 [OP_SETCLIENTID] (nfsd4_dec)nfsd4_decode_notsupp,
1115 [OP_SETCLIENTID_CONFIRM](nfsd4_dec)nfsd4_decode_notsupp,
1116 [OP_VERIFY] (nfsd4_dec)nfsd4_decode_verify,
1117 [OP_WRITE] (nfsd4_dec)nfsd4_decode_write,
1118 [OP_RELEASE_LOCKOWNER] (nfsd4_dec)nfsd4_decode_notsupp,
1119
1120 /* new operations for NFSv4.1 */
1121 [OP_BACKCHANNEL_CTL] (nfsd4_dec)nfsd4_decode_notsupp,
1122 [OP_BIND_CONN_TO_SESSION](nfsd4_dec)nfsd4_decode_notsupp,
1123 [OP_EXCHANGE_ID] (nfsd4_dec)nfsd4_decode_exchange_id,
1124 [OP_CREATE_SESSION] (nfsd4_dec)nfsd4_decode_create_session,
1125 [OP_DESTROY_SESSION] (nfsd4_dec)nfsd4_decode_destroy_session,
1126 [OP_FREE_STATEID] (nfsd4_dec)nfsd4_decode_notsupp,
1127 [OP_GET_DIR_DELEGATION] (nfsd4_dec)nfsd4_decode_notsupp,
1128 [OP_GETDEVICEINFO] (nfsd4_dec)nfsd4_decode_notsupp,
1129 [OP_GETDEVICELIST] (nfsd4_dec)nfsd4_decode_notsupp,
1130 [OP_LAYOUTCOMMIT] (nfsd4_dec)nfsd4_decode_notsupp,
1131 [OP_LAYOUTGET] (nfsd4_dec)nfsd4_decode_notsupp,
1132 [OP_LAYOUTRETURN] (nfsd4_dec)nfsd4_decode_notsupp,
1133 [OP_SECINFO_NO_NAME] (nfsd4_dec)nfsd4_decode_notsupp,
1134 [OP_SEQUENCE] (nfsd4_dec)nfsd4_decode_sequence,
1135 [OP_SET_SSV] (nfsd4_dec)nfsd4_decode_notsupp,
1136 [OP_TEST_STATEID] (nfsd4_dec)nfsd4_decode_notsupp,
1137 [OP_WANT_DELEGATION] (nfsd4_dec)nfsd4_decode_notsupp,
1138 [OP_DESTROY_CLIENTID] (nfsd4_dec)nfsd4_decode_notsupp,
1139 [OP_RECLAIM_COMPLETE] (nfsd4_dec)nfsd4_decode_notsupp,
1140};
1141
Benny Halevyf2feb962008-07-02 11:14:22 +03001142struct nfsd4_minorversion_ops {
1143 nfsd4_dec *decoders;
1144 int nops;
1145};
1146
1147static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001148 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001149 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001150};
1151
Benny Halevy347e0ad2008-07-02 11:13:41 +03001152static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1154{
1155 DECODE_HEAD;
1156 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001157 struct nfsd4_minorversion_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int i;
1159
1160 /*
1161 * XXX: According to spec, we should check the tag
1162 * for UTF-8 compliance. I'm postponing this for
1163 * now because it seems that some clients do use
1164 * binary tags.
1165 */
1166 READ_BUF(4);
1167 READ32(argp->taglen);
1168 READ_BUF(argp->taglen + 8);
1169 SAVEMEM(argp->tag, argp->taglen);
1170 READ32(argp->minorversion);
1171 READ32(argp->opcnt);
1172
1173 if (argp->taglen > NFSD4_MAX_TAGLEN)
1174 goto xdr_error;
1175 if (argp->opcnt > 100)
1176 goto xdr_error;
1177
Tobias Klausere8c96f82006-03-24 03:15:34 -08001178 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1180 if (!argp->ops) {
1181 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001182 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto xdr_error;
1184 }
1185 }
1186
Benny Halevyf2feb962008-07-02 11:14:22 +03001187 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001188 argp->opcnt = 0;
1189
Benny Halevyf2feb962008-07-02 11:14:22 +03001190 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 for (i = 0; i < argp->opcnt; i++) {
1192 op = &argp->ops[i];
1193 op->replay = NULL;
1194
1195 /*
1196 * We can't use READ_BUF() here because we need to handle
1197 * a missing opcode as an OP_WRITE + 1. So we need to check
1198 * to see if we're truly at the end of our buffer or if there
1199 * is another page we need to flip to.
1200 */
1201
1202 if (argp->p == argp->end) {
1203 if (argp->pagelen < 4) {
1204 /* There isn't an opcode still on the wire */
1205 op->opnum = OP_WRITE + 1;
1206 op->status = nfserr_bad_xdr;
1207 argp->opcnt = i+1;
1208 break;
1209 }
1210
1211 /*
1212 * False alarm. We just hit a page boundary, but there
1213 * is still data available. Move pointer across page
1214 * boundary. *snip from READ_BUF*
1215 */
1216 argp->p = page_address(argp->pagelist[0]);
1217 argp->pagelist++;
1218 if (argp->pagelen < PAGE_SIZE) {
1219 argp->end = p + (argp->pagelen>>2);
1220 argp->pagelen = 0;
1221 } else {
1222 argp->end = p + (PAGE_SIZE>>2);
1223 argp->pagelen -= PAGE_SIZE;
1224 }
1225 }
1226 op->opnum = ntohl(*argp->p++);
1227
Benny Halevyf2feb962008-07-02 11:14:22 +03001228 if (op->opnum >= OP_ACCESS && op->opnum < ops->nops)
1229 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001230 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 op->opnum = OP_ILLEGAL;
1232 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 if (op->status) {
1236 argp->opcnt = i+1;
1237 break;
1238 }
1239 }
1240
1241 DECODE_TAIL;
1242}
1243/*
1244 * END OF "GENERIC" DECODE ROUTINES.
1245 */
1246
1247/*
1248 * START OF "GENERIC" ENCODE ROUTINES.
1249 * These may look a little ugly since they are imported from a "generic"
1250 * set of XDR encode/decode routines which are intended to be shared by
1251 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1252 *
1253 * If the pain of reading these is too great, it should be a straightforward
1254 * task to translate them into Linux-specific versions which are more
1255 * consistent with the style used in NFSv2/v3...
1256 */
Al Viro2ebbc012006-10-19 23:28:58 -07001257#define ENCODE_HEAD __be32 *p
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259#define WRITE32(n) *p++ = htonl(n)
1260#define WRITE64(n) do { \
1261 *p++ = htonl((u32)((n) >> 32)); \
1262 *p++ = htonl((u32)(n)); \
1263} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001264#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1266 memcpy(p, ptr, nbytes); \
1267 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001268}} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#define WRITECINFO(c) do { \
1270 *p++ = htonl(c.atomic); \
1271 *p++ = htonl(c.before_ctime_sec); \
1272 *p++ = htonl(c.before_ctime_nsec); \
1273 *p++ = htonl(c.after_ctime_sec); \
1274 *p++ = htonl(c.after_ctime_nsec); \
1275} while (0)
1276
1277#define RESERVE_SPACE(nbytes) do { \
1278 p = resp->p; \
1279 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1280} while (0)
1281#define ADJUST_ARGS() resp->p = p
1282
1283/*
1284 * Header routine to setup seqid operation replay cache
1285 */
1286#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001287 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 \
1289 save = resp->p;
1290
1291/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001292 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1293 * is where sequence id's are incremented, and the replay cache is filled.
1294 * Note that we increment sequence id's here, at the last moment, so we're sure
1295 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297
1298#define ENCODE_SEQID_OP_TAIL(stateowner) do { \
1299 if (seqid_mutating_err(nfserr) && stateowner) { \
NeilBrownbd9aac52005-07-07 17:59:19 -07001300 stateowner->so_seqid++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 stateowner->so_replay.rp_status = nfserr; \
1302 stateowner->so_replay.rp_buflen = \
1303 (((char *)(resp)->p - (char *)save)); \
1304 memcpy(stateowner->so_replay.rp_buf, save, \
1305 stateowner->so_replay.rp_buflen); \
1306 } } while (0);
1307
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001308/* Encode as an array of strings the string given with components
1309 * seperated @sep.
1310 */
Al Virob37ad282006-10-19 23:28:59 -07001311static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001312 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001313{
Al Viro2ebbc012006-10-19 23:28:58 -07001314 __be32 *p = *pp;
1315 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001316 int strlen, count=0;
1317 char *str, *end;
1318
1319 dprintk("nfsd4_encode_components(%s)\n", components);
1320 if ((*buflen -= 4) < 0)
1321 return nfserr_resource;
1322 WRITE32(0); /* We will fill this in with @count later */
1323 end = str = components;
1324 while (*end) {
1325 for (; *end && (*end != sep); end++)
1326 ; /* Point to end of component */
1327 strlen = end - str;
1328 if (strlen) {
1329 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1330 return nfserr_resource;
1331 WRITE32(strlen);
1332 WRITEMEM(str, strlen);
1333 count++;
1334 }
1335 else
1336 end++;
1337 str = end;
1338 }
1339 *pp = p;
1340 p = countp;
1341 WRITE32(count);
1342 return 0;
1343}
1344
1345/*
1346 * encode a location element of a fs_locations structure
1347 */
Al Virob37ad282006-10-19 23:28:59 -07001348static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001349 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001350{
Al Virob37ad282006-10-19 23:28:59 -07001351 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001352 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001353
1354 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1355 if (status)
1356 return status;
1357 status = nfsd4_encode_components('/', location->path, &p, buflen);
1358 if (status)
1359 return status;
1360 *pp = p;
1361 return 0;
1362}
1363
1364/*
1365 * Return the path to an export point in the pseudo filesystem namespace
1366 * Returned string is safe to use as long as the caller holds a reference
1367 * to @exp.
1368 */
Al Virob37ad282006-10-19 23:28:59 -07001369static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001370{
1371 struct svc_fh tmp_fh;
1372 char *path, *rootpath;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001373
1374 fh_init(&tmp_fh, NFS4_FHSIZE);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -07001375 *stat = exp_pseudoroot(rqstp, &tmp_fh);
Al Virocc45f012006-10-19 23:28:44 -07001376 if (*stat)
1377 return NULL;
Jan Blunck54775492008-02-14 19:38:39 -08001378 rootpath = tmp_fh.fh_export->ex_pathname;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001379
Jan Blunck54775492008-02-14 19:38:39 -08001380 path = exp->ex_pathname;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001381
1382 if (strncmp(path, rootpath, strlen(rootpath))) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001383 dprintk("nfsd: fs_locations failed;"
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001384 "%s is not contained in %s\n", path, rootpath);
Al Virocc45f012006-10-19 23:28:44 -07001385 *stat = nfserr_notsupp;
1386 return NULL;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001387 }
1388
1389 return path + strlen(rootpath);
1390}
1391
1392/*
1393 * encode a fs_locations structure
1394 */
Al Virob37ad282006-10-19 23:28:59 -07001395static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001396 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001397 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001398{
Al Virob37ad282006-10-19 23:28:59 -07001399 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001400 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001401 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001402 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
Al Virocc45f012006-10-19 23:28:44 -07001403 char *root = nfsd4_path(rqstp, exp, &status);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001404
Al Virocc45f012006-10-19 23:28:44 -07001405 if (status)
1406 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001407 status = nfsd4_encode_components('/', root, &p, buflen);
1408 if (status)
1409 return status;
1410 if ((*buflen -= 4) < 0)
1411 return nfserr_resource;
1412 WRITE32(fslocs->locations_count);
1413 for (i=0; i<fslocs->locations_count; i++) {
1414 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1415 &p, buflen);
1416 if (status)
1417 return status;
1418 }
1419 *pp = p;
1420 return 0;
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423static u32 nfs4_ftypes[16] = {
1424 NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
1425 NF4DIR, NF4BAD, NF4BLK, NF4BAD,
1426 NF4REG, NF4BAD, NF4LNK, NF4BAD,
1427 NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
1428};
1429
Al Virob37ad282006-10-19 23:28:59 -07001430static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001432 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 int status;
1435
1436 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1437 return nfserr_resource;
1438 if (whotype != NFS4_ACL_WHO_NAMED)
1439 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1440 else if (group)
1441 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1442 else
1443 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1444 if (status < 0)
1445 return nfserrno(status);
1446 *p = xdr_encode_opaque(*p, NULL, status);
1447 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1448 BUG_ON(*buflen < 0);
1449 return 0;
1450}
1451
Al Virob37ad282006-10-19 23:28:59 -07001452static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001453nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1456}
1457
Al Virob37ad282006-10-19 23:28:59 -07001458static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001459nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1462}
1463
Al Virob37ad282006-10-19 23:28:59 -07001464static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001466 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1469}
1470
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001471#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1472 FATTR4_WORD0_RDATTR_ERROR)
1473#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1474
Al Virob37ad282006-10-19 23:28:59 -07001475static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001476{
1477 /* As per referral draft: */
1478 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1479 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1480 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1481 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1482 *rdattr_err = NFSERR_MOVED;
1483 else
1484 return nfserr_moved;
1485 }
1486 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1487 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1488 return 0;
1489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491/*
1492 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1493 * ourselves.
1494 *
1495 * @countp is the buffer size in _words_; upon successful return this becomes
1496 * replaced with the number of words written.
1497 */
Al Virob37ad282006-10-19 23:28:59 -07001498__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001500 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001501 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 u32 bmval0 = bmval[0];
1504 u32 bmval1 = bmval[1];
1505 struct kstat stat;
1506 struct svc_fh tempfh;
1507 struct kstatfs statfs;
1508 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07001509 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 u32 dummy;
1511 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001512 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07001513 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07001514 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07001515 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int aclsupport = 0;
1517 struct nfs4_acl *acl = NULL;
1518
1519 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
1520 BUG_ON(bmval0 & ~NFSD_SUPPORTED_ATTRS_WORD0);
1521 BUG_ON(bmval1 & ~NFSD_SUPPORTED_ATTRS_WORD1);
1522
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001523 if (exp->ex_fslocs.migrated) {
1524 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
1525 if (status)
1526 goto out;
1527 }
1528
Jan Blunck54775492008-02-14 19:38:39 -08001529 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07001530 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04001532 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
1533 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
1535 FATTR4_WORD1_SPACE_TOTAL))) {
Al Virob8dd7b92006-10-19 23:29:01 -07001536 err = vfs_statfs(dentry, &statfs);
1537 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 goto out_nfserr;
1539 }
1540 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
1541 fh_init(&tempfh, NFS4_FHSIZE);
1542 status = fh_compose(&tempfh, exp, dentry, NULL);
1543 if (status)
1544 goto out;
1545 fhp = &tempfh;
1546 }
1547 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
1548 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07001549 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
1550 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07001552 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07001554 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 status = nfserr_attrnotsupp;
1556 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07001557 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 goto out_nfserr;
1559 }
1560 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001561 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
1562 if (exp->ex_fslocs.locations == NULL) {
1563 bmval0 &= ~FATTR4_WORD0_FS_LOCATIONS;
1564 }
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if ((buflen -= 16) < 0)
1567 goto out_resource;
1568
1569 WRITE32(2);
1570 WRITE32(bmval0);
1571 WRITE32(bmval1);
1572 attrlenp = p++; /* to be backfilled later */
1573
1574 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001575 u32 word0 = NFSD_SUPPORTED_ATTRS_WORD0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if ((buflen -= 12) < 0)
1577 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001578 if (!aclsupport)
1579 word0 &= ~FATTR4_WORD0_ACL;
1580 if (!exp->ex_fslocs.locations)
1581 word0 &= ~FATTR4_WORD0_FS_LOCATIONS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 WRITE32(2);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001583 WRITE32(word0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 WRITE32(NFSD_SUPPORTED_ATTRS_WORD1);
1585 }
1586 if (bmval0 & FATTR4_WORD0_TYPE) {
1587 if ((buflen -= 4) < 0)
1588 goto out_resource;
1589 dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
1590 if (dummy == NF4BAD)
1591 goto out_serverfault;
1592 WRITE32(dummy);
1593 }
1594 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
1595 if ((buflen -= 4) < 0)
1596 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07001597 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07001598 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07001599 else
NeilBrowne34ac862005-07-07 17:59:30 -07001600 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 if (bmval0 & FATTR4_WORD0_CHANGE) {
1603 /*
1604 * Note: This _must_ be consistent with the scheme for writing
1605 * change_info, so any changes made here must be reflected there
1606 * as well. (See xdr4.h:set_change_info() and the WRITECINFO()
1607 * macro above.)
1608 */
1609 if ((buflen -= 8) < 0)
1610 goto out_resource;
1611 WRITE32(stat.ctime.tv_sec);
1612 WRITE32(stat.ctime.tv_nsec);
1613 }
1614 if (bmval0 & FATTR4_WORD0_SIZE) {
1615 if ((buflen -= 8) < 0)
1616 goto out_resource;
1617 WRITE64(stat.size);
1618 }
1619 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
1620 if ((buflen -= 4) < 0)
1621 goto out_resource;
1622 WRITE32(1);
1623 }
1624 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
1625 if ((buflen -= 4) < 0)
1626 goto out_resource;
1627 WRITE32(1);
1628 }
1629 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
1630 if ((buflen -= 4) < 0)
1631 goto out_resource;
1632 WRITE32(0);
1633 }
1634 if (bmval0 & FATTR4_WORD0_FSID) {
1635 if ((buflen -= 16) < 0)
1636 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001637 if (exp->ex_fslocs.migrated) {
1638 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
1639 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08001640 } else switch(fsid_source(fhp)) {
1641 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 WRITE64((u64)exp->ex_fsid);
1643 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08001644 break;
1645 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 WRITE32(0);
1647 WRITE32(MAJOR(stat.dev));
1648 WRITE32(0);
1649 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08001650 break;
1651 case FSIDSOURCE_UUID:
1652 WRITEMEM(exp->ex_uuid, 16);
1653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655 }
1656 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
1657 if ((buflen -= 4) < 0)
1658 goto out_resource;
1659 WRITE32(0);
1660 }
1661 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
1662 if ((buflen -= 4) < 0)
1663 goto out_resource;
1664 WRITE32(NFSD_LEASE_TIME);
1665 }
1666 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
1667 if ((buflen -= 4) < 0)
1668 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001669 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 if (bmval0 & FATTR4_WORD0_ACL) {
1672 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 if (acl == NULL) {
1675 if ((buflen -= 4) < 0)
1676 goto out_resource;
1677
1678 WRITE32(0);
1679 goto out_acl;
1680 }
1681 if ((buflen -= 4) < 0)
1682 goto out_resource;
1683 WRITE32(acl->naces);
1684
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08001685 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if ((buflen -= 4*3) < 0)
1687 goto out_resource;
1688 WRITE32(ace->type);
1689 WRITE32(ace->flag);
1690 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
1691 status = nfsd4_encode_aclname(rqstp, ace->whotype,
1692 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
1693 &p, &buflen);
1694 if (status == nfserr_resource)
1695 goto out_resource;
1696 if (status)
1697 goto out;
1698 }
1699 }
1700out_acl:
1701 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
1702 if ((buflen -= 4) < 0)
1703 goto out_resource;
1704 WRITE32(aclsupport ?
1705 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
1706 }
1707 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
1708 if ((buflen -= 4) < 0)
1709 goto out_resource;
1710 WRITE32(1);
1711 }
1712 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
1713 if ((buflen -= 4) < 0)
1714 goto out_resource;
1715 WRITE32(1);
1716 }
1717 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
1718 if ((buflen -= 4) < 0)
1719 goto out_resource;
1720 WRITE32(1);
1721 }
1722 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
1723 if ((buflen -= 4) < 0)
1724 goto out_resource;
1725 WRITE32(1);
1726 }
1727 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
1728 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
1729 if (buflen < 0)
1730 goto out_resource;
1731 WRITE32(fhp->fh_handle.fh_size);
1732 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
1733 }
1734 if (bmval0 & FATTR4_WORD0_FILEID) {
1735 if ((buflen -= 8) < 0)
1736 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001737 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
1740 if ((buflen -= 8) < 0)
1741 goto out_resource;
1742 WRITE64((u64) statfs.f_ffree);
1743 }
1744 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
1745 if ((buflen -= 8) < 0)
1746 goto out_resource;
1747 WRITE64((u64) statfs.f_ffree);
1748 }
1749 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
1750 if ((buflen -= 8) < 0)
1751 goto out_resource;
1752 WRITE64((u64) statfs.f_files);
1753 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001754 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
1755 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
1756 if (status == nfserr_resource)
1757 goto out_resource;
1758 if (status)
1759 goto out;
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
1762 if ((buflen -= 4) < 0)
1763 goto out_resource;
1764 WRITE32(1);
1765 }
1766 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
1767 if ((buflen -= 8) < 0)
1768 goto out_resource;
1769 WRITE64(~(u64)0);
1770 }
1771 if (bmval0 & FATTR4_WORD0_MAXLINK) {
1772 if ((buflen -= 4) < 0)
1773 goto out_resource;
1774 WRITE32(255);
1775 }
1776 if (bmval0 & FATTR4_WORD0_MAXNAME) {
1777 if ((buflen -= 4) < 0)
1778 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04001779 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 if (bmval0 & FATTR4_WORD0_MAXREAD) {
1782 if ((buflen -= 8) < 0)
1783 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07001784 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
1787 if ((buflen -= 8) < 0)
1788 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07001789 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791 if (bmval1 & FATTR4_WORD1_MODE) {
1792 if ((buflen -= 4) < 0)
1793 goto out_resource;
1794 WRITE32(stat.mode & S_IALLUGO);
1795 }
1796 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
1797 if ((buflen -= 4) < 0)
1798 goto out_resource;
1799 WRITE32(1);
1800 }
1801 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
1802 if ((buflen -= 4) < 0)
1803 goto out_resource;
1804 WRITE32(stat.nlink);
1805 }
1806 if (bmval1 & FATTR4_WORD1_OWNER) {
1807 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
1808 if (status == nfserr_resource)
1809 goto out_resource;
1810 if (status)
1811 goto out;
1812 }
1813 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
1814 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
1815 if (status == nfserr_resource)
1816 goto out_resource;
1817 if (status)
1818 goto out;
1819 }
1820 if (bmval1 & FATTR4_WORD1_RAWDEV) {
1821 if ((buflen -= 8) < 0)
1822 goto out_resource;
1823 WRITE32((u32) MAJOR(stat.rdev));
1824 WRITE32((u32) MINOR(stat.rdev));
1825 }
1826 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
1827 if ((buflen -= 8) < 0)
1828 goto out_resource;
1829 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
1830 WRITE64(dummy64);
1831 }
1832 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
1833 if ((buflen -= 8) < 0)
1834 goto out_resource;
1835 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
1836 WRITE64(dummy64);
1837 }
1838 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
1839 if ((buflen -= 8) < 0)
1840 goto out_resource;
1841 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
1842 WRITE64(dummy64);
1843 }
1844 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
1845 if ((buflen -= 8) < 0)
1846 goto out_resource;
1847 dummy64 = (u64)stat.blocks << 9;
1848 WRITE64(dummy64);
1849 }
1850 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
1851 if ((buflen -= 12) < 0)
1852 goto out_resource;
1853 WRITE32(0);
1854 WRITE32(stat.atime.tv_sec);
1855 WRITE32(stat.atime.tv_nsec);
1856 }
1857 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
1858 if ((buflen -= 12) < 0)
1859 goto out_resource;
1860 WRITE32(0);
1861 WRITE32(1);
1862 WRITE32(0);
1863 }
1864 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
1865 if ((buflen -= 12) < 0)
1866 goto out_resource;
1867 WRITE32(0);
1868 WRITE32(stat.ctime.tv_sec);
1869 WRITE32(stat.ctime.tv_nsec);
1870 }
1871 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
1872 if ((buflen -= 12) < 0)
1873 goto out_resource;
1874 WRITE32(0);
1875 WRITE32(stat.mtime.tv_sec);
1876 WRITE32(stat.mtime.tv_nsec);
1877 }
1878 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if ((buflen -= 8) < 0)
1880 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08001881 /*
1882 * Get parent's attributes if not ignoring crossmount
1883 * and this is the root of a cross-mounted filesystem.
1884 */
1885 if (ignore_crossmnt == 0 &&
Jan Blunck54775492008-02-14 19:38:39 -08001886 exp->ex_path.mnt->mnt_root->d_inode == dentry->d_inode) {
1887 err = vfs_getattr(exp->ex_path.mnt->mnt_parent,
1888 exp->ex_path.mnt->mnt_mountpoint, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001889 if (err)
1890 goto out_nfserr;
1891 }
1892 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
1895 *countp = p - buffer;
1896 status = nfs_ok;
1897
1898out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08001899 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (fhp == &tempfh)
1901 fh_put(&tempfh);
1902 return status;
1903out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07001904 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto out;
1906out_resource:
1907 *countp = 0;
1908 status = nfserr_resource;
1909 goto out;
1910out_serverfault:
1911 status = nfserr_serverfault;
1912 goto out;
1913}
1914
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05001915static inline int attributes_need_mount(u32 *bmval)
1916{
1917 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
1918 return 1;
1919 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
1920 return 1;
1921 return 0;
1922}
1923
Al Virob37ad282006-10-19 23:28:59 -07001924static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07001926 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 struct svc_export *exp = cd->rd_fhp->fh_export;
1929 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07001930 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08001931 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
1934 if (IS_ERR(dentry))
1935 return nfserrno(PTR_ERR(dentry));
1936
1937 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08001938 /*
1939 * In the case of a mountpoint, the client may be asking for
1940 * attributes that are only properties of the underlying filesystem
1941 * as opposed to the cross-mounted file system. In such a case,
1942 * we will not follow the cross mount and will fill the attribtutes
1943 * directly from the mountpoint dentry.
1944 */
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05001945 if (d_mountpoint(dentry) && !attributes_need_mount(cd->rd_bmval))
Frank Filz406a7ea2007-11-27 11:34:05 -08001946 ignore_crossmnt = 1;
1947 else if (d_mountpoint(dentry)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08001948 int err;
1949
Andy Adamsondcb488a2007-07-17 04:04:51 -07001950 /*
1951 * Why the heck aren't we just using nfsd_lookup??
1952 * Different "."/".." handling? Something else?
1953 * At least, add a comment here to explain....
1954 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08001955 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
1956 if (err) {
1957 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto out_put;
1959 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07001960 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
1961 if (nfserr)
1962 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 }
1965 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001966 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967out_put:
1968 dput(dentry);
1969 exp_put(exp);
1970 return nfserr;
1971}
1972
Al Viro2ebbc012006-10-19 23:28:58 -07001973static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07001974nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Al Viro2ebbc012006-10-19 23:28:58 -07001976 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 if (buflen < 6)
1979 return NULL;
1980 *p++ = htonl(2);
1981 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
1982 *p++ = htonl(0); /* bmval1 */
1983
1984 attrlenp = p++;
1985 *p++ = nfserr; /* no htonl */
1986 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
1987 return p;
1988}
1989
1990static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001991nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
1992 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
NeilBrowna0ad13e2007-01-26 00:57:10 -08001994 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
1996 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07001997 __be32 *p = cd->buffer;
Al Virob37ad282006-10-19 23:28:59 -07001998 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* In nfsv4, "." and ".." never make it onto the wire.. */
2001 if (name && isdotent(name, namlen)) {
2002 cd->common.err = nfs_ok;
2003 return 0;
2004 }
2005
2006 if (cd->offset)
2007 xdr_encode_hyper(cd->offset, (u64) offset);
2008
2009 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2010 if (buflen < 0)
2011 goto fail;
2012
2013 *p++ = xdr_one; /* mark entry present */
2014 cd->offset = p; /* remember pointer */
2015 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2016 p = xdr_encode_array(p, name, namlen); /* name length & name */
2017
2018 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2019 switch (nfserr) {
2020 case nfs_ok:
2021 p += buflen;
2022 break;
2023 case nfserr_resource:
2024 nfserr = nfserr_toosmall;
2025 goto fail;
2026 case nfserr_dropit:
2027 goto fail;
2028 default:
2029 /*
2030 * If the client requested the RDATTR_ERROR attribute,
2031 * we stuff the error code into this attribute
2032 * and continue. If this attribute was not requested,
2033 * then in accordance with the spec, we fail the
2034 * entire READDIR operation(!)
2035 */
2036 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2037 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002039 if (p == NULL) {
2040 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 cd->buflen -= (p - cd->buffer);
2045 cd->buffer = p;
2046 cd->common.err = nfs_ok;
2047 return 0;
2048fail:
2049 cd->common.err = nfserr;
2050 return -EINVAL;
2051}
2052
Benny Halevye2f282b2008-08-12 20:45:07 +03002053static void
2054nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2055{
2056 ENCODE_HEAD;
2057
2058 RESERVE_SPACE(sizeof(stateid_t));
2059 WRITE32(sid->si_generation);
2060 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2061 ADJUST_ARGS();
2062}
2063
Benny Halevy695e12f2008-07-04 14:38:33 +03002064static __be32
Al Virob37ad282006-10-19 23:28:59 -07002065nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 ENCODE_HEAD;
2068
2069 if (!nfserr) {
2070 RESERVE_SPACE(8);
2071 WRITE32(access->ac_supported);
2072 WRITE32(access->ac_resp_access);
2073 ADJUST_ARGS();
2074 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002075 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Benny Halevy695e12f2008-07-04 14:38:33 +03002078static __be32
Al Virob37ad282006-10-19 23:28:59 -07002079nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 ENCODE_SEQID_OP_HEAD;
2082
Benny Halevye2f282b2008-08-12 20:45:07 +03002083 if (!nfserr)
2084 nfsd4_encode_stateid(resp, &close->cl_stateid);
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002087 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090
Benny Halevy695e12f2008-07-04 14:38:33 +03002091static __be32
Al Virob37ad282006-10-19 23:28:59 -07002092nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 ENCODE_HEAD;
2095
2096 if (!nfserr) {
2097 RESERVE_SPACE(8);
2098 WRITEMEM(commit->co_verf.data, 8);
2099 ADJUST_ARGS();
2100 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002101 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Benny Halevy695e12f2008-07-04 14:38:33 +03002104static __be32
Al Virob37ad282006-10-19 23:28:59 -07002105nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 ENCODE_HEAD;
2108
2109 if (!nfserr) {
2110 RESERVE_SPACE(32);
2111 WRITECINFO(create->cr_cinfo);
2112 WRITE32(2);
2113 WRITE32(create->cr_bmval[0]);
2114 WRITE32(create->cr_bmval[1]);
2115 ADJUST_ARGS();
2116 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002117 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Al Virob37ad282006-10-19 23:28:59 -07002120static __be32
2121nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct svc_fh *fhp = getattr->ga_fhp;
2124 int buflen;
2125
2126 if (nfserr)
2127 return nfserr;
2128
2129 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2130 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2131 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002132 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (!nfserr)
2134 resp->p += buflen;
2135 return nfserr;
2136}
2137
Benny Halevy695e12f2008-07-04 14:38:33 +03002138static __be32
2139nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Benny Halevy695e12f2008-07-04 14:38:33 +03002141 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 unsigned int len;
2143 ENCODE_HEAD;
2144
2145 if (!nfserr) {
2146 len = fhp->fh_handle.fh_size;
2147 RESERVE_SPACE(len + 4);
2148 WRITE32(len);
2149 WRITEMEM(&fhp->fh_handle.fh_base, len);
2150 ADJUST_ARGS();
2151 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002152 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
2155/*
2156* Including all fields other than the name, a LOCK4denied structure requires
2157* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2158*/
2159static void
2160nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2161{
2162 ENCODE_HEAD;
2163
2164 RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
2165 WRITE64(ld->ld_start);
2166 WRITE64(ld->ld_length);
2167 WRITE32(ld->ld_type);
2168 if (ld->ld_sop) {
2169 WRITEMEM(&ld->ld_clientid, 8);
2170 WRITE32(ld->ld_sop->so_owner.len);
2171 WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
2172 kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
2173 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2174 WRITE64((u64)0); /* clientid */
2175 WRITE32(0); /* length of owner name */
2176 }
2177 ADJUST_ARGS();
2178}
2179
Benny Halevy695e12f2008-07-04 14:38:33 +03002180static __be32
Al Virob37ad282006-10-19 23:28:59 -07002181nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 ENCODE_SEQID_OP_HEAD;
2184
Benny Halevye2f282b2008-08-12 20:45:07 +03002185 if (!nfserr)
2186 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2187 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2189
J. Bruce Fields3a655882006-01-18 17:43:19 -08002190 ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002191 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Benny Halevy695e12f2008-07-04 14:38:33 +03002194static __be32
Al Virob37ad282006-10-19 23:28:59 -07002195nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 if (nfserr == nfserr_denied)
2198 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002199 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Benny Halevy695e12f2008-07-04 14:38:33 +03002202static __be32
Al Virob37ad282006-10-19 23:28:59 -07002203nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 ENCODE_SEQID_OP_HEAD;
2206
Benny Halevye2f282b2008-08-12 20:45:07 +03002207 if (!nfserr)
2208 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002211 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214
Benny Halevy695e12f2008-07-04 14:38:33 +03002215static __be32
Al Virob37ad282006-10-19 23:28:59 -07002216nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 ENCODE_HEAD;
2219
2220 if (!nfserr) {
2221 RESERVE_SPACE(20);
2222 WRITECINFO(link->li_cinfo);
2223 ADJUST_ARGS();
2224 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002225 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
2228
Benny Halevy695e12f2008-07-04 14:38:33 +03002229static __be32
Al Virob37ad282006-10-19 23:28:59 -07002230nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Benny Halevy1b6b2252008-08-12 20:45:28 +03002232 ENCODE_HEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 ENCODE_SEQID_OP_HEAD;
2234
2235 if (nfserr)
2236 goto out;
2237
Benny Halevye2f282b2008-08-12 20:45:07 +03002238 nfsd4_encode_stateid(resp, &open->op_stateid);
2239 RESERVE_SPACE(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 WRITECINFO(open->op_cinfo);
2241 WRITE32(open->op_rflags);
2242 WRITE32(2);
2243 WRITE32(open->op_bmval[0]);
2244 WRITE32(open->op_bmval[1]);
2245 WRITE32(open->op_delegate_type);
2246 ADJUST_ARGS();
2247
2248 switch (open->op_delegate_type) {
2249 case NFS4_OPEN_DELEGATE_NONE:
2250 break;
2251 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002252 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2253 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002254 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /*
2257 * TODO: ACE's in delegations
2258 */
2259 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2260 WRITE32(0);
2261 WRITE32(0);
2262 WRITE32(0); /* XXX: is NULL principal ok? */
2263 ADJUST_ARGS();
2264 break;
2265 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002266 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2267 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 WRITE32(0);
2269
2270 /*
2271 * TODO: space_limit's in delegations
2272 */
2273 WRITE32(NFS4_LIMIT_SIZE);
2274 WRITE32(~(u32)0);
2275 WRITE32(~(u32)0);
2276
2277 /*
2278 * TODO: ACE's in delegations
2279 */
2280 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2281 WRITE32(0);
2282 WRITE32(0);
2283 WRITE32(0); /* XXX: is NULL principal ok? */
2284 ADJUST_ARGS();
2285 break;
2286 default:
2287 BUG();
2288 }
2289 /* XXX save filehandle here */
2290out:
2291 ENCODE_SEQID_OP_TAIL(open->op_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002292 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Benny Halevy695e12f2008-07-04 14:38:33 +03002295static __be32
Al Virob37ad282006-10-19 23:28:59 -07002296nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002299
2300 if (!nfserr)
2301 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002304 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Benny Halevy695e12f2008-07-04 14:38:33 +03002307static __be32
Al Virob37ad282006-10-19 23:28:59 -07002308nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002311
2312 if (!nfserr)
2313 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 ENCODE_SEQID_OP_TAIL(od->od_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002316 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Al Virob37ad282006-10-19 23:28:59 -07002319static __be32
2320nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002321 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 u32 eof;
2324 int v, pn;
2325 unsigned long maxcount;
2326 long len;
2327 ENCODE_HEAD;
2328
2329 if (nfserr)
2330 return nfserr;
2331 if (resp->xbuf->page_len)
2332 return nfserr_resource;
2333
2334 RESERVE_SPACE(8); /* eof flag and byte count */
2335
Greg Banks7adae482006-10-04 02:15:47 -07002336 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (maxcount > read->rd_length)
2338 maxcount = read->rd_length;
2339
2340 len = maxcount;
2341 v = 0;
2342 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -07002343 pn = resp->rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -07002344 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002345 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002346 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002347 len < PAGE_SIZE ? len : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 v++;
2349 len -= PAGE_SIZE;
2350 }
2351 read->rd_vlen = v;
2352
2353 nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002354 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 &maxcount);
2356
2357 if (nfserr == nfserr_symlink)
2358 nfserr = nfserr_inval;
2359 if (nfserr)
2360 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002361 eof = (read->rd_offset + maxcount >=
2362 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 WRITE32(eof);
2365 WRITE32(maxcount);
2366 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002367 resp->xbuf->head[0].iov_len = (char*)p
2368 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 resp->xbuf->page_len = maxcount;
2370
NeilBrown6ed6dec2006-04-10 22:55:32 -07002371 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002372 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002375 RESERVE_SPACE(4);
2376 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 resp->xbuf->tail[0].iov_base += maxcount&3;
2378 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002379 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381 return 0;
2382}
2383
Al Virob37ad282006-10-19 23:28:59 -07002384static __be32
2385nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
2387 int maxcount;
2388 char *page;
2389 ENCODE_HEAD;
2390
2391 if (nfserr)
2392 return nfserr;
2393 if (resp->xbuf->page_len)
2394 return nfserr_resource;
2395
NeilBrown44524352006-10-04 02:15:46 -07002396 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 maxcount = PAGE_SIZE;
2399 RESERVE_SPACE(4);
2400
2401 /*
2402 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2403 * if they would overflow the buffer. Is this kosher in NFSv4? If
2404 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2405 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2406 */
2407 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2408 if (nfserr == nfserr_isdir)
2409 return nfserr_inval;
2410 if (nfserr)
2411 return nfserr;
2412
2413 WRITE32(maxcount);
2414 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002415 resp->xbuf->head[0].iov_len = (char*)p
2416 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07002418
2419 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002420 resp->xbuf->tail[0].iov_base = p;
2421 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002423 RESERVE_SPACE(4);
2424 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 resp->xbuf->tail[0].iov_base += maxcount&3;
2426 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002427 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429 return 0;
2430}
2431
Al Virob37ad282006-10-19 23:28:59 -07002432static __be32
2433nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 int maxcount;
2436 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07002437 __be32 *page, *savep, *tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 ENCODE_HEAD;
2439
2440 if (nfserr)
2441 return nfserr;
2442 if (resp->xbuf->page_len)
2443 return nfserr_resource;
2444
2445 RESERVE_SPACE(8); /* verifier */
2446 savep = p;
2447
2448 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2449 WRITE32(0);
2450 WRITE32(0);
2451 ADJUST_ARGS();
2452 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002453 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 maxcount = PAGE_SIZE;
2456 if (maxcount > readdir->rd_maxcount)
2457 maxcount = readdir->rd_maxcount;
2458
2459 /*
2460 * Convert from bytes to words, account for the two words already
2461 * written, make sure to leave two words at the end for the next
2462 * pointer and eof field.
2463 */
2464 maxcount = (maxcount >> 2) - 4;
2465 if (maxcount < 0) {
2466 nfserr = nfserr_toosmall;
2467 goto err_no_verf;
2468 }
2469
NeilBrown44524352006-10-04 02:15:46 -07002470 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 readdir->common.err = 0;
2472 readdir->buflen = maxcount;
2473 readdir->buffer = page;
2474 readdir->offset = NULL;
2475
2476 offset = readdir->rd_cookie;
2477 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
2478 &offset,
2479 &readdir->common, nfsd4_encode_dirent);
2480 if (nfserr == nfs_ok &&
2481 readdir->common.err == nfserr_toosmall &&
2482 readdir->buffer == page)
2483 nfserr = nfserr_toosmall;
2484 if (nfserr == nfserr_symlink)
2485 nfserr = nfserr_notdir;
2486 if (nfserr)
2487 goto err_no_verf;
2488
2489 if (readdir->offset)
2490 xdr_encode_hyper(readdir->offset, offset);
2491
2492 p = readdir->buffer;
2493 *p++ = 0; /* no more entries */
2494 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07002495 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
2496 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
NeilBrownbb6e8a92006-04-10 22:55:33 -07002498 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07002499 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 resp->xbuf->tail[0].iov_len = 0;
2501 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002502 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 return 0;
2505err_no_verf:
2506 p = savep;
2507 ADJUST_ARGS();
2508 return nfserr;
2509}
2510
Benny Halevy695e12f2008-07-04 14:38:33 +03002511static __be32
Al Virob37ad282006-10-19 23:28:59 -07002512nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 ENCODE_HEAD;
2515
2516 if (!nfserr) {
2517 RESERVE_SPACE(20);
2518 WRITECINFO(remove->rm_cinfo);
2519 ADJUST_ARGS();
2520 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002521 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Benny Halevy695e12f2008-07-04 14:38:33 +03002524static __be32
Al Virob37ad282006-10-19 23:28:59 -07002525nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 ENCODE_HEAD;
2528
2529 if (!nfserr) {
2530 RESERVE_SPACE(40);
2531 WRITECINFO(rename->rn_sinfo);
2532 WRITECINFO(rename->rn_tinfo);
2533 ADJUST_ARGS();
2534 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002535 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
Benny Halevy695e12f2008-07-04 14:38:33 +03002538static __be32
Al Viroca5c8cd2007-07-26 17:33:49 +01002539nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamsondcb488a2007-07-17 04:04:51 -07002540 struct nfsd4_secinfo *secinfo)
2541{
2542 int i = 0;
2543 struct svc_export *exp = secinfo->si_exp;
J. Bruce Fields4796f452007-07-17 04:04:51 -07002544 u32 nflavs;
2545 struct exp_flavor_info *flavs;
2546 struct exp_flavor_info def_flavs[2];
Andy Adamsondcb488a2007-07-17 04:04:51 -07002547 ENCODE_HEAD;
2548
2549 if (nfserr)
2550 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07002551 if (exp->ex_nflavors) {
2552 flavs = exp->ex_flavors;
2553 nflavs = exp->ex_nflavors;
2554 } else { /* Handling of some defaults in absence of real secinfo: */
2555 flavs = def_flavs;
2556 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
2557 nflavs = 2;
2558 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
2559 flavs[1].pseudoflavor = RPC_AUTH_NULL;
2560 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
2561 nflavs = 1;
2562 flavs[0].pseudoflavor
2563 = svcauth_gss_flavor(exp->ex_client);
2564 } else {
2565 nflavs = 1;
2566 flavs[0].pseudoflavor
2567 = exp->ex_client->flavour->flavour;
2568 }
2569 }
2570
Andy Adamsondcb488a2007-07-17 04:04:51 -07002571 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07002572 WRITE32(nflavs);
Andy Adamsondcb488a2007-07-17 04:04:51 -07002573 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07002574 for (i = 0; i < nflavs; i++) {
2575 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a2007-07-17 04:04:51 -07002576 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
2577
2578 if (gm) {
2579 RESERVE_SPACE(4);
2580 WRITE32(RPC_AUTH_GSS);
2581 ADJUST_ARGS();
2582 RESERVE_SPACE(4 + gm->gm_oid.len);
2583 WRITE32(gm->gm_oid.len);
2584 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
2585 ADJUST_ARGS();
2586 RESERVE_SPACE(4);
2587 WRITE32(0); /* qop */
2588 ADJUST_ARGS();
2589 RESERVE_SPACE(4);
2590 WRITE32(gss_pseudoflavor_to_service(gm, flav));
2591 ADJUST_ARGS();
2592 gss_mech_put(gm);
2593 } else {
2594 RESERVE_SPACE(4);
2595 WRITE32(flav);
2596 ADJUST_ARGS();
2597 }
2598 }
2599out:
2600 if (exp)
2601 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03002602 return nfserr;
Andy Adamsondcb488a2007-07-17 04:04:51 -07002603}
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605/*
2606 * The SETATTR encode routine is special -- it always encodes a bitmap,
2607 * regardless of the error status.
2608 */
Benny Halevy695e12f2008-07-04 14:38:33 +03002609static __be32
Al Virob37ad282006-10-19 23:28:59 -07002610nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 ENCODE_HEAD;
2613
2614 RESERVE_SPACE(12);
2615 if (nfserr) {
2616 WRITE32(2);
2617 WRITE32(0);
2618 WRITE32(0);
2619 }
2620 else {
2621 WRITE32(2);
2622 WRITE32(setattr->sa_bmval[0]);
2623 WRITE32(setattr->sa_bmval[1]);
2624 }
2625 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03002626 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Benny Halevy695e12f2008-07-04 14:38:33 +03002629static __be32
Al Virob37ad282006-10-19 23:28:59 -07002630nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 ENCODE_HEAD;
2633
2634 if (!nfserr) {
2635 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
2636 WRITEMEM(&scd->se_clientid, 8);
2637 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
2638 ADJUST_ARGS();
2639 }
2640 else if (nfserr == nfserr_clid_inuse) {
2641 RESERVE_SPACE(8);
2642 WRITE32(0);
2643 WRITE32(0);
2644 ADJUST_ARGS();
2645 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002646 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
Benny Halevy695e12f2008-07-04 14:38:33 +03002649static __be32
Al Virob37ad282006-10-19 23:28:59 -07002650nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 ENCODE_HEAD;
2653
2654 if (!nfserr) {
2655 RESERVE_SPACE(16);
2656 WRITE32(write->wr_bytes_written);
2657 WRITE32(write->wr_how_written);
2658 WRITEMEM(write->wr_verifier.data, 8);
2659 ADJUST_ARGS();
2660 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002661 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Benny Halevy695e12f2008-07-04 14:38:33 +03002664static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03002665nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
2666 struct nfsd4_exchange_id *exid)
2667{
2668 /* stub */
2669 return nfserr;
2670}
2671
2672static __be32
2673nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
2674 struct nfsd4_create_session *sess)
2675{
2676 /* stub */
2677 return nfserr;
2678}
2679
2680static __be32
2681nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
2682 struct nfsd4_destroy_session *destroy_session)
2683{
2684 /* stub */
2685 return nfserr;
2686}
2687
2688static __be32
2689nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
2690 struct nfsd4_sequence *seq)
2691{
2692 /* stub */
2693 return nfserr;
2694}
2695
2696static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03002697nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
2698{
2699 return nfserr;
2700}
2701
2702typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
2703
Andy Adamson2db134e2009-04-03 08:27:55 +03002704/*
2705 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
2706 * since we don't need to filter out obsolete ops as this is
2707 * done in the decoding phase.
2708 */
Benny Halevy695e12f2008-07-04 14:38:33 +03002709static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002710 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
2711 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
2712 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
2713 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
2714 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
2715 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
2716 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
2717 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
2718 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
2719 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
2720 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
2721 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
2722 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
2723 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
2724 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
2725 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02002726 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002727 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
2728 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
2729 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
2730 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
2731 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
2732 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
2733 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
2734 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
2735 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
2736 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
2737 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
2738 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
2739 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
2740 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
2741 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
2742 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
2743 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
2744 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
2745 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
2746 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03002747
2748 /* NFSv4.1 operations */
2749 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
2750 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
2751 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
2752 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
2753 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
2754 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
2755 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
2756 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
2757 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
2758 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
2759 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
2760 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
2761 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_noop,
2762 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
2763 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
2764 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
2765 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
2766 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
2767 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03002768};
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770void
2771nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
2772{
Al Viro2ebbc012006-10-19 23:28:58 -07002773 __be32 *statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 ENCODE_HEAD;
2775
2776 RESERVE_SPACE(8);
2777 WRITE32(op->opnum);
2778 statp = p++; /* to be backfilled at the end */
2779 ADJUST_ARGS();
2780
Benny Halevy695e12f2008-07-04 14:38:33 +03002781 if (op->opnum == OP_ILLEGAL)
2782 goto status;
2783 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
2784 !nfsd4_enc_ops[op->opnum]);
2785 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
2786status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 /*
2788 * Note: We write the status directly, instead of using WRITE32(),
2789 * since it is already in network byte order.
2790 */
2791 *statp = op->status;
2792}
2793
2794/*
2795 * Encode the reply stored in the stateowner reply cache
2796 *
2797 * XDR note: do not encode rp->rp_buflen: the buffer contains the
2798 * previously sent already encoded operation.
2799 *
2800 * called with nfs4_lock_state() held
2801 */
2802void
2803nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
2804{
2805 ENCODE_HEAD;
2806 struct nfs4_replay *rp = op->replay;
2807
2808 BUG_ON(!rp);
2809
2810 RESERVE_SPACE(8);
2811 WRITE32(op->opnum);
2812 *p++ = rp->rp_status; /* already xdr'ed */
2813 ADJUST_ARGS();
2814
2815 RESERVE_SPACE(rp->rp_buflen);
2816 WRITEMEM(rp->rp_buf, rp->rp_buflen);
2817 ADJUST_ARGS();
2818}
2819
2820/*
2821 * END OF "GENERIC" ENCODE ROUTINES.
2822 */
2823
2824int
Al Viro2ebbc012006-10-19 23:28:58 -07002825nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 return xdr_ressize_check(rqstp, p);
2828}
2829
2830void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
2831{
2832 if (args->ops != args->iops) {
2833 kfree(args->ops);
2834 args->ops = args->iops;
2835 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08002836 kfree(args->tmpp);
2837 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 while (args->to_free) {
2839 struct tmpbuf *tb = args->to_free;
2840 args->to_free = tb->next;
2841 tb->release(tb->buf);
2842 kfree(tb);
2843 }
2844}
2845
2846int
Al Viro2ebbc012006-10-19 23:28:58 -07002847nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Al Virob37ad282006-10-19 23:28:59 -07002849 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 args->p = p;
2852 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
2853 args->pagelist = rqstp->rq_arg.pages;
2854 args->pagelen = rqstp->rq_arg.page_len;
2855 args->tmpp = NULL;
2856 args->to_free = NULL;
2857 args->ops = args->iops;
2858 args->rqstp = rqstp;
2859
2860 status = nfsd4_decode_compound(args);
2861 if (status) {
2862 nfsd4_release_compoundargs(args);
2863 }
2864 return !status;
2865}
2866
2867int
Al Viro2ebbc012006-10-19 23:28:58 -07002868nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 /*
2871 * All that remains is to write the tag and operation count...
2872 */
2873 struct kvec *iov;
2874 p = resp->tagp;
2875 *p++ = htonl(resp->taglen);
2876 memcpy(p, resp->tag, resp->taglen);
2877 p += XDR_QUADLEN(resp->taglen);
2878 *p++ = htonl(resp->opcnt);
2879
2880 if (rqstp->rq_res.page_len)
2881 iov = &rqstp->rq_res.tail[0];
2882 else
2883 iov = &rqstp->rq_res.head[0];
2884 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
2885 BUG_ON(iov->iov_len > PAGE_SIZE);
2886 return 1;
2887}
2888
2889/*
2890 * Local variables:
2891 * c-basic-offset: 8
2892 * End:
2893 */