blob: 70296fedee417ecf5a6d323cee1a858bf9989fe3 [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 Adamsondcb488a2007-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 Adamsondcb488a2007-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 }
Andy Adamson7e705702009-04-03 08:29:11 +03002151 BUG_ON(bmval2); /* FIXME: not implemented yet */
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2154 *countp = p - buffer;
2155 status = nfs_ok;
2156
2157out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002158 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (fhp == &tempfh)
2160 fh_put(&tempfh);
2161 return status;
2162out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002163 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto out;
2165out_resource:
2166 *countp = 0;
2167 status = nfserr_resource;
2168 goto out;
2169out_serverfault:
2170 status = nfserr_serverfault;
2171 goto out;
2172}
2173
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002174static inline int attributes_need_mount(u32 *bmval)
2175{
2176 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2177 return 1;
2178 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2179 return 1;
2180 return 0;
2181}
2182
Al Virob37ad282006-10-19 23:28:59 -07002183static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002185 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 struct svc_export *exp = cd->rd_fhp->fh_export;
2188 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002189 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002190 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2193 if (IS_ERR(dentry))
2194 return nfserrno(PTR_ERR(dentry));
2195
2196 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002197 /*
2198 * In the case of a mountpoint, the client may be asking for
2199 * attributes that are only properties of the underlying filesystem
2200 * as opposed to the cross-mounted file system. In such a case,
2201 * we will not follow the cross mount and will fill the attribtutes
2202 * directly from the mountpoint dentry.
2203 */
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002204 if (d_mountpoint(dentry) && !attributes_need_mount(cd->rd_bmval))
Frank Filz406a7ea2007-11-27 11:34:05 -08002205 ignore_crossmnt = 1;
2206 else if (d_mountpoint(dentry)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002207 int err;
2208
Andy Adamsondcb488a2007-07-17 04:04:51 -07002209 /*
2210 * Why the heck aren't we just using nfsd_lookup??
2211 * Different "."/".." handling? Something else?
2212 * At least, add a comment here to explain....
2213 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002214 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2215 if (err) {
2216 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto out_put;
2218 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002219 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2220 if (nfserr)
2221 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 }
2224 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002225 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226out_put:
2227 dput(dentry);
2228 exp_put(exp);
2229 return nfserr;
2230}
2231
Al Viro2ebbc012006-10-19 23:28:58 -07002232static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002233nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Al Viro2ebbc012006-10-19 23:28:58 -07002235 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 if (buflen < 6)
2238 return NULL;
2239 *p++ = htonl(2);
2240 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2241 *p++ = htonl(0); /* bmval1 */
2242
2243 attrlenp = p++;
2244 *p++ = nfserr; /* no htonl */
2245 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2246 return p;
2247}
2248
2249static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002250nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2251 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002253 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2255 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002256 __be32 *p = cd->buffer;
Al Virob37ad282006-10-19 23:28:59 -07002257 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /* In nfsv4, "." and ".." never make it onto the wire.. */
2260 if (name && isdotent(name, namlen)) {
2261 cd->common.err = nfs_ok;
2262 return 0;
2263 }
2264
2265 if (cd->offset)
2266 xdr_encode_hyper(cd->offset, (u64) offset);
2267
2268 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2269 if (buflen < 0)
2270 goto fail;
2271
2272 *p++ = xdr_one; /* mark entry present */
2273 cd->offset = p; /* remember pointer */
2274 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2275 p = xdr_encode_array(p, name, namlen); /* name length & name */
2276
2277 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2278 switch (nfserr) {
2279 case nfs_ok:
2280 p += buflen;
2281 break;
2282 case nfserr_resource:
2283 nfserr = nfserr_toosmall;
2284 goto fail;
2285 case nfserr_dropit:
2286 goto fail;
2287 default:
2288 /*
2289 * If the client requested the RDATTR_ERROR attribute,
2290 * we stuff the error code into this attribute
2291 * and continue. If this attribute was not requested,
2292 * then in accordance with the spec, we fail the
2293 * entire READDIR operation(!)
2294 */
2295 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2296 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002298 if (p == NULL) {
2299 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 cd->buflen -= (p - cd->buffer);
2304 cd->buffer = p;
2305 cd->common.err = nfs_ok;
2306 return 0;
2307fail:
2308 cd->common.err = nfserr;
2309 return -EINVAL;
2310}
2311
Benny Halevye2f282b2008-08-12 20:45:07 +03002312static void
2313nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2314{
2315 ENCODE_HEAD;
2316
2317 RESERVE_SPACE(sizeof(stateid_t));
2318 WRITE32(sid->si_generation);
2319 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2320 ADJUST_ARGS();
2321}
2322
Benny Halevy695e12f2008-07-04 14:38:33 +03002323static __be32
Al Virob37ad282006-10-19 23:28:59 -07002324nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 ENCODE_HEAD;
2327
2328 if (!nfserr) {
2329 RESERVE_SPACE(8);
2330 WRITE32(access->ac_supported);
2331 WRITE32(access->ac_resp_access);
2332 ADJUST_ARGS();
2333 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002334 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Benny Halevy695e12f2008-07-04 14:38:33 +03002337static __be32
Al Virob37ad282006-10-19 23:28:59 -07002338nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 ENCODE_SEQID_OP_HEAD;
2341
Benny Halevye2f282b2008-08-12 20:45:07 +03002342 if (!nfserr)
2343 nfsd4_encode_stateid(resp, &close->cl_stateid);
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002346 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
2349
Benny Halevy695e12f2008-07-04 14:38:33 +03002350static __be32
Al Virob37ad282006-10-19 23:28:59 -07002351nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 ENCODE_HEAD;
2354
2355 if (!nfserr) {
2356 RESERVE_SPACE(8);
2357 WRITEMEM(commit->co_verf.data, 8);
2358 ADJUST_ARGS();
2359 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002360 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Benny Halevy695e12f2008-07-04 14:38:33 +03002363static __be32
Al Virob37ad282006-10-19 23:28:59 -07002364nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 ENCODE_HEAD;
2367
2368 if (!nfserr) {
2369 RESERVE_SPACE(32);
2370 WRITECINFO(create->cr_cinfo);
2371 WRITE32(2);
2372 WRITE32(create->cr_bmval[0]);
2373 WRITE32(create->cr_bmval[1]);
2374 ADJUST_ARGS();
2375 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002376 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Al Virob37ad282006-10-19 23:28:59 -07002379static __be32
2380nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct svc_fh *fhp = getattr->ga_fhp;
2383 int buflen;
2384
2385 if (nfserr)
2386 return nfserr;
2387
2388 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2389 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2390 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002391 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (!nfserr)
2393 resp->p += buflen;
2394 return nfserr;
2395}
2396
Benny Halevy695e12f2008-07-04 14:38:33 +03002397static __be32
2398nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Benny Halevy695e12f2008-07-04 14:38:33 +03002400 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 unsigned int len;
2402 ENCODE_HEAD;
2403
2404 if (!nfserr) {
2405 len = fhp->fh_handle.fh_size;
2406 RESERVE_SPACE(len + 4);
2407 WRITE32(len);
2408 WRITEMEM(&fhp->fh_handle.fh_base, len);
2409 ADJUST_ARGS();
2410 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002411 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
2414/*
2415* Including all fields other than the name, a LOCK4denied structure requires
2416* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2417*/
2418static void
2419nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2420{
2421 ENCODE_HEAD;
2422
2423 RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
2424 WRITE64(ld->ld_start);
2425 WRITE64(ld->ld_length);
2426 WRITE32(ld->ld_type);
2427 if (ld->ld_sop) {
2428 WRITEMEM(&ld->ld_clientid, 8);
2429 WRITE32(ld->ld_sop->so_owner.len);
2430 WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
2431 kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
2432 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2433 WRITE64((u64)0); /* clientid */
2434 WRITE32(0); /* length of owner name */
2435 }
2436 ADJUST_ARGS();
2437}
2438
Benny Halevy695e12f2008-07-04 14:38:33 +03002439static __be32
Al Virob37ad282006-10-19 23:28:59 -07002440nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 ENCODE_SEQID_OP_HEAD;
2443
Benny Halevye2f282b2008-08-12 20:45:07 +03002444 if (!nfserr)
2445 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2446 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2448
J. Bruce Fields3a655882006-01-18 17:43:19 -08002449 ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002450 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Benny Halevy695e12f2008-07-04 14:38:33 +03002453static __be32
Al Virob37ad282006-10-19 23:28:59 -07002454nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 if (nfserr == nfserr_denied)
2457 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002458 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Benny Halevy695e12f2008-07-04 14:38:33 +03002461static __be32
Al Virob37ad282006-10-19 23:28:59 -07002462nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 ENCODE_SEQID_OP_HEAD;
2465
Benny Halevye2f282b2008-08-12 20:45:07 +03002466 if (!nfserr)
2467 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002470 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473
Benny Halevy695e12f2008-07-04 14:38:33 +03002474static __be32
Al Virob37ad282006-10-19 23:28:59 -07002475nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 ENCODE_HEAD;
2478
2479 if (!nfserr) {
2480 RESERVE_SPACE(20);
2481 WRITECINFO(link->li_cinfo);
2482 ADJUST_ARGS();
2483 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002484 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
2487
Benny Halevy695e12f2008-07-04 14:38:33 +03002488static __be32
Al Virob37ad282006-10-19 23:28:59 -07002489nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Benny Halevy1b6b2252008-08-12 20:45:28 +03002491 ENCODE_HEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 ENCODE_SEQID_OP_HEAD;
2493
2494 if (nfserr)
2495 goto out;
2496
Benny Halevye2f282b2008-08-12 20:45:07 +03002497 nfsd4_encode_stateid(resp, &open->op_stateid);
2498 RESERVE_SPACE(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 WRITECINFO(open->op_cinfo);
2500 WRITE32(open->op_rflags);
2501 WRITE32(2);
2502 WRITE32(open->op_bmval[0]);
2503 WRITE32(open->op_bmval[1]);
2504 WRITE32(open->op_delegate_type);
2505 ADJUST_ARGS();
2506
2507 switch (open->op_delegate_type) {
2508 case NFS4_OPEN_DELEGATE_NONE:
2509 break;
2510 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002511 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2512 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002513 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /*
2516 * TODO: ACE's in delegations
2517 */
2518 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2519 WRITE32(0);
2520 WRITE32(0);
2521 WRITE32(0); /* XXX: is NULL principal ok? */
2522 ADJUST_ARGS();
2523 break;
2524 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002525 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2526 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 WRITE32(0);
2528
2529 /*
2530 * TODO: space_limit's in delegations
2531 */
2532 WRITE32(NFS4_LIMIT_SIZE);
2533 WRITE32(~(u32)0);
2534 WRITE32(~(u32)0);
2535
2536 /*
2537 * TODO: ACE's in delegations
2538 */
2539 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2540 WRITE32(0);
2541 WRITE32(0);
2542 WRITE32(0); /* XXX: is NULL principal ok? */
2543 ADJUST_ARGS();
2544 break;
2545 default:
2546 BUG();
2547 }
2548 /* XXX save filehandle here */
2549out:
2550 ENCODE_SEQID_OP_TAIL(open->op_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002551 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Benny Halevy695e12f2008-07-04 14:38:33 +03002554static __be32
Al Virob37ad282006-10-19 23:28:59 -07002555nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002558
2559 if (!nfserr)
2560 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002563 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Benny Halevy695e12f2008-07-04 14:38:33 +03002566static __be32
Al Virob37ad282006-10-19 23:28:59 -07002567nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002570
2571 if (!nfserr)
2572 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 ENCODE_SEQID_OP_TAIL(od->od_stateowner);
Benny Halevy695e12f2008-07-04 14:38:33 +03002575 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Al Virob37ad282006-10-19 23:28:59 -07002578static __be32
2579nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002580 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 u32 eof;
2583 int v, pn;
2584 unsigned long maxcount;
2585 long len;
2586 ENCODE_HEAD;
2587
2588 if (nfserr)
2589 return nfserr;
2590 if (resp->xbuf->page_len)
2591 return nfserr_resource;
2592
2593 RESERVE_SPACE(8); /* eof flag and byte count */
2594
Greg Banks7adae482006-10-04 02:15:47 -07002595 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (maxcount > read->rd_length)
2597 maxcount = read->rd_length;
2598
2599 len = maxcount;
2600 v = 0;
2601 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -07002602 pn = resp->rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -07002603 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002604 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002605 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002606 len < PAGE_SIZE ? len : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 v++;
2608 len -= PAGE_SIZE;
2609 }
2610 read->rd_vlen = v;
2611
2612 nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002613 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 &maxcount);
2615
2616 if (nfserr == nfserr_symlink)
2617 nfserr = nfserr_inval;
2618 if (nfserr)
2619 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002620 eof = (read->rd_offset + maxcount >=
2621 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 WRITE32(eof);
2624 WRITE32(maxcount);
2625 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002626 resp->xbuf->head[0].iov_len = (char*)p
2627 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 resp->xbuf->page_len = maxcount;
2629
NeilBrown6ed6dec2006-04-10 22:55:32 -07002630 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002631 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002634 RESERVE_SPACE(4);
2635 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 resp->xbuf->tail[0].iov_base += maxcount&3;
2637 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002638 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640 return 0;
2641}
2642
Al Virob37ad282006-10-19 23:28:59 -07002643static __be32
2644nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
2646 int maxcount;
2647 char *page;
2648 ENCODE_HEAD;
2649
2650 if (nfserr)
2651 return nfserr;
2652 if (resp->xbuf->page_len)
2653 return nfserr_resource;
2654
NeilBrown44524352006-10-04 02:15:46 -07002655 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 maxcount = PAGE_SIZE;
2658 RESERVE_SPACE(4);
2659
2660 /*
2661 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2662 * if they would overflow the buffer. Is this kosher in NFSv4? If
2663 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2664 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2665 */
2666 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2667 if (nfserr == nfserr_isdir)
2668 return nfserr_inval;
2669 if (nfserr)
2670 return nfserr;
2671
2672 WRITE32(maxcount);
2673 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002674 resp->xbuf->head[0].iov_len = (char*)p
2675 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07002677
2678 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002679 resp->xbuf->tail[0].iov_base = p;
2680 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002682 RESERVE_SPACE(4);
2683 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 resp->xbuf->tail[0].iov_base += maxcount&3;
2685 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002686 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688 return 0;
2689}
2690
Al Virob37ad282006-10-19 23:28:59 -07002691static __be32
2692nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 int maxcount;
2695 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07002696 __be32 *page, *savep, *tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 ENCODE_HEAD;
2698
2699 if (nfserr)
2700 return nfserr;
2701 if (resp->xbuf->page_len)
2702 return nfserr_resource;
2703
2704 RESERVE_SPACE(8); /* verifier */
2705 savep = p;
2706
2707 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2708 WRITE32(0);
2709 WRITE32(0);
2710 ADJUST_ARGS();
2711 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002712 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 maxcount = PAGE_SIZE;
2715 if (maxcount > readdir->rd_maxcount)
2716 maxcount = readdir->rd_maxcount;
2717
2718 /*
2719 * Convert from bytes to words, account for the two words already
2720 * written, make sure to leave two words at the end for the next
2721 * pointer and eof field.
2722 */
2723 maxcount = (maxcount >> 2) - 4;
2724 if (maxcount < 0) {
2725 nfserr = nfserr_toosmall;
2726 goto err_no_verf;
2727 }
2728
NeilBrown44524352006-10-04 02:15:46 -07002729 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 readdir->common.err = 0;
2731 readdir->buflen = maxcount;
2732 readdir->buffer = page;
2733 readdir->offset = NULL;
2734
2735 offset = readdir->rd_cookie;
2736 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
2737 &offset,
2738 &readdir->common, nfsd4_encode_dirent);
2739 if (nfserr == nfs_ok &&
2740 readdir->common.err == nfserr_toosmall &&
2741 readdir->buffer == page)
2742 nfserr = nfserr_toosmall;
2743 if (nfserr == nfserr_symlink)
2744 nfserr = nfserr_notdir;
2745 if (nfserr)
2746 goto err_no_verf;
2747
2748 if (readdir->offset)
2749 xdr_encode_hyper(readdir->offset, offset);
2750
2751 p = readdir->buffer;
2752 *p++ = 0; /* no more entries */
2753 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07002754 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
2755 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
NeilBrownbb6e8a92006-04-10 22:55:33 -07002757 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07002758 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 resp->xbuf->tail[0].iov_len = 0;
2760 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002761 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 return 0;
2764err_no_verf:
2765 p = savep;
2766 ADJUST_ARGS();
2767 return nfserr;
2768}
2769
Benny Halevy695e12f2008-07-04 14:38:33 +03002770static __be32
Al Virob37ad282006-10-19 23:28:59 -07002771nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 ENCODE_HEAD;
2774
2775 if (!nfserr) {
2776 RESERVE_SPACE(20);
2777 WRITECINFO(remove->rm_cinfo);
2778 ADJUST_ARGS();
2779 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002780 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
Benny Halevy695e12f2008-07-04 14:38:33 +03002783static __be32
Al Virob37ad282006-10-19 23:28:59 -07002784nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
2786 ENCODE_HEAD;
2787
2788 if (!nfserr) {
2789 RESERVE_SPACE(40);
2790 WRITECINFO(rename->rn_sinfo);
2791 WRITECINFO(rename->rn_tinfo);
2792 ADJUST_ARGS();
2793 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002794 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
Benny Halevy695e12f2008-07-04 14:38:33 +03002797static __be32
Al Viroca5c8cd2007-07-26 17:33:49 +01002798nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamsondcb488a2007-07-17 04:04:51 -07002799 struct nfsd4_secinfo *secinfo)
2800{
2801 int i = 0;
2802 struct svc_export *exp = secinfo->si_exp;
J. Bruce Fields4796f452007-07-17 04:04:51 -07002803 u32 nflavs;
2804 struct exp_flavor_info *flavs;
2805 struct exp_flavor_info def_flavs[2];
Andy Adamsondcb488a2007-07-17 04:04:51 -07002806 ENCODE_HEAD;
2807
2808 if (nfserr)
2809 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07002810 if (exp->ex_nflavors) {
2811 flavs = exp->ex_flavors;
2812 nflavs = exp->ex_nflavors;
2813 } else { /* Handling of some defaults in absence of real secinfo: */
2814 flavs = def_flavs;
2815 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
2816 nflavs = 2;
2817 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
2818 flavs[1].pseudoflavor = RPC_AUTH_NULL;
2819 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
2820 nflavs = 1;
2821 flavs[0].pseudoflavor
2822 = svcauth_gss_flavor(exp->ex_client);
2823 } else {
2824 nflavs = 1;
2825 flavs[0].pseudoflavor
2826 = exp->ex_client->flavour->flavour;
2827 }
2828 }
2829
Andy Adamsondcb488a2007-07-17 04:04:51 -07002830 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07002831 WRITE32(nflavs);
Andy Adamsondcb488a2007-07-17 04:04:51 -07002832 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07002833 for (i = 0; i < nflavs; i++) {
2834 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a2007-07-17 04:04:51 -07002835 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
2836
2837 if (gm) {
2838 RESERVE_SPACE(4);
2839 WRITE32(RPC_AUTH_GSS);
2840 ADJUST_ARGS();
2841 RESERVE_SPACE(4 + gm->gm_oid.len);
2842 WRITE32(gm->gm_oid.len);
2843 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
2844 ADJUST_ARGS();
2845 RESERVE_SPACE(4);
2846 WRITE32(0); /* qop */
2847 ADJUST_ARGS();
2848 RESERVE_SPACE(4);
2849 WRITE32(gss_pseudoflavor_to_service(gm, flav));
2850 ADJUST_ARGS();
2851 gss_mech_put(gm);
2852 } else {
2853 RESERVE_SPACE(4);
2854 WRITE32(flav);
2855 ADJUST_ARGS();
2856 }
2857 }
2858out:
2859 if (exp)
2860 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03002861 return nfserr;
Andy Adamsondcb488a2007-07-17 04:04:51 -07002862}
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864/*
2865 * The SETATTR encode routine is special -- it always encodes a bitmap,
2866 * regardless of the error status.
2867 */
Benny Halevy695e12f2008-07-04 14:38:33 +03002868static __be32
Al Virob37ad282006-10-19 23:28:59 -07002869nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
2871 ENCODE_HEAD;
2872
2873 RESERVE_SPACE(12);
2874 if (nfserr) {
2875 WRITE32(2);
2876 WRITE32(0);
2877 WRITE32(0);
2878 }
2879 else {
2880 WRITE32(2);
2881 WRITE32(setattr->sa_bmval[0]);
2882 WRITE32(setattr->sa_bmval[1]);
2883 }
2884 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03002885 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
Benny Halevy695e12f2008-07-04 14:38:33 +03002888static __be32
Al Virob37ad282006-10-19 23:28:59 -07002889nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 ENCODE_HEAD;
2892
2893 if (!nfserr) {
2894 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
2895 WRITEMEM(&scd->se_clientid, 8);
2896 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
2897 ADJUST_ARGS();
2898 }
2899 else if (nfserr == nfserr_clid_inuse) {
2900 RESERVE_SPACE(8);
2901 WRITE32(0);
2902 WRITE32(0);
2903 ADJUST_ARGS();
2904 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002905 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
Benny Halevy695e12f2008-07-04 14:38:33 +03002908static __be32
Al Virob37ad282006-10-19 23:28:59 -07002909nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 ENCODE_HEAD;
2912
2913 if (!nfserr) {
2914 RESERVE_SPACE(16);
2915 WRITE32(write->wr_bytes_written);
2916 WRITE32(write->wr_how_written);
2917 WRITEMEM(write->wr_verifier.data, 8);
2918 ADJUST_ARGS();
2919 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002920 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Benny Halevy695e12f2008-07-04 14:38:33 +03002923static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03002924nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
2925 struct nfsd4_exchange_id *exid)
2926{
Andy Adamson0733d212009-04-03 08:28:01 +03002927 ENCODE_HEAD;
2928 char *major_id;
2929 char *server_scope;
2930 int major_id_sz;
2931 int server_scope_sz;
2932 uint64_t minor_id = 0;
2933
2934 if (nfserr)
2935 return nfserr;
2936
2937 major_id = utsname()->nodename;
2938 major_id_sz = strlen(major_id);
2939 server_scope = utsname()->nodename;
2940 server_scope_sz = strlen(server_scope);
2941
2942 RESERVE_SPACE(
2943 8 /* eir_clientid */ +
2944 4 /* eir_sequenceid */ +
2945 4 /* eir_flags */ +
2946 4 /* spr_how (SP4_NONE) */ +
2947 8 /* so_minor_id */ +
2948 4 /* so_major_id.len */ +
2949 (XDR_QUADLEN(major_id_sz) * 4) +
2950 4 /* eir_server_scope.len */ +
2951 (XDR_QUADLEN(server_scope_sz) * 4) +
2952 4 /* eir_server_impl_id.count (0) */);
2953
2954 WRITEMEM(&exid->clientid, 8);
2955 WRITE32(exid->seqid);
2956 WRITE32(exid->flags);
2957
2958 /* state_protect4_r. Currently only support SP4_NONE */
2959 BUG_ON(exid->spa_how != SP4_NONE);
2960 WRITE32(exid->spa_how);
2961
2962 /* The server_owner struct */
2963 WRITE64(minor_id); /* Minor id */
2964 /* major id */
2965 WRITE32(major_id_sz);
2966 WRITEMEM(major_id, major_id_sz);
2967
2968 /* Server scope */
2969 WRITE32(server_scope_sz);
2970 WRITEMEM(server_scope, server_scope_sz);
2971
2972 /* Implementation id */
2973 WRITE32(0); /* zero length nfs_impl_id4 array */
2974 ADJUST_ARGS();
2975 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03002976}
2977
2978static __be32
2979nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
2980 struct nfsd4_create_session *sess)
2981{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002982 ENCODE_HEAD;
2983
2984 if (nfserr)
2985 return nfserr;
2986
2987 RESERVE_SPACE(24);
2988 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2989 WRITE32(sess->seqid);
2990 WRITE32(sess->flags);
2991 ADJUST_ARGS();
2992
2993 RESERVE_SPACE(28);
2994 WRITE32(0); /* headerpadsz */
2995 WRITE32(sess->fore_channel.maxreq_sz);
2996 WRITE32(sess->fore_channel.maxresp_sz);
2997 WRITE32(sess->fore_channel.maxresp_cached);
2998 WRITE32(sess->fore_channel.maxops);
2999 WRITE32(sess->fore_channel.maxreqs);
3000 WRITE32(sess->fore_channel.nr_rdma_attrs);
3001 ADJUST_ARGS();
3002
3003 if (sess->fore_channel.nr_rdma_attrs) {
3004 RESERVE_SPACE(4);
3005 WRITE32(sess->fore_channel.rdma_attrs);
3006 ADJUST_ARGS();
3007 }
3008
3009 RESERVE_SPACE(28);
3010 WRITE32(0); /* headerpadsz */
3011 WRITE32(sess->back_channel.maxreq_sz);
3012 WRITE32(sess->back_channel.maxresp_sz);
3013 WRITE32(sess->back_channel.maxresp_cached);
3014 WRITE32(sess->back_channel.maxops);
3015 WRITE32(sess->back_channel.maxreqs);
3016 WRITE32(sess->back_channel.nr_rdma_attrs);
3017 ADJUST_ARGS();
3018
3019 if (sess->back_channel.nr_rdma_attrs) {
3020 RESERVE_SPACE(4);
3021 WRITE32(sess->back_channel.rdma_attrs);
3022 ADJUST_ARGS();
3023 }
3024 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003025}
3026
3027static __be32
3028nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3029 struct nfsd4_destroy_session *destroy_session)
3030{
Andy Adamson2db134e2009-04-03 08:27:55 +03003031 return nfserr;
3032}
3033
Andy Adamsonbf864a32009-04-03 08:28:35 +03003034__be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003035nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3036 struct nfsd4_sequence *seq)
3037{
Benny Halevyb85d4c02009-04-03 08:28:08 +03003038 ENCODE_HEAD;
3039
3040 if (nfserr)
3041 return nfserr;
3042
3043 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3044 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3045 WRITE32(seq->seqid);
3046 WRITE32(seq->slotid);
3047 WRITE32(seq->maxslots);
3048 /*
3049 * FIXME: for now:
3050 * target_maxslots = maxslots
3051 * status_flags = 0
3052 */
3053 WRITE32(seq->maxslots);
3054 WRITE32(0);
3055
3056 ADJUST_ARGS();
3057 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003058}
3059
3060static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003061nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3062{
3063 return nfserr;
3064}
3065
3066typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3067
Andy Adamson2db134e2009-04-03 08:27:55 +03003068/*
3069 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3070 * since we don't need to filter out obsolete ops as this is
3071 * done in the decoding phase.
3072 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003073static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003074 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3075 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3076 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3077 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3078 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3079 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3080 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3081 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3082 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3083 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3084 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3085 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3086 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3087 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3088 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3089 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003090 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003091 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3092 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3093 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3094 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3095 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3096 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3097 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3098 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3099 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3100 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3101 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3102 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3103 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3104 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3105 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3106 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3107 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3108 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3109 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3110 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003111
3112 /* NFSv4.1 operations */
3113 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3114 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
3115 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3116 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3117 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3118 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
3119 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3120 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3121 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3122 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3123 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3124 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3125 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_noop,
3126 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3127 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3128 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
3129 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3130 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3131 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003132};
3133
Andy Adamson496c2622009-04-03 08:28:48 +03003134/*
3135 * Calculate the total amount of memory that the compound response has taken
3136 * after encoding the current operation.
3137 *
3138 * pad: add on 8 bytes for the next operation's op_code and status so that
3139 * there is room to cache a failure on the next operation.
3140 *
3141 * Compare this length to the session se_fmaxresp_cached.
3142 *
3143 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3144 * will be at least a page and will therefore hold the xdr_buf head.
3145 */
3146static int nfsd4_check_drc_limit(struct nfsd4_compoundres *resp)
3147{
3148 int status = 0;
3149 struct xdr_buf *xb = &resp->rqstp->rq_res;
3150 struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
3151 struct nfsd4_session *session = NULL;
3152 struct nfsd4_slot *slot = resp->cstate.slot;
3153 u32 length, tlen = 0, pad = 8;
3154
3155 if (!nfsd4_has_session(&resp->cstate))
3156 return status;
3157
3158 session = resp->cstate.session;
3159 if (session == NULL || slot->sl_cache_entry.ce_cachethis == 0)
3160 return status;
3161
3162 if (resp->opcnt >= args->opcnt)
3163 pad = 0; /* this is the last operation */
3164
3165 if (xb->page_len == 0) {
3166 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3167 } else {
3168 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3169 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3170
3171 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3172 }
3173 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3174 length, xb->page_len, tlen, pad);
3175
3176 if (length <= session->se_fmaxresp_cached)
3177 return status;
3178 else
3179 return nfserr_rep_too_big_to_cache;
3180}
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182void
3183nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3184{
Al Viro2ebbc012006-10-19 23:28:58 -07003185 __be32 *statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 ENCODE_HEAD;
3187
3188 RESERVE_SPACE(8);
3189 WRITE32(op->opnum);
3190 statp = p++; /* to be backfilled at the end */
3191 ADJUST_ARGS();
3192
Benny Halevy695e12f2008-07-04 14:38:33 +03003193 if (op->opnum == OP_ILLEGAL)
3194 goto status;
3195 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3196 !nfsd4_enc_ops[op->opnum]);
3197 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003198 /* nfsd4_check_drc_limit guarantees enough room for error status */
3199 if (!op->status && nfsd4_check_drc_limit(resp))
3200 op->status = nfserr_rep_too_big_to_cache;
Benny Halevy695e12f2008-07-04 14:38:33 +03003201status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 /*
3203 * Note: We write the status directly, instead of using WRITE32(),
3204 * since it is already in network byte order.
3205 */
3206 *statp = op->status;
3207}
3208
3209/*
3210 * Encode the reply stored in the stateowner reply cache
3211 *
3212 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3213 * previously sent already encoded operation.
3214 *
3215 * called with nfs4_lock_state() held
3216 */
3217void
3218nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3219{
3220 ENCODE_HEAD;
3221 struct nfs4_replay *rp = op->replay;
3222
3223 BUG_ON(!rp);
3224
3225 RESERVE_SPACE(8);
3226 WRITE32(op->opnum);
3227 *p++ = rp->rp_status; /* already xdr'ed */
3228 ADJUST_ARGS();
3229
3230 RESERVE_SPACE(rp->rp_buflen);
3231 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3232 ADJUST_ARGS();
3233}
3234
3235/*
3236 * END OF "GENERIC" ENCODE ROUTINES.
3237 */
3238
3239int
Al Viro2ebbc012006-10-19 23:28:58 -07003240nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
3242 return xdr_ressize_check(rqstp, p);
3243}
3244
3245void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
3246{
3247 if (args->ops != args->iops) {
3248 kfree(args->ops);
3249 args->ops = args->iops;
3250 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003251 kfree(args->tmpp);
3252 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 while (args->to_free) {
3254 struct tmpbuf *tb = args->to_free;
3255 args->to_free = tb->next;
3256 tb->release(tb->buf);
3257 kfree(tb);
3258 }
3259}
3260
3261int
Al Viro2ebbc012006-10-19 23:28:58 -07003262nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Al Virob37ad282006-10-19 23:28:59 -07003264 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 args->p = p;
3267 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3268 args->pagelist = rqstp->rq_arg.pages;
3269 args->pagelen = rqstp->rq_arg.page_len;
3270 args->tmpp = NULL;
3271 args->to_free = NULL;
3272 args->ops = args->iops;
3273 args->rqstp = rqstp;
3274
3275 status = nfsd4_decode_compound(args);
3276 if (status) {
3277 nfsd4_release_compoundargs(args);
3278 }
3279 return !status;
3280}
3281
3282int
Al Viro2ebbc012006-10-19 23:28:58 -07003283nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 /*
3286 * All that remains is to write the tag and operation count...
3287 */
3288 struct kvec *iov;
3289 p = resp->tagp;
3290 *p++ = htonl(resp->taglen);
3291 memcpy(p, resp->tag, resp->taglen);
3292 p += XDR_QUADLEN(resp->taglen);
3293 *p++ = htonl(resp->opcnt);
3294
3295 if (rqstp->rq_res.page_len)
3296 iov = &rqstp->rq_res.tail[0];
3297 else
3298 iov = &rqstp->rq_res.head[0];
3299 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3300 BUG_ON(iov->iov_len > PAGE_SIZE);
Andy Adamson66689582009-04-03 08:28:45 +03003301 if (nfsd4_has_session(&resp->cstate)) {
Andy Adamsonbf864a32009-04-03 08:28:35 +03003302 if (resp->cstate.status == nfserr_replay_cache &&
3303 !nfsd4_not_cached(resp)) {
Andy Adamsonda3846a2009-04-03 08:28:22 +03003304 iov->iov_len = resp->cstate.iovlen;
3305 } else {
3306 nfsd4_store_cache_entry(resp);
3307 dprintk("%s: SET SLOT STATE TO AVAILABLE\n", __func__);
3308 resp->cstate.slot->sl_inuse = 0;
3309 }
3310 if (resp->cstate.session)
3311 nfsd4_put_session(resp->cstate.session);
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return 1;
3314}
3315
3316/*
3317 * Local variables:
3318 * c-basic-offset: 8
3319 * End:
3320 */