blob: 974533e5a427d04a039a6180cb769ef4247843fd [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020038#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030039#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070041#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020042
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050043#include "idmap.h"
44#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050046#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040047#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050048#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030049#include "netns.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050050
David Quigley18032ca2013-05-02 13:19:10 -040051#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
52#include <linux/security.h>
53#endif
54
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define NFSDDBG_FACILITY NFSDDBG_XDR
57
J.Bruce Fields42ca0992006-10-04 02:16:20 -070058/*
59 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
60 * directory in order to indicate to the client that a filesystem boundary is present
61 * We use a fixed fsid for a referral
62 */
63#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
64#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
65
Al Virob37ad282006-10-19 23:28:59 -070066static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050067check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int i;
70
71 if (len == 0)
72 return nfserr_inval;
73 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050074 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 for (i = 0; i < len; i++)
76 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050077 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070082 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070083 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define DECODE_TAIL \
85 status = 0; \
86out: \
87 return status; \
88xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -040089 dprintk("NFSD: xdr error (%s:%d)\n", \
90 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 status = nfserr_bad_xdr; \
92 goto out
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define READMEM(x,nbytes) do { \
95 x = (char *)p; \
96 p += XDR_QUADLEN(nbytes); \
97} while (0)
98#define SAVEMEM(x,nbytes) do { \
99 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
100 savemem(argp, p, nbytes) : \
101 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400102 dprintk("NFSD: xdr error (%s:%d)\n", \
103 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 goto xdr_error; \
105 } \
106 p += XDR_QUADLEN(nbytes); \
107} while (0)
108#define COPYMEM(x,nbytes) do { \
109 memcpy((x), p, nbytes); \
110 p += XDR_QUADLEN(nbytes); \
111} while (0)
112
113/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
114#define READ_BUF(nbytes) do { \
115 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
116 p = argp->p; \
117 argp->p += XDR_QUADLEN(nbytes); \
118 } else if (!(p = read_buf(argp, nbytes))) { \
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} while (0)
124
J. Bruce Fields590b7432013-06-21 17:06:29 -0400125static void next_decode_page(struct nfsd4_compoundargs *argp)
126{
J. Bruce Fields590b7432013-06-21 17:06:29 -0400127 argp->p = page_address(argp->pagelist[0]);
J. Bruce Fields365da4a2013-11-19 17:32:43 -0500128 argp->pagelist++;
J. Bruce Fields590b7432013-06-21 17:06:29 -0400129 if (argp->pagelen < PAGE_SIZE) {
130 argp->end = argp->p + (argp->pagelen>>2);
131 argp->pagelen = 0;
132 } else {
133 argp->end = argp->p + (PAGE_SIZE>>2);
134 argp->pagelen -= PAGE_SIZE;
135 }
136}
137
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500138static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 /* We want more bytes than seem to be available.
141 * Maybe we need a new page, maybe we have just run out
142 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500143 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700144 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (avail + argp->pagelen < nbytes)
146 return NULL;
147 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
148 return NULL;
149 /* ok, we can do it with the current plus the next page */
150 if (nbytes <= sizeof(argp->tmp))
151 p = argp->tmp;
152 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800153 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
155 if (!p)
156 return NULL;
157
158 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500159 /*
160 * The following memcpy is safe because read_buf is always
161 * called with nbytes > avail, and the two cases above both
162 * guarantee p points to at least nbytes bytes.
163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 memcpy(p, argp->p, avail);
J. Bruce Fields590b7432013-06-21 17:06:29 -0400165 next_decode_page(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
167 argp->p += XDR_QUADLEN(nbytes - avail);
168 return p;
169}
170
Andy Adamson60adfc52009-04-03 08:28:50 +0300171static int zero_clientid(clientid_t *clid)
172{
173 return (clid->cl_boot == 0) && (clid->cl_id == 0);
174}
175
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800176/**
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400177 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
J. Bruce Fieldsce043ac2014-06-24 16:51:12 -0400178 * @argp: NFSv4 compound argument structure
179 * @p: pointer to be freed (with kfree())
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800180 *
181 * Marks @p to be freed when processing the compound operation
182 * described in @argp finishes.
183 */
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400184static void *
185svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400187 struct svcxdr_tmpbuf *tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400189 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!tb)
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400191 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 tb->next = argp->to_free;
193 argp->to_free = tb;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400194 return tb->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400197/*
198 * For xdr strings that need to be passed to other kernel api's
199 * as null-terminated strings.
200 *
201 * Note null-terminating in place usually isn't safe since the
202 * buffer might end on a page boundary.
203 */
204static char *
205svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
206{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400207 char *p = svcxdr_tmpalloc(argp, len + 1);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400208
209 if (!p)
210 return NULL;
211 memcpy(p, buf, len);
212 p[len] = '\0';
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400213 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800216/**
217 * savemem - duplicate a chunk of memory for later processing
218 * @argp: NFSv4 compound argument structure to be freed with
219 * @p: pointer to be duplicated
220 * @nbytes: length to be duplicated
221 *
222 * Returns a pointer to a copy of @nbytes bytes of memory at @p
223 * that are preserved until processing of the NFSv4 compound
224 * operation described by @argp finishes.
225 */
Al Viro2ebbc012006-10-19 23:28:58 -0700226static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400228 void *ret;
229
230 ret = svcxdr_tmpalloc(argp, nbytes);
231 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return NULL;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400233 memcpy(ret, p, nbytes);
234 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100237/*
238 * We require the high 32 bits of 'seconds' to be 0, and
239 * we ignore all 32 bits of 'nseconds'.
240 */
241static __be32
242nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec *tv)
243{
244 DECODE_HEAD;
245 u64 sec;
246
247 READ_BUF(12);
248 p = xdr_decode_hyper(p, &sec);
249 tv->tv_sec = sec;
250 tv->tv_nsec = be32_to_cpup(p++);
251 if (tv->tv_nsec >= (u32)1000000000)
252 return nfserr_inval;
253
254 DECODE_TAIL;
255}
256
Al Virob37ad282006-10-19 23:28:59 -0700257static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
259{
260 u32 bmlen;
261 DECODE_HEAD;
262
263 bmval[0] = 0;
264 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300265 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400268 bmlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (bmlen > 1000)
270 goto xdr_error;
271
272 READ_BUF(bmlen << 2);
273 if (bmlen > 0)
J. Bruce Fields06553992014-06-02 12:32:51 -0400274 bmval[0] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (bmlen > 1)
J. Bruce Fields06553992014-06-02 12:32:51 -0400276 bmval[1] = be32_to_cpup(p++);
Andy Adamson7e705702009-04-03 08:29:11 +0300277 if (bmlen > 2)
J. Bruce Fields06553992014-06-02 12:32:51 -0400278 bmval[2] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 DECODE_TAIL;
281}
282
Al Virob37ad282006-10-19 23:28:59 -0700283static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800284nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400285 struct iattr *iattr, struct nfs4_acl **acl,
286 struct xdr_netobj *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int expected_len, len = 0;
289 u32 dummy32;
290 char *buf;
291
292 DECODE_HEAD;
293 iattr->ia_valid = 0;
294 if ((status = nfsd4_decode_bitmap(argp, bmval)))
295 return status;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400298 expected_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (bmval[0] & FATTR4_WORD0_SIZE) {
301 READ_BUF(8);
302 len += 8;
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400303 p = xdr_decode_hyper(p, &iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 iattr->ia_valid |= ATTR_SIZE;
305 }
306 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields64a817c2013-03-26 14:11:13 -0400307 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800308 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 READ_BUF(4); len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400311 nace = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800313 if (nace > NFS4_ACL_MAX)
J. Bruce Fields798df332014-01-29 16:39:05 -0500314 return nfserr_fbig;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800315
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400316 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
Kinglong Meeeba1c992013-12-09 18:23:46 +0800317 if (*acl == NULL)
318 return nfserr_jukebox;
319
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800320 (*acl)->naces = nace;
321 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 READ_BUF(16); len += 16;
J. Bruce Fields06553992014-06-02 12:32:51 -0400323 ace->type = be32_to_cpup(p++);
324 ace->flag = be32_to_cpup(p++);
325 ace->access_mask = be32_to_cpup(p++);
326 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 READ_BUF(dummy32);
328 len += XDR_QUADLEN(dummy32) << 2;
329 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800330 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500331 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800332 if (ace->whotype != NFS4_ACL_WHO_NAMED)
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800333 ;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800334 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500335 status = nfsd_map_name_to_gid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800336 buf, dummy32, &ace->who_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500338 status = nfsd_map_name_to_uid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800339 buf, dummy32, &ace->who_uid);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500340 if (status)
341 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 } else
344 *acl = NULL;
345 if (bmval[1] & FATTR4_WORD1_MODE) {
346 READ_BUF(4);
347 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400348 iattr->ia_mode = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
350 iattr->ia_valid |= ATTR_MODE;
351 }
352 if (bmval[1] & FATTR4_WORD1_OWNER) {
353 READ_BUF(4);
354 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400355 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 READ_BUF(dummy32);
357 len += (XDR_QUADLEN(dummy32) << 2);
358 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100359 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 iattr->ia_valid |= ATTR_UID;
362 }
363 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
364 READ_BUF(4);
365 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400366 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 READ_BUF(dummy32);
368 len += (XDR_QUADLEN(dummy32) << 2);
369 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100370 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
371 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 iattr->ia_valid |= ATTR_GID;
373 }
374 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
375 READ_BUF(4);
376 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400377 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 switch (dummy32) {
379 case NFS4_SET_TO_CLIENT_TIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 len += 12;
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100381 status = nfsd4_decode_time(argp, &iattr->ia_atime);
382 if (status)
383 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
385 break;
386 case NFS4_SET_TO_SERVER_TIME:
387 iattr->ia_valid |= ATTR_ATIME;
388 break;
389 default:
390 goto xdr_error;
391 }
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
394 READ_BUF(4);
395 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400396 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 switch (dummy32) {
398 case NFS4_SET_TO_CLIENT_TIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 len += 12;
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100400 status = nfsd4_decode_time(argp, &iattr->ia_mtime);
401 if (status)
402 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
404 break;
405 case NFS4_SET_TO_SERVER_TIME:
406 iattr->ia_valid |= ATTR_MTIME;
407 break;
408 default:
409 goto xdr_error;
410 }
411 }
David Quigley18032ca2013-05-02 13:19:10 -0400412
413 label->len = 0;
414#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
415 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
416 READ_BUF(4);
417 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400418 dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
David Quigley18032ca2013-05-02 13:19:10 -0400419 READ_BUF(4);
420 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400421 dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
David Quigley18032ca2013-05-02 13:19:10 -0400422 READ_BUF(4);
423 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400424 dummy32 = be32_to_cpup(p++);
David Quigley18032ca2013-05-02 13:19:10 -0400425 READ_BUF(dummy32);
426 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
427 return nfserr_badlabel;
428 len += (XDR_QUADLEN(dummy32) << 2);
429 READMEM(buf, dummy32);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400430 label->len = dummy32;
431 label->data = svcxdr_dupstr(argp, buf, dummy32);
David Quigley18032ca2013-05-02 13:19:10 -0400432 if (!label->data)
433 return nfserr_jukebox;
David Quigley18032ca2013-05-02 13:19:10 -0400434 }
435#endif
436
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800437 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
438 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
439 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
440 READ_BUF(expected_len - len);
441 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 goto xdr_error;
443
444 DECODE_TAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Al Virob37ad282006-10-19 23:28:59 -0700447static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300448nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
449{
450 DECODE_HEAD;
451
452 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400453 sid->si_generation = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300454 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
455
456 DECODE_TAIL;
457}
458
459static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
461{
462 DECODE_HEAD;
463
464 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400465 access->ac_req_access = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 DECODE_TAIL;
468}
469
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400470static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
471{
472 DECODE_HEAD;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500473 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400474 char *machine_name;
475 int i;
476 int nr_secflavs;
477
478 /* callback_sec_params4 */
479 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400480 nr_secflavs = be32_to_cpup(p++);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400481 if (nr_secflavs)
482 cbs->flavor = (u32)(-1);
483 else
484 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
485 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400486 for (i = 0; i < nr_secflavs; ++i) {
487 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400488 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400489 switch (dummy) {
490 case RPC_AUTH_NULL:
491 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500492 if (cbs->flavor == (u32)(-1))
493 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400494 break;
495 case RPC_AUTH_UNIX:
496 READ_BUF(8);
497 /* stamp */
J. Bruce Fields06553992014-06-02 12:32:51 -0400498 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400499
500 /* machine name */
J. Bruce Fields06553992014-06-02 12:32:51 -0400501 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400502 READ_BUF(dummy);
503 SAVEMEM(machine_name, dummy);
504
505 /* uid, gid */
506 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400507 uid = be32_to_cpup(p++);
508 gid = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400509
510 /* more gids */
511 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400512 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400513 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500514 if (cbs->flavor == (u32)(-1)) {
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800515 kuid_t kuid = make_kuid(&init_user_ns, uid);
516 kgid_t kgid = make_kgid(&init_user_ns, gid);
517 if (uid_valid(kuid) && gid_valid(kgid)) {
518 cbs->uid = kuid;
519 cbs->gid = kgid;
520 cbs->flavor = RPC_AUTH_UNIX;
521 } else {
522 dprintk("RPC_AUTH_UNIX with invalid"
523 "uid or gid ignoring!\n");
524 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500525 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400526 break;
527 case RPC_AUTH_GSS:
528 dprintk("RPC_AUTH_GSS callback secflavor "
529 "not supported!\n");
530 READ_BUF(8);
531 /* gcbp_service */
J. Bruce Fields06553992014-06-02 12:32:51 -0400532 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400533 /* gcbp_handle_from_server */
J. Bruce Fields06553992014-06-02 12:32:51 -0400534 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400535 READ_BUF(dummy);
536 p += XDR_QUADLEN(dummy);
537 /* gcbp_handle_from_client */
538 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400539 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400540 READ_BUF(dummy);
541 break;
542 default:
543 dprintk("Illegal callback secflavor\n");
544 return nfserr_inval;
545 }
546 }
547 DECODE_TAIL;
548}
549
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400550static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
551{
552 DECODE_HEAD;
553
554 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400555 bc->bc_cb_program = be32_to_cpup(p++);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400556 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
557
558 DECODE_TAIL;
559}
560
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400561static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
562{
563 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400564
565 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
566 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -0400567 bcts->dir = be32_to_cpup(p++);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400568 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
569 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400570 DECODE_TAIL;
571}
572
Al Virob37ad282006-10-19 23:28:59 -0700573static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
575{
576 DECODE_HEAD;
577
Benny Halevye31a1b62008-08-12 20:46:18 +0300578 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400579 close->cl_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300580 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 DECODE_TAIL;
583}
584
585
Al Virob37ad282006-10-19 23:28:59 -0700586static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
588{
589 DECODE_HEAD;
590
591 READ_BUF(12);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400592 p = xdr_decode_hyper(p, &commit->co_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -0400593 commit->co_count = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 DECODE_TAIL;
596}
597
Al Virob37ad282006-10-19 23:28:59 -0700598static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
600{
601 DECODE_HEAD;
602
603 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400604 create->cr_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 switch (create->cr_type) {
606 case NF4LNK:
607 READ_BUF(4);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400608 create->cr_datalen = be32_to_cpup(p++);
609 READ_BUF(create->cr_datalen);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400610 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400611 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400612 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case NF4BLK:
615 case NF4CHR:
616 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400617 create->cr_specdata1 = be32_to_cpup(p++);
618 create->cr_specdata2 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 break;
620 case NF4SOCK:
621 case NF4FIFO:
622 case NF4DIR:
623 default:
624 break;
625 }
626
627 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400628 create->cr_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 READ_BUF(create->cr_namelen);
630 SAVEMEM(create->cr_name, create->cr_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500631 if ((status = check_filename(create->cr_name, create->cr_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return status;
633
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800634 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
David Quigley18032ca2013-05-02 13:19:10 -0400635 &create->cr_acl, &create->cr_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300636 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 goto out;
638
639 DECODE_TAIL;
640}
641
Al Virob37ad282006-10-19 23:28:59 -0700642static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
644{
Benny Halevye31a1b62008-08-12 20:46:18 +0300645 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Al Virob37ad282006-10-19 23:28:59 -0700648static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
650{
651 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
652}
653
Al Virob37ad282006-10-19 23:28:59 -0700654static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
656{
657 DECODE_HEAD;
658
659 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400660 link->li_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 READ_BUF(link->li_namelen);
662 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500663 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return status;
665
666 DECODE_TAIL;
667}
668
Al Virob37ad282006-10-19 23:28:59 -0700669static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
671{
672 DECODE_HEAD;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
676 */
677 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -0400678 lock->lk_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
680 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400681 lock->lk_reclaim = be32_to_cpup(p++);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400682 p = xdr_decode_hyper(p, &lock->lk_offset);
683 p = xdr_decode_hyper(p, &lock->lk_length);
J. Bruce Fields06553992014-06-02 12:32:51 -0400684 lock->lk_is_new = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300687 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400688 lock->lk_new_open_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300689 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
690 if (status)
691 return status;
692 READ_BUF(8 + sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400693 lock->lk_new_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400695 lock->lk_new_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 READ_BUF(lock->lk_new_owner.len);
697 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
698 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300699 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
700 if (status)
701 return status;
702 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400703 lock->lk_old_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 DECODE_TAIL;
707}
708
Al Virob37ad282006-10-19 23:28:59 -0700709static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
711{
712 DECODE_HEAD;
713
714 READ_BUF(32);
J. Bruce Fields06553992014-06-02 12:32:51 -0400715 lockt->lt_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
717 goto xdr_error;
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400718 p = xdr_decode_hyper(p, &lockt->lt_offset);
719 p = xdr_decode_hyper(p, &lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 COPYMEM(&lockt->lt_clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400721 lockt->lt_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 READ_BUF(lockt->lt_owner.len);
723 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
724
725 DECODE_TAIL;
726}
727
Al Virob37ad282006-10-19 23:28:59 -0700728static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
730{
731 DECODE_HEAD;
732
Benny Halevye31a1b62008-08-12 20:46:18 +0300733 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400734 locku->lu_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
736 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400737 locku->lu_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300738 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
739 if (status)
740 return status;
741 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400742 p = xdr_decode_hyper(p, &locku->lu_offset);
743 p = xdr_decode_hyper(p, &locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 DECODE_TAIL;
746}
747
Al Virob37ad282006-10-19 23:28:59 -0700748static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
750{
751 DECODE_HEAD;
752
753 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400754 lookup->lo_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 READ_BUF(lookup->lo_len);
756 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500757 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return status;
759
760 DECODE_TAIL;
761}
762
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200763static __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 -0400764{
765 __be32 *p;
766 u32 w;
767
768 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400769 w = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200770 *share_access = w & NFS4_SHARE_ACCESS_MASK;
771 *deleg_want = w & NFS4_SHARE_WANT_MASK;
772 if (deleg_when)
773 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
774
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400775 switch (w & NFS4_SHARE_ACCESS_MASK) {
776 case NFS4_SHARE_ACCESS_READ:
777 case NFS4_SHARE_ACCESS_WRITE:
778 case NFS4_SHARE_ACCESS_BOTH:
779 break;
780 default:
781 return nfserr_bad_xdr;
782 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200783 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400784 if (!w)
785 return nfs_ok;
786 if (!argp->minorversion)
787 return nfserr_bad_xdr;
788 switch (w & NFS4_SHARE_WANT_MASK) {
789 case NFS4_SHARE_WANT_NO_PREFERENCE:
790 case NFS4_SHARE_WANT_READ_DELEG:
791 case NFS4_SHARE_WANT_WRITE_DELEG:
792 case NFS4_SHARE_WANT_ANY_DELEG:
793 case NFS4_SHARE_WANT_NO_DELEG:
794 case NFS4_SHARE_WANT_CANCEL:
795 break;
796 default:
797 return nfserr_bad_xdr;
798 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700799 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400800 if (!w)
801 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200802
803 if (!deleg_when) /* open_downgrade */
804 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400805 switch (w) {
806 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
807 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700808 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
809 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400810 return nfs_ok;
811 }
812xdr_error:
813 return nfserr_bad_xdr;
814}
815
816static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
817{
818 __be32 *p;
819
820 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400821 *x = be32_to_cpup(p++);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400822 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300823 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400824 return nfserr_bad_xdr;
825 return nfs_ok;
826xdr_error:
827 return nfserr_bad_xdr;
828}
829
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400830static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
831{
832 __be32 *p;
833
834 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400835 o->len = be32_to_cpup(p++);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400836
837 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
838 return nfserr_bad_xdr;
839
840 READ_BUF(o->len);
841 SAVEMEM(o->data, o->len);
842 return nfs_ok;
843xdr_error:
844 return nfserr_bad_xdr;
845}
846
Al Virob37ad282006-10-19 23:28:59 -0700847static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
849{
850 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200851 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 memset(open->op_bmval, 0, sizeof(open->op_bmval));
854 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400855 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800857 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400859 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400860 open->op_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200861 /* decode, yet ignore deleg_when until supported */
862 status = nfsd4_decode_share_access(argp, &open->op_share_access,
863 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400864 if (status)
865 goto xdr_error;
866 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
867 if (status)
868 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400869 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400871 status = nfsd4_decode_opaque(argp, &open->op_owner);
872 if (status)
873 goto xdr_error;
874 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400875 open->op_create = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 switch (open->op_create) {
877 case NFS4_OPEN_NOCREATE:
878 break;
879 case NFS4_OPEN_CREATE:
880 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400881 open->op_createmode = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 switch (open->op_createmode) {
883 case NFS4_CREATE_UNCHECKED:
884 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300885 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400886 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300887 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto out;
889 break;
890 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500891 READ_BUF(NFS4_VERIFIER_SIZE);
892 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300894 case NFS4_CREATE_EXCLUSIVE4_1:
895 if (argp->minorversion < 1)
896 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500897 READ_BUF(NFS4_VERIFIER_SIZE);
898 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300899 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400900 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300901 if (status)
902 goto out;
903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 default:
905 goto xdr_error;
906 }
907 break;
908 default:
909 goto xdr_error;
910 }
911
912 /* open_claim */
913 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400914 open->op_claim_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 switch (open->op_claim_type) {
916 case NFS4_OPEN_CLAIM_NULL:
917 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
918 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400919 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 READ_BUF(open->op_fname.len);
921 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500922 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return status;
924 break;
925 case NFS4_OPEN_CLAIM_PREVIOUS:
926 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400927 open->op_delegate_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 break;
929 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300930 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
931 if (status)
932 return status;
933 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400934 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 READ_BUF(open->op_fname.len);
936 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500937 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return status;
939 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400940 case NFS4_OPEN_CLAIM_FH:
941 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
942 if (argp->minorversion < 1)
943 goto xdr_error;
944 /* void */
945 break;
946 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
947 if (argp->minorversion < 1)
948 goto xdr_error;
949 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
950 if (status)
951 return status;
952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 default:
954 goto xdr_error;
955 }
956
957 DECODE_TAIL;
958}
959
Al Virob37ad282006-10-19 23:28:59 -0700960static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
962{
963 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400964
965 if (argp->minorversion >= 1)
966 return nfserr_notsupp;
967
Benny Halevye31a1b62008-08-12 20:46:18 +0300968 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
969 if (status)
970 return status;
971 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400972 open_conf->oc_seqid = be32_to_cpup(p++);
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 DECODE_TAIL;
975}
976
Al Virob37ad282006-10-19 23:28:59 -0700977static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
979{
980 DECODE_HEAD;
981
Benny Halevye31a1b62008-08-12 20:46:18 +0300982 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
983 if (status)
984 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400985 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400986 open_down->od_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200987 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
988 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400989 if (status)
990 return status;
991 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
992 if (status)
993 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 DECODE_TAIL;
995}
996
Al Virob37ad282006-10-19 23:28:59 -0700997static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
999{
1000 DECODE_HEAD;
1001
1002 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001003 putfh->pf_fhlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (putfh->pf_fhlen > NFS4_FHSIZE)
1005 goto xdr_error;
1006 READ_BUF(putfh->pf_fhlen);
1007 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1008
1009 DECODE_TAIL;
1010}
1011
Al Virob37ad282006-10-19 23:28:59 -07001012static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001013nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1014{
1015 if (argp->minorversion == 0)
1016 return nfs_ok;
1017 return nfserr_notsupp;
1018}
1019
1020static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1022{
1023 DECODE_HEAD;
1024
Benny Halevye31a1b62008-08-12 20:46:18 +03001025 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1026 if (status)
1027 return status;
1028 READ_BUF(12);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001029 p = xdr_decode_hyper(p, &read->rd_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001030 read->rd_length = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 DECODE_TAIL;
1033}
1034
Al Virob37ad282006-10-19 23:28:59 -07001035static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1037{
1038 DECODE_HEAD;
1039
1040 READ_BUF(24);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001041 p = xdr_decode_hyper(p, &readdir->rd_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
J. Bruce Fields06553992014-06-02 12:32:51 -04001043 readdir->rd_dircount = be32_to_cpup(p++);
1044 readdir->rd_maxcount = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1046 goto out;
1047
1048 DECODE_TAIL;
1049}
1050
Al Virob37ad282006-10-19 23:28:59 -07001051static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1053{
1054 DECODE_HEAD;
1055
1056 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001057 remove->rm_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 READ_BUF(remove->rm_namelen);
1059 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001060 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return status;
1062
1063 DECODE_TAIL;
1064}
1065
Al Virob37ad282006-10-19 23:28:59 -07001066static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1068{
1069 DECODE_HEAD;
1070
1071 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001072 rename->rn_snamelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 READ_BUF(rename->rn_snamelen + 4);
1074 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
J. Bruce Fields06553992014-06-02 12:32:51 -04001075 rename->rn_tnamelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 READ_BUF(rename->rn_tnamelen);
1077 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001078 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001080 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return status;
1082
1083 DECODE_TAIL;
1084}
1085
Al Virob37ad282006-10-19 23:28:59 -07001086static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1088{
1089 DECODE_HEAD;
1090
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001091 if (argp->minorversion >= 1)
1092 return nfserr_notsupp;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 READ_BUF(sizeof(clientid_t));
1095 COPYMEM(clientid, sizeof(clientid_t));
1096
1097 DECODE_TAIL;
1098}
1099
Al Virob37ad282006-10-19 23:28:59 -07001100static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001101nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1102 struct nfsd4_secinfo *secinfo)
1103{
1104 DECODE_HEAD;
1105
1106 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001107 secinfo->si_namelen = be32_to_cpup(p++);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001108 READ_BUF(secinfo->si_namelen);
1109 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001110 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001111 if (status)
1112 return status;
1113 DECODE_TAIL;
1114}
1115
1116static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001117nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1118 struct nfsd4_secinfo_no_name *sin)
1119{
1120 DECODE_HEAD;
1121
1122 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001123 sin->sin_style = be32_to_cpup(p++);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001124 DECODE_TAIL;
1125}
1126
1127static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1129{
Benny Halevye31a1b62008-08-12 20:46:18 +03001130 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Benny Halevye31a1b62008-08-12 20:46:18 +03001132 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1133 if (status)
1134 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001135 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
David Quigley18032ca2013-05-02 13:19:10 -04001136 &setattr->sa_acl, &setattr->sa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Al Virob37ad282006-10-19 23:28:59 -07001139static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1141{
1142 DECODE_HEAD;
1143
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001144 if (argp->minorversion >= 1)
1145 return nfserr_notsupp;
1146
Chuck Leverab4684d2012-03-02 17:13:50 -05001147 READ_BUF(NFS4_VERIFIER_SIZE);
1148 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001150 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1151 if (status)
1152 return nfserr_bad_xdr;
1153 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001154 setclientid->se_callback_prog = be32_to_cpup(p++);
1155 setclientid->se_callback_netid_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 READ_BUF(setclientid->se_callback_netid_len + 4);
1158 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
J. Bruce Fields06553992014-06-02 12:32:51 -04001159 setclientid->se_callback_addr_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 READ_BUF(setclientid->se_callback_addr_len + 4);
1162 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
J. Bruce Fields06553992014-06-02 12:32:51 -04001163 setclientid->se_callback_ident = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 DECODE_TAIL;
1166}
1167
Al Virob37ad282006-10-19 23:28:59 -07001168static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1170{
1171 DECODE_HEAD;
1172
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001173 if (argp->minorversion >= 1)
1174 return nfserr_notsupp;
1175
Chuck Leverab4684d2012-03-02 17:13:50 -05001176 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001178 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 DECODE_TAIL;
1181}
1182
1183/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001184static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 DECODE_HEAD;
1188
1189 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1190 goto out;
1191
1192 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001193 * nfsd4_proc_verify */
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001196 verify->ve_attrlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 READ_BUF(verify->ve_attrlen);
1198 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1199
1200 DECODE_TAIL;
1201}
1202
Al Virob37ad282006-10-19 23:28:59 -07001203static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1205{
1206 int avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int len;
1208 DECODE_HEAD;
1209
Benny Halevye31a1b62008-08-12 20:46:18 +03001210 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1211 if (status)
1212 return status;
1213 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001214 p = xdr_decode_hyper(p, &write->wr_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001215 write->wr_stable_how = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (write->wr_stable_how > 2)
1217 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -04001218 write->wr_buflen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* Sorry .. no magic macros for this.. *
1221 * READ_BUF(write->wr_buflen);
1222 * SAVEMEM(write->wr_buf, write->wr_buflen);
1223 */
1224 avail = (char*)argp->end - (char*)argp->p;
1225 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001226 dprintk("NFSD: xdr error (%s:%d)\n",
1227 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto xdr_error;
1229 }
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001230 write->wr_head.iov_base = p;
1231 write->wr_head.iov_len = avail;
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001232 write->wr_pagelist = argp->pagelist;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001233
1234 len = XDR_QUADLEN(write->wr_buflen) << 2;
1235 if (len >= avail) {
1236 int pages;
1237
1238 len -= avail;
1239
1240 pages = len >> PAGE_SHIFT;
1241 argp->pagelist += pages;
1242 argp->pagelen -= pages * PAGE_SIZE;
1243 len -= pages * PAGE_SIZE;
1244
1245 argp->p = (__be32 *)page_address(argp->pagelist[0]);
J. Bruce Fields365da4a2013-11-19 17:32:43 -05001246 argp->pagelist++;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001247 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001249 argp->p += XDR_QUADLEN(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 DECODE_TAIL;
1252}
1253
Al Virob37ad282006-10-19 23:28:59 -07001254static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1256{
1257 DECODE_HEAD;
1258
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001259 if (argp->minorversion >= 1)
1260 return nfserr_notsupp;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 READ_BUF(12);
1263 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001264 rlockowner->rl_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 READ_BUF(rlockowner->rl_owner.len);
1266 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1267
Andy Adamson60adfc52009-04-03 08:28:50 +03001268 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1269 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 DECODE_TAIL;
1271}
1272
Al Virob37ad282006-10-19 23:28:59 -07001273static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001274nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001275 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001276{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001277 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001278 DECODE_HEAD;
1279
1280 READ_BUF(NFS4_VERIFIER_SIZE);
1281 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1282
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001283 status = nfsd4_decode_opaque(argp, &exid->clname);
1284 if (status)
1285 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001286
1287 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001288 exid->flags = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001289
1290 /* Ignore state_protect4_a */
1291 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001292 exid->spa_how = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001293 switch (exid->spa_how) {
1294 case SP4_NONE:
1295 break;
1296 case SP4_MACH_CRED:
1297 /* spo_must_enforce */
1298 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001299 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001300 READ_BUF(dummy * 4);
1301 p += dummy;
1302
1303 /* spo_must_allow */
1304 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001305 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001306 READ_BUF(dummy * 4);
1307 p += dummy;
1308 break;
1309 case SP4_SSV:
1310 /* ssp_ops */
1311 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001312 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001313 READ_BUF(dummy * 4);
1314 p += dummy;
1315
1316 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001317 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001318 READ_BUF(dummy * 4);
1319 p += dummy;
1320
1321 /* ssp_hash_algs<> */
1322 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001323 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001324 while (tmp--) {
1325 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001326 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001327 READ_BUF(dummy);
1328 p += XDR_QUADLEN(dummy);
1329 }
Andy Adamson0733d212009-04-03 08:28:01 +03001330
1331 /* ssp_encr_algs<> */
1332 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001333 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001334 while (tmp--) {
1335 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001336 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001337 READ_BUF(dummy);
1338 p += XDR_QUADLEN(dummy);
1339 }
Andy Adamson0733d212009-04-03 08:28:01 +03001340
1341 /* ssp_window and ssp_num_gss_handles */
1342 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001343 dummy = be32_to_cpup(p++);
1344 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001345 break;
1346 default:
1347 goto xdr_error;
1348 }
1349
1350 /* Ignore Implementation ID */
1351 READ_BUF(4); /* nfs_impl_id4 array length */
J. Bruce Fields06553992014-06-02 12:32:51 -04001352 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001353
1354 if (dummy > 1)
1355 goto xdr_error;
1356
1357 if (dummy == 1) {
1358 /* nii_domain */
1359 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001360 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001361 READ_BUF(dummy);
1362 p += XDR_QUADLEN(dummy);
1363
1364 /* nii_name */
1365 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001366 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001367 READ_BUF(dummy);
1368 p += XDR_QUADLEN(dummy);
1369
1370 /* nii_date */
1371 READ_BUF(12);
1372 p += 3;
1373 }
1374 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001375}
1376
1377static __be32
1378nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1379 struct nfsd4_create_session *sess)
1380{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001381 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001382 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001383
1384 READ_BUF(16);
1385 COPYMEM(&sess->clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001386 sess->seqid = be32_to_cpup(p++);
1387 sess->flags = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001388
1389 /* Fore channel attrs */
1390 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -04001391 dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
1392 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1393 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1394 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1395 sess->fore_channel.maxops = be32_to_cpup(p++);
1396 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1397 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001398 if (sess->fore_channel.nr_rdma_attrs == 1) {
1399 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001400 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001401 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1402 dprintk("Too many fore channel attr bitmaps!\n");
1403 goto xdr_error;
1404 }
1405
1406 /* Back channel attrs */
1407 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -04001408 dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
1409 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1410 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1411 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1412 sess->back_channel.maxops = be32_to_cpup(p++);
1413 sess->back_channel.maxreqs = be32_to_cpup(p++);
1414 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001415 if (sess->back_channel.nr_rdma_attrs == 1) {
1416 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001417 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001418 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1419 dprintk("Too many back channel attr bitmaps!\n");
1420 goto xdr_error;
1421 }
1422
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001423 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001424 sess->callback_prog = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001425 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001426 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001427}
1428
1429static __be32
1430nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1431 struct nfsd4_destroy_session *destroy_session)
1432{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001433 DECODE_HEAD;
1434 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1435 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1436
1437 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001438}
1439
1440static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001441nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1442 struct nfsd4_free_stateid *free_stateid)
1443{
1444 DECODE_HEAD;
1445
1446 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001447 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001448 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1449
1450 DECODE_TAIL;
1451}
1452
1453static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001454nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1455 struct nfsd4_sequence *seq)
1456{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001457 DECODE_HEAD;
1458
1459 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1460 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -04001461 seq->seqid = be32_to_cpup(p++);
1462 seq->slotid = be32_to_cpup(p++);
1463 seq->maxslots = be32_to_cpup(p++);
1464 seq->cachethis = be32_to_cpup(p++);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001465
1466 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001467}
1468
Bryan Schumaker17456802011-07-13 10:50:48 -04001469static __be32
1470nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1471{
Bryan Schumaker17456802011-07-13 10:50:48 -04001472 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001473 __be32 *p, status;
1474 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001475
1476 READ_BUF(4);
1477 test_stateid->ts_num_ids = ntohl(*p++);
1478
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001479 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001480
1481 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001482 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001483 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001484 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001485 goto out;
1486 }
1487
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001488 INIT_LIST_HEAD(&stateid->ts_id_list);
1489 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1490
1491 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001492 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001493 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001494 }
1495
1496 status = 0;
1497out:
1498 return status;
1499xdr_error:
1500 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1501 status = nfserr_bad_xdr;
1502 goto out;
1503}
1504
Mi Jinlong345c2842011-10-20 17:51:39 +08001505static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1506{
1507 DECODE_HEAD;
1508
1509 READ_BUF(8);
1510 COPYMEM(&dc->clientid, 8);
1511
1512 DECODE_TAIL;
1513}
1514
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001515static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1516{
1517 DECODE_HEAD;
1518
1519 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001520 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001521
1522 DECODE_TAIL;
1523}
1524
Andy Adamson2db134e2009-04-03 08:27:55 +03001525static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001526nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1527 struct nfsd4_fallocate *fallocate)
1528{
1529 DECODE_HEAD;
1530
1531 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1532 if (status)
1533 return status;
1534
1535 READ_BUF(16);
1536 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1537 xdr_decode_hyper(p, &fallocate->falloc_length);
1538
1539 DECODE_TAIL;
1540}
1541
1542static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001543nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1544{
1545 DECODE_HEAD;
1546
1547 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1548 if (status)
1549 return status;
1550
1551 READ_BUF(8 + 4);
1552 p = xdr_decode_hyper(p, &seek->seek_offset);
1553 seek->seek_whence = be32_to_cpup(p);
1554
1555 DECODE_TAIL;
1556}
1557
1558static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001559nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1560{
1561 return nfs_ok;
1562}
1563
Benny Halevy3c375c62008-07-02 11:14:01 +03001564static __be32
1565nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1566{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001567 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001568}
1569
Benny Halevy347e0ad2008-07-02 11:13:41 +03001570typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1571
1572static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001573 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1574 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1575 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1576 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1577 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1578 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1579 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1580 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1581 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1582 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1583 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1584 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1585 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1586 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1587 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1588 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1589 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1590 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1591 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1592 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001593 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001594 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1595 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1596 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1597 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1598 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1599 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1600 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1601 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1602 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1603 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1604 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1605 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1606 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1607 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1608 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1609 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03001610
1611 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001612 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001613 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001614 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1615 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1616 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001617 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001618 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1619 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1620 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1621 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1622 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1623 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001624 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001625 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1626 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001627 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001628 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001629 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001630 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001631
1632 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05001633 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001634 [OP_COPY] = (nfsd4_dec)nfsd4_decode_notsupp,
1635 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001636 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001637 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
1638 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
1639 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
1640 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
1641 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
1642 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
Anna Schumaker24bab492014-09-26 13:58:27 -04001643 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001644 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001645};
1646
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001647static inline bool
1648nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1649{
Anna Schumaker8217d142013-10-30 13:38:13 -04001650 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001651 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001652 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001653 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001654 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1655 return false;
1656 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001657 return false;
1658 return true;
1659}
Benny Halevyf2feb962008-07-02 11:14:22 +03001660
Benny Halevy347e0ad2008-07-02 11:13:41 +03001661static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1663{
1664 DECODE_HEAD;
1665 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05001666 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04001667 int auth_slack= argp->rqstp->rq_auth_slack;
1668 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001669 int readcount = 0;
1670 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int i;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001674 argp->taglen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 READ_BUF(argp->taglen + 8);
1676 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields06553992014-06-02 12:32:51 -04001677 argp->minorversion = be32_to_cpup(p++);
1678 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04001679 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (argp->taglen > NFSD4_MAX_TAGLEN)
1682 goto xdr_error;
1683 if (argp->opcnt > 100)
1684 goto xdr_error;
1685
Tobias Klausere8c96f82006-03-24 03:15:34 -08001686 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04001687 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (!argp->ops) {
1689 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001690 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 goto xdr_error;
1692 }
1693 }
1694
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001695 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03001696 argp->opcnt = 0;
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 for (i = 0; i < argp->opcnt; i++) {
1699 op = &argp->ops[i];
1700 op->replay = NULL;
1701
J. Bruce Fields8a61b182012-11-16 22:28:38 -05001702 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001703 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001705 if (nfsd4_opnum_in_range(argp, op))
1706 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001707 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 op->opnum = OP_ILLEGAL;
1709 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001711 /*
1712 * We'll try to cache the result in the DRC if any one
1713 * op in the compound wants to be cached:
1714 */
1715 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05001716
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001717 if (op->opnum == OP_READ) {
1718 readcount++;
1719 readbytes += nfsd4_max_reply(argp->rqstp, op);
1720 } else
1721 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04001722 /*
1723 * OP_LOCK may return a conflicting lock. (Special case
1724 * because it will just skip encoding this if it runs
1725 * out of xdr buffer space, and it is the only operation
1726 * that behaves this way.)
1727 */
1728 if (op->opnum == OP_LOCK)
1729 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04001730
1731 if (op->status) {
1732 argp->opcnt = i+1;
1733 break;
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001736 /* Sessions make the DRC unnecessary: */
1737 if (argp->minorversion)
1738 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001739 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05001740 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04001742 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05001743 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 DECODE_TAIL;
1746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001748static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001749{
1750 if (IS_I_VERSION(inode)) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001751 p = xdr_encode_hyper(p, inode->i_version);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001752 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001753 *p++ = cpu_to_be32(stat->ctime.tv_sec);
1754 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001755 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001756 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001757}
1758
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001759static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001760{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001761 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001762 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001763 p = xdr_encode_hyper(p, c->before_change);
1764 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001765 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001766 *p++ = cpu_to_be32(c->before_ctime_sec);
1767 *p++ = cpu_to_be32(c->before_ctime_nsec);
1768 *p++ = cpu_to_be32(c->after_ctime_sec);
1769 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001770 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04001771 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001774/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001775 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001776 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001777static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
1778 char *components, char esc_enter,
1779 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001780{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001781 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04001782 __be32 pathlen;
1783 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001784 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001785 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001786
1787 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04001788
1789 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001790 p = xdr_reserve_space(xdr, 4);
1791 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001792 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04001793 p++; /* We will fill this in with @count later */
1794
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001795 end = str = components;
1796 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001797 bool found_esc = false;
1798
1799 /* try to parse as esc_start, ..., esc_end, sep */
1800 if (*str == esc_enter) {
1801 for (; *end && (*end != esc_exit); end++)
1802 /* find esc_exit or end of string */;
1803 next = end + 1;
1804 if (*end && (!*next || *next == sep)) {
1805 str++;
1806 found_esc = true;
1807 }
1808 }
1809
1810 if (!found_esc)
1811 for (; *end && (*end != sep); end++)
1812 /* find sep or end of string */;
1813
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001814 strlen = end - str;
1815 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001816 p = xdr_reserve_space(xdr, strlen + 4);
1817 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001818 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04001819 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001820 count++;
1821 }
1822 else
1823 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05001824 if (found_esc)
1825 end = next;
1826
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001827 str = end;
1828 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05001829 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04001830 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001831 return 0;
1832}
1833
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001834/* Encode as an array of strings the string given with components
1835 * separated @sep.
1836 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001837static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
1838 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001839{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001840 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001841}
1842
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001843/*
1844 * encode a location element of a fs_locations structure
1845 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001846static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
1847 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001848{
Al Virob37ad282006-10-19 23:28:59 -07001849 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001850
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001851 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001852 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001853 if (status)
1854 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001855 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001856 if (status)
1857 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001858 return 0;
1859}
1860
1861/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001862 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001863 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001864static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
1865 const struct path *root,
1866 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001867{
Al Viro301f0262013-09-01 16:06:39 -04001868 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001869 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001870 struct dentry **components = NULL;
1871 unsigned int ncomponents = 0;
1872 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001873
Trond Myklebusted748aa2011-09-12 19:37:06 -04001874 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001875
Trond Myklebusted748aa2011-09-12 19:37:06 -04001876 path_get(&cur);
1877 /* First walk the path up to the nfsd root, and store the
1878 * dentries/path components in an array.
1879 */
1880 for (;;) {
1881 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1882 break;
1883 if (cur.dentry == cur.mnt->mnt_root) {
1884 if (follow_up(&cur))
1885 continue;
1886 goto out_free;
1887 }
1888 if ((ncomponents & 15) == 0) {
1889 struct dentry **new;
1890 new = krealloc(components,
1891 sizeof(*new) * (ncomponents + 16),
1892 GFP_KERNEL);
1893 if (!new)
1894 goto out_free;
1895 components = new;
1896 }
1897 components[ncomponents++] = cur.dentry;
1898 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001899 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001900 err = nfserr_resource;
1901 p = xdr_reserve_space(xdr, 4);
1902 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04001903 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04001904 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001905
1906 while (ncomponents) {
1907 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04001908 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001909
Al Viro301f0262013-09-01 16:06:39 -04001910 spin_lock(&dentry->d_lock);
1911 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001912 p = xdr_reserve_space(xdr, len + 4);
1913 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04001914 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001915 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04001916 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04001917 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04001918 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04001919 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001920 dput(dentry);
1921 ncomponents--;
1922 }
1923
Trond Myklebusted748aa2011-09-12 19:37:06 -04001924 err = 0;
1925out_free:
1926 dprintk(")\n");
1927 while (ncomponents)
1928 dput(components[--ncomponents]);
1929 kfree(components);
1930 path_put(&cur);
1931 return err;
1932}
1933
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001934static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
1935 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04001936{
1937 struct svc_export *exp_ps;
1938 __be32 res;
1939
1940 exp_ps = rqst_find_fsidzero_export(rqstp);
1941 if (IS_ERR(exp_ps))
1942 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001943 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001944 exp_put(exp_ps);
1945 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001946}
1947
1948/*
1949 * encode a fs_locations structure
1950 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001951static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
1952 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001953{
Al Virob37ad282006-10-19 23:28:59 -07001954 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001955 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001956 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001957 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001958
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001959 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001960 if (status)
1961 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001962 p = xdr_reserve_space(xdr, 4);
1963 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001964 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04001965 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001966 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001967 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001968 if (status)
1969 return status;
1970 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001971 return 0;
1972}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001974static u32 nfs4_file_type(umode_t mode)
1975{
1976 switch (mode & S_IFMT) {
1977 case S_IFIFO: return NF4FIFO;
1978 case S_IFCHR: return NF4CHR;
1979 case S_IFDIR: return NF4DIR;
1980 case S_IFBLK: return NF4BLK;
1981 case S_IFLNK: return NF4LNK;
1982 case S_IFREG: return NF4REG;
1983 case S_IFSOCK: return NF4SOCK;
1984 default: return NF4BAD;
1985 };
1986}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Al Virob37ad282006-10-19 23:28:59 -07001988static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001989nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
1990 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
J. Bruce Fields35541162014-01-08 09:49:01 -05001992 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001993 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05001994 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001995 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05001996 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001997 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002000#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2001 FATTR4_WORD0_RDATTR_ERROR)
2002#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2003
David Quigley18032ca2013-05-02 13:19:10 -04002004#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2005static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002006nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2007 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002008{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002009 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002010
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002011 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2012 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002013 return nfserr_resource;
2014
2015 /*
2016 * For now we use a 0 here to indicate the null translation; in
2017 * the future we may place a call to translation code here.
2018 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002019 *p++ = cpu_to_be32(0); /* lfs */
2020 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002021 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002022 return 0;
2023}
2024#else
2025static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002026nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2027 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002028{ return 0; }
2029#endif
2030
Al Virob37ad282006-10-19 23:28:59 -07002031static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002032{
2033 /* As per referral draft: */
2034 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2035 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2036 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2037 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2038 *rdattr_err = NFSERR_MOVED;
2039 else
2040 return nfserr_moved;
2041 }
2042 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2043 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2044 return 0;
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002047
2048static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2049{
2050 struct path path = exp->ex_path;
2051 int err;
2052
2053 path_get(&path);
2054 while (follow_up(&path)) {
2055 if (path.dentry != path.mnt->mnt_root)
2056 break;
2057 }
Al Viro3dadecc2013-01-24 02:18:08 -05002058 err = vfs_getattr(&path, stat);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002059 path_put(&path);
2060 return err;
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063/*
2064 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2065 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002067static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002068nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2069 struct svc_export *exp,
2070 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002071 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 u32 bmval0 = bmval[0];
2074 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002075 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002077 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002079 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002080 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002081 int attrlen_offset;
2082 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 u32 dummy;
2084 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002085 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002086 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002087 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int aclsupport = 0;
2089 struct nfs4_acl *acl = NULL;
David Quigley18032ca2013-05-02 13:19:10 -04002090 void *context = NULL;
2091 int contextlen;
2092 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002093 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2094 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002095 struct path path = {
2096 .mnt = exp->ex_path.mnt,
2097 .dentry = dentry,
2098 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002099 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002102 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2103 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2104 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002106 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002107 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002108 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2109 if (status)
2110 goto out;
2111 }
2112
Al Viro3dadecc2013-01-24 02:18:08 -05002113 err = vfs_getattr(&path, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002114 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002116 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2117 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2119 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002120 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002121 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto out_nfserr;
2123 }
2124 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002125 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2126 status = nfserr_jukebox;
2127 if (!tempfh)
2128 goto out;
2129 fh_init(tempfh, NFS4_FHSIZE);
2130 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (status)
2132 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002133 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2136 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002137 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2138 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002140 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002142 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 status = nfserr_attrnotsupp;
2144 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002145 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 goto out_nfserr;
2147 }
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Quigley18032ca2013-05-02 13:19:10 -04002150#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2151 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2152 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2153 err = security_inode_getsecctx(dentry->d_inode,
2154 &context, &contextlen);
2155 contextsupport = (err == 0);
2156 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2157 if (err == -EOPNOTSUPP)
2158 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2159 else if (err)
2160 goto out_nfserr;
2161 }
2162 }
2163#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2164
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002165 if (bmval2) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002166 p = xdr_reserve_space(xdr, 16);
2167 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002168 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002169 *p++ = cpu_to_be32(3);
2170 *p++ = cpu_to_be32(bmval0);
2171 *p++ = cpu_to_be32(bmval1);
2172 *p++ = cpu_to_be32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002173 } else if (bmval1) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002174 p = xdr_reserve_space(xdr, 12);
2175 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002176 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002177 *p++ = cpu_to_be32(2);
2178 *p++ = cpu_to_be32(bmval0);
2179 *p++ = cpu_to_be32(bmval1);
Andy Adamson7e705702009-04-03 08:29:11 +03002180 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002181 p = xdr_reserve_space(xdr, 8);
2182 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002183 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002184 *p++ = cpu_to_be32(1);
2185 *p++ = cpu_to_be32(bmval0);
Andy Adamson7e705702009-04-03 08:29:11 +03002186 }
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002187
2188 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002189 p = xdr_reserve_space(xdr, 4);
2190 if (!p)
2191 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002192 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002195 u32 word0 = nfsd_suppattrs0(minorversion);
2196 u32 word1 = nfsd_suppattrs1(minorversion);
2197 u32 word2 = nfsd_suppattrs2(minorversion);
2198
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002199 if (!aclsupport)
2200 word0 &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002201 if (!contextsupport)
2202 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
Andy Adamson7e705702009-04-03 08:29:11 +03002203 if (!word2) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002204 p = xdr_reserve_space(xdr, 12);
2205 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002206 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002207 *p++ = cpu_to_be32(2);
2208 *p++ = cpu_to_be32(word0);
2209 *p++ = cpu_to_be32(word1);
Andy Adamson7e705702009-04-03 08:29:11 +03002210 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002211 p = xdr_reserve_space(xdr, 16);
2212 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002213 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002214 *p++ = cpu_to_be32(3);
2215 *p++ = cpu_to_be32(word0);
2216 *p++ = cpu_to_be32(word1);
2217 *p++ = cpu_to_be32(word2);
Andy Adamson7e705702009-04-03 08:29:11 +03002218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002221 p = xdr_reserve_space(xdr, 4);
2222 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002224 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002225 if (dummy == NF4BAD) {
2226 status = nfserr_serverfault;
2227 goto out;
2228 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002229 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002232 p = xdr_reserve_space(xdr, 4);
2233 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002235 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002236 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002237 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002238 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2239 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002242 p = xdr_reserve_space(xdr, 8);
2243 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto out_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002245 p = encode_change(p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002248 p = xdr_reserve_space(xdr, 8);
2249 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002251 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002254 p = xdr_reserve_space(xdr, 4);
2255 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002257 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002260 p = xdr_reserve_space(xdr, 4);
2261 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002263 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002266 p = xdr_reserve_space(xdr, 4);
2267 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002269 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002272 p = xdr_reserve_space(xdr, 16);
2273 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002275 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002276 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2277 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002278 } else switch(fsid_source(fhp)) {
2279 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002280 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2281 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002282 break;
2283 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002284 *p++ = cpu_to_be32(0);
2285 *p++ = cpu_to_be32(MAJOR(stat.dev));
2286 *p++ = cpu_to_be32(0);
2287 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002288 break;
2289 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08002290 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2291 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294 }
2295 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002296 p = xdr_reserve_space(xdr, 4);
2297 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002299 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002302 p = xdr_reserve_space(xdr, 4);
2303 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002305 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002308 p = xdr_reserve_space(xdr, 4);
2309 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002311 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 if (bmval0 & FATTR4_WORD0_ACL) {
2314 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002317 p = xdr_reserve_space(xdr, 4);
2318 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 goto out_resource;
2320
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002321 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 goto out_acl;
2323 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002324 p = xdr_reserve_space(xdr, 4);
2325 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002327 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002329 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002330 p = xdr_reserve_space(xdr, 4*3);
2331 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002333 *p++ = cpu_to_be32(ace->type);
2334 *p++ = cpu_to_be32(ace->flag);
2335 *p++ = cpu_to_be32(ace->access_mask &
2336 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002337 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (status)
2339 goto out;
2340 }
2341 }
2342out_acl:
2343 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002344 p = xdr_reserve_space(xdr, 4);
2345 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002347 *p++ = cpu_to_be32(aclsupport ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2349 }
2350 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002351 p = xdr_reserve_space(xdr, 4);
2352 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002354 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002357 p = xdr_reserve_space(xdr, 4);
2358 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002360 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002363 p = xdr_reserve_space(xdr, 4);
2364 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002366 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002369 p = xdr_reserve_space(xdr, 4);
2370 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002372 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002375 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
2376 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002378 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
2379 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002382 p = xdr_reserve_space(xdr, 8);
2383 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002385 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002388 p = xdr_reserve_space(xdr, 8);
2389 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002391 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002394 p = xdr_reserve_space(xdr, 8);
2395 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002397 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002400 p = xdr_reserve_space(xdr, 8);
2401 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002403 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002405 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002406 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002407 if (status)
2408 goto out;
2409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002411 p = xdr_reserve_space(xdr, 4);
2412 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002414 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002417 p = xdr_reserve_space(xdr, 8);
2418 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002420 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002423 p = xdr_reserve_space(xdr, 4);
2424 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002426 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002429 p = xdr_reserve_space(xdr, 4);
2430 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002432 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002435 p = xdr_reserve_space(xdr, 8);
2436 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002438 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002441 p = xdr_reserve_space(xdr, 8);
2442 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002444 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002447 p = xdr_reserve_space(xdr, 4);
2448 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002450 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002453 p = xdr_reserve_space(xdr, 4);
2454 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002456 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002459 p = xdr_reserve_space(xdr, 4);
2460 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002462 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002465 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (status)
2467 goto out;
2468 }
2469 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002470 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (status)
2472 goto out;
2473 }
2474 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002475 p = xdr_reserve_space(xdr, 8);
2476 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002478 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
2479 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002482 p = xdr_reserve_space(xdr, 8);
2483 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 goto out_resource;
2485 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002486 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002489 p = xdr_reserve_space(xdr, 8);
2490 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto out_resource;
2492 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002493 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002496 p = xdr_reserve_space(xdr, 8);
2497 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 goto out_resource;
2499 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002500 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002503 p = xdr_reserve_space(xdr, 8);
2504 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 goto out_resource;
2506 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002507 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
2509 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002510 p = xdr_reserve_space(xdr, 12);
2511 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002513 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002514 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002517 p = xdr_reserve_space(xdr, 12);
2518 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002520 *p++ = cpu_to_be32(0);
2521 *p++ = cpu_to_be32(1);
2522 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002525 p = xdr_reserve_space(xdr, 12);
2526 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002528 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002529 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002532 p = xdr_reserve_space(xdr, 12);
2533 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002535 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002536 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002539 p = xdr_reserve_space(xdr, 8);
2540 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002542 /*
2543 * Get parent's attributes if not ignoring crossmount
2544 * and this is the root of a cross-mounted filesystem.
2545 */
2546 if (ignore_crossmnt == 0 &&
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002547 dentry == exp->ex_path.mnt->mnt_root)
2548 get_parent_attributes(exp, &stat);
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002549 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
David Quigley18032ca2013-05-02 13:19:10 -04002551 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002552 status = nfsd4_encode_security_label(xdr, rqstp, context,
2553 contextlen);
David Quigley18032ca2013-05-02 13:19:10 -04002554 if (status)
2555 goto out;
2556 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002557 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002558 p = xdr_reserve_space(xdr, 16);
2559 if (!p)
J. Bruce Fieldsde3997a2014-01-28 16:05:15 -05002560 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002561 *p++ = cpu_to_be32(3);
2562 *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2563 *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2564 *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
Benny Halevy8c18f202009-04-03 08:29:14 +03002565 }
Andy Adamson7e705702009-04-03 08:29:11 +03002566
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002567 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
2568 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 status = nfs_ok;
2570
2571out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002572#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002573 if (context)
2574 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002575#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002576 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08002577 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002578 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08002579 kfree(tempfh);
2580 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002581 if (status)
2582 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return status;
2584out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002585 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 goto out;
2587out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 status = nfserr_resource;
2589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002592static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
2593 struct xdr_buf *buf, __be32 *p, int bytes)
2594{
2595 xdr->scratch.iov_len = 0;
2596 memset(buf, 0, sizeof(struct xdr_buf));
2597 buf->head[0].iov_base = p;
2598 buf->head[0].iov_len = 0;
2599 buf->len = 0;
2600 xdr->buf = buf;
2601 xdr->iov = buf->head;
2602 xdr->p = p;
2603 xdr->end = (void *)p + bytes;
2604 buf->buflen = bytes;
2605}
2606
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002607__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
2608 struct svc_fh *fhp, struct svc_export *exp,
2609 struct dentry *dentry, u32 *bmval,
2610 struct svc_rqst *rqstp, int ignore_crossmnt)
2611{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002612 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002613 struct xdr_stream xdr;
2614 __be32 ret;
2615
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002616 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002617 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
2618 ignore_crossmnt);
2619 *p = xdr.p;
2620 return ret;
2621}
2622
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002623static inline int attributes_need_mount(u32 *bmval)
2624{
2625 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2626 return 1;
2627 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2628 return 1;
2629 return 0;
2630}
2631
Al Virob37ad282006-10-19 23:28:59 -07002632static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002633nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
2634 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 struct svc_export *exp = cd->rd_fhp->fh_export;
2637 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002638 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002639 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2642 if (IS_ERR(dentry))
2643 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002644 if (!dentry->d_inode) {
2645 /*
2646 * nfsd_buffered_readdir drops the i_mutex between
2647 * readdir and calling this callback, leaving a window
2648 * where this directory entry could have gone away.
2649 */
2650 dput(dentry);
2651 return nfserr_noent;
2652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002655 /*
2656 * In the case of a mountpoint, the client may be asking for
2657 * attributes that are only properties of the underlying filesystem
2658 * as opposed to the cross-mounted file system. In such a case,
2659 * we will not follow the cross mount and will fill the attribtutes
2660 * directly from the mountpoint dentry.
2661 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002662 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002663 int err;
2664
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002665 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2666 && !attributes_need_mount(cd->rd_bmval)) {
2667 ignore_crossmnt = 1;
2668 goto out_encode;
2669 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002670 /*
2671 * Why the heck aren't we just using nfsd_lookup??
2672 * Different "."/".." handling? Something else?
2673 * At least, add a comment here to explain....
2674 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002675 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2676 if (err) {
2677 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 goto out_put;
2679 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002680 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2681 if (nfserr)
2682 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002685out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002686 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002687 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688out_put:
2689 dput(dentry);
2690 exp_put(exp);
2691 return nfserr;
2692}
2693
Al Viro2ebbc012006-10-19 23:28:58 -07002694static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002695nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002697 __be32 *p;
2698
Kinglong Meec3a45612014-07-06 11:34:43 +08002699 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002700 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return NULL;
2702 *p++ = htonl(2);
2703 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2704 *p++ = htonl(0); /* bmval1 */
2705
J. Bruce Fields87915c62013-01-16 17:33:28 -05002706 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return p;
2709}
2710
2711static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002712nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2713 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002715 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002717 struct xdr_stream *xdr = cd->xdr;
2718 int start_offset = xdr->buf->len;
2719 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04002720 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002721 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07002722 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002723 __be64 wire_offset;
2724 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 /* In nfsv4, "." and ".." never make it onto the wire.. */
2727 if (name && isdotent(name, namlen)) {
2728 cd->common.err = nfs_ok;
2729 return 0;
2730 }
2731
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002732 if (cd->cookie_offset) {
2733 wire_offset = cpu_to_be64(offset);
2734 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
2735 &wire_offset, 8);
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002738 p = xdr_reserve_space(xdr, 4);
2739 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002742 cookie_offset = xdr->buf->len;
2743 p = xdr_reserve_space(xdr, 3*4 + namlen);
2744 if (!p)
2745 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2747 p = xdr_encode_array(p, name, namlen); /* name length & name */
2748
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002749 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 switch (nfserr) {
2751 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 break;
2753 case nfserr_resource:
2754 nfserr = nfserr_toosmall;
2755 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002756 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08002757 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002758 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 default:
2760 /*
2761 * If the client requested the RDATTR_ERROR attribute,
2762 * we stuff the error code into this attribute
2763 * and continue. If this attribute was not requested,
2764 * then in accordance with the spec, we fail the
2765 * entire READDIR operation(!)
2766 */
2767 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2768 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002769 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002770 if (p == NULL) {
2771 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002775 nfserr = nfserr_toosmall;
2776 entry_bytes = xdr->buf->len - start_offset;
2777 if (entry_bytes > cd->rd_maxcount)
2778 goto fail;
2779 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04002780 /*
2781 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
2782 * let's always let through the first entry, at least:
2783 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05002784 if (!cd->rd_dircount)
2785 goto fail;
2786 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04002787 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
2788 goto fail;
2789 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05002790
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002791 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002792skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 cd->common.err = nfs_ok;
2794 return 0;
2795fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002796 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 cd->common.err = nfserr;
2798 return -EINVAL;
2799}
2800
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002801static __be32
2802nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03002803{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002804 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002805
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002806 p = xdr_reserve_space(xdr, sizeof(stateid_t));
2807 if (!p)
2808 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002809 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002810 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
2811 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002812 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03002813}
2814
Benny Halevy695e12f2008-07-04 14:38:33 +03002815static __be32
Al Virob37ad282006-10-19 23:28:59 -07002816nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002818 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002819 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002822 p = xdr_reserve_space(xdr, 8);
2823 if (!p)
2824 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002825 *p++ = cpu_to_be32(access->ac_supported);
2826 *p++ = cpu_to_be32(access->ac_resp_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002828 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002831static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2832{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002833 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002834 __be32 *p;
2835
2836 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002837 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
2838 if (!p)
2839 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002840 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
2841 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002842 *p++ = cpu_to_be32(bcts->dir);
J. Bruce Fields6e67b5d2012-09-13 09:49:43 -04002843 /* Sorry, we do not yet support RDMA over 4.1: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002844 *p++ = cpu_to_be32(0);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002845 }
2846 return nfserr;
2847}
2848
Benny Halevy695e12f2008-07-04 14:38:33 +03002849static __be32
Al Virob37ad282006-10-19 23:28:59 -07002850nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002852 struct xdr_stream *xdr = &resp->xdr;
2853
Benny Halevye2f282b2008-08-12 20:45:07 +03002854 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002855 nfserr = nfsd4_encode_stateid(xdr, &close->cl_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03002856
Benny Halevy695e12f2008-07-04 14:38:33 +03002857 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
2860
Benny Halevy695e12f2008-07-04 14:38:33 +03002861static __be32
Al Virob37ad282006-10-19 23:28:59 -07002862nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002864 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002865 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002868 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
2869 if (!p)
2870 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002871 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
2872 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002874 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Benny Halevy695e12f2008-07-04 14:38:33 +03002877static __be32
Al Virob37ad282006-10-19 23:28:59 -07002878nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002880 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002881 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002884 p = xdr_reserve_space(xdr, 32);
2885 if (!p)
2886 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002887 p = encode_cinfo(p, &create->cr_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002888 *p++ = cpu_to_be32(2);
2889 *p++ = cpu_to_be32(create->cr_bmval[0]);
2890 *p++ = cpu_to_be32(create->cr_bmval[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002892 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
Al Virob37ad282006-10-19 23:28:59 -07002895static __be32
2896nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002899 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 if (nfserr)
2902 return nfserr;
2903
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002904 nfserr = nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
2905 getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002906 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return nfserr;
2908}
2909
Benny Halevy695e12f2008-07-04 14:38:33 +03002910static __be32
2911nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002913 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03002914 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002916 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 if (!nfserr) {
2919 len = fhp->fh_handle.fh_size;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002920 p = xdr_reserve_space(xdr, len + 4);
2921 if (!p)
2922 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002923 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002925 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928/*
2929* Including all fields other than the name, a LOCK4denied structure requires
2930* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2931*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002932static __be32
2933nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002935 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002936 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002938again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002939 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002940 if (!p) {
2941 /*
2942 * Don't fail to return the result just because we can't
2943 * return the conflicting open:
2944 */
2945 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08002946 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002947 conf->len = 0;
2948 conf->data = NULL;
2949 goto again;
2950 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002951 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002952 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002953 p = xdr_encode_hyper(p, ld->ld_start);
2954 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002955 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002956 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002957 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
2958 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08002959 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002961 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002962 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002964 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Benny Halevy695e12f2008-07-04 14:38:33 +03002967static __be32
Al Virob37ad282006-10-19 23:28:59 -07002968nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002970 struct xdr_stream *xdr = &resp->xdr;
2971
Benny Halevye2f282b2008-08-12 20:45:07 +03002972 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002973 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03002974 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002975 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08002976
Benny Halevy695e12f2008-07-04 14:38:33 +03002977 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Benny Halevy695e12f2008-07-04 14:38:33 +03002980static __be32
Al Virob37ad282006-10-19 23:28:59 -07002981nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002983 struct xdr_stream *xdr = &resp->xdr;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002986 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002987 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
Benny Halevy695e12f2008-07-04 14:38:33 +03002990static __be32
Al Virob37ad282006-10-19 23:28:59 -07002991nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002993 struct xdr_stream *xdr = &resp->xdr;
2994
Benny Halevye2f282b2008-08-12 20:45:07 +03002995 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002996 nfserr = nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03002997
Benny Halevy695e12f2008-07-04 14:38:33 +03002998 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001
Benny Halevy695e12f2008-07-04 14:38:33 +03003002static __be32
Al Virob37ad282006-10-19 23:28:59 -07003003nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003005 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003006 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003009 p = xdr_reserve_space(xdr, 20);
3010 if (!p)
3011 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003012 p = encode_cinfo(p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003014 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017
Benny Halevy695e12f2008-07-04 14:38:33 +03003018static __be32
Al Virob37ad282006-10-19 23:28:59 -07003019nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003021 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003022 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 if (nfserr)
3025 goto out;
3026
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003027 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3028 if (nfserr)
3029 goto out;
3030 p = xdr_reserve_space(xdr, 40);
3031 if (!p)
3032 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003033 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003034 *p++ = cpu_to_be32(open->op_rflags);
3035 *p++ = cpu_to_be32(2);
3036 *p++ = cpu_to_be32(open->op_bmval[0]);
3037 *p++ = cpu_to_be32(open->op_bmval[1]);
3038 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 switch (open->op_delegate_type) {
3041 case NFS4_OPEN_DELEGATE_NONE:
3042 break;
3043 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003044 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3045 if (nfserr)
3046 return nfserr;
3047 p = xdr_reserve_space(xdr, 20);
3048 if (!p)
3049 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003050 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 /*
3053 * TODO: ACE's in delegations
3054 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003055 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3056 *p++ = cpu_to_be32(0);
3057 *p++ = cpu_to_be32(0);
3058 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 break;
3060 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003061 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3062 if (nfserr)
3063 return nfserr;
3064 p = xdr_reserve_space(xdr, 32);
3065 if (!p)
3066 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003067 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 /*
3070 * TODO: space_limit's in delegations
3071 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003072 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3073 *p++ = cpu_to_be32(~(u32)0);
3074 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 /*
3077 * TODO: ACE's in delegations
3078 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003079 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3080 *p++ = cpu_to_be32(0);
3081 *p++ = cpu_to_be32(0);
3082 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003084 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3085 switch (open->op_why_no_deleg) {
3086 case WND4_CONTENTION:
3087 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003088 p = xdr_reserve_space(xdr, 8);
3089 if (!p)
3090 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003091 *p++ = cpu_to_be32(open->op_why_no_deleg);
3092 /* deleg signaling not supported yet: */
3093 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003094 break;
3095 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003096 p = xdr_reserve_space(xdr, 4);
3097 if (!p)
3098 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003099 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003100 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 default:
3103 BUG();
3104 }
3105 /* XXX save filehandle here */
3106out:
Benny Halevy695e12f2008-07-04 14:38:33 +03003107 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Benny Halevy695e12f2008-07-04 14:38:33 +03003110static __be32
Al Virob37ad282006-10-19 23:28:59 -07003111nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003113 struct xdr_stream *xdr = &resp->xdr;
3114
Benny Halevye2f282b2008-08-12 20:45:07 +03003115 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003116 nfserr = nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Benny Halevy695e12f2008-07-04 14:38:33 +03003118 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Benny Halevy695e12f2008-07-04 14:38:33 +03003121static __be32
Al Virob37ad282006-10-19 23:28:59 -07003122nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003124 struct xdr_stream *xdr = &resp->xdr;
3125
Benny Halevye2f282b2008-08-12 20:45:07 +03003126 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003127 nfserr = nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Benny Halevy695e12f2008-07-04 14:38:33 +03003129 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003132static __be32 nfsd4_encode_splice_read(
3133 struct nfsd4_compoundres *resp,
3134 struct nfsd4_read *read,
3135 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003137 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b42014-05-13 16:32:04 -04003138 struct xdr_buf *buf = xdr->buf;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003139 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003140 int space_left;
3141 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003142 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003143
Kinglong Meed5d5c302014-07-09 21:51:27 +08003144 /* Make sure there will be room for padding if needed */
3145 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003146 return nfserr_resource;
3147
3148 nfserr = nfsd_splice_read(read->rd_rqstp, file,
3149 read->rd_offset, &maxcount);
3150 if (nfserr) {
3151 /*
3152 * nfsd_splice_actor may have already messed with the
3153 * page length; reset it so as not to confuse
3154 * xdr_truncate_encode:
3155 */
J. Bruce Fields34a78b42014-05-13 16:32:04 -04003156 buf->page_len = 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003157 return nfserr;
3158 }
3159
3160 eof = (read->rd_offset + maxcount >=
3161 read->rd_fhp->fh_dentry->d_inode->i_size);
3162
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003163 *(p++) = htonl(eof);
3164 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003165
J. Bruce Fields34a78b42014-05-13 16:32:04 -04003166 buf->page_len = maxcount;
3167 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003168 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3169 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003170
3171 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b42014-05-13 16:32:04 -04003172 buf->tail[0].iov_base = xdr->p;
3173 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003174 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003175 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003176 int pad = 4 - (maxcount&3);
3177
3178 *(xdr->p++) = 0;
3179
J. Bruce Fields34a78b42014-05-13 16:32:04 -04003180 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003181 buf->tail[0].iov_len = pad;
3182 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003183 }
3184
3185 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b42014-05-13 16:32:04 -04003186 buf->buflen - buf->len);
3187 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003188 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3189
3190 return 0;
3191}
3192
3193static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3194 struct nfsd4_read *read,
3195 struct file *file, unsigned long maxcount)
3196{
3197 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 u32 eof;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003199 int v;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003200 int starting_len = xdr->buf->len - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 long len;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003202 int thislen;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003203 __be32 nfserr;
3204 __be32 tmp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003205 __be32 *p;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003206 u32 zzz = 0;
3207 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 len = maxcount;
3210 v = 0;
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003211
Kinglong Mee10554142014-06-29 19:18:17 +08003212 thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003213 p = xdr_reserve_space(xdr, (thislen+3)&~3);
3214 WARN_ON_ONCE(!p);
3215 resp->rqstp->rq_vec[v].iov_base = p;
3216 resp->rqstp->rq_vec[v].iov_len = thislen;
3217 v++;
3218 len -= thislen;
3219
3220 while (len) {
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003221 thislen = min_t(long, len, PAGE_SIZE);
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003222 p = xdr_reserve_space(xdr, (thislen+3)&~3);
3223 WARN_ON_ONCE(!p);
3224 resp->rqstp->rq_vec[v].iov_base = p;
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003225 resp->rqstp->rq_vec[v].iov_len = thislen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 v++;
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003227 len -= thislen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
3229 read->rd_vlen = v;
3230
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003231 nfserr = nfsd_readv(file, read->rd_offset, resp->rqstp->rq_vec,
3232 read->rd_vlen, &maxcount);
3233 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return nfserr;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003235 xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003236
NeilBrown44524352006-10-04 02:15:46 -07003237 eof = (read->rd_offset + maxcount >=
3238 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003240 tmp = htonl(eof);
3241 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3242 tmp = htonl(maxcount);
3243 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3244
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003245 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3246 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
3247 &zzz, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003249
3250}
3251
3252static __be32
3253nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3254 struct nfsd4_read *read)
3255{
3256 unsigned long maxcount;
3257 struct xdr_stream *xdr = &resp->xdr;
3258 struct file *file = read->rd_filp;
3259 int starting_len = xdr->buf->len;
3260 struct raparms *ra;
3261 __be32 *p;
3262 __be32 err;
3263
3264 if (nfserr)
3265 return nfserr;
3266
3267 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3268 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003269 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003270 return nfserr_resource;
3271 }
Jeff Layton779fb0f2014-11-19 07:51:18 -05003272 if (resp->xdr.buf->page_len && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003273 WARN_ON_ONCE(1);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003274 return nfserr_resource;
3275 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003276 xdr_commit_encode(xdr);
3277
3278 maxcount = svc_max_payload(resp->rqstp);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08003279 maxcount = min_t(unsigned long, maxcount, (xdr->buf->buflen - xdr->buf->len));
3280 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003281
3282 if (!read->rd_filp) {
3283 err = nfsd_get_tmp_read_open(resp->rqstp, read->rd_fhp,
3284 &file, &ra);
3285 if (err)
3286 goto err_truncate;
3287 }
3288
Jeff Layton779fb0f2014-11-19 07:51:18 -05003289 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003290 err = nfsd4_encode_splice_read(resp, read, file, maxcount);
3291 else
3292 err = nfsd4_encode_readv(resp, read, file, maxcount);
3293
3294 if (!read->rd_filp)
3295 nfsd_put_tmp_read_open(file, ra);
3296
3297err_truncate:
3298 if (err)
3299 xdr_truncate_encode(xdr, starting_len);
3300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Al Virob37ad282006-10-19 23:28:59 -07003303static __be32
3304nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003307 __be32 wire_count;
3308 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003309 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003310 int length_offset = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003311 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 if (nfserr)
3314 return nfserr;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003315
3316 p = xdr_reserve_space(xdr, 4);
3317 if (!p)
3318 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003320
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003321 p = xdr_reserve_space(xdr, maxcount);
3322 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04003323 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /*
3325 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3326 * if they would overflow the buffer. Is this kosher in NFSv4? If
3327 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3328 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3329 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003330 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
3331 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003333 nfserr = nfserr_inval;
3334 if (nfserr) {
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003335 xdr_truncate_encode(xdr, length_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return nfserr;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003339 wire_count = htonl(maxcount);
3340 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03003341 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003342 if (maxcount & 3)
3343 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
3344 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return 0;
3346}
3347
Al Virob37ad282006-10-19 23:28:59 -07003348static __be32
3349nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
3351 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003352 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003354 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003355 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003356 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003357 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 if (nfserr)
3360 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003362 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3363 if (!p)
3364 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003367 *p++ = cpu_to_be32(0);
3368 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05003369 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
3370 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003373 * Number of bytes left for directory entries allowing for the
3374 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003376 bytes_left = xdr->buf->buflen - xdr->buf->len
3377 - COMPOUND_ERR_SLACK_SPACE - 8;
3378 if (bytes_left < 0) {
3379 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 goto err_no_verf;
3381 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003382 maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
3383 /*
3384 * Note the rfc defines rd_maxcount as the size of the
3385 * READDIR4resok structure, which includes the verifier above
3386 * and the 8 bytes encoded at the end of this function:
3387 */
3388 if (maxcount < 16) {
3389 nfserr = nfserr_toosmall;
3390 goto err_no_verf;
3391 }
3392 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003394 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
3395 if (!readdir->rd_dircount)
3396 readdir->rd_dircount = INT_MAX;
3397
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003398 readdir->xdr = xdr;
3399 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003401 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 offset = readdir->rd_cookie;
3404 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3405 &offset,
3406 &readdir->common, nfsd4_encode_dirent);
3407 if (nfserr == nfs_ok &&
3408 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003409 xdr->buf->len == starting_len + 8) {
3410 /* nothing encoded; which limit did we hit?: */
3411 if (maxcount - 16 < bytes_left)
3412 /* It was the fault of rd_maxcount: */
3413 nfserr = nfserr_toosmall;
3414 else
3415 /* We ran out of buffer space: */
3416 nfserr = nfserr_resource;
3417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (nfserr)
3419 goto err_no_verf;
3420
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003421 if (readdir->cookie_offset) {
3422 wire_offset = cpu_to_be64(offset);
3423 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
3424 &wire_offset, 8);
3425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003427 p = xdr_reserve_space(xdr, 8);
3428 if (!p) {
3429 WARN_ON_ONCE(1);
3430 goto err_no_verf;
3431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 *p++ = 0; /* no more entries */
3433 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 return 0;
3436err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003437 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return nfserr;
3439}
3440
Benny Halevy695e12f2008-07-04 14:38:33 +03003441static __be32
Al Virob37ad282006-10-19 23:28:59 -07003442nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003444 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003445 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003448 p = xdr_reserve_space(xdr, 20);
3449 if (!p)
3450 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003451 p = encode_cinfo(p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003453 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Benny Halevy695e12f2008-07-04 14:38:33 +03003456static __be32
Al Virob37ad282006-10-19 23:28:59 -07003457nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003459 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003460 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003463 p = xdr_reserve_space(xdr, 40);
3464 if (!p)
3465 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003466 p = encode_cinfo(p, &rename->rn_sinfo);
3467 p = encode_cinfo(p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003469 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Benny Halevy695e12f2008-07-04 14:38:33 +03003472static __be32
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003473nfsd4_do_encode_secinfo(struct xdr_stream *xdr,
Chuck Levera77c8062013-03-16 15:55:10 -04003474 __be32 nfserr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003475{
Chuck Lever676e4eb2013-04-30 18:48:54 -04003476 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003477 struct exp_flavor_info *flavs;
3478 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04003479 __be32 *p, *flavorsp;
3480 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003481
3482 if (nfserr)
3483 goto out;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003484 nfserr = nfserr_resource;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003485 if (exp->ex_nflavors) {
3486 flavs = exp->ex_flavors;
3487 nflavs = exp->ex_nflavors;
3488 } else { /* Handling of some defaults in absence of real secinfo: */
3489 flavs = def_flavs;
3490 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3491 nflavs = 2;
3492 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3493 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3494 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3495 nflavs = 1;
3496 flavs[0].pseudoflavor
3497 = svcauth_gss_flavor(exp->ex_client);
3498 } else {
3499 nflavs = 1;
3500 flavs[0].pseudoflavor
3501 = exp->ex_client->flavour->flavour;
3502 }
3503 }
3504
Chuck Lever676e4eb2013-04-30 18:48:54 -04003505 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003506 p = xdr_reserve_space(xdr, 4);
3507 if (!p)
3508 goto out;
Chuck Lever676e4eb2013-04-30 18:48:54 -04003509 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04003510
J. Bruce Fields4796f452007-07-17 04:04:51 -07003511 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04003512 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04003513 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003514
Chuck Lever676e4eb2013-04-30 18:48:54 -04003515 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3516 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003517 p = xdr_reserve_space(xdr, 4 + 4 +
3518 XDR_LEN(info.oid.len) + 4 + 4);
3519 if (!p)
3520 goto out;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003521 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003522 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003523 *p++ = cpu_to_be32(info.qop);
3524 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003525 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3526 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003527 p = xdr_reserve_space(xdr, 4);
3528 if (!p)
3529 goto out;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003530 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003531 } else {
3532 if (report)
3533 pr_warn("NFS: SECINFO: security flavor %u "
3534 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003535 }
3536 }
Chuck Levera77c8062013-03-16 15:55:10 -04003537
Chuck Lever676e4eb2013-04-30 18:48:54 -04003538 if (nflavs != supported)
3539 report = false;
3540 *flavorsp = htonl(supported);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003541 nfserr = 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003542out:
3543 if (exp)
3544 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003545 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003546}
3547
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003548static __be32
3549nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3550 struct nfsd4_secinfo *secinfo)
3551{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003552 struct xdr_stream *xdr = &resp->xdr;
3553
3554 return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003555}
3556
3557static __be32
3558nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3559 struct nfsd4_secinfo_no_name *secinfo)
3560{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003561 struct xdr_stream *xdr = &resp->xdr;
3562
3563 return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003564}
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566/*
3567 * The SETATTR encode routine is special -- it always encodes a bitmap,
3568 * regardless of the error status.
3569 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003570static __be32
Al Virob37ad282006-10-19 23:28:59 -07003571nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003573 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003574 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003576 p = xdr_reserve_space(xdr, 16);
3577 if (!p)
3578 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003580 *p++ = cpu_to_be32(3);
3581 *p++ = cpu_to_be32(0);
3582 *p++ = cpu_to_be32(0);
3583 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
3585 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003586 *p++ = cpu_to_be32(3);
3587 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
3588 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
3589 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003591 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
Benny Halevy695e12f2008-07-04 14:38:33 +03003594static __be32
Al Virob37ad282006-10-19 23:28:59 -07003595nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003597 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003598 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003601 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
3602 if (!p)
3603 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003604 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
3605 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
3606 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 }
3608 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003609 p = xdr_reserve_space(xdr, 8);
3610 if (!p)
3611 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003612 *p++ = cpu_to_be32(0);
3613 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003615 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
Benny Halevy695e12f2008-07-04 14:38:33 +03003618static __be32
Al Virob37ad282006-10-19 23:28:59 -07003619nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003621 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003622 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003625 p = xdr_reserve_space(xdr, 16);
3626 if (!p)
3627 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003628 *p++ = cpu_to_be32(write->wr_bytes_written);
3629 *p++ = cpu_to_be32(write->wr_how_written);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003630 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
3631 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003633 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
J. Bruce Fields57266a62013-04-13 14:27:29 -04003636static const u32 nfs4_minimal_spo_must_enforce[2] = {
3637 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3638 1 << (OP_EXCHANGE_ID - 32) |
3639 1 << (OP_CREATE_SESSION - 32) |
3640 1 << (OP_DESTROY_SESSION - 32) |
3641 1 << (OP_DESTROY_CLIENTID - 32)
3642};
3643
Benny Halevy695e12f2008-07-04 14:38:33 +03003644static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003645nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003646 struct nfsd4_exchange_id *exid)
3647{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003648 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003649 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003650 char *major_id;
3651 char *server_scope;
3652 int major_id_sz;
3653 int server_scope_sz;
3654 uint64_t minor_id = 0;
3655
3656 if (nfserr)
3657 return nfserr;
3658
3659 major_id = utsname()->nodename;
3660 major_id_sz = strlen(major_id);
3661 server_scope = utsname()->nodename;
3662 server_scope_sz = strlen(server_scope);
3663
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003664 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03003665 8 /* eir_clientid */ +
3666 4 /* eir_sequenceid */ +
3667 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003668 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003669 if (!p)
3670 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03003671
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003672 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003673 *p++ = cpu_to_be32(exid->seqid);
3674 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003675
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003676 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003677
J. Bruce Fields57266a62013-04-13 14:27:29 -04003678 switch (exid->spa_how) {
3679 case SP4_NONE:
3680 break;
3681 case SP4_MACH_CRED:
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003682 /* spo_must_enforce, spo_must_allow */
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003683 p = xdr_reserve_space(xdr, 16);
3684 if (!p)
3685 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003686
J. Bruce Fields57266a62013-04-13 14:27:29 -04003687 /* spo_must_enforce bitmap: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003688 *p++ = cpu_to_be32(2);
3689 *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[0]);
3690 *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[1]);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003691 /* empty spo_must_allow bitmap: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003692 *p++ = cpu_to_be32(0);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003693
J. Bruce Fields57266a62013-04-13 14:27:29 -04003694 break;
3695 default:
3696 WARN_ON_ONCE(1);
3697 }
Andy Adamson0733d212009-04-03 08:28:01 +03003698
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003699 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003700 8 /* so_minor_id */ +
3701 4 /* so_major_id.len */ +
3702 (XDR_QUADLEN(major_id_sz) * 4) +
3703 4 /* eir_server_scope.len */ +
3704 (XDR_QUADLEN(server_scope_sz) * 4) +
3705 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003706 if (!p)
3707 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003708
Andy Adamson0733d212009-04-03 08:28:01 +03003709 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003710 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03003711 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003712 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03003713
3714 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003715 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03003716
3717 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003718 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03003719 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003720}
3721
3722static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003723nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003724 struct nfsd4_create_session *sess)
3725{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003726 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003727 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003728
3729 if (nfserr)
3730 return nfserr;
3731
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003732 p = xdr_reserve_space(xdr, 24);
3733 if (!p)
3734 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003735 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
3736 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003737 *p++ = cpu_to_be32(sess->seqid);
3738 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003739
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003740 p = xdr_reserve_space(xdr, 28);
3741 if (!p)
3742 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003743 *p++ = cpu_to_be32(0); /* headerpadsz */
3744 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
3745 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
3746 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
3747 *p++ = cpu_to_be32(sess->fore_channel.maxops);
3748 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
3749 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003750
3751 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003752 p = xdr_reserve_space(xdr, 4);
3753 if (!p)
3754 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003755 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003756 }
3757
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003758 p = xdr_reserve_space(xdr, 28);
3759 if (!p)
3760 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003761 *p++ = cpu_to_be32(0); /* headerpadsz */
3762 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
3763 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
3764 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
3765 *p++ = cpu_to_be32(sess->back_channel.maxops);
3766 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
3767 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003768
3769 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003770 p = xdr_reserve_space(xdr, 4);
3771 if (!p)
3772 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003773 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003774 }
3775 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003776}
3777
3778static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003779nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003780 struct nfsd4_sequence *seq)
3781{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003782 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003783 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003784
3785 if (nfserr)
3786 return nfserr;
3787
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003788 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
3789 if (!p)
3790 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003791 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
3792 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003793 *p++ = cpu_to_be32(seq->seqid);
3794 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003795 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003796 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
3797 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
3798 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003799
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003800 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003801 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003802}
3803
J. Bruce Fields2355c592012-04-25 16:56:22 -04003804static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003805nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04003806 struct nfsd4_test_stateid *test_stateid)
3807{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003808 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003809 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003810 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003811
J. Bruce Fieldsa11fcce2014-02-03 16:31:42 -05003812 if (nfserr)
3813 return nfserr;
3814
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003815 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
3816 if (!p)
3817 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04003818 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003819
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003820 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003821 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003822 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003823
3824 return nfserr;
3825}
3826
Andy Adamson2db134e2009-04-03 08:27:55 +03003827static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04003828nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
3829 struct nfsd4_seek *seek)
3830{
3831 __be32 *p;
3832
3833 if (nfserr)
3834 return nfserr;
3835
3836 p = xdr_reserve_space(&resp->xdr, 4 + 8);
3837 *p++ = cpu_to_be32(seek->seek_eof);
3838 p = xdr_encode_hyper(p, seek->seek_pos);
3839
3840 return nfserr;
3841}
3842
3843static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003844nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3845{
3846 return nfserr;
3847}
3848
3849typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3850
Andy Adamson2db134e2009-04-03 08:27:55 +03003851/*
3852 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3853 * since we don't need to filter out obsolete ops as this is
3854 * done in the decoding phase.
3855 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003856static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003857 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3858 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3859 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3860 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3861 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3862 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3863 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3864 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3865 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3866 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3867 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3868 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3869 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3870 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3871 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3872 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003873 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003874 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3875 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3876 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3877 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3878 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3879 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3880 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3881 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3882 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3883 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3884 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3885 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3886 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3887 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3888 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3889 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3890 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3891 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3892 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3893 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003894
3895 /* NFSv4.1 operations */
3896 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003897 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003898 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3899 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08003900 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
3901 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003902 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3903 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3904 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3905 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3906 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3907 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003908 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003909 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3910 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003911 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003912 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3913 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3914 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04003915
3916 /* NFSv4.2 operations */
3917 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
3918 [OP_COPY] = (nfsd4_enc)nfsd4_encode_noop,
3919 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
3920 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
3921 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
3922 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
3923 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
3924 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
3925 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
3926 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker24bab492014-09-26 13:58:27 -04003927 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04003928 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003929};
3930
Andy Adamson496c2622009-04-03 08:28:48 +03003931/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04003932 * Calculate whether we still have space to encode repsize bytes.
3933 * There are two considerations:
3934 * - For NFS versions >=4.1, the size of the reply must stay within
3935 * session limits
3936 * - For all NFS versions, we must stay within limited preallocated
3937 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03003938 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04003939 * This is called before the operation is processed, so can only provide
3940 * an upper estimate. For some nonidempotent operations (such as
3941 * getattr), it's not necessarily a problem if that estimate is wrong,
3942 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03003943 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04003944__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03003945{
J. Bruce Fields67492c92014-03-08 16:42:05 -05003946 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003947 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03003948
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003949 if (buf->len + respsize <= buf->buflen)
3950 return nfs_ok;
3951 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05003952 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003953 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
3954 WARN_ON_ONCE(1);
3955 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05003956 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003957 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03003958}
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960void
3961nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3962{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003963 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003964 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05003965 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003966 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05003967 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003968 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003970 p = xdr_reserve_space(xdr, 8);
3971 if (!p) {
3972 WARN_ON_ONCE(1);
3973 return;
3974 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003975 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003976 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Benny Halevy695e12f2008-07-04 14:38:33 +03003978 if (op->opnum == OP_ILLEGAL)
3979 goto status;
3980 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3981 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05003982 encoder = nfsd4_enc_ops[op->opnum];
3983 op->status = encoder(resp, op->status, &op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003984 xdr_commit_encode(xdr);
3985
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04003986 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05003987 if (!op->status) {
3988 int space_needed = 0;
3989 if (!nfsd4_last_compound_op(rqstp))
3990 space_needed = COMPOUND_ERR_SLACK_SPACE;
3991 op->status = nfsd4_check_resp_size(resp, space_needed);
3992 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04003993 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
3994 struct nfsd4_slot *slot = resp->cstate.slot;
3995
3996 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
3997 op->status = nfserr_rep_too_big_to_cache;
3998 else
3999 op->status = nfserr_rep_too_big;
4000 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05004001 if (op->status == nfserr_resource ||
4002 op->status == nfserr_rep_too_big ||
4003 op->status == nfserr_rep_too_big_to_cache) {
4004 /*
4005 * The operation may have already been encoded or
4006 * partially encoded. No op returns anything additional
4007 * in the case of one of these three errors, so we can
4008 * just truncate back to after the status. But it's a
4009 * bug if we had to do this on a non-idempotent op:
4010 */
4011 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004012 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05004013 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004014 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004015 int len = xdr->buf->len - post_err_offset;
4016
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004017 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004018 so->so_replay.rp_buflen = len;
4019 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
4020 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004021 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004022status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004023 /* Note that op->status is already in network byte order: */
4024 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
4027/*
4028 * Encode the reply stored in the stateowner reply cache
4029 *
4030 * XDR note: do not encode rp->rp_buflen: the buffer contains the
4031 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 */
4033void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004034nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004036 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 struct nfs4_replay *rp = op->replay;
4038
4039 BUG_ON(!rp);
4040
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004041 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
4042 if (!p) {
4043 WARN_ON_ONCE(1);
4044 return;
4045 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004046 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004049 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052int
Al Viro2ebbc012006-10-19 23:28:58 -07004053nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
4055 return xdr_ressize_check(rqstp, p);
4056}
4057
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04004058int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04004060 struct svc_rqst *rqstp = rq;
4061 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (args->ops != args->iops) {
4064 kfree(args->ops);
4065 args->ops = args->iops;
4066 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08004067 kfree(args->tmpp);
4068 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04004070 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 kfree(tb);
4073 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04004074 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
4077int
Al Viro2ebbc012006-10-19 23:28:58 -07004078nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
Jeff Laytone874f9f2014-03-10 11:34:55 -04004080 if (rqstp->rq_arg.head[0].iov_len % 4) {
4081 /* client is nuts */
4082 dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
4083 __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
4084 return 0;
4085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 args->p = p;
4087 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
4088 args->pagelist = rqstp->rq_arg.pages;
4089 args->pagelen = rqstp->rq_arg.page_len;
4090 args->tmpp = NULL;
4091 args->to_free = NULL;
4092 args->ops = args->iops;
4093 args->rqstp = rqstp;
4094
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04004095 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
4098int
Al Viro2ebbc012006-10-19 23:28:58 -07004099nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100{
4101 /*
4102 * All that remains is to write the tag and operation count...
4103 */
J. Bruce Fields6ac90392014-02-26 17:39:35 -05004104 struct xdr_buf *buf = resp->xdr.buf;
4105
4106 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
4107 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05004108
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004109 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 p = resp->tagp;
4112 *p++ = htonl(resp->taglen);
4113 memcpy(p, resp->tag, resp->taglen);
4114 p += XDR_QUADLEN(resp->taglen);
4115 *p++ = htonl(resp->opcnt);
4116
Trond Myklebustb6076642014-06-30 11:48:35 -04004117 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return 1;
4119}
4120
4121/*
4122 * Local variables:
4123 * c-basic-offset: 8
4124 * End:
4125 */