blob: 533d14fec99e9dbe8ffe7358e7456f2ab280cf7e [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>
Andy Adamson0733d212009-04-03 08:28:01 +030048#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/sunrpc/xdr.h>
50#include <linux/sunrpc/svc.h>
51#include <linux/sunrpc/clnt.h>
52#include <linux/nfsd/nfsd.h>
53#include <linux/nfsd/state.h>
54#include <linux/nfsd/xdr4.h>
55#include <linux/nfsd_idmap.h>
56#include <linux/nfs4.h>
57#include <linux/nfs4_acl.h>
Andy Adamsondcb488a32007-07-17 04:04:51 -070058#include <linux/sunrpc/gss_api.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070059#include <linux/sunrpc/svcauth_gss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define NFSDDBG_FACILITY NFSDDBG_XDR
62
J.Bruce Fields42ca0992006-10-04 02:16:20 -070063/*
64 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
65 * directory in order to indicate to the client that a filesystem boundary is present
66 * We use a fixed fsid for a referral
67 */
68#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
69#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
70
Al Virob37ad282006-10-19 23:28:59 -070071static __be32
72check_filename(char *str, int len, __be32 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 int i;
75
76 if (len == 0)
77 return nfserr_inval;
78 if (isdotent(str, len))
79 return err;
80 for (i = 0; i < len; i++)
81 if (str[i] == '/')
82 return err;
83 return 0;
84}
85
86/*
87 * START OF "GENERIC" DECODE ROUTINES.
88 * These may look a little ugly since they are imported from a "generic"
89 * set of XDR encode/decode routines which are intended to be shared by
90 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
91 *
92 * If the pain of reading these is too great, it should be a straightforward
93 * task to translate them into Linux-specific versions which are more
94 * consistent with the style used in NFSv2/v3...
95 */
96#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070097 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070098 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define DECODE_TAIL \
100 status = 0; \
101out: \
102 return status; \
103xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400104 dprintk("NFSD: xdr error (%s:%d)\n", \
105 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 status = nfserr_bad_xdr; \
107 goto out
108
109#define READ32(x) (x) = ntohl(*p++)
110#define READ64(x) do { \
111 (x) = (u64)ntohl(*p++) << 32; \
112 (x) |= ntohl(*p++); \
113} while (0)
114#define READTIME(x) do { \
115 p++; \
116 (x) = ntohl(*p++); \
117 p++; \
118} while (0)
119#define READMEM(x,nbytes) do { \
120 x = (char *)p; \
121 p += XDR_QUADLEN(nbytes); \
122} while (0)
123#define SAVEMEM(x,nbytes) do { \
124 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
125 savemem(argp, p, nbytes) : \
126 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400127 dprintk("NFSD: xdr error (%s:%d)\n", \
128 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto xdr_error; \
130 } \
131 p += XDR_QUADLEN(nbytes); \
132} while (0)
133#define COPYMEM(x,nbytes) do { \
134 memcpy((x), p, nbytes); \
135 p += XDR_QUADLEN(nbytes); \
136} while (0)
137
138/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
139#define READ_BUF(nbytes) do { \
140 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
141 p = argp->p; \
142 argp->p += XDR_QUADLEN(nbytes); \
143 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400144 dprintk("NFSD: xdr error (%s:%d)\n", \
145 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto xdr_error; \
147 } \
148} while (0)
149
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500150static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 /* We want more bytes than seem to be available.
153 * Maybe we need a new page, maybe we have just run out
154 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500155 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700156 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (avail + argp->pagelen < nbytes)
158 return NULL;
159 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
160 return NULL;
161 /* ok, we can do it with the current plus the next page */
162 if (nbytes <= sizeof(argp->tmp))
163 p = argp->tmp;
164 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800165 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
167 if (!p)
168 return NULL;
169
170 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500171 /*
172 * The following memcpy is safe because read_buf is always
173 * called with nbytes > avail, and the two cases above both
174 * guarantee p points to at least nbytes bytes.
175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 memcpy(p, argp->p, avail);
177 /* step to next page */
178 argp->p = page_address(argp->pagelist[0]);
179 argp->pagelist++;
180 if (argp->pagelen < PAGE_SIZE) {
181 argp->end = p + (argp->pagelen>>2);
182 argp->pagelen = 0;
183 } else {
184 argp->end = p + (PAGE_SIZE>>2);
185 argp->pagelen -= PAGE_SIZE;
186 }
187 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
188 argp->p += XDR_QUADLEN(nbytes - avail);
189 return p;
190}
191
Andy Adamson60adfc52009-04-03 08:28:50 +0300192static int zero_clientid(clientid_t *clid)
193{
194 return (clid->cl_boot == 0) && (clid->cl_id == 0);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static int
198defer_free(struct nfsd4_compoundargs *argp,
199 void (*release)(const void *), void *p)
200{
201 struct tmpbuf *tb;
202
203 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
204 if (!tb)
205 return -ENOMEM;
206 tb->buf = p;
207 tb->release = release;
208 tb->next = argp->to_free;
209 argp->to_free = tb;
210 return 0;
211}
212
Al Viro2ebbc012006-10-19 23:28:58 -0700213static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (p == argp->tmp) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800216 p = kmalloc(nbytes, GFP_KERNEL);
217 if (!p)
218 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 memcpy(p, argp->tmp, nbytes);
220 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200221 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 argp->tmpp = NULL;
223 }
224 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800225 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return NULL;
227 } else
228 return (char *)p;
229}
230
Al Virob37ad282006-10-19 23:28:59 -0700231static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
233{
234 u32 bmlen;
235 DECODE_HEAD;
236
237 bmval[0] = 0;
238 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300239 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 READ_BUF(4);
242 READ32(bmlen);
243 if (bmlen > 1000)
244 goto xdr_error;
245
246 READ_BUF(bmlen << 2);
247 if (bmlen > 0)
248 READ32(bmval[0]);
249 if (bmlen > 1)
250 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300251 if (bmlen > 2)
252 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 DECODE_TAIL;
255}
256
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300257static u32 nfsd_attrmask[] = {
258 NFSD_WRITEABLE_ATTRS_WORD0,
Andy Adamson7e705702009-04-03 08:29:11 +0300259 NFSD_WRITEABLE_ATTRS_WORD1,
260 NFSD_WRITEABLE_ATTRS_WORD2
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300261};
262
Al Virob37ad282006-10-19 23:28:59 -0700263static __be32
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300264nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, u32 *writable,
265 struct iattr *iattr, struct nfs4_acl **acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int expected_len, len = 0;
268 u32 dummy32;
269 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700270 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 DECODE_HEAD;
273 iattr->ia_valid = 0;
274 if ((status = nfsd4_decode_bitmap(argp, bmval)))
275 return status;
276
277 /*
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800278 * According to spec, unsupported attributes return ERR_ATTRNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * read-only attributes return ERR_INVAL.
280 */
Andy Adamson7e705702009-04-03 08:29:11 +0300281 if ((bmval[0] & ~nfsd_suppattrs0(argp->minorversion)) ||
282 (bmval[1] & ~nfsd_suppattrs1(argp->minorversion)) ||
283 (bmval[2] & ~nfsd_suppattrs2(argp->minorversion)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return nfserr_attrnotsupp;
Andy Adamson7e705702009-04-03 08:29:11 +0300285 if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
286 (bmval[2] & ~writable[2]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return nfserr_inval;
288
289 READ_BUF(4);
290 READ32(expected_len);
291
292 if (bmval[0] & FATTR4_WORD0_SIZE) {
293 READ_BUF(8);
294 len += 8;
295 READ64(iattr->ia_size);
296 iattr->ia_valid |= ATTR_SIZE;
297 }
298 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800299 int nace;
300 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 READ_BUF(4); len += 4;
303 READ32(nace);
304
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800305 if (nace > NFS4_ACL_MAX)
306 return nfserr_resource;
307
308 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700310 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out_nfserr;
312 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800313 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800315 (*acl)->naces = nace;
316 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800318 READ32(ace->type);
319 READ32(ace->flag);
320 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 READ32(dummy32);
322 READ_BUF(dummy32);
323 len += XDR_QUADLEN(dummy32) << 2;
324 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800325 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
Al Virob8dd7b92006-10-19 23:29:01 -0700326 host_err = 0;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800327 if (ace->whotype != NFS4_ACL_WHO_NAMED)
328 ace->who = 0;
329 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
Al Virob8dd7b92006-10-19 23:29:01 -0700330 host_err = nfsd_map_name_to_gid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800331 buf, dummy32, &ace->who);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 else
Al Virob8dd7b92006-10-19 23:29:01 -0700333 host_err = nfsd_map_name_to_uid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800334 buf, dummy32, &ace->who);
Al Virob8dd7b92006-10-19 23:29:01 -0700335 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 } else
339 *acl = NULL;
340 if (bmval[1] & FATTR4_WORD1_MODE) {
341 READ_BUF(4);
342 len += 4;
343 READ32(iattr->ia_mode);
344 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
345 iattr->ia_valid |= ATTR_MODE;
346 }
347 if (bmval[1] & FATTR4_WORD1_OWNER) {
348 READ_BUF(4);
349 len += 4;
350 READ32(dummy32);
351 READ_BUF(dummy32);
352 len += (XDR_QUADLEN(dummy32) << 2);
353 READMEM(buf, dummy32);
Al Virob8dd7b92006-10-19 23:29:01 -0700354 if ((host_err = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 goto out_nfserr;
356 iattr->ia_valid |= ATTR_UID;
357 }
358 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
359 READ_BUF(4);
360 len += 4;
361 READ32(dummy32);
362 READ_BUF(dummy32);
363 len += (XDR_QUADLEN(dummy32) << 2);
364 READMEM(buf, dummy32);
Al Virob8dd7b92006-10-19 23:29:01 -0700365 if ((host_err = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto out_nfserr;
367 iattr->ia_valid |= ATTR_GID;
368 }
369 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
370 READ_BUF(4);
371 len += 4;
372 READ32(dummy32);
373 switch (dummy32) {
374 case NFS4_SET_TO_CLIENT_TIME:
375 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
376 all 32 bits of 'nseconds'. */
377 READ_BUF(12);
378 len += 12;
379 READ32(dummy32);
380 if (dummy32)
381 return nfserr_inval;
382 READ32(iattr->ia_atime.tv_sec);
383 READ32(iattr->ia_atime.tv_nsec);
384 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
385 return nfserr_inval;
386 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
387 break;
388 case NFS4_SET_TO_SERVER_TIME:
389 iattr->ia_valid |= ATTR_ATIME;
390 break;
391 default:
392 goto xdr_error;
393 }
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
396 READ_BUF(4);
397 len += 4;
398 READ32(dummy32);
399 switch (dummy32) {
400 case NFS4_SET_TO_CLIENT_TIME:
401 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
402 all 32 bits of 'nseconds'. */
403 READ_BUF(12);
404 len += 12;
405 READ32(dummy32);
406 if (dummy32)
407 return nfserr_inval;
408 READ32(iattr->ia_mtime.tv_sec);
409 READ32(iattr->ia_mtime.tv_nsec);
410 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
411 return nfserr_inval;
412 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
413 break;
414 case NFS4_SET_TO_SERVER_TIME:
415 iattr->ia_valid |= ATTR_MTIME;
416 break;
417 default:
418 goto xdr_error;
419 }
420 }
Andy Adamson7e705702009-04-03 08:29:11 +0300421 BUG_ON(bmval[2]); /* no such writeable attr supported yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (len != expected_len)
423 goto xdr_error;
424
425 DECODE_TAIL;
426
427out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700428 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto out;
430}
431
Al Virob37ad282006-10-19 23:28:59 -0700432static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300433nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
434{
435 DECODE_HEAD;
436
437 READ_BUF(sizeof(stateid_t));
438 READ32(sid->si_generation);
439 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
440
441 DECODE_TAIL;
442}
443
444static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
446{
447 DECODE_HEAD;
448
449 READ_BUF(4);
450 READ32(access->ac_req_access);
451
452 DECODE_TAIL;
453}
454
Al Virob37ad282006-10-19 23:28:59 -0700455static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
457{
458 DECODE_HEAD;
459
460 close->cl_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300461 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300463 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 DECODE_TAIL;
466}
467
468
Al Virob37ad282006-10-19 23:28:59 -0700469static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
471{
472 DECODE_HEAD;
473
474 READ_BUF(12);
475 READ64(commit->co_offset);
476 READ32(commit->co_count);
477
478 DECODE_TAIL;
479}
480
Al Virob37ad282006-10-19 23:28:59 -0700481static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
483{
484 DECODE_HEAD;
485
486 READ_BUF(4);
487 READ32(create->cr_type);
488 switch (create->cr_type) {
489 case NF4LNK:
490 READ_BUF(4);
491 READ32(create->cr_linklen);
492 READ_BUF(create->cr_linklen);
493 SAVEMEM(create->cr_linkname, create->cr_linklen);
494 break;
495 case NF4BLK:
496 case NF4CHR:
497 READ_BUF(8);
498 READ32(create->cr_specdata1);
499 READ32(create->cr_specdata2);
500 break;
501 case NF4SOCK:
502 case NF4FIFO:
503 case NF4DIR:
504 default:
505 break;
506 }
507
508 READ_BUF(4);
509 READ32(create->cr_namelen);
510 READ_BUF(create->cr_namelen);
511 SAVEMEM(create->cr_name, create->cr_namelen);
512 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
513 return status;
514
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300515 status = nfsd4_decode_fattr(argp, create->cr_bmval, nfsd_attrmask,
516 &create->cr_iattr, &create->cr_acl);
517 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto out;
519
520 DECODE_TAIL;
521}
522
Al Virob37ad282006-10-19 23:28:59 -0700523static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
525{
Benny Halevye31a1b62008-08-12 20:46:18 +0300526 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Al Virob37ad282006-10-19 23:28:59 -0700529static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
531{
532 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
533}
534
Al Virob37ad282006-10-19 23:28:59 -0700535static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
537{
538 DECODE_HEAD;
539
540 READ_BUF(4);
541 READ32(link->li_namelen);
542 READ_BUF(link->li_namelen);
543 SAVEMEM(link->li_name, link->li_namelen);
544 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
545 return status;
546
547 DECODE_TAIL;
548}
549
Al Virob37ad282006-10-19 23:28:59 -0700550static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
552{
553 DECODE_HEAD;
554
J. Bruce Fields3a655882006-01-18 17:43:19 -0800555 lock->lk_replay_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
557 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
558 */
559 READ_BUF(28);
560 READ32(lock->lk_type);
561 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
562 goto xdr_error;
563 READ32(lock->lk_reclaim);
564 READ64(lock->lk_offset);
565 READ64(lock->lk_length);
566 READ32(lock->lk_is_new);
567
568 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300569 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300571 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
572 if (status)
573 return status;
574 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 READ32(lock->lk_new_lock_seqid);
576 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
577 READ32(lock->lk_new_owner.len);
578 READ_BUF(lock->lk_new_owner.len);
579 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
580 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300581 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
582 if (status)
583 return status;
584 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 READ32(lock->lk_old_lock_seqid);
586 }
587
588 DECODE_TAIL;
589}
590
Al Virob37ad282006-10-19 23:28:59 -0700591static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
593{
594 DECODE_HEAD;
595
596 READ_BUF(32);
597 READ32(lockt->lt_type);
598 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
599 goto xdr_error;
600 READ64(lockt->lt_offset);
601 READ64(lockt->lt_length);
602 COPYMEM(&lockt->lt_clientid, 8);
603 READ32(lockt->lt_owner.len);
604 READ_BUF(lockt->lt_owner.len);
605 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
606
Andy Adamson60adfc52009-04-03 08:28:50 +0300607 if (argp->minorversion && !zero_clientid(&lockt->lt_clientid))
608 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 DECODE_TAIL;
610}
611
Al Virob37ad282006-10-19 23:28:59 -0700612static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
614{
615 DECODE_HEAD;
616
617 locku->lu_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300618 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 READ32(locku->lu_type);
620 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
621 goto xdr_error;
622 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300623 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
624 if (status)
625 return status;
626 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 READ64(locku->lu_offset);
628 READ64(locku->lu_length);
629
630 DECODE_TAIL;
631}
632
Al Virob37ad282006-10-19 23:28:59 -0700633static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
635{
636 DECODE_HEAD;
637
638 READ_BUF(4);
639 READ32(lookup->lo_len);
640 READ_BUF(lookup->lo_len);
641 SAVEMEM(lookup->lo_name, lookup->lo_len);
642 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
643 return status;
644
645 DECODE_TAIL;
646}
647
Al Virob37ad282006-10-19 23:28:59 -0700648static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
650{
651 DECODE_HEAD;
652
653 memset(open->op_bmval, 0, sizeof(open->op_bmval));
654 open->op_iattr.ia_valid = 0;
655 open->op_stateowner = NULL;
656
657 /* seqid, share_access, share_deny, clientid, ownerlen */
658 READ_BUF(16 + sizeof(clientid_t));
659 READ32(open->op_seqid);
660 READ32(open->op_share_access);
661 READ32(open->op_share_deny);
662 COPYMEM(&open->op_clientid, sizeof(clientid_t));
663 READ32(open->op_owner.len);
664
665 /* owner, open_flag */
666 READ_BUF(open->op_owner.len + 4);
667 SAVEMEM(open->op_owner.data, open->op_owner.len);
668 READ32(open->op_create);
669 switch (open->op_create) {
670 case NFS4_OPEN_NOCREATE:
671 break;
672 case NFS4_OPEN_CREATE:
673 READ_BUF(4);
674 READ32(open->op_createmode);
675 switch (open->op_createmode) {
676 case NFS4_CREATE_UNCHECKED:
677 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300678 status = nfsd4_decode_fattr(argp, open->op_bmval,
679 nfsd_attrmask, &open->op_iattr, &open->op_acl);
680 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto out;
682 break;
683 case NFS4_CREATE_EXCLUSIVE:
684 READ_BUF(8);
685 COPYMEM(open->op_verf.data, 8);
686 break;
687 default:
688 goto xdr_error;
689 }
690 break;
691 default:
692 goto xdr_error;
693 }
694
695 /* open_claim */
696 READ_BUF(4);
697 READ32(open->op_claim_type);
698 switch (open->op_claim_type) {
699 case NFS4_OPEN_CLAIM_NULL:
700 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
701 READ_BUF(4);
702 READ32(open->op_fname.len);
703 READ_BUF(open->op_fname.len);
704 SAVEMEM(open->op_fname.data, open->op_fname.len);
705 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
706 return status;
707 break;
708 case NFS4_OPEN_CLAIM_PREVIOUS:
709 READ_BUF(4);
710 READ32(open->op_delegate_type);
711 break;
712 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300713 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
714 if (status)
715 return status;
716 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 READ32(open->op_fname.len);
718 READ_BUF(open->op_fname.len);
719 SAVEMEM(open->op_fname.data, open->op_fname.len);
720 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
721 return status;
722 break;
723 default:
724 goto xdr_error;
725 }
726
727 DECODE_TAIL;
728}
729
Al Virob37ad282006-10-19 23:28:59 -0700730static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
732{
733 DECODE_HEAD;
734
735 open_conf->oc_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300736 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
737 if (status)
738 return status;
739 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 READ32(open_conf->oc_seqid);
741
742 DECODE_TAIL;
743}
744
Al Virob37ad282006-10-19 23:28:59 -0700745static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
747{
748 DECODE_HEAD;
749
750 open_down->od_stateowner = NULL;
Benny Halevye31a1b62008-08-12 20:46:18 +0300751 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
752 if (status)
753 return status;
754 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 READ32(open_down->od_seqid);
756 READ32(open_down->od_share_access);
757 READ32(open_down->od_share_deny);
758
759 DECODE_TAIL;
760}
761
Al Virob37ad282006-10-19 23:28:59 -0700762static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
764{
765 DECODE_HEAD;
766
767 READ_BUF(4);
768 READ32(putfh->pf_fhlen);
769 if (putfh->pf_fhlen > NFS4_FHSIZE)
770 goto xdr_error;
771 READ_BUF(putfh->pf_fhlen);
772 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
773
774 DECODE_TAIL;
775}
776
Al Virob37ad282006-10-19 23:28:59 -0700777static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
779{
780 DECODE_HEAD;
781
Benny Halevye31a1b62008-08-12 20:46:18 +0300782 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
783 if (status)
784 return status;
785 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 READ64(read->rd_offset);
787 READ32(read->rd_length);
788
789 DECODE_TAIL;
790}
791
Al Virob37ad282006-10-19 23:28:59 -0700792static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
794{
795 DECODE_HEAD;
796
797 READ_BUF(24);
798 READ64(readdir->rd_cookie);
799 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
800 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
801 READ32(readdir->rd_maxcount);
802 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
803 goto out;
804
805 DECODE_TAIL;
806}
807
Al Virob37ad282006-10-19 23:28:59 -0700808static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
810{
811 DECODE_HEAD;
812
813 READ_BUF(4);
814 READ32(remove->rm_namelen);
815 READ_BUF(remove->rm_namelen);
816 SAVEMEM(remove->rm_name, remove->rm_namelen);
817 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
818 return status;
819
820 DECODE_TAIL;
821}
822
Al Virob37ad282006-10-19 23:28:59 -0700823static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
825{
826 DECODE_HEAD;
827
828 READ_BUF(4);
829 READ32(rename->rn_snamelen);
830 READ_BUF(rename->rn_snamelen + 4);
831 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
832 READ32(rename->rn_tnamelen);
833 READ_BUF(rename->rn_tnamelen);
834 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
835 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
836 return status;
837 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
838 return status;
839
840 DECODE_TAIL;
841}
842
Al Virob37ad282006-10-19 23:28:59 -0700843static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
845{
846 DECODE_HEAD;
847
848 READ_BUF(sizeof(clientid_t));
849 COPYMEM(clientid, sizeof(clientid_t));
850
851 DECODE_TAIL;
852}
853
Al Virob37ad282006-10-19 23:28:59 -0700854static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -0700855nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
856 struct nfsd4_secinfo *secinfo)
857{
858 DECODE_HEAD;
859
860 READ_BUF(4);
861 READ32(secinfo->si_namelen);
862 READ_BUF(secinfo->si_namelen);
863 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
864 status = check_filename(secinfo->si_name, secinfo->si_namelen,
865 nfserr_noent);
866 if (status)
867 return status;
868 DECODE_TAIL;
869}
870
871static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
873{
Benny Halevye31a1b62008-08-12 20:46:18 +0300874 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Benny Halevye31a1b62008-08-12 20:46:18 +0300876 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
877 if (status)
878 return status;
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300879 return nfsd4_decode_fattr(argp, setattr->sa_bmval, nfsd_attrmask,
Benny Halevye31a1b62008-08-12 20:46:18 +0300880 &setattr->sa_iattr, &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Al Virob37ad282006-10-19 23:28:59 -0700883static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
885{
886 DECODE_HEAD;
887
888 READ_BUF(12);
889 COPYMEM(setclientid->se_verf.data, 8);
890 READ32(setclientid->se_namelen);
891
892 READ_BUF(setclientid->se_namelen + 8);
893 SAVEMEM(setclientid->se_name, setclientid->se_namelen);
894 READ32(setclientid->se_callback_prog);
895 READ32(setclientid->se_callback_netid_len);
896
897 READ_BUF(setclientid->se_callback_netid_len + 4);
898 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
899 READ32(setclientid->se_callback_addr_len);
900
901 READ_BUF(setclientid->se_callback_addr_len + 4);
902 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
903 READ32(setclientid->se_callback_ident);
904
905 DECODE_TAIL;
906}
907
Al Virob37ad282006-10-19 23:28:59 -0700908static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
910{
911 DECODE_HEAD;
912
913 READ_BUF(8 + sizeof(nfs4_verifier));
914 COPYMEM(&scd_c->sc_clientid, 8);
915 COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
916
917 DECODE_TAIL;
918}
919
920/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -0700921static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
923{
924#if 0
925 struct nfsd4_compoundargs save = {
926 .p = argp->p,
927 .end = argp->end,
928 .rqstp = argp->rqstp,
929 };
930 u32 ve_bmval[2];
931 struct iattr ve_iattr; /* request */
932 struct nfs4_acl *ve_acl; /* request */
933#endif
934 DECODE_HEAD;
935
936 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
937 goto out;
938
939 /* For convenience's sake, we compare raw xdr'd attributes in
940 * nfsd4_proc_verify; however we still decode here just to return
941 * correct error in case of bad xdr. */
942#if 0
943 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
944 if (status == nfserr_inval) {
945 status = nfserrno(status);
946 goto out;
947 }
948#endif
949 READ_BUF(4);
950 READ32(verify->ve_attrlen);
951 READ_BUF(verify->ve_attrlen);
952 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
953
954 DECODE_TAIL;
955}
956
Al Virob37ad282006-10-19 23:28:59 -0700957static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
959{
960 int avail;
961 int v;
962 int len;
963 DECODE_HEAD;
964
Benny Halevye31a1b62008-08-12 20:46:18 +0300965 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
966 if (status)
967 return status;
968 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 READ64(write->wr_offset);
970 READ32(write->wr_stable_how);
971 if (write->wr_stable_how > 2)
972 goto xdr_error;
973 READ32(write->wr_buflen);
974
975 /* Sorry .. no magic macros for this.. *
976 * READ_BUF(write->wr_buflen);
977 * SAVEMEM(write->wr_buf, write->wr_buflen);
978 */
979 avail = (char*)argp->end - (char*)argp->p;
980 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -0400981 dprintk("NFSD: xdr error (%s:%d)\n",
982 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto xdr_error;
984 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700985 argp->rqstp->rq_vec[0].iov_base = p;
986 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 v = 0;
988 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -0700989 while (len > argp->rqstp->rq_vec[v].iov_len) {
990 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700992 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 argp->pagelist++;
994 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -0700995 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 argp->pagelen -= PAGE_SIZE;
997 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -0700998 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 argp->pagelen -= len;
1000 }
1001 }
Al Viro2ebbc012006-10-19 23:28:58 -07001002 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1003 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -07001004 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 write->wr_vlen = v+1;
1006
1007 DECODE_TAIL;
1008}
1009
Al Virob37ad282006-10-19 23:28:59 -07001010static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1012{
1013 DECODE_HEAD;
1014
1015 READ_BUF(12);
1016 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1017 READ32(rlockowner->rl_owner.len);
1018 READ_BUF(rlockowner->rl_owner.len);
1019 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1020
Andy Adamson60adfc52009-04-03 08:28:50 +03001021 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1022 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 DECODE_TAIL;
1024}
1025
Al Virob37ad282006-10-19 23:28:59 -07001026static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001027nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001028 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001029{
Andy Adamson0733d212009-04-03 08:28:01 +03001030 int dummy;
1031 DECODE_HEAD;
1032
1033 READ_BUF(NFS4_VERIFIER_SIZE);
1034 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1035
1036 READ_BUF(4);
1037 READ32(exid->clname.len);
1038
1039 READ_BUF(exid->clname.len);
1040 SAVEMEM(exid->clname.data, exid->clname.len);
1041
1042 READ_BUF(4);
1043 READ32(exid->flags);
1044
1045 /* Ignore state_protect4_a */
1046 READ_BUF(4);
1047 READ32(exid->spa_how);
1048 switch (exid->spa_how) {
1049 case SP4_NONE:
1050 break;
1051 case SP4_MACH_CRED:
1052 /* spo_must_enforce */
1053 READ_BUF(4);
1054 READ32(dummy);
1055 READ_BUF(dummy * 4);
1056 p += dummy;
1057
1058 /* spo_must_allow */
1059 READ_BUF(4);
1060 READ32(dummy);
1061 READ_BUF(dummy * 4);
1062 p += dummy;
1063 break;
1064 case SP4_SSV:
1065 /* ssp_ops */
1066 READ_BUF(4);
1067 READ32(dummy);
1068 READ_BUF(dummy * 4);
1069 p += dummy;
1070
1071 READ_BUF(4);
1072 READ32(dummy);
1073 READ_BUF(dummy * 4);
1074 p += dummy;
1075
1076 /* ssp_hash_algs<> */
1077 READ_BUF(4);
1078 READ32(dummy);
1079 READ_BUF(dummy);
1080 p += XDR_QUADLEN(dummy);
1081
1082 /* ssp_encr_algs<> */
1083 READ_BUF(4);
1084 READ32(dummy);
1085 READ_BUF(dummy);
1086 p += XDR_QUADLEN(dummy);
1087
1088 /* ssp_window and ssp_num_gss_handles */
1089 READ_BUF(8);
1090 READ32(dummy);
1091 READ32(dummy);
1092 break;
1093 default:
1094 goto xdr_error;
1095 }
1096
1097 /* Ignore Implementation ID */
1098 READ_BUF(4); /* nfs_impl_id4 array length */
1099 READ32(dummy);
1100
1101 if (dummy > 1)
1102 goto xdr_error;
1103
1104 if (dummy == 1) {
1105 /* nii_domain */
1106 READ_BUF(4);
1107 READ32(dummy);
1108 READ_BUF(dummy);
1109 p += XDR_QUADLEN(dummy);
1110
1111 /* nii_name */
1112 READ_BUF(4);
1113 READ32(dummy);
1114 READ_BUF(dummy);
1115 p += XDR_QUADLEN(dummy);
1116
1117 /* nii_date */
1118 READ_BUF(12);
1119 p += 3;
1120 }
1121 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001122}
1123
1124static __be32
1125nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1126 struct nfsd4_create_session *sess)
1127{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001128 DECODE_HEAD;
1129
1130 u32 dummy;
1131 char *machine_name;
1132 int i;
1133 int nr_secflavs;
1134
1135 READ_BUF(16);
1136 COPYMEM(&sess->clientid, 8);
1137 READ32(sess->seqid);
1138 READ32(sess->flags);
1139
1140 /* Fore channel attrs */
1141 READ_BUF(28);
1142 READ32(dummy); /* headerpadsz is always 0 */
1143 READ32(sess->fore_channel.maxreq_sz);
1144 READ32(sess->fore_channel.maxresp_sz);
1145 READ32(sess->fore_channel.maxresp_cached);
1146 READ32(sess->fore_channel.maxops);
1147 READ32(sess->fore_channel.maxreqs);
1148 READ32(sess->fore_channel.nr_rdma_attrs);
1149 if (sess->fore_channel.nr_rdma_attrs == 1) {
1150 READ_BUF(4);
1151 READ32(sess->fore_channel.rdma_attrs);
1152 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1153 dprintk("Too many fore channel attr bitmaps!\n");
1154 goto xdr_error;
1155 }
1156
1157 /* Back channel attrs */
1158 READ_BUF(28);
1159 READ32(dummy); /* headerpadsz is always 0 */
1160 READ32(sess->back_channel.maxreq_sz);
1161 READ32(sess->back_channel.maxresp_sz);
1162 READ32(sess->back_channel.maxresp_cached);
1163 READ32(sess->back_channel.maxops);
1164 READ32(sess->back_channel.maxreqs);
1165 READ32(sess->back_channel.nr_rdma_attrs);
1166 if (sess->back_channel.nr_rdma_attrs == 1) {
1167 READ_BUF(4);
1168 READ32(sess->back_channel.rdma_attrs);
1169 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1170 dprintk("Too many back channel attr bitmaps!\n");
1171 goto xdr_error;
1172 }
1173
1174 READ_BUF(8);
1175 READ32(sess->callback_prog);
1176
1177 /* callback_sec_params4 */
1178 READ32(nr_secflavs);
1179 for (i = 0; i < nr_secflavs; ++i) {
1180 READ_BUF(4);
1181 READ32(dummy);
1182 switch (dummy) {
1183 case RPC_AUTH_NULL:
1184 /* Nothing to read */
1185 break;
1186 case RPC_AUTH_UNIX:
1187 READ_BUF(8);
1188 /* stamp */
1189 READ32(dummy);
1190
1191 /* machine name */
1192 READ32(dummy);
1193 READ_BUF(dummy);
1194 SAVEMEM(machine_name, dummy);
1195
1196 /* uid, gid */
1197 READ_BUF(8);
1198 READ32(sess->uid);
1199 READ32(sess->gid);
1200
1201 /* more gids */
1202 READ_BUF(4);
1203 READ32(dummy);
1204 READ_BUF(dummy * 4);
1205 for (i = 0; i < dummy; ++i)
1206 READ32(dummy);
1207 break;
1208 case RPC_AUTH_GSS:
1209 dprintk("RPC_AUTH_GSS callback secflavor "
1210 "not supported!\n");
1211 READ_BUF(8);
1212 /* gcbp_service */
1213 READ32(dummy);
1214 /* gcbp_handle_from_server */
1215 READ32(dummy);
1216 READ_BUF(dummy);
1217 p += XDR_QUADLEN(dummy);
1218 /* gcbp_handle_from_client */
1219 READ_BUF(4);
1220 READ32(dummy);
1221 READ_BUF(dummy);
1222 p += XDR_QUADLEN(dummy);
1223 break;
1224 default:
1225 dprintk("Illegal callback secflavor\n");
1226 return nfserr_inval;
1227 }
1228 }
1229 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001230}
1231
1232static __be32
1233nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1234 struct nfsd4_destroy_session *destroy_session)
1235{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001236 DECODE_HEAD;
1237 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1238 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1239
1240 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001241}
1242
1243static __be32
1244nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1245 struct nfsd4_sequence *seq)
1246{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001247 DECODE_HEAD;
1248
1249 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1250 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1251 READ32(seq->seqid);
1252 READ32(seq->slotid);
1253 READ32(seq->maxslots);
1254 READ32(seq->cachethis);
1255
1256 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001257}
1258
1259static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001260nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1261{
1262 return nfs_ok;
1263}
1264
Benny Halevy3c375c62008-07-02 11:14:01 +03001265static __be32
1266nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1267{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001268 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001269}
1270
Benny Halevy347e0ad2008-07-02 11:13:41 +03001271typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1272
1273static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001274 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1275 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1276 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1277 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1278 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1279 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1280 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1281 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1282 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1283 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1284 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1285 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1286 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1287 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1288 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1289 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1290 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1291 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1292 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1293 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001294 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001295 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1296 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1297 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1298 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1299 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1300 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1301 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1302 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1303 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1304 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1305 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1306 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1307 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1308 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1309 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1310 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001311};
1312
Andy Adamson2db134e2009-04-03 08:27:55 +03001313static nfsd4_dec nfsd41_dec_ops[] = {
1314 [OP_ACCESS] (nfsd4_dec)nfsd4_decode_access,
1315 [OP_CLOSE] (nfsd4_dec)nfsd4_decode_close,
1316 [OP_COMMIT] (nfsd4_dec)nfsd4_decode_commit,
1317 [OP_CREATE] (nfsd4_dec)nfsd4_decode_create,
1318 [OP_DELEGPURGE] (nfsd4_dec)nfsd4_decode_notsupp,
1319 [OP_DELEGRETURN] (nfsd4_dec)nfsd4_decode_delegreturn,
1320 [OP_GETATTR] (nfsd4_dec)nfsd4_decode_getattr,
1321 [OP_GETFH] (nfsd4_dec)nfsd4_decode_noop,
1322 [OP_LINK] (nfsd4_dec)nfsd4_decode_link,
1323 [OP_LOCK] (nfsd4_dec)nfsd4_decode_lock,
1324 [OP_LOCKT] (nfsd4_dec)nfsd4_decode_lockt,
1325 [OP_LOCKU] (nfsd4_dec)nfsd4_decode_locku,
1326 [OP_LOOKUP] (nfsd4_dec)nfsd4_decode_lookup,
1327 [OP_LOOKUPP] (nfsd4_dec)nfsd4_decode_noop,
1328 [OP_NVERIFY] (nfsd4_dec)nfsd4_decode_verify,
1329 [OP_OPEN] (nfsd4_dec)nfsd4_decode_open,
1330 [OP_OPENATTR] (nfsd4_dec)nfsd4_decode_notsupp,
1331 [OP_OPEN_CONFIRM] (nfsd4_dec)nfsd4_decode_notsupp,
1332 [OP_OPEN_DOWNGRADE] (nfsd4_dec)nfsd4_decode_open_downgrade,
1333 [OP_PUTFH] (nfsd4_dec)nfsd4_decode_putfh,
1334 [OP_PUTPUBFH] (nfsd4_dec)nfsd4_decode_notsupp,
1335 [OP_PUTROOTFH] (nfsd4_dec)nfsd4_decode_noop,
1336 [OP_READ] (nfsd4_dec)nfsd4_decode_read,
1337 [OP_READDIR] (nfsd4_dec)nfsd4_decode_readdir,
1338 [OP_READLINK] (nfsd4_dec)nfsd4_decode_noop,
1339 [OP_REMOVE] (nfsd4_dec)nfsd4_decode_remove,
1340 [OP_RENAME] (nfsd4_dec)nfsd4_decode_rename,
1341 [OP_RENEW] (nfsd4_dec)nfsd4_decode_notsupp,
1342 [OP_RESTOREFH] (nfsd4_dec)nfsd4_decode_noop,
1343 [OP_SAVEFH] (nfsd4_dec)nfsd4_decode_noop,
1344 [OP_SECINFO] (nfsd4_dec)nfsd4_decode_secinfo,
1345 [OP_SETATTR] (nfsd4_dec)nfsd4_decode_setattr,
1346 [OP_SETCLIENTID] (nfsd4_dec)nfsd4_decode_notsupp,
1347 [OP_SETCLIENTID_CONFIRM](nfsd4_dec)nfsd4_decode_notsupp,
1348 [OP_VERIFY] (nfsd4_dec)nfsd4_decode_verify,
1349 [OP_WRITE] (nfsd4_dec)nfsd4_decode_write,
1350 [OP_RELEASE_LOCKOWNER] (nfsd4_dec)nfsd4_decode_notsupp,
1351
1352 /* new operations for NFSv4.1 */
1353 [OP_BACKCHANNEL_CTL] (nfsd4_dec)nfsd4_decode_notsupp,
1354 [OP_BIND_CONN_TO_SESSION](nfsd4_dec)nfsd4_decode_notsupp,
1355 [OP_EXCHANGE_ID] (nfsd4_dec)nfsd4_decode_exchange_id,
1356 [OP_CREATE_SESSION] (nfsd4_dec)nfsd4_decode_create_session,
1357 [OP_DESTROY_SESSION] (nfsd4_dec)nfsd4_decode_destroy_session,
1358 [OP_FREE_STATEID] (nfsd4_dec)nfsd4_decode_notsupp,
1359 [OP_GET_DIR_DELEGATION] (nfsd4_dec)nfsd4_decode_notsupp,
1360 [OP_GETDEVICEINFO] (nfsd4_dec)nfsd4_decode_notsupp,
1361 [OP_GETDEVICELIST] (nfsd4_dec)nfsd4_decode_notsupp,
1362 [OP_LAYOUTCOMMIT] (nfsd4_dec)nfsd4_decode_notsupp,
1363 [OP_LAYOUTGET] (nfsd4_dec)nfsd4_decode_notsupp,
1364 [OP_LAYOUTRETURN] (nfsd4_dec)nfsd4_decode_notsupp,
1365 [OP_SECINFO_NO_NAME] (nfsd4_dec)nfsd4_decode_notsupp,
1366 [OP_SEQUENCE] (nfsd4_dec)nfsd4_decode_sequence,
1367 [OP_SET_SSV] (nfsd4_dec)nfsd4_decode_notsupp,
1368 [OP_TEST_STATEID] (nfsd4_dec)nfsd4_decode_notsupp,
1369 [OP_WANT_DELEGATION] (nfsd4_dec)nfsd4_decode_notsupp,
1370 [OP_DESTROY_CLIENTID] (nfsd4_dec)nfsd4_decode_notsupp,
1371 [OP_RECLAIM_COMPLETE] (nfsd4_dec)nfsd4_decode_notsupp,
1372};
1373
Benny Halevyf2feb962008-07-02 11:14:22 +03001374struct nfsd4_minorversion_ops {
1375 nfsd4_dec *decoders;
1376 int nops;
1377};
1378
1379static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001380 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001381 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001382};
1383
Benny Halevy347e0ad2008-07-02 11:13:41 +03001384static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1386{
1387 DECODE_HEAD;
1388 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001389 struct nfsd4_minorversion_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int i;
1391
1392 /*
1393 * XXX: According to spec, we should check the tag
1394 * for UTF-8 compliance. I'm postponing this for
1395 * now because it seems that some clients do use
1396 * binary tags.
1397 */
1398 READ_BUF(4);
1399 READ32(argp->taglen);
1400 READ_BUF(argp->taglen + 8);
1401 SAVEMEM(argp->tag, argp->taglen);
1402 READ32(argp->minorversion);
1403 READ32(argp->opcnt);
1404
1405 if (argp->taglen > NFSD4_MAX_TAGLEN)
1406 goto xdr_error;
1407 if (argp->opcnt > 100)
1408 goto xdr_error;
1409
Tobias Klausere8c96f82006-03-24 03:15:34 -08001410 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1412 if (!argp->ops) {
1413 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001414 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 goto xdr_error;
1416 }
1417 }
1418
Benny Halevyf2feb962008-07-02 11:14:22 +03001419 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001420 argp->opcnt = 0;
1421
Benny Halevyf2feb962008-07-02 11:14:22 +03001422 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 for (i = 0; i < argp->opcnt; i++) {
1424 op = &argp->ops[i];
1425 op->replay = NULL;
1426
1427 /*
1428 * We can't use READ_BUF() here because we need to handle
1429 * a missing opcode as an OP_WRITE + 1. So we need to check
1430 * to see if we're truly at the end of our buffer or if there
1431 * is another page we need to flip to.
1432 */
1433
1434 if (argp->p == argp->end) {
1435 if (argp->pagelen < 4) {
1436 /* There isn't an opcode still on the wire */
1437 op->opnum = OP_WRITE + 1;
1438 op->status = nfserr_bad_xdr;
1439 argp->opcnt = i+1;
1440 break;
1441 }
1442
1443 /*
1444 * False alarm. We just hit a page boundary, but there
1445 * is still data available. Move pointer across page
1446 * boundary. *snip from READ_BUF*
1447 */
1448 argp->p = page_address(argp->pagelist[0]);
1449 argp->pagelist++;
1450 if (argp->pagelen < PAGE_SIZE) {
1451 argp->end = p + (argp->pagelen>>2);
1452 argp->pagelen = 0;
1453 } else {
1454 argp->end = p + (PAGE_SIZE>>2);
1455 argp->pagelen -= PAGE_SIZE;
1456 }
1457 }
1458 op->opnum = ntohl(*argp->p++);
1459
Benny Halevyf2feb962008-07-02 11:14:22 +03001460 if (op->opnum >= OP_ACCESS && op->opnum < ops->nops)
1461 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001462 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 op->opnum = OP_ILLEGAL;
1464 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
1467 if (op->status) {
1468 argp->opcnt = i+1;
1469 break;
1470 }
1471 }
1472
1473 DECODE_TAIL;
1474}
1475/*
1476 * END OF "GENERIC" DECODE ROUTINES.
1477 */
1478
1479/*
1480 * START OF "GENERIC" ENCODE ROUTINES.
1481 * These may look a little ugly since they are imported from a "generic"
1482 * set of XDR encode/decode routines which are intended to be shared by
1483 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1484 *
1485 * If the pain of reading these is too great, it should be a straightforward
1486 * task to translate them into Linux-specific versions which are more
1487 * consistent with the style used in NFSv2/v3...
1488 */
Al Viro2ebbc012006-10-19 23:28:58 -07001489#define ENCODE_HEAD __be32 *p
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491#define WRITE32(n) *p++ = htonl(n)
1492#define WRITE64(n) do { \
1493 *p++ = htonl((u32)((n) >> 32)); \
1494 *p++ = htonl((u32)(n)); \
1495} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001496#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1498 memcpy(p, ptr, nbytes); \
1499 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001500}} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501#define WRITECINFO(c) do { \
1502 *p++ = htonl(c.atomic); \
1503 *p++ = htonl(c.before_ctime_sec); \
1504 *p++ = htonl(c.before_ctime_nsec); \
1505 *p++ = htonl(c.after_ctime_sec); \
1506 *p++ = htonl(c.after_ctime_nsec); \
1507} while (0)
1508
1509#define RESERVE_SPACE(nbytes) do { \
1510 p = resp->p; \
1511 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1512} while (0)
1513#define ADJUST_ARGS() resp->p = p
1514
1515/*
1516 * Header routine to setup seqid operation replay cache
1517 */
1518#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001519 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 \
1521 save = resp->p;
1522
1523/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001524 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1525 * is where sequence id's are incremented, and the replay cache is filled.
1526 * Note that we increment sequence id's here, at the last moment, so we're sure
1527 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
1529
1530#define ENCODE_SEQID_OP_TAIL(stateowner) do { \
1531 if (seqid_mutating_err(nfserr) && stateowner) { \
NeilBrownbd9aac52005-07-07 17:59:19 -07001532 stateowner->so_seqid++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 stateowner->so_replay.rp_status = nfserr; \
1534 stateowner->so_replay.rp_buflen = \
1535 (((char *)(resp)->p - (char *)save)); \
1536 memcpy(stateowner->so_replay.rp_buf, save, \
1537 stateowner->so_replay.rp_buflen); \
1538 } } while (0);
1539
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001540/* Encode as an array of strings the string given with components
1541 * seperated @sep.
1542 */
Al Virob37ad282006-10-19 23:28:59 -07001543static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001544 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001545{
Al Viro2ebbc012006-10-19 23:28:58 -07001546 __be32 *p = *pp;
1547 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001548 int strlen, count=0;
1549 char *str, *end;
1550
1551 dprintk("nfsd4_encode_components(%s)\n", components);
1552 if ((*buflen -= 4) < 0)
1553 return nfserr_resource;
1554 WRITE32(0); /* We will fill this in with @count later */
1555 end = str = components;
1556 while (*end) {
1557 for (; *end && (*end != sep); end++)
1558 ; /* Point to end of component */
1559 strlen = end - str;
1560 if (strlen) {
1561 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1562 return nfserr_resource;
1563 WRITE32(strlen);
1564 WRITEMEM(str, strlen);
1565 count++;
1566 }
1567 else
1568 end++;
1569 str = end;
1570 }
1571 *pp = p;
1572 p = countp;
1573 WRITE32(count);
1574 return 0;
1575}
1576
1577/*
1578 * encode a location element of a fs_locations structure
1579 */
Al Virob37ad282006-10-19 23:28:59 -07001580static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001581 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001582{
Al Virob37ad282006-10-19 23:28:59 -07001583 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001584 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001585
1586 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1587 if (status)
1588 return status;
1589 status = nfsd4_encode_components('/', location->path, &p, buflen);
1590 if (status)
1591 return status;
1592 *pp = p;
1593 return 0;
1594}
1595
1596/*
1597 * Return the path to an export point in the pseudo filesystem namespace
1598 * Returned string is safe to use as long as the caller holds a reference
1599 * to @exp.
1600 */
Al Virob37ad282006-10-19 23:28:59 -07001601static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001602{
1603 struct svc_fh tmp_fh;
1604 char *path, *rootpath;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001605
1606 fh_init(&tmp_fh, NFS4_FHSIZE);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -07001607 *stat = exp_pseudoroot(rqstp, &tmp_fh);
Al Virocc45f012006-10-19 23:28:44 -07001608 if (*stat)
1609 return NULL;
Jan Blunck54775492008-02-14 19:38:39 -08001610 rootpath = tmp_fh.fh_export->ex_pathname;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001611
Jan Blunck54775492008-02-14 19:38:39 -08001612 path = exp->ex_pathname;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001613
1614 if (strncmp(path, rootpath, strlen(rootpath))) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001615 dprintk("nfsd: fs_locations failed;"
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001616 "%s is not contained in %s\n", path, rootpath);
Al Virocc45f012006-10-19 23:28:44 -07001617 *stat = nfserr_notsupp;
1618 return NULL;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001619 }
1620
1621 return path + strlen(rootpath);
1622}
1623
1624/*
1625 * encode a fs_locations structure
1626 */
Al Virob37ad282006-10-19 23:28:59 -07001627static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001628 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001629 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001630{
Al Virob37ad282006-10-19 23:28:59 -07001631 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001632 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001633 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001634 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
Al Virocc45f012006-10-19 23:28:44 -07001635 char *root = nfsd4_path(rqstp, exp, &status);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001636
Al Virocc45f012006-10-19 23:28:44 -07001637 if (status)
1638 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001639 status = nfsd4_encode_components('/', root, &p, buflen);
1640 if (status)
1641 return status;
1642 if ((*buflen -= 4) < 0)
1643 return nfserr_resource;
1644 WRITE32(fslocs->locations_count);
1645 for (i=0; i<fslocs->locations_count; i++) {
1646 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1647 &p, buflen);
1648 if (status)
1649 return status;
1650 }
1651 *pp = p;
1652 return 0;
1653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655static u32 nfs4_ftypes[16] = {
1656 NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
1657 NF4DIR, NF4BAD, NF4BLK, NF4BAD,
1658 NF4REG, NF4BAD, NF4LNK, NF4BAD,
1659 NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
1660};
1661
Al Virob37ad282006-10-19 23:28:59 -07001662static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001664 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 int status;
1667
1668 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1669 return nfserr_resource;
1670 if (whotype != NFS4_ACL_WHO_NAMED)
1671 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1672 else if (group)
1673 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1674 else
1675 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1676 if (status < 0)
1677 return nfserrno(status);
1678 *p = xdr_encode_opaque(*p, NULL, status);
1679 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1680 BUG_ON(*buflen < 0);
1681 return 0;
1682}
1683
Al Virob37ad282006-10-19 23:28:59 -07001684static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001685nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1688}
1689
Al Virob37ad282006-10-19 23:28:59 -07001690static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001691nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1694}
1695
Al Virob37ad282006-10-19 23:28:59 -07001696static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001698 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1701}
1702
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001703#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1704 FATTR4_WORD0_RDATTR_ERROR)
1705#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1706
Al Virob37ad282006-10-19 23:28:59 -07001707static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001708{
1709 /* As per referral draft: */
1710 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1711 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1712 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1713 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1714 *rdattr_err = NFSERR_MOVED;
1715 else
1716 return nfserr_moved;
1717 }
1718 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1719 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1720 return 0;
1721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723/*
1724 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1725 * ourselves.
1726 *
1727 * @countp is the buffer size in _words_; upon successful return this becomes
1728 * replaced with the number of words written.
1729 */
Al Virob37ad282006-10-19 23:28:59 -07001730__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001732 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001733 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 u32 bmval0 = bmval[0];
1736 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03001737 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct kstat stat;
1739 struct svc_fh tempfh;
1740 struct kstatfs statfs;
1741 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07001742 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 u32 dummy;
1744 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001745 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07001746 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07001747 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07001748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int aclsupport = 0;
1750 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03001751 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1752 u32 minorversion = resp->cstate.minorversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03001755 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
1756 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
1757 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001759 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03001760 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001761 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
1762 if (status)
1763 goto out;
1764 }
1765
Jan Blunck54775492008-02-14 19:38:39 -08001766 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07001767 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04001769 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
1770 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
1772 FATTR4_WORD1_SPACE_TOTAL))) {
Al Virob8dd7b92006-10-19 23:29:01 -07001773 err = vfs_statfs(dentry, &statfs);
1774 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto out_nfserr;
1776 }
1777 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
1778 fh_init(&tempfh, NFS4_FHSIZE);
1779 status = fh_compose(&tempfh, exp, dentry, NULL);
1780 if (status)
1781 goto out;
1782 fhp = &tempfh;
1783 }
1784 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
1785 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07001786 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
1787 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07001789 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07001791 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 status = nfserr_attrnotsupp;
1793 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07001794 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto out_nfserr;
1796 }
1797 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001798 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
1799 if (exp->ex_fslocs.locations == NULL) {
1800 bmval0 &= ~FATTR4_WORD0_FS_LOCATIONS;
1801 }
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if ((buflen -= 16) < 0)
1804 goto out_resource;
1805
Andy Adamson7e705702009-04-03 08:29:11 +03001806 if (unlikely(bmval2)) {
1807 WRITE32(3);
1808 WRITE32(bmval0);
1809 WRITE32(bmval1);
1810 WRITE32(bmval2);
1811 } else if (likely(bmval1)) {
1812 WRITE32(2);
1813 WRITE32(bmval0);
1814 WRITE32(bmval1);
1815 } else {
1816 WRITE32(1);
1817 WRITE32(bmval0);
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 attrlenp = p++; /* to be backfilled later */
1820
1821 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03001822 u32 word0 = nfsd_suppattrs0(minorversion);
1823 u32 word1 = nfsd_suppattrs1(minorversion);
1824 u32 word2 = nfsd_suppattrs2(minorversion);
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if ((buflen -= 12) < 0)
1827 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001828 if (!aclsupport)
1829 word0 &= ~FATTR4_WORD0_ACL;
1830 if (!exp->ex_fslocs.locations)
1831 word0 &= ~FATTR4_WORD0_FS_LOCATIONS;
Andy Adamson7e705702009-04-03 08:29:11 +03001832 if (!word2) {
1833 WRITE32(2);
1834 WRITE32(word0);
1835 WRITE32(word1);
1836 } else {
1837 WRITE32(3);
1838 WRITE32(word0);
1839 WRITE32(word1);
1840 WRITE32(word2);
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 if (bmval0 & FATTR4_WORD0_TYPE) {
1844 if ((buflen -= 4) < 0)
1845 goto out_resource;
1846 dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
1847 if (dummy == NF4BAD)
1848 goto out_serverfault;
1849 WRITE32(dummy);
1850 }
1851 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
1852 if ((buflen -= 4) < 0)
1853 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07001854 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07001855 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07001856 else
NeilBrowne34ac862005-07-07 17:59:30 -07001857 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 if (bmval0 & FATTR4_WORD0_CHANGE) {
1860 /*
1861 * Note: This _must_ be consistent with the scheme for writing
1862 * change_info, so any changes made here must be reflected there
1863 * as well. (See xdr4.h:set_change_info() and the WRITECINFO()
1864 * macro above.)
1865 */
1866 if ((buflen -= 8) < 0)
1867 goto out_resource;
1868 WRITE32(stat.ctime.tv_sec);
1869 WRITE32(stat.ctime.tv_nsec);
1870 }
1871 if (bmval0 & FATTR4_WORD0_SIZE) {
1872 if ((buflen -= 8) < 0)
1873 goto out_resource;
1874 WRITE64(stat.size);
1875 }
1876 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
1877 if ((buflen -= 4) < 0)
1878 goto out_resource;
1879 WRITE32(1);
1880 }
1881 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
1882 if ((buflen -= 4) < 0)
1883 goto out_resource;
1884 WRITE32(1);
1885 }
1886 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
1887 if ((buflen -= 4) < 0)
1888 goto out_resource;
1889 WRITE32(0);
1890 }
1891 if (bmval0 & FATTR4_WORD0_FSID) {
1892 if ((buflen -= 16) < 0)
1893 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001894 if (exp->ex_fslocs.migrated) {
1895 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
1896 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08001897 } else switch(fsid_source(fhp)) {
1898 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 WRITE64((u64)exp->ex_fsid);
1900 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08001901 break;
1902 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 WRITE32(0);
1904 WRITE32(MAJOR(stat.dev));
1905 WRITE32(0);
1906 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08001907 break;
1908 case FSIDSOURCE_UUID:
1909 WRITEMEM(exp->ex_uuid, 16);
1910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 }
1913 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
1914 if ((buflen -= 4) < 0)
1915 goto out_resource;
1916 WRITE32(0);
1917 }
1918 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
1919 if ((buflen -= 4) < 0)
1920 goto out_resource;
1921 WRITE32(NFSD_LEASE_TIME);
1922 }
1923 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
1924 if ((buflen -= 4) < 0)
1925 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001926 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 if (bmval0 & FATTR4_WORD0_ACL) {
1929 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (acl == NULL) {
1932 if ((buflen -= 4) < 0)
1933 goto out_resource;
1934
1935 WRITE32(0);
1936 goto out_acl;
1937 }
1938 if ((buflen -= 4) < 0)
1939 goto out_resource;
1940 WRITE32(acl->naces);
1941
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08001942 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if ((buflen -= 4*3) < 0)
1944 goto out_resource;
1945 WRITE32(ace->type);
1946 WRITE32(ace->flag);
1947 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
1948 status = nfsd4_encode_aclname(rqstp, ace->whotype,
1949 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
1950 &p, &buflen);
1951 if (status == nfserr_resource)
1952 goto out_resource;
1953 if (status)
1954 goto out;
1955 }
1956 }
1957out_acl:
1958 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
1959 if ((buflen -= 4) < 0)
1960 goto out_resource;
1961 WRITE32(aclsupport ?
1962 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
1963 }
1964 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
1965 if ((buflen -= 4) < 0)
1966 goto out_resource;
1967 WRITE32(1);
1968 }
1969 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
1970 if ((buflen -= 4) < 0)
1971 goto out_resource;
1972 WRITE32(1);
1973 }
1974 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
1975 if ((buflen -= 4) < 0)
1976 goto out_resource;
1977 WRITE32(1);
1978 }
1979 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
1980 if ((buflen -= 4) < 0)
1981 goto out_resource;
1982 WRITE32(1);
1983 }
1984 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
1985 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
1986 if (buflen < 0)
1987 goto out_resource;
1988 WRITE32(fhp->fh_handle.fh_size);
1989 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
1990 }
1991 if (bmval0 & FATTR4_WORD0_FILEID) {
1992 if ((buflen -= 8) < 0)
1993 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001994 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
1997 if ((buflen -= 8) < 0)
1998 goto out_resource;
1999 WRITE64((u64) statfs.f_ffree);
2000 }
2001 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2002 if ((buflen -= 8) < 0)
2003 goto out_resource;
2004 WRITE64((u64) statfs.f_ffree);
2005 }
2006 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2007 if ((buflen -= 8) < 0)
2008 goto out_resource;
2009 WRITE64((u64) statfs.f_files);
2010 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002011 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2012 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2013 if (status == nfserr_resource)
2014 goto out_resource;
2015 if (status)
2016 goto out;
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2019 if ((buflen -= 4) < 0)
2020 goto out_resource;
2021 WRITE32(1);
2022 }
2023 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2024 if ((buflen -= 8) < 0)
2025 goto out_resource;
2026 WRITE64(~(u64)0);
2027 }
2028 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2029 if ((buflen -= 4) < 0)
2030 goto out_resource;
2031 WRITE32(255);
2032 }
2033 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2034 if ((buflen -= 4) < 0)
2035 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002036 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2039 if ((buflen -= 8) < 0)
2040 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002041 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2044 if ((buflen -= 8) < 0)
2045 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002046 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 if (bmval1 & FATTR4_WORD1_MODE) {
2049 if ((buflen -= 4) < 0)
2050 goto out_resource;
2051 WRITE32(stat.mode & S_IALLUGO);
2052 }
2053 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2054 if ((buflen -= 4) < 0)
2055 goto out_resource;
2056 WRITE32(1);
2057 }
2058 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2059 if ((buflen -= 4) < 0)
2060 goto out_resource;
2061 WRITE32(stat.nlink);
2062 }
2063 if (bmval1 & FATTR4_WORD1_OWNER) {
2064 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2065 if (status == nfserr_resource)
2066 goto out_resource;
2067 if (status)
2068 goto out;
2069 }
2070 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2071 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2072 if (status == nfserr_resource)
2073 goto out_resource;
2074 if (status)
2075 goto out;
2076 }
2077 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2078 if ((buflen -= 8) < 0)
2079 goto out_resource;
2080 WRITE32((u32) MAJOR(stat.rdev));
2081 WRITE32((u32) MINOR(stat.rdev));
2082 }
2083 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2084 if ((buflen -= 8) < 0)
2085 goto out_resource;
2086 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2087 WRITE64(dummy64);
2088 }
2089 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2090 if ((buflen -= 8) < 0)
2091 goto out_resource;
2092 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2093 WRITE64(dummy64);
2094 }
2095 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2096 if ((buflen -= 8) < 0)
2097 goto out_resource;
2098 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2099 WRITE64(dummy64);
2100 }
2101 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2102 if ((buflen -= 8) < 0)
2103 goto out_resource;
2104 dummy64 = (u64)stat.blocks << 9;
2105 WRITE64(dummy64);
2106 }
2107 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2108 if ((buflen -= 12) < 0)
2109 goto out_resource;
2110 WRITE32(0);
2111 WRITE32(stat.atime.tv_sec);
2112 WRITE32(stat.atime.tv_nsec);
2113 }
2114 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2115 if ((buflen -= 12) < 0)
2116 goto out_resource;
2117 WRITE32(0);
2118 WRITE32(1);
2119 WRITE32(0);
2120 }
2121 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2122 if ((buflen -= 12) < 0)
2123 goto out_resource;
2124 WRITE32(0);
2125 WRITE32(stat.ctime.tv_sec);
2126 WRITE32(stat.ctime.tv_nsec);
2127 }
2128 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2129 if ((buflen -= 12) < 0)
2130 goto out_resource;
2131 WRITE32(0);
2132 WRITE32(stat.mtime.tv_sec);
2133 WRITE32(stat.mtime.tv_nsec);
2134 }
2135 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if ((buflen -= 8) < 0)
2137 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002138 /*
2139 * Get parent's attributes if not ignoring crossmount
2140 * and this is the root of a cross-mounted filesystem.
2141 */
2142 if (ignore_crossmnt == 0 &&
Jan Blunck54775492008-02-14 19:38:39 -08002143 exp->ex_path.mnt->mnt_root->d_inode == dentry->d_inode) {
2144 err = vfs_getattr(exp->ex_path.mnt->mnt_parent,
2145 exp->ex_path.mnt->mnt_mountpoint, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002146 if (err)
2147 goto out_nfserr;
2148 }
2149 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002151 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2152 WRITE32(3);
2153 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2154 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2155 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2156 }
Andy Adamson7e705702009-04-03 08:29:11 +03002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2159 *countp = p - buffer;
2160 status = nfs_ok;
2161
2162out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002163 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (fhp == &tempfh)
2165 fh_put(&tempfh);
2166 return status;
2167out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002168 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto out;
2170out_resource:
2171 *countp = 0;
2172 status = nfserr_resource;
2173 goto out;
2174out_serverfault:
2175 status = nfserr_serverfault;
2176 goto out;
2177}
2178
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002179static inline int attributes_need_mount(u32 *bmval)
2180{
2181 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2182 return 1;
2183 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2184 return 1;
2185 return 0;
2186}
2187
Al Virob37ad282006-10-19 23:28:59 -07002188static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002190 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct svc_export *exp = cd->rd_fhp->fh_export;
2193 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002194 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002195 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2198 if (IS_ERR(dentry))
2199 return nfserrno(PTR_ERR(dentry));
2200
2201 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002202 /*
2203 * In the case of a mountpoint, the client may be asking for
2204 * attributes that are only properties of the underlying filesystem
2205 * as opposed to the cross-mounted file system. In such a case,
2206 * we will not follow the cross mount and will fill the attribtutes
2207 * directly from the mountpoint dentry.
2208 */
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002209 if (d_mountpoint(dentry) && !attributes_need_mount(cd->rd_bmval))
Frank Filz406a7ea2007-11-27 11:34:05 -08002210 ignore_crossmnt = 1;
2211 else if (d_mountpoint(dentry)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002212 int err;
2213
Andy Adamsondcb488a32007-07-17 04:04:51 -07002214 /*
2215 * Why the heck aren't we just using nfsd_lookup??
2216 * Different "."/".." handling? Something else?
2217 * At least, add a comment here to explain....
2218 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002219 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2220 if (err) {
2221 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto out_put;
2223 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002224 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2225 if (nfserr)
2226 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 }
2229 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002230 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231out_put:
2232 dput(dentry);
2233 exp_put(exp);
2234 return nfserr;
2235}
2236
Al Viro2ebbc012006-10-19 23:28:58 -07002237static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002238nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Al Viro2ebbc012006-10-19 23:28:58 -07002240 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 if (buflen < 6)
2243 return NULL;
2244 *p++ = htonl(2);
2245 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2246 *p++ = htonl(0); /* bmval1 */
2247
2248 attrlenp = p++;
2249 *p++ = nfserr; /* no htonl */
2250 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2251 return p;
2252}
2253
2254static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002255nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2256 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002258 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2260 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002261 __be32 *p = cd->buffer;
Al Virob37ad282006-10-19 23:28:59 -07002262 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 /* In nfsv4, "." and ".." never make it onto the wire.. */
2265 if (name && isdotent(name, namlen)) {
2266 cd->common.err = nfs_ok;
2267 return 0;
2268 }
2269
2270 if (cd->offset)
2271 xdr_encode_hyper(cd->offset, (u64) offset);
2272
2273 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2274 if (buflen < 0)
2275 goto fail;
2276
2277 *p++ = xdr_one; /* mark entry present */
2278 cd->offset = p; /* remember pointer */
2279 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2280 p = xdr_encode_array(p, name, namlen); /* name length & name */
2281
2282 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2283 switch (nfserr) {
2284 case nfs_ok:
2285 p += buflen;
2286 break;
2287 case nfserr_resource:
2288 nfserr = nfserr_toosmall;
2289 goto fail;
2290 case nfserr_dropit:
2291 goto fail;
2292 default:
2293 /*
2294 * If the client requested the RDATTR_ERROR attribute,
2295 * we stuff the error code into this attribute
2296 * and continue. If this attribute was not requested,
2297 * then in accordance with the spec, we fail the
2298 * entire READDIR operation(!)
2299 */
2300 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2301 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002303 if (p == NULL) {
2304 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308 cd->buflen -= (p - cd->buffer);
2309 cd->buffer = p;
2310 cd->common.err = nfs_ok;
2311 return 0;
2312fail:
2313 cd->common.err = nfserr;
2314 return -EINVAL;
2315}
2316
Benny Halevye2f282b2008-08-12 20:45:07 +03002317static void
2318nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2319{
2320 ENCODE_HEAD;
2321
2322 RESERVE_SPACE(sizeof(stateid_t));
2323 WRITE32(sid->si_generation);
2324 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2325 ADJUST_ARGS();
2326}
2327
Benny Halevy695e12f2008-07-04 14:38:33 +03002328static __be32
Al Virob37ad282006-10-19 23:28:59 -07002329nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 ENCODE_HEAD;
2332
2333 if (!nfserr) {
2334 RESERVE_SPACE(8);
2335 WRITE32(access->ac_supported);
2336 WRITE32(access->ac_resp_access);
2337 ADJUST_ARGS();
2338 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002339 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Benny Halevy695e12f2008-07-04 14:38:33 +03002342static __be32
Al Virob37ad282006-10-19 23:28:59 -07002343nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 ENCODE_SEQID_OP_HEAD;
2346
Benny Halevye2f282b2008-08-12 20:45:07 +03002347 if (!nfserr)
2348 nfsd4_encode_stateid(resp, &close->cl_stateid);
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002351 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
2354
Benny Halevy695e12f2008-07-04 14:38:33 +03002355static __be32
Al Virob37ad282006-10-19 23:28:59 -07002356nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 ENCODE_HEAD;
2359
2360 if (!nfserr) {
2361 RESERVE_SPACE(8);
2362 WRITEMEM(commit->co_verf.data, 8);
2363 ADJUST_ARGS();
2364 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002365 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Benny Halevy695e12f2008-07-04 14:38:33 +03002368static __be32
Al Virob37ad282006-10-19 23:28:59 -07002369nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 ENCODE_HEAD;
2372
2373 if (!nfserr) {
2374 RESERVE_SPACE(32);
2375 WRITECINFO(create->cr_cinfo);
2376 WRITE32(2);
2377 WRITE32(create->cr_bmval[0]);
2378 WRITE32(create->cr_bmval[1]);
2379 ADJUST_ARGS();
2380 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002381 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Al Virob37ad282006-10-19 23:28:59 -07002384static __be32
2385nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
2387 struct svc_fh *fhp = getattr->ga_fhp;
2388 int buflen;
2389
2390 if (nfserr)
2391 return nfserr;
2392
2393 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2394 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2395 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002396 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (!nfserr)
2398 resp->p += buflen;
2399 return nfserr;
2400}
2401
Benny Halevy695e12f2008-07-04 14:38:33 +03002402static __be32
2403nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Benny Halevy695e12f2008-07-04 14:38:33 +03002405 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 unsigned int len;
2407 ENCODE_HEAD;
2408
2409 if (!nfserr) {
2410 len = fhp->fh_handle.fh_size;
2411 RESERVE_SPACE(len + 4);
2412 WRITE32(len);
2413 WRITEMEM(&fhp->fh_handle.fh_base, len);
2414 ADJUST_ARGS();
2415 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002416 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
2419/*
2420* Including all fields other than the name, a LOCK4denied structure requires
2421* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2422*/
2423static void
2424nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2425{
2426 ENCODE_HEAD;
2427
2428 RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
2429 WRITE64(ld->ld_start);
2430 WRITE64(ld->ld_length);
2431 WRITE32(ld->ld_type);
2432 if (ld->ld_sop) {
2433 WRITEMEM(&ld->ld_clientid, 8);
2434 WRITE32(ld->ld_sop->so_owner.len);
2435 WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
2436 kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
2437 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2438 WRITE64((u64)0); /* clientid */
2439 WRITE32(0); /* length of owner name */
2440 }
2441 ADJUST_ARGS();
2442}
2443
Benny Halevy695e12f2008-07-04 14:38:33 +03002444static __be32
Al Virob37ad282006-10-19 23:28:59 -07002445nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 ENCODE_SEQID_OP_HEAD;
2448
Benny Halevye2f282b2008-08-12 20:45:07 +03002449 if (!nfserr)
2450 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2451 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2453
J. Bruce Fields3a655882006-01-18 17:43:19 -08002454 ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002455 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Benny Halevy695e12f2008-07-04 14:38:33 +03002458static __be32
Al Virob37ad282006-10-19 23:28:59 -07002459nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 if (nfserr == nfserr_denied)
2462 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002463 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Benny Halevy695e12f2008-07-04 14:38:33 +03002466static __be32
Al Virob37ad282006-10-19 23:28:59 -07002467nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
2469 ENCODE_SEQID_OP_HEAD;
2470
Benny Halevye2f282b2008-08-12 20:45:07 +03002471 if (!nfserr)
2472 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002475 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
2478
Benny Halevy695e12f2008-07-04 14:38:33 +03002479static __be32
Al Virob37ad282006-10-19 23:28:59 -07002480nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 ENCODE_HEAD;
2483
2484 if (!nfserr) {
2485 RESERVE_SPACE(20);
2486 WRITECINFO(link->li_cinfo);
2487 ADJUST_ARGS();
2488 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002489 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492
Benny Halevy695e12f2008-07-04 14:38:33 +03002493static __be32
Al Virob37ad282006-10-19 23:28:59 -07002494nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Benny Halevy1b6b2252008-08-12 20:45:28 +03002496 ENCODE_HEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ENCODE_SEQID_OP_HEAD;
2498
2499 if (nfserr)
2500 goto out;
2501
Benny Halevye2f282b2008-08-12 20:45:07 +03002502 nfsd4_encode_stateid(resp, &open->op_stateid);
2503 RESERVE_SPACE(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 WRITECINFO(open->op_cinfo);
2505 WRITE32(open->op_rflags);
2506 WRITE32(2);
2507 WRITE32(open->op_bmval[0]);
2508 WRITE32(open->op_bmval[1]);
2509 WRITE32(open->op_delegate_type);
2510 ADJUST_ARGS();
2511
2512 switch (open->op_delegate_type) {
2513 case NFS4_OPEN_DELEGATE_NONE:
2514 break;
2515 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002516 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2517 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002518 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /*
2521 * TODO: ACE's in delegations
2522 */
2523 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2524 WRITE32(0);
2525 WRITE32(0);
2526 WRITE32(0); /* XXX: is NULL principal ok? */
2527 ADJUST_ARGS();
2528 break;
2529 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002530 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2531 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 WRITE32(0);
2533
2534 /*
2535 * TODO: space_limit's in delegations
2536 */
2537 WRITE32(NFS4_LIMIT_SIZE);
2538 WRITE32(~(u32)0);
2539 WRITE32(~(u32)0);
2540
2541 /*
2542 * TODO: ACE's in delegations
2543 */
2544 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2545 WRITE32(0);
2546 WRITE32(0);
2547 WRITE32(0); /* XXX: is NULL principal ok? */
2548 ADJUST_ARGS();
2549 break;
2550 default:
2551 BUG();
2552 }
2553 /* XXX save filehandle here */
2554out:
2555 ENCODE_SEQID_OP_TAIL(open->op_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002556 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Benny Halevy695e12f2008-07-04 14:38:33 +03002559static __be32
Al Virob37ad282006-10-19 23:28:59 -07002560nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002563
2564 if (!nfserr)
2565 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002568 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
Benny Halevy695e12f2008-07-04 14:38:33 +03002571static __be32
Al Virob37ad282006-10-19 23:28:59 -07002572nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
2574 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002575
2576 if (!nfserr)
2577 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 ENCODE_SEQID_OP_TAIL(od->od_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002580 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Al Virob37ad282006-10-19 23:28:59 -07002583static __be32
2584nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002585 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 u32 eof;
2588 int v, pn;
2589 unsigned long maxcount;
2590 long len;
2591 ENCODE_HEAD;
2592
2593 if (nfserr)
2594 return nfserr;
2595 if (resp->xbuf->page_len)
2596 return nfserr_resource;
2597
2598 RESERVE_SPACE(8); /* eof flag and byte count */
2599
Greg Banks7adae482006-10-04 02:15:47 -07002600 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (maxcount > read->rd_length)
2602 maxcount = read->rd_length;
2603
2604 len = maxcount;
2605 v = 0;
2606 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -07002607 pn = resp->rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -07002608 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002609 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002610 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002611 len < PAGE_SIZE ? len : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 v++;
2613 len -= PAGE_SIZE;
2614 }
2615 read->rd_vlen = v;
2616
2617 nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002618 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 &maxcount);
2620
2621 if (nfserr == nfserr_symlink)
2622 nfserr = nfserr_inval;
2623 if (nfserr)
2624 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002625 eof = (read->rd_offset + maxcount >=
2626 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 WRITE32(eof);
2629 WRITE32(maxcount);
2630 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002631 resp->xbuf->head[0].iov_len = (char*)p
2632 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 resp->xbuf->page_len = maxcount;
2634
NeilBrown6ed6dec2006-04-10 22:55:32 -07002635 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002636 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002639 RESERVE_SPACE(4);
2640 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 resp->xbuf->tail[0].iov_base += maxcount&3;
2642 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002643 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 return 0;
2646}
2647
Al Virob37ad282006-10-19 23:28:59 -07002648static __be32
2649nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 int maxcount;
2652 char *page;
2653 ENCODE_HEAD;
2654
2655 if (nfserr)
2656 return nfserr;
2657 if (resp->xbuf->page_len)
2658 return nfserr_resource;
2659
NeilBrown44524352006-10-04 02:15:46 -07002660 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 maxcount = PAGE_SIZE;
2663 RESERVE_SPACE(4);
2664
2665 /*
2666 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2667 * if they would overflow the buffer. Is this kosher in NFSv4? If
2668 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2669 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2670 */
2671 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2672 if (nfserr == nfserr_isdir)
2673 return nfserr_inval;
2674 if (nfserr)
2675 return nfserr;
2676
2677 WRITE32(maxcount);
2678 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002679 resp->xbuf->head[0].iov_len = (char*)p
2680 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07002682
2683 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002684 resp->xbuf->tail[0].iov_base = p;
2685 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002687 RESERVE_SPACE(4);
2688 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 resp->xbuf->tail[0].iov_base += maxcount&3;
2690 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002691 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693 return 0;
2694}
2695
Al Virob37ad282006-10-19 23:28:59 -07002696static __be32
2697nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 int maxcount;
2700 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07002701 __be32 *page, *savep, *tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 ENCODE_HEAD;
2703
2704 if (nfserr)
2705 return nfserr;
2706 if (resp->xbuf->page_len)
2707 return nfserr_resource;
2708
2709 RESERVE_SPACE(8); /* verifier */
2710 savep = p;
2711
2712 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2713 WRITE32(0);
2714 WRITE32(0);
2715 ADJUST_ARGS();
2716 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002717 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 maxcount = PAGE_SIZE;
2720 if (maxcount > readdir->rd_maxcount)
2721 maxcount = readdir->rd_maxcount;
2722
2723 /*
2724 * Convert from bytes to words, account for the two words already
2725 * written, make sure to leave two words at the end for the next
2726 * pointer and eof field.
2727 */
2728 maxcount = (maxcount >> 2) - 4;
2729 if (maxcount < 0) {
2730 nfserr = nfserr_toosmall;
2731 goto err_no_verf;
2732 }
2733
NeilBrown44524352006-10-04 02:15:46 -07002734 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 readdir->common.err = 0;
2736 readdir->buflen = maxcount;
2737 readdir->buffer = page;
2738 readdir->offset = NULL;
2739
2740 offset = readdir->rd_cookie;
2741 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
2742 &offset,
2743 &readdir->common, nfsd4_encode_dirent);
2744 if (nfserr == nfs_ok &&
2745 readdir->common.err == nfserr_toosmall &&
2746 readdir->buffer == page)
2747 nfserr = nfserr_toosmall;
2748 if (nfserr == nfserr_symlink)
2749 nfserr = nfserr_notdir;
2750 if (nfserr)
2751 goto err_no_verf;
2752
2753 if (readdir->offset)
2754 xdr_encode_hyper(readdir->offset, offset);
2755
2756 p = readdir->buffer;
2757 *p++ = 0; /* no more entries */
2758 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07002759 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
2760 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
NeilBrownbb6e8a92006-04-10 22:55:33 -07002762 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07002763 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 resp->xbuf->tail[0].iov_len = 0;
2765 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002766 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 return 0;
2769err_no_verf:
2770 p = savep;
2771 ADJUST_ARGS();
2772 return nfserr;
2773}
2774
Benny Halevy695e12f2008-07-04 14:38:33 +03002775static __be32
Al Virob37ad282006-10-19 23:28:59 -07002776nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 ENCODE_HEAD;
2779
2780 if (!nfserr) {
2781 RESERVE_SPACE(20);
2782 WRITECINFO(remove->rm_cinfo);
2783 ADJUST_ARGS();
2784 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002785 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
Benny Halevy695e12f2008-07-04 14:38:33 +03002788static __be32
Al Virob37ad282006-10-19 23:28:59 -07002789nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 ENCODE_HEAD;
2792
2793 if (!nfserr) {
2794 RESERVE_SPACE(40);
2795 WRITECINFO(rename->rn_sinfo);
2796 WRITECINFO(rename->rn_tinfo);
2797 ADJUST_ARGS();
2798 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002799 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
Benny Halevy695e12f2008-07-04 14:38:33 +03002802static __be32
Al Viroca5c8cd2007-07-26 17:33:49 +01002803nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamsondcb488a32007-07-17 04:04:51 -07002804 struct nfsd4_secinfo *secinfo)
2805{
2806 int i = 0;
2807 struct svc_export *exp = secinfo->si_exp;
J. Bruce Fields4796f452007-07-17 04:04:51 -07002808 u32 nflavs;
2809 struct exp_flavor_info *flavs;
2810 struct exp_flavor_info def_flavs[2];
Andy Adamsondcb488a32007-07-17 04:04:51 -07002811 ENCODE_HEAD;
2812
2813 if (nfserr)
2814 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07002815 if (exp->ex_nflavors) {
2816 flavs = exp->ex_flavors;
2817 nflavs = exp->ex_nflavors;
2818 } else { /* Handling of some defaults in absence of real secinfo: */
2819 flavs = def_flavs;
2820 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
2821 nflavs = 2;
2822 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
2823 flavs[1].pseudoflavor = RPC_AUTH_NULL;
2824 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
2825 nflavs = 1;
2826 flavs[0].pseudoflavor
2827 = svcauth_gss_flavor(exp->ex_client);
2828 } else {
2829 nflavs = 1;
2830 flavs[0].pseudoflavor
2831 = exp->ex_client->flavour->flavour;
2832 }
2833 }
2834
Andy Adamsondcb488a32007-07-17 04:04:51 -07002835 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07002836 WRITE32(nflavs);
Andy Adamsondcb488a32007-07-17 04:04:51 -07002837 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07002838 for (i = 0; i < nflavs; i++) {
2839 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a32007-07-17 04:04:51 -07002840 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
2841
2842 if (gm) {
2843 RESERVE_SPACE(4);
2844 WRITE32(RPC_AUTH_GSS);
2845 ADJUST_ARGS();
2846 RESERVE_SPACE(4 + gm->gm_oid.len);
2847 WRITE32(gm->gm_oid.len);
2848 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
2849 ADJUST_ARGS();
2850 RESERVE_SPACE(4);
2851 WRITE32(0); /* qop */
2852 ADJUST_ARGS();
2853 RESERVE_SPACE(4);
2854 WRITE32(gss_pseudoflavor_to_service(gm, flav));
2855 ADJUST_ARGS();
2856 gss_mech_put(gm);
2857 } else {
2858 RESERVE_SPACE(4);
2859 WRITE32(flav);
2860 ADJUST_ARGS();
2861 }
2862 }
2863out:
2864 if (exp)
2865 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03002866 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07002867}
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869/*
2870 * The SETATTR encode routine is special -- it always encodes a bitmap,
2871 * regardless of the error status.
2872 */
Benny Halevy695e12f2008-07-04 14:38:33 +03002873static __be32
Al Virob37ad282006-10-19 23:28:59 -07002874nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
2876 ENCODE_HEAD;
2877
2878 RESERVE_SPACE(12);
2879 if (nfserr) {
2880 WRITE32(2);
2881 WRITE32(0);
2882 WRITE32(0);
2883 }
2884 else {
2885 WRITE32(2);
2886 WRITE32(setattr->sa_bmval[0]);
2887 WRITE32(setattr->sa_bmval[1]);
2888 }
2889 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03002890 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
Benny Halevy695e12f2008-07-04 14:38:33 +03002893static __be32
Al Virob37ad282006-10-19 23:28:59 -07002894nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
2896 ENCODE_HEAD;
2897
2898 if (!nfserr) {
2899 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
2900 WRITEMEM(&scd->se_clientid, 8);
2901 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
2902 ADJUST_ARGS();
2903 }
2904 else if (nfserr == nfserr_clid_inuse) {
2905 RESERVE_SPACE(8);
2906 WRITE32(0);
2907 WRITE32(0);
2908 ADJUST_ARGS();
2909 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002910 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
Benny Halevy695e12f2008-07-04 14:38:33 +03002913static __be32
Al Virob37ad282006-10-19 23:28:59 -07002914nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 ENCODE_HEAD;
2917
2918 if (!nfserr) {
2919 RESERVE_SPACE(16);
2920 WRITE32(write->wr_bytes_written);
2921 WRITE32(write->wr_how_written);
2922 WRITEMEM(write->wr_verifier.data, 8);
2923 ADJUST_ARGS();
2924 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002925 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Benny Halevy695e12f2008-07-04 14:38:33 +03002928static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03002929nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
2930 struct nfsd4_exchange_id *exid)
2931{
Andy Adamson0733d212009-04-03 08:28:01 +03002932 ENCODE_HEAD;
2933 char *major_id;
2934 char *server_scope;
2935 int major_id_sz;
2936 int server_scope_sz;
2937 uint64_t minor_id = 0;
2938
2939 if (nfserr)
2940 return nfserr;
2941
2942 major_id = utsname()->nodename;
2943 major_id_sz = strlen(major_id);
2944 server_scope = utsname()->nodename;
2945 server_scope_sz = strlen(server_scope);
2946
2947 RESERVE_SPACE(
2948 8 /* eir_clientid */ +
2949 4 /* eir_sequenceid */ +
2950 4 /* eir_flags */ +
2951 4 /* spr_how (SP4_NONE) */ +
2952 8 /* so_minor_id */ +
2953 4 /* so_major_id.len */ +
2954 (XDR_QUADLEN(major_id_sz) * 4) +
2955 4 /* eir_server_scope.len */ +
2956 (XDR_QUADLEN(server_scope_sz) * 4) +
2957 4 /* eir_server_impl_id.count (0) */);
2958
2959 WRITEMEM(&exid->clientid, 8);
2960 WRITE32(exid->seqid);
2961 WRITE32(exid->flags);
2962
2963 /* state_protect4_r. Currently only support SP4_NONE */
2964 BUG_ON(exid->spa_how != SP4_NONE);
2965 WRITE32(exid->spa_how);
2966
2967 /* The server_owner struct */
2968 WRITE64(minor_id); /* Minor id */
2969 /* major id */
2970 WRITE32(major_id_sz);
2971 WRITEMEM(major_id, major_id_sz);
2972
2973 /* Server scope */
2974 WRITE32(server_scope_sz);
2975 WRITEMEM(server_scope, server_scope_sz);
2976
2977 /* Implementation id */
2978 WRITE32(0); /* zero length nfs_impl_id4 array */
2979 ADJUST_ARGS();
2980 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03002981}
2982
2983static __be32
2984nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
2985 struct nfsd4_create_session *sess)
2986{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002987 ENCODE_HEAD;
2988
2989 if (nfserr)
2990 return nfserr;
2991
2992 RESERVE_SPACE(24);
2993 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2994 WRITE32(sess->seqid);
2995 WRITE32(sess->flags);
2996 ADJUST_ARGS();
2997
2998 RESERVE_SPACE(28);
2999 WRITE32(0); /* headerpadsz */
3000 WRITE32(sess->fore_channel.maxreq_sz);
3001 WRITE32(sess->fore_channel.maxresp_sz);
3002 WRITE32(sess->fore_channel.maxresp_cached);
3003 WRITE32(sess->fore_channel.maxops);
3004 WRITE32(sess->fore_channel.maxreqs);
3005 WRITE32(sess->fore_channel.nr_rdma_attrs);
3006 ADJUST_ARGS();
3007
3008 if (sess->fore_channel.nr_rdma_attrs) {
3009 RESERVE_SPACE(4);
3010 WRITE32(sess->fore_channel.rdma_attrs);
3011 ADJUST_ARGS();
3012 }
3013
3014 RESERVE_SPACE(28);
3015 WRITE32(0); /* headerpadsz */
3016 WRITE32(sess->back_channel.maxreq_sz);
3017 WRITE32(sess->back_channel.maxresp_sz);
3018 WRITE32(sess->back_channel.maxresp_cached);
3019 WRITE32(sess->back_channel.maxops);
3020 WRITE32(sess->back_channel.maxreqs);
3021 WRITE32(sess->back_channel.nr_rdma_attrs);
3022 ADJUST_ARGS();
3023
3024 if (sess->back_channel.nr_rdma_attrs) {
3025 RESERVE_SPACE(4);
3026 WRITE32(sess->back_channel.rdma_attrs);
3027 ADJUST_ARGS();
3028 }
3029 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003030}
3031
3032static __be32
3033nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3034 struct nfsd4_destroy_session *destroy_session)
3035{
Andy Adamson2db134e2009-04-03 08:27:55 +03003036 return nfserr;
3037}
3038
Andy Adamsonbf864a32009-04-03 08:28:35 +03003039__be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003040nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3041 struct nfsd4_sequence *seq)
3042{
Benny Halevyb85d4c02009-04-03 08:28:08 +03003043 ENCODE_HEAD;
3044
3045 if (nfserr)
3046 return nfserr;
3047
3048 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3049 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3050 WRITE32(seq->seqid);
3051 WRITE32(seq->slotid);
3052 WRITE32(seq->maxslots);
3053 /*
3054 * FIXME: for now:
3055 * target_maxslots = maxslots
3056 * status_flags = 0
3057 */
3058 WRITE32(seq->maxslots);
3059 WRITE32(0);
3060
3061 ADJUST_ARGS();
3062 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003063}
3064
3065static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003066nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3067{
3068 return nfserr;
3069}
3070
3071typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3072
Andy Adamson2db134e2009-04-03 08:27:55 +03003073/*
3074 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3075 * since we don't need to filter out obsolete ops as this is
3076 * done in the decoding phase.
3077 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003078static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003079 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3080 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3081 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3082 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3083 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3084 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3085 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3086 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3087 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3088 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3089 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3090 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3091 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3092 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3093 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3094 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003095 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003096 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3097 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3098 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3099 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3100 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3101 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3102 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3103 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3104 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3105 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3106 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3107 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3108 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3109 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3110 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3111 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3112 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3113 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3114 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3115 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003116
3117 /* NFSv4.1 operations */
3118 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3119 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
3120 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3121 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3122 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3123 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
3124 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3125 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3126 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3127 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3128 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3129 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3130 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_noop,
3131 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3132 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3133 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
3134 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3135 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3136 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003137};
3138
Andy Adamson496c2622009-04-03 08:28:48 +03003139/*
3140 * Calculate the total amount of memory that the compound response has taken
3141 * after encoding the current operation.
3142 *
3143 * pad: add on 8 bytes for the next operation's op_code and status so that
3144 * there is room to cache a failure on the next operation.
3145 *
3146 * Compare this length to the session se_fmaxresp_cached.
3147 *
3148 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3149 * will be at least a page and will therefore hold the xdr_buf head.
3150 */
3151static int nfsd4_check_drc_limit(struct nfsd4_compoundres *resp)
3152{
3153 int status = 0;
3154 struct xdr_buf *xb = &resp->rqstp->rq_res;
3155 struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
3156 struct nfsd4_session *session = NULL;
3157 struct nfsd4_slot *slot = resp->cstate.slot;
3158 u32 length, tlen = 0, pad = 8;
3159
3160 if (!nfsd4_has_session(&resp->cstate))
3161 return status;
3162
3163 session = resp->cstate.session;
3164 if (session == NULL || slot->sl_cache_entry.ce_cachethis == 0)
3165 return status;
3166
3167 if (resp->opcnt >= args->opcnt)
3168 pad = 0; /* this is the last operation */
3169
3170 if (xb->page_len == 0) {
3171 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3172 } else {
3173 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3174 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3175
3176 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3177 }
3178 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3179 length, xb->page_len, tlen, pad);
3180
3181 if (length <= session->se_fmaxresp_cached)
3182 return status;
3183 else
3184 return nfserr_rep_too_big_to_cache;
3185}
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187void
3188nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3189{
Al Viro2ebbc012006-10-19 23:28:58 -07003190 __be32 *statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 ENCODE_HEAD;
3192
3193 RESERVE_SPACE(8);
3194 WRITE32(op->opnum);
3195 statp = p++; /* to be backfilled at the end */
3196 ADJUST_ARGS();
3197
Benny Halevy695e12f2008-07-04 14:38:33 +03003198 if (op->opnum == OP_ILLEGAL)
3199 goto status;
3200 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3201 !nfsd4_enc_ops[op->opnum]);
3202 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003203 /* nfsd4_check_drc_limit guarantees enough room for error status */
3204 if (!op->status && nfsd4_check_drc_limit(resp))
3205 op->status = nfserr_rep_too_big_to_cache;
Benny Halevy695e12f2008-07-04 14:38:33 +03003206status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 /*
3208 * Note: We write the status directly, instead of using WRITE32(),
3209 * since it is already in network byte order.
3210 */
3211 *statp = op->status;
3212}
3213
3214/*
3215 * Encode the reply stored in the stateowner reply cache
3216 *
3217 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3218 * previously sent already encoded operation.
3219 *
3220 * called with nfs4_lock_state() held
3221 */
3222void
3223nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3224{
3225 ENCODE_HEAD;
3226 struct nfs4_replay *rp = op->replay;
3227
3228 BUG_ON(!rp);
3229
3230 RESERVE_SPACE(8);
3231 WRITE32(op->opnum);
3232 *p++ = rp->rp_status; /* already xdr'ed */
3233 ADJUST_ARGS();
3234
3235 RESERVE_SPACE(rp->rp_buflen);
3236 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3237 ADJUST_ARGS();
3238}
3239
3240/*
3241 * END OF "GENERIC" ENCODE ROUTINES.
3242 */
3243
3244int
Al Viro2ebbc012006-10-19 23:28:58 -07003245nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
3247 return xdr_ressize_check(rqstp, p);
3248}
3249
3250void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
3251{
3252 if (args->ops != args->iops) {
3253 kfree(args->ops);
3254 args->ops = args->iops;
3255 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003256 kfree(args->tmpp);
3257 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 while (args->to_free) {
3259 struct tmpbuf *tb = args->to_free;
3260 args->to_free = tb->next;
3261 tb->release(tb->buf);
3262 kfree(tb);
3263 }
3264}
3265
3266int
Al Viro2ebbc012006-10-19 23:28:58 -07003267nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Al Virob37ad282006-10-19 23:28:59 -07003269 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 args->p = p;
3272 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3273 args->pagelist = rqstp->rq_arg.pages;
3274 args->pagelen = rqstp->rq_arg.page_len;
3275 args->tmpp = NULL;
3276 args->to_free = NULL;
3277 args->ops = args->iops;
3278 args->rqstp = rqstp;
3279
3280 status = nfsd4_decode_compound(args);
3281 if (status) {
3282 nfsd4_release_compoundargs(args);
3283 }
3284 return !status;
3285}
3286
3287int
Al Viro2ebbc012006-10-19 23:28:58 -07003288nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 /*
3291 * All that remains is to write the tag and operation count...
3292 */
3293 struct kvec *iov;
3294 p = resp->tagp;
3295 *p++ = htonl(resp->taglen);
3296 memcpy(p, resp->tag, resp->taglen);
3297 p += XDR_QUADLEN(resp->taglen);
3298 *p++ = htonl(resp->opcnt);
3299
3300 if (rqstp->rq_res.page_len)
3301 iov = &rqstp->rq_res.tail[0];
3302 else
3303 iov = &rqstp->rq_res.head[0];
3304 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3305 BUG_ON(iov->iov_len > PAGE_SIZE);
Andy Adamson66689582009-04-03 08:28:45 +03003306 if (nfsd4_has_session(&resp->cstate)) {
Andy Adamsonbf864a32009-04-03 08:28:35 +03003307 if (resp->cstate.status == nfserr_replay_cache &&
3308 !nfsd4_not_cached(resp)) {
Andy Adamsonda3846a2009-04-03 08:28:22 +03003309 iov->iov_len = resp->cstate.iovlen;
3310 } else {
3311 nfsd4_store_cache_entry(resp);
3312 dprintk("%s: SET SLOT STATE TO AVAILABLE\n", __func__);
3313 resp->cstate.slot->sl_inuse = 0;
3314 }
3315 if (resp->cstate.session)
3316 nfsd4_put_session(resp->cstate.session);
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return 1;
3319}
3320
3321/*
3322 * Local variables:
3323 * c-basic-offset: 8
3324 * End:
3325 */