blob: dd72cdf3662e59caa623b4466aace61fa8cb84f9 [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
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +010036#include <linux/fs_struct.h>
Christoph Hellwig96bcad52015-06-18 16:45:01 +020037#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020040#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030041#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040042#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070043#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050045#include "idmap.h"
46#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020047#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050048#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040049#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050050#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030051#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020052#include "pnfs.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050053
David Quigley18032ca2013-05-02 13:19:10 -040054#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
55#include <linux/security.h>
56#endif
57
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFSDDBG_FACILITY NFSDDBG_XDR
60
J. Bruce Fields5cf23db2017-01-11 20:40:36 -050061const u32 nfsd_suppattrs[3][3] = {
J. Bruce Fields916d2d82016-10-18 14:18:40 -040062 {NFSD4_SUPPORTED_ATTRS_WORD0,
63 NFSD4_SUPPORTED_ATTRS_WORD1,
64 NFSD4_SUPPORTED_ATTRS_WORD2},
65
66 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
67 NFSD4_1_SUPPORTED_ATTRS_WORD1,
68 NFSD4_1_SUPPORTED_ATTRS_WORD2},
69
70 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
71 NFSD4_1_SUPPORTED_ATTRS_WORD1,
72 NFSD4_2_SUPPORTED_ATTRS_WORD2},
73};
74
J.Bruce Fields42ca0992006-10-04 02:16:20 -070075/*
76 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
77 * directory in order to indicate to the client that a filesystem boundary is present
78 * We use a fixed fsid for a referral
79 */
80#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
81#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
82
Al Virob37ad282006-10-19 23:28:59 -070083static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050084check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 int i;
87
88 if (len == 0)
89 return nfserr_inval;
90 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050091 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 for (i = 0; i < len; i++)
93 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050094 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070099 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -0700100 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define DECODE_TAIL \
102 status = 0; \
103out: \
104 return status; \
105xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400106 dprintk("NFSD: xdr error (%s:%d)\n", \
107 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 status = nfserr_bad_xdr; \
109 goto out
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define READMEM(x,nbytes) do { \
112 x = (char *)p; \
113 p += XDR_QUADLEN(nbytes); \
114} while (0)
115#define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
118 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto xdr_error; \
122 } \
123 p += XDR_QUADLEN(nbytes); \
124} while (0)
125#define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
128} while (0)
129
130/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131#define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
133 p = argp->p; \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto xdr_error; \
139 } \
140} while (0)
141
J. Bruce Fields590b7432013-06-21 17:06:29 -0400142static void next_decode_page(struct nfsd4_compoundargs *argp)
143{
J. Bruce Fields590b7432013-06-21 17:06:29 -0400144 argp->p = page_address(argp->pagelist[0]);
J. Bruce Fields365da4a2013-11-19 17:32:43 -0500145 argp->pagelist++;
J. Bruce Fields590b7432013-06-21 17:06:29 -0400146 if (argp->pagelen < PAGE_SIZE) {
Chuck Leverfc788f62017-08-18 11:12:19 -0400147 argp->end = argp->p + XDR_QUADLEN(argp->pagelen);
J. Bruce Fields590b7432013-06-21 17:06:29 -0400148 argp->pagelen = 0;
149 } else {
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
152 }
153}
154
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500155static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
159 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500160 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700161 __be32 *p;
Chuck Levereae03e22017-08-18 11:12:27 -0400162
163 if (argp->pagelen == 0) {
164 struct kvec *vec = &argp->rqstp->rq_arg.tail[0];
165
166 if (!argp->tail) {
167 argp->tail = true;
168 avail = vec->iov_len;
169 argp->p = vec->iov_base;
170 argp->end = vec->iov_base + avail;
171 }
172
173 if (avail < nbytes)
174 return NULL;
175
176 p = argp->p;
177 argp->p += XDR_QUADLEN(nbytes);
178 return p;
179 }
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (avail + argp->pagelen < nbytes)
182 return NULL;
183 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
184 return NULL;
185 /* ok, we can do it with the current plus the next page */
186 if (nbytes <= sizeof(argp->tmp))
187 p = argp->tmp;
188 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800189 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
191 if (!p)
192 return NULL;
193
194 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500195 /*
196 * The following memcpy is safe because read_buf is always
197 * called with nbytes > avail, and the two cases above both
198 * guarantee p points to at least nbytes bytes.
199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 memcpy(p, argp->p, avail);
J. Bruce Fields590b7432013-06-21 17:06:29 -0400201 next_decode_page(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
203 argp->p += XDR_QUADLEN(nbytes - avail);
204 return p;
205}
206
Andy Adamson60adfc52009-04-03 08:28:50 +0300207static int zero_clientid(clientid_t *clid)
208{
209 return (clid->cl_boot == 0) && (clid->cl_id == 0);
210}
211
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800212/**
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400213 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
J. Bruce Fieldsce043ac2014-06-24 16:51:12 -0400214 * @argp: NFSv4 compound argument structure
215 * @p: pointer to be freed (with kfree())
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800216 *
217 * Marks @p to be freed when processing the compound operation
218 * described in @argp finishes.
219 */
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400220static void *
221svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400223 struct svcxdr_tmpbuf *tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400225 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!tb)
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400227 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 tb->next = argp->to_free;
229 argp->to_free = tb;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400230 return tb->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400233/*
234 * For xdr strings that need to be passed to other kernel api's
235 * as null-terminated strings.
236 *
237 * Note null-terminating in place usually isn't safe since the
238 * buffer might end on a page boundary.
239 */
240static char *
241svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
242{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400243 char *p = svcxdr_tmpalloc(argp, len + 1);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400244
245 if (!p)
246 return NULL;
247 memcpy(p, buf, len);
248 p[len] = '\0';
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400249 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800252/**
253 * savemem - duplicate a chunk of memory for later processing
254 * @argp: NFSv4 compound argument structure to be freed with
255 * @p: pointer to be duplicated
256 * @nbytes: length to be duplicated
257 *
258 * Returns a pointer to a copy of @nbytes bytes of memory at @p
259 * that are preserved until processing of the NFSv4 compound
260 * operation described by @argp finishes.
261 */
Al Viro2ebbc012006-10-19 23:28:58 -0700262static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400264 void *ret;
265
266 ret = svcxdr_tmpalloc(argp, nbytes);
267 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return NULL;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400269 memcpy(ret, p, nbytes);
270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100273/*
274 * We require the high 32 bits of 'seconds' to be 0, and
275 * we ignore all 32 bits of 'nseconds'.
276 */
277static __be32
278nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec *tv)
279{
280 DECODE_HEAD;
281 u64 sec;
282
283 READ_BUF(12);
284 p = xdr_decode_hyper(p, &sec);
285 tv->tv_sec = sec;
286 tv->tv_nsec = be32_to_cpup(p++);
287 if (tv->tv_nsec >= (u32)1000000000)
288 return nfserr_inval;
289
290 DECODE_TAIL;
291}
292
Al Virob37ad282006-10-19 23:28:59 -0700293static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
295{
296 u32 bmlen;
297 DECODE_HEAD;
298
299 bmval[0] = 0;
300 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300301 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400304 bmlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (bmlen > 1000)
306 goto xdr_error;
307
308 READ_BUF(bmlen << 2);
309 if (bmlen > 0)
J. Bruce Fields06553992014-06-02 12:32:51 -0400310 bmval[0] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (bmlen > 1)
J. Bruce Fields06553992014-06-02 12:32:51 -0400312 bmval[1] = be32_to_cpup(p++);
Andy Adamson7e705702009-04-03 08:29:11 +0300313 if (bmlen > 2)
J. Bruce Fields06553992014-06-02 12:32:51 -0400314 bmval[2] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 DECODE_TAIL;
317}
318
Al Virob37ad282006-10-19 23:28:59 -0700319static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800320nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400321 struct iattr *iattr, struct nfs4_acl **acl,
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100322 struct xdr_netobj *label, int *umask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int expected_len, len = 0;
325 u32 dummy32;
326 char *buf;
327
328 DECODE_HEAD;
329 iattr->ia_valid = 0;
330 if ((status = nfsd4_decode_bitmap(argp, bmval)))
331 return status;
332
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400333 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
334 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
335 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
336 if (nfsd_attrs_supported(argp->minorversion, bmval))
337 return nfserr_inval;
338 return nfserr_attrnotsupp;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400342 expected_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (bmval[0] & FATTR4_WORD0_SIZE) {
345 READ_BUF(8);
346 len += 8;
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400347 p = xdr_decode_hyper(p, &iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 iattr->ia_valid |= ATTR_SIZE;
349 }
350 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields64a817c2013-03-26 14:11:13 -0400351 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800352 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 READ_BUF(4); len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400355 nace = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800357 if (nace > NFS4_ACL_MAX)
J. Bruce Fields798df332014-01-29 16:39:05 -0500358 return nfserr_fbig;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800359
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400360 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
Kinglong Meeeba1c992013-12-09 18:23:46 +0800361 if (*acl == NULL)
362 return nfserr_jukebox;
363
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800364 (*acl)->naces = nace;
365 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 READ_BUF(16); len += 16;
J. Bruce Fields06553992014-06-02 12:32:51 -0400367 ace->type = be32_to_cpup(p++);
368 ace->flag = be32_to_cpup(p++);
369 ace->access_mask = be32_to_cpup(p++);
370 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 READ_BUF(dummy32);
372 len += XDR_QUADLEN(dummy32) << 2;
373 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800374 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500375 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800376 if (ace->whotype != NFS4_ACL_WHO_NAMED)
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800377 ;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800378 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500379 status = nfsd_map_name_to_gid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800380 buf, dummy32, &ace->who_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500382 status = nfsd_map_name_to_uid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800383 buf, dummy32, &ace->who_uid);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500384 if (status)
385 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 } else
388 *acl = NULL;
389 if (bmval[1] & FATTR4_WORD1_MODE) {
390 READ_BUF(4);
391 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400392 iattr->ia_mode = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
394 iattr->ia_valid |= ATTR_MODE;
395 }
396 if (bmval[1] & FATTR4_WORD1_OWNER) {
397 READ_BUF(4);
398 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400399 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 READ_BUF(dummy32);
401 len += (XDR_QUADLEN(dummy32) << 2);
402 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100403 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
404 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 iattr->ia_valid |= ATTR_UID;
406 }
407 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
408 READ_BUF(4);
409 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400410 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 READ_BUF(dummy32);
412 len += (XDR_QUADLEN(dummy32) << 2);
413 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100414 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
415 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 iattr->ia_valid |= ATTR_GID;
417 }
418 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
419 READ_BUF(4);
420 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400421 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 switch (dummy32) {
423 case NFS4_SET_TO_CLIENT_TIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 len += 12;
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100425 status = nfsd4_decode_time(argp, &iattr->ia_atime);
426 if (status)
427 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
429 break;
430 case NFS4_SET_TO_SERVER_TIME:
431 iattr->ia_valid |= ATTR_ATIME;
432 break;
433 default:
434 goto xdr_error;
435 }
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
438 READ_BUF(4);
439 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400440 dummy32 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 switch (dummy32) {
442 case NFS4_SET_TO_CLIENT_TIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 len += 12;
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100444 status = nfsd4_decode_time(argp, &iattr->ia_mtime);
445 if (status)
446 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
448 break;
449 case NFS4_SET_TO_SERVER_TIME:
450 iattr->ia_valid |= ATTR_MTIME;
451 break;
452 default:
453 goto xdr_error;
454 }
455 }
David Quigley18032ca2013-05-02 13:19:10 -0400456
457 label->len = 0;
Arnd Bergmann2285ae72018-01-22 22:09:12 +0100458 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
459 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
David Quigley18032ca2013-05-02 13:19:10 -0400460 READ_BUF(4);
461 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400462 dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
David Quigley18032ca2013-05-02 13:19:10 -0400463 READ_BUF(4);
464 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400465 dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
David Quigley18032ca2013-05-02 13:19:10 -0400466 READ_BUF(4);
467 len += 4;
J. Bruce Fields06553992014-06-02 12:32:51 -0400468 dummy32 = be32_to_cpup(p++);
David Quigley18032ca2013-05-02 13:19:10 -0400469 READ_BUF(dummy32);
Kinglong Mee1ec8c0c2015-03-28 23:46:09 +0800470 if (dummy32 > NFS4_MAXLABELLEN)
David Quigley18032ca2013-05-02 13:19:10 -0400471 return nfserr_badlabel;
472 len += (XDR_QUADLEN(dummy32) << 2);
473 READMEM(buf, dummy32);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400474 label->len = dummy32;
475 label->data = svcxdr_dupstr(argp, buf, dummy32);
David Quigley18032ca2013-05-02 13:19:10 -0400476 if (!label->data)
477 return nfserr_jukebox;
David Quigley18032ca2013-05-02 13:19:10 -0400478 }
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100479 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
480 if (!umask)
481 goto xdr_error;
482 READ_BUF(8);
483 len += 8;
484 dummy32 = be32_to_cpup(p++);
485 iattr->ia_mode = dummy32 & (S_IFMT | S_IALLUGO);
486 dummy32 = be32_to_cpup(p++);
487 *umask = dummy32 & S_IRWXUGO;
488 iattr->ia_valid |= ATTR_MODE;
489 }
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400490 if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto xdr_error;
492
493 DECODE_TAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Al Virob37ad282006-10-19 23:28:59 -0700496static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300497nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
498{
499 DECODE_HEAD;
500
501 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400502 sid->si_generation = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300503 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
504
505 DECODE_TAIL;
506}
507
508static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
510{
511 DECODE_HEAD;
512
513 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400514 access->ac_req_access = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 DECODE_TAIL;
517}
518
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400519static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
520{
521 DECODE_HEAD;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500522 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400523 char *machine_name;
524 int i;
525 int nr_secflavs;
526
527 /* callback_sec_params4 */
528 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400529 nr_secflavs = be32_to_cpup(p++);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400530 if (nr_secflavs)
531 cbs->flavor = (u32)(-1);
532 else
533 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
534 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400535 for (i = 0; i < nr_secflavs; ++i) {
536 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400537 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400538 switch (dummy) {
539 case RPC_AUTH_NULL:
540 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500541 if (cbs->flavor == (u32)(-1))
542 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400543 break;
544 case RPC_AUTH_UNIX:
545 READ_BUF(8);
546 /* stamp */
J. Bruce Fields06553992014-06-02 12:32:51 -0400547 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400548
549 /* machine name */
J. Bruce Fields06553992014-06-02 12:32:51 -0400550 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400551 READ_BUF(dummy);
552 SAVEMEM(machine_name, dummy);
553
554 /* uid, gid */
555 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400556 uid = be32_to_cpup(p++);
557 gid = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400558
559 /* more gids */
560 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400561 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400562 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500563 if (cbs->flavor == (u32)(-1)) {
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800564 kuid_t kuid = make_kuid(&init_user_ns, uid);
565 kgid_t kgid = make_kgid(&init_user_ns, gid);
566 if (uid_valid(kuid) && gid_valid(kgid)) {
567 cbs->uid = kuid;
568 cbs->gid = kgid;
569 cbs->flavor = RPC_AUTH_UNIX;
570 } else {
571 dprintk("RPC_AUTH_UNIX with invalid"
572 "uid or gid ignoring!\n");
573 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500574 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400575 break;
576 case RPC_AUTH_GSS:
577 dprintk("RPC_AUTH_GSS callback secflavor "
578 "not supported!\n");
579 READ_BUF(8);
580 /* gcbp_service */
J. Bruce Fields06553992014-06-02 12:32:51 -0400581 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400582 /* gcbp_handle_from_server */
J. Bruce Fields06553992014-06-02 12:32:51 -0400583 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400584 READ_BUF(dummy);
585 p += XDR_QUADLEN(dummy);
586 /* gcbp_handle_from_client */
587 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400588 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400589 READ_BUF(dummy);
590 break;
591 default:
592 dprintk("Illegal callback secflavor\n");
593 return nfserr_inval;
594 }
595 }
596 DECODE_TAIL;
597}
598
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400599static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
600{
601 DECODE_HEAD;
602
603 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400604 bc->bc_cb_program = be32_to_cpup(p++);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400605 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
606
607 DECODE_TAIL;
608}
609
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400610static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
611{
612 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400613
614 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
615 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -0400616 bcts->dir = be32_to_cpup(p++);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400617 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
618 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400619 DECODE_TAIL;
620}
621
Al Virob37ad282006-10-19 23:28:59 -0700622static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
624{
625 DECODE_HEAD;
626
Benny Halevye31a1b62008-08-12 20:46:18 +0300627 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400628 close->cl_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300629 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 DECODE_TAIL;
632}
633
634
Al Virob37ad282006-10-19 23:28:59 -0700635static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
637{
638 DECODE_HEAD;
639
640 READ_BUF(12);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400641 p = xdr_decode_hyper(p, &commit->co_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -0400642 commit->co_count = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 DECODE_TAIL;
645}
646
Al Virob37ad282006-10-19 23:28:59 -0700647static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
649{
650 DECODE_HEAD;
651
652 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400653 create->cr_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 switch (create->cr_type) {
655 case NF4LNK:
656 READ_BUF(4);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400657 create->cr_datalen = be32_to_cpup(p++);
658 READ_BUF(create->cr_datalen);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400659 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400660 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400661 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 case NF4BLK:
664 case NF4CHR:
665 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400666 create->cr_specdata1 = be32_to_cpup(p++);
667 create->cr_specdata2 = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669 case NF4SOCK:
670 case NF4FIFO:
671 case NF4DIR:
672 default:
673 break;
674 }
675
676 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400677 create->cr_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 READ_BUF(create->cr_namelen);
679 SAVEMEM(create->cr_name, create->cr_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500680 if ((status = check_filename(create->cr_name, create->cr_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return status;
682
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800683 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100684 &create->cr_acl, &create->cr_label,
685 &current->fs->umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300686 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto out;
688
689 DECODE_TAIL;
690}
691
Al Virob37ad282006-10-19 23:28:59 -0700692static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
694{
Benny Halevye31a1b62008-08-12 20:46:18 +0300695 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Al Virob37ad282006-10-19 23:28:59 -0700698static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
700{
701 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
702}
703
Al Virob37ad282006-10-19 23:28:59 -0700704static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
706{
707 DECODE_HEAD;
708
709 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400710 link->li_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 READ_BUF(link->li_namelen);
712 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500713 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return status;
715
716 DECODE_TAIL;
717}
718
Al Virob37ad282006-10-19 23:28:59 -0700719static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
721{
722 DECODE_HEAD;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
726 */
727 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -0400728 lock->lk_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
730 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400731 lock->lk_reclaim = be32_to_cpup(p++);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400732 p = xdr_decode_hyper(p, &lock->lk_offset);
733 p = xdr_decode_hyper(p, &lock->lk_length);
J. Bruce Fields06553992014-06-02 12:32:51 -0400734 lock->lk_is_new = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300737 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400738 lock->lk_new_open_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300739 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
740 if (status)
741 return status;
742 READ_BUF(8 + sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400743 lock->lk_new_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400745 lock->lk_new_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 READ_BUF(lock->lk_new_owner.len);
747 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
748 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300749 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
750 if (status)
751 return status;
752 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400753 lock->lk_old_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 DECODE_TAIL;
757}
758
Al Virob37ad282006-10-19 23:28:59 -0700759static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
761{
762 DECODE_HEAD;
763
764 READ_BUF(32);
J. Bruce Fields06553992014-06-02 12:32:51 -0400765 lockt->lt_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
767 goto xdr_error;
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400768 p = xdr_decode_hyper(p, &lockt->lt_offset);
769 p = xdr_decode_hyper(p, &lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 COPYMEM(&lockt->lt_clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400771 lockt->lt_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 READ_BUF(lockt->lt_owner.len);
773 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
774
775 DECODE_TAIL;
776}
777
Al Virob37ad282006-10-19 23:28:59 -0700778static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
780{
781 DECODE_HEAD;
782
Benny Halevye31a1b62008-08-12 20:46:18 +0300783 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400784 locku->lu_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
786 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400787 locku->lu_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300788 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
789 if (status)
790 return status;
791 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400792 p = xdr_decode_hyper(p, &locku->lu_offset);
793 p = xdr_decode_hyper(p, &locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 DECODE_TAIL;
796}
797
Al Virob37ad282006-10-19 23:28:59 -0700798static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
800{
801 DECODE_HEAD;
802
803 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400804 lookup->lo_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 READ_BUF(lookup->lo_len);
806 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500807 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return status;
809
810 DECODE_TAIL;
811}
812
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200813static __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 -0400814{
815 __be32 *p;
816 u32 w;
817
818 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400819 w = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200820 *share_access = w & NFS4_SHARE_ACCESS_MASK;
821 *deleg_want = w & NFS4_SHARE_WANT_MASK;
822 if (deleg_when)
823 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
824
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400825 switch (w & NFS4_SHARE_ACCESS_MASK) {
826 case NFS4_SHARE_ACCESS_READ:
827 case NFS4_SHARE_ACCESS_WRITE:
828 case NFS4_SHARE_ACCESS_BOTH:
829 break;
830 default:
831 return nfserr_bad_xdr;
832 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200833 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400834 if (!w)
835 return nfs_ok;
836 if (!argp->minorversion)
837 return nfserr_bad_xdr;
838 switch (w & NFS4_SHARE_WANT_MASK) {
839 case NFS4_SHARE_WANT_NO_PREFERENCE:
840 case NFS4_SHARE_WANT_READ_DELEG:
841 case NFS4_SHARE_WANT_WRITE_DELEG:
842 case NFS4_SHARE_WANT_ANY_DELEG:
843 case NFS4_SHARE_WANT_NO_DELEG:
844 case NFS4_SHARE_WANT_CANCEL:
845 break;
846 default:
847 return nfserr_bad_xdr;
848 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700849 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400850 if (!w)
851 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200852
853 if (!deleg_when) /* open_downgrade */
854 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400855 switch (w) {
856 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
857 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700858 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
859 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400860 return nfs_ok;
861 }
862xdr_error:
863 return nfserr_bad_xdr;
864}
865
866static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
867{
868 __be32 *p;
869
870 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400871 *x = be32_to_cpup(p++);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400872 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300873 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400874 return nfserr_bad_xdr;
875 return nfs_ok;
876xdr_error:
877 return nfserr_bad_xdr;
878}
879
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400880static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
881{
882 __be32 *p;
883
884 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400885 o->len = be32_to_cpup(p++);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400886
887 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
888 return nfserr_bad_xdr;
889
890 READ_BUF(o->len);
891 SAVEMEM(o->data, o->len);
892 return nfs_ok;
893xdr_error:
894 return nfserr_bad_xdr;
895}
896
Al Virob37ad282006-10-19 23:28:59 -0700897static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
899{
900 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200901 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 memset(open->op_bmval, 0, sizeof(open->op_bmval));
904 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400905 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800907 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400909 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400910 open->op_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200911 /* decode, yet ignore deleg_when until supported */
912 status = nfsd4_decode_share_access(argp, &open->op_share_access,
913 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400914 if (status)
915 goto xdr_error;
916 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
917 if (status)
918 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400919 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400921 status = nfsd4_decode_opaque(argp, &open->op_owner);
922 if (status)
923 goto xdr_error;
924 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400925 open->op_create = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 switch (open->op_create) {
927 case NFS4_OPEN_NOCREATE:
928 break;
929 case NFS4_OPEN_CREATE:
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100930 current->fs->umask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400932 open->op_createmode = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 switch (open->op_createmode) {
934 case NFS4_CREATE_UNCHECKED:
935 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300936 status = nfsd4_decode_fattr(argp, open->op_bmval,
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100937 &open->op_iattr, &open->op_acl, &open->op_label,
938 &current->fs->umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300939 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto out;
941 break;
942 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500943 READ_BUF(NFS4_VERIFIER_SIZE);
944 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300946 case NFS4_CREATE_EXCLUSIVE4_1:
947 if (argp->minorversion < 1)
948 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500949 READ_BUF(NFS4_VERIFIER_SIZE);
950 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300951 status = nfsd4_decode_fattr(argp, open->op_bmval,
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100952 &open->op_iattr, &open->op_acl, &open->op_label,
953 &current->fs->umask);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300954 if (status)
955 goto out;
956 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 default:
958 goto xdr_error;
959 }
960 break;
961 default:
962 goto xdr_error;
963 }
964
965 /* open_claim */
966 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400967 open->op_claim_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 switch (open->op_claim_type) {
969 case NFS4_OPEN_CLAIM_NULL:
970 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
971 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400972 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 READ_BUF(open->op_fname.len);
974 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500975 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return status;
977 break;
978 case NFS4_OPEN_CLAIM_PREVIOUS:
979 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400980 open->op_delegate_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300983 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
984 if (status)
985 return status;
986 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400987 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 READ_BUF(open->op_fname.len);
989 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500990 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return status;
992 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400993 case NFS4_OPEN_CLAIM_FH:
994 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
995 if (argp->minorversion < 1)
996 goto xdr_error;
997 /* void */
998 break;
999 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1000 if (argp->minorversion < 1)
1001 goto xdr_error;
1002 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1003 if (status)
1004 return status;
1005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 default:
1007 goto xdr_error;
1008 }
1009
1010 DECODE_TAIL;
1011}
1012
Al Virob37ad282006-10-19 23:28:59 -07001013static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1015{
1016 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001017
1018 if (argp->minorversion >= 1)
1019 return nfserr_notsupp;
1020
Benny Halevye31a1b62008-08-12 20:46:18 +03001021 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
1022 if (status)
1023 return status;
1024 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001025 open_conf->oc_seqid = be32_to_cpup(p++);
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 DECODE_TAIL;
1028}
1029
Al Virob37ad282006-10-19 23:28:59 -07001030static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1032{
1033 DECODE_HEAD;
1034
Benny Halevye31a1b62008-08-12 20:46:18 +03001035 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
1036 if (status)
1037 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001038 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001039 open_down->od_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001040 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1041 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001042 if (status)
1043 return status;
1044 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1045 if (status)
1046 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 DECODE_TAIL;
1048}
1049
Al Virob37ad282006-10-19 23:28:59 -07001050static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1052{
1053 DECODE_HEAD;
1054
1055 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001056 putfh->pf_fhlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (putfh->pf_fhlen > NFS4_FHSIZE)
1058 goto xdr_error;
1059 READ_BUF(putfh->pf_fhlen);
1060 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1061
1062 DECODE_TAIL;
1063}
1064
Al Virob37ad282006-10-19 23:28:59 -07001065static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001066nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1067{
1068 if (argp->minorversion == 0)
1069 return nfs_ok;
1070 return nfserr_notsupp;
1071}
1072
1073static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1075{
1076 DECODE_HEAD;
1077
Benny Halevye31a1b62008-08-12 20:46:18 +03001078 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1079 if (status)
1080 return status;
1081 READ_BUF(12);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001082 p = xdr_decode_hyper(p, &read->rd_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001083 read->rd_length = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 DECODE_TAIL;
1086}
1087
Al Virob37ad282006-10-19 23:28:59 -07001088static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1090{
1091 DECODE_HEAD;
1092
1093 READ_BUF(24);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001094 p = xdr_decode_hyper(p, &readdir->rd_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
J. Bruce Fields06553992014-06-02 12:32:51 -04001096 readdir->rd_dircount = be32_to_cpup(p++);
1097 readdir->rd_maxcount = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1099 goto out;
1100
1101 DECODE_TAIL;
1102}
1103
Al Virob37ad282006-10-19 23:28:59 -07001104static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1106{
1107 DECODE_HEAD;
1108
1109 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001110 remove->rm_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 READ_BUF(remove->rm_namelen);
1112 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001113 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return status;
1115
1116 DECODE_TAIL;
1117}
1118
Al Virob37ad282006-10-19 23:28:59 -07001119static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1121{
1122 DECODE_HEAD;
1123
1124 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001125 rename->rn_snamelen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001126 READ_BUF(rename->rn_snamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001128 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001129 rename->rn_tnamelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 READ_BUF(rename->rn_tnamelen);
1131 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001132 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001134 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return status;
1136
1137 DECODE_TAIL;
1138}
1139
Al Virob37ad282006-10-19 23:28:59 -07001140static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1142{
1143 DECODE_HEAD;
1144
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001145 if (argp->minorversion >= 1)
1146 return nfserr_notsupp;
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 READ_BUF(sizeof(clientid_t));
1149 COPYMEM(clientid, sizeof(clientid_t));
1150
1151 DECODE_TAIL;
1152}
1153
Al Virob37ad282006-10-19 23:28:59 -07001154static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001155nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1156 struct nfsd4_secinfo *secinfo)
1157{
1158 DECODE_HEAD;
1159
1160 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001161 secinfo->si_namelen = be32_to_cpup(p++);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001162 READ_BUF(secinfo->si_namelen);
1163 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001164 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001165 if (status)
1166 return status;
1167 DECODE_TAIL;
1168}
1169
1170static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001171nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1172 struct nfsd4_secinfo_no_name *sin)
1173{
1174 DECODE_HEAD;
1175
1176 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001177 sin->sin_style = be32_to_cpup(p++);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001178 DECODE_TAIL;
1179}
1180
1181static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1183{
Benny Halevye31a1b62008-08-12 20:46:18 +03001184 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Benny Halevye31a1b62008-08-12 20:46:18 +03001186 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1187 if (status)
1188 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001189 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +01001190 &setattr->sa_acl, &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Al Virob37ad282006-10-19 23:28:59 -07001193static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1195{
1196 DECODE_HEAD;
1197
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001198 if (argp->minorversion >= 1)
1199 return nfserr_notsupp;
1200
Chuck Leverab4684d2012-03-02 17:13:50 -05001201 READ_BUF(NFS4_VERIFIER_SIZE);
1202 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001204 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1205 if (status)
1206 return nfserr_bad_xdr;
1207 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001208 setclientid->se_callback_prog = be32_to_cpup(p++);
1209 setclientid->se_callback_netid_len = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001210 READ_BUF(setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001212 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001213 setclientid->se_callback_addr_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001215 READ_BUF(setclientid->se_callback_addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001217 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001218 setclientid->se_callback_ident = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 DECODE_TAIL;
1221}
1222
Al Virob37ad282006-10-19 23:28:59 -07001223static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1225{
1226 DECODE_HEAD;
1227
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001228 if (argp->minorversion >= 1)
1229 return nfserr_notsupp;
1230
Chuck Leverab4684d2012-03-02 17:13:50 -05001231 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001233 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 DECODE_TAIL;
1236}
1237
1238/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001239static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 DECODE_HEAD;
1243
1244 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1245 goto out;
1246
1247 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001248 * nfsd4_proc_verify */
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001251 verify->ve_attrlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 READ_BUF(verify->ve_attrlen);
1253 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1254
1255 DECODE_TAIL;
1256}
1257
Al Virob37ad282006-10-19 23:28:59 -07001258static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1260{
1261 int avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int len;
1263 DECODE_HEAD;
1264
Benny Halevye31a1b62008-08-12 20:46:18 +03001265 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1266 if (status)
1267 return status;
1268 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001269 p = xdr_decode_hyper(p, &write->wr_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001270 write->wr_stable_how = be32_to_cpup(p++);
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001271 if (write->wr_stable_how > NFS_FILE_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -04001273 write->wr_buflen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 /* Sorry .. no magic macros for this.. *
1276 * READ_BUF(write->wr_buflen);
1277 * SAVEMEM(write->wr_buf, write->wr_buflen);
1278 */
1279 avail = (char*)argp->end - (char*)argp->p;
1280 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001281 dprintk("NFSD: xdr error (%s:%d)\n",
1282 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto xdr_error;
1284 }
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001285 write->wr_head.iov_base = p;
1286 write->wr_head.iov_len = avail;
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001287 write->wr_pagelist = argp->pagelist;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001288
1289 len = XDR_QUADLEN(write->wr_buflen) << 2;
1290 if (len >= avail) {
1291 int pages;
1292
1293 len -= avail;
1294
1295 pages = len >> PAGE_SHIFT;
1296 argp->pagelist += pages;
1297 argp->pagelen -= pages * PAGE_SIZE;
1298 len -= pages * PAGE_SIZE;
1299
Chuck Leverfc788f62017-08-18 11:12:19 -04001300 next_decode_page(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001302 argp->p += XDR_QUADLEN(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 DECODE_TAIL;
1305}
1306
Al Virob37ad282006-10-19 23:28:59 -07001307static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1309{
1310 DECODE_HEAD;
1311
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001312 if (argp->minorversion >= 1)
1313 return nfserr_notsupp;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 READ_BUF(12);
1316 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001317 rlockowner->rl_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 READ_BUF(rlockowner->rl_owner.len);
1319 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1320
Andy Adamson60adfc52009-04-03 08:28:50 +03001321 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1322 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 DECODE_TAIL;
1324}
1325
Al Virob37ad282006-10-19 23:28:59 -07001326static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001327nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001328 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001329{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001330 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001331 DECODE_HEAD;
1332
1333 READ_BUF(NFS4_VERIFIER_SIZE);
1334 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1335
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001336 status = nfsd4_decode_opaque(argp, &exid->clname);
1337 if (status)
1338 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001339
1340 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001341 exid->flags = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001342
1343 /* Ignore state_protect4_a */
1344 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001345 exid->spa_how = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001346 switch (exid->spa_how) {
1347 case SP4_NONE:
1348 break;
1349 case SP4_MACH_CRED:
1350 /* spo_must_enforce */
Andrew Elbleed941642016-06-15 12:52:09 -04001351 status = nfsd4_decode_bitmap(argp,
1352 exid->spo_must_enforce);
1353 if (status)
1354 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001355 /* spo_must_allow */
Andrew Elbleed941642016-06-15 12:52:09 -04001356 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1357 if (status)
1358 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001359 break;
1360 case SP4_SSV:
1361 /* ssp_ops */
1362 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001363 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001364 READ_BUF(dummy * 4);
1365 p += dummy;
1366
1367 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001368 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001369 READ_BUF(dummy * 4);
1370 p += dummy;
1371
1372 /* ssp_hash_algs<> */
1373 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001374 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001375 while (tmp--) {
1376 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001377 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001378 READ_BUF(dummy);
1379 p += XDR_QUADLEN(dummy);
1380 }
Andy Adamson0733d212009-04-03 08:28:01 +03001381
1382 /* ssp_encr_algs<> */
1383 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001384 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001385 while (tmp--) {
1386 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001387 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001388 READ_BUF(dummy);
1389 p += XDR_QUADLEN(dummy);
1390 }
Andy Adamson0733d212009-04-03 08:28:01 +03001391
1392 /* ssp_window and ssp_num_gss_handles */
1393 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001394 dummy = be32_to_cpup(p++);
1395 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001396 break;
1397 default:
1398 goto xdr_error;
1399 }
1400
1401 /* Ignore Implementation ID */
1402 READ_BUF(4); /* nfs_impl_id4 array length */
J. Bruce Fields06553992014-06-02 12:32:51 -04001403 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001404
1405 if (dummy > 1)
1406 goto xdr_error;
1407
1408 if (dummy == 1) {
1409 /* nii_domain */
1410 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001411 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001412 READ_BUF(dummy);
1413 p += XDR_QUADLEN(dummy);
1414
1415 /* nii_name */
1416 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001417 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001418 READ_BUF(dummy);
1419 p += XDR_QUADLEN(dummy);
1420
1421 /* nii_date */
1422 READ_BUF(12);
1423 p += 3;
1424 }
1425 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001426}
1427
1428static __be32
1429nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1430 struct nfsd4_create_session *sess)
1431{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001432 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001434
1435 READ_BUF(16);
1436 COPYMEM(&sess->clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001437 sess->seqid = be32_to_cpup(p++);
1438 sess->flags = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001439
1440 /* Fore channel attrs */
1441 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -04001442 dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
1443 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1444 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1445 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1446 sess->fore_channel.maxops = be32_to_cpup(p++);
1447 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1448 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001449 if (sess->fore_channel.nr_rdma_attrs == 1) {
1450 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001451 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001452 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1453 dprintk("Too many fore channel attr bitmaps!\n");
1454 goto xdr_error;
1455 }
1456
1457 /* Back channel attrs */
1458 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -04001459 dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
1460 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1461 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1462 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1463 sess->back_channel.maxops = be32_to_cpup(p++);
1464 sess->back_channel.maxreqs = be32_to_cpup(p++);
1465 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001466 if (sess->back_channel.nr_rdma_attrs == 1) {
1467 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001468 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001469 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1470 dprintk("Too many back channel attr bitmaps!\n");
1471 goto xdr_error;
1472 }
1473
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001474 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001475 sess->callback_prog = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001476 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001477 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001478}
1479
1480static __be32
1481nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1482 struct nfsd4_destroy_session *destroy_session)
1483{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001484 DECODE_HEAD;
1485 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1486 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1487
1488 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001489}
1490
1491static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001492nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1493 struct nfsd4_free_stateid *free_stateid)
1494{
1495 DECODE_HEAD;
1496
1497 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001498 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001499 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1500
1501 DECODE_TAIL;
1502}
1503
1504static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001505nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1506 struct nfsd4_sequence *seq)
1507{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001508 DECODE_HEAD;
1509
1510 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1511 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -04001512 seq->seqid = be32_to_cpup(p++);
1513 seq->slotid = be32_to_cpup(p++);
1514 seq->maxslots = be32_to_cpup(p++);
1515 seq->cachethis = be32_to_cpup(p++);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001516
1517 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001518}
1519
Bryan Schumaker17456802011-07-13 10:50:48 -04001520static __be32
1521nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1522{
Bryan Schumaker17456802011-07-13 10:50:48 -04001523 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001524 __be32 *p, status;
1525 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001526
1527 READ_BUF(4);
1528 test_stateid->ts_num_ids = ntohl(*p++);
1529
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001530 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001531
1532 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001533 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001534 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001535 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001536 goto out;
1537 }
1538
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001539 INIT_LIST_HEAD(&stateid->ts_id_list);
1540 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1541
1542 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001543 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001544 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001545 }
1546
1547 status = 0;
1548out:
1549 return status;
1550xdr_error:
1551 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1552 status = nfserr_bad_xdr;
1553 goto out;
1554}
1555
Mi Jinlong345c2842011-10-20 17:51:39 +08001556static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1557{
1558 DECODE_HEAD;
1559
1560 READ_BUF(8);
1561 COPYMEM(&dc->clientid, 8);
1562
1563 DECODE_TAIL;
1564}
1565
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001566static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1567{
1568 DECODE_HEAD;
1569
1570 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001571 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001572
1573 DECODE_TAIL;
1574}
1575
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001576#ifdef CONFIG_NFSD_PNFS
1577static __be32
1578nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1579 struct nfsd4_getdeviceinfo *gdev)
1580{
1581 DECODE_HEAD;
1582 u32 num, i;
1583
1584 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1585 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1586 gdev->gd_layout_type = be32_to_cpup(p++);
1587 gdev->gd_maxcount = be32_to_cpup(p++);
1588 num = be32_to_cpup(p++);
1589 if (num) {
1590 READ_BUF(4 * num);
1591 gdev->gd_notify_types = be32_to_cpup(p++);
1592 for (i = 1; i < num; i++) {
1593 if (be32_to_cpup(p++)) {
1594 status = nfserr_inval;
1595 goto out;
1596 }
1597 }
1598 }
1599 DECODE_TAIL;
1600}
1601
1602static __be32
1603nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1604 struct nfsd4_layoutget *lgp)
1605{
1606 DECODE_HEAD;
1607
1608 READ_BUF(36);
1609 lgp->lg_signal = be32_to_cpup(p++);
1610 lgp->lg_layout_type = be32_to_cpup(p++);
1611 lgp->lg_seg.iomode = be32_to_cpup(p++);
1612 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1613 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1614 p = xdr_decode_hyper(p, &lgp->lg_minlength);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001615
1616 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1617 if (status)
1618 return status;
1619
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001620 READ_BUF(4);
1621 lgp->lg_maxcount = be32_to_cpup(p++);
1622
1623 DECODE_TAIL;
1624}
1625
1626static __be32
1627nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1628 struct nfsd4_layoutcommit *lcp)
1629{
1630 DECODE_HEAD;
1631 u32 timechange;
1632
1633 READ_BUF(20);
1634 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1635 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1636 lcp->lc_reclaim = be32_to_cpup(p++);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001637
1638 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1639 if (status)
1640 return status;
1641
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001642 READ_BUF(4);
1643 lcp->lc_newoffset = be32_to_cpup(p++);
1644 if (lcp->lc_newoffset) {
1645 READ_BUF(8);
1646 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1647 } else
1648 lcp->lc_last_wr = 0;
1649 READ_BUF(4);
1650 timechange = be32_to_cpup(p++);
1651 if (timechange) {
1652 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1653 if (status)
1654 return status;
1655 } else {
1656 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1657 }
1658 READ_BUF(8);
1659 lcp->lc_layout_type = be32_to_cpup(p++);
1660
1661 /*
1662 * Save the layout update in XDR format and let the layout driver deal
1663 * with it later.
1664 */
1665 lcp->lc_up_len = be32_to_cpup(p++);
1666 if (lcp->lc_up_len > 0) {
1667 READ_BUF(lcp->lc_up_len);
1668 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1669 }
1670
1671 DECODE_TAIL;
1672}
1673
1674static __be32
1675nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1676 struct nfsd4_layoutreturn *lrp)
1677{
1678 DECODE_HEAD;
1679
1680 READ_BUF(16);
1681 lrp->lr_reclaim = be32_to_cpup(p++);
1682 lrp->lr_layout_type = be32_to_cpup(p++);
1683 lrp->lr_seg.iomode = be32_to_cpup(p++);
1684 lrp->lr_return_type = be32_to_cpup(p++);
1685 if (lrp->lr_return_type == RETURN_FILE) {
1686 READ_BUF(16);
1687 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1688 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001689
1690 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1691 if (status)
1692 return status;
1693
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001694 READ_BUF(4);
1695 lrp->lrf_body_len = be32_to_cpup(p++);
1696 if (lrp->lrf_body_len > 0) {
1697 READ_BUF(lrp->lrf_body_len);
1698 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1699 }
1700 } else {
1701 lrp->lr_seg.offset = 0;
1702 lrp->lr_seg.length = NFS4_MAX_UINT64;
1703 }
1704
1705 DECODE_TAIL;
1706}
1707#endif /* CONFIG_NFSD_PNFS */
1708
Andy Adamson2db134e2009-04-03 08:27:55 +03001709static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001710nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1711 struct nfsd4_fallocate *fallocate)
1712{
1713 DECODE_HEAD;
1714
1715 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1716 if (status)
1717 return status;
1718
1719 READ_BUF(16);
1720 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1721 xdr_decode_hyper(p, &fallocate->falloc_length);
1722
1723 DECODE_TAIL;
1724}
1725
1726static __be32
Christoph Hellwigffa01602015-12-03 12:59:52 +01001727nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1728{
1729 DECODE_HEAD;
1730
1731 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1732 if (status)
1733 return status;
1734 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1735 if (status)
1736 return status;
1737
1738 READ_BUF(8 + 8 + 8);
1739 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1740 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1741 p = xdr_decode_hyper(p, &clone->cl_count);
1742 DECODE_TAIL;
1743}
1744
1745static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001746nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1747{
1748 DECODE_HEAD;
1749 unsigned int tmp;
1750
1751 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1752 if (status)
1753 return status;
1754 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1755 if (status)
1756 return status;
1757
1758 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1759 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1760 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1761 p = xdr_decode_hyper(p, &copy->cp_count);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05001762 p++; /* ca_consecutive: we always do consecutive copies */
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001763 copy->cp_synchronous = be32_to_cpup(p++);
1764 tmp = be32_to_cpup(p); /* Source server list not supported */
1765
1766 DECODE_TAIL;
1767}
1768
1769static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001770nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1771{
1772 DECODE_HEAD;
1773
1774 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1775 if (status)
1776 return status;
1777
1778 READ_BUF(8 + 4);
1779 p = xdr_decode_hyper(p, &seek->seek_offset);
1780 seek->seek_whence = be32_to_cpup(p);
1781
1782 DECODE_TAIL;
1783}
1784
1785static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001786nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1787{
1788 return nfs_ok;
1789}
1790
Benny Halevy3c375c62008-07-02 11:14:01 +03001791static __be32
1792nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1793{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001794 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001795}
1796
Benny Halevy347e0ad2008-07-02 11:13:41 +03001797typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1798
Chuck Leverc1df6092017-08-01 11:59:58 -04001799static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001800 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1801 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1802 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1803 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1804 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1805 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1806 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1807 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1808 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1809 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1810 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1811 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1812 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1813 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1814 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1815 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1816 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1817 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1818 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1819 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001820 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001821 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1822 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1823 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1824 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1825 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1826 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1827 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1828 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1829 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1830 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1831 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1832 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1833 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1834 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1835 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1836 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03001837
1838 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001839 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001840 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001841 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1842 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1843 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001844 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001845 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001846#ifdef CONFIG_NFSD_PNFS
1847 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
1848 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1849 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
1850 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
1851 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
1852#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07001853 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1854 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1855 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1856 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1857 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001858#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001859 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001860 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1861 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001862 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001863 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001864 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001865 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001866
1867 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05001868 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001869 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001870 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001871 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001872 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
1873 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
1874 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
1875 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
1876 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
1877 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
Anna Schumaker24bab492014-09-26 13:58:27 -04001878 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04001879 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01001880 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Andy Adamson2db134e2009-04-03 08:27:55 +03001881};
1882
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001883static inline bool
1884nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1885{
Anna Schumaker8217d142013-10-30 13:38:13 -04001886 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001887 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001888 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001889 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001890 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1891 return false;
1892 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001893 return false;
1894 return true;
1895}
Benny Halevyf2feb962008-07-02 11:14:22 +03001896
Benny Halevy347e0ad2008-07-02 11:13:41 +03001897static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1899{
1900 DECODE_HEAD;
1901 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05001902 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04001903 int auth_slack= argp->rqstp->rq_auth_slack;
1904 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001905 int readcount = 0;
1906 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int i;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001910 argp->taglen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001911 READ_BUF(argp->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001913 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001914 argp->minorversion = be32_to_cpup(p++);
1915 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04001916 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if (argp->taglen > NFSD4_MAX_TAGLEN)
1919 goto xdr_error;
J. Bruce Fields00781172017-11-15 12:30:27 -05001920 /*
1921 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
1922 * here, so we return success at the xdr level so that
1923 * nfsd4_proc can handle this is an NFS-level error.
1924 */
1925 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
1926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Tobias Klausere8c96f82006-03-24 03:15:34 -08001928 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04001929 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (!argp->ops) {
1931 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001932 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto xdr_error;
1934 }
1935 }
1936
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001937 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03001938 argp->opcnt = 0;
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 for (i = 0; i < argp->opcnt; i++) {
1941 op = &argp->ops[i];
1942 op->replay = NULL;
1943
J. Bruce Fields8a61b182012-11-16 22:28:38 -05001944 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001945 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001947 if (nfsd4_opnum_in_range(argp, op))
1948 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001949 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 op->opnum = OP_ILLEGAL;
1951 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04001953 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05001954 /*
1955 * We'll try to cache the result in the DRC if any one
1956 * op in the compound wants to be cached:
1957 */
1958 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05001959
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001960 if (op->opnum == OP_READ) {
1961 readcount++;
1962 readbytes += nfsd4_max_reply(argp->rqstp, op);
1963 } else
1964 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04001965 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08001966 * OP_LOCK and OP_LOCKT may return a conflicting lock.
1967 * (Special case because it will just skip encoding this
1968 * if it runs out of xdr buffer space, and it is the only
1969 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04001970 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08001971 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04001972 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04001973
1974 if (op->status) {
1975 argp->opcnt = i+1;
1976 break;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001979 /* Sessions make the DRC unnecessary: */
1980 if (argp->minorversion)
1981 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001982 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05001983 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04001985 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05001986 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 DECODE_TAIL;
1989}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
NeilBrownb8800922017-01-30 17:17:00 +11001991static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
1992 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001993{
NeilBrownb8800922017-01-30 17:17:00 +11001994 if (exp->ex_flags & NFSEXP_V4ROOT) {
1995 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
1996 *p++ = 0;
1997 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02001998 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001999 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002000 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2001 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002002 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002003 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002004}
2005
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002006static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002007{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002008 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002009 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002010 p = xdr_encode_hyper(p, c->before_change);
2011 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002012 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002013 *p++ = cpu_to_be32(c->before_ctime_sec);
2014 *p++ = cpu_to_be32(c->before_ctime_nsec);
2015 *p++ = cpu_to_be32(c->after_ctime_sec);
2016 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002017 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002018 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002021/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002022 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002023 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002024static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2025 char *components, char esc_enter,
2026 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002027{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002028 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002029 __be32 pathlen;
2030 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002031 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002032 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002033
2034 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002035
2036 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002037 p = xdr_reserve_space(xdr, 4);
2038 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002039 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002040 p++; /* We will fill this in with @count later */
2041
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002042 end = str = components;
2043 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002044 bool found_esc = false;
2045
2046 /* try to parse as esc_start, ..., esc_end, sep */
2047 if (*str == esc_enter) {
2048 for (; *end && (*end != esc_exit); end++)
2049 /* find esc_exit or end of string */;
2050 next = end + 1;
2051 if (*end && (!*next || *next == sep)) {
2052 str++;
2053 found_esc = true;
2054 }
2055 }
2056
2057 if (!found_esc)
2058 for (; *end && (*end != sep); end++)
2059 /* find sep or end of string */;
2060
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002061 strlen = end - str;
2062 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002063 p = xdr_reserve_space(xdr, strlen + 4);
2064 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002065 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002066 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002067 count++;
2068 }
2069 else
2070 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002071 if (found_esc)
2072 end = next;
2073
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002074 str = end;
2075 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002076 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002077 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002078 return 0;
2079}
2080
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002081/* Encode as an array of strings the string given with components
2082 * separated @sep.
2083 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002084static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2085 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002086{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002087 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002088}
2089
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002090/*
2091 * encode a location element of a fs_locations structure
2092 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002093static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2094 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002095{
Al Virob37ad282006-10-19 23:28:59 -07002096 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002097
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002098 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002099 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002100 if (status)
2101 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002102 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002103 if (status)
2104 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002105 return 0;
2106}
2107
2108/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002109 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002110 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002111static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2112 const struct path *root,
2113 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002114{
Al Viro301f0262013-09-01 16:06:39 -04002115 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002116 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002117 struct dentry **components = NULL;
2118 unsigned int ncomponents = 0;
2119 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002120
Trond Myklebusted748aa2011-09-12 19:37:06 -04002121 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002122
Trond Myklebusted748aa2011-09-12 19:37:06 -04002123 path_get(&cur);
2124 /* First walk the path up to the nfsd root, and store the
2125 * dentries/path components in an array.
2126 */
2127 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002128 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002129 break;
2130 if (cur.dentry == cur.mnt->mnt_root) {
2131 if (follow_up(&cur))
2132 continue;
2133 goto out_free;
2134 }
2135 if ((ncomponents & 15) == 0) {
2136 struct dentry **new;
2137 new = krealloc(components,
2138 sizeof(*new) * (ncomponents + 16),
2139 GFP_KERNEL);
2140 if (!new)
2141 goto out_free;
2142 components = new;
2143 }
2144 components[ncomponents++] = cur.dentry;
2145 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002146 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002147 err = nfserr_resource;
2148 p = xdr_reserve_space(xdr, 4);
2149 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002150 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002151 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002152
2153 while (ncomponents) {
2154 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002155 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002156
Al Viro301f0262013-09-01 16:06:39 -04002157 spin_lock(&dentry->d_lock);
2158 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002159 p = xdr_reserve_space(xdr, len + 4);
2160 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002161 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002162 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002163 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002164 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002165 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002166 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002167 dput(dentry);
2168 ncomponents--;
2169 }
2170
Trond Myklebusted748aa2011-09-12 19:37:06 -04002171 err = 0;
2172out_free:
2173 dprintk(")\n");
2174 while (ncomponents)
2175 dput(components[--ncomponents]);
2176 kfree(components);
2177 path_put(&cur);
2178 return err;
2179}
2180
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002181static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2182 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002183{
2184 struct svc_export *exp_ps;
2185 __be32 res;
2186
2187 exp_ps = rqst_find_fsidzero_export(rqstp);
2188 if (IS_ERR(exp_ps))
2189 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002190 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002191 exp_put(exp_ps);
2192 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002193}
2194
2195/*
2196 * encode a fs_locations structure
2197 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002198static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2199 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002200{
Al Virob37ad282006-10-19 23:28:59 -07002201 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002202 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002203 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002204 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002205
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002206 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002207 if (status)
2208 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002209 p = xdr_reserve_space(xdr, 4);
2210 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002211 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002212 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002213 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002214 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002215 if (status)
2216 return status;
2217 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002218 return 0;
2219}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002221static u32 nfs4_file_type(umode_t mode)
2222{
2223 switch (mode & S_IFMT) {
2224 case S_IFIFO: return NF4FIFO;
2225 case S_IFCHR: return NF4CHR;
2226 case S_IFDIR: return NF4DIR;
2227 case S_IFBLK: return NF4BLK;
2228 case S_IFLNK: return NF4LNK;
2229 case S_IFREG: return NF4REG;
2230 case S_IFSOCK: return NF4SOCK;
2231 default: return NF4BAD;
2232 };
2233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Al Virob37ad282006-10-19 23:28:59 -07002235static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002236nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2237 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
J. Bruce Fields35541162014-01-08 09:49:01 -05002239 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002240 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002241 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002242 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002243 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002244 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Kinglong Mee6896f152015-07-30 21:52:44 +08002247static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002248nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002249{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002250 __be32 *p;
2251 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002252
Jeff Layton8a4c3922016-07-10 15:55:58 -04002253 p = xdr_reserve_space(xdr, 4 + 4 * i);
2254 if (!p)
2255 return nfserr_resource;
2256
2257 *p++ = cpu_to_be32(i);
2258
2259 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2260 if (layout_types & (1 << i))
2261 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002262
2263 return 0;
2264}
2265
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002266#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2267 FATTR4_WORD0_RDATTR_ERROR)
2268#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002269#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002270
David Quigley18032ca2013-05-02 13:19:10 -04002271#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2272static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002273nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2274 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002275{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002276 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002277
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002278 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2279 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002280 return nfserr_resource;
2281
2282 /*
2283 * For now we use a 0 here to indicate the null translation; in
2284 * the future we may place a call to translation code here.
2285 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002286 *p++ = cpu_to_be32(0); /* lfs */
2287 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002288 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002289 return 0;
2290}
2291#else
2292static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002293nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2294 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002295{ return 0; }
2296#endif
2297
Kinglong Meec2227a32015-07-07 10:16:37 +08002298static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002299{
2300 /* As per referral draft: */
2301 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2302 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2303 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2304 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2305 *rdattr_err = NFSERR_MOVED;
2306 else
2307 return nfserr_moved;
2308 }
2309 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2310 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002311 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002312 return 0;
2313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002315
2316static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2317{
2318 struct path path = exp->ex_path;
2319 int err;
2320
2321 path_get(&path);
2322 while (follow_up(&path)) {
2323 if (path.dentry != path.mnt->mnt_root)
2324 break;
2325 }
David Howellsa528d352017-01-31 16:46:22 +00002326 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002327 path_put(&path);
2328 return err;
2329}
2330
Kinglong Mee75976de2015-07-30 21:55:30 +08002331static __be32
2332nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2333{
2334 __be32 *p;
2335
2336 if (bmval2) {
2337 p = xdr_reserve_space(xdr, 16);
2338 if (!p)
2339 goto out_resource;
2340 *p++ = cpu_to_be32(3);
2341 *p++ = cpu_to_be32(bmval0);
2342 *p++ = cpu_to_be32(bmval1);
2343 *p++ = cpu_to_be32(bmval2);
2344 } else if (bmval1) {
2345 p = xdr_reserve_space(xdr, 12);
2346 if (!p)
2347 goto out_resource;
2348 *p++ = cpu_to_be32(2);
2349 *p++ = cpu_to_be32(bmval0);
2350 *p++ = cpu_to_be32(bmval1);
2351 } else {
2352 p = xdr_reserve_space(xdr, 8);
2353 if (!p)
2354 goto out_resource;
2355 *p++ = cpu_to_be32(1);
2356 *p++ = cpu_to_be32(bmval0);
2357 }
2358
2359 return 0;
2360out_resource:
2361 return nfserr_resource;
2362}
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364/*
2365 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2366 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002368static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002369nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2370 struct svc_export *exp,
2371 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002372 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 u32 bmval0 = bmval[0];
2375 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002376 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002378 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002380 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002381 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002382 int attrlen_offset;
2383 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 u32 dummy;
2385 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002386 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002387 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002388 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct nfs4_acl *acl = NULL;
David Quigley18032ca2013-05-02 13:19:10 -04002390 void *context = NULL;
2391 int contextlen;
2392 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002393 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2394 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002395 struct path path = {
2396 .mnt = exp->ex_path.mnt,
2397 .dentry = dentry,
2398 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002399 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002402 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002404 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002405 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002406 if (status)
2407 goto out;
2408 }
2409
David Howellsa528d352017-01-31 16:46:22 +00002410 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002411 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002413 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2414 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2416 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002417 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002418 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto out_nfserr;
2420 }
2421 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002422 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2423 status = nfserr_jukebox;
2424 if (!tempfh)
2425 goto out;
2426 fh_init(tempfh, NFS4_FHSIZE);
2427 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (status)
2429 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002430 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002432 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002433 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002434 if (err == -EOPNOTSUPP)
2435 bmval0 &= ~FATTR4_WORD0_ACL;
2436 else if (err == -EINVAL) {
2437 status = nfserr_attrnotsupp;
2438 goto out;
2439 } else if (err != 0)
2440 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
David Quigley18032ca2013-05-02 13:19:10 -04002443#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002444 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2445 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002446 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2447 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002448 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002449 else
2450 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002451 contextsupport = (err == 0);
2452 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2453 if (err == -EOPNOTSUPP)
2454 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2455 else if (err)
2456 goto out_nfserr;
2457 }
2458 }
2459#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2460
Kinglong Mee75976de2015-07-30 21:55:30 +08002461 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2462 if (status)
2463 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002464
2465 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002466 p = xdr_reserve_space(xdr, 4);
2467 if (!p)
2468 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002469 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002472 u32 supp[3];
2473
2474 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002475
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002476 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002477 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002478 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002479 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2480 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002481 p = xdr_reserve_space(xdr, 12);
2482 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002483 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002484 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002485 *p++ = cpu_to_be32(supp[0]);
2486 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002487 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002488 p = xdr_reserve_space(xdr, 16);
2489 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002490 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002491 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002492 *p++ = cpu_to_be32(supp[0]);
2493 *p++ = cpu_to_be32(supp[1]);
2494 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002498 p = xdr_reserve_space(xdr, 4);
2499 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002501 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002502 if (dummy == NF4BAD) {
2503 status = nfserr_serverfault;
2504 goto out;
2505 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002506 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002509 p = xdr_reserve_space(xdr, 4);
2510 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002512 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002513 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002514 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002515 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2516 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002519 p = xdr_reserve_space(xdr, 8);
2520 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002522 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002525 p = xdr_reserve_space(xdr, 8);
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, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002531 p = xdr_reserve_space(xdr, 4);
2532 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002534 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002537 p = xdr_reserve_space(xdr, 4);
2538 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002540 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002543 p = xdr_reserve_space(xdr, 4);
2544 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002546 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002549 p = xdr_reserve_space(xdr, 16);
2550 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002552 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002553 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2554 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002555 } else switch(fsid_source(fhp)) {
2556 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002557 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2558 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002559 break;
2560 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002561 *p++ = cpu_to_be32(0);
2562 *p++ = cpu_to_be32(MAJOR(stat.dev));
2563 *p++ = cpu_to_be32(0);
2564 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002565 break;
2566 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08002567 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2568 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 }
2572 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002573 p = xdr_reserve_space(xdr, 4);
2574 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002576 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002579 p = xdr_reserve_space(xdr, 4);
2580 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002582 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
2584 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002585 p = xdr_reserve_space(xdr, 4);
2586 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002588 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 if (bmval0 & FATTR4_WORD0_ACL) {
2591 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002594 p = xdr_reserve_space(xdr, 4);
2595 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 goto out_resource;
2597
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002598 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 goto out_acl;
2600 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002601 p = xdr_reserve_space(xdr, 4);
2602 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002604 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002606 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002607 p = xdr_reserve_space(xdr, 4*3);
2608 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002610 *p++ = cpu_to_be32(ace->type);
2611 *p++ = cpu_to_be32(ace->flag);
2612 *p++ = cpu_to_be32(ace->access_mask &
2613 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002614 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (status)
2616 goto out;
2617 }
2618 }
2619out_acl:
2620 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002621 p = xdr_reserve_space(xdr, 4);
2622 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002624 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2626 }
2627 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002628 p = xdr_reserve_space(xdr, 4);
2629 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002631 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
2633 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002634 p = xdr_reserve_space(xdr, 4);
2635 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002637 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002640 p = xdr_reserve_space(xdr, 4);
2641 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002643 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002646 p = xdr_reserve_space(xdr, 4);
2647 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002649 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
2651 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002652 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
2653 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002655 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
2656 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002659 p = xdr_reserve_space(xdr, 8);
2660 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002662 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002665 p = xdr_reserve_space(xdr, 8);
2666 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002668 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002671 p = xdr_reserve_space(xdr, 8);
2672 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002674 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002677 p = xdr_reserve_space(xdr, 8);
2678 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002680 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002682 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002683 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002684 if (status)
2685 goto out;
2686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002688 p = xdr_reserve_space(xdr, 4);
2689 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002691 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002694 p = xdr_reserve_space(xdr, 8);
2695 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002697 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002700 p = xdr_reserve_space(xdr, 4);
2701 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002703 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002706 p = xdr_reserve_space(xdr, 4);
2707 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002709 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002712 p = xdr_reserve_space(xdr, 8);
2713 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002715 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
2717 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002718 p = xdr_reserve_space(xdr, 8);
2719 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002721 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002724 p = xdr_reserve_space(xdr, 4);
2725 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002727 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
2729 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002730 p = xdr_reserve_space(xdr, 4);
2731 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002733 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
2735 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002736 p = xdr_reserve_space(xdr, 4);
2737 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002739 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002742 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (status)
2744 goto out;
2745 }
2746 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002747 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (status)
2749 goto out;
2750 }
2751 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002752 p = xdr_reserve_space(xdr, 8);
2753 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002755 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
2756 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
2758 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002759 p = xdr_reserve_space(xdr, 8);
2760 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 goto out_resource;
2762 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002763 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002766 p = xdr_reserve_space(xdr, 8);
2767 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 goto out_resource;
2769 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002770 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002773 p = xdr_reserve_space(xdr, 8);
2774 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 goto out_resource;
2776 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002777 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
2779 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002780 p = xdr_reserve_space(xdr, 8);
2781 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 goto out_resource;
2783 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002784 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002787 p = xdr_reserve_space(xdr, 12);
2788 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002790 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002791 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002794 p = xdr_reserve_space(xdr, 12);
2795 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002797 *p++ = cpu_to_be32(0);
2798 *p++ = cpu_to_be32(1);
2799 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002802 p = xdr_reserve_space(xdr, 12);
2803 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002805 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002806 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002809 p = xdr_reserve_space(xdr, 12);
2810 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002812 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002813 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08002816 struct kstat parent_stat;
2817 u64 ino = stat.ino;
2818
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002819 p = xdr_reserve_space(xdr, 8);
2820 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002822 /*
2823 * Get parent's attributes if not ignoring crossmount
2824 * and this is the root of a cross-mounted filesystem.
2825 */
2826 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08002827 dentry == exp->ex_path.mnt->mnt_root) {
2828 err = get_parent_attributes(exp, &parent_stat);
2829 if (err)
2830 goto out_nfserr;
2831 ino = parent_stat.ino;
2832 }
2833 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002835#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08002836 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04002837 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002838 if (status)
2839 goto out;
2840 }
2841
2842 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04002843 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002844 if (status)
2845 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002846 }
2847
2848 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
2849 p = xdr_reserve_space(xdr, 4);
2850 if (!p)
2851 goto out_resource;
2852 *p++ = cpu_to_be32(stat.blksize);
2853 }
2854#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03002855 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04002856 u32 supp[3];
2857
2858 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2859 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
2860 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
2861 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
2862
2863 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08002864 if (status)
2865 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03002866 }
Andy Adamson7e705702009-04-03 08:29:11 +03002867
Kinglong Mee7d580722015-07-30 21:54:26 +08002868 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2869 status = nfsd4_encode_security_label(xdr, rqstp, context,
2870 contextlen);
2871 if (status)
2872 goto out;
2873 }
2874
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002875 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
2876 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 status = nfs_ok;
2878
2879out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002880#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002881 if (context)
2882 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002883#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002884 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08002885 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002886 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08002887 kfree(tempfh);
2888 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002889 if (status)
2890 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return status;
2892out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002893 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 goto out;
2895out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 status = nfserr_resource;
2897 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002900static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
2901 struct xdr_buf *buf, __be32 *p, int bytes)
2902{
2903 xdr->scratch.iov_len = 0;
2904 memset(buf, 0, sizeof(struct xdr_buf));
2905 buf->head[0].iov_base = p;
2906 buf->head[0].iov_len = 0;
2907 buf->len = 0;
2908 xdr->buf = buf;
2909 xdr->iov = buf->head;
2910 xdr->p = p;
2911 xdr->end = (void *)p + bytes;
2912 buf->buflen = bytes;
2913}
2914
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002915__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
2916 struct svc_fh *fhp, struct svc_export *exp,
2917 struct dentry *dentry, u32 *bmval,
2918 struct svc_rqst *rqstp, int ignore_crossmnt)
2919{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002920 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002921 struct xdr_stream xdr;
2922 __be32 ret;
2923
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002924 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002925 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
2926 ignore_crossmnt);
2927 *p = xdr.p;
2928 return ret;
2929}
2930
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002931static inline int attributes_need_mount(u32 *bmval)
2932{
2933 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2934 return 1;
2935 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2936 return 1;
2937 return 0;
2938}
2939
Al Virob37ad282006-10-19 23:28:59 -07002940static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002941nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
2942 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 struct svc_export *exp = cd->rd_fhp->fh_export;
2945 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002946 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002947 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
NeilBrownbbddca82016-01-07 16:08:20 -05002949 dentry = lookup_one_len_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (IS_ERR(dentry))
2951 return nfserrno(PTR_ERR(dentry));
David Howells2b0143b2015-03-17 22:25:59 +00002952 if (d_really_is_negative(dentry)) {
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002953 /*
NeilBrownbbddca82016-01-07 16:08:20 -05002954 * we're not holding the i_mutex here, so there's
2955 * a window where this directory entry could have gone
2956 * away.
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002957 */
2958 dput(dentry);
2959 return nfserr_noent;
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002963 /*
2964 * In the case of a mountpoint, the client may be asking for
2965 * attributes that are only properties of the underlying filesystem
2966 * as opposed to the cross-mounted file system. In such a case,
2967 * we will not follow the cross mount and will fill the attribtutes
2968 * directly from the mountpoint dentry.
2969 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002970 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002971 int err;
2972
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002973 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2974 && !attributes_need_mount(cd->rd_bmval)) {
2975 ignore_crossmnt = 1;
2976 goto out_encode;
2977 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002978 /*
2979 * Why the heck aren't we just using nfsd_lookup??
2980 * Different "."/".." handling? Something else?
2981 * At least, add a comment here to explain....
2982 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002983 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2984 if (err) {
2985 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto out_put;
2987 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002988 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2989 if (nfserr)
2990 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002993out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002994 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002995 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996out_put:
2997 dput(dentry);
2998 exp_put(exp);
2999 return nfserr;
3000}
3001
Al Viro2ebbc012006-10-19 23:28:58 -07003002static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003003nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003005 __be32 *p;
3006
Kinglong Meec3a45612014-07-06 11:34:43 +08003007 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003008 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return NULL;
3010 *p++ = htonl(2);
3011 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3012 *p++ = htonl(0); /* bmval1 */
3013
J. Bruce Fields87915c62013-01-16 17:33:28 -05003014 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return p;
3017}
3018
3019static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003020nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3021 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003023 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003025 struct xdr_stream *xdr = cd->xdr;
3026 int start_offset = xdr->buf->len;
3027 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003028 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003029 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003030 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003031 __be64 wire_offset;
3032 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 /* In nfsv4, "." and ".." never make it onto the wire.. */
3035 if (name && isdotent(name, namlen)) {
3036 cd->common.err = nfs_ok;
3037 return 0;
3038 }
3039
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003040 if (cd->cookie_offset) {
3041 wire_offset = cpu_to_be64(offset);
3042 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3043 &wire_offset, 8);
3044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003046 p = xdr_reserve_space(xdr, 4);
3047 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003050 cookie_offset = xdr->buf->len;
3051 p = xdr_reserve_space(xdr, 3*4 + namlen);
3052 if (!p)
3053 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3055 p = xdr_encode_array(p, name, namlen); /* name length & name */
3056
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003057 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 switch (nfserr) {
3059 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 break;
3061 case nfserr_resource:
3062 nfserr = nfserr_toosmall;
3063 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003064 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003065 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003066 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 default:
3068 /*
3069 * If the client requested the RDATTR_ERROR attribute,
3070 * we stuff the error code into this attribute
3071 * and continue. If this attribute was not requested,
3072 * then in accordance with the spec, we fail the
3073 * entire READDIR operation(!)
3074 */
3075 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3076 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003077 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003078 if (p == NULL) {
3079 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003083 nfserr = nfserr_toosmall;
3084 entry_bytes = xdr->buf->len - start_offset;
3085 if (entry_bytes > cd->rd_maxcount)
3086 goto fail;
3087 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003088 /*
3089 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3090 * let's always let through the first entry, at least:
3091 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003092 if (!cd->rd_dircount)
3093 goto fail;
3094 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003095 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3096 goto fail;
3097 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003098
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003099 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003100skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 cd->common.err = nfs_ok;
3102 return 0;
3103fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003104 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 cd->common.err = nfserr;
3106 return -EINVAL;
3107}
3108
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003109static __be32
3110nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003111{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003112 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003113
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003114 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3115 if (!p)
3116 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003117 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003118 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3119 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003120 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003121}
3122
Benny Halevy695e12f2008-07-04 14:38:33 +03003123static __be32
Al Virob37ad282006-10-19 23:28:59 -07003124nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003126 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003127 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003129 p = xdr_reserve_space(xdr, 8);
3130 if (!p)
3131 return nfserr_resource;
3132 *p++ = cpu_to_be32(access->ac_supported);
3133 *p++ = cpu_to_be32(access->ac_resp_access);
3134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003137static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3138{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003139 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003140 __be32 *p;
3141
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003142 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3143 if (!p)
3144 return nfserr_resource;
3145 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3146 NFS4_MAX_SESSIONID_LEN);
3147 *p++ = cpu_to_be32(bcts->dir);
3148 /* Upshifting from TCP to RDMA is not supported */
3149 *p++ = cpu_to_be32(0);
3150 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003151}
3152
Benny Halevy695e12f2008-07-04 14:38:33 +03003153static __be32
Al Virob37ad282006-10-19 23:28:59 -07003154nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003156 struct xdr_stream *xdr = &resp->xdr;
3157
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003158 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161
Benny Halevy695e12f2008-07-04 14:38:33 +03003162static __be32
Al Virob37ad282006-10-19 23:28:59 -07003163nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003165 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003166 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003168 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3169 if (!p)
3170 return nfserr_resource;
3171 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003172 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Benny Halevy695e12f2008-07-04 14:38:33 +03003176static __be32
Al Virob37ad282006-10-19 23:28:59 -07003177nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003179 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003180 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003182 p = xdr_reserve_space(xdr, 20);
3183 if (!p)
3184 return nfserr_resource;
3185 encode_cinfo(p, &create->cr_cinfo);
3186 nfserr = nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
3187 create->cr_bmval[1], create->cr_bmval[2]);
3188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
Al Virob37ad282006-10-19 23:28:59 -07003191static __be32
3192nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003195 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003197 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3198 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
Benny Halevy695e12f2008-07-04 14:38:33 +03003201static __be32
3202nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003204 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003205 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003207 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003209 len = fhp->fh_handle.fh_size;
3210 p = xdr_reserve_space(xdr, len + 4);
3211 if (!p)
3212 return nfserr_resource;
3213 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
3217/*
3218* Including all fields other than the name, a LOCK4denied structure requires
3219* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3220*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003221static __be32
3222nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003224 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003225 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003227again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003228 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003229 if (!p) {
3230 /*
3231 * Don't fail to return the result just because we can't
3232 * return the conflicting open:
3233 */
3234 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003235 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003236 conf->len = 0;
3237 conf->data = NULL;
3238 goto again;
3239 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003240 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003241 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003242 p = xdr_encode_hyper(p, ld->ld_start);
3243 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003244 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003245 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003246 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3247 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003248 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003250 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003251 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003253 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
Benny Halevy695e12f2008-07-04 14:38:33 +03003256static __be32
Al Virob37ad282006-10-19 23:28:59 -07003257nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003259 struct xdr_stream *xdr = &resp->xdr;
3260
Benny Halevye2f282b2008-08-12 20:45:07 +03003261 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003262 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003263 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003264 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003265
Benny Halevy695e12f2008-07-04 14:38:33 +03003266 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Benny Halevy695e12f2008-07-04 14:38:33 +03003269static __be32
Al Virob37ad282006-10-19 23:28:59 -07003270nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003272 struct xdr_stream *xdr = &resp->xdr;
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003275 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003276 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Benny Halevy695e12f2008-07-04 14:38:33 +03003279static __be32
Al Virob37ad282006-10-19 23:28:59 -07003280nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003282 struct xdr_stream *xdr = &resp->xdr;
3283
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003284 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
3287
Benny Halevy695e12f2008-07-04 14:38:33 +03003288static __be32
Al Virob37ad282006-10-19 23:28:59 -07003289nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003291 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003292 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003294 p = xdr_reserve_space(xdr, 20);
3295 if (!p)
3296 return nfserr_resource;
3297 p = encode_cinfo(p, &link->li_cinfo);
3298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
3301
Benny Halevy695e12f2008-07-04 14:38:33 +03003302static __be32
Al Virob37ad282006-10-19 23:28:59 -07003303nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003305 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003306 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003308 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3309 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003310 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003311 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003312 if (!p)
3313 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003314 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003315 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Kinglong Mee75976de2015-07-30 21:55:30 +08003317 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3318 open->op_bmval[2]);
3319 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003320 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003321
3322 p = xdr_reserve_space(xdr, 4);
3323 if (!p)
3324 return nfserr_resource;
3325
3326 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 switch (open->op_delegate_type) {
3328 case NFS4_OPEN_DELEGATE_NONE:
3329 break;
3330 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003331 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3332 if (nfserr)
3333 return nfserr;
3334 p = xdr_reserve_space(xdr, 20);
3335 if (!p)
3336 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003337 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339 /*
3340 * TODO: ACE's in delegations
3341 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003342 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3343 *p++ = cpu_to_be32(0);
3344 *p++ = cpu_to_be32(0);
3345 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 break;
3347 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003348 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3349 if (nfserr)
3350 return nfserr;
3351 p = xdr_reserve_space(xdr, 32);
3352 if (!p)
3353 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003354 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 /*
3357 * TODO: space_limit's in delegations
3358 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003359 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3360 *p++ = cpu_to_be32(~(u32)0);
3361 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 /*
3364 * TODO: ACE's in delegations
3365 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003366 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3367 *p++ = cpu_to_be32(0);
3368 *p++ = cpu_to_be32(0);
3369 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003371 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3372 switch (open->op_why_no_deleg) {
3373 case WND4_CONTENTION:
3374 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003375 p = xdr_reserve_space(xdr, 8);
3376 if (!p)
3377 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003378 *p++ = cpu_to_be32(open->op_why_no_deleg);
3379 /* deleg signaling not supported yet: */
3380 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003381 break;
3382 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003383 p = xdr_reserve_space(xdr, 4);
3384 if (!p)
3385 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003386 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003387 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 default:
3390 BUG();
3391 }
3392 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
Benny Halevy695e12f2008-07-04 14:38:33 +03003396static __be32
Al Virob37ad282006-10-19 23:28:59 -07003397nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003399 struct xdr_stream *xdr = &resp->xdr;
3400
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003401 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Benny Halevy695e12f2008-07-04 14:38:33 +03003404static __be32
Al Virob37ad282006-10-19 23:28:59 -07003405nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003407 struct xdr_stream *xdr = &resp->xdr;
3408
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003409 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003412static __be32 nfsd4_encode_splice_read(
3413 struct nfsd4_compoundres *resp,
3414 struct nfsd4_read *read,
3415 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003417 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003418 struct xdr_buf *buf = xdr->buf;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003419 u32 eof;
Benjamin Coddingtonac503e42016-03-22 10:28:36 -04003420 long len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003421 int space_left;
3422 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003423 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003424
Kinglong Meed5d5c302014-07-09 21:51:27 +08003425 /* Make sure there will be room for padding if needed */
3426 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003427 return nfserr_resource;
3428
Benjamin Coddingtonac503e42016-03-22 10:28:36 -04003429 len = maxcount;
Chuck Lever87c59422018-03-28 13:29:11 -04003430 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
3431 file, read->rd_offset, &maxcount);
3432 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003433 if (nfserr) {
3434 /*
3435 * nfsd_splice_actor may have already messed with the
3436 * page length; reset it so as not to confuse
3437 * xdr_truncate_encode:
3438 */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003439 buf->page_len = 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003440 return nfserr;
3441 }
3442
Benjamin Coddingtonac503e42016-03-22 10:28:36 -04003443 eof = nfsd_eof_on_read(len, maxcount, read->rd_offset,
3444 d_inode(read->rd_fhp->fh_dentry)->i_size);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003445
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003446 *(p++) = htonl(eof);
3447 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003448
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003449 buf->page_len = maxcount;
3450 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003451 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3452 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003453
3454 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003455 buf->tail[0].iov_base = xdr->p;
3456 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003457 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003458 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003459 int pad = 4 - (maxcount&3);
3460
3461 *(xdr->p++) = 0;
3462
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003463 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003464 buf->tail[0].iov_len = pad;
3465 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003466 }
3467
3468 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003469 buf->buflen - buf->len);
3470 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003471 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3472
3473 return 0;
3474}
3475
3476static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3477 struct nfsd4_read *read,
3478 struct file *file, unsigned long maxcount)
3479{
3480 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 u32 eof;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003482 int v;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003483 int starting_len = xdr->buf->len - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 long len;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003485 int thislen;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003486 __be32 nfserr;
3487 __be32 tmp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003488 __be32 *p;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003489 u32 zzz = 0;
3490 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 len = maxcount;
3493 v = 0;
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003494
Kinglong Mee10554142014-06-29 19:18:17 +08003495 thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003496 p = xdr_reserve_space(xdr, (thislen+3)&~3);
3497 WARN_ON_ONCE(!p);
3498 resp->rqstp->rq_vec[v].iov_base = p;
3499 resp->rqstp->rq_vec[v].iov_len = thislen;
3500 v++;
3501 len -= thislen;
3502
3503 while (len) {
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003504 thislen = min_t(long, len, PAGE_SIZE);
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003505 p = xdr_reserve_space(xdr, (thislen+3)&~3);
3506 WARN_ON_ONCE(!p);
3507 resp->rqstp->rq_vec[v].iov_base = p;
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003508 resp->rqstp->rq_vec[v].iov_len = thislen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 v++;
J. Bruce Fields6ff98972014-03-11 16:51:23 -04003510 len -= thislen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
3512 read->rd_vlen = v;
3513
Benjamin Coddingtonac503e42016-03-22 10:28:36 -04003514 len = maxcount;
Chuck Lever87c59422018-03-28 13:29:11 -04003515 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
3516 resp->rqstp->rq_vec, read->rd_vlen, &maxcount);
3517 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003518 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 return nfserr;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003520 xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003521
Benjamin Coddingtonac503e42016-03-22 10:28:36 -04003522 eof = nfsd_eof_on_read(len, maxcount, read->rd_offset,
3523 d_inode(read->rd_fhp->fh_dentry)->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003525 tmp = htonl(eof);
3526 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3527 tmp = htonl(maxcount);
3528 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3529
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003530 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3531 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
3532 &zzz, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003534
3535}
3536
3537static __be32
3538nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3539 struct nfsd4_read *read)
3540{
3541 unsigned long maxcount;
3542 struct xdr_stream *xdr = &resp->xdr;
3543 struct file *file = read->rd_filp;
3544 int starting_len = xdr->buf->len;
Christoph Hellwige749a462015-06-18 16:44:58 +02003545 struct raparms *ra = NULL;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003546 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003547
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003548 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3549 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003550 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003551 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003552 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003553 if (resp->xdr.buf->page_len &&
3554 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003555 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003556 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003557 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003558 xdr_commit_encode(xdr);
3559
3560 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003561 maxcount = min_t(unsigned long, maxcount,
3562 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08003563 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003564
Christoph Hellwigaf90f702015-06-18 16:45:00 +02003565 if (read->rd_tmp_file)
Christoph Hellwige749a462015-06-18 16:44:58 +02003566 ra = nfsd_init_raparms(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003567
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003568 if (file->f_op->splice_read &&
3569 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003570 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003571 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003572 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003573
Christoph Hellwige749a462015-06-18 16:44:58 +02003574 if (ra)
3575 nfsd_put_raparams(file, ra);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003576
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003577 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003578 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003579
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003580 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Al Virob37ad282006-10-19 23:28:59 -07003583static __be32
3584nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003587 __be32 wire_count;
3588 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003589 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003590 int length_offset = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003591 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003593 p = xdr_reserve_space(xdr, 4);
3594 if (!p)
3595 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003597
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003598 p = xdr_reserve_space(xdr, maxcount);
3599 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04003600 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01003602 * XXX: By default, vfs_readlink() will truncate symlinks if they
3603 * would overflow the buffer. Is this kosher in NFSv4? If not, one
3604 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
3605 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003607 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
3608 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003610 nfserr = nfserr_inval;
3611 if (nfserr) {
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003612 xdr_truncate_encode(xdr, length_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return nfserr;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003616 wire_count = htonl(maxcount);
3617 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03003618 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003619 if (maxcount & 3)
3620 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
3621 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return 0;
3623}
3624
Al Virob37ad282006-10-19 23:28:59 -07003625static __be32
3626nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003629 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003631 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003632 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003633 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003634 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003636 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3637 if (!p)
3638 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003641 *p++ = cpu_to_be32(0);
3642 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05003643 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
3644 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003647 * Number of bytes left for directory entries allowing for the
3648 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003650 bytes_left = xdr->buf->buflen - xdr->buf->len
3651 - COMPOUND_ERR_SLACK_SPACE - 8;
3652 if (bytes_left < 0) {
3653 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 goto err_no_verf;
3655 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003656 maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
3657 /*
3658 * Note the rfc defines rd_maxcount as the size of the
3659 * READDIR4resok structure, which includes the verifier above
3660 * and the 8 bytes encoded at the end of this function:
3661 */
3662 if (maxcount < 16) {
3663 nfserr = nfserr_toosmall;
3664 goto err_no_verf;
3665 }
3666 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003668 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
3669 if (!readdir->rd_dircount)
3670 readdir->rd_dircount = INT_MAX;
3671
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003672 readdir->xdr = xdr;
3673 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003675 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677 offset = readdir->rd_cookie;
3678 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3679 &offset,
3680 &readdir->common, nfsd4_encode_dirent);
3681 if (nfserr == nfs_ok &&
3682 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003683 xdr->buf->len == starting_len + 8) {
3684 /* nothing encoded; which limit did we hit?: */
3685 if (maxcount - 16 < bytes_left)
3686 /* It was the fault of rd_maxcount: */
3687 nfserr = nfserr_toosmall;
3688 else
3689 /* We ran out of buffer space: */
3690 nfserr = nfserr_resource;
3691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 if (nfserr)
3693 goto err_no_verf;
3694
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003695 if (readdir->cookie_offset) {
3696 wire_offset = cpu_to_be64(offset);
3697 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
3698 &wire_offset, 8);
3699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003701 p = xdr_reserve_space(xdr, 8);
3702 if (!p) {
3703 WARN_ON_ONCE(1);
3704 goto err_no_verf;
3705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 *p++ = 0; /* no more entries */
3707 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 return 0;
3710err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003711 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return nfserr;
3713}
3714
Benny Halevy695e12f2008-07-04 14:38:33 +03003715static __be32
Al Virob37ad282006-10-19 23:28:59 -07003716nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003718 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003719 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003721 p = xdr_reserve_space(xdr, 20);
3722 if (!p)
3723 return nfserr_resource;
3724 p = encode_cinfo(p, &remove->rm_cinfo);
3725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Benny Halevy695e12f2008-07-04 14:38:33 +03003728static __be32
Al Virob37ad282006-10-19 23:28:59 -07003729nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003731 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003732 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003734 p = xdr_reserve_space(xdr, 40);
3735 if (!p)
3736 return nfserr_resource;
3737 p = encode_cinfo(p, &rename->rn_sinfo);
3738 p = encode_cinfo(p, &rename->rn_tinfo);
3739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
Benny Halevy695e12f2008-07-04 14:38:33 +03003742static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003743nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003744{
Chuck Lever676e4eb2013-04-30 18:48:54 -04003745 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003746 struct exp_flavor_info *flavs;
3747 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04003748 __be32 *p, *flavorsp;
3749 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003750
J. Bruce Fields4796f452007-07-17 04:04:51 -07003751 if (exp->ex_nflavors) {
3752 flavs = exp->ex_flavors;
3753 nflavs = exp->ex_nflavors;
3754 } else { /* Handling of some defaults in absence of real secinfo: */
3755 flavs = def_flavs;
3756 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3757 nflavs = 2;
3758 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3759 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3760 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3761 nflavs = 1;
3762 flavs[0].pseudoflavor
3763 = svcauth_gss_flavor(exp->ex_client);
3764 } else {
3765 nflavs = 1;
3766 flavs[0].pseudoflavor
3767 = exp->ex_client->flavour->flavour;
3768 }
3769 }
3770
Chuck Lever676e4eb2013-04-30 18:48:54 -04003771 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003772 p = xdr_reserve_space(xdr, 4);
3773 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003774 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04003775 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04003776
J. Bruce Fields4796f452007-07-17 04:04:51 -07003777 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04003778 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04003779 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003780
Chuck Lever676e4eb2013-04-30 18:48:54 -04003781 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3782 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003783 p = xdr_reserve_space(xdr, 4 + 4 +
3784 XDR_LEN(info.oid.len) + 4 + 4);
3785 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003786 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003787 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003788 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003789 *p++ = cpu_to_be32(info.qop);
3790 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003791 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3792 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003793 p = xdr_reserve_space(xdr, 4);
3794 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003795 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003796 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003797 } else {
3798 if (report)
3799 pr_warn("NFS: SECINFO: security flavor %u "
3800 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003801 }
3802 }
Chuck Levera77c8062013-03-16 15:55:10 -04003803
Chuck Lever676e4eb2013-04-30 18:48:54 -04003804 if (nflavs != supported)
3805 report = false;
3806 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003807 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003808}
3809
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003810static __be32
3811nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3812 struct nfsd4_secinfo *secinfo)
3813{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003814 struct xdr_stream *xdr = &resp->xdr;
3815
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003816 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003817}
3818
3819static __be32
3820nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3821 struct nfsd4_secinfo_no_name *secinfo)
3822{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003823 struct xdr_stream *xdr = &resp->xdr;
3824
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003825 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003826}
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828/*
3829 * The SETATTR encode routine is special -- it always encodes a bitmap,
3830 * regardless of the error status.
3831 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003832static __be32
Al Virob37ad282006-10-19 23:28:59 -07003833nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003835 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003836 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003838 p = xdr_reserve_space(xdr, 16);
3839 if (!p)
3840 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003842 *p++ = cpu_to_be32(3);
3843 *p++ = cpu_to_be32(0);
3844 *p++ = cpu_to_be32(0);
3845 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
3847 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003848 *p++ = cpu_to_be32(3);
3849 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
3850 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
3851 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003853 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
Benny Halevy695e12f2008-07-04 14:38:33 +03003856static __be32
Al Virob37ad282006-10-19 23:28:59 -07003857nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003859 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003860 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003863 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
3864 if (!p)
3865 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003866 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
3867 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
3868 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
3870 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003871 p = xdr_reserve_space(xdr, 8);
3872 if (!p)
3873 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003874 *p++ = cpu_to_be32(0);
3875 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003877 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
Benny Halevy695e12f2008-07-04 14:38:33 +03003880static __be32
Al Virob37ad282006-10-19 23:28:59 -07003881nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003883 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003884 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003886 p = xdr_reserve_space(xdr, 16);
3887 if (!p)
3888 return nfserr_resource;
3889 *p++ = cpu_to_be32(write->wr_bytes_written);
3890 *p++ = cpu_to_be32(write->wr_how_written);
3891 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
3892 NFS4_VERIFIER_SIZE);
3893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
Benny Halevy695e12f2008-07-04 14:38:33 +03003896static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003897nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003898 struct nfsd4_exchange_id *exid)
3899{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003900 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003901 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003902 char *major_id;
3903 char *server_scope;
3904 int major_id_sz;
3905 int server_scope_sz;
3906 uint64_t minor_id = 0;
3907
Andy Adamson0733d212009-04-03 08:28:01 +03003908 major_id = utsname()->nodename;
3909 major_id_sz = strlen(major_id);
3910 server_scope = utsname()->nodename;
3911 server_scope_sz = strlen(server_scope);
3912
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003913 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03003914 8 /* eir_clientid */ +
3915 4 /* eir_sequenceid */ +
3916 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003917 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003918 if (!p)
3919 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03003920
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003921 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003922 *p++ = cpu_to_be32(exid->seqid);
3923 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003924
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003925 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003926
J. Bruce Fields57266a62013-04-13 14:27:29 -04003927 switch (exid->spa_how) {
3928 case SP4_NONE:
3929 break;
3930 case SP4_MACH_CRED:
3931 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003932 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04003933 exid->spo_must_enforce[0],
3934 exid->spo_must_enforce[1],
3935 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003936 if (nfserr)
3937 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04003938 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003939 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04003940 exid->spo_must_allow[0],
3941 exid->spo_must_allow[1],
3942 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003943 if (nfserr)
3944 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003945 break;
3946 default:
3947 WARN_ON_ONCE(1);
3948 }
Andy Adamson0733d212009-04-03 08:28:01 +03003949
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003950 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003951 8 /* so_minor_id */ +
3952 4 /* so_major_id.len */ +
3953 (XDR_QUADLEN(major_id_sz) * 4) +
3954 4 /* eir_server_scope.len */ +
3955 (XDR_QUADLEN(server_scope_sz) * 4) +
3956 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003957 if (!p)
3958 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003959
Andy Adamson0733d212009-04-03 08:28:01 +03003960 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003961 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03003962 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003963 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03003964
3965 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003966 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03003967
3968 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003969 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03003970 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003971}
3972
3973static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003974nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003975 struct nfsd4_create_session *sess)
3976{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003977 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003978 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003979
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003980 p = xdr_reserve_space(xdr, 24);
3981 if (!p)
3982 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003983 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
3984 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003985 *p++ = cpu_to_be32(sess->seqid);
3986 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003987
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003988 p = xdr_reserve_space(xdr, 28);
3989 if (!p)
3990 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003991 *p++ = cpu_to_be32(0); /* headerpadsz */
3992 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
3993 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
3994 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
3995 *p++ = cpu_to_be32(sess->fore_channel.maxops);
3996 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
3997 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003998
3999 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004000 p = xdr_reserve_space(xdr, 4);
4001 if (!p)
4002 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004003 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004004 }
4005
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004006 p = xdr_reserve_space(xdr, 28);
4007 if (!p)
4008 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004009 *p++ = cpu_to_be32(0); /* headerpadsz */
4010 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4011 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4012 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4013 *p++ = cpu_to_be32(sess->back_channel.maxops);
4014 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4015 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004016
4017 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004018 p = xdr_reserve_space(xdr, 4);
4019 if (!p)
4020 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004021 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004022 }
4023 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004024}
4025
4026static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004027nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004028 struct nfsd4_sequence *seq)
4029{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004030 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004031 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004032
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004033 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4034 if (!p)
4035 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004036 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4037 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004038 *p++ = cpu_to_be32(seq->seqid);
4039 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004040 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004041 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4042 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4043 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004044
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004045 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004046 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004047}
4048
J. Bruce Fields2355c592012-04-25 16:56:22 -04004049static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004050nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004051 struct nfsd4_test_stateid *test_stateid)
4052{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004053 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004054 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004055 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004056
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004057 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4058 if (!p)
4059 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004060 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004061
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004062 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004063 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004064 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004065
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004066 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004067}
4068
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004069#ifdef CONFIG_NFSD_PNFS
4070static __be32
4071nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4072 struct nfsd4_getdeviceinfo *gdev)
4073{
4074 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004075 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004076 u32 starting_len = xdr->buf->len, needed_len;
4077 __be32 *p;
4078
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004079 p = xdr_reserve_space(xdr, 4);
4080 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004081 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004082
4083 *p++ = cpu_to_be32(gdev->gd_layout_type);
4084
4085 /* If maxcount is 0 then just update notifications */
4086 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004087 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004088 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4089 if (nfserr) {
4090 /*
4091 * We don't bother to burden the layout drivers with
4092 * enforcing gd_maxcount, just tell the client to
4093 * come back with a bigger buffer if it's not enough.
4094 */
4095 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4096 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004097 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004098 }
4099 }
4100
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004101 if (gdev->gd_notify_types) {
4102 p = xdr_reserve_space(xdr, 4 + 4);
4103 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004104 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004105 *p++ = cpu_to_be32(1); /* bitmap length */
4106 *p++ = cpu_to_be32(gdev->gd_notify_types);
4107 } else {
4108 p = xdr_reserve_space(xdr, 4);
4109 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004110 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004111 *p++ = 0;
4112 }
4113
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004114 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004115toosmall:
4116 dprintk("%s: maxcount too small\n", __func__);
4117 needed_len = xdr->buf->len + 4 /* notifications */;
4118 xdr_truncate_encode(xdr, starting_len);
4119 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004120 if (!p)
4121 return nfserr_resource;
4122 *p++ = cpu_to_be32(needed_len);
4123 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004124}
4125
4126static __be32
4127nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4128 struct nfsd4_layoutget *lgp)
4129{
4130 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004131 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004132 __be32 *p;
4133
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004134 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4135 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004136 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004137
4138 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4139 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4140 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4141 sizeof(stateid_opaque_t));
4142
4143 *p++ = cpu_to_be32(1); /* we always return a single layout */
4144 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4145 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4146 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4147 *p++ = cpu_to_be32(lgp->lg_layout_type);
4148
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004149 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004150 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004151}
4152
4153static __be32
4154nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4155 struct nfsd4_layoutcommit *lcp)
4156{
4157 struct xdr_stream *xdr = &resp->xdr;
4158 __be32 *p;
4159
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004160 p = xdr_reserve_space(xdr, 4);
4161 if (!p)
4162 return nfserr_resource;
4163 *p++ = cpu_to_be32(lcp->lc_size_chg);
4164 if (lcp->lc_size_chg) {
4165 p = xdr_reserve_space(xdr, 8);
4166 if (!p)
4167 return nfserr_resource;
4168 p = xdr_encode_hyper(p, lcp->lc_newsize);
4169 }
4170
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004171 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004172}
4173
4174static __be32
4175nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4176 struct nfsd4_layoutreturn *lrp)
4177{
4178 struct xdr_stream *xdr = &resp->xdr;
4179 __be32 *p;
4180
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004181 p = xdr_reserve_space(xdr, 4);
4182 if (!p)
4183 return nfserr_resource;
4184 *p++ = cpu_to_be32(lrp->lrs_present);
4185 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004186 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004187 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004188}
4189#endif /* CONFIG_NFSD_PNFS */
4190
Andy Adamson2db134e2009-04-03 08:27:55 +03004191static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004192nfsd42_encode_write_res(struct nfsd4_compoundres *resp, struct nfsd42_write_res *write)
4193{
4194 __be32 *p;
4195
4196 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 4 + NFS4_VERIFIER_SIZE);
4197 if (!p)
4198 return nfserr_resource;
4199
4200 *p++ = cpu_to_be32(0);
4201 p = xdr_encode_hyper(p, write->wr_bytes_written);
4202 *p++ = cpu_to_be32(write->wr_stable_how);
4203 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4204 NFS4_VERIFIER_SIZE);
4205 return nfs_ok;
4206}
4207
4208static __be32
4209nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4210 struct nfsd4_copy *copy)
4211{
4212 __be32 *p;
4213
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004214 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res);
4215 if (nfserr)
4216 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004217
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004218 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004219 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004220 *p++ = cpu_to_be32(copy->cp_synchronous);
4221 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004222}
4223
4224static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004225nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4226 struct nfsd4_seek *seek)
4227{
4228 __be32 *p;
4229
Anna Schumaker24bab492014-09-26 13:58:27 -04004230 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4231 *p++ = cpu_to_be32(seek->seek_eof);
4232 p = xdr_encode_hyper(p, seek->seek_pos);
4233
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004234 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004235}
4236
4237static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004238nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4239{
4240 return nfserr;
4241}
4242
4243typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
4244
Andy Adamson2db134e2009-04-03 08:27:55 +03004245/*
4246 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
4247 * since we don't need to filter out obsolete ops as this is
4248 * done in the decoding phase.
4249 */
Chuck Leverc1df6092017-08-01 11:59:58 -04004250static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04004251 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
4252 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
4253 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
4254 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
4255 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
4256 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
4257 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
4258 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
4259 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
4260 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
4261 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
4262 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
4263 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
4264 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
4265 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
4266 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02004267 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04004268 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
4269 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
4270 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
4271 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
4272 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
4273 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
4274 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
4275 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
4276 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
4277 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
4278 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
4279 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
4280 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
4281 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
4282 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
4283 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
4284 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
4285 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
4286 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
4287 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03004288
4289 /* NFSv4.1 operations */
4290 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04004291 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03004292 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
4293 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08004294 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
4295 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03004296 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004297#ifdef CONFIG_NFSD_PNFS
4298 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
4299 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
4300 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
4301 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
4302 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
4303#else
Andy Adamson2db134e2009-04-03 08:27:55 +03004304 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
4305 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
4306 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
4307 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
4308 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004309#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004310 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03004311 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
4312 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04004313 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03004314 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
4315 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
4316 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04004317
4318 /* NFSv4.2 operations */
4319 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004320 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Anna Schumaker87a15a82014-09-26 13:58:26 -04004321 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
4322 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
4323 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
4324 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
4325 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
4326 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
4327 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
4328 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker24bab492014-09-26 13:58:27 -04004329 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04004330 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01004331 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03004332};
4333
Andy Adamson496c2622009-04-03 08:28:48 +03004334/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04004335 * Calculate whether we still have space to encode repsize bytes.
4336 * There are two considerations:
4337 * - For NFS versions >=4.1, the size of the reply must stay within
4338 * session limits
4339 * - For all NFS versions, we must stay within limited preallocated
4340 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03004341 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04004342 * This is called before the operation is processed, so can only provide
4343 * an upper estimate. For some nonidempotent operations (such as
4344 * getattr), it's not necessarily a problem if that estimate is wrong,
4345 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03004346 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04004347__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03004348{
J. Bruce Fields67492c92014-03-08 16:42:05 -05004349 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04004350 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03004351
J. Bruce Fields47ee5292014-03-12 21:39:35 -04004352 if (buf->len + respsize <= buf->buflen)
4353 return nfs_ok;
4354 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05004355 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04004356 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
4357 WARN_ON_ONCE(1);
4358 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05004359 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04004360 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03004361}
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363void
4364nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
4365{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004366 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004367 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05004368 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04004369 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004370 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05004371 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004372 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004374 p = xdr_reserve_space(xdr, 8);
4375 if (!p) {
4376 WARN_ON_ONCE(1);
4377 return;
4378 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004379 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004380 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Benny Halevy695e12f2008-07-04 14:38:33 +03004382 if (op->opnum == OP_ILLEGAL)
4383 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04004384 if (op->status && opdesc &&
4385 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
4386 goto status;
Benny Halevy695e12f2008-07-04 14:38:33 +03004387 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
4388 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05004389 encoder = nfsd4_enc_ops[op->opnum];
4390 op->status = encoder(resp, op->status, &op->u);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04004391 if (opdesc && opdesc->op_release)
4392 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004393 xdr_commit_encode(xdr);
4394
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04004395 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05004396 if (!op->status) {
4397 int space_needed = 0;
4398 if (!nfsd4_last_compound_op(rqstp))
4399 space_needed = COMPOUND_ERR_SLACK_SPACE;
4400 op->status = nfsd4_check_resp_size(resp, space_needed);
4401 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04004402 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
4403 struct nfsd4_slot *slot = resp->cstate.slot;
4404
4405 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
4406 op->status = nfserr_rep_too_big_to_cache;
4407 else
4408 op->status = nfserr_rep_too_big;
4409 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05004410 if (op->status == nfserr_resource ||
4411 op->status == nfserr_rep_too_big ||
4412 op->status == nfserr_rep_too_big_to_cache) {
4413 /*
4414 * The operation may have already been encoded or
4415 * partially encoded. No op returns anything additional
4416 * in the case of one of these three errors, so we can
4417 * just truncate back to after the status. But it's a
4418 * bug if we had to do this on a non-idempotent op:
4419 */
4420 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004421 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05004422 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004423 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004424 int len = xdr->buf->len - post_err_offset;
4425
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004426 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004427 so->so_replay.rp_buflen = len;
4428 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
4429 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004430 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004431status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04004432 /* Note that op->status is already in network byte order: */
4433 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
4436/*
4437 * Encode the reply stored in the stateowner reply cache
4438 *
4439 * XDR note: do not encode rp->rp_buflen: the buffer contains the
4440 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 */
4442void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004443nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004445 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct nfs4_replay *rp = op->replay;
4447
4448 BUG_ON(!rp);
4449
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004450 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
4451 if (!p) {
4452 WARN_ON_ONCE(1);
4453 return;
4454 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004455 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004458 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02004462nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
4464 return xdr_ressize_check(rqstp, p);
4465}
4466
Christoph Hellwig85374882017-05-08 18:48:24 +02004467void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04004469 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (args->ops != args->iops) {
4472 kfree(args->ops);
4473 args->ops = args->iops;
4474 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08004475 kfree(args->tmpp);
4476 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04004478 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 kfree(tb);
4481 }
4482}
4483
4484int
Christoph Hellwig026fec72017-05-08 19:01:48 +02004485nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Christoph Hellwig026fec72017-05-08 19:01:48 +02004487 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4488
Jeff Laytone874f9f2014-03-10 11:34:55 -04004489 if (rqstp->rq_arg.head[0].iov_len % 4) {
4490 /* client is nuts */
4491 dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
4492 __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
4493 return 0;
4494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 args->p = p;
4496 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
4497 args->pagelist = rqstp->rq_arg.pages;
4498 args->pagelen = rqstp->rq_arg.page_len;
Chuck Levereae03e22017-08-18 11:12:27 -04004499 args->tail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 args->tmpp = NULL;
4501 args->to_free = NULL;
4502 args->ops = args->iops;
4503 args->rqstp = rqstp;
4504
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04004505 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
4508int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02004509nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 /*
4512 * All that remains is to write the tag and operation count...
4513 */
Christoph Hellwig63f8de32017-05-08 19:42:02 +02004514 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05004515 struct xdr_buf *buf = resp->xdr.buf;
4516
4517 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
4518 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05004519
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004520 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 p = resp->tagp;
4523 *p++ = htonl(resp->taglen);
4524 memcpy(p, resp->tag, resp->taglen);
4525 p += XDR_QUADLEN(resp->taglen);
4526 *p++ = htonl(resp->opcnt);
4527
Trond Myklebustb6076642014-06-30 11:48:35 -04004528 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return 1;
4530}
4531
4532/*
4533 * Local variables:
4534 * c-basic-offset: 8
4535 * End:
4536 */