blob: 83db5a742aa9caf471c984fa616fc4645761d104 [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;
414 defer_free(argp, kfree, label->data);
415 memcpy(label->data, buf, dummy32);
416 }
417#endif
418
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800419 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
420 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
421 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
422 READ_BUF(expected_len - len);
423 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto xdr_error;
425
426 DECODE_TAIL;
427
428out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700429 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto out;
431}
432
Al Virob37ad282006-10-19 23:28:59 -0700433static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300434nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
435{
436 DECODE_HEAD;
437
438 READ_BUF(sizeof(stateid_t));
439 READ32(sid->si_generation);
440 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
441
442 DECODE_TAIL;
443}
444
445static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
447{
448 DECODE_HEAD;
449
450 READ_BUF(4);
451 READ32(access->ac_req_access);
452
453 DECODE_TAIL;
454}
455
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400456static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
457{
458 DECODE_HEAD;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500459 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400460 char *machine_name;
461 int i;
462 int nr_secflavs;
463
464 /* callback_sec_params4 */
465 READ_BUF(4);
466 READ32(nr_secflavs);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400467 if (nr_secflavs)
468 cbs->flavor = (u32)(-1);
469 else
470 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
471 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400472 for (i = 0; i < nr_secflavs; ++i) {
473 READ_BUF(4);
474 READ32(dummy);
475 switch (dummy) {
476 case RPC_AUTH_NULL:
477 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500478 if (cbs->flavor == (u32)(-1))
479 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400480 break;
481 case RPC_AUTH_UNIX:
482 READ_BUF(8);
483 /* stamp */
484 READ32(dummy);
485
486 /* machine name */
487 READ32(dummy);
488 READ_BUF(dummy);
489 SAVEMEM(machine_name, dummy);
490
491 /* uid, gid */
492 READ_BUF(8);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500493 READ32(uid);
494 READ32(gid);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400495
496 /* more gids */
497 READ_BUF(4);
498 READ32(dummy);
499 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500500 if (cbs->flavor == (u32)(-1)) {
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800501 kuid_t kuid = make_kuid(&init_user_ns, uid);
502 kgid_t kgid = make_kgid(&init_user_ns, gid);
503 if (uid_valid(kuid) && gid_valid(kgid)) {
504 cbs->uid = kuid;
505 cbs->gid = kgid;
506 cbs->flavor = RPC_AUTH_UNIX;
507 } else {
508 dprintk("RPC_AUTH_UNIX with invalid"
509 "uid or gid ignoring!\n");
510 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500511 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400512 break;
513 case RPC_AUTH_GSS:
514 dprintk("RPC_AUTH_GSS callback secflavor "
515 "not supported!\n");
516 READ_BUF(8);
517 /* gcbp_service */
518 READ32(dummy);
519 /* gcbp_handle_from_server */
520 READ32(dummy);
521 READ_BUF(dummy);
522 p += XDR_QUADLEN(dummy);
523 /* gcbp_handle_from_client */
524 READ_BUF(4);
525 READ32(dummy);
526 READ_BUF(dummy);
527 break;
528 default:
529 dprintk("Illegal callback secflavor\n");
530 return nfserr_inval;
531 }
532 }
533 DECODE_TAIL;
534}
535
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400536static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
537{
538 DECODE_HEAD;
539
540 READ_BUF(4);
541 READ32(bc->bc_cb_program);
542 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
543
544 DECODE_TAIL;
545}
546
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400547static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
548{
549 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400550
551 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
552 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
553 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400554 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
555 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400556 DECODE_TAIL;
557}
558
Al Virob37ad282006-10-19 23:28:59 -0700559static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
561{
562 DECODE_HEAD;
563
Benny Halevye31a1b62008-08-12 20:46:18 +0300564 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300566 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 DECODE_TAIL;
569}
570
571
Al Virob37ad282006-10-19 23:28:59 -0700572static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
574{
575 DECODE_HEAD;
576
577 READ_BUF(12);
578 READ64(commit->co_offset);
579 READ32(commit->co_count);
580
581 DECODE_TAIL;
582}
583
Al Virob37ad282006-10-19 23:28:59 -0700584static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
586{
587 DECODE_HEAD;
588
589 READ_BUF(4);
590 READ32(create->cr_type);
591 switch (create->cr_type) {
592 case NF4LNK:
593 READ_BUF(4);
594 READ32(create->cr_linklen);
595 READ_BUF(create->cr_linklen);
596 SAVEMEM(create->cr_linkname, create->cr_linklen);
597 break;
598 case NF4BLK:
599 case NF4CHR:
600 READ_BUF(8);
601 READ32(create->cr_specdata1);
602 READ32(create->cr_specdata2);
603 break;
604 case NF4SOCK:
605 case NF4FIFO:
606 case NF4DIR:
607 default:
608 break;
609 }
610
611 READ_BUF(4);
612 READ32(create->cr_namelen);
613 READ_BUF(create->cr_namelen);
614 SAVEMEM(create->cr_name, create->cr_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500615 if ((status = check_filename(create->cr_name, create->cr_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return status;
617
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800618 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
David Quigley18032ca2013-05-02 13:19:10 -0400619 &create->cr_acl, &create->cr_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300620 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 goto out;
622
623 DECODE_TAIL;
624}
625
Al Virob37ad282006-10-19 23:28:59 -0700626static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
628{
Benny Halevye31a1b62008-08-12 20:46:18 +0300629 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Al Virob37ad282006-10-19 23:28:59 -0700632static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
634{
635 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
636}
637
Al Virob37ad282006-10-19 23:28:59 -0700638static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
640{
641 DECODE_HEAD;
642
643 READ_BUF(4);
644 READ32(link->li_namelen);
645 READ_BUF(link->li_namelen);
646 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500647 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return status;
649
650 DECODE_TAIL;
651}
652
Al Virob37ad282006-10-19 23:28:59 -0700653static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
655{
656 DECODE_HEAD;
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
660 */
661 READ_BUF(28);
662 READ32(lock->lk_type);
663 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
664 goto xdr_error;
665 READ32(lock->lk_reclaim);
666 READ64(lock->lk_offset);
667 READ64(lock->lk_length);
668 READ32(lock->lk_is_new);
669
670 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300671 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300673 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
674 if (status)
675 return status;
676 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 READ32(lock->lk_new_lock_seqid);
678 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
679 READ32(lock->lk_new_owner.len);
680 READ_BUF(lock->lk_new_owner.len);
681 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
682 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300683 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
684 if (status)
685 return status;
686 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 READ32(lock->lk_old_lock_seqid);
688 }
689
690 DECODE_TAIL;
691}
692
Al Virob37ad282006-10-19 23:28:59 -0700693static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
695{
696 DECODE_HEAD;
697
698 READ_BUF(32);
699 READ32(lockt->lt_type);
700 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
701 goto xdr_error;
702 READ64(lockt->lt_offset);
703 READ64(lockt->lt_length);
704 COPYMEM(&lockt->lt_clientid, 8);
705 READ32(lockt->lt_owner.len);
706 READ_BUF(lockt->lt_owner.len);
707 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
708
709 DECODE_TAIL;
710}
711
Al Virob37ad282006-10-19 23:28:59 -0700712static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
714{
715 DECODE_HEAD;
716
Benny Halevye31a1b62008-08-12 20:46:18 +0300717 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 READ32(locku->lu_type);
719 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
720 goto xdr_error;
721 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300722 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
723 if (status)
724 return status;
725 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 READ64(locku->lu_offset);
727 READ64(locku->lu_length);
728
729 DECODE_TAIL;
730}
731
Al Virob37ad282006-10-19 23:28:59 -0700732static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
734{
735 DECODE_HEAD;
736
737 READ_BUF(4);
738 READ32(lookup->lo_len);
739 READ_BUF(lookup->lo_len);
740 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500741 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return status;
743
744 DECODE_TAIL;
745}
746
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200747static __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 -0400748{
749 __be32 *p;
750 u32 w;
751
752 READ_BUF(4);
753 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200754 *share_access = w & NFS4_SHARE_ACCESS_MASK;
755 *deleg_want = w & NFS4_SHARE_WANT_MASK;
756 if (deleg_when)
757 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
758
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400759 switch (w & NFS4_SHARE_ACCESS_MASK) {
760 case NFS4_SHARE_ACCESS_READ:
761 case NFS4_SHARE_ACCESS_WRITE:
762 case NFS4_SHARE_ACCESS_BOTH:
763 break;
764 default:
765 return nfserr_bad_xdr;
766 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200767 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400768 if (!w)
769 return nfs_ok;
770 if (!argp->minorversion)
771 return nfserr_bad_xdr;
772 switch (w & NFS4_SHARE_WANT_MASK) {
773 case NFS4_SHARE_WANT_NO_PREFERENCE:
774 case NFS4_SHARE_WANT_READ_DELEG:
775 case NFS4_SHARE_WANT_WRITE_DELEG:
776 case NFS4_SHARE_WANT_ANY_DELEG:
777 case NFS4_SHARE_WANT_NO_DELEG:
778 case NFS4_SHARE_WANT_CANCEL:
779 break;
780 default:
781 return nfserr_bad_xdr;
782 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700783 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400784 if (!w)
785 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200786
787 if (!deleg_when) /* open_downgrade */
788 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400789 switch (w) {
790 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
791 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700792 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
793 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400794 return nfs_ok;
795 }
796xdr_error:
797 return nfserr_bad_xdr;
798}
799
800static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
801{
802 __be32 *p;
803
804 READ_BUF(4);
805 READ32(*x);
806 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4afa2011-10-17 10:41:17 +0300807 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400808 return nfserr_bad_xdr;
809 return nfs_ok;
810xdr_error:
811 return nfserr_bad_xdr;
812}
813
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400814static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
815{
816 __be32 *p;
817
818 READ_BUF(4);
819 READ32(o->len);
820
821 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
822 return nfserr_bad_xdr;
823
824 READ_BUF(o->len);
825 SAVEMEM(o->data, o->len);
826 return nfs_ok;
827xdr_error:
828 return nfserr_bad_xdr;
829}
830
Al Virob37ad282006-10-19 23:28:59 -0700831static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
833{
834 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200835 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 memset(open->op_bmval, 0, sizeof(open->op_bmval));
838 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400839 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800841 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400843 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200845 /* decode, yet ignore deleg_when until supported */
846 status = nfsd4_decode_share_access(argp, &open->op_share_access,
847 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400848 if (status)
849 goto xdr_error;
850 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
851 if (status)
852 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400853 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400855 status = nfsd4_decode_opaque(argp, &open->op_owner);
856 if (status)
857 goto xdr_error;
858 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 READ32(open->op_create);
860 switch (open->op_create) {
861 case NFS4_OPEN_NOCREATE:
862 break;
863 case NFS4_OPEN_CREATE:
864 READ_BUF(4);
865 READ32(open->op_createmode);
866 switch (open->op_createmode) {
867 case NFS4_CREATE_UNCHECKED:
868 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300869 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400870 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300871 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 goto out;
873 break;
874 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500875 READ_BUF(NFS4_VERIFIER_SIZE);
876 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300878 case NFS4_CREATE_EXCLUSIVE4_1:
879 if (argp->minorversion < 1)
880 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500881 READ_BUF(NFS4_VERIFIER_SIZE);
882 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300883 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400884 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300885 if (status)
886 goto out;
887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 default:
889 goto xdr_error;
890 }
891 break;
892 default:
893 goto xdr_error;
894 }
895
896 /* open_claim */
897 READ_BUF(4);
898 READ32(open->op_claim_type);
899 switch (open->op_claim_type) {
900 case NFS4_OPEN_CLAIM_NULL:
901 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
902 READ_BUF(4);
903 READ32(open->op_fname.len);
904 READ_BUF(open->op_fname.len);
905 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500906 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return status;
908 break;
909 case NFS4_OPEN_CLAIM_PREVIOUS:
910 READ_BUF(4);
911 READ32(open->op_delegate_type);
912 break;
913 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300914 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
915 if (status)
916 return status;
917 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 READ32(open->op_fname.len);
919 READ_BUF(open->op_fname.len);
920 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500921 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return status;
923 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400924 case NFS4_OPEN_CLAIM_FH:
925 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
926 if (argp->minorversion < 1)
927 goto xdr_error;
928 /* void */
929 break;
930 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
931 if (argp->minorversion < 1)
932 goto xdr_error;
933 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
934 if (status)
935 return status;
936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 default:
938 goto xdr_error;
939 }
940
941 DECODE_TAIL;
942}
943
Al Virob37ad282006-10-19 23:28:59 -0700944static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
946{
947 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400948
949 if (argp->minorversion >= 1)
950 return nfserr_notsupp;
951
Benny Halevye31a1b62008-08-12 20:46:18 +0300952 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
953 if (status)
954 return status;
955 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 READ32(open_conf->oc_seqid);
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 DECODE_TAIL;
959}
960
Al Virob37ad282006-10-19 23:28:59 -0700961static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
963{
964 DECODE_HEAD;
965
Benny Halevye31a1b62008-08-12 20:46:18 +0300966 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
967 if (status)
968 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400969 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200971 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
972 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400973 if (status)
974 return status;
975 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
976 if (status)
977 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 DECODE_TAIL;
979}
980
Al Virob37ad282006-10-19 23:28:59 -0700981static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
983{
984 DECODE_HEAD;
985
986 READ_BUF(4);
987 READ32(putfh->pf_fhlen);
988 if (putfh->pf_fhlen > NFS4_FHSIZE)
989 goto xdr_error;
990 READ_BUF(putfh->pf_fhlen);
991 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
992
993 DECODE_TAIL;
994}
995
Al Virob37ad282006-10-19 23:28:59 -0700996static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400997nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
998{
999 if (argp->minorversion == 0)
1000 return nfs_ok;
1001 return nfserr_notsupp;
1002}
1003
1004static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1006{
1007 DECODE_HEAD;
1008
Benny Halevye31a1b62008-08-12 20:46:18 +03001009 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1010 if (status)
1011 return status;
1012 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 READ64(read->rd_offset);
1014 READ32(read->rd_length);
1015
1016 DECODE_TAIL;
1017}
1018
Al Virob37ad282006-10-19 23:28:59 -07001019static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1021{
1022 DECODE_HEAD;
1023
1024 READ_BUF(24);
1025 READ64(readdir->rd_cookie);
1026 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1027 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1028 READ32(readdir->rd_maxcount);
1029 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1030 goto out;
1031
1032 DECODE_TAIL;
1033}
1034
Al Virob37ad282006-10-19 23:28:59 -07001035static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1037{
1038 DECODE_HEAD;
1039
1040 READ_BUF(4);
1041 READ32(remove->rm_namelen);
1042 READ_BUF(remove->rm_namelen);
1043 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001044 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return status;
1046
1047 DECODE_TAIL;
1048}
1049
Al Virob37ad282006-10-19 23:28:59 -07001050static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1052{
1053 DECODE_HEAD;
1054
1055 READ_BUF(4);
1056 READ32(rename->rn_snamelen);
1057 READ_BUF(rename->rn_snamelen + 4);
1058 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1059 READ32(rename->rn_tnamelen);
1060 READ_BUF(rename->rn_tnamelen);
1061 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001062 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001064 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return status;
1066
1067 DECODE_TAIL;
1068}
1069
Al Virob37ad282006-10-19 23:28:59 -07001070static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1072{
1073 DECODE_HEAD;
1074
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001075 if (argp->minorversion >= 1)
1076 return nfserr_notsupp;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 READ_BUF(sizeof(clientid_t));
1079 COPYMEM(clientid, sizeof(clientid_t));
1080
1081 DECODE_TAIL;
1082}
1083
Al Virob37ad282006-10-19 23:28:59 -07001084static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001085nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1086 struct nfsd4_secinfo *secinfo)
1087{
1088 DECODE_HEAD;
1089
1090 READ_BUF(4);
1091 READ32(secinfo->si_namelen);
1092 READ_BUF(secinfo->si_namelen);
1093 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001094 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001095 if (status)
1096 return status;
1097 DECODE_TAIL;
1098}
1099
1100static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001101nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1102 struct nfsd4_secinfo_no_name *sin)
1103{
1104 DECODE_HEAD;
1105
1106 READ_BUF(4);
1107 READ32(sin->sin_style);
1108 DECODE_TAIL;
1109}
1110
1111static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1113{
Benny Halevye31a1b62008-08-12 20:46:18 +03001114 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Benny Halevye31a1b62008-08-12 20:46:18 +03001116 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1117 if (status)
1118 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001119 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
David Quigley18032ca2013-05-02 13:19:10 -04001120 &setattr->sa_acl, &setattr->sa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Al Virob37ad282006-10-19 23:28:59 -07001123static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1125{
1126 DECODE_HEAD;
1127
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001128 if (argp->minorversion >= 1)
1129 return nfserr_notsupp;
1130
Chuck Leverab4684d2012-03-02 17:13:50 -05001131 READ_BUF(NFS4_VERIFIER_SIZE);
1132 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001134 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1135 if (status)
1136 return nfserr_bad_xdr;
1137 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 READ32(setclientid->se_callback_prog);
1139 READ32(setclientid->se_callback_netid_len);
1140
1141 READ_BUF(setclientid->se_callback_netid_len + 4);
1142 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1143 READ32(setclientid->se_callback_addr_len);
1144
1145 READ_BUF(setclientid->se_callback_addr_len + 4);
1146 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1147 READ32(setclientid->se_callback_ident);
1148
1149 DECODE_TAIL;
1150}
1151
Al Virob37ad282006-10-19 23:28:59 -07001152static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1154{
1155 DECODE_HEAD;
1156
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001157 if (argp->minorversion >= 1)
1158 return nfserr_notsupp;
1159
Chuck Leverab4684d2012-03-02 17:13:50 -05001160 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001162 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 DECODE_TAIL;
1165}
1166
1167/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001168static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1170{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 DECODE_HEAD;
1172
1173 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1174 goto out;
1175
1176 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001177 * nfsd4_proc_verify */
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 READ_BUF(4);
1180 READ32(verify->ve_attrlen);
1181 READ_BUF(verify->ve_attrlen);
1182 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1183
1184 DECODE_TAIL;
1185}
1186
Al Virob37ad282006-10-19 23:28:59 -07001187static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1189{
1190 int avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int len;
1192 DECODE_HEAD;
1193
Benny Halevye31a1b62008-08-12 20:46:18 +03001194 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1195 if (status)
1196 return status;
1197 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 READ64(write->wr_offset);
1199 READ32(write->wr_stable_how);
1200 if (write->wr_stable_how > 2)
1201 goto xdr_error;
1202 READ32(write->wr_buflen);
1203
1204 /* Sorry .. no magic macros for this.. *
1205 * READ_BUF(write->wr_buflen);
1206 * SAVEMEM(write->wr_buf, write->wr_buflen);
1207 */
1208 avail = (char*)argp->end - (char*)argp->p;
1209 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001210 dprintk("NFSD: xdr error (%s:%d)\n",
1211 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto xdr_error;
1213 }
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001214 write->wr_head.iov_base = p;
1215 write->wr_head.iov_len = avail;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001216 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001217 write->wr_pagelist = argp->pagelist;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001218
1219 len = XDR_QUADLEN(write->wr_buflen) << 2;
1220 if (len >= avail) {
1221 int pages;
1222
1223 len -= avail;
1224
1225 pages = len >> PAGE_SHIFT;
1226 argp->pagelist += pages;
1227 argp->pagelen -= pages * PAGE_SIZE;
1228 len -= pages * PAGE_SIZE;
1229
1230 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1231 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001233 argp->p += XDR_QUADLEN(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 DECODE_TAIL;
1236}
1237
Al Virob37ad282006-10-19 23:28:59 -07001238static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1240{
1241 DECODE_HEAD;
1242
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001243 if (argp->minorversion >= 1)
1244 return nfserr_notsupp;
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 READ_BUF(12);
1247 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1248 READ32(rlockowner->rl_owner.len);
1249 READ_BUF(rlockowner->rl_owner.len);
1250 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1251
Andy Adamson60adfc52009-04-03 08:28:50 +03001252 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1253 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 DECODE_TAIL;
1255}
1256
Al Virob37ad282006-10-19 23:28:59 -07001257static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001258nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001259 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001260{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001261 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001262 DECODE_HEAD;
1263
1264 READ_BUF(NFS4_VERIFIER_SIZE);
1265 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1266
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001267 status = nfsd4_decode_opaque(argp, &exid->clname);
1268 if (status)
1269 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001270
1271 READ_BUF(4);
1272 READ32(exid->flags);
1273
1274 /* Ignore state_protect4_a */
1275 READ_BUF(4);
1276 READ32(exid->spa_how);
1277 switch (exid->spa_how) {
1278 case SP4_NONE:
1279 break;
1280 case SP4_MACH_CRED:
1281 /* spo_must_enforce */
1282 READ_BUF(4);
1283 READ32(dummy);
1284 READ_BUF(dummy * 4);
1285 p += dummy;
1286
1287 /* spo_must_allow */
1288 READ_BUF(4);
1289 READ32(dummy);
1290 READ_BUF(dummy * 4);
1291 p += dummy;
1292 break;
1293 case SP4_SSV:
1294 /* ssp_ops */
1295 READ_BUF(4);
1296 READ32(dummy);
1297 READ_BUF(dummy * 4);
1298 p += dummy;
1299
1300 READ_BUF(4);
1301 READ32(dummy);
1302 READ_BUF(dummy * 4);
1303 p += dummy;
1304
1305 /* ssp_hash_algs<> */
1306 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001307 READ32(tmp);
1308 while (tmp--) {
1309 READ_BUF(4);
1310 READ32(dummy);
1311 READ_BUF(dummy);
1312 p += XDR_QUADLEN(dummy);
1313 }
Andy Adamson0733d212009-04-03 08:28:01 +03001314
1315 /* ssp_encr_algs<> */
1316 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001317 READ32(tmp);
1318 while (tmp--) {
1319 READ_BUF(4);
1320 READ32(dummy);
1321 READ_BUF(dummy);
1322 p += XDR_QUADLEN(dummy);
1323 }
Andy Adamson0733d212009-04-03 08:28:01 +03001324
1325 /* ssp_window and ssp_num_gss_handles */
1326 READ_BUF(8);
1327 READ32(dummy);
1328 READ32(dummy);
1329 break;
1330 default:
1331 goto xdr_error;
1332 }
1333
1334 /* Ignore Implementation ID */
1335 READ_BUF(4); /* nfs_impl_id4 array length */
1336 READ32(dummy);
1337
1338 if (dummy > 1)
1339 goto xdr_error;
1340
1341 if (dummy == 1) {
1342 /* nii_domain */
1343 READ_BUF(4);
1344 READ32(dummy);
1345 READ_BUF(dummy);
1346 p += XDR_QUADLEN(dummy);
1347
1348 /* nii_name */
1349 READ_BUF(4);
1350 READ32(dummy);
1351 READ_BUF(dummy);
1352 p += XDR_QUADLEN(dummy);
1353
1354 /* nii_date */
1355 READ_BUF(12);
1356 p += 3;
1357 }
1358 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001359}
1360
1361static __be32
1362nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1363 struct nfsd4_create_session *sess)
1364{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001365 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001366 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001367
1368 READ_BUF(16);
1369 COPYMEM(&sess->clientid, 8);
1370 READ32(sess->seqid);
1371 READ32(sess->flags);
1372
1373 /* Fore channel attrs */
1374 READ_BUF(28);
1375 READ32(dummy); /* headerpadsz is always 0 */
1376 READ32(sess->fore_channel.maxreq_sz);
1377 READ32(sess->fore_channel.maxresp_sz);
1378 READ32(sess->fore_channel.maxresp_cached);
1379 READ32(sess->fore_channel.maxops);
1380 READ32(sess->fore_channel.maxreqs);
1381 READ32(sess->fore_channel.nr_rdma_attrs);
1382 if (sess->fore_channel.nr_rdma_attrs == 1) {
1383 READ_BUF(4);
1384 READ32(sess->fore_channel.rdma_attrs);
1385 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1386 dprintk("Too many fore channel attr bitmaps!\n");
1387 goto xdr_error;
1388 }
1389
1390 /* Back channel attrs */
1391 READ_BUF(28);
1392 READ32(dummy); /* headerpadsz is always 0 */
1393 READ32(sess->back_channel.maxreq_sz);
1394 READ32(sess->back_channel.maxresp_sz);
1395 READ32(sess->back_channel.maxresp_cached);
1396 READ32(sess->back_channel.maxops);
1397 READ32(sess->back_channel.maxreqs);
1398 READ32(sess->back_channel.nr_rdma_attrs);
1399 if (sess->back_channel.nr_rdma_attrs == 1) {
1400 READ_BUF(4);
1401 READ32(sess->back_channel.rdma_attrs);
1402 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1403 dprintk("Too many back channel attr bitmaps!\n");
1404 goto xdr_error;
1405 }
1406
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001407 READ_BUF(4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001408 READ32(sess->callback_prog);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001409 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001410 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001411}
1412
1413static __be32
1414nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1415 struct nfsd4_destroy_session *destroy_session)
1416{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001417 DECODE_HEAD;
1418 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1419 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1420
1421 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001422}
1423
1424static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001425nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1426 struct nfsd4_free_stateid *free_stateid)
1427{
1428 DECODE_HEAD;
1429
1430 READ_BUF(sizeof(stateid_t));
1431 READ32(free_stateid->fr_stateid.si_generation);
1432 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1433
1434 DECODE_TAIL;
1435}
1436
1437static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001438nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1439 struct nfsd4_sequence *seq)
1440{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001441 DECODE_HEAD;
1442
1443 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1444 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1445 READ32(seq->seqid);
1446 READ32(seq->slotid);
1447 READ32(seq->maxslots);
1448 READ32(seq->cachethis);
1449
1450 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001451}
1452
Bryan Schumaker17456802011-07-13 10:50:48 -04001453static __be32
1454nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1455{
Bryan Schumaker17456802011-07-13 10:50:48 -04001456 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001457 __be32 *p, status;
1458 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001459
1460 READ_BUF(4);
1461 test_stateid->ts_num_ids = ntohl(*p++);
1462
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001463 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001464
1465 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001466 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1467 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001468 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001469 goto out;
1470 }
1471
1472 defer_free(argp, kfree, stateid);
1473 INIT_LIST_HEAD(&stateid->ts_id_list);
1474 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1475
1476 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001477 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001478 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001479 }
1480
1481 status = 0;
1482out:
1483 return status;
1484xdr_error:
1485 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1486 status = nfserr_bad_xdr;
1487 goto out;
1488}
1489
Mi Jinlong345c2842011-10-20 17:51:39 +08001490static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1491{
1492 DECODE_HEAD;
1493
1494 READ_BUF(8);
1495 COPYMEM(&dc->clientid, 8);
1496
1497 DECODE_TAIL;
1498}
1499
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001500static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1501{
1502 DECODE_HEAD;
1503
1504 READ_BUF(4);
1505 READ32(rc->rca_one_fs);
1506
1507 DECODE_TAIL;
1508}
1509
Andy Adamson2db134e2009-04-03 08:27:55 +03001510static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001511nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1512{
1513 return nfs_ok;
1514}
1515
Benny Halevy3c375c62008-07-02 11:14:01 +03001516static __be32
1517nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1518{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001519 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001520}
1521
Benny Halevy347e0ad2008-07-02 11:13:41 +03001522typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1523
1524static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001525 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1526 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1527 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1528 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1529 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1530 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1531 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1532 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1533 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1534 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1535 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1536 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1537 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1538 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1539 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1540 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1541 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1542 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1543 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1544 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001545 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001546 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1547 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1548 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1549 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1550 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1551 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1552 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1553 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1554 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1555 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1556 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1557 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1558 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1559 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1560 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1561 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03001562
1563 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001564 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001565 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001566 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1567 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1568 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001569 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001570 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1571 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1572 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1573 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1574 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1575 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001576 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001577 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1578 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001579 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001580 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001581 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001582 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001583};
1584
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001585static inline bool
1586nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1587{
Anna Schumaker8217d142013-10-30 13:38:13 -04001588 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001589 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001590 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001591 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001592 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1593 return false;
1594 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001595 return false;
1596 return true;
1597}
Benny Halevyf2feb962008-07-02 11:14:22 +03001598
Benny Halevy347e0ad2008-07-02 11:13:41 +03001599static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1601{
1602 DECODE_HEAD;
1603 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05001604 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int i;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 READ_BUF(4);
1608 READ32(argp->taglen);
1609 READ_BUF(argp->taglen + 8);
1610 SAVEMEM(argp->tag, argp->taglen);
1611 READ32(argp->minorversion);
1612 READ32(argp->opcnt);
1613
1614 if (argp->taglen > NFSD4_MAX_TAGLEN)
1615 goto xdr_error;
1616 if (argp->opcnt > 100)
1617 goto xdr_error;
1618
Tobias Klausere8c96f82006-03-24 03:15:34 -08001619 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1621 if (!argp->ops) {
1622 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001623 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto xdr_error;
1625 }
1626 }
1627
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001628 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03001629 argp->opcnt = 0;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 for (i = 0; i < argp->opcnt; i++) {
1632 op = &argp->ops[i];
1633 op->replay = NULL;
1634
J. Bruce Fields8a61b182012-11-16 22:28:38 -05001635 READ_BUF(4);
1636 READ32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001638 if (nfsd4_opnum_in_range(argp, op))
1639 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001640 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 op->opnum = OP_ILLEGAL;
1642 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
1645 if (op->status) {
1646 argp->opcnt = i+1;
1647 break;
1648 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001649 /*
1650 * We'll try to cache the result in the DRC if any one
1651 * op in the compound wants to be cached:
1652 */
1653 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001655 /* Sessions make the DRC unnecessary: */
1656 if (argp->minorversion)
1657 cachethis = false;
1658 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 DECODE_TAIL;
1661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663#define WRITE32(n) *p++ = htonl(n)
1664#define WRITE64(n) do { \
1665 *p++ = htonl((u32)((n) >> 32)); \
1666 *p++ = htonl((u32)(n)); \
1667} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001668#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1670 memcpy(p, ptr, nbytes); \
1671 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001672}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001673
1674static void write32(__be32 **p, u32 n)
1675{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001676 *(*p)++ = htonl(n);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001677}
1678
1679static void write64(__be32 **p, u64 n)
1680{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001681 write32(p, (n >> 32));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001682 write32(p, (u32)n);
1683}
1684
1685static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1686{
1687 if (IS_I_VERSION(inode)) {
1688 write64(p, inode->i_version);
1689 } else {
1690 write32(p, stat->ctime.tv_sec);
1691 write32(p, stat->ctime.tv_nsec);
1692 }
1693}
1694
1695static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1696{
1697 write32(p, c->atomic);
1698 if (c->change_supported) {
1699 write64(p, c->before_change);
1700 write64(p, c->after_change);
1701 } else {
1702 write32(p, c->before_ctime_sec);
1703 write32(p, c->before_ctime_nsec);
1704 write32(p, c->after_ctime_sec);
1705 write32(p, c->after_ctime_nsec);
1706 }
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709#define RESERVE_SPACE(nbytes) do { \
1710 p = resp->p; \
1711 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1712} while (0)
1713#define ADJUST_ARGS() resp->p = p
1714
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001715/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001716 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001717 */
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001718static __be32 nfsd4_encode_components_esc(char sep, char *components,
1719 __be32 **pp, int *buflen,
1720 char esc_enter, char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001721{
Al Viro2ebbc012006-10-19 23:28:58 -07001722 __be32 *p = *pp;
1723 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001724 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001725 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001726
1727 dprintk("nfsd4_encode_components(%s)\n", components);
1728 if ((*buflen -= 4) < 0)
1729 return nfserr_resource;
1730 WRITE32(0); /* We will fill this in with @count later */
1731 end = str = components;
1732 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001733 bool found_esc = false;
1734
1735 /* try to parse as esc_start, ..., esc_end, sep */
1736 if (*str == esc_enter) {
1737 for (; *end && (*end != esc_exit); end++)
1738 /* find esc_exit or end of string */;
1739 next = end + 1;
1740 if (*end && (!*next || *next == sep)) {
1741 str++;
1742 found_esc = true;
1743 }
1744 }
1745
1746 if (!found_esc)
1747 for (; *end && (*end != sep); end++)
1748 /* find sep or end of string */;
1749
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001750 strlen = end - str;
1751 if (strlen) {
1752 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1753 return nfserr_resource;
1754 WRITE32(strlen);
1755 WRITEMEM(str, strlen);
1756 count++;
1757 }
1758 else
1759 end++;
1760 str = end;
1761 }
1762 *pp = p;
1763 p = countp;
1764 WRITE32(count);
1765 return 0;
1766}
1767
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001768/* Encode as an array of strings the string given with components
1769 * separated @sep.
1770 */
1771static __be32 nfsd4_encode_components(char sep, char *components,
1772 __be32 **pp, int *buflen)
1773{
1774 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1775}
1776
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001777/*
1778 * encode a location element of a fs_locations structure
1779 */
Al Virob37ad282006-10-19 23:28:59 -07001780static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001781 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001782{
Al Virob37ad282006-10-19 23:28:59 -07001783 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001784 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001785
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001786 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1787 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001788 if (status)
1789 return status;
1790 status = nfsd4_encode_components('/', location->path, &p, buflen);
1791 if (status)
1792 return status;
1793 *pp = p;
1794 return 0;
1795}
1796
1797/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001798 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001799 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001800static __be32 nfsd4_encode_path(const struct path *root,
1801 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001802{
Al Viro301f0262013-09-01 16:06:39 -04001803 struct path cur = *path;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001804 __be32 *p = *pp;
1805 struct dentry **components = NULL;
1806 unsigned int ncomponents = 0;
1807 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001808
Trond Myklebusted748aa2011-09-12 19:37:06 -04001809 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001810
Trond Myklebusted748aa2011-09-12 19:37:06 -04001811 path_get(&cur);
1812 /* First walk the path up to the nfsd root, and store the
1813 * dentries/path components in an array.
1814 */
1815 for (;;) {
1816 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1817 break;
1818 if (cur.dentry == cur.mnt->mnt_root) {
1819 if (follow_up(&cur))
1820 continue;
1821 goto out_free;
1822 }
1823 if ((ncomponents & 15) == 0) {
1824 struct dentry **new;
1825 new = krealloc(components,
1826 sizeof(*new) * (ncomponents + 16),
1827 GFP_KERNEL);
1828 if (!new)
1829 goto out_free;
1830 components = new;
1831 }
1832 components[ncomponents++] = cur.dentry;
1833 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001834 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001835
1836 *buflen -= 4;
1837 if (*buflen < 0)
1838 goto out_free;
1839 WRITE32(ncomponents);
1840
1841 while (ncomponents) {
1842 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04001843 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001844
Al Viro301f0262013-09-01 16:06:39 -04001845 spin_lock(&dentry->d_lock);
1846 len = dentry->d_name.len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001847 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
Al Viro301f0262013-09-01 16:06:39 -04001848 if (*buflen < 0) {
1849 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001850 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04001851 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001852 WRITE32(len);
1853 WRITEMEM(dentry->d_name.name, len);
1854 dprintk("/%s", dentry->d_name.name);
Al Viro301f0262013-09-01 16:06:39 -04001855 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001856 dput(dentry);
1857 ncomponents--;
1858 }
1859
1860 *pp = p;
1861 err = 0;
1862out_free:
1863 dprintk(")\n");
1864 while (ncomponents)
1865 dput(components[--ncomponents]);
1866 kfree(components);
1867 path_put(&cur);
1868 return err;
1869}
1870
1871static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1872 const struct path *path, __be32 **pp, int *buflen)
1873{
1874 struct svc_export *exp_ps;
1875 __be32 res;
1876
1877 exp_ps = rqst_find_fsidzero_export(rqstp);
1878 if (IS_ERR(exp_ps))
1879 return nfserrno(PTR_ERR(exp_ps));
1880 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1881 exp_put(exp_ps);
1882 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001883}
1884
1885/*
1886 * encode a fs_locations structure
1887 */
Al Virob37ad282006-10-19 23:28:59 -07001888static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001889 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001890 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001891{
Al Virob37ad282006-10-19 23:28:59 -07001892 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001893 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001894 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001895 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001896
Trond Myklebusted748aa2011-09-12 19:37:06 -04001897 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001898 if (status)
1899 return status;
1900 if ((*buflen -= 4) < 0)
1901 return nfserr_resource;
1902 WRITE32(fslocs->locations_count);
1903 for (i=0; i<fslocs->locations_count; i++) {
1904 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1905 &p, buflen);
1906 if (status)
1907 return status;
1908 }
1909 *pp = p;
1910 return 0;
1911}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001913static u32 nfs4_file_type(umode_t mode)
1914{
1915 switch (mode & S_IFMT) {
1916 case S_IFIFO: return NF4FIFO;
1917 case S_IFCHR: return NF4CHR;
1918 case S_IFDIR: return NF4DIR;
1919 case S_IFBLK: return NF4BLK;
1920 case S_IFLNK: return NF4LNK;
1921 case S_IFREG: return NF4REG;
1922 case S_IFSOCK: return NF4SOCK;
1923 default: return NF4BAD;
1924 };
1925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Al Virob37ad282006-10-19 23:28:59 -07001927static __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001928nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
Al Viro2ebbc012006-10-19 23:28:58 -07001929 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int status;
1932
1933 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1934 return nfserr_resource;
1935 if (whotype != NFS4_ACL_WHO_NAMED)
1936 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001937 else if (gid_valid(gid))
1938 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 else
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001940 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (status < 0)
1942 return nfserrno(status);
1943 *p = xdr_encode_opaque(*p, NULL, status);
1944 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1945 BUG_ON(*buflen < 0);
1946 return 0;
1947}
1948
Al Virob37ad282006-10-19 23:28:59 -07001949static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001950nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001952 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1953 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Al Virob37ad282006-10-19 23:28:59 -07001956static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001957nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001959 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1960 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Al Virob37ad282006-10-19 23:28:59 -07001963static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001964nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
Al Viro2ebbc012006-10-19 23:28:58 -07001965 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001967 kuid_t uid = INVALID_UID;
1968 kgid_t gid = INVALID_GID;
1969
1970 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1971 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1972 gid = ace->who_gid;
1973 else
1974 uid = ace->who_uid;
1975 }
1976 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001979#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1980 FATTR4_WORD0_RDATTR_ERROR)
1981#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1982
David Quigley18032ca2013-05-02 13:19:10 -04001983#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1984static inline __be32
1985nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
1986{
1987 __be32 *p = *pp;
1988
1989 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
1990 return nfserr_resource;
1991
1992 /*
1993 * For now we use a 0 here to indicate the null translation; in
1994 * the future we may place a call to translation code here.
1995 */
1996 if ((*buflen -= 8) < 0)
1997 return nfserr_resource;
1998
1999 WRITE32(0); /* lfs */
2000 WRITE32(0); /* pi */
2001 p = xdr_encode_opaque(p, context, len);
2002 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2003
2004 *pp = p;
2005 return 0;
2006}
2007#else
2008static inline __be32
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002009nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
David Quigley18032ca2013-05-02 13:19:10 -04002010{ return 0; }
2011#endif
2012
Al Virob37ad282006-10-19 23:28:59 -07002013static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002014{
2015 /* As per referral draft: */
2016 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2017 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2018 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2019 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2020 *rdattr_err = NFSERR_MOVED;
2021 else
2022 return nfserr_moved;
2023 }
2024 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2025 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2026 return 0;
2027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002029
2030static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2031{
2032 struct path path = exp->ex_path;
2033 int err;
2034
2035 path_get(&path);
2036 while (follow_up(&path)) {
2037 if (path.dentry != path.mnt->mnt_root)
2038 break;
2039 }
Al Viro3dadecc2013-01-24 02:18:08 -05002040 err = vfs_getattr(&path, stat);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002041 path_put(&path);
2042 return err;
2043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/*
2046 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2047 * ourselves.
2048 *
J. Bruce Fields84822d02012-12-14 17:57:50 -05002049 * countp is the buffer size in _words_
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Al Virob37ad282006-10-19 23:28:59 -07002051__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002053 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002054 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 u32 bmval0 = bmval[0];
2057 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002058 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct kstat stat;
2060 struct svc_fh tempfh;
2061 struct kstatfs statfs;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002062 int buflen = count << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002063 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 u32 dummy;
2065 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002066 u32 rdattr_err = 0;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002067 __be32 *p = *buffer;
Al Virob37ad282006-10-19 23:28:59 -07002068 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002069 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 int aclsupport = 0;
2071 struct nfs4_acl *acl = NULL;
David Quigley18032ca2013-05-02 13:19:10 -04002072 void *context = NULL;
2073 int contextlen;
2074 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002075 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2076 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002077 struct path path = {
2078 .mnt = exp->ex_path.mnt,
2079 .dentry = dentry,
2080 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002081 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002084 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2085 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2086 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002088 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002089 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002090 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2091 if (status)
2092 goto out;
2093 }
2094
Al Viro3dadecc2013-01-24 02:18:08 -05002095 err = vfs_getattr(&path, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002096 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002098 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2099 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2101 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002102 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002103 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto out_nfserr;
2105 }
2106 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2107 fh_init(&tempfh, NFS4_FHSIZE);
2108 status = fh_compose(&tempfh, exp, dentry, NULL);
2109 if (status)
2110 goto out;
2111 fhp = &tempfh;
2112 }
2113 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2114 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002115 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2116 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002118 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002120 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 status = nfserr_attrnotsupp;
2122 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002123 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 goto out_nfserr;
2125 }
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
David Quigley18032ca2013-05-02 13:19:10 -04002128#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2129 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2130 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2131 err = security_inode_getsecctx(dentry->d_inode,
2132 &context, &contextlen);
2133 contextsupport = (err == 0);
2134 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2135 if (err == -EOPNOTSUPP)
2136 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2137 else if (err)
2138 goto out_nfserr;
2139 }
2140 }
2141#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2142
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002143 if (bmval2) {
2144 if ((buflen -= 16) < 0)
2145 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002146 WRITE32(3);
2147 WRITE32(bmval0);
2148 WRITE32(bmval1);
2149 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002150 } else if (bmval1) {
2151 if ((buflen -= 12) < 0)
2152 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002153 WRITE32(2);
2154 WRITE32(bmval0);
2155 WRITE32(bmval1);
2156 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002157 if ((buflen -= 8) < 0)
2158 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002159 WRITE32(1);
2160 WRITE32(bmval0);
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 attrlenp = p++; /* to be backfilled later */
2163
2164 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002165 u32 word0 = nfsd_suppattrs0(minorversion);
2166 u32 word1 = nfsd_suppattrs1(minorversion);
2167 u32 word2 = nfsd_suppattrs2(minorversion);
2168
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002169 if (!aclsupport)
2170 word0 &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002171 if (!contextsupport)
2172 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
Andy Adamson7e705702009-04-03 08:29:11 +03002173 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002174 if ((buflen -= 12) < 0)
2175 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002176 WRITE32(2);
2177 WRITE32(word0);
2178 WRITE32(word1);
2179 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002180 if ((buflen -= 16) < 0)
2181 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002182 WRITE32(3);
2183 WRITE32(word0);
2184 WRITE32(word1);
2185 WRITE32(word2);
2186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188 if (bmval0 & FATTR4_WORD0_TYPE) {
2189 if ((buflen -= 4) < 0)
2190 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002191 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (dummy == NF4BAD)
2193 goto out_serverfault;
2194 WRITE32(dummy);
2195 }
2196 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2197 if ((buflen -= 4) < 0)
2198 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002199 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002200 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002201 else
NeilBrowne34ac862005-07-07 17:59:30 -07002202 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if ((buflen -= 8) < 0)
2206 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002207 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 if (bmval0 & FATTR4_WORD0_SIZE) {
2210 if ((buflen -= 8) < 0)
2211 goto out_resource;
2212 WRITE64(stat.size);
2213 }
2214 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2215 if ((buflen -= 4) < 0)
2216 goto out_resource;
2217 WRITE32(1);
2218 }
2219 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2220 if ((buflen -= 4) < 0)
2221 goto out_resource;
2222 WRITE32(1);
2223 }
2224 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2225 if ((buflen -= 4) < 0)
2226 goto out_resource;
2227 WRITE32(0);
2228 }
2229 if (bmval0 & FATTR4_WORD0_FSID) {
2230 if ((buflen -= 16) < 0)
2231 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002232 if (exp->ex_fslocs.migrated) {
2233 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2234 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002235 } else switch(fsid_source(fhp)) {
2236 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 WRITE64((u64)exp->ex_fsid);
2238 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002239 break;
2240 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 WRITE32(0);
2242 WRITE32(MAJOR(stat.dev));
2243 WRITE32(0);
2244 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002245 break;
2246 case FSIDSOURCE_UUID:
2247 WRITEMEM(exp->ex_uuid, 16);
2248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 }
2251 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2252 if ((buflen -= 4) < 0)
2253 goto out_resource;
2254 WRITE32(0);
2255 }
2256 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2257 if ((buflen -= 4) < 0)
2258 goto out_resource;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002259 WRITE32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2262 if ((buflen -= 4) < 0)
2263 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002264 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266 if (bmval0 & FATTR4_WORD0_ACL) {
2267 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 if (acl == NULL) {
2270 if ((buflen -= 4) < 0)
2271 goto out_resource;
2272
2273 WRITE32(0);
2274 goto out_acl;
2275 }
2276 if ((buflen -= 4) < 0)
2277 goto out_resource;
2278 WRITE32(acl->naces);
2279
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002280 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if ((buflen -= 4*3) < 0)
2282 goto out_resource;
2283 WRITE32(ace->type);
2284 WRITE32(ace->flag);
2285 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002286 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (status == nfserr_resource)
2288 goto out_resource;
2289 if (status)
2290 goto out;
2291 }
2292 }
2293out_acl:
2294 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2295 if ((buflen -= 4) < 0)
2296 goto out_resource;
2297 WRITE32(aclsupport ?
2298 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2299 }
2300 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2301 if ((buflen -= 4) < 0)
2302 goto out_resource;
2303 WRITE32(1);
2304 }
2305 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2306 if ((buflen -= 4) < 0)
2307 goto out_resource;
J. Bruce Fields2930d382012-06-05 16:52:06 -04002308 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2311 if ((buflen -= 4) < 0)
2312 goto out_resource;
2313 WRITE32(1);
2314 }
2315 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2316 if ((buflen -= 4) < 0)
2317 goto out_resource;
2318 WRITE32(1);
2319 }
2320 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2321 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2322 if (buflen < 0)
2323 goto out_resource;
2324 WRITE32(fhp->fh_handle.fh_size);
2325 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2326 }
2327 if (bmval0 & FATTR4_WORD0_FILEID) {
2328 if ((buflen -= 8) < 0)
2329 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002330 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2333 if ((buflen -= 8) < 0)
2334 goto out_resource;
2335 WRITE64((u64) statfs.f_ffree);
2336 }
2337 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2338 if ((buflen -= 8) < 0)
2339 goto out_resource;
2340 WRITE64((u64) statfs.f_ffree);
2341 }
2342 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2343 if ((buflen -= 8) < 0)
2344 goto out_resource;
2345 WRITE64((u64) statfs.f_files);
2346 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002347 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2348 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2349 if (status == nfserr_resource)
2350 goto out_resource;
2351 if (status)
2352 goto out;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2355 if ((buflen -= 4) < 0)
2356 goto out_resource;
2357 WRITE32(1);
2358 }
2359 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2360 if ((buflen -= 8) < 0)
2361 goto out_resource;
2362 WRITE64(~(u64)0);
2363 }
2364 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2365 if ((buflen -= 4) < 0)
2366 goto out_resource;
2367 WRITE32(255);
2368 }
2369 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2370 if ((buflen -= 4) < 0)
2371 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002372 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2375 if ((buflen -= 8) < 0)
2376 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002377 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2380 if ((buflen -= 8) < 0)
2381 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002382 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 if (bmval1 & FATTR4_WORD1_MODE) {
2385 if ((buflen -= 4) < 0)
2386 goto out_resource;
2387 WRITE32(stat.mode & S_IALLUGO);
2388 }
2389 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2390 if ((buflen -= 4) < 0)
2391 goto out_resource;
2392 WRITE32(1);
2393 }
2394 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2395 if ((buflen -= 4) < 0)
2396 goto out_resource;
2397 WRITE32(stat.nlink);
2398 }
2399 if (bmval1 & FATTR4_WORD1_OWNER) {
2400 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2401 if (status == nfserr_resource)
2402 goto out_resource;
2403 if (status)
2404 goto out;
2405 }
2406 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2407 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2408 if (status == nfserr_resource)
2409 goto out_resource;
2410 if (status)
2411 goto out;
2412 }
2413 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2414 if ((buflen -= 8) < 0)
2415 goto out_resource;
2416 WRITE32((u32) MAJOR(stat.rdev));
2417 WRITE32((u32) MINOR(stat.rdev));
2418 }
2419 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2420 if ((buflen -= 8) < 0)
2421 goto out_resource;
2422 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2423 WRITE64(dummy64);
2424 }
2425 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2426 if ((buflen -= 8) < 0)
2427 goto out_resource;
2428 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2429 WRITE64(dummy64);
2430 }
2431 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2432 if ((buflen -= 8) < 0)
2433 goto out_resource;
2434 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2435 WRITE64(dummy64);
2436 }
2437 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2438 if ((buflen -= 8) < 0)
2439 goto out_resource;
2440 dummy64 = (u64)stat.blocks << 9;
2441 WRITE64(dummy64);
2442 }
2443 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2444 if ((buflen -= 12) < 0)
2445 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002446 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 WRITE32(stat.atime.tv_nsec);
2448 }
2449 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2450 if ((buflen -= 12) < 0)
2451 goto out_resource;
2452 WRITE32(0);
2453 WRITE32(1);
2454 WRITE32(0);
2455 }
2456 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2457 if ((buflen -= 12) < 0)
2458 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002459 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 WRITE32(stat.ctime.tv_nsec);
2461 }
2462 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2463 if ((buflen -= 12) < 0)
2464 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002465 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 WRITE32(stat.mtime.tv_nsec);
2467 }
2468 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if ((buflen -= 8) < 0)
2470 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002471 /*
2472 * Get parent's attributes if not ignoring crossmount
2473 * and this is the root of a cross-mounted filesystem.
2474 */
2475 if (ignore_crossmnt == 0 &&
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002476 dentry == exp->ex_path.mnt->mnt_root)
2477 get_parent_attributes(exp, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002478 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
David Quigley18032ca2013-05-02 13:19:10 -04002480 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2481 status = nfsd4_encode_security_label(rqstp, context,
2482 contextlen, &p, &buflen);
2483 if (status)
2484 goto out;
2485 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002486 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2487 WRITE32(3);
2488 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2489 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2490 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2491 }
Andy Adamson7e705702009-04-03 08:29:11 +03002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
J. Bruce Fields84822d02012-12-14 17:57:50 -05002494 *buffer = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 status = nfs_ok;
2496
2497out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002498#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002499 if (context)
2500 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002501#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002502 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (fhp == &tempfh)
2504 fh_put(&tempfh);
2505 return status;
2506out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002507 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 goto out;
2509out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 status = nfserr_resource;
2511 goto out;
2512out_serverfault:
2513 status = nfserr_serverfault;
2514 goto out;
2515}
2516
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002517static inline int attributes_need_mount(u32 *bmval)
2518{
2519 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2520 return 1;
2521 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2522 return 1;
2523 return 0;
2524}
2525
Al Virob37ad282006-10-19 23:28:59 -07002526static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002528 const char *name, int namlen, __be32 **p, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 struct svc_export *exp = cd->rd_fhp->fh_export;
2531 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002532 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002533 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2536 if (IS_ERR(dentry))
2537 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002538 if (!dentry->d_inode) {
2539 /*
2540 * nfsd_buffered_readdir drops the i_mutex between
2541 * readdir and calling this callback, leaving a window
2542 * where this directory entry could have gone away.
2543 */
2544 dput(dentry);
2545 return nfserr_noent;
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002549 /*
2550 * In the case of a mountpoint, the client may be asking for
2551 * attributes that are only properties of the underlying filesystem
2552 * as opposed to the cross-mounted file system. In such a case,
2553 * we will not follow the cross mount and will fill the attribtutes
2554 * directly from the mountpoint dentry.
2555 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002556 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002557 int err;
2558
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002559 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2560 && !attributes_need_mount(cd->rd_bmval)) {
2561 ignore_crossmnt = 1;
2562 goto out_encode;
2563 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002564 /*
2565 * Why the heck aren't we just using nfsd_lookup??
2566 * Different "."/".." handling? Something else?
2567 * At least, add a comment here to explain....
2568 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002569 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2570 if (err) {
2571 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 goto out_put;
2573 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002574 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2575 if (nfserr)
2576 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002579out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002581 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582out_put:
2583 dput(dentry);
2584 exp_put(exp);
2585 return nfserr;
2586}
2587
Al Viro2ebbc012006-10-19 23:28:58 -07002588static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002589nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Al Viro2ebbc012006-10-19 23:28:58 -07002591 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 if (buflen < 6)
2594 return NULL;
2595 *p++ = htonl(2);
2596 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2597 *p++ = htonl(0); /* bmval1 */
2598
2599 attrlenp = p++;
2600 *p++ = nfserr; /* no htonl */
2601 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2602 return p;
2603}
2604
2605static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002606nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2607 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002609 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2611 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002612 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002613 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002614 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 /* In nfsv4, "." and ".." never make it onto the wire.. */
2617 if (name && isdotent(name, namlen)) {
2618 cd->common.err = nfs_ok;
2619 return 0;
2620 }
2621
2622 if (cd->offset)
2623 xdr_encode_hyper(cd->offset, (u64) offset);
2624
2625 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2626 if (buflen < 0)
2627 goto fail;
2628
2629 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002630 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2632 p = xdr_encode_array(p, name, namlen); /* name length & name */
2633
J. Bruce Fields84822d02012-12-14 17:57:50 -05002634 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 switch (nfserr) {
2636 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 case nfserr_resource:
2639 nfserr = nfserr_toosmall;
2640 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002641 case nfserr_noent:
2642 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 default:
2644 /*
2645 * If the client requested the RDATTR_ERROR attribute,
2646 * we stuff the error code into this attribute
2647 * and continue. If this attribute was not requested,
2648 * then in accordance with the spec, we fail the
2649 * entire READDIR operation(!)
2650 */
2651 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2652 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002654 if (p == NULL) {
2655 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659 cd->buflen -= (p - cd->buffer);
2660 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002661 cd->offset = cookiep;
2662skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 cd->common.err = nfs_ok;
2664 return 0;
2665fail:
2666 cd->common.err = nfserr;
2667 return -EINVAL;
2668}
2669
Benny Halevye2f282b2008-08-12 20:45:07 +03002670static void
2671nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2672{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002673 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002674
2675 RESERVE_SPACE(sizeof(stateid_t));
2676 WRITE32(sid->si_generation);
2677 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2678 ADJUST_ARGS();
2679}
2680
Benny Halevy695e12f2008-07-04 14:38:33 +03002681static __be32
Al Virob37ad282006-10-19 23:28:59 -07002682nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002684 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 if (!nfserr) {
2687 RESERVE_SPACE(8);
2688 WRITE32(access->ac_supported);
2689 WRITE32(access->ac_resp_access);
2690 ADJUST_ARGS();
2691 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002692 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002695static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2696{
2697 __be32 *p;
2698
2699 if (!nfserr) {
2700 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2701 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2702 WRITE32(bcts->dir);
J. Bruce Fields6e67b5d2012-09-13 09:49:43 -04002703 /* Sorry, we do not yet support RDMA over 4.1: */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002704 WRITE32(0);
2705 ADJUST_ARGS();
2706 }
2707 return nfserr;
2708}
2709
Benny Halevy695e12f2008-07-04 14:38:33 +03002710static __be32
Al Virob37ad282006-10-19 23:28:59 -07002711nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Benny Halevye2f282b2008-08-12 20:45:07 +03002713 if (!nfserr)
2714 nfsd4_encode_stateid(resp, &close->cl_stateid);
2715
Benny Halevy695e12f2008-07-04 14:38:33 +03002716 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
2719
Benny Halevy695e12f2008-07-04 14:38:33 +03002720static __be32
Al Virob37ad282006-10-19 23:28:59 -07002721nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002723 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002726 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2727 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 ADJUST_ARGS();
2729 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002730 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Benny Halevy695e12f2008-07-04 14:38:33 +03002733static __be32
Al Virob37ad282006-10-19 23:28:59 -07002734nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002736 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 if (!nfserr) {
2739 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002740 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 WRITE32(2);
2742 WRITE32(create->cr_bmval[0]);
2743 WRITE32(create->cr_bmval[1]);
2744 ADJUST_ARGS();
2745 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002746 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Al Virob37ad282006-10-19 23:28:59 -07002749static __be32
2750nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 struct svc_fh *fhp = getattr->ga_fhp;
2753 int buflen;
2754
2755 if (nfserr)
2756 return nfserr;
2757
2758 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2759 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002760 &resp->p, buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002761 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return nfserr;
2763}
2764
Benny Halevy695e12f2008-07-04 14:38:33 +03002765static __be32
2766nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Benny Halevy695e12f2008-07-04 14:38:33 +03002768 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002770 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 if (!nfserr) {
2773 len = fhp->fh_handle.fh_size;
2774 RESERVE_SPACE(len + 4);
2775 WRITE32(len);
2776 WRITEMEM(&fhp->fh_handle.fh_base, len);
2777 ADJUST_ARGS();
2778 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002779 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
2782/*
2783* Including all fields other than the name, a LOCK4denied structure requires
2784* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2785*/
2786static void
2787nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2788{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002789 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002790 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002792 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 WRITE64(ld->ld_start);
2794 WRITE64(ld->ld_length);
2795 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002796 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002798 WRITE32(conf->len);
2799 WRITEMEM(conf->data, conf->len);
2800 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2802 WRITE64((u64)0); /* clientid */
2803 WRITE32(0); /* length of owner name */
2804 }
2805 ADJUST_ARGS();
2806}
2807
Benny Halevy695e12f2008-07-04 14:38:33 +03002808static __be32
Al Virob37ad282006-10-19 23:28:59 -07002809nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Benny Halevye2f282b2008-08-12 20:45:07 +03002811 if (!nfserr)
2812 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2813 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2815
Benny Halevy695e12f2008-07-04 14:38:33 +03002816 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
Benny Halevy695e12f2008-07-04 14:38:33 +03002819static __be32
Al Virob37ad282006-10-19 23:28:59 -07002820nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 if (nfserr == nfserr_denied)
2823 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002824 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
Benny Halevy695e12f2008-07-04 14:38:33 +03002827static __be32
Al Virob37ad282006-10-19 23:28:59 -07002828nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Benny Halevye2f282b2008-08-12 20:45:07 +03002830 if (!nfserr)
2831 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2832
Benny Halevy695e12f2008-07-04 14:38:33 +03002833 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836
Benny Halevy695e12f2008-07-04 14:38:33 +03002837static __be32
Al Virob37ad282006-10-19 23:28:59 -07002838nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002840 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 if (!nfserr) {
2843 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002844 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 ADJUST_ARGS();
2846 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002847 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
2850
Benny Halevy695e12f2008-07-04 14:38:33 +03002851static __be32
Al Virob37ad282006-10-19 23:28:59 -07002852nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002854 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 if (nfserr)
2857 goto out;
2858
Benny Halevye2f282b2008-08-12 20:45:07 +03002859 nfsd4_encode_stateid(resp, &open->op_stateid);
2860 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002861 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 WRITE32(open->op_rflags);
2863 WRITE32(2);
2864 WRITE32(open->op_bmval[0]);
2865 WRITE32(open->op_bmval[1]);
2866 WRITE32(open->op_delegate_type);
2867 ADJUST_ARGS();
2868
2869 switch (open->op_delegate_type) {
2870 case NFS4_OPEN_DELEGATE_NONE:
2871 break;
2872 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002873 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2874 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002875 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 /*
2878 * TODO: ACE's in delegations
2879 */
2880 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2881 WRITE32(0);
2882 WRITE32(0);
2883 WRITE32(0); /* XXX: is NULL principal ok? */
2884 ADJUST_ARGS();
2885 break;
2886 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002887 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2888 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 WRITE32(0);
2890
2891 /*
2892 * TODO: space_limit's in delegations
2893 */
2894 WRITE32(NFS4_LIMIT_SIZE);
2895 WRITE32(~(u32)0);
2896 WRITE32(~(u32)0);
2897
2898 /*
2899 * TODO: ACE's in delegations
2900 */
2901 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2902 WRITE32(0);
2903 WRITE32(0);
2904 WRITE32(0); /* XXX: is NULL principal ok? */
2905 ADJUST_ARGS();
2906 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002907 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2908 switch (open->op_why_no_deleg) {
2909 case WND4_CONTENTION:
2910 case WND4_RESOURCE:
2911 RESERVE_SPACE(8);
2912 WRITE32(open->op_why_no_deleg);
2913 WRITE32(0); /* deleg signaling not supported yet */
2914 break;
2915 default:
2916 RESERVE_SPACE(4);
2917 WRITE32(open->op_why_no_deleg);
2918 }
2919 ADJUST_ARGS();
2920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 default:
2922 BUG();
2923 }
2924 /* XXX save filehandle here */
2925out:
Benny Halevy695e12f2008-07-04 14:38:33 +03002926 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Benny Halevy695e12f2008-07-04 14:38:33 +03002929static __be32
Al Virob37ad282006-10-19 23:28:59 -07002930nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Benny Halevye2f282b2008-08-12 20:45:07 +03002932 if (!nfserr)
2933 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Benny Halevy695e12f2008-07-04 14:38:33 +03002935 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
Benny Halevy695e12f2008-07-04 14:38:33 +03002938static __be32
Al Virob37ad282006-10-19 23:28:59 -07002939nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Benny Halevye2f282b2008-08-12 20:45:07 +03002941 if (!nfserr)
2942 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Benny Halevy695e12f2008-07-04 14:38:33 +03002944 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Al Virob37ad282006-10-19 23:28:59 -07002947static __be32
2948nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002949 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 u32 eof;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002952 int v;
2953 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 unsigned long maxcount;
2955 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002956 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 if (nfserr)
2959 return nfserr;
2960 if (resp->xbuf->page_len)
2961 return nfserr_resource;
2962
2963 RESERVE_SPACE(8); /* eof flag and byte count */
2964
Greg Banks7adae482006-10-04 02:15:47 -07002965 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (maxcount > read->rd_length)
2967 maxcount = read->rd_length;
2968
2969 len = maxcount;
2970 v = 0;
2971 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002972 page = *(resp->rqstp->rq_next_page);
2973 if (!page) { /* ran out of pages */
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05002974 maxcount -= len;
2975 break;
2976 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002977 resp->rqstp->rq_vec[v].iov_base = page_address(page);
NeilBrown3cc03b12006-10-04 02:15:47 -07002978 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002979 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002980 resp->rqstp->rq_next_page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 v++;
2982 len -= PAGE_SIZE;
2983 }
2984 read->rd_vlen = v;
2985
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002986 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002987 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 &maxcount);
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (nfserr)
2991 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002992 eof = (read->rd_offset + maxcount >=
2993 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 WRITE32(eof);
2996 WRITE32(maxcount);
2997 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002998 resp->xbuf->head[0].iov_len = (char*)p
2999 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 resp->xbuf->page_len = maxcount;
3001
NeilBrown6ed6dec2006-04-10 22:55:32 -07003002 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003003 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003006 RESERVE_SPACE(4);
3007 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 resp->xbuf->tail[0].iov_base += maxcount&3;
3009 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003010 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 return 0;
3013}
3014
Al Virob37ad282006-10-19 23:28:59 -07003015static __be32
3016nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018 int maxcount;
3019 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003020 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 if (nfserr)
3023 return nfserr;
3024 if (resp->xbuf->page_len)
3025 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003026 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003027 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003029 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 maxcount = PAGE_SIZE;
3032 RESERVE_SPACE(4);
3033
3034 /*
3035 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3036 * if they would overflow the buffer. Is this kosher in NFSv4? If
3037 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3038 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3039 */
3040 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3041 if (nfserr == nfserr_isdir)
3042 return nfserr_inval;
3043 if (nfserr)
3044 return nfserr;
3045
3046 WRITE32(maxcount);
3047 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003048 resp->xbuf->head[0].iov_len = (char*)p
3049 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003051
3052 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003053 resp->xbuf->tail[0].iov_base = p;
3054 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003056 RESERVE_SPACE(4);
3057 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 resp->xbuf->tail[0].iov_base += maxcount&3;
3059 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003060 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062 return 0;
3063}
3064
Al Virob37ad282006-10-19 23:28:59 -07003065static __be32
3066nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 int maxcount;
3069 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003070 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003071 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 if (nfserr)
3074 return nfserr;
3075 if (resp->xbuf->page_len)
3076 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003077 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003078 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Chuck Leverab4684d2012-03-02 17:13:50 -05003080 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 savep = p;
3082
3083 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3084 WRITE32(0);
3085 WRITE32(0);
3086 ADJUST_ARGS();
3087 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003088 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 maxcount = PAGE_SIZE;
3091 if (maxcount > readdir->rd_maxcount)
3092 maxcount = readdir->rd_maxcount;
3093
3094 /*
3095 * Convert from bytes to words, account for the two words already
3096 * written, make sure to leave two words at the end for the next
3097 * pointer and eof field.
3098 */
3099 maxcount = (maxcount >> 2) - 4;
3100 if (maxcount < 0) {
3101 nfserr = nfserr_toosmall;
3102 goto err_no_verf;
3103 }
3104
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003105 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 readdir->common.err = 0;
3107 readdir->buflen = maxcount;
3108 readdir->buffer = page;
3109 readdir->offset = NULL;
3110
3111 offset = readdir->rd_cookie;
3112 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3113 &offset,
3114 &readdir->common, nfsd4_encode_dirent);
3115 if (nfserr == nfs_ok &&
3116 readdir->common.err == nfserr_toosmall &&
3117 readdir->buffer == page)
3118 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (nfserr)
3120 goto err_no_verf;
3121
3122 if (readdir->offset)
3123 xdr_encode_hyper(readdir->offset, offset);
3124
3125 p = readdir->buffer;
3126 *p++ = 0; /* no more entries */
3127 *p++ = htonl(readdir->common.err == nfserr_eof);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003128 resp->xbuf->page_len = ((char*)p) -
3129 (char*)page_address(*(resp->rqstp->rq_next_page-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
NeilBrownbb6e8a92006-04-10 22:55:33 -07003131 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003132 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 resp->xbuf->tail[0].iov_len = 0;
3134 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003135 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 return 0;
3138err_no_verf:
3139 p = savep;
3140 ADJUST_ARGS();
3141 return nfserr;
3142}
3143
Benny Halevy695e12f2008-07-04 14:38:33 +03003144static __be32
Al Virob37ad282006-10-19 23:28:59 -07003145nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003147 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 if (!nfserr) {
3150 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003151 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 ADJUST_ARGS();
3153 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003154 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Benny Halevy695e12f2008-07-04 14:38:33 +03003157static __be32
Al Virob37ad282006-10-19 23:28:59 -07003158nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003160 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 if (!nfserr) {
3163 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003164 write_cinfo(&p, &rename->rn_sinfo);
3165 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 ADJUST_ARGS();
3167 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003168 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Benny Halevy695e12f2008-07-04 14:38:33 +03003171static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003172nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
Chuck Levera77c8062013-03-16 15:55:10 -04003173 __be32 nfserr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003174{
Chuck Lever676e4eb2013-04-30 18:48:54 -04003175 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003176 struct exp_flavor_info *flavs;
3177 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04003178 __be32 *p, *flavorsp;
3179 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003180
3181 if (nfserr)
3182 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003183 if (exp->ex_nflavors) {
3184 flavs = exp->ex_flavors;
3185 nflavs = exp->ex_nflavors;
3186 } else { /* Handling of some defaults in absence of real secinfo: */
3187 flavs = def_flavs;
3188 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3189 nflavs = 2;
3190 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3191 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3192 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3193 nflavs = 1;
3194 flavs[0].pseudoflavor
3195 = svcauth_gss_flavor(exp->ex_client);
3196 } else {
3197 nflavs = 1;
3198 flavs[0].pseudoflavor
3199 = exp->ex_client->flavour->flavour;
3200 }
3201 }
3202
Chuck Lever676e4eb2013-04-30 18:48:54 -04003203 supported = 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003204 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003205 flavorsp = p++; /* to be backfilled later */
Andy Adamsondcb488a32007-07-17 04:04:51 -07003206 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003207
J. Bruce Fields4796f452007-07-17 04:04:51 -07003208 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04003209 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04003210 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003211
Chuck Lever676e4eb2013-04-30 18:48:54 -04003212 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3213 supported++;
Chuck Levered9411a2013-04-30 18:48:45 -04003214 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003215 WRITE32(RPC_AUTH_GSS);
Chuck Levera77c8062013-03-16 15:55:10 -04003216 WRITE32(info.oid.len);
3217 WRITEMEM(info.oid.data, info.oid.len);
Chuck Levera77c8062013-03-16 15:55:10 -04003218 WRITE32(info.qop);
Chuck Levera77c8062013-03-16 15:55:10 -04003219 WRITE32(info.service);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003220 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003221 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3222 supported++;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003223 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003224 WRITE32(pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003225 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003226 } else {
3227 if (report)
3228 pr_warn("NFS: SECINFO: security flavor %u "
3229 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003230 }
3231 }
Chuck Levera77c8062013-03-16 15:55:10 -04003232
Chuck Lever676e4eb2013-04-30 18:48:54 -04003233 if (nflavs != supported)
3234 report = false;
3235 *flavorsp = htonl(supported);
3236
Andy Adamsondcb488a32007-07-17 04:04:51 -07003237out:
3238 if (exp)
3239 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003240 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003241}
3242
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003243static __be32
3244nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3245 struct nfsd4_secinfo *secinfo)
3246{
3247 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3248}
3249
3250static __be32
3251nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3252 struct nfsd4_secinfo_no_name *secinfo)
3253{
3254 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3255}
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257/*
3258 * The SETATTR encode routine is special -- it always encodes a bitmap,
3259 * regardless of the error status.
3260 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003261static __be32
Al Virob37ad282006-10-19 23:28:59 -07003262nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003264 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
David Quigley18032ca2013-05-02 13:19:10 -04003266 RESERVE_SPACE(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (nfserr) {
David Quigley18032ca2013-05-02 13:19:10 -04003268 WRITE32(3);
3269 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 WRITE32(0);
3271 WRITE32(0);
3272 }
3273 else {
David Quigley18032ca2013-05-02 13:19:10 -04003274 WRITE32(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 WRITE32(setattr->sa_bmval[0]);
3276 WRITE32(setattr->sa_bmval[1]);
David Quigley18032ca2013-05-02 13:19:10 -04003277 WRITE32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
3279 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003280 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Benny Halevy695e12f2008-07-04 14:38:33 +03003283static __be32
Al Virob37ad282006-10-19 23:28:59 -07003284nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003286 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003289 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003291 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 ADJUST_ARGS();
3293 }
3294 else if (nfserr == nfserr_clid_inuse) {
3295 RESERVE_SPACE(8);
3296 WRITE32(0);
3297 WRITE32(0);
3298 ADJUST_ARGS();
3299 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003300 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Benny Halevy695e12f2008-07-04 14:38:33 +03003303static __be32
Al Virob37ad282006-10-19 23:28:59 -07003304nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003306 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308 if (!nfserr) {
3309 RESERVE_SPACE(16);
3310 WRITE32(write->wr_bytes_written);
3311 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003312 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 ADJUST_ARGS();
3314 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003315 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
J. Bruce Fields57266a62013-04-13 14:27:29 -04003318static const u32 nfs4_minimal_spo_must_enforce[2] = {
3319 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3320 1 << (OP_EXCHANGE_ID - 32) |
3321 1 << (OP_CREATE_SESSION - 32) |
3322 1 << (OP_DESTROY_SESSION - 32) |
3323 1 << (OP_DESTROY_CLIENTID - 32)
3324};
3325
Benny Halevy695e12f2008-07-04 14:38:33 +03003326static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003327nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003328 struct nfsd4_exchange_id *exid)
3329{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003330 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003331 char *major_id;
3332 char *server_scope;
3333 int major_id_sz;
3334 int server_scope_sz;
3335 uint64_t minor_id = 0;
3336
3337 if (nfserr)
3338 return nfserr;
3339
3340 major_id = utsname()->nodename;
3341 major_id_sz = strlen(major_id);
3342 server_scope = utsname()->nodename;
3343 server_scope_sz = strlen(server_scope);
3344
3345 RESERVE_SPACE(
3346 8 /* eir_clientid */ +
3347 4 /* eir_sequenceid */ +
3348 4 /* eir_flags */ +
Weston Andros Adamson58cd57b2013-08-05 15:47:07 -04003349 4 /* spr_how */ +
3350 8 /* spo_must_enforce, spo_must_allow */ +
Andy Adamson0733d212009-04-03 08:28:01 +03003351 8 /* so_minor_id */ +
3352 4 /* so_major_id.len */ +
3353 (XDR_QUADLEN(major_id_sz) * 4) +
3354 4 /* eir_server_scope.len */ +
3355 (XDR_QUADLEN(server_scope_sz) * 4) +
3356 4 /* eir_server_impl_id.count (0) */);
3357
3358 WRITEMEM(&exid->clientid, 8);
3359 WRITE32(exid->seqid);
3360 WRITE32(exid->flags);
3361
Andy Adamson0733d212009-04-03 08:28:01 +03003362 WRITE32(exid->spa_how);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003363 switch (exid->spa_how) {
3364 case SP4_NONE:
3365 break;
3366 case SP4_MACH_CRED:
3367 /* spo_must_enforce bitmap: */
3368 WRITE32(2);
3369 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3370 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3371 /* empty spo_must_allow bitmap: */
3372 WRITE32(0);
3373 break;
3374 default:
3375 WARN_ON_ONCE(1);
3376 }
Andy Adamson0733d212009-04-03 08:28:01 +03003377
3378 /* The server_owner struct */
3379 WRITE64(minor_id); /* Minor id */
3380 /* major id */
3381 WRITE32(major_id_sz);
3382 WRITEMEM(major_id, major_id_sz);
3383
3384 /* Server scope */
3385 WRITE32(server_scope_sz);
3386 WRITEMEM(server_scope, server_scope_sz);
3387
3388 /* Implementation id */
3389 WRITE32(0); /* zero length nfs_impl_id4 array */
3390 ADJUST_ARGS();
3391 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003392}
3393
3394static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003395nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003396 struct nfsd4_create_session *sess)
3397{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003398 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003399
3400 if (nfserr)
3401 return nfserr;
3402
3403 RESERVE_SPACE(24);
3404 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3405 WRITE32(sess->seqid);
3406 WRITE32(sess->flags);
3407 ADJUST_ARGS();
3408
3409 RESERVE_SPACE(28);
3410 WRITE32(0); /* headerpadsz */
3411 WRITE32(sess->fore_channel.maxreq_sz);
3412 WRITE32(sess->fore_channel.maxresp_sz);
3413 WRITE32(sess->fore_channel.maxresp_cached);
3414 WRITE32(sess->fore_channel.maxops);
3415 WRITE32(sess->fore_channel.maxreqs);
3416 WRITE32(sess->fore_channel.nr_rdma_attrs);
3417 ADJUST_ARGS();
3418
3419 if (sess->fore_channel.nr_rdma_attrs) {
3420 RESERVE_SPACE(4);
3421 WRITE32(sess->fore_channel.rdma_attrs);
3422 ADJUST_ARGS();
3423 }
3424
3425 RESERVE_SPACE(28);
3426 WRITE32(0); /* headerpadsz */
3427 WRITE32(sess->back_channel.maxreq_sz);
3428 WRITE32(sess->back_channel.maxresp_sz);
3429 WRITE32(sess->back_channel.maxresp_cached);
3430 WRITE32(sess->back_channel.maxops);
3431 WRITE32(sess->back_channel.maxreqs);
3432 WRITE32(sess->back_channel.nr_rdma_attrs);
3433 ADJUST_ARGS();
3434
3435 if (sess->back_channel.nr_rdma_attrs) {
3436 RESERVE_SPACE(4);
3437 WRITE32(sess->back_channel.rdma_attrs);
3438 ADJUST_ARGS();
3439 }
3440 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003441}
3442
3443static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003444nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003445 struct nfsd4_destroy_session *destroy_session)
3446{
Andy Adamson2db134e2009-04-03 08:27:55 +03003447 return nfserr;
3448}
3449
Daniel Mackc47d8322011-05-16 16:38:14 +02003450static __be32
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003451nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003452 struct nfsd4_free_stateid *free_stateid)
3453{
3454 __be32 *p;
3455
3456 if (nfserr)
3457 return nfserr;
3458
3459 RESERVE_SPACE(4);
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003460 *p++ = nfserr;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003461 ADJUST_ARGS();
3462 return nfserr;
3463}
3464
3465static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003466nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003467 struct nfsd4_sequence *seq)
3468{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003469 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003470
3471 if (nfserr)
3472 return nfserr;
3473
3474 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3475 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3476 WRITE32(seq->seqid);
3477 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003478 /* Note slotid's are numbered from zero: */
3479 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3480 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003481 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003482
3483 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003484 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003485 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003486}
3487
J. Bruce Fields2355c592012-04-25 16:56:22 -04003488static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003489nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04003490 struct nfsd4_test_stateid *test_stateid)
3491{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003492 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003493 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003494
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003495 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003496 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003497
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003498 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003499 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003500 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003501
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003502 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003503 return nfserr;
3504}
3505
Andy Adamson2db134e2009-04-03 08:27:55 +03003506static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003507nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3508{
3509 return nfserr;
3510}
3511
3512typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3513
Andy Adamson2db134e2009-04-03 08:27:55 +03003514/*
3515 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3516 * since we don't need to filter out obsolete ops as this is
3517 * done in the decoding phase.
3518 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003519static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003520 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3521 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3522 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3523 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3524 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3525 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3526 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3527 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3528 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3529 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3530 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3531 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3532 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3533 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3534 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3535 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003536 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003537 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3538 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3539 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3540 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3541 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3542 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3543 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3544 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3545 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3546 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3547 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3548 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3549 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3550 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3551 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3552 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3553 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3554 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3555 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3556 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003557
3558 /* NFSv4.1 operations */
3559 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003560 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003561 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3562 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3563 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003564 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003565 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3566 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3567 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3568 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3569 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3570 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003571 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003572 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3573 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003574 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003575 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3576 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3577 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003578};
3579
Andy Adamson496c2622009-04-03 08:28:48 +03003580/*
3581 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003582 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003583 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003584 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3585 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003586 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003587 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003588 *
3589 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3590 * will be at least a page and will therefore hold the xdr_buf head.
3591 */
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003592__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003593{
Andy Adamson496c2622009-04-03 08:28:48 +03003594 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003595 struct nfsd4_session *session = NULL;
3596 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003597 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003598
3599 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003600 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003601
3602 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003603 if (session == NULL)
3604 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003605
3606 if (xb->page_len == 0) {
3607 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3608 } else {
3609 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3610 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3611
3612 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3613 }
3614 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3615 length, xb->page_len, tlen, pad);
3616
Mi Jinlong58e7b332011-08-28 18:18:56 +08003617 if (length > session->se_fchannel.maxresp_sz)
3618 return nfserr_rep_too_big;
3619
J. Bruce Fields73e79482012-02-13 16:39:00 -05003620 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003621 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003622 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003623
3624 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003625}
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627void
3628nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3629{
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003630 struct nfs4_stateowner *so = resp->cstate.replay_owner;
Al Viro2ebbc012006-10-19 23:28:58 -07003631 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003632 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 RESERVE_SPACE(8);
3635 WRITE32(op->opnum);
3636 statp = p++; /* to be backfilled at the end */
3637 ADJUST_ARGS();
3638
Benny Halevy695e12f2008-07-04 14:38:33 +03003639 if (op->opnum == OP_ILLEGAL)
3640 goto status;
3641 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3642 !nfsd4_enc_ops[op->opnum]);
3643 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003644 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003645 if (!op->status)
3646 op->status = nfsd4_check_resp_size(resp, 0);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003647 if (so) {
3648 so->so_replay.rp_status = op->status;
3649 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3650 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3651 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003652status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /*
3654 * Note: We write the status directly, instead of using WRITE32(),
3655 * since it is already in network byte order.
3656 */
3657 *statp = op->status;
3658}
3659
3660/*
3661 * Encode the reply stored in the stateowner reply cache
3662 *
3663 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3664 * previously sent already encoded operation.
3665 *
3666 * called with nfs4_lock_state() held
3667 */
3668void
3669nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3670{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003671 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 struct nfs4_replay *rp = op->replay;
3673
3674 BUG_ON(!rp);
3675
3676 RESERVE_SPACE(8);
3677 WRITE32(op->opnum);
3678 *p++ = rp->rp_status; /* already xdr'ed */
3679 ADJUST_ARGS();
3680
3681 RESERVE_SPACE(rp->rp_buflen);
3682 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3683 ADJUST_ARGS();
3684}
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686int
Al Viro2ebbc012006-10-19 23:28:58 -07003687nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
3689 return xdr_ressize_check(rqstp, p);
3690}
3691
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003692int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003694 struct svc_rqst *rqstp = rq;
3695 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 if (args->ops != args->iops) {
3698 kfree(args->ops);
3699 args->ops = args->iops;
3700 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003701 kfree(args->tmpp);
3702 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 while (args->to_free) {
3704 struct tmpbuf *tb = args->to_free;
3705 args->to_free = tb->next;
3706 tb->release(tb->buf);
3707 kfree(tb);
3708 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003709 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
3712int
Al Viro2ebbc012006-10-19 23:28:58 -07003713nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 args->p = p;
3716 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3717 args->pagelist = rqstp->rq_arg.pages;
3718 args->pagelen = rqstp->rq_arg.page_len;
3719 args->tmpp = NULL;
3720 args->to_free = NULL;
3721 args->ops = args->iops;
3722 args->rqstp = rqstp;
3723
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003724 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
3727int
Al Viro2ebbc012006-10-19 23:28:58 -07003728nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
3730 /*
3731 * All that remains is to write the tag and operation count...
3732 */
Andy Adamson557ce262009-08-28 08:45:04 -04003733 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 struct kvec *iov;
3735 p = resp->tagp;
3736 *p++ = htonl(resp->taglen);
3737 memcpy(p, resp->tag, resp->taglen);
3738 p += XDR_QUADLEN(resp->taglen);
3739 *p++ = htonl(resp->opcnt);
3740
3741 if (rqstp->rq_res.page_len)
3742 iov = &rqstp->rq_res.tail[0];
3743 else
3744 iov = &rqstp->rq_res.head[0];
3745 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3746 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003747 if (nfsd4_has_session(cs)) {
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003748 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3749 struct nfs4_client *clp = cs->session->se_client;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003750 if (cs->status != nfserr_replay_cache) {
3751 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003752 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003753 }
Benny Halevyd7682982010-05-12 00:13:54 +03003754 /* Renew the clientid on success and on replay */
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003755 spin_lock(&nn->client_lock);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003756 nfsd4_put_session(cs->session);
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003757 spin_unlock(&nn->client_lock);
3758 put_client_renew(clp);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return 1;
3761}
3762
3763/*
3764 * Local variables:
3765 * c-basic-offset: 8
3766 * End:
3767 */