blob: d9d7fa94967f89beef97614c28f967f21ff7b88a [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020045#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030046#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040047#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070048#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020049
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050050#include "idmap.h"
51#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020052#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050053#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040054#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050055#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030056#include "netns.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050057
David Quigley18032ca2013-05-02 13:19:10 -040058#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59#include <linux/security.h>
60#endif
61
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define NFSDDBG_FACILITY NFSDDBG_XDR
64
J.Bruce Fields42ca0992006-10-04 02:16:20 -070065/*
66 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
67 * directory in order to indicate to the client that a filesystem boundary is present
68 * We use a fixed fsid for a referral
69 */
70#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
72
Al Virob37ad282006-10-19 23:28:59 -070073static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050074check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int i;
77
78 if (len == 0)
79 return nfserr_inval;
80 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050081 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 for (i = 0; i < len; i++)
83 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050084 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070089 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070090 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DECODE_TAIL \
92 status = 0; \
93out: \
94 return status; \
95xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -040096 dprintk("NFSD: xdr error (%s:%d)\n", \
97 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 status = nfserr_bad_xdr; \
99 goto out
100
101#define READ32(x) (x) = ntohl(*p++)
102#define READ64(x) do { \
103 (x) = (u64)ntohl(*p++) << 32; \
104 (x) |= ntohl(*p++); \
105} while (0)
106#define READTIME(x) do { \
107 p++; \
108 (x) = ntohl(*p++); \
109 p++; \
110} while (0)
111#define READMEM(x,nbytes) do { \
112 x = (char *)p; \
113 p += XDR_QUADLEN(nbytes); \
114} while (0)
115#define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
118 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto xdr_error; \
122 } \
123 p += XDR_QUADLEN(nbytes); \
124} while (0)
125#define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
128} while (0)
129
130/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131#define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
133 p = argp->p; \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto xdr_error; \
139 } \
140} while (0)
141
J. Bruce Fields590b7432013-06-21 17:06:29 -0400142static void next_decode_page(struct nfsd4_compoundargs *argp)
143{
144 argp->pagelist++;
145 argp->p = page_address(argp->pagelist[0]);
146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + (argp->pagelen>>2);
148 argp->pagelen = 0;
149 } else {
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
152 }
153}
154
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500155static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
159 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500160 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700161 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (avail + argp->pagelen < nbytes)
163 return NULL;
164 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
165 return NULL;
166 /* ok, we can do it with the current plus the next page */
167 if (nbytes <= sizeof(argp->tmp))
168 p = argp->tmp;
169 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800170 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
172 if (!p)
173 return NULL;
174
175 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500176 /*
177 * The following memcpy is safe because read_buf is always
178 * called with nbytes > avail, and the two cases above both
179 * guarantee p points to at least nbytes bytes.
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 memcpy(p, argp->p, avail);
J. Bruce Fields590b7432013-06-21 17:06:29 -0400182 next_decode_page(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
184 argp->p += XDR_QUADLEN(nbytes - avail);
185 return p;
186}
187
Andy Adamson60adfc52009-04-03 08:28:50 +0300188static int zero_clientid(clientid_t *clid)
189{
190 return (clid->cl_boot == 0) && (clid->cl_id == 0);
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int
194defer_free(struct nfsd4_compoundargs *argp,
195 void (*release)(const void *), void *p)
196{
197 struct tmpbuf *tb;
198
199 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
200 if (!tb)
201 return -ENOMEM;
202 tb->buf = p;
203 tb->release = release;
204 tb->next = argp->to_free;
205 argp->to_free = tb;
206 return 0;
207}
208
Al Viro2ebbc012006-10-19 23:28:58 -0700209static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (p == argp->tmp) {
Thomas Meyer67114fe2011-11-17 23:43:40 +0100212 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800213 if (!p)
214 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200216 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 argp->tmpp = NULL;
218 }
219 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800220 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return NULL;
222 } else
223 return (char *)p;
224}
225
Al Virob37ad282006-10-19 23:28:59 -0700226static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
228{
229 u32 bmlen;
230 DECODE_HEAD;
231
232 bmval[0] = 0;
233 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300234 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 READ_BUF(4);
237 READ32(bmlen);
238 if (bmlen > 1000)
239 goto xdr_error;
240
241 READ_BUF(bmlen << 2);
242 if (bmlen > 0)
243 READ32(bmval[0]);
244 if (bmlen > 1)
245 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300246 if (bmlen > 2)
247 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 DECODE_TAIL;
250}
251
Al Virob37ad282006-10-19 23:28:59 -0700252static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800253nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400254 struct iattr *iattr, struct nfs4_acl **acl,
255 struct xdr_netobj *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 int expected_len, len = 0;
258 u32 dummy32;
259 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700260 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 DECODE_HEAD;
263 iattr->ia_valid = 0;
264 if ((status = nfsd4_decode_bitmap(argp, bmval)))
265 return status;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 READ_BUF(4);
268 READ32(expected_len);
269
270 if (bmval[0] & FATTR4_WORD0_SIZE) {
271 READ_BUF(8);
272 len += 8;
273 READ64(iattr->ia_size);
274 iattr->ia_valid |= ATTR_SIZE;
275 }
276 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields64a817c2013-03-26 14:11:13 -0400277 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800278 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 READ_BUF(4); len += 4;
281 READ32(nace);
282
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800283 if (nace > NFS4_ACL_MAX)
284 return nfserr_resource;
285
286 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700288 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto out_nfserr;
290 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800291 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800293 (*acl)->naces = nace;
294 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800296 READ32(ace->type);
297 READ32(ace->flag);
298 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 READ32(dummy32);
300 READ_BUF(dummy32);
301 len += XDR_QUADLEN(dummy32) << 2;
302 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800303 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500304 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800305 if (ace->whotype != NFS4_ACL_WHO_NAMED)
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800306 ;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800307 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500308 status = nfsd_map_name_to_gid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800309 buf, dummy32, &ace->who_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500311 status = nfsd_map_name_to_uid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800312 buf, dummy32, &ace->who_uid);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500313 if (status)
314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 } else
317 *acl = NULL;
318 if (bmval[1] & FATTR4_WORD1_MODE) {
319 READ_BUF(4);
320 len += 4;
321 READ32(iattr->ia_mode);
322 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
323 iattr->ia_valid |= ATTR_MODE;
324 }
325 if (bmval[1] & FATTR4_WORD1_OWNER) {
326 READ_BUF(4);
327 len += 4;
328 READ32(dummy32);
329 READ_BUF(dummy32);
330 len += (XDR_QUADLEN(dummy32) << 2);
331 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100332 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
333 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 iattr->ia_valid |= ATTR_UID;
335 }
336 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
337 READ_BUF(4);
338 len += 4;
339 READ32(dummy32);
340 READ_BUF(dummy32);
341 len += (XDR_QUADLEN(dummy32) << 2);
342 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100343 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
344 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 iattr->ia_valid |= ATTR_GID;
346 }
347 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
348 READ_BUF(4);
349 len += 4;
350 READ32(dummy32);
351 switch (dummy32) {
352 case NFS4_SET_TO_CLIENT_TIME:
353 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
354 all 32 bits of 'nseconds'. */
355 READ_BUF(12);
356 len += 12;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -0400357 READ64(iattr->ia_atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 READ32(iattr->ia_atime.tv_nsec);
359 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
360 return nfserr_inval;
361 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
362 break;
363 case NFS4_SET_TO_SERVER_TIME:
364 iattr->ia_valid |= ATTR_ATIME;
365 break;
366 default:
367 goto xdr_error;
368 }
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
371 READ_BUF(4);
372 len += 4;
373 READ32(dummy32);
374 switch (dummy32) {
375 case NFS4_SET_TO_CLIENT_TIME:
376 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
377 all 32 bits of 'nseconds'. */
378 READ_BUF(12);
379 len += 12;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -0400380 READ64(iattr->ia_mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 READ32(iattr->ia_mtime.tv_nsec);
382 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
383 return nfserr_inval;
384 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
385 break;
386 case NFS4_SET_TO_SERVER_TIME:
387 iattr->ia_valid |= ATTR_MTIME;
388 break;
389 default:
390 goto xdr_error;
391 }
392 }
David Quigley18032ca2013-05-02 13:19:10 -0400393
394 label->len = 0;
395#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
396 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
397 READ_BUF(4);
398 len += 4;
399 READ32(dummy32); /* lfs: we don't use it */
400 READ_BUF(4);
401 len += 4;
402 READ32(dummy32); /* pi: we don't use it either */
403 READ_BUF(4);
404 len += 4;
405 READ32(dummy32);
406 READ_BUF(dummy32);
407 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
408 return nfserr_badlabel;
409 len += (XDR_QUADLEN(dummy32) << 2);
410 READMEM(buf, dummy32);
411 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
412 if (!label->data)
413 return nfserr_jukebox;
J. Bruce Fields3378b7f2013-11-01 10:42:15 -0400414 label->len = dummy32;
David Quigley18032ca2013-05-02 13:19:10 -0400415 defer_free(argp, kfree, label->data);
416 memcpy(label->data, buf, dummy32);
417 }
418#endif
419
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800420 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
421 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
422 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
423 READ_BUF(expected_len - len);
424 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto xdr_error;
426
427 DECODE_TAIL;
428
429out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700430 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto out;
432}
433
Al Virob37ad282006-10-19 23:28:59 -0700434static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300435nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
436{
437 DECODE_HEAD;
438
439 READ_BUF(sizeof(stateid_t));
440 READ32(sid->si_generation);
441 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
442
443 DECODE_TAIL;
444}
445
446static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
448{
449 DECODE_HEAD;
450
451 READ_BUF(4);
452 READ32(access->ac_req_access);
453
454 DECODE_TAIL;
455}
456
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400457static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
458{
459 DECODE_HEAD;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500460 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400461 char *machine_name;
462 int i;
463 int nr_secflavs;
464
465 /* callback_sec_params4 */
466 READ_BUF(4);
467 READ32(nr_secflavs);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400468 if (nr_secflavs)
469 cbs->flavor = (u32)(-1);
470 else
471 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
472 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400473 for (i = 0; i < nr_secflavs; ++i) {
474 READ_BUF(4);
475 READ32(dummy);
476 switch (dummy) {
477 case RPC_AUTH_NULL:
478 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500479 if (cbs->flavor == (u32)(-1))
480 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400481 break;
482 case RPC_AUTH_UNIX:
483 READ_BUF(8);
484 /* stamp */
485 READ32(dummy);
486
487 /* machine name */
488 READ32(dummy);
489 READ_BUF(dummy);
490 SAVEMEM(machine_name, dummy);
491
492 /* uid, gid */
493 READ_BUF(8);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500494 READ32(uid);
495 READ32(gid);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400496
497 /* more gids */
498 READ_BUF(4);
499 READ32(dummy);
500 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500501 if (cbs->flavor == (u32)(-1)) {
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800502 kuid_t kuid = make_kuid(&init_user_ns, uid);
503 kgid_t kgid = make_kgid(&init_user_ns, gid);
504 if (uid_valid(kuid) && gid_valid(kgid)) {
505 cbs->uid = kuid;
506 cbs->gid = kgid;
507 cbs->flavor = RPC_AUTH_UNIX;
508 } else {
509 dprintk("RPC_AUTH_UNIX with invalid"
510 "uid or gid ignoring!\n");
511 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500512 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400513 break;
514 case RPC_AUTH_GSS:
515 dprintk("RPC_AUTH_GSS callback secflavor "
516 "not supported!\n");
517 READ_BUF(8);
518 /* gcbp_service */
519 READ32(dummy);
520 /* gcbp_handle_from_server */
521 READ32(dummy);
522 READ_BUF(dummy);
523 p += XDR_QUADLEN(dummy);
524 /* gcbp_handle_from_client */
525 READ_BUF(4);
526 READ32(dummy);
527 READ_BUF(dummy);
528 break;
529 default:
530 dprintk("Illegal callback secflavor\n");
531 return nfserr_inval;
532 }
533 }
534 DECODE_TAIL;
535}
536
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400537static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
538{
539 DECODE_HEAD;
540
541 READ_BUF(4);
542 READ32(bc->bc_cb_program);
543 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
544
545 DECODE_TAIL;
546}
547
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400548static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
549{
550 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400551
552 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
553 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
554 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400555 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
556 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400557 DECODE_TAIL;
558}
559
Al Virob37ad282006-10-19 23:28:59 -0700560static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
562{
563 DECODE_HEAD;
564
Benny Halevye31a1b62008-08-12 20:46:18 +0300565 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300567 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 DECODE_TAIL;
570}
571
572
Al Virob37ad282006-10-19 23:28:59 -0700573static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
575{
576 DECODE_HEAD;
577
578 READ_BUF(12);
579 READ64(commit->co_offset);
580 READ32(commit->co_count);
581
582 DECODE_TAIL;
583}
584
Al Virob37ad282006-10-19 23:28:59 -0700585static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
587{
588 DECODE_HEAD;
589
590 READ_BUF(4);
591 READ32(create->cr_type);
592 switch (create->cr_type) {
593 case NF4LNK:
594 READ_BUF(4);
595 READ32(create->cr_linklen);
596 READ_BUF(create->cr_linklen);
597 SAVEMEM(create->cr_linkname, create->cr_linklen);
598 break;
599 case NF4BLK:
600 case NF4CHR:
601 READ_BUF(8);
602 READ32(create->cr_specdata1);
603 READ32(create->cr_specdata2);
604 break;
605 case NF4SOCK:
606 case NF4FIFO:
607 case NF4DIR:
608 default:
609 break;
610 }
611
612 READ_BUF(4);
613 READ32(create->cr_namelen);
614 READ_BUF(create->cr_namelen);
615 SAVEMEM(create->cr_name, create->cr_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500616 if ((status = check_filename(create->cr_name, create->cr_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return status;
618
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800619 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
David Quigley18032ca2013-05-02 13:19:10 -0400620 &create->cr_acl, &create->cr_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300621 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto out;
623
624 DECODE_TAIL;
625}
626
Al Virob37ad282006-10-19 23:28:59 -0700627static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
629{
Benny Halevye31a1b62008-08-12 20:46:18 +0300630 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Al Virob37ad282006-10-19 23:28:59 -0700633static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
635{
636 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
637}
638
Al Virob37ad282006-10-19 23:28:59 -0700639static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
641{
642 DECODE_HEAD;
643
644 READ_BUF(4);
645 READ32(link->li_namelen);
646 READ_BUF(link->li_namelen);
647 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500648 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return status;
650
651 DECODE_TAIL;
652}
653
Al Virob37ad282006-10-19 23:28:59 -0700654static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
656{
657 DECODE_HEAD;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
661 */
662 READ_BUF(28);
663 READ32(lock->lk_type);
664 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
665 goto xdr_error;
666 READ32(lock->lk_reclaim);
667 READ64(lock->lk_offset);
668 READ64(lock->lk_length);
669 READ32(lock->lk_is_new);
670
671 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300672 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300674 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
675 if (status)
676 return status;
677 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 READ32(lock->lk_new_lock_seqid);
679 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
680 READ32(lock->lk_new_owner.len);
681 READ_BUF(lock->lk_new_owner.len);
682 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
683 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300684 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
685 if (status)
686 return status;
687 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 READ32(lock->lk_old_lock_seqid);
689 }
690
691 DECODE_TAIL;
692}
693
Al Virob37ad282006-10-19 23:28:59 -0700694static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
696{
697 DECODE_HEAD;
698
699 READ_BUF(32);
700 READ32(lockt->lt_type);
701 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
702 goto xdr_error;
703 READ64(lockt->lt_offset);
704 READ64(lockt->lt_length);
705 COPYMEM(&lockt->lt_clientid, 8);
706 READ32(lockt->lt_owner.len);
707 READ_BUF(lockt->lt_owner.len);
708 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
709
710 DECODE_TAIL;
711}
712
Al Virob37ad282006-10-19 23:28:59 -0700713static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
715{
716 DECODE_HEAD;
717
Benny Halevye31a1b62008-08-12 20:46:18 +0300718 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 READ32(locku->lu_type);
720 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
721 goto xdr_error;
722 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300723 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
724 if (status)
725 return status;
726 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 READ64(locku->lu_offset);
728 READ64(locku->lu_length);
729
730 DECODE_TAIL;
731}
732
Al Virob37ad282006-10-19 23:28:59 -0700733static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
735{
736 DECODE_HEAD;
737
738 READ_BUF(4);
739 READ32(lookup->lo_len);
740 READ_BUF(lookup->lo_len);
741 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500742 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return status;
744
745 DECODE_TAIL;
746}
747
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200748static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400749{
750 __be32 *p;
751 u32 w;
752
753 READ_BUF(4);
754 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200755 *share_access = w & NFS4_SHARE_ACCESS_MASK;
756 *deleg_want = w & NFS4_SHARE_WANT_MASK;
757 if (deleg_when)
758 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
759
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400760 switch (w & NFS4_SHARE_ACCESS_MASK) {
761 case NFS4_SHARE_ACCESS_READ:
762 case NFS4_SHARE_ACCESS_WRITE:
763 case NFS4_SHARE_ACCESS_BOTH:
764 break;
765 default:
766 return nfserr_bad_xdr;
767 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200768 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400769 if (!w)
770 return nfs_ok;
771 if (!argp->minorversion)
772 return nfserr_bad_xdr;
773 switch (w & NFS4_SHARE_WANT_MASK) {
774 case NFS4_SHARE_WANT_NO_PREFERENCE:
775 case NFS4_SHARE_WANT_READ_DELEG:
776 case NFS4_SHARE_WANT_WRITE_DELEG:
777 case NFS4_SHARE_WANT_ANY_DELEG:
778 case NFS4_SHARE_WANT_NO_DELEG:
779 case NFS4_SHARE_WANT_CANCEL:
780 break;
781 default:
782 return nfserr_bad_xdr;
783 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700784 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400785 if (!w)
786 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200787
788 if (!deleg_when) /* open_downgrade */
789 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400790 switch (w) {
791 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
792 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700793 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
794 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400795 return nfs_ok;
796 }
797xdr_error:
798 return nfserr_bad_xdr;
799}
800
801static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
802{
803 __be32 *p;
804
805 READ_BUF(4);
806 READ32(*x);
807 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4afa2011-10-17 10:41:17 +0300808 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400809 return nfserr_bad_xdr;
810 return nfs_ok;
811xdr_error:
812 return nfserr_bad_xdr;
813}
814
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400815static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
816{
817 __be32 *p;
818
819 READ_BUF(4);
820 READ32(o->len);
821
822 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
823 return nfserr_bad_xdr;
824
825 READ_BUF(o->len);
826 SAVEMEM(o->data, o->len);
827 return nfs_ok;
828xdr_error:
829 return nfserr_bad_xdr;
830}
831
Al Virob37ad282006-10-19 23:28:59 -0700832static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
834{
835 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200836 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 memset(open->op_bmval, 0, sizeof(open->op_bmval));
839 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400840 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800842 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400844 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200846 /* decode, yet ignore deleg_when until supported */
847 status = nfsd4_decode_share_access(argp, &open->op_share_access,
848 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400849 if (status)
850 goto xdr_error;
851 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
852 if (status)
853 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400854 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400856 status = nfsd4_decode_opaque(argp, &open->op_owner);
857 if (status)
858 goto xdr_error;
859 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 READ32(open->op_create);
861 switch (open->op_create) {
862 case NFS4_OPEN_NOCREATE:
863 break;
864 case NFS4_OPEN_CREATE:
865 READ_BUF(4);
866 READ32(open->op_createmode);
867 switch (open->op_createmode) {
868 case NFS4_CREATE_UNCHECKED:
869 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300870 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400871 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300872 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto out;
874 break;
875 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500876 READ_BUF(NFS4_VERIFIER_SIZE);
877 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300879 case NFS4_CREATE_EXCLUSIVE4_1:
880 if (argp->minorversion < 1)
881 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500882 READ_BUF(NFS4_VERIFIER_SIZE);
883 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300884 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400885 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300886 if (status)
887 goto out;
888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 default:
890 goto xdr_error;
891 }
892 break;
893 default:
894 goto xdr_error;
895 }
896
897 /* open_claim */
898 READ_BUF(4);
899 READ32(open->op_claim_type);
900 switch (open->op_claim_type) {
901 case NFS4_OPEN_CLAIM_NULL:
902 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
903 READ_BUF(4);
904 READ32(open->op_fname.len);
905 READ_BUF(open->op_fname.len);
906 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500907 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return status;
909 break;
910 case NFS4_OPEN_CLAIM_PREVIOUS:
911 READ_BUF(4);
912 READ32(open->op_delegate_type);
913 break;
914 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300915 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
916 if (status)
917 return status;
918 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 READ32(open->op_fname.len);
920 READ_BUF(open->op_fname.len);
921 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500922 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return status;
924 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400925 case NFS4_OPEN_CLAIM_FH:
926 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
927 if (argp->minorversion < 1)
928 goto xdr_error;
929 /* void */
930 break;
931 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
932 if (argp->minorversion < 1)
933 goto xdr_error;
934 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
935 if (status)
936 return status;
937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 default:
939 goto xdr_error;
940 }
941
942 DECODE_TAIL;
943}
944
Al Virob37ad282006-10-19 23:28:59 -0700945static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
947{
948 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400949
950 if (argp->minorversion >= 1)
951 return nfserr_notsupp;
952
Benny Halevye31a1b62008-08-12 20:46:18 +0300953 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
954 if (status)
955 return status;
956 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 READ32(open_conf->oc_seqid);
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 DECODE_TAIL;
960}
961
Al Virob37ad282006-10-19 23:28:59 -0700962static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
964{
965 DECODE_HEAD;
966
Benny Halevye31a1b62008-08-12 20:46:18 +0300967 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
968 if (status)
969 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400970 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200972 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
973 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400974 if (status)
975 return status;
976 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
977 if (status)
978 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 DECODE_TAIL;
980}
981
Al Virob37ad282006-10-19 23:28:59 -0700982static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
984{
985 DECODE_HEAD;
986
987 READ_BUF(4);
988 READ32(putfh->pf_fhlen);
989 if (putfh->pf_fhlen > NFS4_FHSIZE)
990 goto xdr_error;
991 READ_BUF(putfh->pf_fhlen);
992 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
993
994 DECODE_TAIL;
995}
996
Al Virob37ad282006-10-19 23:28:59 -0700997static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400998nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
999{
1000 if (argp->minorversion == 0)
1001 return nfs_ok;
1002 return nfserr_notsupp;
1003}
1004
1005static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1007{
1008 DECODE_HEAD;
1009
Benny Halevye31a1b62008-08-12 20:46:18 +03001010 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1011 if (status)
1012 return status;
1013 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 READ64(read->rd_offset);
1015 READ32(read->rd_length);
1016
1017 DECODE_TAIL;
1018}
1019
Al Virob37ad282006-10-19 23:28:59 -07001020static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1022{
1023 DECODE_HEAD;
1024
1025 READ_BUF(24);
1026 READ64(readdir->rd_cookie);
1027 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1028 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1029 READ32(readdir->rd_maxcount);
1030 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1031 goto out;
1032
1033 DECODE_TAIL;
1034}
1035
Al Virob37ad282006-10-19 23:28:59 -07001036static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1038{
1039 DECODE_HEAD;
1040
1041 READ_BUF(4);
1042 READ32(remove->rm_namelen);
1043 READ_BUF(remove->rm_namelen);
1044 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001045 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return status;
1047
1048 DECODE_TAIL;
1049}
1050
Al Virob37ad282006-10-19 23:28:59 -07001051static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1053{
1054 DECODE_HEAD;
1055
1056 READ_BUF(4);
1057 READ32(rename->rn_snamelen);
1058 READ_BUF(rename->rn_snamelen + 4);
1059 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1060 READ32(rename->rn_tnamelen);
1061 READ_BUF(rename->rn_tnamelen);
1062 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001063 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001065 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return status;
1067
1068 DECODE_TAIL;
1069}
1070
Al Virob37ad282006-10-19 23:28:59 -07001071static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1073{
1074 DECODE_HEAD;
1075
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001076 if (argp->minorversion >= 1)
1077 return nfserr_notsupp;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 READ_BUF(sizeof(clientid_t));
1080 COPYMEM(clientid, sizeof(clientid_t));
1081
1082 DECODE_TAIL;
1083}
1084
Al Virob37ad282006-10-19 23:28:59 -07001085static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001086nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1087 struct nfsd4_secinfo *secinfo)
1088{
1089 DECODE_HEAD;
1090
1091 READ_BUF(4);
1092 READ32(secinfo->si_namelen);
1093 READ_BUF(secinfo->si_namelen);
1094 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001095 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001096 if (status)
1097 return status;
1098 DECODE_TAIL;
1099}
1100
1101static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001102nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1103 struct nfsd4_secinfo_no_name *sin)
1104{
1105 DECODE_HEAD;
1106
1107 READ_BUF(4);
1108 READ32(sin->sin_style);
1109 DECODE_TAIL;
1110}
1111
1112static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1114{
Benny Halevye31a1b62008-08-12 20:46:18 +03001115 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Benny Halevye31a1b62008-08-12 20:46:18 +03001117 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1118 if (status)
1119 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001120 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
David Quigley18032ca2013-05-02 13:19:10 -04001121 &setattr->sa_acl, &setattr->sa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Al Virob37ad282006-10-19 23:28:59 -07001124static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1126{
1127 DECODE_HEAD;
1128
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001129 if (argp->minorversion >= 1)
1130 return nfserr_notsupp;
1131
Chuck Leverab4684d2012-03-02 17:13:50 -05001132 READ_BUF(NFS4_VERIFIER_SIZE);
1133 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001135 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1136 if (status)
1137 return nfserr_bad_xdr;
1138 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 READ32(setclientid->se_callback_prog);
1140 READ32(setclientid->se_callback_netid_len);
1141
1142 READ_BUF(setclientid->se_callback_netid_len + 4);
1143 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1144 READ32(setclientid->se_callback_addr_len);
1145
1146 READ_BUF(setclientid->se_callback_addr_len + 4);
1147 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1148 READ32(setclientid->se_callback_ident);
1149
1150 DECODE_TAIL;
1151}
1152
Al Virob37ad282006-10-19 23:28:59 -07001153static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1155{
1156 DECODE_HEAD;
1157
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001158 if (argp->minorversion >= 1)
1159 return nfserr_notsupp;
1160
Chuck Leverab4684d2012-03-02 17:13:50 -05001161 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001163 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 DECODE_TAIL;
1166}
1167
1168/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001169static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 DECODE_HEAD;
1173
1174 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1175 goto out;
1176
1177 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001178 * nfsd4_proc_verify */
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 READ_BUF(4);
1181 READ32(verify->ve_attrlen);
1182 READ_BUF(verify->ve_attrlen);
1183 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1184
1185 DECODE_TAIL;
1186}
1187
Al Virob37ad282006-10-19 23:28:59 -07001188static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1190{
1191 int avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int len;
1193 DECODE_HEAD;
1194
Benny Halevye31a1b62008-08-12 20:46:18 +03001195 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1196 if (status)
1197 return status;
1198 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 READ64(write->wr_offset);
1200 READ32(write->wr_stable_how);
1201 if (write->wr_stable_how > 2)
1202 goto xdr_error;
1203 READ32(write->wr_buflen);
1204
1205 /* Sorry .. no magic macros for this.. *
1206 * READ_BUF(write->wr_buflen);
1207 * SAVEMEM(write->wr_buf, write->wr_buflen);
1208 */
1209 avail = (char*)argp->end - (char*)argp->p;
1210 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001211 dprintk("NFSD: xdr error (%s:%d)\n",
1212 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 goto xdr_error;
1214 }
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001215 write->wr_head.iov_base = p;
1216 write->wr_head.iov_len = avail;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001217 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001218 write->wr_pagelist = argp->pagelist;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001219
1220 len = XDR_QUADLEN(write->wr_buflen) << 2;
1221 if (len >= avail) {
1222 int pages;
1223
1224 len -= avail;
1225
1226 pages = len >> PAGE_SHIFT;
1227 argp->pagelist += pages;
1228 argp->pagelen -= pages * PAGE_SIZE;
1229 len -= pages * PAGE_SIZE;
1230
1231 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1232 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001234 argp->p += XDR_QUADLEN(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 DECODE_TAIL;
1237}
1238
Al Virob37ad282006-10-19 23:28:59 -07001239static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1241{
1242 DECODE_HEAD;
1243
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001244 if (argp->minorversion >= 1)
1245 return nfserr_notsupp;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 READ_BUF(12);
1248 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1249 READ32(rlockowner->rl_owner.len);
1250 READ_BUF(rlockowner->rl_owner.len);
1251 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1252
Andy Adamson60adfc52009-04-03 08:28:50 +03001253 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1254 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 DECODE_TAIL;
1256}
1257
Al Virob37ad282006-10-19 23:28:59 -07001258static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001259nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001260 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001261{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001262 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001263 DECODE_HEAD;
1264
1265 READ_BUF(NFS4_VERIFIER_SIZE);
1266 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1267
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001268 status = nfsd4_decode_opaque(argp, &exid->clname);
1269 if (status)
1270 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001271
1272 READ_BUF(4);
1273 READ32(exid->flags);
1274
1275 /* Ignore state_protect4_a */
1276 READ_BUF(4);
1277 READ32(exid->spa_how);
1278 switch (exid->spa_how) {
1279 case SP4_NONE:
1280 break;
1281 case SP4_MACH_CRED:
1282 /* spo_must_enforce */
1283 READ_BUF(4);
1284 READ32(dummy);
1285 READ_BUF(dummy * 4);
1286 p += dummy;
1287
1288 /* spo_must_allow */
1289 READ_BUF(4);
1290 READ32(dummy);
1291 READ_BUF(dummy * 4);
1292 p += dummy;
1293 break;
1294 case SP4_SSV:
1295 /* ssp_ops */
1296 READ_BUF(4);
1297 READ32(dummy);
1298 READ_BUF(dummy * 4);
1299 p += dummy;
1300
1301 READ_BUF(4);
1302 READ32(dummy);
1303 READ_BUF(dummy * 4);
1304 p += dummy;
1305
1306 /* ssp_hash_algs<> */
1307 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001308 READ32(tmp);
1309 while (tmp--) {
1310 READ_BUF(4);
1311 READ32(dummy);
1312 READ_BUF(dummy);
1313 p += XDR_QUADLEN(dummy);
1314 }
Andy Adamson0733d212009-04-03 08:28:01 +03001315
1316 /* ssp_encr_algs<> */
1317 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001318 READ32(tmp);
1319 while (tmp--) {
1320 READ_BUF(4);
1321 READ32(dummy);
1322 READ_BUF(dummy);
1323 p += XDR_QUADLEN(dummy);
1324 }
Andy Adamson0733d212009-04-03 08:28:01 +03001325
1326 /* ssp_window and ssp_num_gss_handles */
1327 READ_BUF(8);
1328 READ32(dummy);
1329 READ32(dummy);
1330 break;
1331 default:
1332 goto xdr_error;
1333 }
1334
1335 /* Ignore Implementation ID */
1336 READ_BUF(4); /* nfs_impl_id4 array length */
1337 READ32(dummy);
1338
1339 if (dummy > 1)
1340 goto xdr_error;
1341
1342 if (dummy == 1) {
1343 /* nii_domain */
1344 READ_BUF(4);
1345 READ32(dummy);
1346 READ_BUF(dummy);
1347 p += XDR_QUADLEN(dummy);
1348
1349 /* nii_name */
1350 READ_BUF(4);
1351 READ32(dummy);
1352 READ_BUF(dummy);
1353 p += XDR_QUADLEN(dummy);
1354
1355 /* nii_date */
1356 READ_BUF(12);
1357 p += 3;
1358 }
1359 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001360}
1361
1362static __be32
1363nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1364 struct nfsd4_create_session *sess)
1365{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001366 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001367 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001368
1369 READ_BUF(16);
1370 COPYMEM(&sess->clientid, 8);
1371 READ32(sess->seqid);
1372 READ32(sess->flags);
1373
1374 /* Fore channel attrs */
1375 READ_BUF(28);
1376 READ32(dummy); /* headerpadsz is always 0 */
1377 READ32(sess->fore_channel.maxreq_sz);
1378 READ32(sess->fore_channel.maxresp_sz);
1379 READ32(sess->fore_channel.maxresp_cached);
1380 READ32(sess->fore_channel.maxops);
1381 READ32(sess->fore_channel.maxreqs);
1382 READ32(sess->fore_channel.nr_rdma_attrs);
1383 if (sess->fore_channel.nr_rdma_attrs == 1) {
1384 READ_BUF(4);
1385 READ32(sess->fore_channel.rdma_attrs);
1386 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1387 dprintk("Too many fore channel attr bitmaps!\n");
1388 goto xdr_error;
1389 }
1390
1391 /* Back channel attrs */
1392 READ_BUF(28);
1393 READ32(dummy); /* headerpadsz is always 0 */
1394 READ32(sess->back_channel.maxreq_sz);
1395 READ32(sess->back_channel.maxresp_sz);
1396 READ32(sess->back_channel.maxresp_cached);
1397 READ32(sess->back_channel.maxops);
1398 READ32(sess->back_channel.maxreqs);
1399 READ32(sess->back_channel.nr_rdma_attrs);
1400 if (sess->back_channel.nr_rdma_attrs == 1) {
1401 READ_BUF(4);
1402 READ32(sess->back_channel.rdma_attrs);
1403 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1404 dprintk("Too many back channel attr bitmaps!\n");
1405 goto xdr_error;
1406 }
1407
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001408 READ_BUF(4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001409 READ32(sess->callback_prog);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001410 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001411 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001412}
1413
1414static __be32
1415nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1416 struct nfsd4_destroy_session *destroy_session)
1417{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001418 DECODE_HEAD;
1419 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1420 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1421
1422 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001423}
1424
1425static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001426nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1427 struct nfsd4_free_stateid *free_stateid)
1428{
1429 DECODE_HEAD;
1430
1431 READ_BUF(sizeof(stateid_t));
1432 READ32(free_stateid->fr_stateid.si_generation);
1433 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1434
1435 DECODE_TAIL;
1436}
1437
1438static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001439nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1440 struct nfsd4_sequence *seq)
1441{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001442 DECODE_HEAD;
1443
1444 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1445 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1446 READ32(seq->seqid);
1447 READ32(seq->slotid);
1448 READ32(seq->maxslots);
1449 READ32(seq->cachethis);
1450
1451 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001452}
1453
Bryan Schumaker17456802011-07-13 10:50:48 -04001454static __be32
1455nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1456{
Bryan Schumaker17456802011-07-13 10:50:48 -04001457 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001458 __be32 *p, status;
1459 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001460
1461 READ_BUF(4);
1462 test_stateid->ts_num_ids = ntohl(*p++);
1463
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001464 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001465
1466 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001467 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1468 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001469 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001470 goto out;
1471 }
1472
1473 defer_free(argp, kfree, stateid);
1474 INIT_LIST_HEAD(&stateid->ts_id_list);
1475 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1476
1477 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001478 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001479 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001480 }
1481
1482 status = 0;
1483out:
1484 return status;
1485xdr_error:
1486 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1487 status = nfserr_bad_xdr;
1488 goto out;
1489}
1490
Mi Jinlong345c2842011-10-20 17:51:39 +08001491static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1492{
1493 DECODE_HEAD;
1494
1495 READ_BUF(8);
1496 COPYMEM(&dc->clientid, 8);
1497
1498 DECODE_TAIL;
1499}
1500
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001501static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1502{
1503 DECODE_HEAD;
1504
1505 READ_BUF(4);
1506 READ32(rc->rca_one_fs);
1507
1508 DECODE_TAIL;
1509}
1510
Andy Adamson2db134e2009-04-03 08:27:55 +03001511static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001512nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1513{
1514 return nfs_ok;
1515}
1516
Benny Halevy3c375c62008-07-02 11:14:01 +03001517static __be32
1518nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1519{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001520 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001521}
1522
Benny Halevy347e0ad2008-07-02 11:13:41 +03001523typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1524
1525static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001526 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1527 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1528 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1529 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1530 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1531 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1532 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1533 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1534 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1535 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1536 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1537 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1538 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1539 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1540 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1541 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1542 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1543 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1544 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1545 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001546 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001547 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1548 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1549 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1550 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1551 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1552 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1553 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1554 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1555 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1556 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1557 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1558 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1559 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1560 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1561 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1562 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03001563
1564 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001565 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001566 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001567 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1568 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1569 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001570 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001571 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1572 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1573 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1574 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1575 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1576 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001577 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001578 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1579 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001580 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001581 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001582 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001583 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001584};
1585
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001586static inline bool
1587nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1588{
Anna Schumaker8217d142013-10-30 13:38:13 -04001589 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001590 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001591 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001592 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001593 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1594 return false;
1595 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001596 return false;
1597 return true;
1598}
Benny Halevyf2feb962008-07-02 11:14:22 +03001599
Benny Halevy347e0ad2008-07-02 11:13:41 +03001600static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1602{
1603 DECODE_HEAD;
1604 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05001605 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int i;
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 READ_BUF(4);
1609 READ32(argp->taglen);
1610 READ_BUF(argp->taglen + 8);
1611 SAVEMEM(argp->tag, argp->taglen);
1612 READ32(argp->minorversion);
1613 READ32(argp->opcnt);
1614
1615 if (argp->taglen > NFSD4_MAX_TAGLEN)
1616 goto xdr_error;
1617 if (argp->opcnt > 100)
1618 goto xdr_error;
1619
Tobias Klausere8c96f82006-03-24 03:15:34 -08001620 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1622 if (!argp->ops) {
1623 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001624 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto xdr_error;
1626 }
1627 }
1628
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001629 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03001630 argp->opcnt = 0;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 for (i = 0; i < argp->opcnt; i++) {
1633 op = &argp->ops[i];
1634 op->replay = NULL;
1635
J. Bruce Fields8a61b182012-11-16 22:28:38 -05001636 READ_BUF(4);
1637 READ32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001639 if (nfsd4_opnum_in_range(argp, op))
1640 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001641 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 op->opnum = OP_ILLEGAL;
1643 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
1646 if (op->status) {
1647 argp->opcnt = i+1;
1648 break;
1649 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001650 /*
1651 * We'll try to cache the result in the DRC if any one
1652 * op in the compound wants to be cached:
1653 */
1654 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001656 /* Sessions make the DRC unnecessary: */
1657 if (argp->minorversion)
1658 cachethis = false;
1659 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 DECODE_TAIL;
1662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664#define WRITE32(n) *p++ = htonl(n)
1665#define WRITE64(n) do { \
1666 *p++ = htonl((u32)((n) >> 32)); \
1667 *p++ = htonl((u32)(n)); \
1668} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001669#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1671 memcpy(p, ptr, nbytes); \
1672 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001673}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001674
1675static void write32(__be32 **p, u32 n)
1676{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001677 *(*p)++ = htonl(n);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001678}
1679
1680static void write64(__be32 **p, u64 n)
1681{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001682 write32(p, (n >> 32));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001683 write32(p, (u32)n);
1684}
1685
1686static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1687{
1688 if (IS_I_VERSION(inode)) {
1689 write64(p, inode->i_version);
1690 } else {
1691 write32(p, stat->ctime.tv_sec);
1692 write32(p, stat->ctime.tv_nsec);
1693 }
1694}
1695
1696static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1697{
1698 write32(p, c->atomic);
1699 if (c->change_supported) {
1700 write64(p, c->before_change);
1701 write64(p, c->after_change);
1702 } else {
1703 write32(p, c->before_ctime_sec);
1704 write32(p, c->before_ctime_nsec);
1705 write32(p, c->after_ctime_sec);
1706 write32(p, c->after_ctime_nsec);
1707 }
1708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710#define RESERVE_SPACE(nbytes) do { \
1711 p = resp->p; \
1712 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1713} while (0)
1714#define ADJUST_ARGS() resp->p = p
1715
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001716/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001717 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001718 */
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001719static __be32 nfsd4_encode_components_esc(char sep, char *components,
1720 __be32 **pp, int *buflen,
1721 char esc_enter, char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001722{
Al Viro2ebbc012006-10-19 23:28:58 -07001723 __be32 *p = *pp;
1724 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001725 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001726 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001727
1728 dprintk("nfsd4_encode_components(%s)\n", components);
1729 if ((*buflen -= 4) < 0)
1730 return nfserr_resource;
1731 WRITE32(0); /* We will fill this in with @count later */
1732 end = str = components;
1733 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001734 bool found_esc = false;
1735
1736 /* try to parse as esc_start, ..., esc_end, sep */
1737 if (*str == esc_enter) {
1738 for (; *end && (*end != esc_exit); end++)
1739 /* find esc_exit or end of string */;
1740 next = end + 1;
1741 if (*end && (!*next || *next == sep)) {
1742 str++;
1743 found_esc = true;
1744 }
1745 }
1746
1747 if (!found_esc)
1748 for (; *end && (*end != sep); end++)
1749 /* find sep or end of string */;
1750
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001751 strlen = end - str;
1752 if (strlen) {
1753 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1754 return nfserr_resource;
1755 WRITE32(strlen);
1756 WRITEMEM(str, strlen);
1757 count++;
1758 }
1759 else
1760 end++;
1761 str = end;
1762 }
1763 *pp = p;
1764 p = countp;
1765 WRITE32(count);
1766 return 0;
1767}
1768
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001769/* Encode as an array of strings the string given with components
1770 * separated @sep.
1771 */
1772static __be32 nfsd4_encode_components(char sep, char *components,
1773 __be32 **pp, int *buflen)
1774{
1775 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1776}
1777
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001778/*
1779 * encode a location element of a fs_locations structure
1780 */
Al Virob37ad282006-10-19 23:28:59 -07001781static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001782 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001783{
Al Virob37ad282006-10-19 23:28:59 -07001784 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001785 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001786
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001787 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1788 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001789 if (status)
1790 return status;
1791 status = nfsd4_encode_components('/', location->path, &p, buflen);
1792 if (status)
1793 return status;
1794 *pp = p;
1795 return 0;
1796}
1797
1798/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001799 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001800 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001801static __be32 nfsd4_encode_path(const struct path *root,
1802 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001803{
Al Viro301f0262013-09-01 16:06:39 -04001804 struct path cur = *path;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001805 __be32 *p = *pp;
1806 struct dentry **components = NULL;
1807 unsigned int ncomponents = 0;
1808 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001809
Trond Myklebusted748aa2011-09-12 19:37:06 -04001810 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001811
Trond Myklebusted748aa2011-09-12 19:37:06 -04001812 path_get(&cur);
1813 /* First walk the path up to the nfsd root, and store the
1814 * dentries/path components in an array.
1815 */
1816 for (;;) {
1817 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1818 break;
1819 if (cur.dentry == cur.mnt->mnt_root) {
1820 if (follow_up(&cur))
1821 continue;
1822 goto out_free;
1823 }
1824 if ((ncomponents & 15) == 0) {
1825 struct dentry **new;
1826 new = krealloc(components,
1827 sizeof(*new) * (ncomponents + 16),
1828 GFP_KERNEL);
1829 if (!new)
1830 goto out_free;
1831 components = new;
1832 }
1833 components[ncomponents++] = cur.dentry;
1834 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001835 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001836
1837 *buflen -= 4;
1838 if (*buflen < 0)
1839 goto out_free;
1840 WRITE32(ncomponents);
1841
1842 while (ncomponents) {
1843 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04001844 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001845
Al Viro301f0262013-09-01 16:06:39 -04001846 spin_lock(&dentry->d_lock);
1847 len = dentry->d_name.len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001848 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
Al Viro301f0262013-09-01 16:06:39 -04001849 if (*buflen < 0) {
1850 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001851 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04001852 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001853 WRITE32(len);
1854 WRITEMEM(dentry->d_name.name, len);
1855 dprintk("/%s", dentry->d_name.name);
Al Viro301f0262013-09-01 16:06:39 -04001856 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001857 dput(dentry);
1858 ncomponents--;
1859 }
1860
1861 *pp = p;
1862 err = 0;
1863out_free:
1864 dprintk(")\n");
1865 while (ncomponents)
1866 dput(components[--ncomponents]);
1867 kfree(components);
1868 path_put(&cur);
1869 return err;
1870}
1871
1872static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1873 const struct path *path, __be32 **pp, int *buflen)
1874{
1875 struct svc_export *exp_ps;
1876 __be32 res;
1877
1878 exp_ps = rqst_find_fsidzero_export(rqstp);
1879 if (IS_ERR(exp_ps))
1880 return nfserrno(PTR_ERR(exp_ps));
1881 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1882 exp_put(exp_ps);
1883 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001884}
1885
1886/*
1887 * encode a fs_locations structure
1888 */
Al Virob37ad282006-10-19 23:28:59 -07001889static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001890 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001891 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001892{
Al Virob37ad282006-10-19 23:28:59 -07001893 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001894 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001895 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001896 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001897
Trond Myklebusted748aa2011-09-12 19:37:06 -04001898 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001899 if (status)
1900 return status;
1901 if ((*buflen -= 4) < 0)
1902 return nfserr_resource;
1903 WRITE32(fslocs->locations_count);
1904 for (i=0; i<fslocs->locations_count; i++) {
1905 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1906 &p, buflen);
1907 if (status)
1908 return status;
1909 }
1910 *pp = p;
1911 return 0;
1912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001914static u32 nfs4_file_type(umode_t mode)
1915{
1916 switch (mode & S_IFMT) {
1917 case S_IFIFO: return NF4FIFO;
1918 case S_IFCHR: return NF4CHR;
1919 case S_IFDIR: return NF4DIR;
1920 case S_IFBLK: return NF4BLK;
1921 case S_IFLNK: return NF4LNK;
1922 case S_IFREG: return NF4REG;
1923 case S_IFSOCK: return NF4SOCK;
1924 default: return NF4BAD;
1925 };
1926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Al Virob37ad282006-10-19 23:28:59 -07001928static __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001929nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
Al Viro2ebbc012006-10-19 23:28:58 -07001930 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 int status;
1933
1934 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1935 return nfserr_resource;
1936 if (whotype != NFS4_ACL_WHO_NAMED)
1937 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001938 else if (gid_valid(gid))
1939 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 else
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001941 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (status < 0)
1943 return nfserrno(status);
1944 *p = xdr_encode_opaque(*p, NULL, status);
1945 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1946 BUG_ON(*buflen < 0);
1947 return 0;
1948}
1949
Al Virob37ad282006-10-19 23:28:59 -07001950static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001951nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001953 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1954 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Al Virob37ad282006-10-19 23:28:59 -07001957static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001958nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001960 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1961 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Al Virob37ad282006-10-19 23:28:59 -07001964static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001965nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
Al Viro2ebbc012006-10-19 23:28:58 -07001966 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001968 kuid_t uid = INVALID_UID;
1969 kgid_t gid = INVALID_GID;
1970
1971 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1972 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1973 gid = ace->who_gid;
1974 else
1975 uid = ace->who_uid;
1976 }
1977 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001980#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1981 FATTR4_WORD0_RDATTR_ERROR)
1982#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1983
David Quigley18032ca2013-05-02 13:19:10 -04001984#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1985static inline __be32
1986nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
1987{
1988 __be32 *p = *pp;
1989
1990 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
1991 return nfserr_resource;
1992
1993 /*
1994 * For now we use a 0 here to indicate the null translation; in
1995 * the future we may place a call to translation code here.
1996 */
1997 if ((*buflen -= 8) < 0)
1998 return nfserr_resource;
1999
2000 WRITE32(0); /* lfs */
2001 WRITE32(0); /* pi */
2002 p = xdr_encode_opaque(p, context, len);
2003 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2004
2005 *pp = p;
2006 return 0;
2007}
2008#else
2009static inline __be32
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002010nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
David Quigley18032ca2013-05-02 13:19:10 -04002011{ return 0; }
2012#endif
2013
Al Virob37ad282006-10-19 23:28:59 -07002014static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002015{
2016 /* As per referral draft: */
2017 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2018 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2019 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2020 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2021 *rdattr_err = NFSERR_MOVED;
2022 else
2023 return nfserr_moved;
2024 }
2025 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2026 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2027 return 0;
2028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002030
2031static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2032{
2033 struct path path = exp->ex_path;
2034 int err;
2035
2036 path_get(&path);
2037 while (follow_up(&path)) {
2038 if (path.dentry != path.mnt->mnt_root)
2039 break;
2040 }
Al Viro3dadecc2013-01-24 02:18:08 -05002041 err = vfs_getattr(&path, stat);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002042 path_put(&path);
2043 return err;
2044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/*
2047 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2048 * ourselves.
2049 *
J. Bruce Fields84822d02012-12-14 17:57:50 -05002050 * countp is the buffer size in _words_
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Al Virob37ad282006-10-19 23:28:59 -07002052__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002054 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002055 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 u32 bmval0 = bmval[0];
2058 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002059 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct kstat stat;
2061 struct svc_fh tempfh;
2062 struct kstatfs statfs;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002063 int buflen = count << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002064 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 u32 dummy;
2066 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002067 u32 rdattr_err = 0;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002068 __be32 *p = *buffer;
Al Virob37ad282006-10-19 23:28:59 -07002069 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002070 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int aclsupport = 0;
2072 struct nfs4_acl *acl = NULL;
David Quigley18032ca2013-05-02 13:19:10 -04002073 void *context = NULL;
2074 int contextlen;
2075 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002076 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2077 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002078 struct path path = {
2079 .mnt = exp->ex_path.mnt,
2080 .dentry = dentry,
2081 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002082 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002085 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2086 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2087 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002089 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002090 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002091 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2092 if (status)
2093 goto out;
2094 }
2095
Al Viro3dadecc2013-01-24 02:18:08 -05002096 err = vfs_getattr(&path, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002097 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002099 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2100 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2102 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002103 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002104 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto out_nfserr;
2106 }
2107 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2108 fh_init(&tempfh, NFS4_FHSIZE);
2109 status = fh_compose(&tempfh, exp, dentry, NULL);
2110 if (status)
2111 goto out;
2112 fhp = &tempfh;
2113 }
2114 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2115 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002116 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2117 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002119 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002121 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 status = nfserr_attrnotsupp;
2123 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002124 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto out_nfserr;
2126 }
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
David Quigley18032ca2013-05-02 13:19:10 -04002129#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2130 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2131 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2132 err = security_inode_getsecctx(dentry->d_inode,
2133 &context, &contextlen);
2134 contextsupport = (err == 0);
2135 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2136 if (err == -EOPNOTSUPP)
2137 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2138 else if (err)
2139 goto out_nfserr;
2140 }
2141 }
2142#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2143
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002144 if (bmval2) {
2145 if ((buflen -= 16) < 0)
2146 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002147 WRITE32(3);
2148 WRITE32(bmval0);
2149 WRITE32(bmval1);
2150 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002151 } else if (bmval1) {
2152 if ((buflen -= 12) < 0)
2153 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002154 WRITE32(2);
2155 WRITE32(bmval0);
2156 WRITE32(bmval1);
2157 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002158 if ((buflen -= 8) < 0)
2159 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002160 WRITE32(1);
2161 WRITE32(bmval0);
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 attrlenp = p++; /* to be backfilled later */
2164
2165 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002166 u32 word0 = nfsd_suppattrs0(minorversion);
2167 u32 word1 = nfsd_suppattrs1(minorversion);
2168 u32 word2 = nfsd_suppattrs2(minorversion);
2169
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002170 if (!aclsupport)
2171 word0 &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002172 if (!contextsupport)
2173 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
Andy Adamson7e705702009-04-03 08:29:11 +03002174 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002175 if ((buflen -= 12) < 0)
2176 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002177 WRITE32(2);
2178 WRITE32(word0);
2179 WRITE32(word1);
2180 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002181 if ((buflen -= 16) < 0)
2182 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002183 WRITE32(3);
2184 WRITE32(word0);
2185 WRITE32(word1);
2186 WRITE32(word2);
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 if (bmval0 & FATTR4_WORD0_TYPE) {
2190 if ((buflen -= 4) < 0)
2191 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002192 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (dummy == NF4BAD)
2194 goto out_serverfault;
2195 WRITE32(dummy);
2196 }
2197 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2198 if ((buflen -= 4) < 0)
2199 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002200 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002201 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002202 else
NeilBrowne34ac862005-07-07 17:59:30 -07002203 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if ((buflen -= 8) < 0)
2207 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002208 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210 if (bmval0 & FATTR4_WORD0_SIZE) {
2211 if ((buflen -= 8) < 0)
2212 goto out_resource;
2213 WRITE64(stat.size);
2214 }
2215 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2216 if ((buflen -= 4) < 0)
2217 goto out_resource;
2218 WRITE32(1);
2219 }
2220 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2221 if ((buflen -= 4) < 0)
2222 goto out_resource;
2223 WRITE32(1);
2224 }
2225 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2226 if ((buflen -= 4) < 0)
2227 goto out_resource;
2228 WRITE32(0);
2229 }
2230 if (bmval0 & FATTR4_WORD0_FSID) {
2231 if ((buflen -= 16) < 0)
2232 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002233 if (exp->ex_fslocs.migrated) {
2234 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2235 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002236 } else switch(fsid_source(fhp)) {
2237 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 WRITE64((u64)exp->ex_fsid);
2239 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002240 break;
2241 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 WRITE32(0);
2243 WRITE32(MAJOR(stat.dev));
2244 WRITE32(0);
2245 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002246 break;
2247 case FSIDSOURCE_UUID:
2248 WRITEMEM(exp->ex_uuid, 16);
2249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251 }
2252 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2253 if ((buflen -= 4) < 0)
2254 goto out_resource;
2255 WRITE32(0);
2256 }
2257 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2258 if ((buflen -= 4) < 0)
2259 goto out_resource;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002260 WRITE32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2263 if ((buflen -= 4) < 0)
2264 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002265 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 if (bmval0 & FATTR4_WORD0_ACL) {
2268 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 if (acl == NULL) {
2271 if ((buflen -= 4) < 0)
2272 goto out_resource;
2273
2274 WRITE32(0);
2275 goto out_acl;
2276 }
2277 if ((buflen -= 4) < 0)
2278 goto out_resource;
2279 WRITE32(acl->naces);
2280
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002281 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if ((buflen -= 4*3) < 0)
2283 goto out_resource;
2284 WRITE32(ace->type);
2285 WRITE32(ace->flag);
2286 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002287 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (status == nfserr_resource)
2289 goto out_resource;
2290 if (status)
2291 goto out;
2292 }
2293 }
2294out_acl:
2295 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2296 if ((buflen -= 4) < 0)
2297 goto out_resource;
2298 WRITE32(aclsupport ?
2299 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2300 }
2301 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2302 if ((buflen -= 4) < 0)
2303 goto out_resource;
2304 WRITE32(1);
2305 }
2306 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2307 if ((buflen -= 4) < 0)
2308 goto out_resource;
J. Bruce Fields2930d382012-06-05 16:52:06 -04002309 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2312 if ((buflen -= 4) < 0)
2313 goto out_resource;
2314 WRITE32(1);
2315 }
2316 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2317 if ((buflen -= 4) < 0)
2318 goto out_resource;
2319 WRITE32(1);
2320 }
2321 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2322 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2323 if (buflen < 0)
2324 goto out_resource;
2325 WRITE32(fhp->fh_handle.fh_size);
2326 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2327 }
2328 if (bmval0 & FATTR4_WORD0_FILEID) {
2329 if ((buflen -= 8) < 0)
2330 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002331 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2334 if ((buflen -= 8) < 0)
2335 goto out_resource;
2336 WRITE64((u64) statfs.f_ffree);
2337 }
2338 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2339 if ((buflen -= 8) < 0)
2340 goto out_resource;
2341 WRITE64((u64) statfs.f_ffree);
2342 }
2343 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2344 if ((buflen -= 8) < 0)
2345 goto out_resource;
2346 WRITE64((u64) statfs.f_files);
2347 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002348 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2349 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2350 if (status == nfserr_resource)
2351 goto out_resource;
2352 if (status)
2353 goto out;
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2356 if ((buflen -= 4) < 0)
2357 goto out_resource;
2358 WRITE32(1);
2359 }
2360 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2361 if ((buflen -= 8) < 0)
2362 goto out_resource;
2363 WRITE64(~(u64)0);
2364 }
2365 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2366 if ((buflen -= 4) < 0)
2367 goto out_resource;
2368 WRITE32(255);
2369 }
2370 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2371 if ((buflen -= 4) < 0)
2372 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002373 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2376 if ((buflen -= 8) < 0)
2377 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002378 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2381 if ((buflen -= 8) < 0)
2382 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002383 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385 if (bmval1 & FATTR4_WORD1_MODE) {
2386 if ((buflen -= 4) < 0)
2387 goto out_resource;
2388 WRITE32(stat.mode & S_IALLUGO);
2389 }
2390 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2391 if ((buflen -= 4) < 0)
2392 goto out_resource;
2393 WRITE32(1);
2394 }
2395 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2396 if ((buflen -= 4) < 0)
2397 goto out_resource;
2398 WRITE32(stat.nlink);
2399 }
2400 if (bmval1 & FATTR4_WORD1_OWNER) {
2401 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2402 if (status == nfserr_resource)
2403 goto out_resource;
2404 if (status)
2405 goto out;
2406 }
2407 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2408 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2409 if (status == nfserr_resource)
2410 goto out_resource;
2411 if (status)
2412 goto out;
2413 }
2414 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2415 if ((buflen -= 8) < 0)
2416 goto out_resource;
2417 WRITE32((u32) MAJOR(stat.rdev));
2418 WRITE32((u32) MINOR(stat.rdev));
2419 }
2420 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2421 if ((buflen -= 8) < 0)
2422 goto out_resource;
2423 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2424 WRITE64(dummy64);
2425 }
2426 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2427 if ((buflen -= 8) < 0)
2428 goto out_resource;
2429 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2430 WRITE64(dummy64);
2431 }
2432 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2433 if ((buflen -= 8) < 0)
2434 goto out_resource;
2435 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2436 WRITE64(dummy64);
2437 }
2438 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2439 if ((buflen -= 8) < 0)
2440 goto out_resource;
2441 dummy64 = (u64)stat.blocks << 9;
2442 WRITE64(dummy64);
2443 }
2444 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2445 if ((buflen -= 12) < 0)
2446 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002447 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 WRITE32(stat.atime.tv_nsec);
2449 }
2450 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2451 if ((buflen -= 12) < 0)
2452 goto out_resource;
2453 WRITE32(0);
2454 WRITE32(1);
2455 WRITE32(0);
2456 }
2457 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2458 if ((buflen -= 12) < 0)
2459 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002460 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 WRITE32(stat.ctime.tv_nsec);
2462 }
2463 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2464 if ((buflen -= 12) < 0)
2465 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002466 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 WRITE32(stat.mtime.tv_nsec);
2468 }
2469 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if ((buflen -= 8) < 0)
2471 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002472 /*
2473 * Get parent's attributes if not ignoring crossmount
2474 * and this is the root of a cross-mounted filesystem.
2475 */
2476 if (ignore_crossmnt == 0 &&
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002477 dentry == exp->ex_path.mnt->mnt_root)
2478 get_parent_attributes(exp, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002479 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
David Quigley18032ca2013-05-02 13:19:10 -04002481 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2482 status = nfsd4_encode_security_label(rqstp, context,
2483 contextlen, &p, &buflen);
2484 if (status)
2485 goto out;
2486 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002487 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2488 WRITE32(3);
2489 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2490 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2491 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2492 }
Andy Adamson7e705702009-04-03 08:29:11 +03002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
J. Bruce Fields84822d02012-12-14 17:57:50 -05002495 *buffer = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 status = nfs_ok;
2497
2498out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002499#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002500 if (context)
2501 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002502#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002503 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (fhp == &tempfh)
2505 fh_put(&tempfh);
2506 return status;
2507out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002508 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 goto out;
2510out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 status = nfserr_resource;
2512 goto out;
2513out_serverfault:
2514 status = nfserr_serverfault;
2515 goto out;
2516}
2517
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002518static inline int attributes_need_mount(u32 *bmval)
2519{
2520 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2521 return 1;
2522 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2523 return 1;
2524 return 0;
2525}
2526
Al Virob37ad282006-10-19 23:28:59 -07002527static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002529 const char *name, int namlen, __be32 **p, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 struct svc_export *exp = cd->rd_fhp->fh_export;
2532 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002533 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002534 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2537 if (IS_ERR(dentry))
2538 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002539 if (!dentry->d_inode) {
2540 /*
2541 * nfsd_buffered_readdir drops the i_mutex between
2542 * readdir and calling this callback, leaving a window
2543 * where this directory entry could have gone away.
2544 */
2545 dput(dentry);
2546 return nfserr_noent;
2547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002550 /*
2551 * In the case of a mountpoint, the client may be asking for
2552 * attributes that are only properties of the underlying filesystem
2553 * as opposed to the cross-mounted file system. In such a case,
2554 * we will not follow the cross mount and will fill the attribtutes
2555 * directly from the mountpoint dentry.
2556 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002557 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002558 int err;
2559
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002560 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2561 && !attributes_need_mount(cd->rd_bmval)) {
2562 ignore_crossmnt = 1;
2563 goto out_encode;
2564 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002565 /*
2566 * Why the heck aren't we just using nfsd_lookup??
2567 * Different "."/".." handling? Something else?
2568 * At least, add a comment here to explain....
2569 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002570 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2571 if (err) {
2572 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 goto out_put;
2574 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002575 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2576 if (nfserr)
2577 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002580out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002582 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583out_put:
2584 dput(dentry);
2585 exp_put(exp);
2586 return nfserr;
2587}
2588
Al Viro2ebbc012006-10-19 23:28:58 -07002589static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002590nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Al Viro2ebbc012006-10-19 23:28:58 -07002592 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 if (buflen < 6)
2595 return NULL;
2596 *p++ = htonl(2);
2597 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2598 *p++ = htonl(0); /* bmval1 */
2599
2600 attrlenp = p++;
2601 *p++ = nfserr; /* no htonl */
2602 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2603 return p;
2604}
2605
2606static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002607nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2608 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002610 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2612 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002613 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002614 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002615 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 /* In nfsv4, "." and ".." never make it onto the wire.. */
2618 if (name && isdotent(name, namlen)) {
2619 cd->common.err = nfs_ok;
2620 return 0;
2621 }
2622
2623 if (cd->offset)
2624 xdr_encode_hyper(cd->offset, (u64) offset);
2625
2626 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2627 if (buflen < 0)
2628 goto fail;
2629
2630 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002631 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2633 p = xdr_encode_array(p, name, namlen); /* name length & name */
2634
J. Bruce Fields84822d02012-12-14 17:57:50 -05002635 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 switch (nfserr) {
2637 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 break;
2639 case nfserr_resource:
2640 nfserr = nfserr_toosmall;
2641 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002642 case nfserr_noent:
2643 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 default:
2645 /*
2646 * If the client requested the RDATTR_ERROR attribute,
2647 * we stuff the error code into this attribute
2648 * and continue. If this attribute was not requested,
2649 * then in accordance with the spec, we fail the
2650 * entire READDIR operation(!)
2651 */
2652 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2653 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002655 if (p == NULL) {
2656 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660 cd->buflen -= (p - cd->buffer);
2661 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002662 cd->offset = cookiep;
2663skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 cd->common.err = nfs_ok;
2665 return 0;
2666fail:
2667 cd->common.err = nfserr;
2668 return -EINVAL;
2669}
2670
Benny Halevye2f282b2008-08-12 20:45:07 +03002671static void
2672nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2673{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002674 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002675
2676 RESERVE_SPACE(sizeof(stateid_t));
2677 WRITE32(sid->si_generation);
2678 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2679 ADJUST_ARGS();
2680}
2681
Benny Halevy695e12f2008-07-04 14:38:33 +03002682static __be32
Al Virob37ad282006-10-19 23:28:59 -07002683nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002685 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 if (!nfserr) {
2688 RESERVE_SPACE(8);
2689 WRITE32(access->ac_supported);
2690 WRITE32(access->ac_resp_access);
2691 ADJUST_ARGS();
2692 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002693 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002696static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2697{
2698 __be32 *p;
2699
2700 if (!nfserr) {
2701 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2702 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2703 WRITE32(bcts->dir);
J. Bruce Fields6e67b5d2012-09-13 09:49:43 -04002704 /* Sorry, we do not yet support RDMA over 4.1: */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002705 WRITE32(0);
2706 ADJUST_ARGS();
2707 }
2708 return nfserr;
2709}
2710
Benny Halevy695e12f2008-07-04 14:38:33 +03002711static __be32
Al Virob37ad282006-10-19 23:28:59 -07002712nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Benny Halevye2f282b2008-08-12 20:45:07 +03002714 if (!nfserr)
2715 nfsd4_encode_stateid(resp, &close->cl_stateid);
2716
Benny Halevy695e12f2008-07-04 14:38:33 +03002717 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720
Benny Halevy695e12f2008-07-04 14:38:33 +03002721static __be32
Al Virob37ad282006-10-19 23:28:59 -07002722nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002724 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002727 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2728 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 ADJUST_ARGS();
2730 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002731 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Benny Halevy695e12f2008-07-04 14:38:33 +03002734static __be32
Al Virob37ad282006-10-19 23:28:59 -07002735nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002737 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 if (!nfserr) {
2740 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002741 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 WRITE32(2);
2743 WRITE32(create->cr_bmval[0]);
2744 WRITE32(create->cr_bmval[1]);
2745 ADJUST_ARGS();
2746 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002747 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
Al Virob37ad282006-10-19 23:28:59 -07002750static __be32
2751nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 struct svc_fh *fhp = getattr->ga_fhp;
2754 int buflen;
2755
2756 if (nfserr)
2757 return nfserr;
2758
2759 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2760 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002761 &resp->p, buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002762 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return nfserr;
2764}
2765
Benny Halevy695e12f2008-07-04 14:38:33 +03002766static __be32
2767nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Benny Halevy695e12f2008-07-04 14:38:33 +03002769 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002771 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 if (!nfserr) {
2774 len = fhp->fh_handle.fh_size;
2775 RESERVE_SPACE(len + 4);
2776 WRITE32(len);
2777 WRITEMEM(&fhp->fh_handle.fh_base, len);
2778 ADJUST_ARGS();
2779 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002780 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783/*
2784* Including all fields other than the name, a LOCK4denied structure requires
2785* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2786*/
2787static void
2788nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2789{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002790 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002791 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002793 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 WRITE64(ld->ld_start);
2795 WRITE64(ld->ld_length);
2796 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002797 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002799 WRITE32(conf->len);
2800 WRITEMEM(conf->data, conf->len);
2801 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2803 WRITE64((u64)0); /* clientid */
2804 WRITE32(0); /* length of owner name */
2805 }
2806 ADJUST_ARGS();
2807}
2808
Benny Halevy695e12f2008-07-04 14:38:33 +03002809static __be32
Al Virob37ad282006-10-19 23:28:59 -07002810nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Benny Halevye2f282b2008-08-12 20:45:07 +03002812 if (!nfserr)
2813 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2814 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2816
Benny Halevy695e12f2008-07-04 14:38:33 +03002817 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
Benny Halevy695e12f2008-07-04 14:38:33 +03002820static __be32
Al Virob37ad282006-10-19 23:28:59 -07002821nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 if (nfserr == nfserr_denied)
2824 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002825 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
Benny Halevy695e12f2008-07-04 14:38:33 +03002828static __be32
Al Virob37ad282006-10-19 23:28:59 -07002829nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Benny Halevye2f282b2008-08-12 20:45:07 +03002831 if (!nfserr)
2832 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2833
Benny Halevy695e12f2008-07-04 14:38:33 +03002834 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837
Benny Halevy695e12f2008-07-04 14:38:33 +03002838static __be32
Al Virob37ad282006-10-19 23:28:59 -07002839nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002841 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 if (!nfserr) {
2844 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002845 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 ADJUST_ARGS();
2847 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002848 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
2851
Benny Halevy695e12f2008-07-04 14:38:33 +03002852static __be32
Al Virob37ad282006-10-19 23:28:59 -07002853nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002855 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 if (nfserr)
2858 goto out;
2859
Benny Halevye2f282b2008-08-12 20:45:07 +03002860 nfsd4_encode_stateid(resp, &open->op_stateid);
2861 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002862 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 WRITE32(open->op_rflags);
2864 WRITE32(2);
2865 WRITE32(open->op_bmval[0]);
2866 WRITE32(open->op_bmval[1]);
2867 WRITE32(open->op_delegate_type);
2868 ADJUST_ARGS();
2869
2870 switch (open->op_delegate_type) {
2871 case NFS4_OPEN_DELEGATE_NONE:
2872 break;
2873 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002874 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2875 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002876 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 /*
2879 * TODO: ACE's in delegations
2880 */
2881 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2882 WRITE32(0);
2883 WRITE32(0);
2884 WRITE32(0); /* XXX: is NULL principal ok? */
2885 ADJUST_ARGS();
2886 break;
2887 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002888 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2889 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 WRITE32(0);
2891
2892 /*
2893 * TODO: space_limit's in delegations
2894 */
2895 WRITE32(NFS4_LIMIT_SIZE);
2896 WRITE32(~(u32)0);
2897 WRITE32(~(u32)0);
2898
2899 /*
2900 * TODO: ACE's in delegations
2901 */
2902 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2903 WRITE32(0);
2904 WRITE32(0);
2905 WRITE32(0); /* XXX: is NULL principal ok? */
2906 ADJUST_ARGS();
2907 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002908 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2909 switch (open->op_why_no_deleg) {
2910 case WND4_CONTENTION:
2911 case WND4_RESOURCE:
2912 RESERVE_SPACE(8);
2913 WRITE32(open->op_why_no_deleg);
2914 WRITE32(0); /* deleg signaling not supported yet */
2915 break;
2916 default:
2917 RESERVE_SPACE(4);
2918 WRITE32(open->op_why_no_deleg);
2919 }
2920 ADJUST_ARGS();
2921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 default:
2923 BUG();
2924 }
2925 /* XXX save filehandle here */
2926out:
Benny Halevy695e12f2008-07-04 14:38:33 +03002927 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Benny Halevy695e12f2008-07-04 14:38:33 +03002930static __be32
Al Virob37ad282006-10-19 23:28:59 -07002931nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Benny Halevye2f282b2008-08-12 20:45:07 +03002933 if (!nfserr)
2934 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Benny Halevy695e12f2008-07-04 14:38:33 +03002936 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Benny Halevy695e12f2008-07-04 14:38:33 +03002939static __be32
Al Virob37ad282006-10-19 23:28:59 -07002940nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Benny Halevye2f282b2008-08-12 20:45:07 +03002942 if (!nfserr)
2943 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Benny Halevy695e12f2008-07-04 14:38:33 +03002945 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
Al Virob37ad282006-10-19 23:28:59 -07002948static __be32
2949nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002950 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 u32 eof;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002953 int v;
2954 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 unsigned long maxcount;
2956 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002957 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 if (nfserr)
2960 return nfserr;
2961 if (resp->xbuf->page_len)
2962 return nfserr_resource;
2963
2964 RESERVE_SPACE(8); /* eof flag and byte count */
2965
Greg Banks7adae482006-10-04 02:15:47 -07002966 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (maxcount > read->rd_length)
2968 maxcount = read->rd_length;
2969
2970 len = maxcount;
2971 v = 0;
2972 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002973 page = *(resp->rqstp->rq_next_page);
2974 if (!page) { /* ran out of pages */
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05002975 maxcount -= len;
2976 break;
2977 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002978 resp->rqstp->rq_vec[v].iov_base = page_address(page);
NeilBrown3cc03b12006-10-04 02:15:47 -07002979 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002980 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002981 resp->rqstp->rq_next_page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 v++;
2983 len -= PAGE_SIZE;
2984 }
2985 read->rd_vlen = v;
2986
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002987 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002988 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 &maxcount);
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (nfserr)
2992 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002993 eof = (read->rd_offset + maxcount >=
2994 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 WRITE32(eof);
2997 WRITE32(maxcount);
2998 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002999 resp->xbuf->head[0].iov_len = (char*)p
3000 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 resp->xbuf->page_len = maxcount;
3002
NeilBrown6ed6dec2006-04-10 22:55:32 -07003003 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003004 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003007 RESERVE_SPACE(4);
3008 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 resp->xbuf->tail[0].iov_base += maxcount&3;
3010 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003011 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
3013 return 0;
3014}
3015
Al Virob37ad282006-10-19 23:28:59 -07003016static __be32
3017nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
3019 int maxcount;
3020 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003021 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 if (nfserr)
3024 return nfserr;
3025 if (resp->xbuf->page_len)
3026 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003027 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003028 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003030 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 maxcount = PAGE_SIZE;
3033 RESERVE_SPACE(4);
3034
3035 /*
3036 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3037 * if they would overflow the buffer. Is this kosher in NFSv4? If
3038 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3039 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3040 */
3041 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3042 if (nfserr == nfserr_isdir)
3043 return nfserr_inval;
3044 if (nfserr)
3045 return nfserr;
3046
3047 WRITE32(maxcount);
3048 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003049 resp->xbuf->head[0].iov_len = (char*)p
3050 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003052
3053 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003054 resp->xbuf->tail[0].iov_base = p;
3055 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003057 RESERVE_SPACE(4);
3058 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 resp->xbuf->tail[0].iov_base += maxcount&3;
3060 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003061 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
3063 return 0;
3064}
3065
Al Virob37ad282006-10-19 23:28:59 -07003066static __be32
3067nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 int maxcount;
3070 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003071 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003072 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 if (nfserr)
3075 return nfserr;
3076 if (resp->xbuf->page_len)
3077 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003078 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003079 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Chuck Leverab4684d2012-03-02 17:13:50 -05003081 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 savep = p;
3083
3084 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3085 WRITE32(0);
3086 WRITE32(0);
3087 ADJUST_ARGS();
3088 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003089 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 maxcount = PAGE_SIZE;
3092 if (maxcount > readdir->rd_maxcount)
3093 maxcount = readdir->rd_maxcount;
3094
3095 /*
3096 * Convert from bytes to words, account for the two words already
3097 * written, make sure to leave two words at the end for the next
3098 * pointer and eof field.
3099 */
3100 maxcount = (maxcount >> 2) - 4;
3101 if (maxcount < 0) {
3102 nfserr = nfserr_toosmall;
3103 goto err_no_verf;
3104 }
3105
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003106 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 readdir->common.err = 0;
3108 readdir->buflen = maxcount;
3109 readdir->buffer = page;
3110 readdir->offset = NULL;
3111
3112 offset = readdir->rd_cookie;
3113 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3114 &offset,
3115 &readdir->common, nfsd4_encode_dirent);
3116 if (nfserr == nfs_ok &&
3117 readdir->common.err == nfserr_toosmall &&
3118 readdir->buffer == page)
3119 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (nfserr)
3121 goto err_no_verf;
3122
3123 if (readdir->offset)
3124 xdr_encode_hyper(readdir->offset, offset);
3125
3126 p = readdir->buffer;
3127 *p++ = 0; /* no more entries */
3128 *p++ = htonl(readdir->common.err == nfserr_eof);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003129 resp->xbuf->page_len = ((char*)p) -
3130 (char*)page_address(*(resp->rqstp->rq_next_page-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
NeilBrownbb6e8a92006-04-10 22:55:33 -07003132 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003133 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 resp->xbuf->tail[0].iov_len = 0;
3135 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003136 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 return 0;
3139err_no_verf:
3140 p = savep;
3141 ADJUST_ARGS();
3142 return nfserr;
3143}
3144
Benny Halevy695e12f2008-07-04 14:38:33 +03003145static __be32
Al Virob37ad282006-10-19 23:28:59 -07003146nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003148 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150 if (!nfserr) {
3151 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003152 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 ADJUST_ARGS();
3154 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003155 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Benny Halevy695e12f2008-07-04 14:38:33 +03003158static __be32
Al Virob37ad282006-10-19 23:28:59 -07003159nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003161 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 if (!nfserr) {
3164 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003165 write_cinfo(&p, &rename->rn_sinfo);
3166 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 ADJUST_ARGS();
3168 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003169 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Benny Halevy695e12f2008-07-04 14:38:33 +03003172static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003173nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
Chuck Levera77c8062013-03-16 15:55:10 -04003174 __be32 nfserr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003175{
Chuck Lever676e4eb2013-04-30 18:48:54 -04003176 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003177 struct exp_flavor_info *flavs;
3178 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04003179 __be32 *p, *flavorsp;
3180 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003181
3182 if (nfserr)
3183 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003184 if (exp->ex_nflavors) {
3185 flavs = exp->ex_flavors;
3186 nflavs = exp->ex_nflavors;
3187 } else { /* Handling of some defaults in absence of real secinfo: */
3188 flavs = def_flavs;
3189 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3190 nflavs = 2;
3191 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3192 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3193 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3194 nflavs = 1;
3195 flavs[0].pseudoflavor
3196 = svcauth_gss_flavor(exp->ex_client);
3197 } else {
3198 nflavs = 1;
3199 flavs[0].pseudoflavor
3200 = exp->ex_client->flavour->flavour;
3201 }
3202 }
3203
Chuck Lever676e4eb2013-04-30 18:48:54 -04003204 supported = 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003205 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003206 flavorsp = p++; /* to be backfilled later */
Andy Adamsondcb488a32007-07-17 04:04:51 -07003207 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003208
J. Bruce Fields4796f452007-07-17 04:04:51 -07003209 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04003210 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04003211 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003212
Chuck Lever676e4eb2013-04-30 18:48:54 -04003213 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3214 supported++;
Chuck Levered9411a2013-04-30 18:48:45 -04003215 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003216 WRITE32(RPC_AUTH_GSS);
Chuck Levera77c8062013-03-16 15:55:10 -04003217 WRITE32(info.oid.len);
3218 WRITEMEM(info.oid.data, info.oid.len);
Chuck Levera77c8062013-03-16 15:55:10 -04003219 WRITE32(info.qop);
Chuck Levera77c8062013-03-16 15:55:10 -04003220 WRITE32(info.service);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003221 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003222 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3223 supported++;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003224 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003225 WRITE32(pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003226 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003227 } else {
3228 if (report)
3229 pr_warn("NFS: SECINFO: security flavor %u "
3230 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003231 }
3232 }
Chuck Levera77c8062013-03-16 15:55:10 -04003233
Chuck Lever676e4eb2013-04-30 18:48:54 -04003234 if (nflavs != supported)
3235 report = false;
3236 *flavorsp = htonl(supported);
3237
Andy Adamsondcb488a32007-07-17 04:04:51 -07003238out:
3239 if (exp)
3240 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003241 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003242}
3243
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003244static __be32
3245nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3246 struct nfsd4_secinfo *secinfo)
3247{
3248 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3249}
3250
3251static __be32
3252nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3253 struct nfsd4_secinfo_no_name *secinfo)
3254{
3255 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3256}
3257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258/*
3259 * The SETATTR encode routine is special -- it always encodes a bitmap,
3260 * regardless of the error status.
3261 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003262static __be32
Al Virob37ad282006-10-19 23:28:59 -07003263nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003265 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
David Quigley18032ca2013-05-02 13:19:10 -04003267 RESERVE_SPACE(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (nfserr) {
David Quigley18032ca2013-05-02 13:19:10 -04003269 WRITE32(3);
3270 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 WRITE32(0);
3272 WRITE32(0);
3273 }
3274 else {
David Quigley18032ca2013-05-02 13:19:10 -04003275 WRITE32(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 WRITE32(setattr->sa_bmval[0]);
3277 WRITE32(setattr->sa_bmval[1]);
David Quigley18032ca2013-05-02 13:19:10 -04003278 WRITE32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003281 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
Benny Halevy695e12f2008-07-04 14:38:33 +03003284static __be32
Al Virob37ad282006-10-19 23:28:59 -07003285nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003287 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003290 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003292 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 ADJUST_ARGS();
3294 }
3295 else if (nfserr == nfserr_clid_inuse) {
3296 RESERVE_SPACE(8);
3297 WRITE32(0);
3298 WRITE32(0);
3299 ADJUST_ARGS();
3300 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003301 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
Benny Halevy695e12f2008-07-04 14:38:33 +03003304static __be32
Al Virob37ad282006-10-19 23:28:59 -07003305nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003307 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 if (!nfserr) {
3310 RESERVE_SPACE(16);
3311 WRITE32(write->wr_bytes_written);
3312 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003313 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 ADJUST_ARGS();
3315 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003316 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
J. Bruce Fields57266a62013-04-13 14:27:29 -04003319static const u32 nfs4_minimal_spo_must_enforce[2] = {
3320 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3321 1 << (OP_EXCHANGE_ID - 32) |
3322 1 << (OP_CREATE_SESSION - 32) |
3323 1 << (OP_DESTROY_SESSION - 32) |
3324 1 << (OP_DESTROY_CLIENTID - 32)
3325};
3326
Benny Halevy695e12f2008-07-04 14:38:33 +03003327static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003328nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003329 struct nfsd4_exchange_id *exid)
3330{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003331 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003332 char *major_id;
3333 char *server_scope;
3334 int major_id_sz;
3335 int server_scope_sz;
3336 uint64_t minor_id = 0;
3337
3338 if (nfserr)
3339 return nfserr;
3340
3341 major_id = utsname()->nodename;
3342 major_id_sz = strlen(major_id);
3343 server_scope = utsname()->nodename;
3344 server_scope_sz = strlen(server_scope);
3345
3346 RESERVE_SPACE(
3347 8 /* eir_clientid */ +
3348 4 /* eir_sequenceid */ +
3349 4 /* eir_flags */ +
Weston Andros Adamson58cd57b2013-08-05 15:47:07 -04003350 4 /* spr_how */ +
3351 8 /* spo_must_enforce, spo_must_allow */ +
Andy Adamson0733d212009-04-03 08:28:01 +03003352 8 /* so_minor_id */ +
3353 4 /* so_major_id.len */ +
3354 (XDR_QUADLEN(major_id_sz) * 4) +
3355 4 /* eir_server_scope.len */ +
3356 (XDR_QUADLEN(server_scope_sz) * 4) +
3357 4 /* eir_server_impl_id.count (0) */);
3358
3359 WRITEMEM(&exid->clientid, 8);
3360 WRITE32(exid->seqid);
3361 WRITE32(exid->flags);
3362
Andy Adamson0733d212009-04-03 08:28:01 +03003363 WRITE32(exid->spa_how);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003364 switch (exid->spa_how) {
3365 case SP4_NONE:
3366 break;
3367 case SP4_MACH_CRED:
3368 /* spo_must_enforce bitmap: */
3369 WRITE32(2);
3370 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3371 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3372 /* empty spo_must_allow bitmap: */
3373 WRITE32(0);
3374 break;
3375 default:
3376 WARN_ON_ONCE(1);
3377 }
Andy Adamson0733d212009-04-03 08:28:01 +03003378
3379 /* The server_owner struct */
3380 WRITE64(minor_id); /* Minor id */
3381 /* major id */
3382 WRITE32(major_id_sz);
3383 WRITEMEM(major_id, major_id_sz);
3384
3385 /* Server scope */
3386 WRITE32(server_scope_sz);
3387 WRITEMEM(server_scope, server_scope_sz);
3388
3389 /* Implementation id */
3390 WRITE32(0); /* zero length nfs_impl_id4 array */
3391 ADJUST_ARGS();
3392 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003393}
3394
3395static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003396nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003397 struct nfsd4_create_session *sess)
3398{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003399 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003400
3401 if (nfserr)
3402 return nfserr;
3403
3404 RESERVE_SPACE(24);
3405 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3406 WRITE32(sess->seqid);
3407 WRITE32(sess->flags);
3408 ADJUST_ARGS();
3409
3410 RESERVE_SPACE(28);
3411 WRITE32(0); /* headerpadsz */
3412 WRITE32(sess->fore_channel.maxreq_sz);
3413 WRITE32(sess->fore_channel.maxresp_sz);
3414 WRITE32(sess->fore_channel.maxresp_cached);
3415 WRITE32(sess->fore_channel.maxops);
3416 WRITE32(sess->fore_channel.maxreqs);
3417 WRITE32(sess->fore_channel.nr_rdma_attrs);
3418 ADJUST_ARGS();
3419
3420 if (sess->fore_channel.nr_rdma_attrs) {
3421 RESERVE_SPACE(4);
3422 WRITE32(sess->fore_channel.rdma_attrs);
3423 ADJUST_ARGS();
3424 }
3425
3426 RESERVE_SPACE(28);
3427 WRITE32(0); /* headerpadsz */
3428 WRITE32(sess->back_channel.maxreq_sz);
3429 WRITE32(sess->back_channel.maxresp_sz);
3430 WRITE32(sess->back_channel.maxresp_cached);
3431 WRITE32(sess->back_channel.maxops);
3432 WRITE32(sess->back_channel.maxreqs);
3433 WRITE32(sess->back_channel.nr_rdma_attrs);
3434 ADJUST_ARGS();
3435
3436 if (sess->back_channel.nr_rdma_attrs) {
3437 RESERVE_SPACE(4);
3438 WRITE32(sess->back_channel.rdma_attrs);
3439 ADJUST_ARGS();
3440 }
3441 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003442}
3443
3444static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003445nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003446 struct nfsd4_destroy_session *destroy_session)
3447{
Andy Adamson2db134e2009-04-03 08:27:55 +03003448 return nfserr;
3449}
3450
Daniel Mackc47d8322011-05-16 16:38:14 +02003451static __be32
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003452nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003453 struct nfsd4_free_stateid *free_stateid)
3454{
3455 __be32 *p;
3456
3457 if (nfserr)
3458 return nfserr;
3459
3460 RESERVE_SPACE(4);
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003461 *p++ = nfserr;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003462 ADJUST_ARGS();
3463 return nfserr;
3464}
3465
3466static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003467nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003468 struct nfsd4_sequence *seq)
3469{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003470 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003471
3472 if (nfserr)
3473 return nfserr;
3474
3475 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3476 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3477 WRITE32(seq->seqid);
3478 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003479 /* Note slotid's are numbered from zero: */
3480 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3481 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003482 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003483
3484 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003485 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003486 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003487}
3488
J. Bruce Fields2355c592012-04-25 16:56:22 -04003489static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003490nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04003491 struct nfsd4_test_stateid *test_stateid)
3492{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003493 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003494 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003495
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003496 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003497 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003498
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003499 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003500 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003501 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003502
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003503 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003504 return nfserr;
3505}
3506
Andy Adamson2db134e2009-04-03 08:27:55 +03003507static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003508nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3509{
3510 return nfserr;
3511}
3512
3513typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3514
Andy Adamson2db134e2009-04-03 08:27:55 +03003515/*
3516 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3517 * since we don't need to filter out obsolete ops as this is
3518 * done in the decoding phase.
3519 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003520static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003521 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3522 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3523 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3524 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3525 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3526 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3527 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3528 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3529 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3530 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3531 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3532 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3533 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3534 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3535 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3536 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003537 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003538 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3539 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3540 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3541 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3542 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3543 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3544 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3545 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3546 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3547 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3548 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3549 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3550 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3551 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3552 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3553 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3554 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3555 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3556 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3557 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003558
3559 /* NFSv4.1 operations */
3560 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003561 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003562 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3563 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3564 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003565 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003566 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3567 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3568 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3569 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3570 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3571 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003572 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003573 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3574 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003575 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003576 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3577 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3578 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003579};
3580
Andy Adamson496c2622009-04-03 08:28:48 +03003581/*
3582 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003583 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003584 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003585 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3586 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003587 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003588 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003589 *
3590 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3591 * will be at least a page and will therefore hold the xdr_buf head.
3592 */
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003593__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003594{
Andy Adamson496c2622009-04-03 08:28:48 +03003595 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003596 struct nfsd4_session *session = NULL;
3597 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003598 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003599
3600 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003601 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003602
3603 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003604 if (session == NULL)
3605 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003606
3607 if (xb->page_len == 0) {
3608 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3609 } else {
3610 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3611 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3612
3613 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3614 }
3615 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3616 length, xb->page_len, tlen, pad);
3617
Mi Jinlong58e7b332011-08-28 18:18:56 +08003618 if (length > session->se_fchannel.maxresp_sz)
3619 return nfserr_rep_too_big;
3620
J. Bruce Fields73e79482012-02-13 16:39:00 -05003621 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003622 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003623 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003624
3625 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003626}
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628void
3629nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3630{
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003631 struct nfs4_stateowner *so = resp->cstate.replay_owner;
Al Viro2ebbc012006-10-19 23:28:58 -07003632 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003633 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 RESERVE_SPACE(8);
3636 WRITE32(op->opnum);
3637 statp = p++; /* to be backfilled at the end */
3638 ADJUST_ARGS();
3639
Benny Halevy695e12f2008-07-04 14:38:33 +03003640 if (op->opnum == OP_ILLEGAL)
3641 goto status;
3642 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3643 !nfsd4_enc_ops[op->opnum]);
3644 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003645 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003646 if (!op->status)
3647 op->status = nfsd4_check_resp_size(resp, 0);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003648 if (so) {
3649 so->so_replay.rp_status = op->status;
3650 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3651 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3652 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003653status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /*
3655 * Note: We write the status directly, instead of using WRITE32(),
3656 * since it is already in network byte order.
3657 */
3658 *statp = op->status;
3659}
3660
3661/*
3662 * Encode the reply stored in the stateowner reply cache
3663 *
3664 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3665 * previously sent already encoded operation.
3666 *
3667 * called with nfs4_lock_state() held
3668 */
3669void
3670nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3671{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003672 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 struct nfs4_replay *rp = op->replay;
3674
3675 BUG_ON(!rp);
3676
3677 RESERVE_SPACE(8);
3678 WRITE32(op->opnum);
3679 *p++ = rp->rp_status; /* already xdr'ed */
3680 ADJUST_ARGS();
3681
3682 RESERVE_SPACE(rp->rp_buflen);
3683 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3684 ADJUST_ARGS();
3685}
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687int
Al Viro2ebbc012006-10-19 23:28:58 -07003688nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
3690 return xdr_ressize_check(rqstp, p);
3691}
3692
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003693int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003695 struct svc_rqst *rqstp = rq;
3696 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 if (args->ops != args->iops) {
3699 kfree(args->ops);
3700 args->ops = args->iops;
3701 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003702 kfree(args->tmpp);
3703 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 while (args->to_free) {
3705 struct tmpbuf *tb = args->to_free;
3706 args->to_free = tb->next;
3707 tb->release(tb->buf);
3708 kfree(tb);
3709 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003710 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
3713int
Al Viro2ebbc012006-10-19 23:28:58 -07003714nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 args->p = p;
3717 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3718 args->pagelist = rqstp->rq_arg.pages;
3719 args->pagelen = rqstp->rq_arg.page_len;
3720 args->tmpp = NULL;
3721 args->to_free = NULL;
3722 args->ops = args->iops;
3723 args->rqstp = rqstp;
3724
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003725 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
3728int
Al Viro2ebbc012006-10-19 23:28:58 -07003729nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 /*
3732 * All that remains is to write the tag and operation count...
3733 */
Andy Adamson557ce262009-08-28 08:45:04 -04003734 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 struct kvec *iov;
3736 p = resp->tagp;
3737 *p++ = htonl(resp->taglen);
3738 memcpy(p, resp->tag, resp->taglen);
3739 p += XDR_QUADLEN(resp->taglen);
3740 *p++ = htonl(resp->opcnt);
3741
3742 if (rqstp->rq_res.page_len)
3743 iov = &rqstp->rq_res.tail[0];
3744 else
3745 iov = &rqstp->rq_res.head[0];
3746 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3747 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003748 if (nfsd4_has_session(cs)) {
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003749 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3750 struct nfs4_client *clp = cs->session->se_client;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003751 if (cs->status != nfserr_replay_cache) {
3752 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003753 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003754 }
Benny Halevyd7682982010-05-12 00:13:54 +03003755 /* Renew the clientid on success and on replay */
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003756 spin_lock(&nn->client_lock);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003757 nfsd4_put_session(cs->session);
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003758 spin_unlock(&nn->client_lock);
3759 put_client_renew(clp);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return 1;
3762}
3763
3764/*
3765 * Local variables:
3766 * c-basic-offset: 8
3767 * End:
3768 */